refactor(secretsmanager): move library usages to imports (#3897)

* refactor(secretsmanager): move library usages to imports

* refactor(secretsmanager): complete qualification cleanup and formatting in test files

* refactor(secretsmanager): import ParameterizedTest and CsvSource in service test
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-09-19 15:15:23 -04:00
1 parent 709214557c
commit b9fab02849
3 files changed
+146 -142

No files matched your search

@@ -1,7 +1,9 @@
package io.github.hectorvent.floci.services.secretsmanager;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.lambda.LambdaService;
import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
@@ -15,6 +17,7 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
@@ -94,12 +97,10 @@ class SecretRotationSchedulerTest {
@Test
void anUnparseableScheduleExpressionIsRejected() {
service.createSecret("bad-cron", "v1", null, null, null, null, REGION);
io.github.hectorvent.floci.core.common.AwsException ex =
org.junit.jupiter.api.Assertions.assertThrows(
io.github.hectorvent.floci.core.common.AwsException.class,
() -> service.rotateSecret("bad-cron", TOKEN, LAMBDA_ARN,
new Secret.RotationRules(null, null, "every other tuesday"),
false, REGION));
AwsException ex = assertThrows(AwsException.class,
() -> service.rotateSecret("bad-cron", TOKEN, LAMBDA_ARN,
new Secret.RotationRules(null, null, "every other tuesday"),
false, REGION));
assertEquals("InvalidParameterException", ex.getErrorCode());
}
@@ -195,8 +196,8 @@ class SecretRotationSchedulerTest {
// one. Enumerating and writing through that fallback would leave every other account's
// secrets unrotated - and worse, write a phantom copy into the default account.
String foreignAccount = "111122223333";
io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend<Secret> store =
io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend.inMemory("000000000000");
AccountAwareStorageBackend<Secret> store =
AccountAwareStorageBackend.inMemory("000000000000");
SecretsManagerService accountAware = new SecretsManagerService(store, 30,
new RegionResolver(REGION, "000000000000"), mock(LambdaService.class), new ObjectMapper());
SecretRotationScheduler sweep = new SecretRotationScheduler(accountAware);
@@ -1,16 +1,22 @@
package io.github.hectorvent.floci.services.secretsmanager;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.function.Consumer;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
class SecretsManagerJsonHandlerTest {
@@ -336,7 +342,7 @@ class SecretsManagerJsonHandlerTest {
Response response = handler.handle("ListSecrets", req, REGION);
assertThat(response.getStatus(), is(400));
// ListSecrets does not model ValidationException; AWS returns InvalidParameterException.
assertThat(((io.github.hectorvent.floci.core.common.AwsErrorResponse) response.getEntity()).type(),
assertThat(((AwsErrorResponse) response.getEntity()).type(),
is("InvalidParameterException"));
}
@@ -592,8 +598,8 @@ class SecretsManagerJsonHandlerTest {
rules.put("ScheduleExpression", "cron(0 16 ? * 2 *)");
rotateReq.set("RotationRules", rules);
io.github.hectorvent.floci.core.common.AwsException ex = org.junit.jupiter.api.Assertions.assertThrows(
io.github.hectorvent.floci.core.common.AwsException.class,
AwsException ex = assertThrows(
AwsException.class,
() -> handler.handle("RotateSecret", rotateReq, REGION)
);
assertThat(ex.getErrorCode(), is("InvalidParameterException"));
@@ -753,8 +759,8 @@ class SecretsManagerJsonHandlerTest {
for (String action : new String[] { "GetResourcePolicy", "DeleteResourcePolicy" }) {
ObjectNode request = MAPPER.createObjectNode();
request.put("SecretId", "missing-secret");
io.github.hectorvent.floci.core.common.AwsException ex = org.junit.jupiter.api.Assertions.assertThrows(
io.github.hectorvent.floci.core.common.AwsException.class,
AwsException ex = assertThrows(
AwsException.class,
() -> handler.handle(action, request, REGION));
assertThat(ex.getErrorCode(), is("ResourceNotFoundException"));
}
@@ -762,8 +768,8 @@ class SecretsManagerJsonHandlerTest {
ObjectNode put = MAPPER.createObjectNode();
put.put("SecretId", "missing-secret");
put.put("ResourcePolicy", RESOURCE_POLICY);
io.github.hectorvent.floci.core.common.AwsException ex = org.junit.jupiter.api.Assertions.assertThrows(
io.github.hectorvent.floci.core.common.AwsException.class,
AwsException ex = assertThrows(
AwsException.class,
() -> handler.handle("PutResourcePolicy", put, REGION));
assertThat(ex.getErrorCode(), is("ResourceNotFoundException"));
}
@@ -780,8 +786,8 @@ class SecretsManagerJsonHandlerTest {
for (String action : new String[] { "GetResourcePolicy", "DeleteResourcePolicy" }) {
ObjectNode request = MAPPER.createObjectNode();
request.put("SecretId", arn);
io.github.hectorvent.floci.core.common.AwsException ex = org.junit.jupiter.api.Assertions.assertThrows(
io.github.hectorvent.floci.core.common.AwsException.class,
AwsException ex = assertThrows(
AwsException.class,
() -> handler.handle(action, request, REGION));
assertThat(ex.getErrorCode(), is("InvalidRequestException"));
assertThat(ex.getMessage(), containsString("marked for deletion"));
@@ -790,8 +796,8 @@ class SecretsManagerJsonHandlerTest {
ObjectNode put = MAPPER.createObjectNode();
put.put("SecretId", arn);
put.put("ResourcePolicy", RESOURCE_POLICY);
io.github.hectorvent.floci.core.common.AwsException ex = org.junit.jupiter.api.Assertions.assertThrows(
io.github.hectorvent.floci.core.common.AwsException.class,
AwsException ex = assertThrows(
AwsException.class,
() -> handler.handle("PutResourcePolicy", put, REGION));
assertThat(ex.getErrorCode(), is("InvalidRequestException"));
assertThat(ex.getMessage(), containsString("marked for deletion"));
@@ -876,7 +882,7 @@ class SecretsManagerJsonHandlerTest {
createSecret("cancel-with-pending");
String pendingToken = "a1b2c3d4-e5f6-7890-abcd-ef1234567890";
service.putSecretValue("cancel-with-pending", "half-rotated", null,
pendingToken, REGION, java.util.List.of("AWSPENDING"));
pendingToken, REGION, List.of("AWSPENDING"));
ObjectNode req = MAPPER.createObjectNode();
req.put("SecretId", "cancel-with-pending");
@@ -901,20 +907,16 @@ class SecretsManagerJsonHandlerTest {
void cancelRotateSecretOnUnknownSecretThrowsResourceNotFound() {
ObjectNode req = MAPPER.createObjectNode();
req.put("SecretId", "nope");
io.github.hectorvent.floci.core.common.AwsException ex =
org.junit.jupiter.api.Assertions.assertThrows(
io.github.hectorvent.floci.core.common.AwsException.class,
() -> handler.handle("CancelRotateSecret", req, REGION));
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("CancelRotateSecret", req, REGION));
assertThat(ex.getErrorCode(), is("ResourceNotFoundException"));
}
// ─── DeleteSecret recovery-window validation ───────────────────────────────
private void assertDeleteSecretRejected(ObjectNode request) {
io.github.hectorvent.floci.core.common.AwsException ex =
org.junit.jupiter.api.Assertions.assertThrows(
io.github.hectorvent.floci.core.common.AwsException.class,
() -> handler.handle("DeleteSecret", request, REGION));
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("DeleteSecret", request, REGION));
assertThat(ex.getErrorCode(), is("InvalidParameterException"));
}
@@ -1033,7 +1035,7 @@ class SecretsManagerJsonHandlerTest {
assertThat(list.get("SecretList").size(), is(2));
ObjectNode deleted = null;
for (com.fasterxml.jackson.databind.JsonNode node : list.get("SecretList")) {
for (JsonNode node : list.get("SecretList")) {
if ("going".equals(node.get("Name").asText())) {
deleted = (ObjectNode) node;
}
@@ -1104,9 +1106,9 @@ class SecretsManagerJsonHandlerTest {
/** Small helper so the filter-cap test stays readable. */
private static final class ArrayNodeBuilder {
private final com.fasterxml.jackson.databind.node.ArrayNode array;
private final ArrayNode array;
ArrayNodeBuilder(com.fasterxml.jackson.databind.node.ArrayNode array) {
ArrayNodeBuilder(ArrayNode array) {
this.array = array;
}
@@ -1151,10 +1153,10 @@ class SecretsManagerJsonHandlerTest {
// ─── CreateSecret / UpdateSecret validation ────────────────────────────────
private io.github.hectorvent.floci.core.common.AwsException expectAwsException(
private AwsException expectAwsException(
String action, ObjectNode request) {
return org.junit.jupiter.api.Assertions.assertThrows(
io.github.hectorvent.floci.core.common.AwsException.class,
return assertThrows(
AwsException.class,
() -> handler.handle(action, request, REGION));
}
@@ -1224,7 +1226,7 @@ class SecretsManagerJsonHandlerTest {
createSecret("deprecating");
String pendingToken = "a1b2c3d4-e5f6-7890-abcd-ef1234567890";
service.putSecretValue("deprecating", "pending", null, pendingToken, REGION,
java.util.List.of("AWSPENDING"));
List.of("AWSPENDING"));
service.updateSecretVersionStage("deprecating", null, pendingToken, "AWSPENDING", REGION);
ObjectNode stages = (ObjectNode) describe("deprecating").get("VersionIdsToStages");
@@ -1427,7 +1429,7 @@ class SecretsManagerJsonHandlerTest {
createSecret("deprecated-read");
String token = "a1b2c3d4-e5f6-7890-abcd-ef1234567890";
service.putSecretValue("deprecated-read", "pending", null, token, REGION,
java.util.List.of("AWSPENDING"));
List.of("AWSPENDING"));
service.updateSecretVersionStage("deprecated-read", null, token, "AWSPENDING", REGION);
ObjectNode req = MAPPER.createObjectNode();
@@ -1594,7 +1596,7 @@ class SecretsManagerJsonHandlerTest {
// ─── RotationRules validation ──────────────────────────────────────────────
private ObjectNode rotateRequest(String secretId, java.util.function.Consumer<ObjectNode> rules) {
private ObjectNode rotateRequest(String secretId, Consumer<ObjectNode> rules) {
ObjectNode req = MAPPER.createObjectNode();
req.put("SecretId", secretId);
req.put("RotationLambdaARN", LAMBDA_ARN);
@@ -1720,7 +1722,7 @@ class SecretsManagerJsonHandlerTest {
private String stageThenDeprecate(String secretName) {
String token = "a1b2c3d4-e5f6-7890-abcd-ef1234567890";
service.putSecretValue(secretName, "pending", null, token, REGION,
java.util.List.of("AWSPENDING"));
List.of("AWSPENDING"));
service.updateSecretVersionStage(secretName, null, token, "AWSPENDING", REGION);
return token;
}
@@ -1751,7 +1753,7 @@ class SecretsManagerJsonHandlerTest {
assertThat(body.get("Versions").size(), is(2));
ObjectNode deprecatedEntry = null;
for (com.fasterxml.jackson.databind.JsonNode node : body.get("Versions")) {
for (JsonNode node : body.get("Versions")) {
if (deprecated.equals(node.get("VersionId").asText())) {
deprecatedEntry = (ObjectNode) node;
}
@@ -1766,7 +1768,7 @@ class SecretsManagerJsonHandlerTest {
createSecret("versioned");
for (int i = 1; i <= 3; i++) {
service.putSecretValue("versioned", "v" + i, null, null, REGION,
java.util.List.of("stage-" + i));
List.of("stage-" + i));
}
ObjectNode req = MAPPER.createObjectNode();
@@ -1877,10 +1879,8 @@ class SecretsManagerJsonHandlerTest {
req.put("SecretString", "value");
req.put("ClientRequestToken", "too-short");
io.github.hectorvent.floci.core.common.AwsException ex =
org.junit.jupiter.api.Assertions.assertThrows(
io.github.hectorvent.floci.core.common.AwsException.class,
() -> handler.handle("CreateSecret", req, REGION));
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("CreateSecret", req, REGION));
assertThat(ex.getErrorCode(), is("InvalidParameterException"));
}
}
@@ -1,18 +1,30 @@
package io.github.hectorvent.floci.services.secretsmanager;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.kms.KmsService;
import io.github.hectorvent.floci.services.kms.model.KmsKey;
import io.github.hectorvent.floci.services.lambda.LambdaService;
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.*;
@@ -152,13 +164,13 @@ class SecretsManagerServiceTest {
service.createSecret("my-secret", "v1", null, null, null, null, REGION);
// no stages
Assertions.assertThrows(AwsException.class, () ->
assertThrows(AwsException.class, () ->
service.putSecretValue("my-secret", "v2", null, null, REGION, List.of())
);
// more than 20
List<String> stages =
IntStream.range(0, 21).mapToObj(i -> "stage" + i).toList();
Assertions.assertThrows(AwsException.class, () ->
assertThrows(AwsException.class, () ->
service.putSecretValue("my-secret", "v2", null, null, REGION, stages)
);
}
@@ -167,12 +179,12 @@ class SecretsManagerServiceTest {
void putSecretValueInvalidStageName() {
service.createSecret("my-secret", "v1", null, null, null, null, REGION);
// Stage name is 0-length
Assertions.assertThrows(AwsException.class, () ->
assertThrows(AwsException.class, () ->
service.putSecretValue("my-secret", "v2", null, null, REGION, List.of(""))
);
// Stage name is larger than 256 characters
String stageName = RandomStringUtils.randomAlphanumeric(257);
Assertions.assertThrows(AwsException.class, () ->
assertThrows(AwsException.class, () ->
service.putSecretValue("my-secret", "v2", null, null, REGION, List.of(stageName))
);
@@ -359,17 +371,17 @@ class SecretsManagerServiceTest {
void rotateSecret() throws Exception {
// We need a mocked LambdaService for testing the orchestrator.
// The service in the setUp method doesn't have it.
io.github.hectorvent.floci.services.lambda.LambdaService mockLambda = org.mockito.Mockito.mock(io.github.hectorvent.floci.services.lambda.LambdaService.class);
LambdaService mockLambda = Mockito.mock(LambdaService.class);
io.github.hectorvent.floci.services.lambda.model.InvokeResult successResult = new io.github.hectorvent.floci.services.lambda.model.InvokeResult();
InvokeResult successResult = new InvokeResult();
// Assume success response
org.mockito.Mockito.when(mockLambda.invoke(org.mockito.Mockito.anyString(), org.mockito.Mockito.anyString(), org.mockito.Mockito.any(byte[].class), org.mockito.Mockito.any()))
Mockito.when(mockLambda.invoke(Mockito.anyString(), Mockito.anyString(), Mockito.any(byte[].class), Mockito.any()))
.thenReturn(successResult);
SecretsManagerService svc = new SecretsManagerService(
new InMemoryStorage<String, Secret>(), 30,
new io.github.hectorvent.floci.core.common.RegionResolver("us-east-1", "000000000000"),
mockLambda, new com.fasterxml.jackson.databind.ObjectMapper());
new RegionResolver("us-east-1", "000000000000"),
mockLambda, new ObjectMapper());
svc.createSecret("my-secret", "value", null, null, null, null, REGION);
@@ -382,25 +394,25 @@ class SecretsManagerServiceTest {
assertTrue(rotated.isRotationEnabled());
// Verify lambda invoked 4 times
org.mockito.Mockito.verify(mockLambda, org.mockito.Mockito.timeout(5000).times(4))
.invoke(org.mockito.Mockito.eq(REGION),
org.mockito.Mockito.eq("arn:aws:lambda:us-east-1:000000000000:function:rotate"),
org.mockito.Mockito.any(byte[].class),
org.mockito.Mockito.eq(io.github.hectorvent.floci.services.lambda.model.InvocationType.RequestResponse));
Mockito.verify(mockLambda, Mockito.timeout(5000).times(4))
.invoke(Mockito.eq(REGION),
Mockito.eq("arn:aws:lambda:us-east-1:000000000000:function:rotate"),
Mockito.any(byte[].class),
Mockito.eq(InvocationType.RequestResponse));
}
@Test
void rotateSecretNotImmediate() throws Exception {
io.github.hectorvent.floci.services.lambda.LambdaService mockLambda = org.mockito.Mockito.mock(io.github.hectorvent.floci.services.lambda.LambdaService.class);
LambdaService mockLambda = Mockito.mock(LambdaService.class);
io.github.hectorvent.floci.services.lambda.model.InvokeResult successResult = new io.github.hectorvent.floci.services.lambda.model.InvokeResult();
org.mockito.Mockito.when(mockLambda.invoke(org.mockito.Mockito.anyString(), org.mockito.Mockito.anyString(), org.mockito.Mockito.any(byte[].class), org.mockito.Mockito.any()))
InvokeResult successResult = new InvokeResult();
Mockito.when(mockLambda.invoke(Mockito.anyString(), Mockito.anyString(), Mockito.any(byte[].class), Mockito.any()))
.thenReturn(successResult);
SecretsManagerService svc = new SecretsManagerService(
new InMemoryStorage<String, Secret>(), 30,
new io.github.hectorvent.floci.core.common.RegionResolver("us-east-1", "000000000000"),
mockLambda, new com.fasterxml.jackson.databind.ObjectMapper());
new InMemoryStorage<>(), 30,
new RegionResolver("us-east-1", "000000000000"),
mockLambda, new ObjectMapper());
svc.createSecret("my-secret", "value", null, null, null, null, REGION);
Secret.RotationRules rules = new Secret.RotationRules(30, null, null);
@@ -410,11 +422,11 @@ class SecretsManagerServiceTest {
rules, false, REGION);
// Verify lambda invoked 1 time (testSecret only for non-immediate rotation)
org.mockito.Mockito.verify(mockLambda, org.mockito.Mockito.timeout(5000).times(1))
.invoke(org.mockito.Mockito.eq(REGION),
org.mockito.Mockito.eq("arn:aws:lambda:us-east-1:000000000000:function:rotate"),
org.mockito.Mockito.any(byte[].class),
org.mockito.Mockito.eq(io.github.hectorvent.floci.services.lambda.model.InvocationType.RequestResponse));
Mockito.verify(mockLambda, Mockito.timeout(5000).times(1))
.invoke(Mockito.eq(REGION),
Mockito.eq("arn:aws:lambda:us-east-1:000000000000:function:rotate"),
Mockito.any(byte[].class),
Mockito.eq(InvocationType.RequestResponse));
}
@Test
@@ -430,12 +442,11 @@ class SecretsManagerServiceTest {
@Test
void rotateServiceManagedSecretNeedsNoLambdaArn() {
io.github.hectorvent.floci.services.lambda.LambdaService mockLambda =
org.mockito.Mockito.mock(io.github.hectorvent.floci.services.lambda.LambdaService.class);
LambdaService mockLambda = Mockito.mock(LambdaService.class);
SecretsManagerService svc = new SecretsManagerService(
new InMemoryStorage<String, Secret>(), 30,
new io.github.hectorvent.floci.core.common.RegionResolver("us-east-1", "000000000000"),
mockLambda, new com.fasterxml.jackson.databind.ObjectMapper());
new InMemoryStorage<>(), 30,
new RegionResolver("us-east-1", "000000000000"),
mockLambda, new ObjectMapper());
// The secret RDS manages: rotated by RDS itself, so it carries no rotation Lambda.
svc.createSecret("rds!db-1234", "value", null, null, null, null, "rds", REGION);
@@ -449,7 +460,7 @@ class SecretsManagerServiceTest {
assertNull(rotated.getLastRotatedDate());
assertNull(rotated.getRotationLambdaArn());
// No Lambda exists to drive the rotation lifecycle, so none may be invoked.
org.mockito.Mockito.verifyNoInteractions(mockLambda);
Mockito.verifyNoInteractions(mockLambda);
}
@Test
@@ -698,8 +709,8 @@ class SecretsManagerServiceTest {
* commercial partition a secret's own ARN, minus the random suffix AWS lets clients omit,
* resolved to nothing. Round trip through the ARN the emulator itself minted.
*/
@org.junit.jupiter.params.ParameterizedTest
@org.junit.jupiter.params.provider.CsvSource({
@ParameterizedTest
@CsvSource({
"us-east-1, arn:aws:secretsmanager:",
"us-gov-west-1, arn:aws-us-gov:secretsmanager:",
"cn-north-1, arn:aws-cn:secretsmanager:"})
@@ -941,21 +952,21 @@ class SecretsManagerServiceTest {
@Test
void rotateSecretFailsEarly() throws Exception {
io.github.hectorvent.floci.services.lambda.LambdaService mockLambda = org.mockito.Mockito.mock(io.github.hectorvent.floci.services.lambda.LambdaService.class);
LambdaService mockLambda = Mockito.mock(LambdaService.class);
io.github.hectorvent.floci.services.lambda.model.InvokeResult successResult = new io.github.hectorvent.floci.services.lambda.model.InvokeResult();
io.github.hectorvent.floci.services.lambda.model.InvokeResult errorResult = new io.github.hectorvent.floci.services.lambda.model.InvokeResult();
InvokeResult successResult = new InvokeResult();
InvokeResult errorResult = new InvokeResult();
errorResult.setFunctionError("Unhandled");
// Fail on second invocation (setSecret)
org.mockito.Mockito.when(mockLambda.invoke(org.mockito.Mockito.anyString(), org.mockito.Mockito.anyString(), org.mockito.Mockito.any(byte[].class), org.mockito.Mockito.any()))
Mockito.when(mockLambda.invoke(Mockito.anyString(), Mockito.anyString(), Mockito.any(byte[].class), Mockito.any()))
.thenReturn(successResult)
.thenReturn(errorResult);
SecretsManagerService svc = new SecretsManagerService(
new InMemoryStorage<String, Secret>(), 30,
new io.github.hectorvent.floci.core.common.RegionResolver("us-east-1", "000000000000"),
mockLambda, new com.fasterxml.jackson.databind.ObjectMapper());
new InMemoryStorage<>(), 30,
new RegionResolver("us-east-1", "000000000000"),
mockLambda, new ObjectMapper());
svc.createSecret("my-secret", "value", null, null, null, null, REGION);
Secret.RotationRules rules = new Secret.RotationRules(30, null, null);
@@ -965,29 +976,29 @@ class SecretsManagerServiceTest {
rules, true, REGION);
// Verify lambda invoked 2 times only (createSecret, setSecret)
org.mockito.Mockito.verify(mockLambda, org.mockito.Mockito.timeout(5000).times(2))
.invoke(org.mockito.Mockito.eq(REGION),
org.mockito.Mockito.eq("arn:aws:lambda:us-east-1:000000000000:function:rotate"),
org.mockito.Mockito.any(byte[].class),
org.mockito.Mockito.eq(io.github.hectorvent.floci.services.lambda.model.InvocationType.RequestResponse));
Mockito.verify(mockLambda, Mockito.timeout(5000).times(2))
.invoke(Mockito.eq(REGION),
Mockito.eq("arn:aws:lambda:us-east-1:000000000000:function:rotate"),
Mockito.any(byte[].class),
Mockito.eq(InvocationType.RequestResponse));
}
@Test
void rotateSecret_actuallyChangesTheStoredSecretValue() throws Exception {
io.github.hectorvent.floci.services.lambda.LambdaService mockLambda = org.mockito.Mockito.mock(io.github.hectorvent.floci.services.lambda.LambdaService.class);
LambdaService mockLambda = Mockito.mock(LambdaService.class);
String lambdaArn = "arn:aws:lambda:us-east-1:000000000000:function:rotate";
SecretsManagerService svc = new SecretsManagerService(
new InMemoryStorage<String, Secret>(), 30,
new io.github.hectorvent.floci.core.common.RegionResolver("us-east-1", "000000000000"),
mockLambda, new com.fasterxml.jackson.databind.ObjectMapper());
new InMemoryStorage<>(), 30,
new RegionResolver("us-east-1", "000000000000"),
mockLambda, new ObjectMapper());
svc.createSecret("my-secret", "old-value", null, null, null, null, REGION);
org.mockito.Mockito.when(mockLambda.invoke(org.mockito.Mockito.anyString(), org.mockito.Mockito.anyString(), org.mockito.Mockito.any(byte[].class), org.mockito.Mockito.any()))
Mockito.when(mockLambda.invoke(Mockito.anyString(), Mockito.anyString(), Mockito.any(byte[].class), Mockito.any()))
.thenAnswer(invocation -> {
byte[] payloadBytes = invocation.getArgument(2);
com.fasterxml.jackson.databind.JsonNode payload = new com.fasterxml.jackson.databind.ObjectMapper().readTree(payloadBytes);
JsonNode payload = new ObjectMapper().readTree(payloadBytes);
String step = payload.get("Step").asText();
String token = payload.get("ClientRequestToken").asText();
@@ -1000,7 +1011,7 @@ class SecretsManagerServiceTest {
"AWSCURRENT", REGION);
default -> { /* setSecret, testSecret: no-op for this test */ }
}
io.github.hectorvent.floci.services.lambda.model.InvokeResult ok = new io.github.hectorvent.floci.services.lambda.model.InvokeResult();
InvokeResult ok = new InvokeResult();
ok.setStatusCode(200);
return ok;
});
@@ -1025,13 +1036,13 @@ class SecretsManagerServiceTest {
@Test
void rotateSecret_previousRotationInProgressThrows() throws Exception {
io.github.hectorvent.floci.services.lambda.LambdaService mockLambda = org.mockito.Mockito.mock(io.github.hectorvent.floci.services.lambda.LambdaService.class);
LambdaService mockLambda = Mockito.mock(LambdaService.class);
String lambdaArn = "arn:aws:lambda:us-east-1:000000000000:function:rotate";
SecretsManagerService svc = new SecretsManagerService(
new InMemoryStorage<String, Secret>(), 30,
new io.github.hectorvent.floci.core.common.RegionResolver("us-east-1", "000000000000"),
mockLambda, new com.fasterxml.jackson.databind.ObjectMapper());
new RegionResolver("us-east-1", "000000000000"),
mockLambda, new ObjectMapper());
svc.createSecret("my-secret", "old-value", null, null, null, null, REGION);
@@ -1047,23 +1058,23 @@ class SecretsManagerServiceTest {
@Test
void rotateSecret_concurrentCallsLockingWorks() throws Exception {
io.github.hectorvent.floci.services.lambda.LambdaService mockLambda = org.mockito.Mockito.mock(io.github.hectorvent.floci.services.lambda.LambdaService.class);
LambdaService mockLambda = Mockito.mock(LambdaService.class);
String lambdaArn = "arn:aws:lambda:us-east-1:000000000000:function:rotate";
SecretsManagerService svc = new SecretsManagerService(
new InMemoryStorage<String, Secret>(), 30,
new io.github.hectorvent.floci.core.common.RegionResolver("us-east-1", "000000000000"),
mockLambda, new com.fasterxml.jackson.databind.ObjectMapper());
new RegionResolver("us-east-1", "000000000000"),
mockLambda, new ObjectMapper());
svc.createSecret("my-secret", "old-value", null, null, null, null, REGION);
java.util.concurrent.CountDownLatch latch = new java.util.concurrent.CountDownLatch(1);
CountDownLatch latch = new CountDownLatch(1);
org.mockito.Mockito.when(mockLambda.invoke(org.mockito.Mockito.anyString(), org.mockito.Mockito.anyString(), org.mockito.Mockito.any(byte[].class), org.mockito.Mockito.any()))
Mockito.when(mockLambda.invoke(Mockito.anyString(), Mockito.anyString(), Mockito.any(byte[].class), Mockito.any()))
.thenAnswer(invocation -> {
// Create secret and hold the lock on executor to simulate delay
byte[] payloadBytes = invocation.getArgument(2);
com.fasterxml.jackson.databind.JsonNode payload = new com.fasterxml.jackson.databind.ObjectMapper().readTree(payloadBytes);
JsonNode payload = new ObjectMapper().readTree(payloadBytes);
String step = payload.get("Step").asText();
String token = payload.get("ClientRequestToken").asText();
@@ -1072,7 +1083,7 @@ class SecretsManagerServiceTest {
latch.await();
}
io.github.hectorvent.floci.services.lambda.model.InvokeResult ok = new io.github.hectorvent.floci.services.lambda.model.InvokeResult();
InvokeResult ok = new InvokeResult();
ok.setStatusCode(200);
return ok;
});
@@ -1277,14 +1288,13 @@ class SecretsManagerServiceTest {
* to {@code services.kms}, so it is mocked here the same way the rotation tests mock
* LambdaService.
*/
private SecretsManagerService serviceWithKms(io.github.hectorvent.floci.services.kms.KmsService kms) {
private SecretsManagerService serviceWithKms(KmsService kms) {
return new SecretsManagerService(new InMemoryStorage<>(), 30,
new RegionResolver(REGION, "000000000000"), null, new com.fasterxml.jackson.databind.ObjectMapper(), kms);
new RegionResolver(REGION, "000000000000"), null, new ObjectMapper(), kms);
}
private static io.github.hectorvent.floci.services.kms.model.KmsKey kmsKey(boolean enabled, String state) {
io.github.hectorvent.floci.services.kms.model.KmsKey key =
new io.github.hectorvent.floci.services.kms.model.KmsKey();
private static KmsKey kmsKey(boolean enabled, String state) {
KmsKey key = new KmsKey();
key.setKeyId("abcd1234-ef56-7890-abcd-ef1234567890");
key.setEnabled(enabled);
key.setKeyState(state);
@@ -1293,10 +1303,9 @@ class SecretsManagerServiceTest {
@Test
void createSecretRejectsAKmsKeyThatDoesNotExist() {
io.github.hectorvent.floci.services.kms.KmsService kms =
org.mockito.Mockito.mock(io.github.hectorvent.floci.services.kms.KmsService.class);
org.mockito.Mockito.when(kms.describeKey(org.mockito.ArgumentMatchers.anyString(),
org.mockito.ArgumentMatchers.anyString()))
KmsService kms = Mockito.mock(KmsService.class);
Mockito.when(kms.describeKey(ArgumentMatchers.anyString(),
ArgumentMatchers.anyString()))
.thenThrow(new AwsException("NotFoundException", "Key not found", 404));
AwsException ex = assertThrows(AwsException.class, () -> serviceWithKms(kms)
@@ -1306,10 +1315,9 @@ class SecretsManagerServiceTest {
@Test
void createSecretRejectsADisabledKmsKey() {
io.github.hectorvent.floci.services.kms.KmsService kms =
org.mockito.Mockito.mock(io.github.hectorvent.floci.services.kms.KmsService.class);
org.mockito.Mockito.when(kms.describeKey(org.mockito.ArgumentMatchers.anyString(),
org.mockito.ArgumentMatchers.anyString()))
KmsService kms = Mockito.mock(KmsService.class);
Mockito.when(kms.describeKey(ArgumentMatchers.anyString(),
ArgumentMatchers.anyString()))
.thenReturn(kmsKey(false, "Disabled"));
AwsException ex = assertThrows(AwsException.class, () -> serviceWithKms(kms)
@@ -1319,10 +1327,9 @@ class SecretsManagerServiceTest {
@Test
void createSecretRejectsAKmsKeyPendingDeletion() {
io.github.hectorvent.floci.services.kms.KmsService kms =
org.mockito.Mockito.mock(io.github.hectorvent.floci.services.kms.KmsService.class);
org.mockito.Mockito.when(kms.describeKey(org.mockito.ArgumentMatchers.anyString(),
org.mockito.ArgumentMatchers.anyString()))
KmsService kms = Mockito.mock(KmsService.class);
Mockito.when(kms.describeKey(ArgumentMatchers.anyString(),
ArgumentMatchers.anyString()))
.thenReturn(kmsKey(true, "PendingDeletion"));
AwsException ex = assertThrows(AwsException.class, () -> serviceWithKms(kms)
@@ -1332,10 +1339,9 @@ class SecretsManagerServiceTest {
@Test
void createSecretAcceptsAnEnabledKmsKey() {
io.github.hectorvent.floci.services.kms.KmsService kms =
org.mockito.Mockito.mock(io.github.hectorvent.floci.services.kms.KmsService.class);
org.mockito.Mockito.when(kms.describeKey(org.mockito.ArgumentMatchers.anyString(),
org.mockito.ArgumentMatchers.anyString()))
KmsService kms = Mockito.mock(KmsService.class);
Mockito.when(kms.describeKey(ArgumentMatchers.anyString(),
ArgumentMatchers.anyString()))
.thenReturn(kmsKey(true, "Enabled"));
Secret secret = serviceWithKms(kms)
@@ -1347,26 +1353,24 @@ class SecretsManagerServiceTest {
void theAwsManagedDefaultKeyIsNeverLookedUp() {
// AWS creates aws/secretsmanager on demand, so it always resolves. Looking it up would
// fail on a fresh emulator where nobody has created it.
io.github.hectorvent.floci.services.kms.KmsService kms =
org.mockito.Mockito.mock(io.github.hectorvent.floci.services.kms.KmsService.class);
KmsService kms = Mockito.mock(KmsService.class);
SecretsManagerService kmsAware = serviceWithKms(kms);
kmsAware.createSecret("default-key", "v1", null, null, "alias/aws/secretsmanager", null, REGION);
kmsAware.createSecret("bare-default", "v1", null, null, "aws/secretsmanager", null, REGION);
org.mockito.Mockito.verify(kms, org.mockito.Mockito.never())
.describeKey(org.mockito.ArgumentMatchers.anyString(), org.mockito.ArgumentMatchers.anyString());
Mockito.verify(kms, Mockito.never())
.describeKey(ArgumentMatchers.anyString(), ArgumentMatchers.anyString());
}
@Test
void updateSecretRejectsAKmsKeyThatDoesNotExist() {
io.github.hectorvent.floci.services.kms.KmsService kms =
org.mockito.Mockito.mock(io.github.hectorvent.floci.services.kms.KmsService.class);
KmsService kms = Mockito.mock(KmsService.class);
SecretsManagerService kmsAware = serviceWithKms(kms);
kmsAware.createSecret("kms-secret", "v1", null, null, null, null, REGION);
org.mockito.Mockito.when(kms.describeKey(org.mockito.ArgumentMatchers.anyString(),
org.mockito.ArgumentMatchers.anyString()))
Mockito.when(kms.describeKey(ArgumentMatchers.anyString(),
ArgumentMatchers.anyString()))
.thenThrow(new AwsException("NotFoundException", "Key not found", 404));
AwsException ex = assertThrows(AwsException.class, () ->
@@ -1376,12 +1380,11 @@ class SecretsManagerServiceTest {
@Test
void aSecretWithoutAKmsKeyNeedsNoLookup() {
io.github.hectorvent.floci.services.kms.KmsService kms =
org.mockito.Mockito.mock(io.github.hectorvent.floci.services.kms.KmsService.class);
KmsService kms = Mockito.mock(KmsService.class);
serviceWithKms(kms).createSecret("no-key", "v1", null, null, null, null, REGION);
org.mockito.Mockito.verify(kms, org.mockito.Mockito.never())
.describeKey(org.mockito.ArgumentMatchers.anyString(), org.mockito.ArgumentMatchers.anyString());
Mockito.verify(kms, Mockito.never())
.describeKey(ArgumentMatchers.anyString(), ArgumentMatchers.anyString());
}
// ─── Replication ───────────────────────────────────────────────────────────
@@ -1389,7 +1392,7 @@ class SecretsManagerServiceTest {
private static final String REPLICA_REGION = "eu-west-1";
private Secret replicate(String name, String... regions) {
List<SecretsManagerService.ReplicaRegion> targets = new java.util.ArrayList<>();
List<SecretsManagerService.ReplicaRegion> targets = new ArrayList<>();
for (String r : regions) {
targets.add(new SecretsManagerService.ReplicaRegion(r, null));
}