From 44e9b1600e62260636a85b97f3bf06b1bdc44e54 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Thu, 1 Oct 2026 22:17:37 -0400 Subject: [PATCH] fix(secretsmanager): refuse an unreadable rotation function reference with InvalidParameterException (#4768) Follow-up from the review of #4709. A RotationLambdaARN that Lambda's reference parser cannot read was refused with Lambda's own error, InvalidParameterValueException for a malformed reference or ValidationException for a name outside the FunctionName pattern. Neither is an error RotateSecret declares: its four are ResourceNotFoundException, InvalidParameterException, InternalServiceError and InvalidRequestException, so an SDK client could not type the refusal. It is now Secrets Manager's InvalidParameterException ("The parameter name or value is invalid.", HTTP 400), with Lambda's explanation kept in the message. The unit test that pinned the old code used a Region-prefixed partial ARN, which Lambda's documented FunctionName pattern accepts even though Floci's parser does not yet. It now uses references that are invalid under that pattern too: an empty function name, a character the name pattern excludes, and an empty qualifier. They cover both of Lambda's codes. An integration test checks the error type on the wire. --- .../secretsmanager/SecretsManagerService.java | 21 ++++++++++++---- ...SecretsManagerRotationIntegrationTest.java | 24 +++++++++++++++++++ ...anagerRotationInvokeAuthorizationTest.java | 20 ++++++++++++---- 3 files changed, 57 insertions(+), 8 deletions(-) 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()); }