mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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 <hectorvent@gmail.com>
This commit is contained in:
1 parent
de6710ff0e
commit
d2f0455cd9
4 files changed
+38
-20
No files matched your search
@@ -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<Arn> 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
|
||||
|
||||
+6
-14
@@ -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<String, ObjectNode> 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);
|
||||
}
|
||||
|
||||
+1
-6
@@ -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<StateMachine> listStateMachines(String region) {
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user