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)
| 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 |
no outgoing calls
no test coverage detected