forked from cms-PdmV/mcm_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.py
320 lines (276 loc) · 11.8 KB
/
rest.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import sys
import os
import json
import httplib
import pycurl
import cStringIO
import traceback
import time
class McM:
def __init__(self, id='sso', debug=False, cookie=None, dev=True, int=False):
self.dev = dev
if self.dev:
self.server = 'cms-pdmv-dev.cern.ch'
elif int:
self.server = 'cms-pdmv-int.cern.ch'
else:
self.server = 'cms-pdmv.cern.ch'
if id == 'cert' or id == 'sso':
self.server += '/mcm/'
self.headers = {'USER_AGENT': 'McM Scripting'}
self.id = id
self.debug = debug
if cookie:
self.cookie_filename = cookie
else:
if self.dev:
self.cookie_filename = '%s/private/dev-cookie.txt' % (os.getenv('HOME'))
else:
self.cookie_filename = '%s/private/prod-cookie.txt' % (os.getenv('HOME'))
self.__connect()
def __connect(self):
if self.id == 'cert':
self.http_client = httplib.HTTPSConnection(self.server,
cert_file=os.getenv('X509_USER_PROXY'),
key_file=os.getenv('X509_USER_PROXY'))
elif self.id == 'sso':
if not os.path.isfile(self.cookie_filename):
print('The required sso cookie file is absent. Trying to make one for you')
self.__generate_cookie()
if not os.path.isfile(self.cookie_filename):
print('Unfortunately sso cookie file cannot be made automatically. Quitting...')
sys.exit(1)
else:
print('Found a cookie file at %s. Make sure it\'s not expired!' % (self.cookie_filename))
self.curl = pycurl.Curl()
print('Using sso-cookie file %s' % (self.cookie_filename))
self.curl.setopt(pycurl.COOKIEFILE, self.cookie_filename)
self.output = cStringIO.StringIO()
self.curl.setopt(pycurl.SSL_VERIFYPEER, 1)
self.curl.setopt(pycurl.SSL_VERIFYHOST, 2)
self.curl.setopt(pycurl.CAPATH, '/etc/pki/tls/certs')
self.curl.setopt(pycurl.WRITEFUNCTION, self.output.write)
else:
self.http_client = httplib.HTTPSConnection(self.server)
def __generate_cookie(self):
if self.debug:
print('cern-get-sso-cookie -u https://%s -o %s --krb' % (self.server, self.cookie_filename))
output = os.popen('cern-get-sso-cookie -u https://%s -o %s --krb' % (self.server, self.cookie_filename)).read()
if not os.path.isfile(self.cookie_filename) or self.debug:
print(output)
# Generic methods for GET, PUT, DELETE HTTP methods
def __get(self, url):
retries = 0
while retries < 3:
if self.id != 'sso' and self.id != 'cert':
url = '/mcm/' + url
fullurl = 'https://' + self.server + url
if self.debug:
print('GET |%s|' % (fullurl))
if self.id == 'sso':
self.curl.setopt(pycurl.URL, str(fullurl))
self.curl.setopt(pycurl.HTTPGET, 1)
self.curl.setopt(pycurl.CUSTOMREQUEST, 'GET')
self.curl.perform()
else:
self.http_client.request("GET", url, headers=self.headers)
retries += 1
try:
res = json.loads(self.__response())
return res
except ValueError as ve:
print('Most likely cookie is expired, will remake it for you after %s seconds' % (retries ** 3))
time.sleep(retries ** 3)
self.__generate_cookie()
self.__connect()
except Exception as ex:
print('Error while making a GET request to %s. Exception: %s' % (fullurl, ex))
print(traceback.format_exc())
return None
return None
def __put(self, url, data):
retries = 0
while retries < 3:
if self.id != 'sso' and self.id != 'cert':
url = '/mcm/' + url
fullurl = 'https://' + self.server + url
post_data = json.dumps(data)
if self.debug:
print('PUT |%s| DATA |%s|' % (fullurl, post_data))
if self.id == 'sso':
self.curl.setopt(pycurl.URL, str(fullurl))
self.curl.setopt(pycurl.CUSTOMREQUEST, "PUT")
self.curl.setopt(pycurl.HTTPHEADER, ["Content-Type: application/json"])
self.curl.setopt(pycurl.POSTFIELDS, post_data)
self.curl.perform()
else:
self.http_client.request("PUT", url, json.dumps(data), headers=self.headers)
retries += 1
try:
res = json.loads(self.__response())
return res
except ValueError as ve:
print('Most likely cookie is expired, will remake it for you after %s seconds' % (retries ** 3))
time.sleep(retries ** 3)
self.__generate_cookie()
self.__connect()
except Exception as ex:
print('Error while making a PUT request to %s. Exception: %s' % (fullurl, ex))
print(traceback.format_exc())
return None
def __post(self, url, data):
retries = 0
while retries < 3:
if self.id != 'sso' and self.id != 'cert':
url = '/mcm/' + url
fullurl = 'https://' + self.server + url
post_data = json.dumps(data)
if self.debug:
print('POST |%s| DATA |%s|' % (fullurl, post_data))
if self.id == 'sso':
self.curl.setopt(pycurl.URL, str(fullurl))
self.curl.setopt(pycurl.CUSTOMREQUEST, "POST")
self.curl.setopt(pycurl.HTTPHEADER, ["Content-Type: application/json"])
self.curl.setopt(pycurl.POSTFIELDS, post_data)
self.curl.perform()
else:
self.http_client.request("POST", url, json.dumps(data), headers=self.headers)
retries += 1
try:
res = json.loads(self.__response())
return res
except ValueError as ve:
print('Most likely cookie is expired, will remake it for you after %s seconds' % (retries ** 3))
time.sleep(retries ** 3)
self.__generate_cookie()
self.__connect()
except Exception as ex:
print('Error while making a POST request to %s. Exception: %s' % (fullurl, ex))
print(traceback.format_exc())
return None
def __delete(self, url):
if self.id != 'sso' and self.id != 'cert':
url = '/mcm/' + url
fullurl = 'https://' + self.server + url
if self.debug:
print('DELETE |%s|' % (fullurl))
if self.id == 'sso':
self.curl.setopt(pycurl.URL, str(fullurl))
self.curl.setopt(pycurl.CUSTOMREQUEST, 'DELETE')
self.curl.perform()
else:
self.http_client.request("DELETE", url, headers=self.headers)
try:
res = json.loads(self.__response())
return res
except Exception as ex:
print('Error while making a DELETE request to %s. Exception: %s' % (fullurl, ex))
print(traceback.format_exc())
return None
# Generic methods for I/O
def __clear(self):
if self.id == 'sso':
self.output = cStringIO.StringIO()
self.curl.setopt(pycurl.WRITEFUNCTION, self.output.write)
else:
self.http_client.close()
def __response(self):
if self.id == 'sso':
response = self.output.getvalue()
self.__clear()
return response
else:
response = self.http_client.getresponse().read()
self.__clear()
return response
# McM methods
def get(self, object_type, object_id=None, query='', method='get', page=-1):
"""
Get data from McM
object_type - [chained_campaigns, chained_requests, campaigns, requests, flows, etc.]
object_id - usually prep id of desired object
query - query to be run in order to receive an object, e.g. tags=M17p1A, multiple parameters can be used with & tags=M17p1A&pwg=HIG
method - action to be performed, such as get, migrate or inspect
page - which page to be fetched. -1 means no paginantion, return all results
"""
if object_id:
if self.debug:
print('Object ID provided, getting %s' % (object_id.strip()))
url = 'restapi/%s/%s/%s' % (object_type, method, object_id.strip())
res = self.__get(url)
if res:
return res['results']
else:
return None
else:
if page != -1:
if self.debug:
print('Fetching %s page of %s for query %s' % (page, object_type, query))
url = 'search/?db_name=%s&page=%d&%s' % (object_type, page, query)
res = self.__get(url)
if res:
return res["results"]
else:
return None
else:
if self.debug:
print('No page provided, will use pagination to build the response list')
res_page = [{}]
res = []
page = 0
while len(res_page) != 0:
res_page = self.get(object_type=object_type, query=query, method=method, page=page)
if res_page:
res += res_page
if self.debug:
print('Found %s %s in page %s. Total results: %s' % (len(res_page), object_type, page, len(res)))
page += 1
else:
break
if res:
return res
else:
return None
def update(self, object_type, object_data):
"""
Update data in McM
object_type - [chained_campaigns, chained_requests, campaigns, requests, flows, etc.]
object_data - new JSON of an object to be updated
"""
return self.put(object_type, object_data, method='update')
def put(self, object_type, object_data, method='save'):
"""
Put data into McM
object_type - [chained_campaigns, chained_requests, campaigns, requests, flows, etc.]
object_data - new JSON of an object to be updated
method - action to be performed, default is 'save'
"""
url = 'restapi/%s/%s' % (object_type, method)
res = self.__put(url, object_data)
return res
def approve(self, object_type, object_id, level=None):
if level is None:
url = 'restapi/%s/approve/%s' % (object_type, object_id)
else:
url = 'restapi/%s/approve/%s/%d' % (object_type, object_id, level)
return self.__get(url)
def clone_request(self, object_data):
return self.put('requests', object_data, method='clone')
def get_range_of_requests(self, query):
res = self.__put('restapi/requests/listwithfile', data={'contents': query})
if res:
return res["results"]
else:
return None
def delete(self, object_type, object_id):
url = 'restapi/%s/delete/%s' % (object_type, object_id)
self.__delete(url)
def forceflow(self, prepid):
"""
Forceflow a chained request with given prepid
"""
res = self.__get('restapi/chained_requests/flow/%s/force' % (prepid))
if res:
return res["results"]
else:
return None