【发布时间】:2021-06-30 17:15:52
【问题描述】:
我正在尝试设置 hashcorp 保险库并获取我们存储在保险库中的键值对(数据库凭据)。
我按照下面的链接连接到保险库并从保险库中获取凭据 https://github.com/rajanadar/VaultSharp
我可以很好地连接并从保管库中获取凭据,但我的问题是如何将此凭据传递给我的数据库上下文。 我是否需要将这些凭据存储在某个地方,从那里获取然后传递给我的数据库上下文,或者我是否需要每次都初始化这个类。 下面是获取凭证的示例代码
public class VaultService : IVaultService
{
public async Task Configure()
{
//code to authenticate role and connect vault here
//Below is the code that actually fetches the credentials. I am just providing relevant code.
Secret<SecretData> secret = await vaultClient.V1.Secrets.KeyValue.V2.ReadSecretAsync(kvpPath.Value, mountPoint: "kv");
foreach (var kvp in secret.Data.Data)
{
// Console.WriteLine(kvp.Key + " : " + kvp.Value);
}
}
}
如何使用上述类来获取和传递凭据。
下面是定义我的数据库上下文的启动类:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IDbAdapterService, DbAdapterService>();
}
}
下面是我需要使用凭据的 DbAdapterService
public class DbAdapterService : DbAdapterService
{
private readonly AppSettings _settings;
public DbAdapterService(IOptions<AppSettings> settings)
{
_settings = settings?.Value;
DbConnectionStringBuilder builder = new DbConnectionStringBuilder();
//Below is where I need to update the credentials
builder.ConnectionString = _settings.ConnectionString;
}
}
【问题讨论】:
-
你不需要把它复杂化。使用github.com/andrewlock/… 为Hashicorp Vault 安装
IConfigurationProvider,然后使用IConfiguration配置dbcontext。 -
对使用保管库和使用凭据而不是使用您建议的方法的任何输入?
-
没有其他输入?
标签: c# .net .net-core hashicorp-vault vaultsharp