-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Logging, keepAlive default på for å fikse problem med at maskinporten…
… token blir ikke refreshed (#137) * Lagt på logging * Endret fra CRLF til LF * Endret fra ILoggerFactory til ILogger * Endret struktur og til at den tar loggingfactory * Nytt forsøk * Logging og bedre eksempel applikasjon * Keepalive default true, mulighet for å sette interval selv og fikset keepalive funksjonaliteten som neppe har funket som den skal * Korrigert * Rydding * Tatt bort ref til lokal fil * Slettet utkommentert kode --------- Co-authored-by: Jarle Børsheim <[email protected]>
- Loading branch information
1 parent
c063bf3
commit f0290ce
Showing
18 changed files
with
642 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# IDEs and editors | ||
/.idea | ||
.c9/ | ||
*.sublime-workspace | ||
.vs/ | ||
|
||
*.swp | ||
*.*~ | ||
project.lock.json | ||
.DS_Store | ||
*.pyc | ||
nupkg/ | ||
|
||
# Visual Studio Code | ||
.vscode | ||
|
||
# User-specific files | ||
*.suo | ||
*.user | ||
*.userosscache | ||
*.sln.docstates | ||
|
||
# Build results | ||
[Dd]ebug/ | ||
[Dd]ebugPublic/ | ||
[Rr]elease/ | ||
[Rr]eleases/ | ||
x64/ | ||
x86/ | ||
build/ | ||
bld/ | ||
[Bb]in/ | ||
[Oo]bj/ | ||
[Oo]ut/ | ||
msbuild.log | ||
msbuild.err | ||
msbuild.wrn | ||
|
||
appsettings.Development.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
|
||
namespace ExampleApplication | ||
{ | ||
public class AppSettings | ||
{ | ||
public FiksIOConfig FiksIOConfig { get; set; } | ||
} | ||
|
||
public class FiksIOConfig | ||
{ | ||
public string ApiHost { get; set; } | ||
public int ApiPort { get; set; } | ||
public string ApiScheme { get; set; } | ||
public string AmqpHost { get; set; } | ||
public int AmqpPort { get; set; } | ||
public Guid FiksIoAccountId { get; set; } | ||
public Guid FiksIoIntegrationId { get; set; } | ||
public string FiksIoIntegrationPassword { get; set; } | ||
public string FiksIoIntegrationScope { get; set; } | ||
public string FiksIoPrivateKey { get; set; } | ||
public string MaskinPortenAudienceUrl { get; set; } | ||
public string MaskinPortenCompanyCertificateThumbprint { get; set; } | ||
public string MaskinPortenCompanyCertificatePath { get; set; } | ||
public string MaskinPortenCompanyCertificatePassword { get; set; } | ||
public string MaskinPortenIssuer { get; set; } | ||
public string MaskinPortenTokenUrl { get; set; } | ||
public string AsiceSigningPublicKey { get; set; } | ||
public string AsiceSigningPrivateKey { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace ExampleApplication | ||
{ | ||
public static class AppSettingsBuilder | ||
{ | ||
public static AppSettings CreateAppSettings(IConfiguration configuration) | ||
{ | ||
return configuration.GetSection("AppSettings").Get<AppSettings>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using System.IO; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Text; | ||
using KS.Fiks.IO.Client.Configuration; | ||
using Ks.Fiks.Maskinporten.Client; | ||
|
||
namespace ExampleApplication.FiksIO | ||
{ | ||
public static class FiksIoConfigurationBuilder | ||
{ | ||
|
||
// Create configuration with the fluent builder | ||
public static FiksIOConfiguration CreateConfiguration(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; | ||
var host = appSettings.FiksIOConfig.AmqpHost; | ||
var port = appSettings.FiksIOConfig.AmqpPort; | ||
|
||
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) | ||
.BuildConfiguration(host, port); | ||
} | ||
|
||
// Create a FiksIOConfiguration manually | ||
public static FiksIOConfiguration CreateConfig(string issuer, string p12Filename, string p12Password, string fiksIoAccountId, | ||
string fiksIoPrivateKeyPath, string integrasjonId, string integrasjonPassword) | ||
{ | ||
// ID-porten machine to machine configuration | ||
var maskinportenConfig = new MaskinportenClientConfiguration( | ||
audience: @"https://ver2.maskinporten.no/", // ID-porten audience path | ||
tokenEndpoint: @"https://ver2.maskinporten.no/token", // ID-porten token path | ||
issuer: issuer, // KS issuer name | ||
numberOfSecondsLeftBeforeExpire: 10, // The token will be refreshed 10 seconds before it expires | ||
certificate: new X509Certificate2(p12Filename, p12Password)); | ||
|
||
// Fiks IO account configuration | ||
var kontoConfig = new KontoConfiguration( | ||
kontoId: Guid.Parse(fiksIoAccountId) /* Fiks IO accountId as Guid */, | ||
privatNokkel: ReadFromFile( | ||
fiksIoPrivateKeyPath) /* Private key in PEM format, paired with the public key supplied to Fiks IO account */); | ||
|
||
|
||
// Id and password for integration associated to the Fiks IO account. | ||
var integrasjonConfig = new IntegrasjonConfiguration( | ||
Guid.Parse(integrasjonId) /* Integration id as Guid */, | ||
integrasjonPassword /* Integration password */); | ||
|
||
var asiceSigningConfig = new AsiceSigningConfiguration(new X509Certificate2(p12Filename, p12Password)); | ||
|
||
|
||
// Optional: Use custom api host (i.e. for connecting to test api) | ||
var apiConfig = new ApiConfiguration( | ||
scheme: "https", | ||
host: "api.fiks.test.ks.no", | ||
port: 443); | ||
|
||
// Optional: Use custom amqp host (i.e. for connection to test queue) | ||
var amqpConfig = new AmqpConfiguration( | ||
host: "io.fiks.test.ks.no", | ||
port: 5671); | ||
|
||
// Combine all configurations | ||
return new FiksIOConfiguration(kontoConfig, integrasjonConfig, maskinportenConfig, asiceSigningConfig, apiConfig, amqpConfig); | ||
} | ||
|
||
private static string ReadFromFile(string path) | ||
{ | ||
return File.ReadAllText(path, Encoding.UTF8); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Net; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using KS.Fiks.IO.Client; | ||
using KS.Fiks.IO.Client.Models; | ||
using Microsoft.Extensions.Hosting; | ||
using Serilog; | ||
|
||
namespace ExampleApplication | ||
{ | ||
public class FiksIOSubscriber : BackgroundService | ||
{ | ||
private IFiksIOClient _fiksIoClient; | ||
private readonly AppSettings _appSettings; | ||
private static readonly ILogger Log = Serilog.Log.ForContext(MethodBase.GetCurrentMethod()?.DeclaringType); | ||
private Timer FiksIoClientStatusCheckTimer { get; set; } | ||
private const int HealthCheckInterval = 15 * 1000; | ||
|
||
public FiksIOSubscriber(IFiksIOClient fiksIoClient, AppSettings appSettings) | ||
{ | ||
_fiksIoClient = fiksIoClient; | ||
_appSettings = appSettings; | ||
|
||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
Log.Information("Application is starting subscribe"); | ||
SubscribeToFiksIOClient(); | ||
|
||
Log.Information($"FiksIOSubscriber is starting timer for simple health checks with interval of {HealthCheckInterval} ms"); | ||
|
||
FiksIoClientStatusCheckTimer = new Timer(WriteStatusToLog, null, HealthCheckInterval, HealthCheckInterval); | ||
|
||
await Task.CompletedTask; | ||
} | ||
|
||
private void WriteStatusToLog(object o) | ||
{ | ||
Log.Information($"FiksIOSubscriber status check - FiksIOClient connection IsOpen status: {_fiksIoClient.IsOpen()}"); | ||
Log.Information($"FiksIOSubscriber status check - Maskinporten reachable : {CheckMaskinportenIsReachable()}"); | ||
} | ||
|
||
private bool CheckMaskinportenIsReachable() | ||
{ | ||
var request = (HttpWebRequest) WebRequest.Create(_appSettings.FiksIOConfig.MaskinPortenAudienceUrl); | ||
request.Timeout = 5 * 1000; | ||
request.Method = "HEAD"; | ||
try | ||
{ | ||
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse()) | ||
{ | ||
return response.StatusCode == HttpStatusCode.OK; | ||
} | ||
} | ||
catch (WebException) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
private void OnReceivedMelding(object sender, MottattMeldingArgs mottatt) | ||
{ | ||
Log.Information("Message with messageId {MeldingId} and messagetype {MeldingsType} received. Message will be acked.", mottatt.Melding.MeldingId, | ||
mottatt.Melding.MeldingType); | ||
mottatt.SvarSender.Ack(); | ||
} | ||
|
||
private void SubscribeToFiksIOClient() | ||
{ | ||
var accountId = _appSettings.FiksIOConfig.FiksIoAccountId; | ||
Log.Information($"Starting subscribe on account {accountId}..."); | ||
_fiksIoClient.NewSubscription(OnReceivedMelding); | ||
} | ||
} | ||
} |
Oops, something went wrong.