-
Notifications
You must be signed in to change notification settings - Fork 3
/
hide_secrets.py
executable file
·59 lines (50 loc) · 2 KB
/
hide_secrets.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
#!/usr/bin/env python
import sys
import re
"""Remove secrets from Python configuration file.
Simple text replace for secret values.
Only one secret per line. Secrets can be variable assignments or
dictionary values. Examples:
admin_password = 'secret password'
'non secret': 'normal value', 'admin_password': 'secret value'},
<targets> contains a list of configuration variables to change
"""
def hide_secrets(filename):
"""Remove secrets from Python configuration files.
"""
targets = {'admin_pass': 'secretPassword',
'admindb_password': 'MYDB_admin PW',
'admins': "'list', 'of', 'adminitrator', 'SIDS'",
'backup_admin_mail': '[email protected]',
'contact': '[email protected]',
'owner': 'Full Name',
'container_host': 'Docker host',
'FQDN_host': 'Docker host FQDN',
'Container_ip': 'IP of Docker Server',
'MAIL_TO': '[email protected]'}
var_re = r'(^.*=\s*[\[\'"])(.*)([\]\'"].*)'
def replace_secrets(line, TARGET):
dict_re = r'(.*[\'"]?' + TARGET
dict_re += r'[\'"]?\s?:\s*[\'"])(\w[\w !@#$%^&\.\*=+]*)([\'"].*$)'
if '=' in line:
clean_line = re.sub(var_re, r'\1' + targets[TARGET] + r'\3', line)
return clean_line
if ':' in line:
clean_line = re.sub(dict_re, r'\1' + targets[TARGET] + r'\3', line)
return clean_line
with open(filename) as f:
for line in f:
for target in targets.keys():
if target in line:
line = replace_secrets(line, target)
break
print(line),
if __name__ == '__main__':
"""Input: secify filename on command line
output: written to standard out
"""
if len(sys.argv) != 2:
print('Must speicify a filename as argument. usage: %s [con.fig.py]' %
sys.argv[0])
sys.exit(1)
hide_secrets(sys.argv[1])