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

@@ -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<String, String> 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<String, String> 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<String, String> parseTags(JsonNode request) {
Map<String, String> 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();
@@ -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));
}
}