Skip to content

Commit

Permalink
Fixes Configuration package names (#4008)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
conniey authored Jun 20, 2019
1 parent 84be0dd commit 8db959d
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public KeyAsyncClient build() {
final List<HttpPipelinePolicy> 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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public KeyClient build() {
final List<HttpPipelinePolicy> 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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {

Expand All @@ -64,11 +66,11 @@ <T> T clientSetup(Function<TokenCredential, T> 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()
Expand All @@ -80,17 +82,15 @@ <T> T clientSetup(Function<TokenCredential, T> 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<String> 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);
}
};

Expand All @@ -102,7 +102,7 @@ public Mono<String> 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);

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public SecretAsyncClient build() {
final List<HttpPipelinePolicy> 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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public SecretClient build() {
final List<HttpPipelinePolicy> 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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {

Expand All @@ -60,11 +62,11 @@ <T> T clientSetup(Function<TokenCredential, T> 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()
Expand All @@ -76,17 +78,15 @@ <T> T clientSetup(Function<TokenCredential, T> 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<String> 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);
}
};

Expand All @@ -98,7 +98,7 @@ public Mono<String> 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);

Expand All @@ -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
Expand Down

0 comments on commit 8db959d

Please sign in to comment.