feat(firehose): return ExtendedS3DestinationDescription and support UpdateDestination (#1710)

* feat(firehose): return ExtendedS3DestinationDescription and support UpdateDestination

The Terraform/Pulumi provider creates delivery streams with
ExtendedS3DestinationConfiguration and reads ExtendedS3DestinationDescription
back after create; Floci parsed the config lossily and only ever emitted
S3DestinationDescription, breaking the provider read. Store the full config
(RoleARN, ErrorOutputPrefix, CompressionFormat, EncryptionConfiguration),
emit it under both wire keys like real AWS, add the required output members
(VersionId, DeliveryStreamType, HasMoreDestinations, DestinationId), and
implement UpdateDestination with version checking.

Refs #1043

* fix(firehose): reject empty destination updates and guard version parse

* fix(firehose): reject partial BufferingHints instead of absorbing defaults

AWS requires SizeInMBs and IntervalInSeconds to be specified together
(firehose service-2.json: "if you specify a value for it, you must also
specify a value for IntervalInSeconds, and vice versa"). The primitive
fields with initializers made a partial payload indistinguishable from a
full one, silently persisting a default over an existing value. Members
are now null-honest boxed Integers, create and update reject one-sided
hints with the declared InvalidArgumentException before any mutation,
and defaults apply only when the whole object is absent.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-07-02 23:59:11 -05:00
1 parent d8c09754d5
commit b3c54c1854
9 files changed
+794 -13

No files matched your search

@@ -42,9 +42,24 @@ public class FirehoseJsonHandler {
tags.add(mapper.treeToValue(tNode, io.github.hectorvent.floci.services.firehose.model.DeliveryStreamDescription.Tag.class));
}
}
String arn = firehoseService.createDeliveryStream(name, s3, tags);
String deliveryStreamType = request.has("DeliveryStreamType")
? request.get("DeliveryStreamType").asText() : null;
String arn = firehoseService.createDeliveryStream(name, s3, tags, deliveryStreamType);
yield Response.ok(Map.of("DeliveryStreamARN", arn)).build();
}
case "UpdateDestination" -> {
String name = request.get("DeliveryStreamName").asText();
String currentVersionId = request.get("CurrentDeliveryStreamVersionId").asText();
String destinationId = request.get("DestinationId").asText();
S3Destination update = null;
if (request.has("ExtendedS3DestinationUpdate")) {
update = mapper.treeToValue(request.get("ExtendedS3DestinationUpdate"), S3Destination.class);
} else if (request.has("S3DestinationUpdate")) {
update = mapper.treeToValue(request.get("S3DestinationUpdate"), S3Destination.class);
}
firehoseService.updateDestination(name, currentVersionId, destinationId, update);
yield Response.ok(Map.of()).build();
}
case "DescribeDeliveryStream" -> {
String name = request.get("DeliveryStreamName").asText();
var desc = firehoseService.describeDeliveryStream(name);
@@ -45,16 +45,97 @@ public class FirehoseService {
}
public String createDeliveryStream(String name, S3Destination s3Config, List<DeliveryStreamDescription.Tag> tags) {
return createDeliveryStream(name, s3Config, tags, null);
}
public String createDeliveryStream(String name, S3Destination s3Config, List<DeliveryStreamDescription.Tag> tags,
String deliveryStreamType) {
validateBufferingHints(s3Config);
String arn = AwsArnUtils.Arn.of("firehose", regionResolver.getDefaultRegion(), regionResolver.getAccountId(), "deliverystream/" + name).toString();
DeliveryStreamDescription description = new DeliveryStreamDescription(name, arn, s3Config);
description.setAccountId(regionResolver.getAccountId());
description.setTags(tags);
if (deliveryStreamType != null && !deliveryStreamType.isBlank()) {
description.setDeliveryStreamType(deliveryStreamType);
}
streamStore.put(name, description);
buffers.put(name, Collections.synchronizedList(new ArrayList<>()));
LOG.infov("Created Firehose delivery stream: {0}", name);
return arn;
}
public void updateDestination(String name, String currentVersionId, String destinationId, S3Destination update) {
DeliveryStreamDescription stream = describeDeliveryStream(name);
if (!stream.getVersionId().equals(currentVersionId)) {
throw new AwsException("ConcurrentModificationException",
"Cannot update firehose: " + name + " since the current version id: " + stream.getVersionId()
+ " and specified version id: " + currentVersionId + " do not match", 400);
}
DeliveryStreamDescription.Destination destination = stream.getDestinations() != null && !stream.getDestinations().isEmpty()
? stream.getDestinations().get(0)
: null;
if (destination == null || !destination.getDestinationId().equals(destinationId)) {
throw new AwsException("InvalidArgumentException",
"Destination Id " + destinationId + " not found", 400);
}
if (update == null) {
throw new AwsException("InvalidArgumentException",
"A destination update is required for UpdateDestination.", 400);
}
validateBufferingHints(update);
S3Destination current = destination.getExtendedS3DestinationDescription();
if (current == null) {
update.applyDefaults();
destination.setExtendedS3DestinationDescription(update);
} else {
mergeDestination(current, update);
}
stream.setVersionId(String.valueOf(parseVersionId(stream.getVersionId()) + 1));
stream.setLastUpdateTimestamp(java.time.Instant.now());
streamStore.put(name, stream);
LOG.infov("Updated destination {0} of Firehose delivery stream {1}", destinationId, name);
}
// 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.
private static long parseVersionId(String versionId) {
try {
return Long.parseLong(versionId);
} catch (NumberFormatException e) {
return 0L;
}
}
/**
* AWS requires SizeInMBs and IntervalInSeconds to be specified together:
* "This parameter is optional but if you specify a value for it, you must
* also specify a value for IntervalInSeconds, and vice versa" (firehose
* service-2.json). Rejecting partial hints here is what keeps the
* whole-object replacement in mergeDestination faithful to AWS.
*/
private static void validateBufferingHints(S3Destination config) {
DeliveryStreamDescription.BufferingHints hints = config == null ? null : config.getBufferingHints();
if (hints == null) {
return;
}
if ((hints.getSizeInMBs() == null) != (hints.getIntervalInSeconds() == null)) {
throw new AwsException("InvalidArgumentException",
"If you specify a value for SizeInMBs, you must also specify a value for IntervalInSeconds, and vice versa.",
400);
}
}
private static void mergeDestination(S3Destination current, S3Destination update) {
if (update.getRoleArn() != null) current.setRoleArn(update.getRoleArn());
if (update.getBucketArn() != null) current.setBucketArn(update.getBucketArn());
if (update.getPrefix() != null) current.setPrefix(update.getPrefix());
if (update.getErrorOutputPrefix() != null) current.setErrorOutputPrefix(update.getErrorOutputPrefix());
if (update.getCompressionFormat() != null) current.setCompressionFormat(update.getCompressionFormat());
if (update.getBufferingHints() != null) current.setBufferingHints(update.getBufferingHints());
if (update.getEncryptionConfiguration() != null) current.setEncryptionConfiguration(update.getEncryptionConfiguration());
}
public void tagDeliveryStream(String name, List<DeliveryStreamDescription.Tag> tagsToTag) {
DeliveryStreamDescription stream = describeDeliveryStream(name);
Map<String, String> tagMap = new LinkedHashMap<>();
@@ -105,9 +186,14 @@ public class FirehoseService {
}
public DeliveryStreamDescription describeDeliveryStream(String name) {
return streamStore.get(name)
DeliveryStreamDescription stream = streamStore.get(name)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"Delivery stream not found: " + name, 400));
// Normalizes streams persisted before required output members existed.
if (stream.s3Destination() != null) {
stream.s3Destination().applyDefaults();
}
return stream;
}
public void deleteDeliveryStream(String name) {
@@ -1,6 +1,8 @@
package io.github.hectorvent.floci.services.firehose.model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
@@ -8,6 +10,7 @@ import java.util.List;
import java.util.ArrayList;
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public class DeliveryStreamDescription {
@JsonProperty("DeliveryStreamName")
private String deliveryStreamName;
@@ -16,9 +19,19 @@ public class DeliveryStreamDescription {
private String deliveryStreamARN;
@JsonProperty("DeliveryStreamStatus")
private DeliveryStreamStatus deliveryStreamStatus;
@JsonProperty("DeliveryStreamType")
private String deliveryStreamType = "DirectPut";
@JsonProperty("VersionId")
private String versionId = "1";
@JsonProperty("HasMoreDestinations")
private boolean hasMoreDestinations;
@JsonProperty("CreateTimestamp")
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private Instant createTimestamp;
@JsonProperty("LastUpdateTimestamp")
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
@JsonInclude(JsonInclude.Include.NON_NULL)
private Instant lastUpdateTimestamp;
@JsonProperty("Destinations")
private List<Destination> destinations;
@JsonProperty("Tags")
@@ -30,6 +43,9 @@ public class DeliveryStreamDescription {
this.deliveryStreamARN = arn;
this.deliveryStreamStatus = DeliveryStreamStatus.ACTIVE;
this.createTimestamp = Instant.now();
if (s3 != null) {
s3.applyDefaults();
}
this.destinations = List.of(new Destination(s3));
}
@@ -42,8 +58,16 @@ public class DeliveryStreamDescription {
public void setDeliveryStreamARN(String deliveryStreamARN) { this.deliveryStreamARN = deliveryStreamARN; }
public DeliveryStreamStatus getDeliveryStreamStatus() { return deliveryStreamStatus; }
public void setDeliveryStreamStatus(DeliveryStreamStatus deliveryStreamStatus) { this.deliveryStreamStatus = deliveryStreamStatus; }
public String getDeliveryStreamType() { return deliveryStreamType; }
public void setDeliveryStreamType(String deliveryStreamType) { this.deliveryStreamType = deliveryStreamType; }
public String getVersionId() { return versionId; }
public void setVersionId(String versionId) { this.versionId = versionId; }
public boolean isHasMoreDestinations() { return hasMoreDestinations; }
public void setHasMoreDestinations(boolean hasMoreDestinations) { this.hasMoreDestinations = hasMoreDestinations; }
public Instant getCreateTimestamp() { return createTimestamp; }
public void setCreateTimestamp(Instant createTimestamp) { this.createTimestamp = createTimestamp; }
public Instant getLastUpdateTimestamp() { return lastUpdateTimestamp; }
public void setLastUpdateTimestamp(Instant lastUpdateTimestamp) { this.lastUpdateTimestamp = lastUpdateTimestamp; }
public List<Destination> getDestinations() { return destinations; }
public void setDestinations(List<Destination> destinations) { this.destinations = destinations; }
@@ -54,32 +78,100 @@ public class DeliveryStreamDescription {
}
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class Destination {
@JsonProperty("S3DestinationDescription")
private S3Destination s3DestinationDescription;
@JsonProperty("DestinationId")
private String destinationId = "destinationId-000000000001";
// Single canonical S3 config, serialized under both wire keys: real AWS
// returns ExtendedS3DestinationDescription plus the deprecated
// S3DestinationDescription mirror for every S3-backed stream.
private S3Destination s3;
public Destination() {}
public Destination(S3Destination s3) { this.s3DestinationDescription = s3; }
public S3Destination getS3DestinationDescription() { return s3DestinationDescription; }
public void setS3DestinationDescription(S3Destination s3) { this.s3DestinationDescription = s3; }
public Destination(S3Destination s3) { this.s3 = s3; }
public String getDestinationId() { return destinationId; }
public void setDestinationId(String destinationId) { this.destinationId = destinationId; }
@JsonProperty("S3DestinationDescription")
public S3Destination getS3DestinationDescription() { return s3; }
@JsonProperty("S3DestinationDescription")
public void setS3DestinationDescription(S3Destination s3) {
// Guarded so persisted JSON carrying both keys (identical content) stays
// idempotent while legacy files with only the S3 key still load.
if (this.s3 == null) {
this.s3 = s3;
}
}
@JsonProperty("ExtendedS3DestinationDescription")
public S3Destination getExtendedS3DestinationDescription() { return s3; }
@JsonProperty("ExtendedS3DestinationDescription")
public void setExtendedS3DestinationDescription(S3Destination s3) { this.s3 = s3; }
}
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class S3Destination {
@JsonProperty("RoleARN")
private String roleArn;
@JsonProperty("BucketARN")
private String bucketArn;
@JsonProperty("Prefix")
private String prefix;
@JsonProperty("ErrorOutputPrefix")
private String errorOutputPrefix;
@JsonProperty("CompressionFormat")
private String compressionFormat;
@JsonProperty("BufferingHints")
private BufferingHints bufferingHints;
@JsonProperty("EncryptionConfiguration")
private EncryptionConfiguration encryptionConfiguration;
public S3Destination() {}
public String getRoleArn() { return roleArn; }
public void setRoleArn(String roleArn) { this.roleArn = roleArn; }
public String getBucketArn() { return bucketArn; }
public void setBucketArn(String bucketArn) { this.bucketArn = bucketArn; }
public String getPrefix() { return prefix; }
public void setPrefix(String prefix) { this.prefix = prefix; }
public String getErrorOutputPrefix() { return errorOutputPrefix; }
public void setErrorOutputPrefix(String errorOutputPrefix) { this.errorOutputPrefix = errorOutputPrefix; }
public String getCompressionFormat() { return compressionFormat; }
public void setCompressionFormat(String compressionFormat) { this.compressionFormat = compressionFormat; }
public BufferingHints getBufferingHints() { return bufferingHints; }
public void setBufferingHints(BufferingHints bufferingHints) { this.bufferingHints = bufferingHints; }
public EncryptionConfiguration getEncryptionConfiguration() { return encryptionConfiguration; }
public void setEncryptionConfiguration(EncryptionConfiguration encryptionConfiguration) { this.encryptionConfiguration = encryptionConfiguration; }
/**
* Fills the members the wire contract marks required with the AWS defaults.
* Getters stay null-honest so UpdateDestination merges can tell "not
* specified" from a value; call this only on create and describe paths.
*/
public void applyDefaults() {
if (compressionFormat == null) {
compressionFormat = "UNCOMPRESSED";
}
if (encryptionConfiguration == null) {
encryptionConfiguration = EncryptionConfiguration.noEncryption();
}
if (bufferingHints == null) {
bufferingHints = BufferingHints.defaults();
} else {
// Self-heal legacy persisted state; validation keeps partial
// hints out of the create/update paths.
if (bufferingHints.getSizeInMBs() == null) {
bufferingHints.setSizeInMBs(5);
}
if (bufferingHints.getIntervalInSeconds() == null) {
bufferingHints.setIntervalInSeconds(300);
}
}
}
/** Extracts bucket name from ARN: arn:aws:s3:::my-bucket → my-bucket */
public String bucketName() {
@@ -90,17 +182,66 @@ public class DeliveryStreamDescription {
}
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class EncryptionConfiguration {
@JsonProperty("NoEncryptionConfig")
private String noEncryptionConfig;
@JsonProperty("KMSEncryptionConfig")
private KmsEncryptionConfig kmsEncryptionConfig;
public EncryptionConfiguration() {}
public static EncryptionConfiguration noEncryption() {
EncryptionConfiguration config = new EncryptionConfiguration();
config.noEncryptionConfig = "NoEncryption";
return config;
}
public String getNoEncryptionConfig() { return noEncryptionConfig; }
public void setNoEncryptionConfig(String noEncryptionConfig) { this.noEncryptionConfig = noEncryptionConfig; }
public KmsEncryptionConfig getKmsEncryptionConfig() { return kmsEncryptionConfig; }
public void setKmsEncryptionConfig(KmsEncryptionConfig kmsEncryptionConfig) { this.kmsEncryptionConfig = kmsEncryptionConfig; }
}
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public static class KmsEncryptionConfig {
@JsonProperty("AWSKMSKeyARN")
private String awsKmsKeyArn;
public KmsEncryptionConfig() {}
public String getAwsKmsKeyArn() { return awsKmsKeyArn; }
public void setAwsKmsKeyArn(String awsKmsKeyArn) { this.awsKmsKeyArn = awsKmsKeyArn; }
}
/**
* Members stay boxed and null-honest so validation can tell "not specified"
* from a value: AWS requires SizeInMBs and IntervalInSeconds to be specified
* together, and the defaults (5 MiB / 300 s) apply only when the whole
* object is absent.
*/
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public static class BufferingHints {
@JsonProperty("SizeInMBs")
private int sizeInMBs = 5;
private Integer sizeInMBs;
@JsonProperty("IntervalInSeconds")
private int intervalInSeconds = 300;
private Integer intervalInSeconds;
public BufferingHints() {}
public int getSizeInMBs() { return sizeInMBs; }
public void setSizeInMBs(int sizeInMBs) { this.sizeInMBs = sizeInMBs; }
public int getIntervalInSeconds() { return intervalInSeconds; }
public void setIntervalInSeconds(int intervalInSeconds) { this.intervalInSeconds = intervalInSeconds; }
public static BufferingHints defaults() {
BufferingHints hints = new BufferingHints();
hints.sizeInMBs = 5;
hints.intervalInSeconds = 300;
return hints;
}
public Integer getSizeInMBs() { return sizeInMBs; }
public void setSizeInMBs(Integer sizeInMBs) { this.sizeInMBs = sizeInMBs; }
public Integer getIntervalInSeconds() { return intervalInSeconds; }
public void setIntervalInSeconds(Integer intervalInSeconds) { this.intervalInSeconds = intervalInSeconds; }
}
public List<Tag> getTags() {
@@ -110,6 +251,7 @@ public class DeliveryStreamDescription {
public void setTags(List<Tag> tags) { this.tags = tags; }
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Tag {
@JsonProperty("Key")
private String key;