Use a socket timeout in get_auth
A previous change added socket timeouts, but they weren't plumbed through to the get_auth code path. Make sure we're passing them along everywhere. Original-Author: Monty Taylor <mordred@inaugust.com> Change-Id: I398241898248e66d1f8c436c8ed2ec7a0e9387f6 Closes-bug: 1447847
This commit is contained in:
parent
ea6d2bf74d
commit
e2f41a6635
@ -140,8 +140,7 @@ def encode_meta_headers(headers):
|
|||||||
|
|
||||||
class HTTPConnection(object):
|
class HTTPConnection(object):
|
||||||
def __init__(self, url, proxy=None, cacert=None, insecure=False,
|
def __init__(self, url, proxy=None, cacert=None, insecure=False,
|
||||||
ssl_compression=False, default_user_agent=None,
|
ssl_compression=False, default_user_agent=None, timeout=None):
|
||||||
timeout=None):
|
|
||||||
"""
|
"""
|
||||||
Make an HTTPConnection or HTTPSConnection
|
Make an HTTPConnection or HTTPSConnection
|
||||||
|
|
||||||
@ -266,7 +265,9 @@ def http_connection(*arg, **kwarg):
|
|||||||
def get_auth_1_0(url, user, key, snet, **kwargs):
|
def get_auth_1_0(url, user, key, snet, **kwargs):
|
||||||
cacert = kwargs.get('cacert', None)
|
cacert = kwargs.get('cacert', None)
|
||||||
insecure = kwargs.get('insecure', False)
|
insecure = kwargs.get('insecure', False)
|
||||||
parsed, conn = http_connection(url, cacert=cacert, insecure=insecure)
|
timeout = kwargs.get('timeout', None)
|
||||||
|
parsed, conn = http_connection(url, cacert=cacert, insecure=insecure,
|
||||||
|
timeout=timeout)
|
||||||
method = 'GET'
|
method = 'GET'
|
||||||
conn.request(method, parsed.path, '',
|
conn.request(method, parsed.path, '',
|
||||||
{'X-Auth-User': user, 'X-Auth-Key': key})
|
{'X-Auth-User': user, 'X-Auth-Key': key})
|
||||||
@ -326,6 +327,7 @@ def get_auth_keystone(auth_url, user, key, os_options, **kwargs):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
insecure = kwargs.get('insecure', False)
|
insecure = kwargs.get('insecure', False)
|
||||||
|
timeout = kwargs.get('timeout', None)
|
||||||
auth_version = kwargs.get('auth_version', '2.0')
|
auth_version = kwargs.get('auth_version', '2.0')
|
||||||
debug = logger.isEnabledFor(logging.DEBUG) and True or False
|
debug = logger.isEnabledFor(logging.DEBUG) and True or False
|
||||||
|
|
||||||
@ -346,7 +348,7 @@ def get_auth_keystone(auth_url, user, key, os_options, **kwargs):
|
|||||||
project_domain_id=os_options.get('project_domain_id'),
|
project_domain_id=os_options.get('project_domain_id'),
|
||||||
debug=debug,
|
debug=debug,
|
||||||
cacert=kwargs.get('cacert'),
|
cacert=kwargs.get('cacert'),
|
||||||
auth_url=auth_url, insecure=insecure)
|
auth_url=auth_url, insecure=insecure, timeout=timeout)
|
||||||
except exceptions.Unauthorized:
|
except exceptions.Unauthorized:
|
||||||
msg = 'Unauthorized. Check username, password and tenant name/id.'
|
msg = 'Unauthorized. Check username, password and tenant name/id.'
|
||||||
if auth_version in AUTH_VERSIONS_V3:
|
if auth_version in AUTH_VERSIONS_V3:
|
||||||
@ -394,13 +396,15 @@ def get_auth(auth_url, user, key, **kwargs):
|
|||||||
storage_url, token = None, None
|
storage_url, token = None, None
|
||||||
cacert = kwargs.get('cacert', None)
|
cacert = kwargs.get('cacert', None)
|
||||||
insecure = kwargs.get('insecure', False)
|
insecure = kwargs.get('insecure', False)
|
||||||
|
timeout = kwargs.get('timeout', False)
|
||||||
if auth_version in AUTH_VERSIONS_V1:
|
if auth_version in AUTH_VERSIONS_V1:
|
||||||
storage_url, token = get_auth_1_0(auth_url,
|
storage_url, token = get_auth_1_0(auth_url,
|
||||||
user,
|
user,
|
||||||
key,
|
key,
|
||||||
kwargs.get('snet'),
|
kwargs.get('snet'),
|
||||||
cacert=cacert,
|
cacert=cacert,
|
||||||
insecure=insecure)
|
insecure=insecure,
|
||||||
|
timeout=timeout)
|
||||||
elif auth_version in AUTH_VERSIONS_V2 + AUTH_VERSIONS_V3:
|
elif auth_version in AUTH_VERSIONS_V2 + AUTH_VERSIONS_V3:
|
||||||
# We are handling a special use case here where the user argument
|
# We are handling a special use case here where the user argument
|
||||||
# specifies both the user name and tenant name in the form tenant:user
|
# specifies both the user name and tenant name in the form tenant:user
|
||||||
@ -423,6 +427,7 @@ def get_auth(auth_url, user, key, **kwargs):
|
|||||||
key, os_options,
|
key, os_options,
|
||||||
cacert=cacert,
|
cacert=cacert,
|
||||||
insecure=insecure,
|
insecure=insecure,
|
||||||
|
timeout=timeout,
|
||||||
auth_version=auth_version)
|
auth_version=auth_version)
|
||||||
else:
|
else:
|
||||||
raise ClientException('Unknown auth_version %s specified.'
|
raise ClientException('Unknown auth_version %s specified.'
|
||||||
@ -1190,6 +1195,7 @@ class Connection(object):
|
|||||||
raise an exception to the caller. Setting
|
raise an exception to the caller. Setting
|
||||||
this parameter to True will cause a retry
|
this parameter to True will cause a retry
|
||||||
after a backoff.
|
after a backoff.
|
||||||
|
:param timeout: The connect timeout for the HTTP connection.
|
||||||
"""
|
"""
|
||||||
self.authurl = authurl
|
self.authurl = authurl
|
||||||
self.user = user
|
self.user = user
|
||||||
@ -1231,7 +1237,8 @@ class Connection(object):
|
|||||||
auth_version=self.auth_version,
|
auth_version=self.auth_version,
|
||||||
os_options=self.os_options,
|
os_options=self.os_options,
|
||||||
cacert=self.cacert,
|
cacert=self.cacert,
|
||||||
insecure=self.insecure)
|
insecure=self.insecure,
|
||||||
|
timeout=self.timeout)
|
||||||
|
|
||||||
def http_connection(self, url=None):
|
def http_connection(self, url=None):
|
||||||
return http_connection(url if url else self.url,
|
return http_connection(url if url else self.url,
|
||||||
|
@ -108,18 +108,28 @@ class MockHttpResponse(object):
|
|||||||
self.headers.update(headers)
|
self.headers.update(headers)
|
||||||
|
|
||||||
class Raw(object):
|
class Raw(object):
|
||||||
def read(self):
|
def __init__(self, headers):
|
||||||
pass
|
self.headers = headers
|
||||||
self.raw = Raw()
|
|
||||||
|
def read(self, **kw):
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def getheader(self, name, default):
|
||||||
|
return self.headers.get(name, default)
|
||||||
|
|
||||||
|
self.raw = Raw(headers)
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
pass
|
||||||
|
|
||||||
def getheader(self, name, default):
|
def getheader(self, name, default):
|
||||||
return self.headers.get(name, default)
|
return self.headers.get(name, default)
|
||||||
|
|
||||||
def getheaders(self):
|
def getheaders(self):
|
||||||
return {"key1": "value1", "key2": "value2"}
|
return dict(self.headers)
|
||||||
|
|
||||||
def fake_response(self):
|
def fake_response(self):
|
||||||
return self
|
return self
|
||||||
@ -1404,6 +1414,45 @@ class TestConnection(MockHttpTest):
|
|||||||
('HEAD', '/v1/AUTH_pre_url', '', {'x-auth-token': 'post_token'}),
|
('HEAD', '/v1/AUTH_pre_url', '', {'x-auth-token': 'post_token'}),
|
||||||
])
|
])
|
||||||
|
|
||||||
|
def test_timeout_passed_down(self):
|
||||||
|
# We want to avoid mocking http_connection(), and most especially
|
||||||
|
# avoid passing it down in argument. However, we cannot simply
|
||||||
|
# instantiate C=Connection(), then shim C.http_conn. Doing so would
|
||||||
|
# avoid some of the code under test (where _retry() invokes
|
||||||
|
# http_connection()), and would miss get_auth() completely.
|
||||||
|
# So, with regret, we do mock http_connection(), but with a very
|
||||||
|
# light shim that swaps out _request() as originally intended.
|
||||||
|
|
||||||
|
orig_http_connection = c.http_connection
|
||||||
|
|
||||||
|
timeouts = []
|
||||||
|
|
||||||
|
def my_request_handler(*a, **kw):
|
||||||
|
if 'timeout' in kw:
|
||||||
|
timeouts.append(kw['timeout'])
|
||||||
|
else:
|
||||||
|
timeouts.append(None)
|
||||||
|
return MockHttpResponse(
|
||||||
|
status=200,
|
||||||
|
headers={
|
||||||
|
'x-auth-token': 'a_token',
|
||||||
|
'x-storage-url': 'http://files.example.com/v1/AUTH_user'})
|
||||||
|
|
||||||
|
def shim_connection(*a, **kw):
|
||||||
|
url, conn = orig_http_connection(*a, **kw)
|
||||||
|
conn._request = my_request_handler
|
||||||
|
return url, conn
|
||||||
|
|
||||||
|
conn = c.Connection(
|
||||||
|
'http://auth.example.com', 'user', 'password', timeout=33.0)
|
||||||
|
with mock.patch.multiple('swiftclient.client',
|
||||||
|
http_connection=shim_connection,
|
||||||
|
sleep=mock.DEFAULT):
|
||||||
|
conn.head_account()
|
||||||
|
|
||||||
|
# 1 call is through get_auth, 1 call is HEAD for account
|
||||||
|
self.assertEqual(timeouts, [33.0, 33.0])
|
||||||
|
|
||||||
def test_reset_stream(self):
|
def test_reset_stream(self):
|
||||||
|
|
||||||
class LocalContents(object):
|
class LocalContents(object):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user