diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java index d5fac7c417..d9133d48c3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java @@ -1270,7 +1270,7 @@ public class CloudFormationService implements ResourceProvider { public void deleteChangeSet(String stackName, String changeSetName, String region) { String accountId = currentAccount(); boolean byArnAlone = (stackName == null || stackName.isBlank()) - && changeSetName != null && changeSetName.startsWith("arn:"); + && AwsArnUtils.looksLikeArn(changeSetName); Stack stack = byArnAlone ? findStackForChangeSetArn(changeSetName, region, accountId) : getStackForChangeSet(stackName, changeSetName, region, accountId); @@ -1306,7 +1306,7 @@ public class CloudFormationService implements ResourceProvider { purgeExpiredDeletedStacks(); Stack stack = resolveStack(stackName, region, accountId); if (stack == null) { - if (stackName != null && stackName.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(stackName)) { try { AwsArnUtils.Arn arn = AwsArnUtils.parse(stackName); if (!accountId.equals(arn.accountId()) || !region.equals(arn.region())) { @@ -4388,7 +4388,7 @@ public class CloudFormationService implements ResourceProvider { * when referencing a changeset by the ID returned from CreateChangeSet. */ private String resolveChangeSetName(String changeSetNameOrArn, String region, String accountId) { - if (changeSetNameOrArn == null || !changeSetNameOrArn.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(changeSetNameOrArn)) { return changeSetNameOrArn; } try { @@ -4415,7 +4415,7 @@ public class CloudFormationService implements ResourceProvider { if (stackNameOrArn != null && !stackNameOrArn.isBlank()) { return getStackOrThrow(stackNameOrArn, region, accountId); } - if (changeSetNameOrArn == null || !changeSetNameOrArn.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(changeSetNameOrArn)) { throw new AwsException("ValidationError", "StackName is required when ChangeSetName is not an ARN", 400); } Stack stack = findStackForChangeSetArn(changeSetNameOrArn, region, accountId); diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java index 0331f604c9..85c61e4a3a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; 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.cloudformation.CloudFormationTemplateEngine; import io.github.hectorvent.floci.services.cloudformation.model.StackResource; @@ -346,7 +347,7 @@ public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner { if (nameOrArn == null || nameOrArn.isBlank()) { return null; } - return nameOrArn.startsWith("arn:") ? nameOrArn : ec2Service.resolveIamInstanceProfileName(nameOrArn); + return AwsArnUtils.looksLikeArn(nameOrArn) ? nameOrArn : ec2Service.resolveIamInstanceProfileName(nameOrArn); } private static boolean sameSecurityGroups(Instance prior, List desired) { diff --git a/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.java b/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.java index 57f8af9f9c..5697168377 100644 --- a/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.java +++ b/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.java @@ -559,7 +559,7 @@ public class DocDbService { * name in one comparison, as the db-cluster-id filter already does. */ public boolean hasResourceWithArn(String arn) { - if (arn == null || !arn.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(arn)) { return false; } return clusters.scan(k -> true).stream() @@ -586,7 +586,7 @@ public class DocDbService { // full ARN against each cluster's stored ARN rather than reducing it to // the bare identifier, so a cross-account or cross-region ARN does not // resolve a same-named local cluster. - if (filterId.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(filterId)) { return inRegion(clusters, region, DocDbCluster::getDbClusterIdentifier).stream() .map(c -> filled(c, region)) .filter(c -> filterId.equalsIgnoreCase(c.getDbClusterArn())) @@ -746,7 +746,7 @@ public class DocDbService { if (filterId != null && !filterId.isBlank()) { // The db-instance-id filter accepts ARNs as well as identifiers; see // listDbClusters for why the match is against the stored ARN. - if (filterId.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(filterId)) { return inRegion(instances, region, DocDbInstance::getDbInstanceIdentifier).stream() .map(i -> filled(i, region)) .filter(i -> filterId.equalsIgnoreCase(i.getDbInstanceArn())) diff --git a/src/main/java/io/github/hectorvent/floci/services/neptune/NeptuneQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/neptune/NeptuneQueryHandler.java index dfc91a1439..be872f62b7 100644 --- a/src/main/java/io/github/hectorvent/floci/services/neptune/NeptuneQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/neptune/NeptuneQueryHandler.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.neptune; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsErrorMessages; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.AwsNamespaces; @@ -137,8 +138,7 @@ public class NeptuneQueryHandler { if (region == null || region.isBlank()) { return true; } - String[] parts = arn == null ? new String[0] : arn.split(":", -1); - return parts.length >= 4 && region.equals(parts[3]); + return AwsArnUtils.isArn(arn) && region.equals(AwsArnUtils.parse(arn).region()); } private Response handleDeleteDbCluster(MultivaluedMap params) { diff --git a/src/main/java/io/github/hectorvent/floci/services/neptune/NeptuneService.java b/src/main/java/io/github/hectorvent/floci/services/neptune/NeptuneService.java index 366278178e..892031c56a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/neptune/NeptuneService.java +++ b/src/main/java/io/github/hectorvent/floci/services/neptune/NeptuneService.java @@ -203,7 +203,7 @@ public class NeptuneService { } public boolean hasResourceWithArn(String arn) { - if (arn == null || !arn.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(arn)) { return false; } return scanClusters().stream() @@ -218,7 +218,7 @@ public class NeptuneService { // full ARN against each cluster's stored ARN rather than reducing it to // the bare identifier, so a cross-account or cross-region ARN does not // resolve a same-named local cluster. - if (filterId.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(filterId)) { return scanClusters().stream() .filter(c -> filterId.equalsIgnoreCase(c.getDbClusterArn())) .toList(); @@ -390,7 +390,7 @@ public class NeptuneService { if (filterId != null && !filterId.isBlank()) { // The db-instance-id filter accepts ARNs as well as identifiers; see // listDbClusters for why the match is against the stored ARN. - if (filterId.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(filterId)) { return scanInstances().stream() .filter(i -> filterId.equalsIgnoreCase(i.getDbInstanceArn())) .toList(); diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java index cad5c6d272..75f6c6a5c3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java @@ -1160,7 +1160,7 @@ public class RdsService implements Resettable, ResourceProvider { String sourceId = sourceIdentifier; String sourceRegion = targetRegion; String sourceAccount = currentAccountId(); - if (sourceIdentifier != null && sourceIdentifier.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(sourceIdentifier)) { try { AwsArnUtils.Arn parsed = AwsArnUtils.parse(sourceIdentifier); // Both regions must belong to the ARN's partition; changing its prefix must @@ -1520,7 +1520,7 @@ public class RdsService implements Resettable, ResourceProvider { throw new AwsException("InvalidParameterCombination", "Either SourceDBInstanceIdentifier or SourceDBClusterIdentifier must be specified.", 400); } - boolean sourceByArn = sourceRef.startsWith("arn:"); + boolean sourceByArn = AwsArnUtils.looksLikeArn(sourceRef); if (request.replicaMode() != null && !request.replicaMode().isBlank()) { // ReplicaMode selects mounted or open-read-only for Db2 and Oracle replicas; no // emulated engine is either, so AWS's parameter check applies to all of them. @@ -1711,7 +1711,7 @@ public class RdsService implements Resettable, ResourceProvider { String accountId = currentAccountId(); String region = requestRegion; String sourceId = sourceRef; - if (sourceRef.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(sourceRef)) { // A source in another Region is named by ARN; the replica lands in the request Region. String[] parts = sourceRef.split(":", 7); if (parts.length == 7 && "rds".equals(parts[2]) && "db".equals(parts[5]) @@ -2269,7 +2269,7 @@ public class RdsService implements Resettable, ResourceProvider { /** A cluster named by identifier or by ARN; an ARN for another account or Region names no cluster here. */ private String clusterIdentifierFromArnOrName(String source, String region) { - if (!source.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(source)) { return source; } return clusterIdFromArn(source, region, @@ -2304,7 +2304,7 @@ public class RdsService implements Resettable, ResourceProvider { if (source == null || source.isBlank()) { throw new AwsException("InvalidParameterValue", "A DB cluster snapshot identifier is required.", 400); } - if (!source.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(source)) { return source; } AwsArnUtils.Arn arn; @@ -2470,7 +2470,7 @@ public class RdsService implements Resettable, ResourceProvider { String effectiveRegion = effectiveRegion(region); String type = "db"; String id = resourceName; - if (resourceName.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(resourceName)) { AwsArnUtils.Arn arn; try { arn = AwsArnUtils.parse(resourceName); @@ -7129,7 +7129,7 @@ public class RdsService implements Resettable, ResourceProvider { * ({@code arn:aws:rds:region:account:pg:name}); names cannot contain a colon. */ private static String groupNameFromIdentifier(String identifier) { - if (identifier != null && identifier.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(identifier)) { return identifier.substring(identifier.lastIndexOf(':') + 1); } return identifier; diff --git a/tools/partition/baseline.tsv b/tools/partition/baseline.tsv index d6e9a589b0..729e05f2a3 100644 --- a/tools/partition/baseline.tsv +++ b/tools/partition/baseline.tsv @@ -19,14 +19,11 @@ arn-probe src/main/java/io/github/hectorvent/floci/services/appsync/graphql/reso 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/cloudformation/CloudFormationService.java 4 -arn-probe src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java 1 arn-probe src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/EcsCfnProvisioner.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/docdb/DocDbService.java 3 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 @@ -40,8 +37,6 @@ arn-probe src/main/java/io/github/hectorvent/floci/services/lakeformation/LakeFo arn-probe src/main/java/io/github/hectorvent/floci/services/lambda/LambdaArnUtils.java 2 arn-probe src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java 4 arn-probe src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsService.java 1 -arn-probe src/main/java/io/github/hectorvent/floci/services/neptune/NeptuneService.java 3 -arn-probe src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java 7 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 @@ -65,7 +60,6 @@ arn-split src/main/java/io/github/hectorvent/floci/services/kms/KmsJsonHandler.j arn-split src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/lambda/LambdaTagController.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsController.java 1 -arn-split src/main/java/io/github/hectorvent/floci/services/neptune/NeptuneQueryHandler.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