我正在尝试设置 hashcorp vault 并获取存储在 vault 中的键值对(数据库凭据)。
我按照以下链接连接到保险库并从保险库获取凭据 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;
}
}