def connection(url=None, router=None, timeout=10, ssl_domain=None, sasl=None): """Return a BlockingConnection suitable for connecting to a management node @param url: URL of the management node. @param router: If address does not contain a path, use the management node for this router ID. If not specified and address does not contain a path, use the default management node. """ url = Url(url) # Convert string to Url class. if url.path is None: if router: url.path = u'_topo/0/%s/$management' % router else: url.path = u'$management' if ssl_domain: sasl_enabled = True else: sasl_enabled = True if sasl else False # if sasl_mechanism is unicode, convert it to python string return BlockingConnection(url, timeout=timeout, ssl_domain=ssl_domain, sasl_enabled=sasl_enabled, allowed_mechs=str(sasl.mechs) if sasl and sasl.mechs != None else None, user=str(sasl.user) if sasl and sasl.user != None else None, password=str(sasl.password) if sasl and sasl.password != None else None)
def testDefaults(self): # Check that we allow None for scheme, port url = Url(username='******', password='******', host='myhost', path='foobar', defaults=False) self.assertEqual(str(url), "//me:secret@myhost/foobar") self.assertUrl(url, None, 'me', 'secret', 'myhost', None, 'foobar') self.assertEqual(str(Url("amqp://*****:*****@myhost/foobar")), "amqp://*****:*****@myhost:amqp/foobar") # Empty string vs. None for path self.assertEqual(Url("myhost/").path, "") assert Url("myhost", defaults=False).path is None # Expanding abbreviated url strings. for s, u in [("", "amqp://0.0.0.0:amqp"), ("foo", "amqp://*****:*****@host/topic://test", "amqp://user@host:amqp/topic://test"), ("user@host:3456", "amqp://user@host:3456"), ("user:pass@host/topic://test", "amqp://*****:*****@host:amqp/topic://test")]: self.assertEqual(str(Url(s)), u)
def connection(url=None, router=None, timeout=10, ssl_domain=None, sasl=None): """Return a BlockingConnection suitable for connecting to a management node @param url: URL of the management node. @param router: If address does not contain a path, use the management node for this router ID. If not specified and address does not contain a path, use the default management node. """ url = Url(url) # Convert string to Url class. if url.path is None: if router: url.path = u'_topo/0/%s/$management' % router else: url.path = u'$management' if ssl_domain: sasl_enabled = True else: sasl_enabled = True if sasl else False # if sasl_mechanism is unicode, convert it to python string return BlockingConnection( url, timeout=timeout, ssl_domain=ssl_domain, sasl_enabled=sasl_enabled, allowed_mechs=str(sasl.mechs) if sasl and sasl.mechs != None else None, user=str(sasl.user) if sasl and sasl.user != None else None, password=str(sasl.password) if sasl and sasl.password != None else None)
def opts_url(opts): """Fix up default URL settings based on options""" url = Url(opts.bus) # If the options indicate SSL, make sure we use the amqps scheme. if opts.ssl_certificate or opts.ssl_trustfile or opts.bus.startswith("amqps:"): url.scheme = "amqps" return url
def testArgs(self): u = Url("amqp://*****:*****@host:amqp/path", scheme='foo', host='bar', port=1234, path='garden') self.assertUrl(u, 'foo', 'u', 'p', 'bar', 1234, 'garden') u = Url() self.assertUrl(u, None, None, None, None, None, None)
def opts_url(opts): """Fix up default URL settings based on options""" url = Url(opts.bus) # If the options indicate SSL, make sure we use the amqps scheme. if opts.ssl_certificate or opts.ssl_trustfile: url.scheme = "amqps" return url
def testMissing(self): self.assertUrl(Url(defaults=False), None, None, None, None, None, None) self.assertUrl(Url('amqp://', defaults=False), 'amqp', None, None, None, None, None) self.assertUrl(Url('username@', defaults=False), None, 'username', None, None, None, None) self.assertUrl(Url(':pass@', defaults=False), None, '', 'pass', None, None, None) self.assertUrl(Url('host', defaults=False), None, None, None, 'host', None, None) self.assertUrl(Url(':1234', defaults=False), None, None, None, None, 1234, None) self.assertUrl(Url('/path', defaults=False), None, None, None, None, None, 'path') for s, full in [ ('amqp://', 'amqp://'), ('username@', '//username@'), (':pass@', '//:pass@'), (':1234', '//:1234'), ('/path', '/path') ]: self.assertEqual(str(Url(s, defaults=False)), full) for s, full in [ ('amqp://', 'amqp://0.0.0.0:amqp'), ('username@', 'amqp://[email protected]:amqp'), (':pass@', 'amqp://:[email protected]:amqp'), (':1234', 'amqp://0.0.0.0:1234'), ('/path', 'amqp://0.0.0.0:amqp/path'), ('foo/path', 'amqp://foo:amqp/path'), (':1234/path', 'amqp://0.0.0.0:1234/path') ]: self.assertEqual(str(Url(s)), full)
def testAmqps(self): # Scheme defaults self.assertEqual(str(Url("me:secret@myhost/foobar")), "amqp://*****:*****@myhost:amqp/foobar") # Correct port for amqps vs. amqps self.assertEqual(str(Url("amqps://*****:*****@myhost/foobar")), "amqps://*****:*****@myhost:amqps/foobar") self.assertPort(Url.Port('amqps'), 5671, 'amqps') self.assertEqual(str(Url("host:amqps", defaults=False)), "//host:amqps") self.assertEqual(Url("host:amqps", defaults=False).port, 5671)
def test_reconnect(self): server_handler = ContainerTest._ReconnectServerHandler( "localhost", listen_on_error=True) client_handler = ContainerTest._ReconnectClientHandler(server_handler) container = Container(server_handler) container.connect(url=Url(host="localhost", port=server_handler.port), handler=client_handler) container.run() assert server_handler.peer_hostname == 'localhost', server_handler.peer_hostname assert client_handler.connect_failed assert client_handler.server_addr == Url( host='localhost', port=server_handler.port), client_handler.server_addr
def testEqual(self): self.assertEqual(Url("foo/path"), 'amqp://foo:amqp/path') self.assertEqual('amqp://foo:amqp/path', Url("foo/path")) self.assertEqual(Url("foo/path"), Url("foo/path")) self.assertNotEqual(Url("foo/path"), 'xamqp://foo:amqp/path') self.assertNotEqual('xamqp://foo:amqp/path', Url("foo/path")) self.assertNotEqual(Url("foo/path"), Url("bar/path"))
def connection(url=None, router=None, timeout=10, ssl_domain=None): """Return a BlockingConnection suitable for connecting to a management node @param url: URL of the management node. @param router: If address does not contain a path, use the management node for this router ID. If not specified and address does not contain a path, use the default management node. """ url = Url(url) # Convert string to Url class. if url.path is None: if router: url.path = u'_topo/0/%s/$management' % router else: url.path = u'$management' return BlockingConnection(url, timeout=timeout, ssl_domain=ssl_domain)
def testDefaults(self): # Check that we allow None for scheme, port url = Url(username='******', password='******', host='myhost', path='foobar') self.assertEqual(str(url), "me:secret@myhost/foobar") self.assertUrl(url, None, 'me', 'secret', 'myhost', None, 'foobar') self.assertEqual(str(Url("amqp://*****:*****@myhost/foobar").defaults()), "amqp://*****:*****@myhost:amqp/foobar") # Empty string vs. None for path self.assertEqual(Url("myhost/").path, "") assert Url("myhost").path is None
def create_receiver(self, context, source=None, target=None, name=None, dynamic=False, handler=None, options=None): if isinstance(context, basestring): context = Url(context) if isinstance(context, Url) and not source: source = context.path session = self._get_session(context) rcv = session.receiver( name or self._get_id(session.connection.container, source, target)) if source: rcv.source.address = source if dynamic: rcv.source.dynamic = True if target: rcv.target.address = target if handler: rcv.context = handler _apply_link_options(options, rcv) rcv.open() return rcv
def __init__(self, url, timeout=None, container=None, ssl_domain=None, heartbeat=None, properties=None, offered_capabilities=None, desired_capabilities=None): self.disconnected = False self.timeout = timeout or 60 self.container = container or Container() self.container.timeout = self.timeout self.container.start() self.url = Url(url).defaults() self.conn = self.container.connect( url=self.url, handler=self, ssl_domain=ssl_domain, reconnect=False, heartbeat=heartbeat, properties=properties, offered_capabilities=offered_capabilities, desired_capabilities=desired_capabilities) self.wait(lambda: not (self.conn.state & Endpoint.REMOTE_UNINIT), msg="Opening connection")
def is_proto_allowed(self, listener_port, tls_protocol): """ Opens a simple proton client connection to the provided TCP port using a specific TLS protocol version and returns True in case connection was established and accepted or False otherwise. :param listener_port: TCP port number :param tls_protocol: TLSv1, TLSv1.1 or TLSv1.2 (string) :return: """ # Management address to connect using the given TLS protocol url = Url("amqps://0.0.0.0:%d/$management" % listener_port) # Preparing SSLDomain (client cert) and SASL authentication info domain = SSLDomain(SSLDomain.MODE_CLIENT) # Enforcing given TLS protocol cproton.pn_ssl_domain_set_protocols(domain._domain, tls_protocol) # Try opening the secure and authenticated connection try: connection = BlockingConnection(url, sasl_enabled=False, ssl_domain=domain, timeout=self.TIMEOUT) except proton.Timeout: return False except proton.ConnectionException: return False # TLS version provided was accepted connection.close() return True
def __init__(self, url, timeout=None, container=None, ssl_domain=None): self.timeout = timeout self.container = container or Container() self.url = Url(utf8(url)).defaults() self.conn = self.container.connect(url=self.url, handler=self, ssl_domain=ssl_domain) self.wait(lambda: not (self.conn.state & Endpoint.REMOTE_UNINIT), msg="Opening connection")
def connect(url=None, router=None, timeout=10, ssl_domain=None, sasl=None, edge_router=None): """ Return a Node connected with the given parameters, see L{connection} @param url: URL of the management node. @param router: If address does not contain a path, use the management node for this router ID. If not specified and address does not contain a path, use the default management node. """ url_ = Url(url) # Convert string to Url class. if url_.path is not None: path = url_.path elif router: path = u'_topo/0/%s/$management' % router elif edge_router: path = u'_edge/%s/$management' % edge_router else: path = u'$management' return Node( Node.connection(url, router, timeout, ssl_domain, sasl, edge_router=edge_router), path)
def is_ssl_sasl_client_accepted(self, listener_port, tls_protocol): """ Attempts to connect a proton client to the management address on the given listener_port using the specific tls_protocol provided. If connection was established and accepted, returns True and False otherwise. :param listener_port: :param tls_protocol: :return: """ # Management address to connect using the given TLS protocol url = Url("amqps://0.0.0.0:%d/$management" % listener_port) # Preparing SSLDomain (client cert) and SASL authentication info domain = SSLDomain(SSLDomain.MODE_CLIENT) domain.set_credentials(self.ssl_file('client-certificate.pem'), self.ssl_file('client-private-key.pem'), 'client-password') # Enforcing given TLS protocol cproton.pn_ssl_domain_set_protocols(domain._domain, tls_protocol) # Try opening the secure and authenticated connection try: connection = BlockingConnection(url, sasl_enabled=True, ssl_domain=domain, allowed_mechs='PLAIN', user='******', password='******') except proton.ConnectionException: return False # TLS version provided was accepted connection.close() return True
def connect(url=None, router=None, timeout=10, ssl_domain=None, sasl=None, edge_router=None): """ Return a Node connected with the given parameters, see L{connection} @param url: URL of the management node. @param router: If address does not contain a path, use the management node for this router ID. If not specified and address does not contain a path, use the default management node. """ url_ = Url(url) # Convert string to Url class. if url_.path is not None: path = url_.path elif router: path = '_topo/0/%s/$management' % router elif edge_router: path = '_edge/%s/$management' % edge_router else: path = '$management' connection = Node.connection(url, router, timeout, ssl_domain, sasl, edge_router=edge_router) try: return Node(connection, path) except Exception: # ownership of connection has not been given to a new Node; close the connection connection.close() raise
def create_sender(self, context, target=None, source=None, name=None, handler=None, tags=None, options=None): if isinstance(context, basestring): context = Url(context) if isinstance(context, Url) and not target: target = context.path session = self._get_session(context) snd = session.sender( name or self._get_id(session.connection.container, target, source)) if source: snd.source.address = source if target: snd.target.address = target if handler: snd.context = handler if tags: snd.tag_generator = tags _apply_link_options(options, snd) snd.open() return snd
def __init__(self, url, messages, batch_size): super(TxRecv, self).__init__(prefetch=0, auto_accept=False) self.url = Url(url) self.expected = messages self.batch_size = batch_size self.current_batch = 0 self.committed = 0
def __init__(self, url, timeout=None, container=None, ssl_domain=None, heartbeat=None, **kwargs): self.disconnected = False self.timeout = timeout or 60 self.container = container or Container() self.container.timeout = self.timeout self.container.start() self.url = Url(url).defaults() self.conn = None failed = True try: self.conn = self.container.connect(url=self.url, handler=self, ssl_domain=ssl_domain, reconnect=False, heartbeat=heartbeat, **kwargs) self.wait(lambda: not (self.conn.state & Endpoint.REMOTE_UNINIT), msg="Opening connection") failed = False finally: if failed and self.conn: self.close()
def ssl_test(self, url_name, arg_names): """Run simple SSL connection test with supplied parameters. See test_ssl_* below. """ args = dict( trustfile=['--ssl-trustfile', self.ssl_file('ca-certificate.pem')], bad_trustfile=[ '--ssl-trustfile', self.ssl_file('bad-ca-certificate.pem') ], client_cert=[ '--ssl-certificate', self.ssl_file('client-certificate.pem') ], client_key=['--ssl-key', self.ssl_file('client-private-key.pem')], client_pass=['--ssl-password', 'client-password']) args['client_cert_all'] = args['client_cert'] + args[ 'client_key'] + args['client_pass'] addrs = [self.router.addresses[i] for i in range(4)] urls = dict(zip(['none', 'strict', 'unsecured', 'auth'], addrs)) urls.update( zip(['none_s', 'strict_s', 'unsecured_s', 'auth_s'], (Url(a, scheme="amqps") for a in addrs))) self.run_qdstat(['--general'] + sum([args[n] for n in arg_names], []), address=str(urls[url_name]), regex=r'(?s)Router Statistics.*Mode\s*Standalone')
def __init__(self, url): super(ClientAddressValidator, self).__init__() self.url = Url(url) self.container = Container(self) self.link_error = False self.container.run() signal.signal(signal.SIGALRM, self.timeout) signal.alarm(ClientAddressValidator.TIMEOUT)
def __init__(self, url, period): super(Send, self).__init__() self.url = Url(url) self.sent = 0 self.confirmed = 0 self.period = period self.sender = None self.container = None
def __init__(self, url, messages, batch_size): super(TxSend, self).__init__() self.url = Url(url) self.current_batch = 0 self.committed = 0 self.confirmed = 0 self.total = messages self.batch_size = batch_size
def opts_sasl(opts): url = Url(opts.bus) mechs, user, password, sasl_password_file = opts.sasl_mechanisms, (opts.sasl_username or url.username), (opts.sasl_password or url.password), opts.sasl_password_file if not (mechs or user or password or sasl_password_file): return None return Sasl(mechs, user, password, sasl_password_file)
def __init__(self, address=None, *handlers, **kwargs): if not address: super(EventHubClient, self).__init__(**kwargs) else: super(EventHubClient, self).__init__(self, **kwargs) self.address = Url(address) self.receivers = [] self.shared_connection = None self.shared_session = None
def listen(self, url, ssl_domain=None): """ Initiates a server socket, accepting incoming AMQP connections on the interface and port specified. """ url = Url(url) ssl_config = ssl_domain if not ssl_config and url.scheme == 'amqps': ssl_config = self.ssl_domain return self.acceptor(url.host, url.port)
def test_virtual_host(self): ensureCanTestExtendedSASL() server_handler = ContainerTest._ServerHandler("localhost") container = Container(server_handler) conn = container.connect(url=Url(host="localhost", port=server_handler.port), handler=ContainerTest._ClientHandler(), virtual_host="a.b.c.org") container.run() assert server_handler.peer_hostname == "a.b.c.org", server_handler.peer_hostname
def query( self, entity_type: str = 'org.apache.qpid.dispatch.router.node' ) -> NamedTuple: """ Queries the related router instance, retrieving information for the provided Entity Type. The result is an array of a named tuple, whose fields are the attribute names returned by the router. In example, if querying entity type: org.apache.qpid.dispatch.allocator, the results can be accessed as: result.typeName, result.typeSize, ... same names returned by the router. :param entity_type: :return: """ # Scheme to use scheme = 'amqp' if self._connection_options['ssl_domain']: scheme = 'amqps' # URL to test url = Url("%s://%s:%s/$management" % (scheme, self.host, self.port)) self._logger.info("Querying router at: %s://%s:%s/$management" % (scheme, self.host, self.port)) # Proton connection self._logger.debug("Connection options: %s" % self._connection_options) connection = BlockingConnection(url, **self._connection_options) # Proton sync client client = SyncRequestResponse(connection, url.path) # Request message object request = proton.Message() request.properties = { u'operation': u'QUERY', u'entityType': u'%s' % entity_type } request.body = {u'attributeNames': []} # Sending the request response = client.call(request) # Closing connection client.connection.close() # Namedtuple that represents the query response from the router # so fields can be read based on their attribute names. RouterQueryResults = namedtuple('RouterQueryResults', response.body["attributeNames"]) records = [] for record in response.body["results"]: records.append(RouterQueryResults(*record)) return records
def connection(url=None, timeout=10, ssl_domain=None, sasl=None): """ Return a BlockingConnection for connection to a managemenet node """ url = Url(url) url.path = u'$management' if ssl_domain: sasl_enabled = True else: sasl_enabled = True if sasl else False return BlockingConnection(url, timeout=timeout, ssl_domain=ssl_domain, sasl_enabled=sasl_enabled, allowed_mechs=str(sasl.mechs) if sasl and sasl.mechs != None else None, user=str(sasl.user) if sasl else None, password=str(sasl.password) if sasl else None)