mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
refactor: mint and match ARNs through AwsArnUtils in Step Functions, Route 53 Resolver, SES, AppSync and three more (#6971)
The arn-concat sites in these files now build ARNs with Arn.of, Arn.global and new Arn, and match stored ARNs by their parsed partition, service and region instead of a hand-built prefix. The ARNs they produce are unchanged. tools/partition/baseline.tsv drops from 44 to 34. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
5bf89a5061
commit
9e7bcc4029
9 files changed
+49
-50
No files matched your search
+11
-12
@@ -320,23 +320,22 @@ public class IamAuthValidator {
|
||||
return "test".equals(accessKeyId);
|
||||
}
|
||||
|
||||
static String requestArn(String region, String accountId, String apiId) {
|
||||
return appsyncArn(region, accountId, "apis/" + apiId + "/*");
|
||||
}
|
||||
|
||||
static String fieldArn(String region, String accountId, String apiId, String typeName, String fieldName) {
|
||||
return appsyncArn(region, accountId, "apis/" + apiId + "/types/" + typeName + "/fields/" + fieldName);
|
||||
}
|
||||
|
||||
/**
|
||||
* The resource these two build is matched against a policy the customer wrote using the ARN
|
||||
* AppSync handed them, and that ARN carries the region's partition. Pinning {@code aws} here
|
||||
* meant a GovCloud policy naming its own API never matched, and the request was denied.
|
||||
*/
|
||||
private static String appsyncArnPrefix(String region) {
|
||||
return "arn:" + AwsRegions.partitionFor(region) + ":appsync:";
|
||||
}
|
||||
|
||||
static String requestArn(String region, String accountId, String apiId) {
|
||||
return appsyncArnPrefix(region) + nullToEmpty(region) + ":" + nullToEmpty(accountId)
|
||||
+ ":apis/" + apiId + "/*";
|
||||
}
|
||||
|
||||
static String fieldArn(String region, String accountId, String apiId, String typeName, String fieldName) {
|
||||
return appsyncArnPrefix(region) + nullToEmpty(region) + ":" + nullToEmpty(accountId)
|
||||
+ ":apis/" + apiId + "/types/" + typeName + "/fields/" + fieldName;
|
||||
private static String appsyncArn(String region, String accountId, String resource) {
|
||||
return new AwsArnUtils.Arn(AwsRegions.partitionFor(region), "appsync", nullToEmpty(region),
|
||||
nullToEmpty(accountId), resource).toString();
|
||||
}
|
||||
|
||||
static String usernameFromArn(String userArn, String accessKeyId) {
|
||||
|
||||
+2
-1
@@ -64,7 +64,8 @@ public class ControlCatalogService implements Resettable {
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
String responseArn = definition.globalIdentifier() == null
|
||||
? controlArn
|
||||
: "arn:" + partition + ":controlcatalog:::control/" + definition.globalIdentifier();
|
||||
: AwsArnUtils.Arn.global(partition, "controlcatalog", "", "control/" + definition.globalIdentifier())
|
||||
.toString();
|
||||
response.put("Arn", responseArn);
|
||||
response.set("Aliases", stringArray(definition.aliases()));
|
||||
response.put("Name", definition.name());
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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.services.iam.IamPolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.PublicAccessConditions;
|
||||
@@ -187,7 +188,7 @@ final class DsqlClusterPolicies {
|
||||
List<String> arns = new ArrayList<>();
|
||||
for (String value : texts(type.getValue())) {
|
||||
String arn = ACCOUNT_ID.matcher(value).matches()
|
||||
? "arn:" + partition + ":iam::" + value + ":root" : value;
|
||||
? AwsArnUtils.Arn.global(partition, "iam", value, "root").toString() : value;
|
||||
boolean known = "*".equals(arn) || SESSION_ARN.matcher(arn).matches()
|
||||
|| IAM_ARN.matcher(arn).matches() && principalExists.test(arn);
|
||||
if (!known) {
|
||||
|
||||
@@ -111,7 +111,7 @@ final class AwsManagedPolicies {
|
||||
|
||||
/** {@code arn:<partition>:iam::aws:policy}, without the trailing slash of the path. */
|
||||
static String arnPrefix(String partition) {
|
||||
return "arn:" + partition + ":iam::aws:policy";
|
||||
return AwsArnUtils.Arn.global(partition, "iam", "aws", "policy").toString();
|
||||
}
|
||||
|
||||
/** True for an AWS managed policy ARN in any partition ({@code arn:aws-cn:iam::aws:policy/...}). */
|
||||
@@ -136,7 +136,7 @@ final class AwsManagedPolicies {
|
||||
|
||||
private static List<ManagedPolicyDef> derive(AwsPartition partition) {
|
||||
long started = System.nanoTime();
|
||||
String arnPrefix = "arn:" + partition.id() + ":";
|
||||
String arnPrefix = "arn:" + partition.id() + ":"; // partition-literal: the text that replaces "arn:aws:" in a bundled policy document, not an ARN
|
||||
List<ManagedPolicyDef> derived = new ArrayList<>(POLICIES.size());
|
||||
for (ManagedPolicyDef def : POLICIES) {
|
||||
derived.add(new ManagedPolicyDef(partition.id(), def.name(), def.path(), def.description(),
|
||||
|
||||
+13
-9
@@ -559,10 +559,8 @@ public class Route53ResolverService {
|
||||
if (arn == null || arn.isBlank()) {
|
||||
throw new AwsException(INVALID_PARAMETER, "ResourceArn is required", 400);
|
||||
}
|
||||
String callerPrefix = "arn:" + AwsRegions.partitionFor(region) + ":route53resolver:" + region + ":"
|
||||
+ accountId + ":";
|
||||
String resource = arn.startsWith(callerPrefix)
|
||||
? AwsArnUtils.resourceIfArnFor(arn, "route53resolver").orElse("") : "";
|
||||
AwsArnUtils.Arn parsed = resolverArnIn(arn, region);
|
||||
String resource = parsed != null && parsed.accountId().equals(accountId) ? parsed.resource() : "";
|
||||
int slash = resource.indexOf('/');
|
||||
StorageBackend<String, ObjectNode> store = slash < 0 ? null : switch (resource.substring(0, slash)) {
|
||||
case "resolver-endpoint" -> endpointStore;
|
||||
@@ -577,6 +575,16 @@ 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
|
||||
@@ -685,13 +693,9 @@ public class Route53ResolverService {
|
||||
if (creatorRequestId == null || creatorRequestId.isBlank()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String regionPrefix = "arn:" + AwsRegions.partitionFor(region) + ":route53resolver:" + region + ":";
|
||||
return store.scan(key -> true).stream()
|
||||
.filter(existing -> creatorRequestId.equals(text(existing, "CreatorRequestId")))
|
||||
.filter(existing -> {
|
||||
String arn = text(existing, "Arn");
|
||||
return arn != null && arn.startsWith(regionPrefix);
|
||||
})
|
||||
.filter(existing -> resolverArnIn(text(existing, "Arn"), region) != null)
|
||||
.findFirst()
|
||||
.map(ObjectNode::deepCopy);
|
||||
}
|
||||
|
||||
@@ -1181,24 +1181,24 @@ public class SesService {
|
||||
if (tenant == null) {
|
||||
return;
|
||||
}
|
||||
String arnPrefix = "arn:" + AwsRegions.partitionFor(region) + ":ses:" + region + ":" + callerAccountId()
|
||||
+ ":";
|
||||
String accountId = callerAccountId();
|
||||
List<SesTenantService.AssociationResource> used = new ArrayList<>();
|
||||
if (source != null && !source.isBlank()) {
|
||||
String identityName = sendIdentityName(source, region);
|
||||
used.add(new SesTenantService.AssociationResource(
|
||||
SesTenantService.RESOURCE_TYPE_IDENTITY, identityName,
|
||||
arnPrefix + "identity/" + identityName));
|
||||
AwsArnUtils.Arn.of("ses", region, accountId, "identity/" + identityName).toString()));
|
||||
}
|
||||
if (effectiveConfigSet != null && !effectiveConfigSet.isBlank()) {
|
||||
used.add(new SesTenantService.AssociationResource(
|
||||
SesTenantService.RESOURCE_TYPE_CONFIGURATION_SET, effectiveConfigSet,
|
||||
arnPrefix + "configuration-set/" + effectiveConfigSet));
|
||||
AwsArnUtils.Arn.of("ses", region, accountId, "configuration-set/" + effectiveConfigSet)
|
||||
.toString()));
|
||||
}
|
||||
if (templateName != null && !templateName.isBlank()) {
|
||||
used.add(new SesTenantService.AssociationResource(
|
||||
SesTenantService.RESOURCE_TYPE_TEMPLATE, templateName,
|
||||
arnPrefix + "template/" + templateName));
|
||||
AwsArnUtils.Arn.of("ses", region, accountId, "template/" + templateName).toString()));
|
||||
}
|
||||
tenantService.requireResourcesAssociated(tenant, used, region);
|
||||
}
|
||||
|
||||
+1
-1
@@ -82,7 +82,7 @@ public record StatesIntegration(String partition, String service, String api, bo
|
||||
|
||||
/** The id without its suffix, as an error message names it. */
|
||||
public String withoutSuffix() {
|
||||
return "arn:" + partition + ":states:::" + (sdk ? "aws-sdk:" : "") + service + ":" + api;
|
||||
return AwsArnUtils.Arn.global(partition, "states", "", (sdk ? "aws-sdk:" : "") + service + ":" + api).toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+13
-11
@@ -531,20 +531,23 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
/**
|
||||
* Key prefix matching every Step Functions ARN in {@code region}. This store is keyed by the
|
||||
* full ARN, unlike the other services, which key by {@code region::name}, so the prefix has to
|
||||
* carry the region's partition or a list cannot find what a create wrote.
|
||||
* True for a store key that is a Step Functions ARN in {@code region}. This store is keyed by
|
||||
* the full ARN, unlike the other services, which key by {@code region::name}, so the match has
|
||||
* to carry the region's partition or a list cannot find what a create wrote.
|
||||
*
|
||||
* <p>Deliberately stops before the account segment: the store holds resources for more than
|
||||
* one account under account isolation, so {@code RegionResolver.buildArn} is the wrong helper
|
||||
* here, since it appends its own account id.
|
||||
* <p>Deliberately ignores the account segment: the store holds resources for more than one
|
||||
* account under account isolation.
|
||||
*/
|
||||
private static String regionArnPrefix(String region) {
|
||||
return "arn:" + AwsRegions.partitionFor(region) + ":states:" + region + ":";
|
||||
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);
|
||||
}
|
||||
|
||||
public List<StateMachine> listStateMachines(String region) {
|
||||
return stateMachineStore.scan(k -> k.startsWith(regionArnPrefix(region)));
|
||||
return stateMachineStore.scan(k -> isInRegion(k, region));
|
||||
}
|
||||
|
||||
public PaginatedResult<StateMachine> listStateMachines(String region, Integer maxResults, String nextToken) {
|
||||
@@ -1249,8 +1252,7 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
public List<Activity> listActivities(String region) {
|
||||
String prefix = regionArnPrefix(region);
|
||||
return activityStore.scan(k -> k.startsWith(prefix) && k.contains(":activity:"));
|
||||
return activityStore.scan(k -> isInRegion(k, region) && k.contains(":activity:"));
|
||||
}
|
||||
|
||||
public PaginatedResult<Activity> listActivities(String region, Integer maxResults, String nextToken) {
|
||||
|
||||
Reference in new issue
Block a user