Search in sources :

Example 1 with AzureToolkitAuthenticationException

use of com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException 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 2 with AzureToolkitAuthenticationException

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

the class GradleAuthHelper method accountLogin.

private static Account accountLogin(AuthConfiguration auth) {
    if (auth.getEnvironment() != null) {
        Azure.az(AzureCloud.class).set(auth.getEnvironment());
    }
    if (auth.getType() == null || auth.getType() == AuthType.AUTO) {
        if (StringUtils.isAllBlank(auth.getCertificate(), auth.getCertificatePassword(), auth.getKey())) {
            final Account account = findFirstAvailableAccount().block();
            if (account == null) {
                throw new AzureToolkitAuthenticationException("There are no accounts available.");
            }
            promptForOAuthOrDeviceCodeLogin(account.getAuthType());
            return handleDeviceCodeAccount(Azure.az(AzureAccount.class).loginAsync(account, false).block());
        } else {
            return doServicePrincipalLogin(auth);
        }
    } else {
        promptForOAuthOrDeviceCodeLogin(auth.getType());
        return handleDeviceCodeAccount(Azure.az(AzureAccount.class).loginAsync(auth, false).block());
    }
}
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) AzureCloud(com.microsoft.azure.toolkit.lib.auth.AzureCloud) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) AzureToolkitAuthenticationException(com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException)

Example 3 with AzureToolkitAuthenticationException

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

the class AzureAccount method restoreLogin.

