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.java index e6e57c8745..736cece71e 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 @@ -1139,10 +1139,7 @@ public class SecretsManagerService implements ResourceProvider { // One full ARN for the permission check, the lookup and the invoke, so all three reach the same // function: a name or partial ARN would otherwise be invoked in whatever account the rotation // thread resolves, which is not necessarily the one that was checked. - AwsArnUtils.Arn secretArn = AwsArnUtils.parse(secret.getArn()); - String functionArn = serviceManaged - ? null - : LambdaArnUtils.functionArn(finalLambdaArn, secretArn.partition(), region, secretArn.accountId()); + String functionArn = serviceManaged ? null : rotationFunctionArn(finalLambdaArn, secret, region); if (!serviceManaged) { authorizeInvoke.accept(functionArn); } @@ -1214,6 +1211,22 @@ public class SecretsManagerService implements ResourceProvider { return secret; } + /** + * The full ARN of the rotation function {@code reference} names, in the secret's partition and + * account unless the reference names its own. A reference Lambda cannot read is refused with + * Secrets Manager's {@code InvalidParameterException}: Lambda's codes for it, + * {@code InvalidParameterValueException} and {@code ValidationException}, are not among the + * errors {@code RotateSecret} declares, so an SDK client could not type them. + */ + private static String rotationFunctionArn(String reference, Secret secret, String region) { + AwsArnUtils.Arn secretArn = AwsArnUtils.parse(secret.getArn()); + try { + return 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); + } + } /** * The outcome of {@link #cancelRotateSecret}: the secret with rotation turned off, plus the id diff --git a/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerRotationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerRotationIntegrationTest.java index 3acbb42abb..a55d7a3ba2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerRotationIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerRotationIntegrationTest.java @@ -12,6 +12,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -98,4 +99,27 @@ public class SecretsManagerRotationIntegrationTest { assertTrue(hasTest, "Should invoke testSecret"); assertTrue(hasFinish, "Should invoke finishSecret"); } + + @Test + void rotationFunctionReferenceLambdaCannotReadIsAnInvalidParameter() { + String secretName = "rotation-invalid-reference-secret"; + given() + .header("X-Amz-Target", "secretsmanager.CreateSecret") + .contentType(SM_CONTENT_TYPE) + .body("{\"Name\": \"" + secretName + "\", \"SecretString\": \"initial-value\"}") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "secretsmanager.RotateSecret") + .contentType(SM_CONTENT_TYPE) + .body("{\"SecretId\": \"" + secretName + "\", \"RotationLambdaARN\": \"rotator!\"}") + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("InvalidParameterException")); + } } 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.java index 3b3ae80765..ecaeca93a9 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 @@ -12,6 +12,7 @@ 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.junit.jupiter.params.provider.ValueSource; import java.util.ArrayList; import java.util.List; @@ -74,14 +75,25 @@ class SecretsManagerRotationInvokeAuthorizationTest { verify(lambda, timeout(5000).atLeastOnce()).invoke(eq(REGION), eq(expected), any(byte[].class), any()); } - @Test - void referenceLambdaCannotReadIsRejectedBeforeTheCheck() { + /** + * Each reference is invalid under Lambda's documented {@code FunctionName} pattern, not only + * under Floci's parser: an empty name, a character the name pattern excludes, an empty qualifier. + * RotateSecret answers with its own {@code InvalidParameterException}, not Lambda's codes. + */ + @ParameterizedTest + @ValueSource(strings = { + "arn:aws:lambda:us-east-1:111122223333:function:", + "rotator!", + "rotator:" + }) + void referenceLambdaCannotReadIsRejectedBeforeTheCheck(String reference) { List checked = new ArrayList<>(); AwsException e = assertThrows(AwsException.class, () -> service.rotateSecret("my-secret", TOKEN, - "us-east-1:111122223333:function:rotator", null, false, REGION, checked::add)); + reference, null, false, REGION, checked::add)); - assertEquals("InvalidParameterValueException", e.getErrorCode()); + assertEquals("InvalidParameterException", e.getErrorCode()); + assertEquals(400, e.getHttpStatus()); assertEquals(List.of(), checked); assertNull(service.describeSecret("my-secret", REGION).getRotationLambdaArn()); }