-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.py
executable file
·56 lines (47 loc) · 1.37 KB
/
encode.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
#!/usr/bin/env python3
from pykeepass import PyKeePass
import getpass
import re
import subprocess
import argparse
import sys
import os
from pathlib import Path
def main(argv):
# open source file
f = open('./tmpl/cluster-secrets.yaml','r')
# ask for KeePass password
try:
password = getpass.getpass(prompt='Password: ', stream=None)
except Exception as error:
print('ERROR', error)
exit()
# load KeePass db
kp = PyKeePass(os.environ['HOME']+'/marx.kdbx', password)
# define regex: ${VAR_NAME}
pattern = re.compile('.*?\${(\w+)}.*?')
# set envs
for line in f:
# find in line
match = pattern.findall(line)
for g in match:
# lookup password
try:
entry = kp.find_entries(title=g, first=True)
print ('Setting: ', g)
os.environ[g] = entry.password
except Exception as error:
print('problem with placeholder ', g, error)
exit()
f.close()
print('templating')
myinput = open('./tmpl/cluster-secrets.yaml')
myoutput = open('./cluster/flux/vars/cluster-secrets.sops.yaml', 'w')
subprocess.run(["envsubst"], stdin=myinput, stdout=myoutput)
myinput.close()
myoutput.close()
print('encoding')
subprocess.run(["sops","--encrypt","--in-place", "./cluster/flux/vars/cluster-secrets.sops.yaml"])
print('finished succesfully')
if __name__ == "__main__":
main(sys.argv[1:])