Azure / iotedgedev

The Azure IoT Edge Dev Tool greatly simplifies your Azure IoT Edge development process. It has everything you need to get started and helps with your day-to-day Edge development.
https://aka.ms/iotedgedev
Other
160 stars 70 forks source link

iotedgedev build fails with DPS device connection string #482

Closed EliiseS closed 3 years ago

EliiseS commented 3 years ago

iotedgedev build fails with DPS device connection string. Why does iotedgedev check the device connection string if it is not required for this operation?

Steps to Reproduce:

  1. Set up a valid deployment and .env file
  2. Set DEVICE_CONNECTION_STRING to DPS device connection string ("HostName=soak-iot-hub.azure-devices.net;DeviceId=soak-edge-device;x509=true") in the .env
  3. Run iotedgedev build -f <your_deployment_file>

Expected outcome:

deployment is generated in /config

Actual outcome

Environment Variables loaded from: .env (/__w/1/s/src/edge/deployment/.env)
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/iotedgedev/envvars.py", line 113, in load
    self.DEVICE_CONNECTION_INFO = DeviceConnectionString(self.DEVICE_CONNECTION_STRING)
  File "/usr/local/lib/python3.7/site-packages/iotedgedev/connectionstring.py", line 35, in __init__
    ConnectionString.__init__(self, value)
  File "/usr/local/lib/python3.7/site-packages/iotedgedev/connectionstring.py", line 19, in __init__
    self.shared_access_key = self["sharedaccesskey"]
  File "/usr/local/lib/python3.7/site-packages/iotedgedev/connectionstring.py", line 22, in __getitem__
    return self.data[key]
KeyError: 'sharedaccesskey'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/iotedgedev/envvars.py", line 116, in load
    raise ValueError("Unable to parse DEVICE_CONNECTION_STRING Environment Variable. Please ensure that you have the right connection string set. {0}".format(str(ex)))
ValueError: Unable to parse DEVICE_CONNECTION_STRING Environment Variable. Please ensure that you have the right connection string set. 'sharedaccesskey'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/iotedgedev", line 5, in <module>
    from iotedgedev.cli import main
  File "/usr/local/lib/python3.7/site-packages/iotedgedev/cli.py", line 30, in <module>
    envvars.load()
  File "/usr/local/lib/python3.7/site-packages/iotedgedev/envvars.py", line 143, in load
    raise ValueError(msg)
ValueError: Environment variables not configured correctly. Run `iotedgedev new` to create a new solution with sample .env file. 

Workaround

Remove DEVICE_CONNECTION_STRING from the .env file

konichi3 commented 3 years ago

Hi @EliiseS

The iotedgedev tool only supports shared access key authentication to the device in IoT Hub.

Looks like you have created a Edge device entity with X.509 cert. Can you try to create Edge device with Shared Access Key and see if it works?

Your device connection string.

HostName=soak-iot-hub.azure-devices.net;DeviceId=soak-edge-device;x509=true"

EliiseS commented 3 years ago

Hi @konichi3,

I know my device connection string is a X509 cert connection string and that's why it doesn't work. The command does work if you just don't set the DEVICE_CONNECTION_STRING.

The question I have, why is the connection string checked on the build command if it's not needed for this operation in the first place?

While I know DPS is not supported officially yet, it would be nice to be able to use iotedgedev build with DPS without having to not set the DEVICE_CONNECTION_STRING

marianan commented 3 years ago

We added to our backlog the investigation why iotedgedev build requires the connection string.

cindydeng1998 commented 3 years ago

Hi @EliiseS, the iotedgedev tool initializes and loads environment variables before every single command in the CLI. While we can try to optimize this, we can add in a more helpful error message for DPS connection strings until we provide support for DPS in this tool. Let us know what you think, thanks!

EliiseS commented 3 years ago

@cindydeng1998

How would you feel about changing this line to:

        if "sharedaccesskeyname" not in self:
            # log a warning about how this is not a valid connection string for deploying
            return

        if self.connection_string:
            self.shared_access_key_name = self["sharedaccesskeyname"]

You'd also have to create more tests on making sure deployment has a proper error message when sharedaccesskey is not set.

cindydeng1998 commented 3 years ago

Hello @EliiseS, we published an update to iotedgedev to resolve this issue. Please let us know if you have any questions or feedback, thanks!

EliiseS commented 3 years ago

@cindydeng1998 Looks great!

Issue fixed in PR https://github.com/Azure/iotedgedev/pull/508

Closing this issue 😄