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

@@ -13,9 +13,18 @@ public final class ReservedTags {
public static final String OVERRIDE_ID_KEY = RESERVED_PREFIX + "override-id";
public static final String OVERRIDE_COGNITO_CLIENT_ID_KEY = RESERVED_PREFIX + "override-cognito-client-id";
public static final String OVERRIDE_COGNITO_CLIENT_SECRET_KEY = RESERVED_PREFIX + "override-cognito-client-secret";
/**
* API Gateway accepted a custom id through this key before {@link #OVERRIDE_ID_KEY} existed. It is
* API Gateway specific and deprecated: still honored on create, but no longer persisted in the
* resource tags. Other services never supported it and must not start reserving it.
*/
public static final String DEPRECATED_API_GATEWAY_CUSTOM_ID_KEY = "_custom_id_";
private static final String INVALID_PARAMETER_EXCEPTION = "InvalidParameterException";
private static final String VALIDATION_EXCEPTION = "ValidationException";
private static final String TAG_EXCEPTION = "TagException";
private static final String BAD_REQUEST_EXCEPTION = "BadRequestException";
private static final String CONTROL_CHARACTER_ERROR_MESSAGE = "Override %s must not contain control characters.";
private ReservedTags() {
@@ -29,6 +38,21 @@ public final class ReservedTags {
return getOverride(tags, OVERRIDE_ID_KEY, ReservedTags::validateOverrideId, "Resource ID", INVALID_PARAMETER_EXCEPTION);
}
/**
* API Gateway v1 and v2 override id. {@link #OVERRIDE_ID_KEY} wins when both keys are present, so a
* caller migrating off {@link #DEPRECATED_API_GATEWAY_CUSTOM_ID_KEY} can set both during a rollout.
* Uses {@code BadRequestException}, which is what CreateRestApi and CreateApi declare.
*/
public static String extractOverrideApiId(Map<String, String> tags) {
String override = getOverride(tags, OVERRIDE_ID_KEY, ReservedTags::validateOverrideId,
"Resource ID", BAD_REQUEST_EXCEPTION);
if (override != null) {
return override;
}
return getOverride(tags, DEPRECATED_API_GATEWAY_CUSTOM_ID_KEY, ReservedTags::validateOverrideId,
"Resource ID", BAD_REQUEST_EXCEPTION);
}
public static String extractOverrideCognitoClientId(Map<String, String> tags) {
return getOverride(tags, OVERRIDE_COGNITO_CLIENT_ID_KEY, ReservedTags::validateOverrideId, "Cognito Client ID", INVALID_PARAMETER_EXCEPTION);
}
@@ -50,6 +74,36 @@ public final class ReservedTags {
return stripped;
}
/**
* Like {@link #stripReservedTags(Map)} but also drops API Gateway's deprecated custom-id key, so an
* id override never survives into the tags the API returns.
*/
public static Map<String, String> stripApiGatewayReservedTags(Map<String, String> tags) {
Map<String, String> stripped = stripReservedTags(tags);
stripped.remove(DEPRECATED_API_GATEWAY_CUSTOM_ID_KEY);
return stripped;
}
/**
* API Gateway update-path guard. An id override only makes sense at create time, so both the
* reserved keys and the deprecated custom-id key are rejected here, with API Gateway's declared
* error code rather than the {@code ValidationException} the shared guard uses.
*/
public static void rejectApiGatewayReservedTagsOnUpdate(Map<String, String> tags) {
if (tags == null) {
return;
}
for (String key : tags.keySet()) {
if (isReserved(key) || DEPRECATED_API_GATEWAY_CUSTOM_ID_KEY.equals(key)) {
throw new AwsException(
BAD_REQUEST_EXCEPTION,
"Reserved tag key " + key + " can only be supplied during resource creation.",
400
);
}
}
}
public static void rejectReservedTagsOnUpdate(Map<String, String> tags) {
if (tags == null) {
return;
@@ -14,6 +14,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.ReservedTags;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.apigateway.model.Account;
@@ -172,15 +173,19 @@ public class ApiGatewayService {
Map<String, String> tags = request.get("tags") instanceof Map<?, ?> m
? (Map<String, String>) m : new HashMap<>();
String customId = tags.get("_custom_id_");
String apiId = (customId != null && !customId.isBlank()) ? customId : shortId(10);
String customId = ReservedTags.extractOverrideApiId(tags);
String apiId = customId != null ? customId : shortId(10);
if (apiStore.get(apiKey(region, apiId)).isPresent()) {
throw new AwsException("ConflictException",
"REST API with id '" + apiId + "' already exists", 409);
}
RestApi api = new RestApi();
api.setId(apiId);
api.setName(name);
api.setDescription(description);
api.setCreatedDate(System.currentTimeMillis() / 1000L);
api.setTags(tags);
api.setTags(ReservedTags.stripApiGatewayReservedTags(tags));
EndpointConfiguration endpointConfiguration = new EndpointConfiguration();
if (request.get(EPC_KEY) instanceof Map<?, ?> epMap) {
@@ -1045,6 +1050,7 @@ public class ApiGatewayService {
}
public void tagResource(String region, String apiId, Map<String, String> tags) {
ReservedTags.rejectApiGatewayReservedTagsOnUpdate(tags);
RestApi api = getRestApi(region, apiId);
api.getTags().putAll(tags);
apiStore.put(apiKey(region, apiId), api);
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.ReservedTags;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import com.fasterxml.jackson.core.type.TypeReference;
@@ -81,8 +82,17 @@ public class ApiGatewayV2Service {
routeSelectionExpression = "${request.method} ${request.path}";
}
@SuppressWarnings("unchecked")
Map<String, String> tags = (Map<String, String>) request.get("tags");
String overrideId = ReservedTags.extractOverrideApiId(tags);
String apiId = overrideId != null ? overrideId : shortId(10);
if (apiStore.get(apiKey(region, apiId)).isPresent()) {
throw new AwsException("ConflictException",
"API with id '" + apiId + "' already exists", 409);
}
Api api = new Api();
api.setApiId(shortId(10));
api.setApiId(apiId);
api.setName(name);
api.setProtocolType(protocolType);
api.setCreatedDate(System.currentTimeMillis());
@@ -96,10 +106,8 @@ public class ApiGatewayV2Service {
api.setApiEndpoint(String.format("https://%s.execute-api.%s.amazonaws.com", api.getApiId(), region));
}
@SuppressWarnings("unchecked")
Map<String, String> tags = (Map<String, String>) request.get("tags");
if (tags != null) {
api.setTags(tags);
api.setTags(ReservedTags.stripApiGatewayReservedTags(tags));
}
@SuppressWarnings("unchecked")
@@ -162,6 +170,7 @@ public class ApiGatewayV2Service {
if (request.containsKey("tags")) {
@SuppressWarnings("unchecked")
Map<String, String> tags = (Map<String, String>) request.get("tags");
ReservedTags.rejectApiGatewayReservedTagsOnUpdate(tags);
api.setTags(tags);
}
if (request.containsKey("corsConfiguration")) {
@@ -884,6 +893,7 @@ public class ApiGatewayV2Service {
}
public void tagResource(String resourceArn, Map<String, String> tags) {
ReservedTags.rejectApiGatewayReservedTagsOnUpdate(tags);
String[] parsed = parseArn(resourceArn);
String region = parsed[0];
String apiId = parsed[1];