fix(cloudformation): update an API Gateway authorizer in place and replace it on a RestApiId change (#8841)

This commit is contained in:
Oleksandr Nikolaichuk authored and GitHub committed 2026-11-18 08:06:09 -04:00
1 parent 93fd5690e8
commit 29fc72d156
8 files changed
+402 -21

No files matched your search

@@ -8,6 +8,7 @@ import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
@QuarkusTest
class ApiGatewayUpdateAuthorizerIntegrationTest {
@@ -209,5 +210,27 @@ class ApiGatewayUpdateAuthorizerIntegrationTest {
.then()
.statusCode(200)
.body("name", equalTo("original-name"));
String path = "/restapis/" + apiId + "/authorizers/" + authorizerId;
for (String ops : new String[]{
"{\"op\":\"replace\",\"path\":\"/type\",\"value\":\"BOGUS\"}",
"{\"op\":\"replace\",\"path\":\"/name\",\"value\":\"x\"},"
+ "{\"op\":\"replace\",\"path\":\"/type\",\"value\":\"BOGUS\"}",
"{\"op\":\"remove\",\"path\":\"/identitySource\"}"}) {
given().contentType("application/json").body("{\"patchOperations\":[" + ops + "]}")
.when().patch(path).then().statusCode(400);
given().when().get(path).then().statusCode(200).body("name", equalTo("original-name"))
.body("type", equalTo("TOKEN")).body("identitySource", equalTo("method.request.header.Authorization"))
.body("authorizerUri", equalTo("arn:aws:lambda:us-east-1:123456789012:function:my-authorizer"));
}
String requestId = given().contentType("application/json").body(
"{\"name\":\"req\",\"type\":\"REQUEST\",\"authorizerUri\":\"arn:aws:lambda:us-east-1:123456789012:function:a\","
+ "\"identitySource\":\"method.request.header.X\",\"authorizerResultTtlInSeconds\":0}")
.when().post("/restapis/" + apiId + "/authorizers").then().statusCode(201).extract().path("id");
given().contentType("application/json")
.body("{\"patchOperations\":[{\"op\":\"remove\",\"path\":\"/identitySource\"}]}")
.when().patch("/restapis/" + apiId + "/authorizers/" + requestId).then().statusCode(200)
.body("identitySource", nullValue());
}
}
@@ -0,0 +1,111 @@
package io.floci.aws.services.cloudformation;
import io.floci.aws.testing.AwsTestClients;
import io.floci.aws.testing.CfnStackWaits;
import io.floci.aws.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
import static io.floci.aws.services.cloudformation.CfnStackTestSupport.physicalId;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
/**
* An AWS::ApiGateway::Authorizer updated through a stack: a change that needs no replacement keeps the
* authorizer's id and applies on a later retry after a rollback, and a changed RestApiId replaces it.
*/
@QuarkusTest
class ApiGatewayAuthorizerCfnUpdateIntegrationTest {
private static final String SETTINGS = "\"Name\":\"n1\",\"AuthorizerResultTtlInSeconds\":60";
private static final String CHANGED = "\"Name\":\"n2\"";
/** A Cognito authorizer created without an AuthorizerUri, after Auth; {@code uri} is appended to its properties. */
private static String cognito(String uri) {
return ",\"Cog\":{\"Type\":\"AWS::ApiGateway::Authorizer\",\"DependsOn\":\"Auth\",\"Properties\":{"
+ "\"RestApiId\":{\"Ref\":\"ApiA\"},\"Type\":\"COGNITO_USER_POOLS\",\"Name\":\"cog\","
+ "\"ProviderARNs\":[\"arn:aws:cognito-idp:us-east-1:123456789012:userpool/us-east-1_x\"],"
+ "\"IdentitySource\":\"method.request.header.Authorization\"" + uri + "}}";
}
private static CloudFormationClient client() {
return AwsTestClients.create(CloudFormationClient.builder(), "test", Region.US_EAST_1);
}
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void anInPlaceUpdateKeepsTheAuthorizerAndARollbackRestoresIt() {
String stack = "apigw-cfn-authorizer-inplace-it";
CloudFormationClient cf = client();
try {
CfnStackWaits.createStack(cf, stack, template("ApiA", SETTINGS, cognito("")), "CREATE_COMPLETE");
String apiId = physicalId(cf, stack, "ApiA");
String authorizerId = physicalId(cf, stack, "Auth");
String cognitoId = physicalId(cf, stack, "Cog");
CfnStackWaits.updateStack(cf, stack,
template("ApiA", CHANGED, cognito(",\"AuthorizerUri\":\"u2\"")
+ CfnStackWaits.failingResourceAfter("Cog")),
"UPDATE_ROLLBACK_COMPLETE");
given().when().get("/restapis/" + apiId + "/authorizers/" + cognitoId).then().statusCode(200)
.body("authorizerUri", nullValue());
assertEquals(authorizerId, physicalId(cf, stack, "Auth"));
given().when().get("/restapis/" + apiId + "/authorizers/" + authorizerId).then().statusCode(200)
.body("name", equalTo("n1")).body("authorizerResultTtlInSeconds", equalTo(60));
CfnStackWaits.updateStack(cf, stack, template("ApiA", CHANGED, cognito("")), "UPDATE_COMPLETE");
assertEquals(authorizerId, physicalId(cf, stack, "Auth"));
given().when().get("/restapis/" + apiId + "/authorizers").then().statusCode(200)
.body("item", hasSize(2));
given().when().get("/restapis/" + apiId + "/authorizers/" + authorizerId).then().statusCode(200)
.body("name", equalTo("n2")).body("authorizerResultTtlInSeconds", equalTo(300));
} finally {
CfnStackWaits.deleteStack(cf, stack);
cf.close();
}
}
@Test
void aChangedRestApiIdReplacesTheAuthorizer() {
String stack = "apigw-cfn-authorizer-replace-it";
CloudFormationClient cf = client();
try {
CfnStackWaits.createStack(cf, stack, template("ApiA", SETTINGS, ""), "CREATE_COMPLETE");
String apiA = physicalId(cf, stack, "ApiA");
String apiB = physicalId(cf, stack, "ApiB");
String authorizerId = physicalId(cf, stack, "Auth");
CfnStackWaits.updateStack(cf, stack, template("ApiB", SETTINGS, ""), "UPDATE_COMPLETE");
assertNotEquals(authorizerId, physicalId(cf, stack, "Auth"));
given().when().get("/restapis/" + apiA + "/authorizers").then().statusCode(200)
.body("item", hasSize(0));
given().when().get("/restapis/" + apiB + "/authorizers").then().statusCode(200)
.body("item", hasSize(1));
} finally {
CfnStackWaits.deleteStack(cf, stack);
cf.close();
}
}
/** Two REST APIs and a TOKEN authorizer on {@code apiRef}; {@code later} is appended to the resources. */
private static String template(String apiRef, String settings, String later) {
return """
{"Resources":{
"ApiA":{"Type":"AWS::ApiGateway::RestApi","Properties":{"Name":"cfn-authorizer-a"}},
"ApiB":{"Type":"AWS::ApiGateway::RestApi","Properties":{"Name":"cfn-authorizer-b"}},
"Auth":{"Type":"AWS::ApiGateway::Authorizer","Properties":{
"RestApiId":{"Ref":"%s"},"Type":"TOKEN","AuthorizerUri":"u1",
"IdentitySource":"method.request.header.Auth",%s}}%s}}
""".formatted(apiRef, settings, later);
}
}