Repository navigation
Expand file tree
/
Copy pathkey_pair.py
More file actions
146 lines (123 loc) · 5.73 KB
/
Copy pathkey_pair.py
File metadata and controls
146 lines (123 loc) · 5.73 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import logging
import os
import tempfile
from typing import Optional, Union
import boto3
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
# snippet-start:[python.example_code.ec2.KeyPairWrapper.class]
# snippet-start:[python.example_code.ec2.KeyPairWrapper.decl]
class KeyPairWrapper:
"""
Encapsulates Amazon Elastic Compute Cloud (Amazon EC2) key pair actions.
This class provides methods to create, list, and delete EC2 key pairs.
"""
def __init__(
self,
ec2_client: boto3.client,
key_file_dir: Union[tempfile.TemporaryDirectory, str],
key_pair: Optional[dict] = None,
):
"""
Initializes the KeyPairWrapper with the specified EC2 client, key file directory,
and an optional key pair.
:param ec2_client: A Boto3 Amazon EC2 client. This client provides low-level
access to AWS EC2 services.
:param key_file_dir: The folder where the private key information is stored.
This should be a secure folder.
:param key_pair: A dictionary representing the Boto3 KeyPair object.
This is a high-level object that wraps key pair actions. Optional.
"""
self.ec2_client = ec2_client
self.key_pair = key_pair
self.key_file_path: Optional[str] = None
self.key_file_dir = key_file_dir
@classmethod
def from_client(cls) -> "KeyPairWrapper":
"""
Class method to create an instance of KeyPairWrapper using a new EC2 client
and a temporary directory for storing key files.
:return: An instance of KeyPairWrapper.
"""
ec2_client = boto3.client("ec2")
return cls(ec2_client, tempfile.TemporaryDirectory())
# snippet-end:[python.example_code.ec2.KeyPairWrapper.decl]
# snippet-start:[python.example_code.ec2.CreateKeyPair]
def create(self, key_name: str) -> dict:
"""
Creates a key pair that can be used to securely connect to an EC2 instance.
The returned key pair contains private key information that cannot be retrieved
again. The private key data is stored as a .pem file.
:param key_name: The name of the key pair to create.
:return: A dictionary representing the Boto3 KeyPair object that represents the newly created key pair.
:raises ClientError: If there is an error in creating the key pair, for example, if a key pair with the same name already exists.
"""
try:
response = self.ec2_client.create_key_pair(KeyName=key_name)
self.key_pair = response
self.key_file_path = os.path.join(
self.key_file_dir.name, f"{self.key_pair['KeyName']}.pem"
)
with open(self.key_file_path, "w") as key_file:
key_file.write(self.key_pair["KeyMaterial"])
except ClientError as err:
if err.response["Error"]["Code"] == "InvalidKeyPair.Duplicate":
logger.error(
f"A key pair called {key_name} already exists. "
"Please choose a different name for your key pair "
"or delete the existing key pair before creating."
)
raise
else:
return self.key_pair
# snippet-end:[python.example_code.ec2.CreateKeyPair]
# snippet-start:[python.example_code.ec2.DescribeKeyPairs]
def list(self, limit: Optional[int] = None) -> None:
"""
Displays a list of key pairs for the current account.
WARNING: Results are not paginated.
:param limit: The maximum number of key pairs to list. If not specified,
all key pairs will be listed.
:raises ClientError: If there is an error in listing the key pairs.
"""
try:
response = self.ec2_client.describe_key_pairs()
key_pairs = response.get("KeyPairs", [])
if limit:
key_pairs = key_pairs[:limit]
for key_pair in key_pairs:
logger.info(
f"Found {key_pair['KeyType']} key '{key_pair['KeyName']}' with fingerprint:"
)
logger.info(f"\t{key_pair['KeyFingerprint']}")
except ClientError as err:
logger.error(f"Failed to list key pairs: {str(err)}")
raise
# snippet-end:[python.example_code.ec2.DescribeKeyPairs]
# snippet-start:[python.example_code.ec2.DeleteKeyPair]
def delete(self, key_name: str) -> bool:
"""
Deletes a key pair by its name.
:param key_name: The name of the key pair to delete.
:return: A boolean indicating whether the deletion was successful.
:raises ClientError: If there is an error in deleting the key pair, for example,
if the key pair does not exist.
"""
try:
self.ec2_client.delete_key_pair(KeyName=key_name)
logger.info(f"Successfully deleted key pair: {key_name}")
self.key_pair = None
return True
except self.ec2_client.exceptions.ClientError as err:
logger.error(f"Deletion failed for key pair: {key_name}")
error_code = err.response["Error"]["Code"]
if error_code == "InvalidKeyPair.NotFound":
logger.error(
f"The key pair '{key_name}' does not exist and cannot be deleted. "
"Please verify the key pair name and try again."
)
raise
# snippet-end:[python.example_code.ec2.DeleteKeyPair]
# snippet-end:[python.example_code.ec2.KeyPairWrapper.class]