From 1bd30ee34323f150c63fc537e0d131dca29dc4ef Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Sun, 5 Jun 2022 00:19:10 +0200 Subject: refactor: Implement caching in VaultService and use VaultService instead of IOptions --- server/src/Services/VaultService.cs | 53 ++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 6 deletions(-) (limited to 'server/src/Services/VaultService.cs') diff --git a/server/src/Services/VaultService.cs b/server/src/Services/VaultService.cs index 388f8d4..6034586 100644 --- a/server/src/Services/VaultService.cs +++ b/server/src/Services/VaultService.cs @@ -1,21 +1,52 @@ +using Microsoft.Extensions.Caching.Memory; + namespace IOL.GreatOffice.Api.Services; public class VaultService { private readonly HttpClient _client; + private readonly IMemoryCache _cache; + private readonly IConfiguration _configuration; + private int CACHE_TTL { get; set; } - public VaultService(HttpClient client, IConfiguration configuration) { - var token = configuration.GetValue("VAULT_TOKEN"); - var vaultUrl = configuration.GetValue("VAULT_URL"); + public VaultService(HttpClient client, IConfiguration configuration, IMemoryCache cache) { + var token = configuration.GetValue(AppEnvironmentVariables.VAULT_TOKEN); + var vaultUrl = configuration.GetValue(AppEnvironmentVariables.VAULT_URL); + CACHE_TTL = configuration.GetValue(AppEnvironmentVariables.VAULT_CACHE_TTL, 60 * 60 * 12); 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.DefaultRequestHeaders.Add(AppHeaders.VAULT_TOKEN, token); client.BaseAddress = new Uri(vaultUrl); _client = client; + _cache = cache; + _configuration = configuration; + } + + public static object Data { get; set; } + + public T Get(string path) { + return _cache.GetOrCreate(AppConstants.VAULT_CACHE_KEY, + cacheEntry => { + cacheEntry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(CACHE_TTL); + var getSecretResponse = _client.GetFromJsonAsync>("/v1/kv/data/" + path).Result; + if (getSecretResponse != null) { + Log.Debug("Setting new Vault cache, " + + new { + PATH = path, + CACHE_TTL, + Data = JsonSerializer.Serialize(getSecretResponse.Data.Data) + }); + return getSecretResponse.Data.Data ?? default; + } + + return default; + }); } - public async Task> GetSecretAsync(string path) { - return await _client.GetFromJsonAsync>("/v1/kv/data/" + path); + public T Refresh(string path) { + _cache.Remove(AppConstants.VAULT_CACHE_KEY); + CACHE_TTL = _configuration.GetValue(AppEnvironmentVariables.VAULT_CACHE_TTL, 60 * 60 * 12); + return Get(path); } public async Task RenewTokenAsync(string token) { @@ -30,6 +61,16 @@ public class VaultService return default; } + public AppConfiguration GetCurrentAppConfiguration() { + var path = _configuration.GetValue(AppEnvironmentVariables.MAIN_CONFIG_SHEET); + return Get(path); + } + + public AppConfiguration RefreshCurrentAppConfiguration() { + var path = _configuration.GetValue(AppEnvironmentVariables.MAIN_CONFIG_SHEET); + return Refresh(path); + } + public class RenewTokenResponse { public Guid RequestId { get; set; } -- cgit v1.3