fix(core): read an ARN's resource after its fifth colon, not its last (#5939)

* fix(core): read an ARN's resource after its fifth colon, not its last

Four places cut an ARN by hand and got the wrong resource for a legal name:

- A VPC flow log's S3 destination took everything after the last ':' as
  the bucket, so a prefix with a colon (arn:aws:s3:::bkt/logs:2024/) named
  bucket 2024. It now takes the S3 resource through AwsArnUtils.
- Glue's TagResource and UntagResource split the ARN on every ':', so a
  job, table or other name containing one was looked up by the text before
  it and answered EntityNotFoundException. The split stops at six parts.
- KMS resolved an alias ARN at its last ':', so alias/team:payments
  resolved as payments. The alias name is now the ARN's resource.
- A Kinesis event source mapping read the stream name after the last '/',
  so a stream-consumer ARN gave the consumer segment. It now reads the
  segment after stream/, as KinesisJsonHandler does.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* fix(core): parse only KMS alias ARNs and drop only a consumer suffix

Review follow-ups:

- KMS read the alias name from any ARN containing ":alias/", so another
  service's ARN naming the same alias resolved a local KMS alias. Only a
  KMS ARN is now read that way; anything else keeps the old reading and
  finds no alias.
- The Kinesis poller dropped everything after the stream segment, so a
  malformed suffix (stream/orders/not-a-consumer) still named stream
  orders. It now drops only a /consumer/ suffix, so a malformed ARN names
  no stream.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

---------

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-18 11:56:32 -04:00
1 parent d951fd532c
commit fc1488d17a
8 files changed
+65 -7

No files matched your search

@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.ec2;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.core.common.AvailabilityZoneIds;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
@@ -567,12 +568,13 @@ public class FlowLogService {
}
}
private static String bucketFromArn(String arn) {
static String bucketFromArn(String arn) {
if (arn == null) {
return null;
}
// arn:aws:s3:::bucket-name[/optional/prefix] OR a bare bucket name.
String name = arn.startsWith("arn:") ? arn.substring(arn.lastIndexOf(':') + 1) : arn;
// arn:aws:s3:::bucket-name[/optional/prefix] OR a bare bucket name. The prefix may itself
// contain ':', so the resource is everything after the ARN's fifth ':', not its last.
String name = AwsArnUtils.resourceIfArnFor(arn, "s3").orElse(arn);
int slash = name.indexOf('/');
return slash >= 0 ? name.substring(0, slash) : name;
}
@@ -2964,7 +2964,8 @@ public class GlueService {
}
private void validateResourceExists(String arn) {
String[] parts = arn.split(":");
// At most six parts: a job, table or other name may itself contain ':'.
String[] parts = arn.split(":", 6);
if (parts.length >= 6) {
String resource = parts[5];
if (resource.startsWith("job/")) {
@@ -2104,7 +2104,10 @@ public class KmsService implements ResourceProvider {
String id = keyIdOrArn;
// Alias arn
if (id.contains(":alias/")) {
String aliasName = id.substring(id.lastIndexOf(":") + 1);
// A KMS alias ARN's name follows its fifth ':' and may itself contain ':'. Anything
// else carrying ":alias/" keeps the old reading and finds no alias of that name.
String aliasName = AwsArnUtils.isArnFor(id, "kms") ? AwsArnUtils.parse(id).resource()
: id.substring(id.lastIndexOf(":") + 1);
String aliasKey = region + "::" + aliasName;
id = aliasStore.get(aliasKey)
.map(KmsAlias::getTargetKeyId)
@@ -325,7 +325,19 @@ public class KinesisEventSourcePoller implements Resettable {
esmStore.saveForAccount(esm.getAccountId(), esm);
}
private static String streamNameFromArn(String arn) {
return arn.substring(arn.lastIndexOf("/") + 1);
/**
* The stream name from a stream ARN ({@code .../stream/S}) or a stream-consumer ARN
* ({@code .../stream/S/consumer/C:123}): the segment right after {@code stream/}. Only a
* {@code /consumer/} suffix is dropped; any other suffix stays part of the name, so a
* malformed ARN names no stream rather than a real one.
*/
static String streamNameFromArn(String arn) {
int streamIdx = arn.indexOf(":stream/");
if (streamIdx < 0) {
return arn.substring(arn.lastIndexOf("/") + 1);
}
String after = arn.substring(streamIdx + ":stream/".length());
int consumer = after.indexOf("/consumer/");
return consumer >= 0 ? after.substring(0, consumer) : after;
}
}