diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java index a3afa5aace..191990ef1d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java @@ -116,6 +116,14 @@ public class KinesisJsonHandler { } service.createStream(streamName, shardCount, streamMode, optionalMaxRecordSize(request), region); + // CreateStream's optional Tags member was being dropped. That is invisible to a + // hand-written script but not to Terraform: aws_kinesis_stream sets tags at create + // time and then reads them back with ListTagsForStream, so an empty read produces a + // permanent "tags will be updated in-place" diff on an unchanged configuration. + Map tags = parseTags(request); + if (!tags.isEmpty()) { + service.addTagsToStream(streamName, tags, region); + } return Response.ok(objectMapper.createObjectNode()).build(); } @@ -388,9 +396,7 @@ public class KinesisJsonHandler { private Response handleAddTagsToStream(JsonNode request, String region) { String streamName = resolveStreamName(request); - Map tags = new HashMap<>(); - request.path("Tags").fields().forEachRemaining(entry -> tags.put(entry.getKey(), entry.getValue().asText())); - service.addTagsToStream(streamName, tags, region); + service.addTagsToStream(streamName, parseTags(request), region); return Response.ok(objectMapper.createObjectNode()).build(); } @@ -416,6 +422,17 @@ public class KinesisJsonHandler { return Response.ok(response).build(); } + // Shared by CreateStream and AddTagsToStream so the two paths cannot drift: the same + // request shape has to produce the same stored tags whichever operation carries it. + // A missing or non-object Tags member yields an empty map rather than an error, which + // is what AddTagsToStream already did before CreateStream started calling this. + private Map parseTags(JsonNode request) { + Map tags = new HashMap<>(); + request.path("Tags").fields() + .forEachRemaining(entry -> tags.put(entry.getKey(), entry.getValue().asText())); + return tags; + } + private Response handleStartStreamEncryption(JsonNode request, String region) { String streamName = resolveStreamName(request); String type = request.path("EncryptionType").asText(); diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisIntegrationTest.java index eda060f482..7e025ceb4b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisIntegrationTest.java @@ -1133,6 +1133,66 @@ class KinesisIntegrationTest { .body("Records.size()", equalTo(0)); } + @Test + @Order(65) + void createStreamAppliesTagsFromTheRequest() { + // CreateStream's Tags member used to be dropped, which Terraform surfaces as a + // permanent tags diff: aws_kinesis_stream tags at create time, then reads back + // with ListTagsForStream and finds nothing. + given() + .header("X-Amz-Target", "Kinesis_20131202.CreateStream") + .contentType(KINESIS_CONTENT_TYPE) + .body(""" + {"StreamName": "create-with-tags", "ShardCount": 1, + "Tags": {"Foo": "Bar", "gw:example": "kinesis"}} + """) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "Kinesis_20131202.ListTagsForStream") + .contentType(KINESIS_CONTENT_TYPE) + .body(""" + {"StreamName": "create-with-tags"} + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("Tags.size()", equalTo(2)) + .body("Tags.find { it.Key == 'Foo' }.Value", equalTo("Bar")) + .body("Tags.find { it.Key == 'gw:example' }.Value", equalTo("kinesis")); + } + + @Test + @Order(66) + void createStreamWithoutTagsLeavesTheStreamUntagged() { + given() + .header("X-Amz-Target", "Kinesis_20131202.CreateStream") + .contentType(KINESIS_CONTENT_TYPE) + .body(""" + {"StreamName": "create-without-tags", "ShardCount": 1} + """) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "Kinesis_20131202.ListTagsForStream") + .contentType(KINESIS_CONTENT_TYPE) + .body(""" + {"StreamName": "create-without-tags"} + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("Tags.size()", equalTo(0)); + } + private JsonNode decodeFirstEventStreamMessage(byte[] data) throws Exception { ByteBuffer buf = ByteBuffer.wrap(data); // Skip the first message (initial-response) diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandlerTest.java index 656a3318b9..3cfbc90054 100644 --- a/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandlerTest.java @@ -13,6 +13,7 @@ import org.junit.jupiter.api.Test; import java.math.BigDecimal; import java.time.Instant; import java.util.Base64; +import java.util.Map; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; @@ -876,4 +877,49 @@ class KinesisJsonHandlerTest { () -> handler.handle("UpdateMaxRecordSize", update, REGION)); assertEquals("ResourceNotFoundException", ex.getErrorCode()); } + + @Test + void createStreamAppliesTagsFromTheRequest() { + ObjectNode create = MAPPER.createObjectNode(); + create.put("StreamName", "tagged-stream"); + create.put("ShardCount", 1); + create.putObject("Tags").put("Foo", "Bar").put("gw:example", "kinesis"); + assertThat(handler.handle("CreateStream", create, REGION).getStatus(), is(200)); + + assertEquals(Map.of("Foo", "Bar", "gw:example", "kinesis"), + service.listTagsForStream("tagged-stream", REGION)); + } + + @Test + void createStreamWithoutTagsLeavesTheStreamUntagged() { + createStream("test-stream"); + + assertTrue(service.listTagsForStream("test-stream", REGION).isEmpty()); + } + + /** An empty Tags object is not an error; it simply leaves the stream untagged. */ + @Test + void createStreamWithAnEmptyTagsObjectLeavesTheStreamUntagged() { + ObjectNode create = MAPPER.createObjectNode(); + create.put("StreamName", "empty-tags-stream"); + create.put("ShardCount", 1); + create.putObject("Tags"); + assertThat(handler.handle("CreateStream", create, REGION).getStatus(), is(200)); + + assertTrue(service.listTagsForStream("empty-tags-stream", REGION).isEmpty()); + } + + /** CreateStream and AddTagsToStream share one parser, so the same map has to land either way. */ + @Test + void addTagsToStreamAppliesTheSameTagsCreateStreamWould() { + createStream("test-stream"); + + ObjectNode add = MAPPER.createObjectNode(); + add.put("StreamName", "test-stream"); + add.putObject("Tags").put("Foo", "Bar").put("gw:example", "kinesis"); + assertThat(handler.handle("AddTagsToStream", add, REGION).getStatus(), is(200)); + + assertEquals(Map.of("Foo", "Bar", "gw:example", "kinesis"), + service.listTagsForStream("test-stream", REGION)); + } }