簡體   English   中英

在 .NET 應用程序中加載 Azure Secrets 的最佳方法

[英]Best approach to load Azure Secrets in a .NET application

我正在嘗試尋找在 .NET 5 應用程序中重新加載 Azure Secrets 的最佳解決方案。 我會把我現在所擁有的寫在這里。
首先我這樣做:

.ConfigureAppConfiguration((context, configuration) =>
{
    var configurationRoot = configuration.Build();

    config.AddAzureKeyVault(
        new Uri(configurationRoot["KeyVault:Uri"]),
        new DefaultAzureCredential()
});

但有個問題。 秘密被緩存,直到IConfigurationRoot.Reload()被調用。 過期,禁用,並在關鍵金庫更新的秘密,直到執行刷新不被尊重的應用程序,或者如果ReloadInterval被用作一個選項AddAzureKeyVault

我的第二種方法是添加一個間隔,每 15 分鍾自動重新加載機密。 (並使用IOptionsSnapshot而不是IOptions來獲取最新值)

.ConfigureAppConfiguration((context, configuration) =>
{
    var configurationRoot = configuration.Build();

    int reloadInterval;
    bool success = int.TryParse(configurationRoot["KeyVault:ReloadInterval"], out reloadInterval);
    config.AddAzureKeyVault(
        new Uri(configurationRoot["KeyVault:Uri"]),
        new DefaultAzureCredential(),
        new AzureKeyVaultConfigurationOptions()
        {
            ReloadInterval = TimeSpan.FromMinutes(success ? reloadInterval : 15)
        });
});

我仍然認為這不是最好的解決方案,因為還有一個時間窗口(在本例中為 15 分鍾),可以在其中更改 Azure 中的機密,如果我嘗試使用它們,我將不會獲得最新版本。
因此,我嘗試了一個解決方案,當我遇到有關無效秘密的錯誤時,我手動調用IConfigurationRoot.Reload() 例如在MongoDbContext ,如果連接字符串無效,我將收到MongoConfigurationException
使用Polly我這樣做了:

public MongoDbContext(IOptionsSnapshot<DatabaseSettings> databaseSettings, ILogger<MongoDbContext> logger)
{
    _logger = logger;
    _databaseSettings = databaseSettings.Value;

    _policy = Policy
        .Handle<MongoConfigurationException>()
        .Retry(1, (exception, retryCount) =>
        {
             // Reload configuration
        });
    _policy.Execute(() => _mongoClient = new MongoClient(_databaseSettings.ConnectionString));

    _mongoDatabase = _mongoClient.GetDatabase(_databaseSettings.DatabaseName);
}

或者也許這個更好:

{
    _logger = logger;
    _databaseSettings = databaseSettings.Value;

    _circuitBreakerPolicy = Policy
        .Handle<MongoConfigurationException>()
        .CircuitBreaker(1, TimeSpan.FromSeconds(10));
    _retryPolicy = Policy.Handle<MongoConfigurationException>()
        .Retry(1, (exception, retryCount) =>
        {
             // Reload configuration
        });
    _policy = Policy.Wrap(_retryPolicy, _circuitBreakerPolicy);
    _policy.Execute(() => _mongoClient = new MongoClient(_databaseSettings.ConnectionString));

    _mongoDatabase = _mongoClient.GetDatabase(_databaseSettings.DatabaseName);
}

但是,使用這種方法意味着我應該做同樣的事情並在應用程序中使用秘密的任何地方捕獲該特定異常。 另外,我還不能在這個類中注入IConfigurationRoot ,我不知道這樣做是否可以。 MongoDbContextMongoDbContext項目之外的基礎設施項目的一部分。

使用Azure.Extensions.AspNetCore.Configuration.Secrets在 .NET 應用程序中加載 Azure 機密

AddAzureKeyVault的重載,它采用AzureKeyVaultConfigurationOptions參數並允許指定重新加載間隔。

configurationBuilder.AddAzureKeyVault(new AzureKeyVaultConfigurationOptions 
{ 
    Vault = configuration["KeyVaultUrl"], 
    ReloadInterval = TimeSpan.FromMinutes(10), 
}); 

或者你可以試試這個

更新AddAzureAppConfiguration方法以使用SetSecretRefreshInterval方法為 Key Vault 證書設置刷新間隔。 通過此更改,您的應用程序將每 12 小時重新加載 ExampleCertificate 的公私密鑰對。

config.AddAzureAppConfiguration(options => 
{ 
    options.Connect(settings["ConnectionStrings:AppConfig"]) 
            .ConfigureKeyVault(kv => 
            { 
                kv.SetCredential(new DefaultAzureCredential()); 
                kv.SetSecretRefreshInterval("TestApp:Settings:KeyVaultCertificate", TimeSpan.FromHours(12)); 
            }); 
}); 

有關更多詳細信息,請參閱此鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM