use of com.microsoft.azure.toolkit.lib.auth.AzureAccount 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.AzureAccount in project azure-tools-for-java by Microsoft.
the class AuthMethodManager method initAuthMethodManagerFromSettings.
private void initAuthMethodManagerFromSettings() {
EventUtil.executeWithLog(ACCOUNT, RESIGNIN, operation -> {
try {
AuthMethodDetails targetAuthMethodDetails = loadSettings();
if (targetAuthMethodDetails == null || targetAuthMethodDetails.getAuthMethod() == null) {
targetAuthMethodDetails = new AuthMethodDetails();
targetAuthMethodDetails.setAuthMethod(AuthMethod.IDENTITY);
} else {
// convert old auth method to new ones
switch(targetAuthMethodDetails.getAuthMethod()) {
case AZ:
{
targetAuthMethodDetails.setAuthType(AuthType.AZURE_CLI);
break;
}
case DC:
{
targetAuthMethodDetails.setAuthType(AuthType.DEVICE_CODE);
break;
}
case AD:
// we don't support it now
LOGGER.warn("The AD auth method is not supported now, ignore the credential.");
break;
case SP:
targetAuthMethodDetails.setAuthType(AuthType.SERVICE_PRINCIPAL);
break;
default:
break;
}
targetAuthMethodDetails.setAuthMethod(AuthMethod.IDENTITY);
}
authMethodDetails = this.identityAzureManager.restoreSignIn(targetAuthMethodDetails).block();
List<SubscriptionDetail> persistSubscriptions = identityAzureManager.getSubscriptionManager().loadSubscriptions();
if (CollectionUtils.isNotEmpty(persistSubscriptions)) {
List<String> savedSubscriptionList = persistSubscriptions.stream().filter(SubscriptionDetail::isSelected).map(SubscriptionDetail::getSubscriptionId).distinct().collect(Collectors.toList());
identityAzureManager.selectSubscriptionByIds(savedSubscriptionList);
}
initFuture.complete(true);
// pre-load regions
AzureAccount az = com.microsoft.azure.toolkit.lib.Azure.az(AzureAccount.class);
Optional.of(identityAzureManager.getSelectedSubscriptionIds()).ifPresent(sids -> sids.stream().limit(5).forEach(az::listRegions));
final String authMethod = authMethodDetails.getAuthMethod() == null ? "Empty" : authMethodDetails.getAuthMethod().name();
final Map<String, String> telemetryProperties = new HashMap<String, String>() {
{
put(SIGNIN_METHOD, authMethod);
put(AZURE_ENVIRONMENT, CommonSettings.getEnvironment().getName());
}
};
EventUtil.logEvent(EventType.info, operation, telemetryProperties);
notifySignInEventListener();
} catch (RuntimeException exception) {
initFuture.complete(true);
EventUtil.logError(operation, ErrorType.systemError, exception, null, null);
this.authMethodDetails = new AuthMethodDetails();
this.authMethodDetails.setAuthMethod(AuthMethod.IDENTITY);
notifySignOutEventListener();
}
return this;
});
}
use of com.microsoft.azure.toolkit.lib.auth.AzureAccount in project azure-tools-for-java by Microsoft.
the class SignInCommandHandler method onAzureSignIn.
private void onAzureSignIn(Shell shell) {
final AuthMethodManager authMethodManager = AuthMethodManager.getInstance();
boolean isSignIn = authMethodManager.isSignedIn();
if (isSignIn) {
boolean res = showYesNoDialog(shell, "Azure Sign Out", SignOutCommandHandler.getSignOutWarningMessage(authMethodManager));
if (res) {
EventUtil.executeWithLog(ACCOUNT, SIGNOUT, (operation) -> {
authMethodManager.signOut();
});
}
} else {
signInIfNotSignedIn(shell).subscribe(isLoggedIn -> {
if (isLoggedIn) {
AzureAccount az = Azure.az(AzureAccount.class);
AzureTaskManager.getInstance().runOnPooledThread(() -> authMethodManager.getAzureManager().getSelectedSubscriptions().stream().limit(5).forEach(s -> {
// pre-load regions;
az.listRegions(s.getId());
}));
}
});
}
}
use of com.microsoft.azure.toolkit.lib.auth.AzureAccount 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();
}
Aggregations