From d7204696d44d3072ebcdcc86232bffc2cd3e85f6 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Wed, 28 Oct 2026 23:41:03 -0500 Subject: [PATCH] refactor: read and check ARNs through AwsArnUtils in DataSync, DynamoDB, Glue, Lake Formation, Redshift and S3 Tables (#6903) Ten startsWith("arn:") probes on name-or-ARN parameters become AwsArnUtils.looksLikeArn. DataSync's private arnRegion and arnAccountId copies are deleted in favour of AwsArnUtils.regionOrDefault and accountOrDefault, and its resource, Glue's resource and Redshift's integration source type are read with AwsArnUtils.parse instead of a split. Signed-off-by: Hector Ventura --- .../services/datasync/DataSyncService.java | 21 +++++-------------- .../services/dynamodb/DynamoDbService.java | 2 +- .../services/dynamodb/DynamoDbTableNames.java | 4 ++-- .../backend/DynamoDbLocalBackend.java | 4 ++-- .../floci/services/glue/GlueService.java | 8 +++---- .../lakeformation/LakeFormationService.java | 2 +- .../services/redshift/RedshiftService.java | 8 ++----- .../RedshiftServerlessService.java | 3 ++- .../services/s3tables/S3TablesService.java | 2 +- tools/partition/baseline.tsv | 11 ---------- 10 files changed, 20 insertions(+), 45 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/datasync/DataSyncService.java b/src/main/java/io/github/hectorvent/floci/services/datasync/DataSyncService.java index cb664df7ae..63625a67a1 100644 --- a/src/main/java/io/github/hectorvent/floci/services/datasync/DataSyncService.java +++ b/src/main/java/io/github/hectorvent/floci/services/datasync/DataSyncService.java @@ -363,8 +363,8 @@ public class DataSyncService { String svmArn = configuration.path("StorageVirtualMachineArn").asText(""); String fileSystemId = fsxFileSystemId(svmArn); if (!fileSystemId.isEmpty()) { - configuration.put("FsxFilesystemArn", AwsArnUtils.Arn.of("fsx", arnRegion(svmArn, region), - arnAccountId(svmArn), "file-system/" + fileSystemId).toString()); + configuration.put("FsxFilesystemArn", AwsArnUtils.Arn.of("fsx", AwsArnUtils.regionOrDefault(svmArn, region), + AwsArnUtils.accountOrDefault(svmArn, regionResolver.getAccountId()), "file-system/" + fileSystemId).toString()); } } case HDFS -> { @@ -431,7 +431,7 @@ public class DataSyncService { fileSystemUri(type, configuration.path("FsxFilesystemArn").asText(""), region, configuration); case FSX_ONTAP -> { String svmArn = configuration.path("StorageVirtualMachineArn").asText(""); - yield "fsxn://" + arnRegion(svmArn, region) + "." + fsxFileSystemId(svmArn) + "." + yield "fsxn://" + AwsArnUtils.regionOrDefault(svmArn, region) + "." + fsxFileSystemId(svmArn) + "." + fsxStorageVirtualMachineId(svmArn) + subdirectory(configuration); } case HDFS -> { @@ -459,7 +459,7 @@ public class DataSyncService { if (slash >= 0) { fileSystemId = fileSystemId.substring(slash + 1); } - return type.uriScheme() + "://" + arnRegion(fileSystemArn, region) + "." + fileSystemId + return type.uriScheme() + "://" + AwsArnUtils.regionOrDefault(fileSystemArn, region) + "." + fileSystemId + subdirectory(configuration); } @@ -497,19 +497,8 @@ public class DataSyncService { return segments.length > 2 ? segments[2] : ""; } - private static String arnRegion(String arn, String fallback) { - String[] segments = arn.split(":", 6); - return segments.length > 3 && !segments[3].isEmpty() ? segments[3] : fallback; - } - - private String arnAccountId(String arn) { - String[] segments = arn.split(":", 6); - return segments.length > 4 && !segments[4].isEmpty() ? segments[4] : regionResolver.getAccountId(); - } - private static String arnResource(String arn) { - String[] segments = arn.split(":", 6); - return segments.length > 5 ? segments[5] : ""; + return AwsArnUtils.isArn(arn) ? AwsArnUtils.parse(arn).resource() : ""; } // ---- Filters ---- diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java index 689b7ff1a2..191279df71 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java @@ -3013,7 +3013,7 @@ public class DynamoDbService { */ public DynamoDbTableNames.ResolvedTableRef pointInTimeSourceArn(String sourceTableArn, String region, boolean sseOverride) { - if (!sourceTableArn.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(sourceTableArn)) { throw invalidParameter("ARNs must start with 'arn:': " + sourceTableArn); } AwsArnUtils.Arn arn; diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbTableNames.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbTableNames.java index 600b366adf..7be5fc048c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbTableNames.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbTableNames.java @@ -36,7 +36,7 @@ public final class DynamoDbTableNames { throw SmithyValidationErrors.validationException( SmithyValidationErrors.lengthOutOfRange("tableName", input, 3, 255)); } - if (input.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(input)) { throw invalid("TableName must be a short name, not an ARN: " + input); } validateTableName(input); @@ -60,7 +60,7 @@ public final class DynamoDbTableNames { throw SmithyValidationErrors.validationException( SmithyValidationErrors.lengthOutOfRange("tableName", input, 1, 255)); } - if (input.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(input)) { return parseArn(input); } validateTableName(input); diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackend.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackend.java index b85b3a3874..de311ddb3a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackend.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackend.java @@ -239,7 +239,7 @@ public class DynamoDbLocalBackend implements DynamoDbBackend { } static String tableName(Scope scope, String value) { - if (!value.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(value)) { return value; } DynamoDbTableNames.resolveWithRegion(value, scope.region()); @@ -528,7 +528,7 @@ public class DynamoDbLocalBackend implements DynamoDbBackend { } private String existingTableKey(Scope scope, String resourceArn) { - if (resourceArn == null || !resourceArn.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(resourceArn)) { throw new AwsException("ValidationException", "Invalid TableArn", 400); } String name = ownTableName(scope, resourceArn); diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java b/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java index 560ff36668..4075dc3cd4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java +++ b/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.Base64Url; import io.github.hectorvent.floci.core.common.RegionResolver; @@ -2967,16 +2968,15 @@ public class GlueService { } private void validateArn(String arn) { - if (arn == null || !arn.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(arn)) { throw new AwsException("InvalidInputException", "Invalid ARN", 400); } } private void validateResourceExists(String arn) { // At most six parts: a job, table or other name may itself contain ':'. - String[] parts = arn.split(":", 6); - if (parts.length >= 6) { - String resource = parts[5]; + if (AwsArnUtils.isArn(arn)) { + String resource = AwsArnUtils.parse(arn).resource(); if (resource.startsWith("job/")) { getJob(resource.substring(4)); return; diff --git a/src/main/java/io/github/hectorvent/floci/services/lakeformation/LakeFormationService.java b/src/main/java/io/github/hectorvent/floci/services/lakeformation/LakeFormationService.java index 794672b26d..343bff0700 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lakeformation/LakeFormationService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lakeformation/LakeFormationService.java @@ -41,7 +41,7 @@ public class LakeFormationService { if (request.getResourceArn() == null) { throw new AwsException("InvalidInputException", "ResourceArn is required", 400); } - if (!request.getResourceArn().startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(request.getResourceArn())) { throw new AwsException("InvalidInputException", "ResourceArn must be a valid ARN", 400); } if (storage.describeResource(region, request.getResourceArn()).isPresent()) { diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java index e6a698eecd..6b487bc5e7 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java @@ -784,11 +784,7 @@ public class RedshiftService implements ManagedSecretRotator { } private static String sourceType(String sourceArn) { - if (sourceArn == null) { - return null; - } - String[] parts = sourceArn.split(":"); - return parts.length > 2 ? parts[2] : null; + return AwsArnUtils.isArn(sourceArn) ? AwsArnUtils.parse(sourceArn).service() : null; } private static int resolveMaxRecords(Integer maxRecords) { @@ -2746,7 +2742,7 @@ public class RedshiftService implements ManagedSecretRotator { if (resourceName == null || resourceName.isBlank()) { throw new AwsException("InvalidParameterValue", "ResourceName is required.", 400); } - if (!resourceName.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(resourceName)) { throw new AwsException("InvalidParameterValue", "ResourceName must be a Redshift ARN: " + resourceName, 400); } AwsArnUtils.Arn arn; diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java index 1066cdbafa..31242c3b13 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.redshiftserverless; import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; @@ -1029,7 +1030,7 @@ public class RedshiftServerlessService implements Resettable { if (nameOrArn == null || nameOrArn.isBlank()) { throw ServerlessSupport.validation("workgroupName is required."); } - if (!nameOrArn.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(nameOrArn)) { return getWorkgroup(nameOrArn, region); } return workgroupByArn(nameOrArn, region).orElseThrow(() -> workgroupNotFound(nameOrArn)); diff --git a/src/main/java/io/github/hectorvent/floci/services/s3tables/S3TablesService.java b/src/main/java/io/github/hectorvent/floci/services/s3tables/S3TablesService.java index ff7cd26274..9e2edb6703 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3tables/S3TablesService.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3tables/S3TablesService.java @@ -312,7 +312,7 @@ public class S3TablesService { // Guard and cut must use the same needle. Checking for ":" + prefix but cutting on prefix // alone means a name that itself contains the prefix is cut at the wrong place. String needle = ":" + prefix; - if (arn == null || !arn.startsWith("arn:") || !arn.contains(needle)) { + if (!AwsArnUtils.looksLikeArn(arn) || !arn.contains(needle)) { throw notFound("The table bucket does not exist."); } return arn.substring(arn.indexOf(needle) + needle.length()); diff --git a/tools/partition/baseline.tsv b/tools/partition/baseline.tsv index 1c416f3dc4..4064f46d2a 100644 --- a/tools/partition/baseline.tsv +++ b/tools/partition/baseline.tsv @@ -22,18 +22,10 @@ arn-probe src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFron 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/dynamodb/DynamoDbService.java 1 -arn-probe src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbTableNames.java 2 -arn-probe src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackend.java 2 arn-probe src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2TargetResolver.java 1 -arn-probe src/main/java/io/github/hectorvent/floci/services/glue/GlueService.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/lakeformation/LakeFormationService.java 1 -arn-probe src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java 1 -arn-probe src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.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/s3tables/S3TablesService.java 1 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 @@ -45,13 +37,10 @@ arn-split src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecont 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/datasync/DataSyncService.java 3 arn-split src/main/java/io/github/hectorvent/floci/services/ec2/Ec2InstanceCredentials.java 3 -arn-split src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java 1 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/redshift/RedshiftService.java 1 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