diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-06-04 21:05:47 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-06-04 21:13:00 +0200 |
| commit | cf9597de850de1ef721a35ad79ac67b9fdb9e1d4 (patch) | |
| tree | 4805de53a24bedd91238d6d306476b1921ccc0ad /server/src/Services/VaultService.cs | |
| parent | fdbeffe78e5cd7393d28915189ec518a06b941f1 (diff) | |
| download | greatoffice-cf9597de850de1ef721a35ad79ac67b9fdb9e1d4.tar.xz greatoffice-cf9597de850de1ef721a35ad79ac67b9fdb9e1d4.zip | |
refactor: Use Vault to get configuration
Diffstat (limited to 'server/src/Services/VaultService.cs')
| -rw-r--r-- | server/src/Services/VaultService.cs | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/server/src/Services/VaultService.cs b/server/src/Services/VaultService.cs new file mode 100644 index 0000000..388f8d4 --- /dev/null +++ b/server/src/Services/VaultService.cs @@ -0,0 +1,80 @@ +namespace IOL.GreatOffice.Api.Services; + +public class VaultService +{ + private readonly HttpClient _client; + + public VaultService(HttpClient client, IConfiguration configuration) { + var token = configuration.GetValue<string>("VAULT_TOKEN"); + var vaultUrl = configuration.GetValue<string>("VAULT_URL"); + if (token.IsNullOrWhiteSpace()) throw new ApplicationException("VAULT_TOKEN is empty"); + if (vaultUrl.IsNullOrWhiteSpace()) throw new ApplicationException("VAULT_URL is empty"); + client.DefaultRequestHeaders.Add("X-Vault-Token", token); + client.BaseAddress = new Uri(vaultUrl); + _client = client; + } + + public async Task<GetSecretResponse<T>> GetSecretAsync<T>(string path) { + return await _client.GetFromJsonAsync<GetSecretResponse<T>>("/v1/kv/data/" + path); + } + + public async Task<RenewTokenResponse> RenewTokenAsync<T>(string token) { + var response = await _client.PostAsJsonAsync("v1/auth/token/renew", + new { + Token = token + }); + if (response.IsSuccessStatusCode) { + return await response.Content.ReadFromJsonAsync<RenewTokenResponse>(); + } + + return default; + } + + public class RenewTokenResponse + { + public Guid RequestId { get; set; } + public string LeaseId { get; set; } + public bool Renewable { get; set; } + public long LeaseDuration { get; set; } + public object Data { get; set; } + public object WrapInfo { get; set; } + public List<string> Warnings { get; set; } + public Auth Auth { get; set; } + } + + public class Auth + { + public string ClientToken { get; set; } + public string Accessor { get; set; } + public List<string> Policies { get; set; } + public List<string> TokenPolicies { get; set; } + public object Metadata { get; set; } + public long LeaseDuration { get; set; } + public bool Renewable { get; set; } + public string EntityId { get; set; } + public string TokenType { get; set; } + public bool Orphan { get; set; } + public object MfaRequirement { get; set; } + public long NumUses { get; set; } + } + + public class GetSecretResponse<T> + { + public VaultSecret<T> Data { get; set; } + } + + public class VaultSecret<T> + { + public T Data { get; set; } + public VaultSecretMetadata Metadata { get; set; } + } + + public class VaultSecretMetadata + { + public DateTimeOffset CreatedTime { get; set; } + public object CustomMetadata { get; set; } + public string DeletionTime { get; set; } + public bool Destroyed { get; set; } + public long Version { get; set; } + } +} |
