Skip to content
This repository was archived by the owner on Aug 12, 2019. It is now read-only.

Code that makes deep-security-py compatible with Python 2.6 #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ that are supported are the critical ones to integrating Deep Security into your

## Requirements

Python 2.7 or newer. The project only uses modules in the standard library.
Python 2.6 or newer. The project only uses modules in the standard library.

## Usage

Expand Down
14 changes: 7 additions & 7 deletions deepsecurity/computers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,25 @@ def get(self, detail_level='HIGH', computer_id=None, computer_group_id=None, pol
computer_obj = Computer(self.manager, computer, self.log)
if computer_obj:
self[computer_obj.id] = computer_obj
self.log("Added Computer {}".format(computer_obj.id), level='debug')
self.log("Added Computer {0}".format(computer_obj.id), level='debug')

try:
# add this computer to any appropriate groups on the Manager()
if 'computer_group_id' in dir(computer_obj) and computer_obj.computer_group_id:
if self.manager.computer_groups and self.manager.computer_groups.has_key(computer_obj.computer_group_id):
self.manager.computer_groups[computer_obj.computer_group_id].computers[computer_obj.id] = computer_obj
self.log("Added Computer {} to ComputerGroup {}".format(computer_obj.id, computer_obj.computer_group_id), level='debug')
self.log("Added Computer {0} to ComputerGroup {1}".format(computer_obj.id, computer_obj.computer_group_id), level='debug')
except Exception, hostGroupid_err:
self.log("Could not add Computer {} to ComputerGroup".format(computer_obj.id), err=hostGroupid_err)
self.log("Could not add Computer {0} to ComputerGroup".format(computer_obj.id), err=hostGroupid_err)

try:
# add this computer to any appropriate policies on the Manager()
if 'policy_id' in dir(computer_obj) and computer_obj.policy_id:
if self.manager.policies and self.manager.policies.has_key(computer_obj.policy_id):
self.manager.policies[computer_obj.policy_id].computers[computer_obj.id] = computer_obj
self.log("Added Computer {} to Policy {}".format(computer_obj.id, computer_obj.policy_id), level='debug')
self.log("Added Computer {0} to Policy {1}".format(computer_obj.id, computer_obj.policy_id), level='debug')
except Exception, securityProfileid_err:
self.log("Could not add Computer {} to Policy".format(computer_obj.id), err=securityProfileid_err)
self.log("Could not add Computer {0} to Policy".format(computer_obj.id), err=securityProfileid_err)

return len(self)

Expand Down Expand Up @@ -172,7 +172,7 @@ def get(self, name=None, group_id=None):
elif group_id:
call = self.manager._get_request_format(call='hostGroupRetrieve')
call['data'] = {
'id': '{}'.format(group_id)
'id': '{0}'.format(group_id)
}
else:
call = self.manager._get_request_format(call='hostGroupRetrieveAll')
Expand All @@ -185,7 +185,7 @@ def get(self, name=None, group_id=None):
computer_group_obj = ComputerGroup(self.manager, group, self.log)
if computer_group_obj:
self[computer_group_obj.id] = computer_group_obj
self.log("Added ComputerGroup {}".format(computer_group_obj.id), level='debug')
self.log("Added ComputerGroup {0}".format(computer_group_obj.id), level='debug')

return len(self)

Expand Down
98 changes: 51 additions & 47 deletions deepsecurity/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ssl
import urllib
import urllib2
import requests

# 3rd party libraries
import libs.xmltodict as xmltodict
Expand Down Expand Up @@ -47,7 +48,7 @@ def log_at_level(self, value):
self._set_logging()
else:
if not self._log_at_level:
self._log_at_level = logging.WARNING
self._log_at_level = logging.DEBUG
self._set_logging()

# *******************************************************************
Expand Down Expand Up @@ -129,16 +130,16 @@ def _request(self, request, auth_required=True):
'call'
]:
if not request.has_key(required_key) and request[required_key]:
self.log("All requests are required to have a key [{}] with a value".format(required_key), level='critical')
self.log("All requests are required to have a key [{0}] with a value".format(required_key), level='critical')
return None

