From 42e7f6f2f3c2b34496efc64d64db0a04f0dddee2 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Wed, 28 Oct 2026 21:20:13 -0500 Subject: [PATCH] refactor(rds): read the read replica source ARN through AwsArnUtils.parse (#6887) resolveReadReplicaSource split the source ARN by hand right under the looksLikeArn check. AwsArnUtils.parse gives the same partition, region, account and db: resource; an ARN that is truncated, names another account or names a cluster still finds no source. Signed-off-by: Hector Ventura --- .../hectorvent/floci/services/rds/RdsService.java | 10 +++++----- .../floci/services/rds/RdsServiceTest.java | 13 +++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) 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 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.java index 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); + } + } + @Test void createReadReplicaRollsBackWhenTheCopyFails() { createPostgresSource("primary");