-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
164 lines (122 loc) · 4.21 KB
/
utilities.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from cryptography.hazmat.primitives.asymmetric import ed448
from cryptography.hazmat.primitives import serialization
from botocore.exceptions import ClientError
from boto3 import client
from pathlib import Path
from os import environ
from json import dumps, loads
aws_secret_name = 'pit_secret'
private_key_path = 'private.pem'
package_path = 'package.json'
internal_error_response = {
'error': {
'statusCode': 500,
'body': 'Internal error, please try again later.'
}
}
def parse_request_parameters(event):
if 'queryStringParameters' in event:
return event['queryStringParameters']
if 'body' in event:
return loads(event['body'])
def generate_private_key():
"""
Generate an ECC private key object
:return ed448.Ed448PrivateKey: Private key object
"""
return ed448.Ed448PrivateKey.generate()
def compute_public_key(private_key):
"""
Compute the public key using the private key
:param ed448.Ed448PrivateKey private_key: Private key object
:return ed448.Ed448PublicKey: Public key object
"""
return private_key.public_key()
def output_private_key(private_key):
"""
Compute the byte representation of the private key in PEM format
:param ed448.Ed448PrivateKey private_key: Private key object
:return bytes: Byte array representing the private key
"""
return private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
def output_public_key(public_key):
"""
Compute the byte representation of the public key in PEM format
:param ed448.Ed448PublicKey public_key: Public key object
:return bytes: Byte representation of the public key
"""
return public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
def save_private_key(private_key, file_path):
"""
Output and save private key at the given path
:param ed448.Ed448PrivateKey private_key: Private key object
:param Path file_path: Path to save private key
"""
if isinstance(file_path, str):
file_path = Path(file_path)
with file_path.open('wb') as fl:
fl.write(
output_private_key(private_key)
)
def load_private_key(file_path):
"""
Load private key from file
:param str file_path: Path to file
:return ed448.Ed448PrivateKey: Private key object
"""
if isinstance(file_path, str):
file_path = Path(file_path)
with file_path.open('rb') as fl:
return serialization.load_pem_private_key(
data=fl.read(),
password=None
)
def retrieve_private_key(secret_name):
"""
Retrieve private key from aws secret manager
:param str secret_name: ARN for the secret (i.e. private key)
:return ed448.Ed448PrivateKey: Private key
"""
secret_client = client('secretsmanager')
try:
secret_response = secret_client.get_secret_value(
SecretId=secret_name
)
except ClientError as e:
print(dumps(e.response))
return None
secret = secret_response['SecretBinary']
private_key = serialization.load_pem_private_key(
data=secret,
password=None
)
return private_key
def get_private_key(file_path=None):
"""
Get the private key from a local file or AWS secrets manager
:param str file_path: Local file path
:return dict: Private key or error
"""
if 'secret_name' in environ:
secret = retrieve_private_key(environ['secret_name'])
if not secret:
return internal_error_response
return {
'key': secret
}
if file_path is None:
file_path = Path(private_key_path)
elif isinstance(file_path, str):
file_path = Path(file_path)
if file_path and file_path.exists():
return {
'key': load_private_key(file_path)
}
return internal_error_response