Open tjrobinson opened 6 years ago
Another approach, which also doesn't seem to help:
[assembly: WebJobsStartup(typeof(Startup))]
namespace Empactis.CaseManager.AzureFunctions
{
internal class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.AddCosmosDB(
options => options.ConnectionString = "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");
}
}
}
Could this be moved to https://github.com/Azure/azure-webjobs-sdk-extensions/ ?
Response from the Cosmos DB team:
Dynamic resolution of values in the Functions Bindings and Triggers (in all Bindings and Triggers, not only Cosmos DB) is not currently supported.
If you work with Webjobs though, it should be possible to wire up a KeyVault configuration provider to read your secrets from KeyVault and populate that configuration. I’ve not actually tried it, but here’s docs on wiring it up: https://docs.microsoft.com/en-us/aspnet/core/security/key-vault-configuration?view=aspnetcore-2.1.
Has a work around for this been determined? The idea that we're forced to store secrets in config files or management them in the function portal experience is a deal breaker. We store all secrets in key vault. No exceptions. So unless we can configure this at runtime, we're totally blocked.
@ThinkFr33ly This preview feature may help you: https://azure.microsoft.com/en-us/blog/simplifying-security-for-serverless-and-web-apps-with-azure-functions-and-app-service/
See also: https://docs.microsoft.com/en-gb/azure/app-service/app-service-key-vault-references
@ThinkFr33ly This preview feature may help you: https://azure.microsoft.com/en-us/blog/simplifying-security-for-serverless-and-web-apps-with-azure-functions-and-app-service/
See also: https://docs.microsoft.com/en-gb/azure/app-service/app-service-key-vault-references
@tjrobinson this is great. Had no idea you could do that. I actually managed to get this working using the web job startup class and just setting the configuration value manually. I prefer this method because it keeps my configuration system consistent across all aspects of my app - .config or .json files never have secrets, they only have "secret URIs" and then I grab those using a secret resolver / key vault. This leverages the MSI (probably exactly how your example is doing it).
The approach mentioned here requires the Secret URI to be used. https://azure.microsoft.com/en-us/blog/simplifying-security-for-serverless-and-web-apps-with-azure-functions-and-app-service/.. In our case we don't have access to the Keyvault in prod to get the URI. Are there any options to set the Cosmos DB Trigger Connectionstringsetting through Webjobs ?
@ThinkFr33ly This preview feature may help you: https://azure.microsoft.com/en-us/blog/simplifying-security-for-serverless-and-web-apps-with-azure-functions-and-app-service/ See also: https://docs.microsoft.com/en-gb/azure/app-service/app-service-key-vault-references
@tjrobinson this is great. Had no idea you could do that. I actually managed to get this working using the web job startup class and just setting the configuration value manually. I prefer this method because it keeps my configuration system consistent across all aspects of my app - .config or .json files never have secrets, they only have "secret URIs" and then I grab those using a secret resolver / key vault. This leverages the MSI (probably exactly how your example is doing it).
@ThinkFr33ly would you mind sharing some details about how you implemented your solution? Thanks!
Here is how I did it using the PostConfigure options - note that you have to make sure the ConnectionStringSetting
is set to ""
or else the dynamically set value wont be used: https://briandunnington.github.io/azure_functions_dynamic_connection_string
I would like to be able to configure the
CosmosDBTrigger
connection string at runtime so that I can build the connection string dynamically, for example so I can use secrets stored in Key Vault.Repro steps
I have the following method defined, using the
CosmosDBTrigger
:I've tried the following approaches to set the value of the setting
CosmosDB:ConnectionString
.Hard-code the value in my
appsettings.json
fileThis is the only approach that works but requires the full connection string, including the
AccountKey
to be in the config file, or in the Azure Portal (to avoid checking in secrets) - only really an option for local development.Using
IPostConfigureOptions
This runs, but since the trigger doesn't appear to use
IOptions<CosmosDBOptions>
it doesn't help. I'm noting it here for completeness.Adding the setting that the trigger is looking for using
AddInMemoryCollection
Again, this code runs and stepping through I can see it adding the correct value but the trigger doesn't seem to use it.
Error information
The error I get if the connection string isn't configured is
Request url is invalid
which makes sense since it's presumably null/empty.Expected behaviour
We use the
ServiceBusTriggerAttribute
and configure the connection string in an implementation ofIPostConfigureOptions<ServiceBusOptions>
and this works fine. TheCosmosDBTrigger
behaves differently and doesn't appear to support this.Related information
netcoreapp2.1
Microsoft.Azure.WebJobs
3.0.1
Microsoft.Azure.WebJobs.Extensions.CosmosDB
3.0.1
HostBuilder
inProgram.Main()