Azure / azure-iot-sdk-python

A Python SDK for connecting devices to Microsoft Azure IoT services
MIT License
425 stars 379 forks source link

SASL handshake failed #149

Closed zolvarga closed 6 years ago

zolvarga commented 6 years ago

This issue is copied from: https://github.com/Azure/azure-uamqp-c/issues/244

Hello, we are using azure-uamqp-c as part of the Python Package azure-iothub-service-client (which is part of the azure-iot-sdk-python which is part of the azure-iot-sdk-c).

Now I was trying to change the Share Access Polices from iothubowner to service in my code to imporve the security. But with the service Share Access Polices I get following error:

Error: Time:Wed Jun 20 15:14:29 2018 File:C:\Release\iot-sdks-internals\release\python\automation\aziotsdk_pytools\src\c\uamqp\src\saslclientio.c Func:_on_sasl_frame_received_callback Line:901 SASL handshake failed with code 02

For troubleshooting I granted the service Share Access Polices the same IoT Hub permissions as the iothubowner but I still get this error. Why?

zolvarga commented 6 years ago

@HyP3r-

Please follow up the issue here

Best Regards, Zoltan

zolvarga commented 6 years ago

@HyP3r-

I just tested with the Python SDK (latest Pip package) and it worked using the "service" SAS. Could you please make sure when you made the Service Permission change on the portal you applied it by pressing the "Save" button and waited couple minutes to have it propagated?

Please try and let me know.

Best Regards, Zoltan

HyP3r- commented 6 years ago

Hello and thank you for the quick response.

I really had version 1.3.0 of the azure_iothub_service_client in use. That's why I've updated it to version 1.3.5, which seems to be the latest version.

But unfortunately it still does not work.

Could you please make sure when you made the Service Permission change on the portal you applied it by pressing the "Save" button and waited couple minutes to have it propagated?

The service user is created by default, this should always work. But I have given it all rights for testing, but it still does not work.

Here is the code how I send messages with my Python program:

from iothub_service_client import IoTHubMessaging, IoTHubMessage, IoTHubMessagingError

class Sender:
    """
    Sender
    """

    RETRY = 3

    IOT_HUB_HOSTNAME = "hostname.azure-devices.net"
    IOT_HUB_SHARED_ACCESS_KEY_NAME = "service"
    IOT_HUB_SHARED_ACCESS_KEY = "<<<Key>>>"
    IOT_HUB_CONNECTION_STRING = "HostName={};SharedAccessKeyName={};SharedAccessKey={}"

    def __init__(self):
        """
        Store IoT Hub Messaging Client
        """

        self._iothub_messaging = None

    def run(self):
        """
        Start the Sender
        """

        # create iot hub messaging instance
        self._iothub_messaging = \
            IoTHubMessaging(Sender.IOT_HUB_CONNECTION_STRING.format(Sender.IOT_HUB_HOSTNAME,
                                                                    Sender.IOT_HUB_SHARED_ACCESS_KEY_NAME,
                                                                    Sender.IOT_HUB_SHARED_ACCESS_KEY))
        self._iothub_messaging.open(self.open_complete_callback, 0)

    def send_raw(self, device_id, message):
        """
        Send Message to Device and Retry if Fail
        """

        for _ in range(Sender.RETRY):
            try:
                self._iothub_messaging.send_async(device_id, message, self.send_complete_callback, 0)
                return True
            except IoTHubMessagingError as e:
                log("Error while sending Message. Trying to reconnect. ({})".format(str(e)), error=True, source=Sender)
                self._iothub_messaging.open(self.open_complete_callback, 0)
        return False

    def send_complete_callback(self, context, messaging_result):
        """
        Callback Handler for Completed Messages
        """

        pass

    def open_complete_callback(self, context):
        """
        Callback Handler for Connection Complete
        """

        pass
zolvarga commented 6 years ago

@HyP3r-

Thanks for the code we will investigate it

Best Regards, Zoltan

pierreca commented 6 years ago

@HyP3r- @zolvarga this seems like a duplicate of #124. closing and following up there.