41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using VaultSharp;
|
|
using VaultSharp.V1.AuthMethods;
|
|
using VaultSharp.V1.AuthMethods.Token;
|
|
using VaultSharp.V1.Commons;
|
|
|
|
namespace Inventory.Core
|
|
{
|
|
public class VaultService
|
|
{
|
|
private readonly IVaultClient _vaultClient;
|
|
|
|
public VaultService(string tokenPath, string vaultUrl)
|
|
{
|
|
// Read the token generated by Vault Agent
|
|
var token = File.ReadAllText(tokenPath).Trim();
|
|
|
|
// Initialize the VaultSharp client with the token
|
|
IAuthMethodInfo authMethod = new TokenAuthMethodInfo(token);
|
|
var vaultClientSettings = new VaultClientSettings(vaultUrl, authMethod);
|
|
|
|
_vaultClient = new VaultClient(vaultClientSettings);
|
|
}
|
|
|
|
public Secret<SecretData> GetSecret(string mountPath, string secretPath)
|
|
{
|
|
return _vaultClient.V1.Secrets.KeyValue.V2.ReadSecretAsync(secretPath, mountPoint: mountPath).Result;
|
|
}
|
|
|
|
public string GetSecretValue(string mountPath, string secretPath, string key)
|
|
{
|
|
var secret = GetSecret(mountPath, secretPath);
|
|
return secret!.Data!.Data[key]!.ToString()!;
|
|
}
|
|
|
|
public string GetSecretValue(Secret<SecretData> secret, string key)
|
|
{
|
|
return secret!.Data!.Data[key]!.ToString()!;
|
|
}
|
|
}
|
|
}
|