Skip to content

Commit a2a59ea

Browse files
authored
Added check to ensure a secret name is not null or empty when using the getSecret operations (Azure#36617)
* Added check to ensure a secret name is not null or empty when using the `getSecret()` operation. * Addressed PR feedeback.
1 parent 1b39e6f commit a2a59ea

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

sdk/keyvault/azure-security-keyvault-secrets/src/main/java/com/azure/security/keyvault/secrets/SecretAsyncClient.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,9 @@ public Mono<Response<KeyVaultSecret>> setSecretWithResponse(KeyVaultSecret secre
214214
*
215215
* @param name The name of the secret.
216216
* @return A {@link Mono} containing the requested {@link KeyVaultSecret secret}.
217-
* @throws ResourceNotFoundException when a secret with {@code name} doesn't exist in the key vault.
218-
* @throws HttpResponseException if {@code name} is empty string.
217+
* @throws IllegalArgumentException If {@code name} is either {@code null} or empty.
218+
* @throws ResourceNotFoundException When a secret with the given {@code name} doesn't exist in the vault.
219+
* @throws HttpResponseException If the server reports an error when executing the request.
219220
*/
220221
@ServiceMethod(returns = ReturnType.SINGLE)
221222
public Mono<KeyVaultSecret> getSecret(String name) {
@@ -252,9 +253,10 @@ public Mono<KeyVaultSecret> getSecret(String name) {
252253
* call is equivalent to calling {@link #getSecret(String)}, with the latest version being retrieved.
253254
* @return A {@link Mono} containing a {@link Response} whose {@link Response#getValue() value} contains the
254255
* requested {@link KeyVaultSecret secret}.
255-
* @throws ResourceNotFoundException when a secret with {@code name} and {@code version} doesn't exist in the key
256-
* vault.
257-
* @throws HttpResponseException if {@code name} name} or {@code version} is empty string.
256+
* @throws ResourceNotFoundException When a secret with the given {@code name} and {@code version} doesn't exist in
257+
* the vault.
258+
* @throws IllegalArgumentException If {@code name} is either {@code null} or empty.
259+
* @throws HttpResponseException If the server reports an error when executing the request.
258260
*/
259261
@ServiceMethod(returns = ReturnType.SINGLE)
260262
public Mono<KeyVaultSecret> getSecret(String name, String version) {
@@ -290,9 +292,10 @@ public Mono<KeyVaultSecret> getSecret(String name, String version) {
290292
* to calling {@link #getSecret(String)}, with the latest version being retrieved.
291293
* @return A {@link Mono} containing a {@link Response} whose {@link Response#getValue() value} contains the
292294
* requested {@link KeyVaultSecret secret}.
293-
* @throws ResourceNotFoundException when a secret with {@code name} and {@code version} doesn't exist in the key
294-
* vault.
295-
* @throws HttpResponseException if {@code name} name} or {@code version} is empty string.
295+
* @throws ResourceNotFoundException When a secret with the given {@code name} and {@code version} doesn't exist in
296+
* the vault.
297+
* @throws IllegalArgumentException If {@code name} is either {@code null} or empty.
298+
* @throws HttpResponseException If the server reports an error when executing the request.
296299
*/
297300
@ServiceMethod(returns = ReturnType.SINGLE)
298301
public Mono<Response<KeyVaultSecret>> getSecretWithResponse(String name, String version) {

sdk/keyvault/azure-security-keyvault-secrets/src/main/java/com/azure/security/keyvault/secrets/SecretClient.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,9 @@ public Response<KeyVaultSecret> setSecretWithResponse(KeyVaultSecret secret, Con
163163
*
164164
* @param name The name of the secret.
165165
* @return The requested {@link KeyVaultSecret}.
166-
* @throws ResourceNotFoundException when a secret with {@code name} doesn't exist in the key vault.
167-
* @throws HttpResponseException if {@code name} is empty string.
166+
* @throws ResourceNotFoundException When a secret with the given {@code name} doesn't exist in the vault.
167+
* @throws IllegalArgumentException If {@code name} is either {@code null} or empty.
168+
* @throws HttpResponseException If the server reports an error when executing the request.
168169
*/
169170
@ServiceMethod(returns = ReturnType.SINGLE)
170171
public KeyVaultSecret getSecret(String name) {
@@ -190,9 +191,10 @@ public KeyVaultSecret getSecret(String name) {
190191
* @param version The version of the secret to retrieve. If this is an empty string or null, this call is
191192
* equivalent to calling {@link #getSecret(String)}, with the latest version being retrieved.
192193
* @return The requested {@link KeyVaultSecret secret}.
193-
* @throws ResourceNotFoundException when a secret with {@code name} and {@code version} doesn't exist in the key
194-
* vault.
195-
* @throws HttpResponseException if {@code name} or {@code version} is empty string.
194+
* @throws ResourceNotFoundException When a secret with the given {@code name} and {@code version} doesn't exist in
195+
* the vault.
196+
* @throws IllegalArgumentException If {@code name} is either {@code null} or empty.
197+
* @throws HttpResponseException If the server reports an error when executing the request.
196198
*/
197199
@ServiceMethod(returns = ReturnType.SINGLE)
198200
public KeyVaultSecret getSecret(String name, String version) {
@@ -220,9 +222,9 @@ public KeyVaultSecret getSecret(String name, String version) {
220222
* to calling {@link #getSecret(String)}, with the latest version being retrieved.
221223
* @param context Additional context that is passed through the HTTP pipeline during the service call.
222224
* @return A {@link Response} whose {@link Response#getValue() value} contains the requested {@link KeyVaultSecret}.
223-
* @throws ResourceNotFoundException when a secret with {@code name} and {@code version} doesn't exist in the key
224-
* vault.
225-
* @throws HttpResponseException if {@code name} or {@code version} is empty string.
225+
* @throws ResourceNotFoundException When a secret with the given {@code name} and {@code version} doesn't exist in
226+
* the vault.
227+
* @throws IllegalArgumentException If {@code name} is either {@code null} or empty.
226228
*/
227229
@ServiceMethod(returns = ReturnType.SINGLE)
228230
public Response<KeyVaultSecret> getSecretWithResponse(String name, String version, Context context) {

sdk/keyvault/azure-security-keyvault-secrets/src/main/java/com/azure/security/keyvault/secrets/implementation/SecretClientImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.azure.core.http.rest.RestProxy;
3131
import com.azure.core.http.rest.SimpleResponse;
3232
import com.azure.core.util.Context;
33+
import com.azure.core.util.CoreUtils;
3334
import com.azure.core.util.logging.ClientLogger;
3435
import com.azure.core.util.polling.LongRunningOperationStatus;
3536
import com.azure.core.util.polling.PollResponse;
@@ -532,6 +533,10 @@ public Mono<Response<KeyVaultSecret>> getSecretWithResponseAsync(String name, St
532533
}
533534

534535
public Response<KeyVaultSecret> getSecretWithResponse(String name, String version, Context context) {
536+
if (CoreUtils.isNullOrEmpty(name)) {
537+
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'name' cannot be null or empty."));
538+
}
539+
535540
context = context == null ? Context.NONE : context;
536541
context = enableSyncRestProxy(context);
537542

0 commit comments

Comments
 (0)