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;
}
}
@@ -255,4 +255,11 @@ class FlowLogServiceTest {
return new String(in.readAllBytes(), StandardCharsets.UTF_8).trim();
}
}
@Test
void bucketComesFromTheS3ArnEvenWhenThePrefixHasAColon() {
assertEquals("bkt", FlowLogService.bucketFromArn("arn:aws:s3:::bkt/logs:2024/"));
assertEquals("bkt", FlowLogService.bucketFromArn("arn:aws-cn:s3:::bkt"));
assertEquals("bkt", FlowLogService.bucketFromArn("bkt/prefix"));
}
}
@@ -436,4 +436,14 @@ class GlueJobRunServiceTest {
return AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
}
}
@Test
void tagsReachAJobWhoseNameContainsAColon() {
createJob("nightly:etl", null, null);
String arn = "arn:aws:glue:" + REGION + ":" + ACCOUNT_ID + ":job/nightly:etl";
glueService.tagResource(arn, Map.of("team", "data"), REGION);
assertEquals(Map.of("team", "data"), glueService.getTags(arn, REGION));
}
}
@@ -4035,4 +4035,17 @@ class KmsServiceTest {
assertEquals(List.of("alias/aws/s3"), aliases.stream().map(KmsAlias::getAliasName).toList());
}
}
@Test
void aliasArnResolvesAnAliasNameContainingAColon() {
KmsKey key = kmsService.createKey(null, REGION);
kmsService.createAlias("alias/team:payments", key.getKeyId(), REGION);
String aliasArn = "arn:aws:kms:" + REGION + ":000000000000:alias/team:payments";
assertEquals(key.getKeyId(), kmsService.describeKey(aliasArn, REGION).getKeyId());
// Another service's ARN naming the same alias is not a KMS alias ARN.
assertThrows(AwsException.class, () -> kmsService.describeKey(
"arn:aws:sns:" + REGION + ":000000000000:alias/team:payments", REGION));
}
}
@@ -469,4 +469,14 @@ class KinesisEventSourcePollerTest {
verify(kinesisService, never()).getShardIterator(anyString(), anyString(), anyString(), any(), anyString());
verify(kinesisService, never()).getRecords(anyString(), anyInt(), anyString());
}
@Test
void streamNameComesFromAStreamOrAConsumerArn() {
assertEquals("orders", KinesisEventSourcePoller.streamNameFromArn(
"arn:aws:kinesis:us-east-1:000000000000:stream/orders"));
assertEquals("orders", KinesisEventSourcePoller.streamNameFromArn(
"arn:aws:kinesis:us-east-1:000000000000:stream/orders/consumer/reader:1700000000"));
assertEquals("orders/not-a-consumer", KinesisEventSourcePoller.streamNameFromArn(
"arn:aws:kinesis:us-east-1:000000000000:stream/orders/not-a-consumer"));
}
}