Skip to content

Commit

Permalink
Add NameError check as described in issue #284
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Smith committed Jan 30, 2017
1 parent d09a9a6 commit d92df5c
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions acitoolkit/acisession.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ def get_event_count(self, url):
:returns: Interger number of events in event queue
"""
self._process_event_q()
if url not in self._events: return 0
if url not in self._events:
return 0
return len(self._events[url])

def get_event(self, url):
Expand Down Expand Up @@ -470,7 +471,7 @@ def __init__(self, url, uid, pwd=None, cert_name=None, key=None, verify_ssl=Fals
raise CredentialsError("The URL or APIC address must be a string")
if not isinstance(uid, str):
raise CredentialsError("The user ID must be a string")
if (pwd == None or pwd == 'None') and not cert_name and not key:
if (pwd is None or pwd == 'None') and not cert_name and not key:
raise CredentialsError("An authentication method must be provided")
if pwd:
if not isinstance(pwd, str):
Expand Down Expand Up @@ -499,7 +500,7 @@ def __init__(self, url, uid, pwd=None, cert_name=None, key=None, verify_ssl=Fals
Please install it using "pip install pyopenssl"')

self.cert_auth = True
# Cert based auth does not support subscriptions :(
# Cert based auth does not support subscriptions :(
# there's an exception for appcenter_user relying on the requestAppToken api
if subscription_enabled and not self.appcenter_user:
logging.warning('Disabling subscription support as certificate authentication does not support it.')
Expand All @@ -509,14 +510,14 @@ def __init__(self, url, uid, pwd=None, cert_name=None, key=None, verify_ssl=Fals
if not verify_ssl:
try:
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
except AttributeError:
except (AttributeError, NameError):
pass
with open(self.key, 'r') as f:
key_text = f.read()
try:
self._x509Key = load_privatekey(FILETYPE_PEM, key_text)
except Exception:
raise TypeError ('Could not load private key file %s\
raise TypeError('Could not load private key file %s\
\nAre you sure you provided the private key? (Not the certificate)' % self.key)
else:
self.cert_auth = False
Expand Down Expand Up @@ -594,9 +595,9 @@ def _prep_x509_header(self, method, url, data=None):

signature = base64.b64encode(sign(self._x509Key, payload, 'sha256'))
cookie = {'APIC-Request-Signature': signature,
'APIC-Certificate-Algorithm': 'v1.0',
'APIC-Certificate-Fingerprint': 'fingerprint',
'APIC-Certificate-DN': cert_dn}
'APIC-Certificate-Algorithm': 'v1.0',
'APIC-Certificate-Fingerprint': 'fingerprint',
'APIC-Certificate-DN': cert_dn}

logging.debug('Authentication cookie %s' % cookie)
return cookie
Expand All @@ -609,14 +610,14 @@ def _send_login(self, timeout=None):
if not self.verify_ssl:
try:
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
except AttributeError:
except (AttributeError, NameError):
pass
self.session = requests.Session()
self._logged_in = False

if self.appcenter_user and self._subscription_enabled:
login_url = '/api/requestAppToken.json'
data = {'aaaAppToken':{'attributes':{'appName': self.cert_name}}}
data = {'aaaAppToken': {'attributes': {'appName': self.cert_name}}}
elif self.cert_auth:
logging.warning('Will not explicitly login because certificate based authentication is being used for this session.')
logging.warning('If permanently using cert auth, consider removing the call to login().')
Expand All @@ -625,7 +626,7 @@ def _send_login(self, timeout=None):
else:
login_url = '/api/aaaLogin.json'
data = {'aaaUser': {'attributes': {'name': self.uid,
'pwd': self.pwd}}}
'pwd': self.pwd}}}
ret = self.push_to_apic(login_url, data=data, timeout=timeout)
if not ret.ok:
logging.error('Could not relogin to APIC. Aborting login thread.')
Expand Down Expand Up @@ -774,8 +775,7 @@ def push_to_apic(self, url, data, timeout=None):
post_url = self.api + url
logging.debug('Posting url: %s data: %s', post_url, data)

if self.cert_auth and not \
(self.appcenter_user and self._subscription_enabled and self._logged_in):
if self.cert_auth and not (self.appcenter_user and self._subscription_enabled and self._logged_in):
data = json.dumps(data, sort_keys=True)
cookies = self._prep_x509_header('POST', url, data)
resp = self.session.post(post_url, data=data, verify=self.verify_ssl,
Expand All @@ -785,7 +785,7 @@ def push_to_apic(self, url, data, timeout=None):
resp.raise_for_status()
else:
resp = self.session.post(post_url, data=json.dumps(data, sort_keys=True), verify=self.verify_ssl,
timeout=timeout, proxies=self._proxies)
timeout=timeout, proxies=self._proxies)
if resp.status_code == 403:
logging.error(resp.text)
logging.error('Trying to login again....')
Expand All @@ -794,7 +794,7 @@ def push_to_apic(self, url, data, timeout=None):
logging.error('Trying post again...')
logging.debug(post_url)
resp = self.session.post(post_url, data=json.dumps(data, sort_keys=True), verify=self.verify_ssl,
timeout=timeout, proxies=self._proxies)
timeout=timeout, proxies=self._proxies)
logging.debug('Response: %s %s', resp, resp.text)
return resp

Expand Down

0 comments on commit d92df5c

Please sign in to comment.