refactor(core): add AwsArnUtils.looksLikeArn for name-or-ARN parameters (#6877)

A parameter that takes a name or an ARN, where AWS answers a malformed ARN with its own error, needs to treat any value starting arn: as an attempted ARN. isArn needs all six fields and would send arn:aws:rds down the name path, so the sites the earlier migration PRs left on purpose still probe with a hand-written startsWith. looksLikeArn is that probe, in one place, so the area PRs that follow can move them off the arn-probe baseline without changing the error a caller sees.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-28 20:47:03 -04:00
1 parent d726533659
commit 22ea3ca2e0
3 files changed
+37 -1

No files matched your search

+3 -1
View File
@@ -134,7 +134,9 @@ run `make partition-baseline` and commit the smaller baseline.
- Mint ARNs through `AwsArnUtils.Arn.of(...)` / `RegionResolver.buildArn(...)`, recognise
them 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 own
@@ -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. a
@@ -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.