From d2f0455cd93d3470d54d8d633cc60f5b9f0e35ef Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Sat, 31 Oct 2026 13:01:41 -0500 Subject: [PATCH] refactor: check an ARN's service, region and partition through one AwsArnUtils helper (#7109) Step Functions and Route 53 Resolver each carried the same private check: an ARN of one service, in the request's region and that region's partition. AwsArnUtils.parseIfArnIn now holds it, and both services call it. Signed-off-by: Hector Ventura --- .../floci/core/common/AwsArnUtils.java | 14 +++++++++++++ .../Route53ResolverService.java | 20 ++++++------------- .../stepfunctions/StepFunctionsService.java | 7 +------ .../floci/core/common/AwsArnUtilsTest.java | 17 ++++++++++++++++ 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsArnUtils.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsArnUtils.java index 33433d7f0b..baf8b4b002 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 @@ -175,6 +175,20 @@ public final class AwsArnUtils { return Optional.of(value.split(":", 6)[5]); } + /** + * The parsed ARN when {@code value} is an ARN naming {@code service} in {@code region} and that + * region's partition, e.g. a stored key belonging to the region a list call was made in; empty + * otherwise. + */ + public static Optional parseIfArnIn(String value, String service, String region) { + if (!isArnFor(value, service)) { + return Optional.empty(); + } + Arn arn = parse(value); + return arn.region().equals(region) && arn.partition().equals(AwsRegions.partitionFor(region)) + ? Optional.of(arn) : Optional.empty(); + } + /** * True when the ARN names a partition other than {@code localPartition}, the one the current * request belongs to ({@link RegionResolver#getPartition()}). An empty partition field is diff --git a/src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverService.java b/src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverService.java index 52038012d2..c51de64aad 100644 --- a/src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverService.java +++ b/src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverService.java @@ -7,7 +7,6 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; -import io.github.hectorvent.floci.core.common.AwsRegions; import io.github.hectorvent.floci.core.common.Digests; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; @@ -731,8 +730,10 @@ public class Route53ResolverService { if (arn == null || arn.isBlank()) { throw new AwsException(INVALID_PARAMETER, "ResourceArn is required", 400); } - AwsArnUtils.Arn parsed = resolverArnIn(arn, region); - String resource = parsed != null && parsed.accountId().equals(accountId) ? parsed.resource() : ""; + String resource = AwsArnUtils.parseIfArnIn(arn, "route53resolver", region) + .filter(parsed -> parsed.accountId().equals(accountId)) + .map(AwsArnUtils.Arn::resource) + .orElse(""); int slash = resource.indexOf('/'); StorageBackend store = slash < 0 ? null : switch (resource.substring(0, slash)) { case "resolver-endpoint" -> endpointStore; @@ -747,16 +748,6 @@ public class Route53ResolverService { "Resource not found: " + arn, 400)); } - /** The parsed ARN when it is a Route 53 Resolver ARN in {@code region} and its partition, else null. */ - private static AwsArnUtils.Arn resolverArnIn(String arn, String region) { - if (!AwsArnUtils.isArnFor(arn, "route53resolver")) { - return null; - } - AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn); - return parsed.partition().equals(AwsRegions.partitionFor(region)) && parsed.region().equals(region) - ? parsed : null; - } - /** * The caller's resource by id. Unlike {@link StorageBackend#get}, which migrates an unprefixed * legacy entry into whichever account reads it first, a legacy entry moves only when @@ -867,7 +858,8 @@ public class Route53ResolverService { } return store.scan(key -> true).stream() .filter(existing -> creatorRequestId.equals(text(existing, "CreatorRequestId"))) - .filter(existing -> resolverArnIn(text(existing, "Arn"), region) != null) + .filter(existing -> AwsArnUtils.parseIfArnIn(text(existing, "Arn"), "route53resolver", region) + .isPresent()) .findFirst() .map(ObjectNode::deepCopy); } diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsService.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsService.java index 570a25d19b..28ca99b6c6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsService.java @@ -8,7 +8,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsArnShapes; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; -import io.github.hectorvent.floci.core.common.AwsRegions; import io.github.hectorvent.floci.core.common.Base64Url; import io.github.hectorvent.floci.core.common.Digests; import io.github.hectorvent.floci.core.common.PaginatedResult; @@ -539,11 +538,7 @@ public class StepFunctionsService implements Resettable, ResourceProvider { * account under account isolation. */ private static boolean isInRegion(String key, String region) { - if (!AwsArnUtils.isArnFor(key, "states")) { - return false; - } - AwsArnUtils.Arn arn = AwsArnUtils.parse(key); - return arn.partition().equals(AwsRegions.partitionFor(region)) && arn.region().equals(region); + return AwsArnUtils.parseIfArnIn(key, "states", region).isPresent(); } public List listStateMachines(String region) { diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsArnUtilsTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsArnUtilsTest.java index 04d0eccddc..cb456639fd 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 @@ -206,6 +206,23 @@ class AwsArnUtilsTest { assertTrue(AwsArnUtils.resourceIfArnFor("arn:aws:s3", "s3").isEmpty()); } + @Test + void parseIfArnInMatchesOnlyTheServiceInTheRegionAndItsPartition() { + String arn = "arn:aws-cn:states:cn-north-1:000000000000:stateMachine:m"; + assertEquals("stateMachine:m", AwsArnUtils.parseIfArnIn(arn, "states", "cn-north-1").orElseThrow().resource()); + assertTrue(AwsArnUtils.parseIfArnIn(arn, "states", "cn-northwest-1").isEmpty()); + assertTrue(AwsArnUtils.parseIfArnIn(arn, "sqs", "cn-north-1").isEmpty()); + assertTrue(AwsArnUtils.parseIfArnIn("arn:aws:states:cn-north-1:000000000000:stateMachine:m", + "states", "cn-north-1").isEmpty()); + } + + @Test + void parseIfArnInIsEmptyForNonArns() { + assertTrue(AwsArnUtils.parseIfArnIn(null, "states", "us-east-1").isEmpty()); + assertTrue(AwsArnUtils.parseIfArnIn("m", "states", "us-east-1").isEmpty()); + assertTrue(AwsArnUtils.parseIfArnIn("arn:aws:states", "states", "us-east-1").isEmpty()); + } + @Test void aPseudoRegionResolvesToItsPartitionWhenMinting() { assertEquals("arn:aws-cn:sqs:aws-cn-global:000000000000:q",