예제 #1
0
def test_client_secret_credential(aad_credential, live_eventhub):
    try:
        from azure.identity import EnvironmentCredential
    except ImportError:
        pytest.skip("No azure identity library")
    credential = EnvironmentCredential()
    producer_client = EventHubProducerClient(
        host=live_eventhub['hostname'],
        event_hub_path=live_eventhub['event_hub'],
        credential=credential,
        user_agent='customized information')
    consumer_client = EventHubConsumerClient(
        host=live_eventhub['hostname'],
        event_hub_path=live_eventhub['event_hub'],
        credential=credential,
        user_agent='customized information')
    with producer_client:
        producer_client.send(EventData(body='A single message'))

    def on_events(partition_context, events):
        assert partition_context.partition_id == '0'
        assert len(events) == 1
        assert list(events[0].body)[0] == 'A single message'.encode('utf-8')

    with consumer_client:
        worker = threading.Thread(target=consumer_client.receive,
                                  args=(on_events, ),
                                  kwargs={
                                      "consumer_group": '$default',
                                      "partition_id": '0'
                                  })
        worker.start()
        time.sleep(2)
def test_send_with_long_interval_sync(live_eventhub, sleep):
    sender = EventHubProducerClient(
        live_eventhub['hostname'], live_eventhub['event_hub'],
        EventHubSharedKeyCredential(live_eventhub['key_name'],
                                    live_eventhub['access_key']))
    with sender:
        sender.send(EventData(b"A single event"))
        for _ in range(1):
            if sleep:
                time.sleep(300)
            else:
                sender._producers[-1]._handler._connection._conn.destroy()
            sender.send(EventData(b"A single event"))
        partition_ids = sender.get_partition_ids()

    received = []
    for p in partition_ids:
        uri = "sb://{}/{}".format(live_eventhub['hostname'],
                                  live_eventhub['event_hub'])
        sas_auth = authentication.SASTokenAuth.from_shared_access_key(
            uri, live_eventhub['key_name'], live_eventhub['access_key'])

        source = "amqps://{}/{}/ConsumerGroups/{}/Partitions/{}".format(
            live_eventhub['hostname'], live_eventhub['event_hub'],
            live_eventhub['consumer_group'], p)
        receiver = uamqp.ReceiveClient(source,
                                       auth=sas_auth,
                                       debug=False,
                                       timeout=5000,
                                       prefetch=500)
        try:
            receiver.open()
            received.extend([
                EventData._from_message(x)
                for x in receiver.receive_message_batch(timeout=5000)
            ])
        finally:
            receiver.close()

    assert len(received) == 2
    assert list(received[0].body)[0] == b"A single event"
예제 #3
0
def test_client_secret_credential(aad_credential, live_eventhub):
    try:
        from azure.identity import EnvironmentCredential
    except ImportError as e:
        pytest.skip("No azure identity library")
    credential = EnvironmentCredential()
    producer_client = EventHubProducerClient(
        fully_qualified_namespace=live_eventhub['hostname'],
        eventhub_name=live_eventhub['event_hub'],
        credential=credential,
        user_agent='customized information')
    consumer_client = EventHubConsumerClient(
        fully_qualified_namespace=live_eventhub['hostname'],
        eventhub_name=live_eventhub['event_hub'],
        consumer_group='$default',
        credential=credential,
        user_agent='customized information')
    with producer_client:
        producer_client.send(EventData(body='A single message'),
                             partition_id="0")

    def on_event(partition_context, event):
        on_event.called = True
        on_event.partition_id = partition_context.partition_id
        on_event.event = event

    on_event.called = False
    with consumer_client:
        worker = threading.Thread(target=consumer_client.receive,
                                  args=(on_event, ),
                                  kwargs={"partition_id": '0'})
        worker.start()
        time.sleep(6)

    worker.join()
    assert on_event.called is True
    assert on_event.partition_id == "0"
    assert list(on_event.event.body)[0] == 'A single message'.encode('utf-8')
예제 #4
0
#!/usr/bin/env python

# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
"""
An example to show authentication using aad credentials
"""

import os
from azure.eventhub import EventData, EventHubProducerClient
from azure.identity import EnvironmentCredential


fully_qualified_namespace = os.environ['EVENT_HUB_HOSTNAME']
eventhub_name = os.environ['EVENT_HUB_NAME']


credential = EnvironmentCredential()
producer = EventHubProducerClient(fully_qualified_namespace=fully_qualified_namespace,
                                  eventhub_name=eventhub_name,
                                  credential=credential)

with producer:
    event = EventData(body='A single message')
    producer.send(event, partition_id='0')