refactor(rds): read and check ARNs through AwsArnUtils (#6485)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-26 23:34:32 -04:00
1 parent b0cd70401b
commit 78278f4a04
3 files changed
+13 -24

No files matched your search

@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.rds;
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;
@@ -3356,16 +3357,7 @@ public class RdsQueryHandler {
return fallbackSubnetGroup(instance, "default", "default subnet group");
}
return service.getDbSubnetGroup(
groupName, regionFromRdsArn(instance.getDbInstanceArn()));
}
private String regionFromRdsArn(String arn) {
if (arn == null || arn.isBlank()) {
return config.defaultRegion();
}
String[] parts = arn.split(":", 6);
return parts.length == 6 && !parts[3].isBlank()
? parts[3] : config.defaultRegion();
groupName, AwsArnUtils.regionOrDefault(instance.getDbInstanceArn(), config.defaultRegion()));
}
private DbSubnetGroup fallbackSubnetGroup(DbInstance instance, String name, String description) {
@@ -3388,14 +3380,12 @@ public class RdsQueryHandler {
private static String subnetGroupArnForInstance(DbInstance instance, String name) {
String arn = instance.getDbInstanceArn();
if (arn == null || arn.isBlank()) {
if (!AwsArnUtils.isArn(arn)) {
return null;
}
String[] parts = arn.split(":", 6);
if (parts.length < 6) {
return null;
}
return String.join(":", parts[0], parts[1], parts[2], parts[3], parts[4], "subgrp:" + name);
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
return new AwsArnUtils.Arn(parsed.partition(), parsed.service(), parsed.region(), parsed.accountId(),
"subgrp:" + name).toString();
}
private String paramGroupInnerXml(DbParameterGroup g) {
@@ -1778,7 +1778,7 @@ public class RdsService implements Resettable, ResourceProvider {
* Region as the linking instance, an ARN names one anywhere in the account.
*/
private DbInstance findLinkedInstance(String accountId, DbInstance from, String linkName) {
if (linkName.startsWith("arn:")) {
if (AwsArnUtils.isArn(linkName)) {
for (DbInstance candidate : instances.scan(k -> true)) {
if (linkName.equalsIgnoreCase(candidate.getDbInstanceArn())
&& accountId.equals(accountIdFromArn(candidate.getDbInstanceArn()))) {
@@ -1808,7 +1808,7 @@ public class RdsService implements Resettable, ResourceProvider {
.equalsIgnoreCase(replica.getReadReplicaSourceDbInstanceIdentifier())) {
continue;
}
if (replicaName.startsWith("arn:")) {
if (AwsArnUtils.isArn(replicaName)) {
replica.setReadReplicationStatus(READ_REPLICATION_TERMINATED);
} else {
replica.setReadReplicaSourceDbInstanceIdentifier(null);
@@ -2646,7 +2646,7 @@ public class RdsService implements Resettable, ResourceProvider {
}
private String resourceRegionOrDefault(String resourceName) {
return resourceName != null && resourceName.startsWith("arn:")
return AwsArnUtils.isArn(resourceName)
? regionFromArn(resourceName) : regionResolver.getDefaultRegion();
}
@@ -2939,7 +2939,7 @@ public class RdsService implements Resettable, ResourceProvider {
Map<String, DbInstance> unique = new LinkedHashMap<>();
for (DbInstance instance : instances.scan(k -> true)) {
boolean matchesFilter = filterId == null || filterId.isBlank()
|| (filterId.startsWith("arn:")
|| (AwsArnUtils.isArn(filterId)
? filterId.equalsIgnoreCase(instance.getDbInstanceArn())
: instance.getDbInstanceIdentifier().equalsIgnoreCase(filterId));
if (matchesFilter
@@ -4963,7 +4963,7 @@ public class RdsService implements Resettable, ResourceProvider {
Map<String, DbCluster> unique = new LinkedHashMap<>();
for (DbCluster cluster : clusters.scan(k -> true)) {
boolean matchesFilter = filterId == null || filterId.isBlank()
|| (filterId.startsWith("arn:")
|| (AwsArnUtils.isArn(filterId)
? filterId.equalsIgnoreCase(cluster.getDbClusterArn())
: cluster.getDbClusterIdentifier().equalsIgnoreCase(filterId));
if (matchesFilter
@@ -5781,7 +5781,7 @@ public class RdsService implements Resettable, ResourceProvider {
if (reference == null || reference.isBlank()) {
throw new AwsException("InvalidParameterValue", "DBClusterIdentifier is required.", 400);
}
if (reference.startsWith("arn:")) {
if (AwsArnUtils.isArn(reference)) {
DbCluster cluster = findClusterByArn(accountId, reference);
if (cluster == null) {
throw new AwsException("DBClusterNotFoundFault", "DB cluster " + reference + " not found.", 404);