|
2 | 2 | // Licensed under the MIT License.
|
3 | 3 | package com.azure.security.keyvault.keys.cryptography.implementation;
|
4 | 4 |
|
| 5 | +import com.azure.core.exception.HttpResponseException; |
5 | 6 | import com.azure.core.util.CoreUtils;
|
6 | 7 | import com.azure.core.util.logging.ClientLogger;
|
7 | 8 | import com.azure.security.keyvault.keys.cryptography.models.EncryptionAlgorithm;
|
|
19 | 20 | import java.util.Arrays;
|
20 | 21 | import java.util.Base64;
|
21 | 22 | import java.util.List;
|
| 23 | +import java.util.Locale; |
22 | 24 | import java.util.Objects;
|
23 | 25 |
|
24 | 26 | import static com.azure.security.keyvault.keys.models.KeyType.EC;
|
|
32 | 34 | * Utility methods for the Cryptography portion of KeyVault Keys.
|
33 | 35 | */
|
34 | 36 | public final class CryptographyUtils {
|
| 37 | + private CryptographyUtils() { |
| 38 | + // No-op |
| 39 | + } |
| 40 | + |
35 | 41 | public static final String SECRETS_COLLECTION = "secrets";
|
36 | 42 |
|
37 | 43 | public static List<String> unpackAndValidateId(String keyId, ClientLogger logger) {
|
@@ -66,32 +72,44 @@ public static List<String> unpackAndValidateId(String keyId, ClientLogger logger
|
66 | 72 | }
|
67 | 73 | }
|
68 | 74 |
|
69 |
| - public static LocalKeyCryptographyClient initializeCryptoClient(JsonWebKey jsonWebKey, |
70 |
| - CryptographyClientImpl implClient, ClientLogger logger) { |
| 75 | + public static LocalKeyCryptographyClient initializeLocalClient(JsonWebKey jsonWebKey, |
| 76 | + CryptographyClientImpl implClient) { |
71 | 77 | if (!KeyType.values().contains(jsonWebKey.getKeyType())) {
|
72 |
| - throw logger.logExceptionAsError(new IllegalArgumentException(String.format( |
73 |
| - "The JSON Web Key type: %s is not supported.", jsonWebKey.getKeyType().toString()))); |
| 78 | + throw new IllegalArgumentException(String.format( |
| 79 | + "The JSON Web Key type: %s is not supported.", jsonWebKey.getKeyType().toString())); |
74 | 80 | }
|
75 | 81 |
|
76 |
| - try { |
77 |
| - if (jsonWebKey.getKeyType().equals(RSA) || jsonWebKey.getKeyType().equals(RSA_HSM)) { |
78 |
| - return new RsaKeyCryptographyClient(jsonWebKey, implClient); |
79 |
| - } else if (jsonWebKey.getKeyType().equals(EC) || jsonWebKey.getKeyType().equals(EC_HSM)) { |
80 |
| - return new EcKeyCryptographyClient(jsonWebKey, implClient); |
81 |
| - } else if (jsonWebKey.getKeyType().equals(OCT) || jsonWebKey.getKeyType().equals(OCT_HSM)) { |
82 |
| - return new AesKeyCryptographyClient(jsonWebKey, implClient); |
83 |
| - } |
84 |
| - } catch (RuntimeException e) { |
85 |
| - throw logger.logExceptionAsError(new RuntimeException("Could not initialize local cryptography client.", |
86 |
| - e)); |
| 82 | + if (jsonWebKey.getKeyType().equals(RSA) || jsonWebKey.getKeyType().equals(RSA_HSM)) { |
| 83 | + return new RsaKeyCryptographyClient(jsonWebKey, implClient); |
| 84 | + } else if (jsonWebKey.getKeyType().equals(EC) || jsonWebKey.getKeyType().equals(EC_HSM)) { |
| 85 | + return new EcKeyCryptographyClient(jsonWebKey, implClient); |
| 86 | + } else if (jsonWebKey.getKeyType().equals(OCT) || jsonWebKey.getKeyType().equals(OCT_HSM)) { |
| 87 | + return new AesKeyCryptographyClient(jsonWebKey, implClient); |
87 | 88 | }
|
88 | 89 |
|
89 |
| - // Should not reach here. |
90 |
| - return null; |
| 90 | + // Should never reach this point. |
| 91 | + throw new IllegalStateException("Could not create local cryptography client."); |
91 | 92 | }
|
92 | 93 |
|
93 |
| - public static boolean checkKeyPermissions(List<KeyOperation> operations, KeyOperation keyOperation) { |
94 |
| - return operations.contains(keyOperation); |
| 94 | + public static void verifyKeyPermissions(JsonWebKey jsonWebKey, KeyOperation keyOperation) { |
| 95 | + if (!jsonWebKey.getKeyOps().contains(keyOperation)) { |
| 96 | + throw new UnsupportedOperationException( |
| 97 | + String.format("The %s operation is not allowed for key with id: %s", |
| 98 | + keyOperation.toString().toLowerCase(Locale.ROOT), jsonWebKey.getId())); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public static boolean isThrowableRetryable(Throwable e) { |
| 103 | + if (e instanceof HttpResponseException) { |
| 104 | + int statusCode = ((HttpResponseException) e).getResponse().getStatusCode(); |
| 105 | + |
| 106 | + // Not a retriable error code. |
| 107 | + return statusCode != 501 && statusCode != 505 |
| 108 | + && (statusCode >= 500 || statusCode == 408 || statusCode == 429); |
| 109 | + } else { |
| 110 | + // Not a service-related transient error. |
| 111 | + return false; |
| 112 | + } |
95 | 113 | }
|
96 | 114 |
|
97 | 115 | /*
|
|
0 commit comments