Use case
I have an Iot Hub device which sends telemetry data to IoT Hub. I want to process the telemetry, e.g storing to a database, using a Function.
Funtion
I created the following function in VS2019 and published it to Azure:
[FunctionName("HttpTrigger")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")]HttpRequestMessage req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
var messages = await req.Content.ReadAsAsync<JArray>();
// If the request is for subscription validation, send back the validation code.
if (messages.Count > 0 && string.Equals((string)messages[0]["eventType"],
"Microsoft.EventGrid.SubscriptionValidationEvent",
System.StringComparison.OrdinalIgnoreCase))
{
log.LogInformation("Validate request received");
return req.CreateResponse<object>(new
{
validationResponse = messages[0]["data"]["validationCode"]
});
}
// The request is not for subscription validation, so it's for one or more events.
foreach (JObject message in messages)
{
// Handle one event.
EventGridEvent eventGridEvent = message.ToObject<EventGridEvent>();
log.LogInformation($"Subject: {eventGridEvent.Subject}");
log.LogInformation($"Time: {eventGridEvent.EventTime}");
log.LogInformation($"Event data: {eventGridEvent.Data.ToString()}");
}
return req.CreateResponse(HttpStatusCode.OK);
}
Event Subscription
In IoT Hub I created an Event Subscription which triggers the Function, using the Web Hook Endpoint type.
The problem
The body of the event data seems to be encrypted (?):
{{
"properties": {},
"systemProperties": {
"iothub-connection-device-id": "smartmeter",
"iothub-connection-auth-method": "{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
"iothub-connection-auth-generation-id": "637057961942743477",
"iothub-enqueuedtime": "2019-10-05T08:09:17.973Z",
"iothub-message-source": "Telemetry"
},
"body": "eyJEYXRlVGltZSI6IjIwMTktMTAtMDVUMTA6MDk6MjkiLCJBY3R1YWxUYXJyaWYiOjEsIkFjdHVhbFBvd2VyRGVsaXZlcmVkIjoyNzEuMCwiVG90YWxFbGVjdHJpY2l0eURlbGl2ZXJlZFRhcnJpZjEiOjYwMTU1NzcuMCwiVG90YWxFbGVjdHJpY2l0eURlbGl2ZXJlZFRhcnJpZjIiOjYwMjc5NTIuMH0="
}}
though in the Cloud Shell I can see the readable data. I can also see the readable data by reading the device to cloud messages with the EventHubClient in .Net.
What am I missing? How can I decrypt the body?