A simulator that provides endpoints to mimic the functionality of Azure Event Grid topics and subscribers and is compatible with the Microsoft.Azure.EventGrid
client library.
Topics and their subscribers are configured in the appsettings.json
file. Alternatively, you can specify the configuration file to use by setting the ConfigFile
command line argument, e.g.
AzureEventGridSimulator.exe --ConfigFile=/path/to/config.json
You can add multiple topics. Each topic must have a unique port. Each topic can have multiple subscribers. An example of one topic with one subscriber is shown below.
{
"topics": [
{
"name": "MyAwesomeTopic",
"port": 60101,
"key": "TheLocal+DevelopmentKey=",
"subscribers": [
{
"name": "LocalAzureFunctionSubscription",
"endpoint": "http://localhost:7071/runtime/webhooks/EventGrid?functionName=PersistEventToDb",
"disableValidation": true
}
]
}
]
}
name
: The name of the topic. It can only contain letters, numbers, and dashes.port
: The port to use for the topic endpoint. The topic will listen onhttps://0.0.0.0:{port}/
.key
: The key that will be used to validate theaeg-sas-key
oraeg-sas-token
header in each request. If this is not supplied then no key validation will take place.subscribers
: The subscriptions for this topic.
name
: The name of the subscriber. It can only contain letters, numbers, and dashes.endpoint
: The subscription endpoint url. Events received by topic will be sent to this address.disableValidation
:false
(the default) subscription validation will be attempted each time the simulator starts.true
to disable subscription validation.
When a subscription is added to Azure Event Grid it first sends a validation event to the subscription endpoint. The validation event contains a validationCode
which the subscription endpoint must echo back. If this does not occur then Azure Event Grid will not enable the subscription.
More information about subscription validation can be found at https://docs.microsoft.com/en-us/azure/event-grid/webhook-event-delivery.
The Azure Event Grid Simualator will mimick this validation behaviour at start up but it can be disabled using the disableValidation
setting (above).
Event filtering is configurable on each subscriber using the filter model defined here: https://docs.microsoft.com/en-us/azure/event-grid/event-filtering. This page provides a full guide to the configuration options available and all parts of this guide are currently supported. For ease of transition, explicit limitations have also been adhered to. The restrictions mentioned have been further modified (https://azure.microsoft.com/en-us/updates/advanced-filtering-generally-available-in-event-grid/) and these new less restrictive filtering limits have been observed.
Extending the example above to include a basic filter which will only deliver events to the subscription if they are of a specific type is illustrated below.
{
"topics": [
{
"name": "MyAwesomeTopic",
"port": 60101,
"key": "TheLocal+DevelopmentKey=",
"subscribers": [
{
"name": "LocalAzureFunctionSubscription",
"endpoint": "http://localhost:7071/runtime/webhooks/EventGrid?functionName=PersistEventToDb",
"filter": {
"includedEventTypes": ["my.eventType"]
}
}
]
}
]
}
This can be extended to allow subject filtering:
"filter": {
"subjectBeginsWith": "/blobServices/default/containers/mycontainer/log",
"subjectEndsWith": ".jpg",
"isSubjectCaseSensitive": true
}
or advanced filtering:
"filter": {
"advancedFilters": [
{
"operatorType": "NumberGreaterThanOrEquals",
"key": "Data.Key1",
"value": 5
},
{
"operatorType": "StringContains",
"key": "Subject",
"values": ["container1", "container2"]
}
]
}
You can use that emulater within the Docker, here example how to use it:
FROM mcr.microsoft.com/dotnet/sdk:3.1 as build
WORKDIR /source
# restores nuget packages
COPY AzureEventGridSimulator/src/AzureEventGridSimulator/*.csproj .
RUN dotnet restore
# copy source code
COPY AzureEventGridSimulator/src/AzureEventGridSimulator .
# builds the source code using the SDK
RUN dotnet publish -c release -o /app
# runs the deployable on a separate image
# that is shipped with the .NET Runtime
FROM mcr.microsoft.com/dotnet/aspnet:3.1
WORKDIR /app
COPY --from=build /app .
USER ContainerAdministrator
# if certificate is needed
#COPY YOUR_KEY_HERE.pfx .
#ENV ASPNETCORE_Kestrel__Certificates__Default__Password="YOUR_KEY_PASSWORD_HERE"
#ENV ASPNETCORE_Kestrel__Certificates__Default__Path="C:\\app\\YOUR_KEY_HERE.pfx"
ENV ASPNETCORE_ENVIRONMENT=Development
ENTRYPOINT ["AzureEventGridSimulator.exe"]
docker build -t {TAG_NAME} .
docker run -it --rm {TAG_NAME}
Alternatively, you can specify the configuration file, you have to map you config file
${PWD} - your current folder
C:\temp\ - folder inside the container
docker run -it --rm -v ${PWD}:C:\temp\ {TAG_NAME} --entrypoint AzureEventGridSimulator.exe --ConfigFile=C:\temp\{NAME OF YOUR CONFIGURATION FILE}
Once configured and running, requests are posted
to a topic endpoint. The endpoint of a topic will be in the form: https://localhost:<configured-port>/api/events?api-version=2018-01-01
.
curl -k -H "Content-Type: application/json" -H "aeg-sas-key: TheLocal+DevelopmentKey=" -X POST "https://localhost:60101/api/events?api-version=2018-01-01" -d @Data.json
Data.json
[
{
"id": "8727823",
"subject": "/example/subject",
"data": {
"MyProperty": "This is my awesome data!"
},
"eventType": "Example.DataType",
"eventTime": "2019-01-01T00:00:00.000Z",
"dataVersion": "1"
}
]
An example request that you can import into Postman can be found in the AzureEventGridSimulator repo here https://github.com/pmcilreavy/AzureEventGridSimulator/blob/master/src/Azure%20Event%20Grid%20Simulator.postman_collection.json.
var client = new EventGridClient(new TopicCredentials("TheLocal+DevelopmentKey="));
await client.PublishEventsWithHttpMessagesAsync(
topicHostname: "localhost:60101",
events: new List<EventGridEvent> { <your event> });
Azure Event Grid only accepts connections over https and so the simulator only supports https too. The simulator uses the dotnet development certificate to secure each topic port. You can ensure that this certifcate is installed and trusted by running the following command.
dotnet dev-certs https --trust
A topic can have 0 to n subscribers. When a request is received for a topic, the events will be forwarded to each of the subscribers with the addition of an aeg-event-type: Notification
header. If the message contains multiple events, they will be sent to each subscriber one at a time inline with the Azure Event Grid behaviour. "Event Grid sends the events to subscribers in an array that has a single event. This behavior may change in the future." https://docs.microsoft.com/en-us/azure/event-grid/event-schema
The simulator supports both: aeg-sas-key
or aeg-sas-token
request headers. Using aeg-sas-key
is the simplest way. Just set the value of the aeg-sas-key
to the same key
value configured for the topic. Using an aeg-sas-token
is more secure as the key
is hashed but it's a bit trickier to set up. More information on sas token
can be found here https://docs.microsoft.com/en-us/azure/event-grid/security-authentication#sas-tokens.
If the incoming request contains either an aeg-sas-token
or an aeg-sas-key
header and there is a Key
configured for the topic then the simulator will validate the key and reject the request if the value in the header is not valid.
If you want to skip the validation then set the Key
to null in appsettings.json
.
Azure Event Grid imposes certain size limits to the overall message body and to the each individual event. The overall message body must be <= 1Mb and each individual event must be <= 64Kb. These are the advertised size limits. My testing has shown that the actual limits are 1.5Mb and 65Kb.
Ensures that the properties of each event meets the minimum requirements.
Field | Description |
---|---|
Id | Must be a string. Not null or whitespace. |
Subject | Must be a string. Not null or whitespace. |
EventType | Must be a string. Not null or whitespace. |
EventTime | Must be a valid date/time. |
MetadataVersion | Must be null or 1 . |
Topic | Leave null or empty. Event Grid will populate this field. |
DataVersion | Optional. e.g. 1 . |
Data | Optional. Any custom object. |
There are a couple of similar projects out there. What I found though is that they don't adequately simulate an actual Event Grid Topic endpoint.
Azure Event Grid only excepts connections over https and the Microsoft.Azure.EventGrid
client only sends requests over https. If you're posting events to an Event Grid topic using custom code then maybe this isn't an issue. If you are using the client library though then any test endpoint must be https.
Typically an event grid topic endpoint url is like so: https://topic-name.location-name.eventgrid.azure.net/api/events. Note that all the information needed to post to a topic is contained in the host part. The Microsoft.Azure.EventGrid
client will essentially reduce the url you give it down to just the host part and prefix it with https (regardless of the original scheme).
It posts the payload to https://host:port and drops the query uri. All of the existing simulator/ emulator projects I found don't support https and use a the query uri to distinguish between the topics. This isn't compatible with the Microsoft.Azure.EventGrid
client.
Some features that could be added if there was a need for them: -
- Subscriber retries & dead lettering. https://docs.microsoft.com/en-us/azure/event-grid/delivery-and-retry
- Certificate configuration in
appsettings.json
. - Subscriber token auth
- Better Docker support.
- Maybe a web based console for admin stats etc.