Skip to content

Commit

Permalink
updated with Response, passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samvaity committed Jul 23, 2019
1 parent 5140cd8 commit 0c8c38a
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ Mono<Response<Secret>> setSecret(String name, String value, Context context) {
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Secret> getSecret(String name, String version) {
return withContext(context -> getSecret(name, version, context))
.flatMap(secretResponse -> Mono.justOrEmpty(secretResponse.value()));
.flatMap(response -> Mono.justOrEmpty(response.value()));
}

/**
Expand Down Expand Up @@ -352,6 +352,37 @@ Mono<Response<Secret>> getSecret(String name, Context context) {
return getSecret(name, "", context);
}

/**
* Updates the attributes associated with the specified secret, but not the value of the specified secret in the key vault. The update
* operation changes specified attributes of an existing stored secret and attributes that are not specified in the request are left unchanged.
* The value of a secret itself cannot be changed. This operation requires the {@code secrets/set} permission.
*
* <p><strong>Code Samples</strong></p>
* <p>Gets latest version of the secret, changes its notBefore time and then updates it in the Azure Key Vault. Subscribes to the call asynchronously and prints out the
* returned secret details when a response is received.</p>
* <pre>
* secretAsyncClient.getSecret("secretName").subscribe(secretResponse -&gt; {
* Secret secret = secretResponse.value();
* //Update the not before time of the secret.
* secret.notBefore(OffsetDateTime.now().plusDays(50));
* secretAsyncClient.updateSecret(secret).subscribe(secretResponse -&gt;
* System.out.printf("Secret's updated not before time %s \n", secretResponse.value().notBefore().toString()));
* });
* </pre>
* <p>The {@code secret} is required and its fields {@link SecretBase#name() name} and {@link SecretBase#version() version} cannot be null.</p>
*
* @param secret The {@link SecretBase base secret} object with updated properties.
* @throws NullPointerException if {@code secret} is {@code null}.
* @throws ResourceNotFoundException when a secret with {@link SecretBase#name() name} and {@link SecretBase#version() version} doesn't exist in the key vault.
* @throws HttpRequestException if {@link SecretBase#name()} name} or {@link SecretBase#version() version} is empty string.
* @return A {@link Mono} containing the {@link SecretBase updated secret}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<SecretBase> updateSecret(SecretBase secret) {
return withContext(context -> updateSecret(secret, context))
.flatMap(response -> Mono.justOrEmpty(response.value()));
}

/**
* Updates the attributes associated with the specified secret, but not the value of the specified secret in the key vault. The update
* operation changes specified attributes of an existing stored secret and attributes that are not specified in the request are left unchanged.
Expand All @@ -378,7 +409,7 @@ Mono<Response<Secret>> getSecret(String name, Context context) {
* @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link SecretBase updated secret}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<SecretBase>> updateSecret(SecretBase secret) {
public Mono<Response<SecretBase>> updateSecretWithResponse(SecretBase secret) {
return withContext(context -> updateSecret(secret, context));
}

Expand All @@ -395,6 +426,30 @@ Mono<Response<SecretBase>> updateSecret(SecretBase secret, Context context) {
.doOnError(error -> logger.warning("Failed to update secret - {}", secret.name(), error));
}

/**
* Deletes a secret from the key vault. If soft-delete is enabled on the key vault then the secret is placed in the deleted state
* and requires to be purged for permanent deletion else the secret is permanently deleted. The delete operation applies to any secret stored in Azure Key Vault but
* it cannot be applied to an individual version of a secret. This operation requires the {@code secrets/delete} permission.
*
* <p><strong>Code Samples</strong></p>
* <p>Deletes the secret in the Azure Key Vault. Subscribes to the call asynchronously and prints out the
* deleted secret details when a response is received.</p>
* <pre>
* secretAsyncClient.deleteSecret("secretName").subscribe(deletedSecretResponse -&gt;
* System.out.printf("Deleted Secret's Recovery Id %s \n", deletedSecretResponse.value().recoveryId()));
* </pre>
*
* @param name The name of the secret to be deleted.
* @throws ResourceNotFoundException when a secret with {@code name} doesn't exist in the key vault.
* @throws HttpRequestException when a secret with {@code name} is empty string.
* @return A {@link Mono} containing the {@link DeletedSecret deleted secret}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DeletedSecret> deleteSecret(String name) {
return withContext(context -> deleteSecret(name, context))
.flatMap(response -> Mono.justOrEmpty(response.value()));
}

/**
* Deletes a secret from the key vault. If soft-delete is enabled on the key vault then the secret is placed in the deleted state
* and requires to be purged for permanent deletion else the secret is permanently deleted. The delete operation applies to any secret stored in Azure Key Vault but
Expand All @@ -414,7 +469,7 @@ Mono<Response<SecretBase>> updateSecret(SecretBase secret, Context context) {
* @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link DeletedSecret deleted secret}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<DeletedSecret>> deleteSecret(String name) {
public Mono<Response<DeletedSecret>> deleteSecretWithResponse(String name) {
return withContext(context -> deleteSecret(name, context));
}

Expand All @@ -425,6 +480,32 @@ Mono<Response<DeletedSecret>> deleteSecret(String name, Context context) {
.doOnError(error -> logger.warning("Failed to delete secret - {}", name, error));
}

/**
* The get deleted secret operation returns the secrets that have been deleted for a vault enabled
* for soft-delete. This operation requires the {@code secrets/list} permission.
*
* <p><strong>Code Samples</strong></p>
* <p> Gets the deleted secret from the key vault enabled for soft-delete. Subscribes to the call
* asynchronously and prints out the
* deleted secret details when a response is received.</p>
* <pre>
* //Assuming secret is deleted on a soft-delete enabled vault.
* secretAsyncClient.getDeletedSecret("secretName").subscribe(deletedSecretResponse -&gt;
* System.out.printf("Deleted Secret with recovery Id %s \n", deletedSecretResponse.value().recoveryId()));
* </pre>
*
* @param name The name of the deleted secret.
* @return A {@link Mono} containing the {@link DeletedSecret deleted secret}.
* @throws ResourceNotFoundException when a secret with {@code name} doesn't exist in the key
* vault.
* @throws HttpRequestException when a secret with {@code name} is empty string.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DeletedSecret> getDeletedSecret(String name) {
return withContext(context -> getDeletedSecret(name, context))
.flatMap(response -> Mono.justOrEmpty(response.value()));
}

/**
* The get deleted secret operation returns the secrets that have been deleted for a vault enabled
* for soft-delete. This operation requires the {@code secrets/list} permission.
Expand All @@ -447,7 +528,7 @@ Mono<Response<DeletedSecret>> deleteSecret(String name, Context context) {
* @throws HttpRequestException when a secret with {@code name} is empty string.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<DeletedSecret>> getDeletedSecret(String name) {
public Mono<Response<DeletedSecret>> getDeletedSecretWithResponse(String name) {
return withContext(context -> getDeletedSecret(name, context));
}

Expand Down Expand Up @@ -491,6 +572,30 @@ Mono<VoidResponse> purgeDeletedSecret(String name, Context context) {
.doOnError(error -> logger.warning("Failed to purge deleted secret - {}", name, error));
}

/**
* Recovers the deleted secret in the key vault to its latest version and can only be performed on a soft-delete enabled vault.
* This operation requires the {@code secrets/recover} permission.
*
* <p><strong>Code Samples</strong></p>
* <p>Recovers the deleted secret from the key vault enabled for soft-delete. Subscribes to the call asynchronously and prints out the
* recovered secret details when a response is received.</p>
* <pre>
* //Assuming secret is deleted on a soft-delete enabled vault.
* secretAsyncClient.recoverDeletedSecret("deletedSecretName").subscribe(recoveredSecretResponse -&gt;
* System.out.printf("Recovered Secret with name %s \n", recoveredSecretResponse.value().name()));
* </pre>
*
* @param name The name of the deleted secret to be recovered.
* @throws ResourceNotFoundException when a secret with {@code name} doesn't exist in the key vault.
* @throws HttpRequestException when a secret with {@code name} is empty string.
* @return A {@link Mono} containing the {@link Secret recovered secret}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Secret> recoverDeletedSecret(String name) {
return withContext(context -> recoverDeletedSecret(name, context))
.flatMap(response -> Mono.justOrEmpty(response.value()));
}

/**
* Recovers the deleted secret in the key vault to its latest version and can only be performed on a soft-delete enabled vault.
* This operation requires the {@code secrets/recover} permission.
Expand All @@ -510,7 +615,7 @@ Mono<VoidResponse> purgeDeletedSecret(String name, Context context) {
* @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link Secret recovered secret}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<Secret>> recoverDeletedSecret(String name) {
public Mono<Response<Secret>> recoverDeletedSecretWithResponse(String name) {
return withContext(context -> recoverDeletedSecret(name, context));
}

Expand All @@ -521,6 +626,31 @@ Mono<Response<Secret>> recoverDeletedSecret(String name, Context context) {
.doOnError(error -> logger.warning("Failed to recover deleted secret - {}", name, error));
}

/**
* Requests a backup of the specified secret be downloaded to the client. All versions of the
* secret will be downloaded. This operation requires the {@code secrets/backup} permission.
*
* <p><strong>Code Samples</strong></p>
* <p>Backs up the secret from the key vault. Subscribes to the call asynchronously and prints out
* the
* length of the secret's backup byte array returned in the response.</p>
* <pre>
* secretAsyncClient.backupSecret("secretName").subscribe(secretBackupResponse -&gt;
* System.out.printf("Secret's Backup Byte array's length %s \n", secretBackupResponse.value().length));
* </pre>
*
* @param name The name of the secret.
* @return A {@link Mono} containing the backed up secret blob.
* @throws ResourceNotFoundException when a secret with {@code name} doesn't exist in the key
* vault.
* @throws HttpRequestException when a secret with {@code name} is empty string.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<byte[]> backupSecret(String name) {
return withContext(context -> backupSecret(name, context))
.flatMap(response -> Mono.justOrEmpty(response.value()));
}

/**
* Requests a backup of the specified secret be downloaded to the client. All versions of the
* secret will be downloaded. This operation requires the {@code secrets/backup} permission.
Expand All @@ -542,7 +672,7 @@ Mono<Response<Secret>> recoverDeletedSecret(String name, Context context) {
* @throws HttpRequestException when a secret with {@code name} is empty string.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<byte[]>> backupSecret(String name) {
public Mono<Response<byte[]>> backupSecretWithResponse(String name) {
return withContext(context -> backupSecret(name, context));
}

Expand All @@ -555,6 +685,30 @@ Mono<Response<byte[]>> backupSecret(String name, Context context) {
base64URLResponse.statusCode(), base64URLResponse.headers(), base64URLResponse.value().value())));
}

/**
* Restores a backed up secret, and all its versions, to a vault. This operation requires the
* {@code secrets/restore} permission.
*
* <p><strong>Code Samples</strong></p>
* <p>Restores the secret in the key vault from its backup. Subscribes to the call asynchronously
* and prints out the
* restored secret details when a response is received.</p>
* <pre>
* //Pass the Secret Backup Byte array to the restore operation.
* secretAsyncClient.restoreSecret(secretBackupByteArray).subscribe(secretResponse -&gt;
* System.out.printf("Restored Secret with name %s and value %s \n", secretResponse.value().name(), secretResponse.value().value()));
* </pre>
*
* @param backup The backup blob associated with the secret.
* @return A {@link Mono} containing the {@link Secret restored secret}.
* @throws ResourceModifiedException when {@code backup} blob is malformed.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Secret> restoreSecret(byte[] backup) {
return withContext(context -> restoreSecret(backup, context))
.flatMap(response -> Mono.justOrEmpty(response.value()));
}

/**
* Restores a backed up secret, and all its versions, to a vault. This operation requires the
* {@code secrets/restore} permission.
Expand All @@ -575,7 +729,7 @@ Mono<Response<byte[]>> backupSecret(String name, Context context) {
* @throws ResourceModifiedException when {@code backup} blob is malformed.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<Secret>> restoreSecret(byte[] backup) {
public Mono<Response<Secret>> restoreSecretWithResponse(byte[] backup) {
return withContext(context -> restoreSecret(backup, context));
}

Expand Down Expand Up @@ -729,7 +883,7 @@ PagedFlux<SecretBase> listSecretVersions(String name, Context context) {
* {@link SecretAsyncClient#listSecretVersions()}.
*
* @param continuationToken The {@link PagedResponse#nextLink()} from a previous, successful call to one of the list operations.
*
*
* @return A {@link Mono} of {@link PagedResponse<SecretBase>} from the next page of results.
*/
private Mono<PagedResponse<SecretBase>> listSecretVersionsNextPage(String continuationToken, Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ public byte[] backupSecret(String name) {
* @return A {@link Response} whose {@link Response#value() value} contains the backed up secret blob.
*/
public Response<byte[]> backupSecretWithResponse(String name, Context context) {
return client.backupSecret(name).block();
return client.backupSecret(name, context).block();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void main(String[] args) throws IOException, InterruptedException,
// Backups are good to have, if in case secrets get accidentally deleted by you.
// For long term storage, it is ideal to write the backup to a file.
String backupFilePath = "YOUR_BACKUP_FILE_PATH";
byte[] secretBackup = client.backupSecret("StorageAccountPassword").value();
byte[] secretBackup = client.backupSecret("StorageAccountPassword");
writeBackupToFile(secretBackup, backupFilePath);

// The storage account secret is no longer in use, so you delete it.
Expand All @@ -59,7 +59,7 @@ public static void main(String[] args) throws IOException, InterruptedException,

// After sometime, the secret is required again. We can use the backup value to restore it in the key vault.
byte[] backupFromFile = Files.readAllBytes(new File(backupFilePath).toPath());
Secret restoredSecret = client.restoreSecret(backupFromFile).value();
Secret restoredSecret = client.restoreSecret(backupFromFile);
}

private static void writeBackupToFile(byte[] bytes, String filePath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ public static void main(String[] args) throws IOException, InterruptedException,
secretAsyncClient.setSecret(new Secret("StorageAccountPassword", "f4G34fMh8v-fdsgjsk2323=-asdsdfsdf")
.expires(OffsetDateTime.now().plusYears(1)))
.subscribe(secretResponse ->
System.out.printf("Secret is created with name %s and value %s \n", secretResponse.value().name(), secretResponse.value().value()));
System.out.printf("Secret is created with name %s and value %s \n", secretResponse.name(), secretResponse.value()));

Thread.sleep(2000);

// Backups are good to have, if in case secrets get accidentally deleted by you.
// For long term storage, it is ideal to write the backup to a file.
String backupFilePath = "YOUR_BACKUP_FILE_PATH";
secretAsyncClient.backupSecret("StorageAccountPassword").subscribe(backupResponse -> {
byte[] backupBytes = backupResponse.value();
byte[] backupBytes = backupResponse;
writeBackupToFile(backupBytes, backupFilePath);
});

Thread.sleep(7000);

// The storage account secret is no longer in use, so you delete it.
secretAsyncClient.deleteSecret("StorageAccountPassword").subscribe(deletedSecretResponse ->
System.out.printf("Deleted Secret's Recovery Id %s \n", deletedSecretResponse.value().recoveryId()));
System.out.printf("Deleted Secret's Recovery Id %s \n", deletedSecretResponse.recoveryId()));

//To ensure file is deleted on server side.
Thread.sleep(30000);
Expand All @@ -70,7 +70,7 @@ public static void main(String[] args) throws IOException, InterruptedException,
// After sometime, the secret is required again. We can use the backup value to restore it in the key vault.
byte[] backupFromFile = Files.readAllBytes(new File(backupFilePath).toPath());
secretAsyncClient.restoreSecret(backupFromFile).subscribe(secretResponse ->
System.out.printf("Restored Secret with name %s \n", secretResponse.value().name()));
System.out.printf("Restored Secret with name %s \n", secretResponse.name()));

//To ensure secret is restored on server side.
Thread.sleep(15000);
Expand Down
Loading

0 comments on commit 0c8c38a

Please sign in to comment.