async def test_iothub_client_receive_async(live_iothub_config): operation = '/messages/events/ConsumerGroups/{}/Partitions/'.format( live_iothub_config['consumer_group']) auth = authentication.SASLPlain( live_iothub_config['hostname'], *_build_iothub_amqp_endpoint_from_target(live_iothub_config)) source = 'amqps://' + live_iothub_config['hostname'] + operation log.info("Source: {}".format(source)) async with uamqp.ConnectionAsync(live_iothub_config['hostname'], auth, debug=True) as conn: tasks = [ _receive_mesages(conn, source + '0', auth), _receive_mesages(conn, source + '1', auth) ] results = await asyncio.gather(*tasks) redirect = results[0] new_auth = authentication.SASLPlain(redirect.hostname, live_iothub_config['key_name'], live_iothub_config['access_key']) await conn.redirect_async(redirect, new_auth) tasks = [] for t in results: tasks.append(_receive_mesages(conn, t.address, auth)) messages = await asyncio.gather(*tasks)
def uamqp_receive_simple(): parsed_uri = urlparse(uri) plain_auth = authentication.SASLPlain(parsed_uri.hostname, key_name, access_key) message = uamqp.receive_message(uri, auth=plain_auth) print("Received: {}".format(message))
def test_event_hubs_filter_receive(live_eventhub_config): plain_auth = authentication.SASLPlain( live_eventhub_config['hostname'], live_eventhub_config['key_name'], live_eventhub_config['access_key']) source_url = "amqps://{}/{}/ConsumerGroups/{}/Partitions/{}".format( live_eventhub_config['hostname'], live_eventhub_config['event_hub'], live_eventhub_config['consumer_group'], live_eventhub_config['partition']) source = address.Source(source_url) source.set_filter(b"amqp.annotation.x-opt-enqueuedtimeutc > 1518731960545") with uamqp.ReceiveClient(source, auth=plain_auth, timeout=50, prefetch=50) as receive_client: log.info("Created client, receiving...") batch = receive_client.receive_message_batch(max_batch_size=10) while batch: for message in batch: annotations = message.annotations log.info("Partition Key: {}".format(annotations.get(b'x-opt-partition-key'))) log.info("Sequence Number: {}".format(annotations.get(b'x-opt-sequence-number'))) log.info("Offset: {}".format(annotations.get(b'x-opt-offset'))) log.info("Enqueued Time: {}".format(annotations.get(b'x-opt-enqueued-time'))) log.info("Message format: {}".format(message._message.message_format)) log.info("{}".format(list(message.get_data()))) batch = receive_client.receive_message_batch(max_batch_size=10) log.info("Finished receiving")
def _create_auth(self, username=None, password=None): """ Create an ~uamqp.authentication.SASTokenAuth instance to authenticate the session. :param auth_uri: The URI to authenticate against. :type auth_uri: str :param username: The name of the shared access policy. :type username: str :param password: The shared access key. :type password: str """ username = username or self._auth_config['username'] password = password or self._auth_config['password'] if "@sas.root" in username: return authentication.SASLPlain(self.address.hostname, username, password, http_proxy=self.http_proxy) return authentication.SASTokenAuth.from_shared_access_key( self.auth_uri, username, password, timeout=self.auth_timeout, http_proxy=self.http_proxy)
def _create_auth(self, username=None, password=None): """ Create an ~uamqp.authentication.SASTokenAuth instance to authenticate the session. :param username: The name of the shared access policy. :type username: str :param password: The shared access key. :type password: str """ if self.sas_token: token = self.sas_token() if callable(self.sas_token) else self.sas_token try: expiry = int(parse_sas_token(token)['se']) except (KeyError, TypeError, IndexError): raise ValueError("Supplied SAS token has no valid expiry value.") return authentication.SASTokenAuth( self.auth_uri, self.auth_uri, token, expires_at=expiry, timeout=self.auth_timeout, http_proxy=self.http_proxy) username = username or self._auth_config['username'] password = password or self._auth_config['password'] if "@sas.root" in username: return authentication.SASLPlain( self.address.hostname, username, password, http_proxy=self.http_proxy) return authentication.SASTokenAuth.from_shared_access_key( self.auth_uri, username, password, timeout=self.auth_timeout, http_proxy=self.http_proxy)
def __init__(self, remote_address, auth=None, client_name=None, debug=False, error_policy=None, keep_alive_interval=None, **kwargs): self._encoding = kwargs.pop('encoding', None) or 'UTF-8' self._remote_address = remote_address if isinstance(remote_address, address.Address) \ else address.Address(remote_address) self._hostname = self._remote_address.hostname if not auth: username = self._remote_address.username password = self._remote_address.password if username and password: username = unquote_plus(username) password = unquote_plus(password) auth = authentication.SASLPlain(self._hostname, username, password) self._auth = auth if auth else authentication.SASLAnonymous( self._hostname) self._name = client_name if client_name else str(uuid.uuid4()) self._debug_trace = debug self._counter = c_uamqp.TickCounter() self._shutdown = False self._connection = None self._ext_connection = False self._session = None self._backoff = 0 self._error_policy = error_policy or errors.ErrorPolicy() self._keep_alive_interval = int( keep_alive_interval) if keep_alive_interval else 0 self._keep_alive_thread = None # Connection settings self._max_frame_size = kwargs.pop( 'max_frame_size', None) or constants.MAX_FRAME_SIZE_BYTES self._channel_max = kwargs.pop('channel_max', None) self._idle_timeout = kwargs.pop('idle_timeout', None) self._properties = kwargs.pop('properties', None) self._remote_idle_timeout_empty_frame_send_ratio = kwargs.pop( 'remote_idle_timeout_empty_frame_send_ratio', None) # Session settings self._outgoing_window = kwargs.pop( 'outgoing_window', None) or constants.MAX_FRAME_SIZE_BYTES self._incoming_window = kwargs.pop( 'incoming_window', None) or constants.MAX_FRAME_SIZE_BYTES self._handle_max = kwargs.pop('handle_max', None) # AMQP object settings self.connection_type = Connection self.session_type = Session if kwargs: raise ValueError("Received unrecognized kwargs: {}".format( ", ".join(kwargs.keys())))
def test_iothub_client_receive_sync(live_iothub_config): operation = '/messages/events/ConsumerGroups/{}/Partitions/{}'.format( live_iothub_config['consumer_group'], live_iothub_config['partition']) auth = authentication.SASLPlain( live_iothub_config['hostname'], *_build_iothub_amqp_endpoint_from_target(live_iothub_config)) source = 'amqps://' + live_iothub_config['hostname'] + operation log.info("Source: {}".format(source)) with uamqp.Connection(live_iothub_config['hostname'], auth, debug=True) as conn: result = _receive_message(conn, source, auth) new_auth = authentication.SASLPlain(result.hostname, live_iothub_config['key_name'], live_iothub_config['access_key']) conn.redirect(result, new_auth) result = _receive_message(conn, result.address, new_auth) log.info("Finished receiving")
def uamqp_send_simple(): msg_content = b"Hello world" parsed_uri = urlparse(uri) plain_auth = authentication.SASLPlain(parsed_uri.hostname, key_name, access_key) uamqp.send_message(uri, msg_content, auth=plain_auth) print("Message sent!")
def _create_auth(self, username=None, password=None): """ Create an ~uamqp.authentication.SASTokenAuth instance to authenticate the session. :param username: The name of the shared access policy. :type username: str :param password: The shared access key. :type password: str """ http_proxy = self._config.http_proxy transport_type = self._config.transport_type auth_timeout = self._config.auth_timeout # TODO: the following code can be refactored to create auth from classes directly instead of using if-else if isinstance(self._credential, EventHubSharedKeyCredential): # pylint:disable=no-else-return username = username or self._auth_config['username'] password = password or self._auth_config['password'] if "@sas.root" in username: return authentication.SASLPlain(self._host, username, password, http_proxy=http_proxy, transport_type=transport_type) return authentication.SASTokenAuth.from_shared_access_key( self._auth_uri, username, password, timeout=auth_timeout, http_proxy=http_proxy, transport_type=transport_type) elif isinstance(self._credential, EventHubSASTokenCredential): token = self._credential.get_sas_token() try: expiry = int(parse_sas_token(token)['se']) except (KeyError, TypeError, IndexError): raise ValueError( "Supplied SAS token has no valid expiry value.") return authentication.SASTokenAuth(self._auth_uri, self._auth_uri, token, expires_at=expiry, timeout=auth_timeout, http_proxy=http_proxy, transport_type=transport_type) else: # Azure credential get_jwt_token = functools.partial( self._credential.get_token, 'https://eventhubs.azure.net//.default') return authentication.JWTTokenAuth(self._auth_uri, self._auth_uri, get_jwt_token, http_proxy=http_proxy, transport_type=transport_type)
def test_event_hubs_single_batch_receive(live_eventhub_config): plain_auth = authentication.SASLPlain(live_eventhub_config['hostname'], live_eventhub_config['key_name'], live_eventhub_config['access_key']) source = "amqps://{}/{}/ConsumerGroups/{}/Partitions/{}".format( live_eventhub_config['hostname'], live_eventhub_config['event_hub'], live_eventhub_config['consumer_group'], live_eventhub_config['partition']) message = uamqp.receive_messages(source, auth=plain_auth, timeout=5000) assert len(message) <= 300
def test_event_hubs_simple_receive(live_eventhub_config): plain_auth = authentication.SASLPlain(live_eventhub_config['hostname'], live_eventhub_config['key_name'], live_eventhub_config['access_key']) source = "amqps://{}/{}/ConsumerGroups/{}/Partitions/{}".format( live_eventhub_config['hostname'], live_eventhub_config['event_hub'], live_eventhub_config['consumer_group'], live_eventhub_config['partition']) message = uamqp.receive_message(source, auth=plain_auth) log.info("Received: {}".format(message.get_data()))
async def test_event_hubs_client_send_async(live_eventhub_config): properties = {b"SendData": b"Property_String_Value_1"} msg_content = b"hello world" message = uamqp.Message(msg_content, application_properties=properties) plain_auth = authentication.SASLPlain(live_eventhub_config['hostname'], live_eventhub_config['key_name'], live_eventhub_config['access_key']) target = "amqps://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub']) send_client = a_uamqp.SendClientAsync(target, auth=plain_auth, debug=True) send_client.queue_message(message) results = await send_client.send_all_messages_async() assert not [m for m in results if m == uamqp.constants.MessageState.Failed]
def _create_auth(self): """ Create an ~uamqp.authentication.SASTokenAuth instance to authenticate the session. """ http_proxy = self._config.http_proxy transport_type = self._config.transport_type auth_timeout = self._config.auth_timeout # TODO: the following code can be refactored to create auth from classes directly instead of using if-else if isinstance(self._credential, EventHubSharedKeyCredential): # pylint:disable=no-else-return username = self._credential.policy password = self._credential.key if "@sas.root" in username: return authentication.SASLPlain(self._address.hostname, username, password, http_proxy=http_proxy, transport_type=transport_type) return authentication.SASTokenAuth.from_shared_access_key( self._auth_uri, username, password, timeout=auth_timeout, http_proxy=http_proxy, transport_type=transport_type) elif isinstance(self._credential, EventHubSASTokenCredential): token = self._credential.get_sas_token() try: expiry = int(parse_sas_token(token)['se']) except (KeyError, TypeError, IndexError): raise ValueError( "Supplied SAS token has no valid expiry value.") return authentication.SASTokenAuth(self._auth_uri, self._auth_uri, token, expires_at=expiry, timeout=auth_timeout, http_proxy=http_proxy, transport_type=transport_type) else: # Azure credential get_jwt_token = functools.partial(self._credential.get_token, JWT_TOKEN_SCOPE) return authentication.JWTTokenAuth(self._auth_uri, self._auth_uri, get_jwt_token, http_proxy=http_proxy, transport_type=transport_type)
def _create_auth(self, auth_uri, username, password): # pylint: disable=no-self-use """ Create an ~uamqp.authentication.cbs_auth_async.SASTokenAuthAsync instance to authenticate the session. :param auth_uri: The URI to authenticate against. :type auth_uri: str :param username: The name of the shared access policy. :type username: str :param password: The shared access key. :type password: str """ if "@sas.root" in username: return authentication.SASLPlain(self.address.hostname, username, password) return authentication.SASTokenAsync.from_shared_access_key(auth_uri, username, password)
async def test_event_hubs_filter_receive_async(live_eventhub_config): plain_auth = authentication.SASLPlain(live_eventhub_config['hostname'], live_eventhub_config['key_name'], live_eventhub_config['access_key']) source_url = "amqps://{}/{}/ConsumerGroups/{}/Partitions/{}".format( live_eventhub_config['hostname'], live_eventhub_config['event_hub'], live_eventhub_config['consumer_group'], live_eventhub_config['partition']) source = address.Source(source_url) source.set_filter(b"amqp.annotation.x-opt-enqueuedtimeutc > 1518731960545") receive_client = uamqp.ReceiveClientAsync(source, auth=plain_auth, timeout=5000) await receive_client.receive_messages_async(on_message_received)
async def test_event_hubs_mgmt_op_async(live_eventhub_config): plain_auth = authentication.SASLPlain(live_eventhub_config['hostname'], live_eventhub_config['key_name'], live_eventhub_config['access_key']) target = "amqps://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub']) async with uamqp.AMQPClientAsync(target, auth=plain_auth, debug=True) as send_client: mgmt_msg = uamqp.Message(application_properties={'name': live_eventhub_config['event_hub']}) response = await send_client.mgmt_request_async( mgmt_msg, b'READ', op_type=b'com.microsoft:eventhub', status_code_field=b'status-code', description_fields=b'status-description', timeout=3000) output = response.get_data() assert output[b'partition_ids'] == [b"0", b"1"]
async def test_event_hubs_client_send_multiple_async(live_eventhub_config): properties = {b"SendData": b"Property_String_Value_1"} msg_content = b"hello world" plain_auth = authentication.SASLPlain(live_eventhub_config['hostname'], live_eventhub_config['key_name'], live_eventhub_config['access_key']) assert not plain_auth.consumed target = "amqps://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub']) send_client = uamqp.SendClientAsync(target, auth=plain_auth, debug=False) messages = [] for i in range(10): messages.append(uamqp.Message(msg_content, application_properties=properties)) send_client.queue_message(*messages) assert len(send_client.pending_messages) == 10 results = await send_client.send_all_messages_async() assert len(results) == 10 assert not [m for m in results if m == uamqp.constants.MessageState.SendFailed] assert plain_auth.consumed assert send_client.pending_messages == []
def _receive_message(receive_client, target): try: batch = receive_client.receive_message_batch(max_batch_size=10) except errors.LinkRedirect as redirect: new_auth = authentication.SASLPlain(redirect.hostname, target['key_name'], target['access_key']) #new_auth = authentication.SASTokenAuth.from_shared_access_key(redirect.address.decode('utf-8'), target['key_name'], target['access_key']) receive_client.redirect(redirect, new_auth) batch = receive_client.receive_message_batch(max_batch_size=10) while batch: log.info("Got batch: {}".format(len(batch))) assert len(batch) <= 10 for message in batch: annotations = message.annotations log.info("Sequence Number: {}".format( annotations.get(b'x-opt-sequence-number'))) batch = receive_client.receive_message_batch(max_batch_size=10)
def _create_auth(self): """ Create an ~uamqp.authentication.cbs_auth_async.SASTokenAuthAsync instance to authenticate the session. """ http_proxy = self._config.http_proxy transport_type = self._config.transport_type auth_timeout = self._config.auth_timeout if isinstance(self._credential, EventHubSharedKeyCredential): # pylint:disable=no-else-return username = self._credential.policy password = self._credential.key if "@sas.root" in username: return authentication.SASLPlain( self._host, username, password, http_proxy=http_proxy, transport_type=transport_type) return authentication.SASTokenAsync.from_shared_access_key( self._auth_uri, username, password, timeout=auth_timeout, http_proxy=http_proxy, transport_type=transport_type) elif isinstance(self._credential, EventHubSASTokenCredential): token = self._credential.get_sas_token() try: expiry = int(parse_sas_token(token)['se']) except (KeyError, TypeError, IndexError): raise ValueError("Supplied SAS token has no valid expiry value.") return authentication.SASTokenAsync( self._auth_uri, self._auth_uri, token, expires_at=expiry, timeout=auth_timeout, http_proxy=http_proxy, transport_type=transport_type) else: get_jwt_token = functools.partial(self._credential.get_token, 'https://eventhubs.azure.net//.default') return authentication.JWTTokenAsync(self._auth_uri, self._auth_uri, get_jwt_token, http_proxy=http_proxy, transport_type=transport_type)
def get_plain_auth(config): return authentication.SASLPlain(config['hostname'], config['key_name'], config['access_key'])