private Mono<Account> restoreLogin(@Nonnull AccountEntity accountEntity) {
    Preconditions.checkNotNull(accountEntity.getEnvironment(), "Azure environment for account entity is required.");
    Preconditions.checkNotNull(accountEntity.getType(), "Auth type for account entity is required.");
    Account target;
    if (Arrays.asList(AuthType.DEVICE_CODE, AuthType.OAUTH2).contains(accountEntity.getType())) {
        AzureEnvironmentUtils.setupAzureEnvironment(accountEntity.getEnvironment());
        SharedTokenCacheCredentialBuilder builder = new SharedTokenCacheCredentialBuilder();
        SharedTokenCacheCredential credential = builder.tokenCachePersistenceOptions(new TokenCachePersistenceOptions().setName(Account.TOOLKIT_TOKEN_CACHE_NAME)).username(accountEntity.getEmail()).tenantId(accountEntity.getTenantIds() == null ? "organizations" : accountEntity.getTenantIds().get(0)).clientId(accountEntity.getClientId()).build();
        target = new SimpleAccount(accountEntity, credential);
    } else if (Arrays.asList(AuthType.VSCODE, AuthType.AZURE_CLI).contains(accountEntity.getType())) {
        target = buildAccountMap().get(accountEntity.getType()).get();
    } else {
        return Mono.error(new AzureToolkitAuthenticationException(String.format("login for auth type '%s' cannot be restored.", accountEntity.getType())));
    }
    return target.login().map(ac -> {
        if (ac.getEnvironment() != accountEntity.getEnvironment()) {
            throw new AzureToolkitAuthenticationException(String.format("you have changed the azure cloud to '%s' for auth type: '%s' since last time you signed in.", AzureEnvironmentUtils.getCloudNameForAzureCli(ac.getEnvironment()), accountEntity.getType()));
        }
        if (!StringUtils.equalsIgnoreCase(ac.entity.getEmail(), accountEntity.getEmail())) {
            throw new AzureToolkitAuthenticationException(String.format("you have changed the account from '%s' to '%s' since last time you signed in.", accountEntity.getEmail(), ac.entity.getEmail()));
        }
        return ac;
    }).doOnSuccess(this::setAccount);
}
Also used : SharedTokenCacheCredential(com.azure.identity.SharedTokenCacheCredential) Azure(com.microsoft.azure.toolkit.lib.Azure) Setter(lombok.Setter) Arrays(java.util.Arrays) AzureEnvironmentUtils(com.microsoft.azure.toolkit.lib.auth.util.AzureEnvironmentUtils) IAzureAccount(com.microsoft.azure.toolkit.lib.account.IAzureAccount) Subscription(com.azure.resourcemanager.resources.models.Subscription) StringUtils(org.apache.commons.lang3.StringUtils) Supplier(java.util.function.Supplier) Region(com.microsoft.azure.toolkit.lib.common.model.Region) LinkedHashMap(java.util.LinkedHashMap) AzureCliAccount(com.microsoft.azure.toolkit.lib.auth.core.azurecli.AzureCliAccount) AzureEnvironment(com.azure.core.management.AzureEnvironment) AccessLevel(lombok.AccessLevel) Utils(com.microsoft.azure.toolkit.lib.common.utils.Utils) ServicePrincipalAccount(com.microsoft.azure.toolkit.lib.auth.core.serviceprincipal.ServicePrincipalAccount) Map(java.util.Map) SharedTokenCacheCredentialBuilder(com.azure.identity.SharedTokenCacheCredentialBuilder) Cacheable(com.microsoft.azure.toolkit.lib.common.cache.Cacheable) AccountEntity(com.microsoft.azure.toolkit.lib.auth.model.AccountEntity) Location(com.azure.resourcemanager.resources.models.Location) Nonnull(javax.annotation.Nonnull) LoginFailureException(com.microsoft.azure.toolkit.lib.auth.exception.LoginFailureException) Collection(java.util.Collection) Mono(reactor.core.publisher.Mono) AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) Collectors(java.util.stream.Collectors) RegionType(com.azure.resourcemanager.resources.models.RegionType) OAuthAccount(com.microsoft.azure.toolkit.lib.auth.core.oauth.OAuthAccount) Objects(java.util.Objects) Flux(reactor.core.publisher.Flux) AzureToolkitAuthenticationException(com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException) List(java.util.List) DeviceCodeAccount(com.microsoft.azure.toolkit.lib.auth.core.devicecode.DeviceCodeAccount) TokenCachePersistenceOptions(com.azure.identity.TokenCachePersistenceOptions) TokenCredential(com.azure.core.credential.TokenCredential) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) AuthType(com.microsoft.azure.toolkit.lib.auth.model.AuthType) IAzureAccount(com.microsoft.azure.toolkit.lib.account.IAzureAccount) AzureCliAccount(com.microsoft.azure.toolkit.lib.auth.core.azurecli.AzureCliAccount) ServicePrincipalAccount(com.microsoft.azure.toolkit.lib.auth.core.serviceprincipal.ServicePrincipalAccount) OAuthAccount(com.microsoft.azure.toolkit.lib.auth.core.oauth.OAuthAccount) DeviceCodeAccount(com.microsoft.azure.toolkit.lib.auth.core.devicecode.DeviceCodeAccount) TokenCachePersistenceOptions(com.azure.identity.TokenCachePersistenceOptions) SharedTokenCacheCredentialBuilder(com.azure.identity.SharedTokenCacheCredentialBuilder) SharedTokenCacheCredential(com.azure.identity.SharedTokenCacheCredential) AzureToolkitAuthenticationException(com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException)

Example 4 with AzureToolkitAuthenticationException

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

the class RefreshTokenTokenCredentialManager method getRefreshTokenFromMsalToken.

private static String getRefreshTokenFromMsalToken(MsalToken accessToken) {
    IAuthenticationResult result = accessToken.getAuthenticationResult();
    if (result == null) {
        return null;
    }
    String refreshTokenFromResult;
    try {
        refreshTokenFromResult = (String) FieldUtils.readField(result, "refreshToken", true);
    } catch (IllegalAccessException e) {
        throw new AzureToolkitAuthenticationException("cannot read refreshToken from IAuthenticationResult.");
    }
    return refreshTokenFromResult;
}
Also used : IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) AzureToolkitAuthenticationException(com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException)

Example 5 with AzureToolkitAuthenticationException

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

the class AzureCliAccount method preLoginCheck.

