0

I have a function app function subscribed to an event grid topic, I get publish failures so the function never executes. I have managed identity Event Grid Data Sender and Event Grid Subscription Reader and EG Topic shows publish Fail as per snapshot below.

I have followed this process to authorize the function app but I get Unauthorized.

Subscription Func App func to EG Topic: enter image description here

enter image description here

Func app Managed Identity is ON and added to the Resource Group where EG and Func app live: enter image description here

enter image description here

Addition of RBAC managed identity for function app further confirmed in roles of func app: enter image description here

Subscribed function to EG Topic in func app:

[FunctionName("broadcast")]
    public static async Task SendMessage(
        [EventGridTrigger] EventGridEvent eventGridEvent,
        [SignalR(HubName = "dttelemetry")] IAsyncCollector<SignalRMessage> signalRMessages,
        ILogger log)
    {
        bool broadcast_alert = false;
        bool broadcast_ledState = false;

        var telemetryMessage = new Dictionary<object, object>();

        log.LogInformation("At top Broadcast");

        var egevent = JsonConvert.DeserializeObject<Dictionary<object, object>>(eventGridEvent.Data.ToString());

        if (eventGridEvent.EventType.Contains("telemetry"))
        {
            partId = "MachinePart1";

            if (!b_debug_contractor) log.LogInformation("At broadcast - EvenType is Telemetry");

            foreach (var telemetryProperty in egevent)
            {
                if (!b_debug_contractor) log.LogInformation("broadcast log at telemetry type:" + telemetryProperty.Key + " - " + telemetryProperty.Value);
                telemetryMessage.Add(telemetryProperty.Key, telemetryProperty.Value);
            }

            try
            {
                //log.LogInformation("Success adding broadcast target with args:" + telemetryMessage.ToString());
                await signalRMessages.AddAsync(
                    new SignalRMessage
                    {
                        Target = "TelemetryMessage",
                        Arguments = new[] { telemetryMessage }
                        //Arguments = new[] { output }
                    });

                log.LogWarning("Success Send telemetry transform data to H2");
            }
            catch (Exception e)
            {
                log.LogInformation("Exception - Failed to send broadcast telemetry data to H2:" + e.Message);
            }
        }
        else if (eventGridEvent.EventType.Contains("Twin.Update"))
        {
            log.LogInformation("At broadcast - EvenType is Twin.Update");

            foreach (var twinProperty in egevent)
            {
                if (!b_debug_contractor)
                    log.LogInformation("broadcast log at Twin.Update type:" + twinProperty.Key + " - " + twinProperty.Value);

                if (twinProperty.Key.ToString() == "/Alert")
                {
                    alert = (bool)twinProperty.Value;
                    broadcast_alert = true;
                    if (!b_debug_contractor) log.LogInformation("I've set broadcast_alert to " + broadcast_alert);
                }
                if (twinProperty.Key.ToString() == "/ledState")
                {
                    ledState = (bool)twinProperty.Value;
                    broadcast_ledState = true;
                    if (!b_debug_contractor)
                        log.LogInformation("I've set broadcast_ledState to " + broadcast_ledState);
                }
            }

            var propertyMessage = new Dictionary<object, object>();
            if (broadcast_alert && broadcast_ledState)
            {
                propertyMessage.Add("PartID", partId);
                propertyMessage.Add("Alert", alert);
                propertyMessage.Add("LedState", ledState);

                if (!b_debug_contractor)
                    log.LogInformation($"SingalRFunction - adding alert's PartID to: {partId}");
                if (!b_debug_contractor)
                    log.LogInformation($"SingalRFunction - adding alert to: {alert}");
                if (!b_debug_contractor)
                    log.LogInformation($"SingalRFunction - adding ledState to: {ledState}");
            }
            else if (broadcast_alert && !broadcast_ledState)
            {
                propertyMessage.Add("PartID", partId);
                propertyMessage.Add("Alert", alert);
                if (!b_debug_contractor)
                    log.LogInformation($"SingalRFunction - adding alert's PartID to: {partId}");
                if (!b_debug_contractor)
                    log.LogInformation($"SingalRFunction - adding alert to: {alert}");
            }
            else if (!broadcast_alert && broadcast_ledState)
            {
                propertyMessage.Add("PartID", partId);
                propertyMessage.Add("LedState", ledState);
                if (!b_debug_contractor)
                    log.LogInformation($"SingalRFunction - adding alert's PartID to: {partId}");
                if (!b_debug_contractor)
                    log.LogInformation($"SingalRFunction - adding ledState to: {ledState}");
            }

            try
            {
                await signalRMessages.AddAsync(
                new SignalRMessage
                {
                    Target = "PropertyMessage",
                    Arguments = new[] { propertyMessage }
                });

                log.LogWarning("Success Send Twin.Update transform data to H2");
            }
            catch (Exception e)
            {
                log.LogInformation("Exception at SingalRFunction Twin.Update: " + e.Message);
            }
        }

        else if (eventGridEvent.EventType.Contains("broadcastTransform"))
        {
            var transformMessage = new Dictionary<object, object>();

            log.LogInformation("At broadcast - EvenType is broadcastTransform");

            foreach (var transformProperty in egevent)
            {
                if (!b_debug_contractor) log.LogInformation("broadcast log at broadcastTransform type:" + transformProperty.Key + " - " + transformProperty.Value);
                transformMessage.Add(transformProperty.Key, transformProperty.Value);
            }
            try
            {
                //log.LogInformation("Success adding broadcast target with args:" + telemetryMessage.ToString());
                await signalRMessages.AddAsync(
                new SignalRMessage
                {
                    Target = "TransformMessage",
                    //Arguments = new[] { telemetryMessage }
                    Arguments = new[] { transformMessage }
                });

                log.LogWarning("Success Send broadcast transform data to H2.");
            }
            catch (Exception e)
            {
                log.LogInformation("Exception - Failed to send broadcastTransform data" + e.Message);
            }
        }
        else
            log.LogInformation("At Broadcast - Type not Recognized");
    }

Unauth Error subscribed function:

{ "time": "2023-01-04T08:25:27.3783585Z", "resourceId": "/SUBSCRIPTIONS/XXXX/RESOURCEGROUPS/MY-RG/PROVIDERS/MICROSOFT.EVENTGRID/TOPICS/eventgridtopicname", "category": "PublishFailures", "operationName": "Post", "message": "inputEventsCount=null, requestUri=https://eventgridtopicname.eastus-1.eventgrid.azure.net/api/events, publisherInfo=publisherName=eventgridtopicname.EASTUS-1.EVENTGRID.AZURE.NET, category=User, inputSchema=EventGridEvent, armResourceId=/subscriptions/XXX/resourceGroups/my-rg/providers/Microsoft.EventGrid/topics/eventgridtopicname, filteringPolicy:DnsHost, emitAuditLogs=False, drBoundary=WithinGeopair, regionCategory=Primary, isPublishBlockedDueToDr=False, httpStatusCode=Unauthorized, errorType=Unauthorized, errorMessage=The request authorization key is not authorized for eventgridtopicname.EASTUS-1.EVENTGRID.AZURE.NET."}

Sergio Solorzano
  • 476
  • 9
  • 29

1 Answers1

0

I re-did the whole project and now it works. It's very unfortunate the setup doesn't allow for better debugging to solve a problem.

Sergio Solorzano
  • 476
  • 9
  • 29