url = None
if request['api'] == self.API_TYPE_REST:
url = "{}/{}".format(self._rest_api_endpoint, request['call'].lstrip('/'))
url = "{0}/{1}".format(self._rest_api_endpoint, request['call'].lstrip('/'))
else:
url = self._soap_api_endpoint

self.log("Making a request to {}".format(url), level='debug')
self.log("Making a request to {0}".format(url), level='debug')

# add the authentication parameters
if auth_required:
Expand All @@ -163,19 +164,10 @@ def _request(self, request, auth_required=True):
if v: qs[k] = v

url += '?%s' % urllib.urlencode(qs)
self.log("Added query string. Full URL is now {}".format(url), level='debug')
self.log("Added query string. Full URL is now {0}".format(url), level='debug')

self.log("URL to request is: {}".format(url))
self.log("URL to request is: {0}".format(url))

# Prep the SSL context
ssl_context = ssl.create_default_context()
if self.ignore_ssl_validation:
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
self.log("SSL certificate validation has been disabled for this call", level='warning')

# Prep the URL opener
url_opener = urllib2.build_opener(urllib2.HTTPSHandler(context=ssl_context))

# Prep the request
request_type = 'GET'
Expand All @@ -189,7 +181,7 @@ def _request(self, request, auth_required=True):

# some rest calls use a cookie to pass the sID
if request['api'] == self.API_TYPE_REST and request['use_cookie_auth']:
headers['Cookie'] = 'sID="{}"'.format(self._sessions[self.API_TYPE_REST])
headers['Cookie'] = 'sID="{0}"'.format(self._sessions[self.API_TYPE_REST])

if request['api'] == self.API_TYPE_REST and request['call'] in [
'apiVersion',
Expand All @@ -207,42 +199,54 @@ def _request(self, request, auth_required=True):
'content-type': 'application/soap+xml'
}
data = self._prep_data_for_soap(request['call'], request['data'])
url_request = urllib2.Request(url, data=data, headers=headers)
#url_request = urllib2.Request(url, data=data, headers=headers)
request_type = 'POST'
self.log("Making a SOAP request with headers {}".format(headers), level='debug')
self.log(" and data {}".format(data), level='debug')
self.log("Making a SOAP request with headers {0}".format(headers), level='debug')
self.log(" and data {0}".format(data), level='debug')
elif request['call'] == 'authentication/logout':
url_request = urllib2.Request(url, headers=headers)
setattr(url_request, 'get_method', lambda: 'DELETE') # make this request use the DELETE HTTP verb
request_type = 'DELETE'
self.log("Making a REST DELETE request with headers {}".format(headers), level='debug')
#url_request = urllib2.Request(url, headers=headers)
data = "";
#setattr(url_request, 'get_method', lambda: 'DELETE') # make this request use the DELETE HTTP verb
#request_type = 'DELETE'
self.log("Making a REST DELETE request with headers {0}".format(headers), level='debug')
elif request.has_key('data') and request['data']:
# POST
url_request = urllib2.Request(url, data=json.dumps(request['data']), headers=headers)
data = json.dumps(request['data']);
#url_request = urllib2.Request(url, data=json.dumps(request['data']), headers=headers)
request_type = 'POST'
self.log("Making a REST POST request with headers {}".format(headers), level='debug')
self.log(" and data {}".format(request['data']), level='debug')
self.log("Making a REST POST request with headers {0}".format(headers), level='debug')
self.log(" and data {0}".format(request['data']), level='debug')
else:
# GET
url_request = urllib2.Request(url, headers=headers)
self.log("Making a REST GET request with headers {}".format(headers), level='debug')
#url_request = urllib2.Request(url, headers=headers)
request_type = 'GET';
self.log("Making a REST GET request with headers {0}".format(headers), level='debug')

# Make the request
response = None
try:
response = url_opener.open(url_request)
if request_type == "POST":
response = requests.post(url, data = data, headers=headers, verify = False)

if request_type == "DELETE":
response = requests.delete(url, data=data, headers= headers, verify=False)

if request_type == "GET":
response = requests.get(url, data=data, headers = headers, verify = False)

