Skip to content

Commit a10c0fd

Browse files
mahlerajariy17
andauthored
chore: Modify arn parsing in runtime client to allow for different (#362)
partitions - Added is_valid_partition function to allow for govcloud arns Co-authored-by: Trirmadura J Ariyawansa <tjariy@amazon.com>
1 parent 29b0115 commit a10c0fd

4 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/bedrock_agentcore/runtime/agent_core_runtime_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from botocore.awsrequest import AWSRequest
1818

1919
from .._utils.endpoints import get_data_plane_endpoint
20+
from .utils import is_valid_partition
2021

2122
DEFAULT_PRESIGNED_URL_TIMEOUT = 300
2223
MAX_PRESIGNED_URL_TIMEOUT = 300
@@ -68,7 +69,7 @@ def _parse_runtime_arn(self, runtime_arn: str) -> Dict[str, str]:
6869
if len(parts) != 6:
6970
raise ValueError(f"Invalid runtime ARN format: {runtime_arn}")
7071

71-
if parts[0] != "arn" or parts[1] != "aws" or parts[2] != "bedrock-agentcore":
72+
if parts[0] != "arn" or not is_valid_partition(parts[1]) or parts[2] != "bedrock-agentcore":
7273
raise ValueError(f"Invalid runtime ARN format: {runtime_arn}")
7374

7475
# Parse the resource part (runtime/{runtime_id})

src/bedrock_agentcore/runtime/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ def convert_complex_objects(obj: Any, _depth: int = 0) -> Any:
3333
# Return primitives as-is
3434
else:
3535
return obj
36+
37+
38+
def is_valid_partition(partition: str) -> bool:
39+
"""Returns if parsed-arn partition is valid."""
40+
return partition in ("aws", "aws-us-gov")

tests/bedrock_agentcore/runtime/test_utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from pydantic import BaseModel
77

8-
from bedrock_agentcore.runtime.utils import convert_complex_objects
8+
from bedrock_agentcore.runtime.utils import convert_complex_objects, is_valid_partition
99

1010

1111
class TestConvertComplexObjects:
@@ -335,3 +335,10 @@ class Person(BaseModel):
335335
assert isinstance(result["address"], dict)
336336
assert result["address"]["street"] == "456 Oak St"
337337
assert result["address"]["city"] == "Somewhere"
338+
339+
def test_valid_partitions(self):
340+
"""Test valid partitions for arn parsing"""
341+
342+
assert is_valid_partition("aws")
343+
assert is_valid_partition("aws-us-gov")
344+
assert not is_valid_partition("aws-iso")

tests/unit/runtime/test_agent_core_runtime_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ def test_parse_valid_arn(self):
3636
assert result["account_id"] == "123456789012"
3737
assert result["runtime_id"] == "my-runtime-abc123"
3838

39+
def test_parse_valid_gov_arn(self):
40+
"""Test parsing a valid govcloud runtime ARN."""
41+
client = AgentCoreRuntimeClient(region="us-gov-west-1")
42+
arn = "arn:aws-us-gov:bedrock-agentcore:us-gov-west-1:123456789012:runtime/my-runtime-abc123"
43+
44+
result = client._parse_runtime_arn(arn)
45+
46+
assert result["region"] == "us-gov-west-1"
47+
assert result["account_id"] == "123456789012"
48+
assert result["runtime_id"] == "my-runtime-abc123"
49+
50+
def test_parse_invalid_arn_partition(self):
51+
"""Test parsing an invalid runtime ARN."""
52+
client = AgentCoreRuntimeClient(region="us-iso-east-1")
53+
invalid_arn = "arn:aws-iso:bedrock-agentcore:us-iso-east-1:123456789012:runtime/my-runtime-abc123"
54+
55+
with pytest.raises(ValueError, match="Invalid runtime ARN format"):
56+
client._parse_runtime_arn(invalid_arn)
57+
3958
def test_parse_invalid_arn_raises_error(self):
4059
"""Test that invalid ARN format raises ValueError."""
4160
client = AgentCoreRuntimeClient(region="us-west-2")

0 commit comments

Comments
 (0)