Skip to content

Commit

Permalink
[BUG] Fix environment variables (#106)
Browse files Browse the repository at this point in the history
Environment variable from the helmchart are now properly being mapped from the environment, to the runtime.
  • Loading branch information
sahma19 authored Aug 8, 2024
1 parent 1a74125 commit fb45c5d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 36 deletions.
8 changes: 5 additions & 3 deletions chart/templates/deployment-stamp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ spec:
- name: Retry__RegistryTransactionStillProcessingRetryCount
value: {{ .Values.messageBroker.retry.registryTransactionStillProcessingRetryCount | quote }}

{{- range .Values.registries }}
- name: RegistryUrls__{{ .name }}
value: {{ .address }}
{{- range $i, $registry := .Values.registries }}
- name: Registries__{{ $i }}__name
value: {{ $registry.name }}
- name: Registries__{{ $i }}__address
value: {{ $registry.address }}
{{- end }}

{{- range .Values.issuerPrivateKeyPems }}
Expand Down
21 changes: 14 additions & 7 deletions src/ProjectOrigin.Stamp.Server/Options/RegistryOptions.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
using Microsoft.Extensions.DependencyInjection;
using ProjectOrigin.HierarchicalDeterministicKeys.Implementations;
using ProjectOrigin.HierarchicalDeterministicKeys.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using ProjectOrigin.HierarchicalDeterministicKeys.Implementations;
using ProjectOrigin.HierarchicalDeterministicKeys.Interfaces;

namespace ProjectOrigin.Stamp.Server.Options;

public class Registry
{
public string Name { get; set; }

Check warning on line 13 in src/ProjectOrigin.Stamp.Server/Options/RegistryOptions.cs

View workflow job for this annotation

GitHub Actions / analyse / sonar-analysis

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 13 in src/ProjectOrigin.Stamp.Server/Options/RegistryOptions.cs

View workflow job for this annotation

GitHub Actions / analyse / sonar-analysis

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 13 in src/ProjectOrigin.Stamp.Server/Options/RegistryOptions.cs

View workflow job for this annotation

GitHub Actions / analyse / sonar-analysis

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Address { get; set; }

Check warning on line 14 in src/ProjectOrigin.Stamp.Server/Options/RegistryOptions.cs

View workflow job for this annotation

GitHub Actions / analyse / sonar-analysis

Non-nullable property 'Address' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 14 in src/ProjectOrigin.Stamp.Server/Options/RegistryOptions.cs

View workflow job for this annotation

GitHub Actions / analyse / sonar-analysis

Non-nullable property 'Address' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 14 in src/ProjectOrigin.Stamp.Server/Options/RegistryOptions.cs

View workflow job for this annotation

GitHub Actions / analyse / sonar-analysis

Non-nullable property 'Address' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

public class RegistryOptions
{
[Required]
public Dictionary<string, string> RegistryUrls { get; set; } = new Dictionary<string, string>();
public IList<Registry> Registries { get; set; } = new List<Registry>();

[Required]
public Dictionary<string, byte[]> IssuerPrivateKeyPems { get; set; } = new Dictionary<string, byte[]>();
Expand Down Expand Up @@ -43,12 +49,13 @@ public IPrivateKey GetIssuerKey(string gridArea)

public string GetRegistryUrl(string name)
{
if (RegistryUrls.TryGetValue(name, out var url))
var foundRegistry = Registries.SingleOrDefault(registry => registry.Name == name);
if (foundRegistry is not null)
{
return url;
return foundRegistry.Address;
}

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

Expand Down
1 change: 1 addition & 0 deletions src/ProjectOrigin.Stamp.Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
Expand Down
20 changes: 10 additions & 10 deletions src/ProjectOrigin.Stamp.Test/CertificateIssuingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task WhenEndDateIsBeforeStartDate_BadRequest()
}
};

var response = await client.PostCertificate(recipientId, _fixture.RegistryOptions.RegistryUrls.First().Key, gsrn, cert);
var response = await client.PostCertificate(recipientId, _fixture.RegistryOptions.Registries.First().Name, gsrn, cert);
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}

Expand Down Expand Up @@ -100,7 +100,7 @@ public async Task WhenCertificateWithMeteringPointIdStartAndEndAlreadyExists_Con
}
};

var response = await client.PostCertificate(recipientId, _fixture.RegistryOptions.RegistryUrls.First().Key, gsrn, cert);
var response = await client.PostCertificate(recipientId, _fixture.RegistryOptions.Registries.First().Name, gsrn, cert);
response.StatusCode.Should().Be(HttpStatusCode.Accepted);

var cert2 = new CertificateDto
Expand All @@ -122,7 +122,7 @@ public async Task WhenCertificateWithMeteringPointIdStartAndEndAlreadyExists_Con
}
};

var response2 = await client.PostCertificate(recipientId, _fixture.RegistryOptions.RegistryUrls.First().Key, gsrn, cert2);
var response2 = await client.PostCertificate(recipientId, _fixture.RegistryOptions.Registries.First().Name, gsrn, cert2);
response2.StatusCode.Should().Be(HttpStatusCode.Conflict);
}

Expand Down Expand Up @@ -156,10 +156,10 @@ public async Task WhenCertificateAlreadyExists_Conflict()
}
};

