fix(secretsmanager): serialize DeleteSecret and a rotation's last save with the secret's other writes (#4962)

Follow-up from the review of #4873.

Every write to a secret goes through the same per-secret monitor, except two:
DeleteSecret, and the save that ends a rotation and records LastRotatedDate.
The in-memory store hands out the stored Secret object itself, so a delete
landing between that save's read and its write was undone by the write: a
secret deleted as its rotation finished came back. Both now take the secret's
lock, DeleteSecret resolving the secret again inside it as the other writes
do. A delete during the final save waits for it, and one before it makes the
rotation fail on a secret that is gone instead of restoring it.

The three rotation integration test classes also left what they created in
the shared emulator: rotation-other-region-secret, rotation-test-secret and
rotation-invalid-reference-secret under fixed names, and the rotate-* secrets
and sm-* users of the IAM-enforcement class. Each class now deletes what a test
creates after it. The IAM-enforcement class pushes each deletion as the
resource is created and runs them newest first, so a test whose setup stopped
partway deletes what it got to create, and a user's access key and inline
policy go before the user, as DeleteUser requires. A test that starts a
rotation waits, with a bounded poll, for DescribeSecret to report
LastRotatedDate before cleanup deletes the secret.

SecretsManagerWriteLockTest parks a write and checks that DeleteSecret waits
for an in-flight UpdateSecret and for a rotation's final save, and that the
secret stays deleted. Both fail on main.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-02 17:12:23 -05:00
1 parent bb3dd2de92
commit 8581497b0a
5 files changed
+208 -40

No files matched your search

@@ -673,38 +673,41 @@ public class SecretsManagerService implements ResourceProvider {
"The RecoveryWindowInDays value must be between 7 and 30 days, inclusive.", 400);
}
Secret secret = resolveSecret(secretId, region);
throwIfReplica(secret);
// AWS: "You can't delete a primary secret that is replicated to other Regions. You must
// first delete the replicas." Deleting it here would strand every replica with no primary
// to sync from and no way to remove them.
if (secret.getReplicationStatus() != null && !secret.getReplicationStatus().isEmpty()) {
throw new AwsException("InvalidRequestException",
"You can't delete a secret that is replicated to other Regions. "
+ "Remove the replicas with RemoveRegionsFromReplication first.", 400);
}
String storageKey = regionKey(region, secret.getName());
Secret resolved = resolveSecret(secretId, region);
synchronized (lockFor(resolved.getArn())) {
Secret secret = resolveSecret(resolved.getArn(), region);
throwIfReplica(secret);
// AWS: "You can't delete a primary secret that is replicated to other Regions. You must
// first delete the replicas." Deleting it here would strand every replica with no primary
// to sync from and no way to remove them.
if (secret.getReplicationStatus() != null && !secret.getReplicationStatus().isEmpty()) {
throw new AwsException("InvalidRequestException",
"You can't delete a secret that is replicated to other Regions. "
+ "Remove the replicas with RemoveRegionsFromReplication first.", 400);
}
String storageKey = regionKey(region, secret.getName());
if (forceDelete) {
store.delete(storageKey);
LOG.infov("Force-deleted secret: {0}", secret.getName());
secret.setDeletedDate(Instant.now());
if (forceDelete) {
store.delete(storageKey);
LOG.infov("Force-deleted secret: {0}", secret.getName());
secret.setDeletedDate(Instant.now());
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);
store.put(storageKey, secret);
LOG.infov("Scheduled deletion of secret: {0} at {1}", secret.getName(), deletedDate);
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);
store.put(storageKey, secret);
LOG.infov("Scheduled deletion of secret: {0} at {1}", secret.getName(), deletedDate);
return secret;
}
// ─── Multi-region replication ──────────────────────────────────────────────
@@ -1339,10 +1342,12 @@ public class SecretsManagerService implements ResourceProvider {
}
invokeRotationLambda(secretArn, clientRequestToken, lambdaArn, "testSecret", region);
invokeRotationLambda(secretArn, clientRequestToken, lambdaArn, "finishSecret", region);
Secret refreshed = resolveSecret(secretArn, region);
refreshed.setLastRotatedDate(Instant.now());
persist(refreshed, region);
synchronized (lockFor(secretArn)) {
Secret refreshed = resolveSecret(secretArn, region);
refreshed.setLastRotatedDate(Instant.now());
persist(refreshed, region);
}
} catch (Exception e) {
LOG.errorv(e, "Error during rotation steps for secret {0}", secretArn);
throw e;