fix(secretsmanager): keep the versions map safe to iterate during a rotation (#3754)

RotateSecret stages the new version from the rotation executor while a
request thread answering DescribeSecret, GetSecretValue or
ListSecretVersionIds iterates the same Secret.versions map. The map was a
plain HashMap, so a put landing during that iteration failed the request
with a ConcurrentModificationException. It surfaced as a flaky
SecretsManagerJsonHandlerTest.rotateSecretParsesRotationRules on main, and
any client polling a secret during its rotation could hit it, where AWS
always answers with a consistent version set.

The writes already run under the per-secret lock from #3247; the reads
deliberately do not, because a describe never waits on a rotation, as on
AWS. A fail-fast map cannot support that contract. Secret.setVersions now
copies a plain map into a ConcurrentHashMap and installs a ConcurrentMap
as given, which covers createSecret, the rotation lifecycle and Jackson on
reload from persistence. Iteration is weakly consistent: no exception, and
a reader sees the version set as of before or after the staging put.

A deterministic regression parks the handler's DescribeSecret one entry
into its iteration, stages a version, and resumes it; it throws on main and
passes here. The existing BlockingVersions test helper extends
ConcurrentHashMap so the setter installs it unchanged.

Closes #3753
This commit is contained in:
David authored and GitHub committed 2026-09-16 16:12:32 -04:00
1 parent 1ae5c22f83
commit 0578532851
2 files changed
+160 -3

No files matched your search

@@ -7,6 +7,8 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
@@ -152,8 +154,17 @@ public class Secret {
return versions;
}
/**
* A rotation adds versions from the rotation executor while request threads iterate this map
* to answer DescribeSecret, GetSecretValue and ListSecretVersionIds, and those reads do not
* take the rotation lock (a describe never waits on a rotation, as on AWS). The map must
* therefore never be fail-fast: a plain map, which is what Jackson and most callers hand in,
* is copied into a concurrent one; a concurrent map is installed as given.
*/
public void setVersions(Map<String, SecretVersion> versions) {
this.versions = versions;
this.versions = versions == null || versions instanceof ConcurrentMap<String, SecretVersion>
? versions
: new ConcurrentHashMap<>(versions);
}
public String getCurrentVersionId() {