handle subscription verification

This commit is contained in:
ShunXian Cai 2017-07-27 11:23:48 -07:00
Родитель f303b86b28
Коммит e5a058a92c
3 изменённых файлов: 91 добавлений и 68 удалений

Просмотреть файл

@ -3,6 +3,7 @@ using Microsoft.Azure.WebJobs.Host.Executors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
@ -48,16 +49,44 @@ namespace Microsoft.Azure.WebJobs
}
private async Task<HttpResponseMessage> ProcessAsync(HttpRequestMessage req)
{
var functionName = HttpUtility.ParseQueryString(req.RequestUri.Query)["functionName"];
if (String.IsNullOrEmpty(functionName) || !_listeners.ContainsKey(functionName))
{
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
IEnumerable<string> eventTypeHeaders = null;
string eventTypeHeader = null;
if (req.Headers.TryGetValues("aeg-event-type", out eventTypeHeaders))
{
eventTypeHeader = eventTypeHeaders.First();
}
if (String.Equals(eventTypeHeader, "SubscriptionValidation", StringComparison.OrdinalIgnoreCase))
{
string jsonArray = await req.Content.ReadAsStringAsync();
SubscriptionValidationEvent validationEvent = null;
try
{
List<EventGridEvent> events = JsonConvert.DeserializeObject<List<EventGridEvent>>(jsonArray);
validationEvent = events[0].Data.ToObject<SubscriptionValidationEvent>();
}
catch (JsonException)
{
// TODO remove once validation use JObject
List<EventGridFaultyEvent> events = JsonConvert.DeserializeObject<List<EventGridFaultyEvent>>(jsonArray);
validationEvent = JsonConvert.DeserializeObject<SubscriptionValidationEvent>(events[0].Data);
}
SubscriptionValidationResponse validationResponse = new SubscriptionValidationResponse { ValidationResponse = validationEvent.ValidationCode };
var returnMessage = new HttpResponseMessage(HttpStatusCode.OK);
returnMessage.Content = new StringContent(JsonConvert.SerializeObject(validationResponse));
return returnMessage;
}
else if (String.Equals(eventTypeHeader, "Notification", StringComparison.OrdinalIgnoreCase))
{
string jsonArray = await req.Content.ReadAsStringAsync();
List<EventGridEvent> events = JsonConvert.DeserializeObject<List<EventGridEvent>>(jsonArray);
var functionName = HttpUtility.ParseQueryString(req.RequestUri.Query)["functionName"];
if (_listeners.ContainsKey(functionName))
{
// TODO echo back if its a subscription verification
// return different httpResponseMessage
var listener = _listeners[functionName];
foreach (var ev in events)
{
@ -66,13 +95,19 @@ namespace Microsoft.Azure.WebJobs
TriggerValue = ev
};
await listener.Executor.TryExecuteAsync(triggerData, CancellationToken.None);
await _listeners[functionName].Executor.TryExecuteAsync(triggerData, CancellationToken.None);
}
return new HttpResponseMessage(HttpStatusCode.Accepted);
}
else if (String.Equals(eventTypeHeader, "Unsubscribe", StringComparison.OrdinalIgnoreCase))
{
// TODO disable function?
return new HttpResponseMessage(HttpStatusCode.Accepted);
}
return new HttpResponseMessage(HttpStatusCode.BadRequest);
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
}
}

Просмотреть файл

