Skip to content

Commit

Permalink
Fix configuration usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed Dec 14, 2019
1 parent a838bf1 commit 86ee23a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,32 @@
public class ManagedIdentityCredentialLiveTest {
private static final String AZURE_VAULT_URL = "AZURE_VAULT_URL";
private static final String VAULT_SECRET_NAME = "secret";
private static final Configuration CONFIGURATION = Configuration.getGlobalConfiguration().clone();

@Test
public void testMSIEndpointWithSystemAssigned() throws Exception {
Configuration configuration = Configuration.getGlobalConfiguration();
org.junit.Assume.assumeNotNull(configuration.get(Configuration.PROPERTY_MSI_ENDPOINT));
org.junit.Assume.assumeTrue(configuration.get(Configuration.PROPERTY_AZURE_CLIENT_ID) == null);
org.junit.Assume.assumeNotNull(CONFIGURATION.get(Configuration.PROPERTY_MSI_ENDPOINT));
org.junit.Assume.assumeTrue(CONFIGURATION.get(Configuration.PROPERTY_AZURE_CLIENT_ID) == null);

IdentityClient client = new IdentityClientBuilder().build();
StepVerifier.create(client.authenticateToManagedIdentityEndpoint(
configuration.get(Configuration.PROPERTY_MSI_ENDPOINT),
configuration.get(Configuration.PROPERTY_MSI_SECRET),
CONFIGURATION.get(Configuration.PROPERTY_MSI_ENDPOINT),
CONFIGURATION.get(Configuration.PROPERTY_MSI_SECRET),
new TokenRequestContext().addScopes("https://management.azure.com/.default")))
.expectNextMatches(accessToken -> accessToken != null && accessToken.getToken() != null)
.verifyComplete();
}

@Test
public void testMSIEndpointWithSystemAssignedAccessKeyVault() throws Exception {
Configuration configuration = Configuration.getGlobalConfiguration();
org.junit.Assume.assumeNotNull(configuration.get(Configuration.PROPERTY_MSI_ENDPOINT));
org.junit.Assume.assumeTrue(configuration.get(Configuration.PROPERTY_AZURE_CLIENT_ID) == null);
org.junit.Assume.assumeNotNull(CONFIGURATION.get(Configuration.PROPERTY_MSI_ENDPOINT));
org.junit.Assume.assumeTrue(CONFIGURATION.get(Configuration.PROPERTY_AZURE_CLIENT_ID) == null);

ManagedIdentityCredential credential = new ManagedIdentityCredentialBuilder().build();

SecretClient client = new SecretClientBuilder()
.credential(credential)
.vaultUrl(configuration.get(AZURE_VAULT_URL))
.vaultUrl(CONFIGURATION.get(AZURE_VAULT_URL))
.buildClient();

KeyVaultSecret secret = client.getSecret(VAULT_SECRET_NAME);
Expand All @@ -58,34 +57,32 @@ public void testMSIEndpointWithSystemAssignedAccessKeyVault() throws Exception {

@Test
public void testMSIEndpointWithUserAssigned() throws Exception {
Configuration configuration = Configuration.getGlobalConfiguration();
org.junit.Assume.assumeNotNull(configuration.get(Configuration.PROPERTY_MSI_ENDPOINT));
org.junit.Assume.assumeNotNull(configuration.get(Configuration.PROPERTY_AZURE_CLIENT_ID));
org.junit.Assume.assumeNotNull(CONFIGURATION.get(Configuration.PROPERTY_MSI_ENDPOINT));
org.junit.Assume.assumeNotNull(CONFIGURATION.get(Configuration.PROPERTY_AZURE_CLIENT_ID));

IdentityClient client = new IdentityClientBuilder()
.clientId(configuration.get(Configuration.PROPERTY_AZURE_CLIENT_ID))
.clientId(CONFIGURATION.get(Configuration.PROPERTY_AZURE_CLIENT_ID))
.build();
StepVerifier.create(client.authenticateToManagedIdentityEndpoint(
configuration.get(Configuration.PROPERTY_MSI_ENDPOINT),
configuration.get(Configuration.PROPERTY_MSI_SECRET),
CONFIGURATION.get(Configuration.PROPERTY_MSI_ENDPOINT),
CONFIGURATION.get(Configuration.PROPERTY_MSI_SECRET),
new TokenRequestContext().addScopes("https://management.azure.com/.default")))
.expectNextMatches(accessToken -> accessToken != null && accessToken.getToken() != null)
.verifyComplete();
}

@Test
public void testMSIEndpointWithUserAssignedAccessKeyVault() throws Exception {
Configuration configuration = Configuration.getGlobalConfiguration();
org.junit.Assume.assumeNotNull(configuration.get(Configuration.PROPERTY_MSI_ENDPOINT));
org.junit.Assume.assumeNotNull(configuration.get(Configuration.PROPERTY_AZURE_CLIENT_ID));
org.junit.Assume.assumeNotNull(CONFIGURATION.get(Configuration.PROPERTY_MSI_ENDPOINT));
org.junit.Assume.assumeNotNull(CONFIGURATION.get(Configuration.PROPERTY_AZURE_CLIENT_ID));

ManagedIdentityCredential credential = new ManagedIdentityCredentialBuilder()
.clientId(configuration.get(Configuration.PROPERTY_AZURE_CLIENT_ID))
.clientId(CONFIGURATION.get(Configuration.PROPERTY_AZURE_CLIENT_ID))
.build();

SecretClient client = new SecretClientBuilder()
.credential(credential)
.vaultUrl(configuration.get(AZURE_VAULT_URL))
.vaultUrl(CONFIGURATION.get(AZURE_VAULT_URL))
.buildClient();

KeyVaultSecret secret = client.getSecret(VAULT_SECRET_NAME);
Expand All @@ -96,9 +93,8 @@ public void testMSIEndpointWithUserAssignedAccessKeyVault() throws Exception {

@Test
public void testIMDSEndpointWithSystemAssigned() throws Exception {
Configuration configuration = Configuration.getGlobalConfiguration();
org.junit.Assume.assumeTrue(checkIMDSAvailable());
org.junit.Assume.assumeTrue(configuration.get(Configuration.PROPERTY_AZURE_CLIENT_ID) == null);
org.junit.Assume.assumeTrue(CONFIGURATION.get(Configuration.PROPERTY_AZURE_CLIENT_ID) == null);

IdentityClient client = new IdentityClientBuilder().build();
StepVerifier.create(client.authenticateToIMDSEndpoint(
Expand All @@ -109,15 +105,14 @@ public void testIMDSEndpointWithSystemAssigned() throws Exception {

@Test
public void testIMDSEndpointWithSystemAssignedAccessKeyVault() throws Exception {
Configuration configuration = Configuration.getGlobalConfiguration();
org.junit.Assume.assumeTrue(checkIMDSAvailable());
org.junit.Assume.assumeTrue(configuration.get(Configuration.PROPERTY_AZURE_CLIENT_ID) == null);
org.junit.Assume.assumeTrue(CONFIGURATION.get(Configuration.PROPERTY_AZURE_CLIENT_ID) == null);

ManagedIdentityCredential credential = new ManagedIdentityCredentialBuilder().build();

SecretClient client = new SecretClientBuilder()
.credential(credential)
.vaultUrl(configuration.get(AZURE_VAULT_URL))
.vaultUrl(CONFIGURATION.get(AZURE_VAULT_URL))
.buildClient();

KeyVaultSecret secret = client.getSecret(VAULT_SECRET_NAME);
Expand All @@ -128,12 +123,11 @@ public void testIMDSEndpointWithSystemAssignedAccessKeyVault() throws Exception

@Test
public void testIMDSEndpointWithUserAssigned() throws Exception {
Configuration configuration = Configuration.getGlobalConfiguration();
org.junit.Assume.assumeTrue(checkIMDSAvailable());
org.junit.Assume.assumeNotNull(configuration.get(Configuration.PROPERTY_AZURE_CLIENT_ID));
org.junit.Assume.assumeNotNull(CONFIGURATION.get(Configuration.PROPERTY_AZURE_CLIENT_ID));

IdentityClient client = new IdentityClientBuilder()
.clientId(configuration.get(Configuration.PROPERTY_AZURE_CLIENT_ID))
.clientId(CONFIGURATION.get(Configuration.PROPERTY_AZURE_CLIENT_ID))
.build();
StepVerifier.create(client.authenticateToIMDSEndpoint(
new TokenRequestContext().addScopes("https://management.azure.com/.default")))
Expand All @@ -143,17 +137,16 @@ public void testIMDSEndpointWithUserAssigned() throws Exception {

@Test
public void testIMDSEndpointWithUserAssignedAccessKeyVault() throws Exception {
Configuration configuration = Configuration.getGlobalConfiguration();
org.junit.Assume.assumeTrue(checkIMDSAvailable());
org.junit.Assume.assumeNotNull(configuration.get(Configuration.PROPERTY_AZURE_CLIENT_ID));
org.junit.Assume.assumeNotNull(CONFIGURATION.get(Configuration.PROPERTY_AZURE_CLIENT_ID));

ManagedIdentityCredential credential = new ManagedIdentityCredentialBuilder()
.clientId(configuration.get(Configuration.PROPERTY_AZURE_CLIENT_ID))
.clientId(CONFIGURATION.get(Configuration.PROPERTY_AZURE_CLIENT_ID))
.build();

SecretClient client = new SecretClientBuilder()
.credential(credential)
.vaultUrl(configuration.get(AZURE_VAULT_URL))
.vaultUrl(CONFIGURATION.get(AZURE_VAULT_URL))
.buildClient();

KeyVaultSecret secret = client.getSecret(VAULT_SECRET_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AppServiceMsiCredential {
* @param identityClient The identity client to acquire a token with.
*/
AppServiceMsiCredential(String clientId, IdentityClient identityClient) {
Configuration configuration = Configuration.getGlobalConfiguration();
Configuration configuration = Configuration.getGlobalConfiguration().clone();
this.msiEndpoint = configuration.get(Configuration.PROPERTY_MSI_ENDPOINT);
this.msiSecret = configuration.get(Configuration.PROPERTY_MSI_SECRET);
this.identityClient = identityClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class ManagedIdentityCredential implements TokenCredential {
.clientId(clientId)
.identityClientOptions(identityClientOptions)
.build();
Configuration configuration = Configuration.getGlobalConfiguration();
Configuration configuration = Configuration.getGlobalConfiguration().clone();
if (configuration.contains(Configuration.PROPERTY_MSI_ENDPOINT)) {
appServiceMSICredential = new AppServiceMsiCredential(clientId, identityClient);
virtualMachineMSICredential = null;
Expand Down

0 comments on commit 86ee23a

Please sign in to comment.