Skip to content

Commit

Permalink
Prints env config on error (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnickelsen authored Jul 24, 2024
1 parent 0b07bf0 commit a74807d
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 37 deletions.
6 changes: 3 additions & 3 deletions chart/templates/deployment-stamp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ spec:
value: {{ .Values.messageBroker.retry.registryTransactionStillProcessingRetryCount | quote }}

{{- range .Values.registries }}
- name: Registry__RegistryUrls__{{ .name }}
- name: RegistryUrls__{{ .name }}
value: {{ .address }}
{{- end }}

{{- range .Values.issuerPrivateKeyPems }}
- name: Registry__IssuerPrivateKeyPems__{{ .gridArea }}
- name: IssuerPrivateKeyPems__{{ .gridArea }}
value: {{ .key }}
{{- end }}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
apiVersion: v1
kind: Service
metadata:
name: po-stamp
namespace: {{ .Release.Namespace }}
spec:
type: {{ .Values.service.type }}
selector:
app: po-stamp
ports:
- name: rest
protocol: TCP
port: 5000
targetPort: 5000
---
apiVersion: v1
kind: Service
metadata:
name: po-stamp
namespace: {{ .Release.Namespace }}
spec:
type: {{ .Values.service.type }}
selector:
app: po-stamp
ports:
- name: rest
protocol: TCP
port: 5000
targetPort: 5000
17 changes: 4 additions & 13 deletions src/ProjectOrigin.Stamp.Server/Options/RegistryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace ProjectOrigin.Stamp.Server.Options;

public class RegistryOptions
{
public const string Registry = nameof(Registry);

[Required]
public Dictionary<string, string> RegistryUrls { get; set; } = new Dictionary<string, string>();

Expand Down Expand Up @@ -39,7 +37,8 @@ public IPrivateKey GetIssuerKey(string gridArea)
return ToPrivateKey(issuerPrivateKeyPem);
}

throw new NotSupportedException($"Not supported GridArea {gridArea}");
string gridAreas = string.Join(", ", IssuerPrivateKeyPems.Keys);
throw new NotSupportedException($"Not supported GridArea {gridArea}. Supported GridAreas are: " + gridAreas);
}

public string GetRegistryUrl(string name)
Expand All @@ -49,18 +48,10 @@ public string GetRegistryUrl(string name)
return url;
}

throw new NotSupportedException($"RegistryName {name} not supported");
string registries = string.Join(", ", RegistryUrls.Keys);
throw new NotSupportedException($"RegistryName {name} not supported. Supported registries are: " + registries);
}

private static IPrivateKey ToPrivateKey(byte[] key)
=> new Ed25519Algorithm().ImportPrivateKeyText(Encoding.UTF8.GetString(key));
}

public static class OptionsExtensions
{
public static void AddRegistryOptions(this IServiceCollection services) =>
services.AddOptions<RegistryOptions>()
.BindConfiguration(RegistryOptions.Registry)
.ValidateDataAnnotations()
.ValidateOnStart();
}
6 changes: 5 additions & 1 deletion src/ProjectOrigin.Stamp.Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ public void ConfigureServices(IServiceCollection services)
.ValidateDataAnnotations()
.ValidateOnStart();

services.AddRegistryOptions();
services.AddOptions<RegistryOptions>()
.Bind(_configuration)
.ValidateDataAnnotations()
.ValidateOnStart();

services.AddHttpClient();

services.ConfigurePersistance(_configuration);
Expand Down
54 changes: 54 additions & 0 deletions src/ProjectOrigin.Stamp.Test/Options/RegistryOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using ProjectOrigin.Stamp.Server.Options;
using Xunit;

namespace ProjectOrigin.Stamp.Test.Options;

public class RegistryOptionsTests
{
[Fact]
public void ShouldTellSupportedRegistriesOnNotFoundRegistry()
{
// Arrange
var registryOptions = new RegistryOptions
{
RegistryUrls = new Dictionary<string, string>
{
{ "Narnia", "http://foo" },
{ "TestRegistry", "http://foo" },
{ "death-star", "http://foo" }
}
};

// Act
var sut = () => registryOptions.GetRegistryUrl("Narnia2");

// Assert
sut.Should().Throw<NotSupportedException>()
.WithMessage("RegistryName Narnia2 not supported. Supported registries are: Narnia, TestRegistry, death-star");
}

[Fact]
public void ShouldTellSupportedGridAreasOnNotFoundGridArea()
{
var registryOptions = new RegistryOptions
{
IssuerPrivateKeyPems = new Dictionary<string, byte[]>
{
{ "Narnia", Encoding.UTF8.GetBytes("key") },
{ "TestArea", Encoding.UTF8.GetBytes("key") },
{ "death-star", Encoding.UTF8.GetBytes("key") }
}
};

var sut = () => registryOptions.GetIssuerKey("Narnia2");

sut.Should().Throw<NotSupportedException>()
.WithMessage("Not supported GridArea Narnia2. Supported GridAreas are: Narnia, TestArea, death-star");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public async Task UpgradeDatabase()

var upgrader = new PostgresUpgrader(
loggerFactory.CreateLogger<PostgresUpgrader>(),
Options.Create(new PostgresOptions
Microsoft.Extensions.Options.Options.Create(new PostgresOptions
{
ConnectionString = _postgreSqlContainer.GetConnectionString()
}));

await upgrader.Upgrade();
}

public IDbConnectionFactory GetConnectionFactory() => new PostgresConnectionFactory(Options.Create(new PostgresOptions
public IDbConnectionFactory GetConnectionFactory() => new PostgresConnectionFactory(Microsoft.Extensions.Options.Options.Create(new PostgresOptions
{
ConnectionString = _postgreSqlContainer.GetConnectionString()
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ private void EnsureServer()

foreach (var reg in RegistryOptions.RegistryUrls)
{
envVariables.Add($"Registry:RegistryUrls:{reg.Key}", reg.Value);
envVariables.Add($"RegistryUrls:{reg.Key}", reg.Value);
}

foreach (var pem in RegistryOptions.IssuerPrivateKeyPems)
{
envVariables.Add($"Registry:IssuerPrivateKeyPems:{pem.Key}", Convert.ToBase64String(pem.Value));
envVariables.Add($"IssuerPrivateKeyPems:{pem.Key}", Convert.ToBase64String(pem.Value));
}

ConfigureHostConfiguration(envVariables);
Expand Down

0 comments on commit a74807d

Please sign in to comment.