Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Hector Ventura78278f4a04d9
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java
index f5190f1330..eb8fd452d7 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java
@@ -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()) {
- return null;
- }
- String[] parts = arn.split(":", 6);
- if (parts.length < 6) {
+ if (!AwsArnUtils.isArn(arn)) {
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) {
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 0652913a4a..0077b5ed4e 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
@@ -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);
diff --git a/tools/partition/baseline.tsv b/tools/partition/baseline.tsv
index 80552c13a7..eee9f42596 100644
--- a/tools/partition/baseline.tsv
+++ b/tools/partition/baseline.tsv
@@ -44,7 +44,7 @@ arn-probe src/main/java/io/github/hectorvent/floci/services/lambda/LambdaArnUtil
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 13
+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
@@ -70,7 +70,6 @@ arn-split src/main/java/io/github/hectorvent/floci/services/lambda/LambdaTagCont
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/rds/RdsQueryHandler.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