Search in sources :

Example 6 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 7 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 8 with AzureToolkitAuthenticationException

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

the class Account method selectSubscription.

public void selectSubscription(List<String> selectedSubscriptionIds) {
    requireAuthenticated();
    if (CollectionUtils.isEmpty(selectedSubscriptionIds)) {
        throw new IllegalArgumentException("You must select at least one subscription.");
    }
    if (CollectionUtils.isEmpty(getSubscriptions())) {
        throw new IllegalArgumentException("There are no subscriptions to select.");
    }
    if (entity.getSubscriptions().stream().anyMatch(s -> Utils.containsIgnoreCase(selectedSubscriptionIds, s.getId()))) {
        selectSubscriptionInner(this.getSubscriptions(), selectedSubscriptionIds);
        final AzureTaskManager manager = AzureTaskManager.getInstance();
        if (Objects.nonNull(manager)) {
            manager.runOnPooledThread(Preloader::load);
        }
    } else {
        throw new AzureToolkitAuthenticationException("no subscriptions are selected, " + "make sure you have provided valid subscription list");
    }
}
Also used : Preloader(com.microsoft.azure.toolkit.lib.common.cache.Preloader) AzureTaskManager(com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager) AzureToolkitAuthenticationException(com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException)

Example 9 with AzureToolkitAuthenticationException

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

the class AzureCliUtils method listSubscriptions.

@Nonnull
public static List<AzureCliSubscription> listSubscriptions() {
    final String jsonString = executeAzureCli("az account list --output json");
    final JsonArray result = JsonUtils.getGson().fromJson(jsonString, JsonArray.class);
    final List<AzureCliSubscription> list = new ArrayList<>();
    if (result != null) {
        result.forEach(j -> {
            JsonObject accountObject = j.getAsJsonObject();
            if (!accountObject.has("id")) {
                return;
            }
            // TODO: use utility to handle the json mapping
            String tenantId = accountObject.get("tenantId").getAsString();
            String subscriptionId = accountObject.get("id").getAsString();
            String subscriptionName = accountObject.get("name").getAsString();
            String state = accountObject.get("state").getAsString();
            String cloud = accountObject.get("cloudName").getAsString();
            String email = accountObject.get("user").getAsJsonObject().get("name").getAsString();
            if (StringUtils.equals(state, "Enabled") && StringUtils.isNoneBlank(subscriptionId, subscriptionName)) {
                AzureCliSubscription entity = new AzureCliSubscription();
                entity.setId(subscriptionId);
                entity.setName(subscriptionName);
                entity.setSelected(accountObject.get("isDefault").getAsBoolean());
                entity.setTenantId(tenantId);
                entity.setEmail(email);
                entity.setEnvironment(AzureEnvironmentUtils.stringToAzureEnvironment(cloud));
                list.add(entity);
            }
        });
        return list;
    }
    throw new AzureToolkitAuthenticationException("list subscriptions by command `az account list` failed, please make sure you have signed in Azure Cli using `az login`");
}
Also used : JsonArray(com.google.gson.JsonArray) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) AzureCliSubscription(com.microsoft.azure.toolkit.lib.auth.model.AzureCliSubscription) AzureToolkitAuthenticationException(com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException) Nonnull(javax.annotation.Nonnull)

Example 10 with AzureToolkitAuthenticationException

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

the class AzureCliUtils method ensureMinimumCliVersion.

public static void ensureMinimumCliVersion() {
    try {
        final JsonObject result = JsonUtils.getGson().fromJson(AzureCliUtils.executeAzureCli("az version --output json"), JsonObject.class);
        final String cliVersion = result.get("azure-cli").getAsString();
        // we require at least azure cli version 2.11.0
        if (compareWithMinimVersion(cliVersion) < 0) {
            throw new AzureToolkitAuthenticationException(String.format("your Azure Cli version '%s' is too old, " + "you need to upgrade your CLI with 'az upgrade'.", cliVersion));
        }
    } catch (NullPointerException | NumberFormatException ex) {
        throw new AzureToolkitAuthenticationException(String.format("Azure Cli is not ready, " + "please make sure your Azure Cli is installed and signed-in, the detailed error is : %s", ex.getMessage()));
    }
}
Also used : JsonObject(com.google.gson.JsonObject) 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