fix(kinesis): apply CreateStream's Tags instead of dropping them (#2494)

* fix(kinesis): apply CreateStream's Tags instead of dropping them

CreateStream parsed StreamName, ShardCount, StreamModeDetails and
MaxRecordSizeInKiB and silently ignored the optional Tags member, so a stream
created with tags came back untagged from ListTagsForStream. AddTagsToStream
already worked, so only the create path was affected.

A hand-written script would not notice. Terraform does: aws_kinesis_stream sets
tags at create time and then reads them back, so every plan after a successful
apply reports "tags will be updated in-place" on an unchanged configuration.
That is what fails terraform-aws-messaging's TestKinesis, whose four subtests all
assert `terraform plan` returns exit code 0 after apply -- every other assertion
in that test (shard count, encryption type, retention period) already passed.

* refactor(kinesis): parse CreateStream and AddTagsToStream tags with one helper

The create-time tag support added in the previous commit duplicated the three
lines AddTagsToStream already used to turn the request's Tags object into a
map. Two copies of the same parse is two things to keep in step, and this
service now has three tag operations, so extract parseTags() the way
CodeBuild's and SecretsManager's JSON handlers already do.

No behaviour change: a missing or non-object Tags member still yields an empty
map rather than an error, and CreateStream still skips the store write when
that map is empty. Greptile flagged that asText() coerces a non-string tag
value ("null", "3", "") instead of rejecting it. That is true, but it is
AddTagsToStream's pre-existing behaviour, not something this PR introduced --
sharing one parser is what keeps the two paths from answering the same
malformed request differently. Rejecting non-string tag values is a separate
input-validation fix that should land on its own, across every tag operation.

Adds handler-level unit tests, which the class had none of for tags: create
with tags, create without, create with an empty Tags object, and AddTagsToStream
applying the same map. Unlike the ordered integration tests these get a fresh
service per test.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-08-24 17:39:59 -04:00
1 parent feb669d58b
commit 564dbf204f
3 files changed
+126 -3

No files matched your search

@@ -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)
@@ -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));
}
}