Azure / azure-functions-servicebus-extension

Service Bus extension for Azure Functions
MIT License
65 stars 35 forks source link

Azure Function Service Bus Trigger in docker - connection string 'xxxxx' is missing or empty #131

Open grahambunce opened 3 years ago

grahambunce commented 3 years ago

I'm trying to run an AzureFunction Service Bus Trigger in docker. The connection property is set on an Environment variable in startup in the docker file and the credentials needed are passed when creating the container (though eventually these need to be picked up at runtime from within the app as these will be held in Hashicorp Vault

The connection string as a whole really needs to be dynamic too as the service bus namespace is different per environment, but I can't seem to make that dynamic yet as if the configuration setting doesn't exist then the service will fail to start at all - that is part 2 of the same problem, namely:

How to dynamically configure Azure Service Bus Trigger connections at runtime?

i.e.

Dockerfile

ENV microsoft.servicebus.connectionstring=Endpoint=sb://xxxxx.servicebus.windows.net/;SharedAccessKeyName={{messagebus.connection.userid}};SharedAccessKey={{messagebus.connection.password}}

In the startup.cs

            var serviceBusConnectionString = Environment.GetEnvironmentVariable("microsoft.servicebus.connectionstring");
            if (!string.IsNullOrWhiteSpace(serviceBusConnectionString))
            {
                //TODO: Get Hashicorp Vault eventually - for now pull from the ENV set when container created
                var userid = Environment.GetEnvironmentVariable("messagebus.connection.userid");
                var password = Environment.GetEnvironmentVariable("messagebus.connection.password");

                serviceBusConnectionString = serviceBusConnectionString
                    .Replace("{{messagebus.connection.userid}}", userid)
                    .Replace("{{messagebus.connection.password}}", password);

                Environment.SetEnvironmentVariable("microsoft.servicebus.connectionstring", serviceBusConnectionString);
            }

When the host starts in docker I get the a 401 unauthenticated error when trying to get messages of the service bus. I think because it hasn't picked up the updated credentials and modified connection string from startup.csy

Is there a way to dynamically set the connection string and credentials at runtime?

alrod commented 2 years ago

@grahambunce, are you still experiencing the issue?

grahambunce commented 2 years ago

@alrod I assume the problem still exists as it seems to be a flaw/feature of the way azure functions configure themselves.

However I personally am no longer impacted by this because we gave up and stopped using azure functions in docker as they were too opinionated on how they should work.

in the end we created a simple background worker in a container that did the same thing and gave us total flexibility instead.

This doesn’t mean the problem has gone away and this should be closed though as I expect the scenario and the resulting problem is still valid and should be resolved for future users.