Add warning when credential is not specified for each host

User may not set username/password for each host in config
option transport_url. All hosts should have username/password,
or no host have username/password at the same time.
This commit adds warning if user doesn't set properly.

Closes-Bug: #1595965
Change-Id: I4e204ce8365274566064168a2ba02c3278579717
This commit is contained in:
ChangBo Guo(gcb) 2016-08-03 16:08:57 +08:00
parent 4eef58c5d8
commit 34122ee3cb
2 changed files with 28 additions and 2 deletions

View File

@ -95,6 +95,14 @@ class TestParseURL(test_utils.BaseTestCase):
username='u', password='p'),
dict(host='host2', port=4321),
]))),
('multi_host_partial_creds',
dict(url='foo://u:p@host1,host2/bar', aliases=None,
expect=dict(transport='foo',
virtual_host='bar',
hosts=[
dict(host='host1', username='u', password='p'),
dict(host='host2'),
]))),
('multi_creds',
dict(url='foo://u1:p1@host1:1234,u2:p2@host2:4321/bar', aliases=None,
expect=dict(transport='foo',

View File

@ -34,6 +34,7 @@ import six
from six.moves.urllib import parse
from stevedore import driver
from oslo_messaging._i18n import _LW
from oslo_messaging import exceptions
LOG = logging.getLogger(__name__)
@ -372,8 +373,9 @@ class TransportURL(object):
Netloc is parsed following the sequence bellow:
* It is first split by ',' in order to support multiple hosts
* Username and password should be specified for each host, in
case of lack of specification they will be omitted::
* All hosts should be specified with username/password or not
at the same time. In case of lack of specification, username and
password will be omitted::
user:pass@host1:port1,host2:port2
@ -416,6 +418,8 @@ class TransportURL(object):
if url.path.startswith('/'):
virtual_host = parse.unquote(url.path[1:])
hosts_with_credentials = []
hosts_without_credentials = []
hosts = []
for host in url.netloc.split(','):
@ -454,9 +458,23 @@ class TransportURL(object):
hostname, port = hostname.split(':', 1)
port = int(port)
if username is None or password is None:
hosts_without_credentials.append(hostname)
else:
hosts_with_credentials.append(hostname)
hosts.append(TransportHost(hostname=hostname,
port=port,
username=username,
password=password))
if (len(hosts_with_credentials) > 0 and
len(hosts_without_credentials) > 0):
LOG.warning(_LW("All hosts must be set with username/password or "
"not at the same time. Hosts with credentials "
"are: %(hosts_with_credentials)s. Hosts without "
"credentials are %(hosts_without_credentials)s."),
{'hosts_with_credentials': hosts_with_credentials,
'hosts_without_credentials':
hosts_without_credentials})
return cls(conf, transport, virtual_host, hosts, aliases, query)