feat(kinesis): support stream consumer resource tags (#8057)

* feat(kinesis): support stream consumer resource tags

* fix(kinesis): validate resource tags before updates

* refactor(kinesis): rely on generated tag constraints

* fix(kinesis): align resource tag characters with Tag type

* fix(kinesis): accept colons in resource tags
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-11-12 19:16:10 -04:00
1 parent 91ec6d29ef
commit f41c4ee5c0
7 files changed
+470 -18

No files matched your search

@@ -67,6 +67,9 @@ public class KinesisJsonHandler {
case "AddTagsToStream" -> () -> handleAddTagsToStream(request, region);
case "RemoveTagsFromStream" -> () -> handleRemoveTagsFromStream(request, region);
case "ListTagsForStream" -> () -> handleListTagsForStream(request, region);
case "TagResource" -> () -> handleTagResource(request, region);
case "UntagResource" -> () -> handleUntagResource(request, region);
case "ListTagsForResource" -> () -> handleListTagsForResource(request, region);
case "StartStreamEncryption" -> () -> handleStartStreamEncryption(request, region);
case "StopStreamEncryption" -> () -> handleStopStreamEncryption(request, region);
case "SplitShard" -> () -> handleSplitShard(request, region);
@@ -346,7 +349,8 @@ public class KinesisJsonHandler {
private Response handleRegisterStreamConsumer(JsonNode request, String region) {
String streamArn = request.path("StreamARN").asText();
String consumerName = request.path("ConsumerName").asText();
KinesisConsumer consumer = service.registerStreamConsumer(streamArn, consumerName, region);
Map<String, String> tags = parseTags(request);
KinesisConsumer consumer = service.registerStreamConsumer(streamArn, consumerName, tags, region);
ObjectNode response = objectMapper.createObjectNode();
response.set("Consumer", consumerToNode(consumer));
return Response.ok(response).build();
@@ -467,26 +471,54 @@ public class KinesisJsonHandler {
private Response handleRemoveTagsFromStream(JsonNode request, String region) {
String streamName = resolveStreamName(request);
service.removeTagsFromStream(streamName, parseTagKeys(request.path("TagKeys")), region);
return Response.ok(objectMapper.createObjectNode()).build();
}
private List<String> parseTagKeys(JsonNode tagKeysNode) {
List<String> tagKeys = new ArrayList<>();
for (JsonNode node : request.path("TagKeys")) {
for (JsonNode node : tagKeysNode) {
tagKeys.add(node.textValue());
}
service.removeTagsFromStream(streamName, tagKeys, region);
return Response.ok(objectMapper.createObjectNode()).build();
return tagKeys;
}
private Response handleListTagsForStream(JsonNode request, String region) {
String streamName = resolveStreamName(request);
Map<String, String> tags = service.listTagsForStream(streamName, region);
ObjectNode response = objectMapper.createObjectNode();
ArrayNode tagsArray = response.putArray("Tags");
response.set("Tags", tagsToArray(tags));
response.put("HasMoreTags", false);
return Response.ok(response).build();
}
private Response handleTagResource(JsonNode request, String region) {
Map<String, String> tags = parseTags(request);
service.tagResource(request.path("ResourceARN").asText(null), tags, region);
return Response.ok().build();
}
private Response handleUntagResource(JsonNode request, String region) {
List<String> keys = parseTagKeys(request.path("TagKeys"));
service.untagResource(request.path("ResourceARN").asText(null), keys, region);
return Response.ok().build();
}
private Response handleListTagsForResource(JsonNode request, String region) {
Map<String, String> tags = service.listTagsForResource(request.path("ResourceARN").asText(null), region);
ObjectNode response = objectMapper.createObjectNode();
response.set("Tags", tagsToArray(tags));
return Response.ok(response).build();
}
private ArrayNode tagsToArray(Map<String, String> tags) {
ArrayNode tagsArray = objectMapper.createArrayNode();
tags.forEach((k, v) -> {
ObjectNode tagNode = tagsArray.addObject();
tagNode.put("Key", k);
tagNode.put("Value", v);
});
response.put("HasMoreTags", false);
return Response.ok(response).build();
return tagsArray;
}
private Map<String, String> parseTags(JsonNode request) {
@@ -55,6 +55,8 @@ public class KinesisService implements ResourceProvider {
private static final int MAX_RECORD_SIZE_KIB = 10240;
private static final int MAX_RECORDS_PER_REQUEST = 500;
private static final int MAX_REQUEST_SIZE_BYTES = 10 * 1024 * 1024;
private static final Pattern VALID_RESOURCE_TAG_CHARACTERS =
Pattern.compile("[\\p{L}\\p{Nd}\\s_./=+%@:-]*", Pattern.UNICODE_CHARACTER_CLASS);
/**
* UpdateShardCount's documented default limits: the per-account/region shard-per-stream
* ceiling, and how many scaling calls a stream may take in a rolling 24-hour window.
@@ -221,23 +223,34 @@ public class KinesisService implements ResourceProvider {
}
public KinesisConsumer registerStreamConsumer(String streamArn, String consumerName, String region) {
return registerStreamConsumer(streamArn, consumerName, Map.of(), region);
}
public KinesisConsumer registerStreamConsumer(String streamArn, String consumerName,
Map<String, String> tags, String region) {
validateResourceTagLimit(Map.of(), tags);
String consumerArn = streamArn + "/consumer/" + consumerName + ":" + System.currentTimeMillis();
KinesisConsumer consumer = new KinesisConsumer(consumerName, consumerArn, streamArn);
consumerStore.put(region + "::" + consumerArn, consumer);
consumer.getTags().putAll(tags);
synchronized (consumerStore) {
consumerStore.put(regionKey(region, consumerArn), consumer);
}
LOG.infov("Registered Kinesis consumer: {0} for stream {1}", consumerName, streamArn);
return consumer;
}
public void deregisterStreamConsumer(String streamArn, String consumerName, String consumerArn, String region) {
String resolvedArn = consumerArn;
if (resolvedArn == null && streamArn != null && consumerName != null) {
resolvedArn = consumerStore.scan(k -> true).stream()
.filter(c -> c.getStreamArn().equals(streamArn) && c.getConsumerName().equals(consumerName))
.findFirst().map(KinesisConsumer::getConsumerArn).orElse(null);
}
if (resolvedArn != null) {
consumerStore.delete(region + "::" + resolvedArn);
LOG.infov("Deregistered Kinesis consumer: {0}", resolvedArn);
synchronized (consumerStore) {
String resolvedArn = consumerArn;
if (resolvedArn == null && streamArn != null && consumerName != null) {
resolvedArn = consumerStore.scan(k -> true).stream()
.filter(c -> c.getStreamArn().equals(streamArn) && c.getConsumerName().equals(consumerName))
.findFirst().map(KinesisConsumer::getConsumerArn).orElse(null);
}
if (resolvedArn != null) {
consumerStore.delete(regionKey(region, resolvedArn));
LOG.infov("Deregistered Kinesis consumer: {0}", resolvedArn);
}
}
}
@@ -291,6 +304,95 @@ public class KinesisService implements ResourceProvider {
return resolveStream(streamName, region).getTags();
}
public void tagResource(String resourceArn, Map<String, String> tags, String region) {
if (isConsumerTagResource(resourceArn, region)) {
String key = regionKey(region, resourceArn);
synchronized (consumerStore) {
KinesisConsumer consumer = resolveConsumerForTags(key);
validateResourceTagLimit(consumer.getTags(), tags);
consumer.getTags().putAll(tags);
consumerStore.put(key, consumer);
}
return;
}
String streamName = streamNameFromTagArn(resourceArn);
String key = regionKey(region, streamName);
synchronized (lockFor(key)) {
KinesisStream stream = describeStreamByArn(resourceArn, region);
validateResourceTagLimit(stream.getTags(), tags);
stream.getTags().putAll(tags);
store.put(key, stream);
}
}
public void untagResource(String resourceArn, List<String> tagKeys, String region) {
if (isConsumerTagResource(resourceArn, region)) {
String key = regionKey(region, resourceArn);
synchronized (consumerStore) {
KinesisConsumer consumer = resolveConsumerForTags(key);
tagKeys.forEach(consumer.getTags()::remove);
consumerStore.put(key, consumer);
}
return;
}
String streamName = streamNameFromTagArn(resourceArn);
String key = regionKey(region, streamName);
synchronized (lockFor(key)) {
KinesisStream stream = describeStreamByArn(resourceArn, region);
tagKeys.forEach(stream.getTags()::remove);
store.put(key, stream);
}
}
public Map<String, String> listTagsForResource(String resourceArn, String region) {
if (isConsumerTagResource(resourceArn, region)) {
synchronized (consumerStore) {
return new HashMap<>(resolveConsumerForTags(regionKey(region, resourceArn)).getTags());
}
}
String streamName = streamNameFromTagArn(resourceArn);
synchronized (lockFor(regionKey(region, streamName))) {
return new HashMap<>(describeStreamByArn(resourceArn, region).getTags());
}
}
private boolean isConsumerTagResource(String resourceArn, String region) {
AwsArnUtils.Arn arn = AwsArnUtils.parseIfArnIn(resourceArn, "kinesis", region)
.orElseThrow(() -> new AwsException("InvalidArgumentException",
"ResourceARN must name a Kinesis resource in this region", 400));
if (!arn.accountId().equals(regionResolver.getAccountId())) {
throw new AwsException("ResourceNotFoundException", "Kinesis resource not found", 400);
}
if (!arn.resource().startsWith("stream/")) {
throw new AwsException("InvalidArgumentException", "ResourceARN must name a stream or consumer", 400);
}
return arn.resource().contains("/consumer/");
}
private String streamNameFromTagArn(String resourceArn) {
return AwsArnUtils.parse(resourceArn).resource().substring("stream/".length());
}
private KinesisConsumer resolveConsumerForTags(String key) {
return consumerStore.get(key)
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Consumer not found", 400));
}
private void validateResourceTagLimit(Map<String, String> current, Map<String, String> added) {
for (Map.Entry<String, String> entry : added.entrySet()) {
if (entry.getKey() == null || !VALID_RESOURCE_TAG_CHARACTERS.matcher(entry.getKey()).matches()
|| entry.getKey().regionMatches(true, 0, "aws:", 0, 4)
|| entry.getValue() == null || !VALID_RESOURCE_TAG_CHARACTERS.matcher(entry.getValue()).matches()) {
throw new AwsException("InvalidArgumentException", "Invalid tag key or value", 400);
}
}
Set<String> keys = new HashSet<>(current.keySet());
keys.addAll(added.keySet());
if (keys.size() > 50 && added.keySet().stream().anyMatch(key -> !current.containsKey(key))) {
throw new AwsException("LimitExceededException", "A Kinesis resource can have at most 50 tags", 400);
}
}
public void startStreamEncryption(String streamName, String encryptionType, String keyId, String region) {
String key = regionKey(region, streamName);
synchronized (lockFor(key)) {
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
@@ -13,6 +15,7 @@ public class KinesisConsumer {
private String consumerStatus;
private Instant consumerCreationTimestamp;
private String streamArn;
private Map<String, String> tags = new HashMap<>();
public KinesisConsumer() {}
@@ -38,4 +41,7 @@ public class KinesisConsumer {
public String getStreamArn() { return streamArn; }
public void setStreamArn(String streamArn) { this.streamArn = streamArn; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags == null ? new HashMap<>() : tags; }
}