-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault-init.py
executable file
·176 lines (144 loc) · 6.3 KB
/
vault-init.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
165
166
167
168
169
170
171
172
173
174
175
176
import logging
from pythonjsonlogger import jsonlogger
import os
import time
from kubernetes import client, config
import base64
import hvac
logging.getLogger().setLevel(logging.INFO)
format_str = '%(levelname)%(message)'
formatter = jsonlogger.JsonFormatter(format_str)
logHandler = logging.StreamHandler()
logHandler.setFormatter(formatter)
logger = logging.getLogger()
logger.addHandler(logHandler)
def initialize(vaultClient, shares, threshold):
result = vaultClient.sys.initialize(secret_shares=shares,secret_threshold=threshold)
logger.debug(result)
rootToken = result['root_token']
keys = result['keys']
return keys, rootToken
def getClusterInfo(vaultClient):
logger.debug('Getting cluster info ...')
status = vaultClient.sys.read_health_status(method='GET')
logger.debug(status)
clusterId = status['cluster_id']
clusterName = status['cluster_name']
return clusterId, clusterName
def unseal(vaultClient, keys):
unseal_response = vaultClient.sys.submit_unseal_keys(keys)
logger.debug(unseal_response)
def getKeysFromSecret(k8sApi, k8sNamespace, k8sSecretPrefix):
logger.debug("Getting unseal keys from kubernetes namespace {0}".format(k8sNamespace))
try:
secret = k8sApi.read_namespaced_secret(name=f'{k8sSecretPrefix}unseal',namespace=k8sNamespace)
except Exception as e:
logger.fatal(e)
exit(1)
logger.debug(secret.data)
logger.info('Unseal secret {0}unseal retrieved from kubernetes'.format(k8sSecretPrefix))
keys = []
for k,v in secret.data.items():
keys.append(base64.b64decode(v).decode('utf-8'))
logger.debug(keys)
return keys
def createSecret(k8sApi, k8sNamespace, secretName, secretData, clusterId, clusterName):
secret = {'data' : secretData,
'metadata' : {
'annotations': {
'vault-cluster-name' : clusterName,
'vault-cluster-id' : clusterId
},
'name' : secretName
}
}
logger.debug(secret)
try:
api_response = k8sApi.create_namespaced_secret(namespace=k8sNamespace,body=secret)
except Exception as e:
logger.fatal(e)
exit(1)
logger.debug(api_response)
def main():
logger.info("Starting vault-init ...")
vaultAddr = os.environ.get('VAULT_ADDR', 'http://127.0.0.1:8200')
logger.debug("VAULT_ADDR is {0}".format(vaultAddr))
checkInterval = os.environ.get('CHECK_INTERVAL', '10')
logger.debug("CHECK_INTERVAL is {0}".format(checkInterval))
try:
checkInterval = int(checkInterval)
except ValueError as e:
logger.fatal("CHECK_INTERNAL value needs to be an integer")
raise ValueError('CHECK_INTERNAL value needs to be an integer')
k8sNamespace = os.environ.get('K8S_NAMESPACE', 'default')
logger.debug("K8S_NAMESPACE is {0}".format(k8sNamespace))
k8sSecretPrefix = os.environ.get('K8S_SECRET_PREFIX', 'vault-')
logger.debug('K8S_SECRET_PREFIX is {0}'.format(k8sSecretPrefix))
# SERVICE_HOST_ENV_NAME = "KUBERNETES_SERVICE_HOST"
# SERVICE_PORT_ENV_NAME = "KUBERNETES_SERVICE_PORT"
# SERVICE_TOKEN_FILENAME = "/var/run/secrets/kubernetes.io/serviceaccount/token"
# SERVICE_CERT_FILENAME = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
config.load_incluster_config()
v1 = client.CoreV1Api()
logger.debug('Checking list capabilities of secrets in namespace {0}'.format(k8sNamespace))
try:
api_response = v1.list_namespaced_secret(namespace=k8sNamespace)
except client.rest.ApiException as e:
if e.status == 403:
logger.fatal('No permission to list secrets in the {0} namespace'.format(k8sNamespace))
logger.fatal(e.reason)
logger.fatal(e.body)
exit(403)
except Exception as e:
logger.fatal(e)
exit(1)
else:
logger.debug('List capabilities available for secrets')
keys = clusterId = clusterName = vaultInit = None
vaultClient = hvac.Client(url=vaultAddr)
if not vaultClient.sys.is_initialized():
logger.warning("Vault at {0} is not initialized".format(vaultAddr))
logger.debug("Initializing ...")
keys, rootToken = initialize(vaultClient,5,3)
logger.info("Vault at {0} is initialized".format(vaultAddr))
vaultInit = True
else:
logger.info("Vault at {0} is initialized".format(vaultAddr))
keys = getKeysFromSecret(v1, k8sNamespace, k8sSecretPrefix)
if vaultClient.sys.is_sealed():
logger.warning("Vault at {0} is sealed".format(vaultAddr))
logger.debug("Unsealing ...")
unseal(vaultClient, keys)
logger.info("Vault at {0} is unsealed".format(vaultAddr))
logger.debug("Getting cluster info ...")
clusterId, clusterName = getClusterInfo(vaultClient)
logger.info("Vault cluster name is {0} with id {1}".format(clusterName, clusterId))
# Write unseal keys and root token
if vaultInit == True:
logger.debug("Unseal keys returned by vault init, pushing to kubernetes ...")
secretName = f'{k8sSecretPrefix}unseal'
keysdict = {}
i = 1
for k in keys:
keysdict[f'key{i}'] = base64.b64encode(bytes(k, 'utf-8')).decode('utf-8')
i=i+1
logger.debug(keysdict)
createSecret(v1, k8sNamespace, secretName, keysdict, clusterId, clusterName)
logger.info("Unseal keys stored in kubernetes secret {0}".format(secretName))
logger.debug("Pushing root token secret to kubernetes ...")
secretName = f'{k8sSecretPrefix}root'
token = {'token' : base64.b64encode(bytes(rootToken, 'utf-8')).decode('utf-8')}
createSecret(v1, k8sNamespace, secretName, token, clusterId, clusterName)
logger.info("root token stored in kubernetes secret {0}".format(secretName))
while True:
if vaultClient.sys.is_sealed():
logger.warning("Vault at {0} is sealed".format(vaultAddr))
logger.debug("Unsealing ...")
unseal(vaultClient, keys)
logger.info("Vault at {0} is unsealed".format(vaultAddr))
else:
logger.info("Vault at {0} is still unsealed".format(vaultAddr))
logger.debug("Sleeping for {0} seconds".format(checkInterval))
time.sleep(checkInterval)
if __name__ == '__main__':
main()