-
Notifications
You must be signed in to change notification settings - Fork 1
/
vault-switcher.py
executable file
·277 lines (228 loc) · 8.24 KB
/
vault-switcher.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import os
import sys
import hvac
import json
import logging
import argparse
def read_vault_token_from_file(token_file_path):
with open(token_file_path, "r") as file:
data = file.readline().rstrip()
return data
def check_secret_exists(client, mount_point, path):
try:
client.secrets.kv.v2.read_secret_version(
mount_point=mount_point,
path=path,
)
log.info("Secret %s/%s exists", mount_point, path)
return True
except hvac.exceptions.InvalidPath:
log.info("Secret %s/%s doesn't exists", mount_point, path)
return False
def clone_secret(client, mount_point, source_secret, dest_secret):
read_response = client.secrets.kv.read_secret_version(
mount_point=mount_point, path=source_secret
)
client.secrets.kv.v2.create_or_update_secret(
mount_point=mount_point,
path=dest_secret,
secret=dict(read_response["data"]["data"]),
cas=0,
)
log.info(
"Secret %s/%s copied with the name %s/%s",
mount_point,
source_secret,
mount_point,
dest_secret,
)
def delete_secret(client, mount_point, secret_to_delete):
client.secrets.kv.v2.delete_metadata_and_all_versions(
mount_point=mount_point, path=secret_to_delete
)
log.info("Secret %s/%s deleted", mount_point, secret_to_delete)
def clone_variables_list(
client, mount_point, source_secret, dest_secret, variables_list
):
read_response = client.secrets.kv.read_secret_version(
mount_point=mount_point, path=source_secret
)
if variables_list:
log.info(
"The following variables will be copied: %s",
format(", ".join(map(str, variables_list))),
)
else:
log.info("Variables not specified")
read_response = client.secrets.kv.read_secret_version(
mount_point=mount_point,
path=source_secret,
)
variables_to_add = {}
for secret in variables_list:
if secret in read_response["data"]["data"].keys():
log.info(
"Pre-adding a variable %s to the new secret %s/%s",
secret,
mount_point,
dest_secret,
)
variables_to_add[secret] = read_response["data"]["data"].get(
secret
)
else:
log.info(
"There is no predefined variable %s in %s/%s. Skip copy",
secret,
mount_point,
source_secret,
)
if variables_to_add:
for secret in variables_to_add:
log.info(
"Add variable %s to the new secret %s/%s",
secret,
mount_point,
dest_secret,
)
client.secrets.kv.v2.create_or_update_secret(
mount_point=mount_point,
path=dest_secret,
secret=dict(variables_to_add),
)
if __name__ == "__main__":
# Set logging params
LOGFORMAT = '{"timestamp": "%(asctime)s", "level": "%(levelname)s", "message": "%(message)s"}' # noqa
logging.basicConfig(
stream=sys.stdout,
level=logging.INFO,
format=LOGFORMAT,
)
log = logging.getLogger()
# Define environment variables
VAULT_ADDR = os.getenv("VAULT_ADDR")
VAULT_TOKEN_FILE = os.getenv("VAULT_TOKEN_FILE")
VAULT_TOKEN = os.getenv("VAULT_TOKEN")
VAULT_MOUNT_POINT = os.getenv("VAULT_MOUNT_POINT")
VAULT_SOURCE_SECRET = os.getenv("VAULT_SOURCE_SECRET")
VAULT_DEST_SECRET = os.getenv("VAULT_DEST_SECRET")
VAULT_SECRET_TO_DELETE = os.getenv("VAULT_SECRET_TO_DELETE")
VARIABLES_LIST = json.loads(os.getenv("VARIABLES_LIST", "[]"))
# Checking variables used to connect to HashiCorp Vault
if VAULT_TOKEN is None and VAULT_TOKEN_FILE is None:
log.error("Set VAULT_TOKEN of VAULT_TOKEN_FILE variables")
sys.exit()
if VAULT_ADDR is None:
log.error("Set VAULT_ADDR variable")
sys.exit()
# Using VAULT_TOKEN or read token from VAULT_TOKEN_FILE
# if VAULT_TOKEN variable is not set
if VAULT_TOKEN is not None:
pass
elif VAULT_TOKEN_FILE is not None:
VAULT_TOKEN = read_vault_token_from_file(VAULT_TOKEN_FILE)
# Connecting to Vault and check if authentication succeeded
client = hvac.Client(url=VAULT_ADDR, token=VAULT_TOKEN)
log.info("Authenticated in vault: %s", client.is_authenticated())
# Defining command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument(
"-a",
"--action",
required=True,
choices=["clone-secret", "clone-variables", "delete-secret"],
)
args = parser.parse_args()
# Doing some actions according to command-line arguments
if args.action == "clone-secret":
# Checking required environment variables
if VAULT_MOUNT_POINT is None:
log.error("Set VAULT_MOUNT_POINT variable")
sys.exit()
if VAULT_SOURCE_SECRET is None:
log.error("Set VAULT_SOURCE_SECRET variable")
sys.exit()
if VAULT_DEST_SECRET is None:
log.error("Set VAULT_DEST_SECRET variable")
sys.exit()
# Checking for a destination secret to avoid overwriting it
if check_secret_exists(client, VAULT_MOUNT_POINT, VAULT_DEST_SECRET):
log.info(
"The existing secret %s/%s will not be overwritten",
VAULT_MOUNT_POINT,
VAULT_DEST_SECRET,
)
sys.exit()
# Checking for a source secret
if not check_secret_exists(
client, VAULT_MOUNT_POINT, VAULT_SOURCE_SECRET
):
log.error(
"Make sure the path %s/%s to the source secret is correct",
VAULT_MOUNT_POINT,
VAULT_SOURCE_SECRET,
)
sys.exit()
# Cloning a secret if all checks passed
clone_secret(
client, VAULT_MOUNT_POINT, VAULT_SOURCE_SECRET, VAULT_DEST_SECRET
)
elif args.action == "clone-variables":
# Checking required environment variables
if VAULT_MOUNT_POINT is None:
log.error("Set VAULT_MOUNT_POINT variable")
sys.exit()
if VAULT_SOURCE_SECRET is None:
log.error("Set VAULT_SOURCE_SECRET variable")
sys.exit()
if VAULT_DEST_SECRET is None:
log.error("Set VAULT_DEST_SECRET variable")
sys.exit()
if VARIABLES_LIST is None:
log.error("Set VARIABLES_LIST variable")
sys.exit()
# Checking for a destination secret to avoid overwriting it
if check_secret_exists(client, VAULT_MOUNT_POINT, VAULT_DEST_SECRET):
log.info(
"The existing secret %s/%s will not be overwritten",
VAULT_MOUNT_POINT,
VAULT_DEST_SECRET,
)
sys.exit()
# Checking for a source secret
if not check_secret_exists(
client, VAULT_MOUNT_POINT, VAULT_SOURCE_SECRET
):
log.error(
"Make sure the path %s/%s to the source secret is correct",
VAULT_MOUNT_POINT,
VAULT_SOURCE_SECRET,
)
sys.exit()
# Cloning a variables list if all checks passed
clone_variables_list(
client,
VAULT_MOUNT_POINT,
VAULT_SOURCE_SECRET,
VAULT_DEST_SECRET,
VARIABLES_LIST,
)
elif args.action == "delete-secret":
# Checking required environment variables
if VAULT_MOUNT_POINT is None:
log.error("Set VAULT_MOUNT_POINT variable")
sys.exit()
if VAULT_SECRET_TO_DELETE is None:
log.error("Set VAULT_SECRET_TO_DELETE variable")
sys.exit()
# Checking for a secret to be deleted
if not check_secret_exists(
client, VAULT_MOUNT_POINT, VAULT_SECRET_TO_DELETE
):
log.error(
"Make sure the path %s/%s to the secret to be deleted is correct", # noqa
VAULT_MOUNT_POINT,
VAULT_SECRET_TO_DELETE,
)
sys.exit()
delete_secret(client, VAULT_MOUNT_POINT, VAULT_SECRET_TO_DELETE)