簡體   English   中英

存儲在 Google Secret Manager 中的 DotNet 用戶機密

[英]DotNet User secrets stored in Google Secret Manager

我需要將 dotnet web 應用程序部署到 Google Cloud Run,並希望使用用戶機密文件來存儲憑據。 目前,它們位於不安全的 appsettings 中。 有沒有人使用 Google Secret Manager 做到這一點?

  • 最好存儲鍵值對,一個 json blob?
  • 我想在啟動時而不是在構建時提取值。

如果有現有的示例或片段,他們將不勝感激。

謝謝。

Google Cloud Run 和 Google Secret Manager 可以很好地協同工作。 關鍵是授予 Cloud Run 服務帳戶訪問 Secret Manager 的權限。 這消除了在您的應用程序中訪問 Secret Manager 的需要。

訪問控制

最好存儲鍵值對,一個 json blob?

這取決於存儲的數據量。 通常,您創建一個具有名稱 (secretId) 的密鑰並將數據分配給該密鑰(通過 API 或 CLI gcloud )。 在您的應用程序中,您按名稱 (secretId) 讀取密鑰。

我想在啟動時而不是在構建時提取值。

Seth Vargo(此處重復)提供的鏈接有 C# 和許多其他語言的示例。 您的應用程序在運行時從 Secret Manager 讀取機密。

創建機密和版本

Guillaume Blaquiere 寫了一篇文章,展示了如何使用 Secret Manager、Cloud Run 和環境變量。 絕對值得一讀。

Secret Manager:在不更改代碼的情況下提高 Cloud Run 安全性

恕我直言,使用專用的秘密引擎是“最好的”。

大多數秘密引擎:

Hashicorp Vault << 可能是最靈活的..有人稱其為秘密的“瑞士軍刀”

胸腺秘密商店

Azure KeyVault(天藍色雲)

AKS (AWS)(亞馬遜雲)

(和你的朋友,谷歌)

相似的。

如果您使用的是 Kubernetes,您可以編寫一個混凝土,該混凝土將從 Kubernetes“已安裝的機密”中讀取值。 (我更喜歡虛擬文件掛載的秘密)。

我所做的是創建一個抽象,然后為我的實現選擇編寫一個具體的代碼。

對於開發環境,您還可以編寫具體代碼: https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-3.1&tabs=windows

但是對於生產,我使用下面的抽象,並將我的具體代碼編寫為上述解決方案之一。

https://pbhadani.com/posts/google-secret-manager/

using System.Threading;
using System.Threading.Tasks;


public interface ISecretRetriever
{
    Task<SecretModel> GetSecret(string secretName);

    Task<SecretModel> GetSecret(string secretName, CancellationToken ct);
}

..

using System.Collections.Generic;
using System.Linq;

[System.Diagnostics.DebuggerDisplay("SecretName='{SecretName}', SubSecretsCount='{SubSecrets.Count}'")]
public class SecretModel
{


    public SecretModel()
    {
        this.SubSecrets = new List<SubSecret>();
    }

    public string SecretName { get; set; }

    public ICollection<SubSecret> SubSecrets { get; set; }
}

..

using System.Security;

[System.Diagnostics.DebuggerDisplay("KeyName = '{KeyName}', SecretValueLength='{null == SecretValue ? 0 : SecretValue.Length}'")]
public class SubSecret
{
    public string KeyName { get; set; }

    public SecureString SecretValue { get; set; }
}

然后您的 IoC 注冊將如下所示:

        if (hostingEnvironment.IsDevelopment()) /* https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.hostingenvironmentextensions.isdevelopment?view=aspnetcore-3.1 */
        {
            /* code your LowerEnvironmentsInsecureSecretRetriever to https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-3.1&tabs=windows */
            services.AddSingleton<ISecretRetriever, LowerEnvironmentsInsecureSecretRetriever>();
        }
        else
        {
            /* code your HashicorpVaultSecretRetriever to HashicorpVault (or use a different one */
            services.AddSingleton<ISecretRetriever, HashicorpVaultSecretRetriever>();
        }
        

暫無
暫無

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

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