Light, fluffy, and always free - The AWS Local Emulator alternative
fix(secretsmanager): refuse a rotation function in another Region with InvalidParameterException (#4873)
Priamo Germosena5379cd5d87d
diff --git a/docs/services/secrets-manager.md b/docs/services/secrets-manager.mdindex 3c603744c1..42bc96724d 100644--- a/docs/services/secrets-manager.md+++ b/docs/services/secrets-manager.md@@ -144,6 +144,10 @@ aws secretsmanager validate-resource-policy \- **A rotation function that does not exist** is reported as AWS reports it: `AccessDeniedException`(HTTP 400), "Secrets Manager cannot invoke the specified Lambda function". The secret's rotationsettings are left as they were, including any rotation function configured before.+- **A rotation function reference Lambda refuses**, one it cannot parse or a full ARN in another+ Region, gets `InvalidParameterException` (HTTP 400), the error `RotateSecret` declares for an+ invalid parameter, with Lambda's reason in the message. The secret is left as it was. AWS does not+ document which error a function in another Region gets.- **Replication is synchronous.** Real AWS reports `InProgress` first; floci copies the secretimmediately, so a replica is `InSync` by the time the call returns. Replicas are read-only andtrack later writes to the primary.diff --git a/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java b/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.javaindex 736cece71e..679fe23703 100644--- a/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java+++ b/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java@@ -1144,8 +1144,9 @@ public class SecretsManagerService implements ResourceProvider {authorizeInvoke.accept(functionArn);}- // A function that cannot be invoked is reported the way AWS reports it, whatever the reason:- // AccessDeniedException with HTTP 400, and the secret left as it was.+ // A function that cannot be found is reported the way AWS reports it, AccessDeniedException with+ // HTTP 400; any other reference Lambda refuses is an invalid RotationLambdaARN. Either way the+ // secret is left as it was.if (!serviceManaged && lambdaService != null) {try {lambdaService.getFunction(region, functionArn);@@ -1156,6 +1157,9 @@ public class SecretsManagerService implements ResourceProvider {+ "policy grants access to the principal " + ServicePrincipals.of("secretsmanager")+ ".", 400);}+ if (e.getHttpStatus() == 400) {+ throw invalidRotationFunction(e.getMessage());+ }throw e;}}@@ -1220,12 +1224,29 @@ public class SecretsManagerService implements ResourceProvider {*/private static String rotationFunctionArn(String reference, Secret secret, String region) {AwsArnUtils.Arn secretArn = AwsArnUtils.parse(secret.getArn());+ String functionArn;try {- return LambdaArnUtils.functionArn(reference, secretArn.partition(), region, secretArn.accountId());+ functionArn = LambdaArnUtils.functionArn(reference, secretArn.partition(), region, secretArn.accountId());} catch (AwsException e) {- throw new AwsException("InvalidParameterException",- "RotationLambdaARN is not a valid Lambda function reference: " + e.getMessage(), 400);+ throw invalidRotationFunction(e.getMessage());}+ // Lambda refuses a function ARN from another Region; refusing it here keeps it with the other+ // bad references, ahead of the caller check.+ String functionRegion = AwsArnUtils.parse(functionArn).region();+ if (!region.equals(functionRegion)) {+ throw invalidRotationFunction(+ "Region '" + functionRegion + "' in ARN does not match request region '" + region + "'");+ }+ return functionArn;+ }++ /**+ * RotateSecret's own error for a function reference Lambda refuses, keeping Lambda's reason: the+ * operation declares {@code InvalidParameterException} and none of Lambda's codes.+ */+ private static AwsException invalidRotationFunction(String reason) {+ return new AwsException("InvalidParameterException",+ "RotationLambdaARN is not a valid Lambda function reference: " + reason, 400);}/**diff --git a/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerRotationFunctionRegionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerRotationFunctionRegionIntegrationTest.javanew file mode 100644index 0000000000..e2c48f829f--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerRotationFunctionRegionIntegrationTest.java@@ -0,0 +1,56 @@+package io.github.hectorvent.floci.services.secretsmanager;++import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;+import io.quarkus.test.junit.QuarkusTest;+import io.restassured.response.Response;+import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.Test;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.equalTo;+import static org.hamcrest.Matchers.nullValue;++/**+ * RotateSecret against the real Lambda service: a function reference Lambda refuses is answered with+ * RotateSecret's own {@code InvalidParameterException}, never one of Lambda's codes (Secrets Manager+ * API Reference, RotateSecret, "Errors").+ */+@QuarkusTest+class SecretsManagerRotationFunctionRegionIntegrationTest {++ private static final String SM_CONTENT_TYPE = "application/x-amz-json-1.1";++ @BeforeAll+ static void configureRestAssured() {+ RestAssuredJsonUtils.configureAwsContentTypes();+ }++ @Test+ void rotationFunctionInAnotherRegionIsAnInvalidParameter() {+ String secretName = "rotation-other-region-secret";+ secretsManager("CreateSecret", "{\"Name\": \"" + secretName + "\", \"SecretString\": \"initial-value\"}")+ .then()+ .statusCode(200);++ secretsManager("RotateSecret", "{\"SecretId\": \"" + secretName + "\", "+ + "\"RotationLambdaARN\": \"arn:aws:lambda:us-west-2:000000000000:function:rotator\"}")+ .then()+ .statusCode(400)+ .body("__type", equalTo("InvalidParameterException"));++ secretsManager("DescribeSecret", "{\"SecretId\": \"" + secretName + "\"}")+ .then()+ .statusCode(200)+ .body("RotationEnabled", equalTo(false))+ .body("RotationLambdaARN", nullValue());+ }++ private static Response secretsManager(String action, String body) {+ return given()+ .header("X-Amz-Target", "secretsmanager." + action)+ .contentType(SM_CONTENT_TYPE)+ .body(body)+ .when()+ .post("/");+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerRotationInvokeAuthorizationTest.java b/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerRotationInvokeAuthorizationTest.javaindex ecaeca93a9..e736290b98 100644--- a/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerRotationInvokeAuthorizationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerRotationInvokeAuthorizationTest.java@@ -98,6 +98,35 @@ class SecretsManagerRotationInvokeAuthorizationTest {assertNull(service.describeSecret("my-secret", REGION).getRotationLambdaArn());}+ @Test+ void functionInAnotherRegionIsRejectedBeforeTheCheck() {+ Secret secret = service.describeSecret("my-secret", REGION);+ secret.setRotationLambdaArn(FUNCTION_ARN);+ store.put(REGION + "::my-secret", secret);+ List<String> checked = new ArrayList<>();++ AwsException e = assertThrows(AwsException.class, () -> service.rotateSecret("my-secret", TOKEN,+ "arn:aws:lambda:us-west-2:000000000000:function:rotator", null, false, REGION, checked::add));++ assertEquals("InvalidParameterException", e.getErrorCode());+ assertEquals(400, e.getHttpStatus());+ assertEquals(List.of(), checked);+ assertEquals(FUNCTION_ARN, service.describeSecret("my-secret", REGION).getRotationLambdaArn());+ }++ @Test+ void lambdaRefusingTheReferenceAtTheLookupIsAnInvalidParameter() {+ when(lambda.getFunction(anyString(), anyString()))+ .thenThrow(new AwsException("ValidationException", "refused by Lambda", 400));++ AwsException e = assertThrows(AwsException.class, () ->+ service.rotateSecret("my-secret", TOKEN, FUNCTION_ARN, null, true, REGION, functionArn -> { }));++ assertEquals("InvalidParameterException", e.getErrorCode());+ assertEquals(400, e.getHttpStatus());+ assertNull(service.describeSecret("my-secret", REGION).getRotationLambdaArn());+ }+@Testvoid refusedCallerLeavesTheSecretUntouchedAndNothingRuns() {Consumer<String> refuse = functionArn -> {