Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Hector Venturafc1488d17a8d
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/FlowLogService.java b/src/main/java/io/github/hectorvent/floci/services/ec2/FlowLogService.java
index 425649d993..9c2a339616 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/FlowLogService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/FlowLogService.java
@@ -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;
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java b/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java
index 9c326399a1..01170d969a 100644
--- a/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java
@@ -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/")) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java b/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java
index dab0b78f73..904b86d969 100644
--- a/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java
@@ -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)
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.java b/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.java
index c476cad052..152edb0a79 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.java
@@ -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;
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/FlowLogServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/FlowLogServiceTest.java
index bc40ce5b1c..e88287aaad 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/FlowLogServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/FlowLogServiceTest.java
@@ -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"));
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/glue/GlueJobRunServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/glue/GlueJobRunServiceTest.java
index e75c9e14e0..008e801165 100644
--- a/src/test/java/io/github/hectorvent/floci/services/glue/GlueJobRunServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/glue/GlueJobRunServiceTest.java
@@ -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));
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.java
index 054731895b..955e0bec0b 100644
--- a/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.java
@@ -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));
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePollerTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePollerTest.java
index 823a96d345..114734b52c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePollerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePollerTest.java
@@ -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"));
+ }
}