簡體   English   中英

如何從 azure 應用服務訪問密鑰庫

[英]How to access key vault from azure app service

我有以下方法連接到我的代碼中的密鑰庫:

public static class ConfigurationBuilderExtension
{
    public static IConfigurationBuilder AddKeyVaultconfiguration(this IConfigurationBuilder builder)
    {
        var config = builder.Build();

        string name = config["KeyVault:Name"];
        string clientId = config["KeyVault:ClientId"];
        string clientSecret = config["KeyVault:ClientSecret"];

        Verify.NotNullOrEmpty(name, nameof(name));
        Verify.NotNullOrEmpty(name, nameof(clientId));
        Verify.NotNullOrEmpty(name, nameof(clientSecret));

        builder.AddAzureKeyVault($"https://{name}.vault.azure.net/", clientId, clientSecret);

        return builder;
    }
}

要在本地運行它,我只需將用戶機密添加到項目中:

{
  "KeyVault": {
    "Name": "brajzorekeyvault",
    "ClientId": "xxxx",
    "ClientSecret": "xxxx"
  }
}

這在本地有效。

但是,當我將其發布到 Azure 中的應用程序服務時,如何使用這種方法? 我必須以某種方式注入名稱、clientId 和 clientSecret? 但我不知道這樣做的最佳實踐方法是什么? 我應該在 Azure devops 中創建一個由這些值組成的變量組,然后在管道中使用它們嗎?

您應該使用托管標識訪問 web 應用程序內的密鑰保管庫,以避免注入密鑰。 這個教程

對於本地開發,您可以在 VS 中鏈接一個帳戶,該帳戶將用於針對密鑰保管庫進行身份驗證。 查看文檔

使用托管身份是最佳實踐,事實上,您不會看到任何有關使用客戶端密碼進行連接的官方文檔,但至少會警告您使用托管身份。

當我們在 Azure 中使用 PAAS(大多數資源是 Azure 中的 PAAS,如 App 服務和 KeyVault)時。大多數時候,我們不需要編寫代碼或編寫非常少的代碼來訪問它們,例如在這種情況下。 無需提供任何詳細信息(如連接字符串或其他詳細信息)即可訪問應用服務中的密鑰保管庫,只需在 azure 門戶中進行少量配置並直接從密鑰保管庫中獲取機密。

在 Azure 中,它們是同一資源組中的資源,只需將應用服務的 object id 添加到 keyvault“訪問策略”即可相互通信或進行身份驗證或授權。 所有令人頭疼的問題都在 azure 的驗證上。 這是非常簡單、安全和良好的做法。

步驟如下

  1. 為您的 Web 應用程序/應用程序服務啟用托管服務標識

    一種。 Select Azure Web 應用程序/應用程序服務中左側菜單中的“身份”。

    b. 在“系統分配”選項卡中,將“狀態”切換更改為“打開”。

    c。幾秒鍾后,Object ID 將可用,然后復制“對象 ID”。

在此處輸入圖像描述

  1. 授權 Web App/App Service 訪問您的 Key Vault

    一種。 Select 來自“Key Vault”屏幕的“訪問策略”。

    b. 單擊“添加訪問策略”。

    c。提供“獲取”和“列表”權限。

    d. 在“Select a Principal”選項中,指定您之前為 Azure Web App/App Service 復制的“對象 ID”的值。

    e. 粘貼、搜索,然后從列表中搜索 select。

    F。 單擊“添加”。

    G。 單擊“保存”以保留更改並完成該過程。

Provide Permissions

在此處輸入圖像描述

Copy object Id

在此處輸入圖像描述

  1. 閱讀 .NET Core 中的 Azure Key Vault Secrets

    一種。 安裝 NuGet 包

    可以通過以下兩種方式之一安裝這些軟件包:通過集成到 Visual Studio 2019 IDE 中的 NuGet Package 管理器,或通過在 Package 管理器控制台中運行以下命令:

    安裝包 Microsoft.Extensions.Azure 和安裝包 Azure.Security.KeyVault.Secrets

Note:- In My case, I did not need to install these packages.

b. 從 AzureKeyVault 訪問機密

  1. 在 AppSettings 中指定 Vault Uri:- 在 appsettings.json 文件中創建一個名為“KeyVault”的部分,並在其中指定一個名為“VaultUri”的密鑰,如下所示。

appsettings.Development.json

{
   "SecretName": "xyz"
}

appsettings.OtherEnv.json

       "KeyVault": {
         "VaultUri": "https://yourkeyvaulturl.vault.azure.net/"
         }
  1. 創建 KeyVaultManagement class
public class KeyVaultManagement
   {
      private readonly IConfiguration _config;

      public KeyVaultManagement(IConfiguration config)
      {
        _config= config;
      }

      public SecretClient SecretClient
      {
        get
         {
            return new SecretClient(
                             new Uri($"{this._config["KeyVault:VaultUri"]}"),
                             new DefaultAzureCredential()) ;
         }
       }
  }
  1. 在 Program.cs 中編寫以下代碼
.ConfigureAppConfiguration((context, config) =>
{
   var builtConfig = config.Build();
   if (!context.HostingEnvironment.IsDevelopment())
   {
     config.AddAzureKeyVault(new KeyVaultManagement(builtConfig).SecretClient, new KeyVaultSecretManager());
   }
});
  1. 在下面寫下需要獲取秘密的地方。
var valueofSecret = configuration["SecretName"];

configuration is IConfiguration

Azure Keyvault 訪問策略微軟文檔

如果您的應用程序不是應用程序服務或它是在 .NET 框架中開發的

然后使用證書實現 KEYVAULT

  1. 安裝相同的 Nuget 包

  2. 在 Program.cs 中編寫以下代碼。 可能需要添加 package 或命名空間才能使用證書類

.ConfigureAppConfiguration((context, config) =>
{
   var root = config.Build();
   var KeyVaultName = root["KeyVaultName"];
   var Uri = new Uri($"https://{KeyVaultName}.vault.azure.net/");
   var x509Certifcate = CertifcateHelper.GetCertificate(root["Thumprint"],"KeyVaultCertificate");
   
     config.AddAzureKeyVault(Uri , new ClientCertificateCredential(root["ClientTenantId"], root["ClientAppId"], x509Certifcate));
});

  1. 在 APPSetting 中添加以下行

appsettings.Development.json

{
   "KeyVaultName": "keyvalutname",
   "ClientTenantId": "Get from azure and paste here",
   "ClientAppId": "Get from azure and paste here",
   "Thumbprint": "Get from keyvalut certificate in azure and paste here";
}
  1. 在下面寫下需要獲取秘密的地方。
var valueofSecret = configuration["SecretName"];

暫無
暫無

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

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