Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(core): add AwsArnUtils.looksLikeArn for name-or-ARN parameters (#6877)
Hector Ventura22ea3ca2e027
diff --git a/AGENTS.md b/AGENTS.mdindex 95572ece20..9c7bb0c308 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -134,7 +134,9 @@ run `make partition-baseline` and commit the smaller baseline.- Mint ARNs through `AwsArnUtils.Arn.of(...)` / `RegionResolver.buildArn(...)`, recognisethem with `AwsArnUtils.isArn` / `PARTITION_REGEX`, read their fields with`AwsArnUtils.parse(...)`, and derive hosts from the region's- DNS suffix (`AwsRegions.dnsSuffixFor`).+ DNS suffix (`AwsRegions.dnsSuffixFor`). A parameter that takes a name or an ARN, where AWS+ answers a malformed ARN with its own error, tells the two apart with+ `AwsArnUtils.looksLikeArn` (any value starting `arn:`); `isArn` would treat that value as a name.- A regionless ARN (IAM, STS, CloudFront, Organizations, an S3 bucket) never goes through`Arn.of(service, "", ...)`, which silently means `aws`: use `RegionResolver.buildGlobalArn`for the request's partition, or `Arn.global(partition, ...)` with the resource's owndiff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsArnUtils.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsArnUtils.javaindex 121f59e3af..33433d7f0b 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsArnUtils.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsArnUtils.java@@ -142,6 +142,17 @@ public final class AwsArnUtils {return parts.length == 6 && "arn".equals(parts[0]);}+ /**+ * True when {@code value} is meant as an ARN: it starts with {@code arn:}, whether or not the+ * rest is well formed. For a parameter that takes a name or an ARN, where AWS answers a+ * malformed ARN with its own error (an {@code InvalidParameterValue}, a {@code ValidationError},+ * a not-found) instead of looking it up as a name. {@link #isArn} needs all six segments, so it+ * would send {@code arn:aws:rds} down the name path and change the error the caller sees.+ */+ public static boolean looksLikeArn(String value) {+ return value != null && value.startsWith("arn:"); // partition-literal: the one attempted-ARN check+ }+/*** True when {@code value} is an ARN naming {@code service}, in any partition. The common use* is telling an identifier that may be either a bare name or a full ARN apart, e.g. adiff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsArnUtilsTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsArnUtilsTest.javaindex b8b99203d1..04d0eccddc 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/AwsArnUtilsTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsArnUtilsTest.java@@ -248,6 +248,29 @@ class AwsArnUtilsTest {assertFalse(AwsArnUtils.isArn(null));}+ @ParameterizedTest+ @ValueSource(strings = {"arn:aws:sqs:us-east-1:000000000000:q", "arn:aws-cn:rds", "arn:"})+ void looksLikeArnAcceptsAnyValueMeantAsAnArn(String value) {+ assertTrue(AwsArnUtils.looksLikeArn(value));+ }++ @ParameterizedTest+ @ValueSource(strings = {"my-queue", "", " ", "ARN:aws:sqs:us-east-1:000000000000:q", " arn:aws:sqs"})+ void looksLikeArnRejectsNames(String value) {+ assertFalse(AwsArnUtils.looksLikeArn(value));+ }++ @Test+ void looksLikeArnRejectsNullRatherThanThrowing() {+ assertFalse(AwsArnUtils.looksLikeArn(null));+ }++ @Test+ void looksLikeArnAcceptsAMalformedArnThatIsArnRejects() {+ assertTrue(AwsArnUtils.looksLikeArn("arn:aws:rds:us-east-1"));+ assertFalse(AwsArnUtils.isArn("arn:aws:rds:us-east-1"));+ }+/*** The shape the {@code startsWith("arn:aws:dynamodb:")} probes were reaching for: is this* identifier an ARN for my service, whatever partition it came from.