-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkuma_backuper.py
136 lines (116 loc) · 5.7 KB
/
kuma_backuper.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
import requests
import json
import sys
import random
from termcolor import colored
DEBUG = True
backup_url = 'https://kuma-addres:7223/api/v1/system/backup/'
resources_url = 'https://kuma-addres:7223/api/v1/resources/'
token = "your-token"
version = '0.1'
def kuma_backup():
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
}
response = requests.get(backup_url, headers=headers, verify=False)
if response.status_code == 200:
with open("backup.tar.gz", "wb") as file:
file.write(response.content)
print("Файл успешно сохранен.")
else:
print(f"Ошибка: {response.status_code}, {response.text}")
def generate_readme(answer):
ctr = 0
fctr = 0
with open("responce.json", "wt") as res:
with open("RULES-README.md", "wt") as file:
file.write("<caption style=\"text-align: center;\">KUMA's correlation rules knowledgebase</caption>\n\
<table border=\"1\">\n\
<tr name=\"HEADER\"> \n\
<td style=\"text-align: center;\">LID</td>\n\
<td style=\"text-align: center;\">Tenant</td>\n\
<td style=\"text-align: center;\">Tag</td>\n\
<td style=\"text-align: center;\">Rule name</td>\n\
<td style=\"text-align: center;\">Rule description</td>\n\
<td style=\"text-align: center;\">KUMA ID</td>\n\
<td style=\"text-align: center;\">Created by</td>\n\
</tr>\n")
while len(answer) > ctr:
buff = json.dumps(answer[ctr])
res.write(buff)
buff = json.loads(buff)
name = buff.get("kind")
if name == "correlationRule":
file.write("\t<tr>\n")
file.write("\t\t<td>" + str(fctr) + "</td>\n")
file.write("\t\t<td>" + buff.get("tenantName") + "</td>\n")
file.write("\t\t<td>" + "-" + "</td>\n")
file.write("\t\t<td>" + buff.get("name") + "</td>\n")
file.write("\t\t<td>" + buff.get("description") + "</td>\n")
file.write("\t\t<td>" + buff.get("id") + "</td>\n")
file.write("\t\t<td>" + buff.get("userName") + "</td>\n")
file.write("\t</tr>\n")
fctr +=1
ctr +=1
file.write("</table>\n")
file.close
res.close
def kuma_rules_request():
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
"kind": "correlationRule",
}
try:
response = requests.get(resources_url, headers=headers, verify=False)
if response.status_code == 200:
answer = json.loads(response.text)
print(response.status_code)
return answer
else:
print(f"Ошибка: {response.status_code}, {response.text}")
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
print(colored("Ошибка! \n", "red"))
print(str(exc_value))
return 0
def print_help():
print("-h: To print help\n\
-r: To make rules dump int to README.md\n\
-b: To make backup in 'backup.tar.gz'")
def print_logo():
color = ['red','green', 'yellow', 'blue', 'magenta', 'cyan']
print(colored("""
__ __ __ __
| \ / \ | \ | \
| $$ / $$ __ __ ______ ____ ______ | $$____ ______ _______ | $$ __ __ __ ______ ______ ______
| $$/ $$ | \ | \| \ \ | \ | $$ \ | \ / \| $$ / \| \ | \ / \ / \ / \
| $$ $$ | $$ | $$| $$$$$$\$$$$\ \$$$$$$\ | $$$$$$$\ \$$$$$$\| $$$$$$$| $$_/ $$| $$ | $$| $$$$$$\| $$$$$$\| $$$$$$\\
| $$$$$\ | $$ | $$| $$ | $$ | $$ / $$ | $$ | $$ / $$| $$ | $$ $$ | $$ | $$| $$ | $$| $$ $$| $$ \$$\\
| $$ \$$\ | $$__/ $$| $$ | $$ | $$| $$$$$$$ | $$__/ $$| $$$$$$$| $$_____ | $$$$$$\ | $$__/ $$| $$__/ $$| $$$$$$$$| $$
| $$ \$$\ \$$ $$| $$ | $$ | $$ \$$ $$ | $$ $$ \$$ $$ \$$ \| $$ \$$\ \$$ $$| $$ $$ \$$ \| $$
\$$ \$$\ \$$$$$$ \$$ \$$ \$$ \$$$$$$$ \$$$$$$$ \$$$$$$$ \$$$$$$$ \$$ \$$ \$$$$$$ | $$$$$$$ \$$$$$$$ \$$
| $$
| $$
\$$
""", color[random.randint(0, len(color)-1)]))
print(colored("Kuma backup automatization! \t Now we can backup only Rules and Kuma's data!", 'green', attrs=['bold']))
print (colored('version:' + version, attrs=['bold']))
def main():
print_logo()
if len(sys.argv) < 2:
print(colored("Less Args! \nUse -h to help!", 'red', attrs=['bold']))
else:
if sys.argv[1] == '-h':
print_help()
elif sys.argv[1] == '-r':
responce = kuma_rules_request()
if responce != 0:
generate_readme(responce)
elif sys.argv[1] == '-b':
kuma_backup()
else:
print(colored("Wrong Args! \nUse -h to help!", 'red', attrs=['bold']))
if __name__ == '__main__':
main()