4

我需要将 dotnet Web 应用程序部署到 Google Cloud Run,并希望使用用户机密文件来存储凭据。目前,它们位于不安全的 appsettings 中。有没有人使用 Google Secret Manager 做到这一点?

  • 最好存储键值对,一个json blob?
  • 我想在启动时而不是在构建时提取值。

如果有现有的示例或片段,他们将不胜感激。

谢谢。

4

2 回答 2

4

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 安全性

于 2020-07-24T22:24:27.957 回答
-1

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

大多数秘密引擎:

Hashicorp Vault << 可能是最灵活的..有人称其为秘密的“瑞士军刀”

胸腺秘密商店

Azure KeyVault(天蓝色云)

AKS (AWS)(亚马逊云)

(和你的朋友,谷歌)

相似的。

如果你使用的是 Kubernetes,你可以编写一个具体的代码来从 Kubernetes “mounted secrets”中读取值。(我更喜欢虚拟文件挂载的秘密)。

我所做的是创建一个抽象,然后为我的实现选择编写一个具体的代码。

对于开发环境,您还可以对此进行具体编码: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>();
        }
        
于 2020-07-24T20:44:07.453 回答