protected Mono<Boolean> preLoginCheck() {
    return Mono.fromCallable(() -> {
        AzureCliUtils.ensureMinimumCliVersion();
        AzureCliUtils.executeAzureCli("az account get-access-token --output json");
        List<AzureCliSubscription> subscriptions = AzureCliUtils.listSubscriptions();
        if (subscriptions.isEmpty()) {
            throw new AzureToolkitAuthenticationException("Cannot find any subscriptions in current account.");
        }
        AzureCliSubscription defaultSubscription = subscriptions.stream().filter(AzureCliSubscription::isSelected).findFirst().orElse(subscriptions.get(0));
        AzureEnvironment configEnv = Azure.az(AzureCloud.class).get();
        if (configEnv != null && defaultSubscription.getEnvironment() != configEnv) {
            throw new AzureToolkitAuthenticationException(String.format("The azure cloud from azure cli '%s' doesn't match with your auth configuration, " + "you can change it by executing 'az cloud set --name=%s' command to change the cloud in azure cli.", AzureEnvironmentUtils.getCloudNameForAzureCli(defaultSubscription.getEnvironment()), AzureEnvironmentUtils.getCloudNameForAzureCli(configEnv)));
        }
        this.entity.setEnvironment(defaultSubscription.getEnvironment());
        this.entity.setEmail(defaultSubscription.getEmail());
        subscriptions = subscriptions.stream().filter(s -> StringUtils.equals(this.entity.getEmail(), s.getEmail())).collect(Collectors.toList());
        // use the tenant who has one or more subscriptions
        this.entity.setTenantIds(subscriptions.stream().map(Subscription::getTenantId).distinct().collect(Collectors.toList()));
        this.entity.setSubscriptions(subscriptions.stream().filter(distinctByKey(t -> StringUtils.lowerCase(t.getId()))).map(AzureCliAccount::toSubscription).collect(Collectors.toList()));
        // set initial selection of subscriptions
        this.entity.setSelectedSubscriptionIds(subscriptions.stream().filter(Subscription::isSelected).map(Subscription::getId).distinct().collect(Collectors.toList()));
        return true;
    });
}
Also used : AzureEnvironment(com.azure.core.management.AzureEnvironment) AzureCloud(com.microsoft.azure.toolkit.lib.auth.AzureCloud) AzureCliSubscription(com.microsoft.azure.toolkit.lib.auth.model.AzureCliSubscription) AzureCliSubscription(com.microsoft.azure.toolkit.lib.auth.model.AzureCliSubscription) Subscription(com.microsoft.azure.toolkit.lib.common.model.Subscription) AzureToolkitAuthenticationException(com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException)

Aggregations

AzureToolkitAuthenticationException (com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException)13 Nonnull (javax.annotation.Nonnull)6 DeviceCodeAccount (com.microsoft.azure.toolkit.lib.auth.core.devicecode.DeviceCodeAccount)5 Subscription (com.microsoft.azure.toolkit.lib.common.model.Subscription)5 AzureEnvironment (com.azure.core.management.AzureEnvironment)4 Azure (com.microsoft.azure.toolkit.lib.Azure)4 Account (com.microsoft.azure.toolkit.lib.auth.Account)4 AzureAccount (com.microsoft.azure.toolkit.lib.auth.AzureAccount)4 AzureCloud (com.microsoft.azure.toolkit.lib.auth.AzureCloud)4 AuthType (com.microsoft.azure.toolkit.lib.auth.model.AuthType)3 AzureEnvironmentUtils (com.microsoft.azure.toolkit.lib.auth.util.AzureEnvironmentUtils)3 AzureMessager (com.microsoft.azure.toolkit.lib.common.messager.AzureMessager)3 IOException (java.io.IOException)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 StringUtils (org.apache.commons.lang3.StringUtils)3 DeviceCodeInfo (com.azure.identity.DeviceCodeInfo)2 TokenCachePersistenceOptions (com.azure.identity.TokenCachePersistenceOptions)2