Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(secretsmanager): stop CreateSecret overwriting a secret inside its recovery window (#2555)

Kumar Prabhat1e443acd0614
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 04b1d44d05..fc73c8cc8b 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
@@ -104,7 +104,13 @@ public class SecretsManagerService implements ResourceProvider {
String storageKey = regionKey(region, name);
Secret existing = store.get(storageKey).orElse(null);
- if (existing != null && existing.getDeletedDate() == null) {
+ if (existing != null) {
+ // A name inside its recovery window is still taken: the secret is recoverable via
+ // RestoreSecret, so reusing the name has to be refused rather than allowed to
+ // overwrite it. Without this the create would replace the stored secret at the same
+ // key AND clear its deletedDate, so the original value became unrecoverable and
+ // RestoreSecret then reported "was not deleted" - silent data loss.
+ throwIfPendingDeletion(existing);
throw new AwsException("ResourceExistsException",
"A secret with the name " + name + " already exists.", 400);
}
@@ -549,6 +555,13 @@ public class SecretsManagerService implements ResourceProvider {
return secret;
}
+ // Guard placed AFTER the force-delete branch on purpose: force-deleting a secret that is
+ // already inside its recovery window is a legitimate "skip the window, remove it now"
+ // escape hatch, so only the scheduling path is refused. Re-scheduling an already-scheduled
+ // secret silently moved its DeletionDate, which would quietly extend a window a caller
+ // believed was already counting down.
+ throwIfPendingDeletion(secret);
+
int windowDays = (recoveryWindowInDays != null) ? recoveryWindowInDays : defaultRecoveryWindowDays;
Instant deletedDate = Instant.now().plusSeconds((long) windowDays * 86400);
secret.setDeletedDate(deletedDate);
@@ -739,6 +752,7 @@ public class SecretsManagerService implements ResourceProvider {
public void tagResource(String secretId, List<Secret.Tag> tags, String region) {
Secret secret = resolveSecret(secretId, region);
+ throwIfPendingDeletion(secret);
List<Secret.Tag> existing = secret.getTags() != null ? new ArrayList<>(secret.getTags()) : new ArrayList<>();
for (Secret.Tag newTag : tags) {
@@ -751,6 +765,7 @@ public class SecretsManagerService implements ResourceProvider {
public void untagResource(String secretId, List<String> tagKeys, String region) {
Secret secret = resolveSecret(secretId, region);
+ throwIfPendingDeletion(secret);
List<Secret.Tag> existing = secret.getTags() != null ? new ArrayList<>(secret.getTags()) : new ArrayList<>();
existing.removeIf(t -> tagKeys.contains(t.key()));
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 fa220be69e..77d65a3ef8 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
@@ -281,6 +281,70 @@ class SecretsManagerServiceTest {
service.updateSecretVersionStage("my-secret", null, null, "AWSCURRENT", REGION)).getErrorCode());
}
+ /**
+ * The data-loss case from #2549: CreateSecret against a name inside its recovery window used
+ * to fall through the "already exists" guard (which only fired when {@code deletedDate} was
+ * null), overwrite the recoverable secret at the same storage key, and clear its
+ * {@code deletedDate} as a side effect. The original value became unrecoverable and
+ * RestoreSecret then reported "was not deleted", so nothing surfaced the loss.
+ */
+ @Test
+ void createSecretOnPendingDeletionNameIsRefusedAndLeavesOriginalRecoverable() {
+ service.createSecret("my-secret", "ORIGINAL-VALUE", null, null, null, null, REGION);
+ service.deleteSecret("my-secret", 7, false, REGION);
+
+ assertEquals("InvalidRequestException", assertThrows(AwsException.class, () ->
+ service.createSecret("my-secret", "NEW-VALUE", null, null, null, null, REGION)).getErrorCode());
+
+ // The recovery window still means what it says: restore works and returns the original.
+ service.restoreSecret("my-secret", REGION);
+ assertEquals("ORIGINAL-VALUE",
+ service.getSecretValue("my-secret", null, null, REGION).getSecretString());
+ }
+
+ @Test
+ void tagUntagAndRescheduleOnPendingDeletionSecretRaiseInvalidRequest() {
+ service.createSecret("my-secret", "value", null, null, null, null, REGION);
+ service.deleteSecret("my-secret", 7, false, REGION);
+
+ assertEquals("InvalidRequestException", assertThrows(AwsException.class, () ->
+ service.tagResource("my-secret", List.of(new Secret.Tag("k", "v")), REGION)).getErrorCode());
+ assertEquals("InvalidRequestException", assertThrows(AwsException.class, () ->
+ service.untagResource("my-secret", List.of("k"), REGION)).getErrorCode());
+ // A second non-force DeleteSecret used to silently move DeletionDate forward.
+ assertEquals("InvalidRequestException", assertThrows(AwsException.class, () ->
+ service.deleteSecret("my-secret", 7, false, REGION)).getErrorCode());
+ }
+
+ /**
+ * Force delete stays available as the documented "skip the recovery window" escape hatch, so
+ * the guard added above must not block it. Pins the guard's placement after the force branch.
+ */
+ @Test
+ void forceDeleteStillWorksOnAPendingDeletionSecret() {
+ service.createSecret("my-secret", "value", null, null, null, null, REGION);
+ service.deleteSecret("my-secret", 7, false, REGION);
+
+ service.deleteSecret("my-secret", null, true, REGION);
+
+ assertEquals("ResourceNotFoundException", assertThrows(AwsException.class, () ->
+ service.describeSecret("my-secret", REGION)).getErrorCode());
+ }
+
+ /**
+ * DescribeSecret is how a caller reads a scheduled secret's DeletedDate, and AWS does not
+ * declare InvalidRequestException for it. Pins that the guard was not over-applied.
+ */
+ @Test
+ void describeSecretRemainsAllowedOnPendingDeletionSecret() {
+ service.createSecret("my-secret", "value", null, null, null, null, REGION);
+ service.deleteSecret("my-secret", 7, false, REGION);
+
+ Secret described = service.describeSecret("my-secret", REGION);
+ assertEquals("my-secret", described.getName());
+ assertNotNull(described.getDeletedDate());
+ }
+
@Test
void forceDeleteSecret() {
service.createSecret("my-secret", "value", null, null, null, null, REGION);