forked from aws/bedrock-agentcore-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_policy.py
More file actions
70 lines (53 loc) · 2.48 KB
/
Copy pathresource_policy.py
File metadata and controls
70 lines (53 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Client for managing resource-based policies on Bedrock AgentCore resources."""
import json
import logging
from typing import Optional, Union
import boto3
from bedrock_agentcore._utils.endpoints import get_control_plane_endpoint
class ResourcePolicyClient:
"""Client for managing resource-based policies on Bedrock AgentCore resources.
Resource-based policies control which principals can invoke and manage
Agent Runtime, Endpoint, and Gateway resources.
"""
def __init__(self, region: str):
"""Initialize the client for the specified region."""
self.region = region
self.client = boto3.client(
"bedrock-agentcore-control",
region_name=region,
endpoint_url=get_control_plane_endpoint(region),
)
self.logger = logging.getLogger("bedrock_agentcore.resource_policy_client")
def put_resource_policy(self, resource_arn: str, policy: Union[str, dict]) -> dict:
"""Create or update a resource-based policy.
Args:
resource_arn: ARN of the resource to attach the policy to.
policy: Policy document as a dict (auto-serialized) or JSON string.
Returns:
The stored policy as a dict.
"""
policy_str = json.dumps(policy) if isinstance(policy, dict) else policy
self.logger.info("Putting resource policy for %s", resource_arn)
resp = self.client.put_resource_policy(resourceArn=resource_arn, policy=policy_str)
return json.loads(resp["policy"])
def get_resource_policy(self, resource_arn: str) -> Optional[dict]:
"""Get the resource-based policy for a resource.
Args:
resource_arn: ARN of the resource.
Returns:
The policy as a dict, or None if no policy is attached.
"""
self.logger.info("Getting resource policy for %s", resource_arn)
resp = self.client.get_resource_policy(resourceArn=resource_arn)
return json.loads(resp["policy"]) if "policy" in resp else None
def delete_resource_policy(self, resource_arn: str) -> dict:
"""Delete the resource-based policy from a resource.
Args:
resource_arn: ARN of the resource.
Returns:
Raw boto3 response.
Raises:
ClientError: ResourceNotFoundException if no policy exists.
"""
self.logger.info("Deleting resource policy for %s", resource_arn)
return self.client.delete_resource_policy(resourceArn=resource_arn)