#response = url_opener.open(url_request)
except Exception, url_err:
self.log("Failed to make {} {} call [{}]".format(request['api'].upper(), request_type, request['call'].lstrip('/')), err=url_err)
self.log("Failed to make {0} {1} call [{2}]".format(request['api'].upper(), request_type, request['call'].lstrip('/')), err=url_err)

# Convert the request from JSON
result = {
'status': response.getcode() if response else None,
'raw': response.read() if response else None,
'status': response.status_code if response else None,
'raw': response.text if response else None,
'headers': dict(response.headers) if response else dict(),
'data': None
}
bytes_of_data = len(result['raw']) if result['raw'] else 0
self.log("Call returned HTTP status {} and {} bytes of data".format(result['status'], bytes_of_data), level='debug')
self.log("Call returned HTTP status {0} and {1} bytes of data".format(result['status'], bytes_of_data), level='debug')

if response:
if request['api'] == self.API_TYPE_SOAP:
Expand All @@ -252,15 +256,15 @@ def _request(self, request, auth_required=True):
full_data = xmltodict.parse(result['raw'])
if full_data.has_key('soapenv:Envelope') and full_data['soapenv:Envelope'].has_key('soapenv:Body'):
result['data'] = full_data['soapenv:Envelope']['soapenv:Body']
if result['data'].has_key('{}Response'.format(request['call'])):
if result['data']['{}Response'.format(request['call'])].has_key('{}Return'.format(request['call'])):
result['data'] = result['data']['{}Response'.format(request['call'])]['{}Return'.format(request['call'])]
if result['data'].has_key('{0}Response'.format(request['call'])):
if result['data']['{0}Response'.format(request['call'])].has_key('{0}Return'.format(request['call'])):
result['data'] = result['data']['{0}Response'.format(request['call'])]['{0}Return'.format(request['call'])]
else:
result['data'] = result['data']['{}Response'.format(request['call'])]
result['data'] = result['data']['{0}Response'.format(request['call'])]
else:
result['data'] = full_data
except Exception, xmltodict_err:
self.log("Could not convert response from call {}".format(request['call']), err=xmltodict_err)
self.log("Could not convert response from call {0}".format(request['call']), err=xmltodict_err)
else:
# JSON response
try:
Expand All @@ -270,7 +274,7 @@ def _request(self, request, auth_required=True):
except Exception, json_err:
# report the exception as 'info' because it's not fatal and the data is
# still captured in result['raw']
self.log("Could not convert response from call {} to JSON. Threw exception:\n\t{}".format(request['call'], json_err), level='info')
self.log("Could not convert response from call {0} to JSON. Threw exception:\n\t{1}".format(request['call'], json_err), level='info')

return result

