Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tracing context in Key Vault secrets package #4549

Merged
merged 15 commits into from
Jul 31, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@
<Match>
<Or>
<Class name="com.azure.security.keyvault.keys.KeyClient"/>
<Class name="com.azure.security.keyvault.keys.SecretClient"/>
<Class name="com.azure.security.keyvault.secrets.SecretClient"/>
</Or>
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"/>
</Match>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ Mono<Response<Key>> createKey(String name, KeyType keyType, Context context) {
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> createKey(KeyCreateOptions keyCreateOptions) {
return withContext(context -> createKeyWithResponse(keyCreateOptions))
.flatMap(FluxUtil::toMono);
return this.createKeyWithResponse(keyCreateOptions).flatMap(FluxUtil::toMono);
}

Mono<Response<Key>> createKey(KeyCreateOptions keyCreateOptions, Context context) {
Expand Down Expand Up @@ -196,8 +195,7 @@ Mono<Response<Key>> createKey(KeyCreateOptions keyCreateOptions, Context context
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> createRsaKey(RsaKeyCreateOptions rsaKeyCreateOptions) {
return withContext(context -> createRsaKeyWithResponse(rsaKeyCreateOptions))
.flatMap(FluxUtil::toMono);
return this.createRsaKeyWithResponse(rsaKeyCreateOptions).flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -262,8 +260,7 @@ Mono<Response<Key>> createRsaKey(RsaKeyCreateOptions rsaKeyCreateOptions, Contex
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> createEcKey(EcKeyCreateOptions ecKeyCreateOptions) {
return withContext(context -> createEcKeyWithResponse(ecKeyCreateOptions))
.flatMap(FluxUtil::toMono);
return this.createEcKeyWithResponse(ecKeyCreateOptions).flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -366,8 +363,7 @@ Mono<Response<Key>> importKey(String name, JsonWebKey keyMaterial, Context conte
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> importKey(KeyImportOptions keyImportOptions) {
return withContext(context -> importKeyWithResponse(keyImportOptions))
.flatMap(FluxUtil::toMono);
return this.importKeyWithResponse(keyImportOptions).flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -430,12 +426,7 @@ Mono<Response<Key>> importKey(KeyImportOptions keyImportOptions, Context context
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> getKey(String name, String version) {
if (version == null) {
return getKeyWithResponse(name)
.flatMap(FluxUtil::toMono);
}
return withContext(context -> getKeyWithResponse(name, version))
.flatMap(FluxUtil::toMono);
return this.getKeyWithResponse(name, version).flatMap(FluxUtil::toMono);
}

/**
Expand All @@ -456,7 +447,7 @@ public Mono<Key> getKey(String name, String version) {
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<Key>> getKeyWithResponse(String name, String version) {
if (version == null) {
return getKeyWithResponse(name);
return withContext(context -> getKey(name, "", context));
}
return withContext(context -> getKey(name, version, context));
samvaity marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -485,28 +476,7 @@ Mono<Response<Key>> getKey(String name, String version, Context context) {
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> getKey(String name) {
return getKeyWithResponse(name, "")
.flatMap(FluxUtil::toMono);
}

/**
* Get the public part of the latest version of the specified key from the key vault. The get key operation is applicable to
* all key types and it requires the {@code keys/get} permission.
*
* <p><strong>Code Samples</strong></p>
* <p>Gets latest version of the key in the key vault. Subscribes to the call asynchronously and prints out the
* returned key details when a response has been received.</p>
*
* {@codesnippet com.azure.security.keyvault.keys.async.keyclient.getKeyWithResponse#string}
*
* @param name The name of the key.
* @throws ResourceNotFoundException when a key with {@code name} doesn't exist in the key vault.
* @throws HttpRequestException if {@code name} is empty string.
* @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the requested {@link Key key}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<Key>> getKeyWithResponse(String name) {
return getKeyWithResponse(name, "");
return this.getKeyWithResponse(name, "").flatMap(FluxUtil::toMono);
}

/**
Expand All @@ -526,13 +496,7 @@ public Mono<Response<Key>> getKeyWithResponse(String name) {
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> getKey(KeyBase keyBase) {
Objects.requireNonNull(keyBase, "The Key Base parameter cannot be null.");
if (keyBase.version() == null) {
return getKeyWithResponse(keyBase.name())
.flatMap(FluxUtil::toMono);
}
return getKeyWithResponse(keyBase.name(), keyBase.version())
.flatMap(FluxUtil::toMono);
return this.getKeyWithResponse(keyBase).flatMap(FluxUtil::toMono);
}

/**
Expand All @@ -554,9 +518,9 @@ public Mono<Key> getKey(KeyBase keyBase) {
public Mono<Response<Key>> getKeyWithResponse(KeyBase keyBase) {
Objects.requireNonNull(keyBase, "The Key Base parameter cannot be null.");
if (keyBase.version() == null) {
return getKeyWithResponse(keyBase.name());
return withContext(context -> getKey(keyBase.name(), "", context));
}
return getKeyWithResponse(keyBase.name(), keyBase.version());
return withContext(context -> getKey(keyBase.name(), keyBase.version(), context));
samvaity marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -578,30 +542,7 @@ public Mono<Response<Key>> getKeyWithResponse(KeyBase keyBase) {
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> updateKey(KeyBase key) {
return withContext(context -> updateKeyWithResponse(key))
.flatMap(FluxUtil::toMono);
}

/**
* Updates the attributes associated with the specified key, but not the cryptographic key material of the specified key in the key vault. The update
* operation changes specified attributes of an existing stored key and attributes that are not specified in the request are left unchanged.
* The cryptographic key material of a key itself cannot be changed. This operation requires the {@code keys/set} permission.
*
* <p><strong>Code Samples</strong></p>
* <p>Gets latest version of the key, changes its notBefore time and then updates it in the Azure Key Vault. Subscribes to the call asynchronously and prints out the
* returned key details when a response has been received.</p>
*
* {@codesnippet com.azure.security.keyvault.keys.async.keyclient.updateKeyWithResponse#KeyBase}
*
* @param key The {@link KeyBase base key} object with updated properties.
* @throws NullPointerException if {@code key} is {@code null}.
* @throws ResourceNotFoundException when a key with {@link KeyBase#name() name} and {@link KeyBase#version() version} doesn't exist in the key vault.
* @throws HttpRequestException if {@link KeyBase#name() name} or {@link KeyBase#version() version} is empty string.
* @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link KeyBase updated key}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<Key>> updateKeyWithResponse(KeyBase key) {
return withContext(context -> updateKey(key, context));
return withContext(context -> this.updateKey(key, context).flatMap(FluxUtil::toMono));
}

Mono<Response<Key>> updateKey(KeyBase key, Context context) {
Expand Down Expand Up @@ -658,8 +599,7 @@ public Mono<Response<Key>> updateKeyWithResponse(KeyBase key, KeyOperation... ke
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> updateKey(KeyBase key, KeyOperation... keyOperations) {
return withContext(context -> updateKeyWithResponse(key, keyOperations))
.flatMap(FluxUtil::toMono);
return this.updateKeyWithResponse(key, keyOperations).flatMap(FluxUtil::toMono);
}

Mono<Response<Key>> updateKey(KeyBase key, Context context, KeyOperation... keyOperations) {
Expand Down Expand Up @@ -694,8 +634,7 @@ Mono<Response<Key>> updateKey(KeyBase key, Context context, KeyOperation... keyO
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DeletedKey> deleteKey(String name) {
return withContext(context -> deleteKeyWithResponse(name))
.flatMap(FluxUtil::toMono);
return this.deleteKeyWithResponse(name).flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -747,8 +686,7 @@ Mono<Response<DeletedKey>> deleteKey(String name, Context context) {
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DeletedKey> getDeletedKey(String name) {
return withContext(context -> getDeletedKeyWithResponse(name))
.flatMap(FluxUtil::toMono);
return this.getDeletedKeyWithResponse(name).flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -826,8 +764,7 @@ Mono<VoidResponse> purgeDeletedKey(String name, Context context) {
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> recoverDeletedKey(String name) {
return withContext(context -> recoverDeletedKeyWithResponse(name))
.flatMap(FluxUtil::toMono);
return this.recoverDeletedKeyWithResponse(name).flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -882,8 +819,7 @@ Mono<Response<Key>> recoverDeletedKey(String name, Context context) {
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<byte[]> backupKey(String name) {
return withContext(context -> backupKeyWithResponse(name))
.flatMap(FluxUtil::toMono);
return this.backupKeyWithResponse(name).flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -944,8 +880,7 @@ Mono<Response<byte[]>> backupKey(String name, Context context) {
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> restoreKey(byte[] backup) {
return withContext(context -> restoreKeyWithResponse(backup))
.flatMap(FluxUtil::toMono);
return this.restoreKeyWithResponse(backup).flatMap(FluxUtil::toMono);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public Key createRsaKey(RsaKeyCreateOptions rsaKeyCreateOptions) {
*
* <p><strong>Code Samples</strong></p>
* <p>Creates a new RSA key with size 2048 which activates in one day and expires in one year. Prints out the details of the created key.</p>
* {@codesnippet com.azure.keyvault.keys.keyclient.createRsaKeyWithResponse#keyOptions}
* {@codesnippet com.azure.keyvault.keys.keyclient.createRsaKeyWithResponse#keyOptions-Context}
*
* @param rsaKeyCreateOptions The key options object containing information about the rsa key being created.
* @param context Additional context that is passed through the Http pipeline during the service call.
Expand Down Expand Up @@ -209,7 +209,7 @@ public Key createEcKey(EcKeyCreateOptions ecKeyCreateOptions) {
* <p><strong>Code Samples</strong></p>
* <p>Creates a new EC key with P-384 web key curve. The key activates in one day and expires in one year. Prints out
* the details of the created key.</p>
* {@codesnippet com.azure.keyvault.keys.keyclient.createEcKeyWithResponse#keyOptions}
* {@codesnippet com.azure.keyvault.keys.keyclient.createEcKeyWithResponse#keyOptions-Context}
*
* @param ecKeyCreateOptions The key options object containing information about the ec key being created.
* @param context Additional context that is passed through the Http pipeline during the service call.
Expand Down Expand Up @@ -368,11 +368,30 @@ public Key getKey(String name) {
* @return The requested {@link Key key}.
*/
public Key getKey(KeyBase keyBase) {
return this.getKeyWithResponse(keyBase, Context.NONE).value();
}

/**
* Get public part of the key which represents {@link KeyBase keyBase} from the key vault. The get key operation is applicable
* to all key types and it requires the {@code keys/get} permission.
*
* <p>The list operations {@link KeyClient#listKeys()} and {@link KeyClient#listKeyVersions(String)} return
* the {@link List} containing {@link KeyBase base key} as output excluding the key material of the key.
* This operation can then be used to get the full key with its key material from {@code keyBase}. </p>
* {@codesnippet com.azure.keyvault.keys.keyclient.getKeyWithResponse#KeyBase-Context}
*
* @param keyBase The {@link KeyBase base key} holding attributes of the key being requested.
* @param context Additional context that is passed through the Http pipeline during the service call.
* @throws ResourceNotFoundException when a key with {@link KeyBase#name() name} and {@link KeyBase#version() version} doesn't exist in the key vault.
* @throws HttpRequestException if {@link KeyBase#name()} name} or {@link KeyBase#version() version} is empty string.
* @return A {@link Response} whose {@link Response#value() value} contains the requested {@link Key key}.
*/
public Response<Key> getKeyWithResponse(KeyBase keyBase, Context context) {
Objects.requireNonNull(keyBase, "The Key Base parameter cannot be null.");
if (keyBase.version() == null) {
return getKey(keyBase.name());
return client.getKey(keyBase.name(), "", context).block();
}
return getKeyWithResponse(keyBase.name(), keyBase.version(), Context.NONE).value();
return client.getKey(keyBase.name(), keyBase.version(), context).block();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.azure.core.test.policy.RecordNetworkCallPolicy;
import com.azure.identity.credential.DefaultAzureCredential;
import com.azure.security.keyvault.keys.models.EcKeyCreateOptions;
import com.azure.security.keyvault.keys.models.Key;
import com.azure.security.keyvault.keys.models.KeyBase;
import com.azure.security.keyvault.keys.models.KeyCreateOptions;
import com.azure.security.keyvault.keys.models.RsaKeyCreateOptions;
Expand Down Expand Up @@ -209,14 +208,6 @@ public void getKeyWithResponseSnippets() {
keyResponse.value().name(), keyResponse.value().id()));
// END: com.azure.security.keyvault.keys.async.keyclient.getKeyWithResponse#string-string

// BEGIN: com.azure.security.keyvault.keys.async.keyclient.getKeyWithResponse#string
keyAsyncClient.getKeyWithResponse("keyName")
.subscriberContext(Context.of(key1, value1, key2, value2))
.subscribe(keyResponse ->
System.out.printf("Key is created with name %s and id %s \n",
keyResponse.value().name(), keyResponse.value().id()));
// END: com.azure.security.keyvault.keys.async.keyclient.getKeyWithResponse#string

// BEGIN: com.azure.security.keyvault.keys.async.keyclient.getKeyWithResponse#KeyBase
keyAsyncClient.listKeys().subscribe(keyBase ->
keyAsyncClient.getKeyWithResponse(keyBase)
Expand Down Expand Up @@ -263,12 +254,11 @@ public void updateKeyWithResponseSnippets() {
KeyAsyncClient keyAsyncClient = createAsyncClient();

// BEGIN: com.azure.security.keyvault.keys.async.keyclient.updateKeyWithResponse#KeyBase-keyOperations
keyAsyncClient.getKeyWithResponse("keyName")
keyAsyncClient.getKey("keyName")
.subscribe(keyResponse -> {
Key key = keyResponse.value();
//Update the not before time of the key.
key.notBefore(OffsetDateTime.now().plusDays(50));
keyAsyncClient.updateKeyWithResponse(key, KeyOperation.ENCRYPT, KeyOperation.DECRYPT)
keyResponse.notBefore(OffsetDateTime.now().plusDays(50));
keyAsyncClient.updateKeyWithResponse(keyResponse, KeyOperation.ENCRYPT, KeyOperation.DECRYPT)
.subscriberContext(Context.of(key1, value1, key2, value2))
.subscribe(updatedKeyResponse ->
System.out.printf("Key's updated not before time %s \n",
Expand All @@ -277,12 +267,11 @@ public void updateKeyWithResponseSnippets() {
// END: com.azure.security.keyvault.keys.async.keyclient.updateKeyWithResponse#KeyBase-keyOperations

// BEGIN: com.azure.security.keyvault.keys.async.keyclient.updateKeyWithResponse#KeyBase
keyAsyncClient.getKeyWithResponse("keyName")
keyAsyncClient.getKey("keyName")
.subscribe(keyResponse -> {
Key key = keyResponse.value();
//Update the not before time of the key.
key.notBefore(OffsetDateTime.now().plusDays(50));
keyAsyncClient.updateKeyWithResponse(key)
keyResponse.notBefore(OffsetDateTime.now().plusDays(50));
keyAsyncClient.updateKeyWithResponse(keyResponse)
.subscriberContext(Context.of(key1, value1, key2, value2))
.subscribe(updatedKeyResponse ->
System.out.printf("Key's updated not before time %s \n",
Expand Down
Loading