diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md index 5cf36d4676..2f8ad31bca 100644 --- a/docs/services/api-gateway.md +++ b/docs/services/api-gateway.md @@ -31,8 +31,9 @@ aws apigateway create-rest-api \ ``` The override key is consumed rather than stored, so it never appears in the tags the API returns. Any -other tags in the same request are kept. Because an ID cannot change after creation, supplying either -override key to `TagResource` is rejected with `BadRequestException`. +other tags in the same request are kept. Because an ID cannot change after creation, an override that +differs from the API's ID is rejected with `BadRequestException`. Resending the current ID is accepted, +so CloudFormation and Terraform updates keep working. Values must be non-blank and must not contain whitespace, control characters, or `/`, `?`, `#`, since those would break the endpoint URL. An invalid value is rejected with `BadRequestException`. diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java index 9500e39ec4..a622ef6d7a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java @@ -235,6 +235,12 @@ public class ApiGatewayV2Service { public Api updateApi(String region, String apiId, Map request) { Api api = getApi(region, apiId); + // Validated before any field is set, so a rejected update leaves the API as it was. + @SuppressWarnings("unchecked") + Map tags = (Map) request.get("tags"); + if (request.containsKey("tags")) { + ReservedTags.rejectApiGatewayReservedTagsOnUpdate(tags, apiId); + } if (request.containsKey("name") && request.get("name") != null) { api.setName((String) request.get("name")); @@ -256,10 +262,7 @@ public class ApiGatewayV2Service { api.setDisableExecuteApiEndpoint(booleanValue(request.get("disableExecuteApiEndpoint"))); } if (request.containsKey("tags")) { - @SuppressWarnings("unchecked") - Map tags = (Map) request.get("tags"); - ReservedTags.rejectApiGatewayReservedTagsOnUpdate(tags); - api.setTags(tags); + api.setTags(ReservedTags.stripApiGatewayReservedTags(tags)); } if (request.containsKey("corsConfiguration")) { @SuppressWarnings("unchecked") diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java index 5464aae8d1..389ea68c23 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java @@ -1649,6 +1649,97 @@ class CloudFormationIntegrationTest { .body("Tags.find { it.TagKey == 'floci:override-id' }", nullValue()); } + @Test + void updateStack_httpApiWithOverrideTagKeepsPinnedId() { + String template = """ + { + "Resources": { + "MyApi": { + "Type": "AWS::ApiGatewayV2::Api", + "Properties": { + "Name": "cfn-override-api", + "ProtocolType": "HTTP", + "Description": "%s", + "Tags": { + "floci:override-id": "cfn-pinned-api", + "env": "test" + } + } + } + } + } + """; + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateStack") + .formParam("StackName", "cfn-http-api-override-stack") + .formParam("TemplateBody", template.formatted("before")) + .when() + .post("/") + .then() + .statusCode(200); + + // Infrastructure tools resend the full tag set, override included, on every update. + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "UpdateStack") + .formParam("StackName", "cfn-http-api-override-stack") + .formParam("TemplateBody", template.formatted("after")) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DescribeStacks") + .formParam("StackName", "cfn-http-api-override-stack") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("UPDATE_COMPLETE")); + + given() + .when() + .get("/v2/apis/cfn-pinned-api") + .then() + .statusCode(200) + .body("description", equalTo("after")) + .body("tags.env", equalTo("test")) + .body("tags.'floci:override-id'", nullValue()); + + // A different override would rename the API, which is still refused after creation. + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "UpdateStack") + .formParam("StackName", "cfn-http-api-override-stack") + .formParam("TemplateBody", template.formatted("renamed").replace("cfn-pinned-api", "cfn-renamed-api")) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DescribeStacks") + .formParam("StackName", "cfn-http-api-override-stack") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("UPDATE_ROLLBACK_COMPLETE")); + + given() + .when() + .get("/v2/apis/cfn-pinned-api") + .then() + .statusCode(200) + .body("description", equalTo("after")) + .body("tags.env", equalTo("test")); + } + @Test void createStack_lambdaWithEnvironmentVariables() { String template = """