Skip to content

Commit bbe2797

Browse files
committed
[OPENSTACK-2880] add timeout parameter 4 token auth
in case that the connection never ends e.g. when it calls requests.post() in get_new_token.
1 parent f0de23e commit bbe2797

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

icontrol/authtoken.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class iControlRESTTokenAuth(AuthBase):
6363
of ``login_provider_name``.
6464
"""
6565
def __init__(self, username, password, login_provider_name='tmos',
66-
verify=False, auth_provider=None):
66+
verify=False, auth_provider=None, timeout=30):
6767
self.username = username
6868
self.password = password
6969
self.login_provider_name = login_provider_name
@@ -74,6 +74,7 @@ def __init__(self, username, password, login_provider_name='tmos',
7474
self.auth_provider = auth_provider
7575
# We don't actually do auth at this point because we don't have a
7676
# hostname to authenticate to.
77+
self.timeout = timeout
7778

7879
def _check_token_validity(self):
7980
if not self.token:
@@ -95,7 +96,11 @@ def get_auth_providers(self, netloc):
9596
"""
9697
url = "https://%s/info/system?null" % (netloc)
9798

98-
response = requests.get(url, verify=self.verify)
99+
response = requests.get(
100+
url,
101+
verify=self.verify,
102+
timeout=self.timeout
103+
)
99104
if not response.ok or not hasattr(response, "json"):
100105
error_message = '%s Unexpected Error: %s for uri: %s\nText: %r' %\
101106
(response.status_code,
@@ -147,7 +152,8 @@ def get_new_token(self, netloc):
147152
login_url,
148153
json=login_body,
149154
verify=self.verify,
150-
auth=HTTPBasicAuth(self.username, self.password)
155+
auth=HTTPBasicAuth(self.username, self.password),
156+
timeout=self.timeout
151157
)
152158
self.attempts += 1
153159
if not response.ok or not hasattr(response, "json"):

icontrol/session.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,22 +413,24 @@ def __init__(self, username, password, **kwargs):
413413

414414
# Handle token-based auth.
415415
if token_to_use:
416-
self.session.auth = iControlRESTTokenAuth('admin', 'admin')
416+
self.session.auth = iControlRESTTokenAuth('admin', 'admin', timeout=timeout)
417417
self.session.auth.token = token_to_use
418418
else:
419419
if auth_provider:
420420
self.session.auth = iControlRESTTokenAuth(
421-
username, password, auth_provider=auth_provider, verify=verify
421+
username, password, auth_provider=auth_provider, verify=verify,
422+
timeout=timeout
422423
)
423424
else:
424425
if token_auth is True:
425426
self.session.auth = iControlRESTTokenAuth(
426-
username, password, verify=verify
427+
username, password, verify=verify, timeout=timeout
427428
)
428429
elif token_auth:
429430
# Truthy but not true: non-default loginAuthProvider
430431
self.session.auth = iControlRESTTokenAuth(
431-
username, password, token_auth, verify=verify
432+
username, password, token_auth, verify=verify,
433+
timeout=timeout
432434
)
433435
else:
434436
self.session.auth = (username, password)

0 commit comments

Comments
 (0)