42 lines
1.5 KiB
C#
42 lines
1.5 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using VaultSharp;
|
|
using VaultSharp.V1.AuthMethods.Token;
|
|
using VaultSharp.V1.Commons;
|
|
|
|
namespace Vault.Demo.hashicorp.Console.Handler;
|
|
internal class ValueClientProvider : IValueClientProvider
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public ValueClientProvider(IConfiguration configuration) =>
|
|
this._configuration = configuration;
|
|
|
|
public IVaultClient GetValueClient()
|
|
{
|
|
var token = _configuration.GetValue<string>("vaultsetting:token")
|
|
?? throw new ArgumentNullException("vaultsetting:token");
|
|
var url = _configuration.GetValue<string>("vaultsetting:url")
|
|
?? throw new ArgumentNullException("vaultsetting:url");
|
|
var authMethod = new TokenAuthMethodInfo(token);
|
|
var vaultClientSettings = new VaultClientSettings(url, authMethod);
|
|
return new VaultClient(vaultClientSettings);
|
|
}
|
|
|
|
public async Task<Secret<SecretData>> GetSecret(IVaultClient client)
|
|
{
|
|
var path = _configuration.GetValue<string>("vaultsetting:path")
|
|
?? throw new ArgumentNullException("vaultsetting:path");
|
|
var mountPoint = _configuration.GetValue<string>("vaultsetting:mountPoint")
|
|
?? throw new ArgumentNullException("vaultsetting:mountPoint");
|
|
var kv2Secret = await client.V1.Secrets.KeyValue.V2
|
|
.ReadSecretAsync(path: path, mountPoint: mountPoint);
|
|
|
|
return kv2Secret;
|
|
}
|
|
}
|