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

Class KeyManager

python/example_code/kms/key_management.py:22–256  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20
21# snippet-start:[python.example_code.kms.KeyManager.decl]
22class KeyManager:
23 def __init__(self, kms_client):
24 self.kms_client = kms_client
25 self.created_keys = []
26
27 @classmethod
28 def from_client(cls) -> "KeyManager":
29 """
30 Creates a KeyManager instance with a default KMS client.
31
32 :return: An instance of KeyManager initialized with the default KMS client.
33 """
34 kms_client = boto3.client("kms")
35 return cls(kms_client)
36
37 # snippet-end:[python.example_code.kms.KeyManager.decl]
38
39 # snippet-start:[python.example_code.kms.CreateKey]
40 def create_key(self, key_description: str) -> dict[str, any]:
41 """
42 Creates a key with a user-provided description.
43
44 :param key_description: A description for the key.
45 :return: The key ID.
46 """
47 try:
48 key = self.kms_client.create_key(Description=key_description)["KeyMetadata"]
49 self.created_keys.append(key)
50 return key
51 except ClientError as err:
52 logging.error(
53 "Couldn't create your key. Here's why: %s",
54 err.response["Error"]["Message"],
55 )
56 raise
57
58 # snippet-end:[python.example_code.kms.CreateKey]
59
60 # snippet-start:[python.example_code.kms.CreateAsymmetricKey]
61 def create_asymmetric_key(self) -> str:
62 """
63 Creates an asymmetric key in AWS KMS for signing messages.
64
65 :return: The ID of the created key.
66 """
67 try:
68 key = self.kms_client.create_key(
69 KeySpec="RSA_2048", KeyUsage="SIGN_VERIFY", Origin="AWS_KMS"
70 )["KeyMetadata"]
71 self.created_keys.append(key)
72 return key["KeyId"]
73 except ClientError as err:
74 logger.error(
75 "Couldn't create your key. Here's why: %s",
76 err.response["Error"]["Message"],
77 )
78 raise
79

Callers 3

kms_scenario.pyFile · 0.90
__init__Method · 0.90
key_managementFunction · 0.85

Calls

no outgoing calls

Tested by 1

__init__Method · 0.72