18
18
import computer_group
19
19
import ip_list
20
20
import policy
21
+ import soap_https_handler
21
22
22
23
class Manager (object ):
23
24
"""
24
25
Class representing the Deep Security Manager and all of it's
25
26
functionality. Well, at least the functionality available via the
26
27
SOAP and REST APIs
27
28
"""
28
- def __init__ (self , username = None , password = None , tenant = None , dsm_hostname = None , start_session = True ):
29
+ def __init__ (self , username = None , password = None , tenant = 'Primary' , dsm_hostname = None , dsm_port = 443 , start_session = True , ignore_ssl_validation = False , debug = False ):
29
30
"""
30
31
Create a new reference to a Deep Security Manager
31
32
@@ -34,16 +35,18 @@ def __init__(self, username=None, password=None, tenant=None, dsm_hostname=None,
34
35
tenant = In a multi-tenant deployment (like Deep Security as a Service) this is the tenant/account name.
35
36
For non-multi tenant accounts this can be left blank or set to "primary"
36
37
dsm_hostname = The hostname of the Deep Security Manager to access, defaults to Deep Security as a Service
38
+ dsm_port = The port of the Deep Security Manager to access, defaults to Deep Security as a Service
37
39
start_session = Whether or not to automatically start a session with the specified Deep Security Manager
38
40
"""
39
41
self .version = '9.6'
40
42
self ._hostname = 'app.deepsecurity.trendmicro.com' if not dsm_hostname else dsm_hostname # default to Deep Security as a Service
41
- self ._port = 443 # on-premise defaults to 4119
43
+ self ._port = dsm_port # on-premise defaults to 4119
42
44
self .rest_api_path = 'rest'
43
45
self .soap_api_wsdl = 'webservice/Manager?WSDL'
44
46
self .session_id_rest = None
45
47
self .session_id_soap = None
46
48
self .soap_client = None
49
+ self .ignore_ssl_validation = ignore_ssl_validation
47
50
48
51
# Deep Security data
49
52
self .computer_groups = {}
@@ -54,12 +57,13 @@ def __init__(self, username=None, password=None, tenant=None, dsm_hostname=None,
54
57
self .ip_lists = {}
55
58
56
59
# Setup functions
57
- self .debug = False
60
+ self ._debug = debug
58
61
self .logger = self ._setup_logging ()
59
62
self ._set_url ()
60
63
61
64
# Try to start a session if possible
62
65
if username and password and start_session :
66
+ self .log ("Attempting to start a session" )
63
67
self .start_session (username = username , password = password , tenant = tenant )
64
68
65
69
def __del__ (self ):
@@ -105,6 +109,17 @@ def port(self, val):
105
109
self ._port = val
106
110
self ._set_url ()
107
111
112
+ # Any change to debug requires that logging be reset
113
+ @property
114
+ def debug (self ): return self ._debug
115
+
116
+ @debug .setter
117
+ def debug (self , val ):
118
+ """
119
+ Reset the logging configuration on change
120
+ """
121
+ self ._setup_logging ()
122
+
108
123
# *****************************************************************
109
124
# 'Private' methods
110
125
# *****************************************************************
@@ -115,16 +130,18 @@ def _setup_logging(self):
115
130
116
131
# Based on tips from http://www.blog.pythonlibrary.org/2012/08/02/python-101-an-intro-to-logging/
117
132
logging .basicConfig (level = logging .ERROR )
133
+ if self ._debug :
134
+ logging .basicConfig (level = logging .DEBUG )
118
135
119
136
# turn down suds logging
120
137
logging .getLogger ('suds.client' ).setLevel (logging .ERROR )
121
- if self .debug :
138
+ if self ._debug :
122
139
logging .getLogger ('suds.client' ).setLevel (logging .DEBUG )
123
140
124
141
# setup module logging
125
142
logger = logging .getLogger ("DeepSecurity.API" )
126
143
logger .setLevel (logging .WARNING )
127
- if self .debug :
144
+ if self ._debug :
128
145
logger .setLevel (logging .DEBUG )
129
146
130
147
formatter = logging .Formatter ('[%(asctime)s]\t %(message)s' , '%Y-%m-%d %H:%M:%S' )
@@ -159,25 +176,12 @@ def _get_soap_client(self, force_load_from_url=False):
159
176
"""
160
177
soap_client = None
161
178
162
- # First, try to use a local WSDL
163
- wsdl_path = None
164
- current_path = os .path .realpath (os .path .dirname (inspect .getfile (inspect .currentframe ())))
165
- found_wsdl = []
166
- for fn in os .listdir (current_path ):
167
- if fn .endswith ('.wsdl.xml' ): found_wsdl .append (os .path .join (current_path , fn ))
168
-
169
- found_wsdl .sort ()
170
-
171
- if len (found_wsdl ) > 0 :
172
- if 'deepsecurity.latest.wsdl.xml' in found_wsdl :
173
- wsdl_path = 'file://deepsecurity.latest.wsdl.xml'
174
- else :
175
- wsdl_path = 'file://{}' .format (found_wsdl [0 ])
176
-
177
- if not wsdl_path or force_load_from_url : wsdl_path = self .base_url_for_soap
178
-
179
179
try :
180
- soap_client = suds .client .Client (wsdl_path )
180
+ if self .ignore_ssl_validation :
181
+ self .log ("Ignoring SSL validation for SOAP API access" )
182
+ soap_client = suds .client .Client (self .base_url_for_soap , transport = soap_https_handler .HTTPSIgnoreValidation ())
183
+ else :
184
+ soap_client = suds .client .Client (self .base_url_for_soap )
181
185
except Exception , soap_err :
182
186
self .log ("Could not create a SOAP client. Threw exception: %s" % soap_err )
183
187
soap_client = None
@@ -362,7 +366,7 @@ def start_session(self, username=None, password=None, tenant=None, force_new_ses
362
366
# We need to make different calls for tenants and the primary
363
367
soap_call = None
364
368
rest_call = None
365
- if not tenant :
369
+ if not tenant or tenant . lower () == "primary" :
366
370
soap_call = self ._get_call_structure ()
367
371
soap_call ['auth' ] = False
368
372
soap_call ['method' ] = 'authenticate'
@@ -414,7 +418,10 @@ def start_session(self, username=None, password=None, tenant=None, force_new_ses
414
418
415
419
# Do we have an existing REST session?
416
420
if not self .session_id_rest or force_new_session :
417
- if rest_call : self .session_id_rest = (self ._make_call (rest_call )).text
421
+ if rest_call :
422
+ response = self ._make_call (rest_call )
423
+ if response :
424
+ self .session_id_rest = response .text
418
425
419
426
if self .session_id_rest :
420
427
self .log ("Authenticated successfully, starting REST session [%s]" % self .session_id_rest )
0 commit comments