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:<name> resource; an ARN that is truncated, names another account or names a cluster still finds no source.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-28 22:20:13 -04:00
1 parent 63aa7710d0
commit 42e7f6f2f3
2 files changed
+18 -5

No files matched your search

@@ -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);
@@ -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");