add exception classes and AzureCredentialWrapper (#1206)

* add exception classes and AzureCredentialWrapper

Co-authored-by: Andy Xu(devdiv) <andxu@microsoft>
This commit is contained in:
Andy Xu(devdiv) 2021-01-05 16:26:14 +08:00 коммит произвёл GitHub
Родитель ebe0d610d5
Коммит 5d8e6b9d5b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 291 добавлений и 17 удалений

Просмотреть файл

@ -10,8 +10,48 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>azure-toolkit-auth-lib</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings-builder</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model-builder</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
@ -54,6 +94,11 @@
<artifactId>azure-identity</artifactId>
</dependency>
<dependency>
<groupId>me.alexpanov</groupId>
<artifactId>free-port-finder</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-toolkit-common-lib</artifactId>

Просмотреть файл

@ -0,0 +1,13 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
package com.microsoft.azure.tools.auth.exception;
public class AzureLoginException extends Exception {
public AzureLoginException(String message) {
super(message);
}
}

Просмотреть файл

@ -0,0 +1,15 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
package com.microsoft.azure.tools.auth.exception;
public class DesktopNotSupportedException extends AzureLoginException {
private static final long serialVersionUID = 6774808712719406687L;
public DesktopNotSupportedException(String message) {
super(message);
}
}

Просмотреть файл

@ -0,0 +1,13 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
package com.microsoft.azure.tools.auth.exception;
public class InvalidConfigurationException extends AzureLoginException {
public InvalidConfigurationException(String message) {
super(message);
}
}

Просмотреть файл

@ -0,0 +1,16 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
package com.microsoft.azure.tools.auth.exception;
public class LoginFailureException extends AzureLoginException {
private static final long serialVersionUID = -94207672112213624L;
public LoginFailureException(String message) {
super(message);
}
}

Просмотреть файл

@ -0,0 +1,33 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
package com.microsoft.azure.tools.auth.exception;
public class MavenDecryptException extends AzureLoginException {
private static final long serialVersionUID = 5207024853556212112L;
private String propertyName;
private String propertyValue;
public MavenDecryptException(String propertyName, String propertyValue, String message) {
super(message);
this.propertyName = propertyName;
this.propertyValue = propertyValue;
}
/**
* @return the property name
*/
public String getPropertyName() {
return propertyName;
}
/**
* @return the propertyValue
*/
public String getPropertyValue() {
return propertyValue;
}
}

Просмотреть файл

@ -20,4 +20,4 @@ public enum AuthMethod {
MANAGED_IDENTITY,
MAVEN_CONFIGURATION,
MAVEN_SETTINGS
}
}

Просмотреть файл

@ -8,6 +8,7 @@ package com.microsoft.azure.tools.auth.model;
import java.util.Arrays;
import com.microsoft.azure.tools.auth.exception.InvalidConfigurationException;
import org.apache.commons.lang3.StringUtils;
/**
@ -15,37 +16,45 @@ import org.apache.commons.lang3.StringUtils;
*/
public enum AuthType {
AUTO,
AZURE_CLI,
SERVICE_PRINCIPAL,
AZURE_AUTH_MAVEN_PLUGIN,
MANAGED_IDENTITY,
AZURE_CLI,
VSCODE,
INTELLIJ_IDEA,
VISUAL_STUDIO,
DEVICE_CODE,
OAUTH2;
public static AuthType parseAuthType(String type) {
public static AuthType parseAuthType(String type) throws InvalidConfigurationException {
if (StringUtils.isBlank(type)) {
return AUTO;
}
switch (type.toLowerCase().trim()) {
case "azure_cli":
return AZURE_CLI;
case "intellij":
return INTELLIJ_IDEA;
case "vscode":
return VSCODE;
case "device_code":
return DEVICE_CODE;
case "auto":
return AUTO;
case "service_principal":
return SERVICE_PRINCIPAL;
case "managed_identity":
return MANAGED_IDENTITY;
case "azure_cli":
return AZURE_CLI;
case "vscode":
return VSCODE;
case "intellij":
return INTELLIJ_IDEA;
case "azure_auth_maven_plugin":
return AZURE_AUTH_MAVEN_PLUGIN;
case "device_code":
return DEVICE_CODE;
case "oauth2":
return OAUTH2;
case "visual_studio":
return VISUAL_STUDIO;
case "auto":
return AUTO;
default:
throw new InvalidConfigurationException(String.format("Invalid auth type '%s', supported values are: %s.", type,
StringUtils.join(Arrays.stream(values()).map(t -> StringUtils.lowerCase(t.toString())), ",")));
}
throw new UnsupportedOperationException(String.format("Invalid auth type '%s', supported values are: %s.", type,
StringUtils.join(Arrays.stream(values()).map(t -> StringUtils.lowerCase(t.toString())), ",")));
}
}

Просмотреть файл

@ -0,0 +1,71 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
package com.microsoft.azure.tools.auth.model;
import com.azure.core.credential.TokenCredential;
import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.credentials.AzureTokenCredentials;
import com.microsoft.azure.tools.auth.AzureIdentityCredentialTokenCredentials;
import com.microsoft.azure.tools.common.util.TextUtils;
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
public class AzureCredentialWrapper {
@Getter
private AuthMethod authMethod;
@Getter
private TokenCredential tokenCredential;
@Getter
private AzureEnvironment env;
@Getter
@Setter
private String defaultSubscriptionId;
@Getter
@Setter
private String tenantId;
public AzureCredentialWrapper(AuthMethod method, TokenCredential tokenCredential, AzureEnvironment env) {
Objects.requireNonNull(method, "authMethod is null");
Objects.requireNonNull(tokenCredential, "tokenCredential is null");
Objects.requireNonNull(tokenCredential, "env is null");
this.authMethod = method;
this.tokenCredential = tokenCredential;
this.env = env;
}
public String getCredentialDescription() {
List<String> details = new ArrayList<>();
details.add(String.format("auth type: %s", TextUtils.green(authMethod.toString())));
if (StringUtils.isNotBlank(tenantId)) {
details.add(String.format("tenantId: %s", TextUtils.green(tenantId)));
}
if (StringUtils.isNotBlank(defaultSubscriptionId)) {
details.add(String.format("default subscription: %s", TextUtils.green(defaultSubscriptionId)));
}
return StringUtils.join(details.toArray(), "\n");
}
/**
* Get <class>AzureTokenCredentials</class> instance from track2 credential
* @return AzureTokenCredentials
*/
public AzureTokenCredentials getAzureTokenCredentials() {
return new AzureIdentityCredentialTokenCredentials(env, tenantId, tokenCredential);
}
}

Просмотреть файл

@ -94,10 +94,63 @@
<azure-identity.version>1.2.1</azure-identity.version>
<azure.version>1.36.3</azure.version>
<lombok.version>1.18.8</lombok.version>
<free.port.finder.version>1.0</free.port.finder.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven.version}</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>${maven.plugin-tools.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings-builder</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model-builder</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>${codehaus.plexus-utils.version}</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-tools-common</artifactId>
@ -188,12 +241,18 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.alexpanov</groupId>
<artifactId>free-port-finder</artifactId>
<version>${free.port.finder.version}</version>
</dependency>
<!-- azure sdk -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-client-runtime</artifactId>
<version>{azure.client.version}</version>
<version>${azure.client.version}</version>
</dependency>
<dependency>
<groupId>com.azure.resourcemanager</groupId>