-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.py
166 lines (138 loc) · 5 KB
/
upload.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from collections import OrderedDict
import requests
import urllib
from requests_toolbelt import MultipartEncoder
from bs4 import BeautifulSoup
class Icinga2BPUpload(object):
ICINGA2_UPLOAD_ENDPOINT='/icingaweb2/businessprocess/process/upload'
ICINGA2_CONFIG_ENDPOINT='/icingaweb2/businessprocess/process/config?config={}'
ICINGA2_LOGIN_ENDPOINT='/icingaweb2/authentication/login'
ICINGA2_ENDPOINT='/icingaweb2'
predefined_auth_form_data = {
'btn_submit': 'Login',
'formUID': 'form_login',
}
def __init__(self, url, username, password):
self.url = url
self.username = username
self.password = password
self.session = requests.Session()
self.auth_form_data = {
'username': self.username,
'password': self.password,
'CSRFToken': None,
}
self.auth_form_data.update(
self.predefined_auth_form_data)
self.headers={
'X-Icinga-Accept': 'text/html',
}
def do_post(self, endpoint, form_data):
return self.session.post(
urllib.parse.urljoin(self.url, endpoint),
headers=self.headers,
data=form_data
)
def do_get(self, endpoint):
return self.session.get(
urllib.parse.urljoin(self.url, endpoint)
)
def get_csrf_token(self, endpoint=None, name='CSRFToken'):
response = self.do_get(endpoint or self.ICINGA2_ENDPOINT)
if response.ok:
handler = BeautifulSoup(response.text, "html.parser")
return handler.find('input',
{'name': name}).get('value')
else:
raise CSTFTokenNotFound(
'Unable to find CSRFToken: {}:{} @ {}'.format(
response.status_code,
response.reason,
response.url
)
)
def set_csrf_token(self, token):
self.auth_form_data.update({
'CSRFToken': token
})
def set_x_requested_by(self):
self.headers.update({
'X-Requested-With': 'XMLHttpRequest'
})
def set_content_type(self, form_data):
multipart_form_data = MultipartEncoder(form_data,
boundary='WebKitFormBoundary')
self.headers.update({
'Content-Type': multipart_form_data.content_type
})
return multipart_form_data
def login(self):
self.set_csrf_token(self.get_csrf_token())
self.set_x_requested_by()
return self.do_post(
self.ICINGA2_LOGIN_ENDPOINT,
self.auth_form_data
)
def get_upload_status(self, headers):
icinga_notification = headers.get('X-Icinga-Notification')
if icinga_notification:
return (True, urllib.parse.unquote(icinga_notification))
return (False, str())
def get_delete_status(self, headers):
icinga_redirect = headers.get('X-Icinga-Redirect')
if icinga_redirect:
return (True, urllib.parse.unquote(icinga_redirect))
return (False, str())
def delete(self, name):
delete_csrf_token = self.get_csrf_token(
self.ICINGA2_CONFIG_ENDPOINT.format(name),
name='__FORM_CSRF'
)
form_data = OrderedDict([
('__FORM_NAME', 'IcingaModuleBusinessprocessFormsBpConfigForm'),
('__FORM_CSRF', delete_csrf_token),
('name', name),
('Delete', 'Delete'),
])
response = self.do_post(
self.ICINGA2_CONFIG_ENDPOINT.format(name),
form_data
)
return self.get_delete_status(response.headers)
def upload(self, name, source):
upload_csrf_token = self.get_csrf_token(
self.ICINGA2_UPLOAD_ENDPOINT,
name='__FORM_CSRF'
)
form_data = OrderedDict([
('__FORM_NAME', 'IcingaModuleBusinessprocessFormsBpUploadForm'),
('__FORM_CSRF', upload_csrf_token),
('name', name),
('source', source),
('Store', 'Store'),
])
form_data = self.set_content_type(form_data)
response = self.do_post(
self.ICINGA2_UPLOAD_ENDPOINT,
form_data.to_string()
)
return self.get_upload_status(response.headers)
def update(self, name, source):
try:
ok, result = self.delete(name)
if ok:
print('Conf deleted: {}'.format(result))
except Exception as exc:
print('Unable to delete: {}'.format(exc))
try:
ok, result = self.upload(name, source)
if ok:
print('Conf uploaded: {}'.format(result))
except Exception as exc:
print('Unable to upload: {}'.format(exc))
class CSTFTokenNotFound(Exception):
pass
if __name__ == '__main__':
handler = Icinga2BPUpload('http://some.icinga.url', 'username', 'password')
handler.login()
handler.update(config_name, open('some.conf','rb'))