Search in sources :

Example 6 with AzureAccount

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";
}
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 7 with AzureAccount

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;
    });
}
Also used : AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) HashMap(java.util.HashMap) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount)

Example 8 with AzureAccount

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());
                }));
            }
        });
    }
}
Also used : AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager) Azure(com.microsoft.azure.toolkit.lib.Azure) AzureExecutionException(com.microsoft.azure.toolkit.lib.common.exception.AzureExecutionException) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) StringUtils(org.apache.commons.lang3.StringUtils) IDeviceLoginUI(com.microsoft.azuretools.adauth.IDeviceLoginUI) AzureCloud(com.microsoft.azure.toolkit.lib.auth.AzureCloud) Single(rx.Single) PluginUtil(com.microsoft.azuretools.core.utils.PluginUtil) Duration(java.time.Duration) Map(java.util.Map) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) IdentityAzureManager(com.microsoft.azuretools.sdkmanage.IdentityAzureManager) AccountEntity(com.microsoft.azure.toolkit.lib.auth.model.AccountEntity) MsalClientException(com.microsoft.aad.msal4j.MsalClientException) CancellationException(java.util.concurrent.CancellationException) AuthMethod(com.microsoft.azuretools.authmanage.AuthMethod) ErrorType(com.microsoft.azuretools.telemetrywrapper.ErrorType) Operation(com.microsoft.azuretools.telemetrywrapper.Operation) AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) AzureTaskContext(com.microsoft.azure.toolkit.lib.common.task.AzureTaskContext) Window(org.eclipse.jface.window.Window) List(java.util.List) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) DeviceCodeAccount(com.microsoft.azure.toolkit.lib.auth.core.devicecode.DeviceCodeAccount) SWT(org.eclipse.swt.SWT) DeviceLoginWindow(com.microsoft.azuretools.core.ui.DeviceLoginWindow) EventUtil(com.microsoft.azuretools.telemetrywrapper.EventUtil) Lombok(lombok.Lombok) AzureTaskManager(com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager) SignInDialog(com.microsoft.azuretools.core.ui.SignInDialog) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) AzureAbstractHandler(com.microsoft.azuretools.core.utils.AzureAbstractHandler) AzureEnvironmentUtils(com.microsoft.azure.toolkit.lib.auth.util.AzureEnvironmentUtils) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) Exceptions(rx.exceptions.Exceptions) AzureOperationBundle(com.microsoft.azure.toolkit.lib.common.operation.AzureOperationBundle) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) CollectionUtils(org.apache.commons.collections4.CollectionUtils) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask) HandlerUtil(org.eclipse.ui.handlers.HandlerUtil) ServicePrincipalLoginDialog(com.microsoft.azuretools.core.ui.ServicePrincipalLoginDialog) ErrorWindow(com.microsoft.azuretools.core.ui.ErrorWindow) Schedulers(reactor.core.scheduler.Schedulers) AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) Subscription(com.microsoft.azure.toolkit.lib.common.model.Subscription) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) AzureMessager(com.microsoft.azure.toolkit.lib.common.messager.AzureMessager) Shell(org.eclipse.swt.widgets.Shell) Mono(reactor.core.publisher.Mono) ExecutionException(org.eclipse.core.commands.ExecutionException) Flux(reactor.core.publisher.Flux) TelemetryConstants(com.microsoft.azuretools.telemetry.TelemetryConstants) TelemetryManager(com.microsoft.azuretools.telemetrywrapper.TelemetryManager) MessageBox(org.eclipse.swt.widgets.MessageBox) AuthType(com.microsoft.azure.toolkit.lib.auth.model.AuthType) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager)

Example 9 with AzureAccount

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();
}
Also used : 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)9 AzureString (com.microsoft.azure.toolkit.lib.common.bundle.AzureString)5 HashMap (java.util.HashMap)5 DeviceCodeAccount (com.microsoft.azure.toolkit.lib.auth.core.devicecode.DeviceCodeAccount)4 AzureTask (com.microsoft.azure.toolkit.lib.common.task.AzureTask)4 AccountEntity (com.microsoft.azure.toolkit.lib.auth.model.AccountEntity)3 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)3 AuthMethodDetails (com.microsoft.azuretools.authmanage.models.AuthMethodDetails)3 SubscriptionDetail (com.microsoft.azuretools.authmanage.models.SubscriptionDetail)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 MsalClientException (com.microsoft.aad.msal4j.MsalClientException)2 Azure (com.microsoft.azure.toolkit.lib.Azure)2 AzureCloud (com.microsoft.azure.toolkit.lib.auth.AzureCloud)2 AuthConfiguration (com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration)2 AuthType (com.microsoft.azure.toolkit.lib.auth.model.AuthType)2 AzureEnvironmentUtils (com.microsoft.azure.toolkit.lib.auth.util.AzureEnvironmentUtils)2 AzureMessager (com.microsoft.azure.toolkit.lib.common.messager.AzureMessager)2 Subscription (com.microsoft.azure.toolkit.lib.common.model.Subscription)2 AzureOperation (com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)2 AzureOperationBundle (com.microsoft.azure.toolkit.lib.common.operation.AzureOperationBundle)2