Search in sources :

Example 1 with AccountEntity

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

the class IdentityAzureManager method drop.

@Override
public void drop() {
    if (!isSignedIn()) {
        return;
    }
    final AzureAccount az = Azure.az(AzureAccount.class);
    final AccountEntity account = az.account().getEntity();
    if (StringUtils.isNotBlank(account.getClientId()) && account.getType() == AuthType.SERVICE_PRINCIPAL && secureStore != null) {
        secureStore.forgetPassword(SERVICE_PRINCIPAL_STORE_SERVICE, account.getClientId(), null);
    }
    az.logout();
    super.drop();
}
Also used : AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) AccountEntity(com.microsoft.azure.toolkit.lib.auth.model.AccountEntity)

Example 2 with AccountEntity

use of com.microsoft.azure.toolkit.lib.auth.model.AccountEntity 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

AzureAccount (com.microsoft.azure.toolkit.lib.auth.AzureAccount)2 AccountEntity (com.microsoft.azure.toolkit.lib.auth.model.AccountEntity)2 Account (com.microsoft.azure.toolkit.lib.auth.Account)1 AzureCloud (com.microsoft.azure.toolkit.lib.auth.AzureCloud)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 AuthMethodDetails (com.microsoft.azuretools.authmanage.models.AuthMethodDetails)1