Search code examples
environment-variablesasp.net-core-webapi.net-6.0azure-keyvault

How to fetch value from Azure KeyVault and set as environment variable along with other appSettings values


In below code, I am trying to fetch the secrets from Azure KeyVault and then sets as environment variable and reason for this is that I don't want to read it from appsettings.json file.

 public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        
        var app = builder.RegisterServices().Build();

        var client = new SecretClient(new Uri(Environment.GetEnvironmentVariable("KeyStoreUrl") ?? string.Empty), new DefaultAzureCredential());
        var url = client.GetSecret("APM--Url").Value.Value;
        var token = client.GetSecret("APM--Token").Value.Value;

        Environment.SetEnvironmentVariable("APM--Url", url);
        Environment.SetEnvironmentVariable("APM--Token", token);

        builder.Configuration.AddEnvironmentVariables(prefix: "APM");
        
        app.UseAllElasticApm(builder.Configuration);

        app.Run();
    }

Secrets are coming from Keyvault without any issue but further while calling app.UseAllElasticApm(builder.Configuration); it's not set's the values correctly.

What is missing here?

Please note some variables value (ServiceName) I want to read from appSettings file and some (Url, Token) I want to retrieve from Azure Keyvault and combined value I want to send as builder.Configuration.

"APM": {
"Url": "XXXXXXXXX",
"Token": "XXXXXX",
"ServiceName": "Sample"

},


Solution

  • reason for this is that I don't want to read it from appsettings.json file.

    If you don't want to read it from appsettings.json file, we can set the values in the Application settings in Configuration section of the deployed App or in the Environment Variables.

    The values set in the Application Settings will be then available in the Environment Variables with the prefix Application_KeyName.

    (Url, Token) I want to retrieve from Azure Keyvault

    • Add the secrets in Key Vault.

    enter image description here

    In My Program.cs I have used the same code which you are using to retrieve values from Key Vault .

    var client = new SecretClient(new Uri(Environment.GetEnvironmentVariable("KeyStoreUrl") ?? string.Empty), new DefaultAzureCredential()); 
    var url = client.GetSecret("Url").Value.Value; 
    var token = client.GetSecret("Token").Value.Value; ```
    
    • Url and Token Values from Key Vault.

    enter image description here

    enter image description here

    My Initial local Environment Variables.

    enter image description here

    Now set the secret values (URL and Token) which we retrieved from Key Vault and ServiceName from appsettings.json , to Environment Variables.

    Environment.SetEnvironmentVariable("Url", url);
    Environment.SetEnvironmentVariable("Token", token);
    

    My Complete code from Program.cs file :

    using Azure.Identity;
    using Azure.Security.KeyVault.Secrets;
    
    var builder = WebApplication.CreateBuilder(args);
    
    var client = new SecretClient(new Uri(Environment.GetEnvironmentVariable("KeyStoreUrl") ?? string.Empty), new DefaultAzureCredential());
    var url = client.GetSecret("Url").Value.Value;
    var token = client.GetSecret("Token").Value.Value;
    
    var ServiceName = builder.Configuration["ServiceName"];
    
    Environment.SetEnvironmentVariable("APM--Url", url);
    Environment.SetEnvironmentVariable("APM--Token", token);
    Environment.SetEnvironmentVariable("APM--ServiceName", ServiceName);
    
    builder.Services.AddControllers();
    builder.Services.AddEndpointsApiExplorer();
    builder.Services.AddSwaggerGen();
    
    var app = builder.Build();
    
    var myconfig = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", optional: true)
            .AddEnvironmentVariables(prefix: "APM")
            .Build();
    
    var APMUrl = Environment.GetEnvironmentVariable("APM--Url");
    var APMToken = Environment.GetEnvironmentVariable("APM--Token");
    var APMServiceName= Environment.GetEnvironmentVariable("APM--ServiceName");
    
    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
        app.UseSwaggerUI();
    }
    
    app.UseHttpsRedirection();
    app.UseAuthorization();
    app.MapControllers();
    app.Run();
    
    

    Now Iam able to retrieve the values using GetEnvironmentVariable.

    Output from Environment Variables:

    enter image description here

    enter image description here

    enter image description here