mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(secretsmanager): keep a version written under the rotation token during the test step (#7663)
This commit is contained in:
1 parent
c3058a8de4
commit
44565360e2
2 files changed
+67
-9
No files matched your search
+29
-9
@@ -1700,17 +1700,32 @@ public class SecretsManagerService implements ResourceProvider {
|
||||
invokeRotationLambda(secretArn, clientRequestToken, lambdaArn, "testSecret", region);
|
||||
} finally {
|
||||
if (staged) {
|
||||
removeVersion(secretArn, clientRequestToken, region);
|
||||
removeUntouchedPlaceholder(secretArn, clientRequestToken, region);
|
||||
}
|
||||
}
|
||||
|
||||
synchronized (lockFor(secretArn)) {
|
||||
// Only this rotation's own version, or the current one a finished rotation left
|
||||
// AWSPENDING on: a version another rotation or client staged keeps its stage.
|
||||
Secret refreshed = resolveSecret(secretArn, region);
|
||||
SecretVersion pending = findVersionByStage(refreshed, "AWSPENDING");
|
||||
if (pending != null) {
|
||||
List<String> stages = new ArrayList<>(pending.getVersionStages());
|
||||
stages.remove("AWSPENDING");
|
||||
pending.setVersionStages(stages);
|
||||
List<SecretVersion> retirable = new ArrayList<>();
|
||||
if (refreshed.getVersions() != null && refreshed.getVersions().containsKey(clientRequestToken)) {
|
||||
retirable.add(refreshed.getVersions().get(clientRequestToken));
|
||||
}
|
||||
SecretVersion current = findVersionByStage(refreshed, AWSCURRENT);
|
||||
if (current != null) {
|
||||
retirable.add(current);
|
||||
}
|
||||
boolean retired = false;
|
||||
for (SecretVersion version : retirable) {
|
||||
if (version.getVersionStages() != null && version.getVersionStages().contains("AWSPENDING")) {
|
||||
List<String> stages = new ArrayList<>(version.getVersionStages());
|
||||
stages.remove("AWSPENDING");
|
||||
version.setVersionStages(stages);
|
||||
retired = true;
|
||||
}
|
||||
}
|
||||
if (retired) {
|
||||
persist(refreshed, region);
|
||||
}
|
||||
}
|
||||
@@ -1762,12 +1777,17 @@ public class SecretsManagerService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private void removeVersion(String secretArn, String versionId, String region) {
|
||||
/** Removes the version {@link #stagePendingVersion} added, unless it has since gained a value or another stage. */
|
||||
private void removeUntouchedPlaceholder(String secretArn, String versionId, String region) {
|
||||
synchronized (lockFor(secretArn)) {
|
||||
Secret secret = resolveSecret(secretArn, region);
|
||||
if (secret.getVersions() != null && secret.getVersions().remove(versionId) != null) {
|
||||
persist(secret, region);
|
||||
SecretVersion version = secret.getVersions() == null ? null : secret.getVersions().get(versionId);
|
||||
if (version == null || version.getSecretString() != null || version.getSecretBinary() != null
|
||||
|| version.getVersionStages().stream().anyMatch(stage -> !"AWSPENDING".equals(stage))) {
|
||||
return;
|
||||
}
|
||||
secret.getVersions().remove(versionId);
|
||||
persist(secret, region);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+38
@@ -19,6 +19,7 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
@@ -35,6 +36,7 @@ class SecretsManagerRotateLaterTest {
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String NAME = "rotate-later";
|
||||
private static final String TOKEN = "11111111-2222-3333-4444-555555555555";
|
||||
private static final String OTHER_TOKEN = "66666666-7777-8888-9999-000000000000";
|
||||
private static final String LAMBDA_ARN = "arn:aws:lambda:us-east-1:000000000000:function:rotate";
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
@@ -90,6 +92,42 @@ class SecretsManagerRotateLaterTest {
|
||||
awaitOnlyVersion(created.getCurrentVersionId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aValueWrittenUnderTheTokenDuringTheTestStepIsKept() {
|
||||
when(lambda.invoke(anyString(), anyString(), any(byte[].class), any())).thenAnswer(invocation -> {
|
||||
service.putSecretValue(NAME, "written", null, TOKEN, REGION, List.of("AWSPENDING"));
|
||||
return succeeded();
|
||||
});
|
||||
service.createSecret(NAME, "v1", null, null, null, null, REGION);
|
||||
|
||||
service.rotateSecret(NAME, TOKEN, LAMBDA_ARN, null, false, REGION);
|
||||
|
||||
await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
verify(lambda).invoke(anyString(), anyString(), any(byte[].class), any());
|
||||
SecretVersion version = service.describeSecret(NAME, REGION).getVersions().get(TOKEN);
|
||||
assertTrue(version == null || !version.getVersionStages().contains("AWSPENDING"));
|
||||
});
|
||||
assertEquals("written", service.getSecretValue(NAME, TOKEN, null, REGION).getSecretString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aVersionAnotherClientStagesDuringTheTestStepKeepsItsStage() {
|
||||
when(lambda.invoke(anyString(), anyString(), any(byte[].class), any())).thenAnswer(invocation -> {
|
||||
service.putSecretValue(NAME, "staged-elsewhere", null, OTHER_TOKEN, REGION, List.of("AWSPENDING"));
|
||||
return succeeded();
|
||||
});
|
||||
service.createSecret(NAME, "v1", null, null, null, null, REGION);
|
||||
|
||||
service.rotateSecret(NAME, TOKEN, LAMBDA_ARN, null, false, REGION);
|
||||
|
||||
await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
verify(lambda).invoke(anyString(), anyString(), any(byte[].class), any());
|
||||
assertFalse(service.describeSecret(NAME, REGION).getVersions().containsKey(TOKEN),
|
||||
"the test step's own version must be removed");
|
||||
});
|
||||
assertEquals(OTHER_TOKEN, service.getSecretValue(NAME, null, "AWSPENDING", REGION).getVersionId());
|
||||
}
|
||||
|
||||
/** Waits until the rotation has run its test step and the secret holds only the given version. */
|
||||
private void awaitOnlyVersion(String versionId) {
|
||||
await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
|
||||
Reference in new issue
Block a user