mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 14:10:49 -05:00
fix(secretsmanager): stop CreateSecret overwriting a secret inside its recovery window (#2555)
createSecret's "already exists" guard only fired when deletedDate was null, so a name scheduled for deletion fell straight through and overwrote the recoverable secret at the same storage key. That also cleared deletedDate, so RestoreSecret then reported "was not deleted" and the original value was unrecoverable with nothing surfacing the loss. Real AWS refuses this with InvalidRequestException, which is what makes the recovery window mean anything. tagResource, untagResource, and a repeat non-force deleteSecret had the same shape - no pending-deletion check at all, so all three returned 200. The repeat delete silently moved DeletionDate forward, quietly extending a window the caller believed was counting down. All four reuse the throwIfPendingDeletion helper from #2514. deleteSecret's guard sits after the force-delete branch on purpose: force-deleting an already-scheduled secret is a legitimate "skip the window" escape hatch and stays allowed. DescribeSecret and ListSecretVersionIds are untouched - AWS doesn't declare InvalidRequestException for them, and describing a scheduled secret is how a caller reads its DeletedDate. Fixes #2549
This commit is contained in:
1 parent
ac6cb06af4
commit
1e443acd06
2 files changed
+80
-1
No files matched your search
+16
-1
@@ -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()));
|
||||
|
||||
+64
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user