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";
}
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())));
}
}
Aggregations