Azure事件网格 - Azure功能的事件传递安全性

时间:2018-06-18 13:10:39

标签: azure azure-eventgrid

我一直在研究与Azure功能Azure集成的Azure事件网格相关的POC。如上所述here,我被困在事件传递安全性上。

我正在使用Event Grid Trigger,它由Azure Blob存储中的内置事件网格订阅发送。我在WebHook端点中添加了一个访问令牌作为查询参数,如上面的URL所述。

但我无法在功能代码中访问该参数。有人可以共享样本吗?

仅供参考 - 以下是我的代码中的函数定义。

[FunctionName("EventGridFunc")]
 public static void Run([EventGridTrigger]EventGridEvent eventGridEvent, 
 TraceWriter log)
 {
   log.Info("Received a trigger.");
   log.Info(eventGridEvent.Data.ToString());
 }

1 个答案:

答案 0 :(得分:0)

EventGridTrigger函数的完整subscriberUrl具有以下格式:

#r "Newtonsoft.Json"

using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;
using System.Threading.Tasks;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, IDictionary<string, string> query, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed an EventGrid request.");

    log.Info($"\nHeaders:\n\t{string.Join("\n\t", req.Headers.Where(i => i.Key.StartsWith("aeg-")).Select(i => $"{i.Key}={i.Value.First()}"))}");
    log.Info($"\nQuery:\n\t{string.Join("\n\t", query.Select(i => $"{i.Key}={i.Value}"))}");

    string eventGridValidationHeader = req.Headers.FirstOrDefault( x => string.Compare(x.Key,"Aeg-Event-Type", true) == 0).Value?.FirstOrDefault().Trim();

    // media type = application/json or application/cloudevents+json 
    string jsontext = null;
    var jtoken = JToken.Parse(await req.Content.ReadAsStringAsync());
    log.Info($"\n{jtoken.ToString(Newtonsoft.Json.Formatting.Indented)}");

    if (jtoken is JArray)
        jsontext = jtoken.SingleOrDefault<JToken>().ToString();
    else if (jtoken is JObject)
        jsontext = jtoken.ToString();

    var eventGridEvent = JsonConvert.DeserializeAnonymousType(jsontext, new { EventType = "", Data = new JObject()});
    if (string.IsNullOrEmpty(eventGridValidationHeader) || string.IsNullOrEmpty(eventGridEvent?.EventType) || eventGridEvent?.Data == null)
    {
        return req.CreateErrorResponse(HttpStatusCode.BadRequest, "No EventGrid message.");
    }

    if (eventGridValidationHeader == "SubscriptionValidation" && eventGridEvent.EventType == "Microsoft.EventGrid.SubscriptionValidationEvent")
    {
        log.Verbose(@"Event Grid Validation event received.");
        return req.CreateResponse(HttpStatusCode.OK, JsonConvert.DeserializeObject(JsonConvert.SerializeObject(new { validationResponse = ((dynamic)eventGridEvent.Data).validationCode })));               
    }

    #region Event Processing 

    // for testing a retry delivery policy 
    //return req.CreateResponse(HttpStatusCode.BadRequest, "Testing");

    #endregion

    return req.CreateResponse(HttpStatusCode.NoContent);    
}

如您所见,EventGridTrigger基本上是一个特殊的HttpTrigger(推送)函数,其事件消息的“隐藏预处理”用于其验证。

<强>更新

我没有看到如何在EventGridTrigger中获取查询字符串的方法。但是,您的解决方案很少有解决方法,例如:

  • 使用应用程序设置
  • 使用Azure密钥保管库存储密钥
  • 使用HttpTrigger而不是EventGridTrigger

以下代码段显示了EventGrid(版本2018-05-01-preview)订阅者的HttpTrigger函数示例:

>> fig, ax1 = plt.subplots()
>> ax1.plot(x1, y1)
>> fig, ax2 = plt.subplots()
>> ax2.plot(x2, y2)