diff options
Diffstat (limited to 'server/src/Services')
| -rw-r--r-- | server/src/Services/ForgotPasswordService.cs | 14 | ||||
| -rw-r--r-- | server/src/Services/MailService.cs | 11 | ||||
| -rw-r--r-- | server/src/Services/VaultService.cs | 53 |
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; } |
