-
Notifications
You must be signed in to change notification settings - Fork 155
/
transport.py
540 lines (414 loc) · 18.8 KB
/
transport.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
# Copyright 2013 Red Hat, Inc.
# Copyright (c) 2012 Rackspace Hosting
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
from debtcollector import removals
from oslo_config import cfg
from stevedore import driver
from urllib import parse
from oslo_messaging import exceptions
__all__ = [
'DriverLoadFailure',
'InvalidTransportURL',
'Transport',
'TransportHost',
'TransportURL',
'TransportOptions',
'get_transport',
'set_transport_defaults',
]
LOG = logging.getLogger(__name__)
_transport_opts = [
cfg.StrOpt('transport_url',
default="rabbit://",
secret=True,
help='The network address and optional user credentials for '
'connecting to the messaging backend, in URL format. The '
'expected format is:\n\n'
'driver://[user:pass@]host:port[,[userN:passN@]hostN:'
'portN]/virtual_host?query\n\n'
'Example: rabbit://rabbitmq:password@127.0.0.1:5672//\n\n'
'For full details on the fields in the URL see the '
'documentation of oslo_messaging.TransportURL at '
'https://docs.openstack.org/oslo.messaging/latest/'
'reference/transport.html'),
cfg.StrOpt('control_exchange',
default='openstack',
help='The default exchange under which topics are scoped. May '
'be overridden by an exchange name specified in the '
'transport_url option.'),
]
def set_transport_defaults(control_exchange):
"""Set defaults for messaging transport configuration options.
:param control_exchange: the default exchange under which topics are scoped
:type control_exchange: str
"""
cfg.set_defaults(_transport_opts,
control_exchange=control_exchange)
class Transport(object):
"""A messaging transport.
This is a mostly opaque handle for an underlying messaging transport
driver.
RPCs and Notifications may use separate messaging systems that utilize
different drivers, access permissions, message delivery, etc. To ensure
the correct messaging functionality, the corresponding method should be
used to construct a Transport object from transport configuration
gleaned from the user's configuration and, optionally, a transport URL.
The factory method for RPC Transport objects::
def get_rpc_transport(conf, url=None,
allowed_remote_exmods=None)
If a transport URL is supplied as a parameter, any transport configuration
contained in it takes precedence. If no transport URL is supplied, but
there is a transport URL supplied in the user's configuration then that
URL will take the place of the URL parameter.
The factory method for Notification Transport objects::
def get_notification_transport(conf, url=None,
allowed_remote_exmods=None)
If no transport URL is provided, the URL in the notifications section of
the config file will be used. If that URL is also absent, the same
transport as specified in the user's default section will be used.
The Transport has a single 'conf' property which is the cfg.ConfigOpts
instance used to construct the transport object.
"""
def __init__(self, driver):
self.conf = driver.conf
self._driver = driver
def _require_driver_features(self, requeue=False):
self._driver.require_features(requeue=requeue)
def _send(self, target, ctxt, message, wait_for_reply=None, timeout=None,
call_monitor_timeout=None, retry=None, transport_options=None):
if not target.topic:
raise exceptions.InvalidTarget('A topic is required to send',
target)
return self._driver.send(target, ctxt, message,
wait_for_reply=wait_for_reply,
timeout=timeout,
call_monitor_timeout=call_monitor_timeout,
retry=retry,
transport_options=transport_options)
def _send_notification(self, target, ctxt, message, version, retry=None):
if not target.topic:
raise exceptions.InvalidTarget('A topic is required to send',
target)
self._driver.send_notification(target, ctxt, message, version,
retry=retry)
def _listen(self, target, batch_size, batch_timeout):
if not (target.topic and target.server):
raise exceptions.InvalidTarget('A server\'s target must have '
'topic and server names specified',
target)
return self._driver.listen(target, batch_size,
batch_timeout)
def _listen_for_notifications(self, targets_and_priorities, pool,
batch_size, batch_timeout):
for target, priority in targets_and_priorities:
if not target.topic:
raise exceptions.InvalidTarget('A target must have '
'topic specified',
target)
return self._driver.listen_for_notifications(
targets_and_priorities, pool, batch_size, batch_timeout
)
def cleanup(self):
"""Release all resources associated with this transport."""
self._driver.cleanup()
class RPCTransport(Transport):
"""Transport object for RPC."""
def __init__(self, driver):
super(RPCTransport, self).__init__(driver)
class NotificationTransport(Transport):
"""Transport object for notifications."""
def __init__(self, driver):
super(NotificationTransport, self).__init__(driver)
class InvalidTransportURL(exceptions.MessagingException):
"""Raised if transport URL is invalid."""
def __init__(self, url, msg):
super(InvalidTransportURL, self).__init__(msg)
self.url = url
class DriverLoadFailure(exceptions.MessagingException):
"""Raised if a transport driver can't be loaded."""
def __init__(self, driver, ex):
msg = 'Failed to load transport driver "%s": %s' % (driver, ex)
super(DriverLoadFailure, self).__init__(msg)
self.driver = driver
self.ex = ex
def _get_transport(conf, url=None, allowed_remote_exmods=None,
transport_cls=RPCTransport):
allowed_remote_exmods = allowed_remote_exmods or []
conf.register_opts(_transport_opts)
if not isinstance(url, TransportURL):
url = TransportURL.parse(conf, url)
kwargs = dict(default_exchange=conf.control_exchange,
allowed_remote_exmods=allowed_remote_exmods)
try:
mgr = driver.DriverManager('oslo.messaging.drivers',
url.transport.split('+')[0],
invoke_on_load=True,
invoke_args=[conf, url],
invoke_kwds=kwargs)
except RuntimeError as ex:
raise DriverLoadFailure(url.transport, ex)
return transport_cls(mgr.driver)
@removals.remove(
message='use get_rpc_transport or get_notification_transport'
)
def get_transport(conf, url=None, allowed_remote_exmods=None):
"""A factory method for Transport objects.
This method will construct a Transport object from transport configuration
gleaned from the user's configuration and, optionally, a transport URL.
If a transport URL is supplied as a parameter, any transport configuration
contained in it takes precedence. If no transport URL is supplied, but
there is a transport URL supplied in the user's configuration then that
URL will take the place of the URL parameter. In both cases, any
configuration not supplied in the transport URL may be taken from
individual configuration parameters in the user's configuration.
An example transport URL might be::
rabbit://me:passwd@host:5672/virtual_host
and can either be passed as a string or a TransportURL object.
:param conf: the user configuration
:type conf: cfg.ConfigOpts
:param url: a transport URL, see :py:class:`transport.TransportURL`
:type url: str or TransportURL
:param allowed_remote_exmods: a list of modules which a client using this
transport will deserialize remote exceptions
from
:type allowed_remote_exmods: list
"""
return _get_transport(conf, url, allowed_remote_exmods,
transport_cls=RPCTransport)
class TransportHost(object):
"""A host element of a parsed transport URL."""
def __init__(self, hostname=None, port=None, username=None, password=None):
self.hostname = hostname
self.port = port
self.username = username
self.password = password
def __hash__(self):
return hash((self.hostname, self.port, self.username, self.password))
def __eq__(self, other):
return vars(self) == vars(other)
def __ne__(self, other):
return not self == other
def __repr__(self):
attrs = []
for a in ['hostname', 'port', 'username', 'password']:
v = getattr(self, a)
if v:
attrs.append((a, repr(v)))
values = ', '.join(['%s=%s' % i for i in attrs])
return '<TransportHost ' + values + '>'
class TransportOptions(object):
def __init__(self, at_least_once=False):
self._at_least_once = at_least_once
@property
def at_least_once(self):
return self._at_least_once
class TransportURL(object):
"""A parsed transport URL.
Transport URLs take the form::
driver://[user:pass@]host:port[,[userN:passN@]hostN:portN]/virtual_host?query
where:
driver
Specifies the transport driver to use. Typically this is `rabbit` for the
RabbitMQ broker. See the documentation for other available transport
drivers.
[user:pass@]host:port
Specifies the network location of the broker. `user` and `pass` are the
optional username and password used for authentication with the broker.
`user` and `pass` may contain any of the following ASCII characters:
* Alphabetic (a-z and A-Z)
* Numeric (0-9)
* Special characters: & = $ - _ . + ! * ( )
`user` may include at most one `@` character for compatibility with some
implementations of SASL.
All other characters in `user` and `pass` must be encoded via '%nn'
You may include multiple different network locations separated by commas.
The client will connect to any of the available locations and will
automatically fail over to another should the connection fail.
virtual_host
Specifies the "virtual host" within the broker. Support for virtual hosts
is specific to the message bus used.
query
Permits passing driver-specific options which override the corresponding
values from the configuration file.
:param conf: a ConfigOpts instance
:type conf: oslo.config.cfg.ConfigOpts
:param transport: a transport name for example 'rabbit'
:type transport: str
:param virtual_host: a virtual host path for example '/'
:type virtual_host: str
:param hosts: a list of TransportHost objects
:type hosts: list
:param query: a dictionary of URL query parameters
:type query: dict
"""
def __init__(self, conf, transport=None, virtual_host=None, hosts=None,
query=None):
self.conf = conf
self.conf.register_opts(_transport_opts)
self.transport = transport
self.virtual_host = virtual_host
if hosts is None:
self.hosts = []
else:
self.hosts = hosts
if query is None:
self.query = {}
else:
self.query = query
def __hash__(self):
return hash((tuple(self.hosts), self.transport, self.virtual_host))
def __eq__(self, other):
return (self.transport == other.transport and
self.virtual_host == other.virtual_host and
self.hosts == other.hosts)
def __ne__(self, other):
return not self == other
def __repr__(self):
attrs = []
for a in ['transport', 'virtual_host', 'hosts']:
v = getattr(self, a)
if v:
attrs.append((a, repr(v)))
values = ', '.join(['%s=%s' % i for i in attrs])
return '<TransportURL ' + values + '>'
def __str__(self):
netlocs = []
for host in self.hosts:
username = host.username
password = host.password
hostname = host.hostname
port = host.port
# Starting place for the network location
netloc = ''
# Build the username and password portion of the transport URL
if username is not None or password is not None:
if username is not None:
netloc += parse.quote(username, '')
if password is not None:
netloc += ':%s' % parse.quote(password, '')
netloc += '@'
# Build the network location portion of the transport URL
if hostname:
if ':' in hostname:
netloc += '[%s]' % hostname
else:
netloc += hostname
if port is not None:
netloc += ':%d' % port
netlocs.append(netloc)
# Assemble the transport URL
url = '%s://%s/' % (self.transport, ','.join(netlocs))
if self.virtual_host:
url += parse.quote(self.virtual_host)
if self.query:
url += '?' + parse.urlencode(self.query, doseq=True)
return url
@classmethod
def parse(cls, conf, url=None):
"""Parse a URL as defined by :py:class:`TransportURL` and return a
TransportURL object.
Assuming a URL takes the form of::
transport://user:pass@host:port[,userN:passN@hostN:portN]/virtual_host?query
then parse the URL and return a TransportURL object.
Netloc is parsed following the sequence bellow:
* It is first split by ',' in order to support multiple hosts
* 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
[
{"username": "user", "password": "pass", "host": "host1:port1"},
{"host": "host2:port2"}
]
If the url is not provided conf.transport_url is parsed instead.
:param conf: a ConfigOpts instance
:type conf: oslo.config.cfg.ConfigOpts
:param url: The URL to parse
:type url: str
:returns: A TransportURL
"""
if not url:
conf.register_opts(_transport_opts)
url = url or conf.transport_url
if not isinstance(url, str):
raise InvalidTransportURL(url, 'Wrong URL type')
url = parse.urlparse(url)
if not url.scheme:
raise InvalidTransportURL(url.geturl(), 'No scheme specified')
transport = url.scheme
query = {}
if url.query:
for key, values in parse.parse_qs(url.query).items():
query[key] = ','.join(values)
virtual_host = None
if url.path.startswith('/'):
virtual_host = parse.unquote(url.path[1:])
hosts_with_credentials = []
hosts_without_credentials = []
hosts = []
for host in url.netloc.split(','):
if not host:
continue
hostname = host
username = password = port = None
if '@' in host:
username, hostname = host.rsplit('@', 1)
if ':' in username:
username, password = username.split(':', 1)
password = parse.unquote(password)
username = parse.unquote(username)
if not hostname:
hostname = None
elif hostname.startswith('['):
# Find the closing ']' and extract the hostname
host_end = hostname.find(']')
if host_end < 0:
# NOTE(Vek): Identical to what Python 2.7's
# urlparse.urlparse() raises in this case
raise ValueError('Invalid IPv6 URL')
port_text = hostname[host_end:]
hostname = hostname[1:host_end]
# Now we need the port; this is compliant with how urlparse
# parses the port data
port = None
if ':' in port_text:
port = port_text.split(':', 1)[1]
elif ':' in hostname:
hostname, port = hostname.split(':', 1)
if port == "":
port = None
if port is not None:
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("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, query)