diff --git a/src/PandaVaultClient/ConfigurationManagerExtension.cs b/src/PandaVaultClient/ConfigurationManagerExtension.cs deleted file mode 100644 index 26db184..0000000 --- a/src/PandaVaultClient/ConfigurationManagerExtension.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; - -namespace PandaVaultClient; - -public static class ConfigurationManagerExtension -{ - public static IConfigurationManager AddPandaVault(this IConfigurationManager configurationManager) - { - configurationManager.Add(new PandaVaultConfigurationSource(configurationManager)); - return configurationManager; - } - - public static WebApplicationBuilder RegisterPandaVaultEndpoint(this WebApplicationBuilder builder) - { - builder.Services.AddSingleton(); - return builder; - } -} \ No newline at end of file diff --git a/src/PandaVaultClient/Dtos/AllConfigurationsDto.cs b/src/PandaVaultClient/Dtos/AllConfigurationsDto.cs index a1229e8..6ae3436 100644 --- a/src/PandaVaultClient/Dtos/AllConfigurationsDto.cs +++ b/src/PandaVaultClient/Dtos/AllConfigurationsDto.cs @@ -2,6 +2,6 @@ public class AllConfigurationsDto { - public string Key { get; set; } = null!; - public string? Value { get; set; } + public required string Key { get; set; } + public required string Value { get; set; } } \ No newline at end of file diff --git a/src/PandaVaultClient/Dtos/ConfigurationDto.cs b/src/PandaVaultClient/Dtos/ConfigurationDto.cs index a9849f6..cf0715f 100644 --- a/src/PandaVaultClient/Dtos/ConfigurationDto.cs +++ b/src/PandaVaultClient/Dtos/ConfigurationDto.cs @@ -1,10 +1,12 @@ -using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Serialization; namespace PandaVaultClient.Dtos; -[SuppressMessage("ReSharper", "InconsistentNaming")] public class ConfigurationDto { - public string key { get; set; } = null!; - public string value { get; set; } = null!; + [JsonPropertyName("key")] + public string Key { get; set; } = null!; + + [JsonPropertyName("value")] + public string Value { get; set; } = null!; } \ No newline at end of file diff --git a/src/PandaVaultClient/Dtos/RefreshConfigurationsDto.cs b/src/PandaVaultClient/Dtos/RefreshConfigurationsDto.cs deleted file mode 100644 index 6baebae..0000000 --- a/src/PandaVaultClient/Dtos/RefreshConfigurationsDto.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace PandaVaultClient.Dtos; - -public class RefreshConfigurationsDto -{ - public string Key { get; set; } = null!; - public string OldValue { get; set; } = null!; - public string UpdatedValue { get; set; } = null!; - public bool Success { get; set; } - public string Message { get; set; } = null!; -} \ No newline at end of file diff --git a/src/PandaVaultClient/HttpHelper.cs b/src/PandaVaultClient/HttpHelper.cs index 0894bc5..209a9f4 100644 --- a/src/PandaVaultClient/HttpHelper.cs +++ b/src/PandaVaultClient/HttpHelper.cs @@ -1,7 +1,6 @@ -using System.Net; using System.Net.Http.Json; using PandaVaultClient.Dtos; -using ResponseCrafter.HttpExceptions; +using RegexBox; namespace PandaVaultClient; @@ -14,26 +13,31 @@ public static async Task> FetchConfigurationsAsync() { const string endpoint = "/api/v1/vault-configs"; using var client = new HttpClient(); + var validUrl = PandaValidator.IsUri(Url, false); - if (Url is null) + if (!validUrl) { - throw new ArgumentNullException($"PANDAVAULT_URL environment variable is not set"); + throw new ArgumentNullException($"PANDAVAULT_URL is not valid. Url: {Url}"); } - if (Secret is null) + if (string.IsNullOrWhiteSpace(Secret)) { - throw new ArgumentNullException($"PANDAVAULT_SECRET environment variable is not set"); + throw new ArgumentNullException("PANDAVAULT_SECRET environment variable is not set"); } client.DefaultRequestHeaders.Add("secret", Secret); + var response = await client.GetAsync($"{Url}{endpoint}"); - if (response.StatusCode == HttpStatusCode.BadRequest) - { - throw new BadRequestException("PANDAVAULT_SECRET environment variable's value is not correct"); - } - - if (!response.IsSuccessStatusCode) + + List configurations = new(); + + if (response.IsSuccessStatusCode) { - throw new BadRequestException("Failed to fetch configuration from PandaVault. Please check the client settings and network connectivity."); + configurations = (await response.Content.ReadFromJsonAsync>())!; + + if (configurations.Count == 0) + { + + } } return (await response.Content.ReadFromJsonAsync>())!; diff --git a/src/PandaVaultClient/PandaVaultClient.csproj b/src/PandaVaultClient/PandaVaultClient.csproj index 884c481..9c0554d 100644 --- a/src/PandaVaultClient/PandaVaultClient.csproj +++ b/src/PandaVaultClient/PandaVaultClient.csproj @@ -8,7 +8,7 @@ Readme.md Pandatech MIT - 4.0.1 + 4.0.2 Pandatech.PandaVaultClient Pandatech, library, pandavault, security, configurations Pandatech.PandaVaultClient @@ -24,7 +24,7 @@ - + diff --git a/src/PandaVaultClient/PandaVaultConfigurationProvider.cs b/src/PandaVaultClient/PandaVaultConfigurationProvider.cs index 457cb3f..2820f35 100644 --- a/src/PandaVaultClient/PandaVaultConfigurationProvider.cs +++ b/src/PandaVaultClient/PandaVaultConfigurationProvider.cs @@ -7,21 +7,13 @@ public class PandaVaultConfigurationProvider(IConfiguration existingConfiguratio { public override void Load() { - List lines; - try - { - lines = HttpHelper.FetchConfigurationsAsync() + var lines = HttpHelper.FetchConfigurationsAsync() .Result; - } - catch (Exception ex) - { - throw new InvalidOperationException("Error on fetching configurations", ex); - } var requiredKeys = existingConfiguration.AsEnumerable() .Where(x => x.Value == "**"); - var missingKeys = requiredKeys.Where(x => lines.TrueForAll(y => y.key != x.Key)) + var missingKeys = requiredKeys.Where(x => lines.TrueForAll(y => y.Key != x.Key)) .Select(x => x.Key) .ToList(); @@ -32,25 +24,7 @@ public override void Load() } Data = lines - .Where(x => existingConfiguration[x.key] != null) - .ToDictionary(x => x.key, x => x.value)!; - } - - public List GetAllConfigurations(string pandaVaultSecret) - { - if (pandaVaultSecret != Environment.GetEnvironmentVariable("PANDAVAULT_SECRET")) - { - throw new ArgumentException("PandaVault secret is not correct"); - } - - var allConfigurations = existingConfiguration.AsEnumerable() - .Select(conf => new AllConfigurationsDto - { - Key = conf.Key, - Value = conf.Value - }) - .ToList(); - - return allConfigurations; + .Where(x => existingConfiguration[x.Key] != null) + .ToDictionary(x => x.Key, x => x.Value)!; } } \ No newline at end of file diff --git a/src/PandaVaultClient/WebApplicationBuilderExtensions.cs b/src/PandaVaultClient/WebApplicationBuilderExtensions.cs new file mode 100644 index 0000000..f970ba9 --- /dev/null +++ b/src/PandaVaultClient/WebApplicationBuilderExtensions.cs @@ -0,0 +1,16 @@ +using System.Collections.Immutable; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Configuration; + +namespace PandaVaultClient; + +public static class WebApplicationBuilderExtensions +{ + public static WebApplicationBuilder AddPandaVault(this WebApplicationBuilder builder) + { + var configurationManager = builder.Configuration.Add(); + + configurationManager.Add(new PandaVaultConfigurationSource(configurationManager)); + return configurationManager; + } +} \ No newline at end of file diff --git a/tests/PandaVaultClient.Test/Program.cs b/tests/PandaVaultClient.Test/Program.cs index 8d60755..1820fcb 100644 --- a/tests/PandaVaultClient.Test/Program.cs +++ b/tests/PandaVaultClient.Test/Program.cs @@ -3,8 +3,8 @@ var builder = WebApplication.CreateBuilder(args); + builder.Configuration.AddPandaVault(); // Adding PandaVaultConfigurationSource -builder.RegisterPandaVaultEndpoint(); // optional, if you want to use the endpoint for all configurations builder.Services.AddEndpointsApiExplorer();