feat(apigateway): support floci:override-id for v1 and v2 (#2045)

* feat(apigateway): support floci:override-id for v1 and v2

API IDs were generated randomly, so endpoint URLs changed on every
recreate. v1 accepted an undocumented `_custom_id_` tag and echoed it
back as a normal tag; v2 had no override at all. Other services already
pin IDs through the reserved `floci:override-id` tag.

Both CreateRestApi and CreateApi now accept `floci:override-id`, routed
through ReservedTags.getOverride so the value is validated rather than
used verbatim: non-blank, no whitespace or control characters, and no
`/`, `?`, `#`, since those break the endpoint URL. Override keys are
consumed rather than stored, so they no longer appear in the returned
tags, and supplying either key to TagResource is rejected because an ID
cannot change after creation.

`_custom_id_` keeps working as a deprecated fallback and is now stripped
the same way; `floci:override-id` wins when both are present so callers
can set both during a migration. The key stays API Gateway specific: the
shared strip and reject helpers are untouched, so KMS and Cognito still
treat `_custom_id_` as an ordinary tag.

Error code is BadRequestException, declared by CreateRestApi, CreateApi
and TagResource on both services. The shared reject helper's
ValidationException is declared by neither, which is why API Gateway gets
its own guard rather than reusing it.

Closes #1593

Co-authored-by: Marcel Herhold <herhold.marcel@gmail.com>

* fix(apigateway): reject duplicate override ids with ConflictException

CreateRestApi (v1) and CreateApi (v2) now return 409 ConflictException
when the resolved API id already exists in the region, instead of
silently overwriting the existing API, matching how KMS and Cognito
treat duplicate override ids.

---------

Co-authored-by: Marcel Herhold <herhold.marcel@gmail.com>
This commit is contained in:
Hector VenturaandMarcel Herhold authored and GitHub committed 2026-07-29 23:40:27 -05:00
1 parent 86134e8c7f
commit 02385da37b
8 files changed
+430 -10

No files matched your search

@@ -84,6 +84,116 @@ class ReservedTagsTest {
assertEquals("ValidationException", exception.getErrorCode());
}
// ──────────────────────────── API Gateway override id ────────────────────────────
@Test
void extractOverrideApiIdReturnsNullForNullInput() {
assertNull(ReservedTags.extractOverrideApiId(null));
}
@Test
void extractOverrideApiIdReturnsNullWhenNeitherKeyPresent() {
assertNull(ReservedTags.extractOverrideApiId(Map.of("env", "test")));
}
@Test
void extractOverrideApiIdReadsReservedOverrideKey() {
assertEquals("my-api", ReservedTags.extractOverrideApiId(
Map.of(ReservedTags.OVERRIDE_ID_KEY, "my-api", "env", "test")));
}
@Test
void extractOverrideApiIdFallsBackToDeprecatedCustomIdKey() {
assertEquals("legacy-api", ReservedTags.extractOverrideApiId(
Map.of(ReservedTags.DEPRECATED_API_GATEWAY_CUSTOM_ID_KEY, "legacy-api")));
}
@Test
void extractOverrideApiIdPrefersReservedKeyOverDeprecatedKey() {
assertEquals("winner", ReservedTags.extractOverrideApiId(Map.of(
ReservedTags.OVERRIDE_ID_KEY, "winner",
ReservedTags.DEPRECATED_API_GATEWAY_CUSTOM_ID_KEY, "loser")));
}
@Test
void extractOverrideApiIdRejectsBlankValueWithApiGatewayErrorCode() {
AwsException exception = assertThrows(
AwsException.class,
() -> ReservedTags.extractOverrideApiId(Map.of(ReservedTags.OVERRIDE_ID_KEY, " "))
);
assertEquals("BadRequestException", exception.getErrorCode());
assertEquals(400, exception.getHttpStatus());
}
@Test
void extractOverrideApiIdRejectsUnsupportedCharacters() {
for (String bad : new String[] {"has/slash", "has?query", "has#fragment"}) {
AwsException exception = assertThrows(
AwsException.class,
() -> ReservedTags.extractOverrideApiId(Map.of(ReservedTags.OVERRIDE_ID_KEY, bad)),
"expected rejection for " + bad
);
assertEquals("BadRequestException", exception.getErrorCode());
}
}
@Test
void extractOverrideApiIdValidatesTheDeprecatedKeyToo() {
AwsException exception = assertThrows(
AwsException.class,
() -> ReservedTags.extractOverrideApiId(
Map.of(ReservedTags.DEPRECATED_API_GATEWAY_CUSTOM_ID_KEY, "has/slash"))
);
assertEquals("BadRequestException", exception.getErrorCode());
}
@Test
void stripApiGatewayReservedTagsRemovesBothOverrideKeys() {
Map<String, String> tags = new LinkedHashMap<>();
tags.put("env", "test");
tags.put(ReservedTags.OVERRIDE_ID_KEY, "my-api");
tags.put(ReservedTags.DEPRECATED_API_GATEWAY_CUSTOM_ID_KEY, "legacy-api");
tags.put("team", "platform");
assertEquals(Map.of("env", "test", "team", "platform"),
ReservedTags.stripApiGatewayReservedTags(tags));
}
@Test
void stripReservedTagsLeavesDeprecatedCustomIdForOtherServices() {
// _custom_id_ is API Gateway specific. KMS and Cognito use the shared strip and must keep it.
Map<String, String> tags = Map.of(ReservedTags.DEPRECATED_API_GATEWAY_CUSTOM_ID_KEY, "legacy-api");
assertEquals(tags, ReservedTags.stripReservedTags(tags));
}
@Test
void rejectApiGatewayReservedTagsOnUpdateAllowsNormalTags() {
assertDoesNotThrow(() -> ReservedTags.rejectApiGatewayReservedTagsOnUpdate(Map.of("env", "test")));
assertDoesNotThrow(() -> ReservedTags.rejectApiGatewayReservedTagsOnUpdate(null));
}
@Test
void rejectApiGatewayReservedTagsOnUpdateRejectsBothOverrideKeys() {
for (String key : new String[] {
ReservedTags.OVERRIDE_ID_KEY, ReservedTags.DEPRECATED_API_GATEWAY_CUSTOM_ID_KEY}) {
AwsException exception = assertThrows(
AwsException.class,
() -> ReservedTags.rejectApiGatewayReservedTagsOnUpdate(Map.of(key, "too-late")),
"expected rejection for " + key
);
assertEquals("BadRequestException", exception.getErrorCode());
}
}
@Test
void rejectReservedTagsOnUpdateIgnoresDeprecatedCustomIdForOtherServices() {
assertDoesNotThrow(() -> ReservedTags.rejectReservedTagsOnUpdate(
Map.of(ReservedTags.DEPRECATED_API_GATEWAY_CUSTOM_ID_KEY, "legacy-api")));
}
@Test
void rejectUnknownReservedTagsRejectsReservedTagsWithUserPoolTaggingException() {
AwsException exception = assertThrows(
@@ -373,12 +373,12 @@ class ApiGatewayIntegrationTest {
.statusCode(404);
}
// ──────────────────────────── _custom_id_ tag ────────────────────────────
// ──────────────────────────── _custom_id_ tag (deprecated) ────────────────────────────
@Test @Order(50)
void createRestApi_customIdTag_usesTagValueAsApiId() {
String body = """
{"name":"custom-id-api","tags":{"_custom_id_":"MYCUSTOMNAME"}}
{"name":"custom-id-api","tags":{"_custom_id_":"MYCUSTOMNAME","env":"test"}}
""";
given()
.contentType(ContentType.JSON)
@@ -387,7 +387,9 @@ class ApiGatewayIntegrationTest {
.then()
.statusCode(201)
.body("id", equalTo("MYCUSTOMNAME"))
.body("tags._custom_id_", equalTo("MYCUSTOMNAME"));
// The override key is consumed, not persisted; unrelated tags survive.
.body("tags._custom_id_", nullValue())
.body("tags.env", equalTo("test"));
}
@Test @Order(51)
@@ -400,11 +402,109 @@ class ApiGatewayIntegrationTest {
.body("name", equalTo("custom-id-api"));
}
// ──────────────────────────── floci:override-id tag ────────────────────────────
@Test @Order(52)
void createRestApi_flociOverrideIdTag_usesTagValueAsApiId() {
String body = """
{"name":"override-id-api","tags":{"floci:override-id":"MYOVERRIDEID"}}
""";
given()
.contentType(ContentType.JSON)
.body(body)
.when().post("/restapis")
.then()
.statusCode(201)
.body("id", equalTo("MYOVERRIDEID"))
.body("tags._custom_id_", nullValue())
.body("tags.'floci:override-id'", nullValue());
}
@Test @Order(53)
void createRestApi_bothOverrideKeys_prefersFlociOverrideId() {
String body = """
{"name":"both-keys-api","tags":{"floci:override-id":"WINNER","_custom_id_":"LOSER"}}
""";
given()
.contentType(ContentType.JSON)
.body(body)
.when().post("/restapis")
.then()
.statusCode(201)
.body("id", equalTo("WINNER"));
}
@Test @Order(54)
void createRestApi_blankOverrideId_isRejected() {
String body = """
{"name":"blank-override-api","tags":{"floci:override-id":" "}}
""";
given()
.contentType(ContentType.JSON)
.body(body)
.when().post("/restapis")
.then()
.statusCode(400)
.body("message", containsString("must not be blank"));
}
@Test @Order(55)
void createRestApi_overrideIdWithPathSeparator_isRejected() {
String body = """
{"name":"bad-override-api","tags":{"floci:override-id":"has/slash"}}
""";
given()
.contentType(ContentType.JSON)
.body(body)
.when().post("/restapis")
.then()
.statusCode(400)
.body("message", containsString("unsupported characters"));
}
@Test @Order(56)
void createRestApi_duplicateOverrideId_isRejectedWithConflict() {
String body = """
{"name":"duplicate-override-api","tags":{"floci:override-id":"MYOVERRIDEID"}}
""";
given()
.contentType(ContentType.JSON)
.body(body)
.when().post("/restapis")
.then()
.statusCode(409)
.body("message", containsString("already exists"));
// The original API is untouched.
given()
.when().get("/restapis/MYOVERRIDEID")
.then()
.statusCode(200)
.body("name", equalTo("override-id-api"));
}
@Test @Order(57)
void getRestApi_flociOverrideId_resolvesById() {
given()
.when().get("/restapis/MYOVERRIDEID")
.then()
.statusCode(200)
.body("id", equalTo("MYOVERRIDEID"))
.body("name", equalTo("override-id-api"));
}
@Test @Order(58)
void deleteRestApi_customId() {
given()
.when().delete("/restapis/MYCUSTOMNAME")
.then()
.statusCode(202);
given()
.when().delete("/restapis/MYOVERRIDEID")
.then()
.statusCode(202);
given()
.when().delete("/restapis/WINNER")
.then()
.statusCode(202);
}
}
@@ -330,4 +330,104 @@ class ApiGatewayV2IntegrationTest {
.then()
.statusCode(404);
}
// ──────────────────────────── Override IDs ────────────────────────────
@Test @Order(100)
void createApi_deprecatedCustomIdTag_usesTagValueAsApiId() {
given()
.contentType(ContentType.JSON)
.body("""
{"name":"custom-id-api","protocolType":"HTTP","tags":{"_custom_id_":"MYV2CUSTOM","env":"test"}}
""")
.when().post("/v2/apis")
.then()
.statusCode(201)
.body("apiId", equalTo("MYV2CUSTOM"))
.body("tags._custom_id_", nullValue())
.body("tags.env", equalTo("test"));
}
@Test @Order(101)
void createApi_flociOverrideIdTag_usesTagValueAsApiId() {
given()
.contentType(ContentType.JSON)
.body("""
{"name":"override-id-api","protocolType":"HTTP","tags":{"floci:override-id":"MYV2OVERRIDE"}}
""")
.when().post("/v2/apis")
.then()
.statusCode(201)
.body("apiId", equalTo("MYV2OVERRIDE"))
.body("tags.'floci:override-id'", nullValue());
}
@Test @Order(102)
void createApi_blankOverrideId_isRejected() {
given()
.contentType(ContentType.JSON)
.body("""
{"name":"blank-override","protocolType":"HTTP","tags":{"floci:override-id":" "}}
""")
.when().post("/v2/apis")
.then()
.statusCode(400);
}
@Test @Order(103)
void createApi_duplicateOverrideId_isRejectedWithConflict() {
given()
.contentType(ContentType.JSON)
.body("""
{"name":"duplicate-override","protocolType":"HTTP","tags":{"floci:override-id":"MYV2OVERRIDE"}}
""")
.when().post("/v2/apis")
.then()
.statusCode(409)
.body("message", containsString("already exists"));
given()
.when().get("/v2/apis/MYV2OVERRIDE")
.then()
.statusCode(200)
.body("name", equalTo("override-id-api"));
}
@Test @Order(104)
void getApi_overrideId_resolvesById() {
given()
.when().get("/v2/apis/MYV2CUSTOM")
.then()
.statusCode(200)
.body("apiId", equalTo("MYV2CUSTOM"));
}
@Test @Order(105)
void tagApi_withOverrideKey_isRejectedAfterCreation() {
given()
.contentType(ContentType.JSON)
.body("""
{"tags":{"floci:override-id":"TOOLATE"}}
""")
.when().post("/v2/tags/arn:aws:apigateway:us-east-1::/apis/MYV2OVERRIDE")
.then()
.statusCode(400);
}
@Test @Order(106)
void tagApi_withDeprecatedCustomIdKey_isRejectedAfterCreation() {
given()
.contentType(ContentType.JSON)
.body("""
{"tags":{"_custom_id_":"TOOLATE"}}
""")
.when().post("/v2/tags/arn:aws:apigateway:us-east-1::/apis/MYV2OVERRIDE")
.then()
.statusCode(400);
}
@Test @Order(107)
void deleteApis_customAndOverrideId() {
given().when().delete("/v2/apis/MYV2CUSTOM").then().statusCode(204);
given().when().delete("/v2/apis/MYV2OVERRIDE").then().statusCode(204);
}
}