Skip to content

Commit

Permalink
Identity credential updates (#10769)
Browse files Browse the repository at this point in the history
  • Loading branch information
g2vinay authored May 6, 2020
1 parent 2c53711 commit 1bfd8f7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class EnvironmentCredential implements TokenCredential {
private final Configuration configuration;
private final IdentityClientOptions identityClientOptions;
private final ClientLogger logger = new ClientLogger(EnvironmentCredential.class);
private final TokenCredential tokenCredential;

/**
* Creates an instance of the default environment credential provider.
Expand All @@ -47,36 +48,39 @@ public class EnvironmentCredential implements TokenCredential {
EnvironmentCredential(IdentityClientOptions identityClientOptions) {
this.configuration = Configuration.getGlobalConfiguration().clone();
this.identityClientOptions = identityClientOptions;
}
TokenCredential targetCredential = null;

@Override
public Mono<AccessToken> getToken(TokenRequestContext request) {
return Mono.fromSupplier(() -> {
String clientId = configuration.get(Configuration.PROPERTY_AZURE_CLIENT_ID);
String tenantId = configuration.get(Configuration.PROPERTY_AZURE_TENANT_ID);
String clientSecret = configuration.get(Configuration.PROPERTY_AZURE_CLIENT_SECRET);
String certPath = configuration.get(Configuration.PROPERTY_AZURE_CLIENT_CERTIFICATE_PATH);
String username = configuration.get(Configuration.PROPERTY_AZURE_USERNAME);
String password = configuration.get(Configuration.PROPERTY_AZURE_PASSWORD);
if (verifyNotNull(clientId)) {
if (verifyNotNull(tenantId, clientSecret)) {
// TODO: support other clouds
return new ClientSecretCredential(tenantId, clientId, clientSecret, identityClientOptions);
} else if (verifyNotNull(tenantId, certPath)) {
return new ClientCertificateCredential(tenantId, clientId, certPath, null, identityClientOptions);
} else if (verifyNotNull(username, password)) {
return new UsernamePasswordCredential(clientId,
String clientId = configuration.get(Configuration.PROPERTY_AZURE_CLIENT_ID);
String tenantId = configuration.get(Configuration.PROPERTY_AZURE_TENANT_ID);
String clientSecret = configuration.get(Configuration.PROPERTY_AZURE_CLIENT_SECRET);
String certPath = configuration.get(Configuration.PROPERTY_AZURE_CLIENT_CERTIFICATE_PATH);
String username = configuration.get(Configuration.PROPERTY_AZURE_USERNAME);
String password = configuration.get(Configuration.PROPERTY_AZURE_PASSWORD);
if (verifyNotNull(clientId)) {
if (verifyNotNull(tenantId, clientSecret)) {
targetCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, identityClientOptions);
} else if (verifyNotNull(tenantId, certPath)) {
targetCredential = new ClientCertificateCredential(tenantId, clientId, certPath,
null, identityClientOptions);
} else if (verifyNotNull(username, password)) {
targetCredential = new UsernamePasswordCredential(clientId,
tenantId,
username,
password,
identityClientOptions);
}
}
}
tokenCredential = targetCredential;
}

// Other environment variables
throw logger.logExceptionAsError(new CredentialUnavailableException(
"Cannot create any credentials with the current environment variables"));
}).flatMap(cred -> cred.getToken(request));
@Override
public Mono<AccessToken> getToken(TokenRequestContext request) {
if (tokenCredential == null) {
return Mono.error(logger.logExceptionAsError(new CredentialUnavailableException(
"Cannot create any credentials with the current environment variables")));
} else {
return tokenCredential.getToken(request);
}
}

private boolean verifyNotNull(String... configs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.azure.core.credential.AccessToken;
import com.azure.core.credential.TokenCredential;
import com.azure.core.credential.TokenRequestContext;
import com.azure.core.util.CoreUtils;
import com.azure.identity.implementation.IdentityClient;
import com.azure.identity.implementation.IdentityClientBuilder;
import com.azure.identity.implementation.IdentityClientOptions;
Expand Down Expand Up @@ -49,9 +50,11 @@ class IntelliJCredential implements TokenCredential {
authMethodDetails = null;
}

String azureEnv = authMethodDetails != null ? authMethodDetails.getAzureEnv() : "";
String cloudInstance = accessor.getAzureAuthHost(azureEnv);
options.setAuthorityHost(cloudInstance);
if (CoreUtils.isNullOrEmpty(options.getAuthorityHost())) {
String azureEnv = authMethodDetails != null ? authMethodDetails.getAzureEnv() : "";
String cloudInstance = accessor.getAzureAuthHost(azureEnv);
options.setAuthorityHost(cloudInstance);
}

String tenant = tenantId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.azure.core.credential.AccessToken;
import com.azure.core.credential.TokenCredential;
import com.azure.core.credential.TokenRequestContext;
import com.azure.core.util.CoreUtils;
import com.azure.identity.implementation.IdentityClient;
import com.azure.identity.implementation.IdentityClientBuilder;
import com.azure.identity.implementation.IdentityClientOptions;
Expand Down Expand Up @@ -35,17 +36,26 @@ class VisualStudioCodeCredential implements TokenCredential {

IdentityClientOptions options = (identityClientOptions == null ? new IdentityClientOptions()
: identityClientOptions);
String tenant;

String tenant = tenantId;
if (tenant == null) {
tenant = "common";
}
VisualStudioCacheAccessor accessor = new VisualStudioCacheAccessor();
Map<String, String> userSettings = accessor.getUserSettingsDetails(tenant);
Map<String, String> userSettings = accessor.getUserSettingsDetails();

cloudInstance = userSettings.get("cloud");
options.setAuthorityHost(accessor.getAzureAuthHost(cloudInstance));
if (CoreUtils.isNullOrEmpty(options.getAuthorityHost())) {
options.setAuthorityHost(accessor.getAzureAuthHost(cloudInstance));
}

if (!CoreUtils.isNullOrEmpty(tenantId)) {
tenant = tenantId;
} else if (userSettings.containsKey("tenant")) {
tenant = userSettings.get("tenant");
} else {
tenant = "common";
}

identityClient = new IdentityClientBuilder()
.tenantId(tenant)
.clientId("aebc6443-996d-45c2-90f0-388ff96faa56")
.identityClientOptions(options)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,17 @@ private JsonNode getUserSettings() {
/**
* Get the user configured settings of Visual Studio code.
*
* @param tenantId the user specified tenant id.
* @return a Map containing Vs Code user settings
*/
public Map<String, String> getUserSettingsDetails(String tenantId) {
public Map<String, String> getUserSettingsDetails() {
JsonNode userSettings = getUserSettings();
Map<String, String> details = new HashMap<>();

String tenant = tenantId;

String tenant = null;
String cloud = "Azure";

if (userSettings != null && !userSettings.isNull()) {
if (userSettings.has("azure.tenant") && CoreUtils.isNullOrEmpty(tenant)) {
if (userSettings.has("azure.tenant")) {
tenant = userSettings.get("azure.tenant").asText();
}

Expand All @@ -80,7 +78,10 @@ public Map<String, String> getUserSettingsDetails(String tenantId) {
}
}

details.put("tenant", tenant);
if (!CoreUtils.isNullOrEmpty(tenant)) {
details.put("tenant", tenant);
}

details.put("cloud", cloud);
return details;
}
Expand Down

0 comments on commit 1bfd8f7

Please sign in to comment.