fix(secretsmanager): refuse a rotation function in another Region with InvalidParameterException (#4873)

Follow-up from the review of #4768.

A well-formed full function ARN in another Region passed the reference check,
then Lambda's lookup refused it with InvalidParameterValueException ("Region
'us-west-2' in ARN does not match request region 'us-east-1'") and RotateSecret
rethrew that unchanged. InvalidParameterValueException is not an error
RotateSecret declares, so an SDK client could not type the refusal.

The Region is now checked with the rest of the reference, before the caller's
lambda:InvokeFunction check, and refused with InvalidParameterException and
Lambda's reason in the message, like a reference Lambda cannot parse. Checked
only at the lookup, it came after the caller check, so under IAM enforcement a
caller not allowed to invoke that ARN got AccessDeniedException for a reference
that is invalid whoever sends it. Any other 400 Lambda gives at the lookup is
mapped the same way, through one helper; a missing function is still
AccessDeniedException and anything else still passes through. The secret is
left as it was.

Neither the Secrets Manager User Guide nor the API Reference says which error a
function in another Region gets, or that the function must share the secret's
Region. InvalidParameterException is the one RotateSecret declares for an
invalid parameter value.

An integration test runs RotateSecret against the real Lambda service with a
us-west-2 function ARN from us-east-1; it fails on main with
InvalidParameterValueException. Unit tests check the Region refusal comes
before the caller check and keeps a previously configured function, and that a
Lambda 400 at the lookup maps too. docs/services/secrets-manager.md describes it.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-02 07:45:59 -05:00
1 parent 3b1ac49f4f
commit a5379cd5d8
4 files changed
+115 -5

No files matched your search

+4
View File
@@ -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 rotation
settings 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 secret
immediately, so a replica is `InSync` by the time the call returns. Replicas are read-only and
track later writes to the primary.
@@ -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);
}
/**
@@ -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("/");
}
}
@@ -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());
}
@Test
void refusedCallerLeavesTheSecretUntouchedAndNothingRuns() {
Consumer<String> refuse = functionArn -> {