Search in sources :

Example 1 with Account

use of com.microsoft.azure.toolkit.lib.auth.Account in project azure-tools-for-java by Microsoft.

the class AzureExplorer method getTitle.

private String getTitle() {
    try {
        final AzureAccount az = Azure.az(AzureAccount.class);
        final Account account = az.account();
        final List<Subscription> subscriptions = account.getSelectedSubscriptions();
        if (subscriptions.size() == 1) {
            return String.format("Azure(%s)", subscriptions.get(0).getName());
        }
    } catch (final Exception ignored) {
    }
    return "Azure";
}
Also used : Account(com.microsoft.azure.toolkit.lib.auth.Account) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) Subscription(com.microsoft.azure.toolkit.lib.common.model.Subscription)

Example 2 with Account

use of com.microsoft.azure.toolkit.lib.auth.Account in project azure-tools-for-java by Microsoft.

the class IdentityAzureManager method restoreSignIn.

public Mono<AuthMethodDetails> restoreSignIn(AuthMethodDetails authMethodDetails) {
    if (authMethodDetails == null || authMethodDetails.getAuthMethod() == null || authMethodDetails.getAuthType() == null) {
        return Mono.just(new AuthMethodDetails());
    }
    if (StringUtils.isNotBlank(authMethodDetails.getAzureEnv())) {
        Azure.az(AzureCloud.class).setByName(authMethodDetails.getAzureEnv());
    }
    AuthType authType = authMethodDetails.getAuthType();
    try {
        if (authType == AuthType.SERVICE_PRINCIPAL) {
            AuthConfiguration auth = new AuthConfiguration();
            auth.setType(AuthType.SERVICE_PRINCIPAL);
            auth.setClient(authMethodDetails.getClientId());
            auth.setTenant(authMethodDetails.getTenantId());
            auth.setEnvironment(Azure.az(AzureCloud.class).get());
            if (StringUtils.isNotBlank(authMethodDetails.getCertificate())) {
                auth.setCertificate(authMethodDetails.getCertificate());
            } else {
                secureStore.migratePassword("account|" + auth.getClient(), null, SERVICE_PRINCIPAL_STORE_SERVICE, auth.getClient(), null);
                String key = secureStore == null ? null : secureStore.loadPassword(SERVICE_PRINCIPAL_STORE_SERVICE, authMethodDetails.getClientId(), null);
                if (StringUtils.isBlank(key)) {
                    throw new AzureToolkitRuntimeException(String.format("Cannot find SP security key for '%s' in intellij key pools.", authMethodDetails.getClientId()));
                }
                auth.setKey(key);
            }
            return signInServicePrincipal(auth).map(ac -> authMethodDetails);
        } else {
            if (StringUtils.isNotBlank(authMethodDetails.getClientId())) {
                AccountEntity entity = new AccountEntity();
                entity.setType(authType);
                entity.setEnvironment(Azure.az(AzureCloud.class).get());
                entity.setEmail(authMethodDetails.getAccountEmail());
                entity.setClientId(authMethodDetails.getClientId());
                entity.setTenantIds(StringUtils.isNotBlank(authMethodDetails.getTenantId()) ? Collections.singletonList(authMethodDetails.getTenantId()) : null);
                Account account = Azure.az(AzureAccount.class).account(entity);
                return Mono.just(fromAccountEntity(account.getEntity()));
            } else {
                throw new AzureToolkitRuntimeException("Cannot restore credentials due to version change.");
            }
        }
    } catch (Throwable e) {
        if (StringUtils.isNotBlank(authMethodDetails.getClientId()) && authMethodDetails.getAuthType() == AuthType.SERVICE_PRINCIPAL && secureStore != null) {
            secureStore.forgetPassword(SERVICE_PRINCIPAL_STORE_SERVICE, authMethodDetails.getClientId(), null);
        }
        return Mono.error(new AzureToolkitRuntimeException(String.format("Cannot restore credentials due to error: %s", e.getMessage())));
    }
}
Also used : Account(com.microsoft.azure.toolkit.lib.auth.Account) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) AzureCloud(com.microsoft.azure.toolkit.lib.auth.AzureCloud) AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) AuthType(com.microsoft.azure.toolkit.lib.auth.model.AuthType) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) AccountEntity(com.microsoft.azure.toolkit.lib.auth.model.AccountEntity)

Aggregations

Account (com.microsoft.azure.toolkit.lib.auth.Account)2 AzureAccount (com.microsoft.azure.toolkit.lib.auth.AzureAccount)2 AzureCloud (com.microsoft.azure.toolkit.lib.auth.AzureCloud)1 AccountEntity (com.microsoft.azure.toolkit.lib.auth.model.AccountEntity)1 AuthConfiguration (com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration)1 AuthType (com.microsoft.azure.toolkit.lib.auth.model.AuthType)1 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)1 Subscription (com.microsoft.azure.toolkit.lib.common.model.Subscription)1 AuthMethodDetails (com.microsoft.azuretools.authmanage.models.AuthMethodDetails)1