refactor(secretsmanager): build the rotation function ARN with Lambda's own helper (#4709)

Follow-ups from the review of #4697.

rotationFunctionArn built the partial-ARN case by string concatenation while the bare-name case went
through AwsArnUtils.Arn. Turning a FunctionName reference into a full function ARN is Lambda's
knowledge, so it now lives in LambdaArnUtils.functionArn, next to eventSourceMappingArn: the
reference is read with LambdaArnUtils.resolve, the parser Lambda itself uses, and every case is
minted through AwsArnUtils.Arn from the parts. Secrets Manager calls it with the secret's partition
and account, and keeps no Lambda layout of its own. ResolvedFunctionRef also carries the account a
full or partial ARN names (null for a bare name), which the helper needs.

A reference Lambda cannot read, such as a Region-prefixed partial ARN, is now refused with Lambda's
InvalidParameterValueException before the permission check, instead of being minted with the Region
twice and failing later at the function lookup.

The missing-function message names its service principal through ServicePrincipals.of, as AGENTS.md
now asks, in place of the literal marked partition-invariant in #4697.

The comment on the caller == null branch of IamEnforcementFilter.authorizeAdditionalResource said the
path only served a presigned POST whose signature was already verified. It now also serves CopyObject,
RotateSecret and SSM GetParameter, and even for the presigned POST the signature check runs after
it, and only when S3 auth enforcement is on. The comment now says what actually makes an unknown key
safe on each path.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-30 08:00:21 -05:00
1 parent e69ee61d40
commit f752946acf
5 files changed
+110 -52

No files matched your search

