-
Notifications
You must be signed in to change notification settings - Fork 17
/
cpapi.py
280 lines (257 loc) · 11 KB
/
cpapi.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
#!/usr/bin/env python
import sys
import json
import urllib
import urllib2
import base64
import threading
import datetime
# Class with calls to CloudPassage API
class CPAPI:
def __init__(self):
self.auth_url = 'oauth/access_token'
self.auth_args = {'grant_type': 'client_credentials'}
self.base_url = 'https://api.cloudpassage.com'
self.api_ver = 'v1'
self.port = 443
self.key_id = None
self.secret = None
self.authToken = None
self.lock = threading.RLock()
self.api_count = 0
self.api_time = 0.0
# Dump debug info
def dumpToken(self, token, expires):
if (token):
print "AuthToken=%s" % token
if (expires):
print "Expires in %s minutes" % (expires / 60)
def getHttpStatus(self, code):
if (code == 200):
return "OK" # should never be passed in, only errors
elif (code == 401):
return "Unauthorized"
elif (code == 403):
return "Forbidden"
elif (code == 404):
return "Not found"
elif (code == 422):
return "Validation failed"
elif (code == 500):
return "Internal server error"
elif (code == 502):
return "Gateway error"
else:
return "Unknown code [%d]" % code
def addAuth(self, req, kid, sec):
combined = kid + ":" + sec
encoded = base64.b64encode(combined)
req.add_header("Authorization", "Basic " + encoded)
def getAuthToken(self, url, args, kid, sec):
req = urllib2.Request(url)
self.addAuth(req, kid, sec)
# print >> sys.stderr, "getAuthToken: key=%s secret=%s" % (kid, sec)
# createPasswordMgr(url, kid, sec)
if (args):
args = urllib.urlencode(args)
try:
fh = urllib2.urlopen(req, args)
return fh.read()
except IOError, e:
if hasattr(e, 'reason'):
print >> sys.stderr, "Failed to connect [%s] to '%s'" % (e.reason, url)
elif hasattr(e, 'code'):
msg = self.getHttpStatus(e.code)
print >> sys.stderr, "Failed to authorize [%s] at '%s'" % (msg, url)
data = e.read()
if data:
print >> sys.stderr, "Extra data: %s" % data
print >> sys.stderr, "Likely cause: incorrect API keys, id=%s" % kid
else:
print >> sys.stderr, "Unknown error fetching '%s'" % url
return None
def getInitialLink(self, fromDate, events_per_page):
url = "%s:%d/%s/events?per_page=%d" % (self.base_url, self.port, self.api_ver, events_per_page)
if (fromDate):
url += "&since=" + fromDate
return url
def getEventBatch(self, url):
return self.doGetRequest(url, self.authToken)
def logTime(self, start_time, end_time):
delta = end_time - start_time
with self.lock:
self.api_count += 1
self.api_time += delta.total_seconds()
def getTimeLog(self):
tuple = None
with self.lock:
tuple = (self.api_count, self.api_time)
return tuple
def doGetRequest(self, url, token):
req = urllib2.Request(url)
req.add_header("Authorization", "Bearer " + token)
try:
start_time = datetime.datetime.now()
fh = urllib2.urlopen(req)
data = fh.read()
contentType = fh.info().getheader('Content-type')
(mimetype, encoding) = contentType.split("charset=")
# print >> sys.stderr, "Type=%s Encoding=%s" % (mimetype, encoding)
translatedData = data.decode(encoding,'ignore').encode('utf-8')
results = (translatedData, False)
end_time = datetime.datetime.now()
self.logTime(start_time, end_time)
return results
except IOError, e:
authError = False
if hasattr(e, 'reason'):
print >> sys.stderr, "Failed to connect [%s] to '%s'" % (e.reason, url)
if (e.reason == "Unauthorized"):
authError = True
elif hasattr(e, 'code'):
msg = self.getHttpStatus(e.code)
print >> sys.stderr, "Failed to fetch events [%s] from '%s'" % (msg, url)
if (e.code == 401) or (e.code == 403):
authError = True
print >> sys.stderr, "Error response: %s" % e.read()
else:
print >> sys.stderr, "Unknown error fetching '%s'" % url
return (None, authError)
def doPutRequest(self, url, token, putData):
opener = urllib2.build_opener(urllib2.HTTPHandler)
req = urllib2.Request(url, data=putData)
req.add_header("Authorization", "Bearer " + token)
req.add_header("Content-Type", "application/json")
req.get_method = lambda: 'PUT'
try:
start_time = datetime.datetime.now()
fh = opener.open(req)
results = (fh.read(), False)
end_time = datetime.datetime.now()
self.logTime(start_time, end_time)
return results
except IOError, e:
authError = False
if hasattr(e, 'reason'):
print >> sys.stderr, "Failed to connect [%s] to '%s'" % (e.reason, url)
if hasattr(e, 'code'):
msg = self.getHttpStatus(e.code)
print >> sys.stderr, "Failed to make request: [%s] from '%s'" % (msg, url)
if (e.code == 401) or (e.code == 403):
authError = True
print >> sys.stderr, "Error response: %s" % e.read()
if (not hasattr(e, 'reason')) and (not hasattr(e, 'code')):
print >> sys.stderr, "Unknown error fetching '%s'" % url
return (None, authError)
def doPostRequest(self, url, token, putData):
opener = urllib2.build_opener(urllib2.HTTPHandler)
req = urllib2.Request(url, data=putData)
req.add_header("Authorization", "Bearer " + token)
req.add_header("Content-Type", "application/json")
try:
start_time = datetime.datetime.now()
fh = opener.open(req)
results = (fh.read(), False)
end_time = datetime.datetime.now()
self.logTime(start_time, end_time)
return results
except IOError, e:
authError = False
if hasattr(e, 'reason'):
print >> sys.stderr, "Failed to connect [%s] to '%s'" % (e.reason, url)
if hasattr(e, 'code'):
msg = self.getHttpStatus(e.code)
print >> sys.stderr, "Failed to make request: [%s] from '%s'" % (msg, url)
if (e.code == 401) or (e.code == 403):
authError = True
print >> sys.stderr, "Error response: %s" % e.read()
if (not hasattr(e, 'reason')) and (not hasattr(e, 'code')):
print >> sys.stderr, "Unknown error fetching '%s'" % url
return (None, authError)
def authenticateClient(self):
url = "%s:%d/%s" % (self.base_url, self.port, self.auth_url)
self.token = None
response = self.getAuthToken(url, self.auth_args, self.key_id, self.secret)
if (response):
authRespObj = json.loads(response)
if ('access_token' in authRespObj):
self.authToken = authRespObj['access_token']
if ('expires_in' in authRespObj):
self.expires = authRespObj['expires_in']
# dumpToken(token,expires)
return self.authToken
def getServerList(self):
url = "%s:%d/%s/servers" % (self.base_url, self.port, self.api_ver)
(data, authError) = self.doGetRequest(url, self.authToken)
if (data):
return (json.loads(data), authError)
else:
return (None, authError)
def getServerGroupList(self):
url = "%s:%d/%s/groups" % (self.base_url, self.port, self.api_ver)
(data, authError) = self.doGetRequest(url, self.authToken)
if (data):
return (json.loads(data), authError)
else:
return (None, authError)
def getServersInGroup(self,groupID):
url = "%s:%d/%s/groups/%s/servers" % (self.base_url, self.port, self.api_ver, groupID)
(data, authError) = self.doGetRequest(url, self.authToken)
if (data):
return (json.loads(data), authError)
else:
return (None, authError)
def getFirewallPolicyList(self):
url = "%s:%d/%s/firewall_policies/" % (self.base_url, self.port, self.api_ver)
(data, authError) = self.doGetRequest(url, self.authToken)
if (data):
return (json.loads(data), authError)
else:
return (None, authError)
def getFirewallPolicyDetails(self, policyID):
url = "%s:%d/%s/firewall_policies/%s" % (self.base_url, self.port, self.api_ver, policyID)
(data, authError) = self.doGetRequest(url, self.authToken)
if (data):
return (json.loads(data), authError)
else:
return (None, authError)
def moveServerToGroup(self, serverID, groupID):
url = "%s:%d/%s/servers/%s" % (self.base_url, self.port, self.api_ver, serverID)
reqData = {"server": {"group_id": groupID}}
jsonData = json.dumps(reqData)
# print "move: %s" % jsonData
(data, authError) = self.doPutRequest(url, self.authToken, jsonData)
if (data):
return (json.loads(data), authError)
else:
return (None, authError)
def createServerGroup(self, groupName, linuxFirewallPolicy, windowsFirewallPolicy):
url = "%s:%d/%s/groups" % (self.base_url, self.port, self.api_ver)
groupData = {"name": groupName, "policy_ids": [], "tag": None}
groupData["linux_firewall_policy_id"] = linuxFirewallPolicy
groupData["windows_firewall_policy_id"] = windowsFirewallPolicy
reqData = {"group": groupData}
jsonData = json.dumps(reqData)
(data, authError) = self.doPostRequest(url, self.authToken, jsonData)
if (data):
return (json.loads(data), authError)
else:
return (None, authError)
def createFirewallPolicy(self, policyData):
url = "%s:%d/%s/firewall_policies" % (self.base_url, self.port, self.api_ver)
jsonData = json.dumps(policyData)
# print jsonData # for debugging
(data, authError) = self.doPostRequest(url, self.authToken, jsonData)
if (data):
return (json.loads(data), authError)
else:
return (None, authError)
def assignFirewallPolicyToGroup(self, groupID, attrName, policyID):
url = "%s:%d/%s/groups/%s" % (self.base_url, self.port, self.api_ver, groupID)
reqData = {"group": { attrName: policyID}}
jsonData = json.dumps(reqData)
(data, authError) = self.doPutRequest(url, self.authToken, jsonData)
if (data):
return (json.loads(data), authError)
else:
return (None, authError)