Skip to content

Main #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 26, 2024
Merged

Main #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions src/PandaVaultClient/ConfigurationManagerExtension.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/PandaVaultClient/Dtos/AllConfigurationsDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
10 changes: 6 additions & 4 deletions src/PandaVaultClient/Dtos/ConfigurationDto.cs
Original file line number Diff line number Diff line change
@@ -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!;
}
10 changes: 0 additions & 10 deletions src/PandaVaultClient/Dtos/RefreshConfigurationsDto.cs

This file was deleted.

30 changes: 17 additions & 13 deletions src/PandaVaultClient/HttpHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Net;
using System.Net.Http.Json;
using PandaVaultClient.Dtos;
using ResponseCrafter.HttpExceptions;
using RegexBox;

namespace PandaVaultClient;

Expand All @@ -14,26 +13,31 @@ public static async Task<List<ConfigurationDto>> 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<ConfigurationDto> 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<List<ConfigurationDto>>())!;

if (configurations.Count == 0)
{

}
}

return (await response.Content.ReadFromJsonAsync<List<ConfigurationDto>>())!;
Expand Down
4 changes: 2 additions & 2 deletions src/PandaVaultClient/PandaVaultClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Authors>Pandatech</Authors>
<Copyright>MIT</Copyright>
<Version>4.0.1</Version>
<Version>4.0.2</Version>
<PackageId>Pandatech.PandaVaultClient</PackageId>
<PackageTags>Pandatech, library, pandavault, security, configurations</PackageTags>
<Title>Pandatech.PandaVaultClient</Title>
Expand All @@ -24,7 +24,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
<PackageReference Include="Pandatech.ResponseCrafter" Version="5.0.2" />
<PackageReference Include="Pandatech.RegexBox" Version="3.0.0" />
</ItemGroup>

</Project>
34 changes: 4 additions & 30 deletions src/PandaVaultClient/PandaVaultConfigurationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,13 @@ public class PandaVaultConfigurationProvider(IConfiguration existingConfiguratio
{
public override void Load()
{
List<ConfigurationDto> 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();

Expand All @@ -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<AllConfigurationsDto> 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)!;
}
}
16 changes: 16 additions & 0 deletions src/PandaVaultClient/WebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -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();

Check failure on line 11 in src/PandaVaultClient/WebApplicationBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / deploy

There is no argument given that corresponds to the required parameter 'configureSource' of 'ConfigurationExtensions.Add<TSource>(IConfigurationBuilder, Action<TSource>?)'

Check failure on line 11 in src/PandaVaultClient/WebApplicationBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / deploy

There is no argument given that corresponds to the required parameter 'configureSource' of 'ConfigurationExtensions.Add<TSource>(IConfigurationBuilder, Action<TSource>?)'

configurationManager.Add(new PandaVaultConfigurationSource(configurationManager));

Check failure on line 13 in src/PandaVaultClient/WebApplicationBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / deploy

Argument 1: cannot convert from 'Microsoft.Extensions.Configuration.IConfigurationBuilder' to 'Microsoft.Extensions.Configuration.IConfiguration'

Check failure on line 13 in src/PandaVaultClient/WebApplicationBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / deploy

Argument 1: cannot convert from 'Microsoft.Extensions.Configuration.IConfigurationBuilder' to 'Microsoft.Extensions.Configuration.IConfiguration'
return configurationManager;

Check failure on line 14 in src/PandaVaultClient/WebApplicationBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / deploy

Cannot implicitly convert type 'Microsoft.Extensions.Configuration.IConfigurationBuilder' to 'Microsoft.AspNetCore.Builder.WebApplicationBuilder'

Check failure on line 14 in src/PandaVaultClient/WebApplicationBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / deploy

Cannot implicitly convert type 'Microsoft.Extensions.Configuration.IConfigurationBuilder' to 'Microsoft.AspNetCore.Builder.WebApplicationBuilder'
}
}
2 changes: 1 addition & 1 deletion tests/PandaVaultClient.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Loading