pm7y / AzureEventGridSimulator

A simulator that provides endpoints to mimic the functionality of Azure Event Grid topics and subscribers and is compatible with the Azure.Messaging.EventGrid client library.
MIT License
82 stars 39 forks source link

Running the emulator on Docker #106

Closed rrr-michael-aquilina closed 1 year ago

rrr-michael-aquilina commented 2 years ago

Hi all,

I am wondering if anyone has had luck running the emulator on docker? I am running docker on WSL2 am have been facing issues connecting to the endpoints.

Using the postman collection provided, I receive Error: connect ECONNREFUSED 127.0.0.1:60101. I have run the command to generate the certificates but am still facing issues.

Is there a more detailed example anywhere?

AndreyBespamyatnov commented 2 years ago

Hello @rrr-michael-aquilina I did that, and all works fine on my side.

  1. copy the source to the AzureEventGridSimulator folder
  2. here is my docker file:
    
    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 . COPY YOUR_CERT.com.pfx . COPY appsettings.json .

USER ContainerAdministrator ENV ASPNETCORE_KestrelCertificatesDefaultPassword="CERT_PASSWORD" ENV ASPNETCORE_KestrelCertificatesDefaultPath="C:\app\YOUR_CERT.com.pfx" ENV ASPNETCORE_ENVIRONMENT=Development

ENTRYPOINT ["AzureEventGridSimulator.exe"]

3. create example.appsettings.json and add your settings
4. build your image: 
`docker build -t eventgrid_image_name_tag .`
5. run container, you can specify the configuration file, you have to map you config file. ${PWD} - your curretn folder. C:\temp\ - folder inside the container:

docker run -dit --rm -v ${PWD}:C:\temp\ --name eventgrid-dev eventgrid_image_name_tag --entrypoint AzureEventGridSimulator.exe --ConfigFile=C:\temp\appsettings.json

rrr-michael-aquilina commented 2 years ago

Thanks @AndreyBespamyatnov,

I'm using the image from docker hub.

This is the snippet of my docker compose which is pretty much untouched from the example.

    image: pmcilreavy/azureeventgridsimulator
    container_name: azureeventgridsimulator-dev
    ports:
      # add a port mapping for each topic in the settings file
     - "60101:60101"
    volumes:
      # map a local folder './docker' to a read-only folder '/aegs' in the container
      # this allows us to access files (e.g. settings or certificates) from within the container
      - .:/workspace:cached
    #entrypoint: ["sh", "/workspace/infra/playpen/functions/wait-for-and-trust-eventgrid.sh"]
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      # specify cert details (note: can be generated like so: dotnet dev-certs https --export-path ./docker/azureEventGridSimulator.pfx --password Y0urSup3rCrypt1cPa55w0rd!`
      - ASPNETCORE_Kestrel__Certificates__Default__Path=/usr/local/share/ca-certificates/azureEventGridSimulator.pfx
      - ASPNETCORE_Kestrel__Certificates__Default__Password=Y0urSup3rCrypt1cPa55w0rd!

      # example of how to configure a topic via environment variables
      - AEGS_Topics__0__name=ExampleTopic
      - AEGS_Topics__0__port=60101
      - AEGS_Topics__0__key=TheLocal+DevelopmentKey=

      # add an Azure Function subscriber running on localhost (host.docker.internal)
      - AEGS_Topics__0__subscribers__1__name=AzureFunctionSubscription
      - AEGS_Topics__0__subscribers__1__endpoint=http://host.docker.internal:7071/runtime/webhooks/EventGrid?functionName=Publish_Device_Event
      - AEGS_Topics__0__subscribers__1__disableValidation=false
      # logging configuration
      - AEGS_Serilog__MinimumLevel__Default=Verbose

      # you could also define topics/subscribers via via a configfile
      # - ConfigFile=/aegs/appsettings.docker.json

When I start the container and my function, I can see that that the validate is successful. However, anytime I try post an event from within the container, I get connection refused. I think the issue lies with the certificate but I'm a bit stuck.

dotnet dev-certs https --trust

The above command doesnt work within the docker workspace as its Linux. If I try the below, it says a valid HTTPS cert already exists.

dotnet dev-certs https --export-path ./docker/azureEventGridSimulator.pfx --password Y0urSup3rCrypt1cPa55w0rd!

Any thoughts?

AndreyBespamyatnov commented 2 years ago

Thanks @AndreyBespamyatnov,

I'm using the image from docker hub.

Yeah, sounds like the SSL issue, have you checked that you added your certs to the trust store in the docker and locally?

rrr-michael-aquilina commented 2 years ago

I have tried to create the cert and add to docker but still no luck.

This is what I tried so far.

  1. Manually generate self-signed cert

image

  1. Added to /usr/local/share/ca-certificates/

  2. Ran update-ca-certificates

image

After doing the above and running curl CMD, i still get the same connection refused error.

image

Am I doing something obviously wrong?

pm7y commented 2 years ago

Hi, can you try this...

1) In Docker... open the terminal for the running container...

image

2) navigate to the folder containing your cert (in the default example script this will be the aegs folder...

image

3) when you ls in that folder you should see your certificate there. If you don't then something has gone wrong with the volume binding. The simulator won't be able to find your cert and therefore can't accept any SSL connections.

In your example it looks like you are mounting volume . but then the certificate you are referencing is in /usr/local/share/ca-certificates/ - is this folder definitely being shared? perhaps navigate to this folder and check it contains the cert as you expect.

michalpawlik93 commented 1 year ago

Hey, I had same issue and followed @pmcilreavy post to investigate if my cert is inside docker. That was handy. Once cert appeared inside docker, all started working.

I see differences between our configurations. Take a look, it may help you:

  1. Try to change volumes to: volumes:

    • ./docker:/aegs:ro
  2. Env variable to:

    • ASPNETCORE_KestrelCertificatesDefault__Path=/aegs/azureEventGridSimulator.pfx
  3. Execute command to get your cert: dotnet dev-certs https --export-path ./docker/azureEventGridSimulator.pfx --password

pm7y commented 1 year ago

I will close this as there has been no further activity. Hopefully, your issue is now resolved if you follow my suggestion above.