mirror of
https://github.com/floci-io/floci.git
synced 2026-11-29 16:35:29 -05:00
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:
1 parent
91ec6d29ef
commit
f41c4ee5c0
7 files changed
+470
-18
No files matched your search
@@ -15,6 +15,8 @@ import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
@@ -26,6 +28,7 @@ import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
@@ -132,6 +135,243 @@ class KinesisJsonHandlerTest {
|
||||
assertThat(handler.handle("CreateStream", req, REGION).getStatus(), is(200));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerStreamConsumerPreservesTagsForResourceOperations() {
|
||||
createStream("test-stream");
|
||||
ObjectNode registration = MAPPER.createObjectNode()
|
||||
.put("StreamARN", STREAM_ARN)
|
||||
.put("ConsumerName", "tagged-consumer");
|
||||
registration.putObject("Tags").put("team", "red");
|
||||
String consumerArn = responseEntity(handler.handle("RegisterStreamConsumer", registration, REGION))
|
||||
.path("Consumer").path("ConsumerARN").asText();
|
||||
ObjectNode resource = MAPPER.createObjectNode().put("ResourceARN", consumerArn);
|
||||
|
||||
Response listed = handler.handle("ListTagsForResource", resource, REGION);
|
||||
assertEquals(200, listed.getStatus());
|
||||
assertEquals("team", responseEntity(listed).path("Tags").get(0).path("Key").asText());
|
||||
assertEquals("red", responseEntity(listed).path("Tags").get(0).path("Value").asText());
|
||||
|
||||
ObjectNode tagRequest = resource.deepCopy();
|
||||
tagRequest.putObject("Tags").put("env", "dev");
|
||||
assertEquals(200, handler.handle("TagResource", tagRequest, REGION).getStatus());
|
||||
JsonNode tags = responseEntity(handler.handle("ListTagsForResource", resource, REGION)).path("Tags");
|
||||
assertEquals(2, tags.size());
|
||||
assertEquals("red", tagValue(tags, "team"));
|
||||
assertEquals("dev", tagValue(tags, "env"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceTagOperationsKeepStreamAndConsumerTagsSeparate() {
|
||||
createStream("test-stream");
|
||||
String consumerArn = service.registerStreamConsumer(STREAM_ARN, "tagged-consumer", REGION)
|
||||
.getConsumerArn();
|
||||
ObjectNode streamRequest = MAPPER.createObjectNode().put("ResourceARN", STREAM_ARN);
|
||||
streamRequest.putObject("Tags").put("owner", "stream");
|
||||
assertEquals(200, handler.handle("TagResource", streamRequest, REGION).getStatus());
|
||||
ObjectNode consumerRequest = MAPPER.createObjectNode().put("ResourceARN", consumerArn);
|
||||
consumerRequest.putObject("Tags").put("owner", "consumer");
|
||||
assertEquals(200, handler.handle("TagResource", consumerRequest, REGION).getStatus());
|
||||
|
||||
ObjectNode streamArnOnly = MAPPER.createObjectNode().put("ResourceARN", STREAM_ARN);
|
||||
ObjectNode consumerArnOnly = MAPPER.createObjectNode().put("ResourceARN", consumerArn);
|
||||
assertEquals("stream", tagValue(responseEntity(handler.handle("ListTagsForResource", streamArnOnly, REGION))
|
||||
.path("Tags"), "owner"));
|
||||
assertEquals("consumer", tagValue(responseEntity(handler.handle("ListTagsForResource", consumerArnOnly, REGION))
|
||||
.path("Tags"), "owner"));
|
||||
|
||||
consumerArnOnly.putArray("TagKeys").add("owner");
|
||||
assertEquals(200, handler.handle("UntagResource", consumerArnOnly, REGION).getStatus());
|
||||
consumerArnOnly.remove("TagKeys");
|
||||
assertEquals(0, responseEntity(handler.handle("ListTagsForResource", consumerArnOnly, REGION))
|
||||
.path("Tags").size());
|
||||
assertEquals("stream", tagValue(responseEntity(handler.handle("ListTagsForResource", streamArnOnly, REGION))
|
||||
.path("Tags"), "owner"));
|
||||
ObjectNode legacyList = MAPPER.createObjectNode().put("StreamName", "test-stream");
|
||||
assertEquals("stream", tagValue(responseEntity(handler.handle("ListTagsForStream", legacyList, REGION))
|
||||
.path("Tags"), "owner"));
|
||||
|
||||
streamArnOnly.putArray("TagKeys").add("owner");
|
||||
assertEquals(200, handler.handle("UntagResource", streamArnOnly, REGION).getStatus());
|
||||
streamArnOnly.remove("TagKeys");
|
||||
assertEquals(0, responseEntity(handler.handle("ListTagsForResource", streamArnOnly, REGION))
|
||||
.path("Tags").size());
|
||||
assertEquals(0, responseEntity(handler.handle("ListTagsForResource", consumerArnOnly, REGION))
|
||||
.path("Tags").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceTagsRejectForeignAndMissingResources() {
|
||||
createStream("test-stream");
|
||||
String consumerArn = service.registerStreamConsumer(STREAM_ARN, "tagged-consumer", REGION)
|
||||
.getConsumerArn();
|
||||
ObjectNode request = MAPPER.createObjectNode();
|
||||
|
||||
request.put("ResourceARN", consumerArn.replace(ACCOUNT, "999999999999"));
|
||||
AwsException foreign = assertThrows(AwsException.class,
|
||||
() -> handler.handle("ListTagsForResource", request, REGION));
|
||||
assertEquals("ResourceNotFoundException", foreign.getErrorCode());
|
||||
|
||||
request.put("ResourceARN", consumerArn.replace(REGION, "us-west-2"));
|
||||
AwsException otherRegion = assertThrows(AwsException.class,
|
||||
() -> handler.handle("ListTagsForResource", request, REGION));
|
||||
assertEquals("InvalidArgumentException", otherRegion.getErrorCode());
|
||||
|
||||
service.deregisterStreamConsumer(null, null, consumerArn, REGION);
|
||||
request.put("ResourceARN", consumerArn);
|
||||
AwsException deleted = assertThrows(AwsException.class,
|
||||
() -> handler.handle("ListTagsForResource", request, REGION));
|
||||
assertEquals("ResourceNotFoundException", deleted.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceTagsEnforceCapacityWithoutPartialWrites() {
|
||||
createStream("test-stream");
|
||||
ObjectNode request = MAPPER.createObjectNode().put("ResourceARN", STREAM_ARN);
|
||||
ObjectNode tags = request.putObject("Tags");
|
||||
for (int i = 0; i < 50; i++) {
|
||||
tags.put("key" + i, "value");
|
||||
}
|
||||
assertEquals(200, handler.handle("TagResource", request, REGION).getStatus());
|
||||
|
||||
request.set("Tags", MAPPER.createObjectNode().put("overflow", "value"));
|
||||
AwsException overLimit = assertThrows(AwsException.class,
|
||||
() -> handler.handle("TagResource", request, REGION));
|
||||
assertEquals("LimitExceededException", overLimit.getErrorCode());
|
||||
ObjectNode list = MAPPER.createObjectNode().put("ResourceARN", STREAM_ARN);
|
||||
assertEquals(50, responseEntity(handler.handle("ListTagsForResource", list, REGION)).path("Tags").size());
|
||||
|
||||
request.set("Tags", MAPPER.createObjectNode().put("key0", "updated"));
|
||||
assertEquals(200, handler.handle("TagResource", request, REGION).getStatus());
|
||||
assertEquals("updated", tagValue(responseEntity(handler.handle("ListTagsForResource", list, REGION))
|
||||
.path("Tags"), "key0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagResourceRejectsInvalidCharactersWithoutChangingEitherResource() {
|
||||
createStream("test-stream");
|
||||
String consumerArn = service.registerStreamConsumer(STREAM_ARN, "tagged-consumer", REGION)
|
||||
.getConsumerArn();
|
||||
for (String arn : List.of(STREAM_ARN, consumerArn)) {
|
||||
ObjectNode request = MAPPER.createObjectNode().put("ResourceARN", arn);
|
||||
request.putObject("Tags").put("valid", "initial");
|
||||
assertEquals(200, handler.handle("TagResource", request, REGION).getStatus());
|
||||
|
||||
for (String key : List.of("bad*key")) {
|
||||
request.set("Tags", MAPPER.createObjectNode().put(key, "value"));
|
||||
AwsException badKey = assertThrows(AwsException.class,
|
||||
() -> handler.handle("TagResource", request, REGION));
|
||||
assertEquals("InvalidArgumentException", badKey.getErrorCode());
|
||||
}
|
||||
|
||||
for (String value : List.of("bad*value")) {
|
||||
request.set("Tags", MAPPER.createObjectNode().put("valid", value));
|
||||
AwsException badValue = assertThrows(AwsException.class,
|
||||
() -> handler.handle("TagResource", request, REGION));
|
||||
assertEquals("InvalidArgumentException", badValue.getErrorCode());
|
||||
}
|
||||
|
||||
JsonNode tags = responseEntity(handler.handle("ListTagsForResource",
|
||||
MAPPER.createObjectNode().put("ResourceARN", arn), REGION)).path("Tags");
|
||||
assertEquals(1, tags.size());
|
||||
assertEquals("initial", tagValue(tags, "valid"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagResourceAcceptsDocumentedUnicodeAndSymbols() {
|
||||
createStream("test-stream");
|
||||
String consumerArn = service.registerStreamConsumer(STREAM_ARN, "tagged-consumer", REGION)
|
||||
.getConsumerArn();
|
||||
String key = "\u00c9quipe_./=+@-:%9";
|
||||
String value = "\u7a7a \u767d%:";
|
||||
for (String arn : List.of(STREAM_ARN, consumerArn)) {
|
||||
ObjectNode request = MAPPER.createObjectNode().put("ResourceARN", arn);
|
||||
request.putObject("Tags").put(key, value);
|
||||
assertEquals(200, handler.handle("TagResource", request, REGION).getStatus());
|
||||
JsonNode tags = responseEntity(handler.handle("ListTagsForResource",
|
||||
MAPPER.createObjectNode().put("ResourceARN", arn), REGION)).path("Tags");
|
||||
assertEquals(value, tagValue(tags, key));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagResourceRejectsReservedTagKeyPrefix() {
|
||||
createStream("test-stream");
|
||||
String consumerArn = service.registerStreamConsumer(STREAM_ARN, "tagged-consumer", REGION)
|
||||
.getConsumerArn();
|
||||
for (String arn : List.of(STREAM_ARN, consumerArn)) {
|
||||
ObjectNode request = MAPPER.createObjectNode().put("ResourceARN", arn);
|
||||
request.putObject("Tags").put("AWS:owner", "user");
|
||||
AwsException reserved = assertThrows(AwsException.class,
|
||||
() -> handler.handle("TagResource", request, REGION));
|
||||
assertEquals("InvalidArgumentException", reserved.getErrorCode());
|
||||
assertEquals(0, responseEntity(handler.handle("ListTagsForResource",
|
||||
MAPPER.createObjectNode().put("ResourceARN", arn), REGION)).path("Tags").size());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagResourceUpdatesLegacyStreamBeyondCapacityWithoutAddingKeys() {
|
||||
createStream("test-stream");
|
||||
ObjectNode add = MAPPER.createObjectNode().put("StreamName", "test-stream");
|
||||
ObjectNode legacyTags = add.putObject("Tags");
|
||||
for (int i = 0; i < 51; i++) {
|
||||
legacyTags.put("key" + i, "old");
|
||||
}
|
||||
assertEquals(200, handler.handle("AddTagsToStream", add, REGION).getStatus());
|
||||
|
||||
ObjectNode request = MAPPER.createObjectNode().put("ResourceARN", STREAM_ARN);
|
||||
request.putObject("Tags").put("key0", "updated");
|
||||
assertEquals(200, handler.handle("TagResource", request, REGION).getStatus());
|
||||
|
||||
request.set("Tags", MAPPER.createObjectNode().put("key1", "would-change").put("new", "value"));
|
||||
AwsException overLimit = assertThrows(AwsException.class,
|
||||
() -> handler.handle("TagResource", request, REGION));
|
||||
assertEquals("LimitExceededException", overLimit.getErrorCode());
|
||||
JsonNode tags = responseEntity(handler.handle("ListTagsForResource",
|
||||
MAPPER.createObjectNode().put("ResourceARN", STREAM_ARN), REGION)).path("Tags");
|
||||
assertEquals(51, tags.size());
|
||||
assertEquals("updated", tagValue(tags, "key0"));
|
||||
assertEquals("old", tagValue(tags, "key1"));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> rejectedRegistrationTags() {
|
||||
ObjectNode tooManyTags = MAPPER.createObjectNode();
|
||||
for (int i = 0; i < 51; i++) {
|
||||
tooManyTags.put("key" + i, "value");
|
||||
}
|
||||
return Stream.of(
|
||||
Arguments.of("empty map", MAPPER.createObjectNode(), "InvalidArgumentException"),
|
||||
Arguments.of("invalid key", MAPPER.createObjectNode().put("bad*key", "value"),
|
||||
"InvalidArgumentException"),
|
||||
Arguments.of("reserved prefix", MAPPER.createObjectNode().put("aws:owner", "user"),
|
||||
"InvalidArgumentException"),
|
||||
Arguments.of("over capacity", tooManyTags, "LimitExceededException"));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("rejectedRegistrationTags")
|
||||
void invalidRegistrationTagsDoNotCreateConsumer(String scenario, JsonNode tags, String errorCode) {
|
||||
createStream("test-stream");
|
||||
ObjectNode request = MAPPER.createObjectNode()
|
||||
.put("StreamARN", STREAM_ARN)
|
||||
.put("ConsumerName", "tagged-consumer");
|
||||
request.set("Tags", tags);
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> handler.handle("RegisterStreamConsumer", request, REGION));
|
||||
assertEquals(errorCode, error.getErrorCode());
|
||||
assertTrue(service.listStreamConsumers(STREAM_ARN, REGION).isEmpty());
|
||||
}
|
||||
|
||||
private String tagValue(JsonNode tags, String key) {
|
||||
for (JsonNode tag : tags) {
|
||||
if (key.equals(tag.path("Key").asText())) {
|
||||
return tag.path("Value").asText();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private ObjectNode responseEntity(Response response) {
|
||||
return (ObjectNode) response.getEntity();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user