@@ -12,24 +12,26 @@ class LambdaArnUtilsTest {
@ParameterizedTest
@CsvSource({
// input, expectedName, expectedQualifier, expectedRegion
"my-fn, my-fn, , ",
"my-fn:prod, my-fn, prod, ",
"my-fn:$LATEST, my-fn, $LATEST, ",
"my-fn:42, my-fn, 42, ",
"000000000000:function:my-fn, my-fn, , ",
"000000000000:function:my-fn:prod, my-fn, prod, ",
"arn:aws:lambda:us-east-1:000000000000:function:my-fn, my-fn, , us-east-1",
"arn:aws:lambda:eu-west-1:123456789012:function:my-fn, my-fn, , eu-west-1",
"arn:aws:lambda:us-east-1:000000000000:function:my-fn:prod, my-fn, prod, us-east-1",
"arn:aws:lambda:us-east-1:000000000000:function:my-fn:$LATEST, my-fn, $LATEST, us-east-1",
"arn:aws:lambda:us-east-1:000000000000:function:my_fn-1, my_fn-1, , us-east-1",
// input, expectedName, expectedQualifier, expectedRegion, expectedAccount
"my-fn, my-fn, , , ",
"my-fn:prod, my-fn, prod, , ",
"my-fn:$LATEST, my-fn, $LATEST, , ",
"my-fn:42, my-fn, 42, , ",
"000000000000:function:my-fn, my-fn, , , 000000000000",
"111122223333:function:my-fn:prod, my-fn, prod, , 111122223333",
"arn:aws:lambda:us-east-1:000000000000:function:my-fn, my-fn, , us-east-1, 000000000000",
"arn:aws:lambda:eu-west-1:123456789012:function:my-fn, my-fn, , eu-west-1, 123456789012",
"arn:aws:lambda:us-east-1:000000000000:function:my-fn:prod, my-fn, prod, us-east-1, 000000000000",
"arn:aws:lambda:us-east-1:000000000000:function:my-fn:$LATEST, my-fn, $LATEST, us-east-1, 000000000000",
"arn:aws:lambda:us-east-1:000000000000:function:my_fn-1, my_fn-1, , us-east-1, 000000000000",
})
void resolveAcceptsValidForms(String input, String expectedName, String expectedQualifier, String expectedRegion) {
void resolveAcceptsValidForms(String input, String expectedName, String expectedQualifier, String expectedRegion,
String expectedAccount) {
LambdaArnUtils.ResolvedFunctionRef ref = LambdaArnUtils.resolve(input);
assertEquals(expectedName, ref.name());
assertEquals(emptyToNull(expectedQualifier), ref.qualifier());
assertEquals(emptyToNull(expectedRegion), ref.region());
assertEquals(emptyToNull(expectedAccount), ref.account());
}
@ParameterizedTest
@@ -236,4 +238,34 @@ class LambdaArnUtilsTest {
assertEquals("myFn", ref.name());
assertEquals("PROD", ref.qualifier());
}
@ParameterizedTest
@CsvSource({
// reference, expected full ARN (partition aws, region us-east-1, default account 000000000000)
"rotator, arn:aws:lambda:us-east-1:000000000000:function:rotator",
"rotator:live, arn:aws:lambda:us-east-1:000000000000:function:rotator:live",
"111122223333:function:rotator, arn:aws:lambda:us-east-1:111122223333:function:rotator",
"111122223333:function:rotator:7, arn:aws:lambda:us-east-1:111122223333:function:rotator:7",
"arn:aws:lambda:eu-west-1:111122223333:function:rotator, arn:aws:lambda:eu-west-1:111122223333:function:rotator",
})
void functionArnCompletesAReferenceTheWayLambdaReadsIt(String reference, String expected) {
assertEquals(expected, LambdaArnUtils.functionArn(reference, "aws", "us-east-1", "000000000000"));
}
@Test
void functionArnUsesThePartitionItIsGiven() {
assertEquals("arn:aws-cn:lambda:cn-north-1:000000000000:function:rotator",
LambdaArnUtils.functionArn("rotator", "aws-cn", "cn-north-1", "000000000000"));
}
@ParameterizedTest
@ValueSource(strings = {
"us-east-1:111122223333:function:rotator",
"arn:aws:lambda:us-east-1:abc:function:rotator",
})
void functionArnRefusesAReferenceLambdaCannotRead(String reference) {
AwsException ex = assertThrows(AwsException.class,
() -> LambdaArnUtils.functionArn(reference, "aws", "us-east-1", "000000000000"));
assertEquals("InvalidParameterValueException", ex.getErrorCode());
}
}
@@ -60,6 +60,7 @@ class SecretsManagerRotationInvokeAuthorizationTest {
"rotator, arn:aws:lambda:us-east-1:000000000000:function:rotator",
"rotator:live, arn:aws:lambda:us-east-1:000000000000:function:rotator:live",
"111122223333:function:rotator, arn:aws:lambda:us-east-1:111122223333:function:rotator",
"111122223333:function:rotator:live, arn:aws:lambda:us-east-1:111122223333:function:rotator:live",
"arn:aws:lambda:us-east-1:111122223333:function:rotator, arn:aws:lambda:us-east-1:111122223333:function:rotator"
})
void checkAndInvokeUseTheSameFullArn(String given, String expected) {
@@ -73,6 +74,18 @@ class SecretsManagerRotationInvokeAuthorizationTest {
verify(lambda, timeout(5000).atLeastOnce()).invoke(eq(REGION), eq(expected), any(byte[].class), any());
}
@Test
void referenceLambdaCannotReadIsRejectedBeforeTheCheck() {
List<String> checked = new ArrayList<>();
AwsException e = assertThrows(AwsException.class, () -> service.rotateSecret("my-secret", TOKEN,
"us-east-1:111122223333:function:rotator", null, false, REGION, checked::add));
assertEquals("InvalidParameterValueException", e.getErrorCode());
assertEquals(List.of(), checked);
assertNull(service.describeSecret("my-secret", REGION).getRotationLambdaArn());
}
@Test
void refusedCallerLeavesTheSecretUntouchedAndNothingRuns() {
Consumer<String> refuse = functionArn -> {