Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(rds): read the read replica source ARN through AwsArnUtils.parse (#6887)
Hector Ventura42e7f6f2f3c2
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.javaindex 75f6c6a5c3..22e97e9e3d 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@@ -1713,11 +1713,11 @@ public class RdsService implements Resettable, ResourceProvider {String sourceId = sourceRef;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])- && accountId.equals(parts[4])) {- region = parts[3].isBlank() ? requestRegion : parts[3];- sourceId = parts[6];+ AwsArnUtils.Arn arn = AwsArnUtils.isArn(sourceRef) ? AwsArnUtils.parse(sourceRef) : null;+ if (arn != null && "rds".equals(arn.service()) && arn.resource().startsWith("db:")+ && accountId.equals(arn.accountId())) {+ region = arn.region().isBlank() ? requestRegion : arn.region();+ sourceId = arn.resource().substring("db:".length());} else {throw new AwsException("DBInstanceNotFound","DB instance " + sourceRef + " not found.", 404);diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.javaindex 6447c59aa8..fec927b526 100644--- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java@@ -10156,6 +10156,19 @@ class RdsServiceTest {"a refused request leaves no dangling link on the source");}+ @Test+ void createReadReplicaFindsNoSourceForAnArnThatIsNotThisAccountsInstance() {+ createPostgresSource("primary");++ for (String source : List.of("arn:aws:rds:us-east-1:123456789012:db",+ "arn:aws:rds:us-east-1:999999999999:db:primary",+ "arn:aws:rds:us-east-1:123456789012:cluster:primary")) {+ AwsException notFound = assertThrows(AwsException.class, () ->+ rdsService.createDbInstanceReadReplica(replicaRequest("r", source), null));+ assertEquals("DBInstanceNotFound", notFound.getErrorCode(), source);+ }+ }+@Testvoid createReadReplicaRollsBackWhenTheCopyFails() {createPostgresSource("primary");