summaryrefslogtreecommitdiffstats
path: root/server/src/Services/VaultService.cs
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2022-06-05 00:19:10 +0200
committerivarlovlie <git@ivarlovlie.no>2022-06-05 00:19:10 +0200
commit1bd30ee34323f150c63fc537e0d131dca29dc4ef (patch)
tree12f8315916537bd4c9692a2d220a819e78a892fb /server/src/Services/VaultService.cs
parentd46743d565461144e1aabfb4b6a297d8387c4075 (diff)
downloadgreatoffice-1bd30ee34323f150c63fc537e0d131dca29dc4ef.tar.xz
greatoffice-1bd30ee34323f150c63fc537e0d131dca29dc4ef.zip
refactor: Implement caching in VaultService and use VaultService instead of IOptions
Diffstat (limited to 'server/src/Services/VaultService.cs')
-rw-r--r--server/src/Services/VaultService.cs53
1 files changed, 47 insertions, 6 deletions
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<string>("VAULT_TOKEN");
- var vaultUrl = configuration.GetValue<string>("VAULT_URL");
+ public VaultService(HttpClient client, IConfiguration configuration, IMemoryCache cache) {
+ var token = configuration.GetValue<string>(AppEnvironmentVariables.VAULT_TOKEN);
+ var vaultUrl = configuration.GetValue<string>(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<T>(string path) {
+ return _cache.GetOrCreate(AppConstants.VAULT_CACHE_KEY,
+ cacheEntry => {
+ cacheEntry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(CACHE_TTL);
+ var getSecretResponse = _client.GetFromJsonAsync<GetSecretResponse<T>>("/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<GetSecretResponse<T>> GetSecretAsync<T>(string path) {
- return await _client.GetFromJsonAsync<GetSecretResponse<T>>("/v1/kv/data/" + path);
+ public T Refresh<T>(string path) {
+ _cache.Remove(AppConstants.VAULT_CACHE_KEY);
+ CACHE_TTL = _configuration.GetValue(AppEnvironmentVariables.VAULT_CACHE_TTL, 60 * 60 * 12);
+ return Get<T>(path);
}
public async Task<RenewTokenResponse> RenewTokenAsync<T>(string token) {
@@ -30,6 +61,16 @@ public class VaultService
return default;
}
+ public AppConfiguration GetCurrentAppConfiguration() {
+ var path = _configuration.GetValue<string>(AppEnvironmentVariables.MAIN_CONFIG_SHEET);
+ return Get<AppConfiguration>(path);
+ }
+
+ public AppConfiguration RefreshCurrentAppConfiguration() {
+ var path = _configuration.GetValue<string>(AppEnvironmentVariables.MAIN_CONFIG_SHEET);
+ return Refresh<AppConfiguration>(path);
+ }
+
public class RenewTokenResponse
{
public Guid RequestId { get; set; }