[英]DotNet User secrets stored in Google Secret Manager
我需要将 dotnet web 应用程序部署到 Google Cloud Run,并希望使用用户机密文件来存储凭据。 目前,它们位于不安全的 appsettings 中。 有没有人使用 Google Secret Manager 做到这一点?
如果有现有的示例或片段,他们将不胜感激。
谢谢。
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 和环境变量。 绝对值得一读。
恕我直言,使用专用的秘密引擎是“最好的”。
大多数秘密引擎:
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.