Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to restart the custom edge module while using the inbuilt "RestartModule" direct method of $edgeAgent from custom iot edge module. #7116

Closed
malavvakharia opened this issue Sep 25, 2023 · 6 comments

Comments

@malavvakharia
Copy link

malavvakharia commented Sep 25, 2023

Expected Behavior

We can restart the custom module by invoking the $edgeAgent "RestartModule" direct method.But as of now it will be giving the error.

image

Current Behavior

See the image attached image for current behavior.

Steps to Reproduce

Provide a detailed set of steps to reproduce the bug.

  1. I am using the .NET sdk for invoking the inbuilt direct method of $edgeAgent.(Note: Calling the inbuilt direct method from the custom module not from $edgeAgent itself.)
  2. Code snippet (C#):
var DataAsJson = @"{
                    ""schemaVersion"": ""1.0"",
                    ""id"": ""edgemodule1""
                }";
                var methodRequest = new MethodRequest("RestartModule", Encoding.UTF8.GetBytes(DataAsJson), TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30));
              
                var result = await _moduleClient.InvokeMethodAsync("DeviceId","$edgeAgent", methodRequest);
  1. After deploying the module when the direct method calls it will give the error as shown in image.
@malavvakharia malavvakharia changed the title Not able to restart the custom edge module while using the inbuilt "RestartModule" direct method of $edgeAgent. Not able to restart the custom edge module while using the inbuilt "RestartModule" direct method of $edgeAgent from custom iot edge module. Sep 25, 2023
@rido-min
Copy link
Member

@malavvakharia

edgeAgent methods can only be invoked from the service side (Portal, CLI, program with service credentials, etc.)

from : https://stackoverflow.com/questions/63806134/how-to-call-direct-method-on-iot-edge-agent-from-another-module#comment112887745_63815176

@malavvakharia
Copy link
Author

@malavvakharia

edgeAgent methods can only be invoked from the service side (Portal, CLI, program with service credentials, etc.)

from : https://stackoverflow.com/questions/63806134/how-to-call-direct-method-on-iot-edge-agent-from-another-module#comment112887745_63815176

@rido-min, That I already know. But I don't know how to get the service credential and also, it's supported by .NET sdk or not. Do you have any idea regarding that? The same link I have shared with one of the Microsoft Q & A page.(In the comment section you can read the conversion of mine.)

https://learn.microsoft.com/en-us/answers/questions/1019196/how-to-restart-the-edgehub-module-using-a-custom-i#comment-1379328

Thanks.

@rido-min
Copy link
Member

yes, this API is exposed as a REST endpoint

https://learn.microsoft.com/en-us/rest/api/iothub/service/modules/invoke-method

but keep in mind this requires the iothub credentials (from portal->Security->Shared access policies).

Using those key from the device side is strongly discouraged, since if the key is leaked, it will have access to all your devices and modules.

.. used from the az cli,

https://learn.microsoft.com/en-us/cli/azure/iot/hub?view=azure-cli-latest#az-iot-hub-invoke-module-method(azure-iot)

and the service side sdk

https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.devices.serviceclient.invokedevicemethodasync?view=azure-dotnet#microsoft-azure-devices-serviceclient-invokedevicemethodasync(system-string-system-string-microsoft-azure-devices-cloudtodevicemethod-system-threading-cancellationtoken)

@malavvakharia
Copy link
Author

yes, this API is exposed as a REST endpoint

https://learn.microsoft.com/en-us/rest/api/iothub/service/modules/invoke-method

but keep in mind this requires the iothub credentials (from portal->Security->Shared access policies).

Using those key from the device side is strongly discouraged, since if the key is leaked, it will have access to all your devices and modules.

.. used from the az cli,

https://learn.microsoft.com/en-us/cli/azure/iot/hub?view=azure-cli-latest#az-iot-hub-invoke-module-method(azure-iot)

and the service side sdk

https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.devices.serviceclient.invokedevicemethodasync?view=azure-dotnet#microsoft-azure-devices-serviceclient-invokedevicemethodasync(system-string-system-string-microsoft-azure-devices-cloudtodevicemethod-system-threading-cancellationtoken)

@rido-min ,

The second one I have already tried but got the unauthorize access error. And regarding the first one as you mentioned that's not the best practice as there is a high chance of security issue if key will be leaked.

If you want will provide sample code for second one and also screen shot of the issue.

@rido-min
Copy link
Member

following in the sdk repo
Azure/azure-iot-sdk-csharp#3373

@malavvakharia
Copy link
Author

following in the sdk repo Azure/azure-iot-sdk-csharp#3373

@rido-min, have you close the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants