fix(secretsmanager): raise InvalidRequestException, not ResourceNotFoundException, for pending-deletion secrets (#2514)

DeleteSecret without ForceDeleteWithoutRecovery keeps the secret in
store with deletedDate set to a future date (recoverable via
RestoreSecret) - it's only removed from store outright on a force
delete, so resolveSecret() already raises ResourceNotFoundException on
its own for a genuinely gone secret. Any getDeletedDate() != null
check reached past resolveSecret() therefore always means "still
present, scheduled for deletion", which real AWS reports as
InvalidRequestException.

getSecretValue, putSecretValue, updateSecret,
claimTargetAttachment, rotateSecret, and updateSecretVersionStage
all threw ResourceNotFoundException here instead, making a
recoverable, still-restorable secret indistinguishable from one
that's genuinely gone. batchGetSecretValue in the same file already
had this right (InvalidRequestException with the correct message) -
extracted that into a shared throwIfPendingDeletion(secret) helper
and applied it consistently at the six other sites.

Strengthened the existing deleteSecretWithRecoveryWindow test (it
only asserted AwsException.class, which passes regardless of error
code) to check the specific error code, and added a test covering
all six affected operations.

Fixes #2513
This commit is contained in:
Kumar Prabhat authored and GitHub committed 2026-08-25 10:00:38 -04:00
1 parent a3044e065e
commit fd78ae4f4b
2 files changed
+49 -29

No files matched your search

@@ -138,11 +138,7 @@ public class SecretsManagerService {
public SecretVersion getSecretValue(String secretId, String versionId, String versionStage, String region) {
Secret secret = resolveSecret(secretId, region);
if (secret.getDeletedDate() != null) {
throw new AwsException("ResourceNotFoundException",
"Secrets Manager can't find the specified secret.", 400);
}
throwIfPendingDeletion(secret);
SecretVersion version;
if (versionId != null && !versionId.isEmpty()) {
@@ -178,11 +174,7 @@ public class SecretsManagerService {
String secretBinary, String clientRequestToken, String region,
List<String> versionStages) {
Secret secret = resolveSecret(secretId, region);
if (secret.getDeletedDate() != null) {
throw new AwsException("ResourceNotFoundException",
"Secrets Manager can't find the specified secret.", 400);
}
throwIfPendingDeletion(secret);
if (clientRequestToken != null && (clientRequestToken.length() < 32 || clientRequestToken.length() > 64)) {
throw new AwsException("InvalidParameterException", "ClientRequestToken must be between 32 and 64 characters long.", 400);
@@ -285,11 +277,7 @@ public class SecretsManagerService {
public Secret updateSecret(String secretId, String description, String kmsKeyId, String region) {
Secret secret = resolveSecret(secretId, region);
if (secret.getDeletedDate() != null) {
throw new AwsException("ResourceNotFoundException",
"Secrets Manager can't find the specified secret.", 400);
}
throwIfPendingDeletion(secret);
if (description != null) {
secret.setDescription(description);
@@ -370,10 +358,7 @@ public class SecretsManagerService {
Secret resolved = resolveSecret(secretId, region);
synchronized (lockFor(resolved.getArn())) {
Secret secret = resolveSecret(resolved.getArn(), region);
if (secret.getDeletedDate() != null) {
throw new AwsException("ResourceNotFoundException",
"Secrets Manager can't find the specified secret.", 400);
}
throwIfPendingDeletion(secret);
String existingOwner = secret.getTargetAttachmentOwner();
if (existingOwner != null && !existingOwner.equals(attachmentOwner)) {
@@ -585,11 +570,7 @@ public class SecretsManagerService {
public Secret rotateSecret(String secretId, String clientRequestToken, String rotationLambdaArn, Secret.RotationRules rotationRules,
boolean rotateImmediately, String region) {
Secret secret = resolveSecret(secretId, region);
if (secret.getDeletedDate() != null) {
throw new AwsException("ResourceNotFoundException",
"Secrets Manager can't find the specified secret.", 400);
}
throwIfPendingDeletion(secret);
if (clientRequestToken != null && (clientRequestToken.length() < 32 || clientRequestToken.length() > 64)) {
throw new AwsException("InvalidParameterException", "ClientRequestToken must be between 32 and 64 characters long.", 400);
@@ -838,9 +819,7 @@ public class SecretsManagerService {
}
Secret secret = resolveSecret(secretId, region);
if (secret.getDeletedDate() != null) {
throw new AwsException("ResourceNotFoundException", "Secrets Manager can't find the specified secret.", 400);
}
throwIfPendingDeletion(secret);
SecretVersion versionByStage = findVersionByStage(secret, versionStage);
String currentVersionId = versionByStage != null
@@ -934,6 +913,22 @@ public class SecretsManagerService {
}
}
/**
* A secret found by {@link #resolveSecret} with {@code deletedDate} set is on the
* recovery-window path (still in {@code store}, still restorable) - {@link
* #deleteSecret} force-deletes by removing the entry from {@code store} outright, so a
* fully, permanently gone secret never reaches this check; {@code resolveSecret} throws
* ResourceNotFoundException for that case on its own. Real AWS's error for the
* recoverable case is InvalidRequestException, matching what {@code batchGetSecretValue}
* already does correctly - the other call sites threw ResourceNotFoundException instead.
*/
private void throwIfPendingDeletion(Secret secret) {
if (secret.getDeletedDate() != null) {
throw new AwsException("InvalidRequestException",
"You can't perform this operation on the secret because it was marked for deletion.", 400);
}
}
private Secret resolveSecret(String secretId, String region) {
if (secretId.startsWith("arn:")) {
// 1. Exact full-ARN match
@@ -251,9 +251,34 @@ class SecretsManagerServiceTest {
assertNotNull(deleted.getDeletedDate());
// The secret still exists but marked deleted
assertThrows(AwsException.class, () ->
// The secret still exists but marked deleted: real AWS raises InvalidRequestException
// here, distinct from the ResourceNotFoundException a genuinely absent secret gets.
// A bare assertThrows(AwsException.class, ...) doesn't distinguish the two - regression
// test for the bug where every one of these sites except batchGetSecretValue threw the
// wrong (not-found) code.
AwsException ex = assertThrows(AwsException.class, () ->
service.getSecretValue("my-secret", null, null, REGION));
assertEquals("InvalidRequestException", ex.getErrorCode());
}
@Test
void operationsOnPendingDeletionSecretRaiseInvalidRequestNotResourceNotFound() {
service.createSecret("my-secret", "value", null, null, null, null, REGION);
service.deleteSecret("my-secret", 7, false, REGION);
assertEquals("InvalidRequestException", assertThrows(AwsException.class, () ->
service.getSecretValue("my-secret", null, null, REGION)).getErrorCode());
assertEquals("InvalidRequestException", assertThrows(AwsException.class, () ->
service.putSecretValue("my-secret", "v2", null, null, REGION, null)).getErrorCode());
assertEquals("InvalidRequestException", assertThrows(AwsException.class, () ->
service.updateSecret("my-secret", "new description", null, REGION)).getErrorCode());
assertEquals("InvalidRequestException", assertThrows(AwsException.class, () ->
service.claimTargetAttachment("my-secret", "owner-1", REGION)).getErrorCode());
assertEquals("InvalidRequestException", assertThrows(AwsException.class, () ->
service.rotateSecret("my-secret", null, "arn:aws:lambda:us-east-1:000000000000:function:rotator",
null, false, REGION)).getErrorCode());
assertEquals("InvalidRequestException", assertThrows(AwsException.class, () ->
service.updateSecretVersionStage("my-secret", null, null, "AWSCURRENT", REGION)).getErrorCode());
}
@Test