forked from vectranetworks/vectraautomatedresponse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvar_config_helper.py
137 lines (127 loc) · 4.13 KB
/
var_config_helper.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
import ast
import os
import re
import shutil
import sys
from glob import glob
from pathlib import Path
import questionary
if not Path("config.py").is_file():
shutil.copy("config_template.py", "config.py")
file = "config.py"
# Grab all the variables in the config.py
confs = {}
with open(file, "r") as f:
for line in f:
if line[0] not in ["#", "\n"]:
val = line.strip().split(" = ")
if val[1] == "None":
val[1] = ""
confs[val[0]] = val[1]
# Create the list of third party options
# Display each variable in config.py for configuration
try:
for conf in confs:
if conf == "third_party_clients".upper():
choices = [
client
for client in os.listdir("third_party_clients")
if client
not in [
"__init__.py",
"__pycache__",
".DS_Store",
"README.md",
"third_party_interface.py",
]
]
clients = questionary.checkbox(
f"Choose {conf} (current: {confs[conf]}): ", choices=sorted(choices)
).unsafe_ask()
if clients != []:
confs[conf] = clients
# else:
# clients = ast.literal_eval(confs[conf])
elif conf == "COGNITO_URL":
more = True
urls = []
while more:
arg = questionary.text(
f"Configure {conf} (current: {confs[conf]}): "
).unsafe_ask()
if arg != "":
urls.append(arg)
confs[conf] = urls
more = questionary.confirm(
"Do you have additional Brains? ", default=False
).ask()
else:
if conf in ["LOG_FILE"] and confs["LOG_TO_FILE"] == "False":
continue
if (
conf
in ["SMTP_SERVER", "SMTP_PORT", "SRC_EMAIL", "DST_EMAIL", "SMTP_AUTH"]
and confs["SEND_EMAIL"] == "False"
):
continue
elif conf in ["SMTP_USER"] and confs["SMTP_AUTH"] == "False":
continue
if (
conf
in ["SYSLOG_SERVER", "SYSLOG_PORT", "SYSLOG_PROTO", "SYSLOG_FORMAT"]
and confs["SEND_SYSLOG"] == "False"
):
continue
arg = questionary.text(
f"Configure {conf} (current: {confs[conf]}): "
).unsafe_ask()
if arg != "":
confs[conf] = arg
except KeyboardInterrupt:
sys.exit()
with open(file, "r") as f:
data = f.readlines()
try:
with open(file, "w") as f:
for line in data:
variable = line.split(" = ")[0]
if variable in confs:
f.write(f"{variable} = {confs[variable]}\n")
else:
f.write(line)
except Exception:
pass
# Display variables from each third party client for configuration
for client in clients:
confs = {}
client = client.strip('"')
try:
file = glob(f"third_party_clients/{client}/*_config.py")
file = file[0]
except IndexError:
continue
# for conf in tpc_conf:
print(f"\nConfigure {client.upper()}:")
with open(file, "r") as f:
for line in f:
if line == "# STOP\n":
break
else:
val = line.strip().split(" = ")
if line[0] not in ["#", "\n"]:
arg = questionary.text(
f"Configure {val[0]} (current: {val[1]}): "
).unsafe_ask()
if arg != "":
confs[val[0]] = arg
else:
confs[val[0]] = val[1]
with open(file, "r") as f:
data = f.readlines()
with open(file, "w") as f:
for line in data:
variable = line.split(" = ")[0]
if variable in confs:
f.write(f"{variable} = {confs[variable]}\n")
else:
f.write(line)