feat(cognito,kms): allow caller-pinned resource IDs via reserved tag prefix (#568)

Allow KMS CreateKey and Cognito CreateUserPool to accept a reserved floci:override-id tag at creation time so tests can pin deterministic resource IDs without a lookup round trip.

Strip reserved floci:* tags from stored metadata and responses, reject reserved tags on KMS TagResource, wire KMS CloudFormation provisioning through tagged key creation, and add focused unit, handler, integration, and AWS CLI bats coverage for the new behavior.
This commit is contained in:
TiM authored and GitHub committed 2026-04-22 22:52:42 -05:00
1 parent 86906ebd90
commit 2201c70ef1
15 files changed
+572 -8

No files matched your search

@@ -0,0 +1,55 @@
package io.github.hectorvent.floci.core.common;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.HashMap;
import java.util.Map;
@RegisterForReflection
public final class ReservedTags {
public static final String RESERVED_PREFIX = "floci:";
public static final String OVERRIDE_ID_KEY = RESERVED_PREFIX + "override-id";
private ReservedTags() {
}
public static String extractOverrideId(Map<String, String> tags) {
if (tags == null) {
return null;
}
return tags.get(OVERRIDE_ID_KEY);
}
public static Map<String, String> stripReservedTags(Map<String, String> tags) {
Map<String, String> stripped = new HashMap<>();
if (tags == null) {
return stripped;
}
tags.forEach((key, value) -> {
if (!isReserved(key)) {
stripped.put(key, value);
}
});
return stripped;
}
public static void rejectReservedTagsOnUpdate(Map<String, String> tags) {
if (tags == null) {
return;
}
for (String key : tags.keySet()) {
if (isReserved(key)) {
throw new AwsException(
"ValidationException",
"Reserved tag keys with prefix " + RESERVED_PREFIX + " can only be supplied during resource creation.",
400
);
}
}
}
private static boolean isReserved(String key) {
return key != null && key.startsWith(RESERVED_PREFIX);
}
}
@@ -543,7 +543,8 @@ public class CloudFormationResourceProvisioner {
private void provisionKmsKey(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String region, String accountId) {
String description = resolveOptional(props, "Description", engine);
var key = kmsService.createKey(description, region);
Map<String, String> tags = parseCfnTags(props != null ? props.get("Tags") : null, engine);
var key = kmsService.createKey(description, null, tags, region);
r.setPhysicalId(key.getKeyId());
r.getAttributes().put("Arn", key.getArn());
r.getAttributes().put("KeyId", key.getKeyId());
@@ -4,6 +4,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.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 io.github.hectorvent.floci.services.cognito.model.*;
@@ -78,7 +79,11 @@ public class CognitoService {
@SuppressWarnings("unchecked")
public UserPool createUserPool(Map<String, Object> request, String region) {
String name = (String) request.get("PoolName");
String id = region + "_" + UUID.randomUUID().toString().replace("-", "").substring(0, 9);
Map<String, String> userPoolTags = (Map<String, String>) request.get("UserPoolTags");
String id = resolveUserPoolId(region, userPoolTags);
if (poolStore.get(id).isPresent()) {
throw new AwsException("ResourceConflictException", "User pool already exists", 400);
}
UserPool pool = new UserPool();
pool.setId(id);
pool.setName(name);
@@ -122,7 +127,9 @@ public class CognitoService {
if (request.containsKey("DeviceConfiguration")) pool.setDeviceConfiguration((Map<String, Object>) request.get("DeviceConfiguration"));
if (request.containsKey("EmailConfiguration")) pool.setEmailConfiguration((Map<String, Object>) request.get("EmailConfiguration"));
if (request.containsKey("SmsConfiguration")) pool.setSmsConfiguration((Map<String, Object>) request.get("SmsConfiguration"));
if (request.containsKey("UserPoolTags")) pool.setUserPoolTags((Map<String, String>) request.get("UserPoolTags"));
if (request.containsKey("UserPoolTags")) {
pool.setUserPoolTags(ReservedTags.stripReservedTags((Map<String, String>) request.get("UserPoolTags")));
}
if (request.containsKey("AdminCreateUserConfig")) pool.setAdminCreateUserConfig((Map<String, Object>) request.get("AdminCreateUserConfig"));
if (request.containsKey("UserPoolAddOns")) pool.setUserPoolAddOns((Map<String, Object>) request.get("UserPoolAddOns"));
if (request.containsKey("UsernameConfiguration")) pool.setUsernameConfiguration((Map<String, Object>) request.get("UsernameConfiguration"));
@@ -872,6 +879,32 @@ public class CognitoService {
return baseUrl + "/" + poolId;
}
private String resolveUserPoolId(String region, Map<String, String> tags) {
String overrideId = ReservedTags.extractOverrideId(tags);
if (overrideId == null) {
return region + "_" + UUID.randomUUID().toString().replace("-", "").substring(0, 9);
}
validateOverridePoolId(overrideId);
return overrideId.trim();
}
private void validateOverridePoolId(String overrideId) {
if (overrideId == null || overrideId.trim().isEmpty()) {
throw new AwsException("ValidationException", "Override resource ID must not be blank.", 400);
}
String normalized = overrideId.trim();
if (normalized.chars().anyMatch(Character::isWhitespace)) {
throw new AwsException("ValidationException", "Override resource ID must not contain whitespace.", 400);
}
if (normalized.indexOf('/') >= 0 || normalized.indexOf('?') >= 0 || normalized.indexOf('#') >= 0) {
throw new AwsException("ValidationException", "Override resource ID contains unsupported characters.", 400);
}
if (normalized.chars().anyMatch(Character::isISOControl)) {
throw new AwsException("ValidationException", "Override resource ID must not contain control characters.", 400);
}
}
public String getJwksUri(String poolId) {
return getIssuer(poolId) + "/.well-known/jwks.json";
}
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.kms;
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.ReservedTags;
import io.github.hectorvent.floci.services.kms.model.KmsAlias;
import io.github.hectorvent.floci.services.kms.model.KmsKey;
import com.fasterxml.jackson.databind.JsonNode;
@@ -279,6 +280,7 @@ public class KmsJsonHandler {
String keyId = request.path("KeyId").asText();
Map<String, String> tags = new HashMap<>();
request.path("Tags").forEach(t -> tags.put(t.path("TagKey").asText(), t.path("TagValue").asText()));
ReservedTags.rejectReservedTagsOnUpdate(tags);
service.tagResource(keyId, tags, region);
return Response.ok(objectMapper.createObjectNode()).build();
}
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.kms;
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 io.github.hectorvent.floci.services.kms.model.KmsAlias;
@@ -78,7 +79,10 @@ public class KmsService {
}
public KmsKey createKey(String description, String keyUsage, String customerMasterKeySpec, String policy, Map<String, String> tags, String region) {
String keyId = UUID.randomUUID().toString();
String keyId = resolveKeyId(tags);
if (keyStore.get(region + "::" + keyId).isPresent()) {
throw new AwsException("AlreadyExistsException", "Key already exists", 400);
}
String arn = regionResolver.buildArn("kms", region, "key/" + keyId);
String effectiveUsage = keyUsage != null ? keyUsage : "ENCRYPT_DECRYPT";
@@ -92,9 +96,7 @@ public class KmsService {
key.setKeyUsage(effectiveUsage);
key.setCustomerMasterKeySpec(effectiveSpec);
key.setPolicy(policy != null ? policy : DEFAULT_KEY_POLICY);
if (tags != null) {
key.getTags().putAll(tags);
}
key.getTags().putAll(ReservedTags.stripReservedTags(tags));
generateKeyMaterial(key);
@@ -103,6 +105,22 @@ public class KmsService {
return key;
}
private String resolveKeyId(Map<String, String> tags) {
String overrideId = ReservedTags.extractOverrideId(tags);
if (overrideId == null) {
return UUID.randomUUID().toString();
}
String normalized = overrideId.trim();
if (normalized.isEmpty()) {
throw new AwsException("ValidationException", "Override resource ID must not be blank.", 400);
}
if (normalized.length() > 256) {
throw new AwsException("ValidationException", "Override resource ID must be 256 characters or fewer.", 400);
}
return normalized;
}
private void generateKeyMaterial(KmsKey key) {
String spec = key.getCustomerMasterKeySpec();
if ("SYMMETRIC_DEFAULT".equals(spec)) {
@@ -448,6 +466,7 @@ public class KmsService {
public void tagResource(String keyId, Map<String, String> tags, String region) {
KmsKey key = resolveKey(keyId, region);
ReservedTags.rejectReservedTagsOnUpdate(tags);
key.getTags().putAll(tags);
keyStore.put(region + "::" + key.getKeyId(), key);
}