From 03da5da99785c40bcb8540c9219f6c0318e1cb70 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Thu, 29 Oct 2026 06:31:03 -0500 Subject: [PATCH] refactor: read and check ARNs through AwsArnUtils in RAM, Resource Groups, CloudWatch, Service Catalog and four more (#6904) Seven startsWith("arn:") probes become AwsArnUtils.looksLikeArn. RAM, the Resource Groups Tagging visibility check, the CloudWatch dashboards cross-account ARN and Service Catalog read ARN fields with AwsArnUtils.parse and regionOrDefault instead of a split; the dashboards ARN without its region is rebuilt as an AwsArnUtils.Arn. Signed-off-by: Hector Ventura --- .../floci/services/budgets/BudgetsService.java | 2 +- .../floci/services/cloudfront/CloudFrontService.java | 3 ++- .../floci/services/cloudtrail/CloudTrailService.java | 2 +- .../dashboards/CloudWatchDashboardsService.java | 8 ++++---- .../logs/CloudWatchLogsCrossAccountService.java | 2 +- .../hectorvent/floci/services/ram/RamService.java | 11 +++++------ .../resourcegroups/ResourceGroupsService.java | 4 ++-- .../ResourceGroupsTaggingService.java | 9 ++++++--- .../servicecatalog/ServiceCatalogService.java | 7 ++----- .../hectorvent/floci/services/ssm/SsmService.java | 2 +- tools/partition/baseline.tsv | 10 ---------- 11 files changed, 25 insertions(+), 35 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/budgets/BudgetsService.java b/src/main/java/io/github/hectorvent/floci/services/budgets/BudgetsService.java index b665fab4f0..ffc7b4eb1f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/budgets/BudgetsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/budgets/BudgetsService.java @@ -814,7 +814,7 @@ public class BudgetsService implements Resettable { private ResourceRef parseResourceArn(String arn) { String marker = ":budgets::"; int service = arn.indexOf(marker); - if (!arn.startsWith("arn:") || service < 0) { + if (!AwsArnUtils.looksLikeArn(arn) || service < 0) { throw invalid("ResourceARN is not a valid Budgets ARN."); } int resourceStart = service + marker.length(); diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java index 3e441091a7..0aa1fda099 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudfront; import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.config.LocalStackParity; +import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.CloudFrontEdgeDomain; import io.github.hectorvent.floci.core.common.RegionResolver; @@ -2159,7 +2160,7 @@ public class CloudFrontService implements ServerCertificateReferenceProvider { } public RealtimeLogConfig getRealtimeLogConfig(String nameOrArn) { - if (nameOrArn != null && nameOrArn.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(nameOrArn)) { String name = nameOrArn.substring(nameOrArn.lastIndexOf('/') + 1); return realtimeLogConfigStore.get(name).orElseThrow(() -> new AwsException("NoSuchRealtimeLogConfig", diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailService.java b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailService.java index 7e2e57dd41..902e67440d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailService.java @@ -922,7 +922,7 @@ public class CloudTrailService { private Trail findTrail(String region, String nameOrArn) { // ARN → cross-region scan is valid (callers use ARN to target another Region) - if (nameOrArn != null && nameOrArn.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(nameOrArn)) { for (String k : store.keys()) { CloudTrailEntry entry = store.get(k).orElse(null); if (entry == null) continue; diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/dashboards/CloudWatchDashboardsService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/dashboards/CloudWatchDashboardsService.java index b41bf4a13b..61169196eb 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/dashboards/CloudWatchDashboardsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/dashboards/CloudWatchDashboardsService.java @@ -257,12 +257,12 @@ public class CloudWatchDashboardsService { * returned unchanged, since there is no region field to blank. */ private static String withoutRegion(String arn) { - String[] fields = arn.split(":", 6); - if (fields.length < 6) { + if (!AwsArnUtils.isArn(arn)) { return arn; } - fields[3] = ""; - return String.join(":", fields); + AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn); + return new AwsArnUtils.Arn(parsed.partition(), parsed.service(), "", parsed.accountId(), parsed.resource()) + .toString(); } private static String key(String region, String dashboardName) { diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsCrossAccountService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsCrossAccountService.java index 37264a03e0..953accdb57 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsCrossAccountService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsCrossAccountService.java @@ -319,7 +319,7 @@ public class CloudWatchLogsCrossAccountService { } } private static void requireArn(String value, String field) { - if (value == null || !value.startsWith("arn:") || value.length() < 10) { + if (!AwsArnUtils.looksLikeArn(value) || value.length() < 10) { throw invalid(field + " must be a valid ARN."); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ram/RamService.java b/src/main/java/io/github/hectorvent/floci/services/ram/RamService.java index 6c74b601f0..3dbaf4cbbf 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ram/RamService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ram/RamService.java @@ -313,8 +313,7 @@ public class RamService { /** {@code arn:aws:ram:::resource-share/} → {@code }. */ private static String extractRegion(String resourceShareArn) { - String[] parts = resourceShareArn.split(":", 6); - return parts.length < 4 ? "" : parts[3]; + return AwsArnUtils.regionOrDefault(resourceShareArn, ""); } private void putInvitation(ResourceShareInvitation invitation) { @@ -761,12 +760,12 @@ public class RamService { /** {@code arn:aws:ec2:...:transit-gateway/tgw-1} → {@code ec2:TransitGateway}. */ private static String ramResourceType(String resourceArn) { - String[] parts = resourceArn.split(":", 6); - if (parts.length < 6) { + if (!AwsArnUtils.isArn(resourceArn)) { return ""; } - String service = parts[2]; - String resource = parts[5]; + AwsArnUtils.Arn parsed = AwsArnUtils.parse(resourceArn); + String service = parsed.service(); + String resource = parsed.resource(); int separator = indexOfTypeSeparator(resource); String typeSegment = separator >= 0 ? resource.substring(0, separator) : resource; StringBuilder camel = new StringBuilder(); diff --git a/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsService.java b/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsService.java index 49f8748d62..ae64fd77ac 100644 --- a/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsService.java @@ -90,7 +90,7 @@ public class ResourceGroupsService implements Resettable { public List capacityReservationMembers(String region, String account, String groupArn) { // EC2 holds its capacity lock; GroupResources calls EC2 while holding this service's monitor. // Read the immutable storage snapshot without acquiring that monitor in the reverse order. - if (groupArn == null || !groupArn.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(groupArn)) { throw bad("A capacity reservation group ARN is required."); } return selected(region, account, JsonNodeFactory.instance.objectNode().put("Group", groupArn)).members(); @@ -222,7 +222,7 @@ public class ResourceGroupsService implements Resettable { } String selected = text(request, request.has("Group") ? "Group" : "GroupName", true); String name = selected; - if (selected.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(selected)) { if (!AwsArnUtils.isArn(selected)) { throw bad("Invalid group ARN."); } diff --git a/src/main/java/io/github/hectorvent/floci/services/resourcegroupstagging/ResourceGroupsTaggingService.java b/src/main/java/io/github/hectorvent/floci/services/resourcegroupstagging/ResourceGroupsTaggingService.java index 5c0c2e06cb..8c597ed32d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/resourcegroupstagging/ResourceGroupsTaggingService.java +++ b/src/main/java/io/github/hectorvent/floci/services/resourcegroupstagging/ResourceGroupsTaggingService.java @@ -437,11 +437,14 @@ public class ResourceGroupsTaggingService implements Resettable { // Region and account come from the ARN (arn::svc:region:acct:resource), and a string // that is not an ARN, or an ARN without a region, stays visible as it always has. private static boolean isVisible(String arn, String region, String accountId) { - String[] parts = arn.split(":", 6); - if (parts.length >= 4 && !parts[3].isEmpty() && !parts[3].equals(region)) { + if (!AwsArnUtils.isArn(arn)) { + return true; + } + AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn); + if (!parsed.region().isEmpty() && !parsed.region().equals(region)) { return false; } - return accountId == null || parts.length < 5 || parts[4].isEmpty() || parts[4].equals(accountId); + return accountId == null || parsed.accountId().isEmpty() || parsed.accountId().equals(accountId); } // A provider resource's region is its ARN's, else the owner's region for it. Null means it has none, diff --git a/src/main/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogService.java b/src/main/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogService.java index 7f09b732b3..60744f39e8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogService.java +++ b/src/main/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogService.java @@ -1326,11 +1326,8 @@ public class ServiceCatalogService implements PassedRoleLookup { ObjectNode instance = objectMapper.createObjectNode(); instance.put("Account", text(product, "PhysicalId")); String arn = text(product, "Arn"); - if (arn != null) { - String[] parts = arn.split(":"); - if (parts.length >= 4) { - instance.put("Region", parts[3]); - } + if (AwsArnUtils.isArn(arn)) { + instance.put("Region", AwsArnUtils.parse(arn).region()); } instance.put("StackInstanceStatus", "CURRENT"); return List.of(instance); diff --git a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmService.java b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmService.java index 46cccb3128..8cb56c8f6c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmService.java @@ -1118,7 +1118,7 @@ public class SsmService implements ResourceProvider { } private String parameterName(String nameOrArn, String region, boolean parameterRead) { - if (nameOrArn != null && nameOrArn.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(nameOrArn)) { try { AwsArnUtils.Arn arn = AwsArnUtils.parse(nameOrArn); if ("ssm".equals(arn.service()) && arn.resource().startsWith("parameter")) { diff --git a/tools/partition/baseline.tsv b/tools/partition/baseline.tsv index 4064f46d2a..4efb955da3 100644 --- a/tools/partition/baseline.tsv +++ b/tools/partition/baseline.tsv @@ -17,18 +17,12 @@ arn-probe src/main/java/io/github/hectorvent/floci/services/appsync/graphql/data arn-probe src/main/java/io/github/hectorvent/floci/services/appsync/graphql/resolver/AppSyncResolverExecutor.java 1 arn-probe src/main/java/io/github/hectorvent/floci/services/batch/BatchService.java 4 arn-probe src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockService.java 3 -arn-probe src/main/java/io/github/hectorvent/floci/services/budgets/BudgetsService.java 1 -arn-probe src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java 1 -arn-probe src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailService.java 1 -arn-probe src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsCrossAccountService.java 1 arn-probe src/main/java/io/github/hectorvent/floci/services/connect/ConnectService.java 1 arn-probe src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2TargetResolver.java 1 arn-probe src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java 8 arn-probe src/main/java/io/github/hectorvent/floci/services/iot/IotService.java 1 -arn-probe src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsService.java 2 arn-probe src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java 3 arn-probe src/main/java/io/github/hectorvent/floci/services/ses/SesTemplateService.java 1 -arn-probe src/main/java/io/github/hectorvent/floci/services/ssm/SsmService.java 1 arn-probe src/main/java/io/github/hectorvent/floci/services/ssooidc/SsoOidcController.java 2 arn-split src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/appsync/graphql/resolver/AppSyncResolverExecutor.java 1 @@ -36,14 +30,10 @@ arn-split src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecont arn-split src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreGatewayService.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreMemoryService.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandler.java 1 -arn-split src/main/java/io/github/hectorvent/floci/services/cloudwatch/dashboards/CloudWatchDashboardsService.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/ec2/Ec2InstanceCredentials.java 3 arn-split src/main/java/io/github/hectorvent/floci/services/iam/PublicAccessConditions.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/kms/KmsJsonHandler.java 1 -arn-split src/main/java/io/github/hectorvent/floci/services/ram/RamService.java 2 -arn-split src/main/java/io/github/hectorvent/floci/services/resourcegroupstagging/ResourceGroupsTaggingService.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/sagemaker/SageMakerService.java 1 -arn-split src/main/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogService.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/ses/SesTenantService.java 2 arn-split src/main/java/io/github/hectorvent/floci/services/ssoadmin/SsoAdminService.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/swf/SwfService.java 1