Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Kumar Prabhatfd78ae4f4b47
diff --git a/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java b/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java
index d011e37926..8149478680 100644
--- a/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java
@@ -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
diff --git a/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerServiceTest.java
index d58582d05d..fa220be69e 100644
--- a/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerServiceTest.java
@@ -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