From 8db959d0ee59593a9cc6cdd805a4d284f7c60e19 Mon Sep 17 00:00:00 2001 From: Connie Yau Date: Wed, 19 Jun 2019 18:14:38 -0700 Subject: [PATCH] Fixes Configuration package names (#4008) * Update configuration package-name * Update TokenCredential in KeyVault * Adding scope for key vault builder * Adding a scope for all those KeyVault clients. * Fix playback mode for KeyVault --- .../credential/EnvironmentCredential.java | 6 +-- .../credential/ManagedIdentityCredential.java | 8 ++-- .../AppServiceMSICredential.java | 6 +-- .../identity/EnvironmentCredentialTests.java | 6 +-- .../ManagedIdentityCredentialTests.java | 4 +- .../azure/keyvault/keys/KeyAsyncClient.java | 2 + .../keyvault/keys/KeyAsyncClientBuilder.java | 2 +- .../com/azure/keyvault/keys/KeyClient.java | 2 + .../azure/keyvault/keys/KeyClientBuilder.java | 2 +- .../keyvault/keys/KeyAsyncClientTest.java | 2 +- .../keyvault/keys/KeyClientTestBase.java | 42 ++++++++++--------- .../com/azure/keyvault/SecretAsyncClient.java | 2 + .../keyvault/SecretAsyncClientBuilder.java | 2 +- .../java/com/azure/keyvault/SecretClient.java | 2 + .../azure/keyvault/SecretClientBuilder.java | 2 +- .../azure/keyvault/SecretAsyncClientTest.java | 3 +- .../com/azure/keyvault/SecretClientTest.java | 2 +- .../azure/keyvault/SecretClientTestBase.java | 42 ++++++++++--------- 18 files changed, 77 insertions(+), 60 deletions(-) diff --git a/identity/client/src/main/java/com/azure/identity/credential/EnvironmentCredential.java b/identity/client/src/main/java/com/azure/identity/credential/EnvironmentCredential.java index 5815c0a94f5c1..3faceb97827ac 100644 --- a/identity/client/src/main/java/com/azure/identity/credential/EnvironmentCredential.java +++ b/identity/client/src/main/java/com/azure/identity/credential/EnvironmentCredential.java @@ -3,12 +3,12 @@ package com.azure.identity.credential; -import com.azure.core.configuration.BaseConfigurations; -import com.azure.core.configuration.Configuration; -import com.azure.core.configuration.ConfigurationManager; import com.azure.core.credentials.AccessToken; import com.azure.core.credentials.TokenCredential; import com.azure.core.exception.ClientAuthenticationException; +import com.azure.core.util.configuration.BaseConfigurations; +import com.azure.core.util.configuration.Configuration; +import com.azure.core.util.configuration.ConfigurationManager; import com.azure.identity.IdentityClientOptions; import reactor.core.publisher.Mono; diff --git a/identity/client/src/main/java/com/azure/identity/credential/ManagedIdentityCredential.java b/identity/client/src/main/java/com/azure/identity/credential/ManagedIdentityCredential.java index ee7e17e9f7ac7..3b0f83557bd06 100644 --- a/identity/client/src/main/java/com/azure/identity/credential/ManagedIdentityCredential.java +++ b/identity/client/src/main/java/com/azure/identity/credential/ManagedIdentityCredential.java @@ -3,11 +3,11 @@ package com.azure.identity.credential; -import com.azure.core.configuration.BaseConfigurations; -import com.azure.core.configuration.Configuration; -import com.azure.core.configuration.ConfigurationManager; -import com.azure.core.credentials.TokenCredential; import com.azure.core.credentials.AccessToken; +import com.azure.core.credentials.TokenCredential; +import com.azure.core.util.configuration.BaseConfigurations; +import com.azure.core.util.configuration.Configuration; +import com.azure.core.util.configuration.ConfigurationManager; import com.azure.identity.IdentityClient; import com.azure.identity.IdentityClientOptions; import com.azure.identity.implementation.AppServiceMSICredential; diff --git a/identity/client/src/main/java/com/azure/identity/implementation/AppServiceMSICredential.java b/identity/client/src/main/java/com/azure/identity/implementation/AppServiceMSICredential.java index cfa6eadb77a7f..66800c33c8704 100644 --- a/identity/client/src/main/java/com/azure/identity/implementation/AppServiceMSICredential.java +++ b/identity/client/src/main/java/com/azure/identity/implementation/AppServiceMSICredential.java @@ -3,10 +3,10 @@ package com.azure.identity.implementation; -import com.azure.core.configuration.BaseConfigurations; -import com.azure.core.configuration.Configuration; -import com.azure.core.configuration.ConfigurationManager; import com.azure.core.credentials.AccessToken; +import com.azure.core.util.configuration.BaseConfigurations; +import com.azure.core.util.configuration.Configuration; +import com.azure.core.util.configuration.ConfigurationManager; import com.azure.identity.IdentityClient; import reactor.core.publisher.Mono; diff --git a/identity/client/src/test/java/com/azure/identity/EnvironmentCredentialTests.java b/identity/client/src/test/java/com/azure/identity/EnvironmentCredentialTests.java index ed0d31a07f6a6..d90ed0e462dad 100644 --- a/identity/client/src/test/java/com/azure/identity/EnvironmentCredentialTests.java +++ b/identity/client/src/test/java/com/azure/identity/EnvironmentCredentialTests.java @@ -3,10 +3,10 @@ package com.azure.identity; -import com.azure.core.configuration.BaseConfigurations; -import com.azure.core.configuration.Configuration; -import com.azure.core.configuration.ConfigurationManager; import com.azure.core.credentials.AccessToken; +import com.azure.core.util.configuration.BaseConfigurations; +import com.azure.core.util.configuration.Configuration; +import com.azure.core.util.configuration.ConfigurationManager; import com.azure.identity.credential.EnvironmentCredential; import org.junit.Assert; import org.junit.Test; diff --git a/identity/client/src/test/java/com/azure/identity/ManagedIdentityCredentialTests.java b/identity/client/src/test/java/com/azure/identity/ManagedIdentityCredentialTests.java index 7dc16e39249f9..3e6c634bae51c 100644 --- a/identity/client/src/test/java/com/azure/identity/ManagedIdentityCredentialTests.java +++ b/identity/client/src/test/java/com/azure/identity/ManagedIdentityCredentialTests.java @@ -3,8 +3,8 @@ package com.azure.identity; -import com.azure.core.configuration.BaseConfigurations; -import com.azure.core.configuration.ConfigurationManager; +import com.azure.core.util.configuration.BaseConfigurations; +import com.azure.core.util.configuration.ConfigurationManager; import com.azure.identity.credential.ManagedIdentityCredential; import org.junit.Assert; import org.junit.Test; diff --git a/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyAsyncClient.java b/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyAsyncClient.java index b3b151a075229..aabb79074e558 100644 --- a/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyAsyncClient.java +++ b/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyAsyncClient.java @@ -56,6 +56,8 @@ public final class KeyAsyncClient extends ServiceClient { static final String ACCEPT_LANGUAGE = "en-US"; static final int DEFAULT_MAX_PAGE_RESULTS = 25; static final String CONTENT_TYPE_HEADER_VALUE = "application/json"; + static final String KEY_VAULT_SCOPE = "https://vault.azure.net/.default"; + private String endpoint; private final KeyService service; diff --git a/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyAsyncClientBuilder.java b/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyAsyncClientBuilder.java index f915788e2295d..2add8d80d4c8d 100644 --- a/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyAsyncClientBuilder.java +++ b/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyAsyncClientBuilder.java @@ -104,7 +104,7 @@ public KeyAsyncClient build() { final List policies = new ArrayList<>(); policies.add(new UserAgentPolicy(AzureKeyVaultConfiguration.SDK_NAME, AzureKeyVaultConfiguration.SDK_VERSION, new Configuration())); policies.add(retryPolicy); - policies.add(new BearerTokenAuthenticationPolicy(credential)); + policies.add(new BearerTokenAuthenticationPolicy(credential, KeyAsyncClient.KEY_VAULT_SCOPE)); policies.addAll(this.policies); policies.add(new HttpLoggingPolicy(httpLogDetailLevel)); diff --git a/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyClient.java b/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyClient.java index 311057b2b2197..ea2df55013b5a 100644 --- a/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyClient.java +++ b/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyClient.java @@ -52,6 +52,8 @@ public final class KeyClient extends ServiceClient { static final String ACCEPT_LANGUAGE = "en-US"; static final int DEFAULT_MAX_PAGE_RESULTS = 25; static final String CONTENT_TYPE_HEADER_VALUE = "application/json"; + static final String KEY_VAULT_SCOPE = "https://vault.azure.net/.default"; + private String endpoint; private final KeyService service; diff --git a/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyClientBuilder.java b/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyClientBuilder.java index bfe450eb75342..f00b426453b73 100644 --- a/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyClientBuilder.java +++ b/keyvault/client/keys/src/main/java/com/azure/keyvault/keys/KeyClientBuilder.java @@ -104,7 +104,7 @@ public KeyClient build() { final List policies = new ArrayList<>(); policies.add(new UserAgentPolicy(AzureKeyVaultConfiguration.SDK_NAME, AzureKeyVaultConfiguration.SDK_VERSION, new Configuration())); policies.add(retryPolicy); - policies.add(new BearerTokenAuthenticationPolicy(credential)); + policies.add(new BearerTokenAuthenticationPolicy(credential, KeyClient.KEY_VAULT_SCOPE)); policies.addAll(this.policies); policies.add(new HttpLoggingPolicy(httpLogDetailLevel)); diff --git a/keyvault/client/keys/src/test/java/com/azure/keyvault/keys/KeyAsyncClientTest.java b/keyvault/client/keys/src/test/java/com/azure/keyvault/keys/KeyAsyncClientTest.java index 4f1d3cf1d9925..999aa9f87d63f 100644 --- a/keyvault/client/keys/src/test/java/com/azure/keyvault/keys/KeyAsyncClientTest.java +++ b/keyvault/client/keys/src/test/java/com/azure/keyvault/keys/KeyAsyncClientTest.java @@ -20,9 +20,9 @@ import java.util.HashMap; import java.util.List; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertEquals; public class KeyAsyncClientTest extends KeyClientTestBase { diff --git a/keyvault/client/keys/src/test/java/com/azure/keyvault/keys/KeyClientTestBase.java b/keyvault/client/keys/src/test/java/com/azure/keyvault/keys/KeyClientTestBase.java index 3ae1c327c0ae9..75a6df653e07a 100644 --- a/keyvault/client/keys/src/test/java/com/azure/keyvault/keys/KeyClientTestBase.java +++ b/keyvault/client/keys/src/test/java/com/azure/keyvault/keys/KeyClientTestBase.java @@ -3,6 +3,7 @@ package com.azure.keyvault.keys; +import com.azure.core.credentials.AccessToken; import com.azure.core.credentials.TokenCredential; import com.azure.core.exception.HttpResponseException; import com.azure.core.http.rest.Response; @@ -21,13 +22,14 @@ import reactor.core.publisher.Mono; import java.net.MalformedURLException; +import java.time.Duration; import java.time.OffsetDateTime; import java.time.ZoneOffset; -import java.util.Objects; -import java.util.Map; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.ArrayList; +import java.util.Map; +import java.util.Objects; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -36,9 +38,9 @@ import java.util.function.Consumer; import java.util.function.Function; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; -import static org.junit.Assert.assertEquals; public abstract class KeyClientTestBase extends TestBase { @@ -64,11 +66,11 @@ T clientSetup(Function clientBuilder) { : System.getenv("AZURE_KEYVAULT_ENDPOINT"); final String tenantId = interceptorManager.isPlaybackMode() - ? "" + ? "some-tenant-id" : System.getenv("MICROSOFT_AD_TENANT_ID"); final String clientId = interceptorManager.isPlaybackMode() - ? "" + ? "some-client-id" : System.getenv("ARM_CLIENT_ID"); final String clientKey = interceptorManager.isPlaybackMode() @@ -80,17 +82,15 @@ T clientSetup(Function clientBuilder) { Objects.requireNonNull(clientKey, "ARM_CLIENT_KEY expected to be set."); Objects.requireNonNull(tenantId, "MICROSOFT_AD_TENANT_ID expected to be set."); + TokenCredential credential = resource -> { + if (interceptorManager.isPlaybackMode()) { + return Mono.just(new AccessToken("Some fake token", OffsetDateTime.now(ZoneOffset.UTC).plus(Duration.ofMinutes(30)))); + } - TokenCredential credential = new TokenCredential() { - @Override - public Mono getTokenAsync(String resource) { - String token = ""; - try { - token = getAccessToken(tenantId, clientId, clientKey); - } catch (Exception e) { - e.printStackTrace(); - } - return Mono.just(token); + try { + return Mono.just(getAccessToken(tenantId, clientId, clientKey)); + } catch (Exception e) { + return Mono.error(e); } }; @@ -102,7 +102,7 @@ public Mono getTokenAsync(String resource) { return Objects.requireNonNull(client); } - private String getAccessToken(String tenantId, String clientId, String clientKey) throws MalformedURLException, ExecutionException, InterruptedException { + private AccessToken getAccessToken(String tenantId, String clientId, String clientKey) throws MalformedURLException, ExecutionException, InterruptedException { String authority = "https://login.microsoftonline.com/{tenantId}"; String auth = authority.replace("{tenantId}", tenantId); @@ -114,8 +114,12 @@ private String getAccessToken(String tenantId, String clientId, String clientKey new ClientCredential(clientId, clientKey), null ); - String token = result.get().getAccessToken(); - return token; + + final AuthenticationResult authenticationResult = result.get(); + final String token = authenticationResult.getAccessToken(); + final OffsetDateTime expiresOn = authenticationResult.getExpiresOnDate().toInstant().atOffset(ZoneOffset.UTC); + + return new AccessToken(token, expiresOn); } @Test diff --git a/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretAsyncClient.java b/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretAsyncClient.java index 0e7ed18e1a02b..e673b35ef98c2 100644 --- a/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretAsyncClient.java +++ b/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretAsyncClient.java @@ -47,6 +47,8 @@ public final class SecretAsyncClient extends ServiceClient { static final String ACCEPT_LANGUAGE = "en-US"; static final int DEFAULT_MAX_PAGE_RESULTS = 25; static final String CONTENT_TYPE_HEADER_VALUE = "application/json"; + static final String KEY_VAULT_SCOPE = "https://vault.azure.net/.default"; + private String endpoint; private final SecretService service; diff --git a/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretAsyncClientBuilder.java b/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretAsyncClientBuilder.java index 5d10287039a70..7b15cb5085158 100644 --- a/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretAsyncClientBuilder.java +++ b/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretAsyncClientBuilder.java @@ -104,7 +104,7 @@ public SecretAsyncClient build() { final List policies = new ArrayList<>(); policies.add(new UserAgentPolicy(AzureKeyVaultConfiguration.SDK_NAME, AzureKeyVaultConfiguration.SDK_VERSION, new Configuration())); policies.add(retryPolicy); - policies.add(new BearerTokenAuthenticationPolicy(credential)); + policies.add(new BearerTokenAuthenticationPolicy(credential, SecretAsyncClient.KEY_VAULT_SCOPE)); policies.addAll(this.policies); policies.add(new HttpLoggingPolicy(httpLogDetailLevel)); diff --git a/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretClient.java b/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretClient.java index d4e5dec8a882d..1c1119ce3084a 100644 --- a/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretClient.java +++ b/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretClient.java @@ -47,6 +47,8 @@ public final class SecretClient extends ServiceClient { static final String ACCEPT_LANGUAGE = "en-US"; static final int DEFAULT_MAX_PAGE_RESULTS = 25; static final String CONTENT_TYPE_HEADER_VALUE = "application/json"; + static final String KEY_VAULT_SCOPE = "https://vault.azure.net/.default"; + private String endpoint; private final SecretService service; diff --git a/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretClientBuilder.java b/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretClientBuilder.java index eaf90ba190e17..f040ac3c7312a 100644 --- a/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretClientBuilder.java +++ b/keyvault/client/secrets/src/main/java/com/azure/keyvault/SecretClientBuilder.java @@ -104,7 +104,7 @@ public SecretClient build() { final List policies = new ArrayList<>(); policies.add(new UserAgentPolicy(AzureKeyVaultConfiguration.SDK_NAME, AzureKeyVaultConfiguration.SDK_VERSION, new Configuration())); policies.add(retryPolicy); - policies.add(new BearerTokenAuthenticationPolicy(credential)); + policies.add(new BearerTokenAuthenticationPolicy(credential, SecretClient.KEY_VAULT_SCOPE)); policies.addAll(this.policies); policies.add(new HttpLoggingPolicy(httpLogDetailLevel)); diff --git a/keyvault/client/secrets/src/test/java/com/azure/keyvault/SecretAsyncClientTest.java b/keyvault/client/secrets/src/test/java/com/azure/keyvault/SecretAsyncClientTest.java index 0194608fa4597..be2bc94b19985 100644 --- a/keyvault/client/secrets/src/test/java/com/azure/keyvault/SecretAsyncClientTest.java +++ b/keyvault/client/secrets/src/test/java/com/azure/keyvault/SecretAsyncClientTest.java @@ -14,13 +14,14 @@ import com.azure.keyvault.models.SecretBase; import io.netty.handler.codec.http.HttpResponseStatus; import reactor.test.StepVerifier; + import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertEquals; public class SecretAsyncClientTest extends SecretClientTestBase { diff --git a/keyvault/client/secrets/src/test/java/com/azure/keyvault/SecretClientTest.java b/keyvault/client/secrets/src/test/java/com/azure/keyvault/SecretClientTest.java index 8d773d55be9e7..e84c29658f237 100644 --- a/keyvault/client/secrets/src/test/java/com/azure/keyvault/SecretClientTest.java +++ b/keyvault/client/secrets/src/test/java/com/azure/keyvault/SecretClientTest.java @@ -16,8 +16,8 @@ import java.util.HashMap; import java.util.List; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; public class SecretClientTest extends SecretClientTestBase { diff --git a/keyvault/client/secrets/src/test/java/com/azure/keyvault/SecretClientTestBase.java b/keyvault/client/secrets/src/test/java/com/azure/keyvault/SecretClientTestBase.java index 3d00900138a99..bfe571c0669ae 100644 --- a/keyvault/client/secrets/src/test/java/com/azure/keyvault/SecretClientTestBase.java +++ b/keyvault/client/secrets/src/test/java/com/azure/keyvault/SecretClientTestBase.java @@ -3,6 +3,7 @@ package com.azure.keyvault; +import com.azure.core.credentials.AccessToken; import com.azure.core.credentials.TokenCredential; import com.azure.core.exception.HttpResponseException; import com.azure.core.http.rest.Response; @@ -19,13 +20,14 @@ import reactor.core.publisher.Mono; import java.net.MalformedURLException; +import java.time.Duration; import java.time.OffsetDateTime; import java.time.ZoneOffset; -import java.util.Objects; -import java.util.Map; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.ArrayList; +import java.util.Map; +import java.util.Objects; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -34,9 +36,9 @@ import java.util.function.Consumer; import java.util.function.Function; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; -import static org.junit.Assert.assertEquals; public abstract class SecretClientTestBase extends TestBase { @@ -60,11 +62,11 @@ T clientSetup(Function clientBuilder) { : System.getenv("AZURE_KEYVAULT_ENDPOINT"); final String tenantId = interceptorManager.isPlaybackMode() - ? "" + ? "some-tenant" : System.getenv("MICROSOFT_AD_TENANT_ID"); final String clientId = interceptorManager.isPlaybackMode() - ? "" + ? "some-client-id" : System.getenv("ARM_CLIENT_ID"); final String clientKey = interceptorManager.isPlaybackMode() @@ -76,17 +78,15 @@ T clientSetup(Function clientBuilder) { Objects.requireNonNull(clientKey, "ARM_CLIENT_KEY expected to be set."); Objects.requireNonNull(tenantId, "MICROSOFT_AD_TENANT_ID expected to be set."); + TokenCredential credential = resource -> { + if (interceptorManager.isPlaybackMode()) { + return Mono.just(new AccessToken("Some fake token", OffsetDateTime.now(ZoneOffset.UTC).plus(Duration.ofMinutes(30)))); + } - TokenCredential credential = new TokenCredential() { - @Override - public Mono getTokenAsync(String resource) { - String token = ""; - try { - token = getAccessToken(tenantId, clientId, clientKey); - } catch (Exception e) { - e.printStackTrace(); - } - return Mono.just(token); + try { + return Mono.just(getAccessToken(tenantId, clientId, clientKey)); + } catch (Exception e) { + return Mono.error(e); } }; @@ -98,7 +98,7 @@ public Mono getTokenAsync(String resource) { return Objects.requireNonNull(client); } - private String getAccessToken(String tenantId, String clientId, String clientKey) throws MalformedURLException, ExecutionException, InterruptedException { + private AccessToken getAccessToken(String tenantId, String clientId, String clientKey) throws MalformedURLException, ExecutionException, InterruptedException { String authority = "https://login.microsoftonline.com/{tenantId}"; String auth = authority.replace("{tenantId}", tenantId); @@ -110,8 +110,12 @@ private String getAccessToken(String tenantId, String clientId, String clientKey new ClientCredential(clientId, clientKey), null ); - String token = result.get().getAccessToken(); - return token; + + final AuthenticationResult authenticationResult = result.get(); + final String token = authenticationResult.getAccessToken(); + final OffsetDateTime expiresOn = authenticationResult.getExpiresOnDate().toInstant().atOffset(ZoneOffset.UTC); + + return new AccessToken(token, expiresOn); } @Test