diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-08-16 16:07:37 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-08-16 16:07:37 +0200 |
| commit | 4ab32ee8539e5055aa5270af1b42bae667d6c95f (patch) | |
| tree | 96c1894ccdefa1f06f5dab4e0881d80b70b64eb3 /server/src | |
| parent | bb295c5f275ec56e6f3f655de2f3d9d1adc69465 (diff) | |
| download | greatoffice-4ab32ee8539e5055aa5270af1b42bae667d6c95f.tar.xz greatoffice-4ab32ee8539e5055aa5270af1b42bae667d6c95f.zip | |
feat: Support overwriting database config with environment variables
Diffstat (limited to 'server/src')
| -rw-r--r-- | server/src/Services/VaultService.cs | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/server/src/Services/VaultService.cs b/server/src/Services/VaultService.cs index a7ae57f..732911a 100644 --- a/server/src/Services/VaultService.cs +++ b/server/src/Services/VaultService.cs @@ -7,9 +7,10 @@ public class VaultService private readonly HttpClient _client; private readonly IMemoryCache _cache; private readonly IConfiguration _configuration; + private readonly ILogger<VaultService> _logger; private int CACHE_TTL { get; set; } - public VaultService(HttpClient client, IConfiguration configuration, IMemoryCache cache) { + public VaultService(HttpClient client, IConfiguration configuration, IMemoryCache cache, ILogger<VaultService> logger) { 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); @@ -20,12 +21,13 @@ public class VaultService _client = client; _cache = cache; _configuration = configuration; + _logger = logger; } public static object Data { get; set; } public T Get<T>(string path) { - return _cache.GetOrCreate(AppConstants.VAULT_CACHE_KEY, + var result = _cache.GetOrCreate(AppConstants.VAULT_CACHE_KEY, cacheEntry => { cacheEntry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(CACHE_TTL); var getSecretResponse = _client.GetFromJsonAsync<GetSecretResponse<T>>("/v1/kv/data/" + path).Result; @@ -42,6 +44,7 @@ public class VaultService }); return getSecretResponse.Data.Data ?? default; }); + return result; } public T Refresh<T>(string path) { @@ -64,7 +67,40 @@ public class VaultService public AppConfiguration GetCurrentAppConfiguration() { var path = _configuration.GetValue<string>(AppEnvironmentVariables.MAIN_CONFIG_SHEET); - return Get<AppConfiguration>(path); + var result = Get<AppConfiguration>(path); + var overwrites = new { + DB_HOST = _configuration.GetValue("OVERWRITE_DB_HOST", string.Empty), + DB_PORT = _configuration.GetValue("OVERWRITE_DB_PORT", string.Empty), + DB_USER = _configuration.GetValue("OVERWRITE_DB_USER", string.Empty), + DB_PASSWORD = _configuration.GetValue("OVERWRITE_DB_PASSWORD", string.Empty), + DB_NAME = _configuration.GetValue("OVERWRITE_DB_NAME", string.Empty), + }; + if (overwrites.DB_HOST.HasValue()) { + _logger.LogInformation("OVERWRITE_DB_HOST is specified, using it's value: " + overwrites.DB_HOST); + result.DB_HOST = overwrites.DB_HOST; + } + + if (overwrites.DB_PORT.HasValue()) { + _logger.LogInformation("OVERWRITE_DB_PORT is specified, using it's value: " + overwrites.DB_PORT); + result.DB_PORT = overwrites.DB_PORT; + } + + if (overwrites.DB_USER.HasValue()) { + _logger.LogInformation("OVERWRITE_DB_USER is specified, using it's value: " + overwrites.DB_USER); + result.DB_USER = overwrites.DB_USER; + } + + if (overwrites.DB_PASSWORD.HasValue()) { + _logger.LogInformation("OVERWRITE_DB_PASSWORD is specified, using it's value: " + "(redacted)"); + result.DB_PASSWORD = overwrites.DB_PASSWORD; + } + + if (overwrites.DB_NAME.HasValue()) { + _logger.LogInformation("OVERWRITE_DB_NAME is specified, using it's value: " + overwrites.DB_NAME); + result.DB_NAME = overwrites.DB_NAME; + } + + return result; } public AppConfiguration RefreshCurrentAppConfiguration() { |
