-
Notifications
You must be signed in to change notification settings - Fork 8
/
pem.py
100 lines (80 loc) · 2.61 KB
/
pem.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
import json
import boto3
from botocore.exceptions import ClientError
SECRETS_CLIENT = boto3.client('secretsmanager')
def add_pem(event, context):
"""Add pem file and associate to a k8s cluster via a tag"""
pem = event['body']
cluster_name = event['queryStringParameters']['cluster_name']
try:
print(f'Adding hyper-kube-config-pem-{cluster_name}')
SECRETS_CLIENT.create_secret(
Name=f'hyper-kube-config-pem-{cluster_name}',
SecretString=pem,
Tags=[
{
'Key': 'cluster_name',
'Value': cluster_name
},
{
'Key': 'pem',
'Value': "True"
},
]
)
return {
"statusCode": 200,
"body": json.dumps(
{"message": f'Pem added and associated to {cluster_name}'}
),
}
except ClientError as e:
return {
"statusCode": 503,
"body": json.dumps(
{"message":
f'Failed to add pem and associate to {cluster_name}: {e}'}
)
}
def get_pem(event, context):
"""Get the pem file for a specific cluster"""
cluster_name = event['queryStringParameters']['cluster_name']
try:
print(f'Getting hyper-kube-config-pem-{cluster_name}')
pem = SECRETS_CLIENT.get_secret_value(
SecretId=f'hyper-kube-config-pem-{cluster_name}',
)
return {
"statusCode": 200,
"body": str(pem['SecretString']),
}
except ClientError as e:
return {
"statusCode": 503,
"body": json.dumps(
{"message":
f'Failed to get pem associated to {cluster_name}: {e}'}
)
}
def remove_pem(event, context):
"""Remove pem secret for a specific cluster"""
cluster_name = event['queryStringParameters']['cluster_name']
try:
print(f'Deleting hyper-kube-config-pem-{cluster_name}')
SECRETS_CLIENT.delete_secret(
SecretId=f'hyper-kube-config-pem-{cluster_name}',
ForceDeleteWithoutRecovery=True
)
return {
"statusCode": 200,
"body": {"message":
f'Deleted hyper-kube-config-pem-{cluster_name}'},
}
except ClientError as e:
return {
"statusCode": 503,
"body": json.dumps(
{"message": (f'Failed to get pem associated '
f'to {cluster_name}: {e}')}
)
}