mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(firehose): give resource-tag conditions the tags of the delivery stream a request names (#8743)
The generated request resources give Firehose's deliverystream type the aws:ResourceTag/ key, but no service answered the firehose namespace's stored tags, so a condition on a delivery stream's tags saw none. FirehoseService now answers it: storedTags reads the stream in the request's account, where the handlers read, in place under its key in the ARN's Region or the account's key from before the key named the Region, and matched on the whole ARN. TagDeliveryStream and UntagDeliveryStream read, changed and wrote the stream with no lock, so one could drop another's tags or write back a stream a delete had just removed; they now hold the per-stream lock a delete and a Redshift update hold, and the tags field is volatile.
This commit is contained in:
1 parent
6618ae3de6
commit
f9e88a2215
6 files changed
+139
-27
No files matched your search
@@ -44,9 +44,10 @@ import java.util.Optional;
|
||||
* another account an association runs and the tags of a parameter, a document or an association; CloudFront, for the
|
||||
* key value store a name names and the tags of any resource it tags; ECS, for the revision a task definition
|
||||
* reference resolves to, its default cluster and the tags of a cluster, a task definition, a service, a capacity
|
||||
* provider, a task or a container instance; ACM, for the tags of a certificate); looked up lazily by
|
||||
* {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for
|
||||
* the namespace a request or an ARN is of, so IAM never depends on that service directly.</p>
|
||||
* provider, a task or a container instance; ACM, for the tags of a certificate; Firehose, for the tags of a delivery
|
||||
* stream); looked up lazily by {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through
|
||||
* {@code Instance<StoredResourceArns>} for the namespace a request or an ARN is of, so IAM never depends on that
|
||||
* service directly.</p>
|
||||
*
|
||||
* <p>Each answer is a lookup of what the service holds, asked for by an entry of the generated table ({@code find},
|
||||
* {@code through}, a default, {@code forwarded}, a type's {@code resourceTagKeys}): never a fact about what an operation acts on, which the table alone
|
||||
|
||||
@@ -6,6 +6,7 @@ import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.RequestScopes;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.resource.ExplorerResource;
|
||||
import io.floci.aws.core.resource.ResourceProvider;
|
||||
import io.floci.aws.core.resource.SupportedResourceType;
|
||||
@@ -53,10 +54,12 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ApplicationScoped
|
||||
public class FirehoseService implements ResourceProvider {
|
||||
public class FirehoseService implements ResourceProvider, StoredResourceArns {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(FirehoseService.class);
|
||||
private static final String DEFAULT_BUCKET = "floci-firehose-results";
|
||||
/** The resource part of a delivery stream's ARN, before its name. */
|
||||
private static final String STREAM_RESOURCE = "deliverystream/";
|
||||
private static final int DEFAULT_BUFFERING_INTERVAL_SECONDS = 300;
|
||||
private static final int DEFAULT_REDSHIFT_RETRY_SECONDS = 3600;
|
||||
// AWS retries a failed Redshift load every 5 minutes.
|
||||
@@ -131,7 +134,8 @@ public class FirehoseService implements ResourceProvider {
|
||||
private final Map<String, Deque<PendingRedshiftCopy>> redshiftPending = new ConcurrentHashMap<>();
|
||||
// Tracks streams that currently have a drain task queued or running on redshiftDeliveryExecutor.
|
||||
private final Set<String> activeRedshiftDrains = ConcurrentHashMap.newKeySet();
|
||||
// Serialises the read, version check, merge and write of a Redshift UpdateDestination per stream.
|
||||
// Serialises per stream the read, version check, merge and write of a Redshift UpdateDestination, a delete, and
|
||||
// the read and write of a tag call.
|
||||
private final Map<String, Object> redshiftUpdateLocks = new ConcurrentHashMap<>();
|
||||
private final Clock clock;
|
||||
private final long tickIntervalSeconds;
|
||||
@@ -426,7 +430,7 @@ public class FirehoseService implements ResourceProvider {
|
||||
s3Config.canonicalizeProcessors();
|
||||
}
|
||||
String arn = AwsArnUtils.Arn.of("firehose", region, accountId,
|
||||
"deliverystream/" + name).toString();
|
||||
STREAM_RESOURCE + name).toString();
|
||||
// CreateDeliveryStream's KinesisStreamSourceConfiguration carries only the ARN and
|
||||
// role -- DeliveryStartTimestamp exists only on the Description shape, which AWS
|
||||
// fills in at creation. Stamping it here, at creation time, is what makes the first
|
||||
@@ -816,35 +820,87 @@ public class FirehoseService implements ResourceProvider {
|
||||
return view;
|
||||
}
|
||||
|
||||
/**
|
||||
* Under the lock a delete and a Redshift update hold across their read and write, so a tag call neither writes
|
||||
* back a stream deleted after it read it nor loses another tag call's change.
|
||||
*/
|
||||
public void tagDeliveryStream(String name, List<DeliveryStreamDescription.Tag> tagsToTag) {
|
||||
DeliveryStreamDescription stream = describeDeliveryStream(name);
|
||||
Map<String, String> tagMap = new LinkedHashMap<>();
|
||||
for (DeliveryStreamDescription.Tag t : stream.getTags()) {
|
||||
tagMap.put(t.getKey(), t.getValue());
|
||||
String streamKey = scopedKey(name);
|
||||
synchronized (redshiftUpdateLocks.computeIfAbsent(streamKey, k -> new Object())) {
|
||||
DeliveryStreamDescription stream = describeDeliveryStream(streamKey, name);
|
||||
Map<String, String> tagMap = new LinkedHashMap<>();
|
||||
for (DeliveryStreamDescription.Tag t : stream.getTags()) {
|
||||
tagMap.put(t.getKey(), t.getValue());
|
||||
}
|
||||
for (DeliveryStreamDescription.Tag t : tagsToTag) {
|
||||
tagMap.put(t.getKey(), t.getValue());
|
||||
}
|
||||
List<DeliveryStreamDescription.Tag> newTags = new ArrayList<>();
|
||||
tagMap.forEach((k, v) -> newTags.add(new DeliveryStreamDescription.Tag(k, v)));
|
||||
stream.setTags(newTags);
|
||||
streamPut(streamKey, stream);
|
||||
}
|
||||
for (DeliveryStreamDescription.Tag t : tagsToTag) {
|
||||
tagMap.put(t.getKey(), t.getValue());
|
||||
}
|
||||
List<DeliveryStreamDescription.Tag> newTags = new ArrayList<>();
|
||||
tagMap.forEach((k, v) -> newTags.add(new DeliveryStreamDescription.Tag(k, v)));
|
||||
stream.setTags(newTags);
|
||||
streamPut(scopedKey(name), stream);
|
||||
LOG.infov("Tagged Firehose delivery stream {0}: {1}", name, tagsToTag);
|
||||
}
|
||||
|
||||
/** Under the same lock as {@link #tagDeliveryStream}. */
|
||||
public void untagDeliveryStream(String name, List<String> tagKeys) {
|
||||
DeliveryStreamDescription stream = describeDeliveryStream(name);
|
||||
List<DeliveryStreamDescription.Tag> newTags = new ArrayList<>();
|
||||
for (DeliveryStreamDescription.Tag t : stream.getTags()) {
|
||||
if (!tagKeys.contains(t.getKey())) {
|
||||
newTags.add(t);
|
||||
String streamKey = scopedKey(name);
|
||||
synchronized (redshiftUpdateLocks.computeIfAbsent(streamKey, k -> new Object())) {
|
||||
DeliveryStreamDescription stream = describeDeliveryStream(streamKey, name);
|
||||
List<DeliveryStreamDescription.Tag> newTags = new ArrayList<>();
|
||||
for (DeliveryStreamDescription.Tag t : stream.getTags()) {
|
||||
if (!tagKeys.contains(t.getKey())) {
|
||||
newTags.add(t);
|
||||
}
|
||||
}
|
||||
stream.setTags(newTags);
|
||||
streamPut(streamKey, stream);
|
||||
}
|
||||
stream.setTags(newTags);
|
||||
streamPut(scopedKey(name), stream);
|
||||
LOG.infov("Untagged Firehose delivery stream {0}: {1}", name, tagKeys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "firehose";
|
||||
}
|
||||
|
||||
/** A request names a delivery stream by its name, which its ARN carries whole, so there is nothing to find. */
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the delivery stream whose ARN is {@code arn}, in the request's account, where the handlers read: an
|
||||
* ARN of another account names none. Read in place under the stream's key in the ARN's Region, or the account's
|
||||
* key from before the key named the Region, as the handlers find it, and matched on the whole ARN. A tag call
|
||||
* stores a new list, so the read is of one whole set.
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, "firehose")) {
|
||||
return Map.of();
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
String account = parsed.accountId();
|
||||
String region = parsed.region();
|
||||
if (!account.equals(regionResolver.getAccountId()) || !parsed.resource().startsWith(STREAM_RESOURCE)) {
|
||||
return Map.of();
|
||||
}
|
||||
String name = parsed.resource().substring(STREAM_RESOURCE.length());
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
streamStore.getForAccount(account, logicalKeyFromScopedKey(scopedKey(account, region, name)))
|
||||
.or(() -> streamStore.getForAccount(account, name))
|
||||
.filter(stream -> belongsTo(account, region, stream) && arn.equals(stream.getDeliveryStreamARN()))
|
||||
.ifPresent(stream -> stream.getTags().forEach(tag -> {
|
||||
if (tag.getKey() != null && tag.getValue() != null) {
|
||||
tags.put(tag.getKey(), tag.getValue());
|
||||
}
|
||||
}));
|
||||
return tags;
|
||||
}
|
||||
|
||||
public List<DeliveryStreamDescription.Tag> listTagsForDeliveryStream(String name, String exclusiveStartTagKey, Integer limit) {
|
||||
DeliveryStreamDescription stream = describeDeliveryStream(name);
|
||||
List<DeliveryStreamDescription.Tag> tags = stream.getTags();
|
||||
|
||||
@@ -42,7 +42,7 @@ public class DeliveryStreamDescription {
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
private Source source;
|
||||
@JsonProperty("Tags")
|
||||
private List<Tag> tags = new ArrayList<>();
|
||||
private volatile 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
|
||||
|
||||
Reference in new issue
Block a user