Skip to content
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

Lagt til mulighet for å bygge en dev configuration via fluent builder #171

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
49 changes: 42 additions & 7 deletions ExampleApplication/FiksIO/FiksIOConfigurationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,37 @@ namespace ExampleApplication.FiksIO
public static class FiksIoConfigurationBuilder
{

// Create a configuration for the test environment with the fluent builder
/* Create a configuration for the test environment with the fluent builder.
* API, AMQP and maskinporten host urls and endpoints for test-environment are set by the client.
* Use this for easy setup in a test-environment.
*/
public static FiksIOConfiguration CreateTestConfiguration(AppSettings appSettings)
{
var accountId = appSettings.FiksIOConfig.FiksIoAccountId;
var privateKeyPath = appSettings.FiksIOConfig.FiksIoPrivateKey;
var integrationId = appSettings.FiksIOConfig.FiksIoIntegrationId;
var integrationPassword = appSettings.FiksIOConfig.FiksIoIntegrationPassword;
var issuer = appSettings.FiksIOConfig.MaskinPortenIssuer;
var certPath = appSettings.FiksIOConfig.MaskinPortenCompanyCertificatePath;
var certPassword = appSettings.FiksIOConfig.MaskinPortenCompanyCertificatePassword;
var asiceSigningPublicKey = appSettings.FiksIOConfig.AsiceSigningPublicKey;
var asiceSigningPrivateKey = appSettings.FiksIOConfig.AsiceSigningPrivateKey;

return FiksIOConfigurationBuilder
.Init()
.WithAmqpConfiguration("fiks-io-client-dotnet-example-application", 1, true,20 * 1000)
.WithMaskinportenConfiguration(new X509Certificate2(certPath, certPassword), issuer)
.WithFiksIntegrasjonConfiguration(integrationId, integrationPassword)
.WithFiksKontoConfiguration(accountId, ReadFromFile(privateKeyPath))
.WithAsiceSigningConfiguration(asiceSigningPublicKey, asiceSigningPrivateKey)
.BuildTestConfiguration();
}

/* Create a configuration for any environment with the fluent builder. All required settings must be set from app settings.
* API, AMQP and maskinporten settings must be set by you.
* Use this for configuration that can be used in any environment and the environment holds the settings.
*/

public static FiksIOConfiguration CreateConfiguration(AppSettings appSettings)
{
var accountId = appSettings.FiksIOConfig.FiksIoAccountId;
Expand All @@ -23,21 +53,26 @@ public static FiksIOConfiguration CreateConfiguration(AppSettings appSettings)
var asiceSigningPublicKey = appSettings.FiksIOConfig.AsiceSigningPublicKey;
var asiceSigningPrivateKey = appSettings.FiksIOConfig.AsiceSigningPrivateKey;
var apiHost = appSettings.FiksIOConfig.ApiHost;
var apiScheme = appSettings.FiksIOConfig.ApiScheme;
var apiPort = appSettings.FiksIOConfig.ApiPort;
var amqpHost = appSettings.FiksIOConfig.AmqpHost;
var amqpPort = appSettings.FiksIOConfig.AmqpPort;
var maskinportenAudience = appSettings.FiksIOConfig.MaskinPortenAudienceUrl;
var maskinportenTokenEndpoint = appSettings.FiksIOConfig.MaskinPortenTokenUrl;


return FiksIOConfigurationBuilder
.Init()
.WithAmqpConfiguration("fiks-io-client-dotnet-example-application", 1, true,20 * 1000)
.WithMaskinportenConfiguration(new X509Certificate2(certPath, certPassword), issuer)
.WithAmqpConfiguration("fiks-io-client-dotnet-example-application", 1, true,20 * 1000, amqpHost, amqpPort)
.WithMaskinportenConfiguration(new X509Certificate2(certPath, certPassword), issuer, maskinportenAudience, maskinportenTokenEndpoint)
.WithFiksIntegrasjonConfiguration(integrationId, integrationPassword)
.WithFiksKontoConfiguration(accountId, ReadFromFile(privateKeyPath))
.WithAsiceSigningConfiguration(asiceSigningPublicKey, asiceSigningPrivateKey)
.WithApiConfiguration(apiHost, apiPort)
.BuildTestConfiguration();
.WithApiConfiguration(apiHost, apiScheme, apiPort)
.BuildConfiguration();
}
// Create a FiksIOConfiguration manually. Use this if you want to use internal endpoints for testing, not Fiks-IO test or prod

// Create a FiksIOConfiguration manually.
public static FiksIOConfiguration CreateConfig(string issuer, string p12Filename, string p12Password, string fiksIoAccountId,
string fiksIoPrivateKeyPath, string integrasjonId, string integrasjonPassword)
{
Expand Down
2 changes: 1 addition & 1 deletion ExampleApplication/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static async Task Main(string[] args)

var loggerFactory = InitSerilogConfiguration();
var appSettings = AppSettingsBuilder.CreateAppSettings(configurationRoot);
var configuration = FiksIoConfigurationBuilder.CreateConfiguration(appSettings);
var configuration = FiksIoConfigurationBuilder.CreateTestConfiguration(appSettings);
var fiksIoClient = await FiksIOClient.CreateAsync(configuration, loggerFactory);

// Creating messageSender as a local instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ public void TestConfigurationWithAllRequiredConfigurations()
configuration.AmqpConfiguration.Host.Should().Be(AmqpConfiguration.TestHost);
}

[Fact]
public void FullConfigurationWithAllRequiredConfigurations()
{
var amqpHost = "testAmqpHost";
var apiHost = "testApiHost";
var maskinportenAudience = "testMaskinportenAudience";
var maskinportenTokenEndpoint = "testMaskinportenTokenEndpoint";

var configuration = FiksIOConfigurationBuilder
.Init()
.WithAmqpConfiguration("test_app", 10, host: amqpHost)
.WithApiConfiguration(apiHost)
.WithMaskinportenConfiguration(new X509Certificate2(), "test-issuer", maskinportenAudience, maskinportenTokenEndpoint)
.WithAsiceSigningConfiguration(new X509Certificate2())
.WithFiksIntegrasjonConfiguration(Guid.NewGuid(), "passord")
.WithFiksKontoConfiguration(Guid.NewGuid(), "liksom-en-private-key")
.BuildConfiguration();

configuration.ApiConfiguration.Host.Should().Be(apiHost);
configuration.AmqpConfiguration.Host.Should().Be(amqpHost);
configuration.MaskinportenConfiguration.Audience.Should().Be(maskinportenAudience);
configuration.MaskinportenConfiguration.TokenEndpoint.Should().Be(maskinportenTokenEndpoint);
}

[Fact]
public void ProdConfigurationWithAllRequiredConfigurations()
{
Expand All @@ -42,6 +66,54 @@ public void ProdConfigurationWithAllRequiredConfigurations()
configuration.AmqpConfiguration.Host.Should().Be(AmqpConfiguration.ProdHost);
}

[Fact]
public void TestConfigurationWithCustomConfigurations()
{
var amqpHost = "testAmqpHost";
var apiHost = "testApiHost";
var maskinportenAudience = "testMaskinportenAudience";
var maskinportenTokenEndpoint = "testMaskinportenTokenEndpoint";

var configuration = FiksIOConfigurationBuilder
.Init()
.WithAmqpConfiguration("test_app", 10, host: amqpHost)
.WithMaskinportenConfiguration(new X509Certificate2(), "test-issuer", maskinportenAudience, maskinportenTokenEndpoint)
.WithAsiceSigningConfiguration(new X509Certificate2())
.WithFiksIntegrasjonConfiguration(Guid.NewGuid(), "passord")
.WithFiksKontoConfiguration(Guid.NewGuid(), "liksom-en-private-key")
.WithApiConfiguration(apiHost)
.BuildTestConfiguration();

configuration.ApiConfiguration.Host.Should().Be(apiHost);
configuration.AmqpConfiguration.Host.Should().Be(amqpHost);
configuration.MaskinportenConfiguration.Audience.Should().Be(maskinportenAudience);
configuration.MaskinportenConfiguration.TokenEndpoint.Should().Be(maskinportenTokenEndpoint);
}

[Fact]
public void ProdConfigurationWithCustomConfigurations()
{
var amqpHost = "testAmqpHost";
var apiHost = "testApiHost";
var maskinportenAudience = "testMaskinportenAudience";
var maskinportenTokenEndpoint = "testMaskinportenTokenEndpoint";

var configuration = FiksIOConfigurationBuilder
.Init()
.WithAmqpConfiguration("test_app", 10, host: amqpHost)
.WithMaskinportenConfiguration(new X509Certificate2(), "test-issuer", maskinportenAudience, maskinportenTokenEndpoint)
.WithAsiceSigningConfiguration(new X509Certificate2())
.WithFiksIntegrasjonConfiguration(Guid.NewGuid(), "passord")
.WithFiksKontoConfiguration(Guid.NewGuid(), "liksom-en-private-key")
.WithApiConfiguration(apiHost)
.BuildProdConfiguration();

configuration.ApiConfiguration.Host.Should().Be(apiHost);
configuration.AmqpConfiguration.Host.Should().Be(amqpHost);
configuration.MaskinportenConfiguration.Audience.Should().Be(maskinportenAudience);
configuration.MaskinportenConfiguration.TokenEndpoint.Should().Be(maskinportenTokenEndpoint);
}

[Fact]
public void ConfigWithSinglPrivateKey()
{
Expand Down Expand Up @@ -75,7 +147,7 @@ public void ConfigWithMultiplePrivateKeys()
}

[Fact]
public void ConfigurationFailsWithoutCertificateInMaskinportenConfiguration()
public void ProdConfigurationFailsWithoutCertificateInMaskinportenConfiguration()
{
Assert.Throws<ArgumentException>(() =>
FiksIOConfigurationBuilder
Expand All @@ -88,7 +160,7 @@ public void ConfigurationFailsWithoutCertificateInMaskinportenConfiguration()
}

[Fact]
public void ConfigurationFailsWithoutAsiceSigningConfiguration()
public void ProdConfigurationFailsWithoutAsiceSigningConfiguration()
{
Assert.Throws<ArgumentException>(() =>
FiksIOConfigurationBuilder
Expand All @@ -101,7 +173,7 @@ public void ConfigurationFailsWithoutAsiceSigningConfiguration()
}

[Fact]
public void ConfigurationFailsWithoutMaskinportenConfiguration()
public void ProdConfigurationFailsWithoutMaskinportenConfiguration()
{
Assert.Throws<ArgumentException>(() =>
FiksIOConfigurationBuilder
Expand All @@ -113,7 +185,7 @@ public void ConfigurationFailsWithoutMaskinportenConfiguration()
}

[Fact]
public void ConfigurationFailsWithoutFiksIntegrasjonConfiguration()
public void ProdConfigurationFailsWithoutFiksIntegrasjonConfiguration()
{
Assert.Throws<ArgumentException>(() =>
FiksIOConfigurationBuilder
Expand All @@ -125,7 +197,7 @@ public void ConfigurationFailsWithoutFiksIntegrasjonConfiguration()
}

[Fact]
public void ConfigurationFailsWithoutFiksKontoConfiguration()
public void ProdConfigurationFailsWithoutFiksKontoConfiguration()
{
Assert.Throws<ArgumentException>(() =>
FiksIOConfigurationBuilder
Expand All @@ -135,5 +207,84 @@ public void ConfigurationFailsWithoutFiksKontoConfiguration()
.WithFiksIntegrasjonConfiguration(Guid.NewGuid(), "passord")
.BuildProdConfiguration());
}

[Fact]
public void FullConfigurationFailsWithoutMaskinportenTokenEndpoint()
{
var amqpHost = "testAmqpHost";
var apiHost = "testApiHost";
var maskinportenAudience = "testMaskinportenAudience";
var maskinportenTokenEndpoint = "testMaskinportenTokenEndpoint";

Assert.Throws<ArgumentException>(() =>
FiksIOConfigurationBuilder
.Init()
.WithAmqpConfiguration("test_app", 10, host: amqpHost)
.WithApiConfiguration(apiHost)
.WithMaskinportenConfiguration(new X509Certificate2(), "test-issuer", maskinportenAudience)
.WithAsiceSigningConfiguration(new X509Certificate2())
.WithFiksIntegrasjonConfiguration(Guid.NewGuid(), "passord")
.WithFiksKontoConfiguration(Guid.NewGuid(), "liksom-en-private-key")
.BuildConfiguration());
}

[Fact]
public void FullConfigurationFailsWithoutMaskinportenAudience()
{
var amqpHost = "testAmqpHost";
var apiHost = "testApiHost";
var maskinportenAudience = "testMaskinportenAudience";
var maskinportenTokenEndpoint = "testMaskinportenTokenEndpoint";

Assert.Throws<ArgumentException>(() =>
FiksIOConfigurationBuilder
.Init()
.WithAmqpConfiguration("test_app", 10, host: amqpHost)
.WithApiConfiguration(apiHost)
.WithMaskinportenConfiguration(new X509Certificate2(), "test-issuer", tokenEndpoint: maskinportenTokenEndpoint)
.WithAsiceSigningConfiguration(new X509Certificate2())
.WithFiksIntegrasjonConfiguration(Guid.NewGuid(), "passord")
.WithFiksKontoConfiguration(Guid.NewGuid(), "liksom-en-private-key")
.BuildConfiguration());
}

[Fact]
public void FullConfigurationFailsWithoutApiHost()
{
var amqpHost = "testAmqpHost";
var apiHost = "testApiHost";
var maskinportenAudience = "testMaskinportenAudience";
var maskinportenTokenEndpoint = "testMaskinportenTokenEndpoint";

Assert.Throws<ArgumentException>(() =>
FiksIOConfigurationBuilder
.Init()
.WithAmqpConfiguration("test_app", 10, host: amqpHost)
.WithMaskinportenConfiguration(new X509Certificate2(), "test-issuer", audience: maskinportenAudience, tokenEndpoint: maskinportenTokenEndpoint)
.WithAsiceSigningConfiguration(new X509Certificate2())
.WithFiksIntegrasjonConfiguration(Guid.NewGuid(), "passord")
.WithFiksKontoConfiguration(Guid.NewGuid(), "liksom-en-private-key")
.BuildConfiguration());
}

[Fact]
public void FullConfigurationFailsWithoutAmqpHost()
{
var amqpHost = "testAmqpHost";
var apiHost = "testApiHost";
var maskinportenAudience = "testMaskinportenAudience";
var maskinportenTokenEndpoint = "testMaskinportenTokenEndpoint";

Assert.Throws<ArgumentException>(() =>
FiksIOConfigurationBuilder
.Init()
.WithAmqpConfiguration("test_app", 10)
.WithApiConfiguration(apiHost)
.WithMaskinportenConfiguration(new X509Certificate2(), "test-issuer", audience: maskinportenAudience, tokenEndpoint: maskinportenTokenEndpoint)
.WithAsiceSigningConfiguration(new X509Certificate2())
.WithFiksIntegrasjonConfiguration(Guid.NewGuid(), "passord")
.WithFiksKontoConfiguration(Guid.NewGuid(), "liksom-en-private-key")
.BuildConfiguration());
}
}
}
3 changes: 2 additions & 1 deletion KS.Fiks.IO.Client/Configuration/AmqpConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ public class AmqpConfiguration
public const string ProdHost = "io.fiks.ks.no";
public const string TestHost = "io.fiks.test.ks.no";
public const int DefaultKeepAliveHealthCheckInterval = 1 * 60 * 1000;
public const int DefaultPort = 5671;

public AmqpConfiguration(string host, int port = 5671, SslOption sslOption = null, string applicationName = "Fiks IO klient (dotnet)", ushort prefetchCount = 10, bool keepAlive = true, int keepAliveCheckInterval = DefaultKeepAliveHealthCheckInterval)
public AmqpConfiguration(string host, int port = DefaultPort, SslOption sslOption = null, string applicationName = "Fiks IO klient (dotnet)", ushort prefetchCount = 10, bool keepAlive = true, int keepAliveCheckInterval = DefaultKeepAliveHealthCheckInterval)
{
Host = host;
Port = port;
Expand Down
5 changes: 2 additions & 3 deletions KS.Fiks.IO.Client/Configuration/ApiConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ public class ApiConfiguration
{
public const string ProdHost = "api.fiks.ks.no";
public const string TestHost = "api.fiks.test.ks.no";
private const string DefaultScheme = "https";

private const int DefaultPort = 443;
public const string DefaultScheme = "https";
public const int DefaultPort = 443;

public ApiConfiguration(string scheme = null, string host = null, int? port = null)
{
Expand Down
10 changes: 10 additions & 0 deletions KS.Fiks.IO.Client/Configuration/FiksIOConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,15 @@ public static MaskinportenClientConfiguration CreateMaskinportenTestConfig(strin
numberOfSecondsLeftBeforeExpire: 10,
certificate: certificate);
}

public static MaskinportenClientConfiguration CreateMaskinportenConfig(string audience, string tokenEndpoint, string issuer, X509Certificate2 certificate)
{
return new MaskinportenClientConfiguration(
audience: audience,
tokenEndpoint: tokenEndpoint,
issuer: issuer, // KS issuer name
numberOfSecondsLeftBeforeExpire: 10,
certificate: certificate);
}
}
}
Loading