use of com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException in project azure-gradle-plugins by microsoft.
the class GradleAuthHelper method accountLogin.
private static Account accountLogin(AuthConfiguration auth) {
if (auth.getEnvironment() != null) {
Azure.az(AzureCloud.class).set(auth.getEnvironment());
}
if (auth.getType() == null || auth.getType() == AuthType.AUTO) {
if (StringUtils.isAllBlank(auth.getCertificate(), auth.getCertificatePassword(), auth.getKey())) {
final Account account = findFirstAvailableAccount().block();
if (account == null) {
throw new AzureToolkitAuthenticationException("There are no accounts available.");
}
promptForOAuthOrDeviceCodeLogin(account.getAuthType());
return handleDeviceCodeAccount(Azure.az(AzureAccount.class).loginAsync(account, false).block());
} else {
return doServicePrincipalLogin(auth);
}
} else {
promptForOAuthOrDeviceCodeLogin(auth.getType());
return handleDeviceCodeAccount(Azure.az(AzureAccount.class).loginAsync(auth, false).block());
}
}
use of com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException in project azure-gradle-plugins by microsoft.
the class GradleAuthHelper method login.
public static String login(GradleAuthConfig auth, String subscriptionId) {
try {
Account account = login(toAuthConfiguration(ObjectUtils.firstNonNull(auth, new GradleAuthConfig())));
final List<Subscription> subscriptions = account.getSubscriptions();
final String targetSubscriptionId = getTargetSubscriptionId(subscriptionId, subscriptions, account.getSelectedSubscriptions());
checkSubscription(subscriptions, targetSubscriptionId);
Azure.az(AzureAccount.class).account().selectSubscription(Collections.singletonList(targetSubscriptionId));
printCurrentSubscription(account);
return targetSubscriptionId;
} catch (InvalidConfigurationException e) {
throw new AzureToolkitAuthenticationException("Failed to authenticate with Azure due to error: " + e.getMessage());
}
}
use of com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException in project azure-maven-plugins by microsoft.
the class Account method selectSubscription.
public void selectSubscription(List<String> selectedSubscriptionIds) {
requireAuthenticated();
if (CollectionUtils.isEmpty(selectedSubscriptionIds)) {
throw new IllegalArgumentException("You must select at least one subscription.");
}
if (CollectionUtils.isEmpty(getSubscriptions())) {
throw new IllegalArgumentException("There are no subscriptions to select.");
}
if (entity.getSubscriptions().stream().anyMatch(s -> Utils.containsIgnoreCase(selectedSubscriptionIds, s.getId()))) {
selectSubscriptionInner(this.getSubscriptions(), selectedSubscriptionIds);
final AzureTaskManager manager = AzureTaskManager.getInstance();
if (Objects.nonNull(manager)) {
manager.runOnPooledThread(Preloader::load);
}
} else {
throw new AzureToolkitAuthenticationException("no subscriptions are selected, " + "make sure you have provided valid subscription list");
}
}
use of com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException in project azure-maven-plugins by microsoft.
the class AzureCliUtils method listSubscriptions.
@Nonnull
public static List<AzureCliSubscription> listSubscriptions() {
final String jsonString = executeAzureCli("az account list --output json");
final JsonArray result = JsonUtils.getGson().fromJson(jsonString, JsonArray.class);
final List<AzureCliSubscription> list = new ArrayList<>();
if (result != null) {
result.forEach(j -> {
JsonObject accountObject = j.getAsJsonObject();
if (!accountObject.has("id")) {
return;
}
// TODO: use utility to handle the json mapping
String tenantId = accountObject.get("tenantId").getAsString();
String subscriptionId = accountObject.get("id").getAsString();
String subscriptionName = accountObject.get("name").getAsString();
String state = accountObject.get("state").getAsString();
String cloud = accountObject.get("cloudName").getAsString();
String email = accountObject.get("user").getAsJsonObject().get("name").getAsString();
if (StringUtils.equals(state, "Enabled") && StringUtils.isNoneBlank(subscriptionId, subscriptionName)) {
AzureCliSubscription entity = new AzureCliSubscription();
entity.setId(subscriptionId);
entity.setName(subscriptionName);
entity.setSelected(accountObject.get("isDefault").getAsBoolean());
entity.setTenantId(tenantId);
entity.setEmail(email);
entity.setEnvironment(AzureEnvironmentUtils.stringToAzureEnvironment(cloud));
list.add(entity);
}
});
return list;
}
throw new AzureToolkitAuthenticationException("list subscriptions by command `az account list` failed, please make sure you have signed in Azure Cli using `az login`");
}
use of com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException in project azure-maven-plugins by microsoft.
the class AzureCliUtils method ensureMinimumCliVersion.
public static void ensureMinimumCliVersion() {
try {
final JsonObject result = JsonUtils.getGson().fromJson(AzureCliUtils.executeAzureCli("az version --output json"), JsonObject.class);
final String cliVersion = result.get("azure-cli").getAsString();
// we require at least azure cli version 2.11.0
if (compareWithMinimVersion(cliVersion) < 0) {
throw new AzureToolkitAuthenticationException(String.format("your Azure Cli version '%s' is too old, " + "you need to upgrade your CLI with 'az upgrade'.", cliVersion));
}
} catch (NullPointerException | NumberFormatException ex) {
throw new AzureToolkitAuthenticationException(String.format("Azure Cli is not ready, " + "please make sure your Azure Cli is installed and signed-in, the detailed error is : %s", ex.getMessage()));
}
}
Aggregations