Expand All @@ -282,7 +286,7 @@ def _prefix_keys(self, prefix, d):
if not type(d) == type({}): return d
new_d = d.copy()
for k,v in d.items():
new_key = u"{}:{}".format(prefix, k)
new_key = u"{0}:{1}".format(prefix, k)
new_v = v
if type(v) == type({}): new_v = self._prefix_keys(prefix, v)
new_d[new_key] = new_v
Expand All @@ -300,7 +304,7 @@ def _prep_data_for_soap(self, call, details):
<SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Manager" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns0:Body>
{}
{0}
</ns0:Body>
</SOAP-ENV:Envelope>
""".format(data).strip()
Expand All @@ -325,13 +329,13 @@ def log(self, message='', err=None, level='info'):

if err:
level = 'error'
message += ' Threw exception:\n\t{}'.format(err)
message += ' Threw exception:\n\t{0}'.format(err)

try:
func = getattr(self.logger, level.lower())
func(message)
except Exception, log_err:
self.logger.critical("Could not write to log. Threw exception:\n\t{}".format(log_err))
self.logger.critical("Could not write to log. Threw exception:\n\t{0}".format(log_err))

class CoreDict(dict):
def __init__(self):
Expand Down Expand Up @@ -398,7 +402,7 @@ def find(self, **kwargs):
for match_attr_val in match_attr_vals:
if type(attr_to_check) in [type(''), type(u'')]:
# string comparison
match = re.search(r'{}'.format(match_attr_val), attr_to_check)
match = re.search(r'{0}'.format(match_attr_val), attr_to_check)
if match:
item_matches = True
break # and move on to the new kwarg
Expand Down Expand Up @@ -438,7 +442,7 @@ def _set_properties(self, api_response, log_func):
# make sure any integer IDs are stored as an int
if new_key == 'id' and re.search('^\d+$', v.strip()): val = int(v)
if new_key == 'policy_id':
if '@xsi:nil' in "{}".format(v):
if '@xsi:nil' in "{0}".format(v):
val = None
elif re.search('^\d+$', "".join(v.strip())):
val = int(v)
Expand All @@ -447,7 +451,7 @@ def _set_properties(self, api_response, log_func):
setattr(self, new_key, val)
except Exception, err:
if log_func:
log_func("Could not set property {} to value {} for object {}".format(k, v, s))
log_func("Could not set property {0} to value {1} for object {2}".format(k, v, s))
try:
setattr(self, log, log_func)
except: pass
Expand Down Expand Up @@ -532,7 +536,7 @@ def find(self, **kwargs):
for match_attr_val in match_attr_vals:
if type(attr_to_check) in [type(''), type(u'')]:
# string comparison
match = re.search(r'{}'.format(match_attr_val), attr_to_check)
match = re.search(r'{0}'.format(match_attr_val), attr_to_check)
if match:
item_matches = True
break # and move on to the new kwarg
Expand Down
18 changes: 9 additions & 9 deletions deepsecurity/dsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def __str__(self):
"""
Return a better string representation
"""
dsm_port = ":{}".format(self.port) if self.port else ""
return "Manager <{}{}>".format(self.hostname, dsm_port)
dsm_port = ":{0}".format(self.port) if self.port else ""
return "Manager <{0}{1}>".format(self.hostname, dsm_port)

# *******************************************************************
# properties
Expand Down Expand Up @@ -124,9 +124,9 @@ def _set_endpoints(self):
"""
Set the API endpoints based on the current configuration
"""
dsm_port = ":{}".format(self.port) if self.port else "" # allow for endpoints with no port specified
self._rest_api_endpoint = "https://{}{}/{}rest".format(self.hostname, dsm_port, self.prefix)
self._soap_api_endpoint = "https://{}{}/{}webservice/Manager".format(self.hostname, dsm_port, self.prefix)
dsm_port = ":{0}".format(self.port) if self.port else "" # allow for endpoints with no port specified
self._rest_api_endpoint = "https://{0}{1}/{2}rest".format(self.hostname, dsm_port, self.prefix)
self._soap_api_endpoint = "https://{0}{1}/{2}webservice/Manager".format(self.hostname, dsm_port, self.prefix)

def _reset_session(self):
"""
Expand All @@ -153,7 +153,7 @@ def _get_local_config_file(self):
"""
user_credentials_path = os.path.expanduser('~/.deepsecurity/credentials')
if os.path.exists(user_credentials_path):
self.log("Found local credentials file at [{}]".format(user_credentials_path))
self.log("Found local credentials file at [{0}]".format(user_credentials_path))
credentials = {
'username': None,
'password': None,
Expand All @@ -175,10 +175,10 @@ def _get_local_config_file(self):
if v:
if k in dir(self):
try:
setattr(self, "_{}".format(k), v)
self.log("Loaded {} from local credentials file".format(k))
setattr(self, "_{0}".format(k), v)
self.log("Loaded {0} from local credentials file".format(k))
except Exception, err:
self.log("Unable to load {} from local credentials file".format(k))
self.log("Unable to load {0} from local credentials file".format(k))

def sign_in(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion deepsecurity/environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def add_aws_account(self, name, aws_access_key=None, aws_secret_key=None, region
'accessKey': aws_access_key,
'secretKey': aws_secret_key,
'cloudType': 'AMAZON',
'name': '{} / {}'.format(name, region_to_add),
'name': '{0} / {1}'.format(name, region_to_add),
'cloudRegion': regions[region_to_add],
},
}
Expand Down
Loading