var response = await client.PostCertificate(recipientId, _fixture.RegistryOptions.RegistryUrls.First().Key, gsrn, cert);
var response = await client.PostCertificate(recipientId, _fixture.RegistryOptions.Registries.First().Name, gsrn, cert);
response.StatusCode.Should().Be(HttpStatusCode.Accepted);

response = await client.PostCertificate(recipientId, _fixture.RegistryOptions.RegistryUrls.First().Key, "1234", cert);
response = await client.PostCertificate(recipientId, _fixture.RegistryOptions.Registries.First().Name, "1234", cert);
response.StatusCode.Should().Be(HttpStatusCode.Conflict);
}

Expand Down Expand Up @@ -188,7 +188,7 @@ public async Task WhenNonExistingRecipient_NotFound()
}
};

var response = await client.PostCertificate(Guid.NewGuid(), _fixture.RegistryOptions.RegistryUrls.First().Key, gsrn, cert);
var response = await client.PostCertificate(Guid.NewGuid(), _fixture.RegistryOptions.Registries.First().Name, gsrn, cert);
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
}

Expand Down Expand Up @@ -224,7 +224,7 @@ public async Task IssueCertificate(CertificateType type)
}
};

var response = await client.PostCertificate(recipientId, _fixture.RegistryOptions.RegistryUrls.First().Key, gsrn, cert);
var response = await client.PostCertificate(recipientId, _fixture.RegistryOptions.Registries.First().Name, gsrn, cert);
response.StatusCode.Should().Be(HttpStatusCode.Accepted);

var certs = await walletClient.RepeatedlyGetCertificatesUntil(certs => certs.Any());
Expand All @@ -233,7 +233,7 @@ public async Task IssueCertificate(CertificateType type)
var queriedCert = certs.First();

queriedCert.FederatedStreamId.StreamId.Should().Be(cert.Id);
queriedCert.FederatedStreamId.Registry.Should().Be(_fixture.RegistryOptions.RegistryUrls.First().Key);
queriedCert.FederatedStreamId.Registry.Should().Be(_fixture.RegistryOptions.Registries.First().Name);
queriedCert.Quantity.Should().Be(cert.Quantity);
queriedCert.Start.Should().Be(cert.Start);
queriedCert.End.Should().Be(cert.End);
Expand Down Expand Up @@ -284,7 +284,7 @@ public async Task IssueFiveCertificates(CertificateType type)

foreach (var cert in certs)
{
var response = await client.PostCertificate(recipientId, _fixture.RegistryOptions.RegistryUrls.First().Key, gsrn, cert);
var response = await client.PostCertificate(recipientId, _fixture.RegistryOptions.Registries.First().Name, gsrn, cert);
response.StatusCode.Should().Be(HttpStatusCode.Accepted);
}

Expand All @@ -296,7 +296,7 @@ public async Task IssueFiveCertificates(CertificateType type)
for (int i = 0; i < certsCount; i++)
{
granularCertificates[i].FederatedStreamId.StreamId.Should().Be(certs[i].Id);
granularCertificates[i].FederatedStreamId.Registry.Should().Be(_fixture.RegistryOptions.RegistryUrls.First().Key);
granularCertificates[i].FederatedStreamId.Registry.Should().Be(_fixture.RegistryOptions.Registries.First().Name);
granularCertificates[i].Start.Should().Be(certs[i].Start);
granularCertificates[i].End.Should().Be(certs[i].End);
granularCertificates[i].GridArea.Should().Be(certs[i].GridArea);
Expand Down
19 changes: 8 additions & 11 deletions src/ProjectOrigin.Stamp.Test/Options/RegistryOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
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>
Registries = new List<Server.Options.Registry>
{
{ "Narnia", "http://foo" },
{ "TestRegistry", "http://foo" },
{ "death-star", "http://foo" }
new() { Name = "Narnia", Address = "http://foo"},
new() {Name = "TestRegistry", Address = "http://foo" },
new() {Name = "death-star", Address = "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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public ProjectOriginStack()
{ "DK1", Encoding.UTF8.GetBytes(Dk1IssuerKey.ExportPkixText()) },
{ "DK2", Encoding.UTF8.GetBytes(Dk2IssuerKey.ExportPkixText()) },
},
RegistryUrls = new Dictionary<string, string>
Registries = new List<Server.Options.Registry>
{
{ RegistryName, RegistryUrl }
new() {Name = RegistryName, Address = RegistryUrl }
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class TestServerFixture<TStartup> : IDisposable where TStartup : class

public RegistryOptions RegistryOptions { get; set; } = new()
{
RegistryUrls = new Dictionary<string, string>(),
Registries = new List<Server.Options.Registry>(),
IssuerPrivateKeyPems = new Dictionary<string, byte[]>()
};

Expand Down Expand Up @@ -113,9 +113,11 @@ private void EnsureServer()
{"Retry:RegistryTransactionStillProcessingRetryCount", RetryOptions.RegistryTransactionStillProcessingRetryCount.ToString()}
};

foreach (var reg in RegistryOptions.RegistryUrls)
for (var i = 0; i < RegistryOptions.Registries.Count; i++)
{
envVariables.Add($"RegistryUrls:{reg.Key}", reg.Value);
var registry = RegistryOptions.Registries[i];
envVariables.Add($"Registries:{i}:Name", registry.Name);
envVariables.Add($"Registries:{i}:Address", registry.Address);
}

foreach (var pem in RegistryOptions.IssuerPrivateKeyPems)
Expand Down

0 comments on commit fb45c5d

Please sign in to comment.