Skip to content

Commit 264c384

Browse files
authored
Applied APIView feedback for KV Administration and Keys (Azure#22360)
* Applied APIView feedback for Key Vault Administration. * Applied APIView feedback for Key Vault Keys.
1 parent 5e7f1b2 commit 264c384

File tree

21 files changed

+315
-127
lines changed

21 files changed

+315
-127
lines changed

sdk/keyvault/azure-security-keyvault-administration/src/main/java/com/azure/security/keyvault/administration/implementation/KeyVaultAdministrationUtils.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
package com.azure.security.keyvault.administration.implementation;
55

66
import com.azure.security.keyvault.administration.implementation.models.Error;
7+
import com.azure.security.keyvault.administration.implementation.models.KeyVaultError;
78
import com.azure.security.keyvault.administration.implementation.models.KeyVaultErrorException;
8-
import com.azure.security.keyvault.administration.models.KeyVaultError;
9+
import com.azure.security.keyvault.administration.models.KeyVaultAdministrationError;
910
import com.azure.security.keyvault.administration.models.KeyVaultAdministrationException;
1011

1112
/**
@@ -33,16 +34,13 @@ public static KeyVaultAdministrationException toKeyVaultAdministrationException(
3334
}
3435

3536
/**
36-
* Convert an implementation {@link com.azure.security.keyvault.administration.implementation.models.KeyVaultError}
37-
* to a public {@link KeyVaultError}.
37+
* Convert an implementation {@link KeyVaultError} to a public {@link KeyVaultAdministrationError}.
3838
*
39-
* @param keyVaultError The {@link com.azure.security.keyvault.administration.implementation.models.KeyVaultError}
40-
* returned by the service.
39+
* @param keyVaultError The {@link KeyVaultError} returned by the service.
4140
*
42-
* @return An instance of the public {@link KeyVaultError}.
41+
* @return An instance of the public {@link KeyVaultAdministrationError}.
4342
*/
44-
public static KeyVaultError toKeyVaultError(
45-
com.azure.security.keyvault.administration.implementation.models.KeyVaultError keyVaultError) {
43+
public static KeyVaultAdministrationError toKeyVaultError(KeyVaultError keyVaultError) {
4644

4745
if (keyVaultError == null) {
4846
return null;
@@ -52,21 +50,19 @@ public static KeyVaultError toKeyVaultError(
5250
}
5351

5452
/**
55-
* Convert an error {@link Error} internal to an implementation
56-
* {@link com.azure.security.keyvault.administration.implementation.models.KeyVaultError} to a public
57-
* {@link KeyVaultError}.
53+
* Convert an error {@link Error} internal to an implementation {@link KeyVaultError} to a public
54+
* {@link KeyVaultAdministrationError}.
5855
*
59-
* @param error The {@link Error} internal to an implementation
60-
* {@link com.azure.security.keyvault.administration.implementation.models.KeyVaultError} returned by the service.
56+
* @param error The {@link Error} internal to an implementation {@link KeyVaultError} returned by the service.
6157
*
62-
* @return An instance of the public {@link KeyVaultError}.
58+
* @return An instance of the public {@link KeyVaultAdministrationError}.
6359
*/
64-
public static KeyVaultError createKeyVaultErrorFromError(Error error) {
60+
public static KeyVaultAdministrationError createKeyVaultErrorFromError(Error error) {
6561
if (error == null) {
6662
return null;
6763
}
6864

69-
return new KeyVaultError(error.getCode(), error.getMessage(),
65+
return new KeyVaultAdministrationError(error.getCode(), error.getMessage(),
7066
createKeyVaultErrorFromError(error.getInnerError()));
7167
}
7268

sdk/keyvault/azure-security-keyvault-administration/src/main/java/com/azure/security/keyvault/administration/models/KeyVaultError.java renamed to sdk/keyvault/azure-security-keyvault-administration/src/main/java/com/azure/security/keyvault/administration/models/KeyVaultAdministrationError.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
* A class that represents an error occurred in a Key Vault operation.
1010
*/
1111
@Immutable
12-
public final class KeyVaultError {
12+
public final class KeyVaultAdministrationError {
1313
private final String code;
1414
private final String message;
15-
private final KeyVaultError innerError;
15+
private final KeyVaultAdministrationError innerError;
1616

1717
/**
1818
* Creates an object that represents an error occurred in a Key Vault operation.
@@ -21,7 +21,7 @@ public final class KeyVaultError {
2121
* @param message The error message.
2222
* @param innerError An Key Vault server-side error.
2323
*/
24-
public KeyVaultError(String code, String message, KeyVaultError innerError) {
24+
public KeyVaultAdministrationError(String code, String message, KeyVaultAdministrationError innerError) {
2525
this.code = code;
2626
this.message = message;
2727
this.innerError = innerError;
@@ -50,7 +50,7 @@ public String getMessage() {
5050
*
5151
* @return The Key Vault server-side error.
5252
*/
53-
public KeyVaultError getInnerError() {
53+
public KeyVaultAdministrationError getInnerError() {
5454
return this.innerError;
5555
}
5656
}

sdk/keyvault/azure-security-keyvault-administration/src/main/java/com/azure/security/keyvault/administration/models/KeyVaultAdministrationException.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import com.azure.core.http.HttpResponse;
99

1010
/**
11-
* Exception thrown for an invalid response with {@link KeyVaultError} information.
11+
* Exception thrown for an invalid response with {@link KeyVaultAdministrationError} information.
1212
*/
1313
@Immutable
1414
public final class KeyVaultAdministrationException extends HttpResponseException {
@@ -29,12 +29,12 @@ public KeyVaultAdministrationException(String message, HttpResponse response) {
2929
* @param response The HTTP response.
3030
* @param value The deserialized response value.
3131
*/
32-
public KeyVaultAdministrationException(String message, HttpResponse response, KeyVaultError value) {
32+
public KeyVaultAdministrationException(String message, HttpResponse response, KeyVaultAdministrationError value) {
3333
super(message, response, value);
3434
}
3535

3636
@Override
37-
public KeyVaultError getValue() {
38-
return (KeyVaultError) super.getValue();
37+
public KeyVaultAdministrationError getValue() {
38+
return (KeyVaultAdministrationError) super.getValue();
3939
}
4040
}

sdk/keyvault/azure-security-keyvault-administration/src/main/java/com/azure/security/keyvault/administration/models/KeyVaultBackupOperation.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ public final class KeyVaultBackupOperation extends KeyVaultLongRunningOperation
2222
* @param error Error encountered, if any, during the {@link KeyVaultBackupOperation}.
2323
* @param startTime The start time of the {@link KeyVaultBackupOperation}.
2424
* @param endTime The end time of the {@link KeyVaultBackupOperation}.
25-
* @param jobId Identifier for the full {@link KeyVaultBackupOperation}.
25+
* @param operationId Identifier for the full {@link KeyVaultBackupOperation}.
2626
* @param azureStorageBlobContainerUrl The Azure blob storage container URI which contains the backup.
2727
*/
28-
public KeyVaultBackupOperation(String status, String statusDetails, KeyVaultError error, String jobId, OffsetDateTime startTime, OffsetDateTime endTime, String azureStorageBlobContainerUrl) {
29-
super(status, statusDetails, error, jobId, startTime, endTime);
28+
public KeyVaultBackupOperation(String status, String statusDetails, KeyVaultAdministrationError error,
29+
String operationId, OffsetDateTime startTime, OffsetDateTime endTime,
30+
String azureStorageBlobContainerUrl) {
31+
super(status, statusDetails, error, operationId, startTime, endTime);
3032
this.azureStorageBlobContainerUrl = azureStorageBlobContainerUrl;
3133
}
3234

sdk/keyvault/azure-security-keyvault-administration/src/main/java/com/azure/security/keyvault/administration/models/KeyVaultLongRunningOperation.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
public class KeyVaultLongRunningOperation {
1515
private final String status;
1616
private final String statusDetails;
17-
private final KeyVaultError error;
17+
private final KeyVaultAdministrationError error;
1818
private final String operationId;
1919
private final OffsetDateTime startTime;
2020
private final OffsetDateTime endTime;
@@ -29,7 +29,8 @@ public class KeyVaultLongRunningOperation {
2929
* @param startTime The start time of the {@link KeyVaultLongRunningOperation}.
3030
* @param endTime The end time of the {@link KeyVaultLongRunningOperation}.
3131
*/
32-
public KeyVaultLongRunningOperation(String status, String statusDetails, KeyVaultError error, String operationId, OffsetDateTime startTime, OffsetDateTime endTime) {
32+
public KeyVaultLongRunningOperation(String status, String statusDetails, KeyVaultAdministrationError error,
33+
String operationId, OffsetDateTime startTime, OffsetDateTime endTime) {
3334
this.status = status;
3435
this.statusDetails = statusDetails;
3536
this.error = error;
@@ -70,7 +71,7 @@ public String getOperationId() {
7071
*
7172
* @return The error.
7273
*/
73-
public KeyVaultError getError() {
74+
public KeyVaultAdministrationError getError() {
7475
return error;
7576
}
7677

sdk/keyvault/azure-security-keyvault-administration/src/main/java/com/azure/security/keyvault/administration/models/KeyVaultRestoreOperation.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public final class KeyVaultRestoreOperation extends KeyVaultLongRunningOperation
2020
* @param error Error encountered, if any, during the {@link KeyVaultRestoreOperation}.
2121
* @param startTime The start time of the {@link KeyVaultRestoreOperation}.
2222
* @param endTime The end time of the {@link KeyVaultRestoreOperation}.
23-
* @param jobId Identifier for the full {@link KeyVaultRestoreOperation}.
23+
* @param operationId Identifier for the full {@link KeyVaultRestoreOperation}.
2424
*/
25-
public KeyVaultRestoreOperation(String status, String statusDetails, KeyVaultError error, String jobId,
26-
OffsetDateTime startTime, OffsetDateTime endTime) {
27-
super(status, statusDetails, error, jobId, startTime, endTime);
25+
public KeyVaultRestoreOperation(String status, String statusDetails, KeyVaultAdministrationError error,
26+
String operationId, OffsetDateTime startTime, OffsetDateTime endTime) {
27+
super(status, statusDetails, error, operationId, startTime, endTime);
2828
}
2929
}

sdk/keyvault/azure-security-keyvault-administration/src/main/java/com/azure/security/keyvault/administration/models/KeyVaultRoleDefinition.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ public final class KeyVaultRoleDefinition {
2828
* @param name The name for this {@link KeyVaultRoleDefinition role definition}.
2929
* @param type The type of this {@link KeyVaultRoleDefinition role definition}.
3030
* @param roleName The name of the role.
31-
* @param roleDescription The description of the role.
31+
* @param description The description of this {@link KeyVaultRoleDefinition role definition}.
3232
* @param roleType The type of the role.
3333
* @param permissions The {@link KeyVaultPermission permissions} the {@link KeyVaultRoleDefinition role definition}
3434
* has.
3535
* @param assignableScopes The assignable scopes of the {@link KeyVaultRoleDefinition role definition}.
3636
*/
3737
public KeyVaultRoleDefinition(String id, String name, KeyVaultRoleDefinitionType type, String roleName,
38-
String roleDescription, KeyVaultRoleType roleType,
39-
List<KeyVaultPermission> permissions, List<KeyVaultRoleScope> assignableScopes) {
38+
String description, KeyVaultRoleType roleType, List<KeyVaultPermission> permissions,
39+
List<KeyVaultRoleScope> assignableScopes) {
4040
this.id = id;
4141
this.name = name;
4242
this.type = type;
4343
this.roleName = roleName;
44-
this.description = roleDescription;
44+
this.description = description;
4545
this.roleType = roleType;
4646
this.permissions = permissions;
4747
this.assignableScopes = assignableScopes;

sdk/keyvault/azure-security-keyvault-administration/src/main/java/com/azure/security/keyvault/administration/models/KeyVaultSelectiveKeyRestoreOperation.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public final class KeyVaultSelectiveKeyRestoreOperation extends KeyVaultLongRunn
2020
* @param error Error encountered, if any, during the {@link KeyVaultSelectiveKeyRestoreOperation}.
2121
* @param startTime The start time of the {@link KeyVaultSelectiveKeyRestoreOperation}.
2222
* @param endTime The end time of the {@link KeyVaultSelectiveKeyRestoreOperation}.
23-
* @param jobId Identifier for the full {@link KeyVaultSelectiveKeyRestoreOperation}.
23+
* @param operationId Identifier for the full {@link KeyVaultSelectiveKeyRestoreOperation}.
2424
*/
25-
public KeyVaultSelectiveKeyRestoreOperation(String status, String statusDetails, KeyVaultError error, String jobId,
26-
OffsetDateTime startTime, OffsetDateTime endTime) {
27-
super(status, statusDetails, error, jobId, startTime, endTime);
25+
public KeyVaultSelectiveKeyRestoreOperation(String status, String statusDetails, KeyVaultAdministrationError error,
26+
String operationId, OffsetDateTime startTime, OffsetDateTime endTime) {
27+
super(status, statusDetails, error, operationId, startTime, endTime);
2828
}
2929
}

sdk/keyvault/azure-security-keyvault-keys/src/main/java/com/azure/security/keyvault/keys/cryptography/AesKeyCryptographyClient.java

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
import com.azure.core.util.Context;
77
import com.azure.core.util.logging.ClientLogger;
8+
import com.azure.security.keyvault.keys.cryptography.models.DecryptParameters;
89
import com.azure.security.keyvault.keys.cryptography.models.DecryptResult;
10+
import com.azure.security.keyvault.keys.cryptography.models.EncryptParameters;
911
import com.azure.security.keyvault.keys.cryptography.models.EncryptResult;
1012
import com.azure.security.keyvault.keys.cryptography.models.EncryptionAlgorithm;
1113
import com.azure.security.keyvault.keys.cryptography.models.KeyWrapAlgorithm;
@@ -49,21 +51,40 @@ private byte[] getKey(JsonWebKey key) {
4951
return this.key;
5052
}
5153

54+
55+
56+
@Override
57+
Mono<EncryptResult> encryptAsync(EncryptionAlgorithm algorithm, byte[] plaintext, Context context,
58+
JsonWebKey jsonWebKey) {
59+
return encryptInternal(algorithm, plaintext, null, null, context, jsonWebKey);
60+
}
61+
5262
@Override
5363
Mono<EncryptResult> encryptAsync(EncryptParameters encryptParameters, Context context, JsonWebKey jsonWebKey) {
54-
if (isGcm(encryptParameters.getAlgorithm())) {
64+
return encryptInternal(encryptParameters.getAlgorithm(), encryptParameters.getPlainText(),
65+
encryptParameters.getIv(), encryptParameters.getAdditionalAuthenticatedData(), context, jsonWebKey);
66+
}
67+
68+
private Mono<EncryptResult> encryptInternal(EncryptionAlgorithm algorithm, byte[] plaintext, byte[] iv,
69+
byte[] additionalAuthenticatedData, Context context,
70+
JsonWebKey jsonWebKey) {
71+
if (isGcm(algorithm)) {
5572
return Mono.error(
5673
new UnsupportedOperationException("AES-GCM is not supported for local cryptography operations."));
5774
}
5875

76+
if (!isAes(algorithm)) {
77+
throw logger.logExceptionAsError(
78+
new IllegalStateException("Encryption algorithm provided is not supported: " + algorithm));
79+
}
80+
5981
this.key = getKey(jsonWebKey);
6082

6183
if (key == null || key.length == 0) {
6284
throw logger.logExceptionAsError(new IllegalArgumentException("Key is empty."));
6385
}
6486

6587
// Interpret the algorithm
66-
EncryptionAlgorithm algorithm = encryptParameters.getAlgorithm();
6788
Algorithm baseAlgorithm = AlgorithmResolver.DEFAULT.get(algorithm.toString());
6889

6990
if (!(baseAlgorithm instanceof SymmetricEncryptionAlgorithm)) {
@@ -74,9 +95,6 @@ Mono<EncryptResult> encryptAsync(EncryptParameters encryptParameters, Context co
7495

7596
ICryptoTransform transform;
7697

77-
byte[] iv = encryptParameters.getIv();
78-
byte[] additionalAuthenticatedData = encryptParameters.getAdditionalAuthenticatedData();
79-
8098
if (iv == null) {
8199
if (isAes(algorithm)) {
82100
iv = generateRandomByteArray(AES_BLOCK_SIZE);
@@ -93,42 +111,51 @@ Mono<EncryptResult> encryptAsync(EncryptParameters encryptParameters, Context co
93111
return Mono.error(e);
94112
}
95113

96-
byte[] encrypted;
114+
byte[] ciphertext;
97115

98116
try {
99-
encrypted = transform.doFinal(encryptParameters.getPlainText());
117+
ciphertext = transform.doFinal(plaintext);
100118
} catch (Exception e) {
101119
return Mono.error(e);
102120
}
103121

104-
byte[] ciphertext;
105-
106-
if (isAes(algorithm)) {
107-
ciphertext = encrypted;
108-
} else {
109-
throw logger.logExceptionAsError(
110-
new IllegalStateException("Encryption algorithm provided is not supported: " + algorithm));
111-
}
112-
113122
return Mono.just(new EncryptResult(ciphertext, algorithm, jsonWebKey.getId(), iv, null,
114123
additionalAuthenticatedData));
115124
}
116125

126+
@Override
127+
Mono<DecryptResult> decryptAsync(EncryptionAlgorithm algorithm, byte[] ciphertext, Context context,
128+
JsonWebKey jsonWebKey) {
129+
return decryptInternal(algorithm, ciphertext, null, null, null, context, jsonWebKey);
130+
}
131+
117132
@Override
118133
Mono<DecryptResult> decryptAsync(DecryptParameters decryptParameters, Context context, JsonWebKey jsonWebKey) {
119-
if (isGcm(decryptParameters.getAlgorithm())) {
134+
return decryptInternal(decryptParameters.getAlgorithm(), decryptParameters.getCipherText(),
135+
decryptParameters.getIv(), decryptParameters.getAdditionalAuthenticatedData(),
136+
decryptParameters.getAuthenticationTag(), context, jsonWebKey);
137+
}
138+
139+
private Mono<DecryptResult> decryptInternal(EncryptionAlgorithm algorithm, byte[] ciphertext, byte[] iv,
140+
byte[] additionalAuthenticatedData, byte[] authenticationTag,
141+
Context context, JsonWebKey jsonWebKey) {
142+
if (isGcm(algorithm)) {
120143
return Mono.error(
121144
new UnsupportedOperationException("AES-GCM is not supported for local cryptography operations."));
122145
}
123146

147+
if (!isAes(algorithm)) {
148+
throw logger.logExceptionAsError(
149+
new IllegalStateException("Encryption algorithm provided is not supported: " + algorithm));
150+
}
151+
124152
this.key = getKey(jsonWebKey);
125153

126154
if (key == null || key.length == 0) {
127155
throw logger.logExceptionAsError(new IllegalArgumentException("Key is empty."));
128156
}
129157

130158
// Interpret the algorithm
131-
EncryptionAlgorithm algorithm = decryptParameters.getAlgorithm();
132159
Algorithm baseAlgorithm = AlgorithmResolver.DEFAULT.get(algorithm.toString());
133160

134161
if (!(baseAlgorithm instanceof SymmetricEncryptionAlgorithm)) {
@@ -139,10 +166,7 @@ Mono<DecryptResult> decryptAsync(DecryptParameters decryptParameters, Context co
139166

140167
ICryptoTransform transform;
141168

142-
byte[] iv = Objects.requireNonNull(decryptParameters.getIv(),
143-
"Initialization vector cannot be null in local decryption operations.");
144-
byte[] additionalAuthenticatedData = decryptParameters.getAdditionalAuthenticatedData();
145-
byte[] authenticationTag = decryptParameters.getAuthenticationTag();
169+
Objects.requireNonNull(iv, "'iv' cannot be null in local decryption operations.");
146170

147171
try {
148172
transform = symmetricEncryptionAlgorithm.createDecryptor(this.key, iv, additionalAuthenticatedData,
@@ -151,23 +175,15 @@ Mono<DecryptResult> decryptAsync(DecryptParameters decryptParameters, Context co
151175
return Mono.error(e);
152176
}
153177

154-
byte[] decrypted;
155-
byte[] ciphertext;
156-
157-
if (isAes(algorithm)) {
158-
ciphertext = decryptParameters.getCipherText();
159-
} else {
160-
throw logger.logExceptionAsError(
161-
new IllegalStateException("Encryption algorithm provided is not supported: " + algorithm));
162-
}
178+
byte[] plaintext;
163179

164180
try {
165-
decrypted = transform.doFinal(ciphertext);
181+
plaintext = transform.doFinal(ciphertext);
166182
} catch (Exception e) {
167183
return Mono.error(e);
168184
}
169185

170-
return Mono.just(new DecryptResult(decrypted, algorithm, jsonWebKey.getId()));
186+
return Mono.just(new DecryptResult(plaintext, algorithm, jsonWebKey.getId()));
171187
}
172188

173189
@Override

0 commit comments

Comments
 (0)