Pass cmd line arguments to device application

Peter Bons 151 Reputation points
2020-12-31T09:02:05.613+00:00

I am using docker to build my azure sphere app, according to this. In my app I connect directly to an azure function. Authentication is done using a key and I do not want to store this key in my code as it is available in a public GitHub repo. I have the secret stored in GitHub as an environment variable that I can access during the build phase as seen here. Now I need to somehow pass this secret to the application. So my initial thought is to pass it as an argument to the application as outlined in the docs. But I am stuck on how to proceed with this and to integrate it in the build process.

I think it has to be done using the CmdArgs field in the application manifest but I have no idea how that must be done using the docker approach as outlined in the docs. I need to somehow modify the manifest file during the build phase I guess?

Azure Sphere
Azure Sphere
An Azure internet of things security solution including hardware, operating system, and cloud components.
167 questions
{count} vote

Accepted answer
  1. Willess, Brian 451 Reputation points
    2020-12-31T12:26:13.83+00:00

    @Peter Bons ,

    If your application connects to an Azure IoT Hub or IoT Central, you could use either a device twin property or a direct method to pass the secret to your application.

    The advantage of using a direct method is that you could make passing the secret a one time event. On the device, you could store the secret into persistent/flash memory so that the application will always have access to the secret.

    If you ever "recover" (azsphere device recover) your device the persistent memory will be overwritten, so keep that in mind. In that case you would need to call the direct method again to put your secret back into persistent memory.

    • Blog on using direct methods on Azure Sphere here
    • Example application showing how to use persistent memory on Azure Sphere MutableStorage Example

    The advantage of using a device twin property, is that you would not have to store the secret into flash memory, however your secret would be exposed in the device twin.

    Another option as you're already looking at is to modify your command line arguments in the app_manifest.json file before building/packaging your application. Then in the application modify the code to parse out the new option and use it.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.