Skip to content

Commit 55e2fbc

Browse files
authored
RDS: extract ARN parsing into dedicated helper (#10180)
1 parent bef4271 commit 55e2fbc

4 files changed

Lines changed: 64 additions & 15 deletions

File tree

‎moto/rds/models.py‎

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
FilterDef,
8989
apply_filter,
9090
merge_filters,
91+
split_arn,
9192
valid_preferred_maintenance_window,
9293
validate_filters,
9394
)
@@ -2478,8 +2479,7 @@ def get_snapshot(
24782479
) -> DBSnapshot | DBClusterSnapshot:
24792480
region = self.region_name
24802481
if identifier.startswith("arn"):
2481-
region = identifier.split(":")[3]
2482-
identifier = identifier.split(":")[-1]
2482+
region, _, _, identifier = split_arn(identifier)
24832483
backend = self.get_backend("rds", region=region)
24842484
snapshots = backend.resource_map[resource_type]
24852485
if identifier not in snapshots:
@@ -2758,8 +2758,7 @@ def reboot_db_instance(self, db_instance_identifier: str) -> DBInstance:
27582758
return self.describe_db_instances(db_instance_identifier)[0]
27592759

27602760
def extract_snapshot_name_from_arn(self, snapshot_arn: str) -> str:
2761-
arn_breakdown = snapshot_arn.split(":")
2762-
region_name, account_id, resource_type, snapshot_name = arn_breakdown[3:7]
2761+
_, _, resource_type, snapshot_name = split_arn(snapshot_arn)
27632762
if resource_type != "snapshot":
27642763
raise InvalidParameterValue(
27652764
"The parameter SourceDBSnapshotIdentifier is not a valid identifier. "
@@ -3710,8 +3709,7 @@ def stop_db_cluster(self, cluster_identifier: str) -> DBCluster:
37103709
def start_export_task(self, kwargs: dict[str, Any]) -> ExportTask:
37113710
export_task_id = kwargs["export_task_identifier"]
37123711
source_arn = kwargs["source_arn"]
3713-
snapshot_id = source_arn.split(":")[-1]
3714-
snapshot_type = source_arn.split(":")[-2]
3712+
_, _, snapshot_type, snapshot_id = split_arn(source_arn)
37153713

37163714
if export_task_id in self.export_tasks:
37173715
raise ExportTaskAlreadyExistsError(export_task_id)
@@ -3797,15 +3795,8 @@ def _find_resource(self, resource_type: str, resource_name: str) -> Any:
37973795

37983796
def _get_resource_for_tagging(self, arn: str) -> Any:
37993797
if self.arn_regex.match(arn):
3800-
arn_breakdown = arn.split(":")
3801-
resource_type = arn_breakdown[len(arn_breakdown) - 2]
3802-
resource_name = arn_breakdown[len(arn_breakdown) - 1]
3803-
# FIXME: HACK for automated snapshots
3804-
if resource_type == "rds":
3805-
resource_type = arn_breakdown[-3]
3806-
resource_name = arn_breakdown[-2] + ":" + arn_breakdown[-1]
3807-
resource = self._find_resource(resource_type, resource_name)
3808-
return resource
3798+
_, _, resource_type, resource_name = split_arn(arn)
3799+
return self._find_resource(resource_type, resource_name)
38093800
raise RDSClientError("InvalidParameterValue", f"Invalid resource name: {arn}")
38103801

38113802
def list_tags_for_resource(self, arn: str) -> list[dict[str, str]]:

‎moto/rds/utils.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313
SECONDS_IN_ONE_DAY = 24 * 60 * 60
1414

1515

16+
def split_arn(arn: str) -> tuple[str, str, str, str]:
17+
"""Split an RDS ARN into (region, account_id, resource_type, resource_id).
18+
19+
Example:
20+
arn:aws:rds:<region>:<account_id>:<resource_type>:<resource_id>
21+
22+
The resource identifier can itself contain colons - automated snapshots are
23+
named `rds:<db-instance-identifier>-<timestamp>` - so everything after the
24+
resource type is kept as-is.
25+
"""
26+
_, _, _, region, account_id, resource_type, resource_id = arn.split(":", 6)
27+
return region, account_id, resource_type, resource_id
28+
29+
1630
@dataclass
1731
class FilterDef:
1832
# A list of object attributes to check against the filter values.

‎tests/test_rds/test_rds.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,27 @@ def test_copy_db_snapshots_snapshot_type_is_always_manual(client):
993993
assert snapshot2["SnapshotType"] == "manual"
994994

995995

996+
@mock_aws
997+
def test_copy_db_snapshot_automated_snapshot_by_arn(client):
998+
# Automated snapshots have identifiers that contain a colon,
999+
# e.g. "rds:db-master-1-2025-01-02-00-00", which means their ARN
1000+
# cannot be parsed by naively splitting on the colon.
1001+
create_db_instance(DBInstanceIdentifier="db-primary-1")
1002+
source_snapshot = client.describe_db_snapshots(
1003+
DBInstanceIdentifier="db-primary-1", SnapshotType="automated"
1004+
)["DBSnapshots"][0]
1005+
assert source_snapshot["DBSnapshotIdentifier"].startswith("rds:")
1006+
1007+
target_snapshot = client.copy_db_snapshot(
1008+
SourceDBSnapshotIdentifier=source_snapshot["DBSnapshotArn"],
1009+
TargetDBSnapshotIdentifier="snapshot-copy",
1010+
)["DBSnapshot"]
1011+
1012+
assert target_snapshot["DBSnapshotIdentifier"] == "snapshot-copy"
1013+
assert target_snapshot["DBInstanceIdentifier"] == "db-primary-1"
1014+
assert target_snapshot["SnapshotType"] == "manual"
1015+
1016+
9961017
@mock_aws
9971018
def test_copy_db_snapshot_invalid_arns(client):
9981019
invalid_arn = (

‎tests/test_rds/test_rds_export_tasks.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,29 @@ def test_start_export_task_db():
9393
assert export["SourceType"] == "SNAPSHOT"
9494

9595

96+
@mock_aws
97+
def test_start_export_task_automated_db_snapshot():
98+
# The identifier of an automated snapshot contains a colon,
99+
# so its ARN cannot be parsed by simply splitting on the colon.
100+
client = boto3.client("rds", region_name="us-west-2")
101+
_prepare_db_snapshot(client)
102+
snapshot = client.describe_db_snapshots(
103+
DBInstanceIdentifier="db-primary-1", SnapshotType="automated"
104+
)["DBSnapshots"][0]
105+
source_arn = snapshot["DBSnapshotArn"]
106+
107+
export = client.start_export_task(
108+
ExportTaskIdentifier="export-snapshot-1",
109+
SourceArn=source_arn,
110+
S3BucketName="export-bucket",
111+
IamRoleArn="arn:aws:iam:::role/export-role",
112+
KmsKeyId="arn:aws:kms:::key/0ea3fef3-80a7-4778-9d8c-1c0c6EXAMPLE",
113+
)
114+
115+
assert export["SourceArn"] == source_arn
116+
assert export["SourceType"] == "SNAPSHOT"
117+
118+
96119
@mock_aws
97120
def test_start_export_task_db_cluster():
98121
client = boto3.client("rds", region_name="us-west-2")

0 commit comments

Comments
 (0)