@ -8,6 +8,18 @@ using System.Threading.Tasks;
namespace Microsoft.Azure.WebJobs
{
public class SubscriptionValidationResponse
{
[JsonProperty(PropertyName = "validationResponse")]
public string ValidationResponse { get; set; }
}
public class SubscriptionValidationEvent
{
[JsonProperty(PropertyName = "validationCode")]
public string ValidationCode { get; set; }
}
public class StorageBlob
{
/*
@ -52,6 +64,41 @@ namespace Microsoft.Azure.WebJobs
}
public class EventGridFaultyEvent
{
/*
{
'id': 'eac180e8-92e0-436d-8699-a0324e2a5fef',
'topic': '/subscriptions/5b4b650e-28b9-4790-b3ab-ddbd88d727c4/resourceGroups/canaryeh/providers/microsoft.eventhub/namespaces/canaryeh',
'subject': 'eventhubs/test',
'data': '{\""validationCode\"":\""85fe9560-f63f-469b-b40a-5a6327db05e6\""}', <-- String instead of JObject
'eventType': 'Microsoft.EventGrid/SubscriptionValidationEvent',
'eventTime': '2017-07-28T00:43:28.6153503Z'
}
*/
[JsonProperty(PropertyName = "topic")]
public string Topic { get; set; }
[JsonProperty(PropertyName = "subject")]
public string Subject { get; set; }
[JsonProperty(PropertyName = "data")]
public string Data { get; set; }
[JsonProperty(PropertyName = "eventType")]
public string EventType { get; set; }
[JsonProperty(PropertyName = "publishTime")]
public DateTime PublishTime { get; set; }
[JsonProperty(PropertyName = "eventTime")]
public DateTime EventTime { get; set; }
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
}
public class EventGridEvent
{
/*

Просмотреть файл

@ -1,59 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net461" />
<package id="Microsoft.Azure.WebJobs" version="2.1.0-beta1" targetFramework="net461" />
<package id="Microsoft.Azure.WebJobs.Core" version="2.1.0-beta1" targetFramework="net461" />
<package id="Microsoft.Data.Edm" version="5.6.4" targetFramework="net461" />
<package id="Microsoft.Data.OData" version="5.6.4" targetFramework="net461" />
<package id="Microsoft.Data.Services.Client" version="5.6.4" targetFramework="net461" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="1.1.1" targetFramework="net461" />
<package id="Microsoft.NETCore.Platforms" version="1.1.0" targetFramework="net461" />
<package id="Microsoft.Win32.Primitives" version="4.3.0" targetFramework="net461" />
<package id="NETStandard.Library" version="1.6.1" targetFramework="net461" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net461" />
<package id="System.AppContext" version="4.3.0" targetFramework="net461" />
<package id="System.Collections" version="4.3.0" targetFramework="net461" />
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net461" />
<package id="System.Console" version="4.3.0" targetFramework="net461" />
<package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net461" />
<package id="System.Diagnostics.DiagnosticSource" version="4.3.0" targetFramework="net461" />
<package id="System.Diagnostics.Tools" version="4.3.0" targetFramework="net461" />
<package id="System.Diagnostics.Tracing" version="4.3.0" targetFramework="net461" />
<package id="System.Globalization" version="4.3.0" targetFramework="net461" />
<package id="System.Globalization.Calendars" version="4.3.0" targetFramework="net461" />
<package id="System.IO" version="4.3.0" targetFramework="net461" />
<package id="System.IO.Compression" version="4.3.0" targetFramework="net461" />
<package id="System.IO.Compression.ZipFile" version="4.3.0" targetFramework="net461" />
<package id="System.IO.FileSystem" version="4.3.0" targetFramework="net461" />
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.Linq" version="4.3.0" targetFramework="net461" />
<package id="System.Linq.Expressions" version="4.3.0" targetFramework="net461" />
<package id="System.Net.Http" version="4.3.0" targetFramework="net461" />
<package id="System.Net.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.Net.Sockets" version="4.3.0" targetFramework="net461" />
<package id="System.ObjectModel" version="4.3.0" targetFramework="net461" />
<package id="System.Reflection" version="4.3.0" targetFramework="net461" />
<package id="System.Reflection.Extensions" version="4.3.0" targetFramework="net461" />
<package id="System.Reflection.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.Handles" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.Numerics" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net461" />
<package id="System.Spatial" version="5.6.4" targetFramework="net461" />
<package id="System.Text.Encoding" version="4.3.0" targetFramework="net461" />
<package id="System.Text.Encoding.Extensions" version="4.3.0" targetFramework="net461" />
<package id="System.Text.RegularExpressions" version="4.3.0" targetFramework="net461" />
<package id="System.Threading" version="4.3.0" targetFramework="net461" />
<package id="System.Threading.Tasks" version="4.3.0" targetFramework="net461" />
<package id="System.Threading.Timer" version="4.3.0" targetFramework="net461" />
<package id="System.Xml.ReaderWriter" version="4.3.0" targetFramework="net461" />
<package id="System.Xml.XDocument" version="4.3.0" targetFramework="net461" />
<package id="WindowsAzure.Storage" version="7.2.1" targetFramework="net461" />
</packages>