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,86 @@
package io.github.hectorvent.floci.core.common;
import org.junit.jupiter.api.Test;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ReservedTagsTest {
@Test
void stripReservedTagsReturnsEmptyMapForNullInput() {
assertTrue(ReservedTags.stripReservedTags(null).isEmpty());
}
@Test
void stripReservedTagsReturnsEmptyMapForEmptyInput() {
assertTrue(ReservedTags.stripReservedTags(Map.of()).isEmpty());
}
@Test
void stripReservedTagsKeepsNonReservedTags() {
Map<String, String> tags = Map.of("env", "test", "team", "platform");
assertEquals(tags, ReservedTags.stripReservedTags(tags));
}
@Test
void stripReservedTagsRemovesOnlyReservedTags() {
Map<String, String> tags = new LinkedHashMap<>();
tags.put("env", "test");
tags.put(ReservedTags.OVERRIDE_ID_KEY, "my-id");
tags.put("floci:internal", "hidden");
tags.put("team", "platform");
Map<String, String> stripped = ReservedTags.stripReservedTags(tags);
assertEquals(Map.of("env", "test", "team", "platform"), stripped);
}
@Test
void stripReservedTagsRemovesAllReservedTags() {
Map<String, String> tags = Map.of(
ReservedTags.OVERRIDE_ID_KEY, "my-id",
"floci:internal", "hidden"
);
assertTrue(ReservedTags.stripReservedTags(tags).isEmpty());
}
@Test
void extractOverrideIdReturnsNullForNullInput() {
assertNull(ReservedTags.extractOverrideId(null));
}
@Test
void extractOverrideIdReturnsReservedOverrideOnly() {
Map<String, String> tags = Map.of(
ReservedTags.OVERRIDE_ID_KEY, "my-id",
"floci:internal", "hidden",
"env", "test"
);
assertEquals("my-id", ReservedTags.extractOverrideId(tags));
}
@Test
void rejectReservedTagsOnUpdateAllowsNormalTags() {
assertDoesNotThrow(() -> ReservedTags.rejectReservedTagsOnUpdate(Map.of("env", "test")));
}
@Test
void rejectReservedTagsOnUpdateRejectsReservedTags() {
AwsException exception = assertThrows(
AwsException.class,
() -> ReservedTags.rejectReservedTagsOnUpdate(Map.of(ReservedTags.OVERRIDE_ID_KEY, "my-id"))
);
assertEquals("ValidationException", exception.getErrorCode());
}
}
@@ -17,9 +17,11 @@ import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.matchesRegex;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
@QuarkusTest
@@ -244,6 +246,63 @@ class CloudFormationIntegrationTest {
.body("Configuration.FunctionName", equalTo("cfn-nocode-func"));
}
@Test
void createStack_kmsKeyWithOverrideTagUsesPinnedId() {
String template = """
{
"Resources": {
"MyKey": {
"Type": "AWS::KMS::Key",
"Properties": {
"Description": "cfn override key",
"Tags": [
{ "Key": "floci:override-id", "Value": "cfn-pinned-key" },
{ "Key": "env", "Value": "test" }
]
}
}
}
}
""";
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStack")
.formParam("StackName", "cfn-kms-override-stack")
.formParam("TemplateBody", template)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<StackId>"));
given()
.contentType("application/x-amz-json-1.1")
.header("X-Amz-Target", "TrentService.DescribeKey")
.body("""
{"KeyId":"cfn-pinned-key"}
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("KeyMetadata.KeyId", equalTo("cfn-pinned-key"));
given()
.contentType("application/x-amz-json-1.1")
.header("X-Amz-Target", "TrentService.ListResourceTags")
.body("""
{"KeyId":"cfn-pinned-key"}
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("Tags.TagKey", hasItem("env"))
.body("Tags.find { it.TagKey == 'env' }.TagValue", equalTo("test"))
.body("Tags.find { it.TagKey == 'floci:override-id' }", nullValue());
}
@Test
void createStack_lambdaWithEnvironmentVariables() {
String template = """
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.ReservedTags;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.cognito.model.UserPool;
@@ -100,4 +101,54 @@ class CognitoJsonHandlerTest {
assertNotNull(pool.get("AccountRecoverySetting"));
assertEquals("ESSENTIALS", pool.get("UserPoolTier").asText());
}
@Test
void createUserPoolResponseDoesNotLeakReservedTag() {
ObjectNode request = mapper.createObjectNode();
request.put("PoolName", "pinned-pool");
ObjectNode tags = request.putObject("UserPoolTags");
tags.put(ReservedTags.OVERRIDE_ID_KEY, "us-east-1_testpool1");
tags.put("env", "test");
Response response = handler.handle("CreateUserPool", request, "us-east-1");
assertEquals(200, response.getStatus());
JsonNode body = (JsonNode) response.getEntity();
JsonNode pool = body.get("UserPool");
assertEquals("us-east-1_testpool1", pool.get("Id").asText());
assertEquals("test", pool.get("UserPoolTags").get("env").asText());
assertFalse(pool.get("UserPoolTags").has(ReservedTags.OVERRIDE_ID_KEY));
}
@Test
void updateAndDescribeUserPoolResponsesDoNotLeakReservedTag() {
ObjectNode createRequest = mapper.createObjectNode();
createRequest.put("PoolName", "update-pool");
JsonNode createBody = (JsonNode) handler.handle("CreateUserPool", createRequest, "us-east-1").getEntity();
String poolId = createBody.get("UserPool").get("Id").asText();
ObjectNode updateRequest = mapper.createObjectNode();
updateRequest.put("UserPoolId", poolId);
ObjectNode tags = updateRequest.putObject("UserPoolTags");
tags.put(ReservedTags.OVERRIDE_ID_KEY, "late-id");
tags.put("env", "test");
Response updateResponse = handler.handle("UpdateUserPool", updateRequest, "us-east-1");
assertEquals(200, updateResponse.getStatus());
JsonNode updateBody = (JsonNode) updateResponse.getEntity();
JsonNode updatedPool = updateBody.get("UserPool");
assertEquals("test", updatedPool.get("UserPoolTags").get("env").asText());
assertFalse(updatedPool.get("UserPoolTags").has(ReservedTags.OVERRIDE_ID_KEY));
ObjectNode describeRequest = mapper.createObjectNode();
describeRequest.put("UserPoolId", poolId);
Response describeResponse = handler.handle("DescribeUserPool", describeRequest, "us-east-1");
assertEquals(200, describeResponse.getStatus());
JsonNode describeBody = (JsonNode) describeResponse.getEntity();
JsonNode describedPool = describeBody.get("UserPool");
assertEquals("test", describedPool.get("UserPoolTags").get("env").asText());
assertFalse(describedPool.get("UserPoolTags").has(ReservedTags.OVERRIDE_ID_KEY));
}
}
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.cognito;
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.InMemoryStorage;
import io.github.hectorvent.floci.services.cognito.model.CognitoGroup;
import io.github.hectorvent.floci.services.cognito.model.CognitoUser;
@@ -72,6 +73,125 @@ class CognitoServiceTest {
assertEquals(List.of("email"), pool.getUsernameAttributes());
}
@Test
void createUserPoolWithOverrideIdUsesProvidedId() {
UserPool pool = service.createUserPool(
Map.of(
"PoolName", "PinnedPool",
"UserPoolTags", Map.of(ReservedTags.OVERRIDE_ID_KEY, "us-east-1_testpool1")
),
"us-east-1"
);
assertEquals("us-east-1_testpool1", pool.getId());
assertEquals("arn:aws:cognito-idp:us-east-1:000000000000:userpool/us-east-1_testpool1", pool.getArn());
}
@Test
void createUserPoolWithOverrideIdStripsReservedTagOnCreate() {
UserPool pool = service.createUserPool(
Map.of(
"PoolName", "PinnedPool",
"UserPoolTags", Map.of(ReservedTags.OVERRIDE_ID_KEY, "us-east-1_testpool1", "env", "test")
),
"us-east-1"
);
assertEquals(Map.of("env", "test"), pool.getUserPoolTags());
assertFalse(pool.getUserPoolTags().containsKey(ReservedTags.OVERRIDE_ID_KEY));
}
@Test
void createUserPoolWithDuplicateOverrideIdThrowsResourceConflict() {
service.createUserPool(
Map.of("PoolName", "PinnedPool", "UserPoolTags", Map.of(ReservedTags.OVERRIDE_ID_KEY, "us-east-1_testpool1")),
"us-east-1"
);
AwsException exception = assertThrows(
AwsException.class,
() -> service.createUserPool(
Map.of("PoolName", "PinnedPool2", "UserPoolTags", Map.of(ReservedTags.OVERRIDE_ID_KEY, "us-east-1_testpool1")),
"us-east-1"
)
);
assertEquals("ResourceConflictException", exception.getErrorCode());
}
@Test
void createUserPoolWithBlankOverrideIdThrowsValidation() {
AwsException exception = assertThrows(
AwsException.class,
() -> service.createUserPool(
Map.of("PoolName", "PinnedPool", "UserPoolTags", Map.of(ReservedTags.OVERRIDE_ID_KEY, " ")),
"us-east-1"
)
);
assertEquals("ValidationException", exception.getErrorCode());
}
@Test
void createUserPoolWithSlashInOverrideThrowsValidation() {
AwsException exception = assertThrows(
AwsException.class,
() -> service.createUserPool(
Map.of("PoolName", "PinnedPool", "UserPoolTags", Map.of(ReservedTags.OVERRIDE_ID_KEY, "bad/pool")),
"us-east-1"
)
);
assertEquals("ValidationException", exception.getErrorCode());
}
@Test
void createUserPoolWithQuestionMarkOrHashInOverrideThrowsValidation() {
AwsException questionMarkException = assertThrows(
AwsException.class,
() -> service.createUserPool(
Map.of("PoolName", "PinnedPool", "UserPoolTags", Map.of(ReservedTags.OVERRIDE_ID_KEY, "bad?pool")),
"us-east-1"
)
);
assertEquals("ValidationException", questionMarkException.getErrorCode());
AwsException hashException = assertThrows(
AwsException.class,
() -> service.createUserPool(
Map.of("PoolName", "PinnedPool", "UserPoolTags", Map.of(ReservedTags.OVERRIDE_ID_KEY, "bad#pool")),
"us-east-1"
)
);
assertEquals("ValidationException", hashException.getErrorCode());
}
@Test
void updateUserPoolWithReservedTagStripsIt() {
UserPool pool = service.createUserPool(Map.of("PoolName", "PinnedPool"), "us-east-1");
service.updateUserPool(
Map.of(
"UserPoolId", pool.getId(),
"UserPoolTags", Map.of(ReservedTags.OVERRIDE_ID_KEY, "late-id", "env", "test")
),
"us-east-1"
);
UserPool updated = service.describeUserPool(pool.getId());
assertEquals(Map.of("env", "test"), updated.getUserPoolTags());
}
@Test
void issuerUrlForPinnedPoolResolvesAsBaseUrlSlashPoolId() {
UserPool pool = service.createUserPool(
Map.of("PoolName", "PinnedPool", "UserPoolTags", Map.of(ReservedTags.OVERRIDE_ID_KEY, "custompool")),
"us-east-1"
);
assertEquals("http://localhost:4566/custompool", service.getIssuer(pool.getId()));
}
// =========================================================================
// Groups
// =========================================================================
@@ -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.InMemoryStorage;
import io.github.hectorvent.floci.services.kms.model.KmsAlias;
import io.github.hectorvent.floci.services.kms.model.KmsKey;
@@ -288,6 +289,67 @@ class KmsServiceTest {
assertTrue(key.getTags().isEmpty());
}
@Test
void createKeyWithOverrideIdUsesProvidedId() {
KmsKey key = kmsService.createKey(
"tagged-key",
null,
Map.of(ReservedTags.OVERRIDE_ID_KEY, "my-test-key"),
REGION
);
assertEquals("my-test-key", key.getKeyId());
assertEquals("arn:aws:kms:us-east-1:000000000000:key/my-test-key", key.getArn());
}
@Test
void createKeyWithOverrideIdStripsReservedTagFromStoredKey() {
KmsKey key = kmsService.createKey(
"tagged-key",
null,
Map.of(ReservedTags.OVERRIDE_ID_KEY, "my-test-key", "env", "test"),
REGION
);
KmsKey found = kmsService.describeKey(key.getKeyId(), REGION);
assertEquals("test", found.getTags().get("env"));
assertFalse(found.getTags().containsKey(ReservedTags.OVERRIDE_ID_KEY));
}
@Test
void createKeyWithDuplicateOverrideIdThrowsAlreadyExists() {
kmsService.createKey("first", null, Map.of(ReservedTags.OVERRIDE_ID_KEY, "my-test-key"), REGION);
AwsException exception = assertThrows(
AwsException.class,
() -> kmsService.createKey("second", null, Map.of(ReservedTags.OVERRIDE_ID_KEY, "my-test-key"), REGION)
);
assertEquals("AlreadyExistsException", exception.getErrorCode());
}
@Test
void createKeyWithBlankOverrideIdThrowsValidation() {
AwsException exception = assertThrows(
AwsException.class,
() -> kmsService.createKey("bad", null, Map.of(ReservedTags.OVERRIDE_ID_KEY, " "), REGION)
);
assertEquals("ValidationException", exception.getErrorCode());
}
@Test
void tagResourceWithReservedKeyThrowsValidation() {
KmsKey key = kmsService.createKey(null, REGION);
AwsException exception = assertThrows(
AwsException.class,
() -> kmsService.tagResource(key.getKeyId(), Map.of(ReservedTags.OVERRIDE_ID_KEY, "late-id"), REGION)
);
assertEquals("ValidationException", exception.getErrorCode());
}
// ── Issue #258 — GetKeyPolicy ────────────────────────────────────────────
@Test