mirror of
https://github.com/floci-io/floci.git
synced 2026-12-04 02:28:08 -05:00
fix(iam): check Kinesis Data Streams requests on the resources they name, from the generated table (#8989)
Kinesis was built by hand: ResourceArnBuilder read a request's StreamARN, StreamName or ResourceARN into one ARN and checked everything else on stream/*, the request resources table held no Kinesis operation, and nothing gave a policy condition a stream's tags. kinesis moves from HAND_BUILT to NAMESPACES, so the table gives each operation the resources botocore's members name as the Service Reference types them: the stream by StreamARN or StreamName, a consumer by ConsumerARN, a tagged resource by ResourceARN, and the KMS key StartStreamEncryption and StopStreamEncryption name. Its ResourceArnBuilder branch goes. Only Kinesis's operations change in the table. KinesisStoredResourceArns answers a stream's or a consumer's stored tags; the tag calls give the resource a new map under their lock, and the tags fields are volatile.
This commit is contained in:
1 parent
ad0e47910f
commit
43d6ea4961
11 files changed
+237
-61
No files matched your search
@@ -58,10 +58,10 @@ import java.util.Optional;
|
||||
* monitor or subscription; Security Hub, for the tags of a hub or a configuration policy; Control Tower, for the tags
|
||||
* of a landing zone or an enabled control; GuardDuty, for the tags of a detector or a Malware Protection plan; Cloud
|
||||
* Map, for the tags of a namespace or a service; Elastic Load Balancing, for the tags of a load balancer, a target
|
||||
* group, a listener, a rule or a Classic Load Balancer; SQS, for the tags of a queue; SNS, for the tags of a topic);
|
||||
* 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>
|
||||
* group, a listener, a rule or a Classic Load Balancer; SQS, for the tags of a queue; SNS, for the tags of a topic;
|
||||
* Kinesis Data Streams, for the tags of a stream or a consumer); 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
|
||||
|
||||
@@ -186,7 +186,6 @@ public class ResourceArnBuilder {
|
||||
return switch (credentialScope) {
|
||||
case "s3" -> buildS3Resources(operation, path, ctx, region);
|
||||
case "dynamodb" -> buildDynamoDbArns(ctx, region, accountId);
|
||||
case "kinesis" -> List.of(buildKinesisArn(ctx, region, accountId));
|
||||
case "iam" -> buildIamArns(ctx, region, accountId);
|
||||
case "resource-groups" -> List.of(buildResourceGroupArn(ctx, region, accountId));
|
||||
case "pipes" -> List.of(buildPipesArn(path, ctx, region, accountId, partition));
|
||||
@@ -1360,35 +1359,6 @@ public class ResourceArnBuilder {
|
||||
return AwsArnUtils.Arn.of("dynamodb", region, accountId, "table/" + tableName).toString();
|
||||
}
|
||||
|
||||
// ── Kinesis ──────────────────────────────────────────────────────────────────
|
||||
private String buildKinesisArn(ContainerRequestContext ctx, String region, String accountId) {
|
||||
JsonNode json = readJsonBody(ctx);
|
||||
if (json != null && json.isObject()) {
|
||||
if (json.hasNonNull("StreamARN")) {
|
||||
String streamArn = json.get("StreamARN").asText().trim();
|
||||
if (!streamArn.isEmpty()) {
|
||||
return streamArn;
|
||||
}
|
||||
}
|
||||
if (json.hasNonNull("StreamName")) {
|
||||
String streamName = json.get("StreamName").asText().trim();
|
||||
if (!streamName.isEmpty()) {
|
||||
if (AwsArnUtils.isArnFor(streamName, "kinesis")) {
|
||||
return streamName;
|
||||
}
|
||||
return AwsArnUtils.Arn.of("kinesis", region, accountId, "stream/" + streamName).toString();
|
||||
}
|
||||
}
|
||||
if (json.hasNonNull("ResourceARN")) {
|
||||
String resourceArn = json.get("ResourceARN").asText().trim();
|
||||
if (!resourceArn.isEmpty()) {
|
||||
return resourceArn;
|
||||
}
|
||||
}
|
||||
}
|
||||
return AwsArnUtils.Arn.of("kinesis", region, accountId, "stream/*").toString();
|
||||
}
|
||||
|
||||
// ── KMS ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
@@ -371,7 +371,7 @@ public class KinesisService implements ResourceProvider {
|
||||
KinesisStream stream = resolveStream(streamName, region);
|
||||
requireActive(stream);
|
||||
validateTagCapacity(stream.getTags(), tags);
|
||||
stream.getTags().putAll(tags);
|
||||
stream.setTags(withTags(stream.getTags(), tags));
|
||||
store.put(key, stream);
|
||||
}
|
||||
}
|
||||
@@ -381,7 +381,7 @@ public class KinesisService implements ResourceProvider {
|
||||
synchronized (lockFor(key)) {
|
||||
KinesisStream stream = resolveStream(streamName, region);
|
||||
requireActive(stream);
|
||||
tagKeys.forEach(stream.getTags()::remove);
|
||||
stream.setTags(withoutTags(stream.getTags(), tagKeys));
|
||||
store.put(key, stream);
|
||||
}
|
||||
}
|
||||
@@ -396,7 +396,7 @@ public class KinesisService implements ResourceProvider {
|
||||
synchronized (consumerStore) {
|
||||
KinesisConsumer consumer = resolveConsumerForTags(key);
|
||||
validateResourceTagLimit(consumer.getTags(), tags);
|
||||
consumer.getTags().putAll(tags);
|
||||
consumer.setTags(withTags(consumer.getTags(), tags));
|
||||
consumerStore.put(key, consumer);
|
||||
}
|
||||
return;
|
||||
@@ -407,7 +407,7 @@ public class KinesisService implements ResourceProvider {
|
||||
KinesisStream stream = describeStreamByArn(resourceArn, region);
|
||||
requireActive(stream);
|
||||
validateResourceTagLimit(stream.getTags(), tags);
|
||||
stream.getTags().putAll(tags);
|
||||
stream.setTags(withTags(stream.getTags(), tags));
|
||||
store.put(key, stream);
|
||||
}
|
||||
}
|
||||
@@ -417,7 +417,7 @@ public class KinesisService implements ResourceProvider {
|
||||
String key = regionKey(region, resourceArn);
|
||||
synchronized (consumerStore) {
|
||||
KinesisConsumer consumer = resolveConsumerForTags(key);
|
||||
tagKeys.forEach(consumer.getTags()::remove);
|
||||
consumer.setTags(withoutTags(consumer.getTags(), tagKeys));
|
||||
consumerStore.put(key, consumer);
|
||||
}
|
||||
return;
|
||||
@@ -427,11 +427,50 @@ public class KinesisService implements ResourceProvider {
|
||||
synchronized (lockFor(key)) {
|
||||
KinesisStream stream = describeStreamByArn(resourceArn, region);
|
||||
requireActive(stream);
|
||||
tagKeys.forEach(stream.getTags()::remove);
|
||||
stream.setTags(withoutTags(stream.getTags(), tagKeys));
|
||||
store.put(key, stream);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A new map holding {@code tags} and {@code added}: a tag call gives the resource a new map, under its lock, so a
|
||||
* lookup reads one whole set.
|
||||
*/
|
||||
private static Map<String, String> withTags(Map<String, String> tags, Map<String, String> added) {
|
||||
Map<String, String> merged = new HashMap<>(tags);
|
||||
merged.putAll(added);
|
||||
return merged;
|
||||
}
|
||||
|
||||
/** A new map holding {@code tags} without {@code removed}, as {@link #withTags}. */
|
||||
private static Map<String, String> withoutTags(Map<String, String> tags, List<String> removed) {
|
||||
Map<String, String> remaining = new HashMap<>(tags);
|
||||
removed.forEach(remaining::remove);
|
||||
return remaining;
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the stream whose ARN is {@code streamArn} in {@code account}, read in place under the key the
|
||||
* handlers read, the stream's name in the ARN's Region, and matched on the whole ARN it records; none when there
|
||||
* is no such stream.
|
||||
*/
|
||||
Map<String, String> streamTagsInAccount(String account, String streamArn, String streamName) {
|
||||
return AccountAwareStorageBackend.readInAccount(store, account,
|
||||
regionKey(AwsArnUtils.parse(streamArn).region(), streamName))
|
||||
.filter(stream -> streamArn.equals(stream.getStreamArn()))
|
||||
.map(KinesisStream::getTags).orElse(Map.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the consumer whose ARN is {@code consumerArn} in {@code account}, read in place under the key the
|
||||
* handlers read, the ARN in its Region; none when there is no such consumer.
|
||||
*/
|
||||
Map<String, String> consumerTagsInAccount(String account, String consumerArn) {
|
||||
return AccountAwareStorageBackend.readInAccount(consumerStore, account,
|
||||
regionKey(AwsArnUtils.parse(consumerArn).region(), consumerArn))
|
||||
.map(KinesisConsumer::getTags).orElse(Map.of());
|
||||
}
|
||||
|
||||
public Map<String, String> listTagsForResource(String resourceArn, String region) {
|
||||
if (isConsumerTagResource(resourceArn, region)) {
|
||||
synchronized (consumerStore) {
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package io.floci.aws.services.kinesis;
|
||||
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/** IAM's lookup of the tags Kinesis Data Streams holds on a stream or a consumer, for the {@code kinesis} namespace. */
|
||||
@ApplicationScoped
|
||||
public class KinesisStoredResourceArns implements StoredResourceArns {
|
||||
|
||||
private static final String NAMESPACE = "kinesis";
|
||||
private static final String STREAM = "stream/";
|
||||
private static final String CONSUMER = "/consumer/";
|
||||
|
||||
private final KinesisService kinesis;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@Inject
|
||||
public KinesisStoredResourceArns(KinesisService kinesis, RegionResolver regionResolver) {
|
||||
this.kinesis = kinesis;
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
/** A request names a stream by its name or ARN and a consumer by its ARN: nothing to find. */
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the stream or the consumer whose ARN is {@code arn}, in the request's account, where the tag
|
||||
* handlers read: an ARN of another account names none. A consumer's ARN, a stream's with {@code /consumer/} and
|
||||
* the consumer after it, reads the consumer held under that ARN; a stream's reads the stream of its name, matched
|
||||
* on the whole ARN it records. A tag without a value reads as an empty one. Tag calls give the resource a new map,
|
||||
* so the read is of one whole set.
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, NAMESPACE)) {
|
||||
return Map.of();
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
String account = regionResolver.getAccountId();
|
||||
String resource = parsed.resource();
|
||||
if (!parsed.accountId().equals(account) || !resource.startsWith(STREAM)) {
|
||||
return Map.of();
|
||||
}
|
||||
Map<String, String> tags = resource.contains(CONSUMER)
|
||||
? kinesis.consumerTagsInAccount(account, arn)
|
||||
: kinesis.streamTagsInAccount(account, arn, resource.substring(STREAM.length()));
|
||||
return StoredResourceArns.withEmptyValues(tags);
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ public class KinesisConsumer {
|
||||
private String consumerStatus;
|
||||
private Instant consumerCreationTimestamp;
|
||||
private String streamArn;
|
||||
private Map<String, String> tags = new HashMap<>();
|
||||
private volatile Map<String, String> tags = new HashMap<>();
|
||||
|
||||
public KinesisConsumer() {}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ public class KinesisStream {
|
||||
private volatile List<KinesisShard> shards = new CopyOnWriteArrayList<>();
|
||||
private int retentionPeriodHours = 24;
|
||||
private Instant streamCreationTimestamp;
|
||||
private Map<String, String> tags = new HashMap<>();
|
||||
private volatile Map<String, String> tags = new HashMap<>();
|
||||
private String encryptionType = "NONE";
|
||||
private String keyId;
|
||||
private String streamMode = "PROVISIONED";
|
||||
|
||||
Reference in new issue
Block a user