Search in sources :

Example 1 with InvalidConfigurationException

use of com.microsoft.azure.toolkit.lib.auth.exception.InvalidConfigurationException in project azure-maven-plugins by microsoft.

the class MavenAuthUtils method buildAuthConfiguration.

public static AuthConfiguration buildAuthConfiguration(MavenSession session, SettingsDecrypter settingsDecrypter, @Nonnull MavenAuthConfiguration auth) throws AzureExecutionException, MavenDecryptException {
    final String serverId = auth.getServerId();
    final AuthConfiguration authConfiguration;
    try {
        authConfiguration = convertToAuthConfiguration(StringUtils.isNotBlank(auth.getServerId()) ? buildAuthConfigurationByServerId(session, settingsDecrypter, serverId) : auth);
    } catch (InvalidConfigurationException ex) {
        final String messagePostfix = StringUtils.isNotBlank(serverId) ? ("in server: '" + serverId + "' at maven settings.xml.") : "in <auth> configuration.";
        throw new AzureExecutionException(String.format("%s %s", ex.getMessage(), messagePostfix));
    }
    return authConfiguration;
}
Also used : AzureExecutionException(com.microsoft.azure.toolkit.lib.common.exception.AzureExecutionException) MavenAuthConfiguration(com.microsoft.azure.maven.model.MavenAuthConfiguration) AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) InvalidConfigurationException(com.microsoft.azure.toolkit.lib.auth.exception.InvalidConfigurationException)

Example 2 with InvalidConfigurationException

use of com.microsoft.azure.toolkit.lib.auth.exception.InvalidConfigurationException in project azure-gradle-plugins by microsoft.

the class GradleAuthHelper method login.

public static String login(GradleAuthConfig auth, String subscriptionId) {
    try {
        Account account = login(toAuthConfiguration(ObjectUtils.firstNonNull(auth, new GradleAuthConfig())));
        final List<Subscription> subscriptions = account.getSubscriptions();
        final String targetSubscriptionId = getTargetSubscriptionId(subscriptionId, subscriptions, account.getSelectedSubscriptions());
        checkSubscription(subscriptions, targetSubscriptionId);
        Azure.az(AzureAccount.class).account().selectSubscription(Collections.singletonList(targetSubscriptionId));
        printCurrentSubscription(account);
        return targetSubscriptionId;
    } catch (InvalidConfigurationException e) {
        throw new AzureToolkitAuthenticationException("Failed to authenticate with Azure due to error: " + e.getMessage());
    }
}
Also used : Account(com.microsoft.azure.toolkit.lib.auth.Account) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) DeviceCodeAccount(com.microsoft.azure.toolkit.lib.auth.core.devicecode.DeviceCodeAccount) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) Subscription(com.microsoft.azure.toolkit.lib.common.model.Subscription) AzureToolkitAuthenticationException(com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException) InvalidConfigurationException(com.microsoft.azure.toolkit.lib.auth.exception.InvalidConfigurationException)

Example 3 with InvalidConfigurationException

use of com.microsoft.azure.toolkit.lib.auth.exception.InvalidConfigurationException in project azure-maven-plugins by microsoft.

the class MavenSettingHelper method buildAuthConfigurationByServerId.

/**
 * Build maven auth configuration from serverId.
 *
 * @param session           the maven session
 * @param settingsDecrypter the decrypter
 * @param serverId          the server id
 * @return the auth configuration
 * @throws MavenDecryptException when there are errors decrypting maven generated password
 * @throws InvalidConfigurationException where are any illegal configurations
 */
public static MavenAuthConfiguration buildAuthConfigurationByServerId(MavenSession session, SettingsDecrypter settingsDecrypter, String serverId) throws InvalidConfigurationException, MavenDecryptException {
    if (StringUtils.isBlank(serverId)) {
        throw new IllegalArgumentException("Parameter 'serverId' cannot be null or empty.");
    }
    final Server server = session.getSettings().getServer(serverId);
    if (server == null) {
        throw new InvalidConfigurationException(String.format("serverId '%s' cannot be found in maven settings.xml.", serverId));
    }
    final MavenAuthConfiguration configurationFromServer = new MavenAuthConfiguration();
    final Xpp3Dom configuration = (Xpp3Dom) server.getConfiguration();
    configurationFromServer.setServerId(serverId);
    if (configuration == null) {
        return configurationFromServer;
    }
    configurationFromServer.setTenant(getPropertyValue(configuration, "tenant"));
    configurationFromServer.setClient(getPropertyValue(configuration, "client"));
    final String rawKey = getPropertyValue(configuration, "key");
    configurationFromServer.setKey(isPropertyEncrypted(rawKey) ? decryptMavenSettingProperty(settingsDecrypter, "key", rawKey) : rawKey);
    configurationFromServer.setCertificate(getPropertyValue(configuration, "certificate"));
    final String rawCertificatePassword = getPropertyValue(configuration, "certificatePassword");
    configurationFromServer.setCertificatePassword(isPropertyEncrypted(rawCertificatePassword) ? decryptMavenSettingProperty(settingsDecrypter, "certificatePassword", rawCertificatePassword) : rawCertificatePassword);
    configurationFromServer.setEnvironment(getPropertyValue(configuration, "environment"));
    configurationFromServer.setServerId(serverId);
    return configurationFromServer;
}
Also used : MavenAuthConfiguration(com.microsoft.azure.maven.model.MavenAuthConfiguration) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Server(org.apache.maven.settings.Server) InvalidConfigurationException(com.microsoft.azure.toolkit.lib.auth.exception.InvalidConfigurationException)

