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

@@ -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