summaryrefslogtreecommitdiffstats
path: root/server/src/Services
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
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')
-rw-r--r--server/src/Services/ForgotPasswordService.cs14
-rw-r--r--server/src/Services/MailService.cs11
-rw-r--r--server/src/Services/VaultService.cs53
3 files changed, 59 insertions, 19 deletions
diff --git a/server/src/Services/ForgotPasswordService.cs b/server/src/Services/ForgotPasswordService.cs
index e6b6acf..6874d37 100644
--- a/server/src/Services/ForgotPasswordService.cs
+++ b/server/src/Services/ForgotPasswordService.cs
@@ -1,23 +1,21 @@
-using Microsoft.Extensions.Options;
-
namespace IOL.GreatOffice.Api.Services;
public class ForgotPasswordService
{
private readonly AppDbContext _context;
private readonly MailService _mailService;
- private readonly IOptions<AppConfiguration> _configuration;
+ private readonly AppConfiguration _configuration;
private readonly ILogger<ForgotPasswordService> _logger;
public ForgotPasswordService(
AppDbContext context,
- IOptions<AppConfiguration> configuration,
+ VaultService vaultService,
ILogger<ForgotPasswordService> logger,
MailService mailService
) {
_context = context;
- _configuration = configuration;
+ _configuration = vaultService.GetCurrentAppConfiguration();
_logger = logger;
_mailService = mailService;
}
@@ -59,9 +57,9 @@ public class ForgotPasswordService
var request = new ForgotPasswordRequest(user);
_context.ForgotPasswordRequests.Add(request);
await _context.SaveChangesAsync(cancellationToken);
- var portalUrl = _configuration.Value.PORTAL_URL;
- var emailFromAddress = _configuration.Value.EMAIL_FROM_ADDRESS;
- var emailFromDisplayName = _configuration.Value.EMAIL_FROM_DISPLAY_NAME;
+ var portalUrl = _configuration.PORTAL_URL;
+ var emailFromAddress = _configuration.EMAIL_FROM_ADDRESS;
+ var emailFromDisplayName = _configuration.EMAIL_FROM_DISPLAY_NAME;
var zonedExpirationDate = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(request.ExpirationDate, requestTz.Id);
var message = new MailMessage {
From = new MailAddress(emailFromAddress, emailFromDisplayName),
diff --git a/server/src/Services/MailService.cs b/server/src/Services/MailService.cs
index d773303..eaff764 100644
--- a/server/src/Services/MailService.cs
+++ b/server/src/Services/MailService.cs
@@ -15,12 +15,13 @@ public class MailService
/// </summary>
/// <param name="configuration"></param>
/// <param name="logger"></param>
- public MailService(IOptions<AppConfiguration> configuration, ILogger<MailService> logger) {
+ public MailService(VaultService vaultService, ILogger<MailService> logger) {
+ var configuration = vaultService.GetCurrentAppConfiguration();
_logger = logger;
- _emailHost = configuration.Value.SMTP_HOST;
- _emailPort = Convert.ToInt32(configuration.Value.SMTP_PORT);
- _emailUser = configuration.Value.SMTP_USER;
- _emailPassword = configuration.Value.SMTP_PASSWORD;
+ _emailHost = configuration.SMTP_HOST;
+ _emailPort = Convert.ToInt32(configuration.SMTP_PORT);
+ _emailUser = configuration.SMTP_USER;
+ _emailPassword = configuration.SMTP_PASSWORD;
}
/// <summary>
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; }