/
edit_secret
executable file
·95 lines (83 loc) · 3.39 KB
/
edit_secret
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
#!/usr/bin/env python3
import argparse
import subprocess
import json
import os
from base64 import standard_b64decode, standard_b64encode
import shutil
# Pare arguments passed in via command line
parser = argparse.ArgumentParser(description='Process some options')
parser.add_argument('--namespace', '-n', dest="namespace", help='specify a Kubernetes namespace')
parser.add_argument('name', type=str, help='the secret name')
args = parser.parse_args()
# Set the important variables
namespace = args.namespace
secret_name = args.name
# Use kubectl, the Kubernetes CLI, to get the specified secret
result = subprocess.run(['kubectl', 'get', 'secret', '-n', namespace, secret_name, '-o', 'json'],
stdout=subprocess.PIPE)
# Load the JSON file, capture the data attribute we care about
res = json.loads(result.stdout.decode('utf-8'))
data = res.get("data", {})
keys = [key for key in data]
# Create the directory we plan to save the temp files to
os.makedirs(".__temp__/", exist_ok=True)
EXIT_EDIT = False
MAKE_CHANGES = True
# Keep looping until the user chooses "CANCEL" or "DONE"
while not EXIT_EDIT:
# Get user input. Show list of keys in the secret
i = input("Please choose what to edit: \n" +
"".join(["%d) %s\n" % (index + 1, key) for index, key in enumerate(keys)]) +
"%d) ADD\n" % (len(keys) + 1) +
"%d) CANCEL\n" % (len(keys) + 2) +
"%d) DONE\n" % (len(keys) + 3))
# Check that input is a digit
if not i.isdigit() or int(i) < 1:
print("Please enter a digit!\n")
continue
else:
i = int(i)
# Check for 'ADD' option
if i == (len(keys) + 1): # ADD
# Get the name of the new desired key from user input
name = input("Please enter the name of new file/literal: ")
# Append the new keyname to the keys array
keys.append(name)
filename = ".__temp__/__temp__%s" % name
subprocess.run(['touch', filename])
# Check for 'CANCEL' option
elif i == (len(keys) + 2): # CANCEL
EXIT_EDIT = True
MAKE_CHANGES = False
continue
# Check for 'DONE' option
elif i == (len(keys) + 3): # DONE
EXIT_EDIT = True
continue
# If not 'ADD', 'CANCEL', 'DONE', try to locate the key based on index
else:
try:
name = keys[i - 1] # SELECTION MADE
filename = ".__temp__/__temp__%s" % name
with open(filename, 'w') as f:
f.write(standard_b64decode(data.get(name, "")).decode("utf-8"))
# If the given key is not a valid index
except IndexError:
print("Please select a valid number!\n")
continue
a = subprocess.run(['vim', filename])
with open(filename, 'r') as f:
contents = f.read().rstrip()
if len(contents) == 0:
if name in res.get("data"):
del res["data"][name]
delIndex = keys.index(name)
keys.pop(delIndex)
else:
res["data"][name] = standard_b64encode(bytes(contents, 'utf-8')).decode("utf-8")
if MAKE_CHANGES:
with open(".__temp__/__temp__.json", "w") as w:
w.write(json.dumps(res))
subprocess.run(["kubectl", "apply", "-f", ".__temp__/__temp__.json"])
shutil.rmtree('.__temp__')