Example 4 with InvalidConfigurationException

use of com.microsoft.azure.toolkit.lib.auth.exception.InvalidConfigurationException in project azure-maven-plugins by microsoft.

the class MavenAuthUtils method convertToAuthConfiguration.

private static AuthConfiguration convertToAuthConfiguration(MavenAuthConfiguration mavenAuthConfiguration) throws InvalidConfigurationException {
    if (Objects.isNull(mavenAuthConfiguration)) {
        return new AuthConfiguration();
    }
    final AuthConfiguration authConfiguration = new AuthConfiguration();
    authConfiguration.setClient(mavenAuthConfiguration.getClient());
    authConfiguration.setTenant(mavenAuthConfiguration.getTenant());
    authConfiguration.setCertificate(mavenAuthConfiguration.getCertificate());
    authConfiguration.setCertificatePassword(mavenAuthConfiguration.getCertificatePassword());
    authConfiguration.setKey(mavenAuthConfiguration.getKey());
    final String authTypeStr = mavenAuthConfiguration.getType();
    authConfiguration.setType(AuthType.parseAuthType(authTypeStr));
    authConfiguration.setEnvironment(AzureEnvironmentUtils.stringToAzureEnvironment(mavenAuthConfiguration.getEnvironment()));
    if (StringUtils.isNotBlank(mavenAuthConfiguration.getEnvironment()) && Objects.isNull(authConfiguration.getEnvironment())) {
        throw new InvalidConfigurationException(String.format(INVALID_AZURE_ENVIRONMENT, mavenAuthConfiguration.getEnvironment()));
    }
    // if user doesn't specify any authType
    if (StringUtils.isBlank(mavenAuthConfiguration.getType())) {
        if (!StringUtils.isAllBlank(mavenAuthConfiguration.getCertificate(), mavenAuthConfiguration.getKey(), mavenAuthConfiguration.getCertificatePassword())) {
            ValidationUtil.validateAuthConfiguration(authConfiguration);
        }
    } else if (authConfiguration.getType() == AuthType.SERVICE_PRINCIPAL) {
        ValidationUtil.validateAuthConfiguration(authConfiguration);
    }
    return authConfiguration;
}
Also used : MavenAuthConfiguration(com.microsoft.azure.maven.model.MavenAuthConfiguration) AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) InvalidConfigurationException(com.microsoft.azure.toolkit.lib.auth.exception.InvalidConfigurationException)

Example 5 with InvalidConfigurationException

use of com.microsoft.azure.toolkit.lib.auth.exception.InvalidConfigurationException in project azure-gradle-plugins by microsoft.

the class GradleAuthHelper method toAuthConfiguration.

private static AuthConfiguration toAuthConfiguration(GradleAuthConfig gradleAuthConfig) throws InvalidConfigurationException {
    final AuthConfiguration authConfiguration = new AuthConfiguration();
    authConfiguration.setClient(gradleAuthConfig.getClient());
    authConfiguration.setTenant(gradleAuthConfig.getTenant());
    authConfiguration.setCertificate(gradleAuthConfig.getCertificate());
    authConfiguration.setCertificatePassword(gradleAuthConfig.getCertificatePassword());
    authConfiguration.setKey(gradleAuthConfig.getKey());
    final String authTypeStr = gradleAuthConfig.getType();
    authConfiguration.setType(AuthType.parseAuthType(authTypeStr));
    authConfiguration.setEnvironment(AzureEnvironmentUtils.stringToAzureEnvironment(gradleAuthConfig.getEnvironment()));
    if (StringUtils.isNotBlank(gradleAuthConfig.getEnvironment()) && Objects.isNull(authConfiguration.getEnvironment())) {
        throw new InvalidConfigurationException(String.format(INVALID_AZURE_ENVIRONMENT, gradleAuthConfig.getEnvironment()));
    }
    return authConfiguration;
}
Also used : AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) InvalidConfigurationException(com.microsoft.azure.toolkit.lib.auth.exception.InvalidConfigurationException)

Aggregations

InvalidConfigurationException (com.microsoft.azure.toolkit.lib.auth.exception.InvalidConfigurationException)5 MavenAuthConfiguration (com.microsoft.azure.maven.model.MavenAuthConfiguration)3 AuthConfiguration (com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration)3 AzureString (com.microsoft.azure.toolkit.lib.common.bundle.AzureString)2 Account (com.microsoft.azure.toolkit.lib.auth.Account)1 AzureAccount (com.microsoft.azure.toolkit.lib.auth.AzureAccount)1 DeviceCodeAccount (com.microsoft.azure.toolkit.lib.auth.core.devicecode.DeviceCodeAccount)1 AzureToolkitAuthenticationException (com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException)1 AzureExecutionException (com.microsoft.azure.toolkit.lib.common.exception.AzureExecutionException)1 Subscription (com.microsoft.azure.toolkit.lib.common.model.Subscription)1 Server (org.apache.maven.settings.Server)1 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)1