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

@@ -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 -> {