feat(firehose): add StartDeliveryStreamEncryption and StopDeliveryStreamEncryption (#2583)

* feat(firehose): add Start/StopDeliveryStreamEncryption

Models DeliveryStreamEncryptionConfiguration on the stream description so
the encryption state set by Start is visible to Describe and cleared by Stop.
Limitations documented in docs/services/firehose.md.

* fix(firehose): keep the key identity on StopDeliveryStreamEncryption and reject Start without an encryption input

- Stopping customer-managed encryption now preserves KeyType and KeyARN with
  Status DISABLED, matching what DescribeDeliveryStream reports on real AWS.
- Streams persisted before the encryption field existed deserialize to the
  AWS-shaped DISABLED default instead of null via a field-level initializer.
- StartDeliveryStreamEncryption without DeliveryStreamEncryptionConfigurationInput
  is now rejected with InvalidArgumentException instead of silently defaulting.

* fix(firehose): reject a KeyARN that is not a KMS key ARN

StartDeliveryStreamEncryption stored DeliveryStreamEncryptionConfigurationInput's
KeyARN verbatim, so a bare key id or an alias ARN was accepted and then reported
back by DescribeDeliveryStream as the stream's encryption key. botocore pins
AWSKMSKeyARNForSSE to arn:.*:kms:<region>:<12-digit account>:key/<id> with a
512-character cap; anything else is an InvalidArgumentException on real AWS.

* fix(firehose): enforce the modeled input constraints on the pre-existing operations

DeliveryStreamType is now checked against the modeled enum, and Tags,
Records and TagKeys against their modeled list bounds (1-50, 1-500,
1-50), all answering InvalidArgumentException as the operations model.
Previously all four were accepted verbatim. Included here because this
PR already carries wire-shape hardening for the same handler; found by
a model-grounded constraint sweep.

* fix(firehose): treat StartDeliveryStreamEncryption input as optional

DeliveryStreamEncryptionConfigurationInput is Required: No in the model; omitting
it is the documented way to enable SSE with the service-owned key and defaults
KeyType to AWS_OWNED_CMK. The prior null-input rejection regressed that path -
inverted the pinning test to assert the 200/AWS_OWNED_CMK behavior.
This commit is contained in:
Sam Fakhreddine authored and GitHub committed 2026-08-27 16:48:12 -04:00
1 parent 20ecb89155
commit 4affa05cea
6 files changed
+458 -8

No files matched your search

@@ -53,12 +53,17 @@ public class FirehoseJsonHandler {
}
List<io.github.hectorvent.floci.services.firehose.model.DeliveryStreamDescription.Tag> tags = new ArrayList<>();
if (request.has("Tags")) {
requireListSize(request.get("Tags"), "Tags", 1, 50);
for (JsonNode tNode : request.get("Tags")) {
tags.add(mapper.treeToValue(tNode, io.github.hectorvent.floci.services.firehose.model.DeliveryStreamDescription.Tag.class));
}
}
String deliveryStreamType = request.has("DeliveryStreamType")
? request.get("DeliveryStreamType").asText() : null;
if (deliveryStreamType != null && !DELIVERY_STREAM_TYPES.contains(deliveryStreamType)) {
throw new AwsException("InvalidArgumentException",
"DeliveryStreamType must be one of " + DELIVERY_STREAM_TYPES + ".", 400);
}
KinesisStreamSource source = request.has("KinesisStreamSourceConfiguration")
? mapper.treeToValue(request.get("KinesisStreamSourceConfiguration"), KinesisStreamSource.class)
: null;
@@ -80,6 +85,31 @@ public class FirehoseJsonHandler {
firehoseService.updateDestination(name, currentVersionId, destinationId, update);
yield Response.ok(Map.of()).build();
}
case "StartDeliveryStreamEncryption" -> {
String name = getDeliveryStreamName(request);
JsonNode input = request.get("DeliveryStreamEncryptionConfigurationInput");
// DeliveryStreamEncryptionConfigurationInput is optional (Required: No):
// omitting it is the documented way to enable SSE with the service-owned
// key (KeyType defaults to AWS_OWNED_CMK). KeyType is only required when
// the input object itself is present.
String keyType = null;
String keyArn = null;
if (input != null && !input.isNull()) {
keyType = input.has("KeyType") && !input.get("KeyType").isNull()
? input.get("KeyType").asText() : null;
keyArn = input.has("KeyARN") && !input.get("KeyARN").isNull()
? input.get("KeyARN").asText() : null;
if (keyType == null || keyType.isBlank()) {
throw new AwsException("InvalidArgumentException", "KeyType is required.", 400);
}
}
firehoseService.startDeliveryStreamEncryption(name, keyType, keyArn);
yield Response.ok(Map.of()).build();
}
case "StopDeliveryStreamEncryption" -> {
firehoseService.stopDeliveryStreamEncryption(getDeliveryStreamName(request));
yield Response.ok(Map.of()).build();
}
case "DescribeDeliveryStream" -> {
String name = getDeliveryStreamName(request);
var desc = firehoseService.describeDeliveryStream(name);
@@ -104,6 +134,7 @@ public class FirehoseJsonHandler {
case "PutRecordBatch" -> {
String name = getDeliveryStreamName(request);
List<Record> records = new ArrayList<>();
requireListSize(request.get("Records"), "Records", 1, 500);
for (JsonNode recordNode : request.get("Records")) {
records.add(mapper.treeToValue(recordNode, Record.class));
}
@@ -116,10 +147,9 @@ public class FirehoseJsonHandler {
case "TagDeliveryStream" -> {
String name = getDeliveryStreamName(request);
List<io.github.hectorvent.floci.services.firehose.model.DeliveryStreamDescription.Tag> tags = new ArrayList<>();
if (request.has("Tags")) {
for (JsonNode tNode : request.get("Tags")) {
tags.add(mapper.treeToValue(tNode, io.github.hectorvent.floci.services.firehose.model.DeliveryStreamDescription.Tag.class));
}
requireListSize(request.get("Tags"), "Tags", 1, 50);
for (JsonNode tNode : request.get("Tags")) {
tags.add(mapper.treeToValue(tNode, io.github.hectorvent.floci.services.firehose.model.DeliveryStreamDescription.Tag.class));
}
firehoseService.tagDeliveryStream(name, tags);
yield Response.ok(Map.of()).build();
@@ -127,10 +157,9 @@ public class FirehoseJsonHandler {
case "UntagDeliveryStream" -> {
String name = getDeliveryStreamName(request);
List<String> keys = new ArrayList<>();
if (request.has("TagKeys")) {
for (JsonNode kNode : request.get("TagKeys")) {
keys.add(kNode.asText());
}
requireListSize(request.get("TagKeys"), "TagKeys", 1, 50);
for (JsonNode kNode : request.get("TagKeys")) {
keys.add(kNode.asText());
}
firehoseService.untagDeliveryStream(name, keys);
yield Response.ok(Map.of()).build();
@@ -164,4 +193,21 @@ public class FirehoseJsonHandler {
default -> throw new AwsException("InvalidAction", "Action " + action + " is not supported", 400);
};
}
/** DeliveryStreamType values the 2015-08-04 model enumerates. */
private static final java.util.Set<String> DELIVERY_STREAM_TYPES = java.util.Set.of(
"DirectPut", "KinesisStreamAsSource", "MSKAsSource", "DatabaseAsSource");
/**
* Enforces a modeled list min/max. A missing member on the tag operations is
* treated as the modeled min-1 violation, matching real Firehose, which
* requires the list on TagDeliveryStream/UntagDeliveryStream/PutRecordBatch.
*/
private static void requireListSize(JsonNode list, String member, int min, int max) {
int size = list == null || list.isNull() || !list.isArray() ? 0 : list.size();
if (size < min || size > max) {
throw new AwsException("InvalidArgumentException",
member + " must contain between " + min + " and " + max + " entries.", 400);
}
}
}
@@ -48,6 +48,15 @@ public class FirehoseService implements ResourceProvider {
private static final String DEFAULT_BUCKET = "floci-firehose-results";
private static final int DEFAULT_BUFFERING_INTERVAL_SECONDS = 300;
private static final int DEFAULT_BUFFERING_SIZE_MBS = 5;
/**
* botocore's {@code AWSKMSKeyARNForSSE} shape: a KMS <em>key</em> ARN, not an alias and
* not a bare key id. Accepting anything else would let DescribeDeliveryStream report an
* encryption key that could never exist, where real AWS rejects the call outright.
*/
private static final java.util.regex.Pattern SSE_KEY_ARN_PATTERN = java.util.regex.Pattern.compile(
"arn:.*:kms:[a-zA-Z0-9\\-]+:\\d{12}:key/[a-zA-Z_0-9+=,.@\\-_/]+");
private static final int SSE_KEY_ARN_MAX_LENGTH = 512;
// Per shard, per tick. GetRecords caps at 1000 anyway; a smaller page keeps one
// busy source stream from monopolising the single flusher thread.
private static final int SOURCE_POLL_LIMIT = 500;
@@ -257,6 +266,45 @@ public class FirehoseService implements ResourceProvider {
LOG.infov("Updated destination {0} of Firehose delivery stream {1}", destinationId, name);
}
public void startDeliveryStreamEncryption(String name, String keyType, String keyArn) {
DeliveryStreamDescription stream = describeDeliveryStream(name);
String effectiveKeyType = keyType == null ? "AWS_OWNED_CMK" : keyType;
if (!effectiveKeyType.equals("AWS_OWNED_CMK") && !effectiveKeyType.equals("CUSTOMER_MANAGED_CMK")) {
throw new AwsException("InvalidArgumentException",
"KeyType must be AWS_OWNED_CMK or CUSTOMER_MANAGED_CMK.", 400);
}
if (effectiveKeyType.equals("CUSTOMER_MANAGED_CMK") && (keyArn == null || keyArn.isBlank())) {
throw new AwsException("InvalidArgumentException",
"KeyARN is required for CUSTOMER_MANAGED_CMK.", 400);
}
if (keyArn != null && !keyArn.isBlank()
&& (keyArn.length() > SSE_KEY_ARN_MAX_LENGTH || !SSE_KEY_ARN_PATTERN.matcher(keyArn).matches())) {
throw new AwsException("InvalidArgumentException",
"KeyARN is not a valid KMS key ARN: " + keyArn, 400);
}
stream.setDeliveryStreamEncryptionConfiguration(
new DeliveryStreamDescription.DeliveryStreamEncryptionConfiguration(
effectiveKeyType,
effectiveKeyType.equals("CUSTOMER_MANAGED_CMK") ? keyArn : null,
"ENABLED"));
streamStore.put(name, stream);
}
public void stopDeliveryStreamEncryption(String name) {
DeliveryStreamDescription stream = describeDeliveryStream(name);
DeliveryStreamDescription.DeliveryStreamEncryptionConfiguration current =
stream.getDeliveryStreamEncryptionConfiguration();
String keyType = current == null ? "AWS_OWNED_CMK" : current.getKeyType();
// Keep the customer key identity on disable: real AWS still reports the KeyARN
// of a stopped CUSTOMER_MANAGED_CMK stream, and dropping it here would make a
// later DescribeDeliveryStream forget which key the stream was encrypted with.
String keyArn = current == null ? null : current.getKeyArn();
stream.setDeliveryStreamEncryptionConfiguration(
new DeliveryStreamDescription.DeliveryStreamEncryptionConfiguration(
keyType, keyArn, "DISABLED"));
streamStore.put(name, stream);
}
// A corrupt persisted version can only reach here when the caller echoed it
// (the equality check above passed), so self-heal instead of failing with a 500
// or blaming the client.
@@ -40,6 +40,13 @@ public class DeliveryStreamDescription {
@JsonProperty("Tags")
private List<Tag> tags = new ArrayList<>();
// Field-level default so streams persisted before this field existed deserialize to
// the AWS-shaped DISABLED configuration instead of null (Jackson only overwrites it
// when the stored JSON actually carries the property).
@JsonProperty("DeliveryStreamEncryptionConfiguration")
private DeliveryStreamEncryptionConfiguration deliveryStreamEncryptionConfiguration =
DeliveryStreamEncryptionConfiguration.disabled();
public DeliveryStreamDescription() {}
public DeliveryStreamDescription(String name, String arn, S3Destination s3) {
this(name, arn, s3, null);
@@ -54,6 +61,7 @@ public class DeliveryStreamDescription {
s3.applyDefaults();
}
this.destinations = List.of(new Destination(s3));
this.deliveryStreamEncryptionConfiguration = DeliveryStreamEncryptionConfiguration.disabled();
if (kinesisStreamSource != null) {
this.source = new Source(kinesisStreamSource);
}
@@ -80,6 +88,12 @@ public class DeliveryStreamDescription {
public void setLastUpdateTimestamp(Instant lastUpdateTimestamp) { this.lastUpdateTimestamp = lastUpdateTimestamp; }
public List<Destination> getDestinations() { return destinations; }
public void setDestinations(List<Destination> destinations) { this.destinations = destinations; }
public DeliveryStreamEncryptionConfiguration getDeliveryStreamEncryptionConfiguration() {
return deliveryStreamEncryptionConfiguration;
}
public void setDeliveryStreamEncryptionConfiguration(DeliveryStreamEncryptionConfiguration configuration) {
this.deliveryStreamEncryptionConfiguration = configuration;
}
public Source getSource() { return source; }
public void setSource(Source source) { this.source = source; }
@@ -365,4 +379,35 @@ public class DeliveryStreamDescription {
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
}
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class DeliveryStreamEncryptionConfiguration {
@JsonProperty("KeyType")
private String keyType;
@JsonProperty("KeyARN")
private String keyArn;
@JsonProperty("Status")
private String status;
public DeliveryStreamEncryptionConfiguration() {}
public DeliveryStreamEncryptionConfiguration(String keyType, String keyArn, String status) {
this.keyType = keyType;
this.keyArn = keyArn;
this.status = status;
}
public static DeliveryStreamEncryptionConfiguration disabled() {
return new DeliveryStreamEncryptionConfiguration("AWS_OWNED_CMK", null, "DISABLED");
}
public String getKeyType() { return keyType; }
public void setKeyType(String keyType) { this.keyType = keyType; }
public String getKeyArn() { return keyArn; }
public void setKeyArn(String keyArn) { this.keyArn = keyArn; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
}
}