MCPcopy Create free account
hub / github.com/awsdocs/aws-doc-sdk-examples / get_policy

Method get_policy

python/example_code/kms/key_policies.py:63–87  ·  view source on GitHub ↗

Gets the policy of a key. :param key_id: The ARN or ID of the key to query. :return: The key policy as a dict.

(self, key_id: str)

Source from the content-addressed store, hash-verified

61
62 # snippet-start:[python.example_code.kms.GetKeyPolicy]
63 def get_policy(self, key_id: str) -> dict[str, str]:
64 """
65 Gets the policy of a key.
66
67 :param key_id: The ARN or ID of the key to query.
68 :return: The key policy as a dict.
69 """
70 if key_id != "":
71 try:
72 response = self.kms_client.get_key_policy(
73 KeyId=key_id,
74 )
75 policy = json.loads(response["Policy"])
76 except ClientError as err:
77 logger.error(
78 "Couldn't get policy for key %s. Here's why: %s",
79 key_id,
80 err.response["Error"]["Message"],
81 )
82 raise
83 else:
84 pprint(policy)
85 return policy
86 else:
87 print("Skipping get policy demo.")
88
89 # snippet-end:[python.example_code.kms.GetKeyPolicy]
90

Callers 3

key_policiesFunction · 0.95
kms_scenarioMethod · 0.45
create_policyMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected