refactor(rds,docdb,neptune,cloudformation): tell names from ARNs through AwsArnUtils (#6880)

* refactor(core): add AwsArnUtils.looksLikeArn for name-or-ARN parameters

A parameter that takes a name or an ARN, where AWS answers a malformed ARN with its own error, needs to treat any value starting arn: as an attempted ARN. isArn needs all six fields and would send arn:aws:rds down the name path, so the sites the earlier migration PRs left on purpose still probe with a hand-written startsWith. looksLikeArn is that probe, in one place, so the area PRs that follow can move them off the arn-probe baseline without changing the error a caller sees.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* refactor(rds,docdb,neptune,cloudformation): tell names from ARNs through AwsArnUtils

The startsWith("arn:") probes on name-or-ARN parameters in RDS, DocumentDB, Neptune, CloudFormation and the EC2 instance provisioner become AwsArnUtils.looksLikeArn, the same check, so a malformed ARN still gets the operation's own error. NeptuneQueryHandler reads a resource's region with AwsArnUtils.parse instead of a split.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

---------

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-28 20:48:48 -04:00
1 parent 22ea3ca2e0
commit 42b4bf4245
7 files changed
+21 -26

No files matched your search

@@ -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);
@@ -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<String> desired) {
@@ -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()))
@@ -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<String, String> params) {
@@ -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();
@@ -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;