fix(secretsmanager): register AWSPENDING version before invoking rotation Lambda (#1519)

RotateSecret invoked the Lambda rotation function without first
registering the ClientRequestToken as an AWSPENDING version. Standard
rotation handlers (including the AWS-provided templates) call
DescribeSecret and expect to find their token in VersionIdsToStages,
causing rotation to fail with:

  ValueError: Secret version <token> has no stage for rotation.

Create an empty AWSPENDING placeholder version keyed by the
ClientRequestToken before invoking the Lambda's createSecret step.
This matches the behavior of real AWS and Moto.

Also allow PutSecretValue to overwrite an AWSPENDING placeholder that
has no secret value yet, since the Lambda's createSecret step needs to
populate it.
This commit is contained in:
James Netherton authored and GitHub committed 2026-06-24 22:26:44 -05:00
1 parent 72146eeb02
commit 03f1e44e27
1 file changed
+30 -6
@@ -166,13 +166,19 @@ public class SecretsManagerService {
if (clientRequestToken != null && secret.getVersions() != null && secret.getVersions().containsKey(clientRequestToken)) {
SecretVersion existingVersion = secret.getVersions().get(clientRequestToken);
if (!Objects.equals(existingVersion.getSecretString(), secretString) ||
!Objects.equals(existingVersion.getSecretBinary(), secretBinary)) {
throw new AwsException("ResourceExistsException",
"You can't use ClientRequestToken " + clientRequestToken
+ " because that value is already in use for a version of secret " + secret.getArn(), 400);
boolean isPendingPlaceholder = existingVersion.getSecretString() == null
&& existingVersion.getSecretBinary() == null
&& existingVersion.getVersionStages() != null
&& existingVersion.getVersionStages().contains("AWSPENDING");
if (!isPendingPlaceholder) {
if (!Objects.equals(existingVersion.getSecretString(), secretString) ||
!Objects.equals(existingVersion.getSecretBinary(), secretBinary)) {
throw new AwsException("ResourceExistsException",
"You can't use ClientRequestToken " + clientRequestToken
+ " because that value is already in use for a version of secret " + secret.getArn(), 400);
}
return existingVersion;
}
return existingVersion;
}
Instant now = Instant.now();
@@ -412,6 +418,24 @@ public class SecretsManagerService {
}
try {
if (!isExistingVersion) {
synchronized (lockFor(secretArn)) {
Secret secret = resolveSecret(secretArn, region);
if (secret.getVersions() != null && secret.getVersions().containsKey(clientRequestToken)) {
isExistingVersion = true;
} else {
SecretVersion pendingVersion = new SecretVersion();
pendingVersion.setVersionId(clientRequestToken);
pendingVersion.setVersionStages(List.of("AWSPENDING"));
pendingVersion.setCreatedDate(Instant.now());
if (secret.getVersions() == null) {
secret.setVersions(new java.util.HashMap<>());
}
secret.getVersions().put(clientRequestToken, pendingVersion);
store.put(regionKey(region, secret.getName()), secret);
}
}
}
if (!isExistingVersion) {
invokeRotationLambda(secretArn, clientRequestToken, lambdaArn, "createSecret", region);
invokeRotationLambda(secretArn, clientRequestToken, lambdaArn, "setSecret", region);