From c86d4c88d9187c0a0b07eece1972bee75f1b6b52 Mon Sep 17 00:00:00 2001 From: Paul Volavsek Date: Fri, 3 Nov 2023 15:50:30 +0100 Subject: [PATCH 1/5] allow dynamic port for processhostservice --- .../Configuration/Configuration.cs | 2 +- .../Commands/RunCommand.cs | 2 + .../ProcessHost/ProcessHostMonarcStartup.cs | 40 ++++++++++++++++++- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/fiskaltrust.Launcher.Common/Configuration/Configuration.cs b/src/fiskaltrust.Launcher.Common/Configuration/Configuration.cs index cd02e352..6f6f7232 100644 --- a/src/fiskaltrust.Launcher.Common/Configuration/Configuration.cs +++ b/src/fiskaltrust.Launcher.Common/Configuration/Configuration.cs @@ -79,7 +79,7 @@ private T WithDefault(T value, Func defaultValue) private int? _launcherPort; [JsonPropertyName("launcherPort")] - public int? LauncherPort { get => WithDefault(_launcherPort, 5050); set => _launcherPort = value; } + public int? LauncherPort { get => WithDefault(_launcherPort, 0); set => _launcherPort = value; } private string? _serviceFolder; [JsonPropertyName("serviceFolder")] diff --git a/src/fiskaltrust.Launcher/Commands/RunCommand.cs b/src/fiskaltrust.Launcher/Commands/RunCommand.cs index ab8abd7a..a9da683d 100644 --- a/src/fiskaltrust.Launcher/Commands/RunCommand.cs +++ b/src/fiskaltrust.Launcher/Commands/RunCommand.cs @@ -8,6 +8,8 @@ using fiskaltrust.Launcher.Extensions; using fiskaltrust.Launcher.Helpers; using Microsoft.AspNetCore.Server.Kestrel.Core; +using Microsoft.AspNetCore.Hosting.Server; +using Microsoft.AspNetCore.Hosting.Server.Features; namespace fiskaltrust.Launcher.Commands { diff --git a/src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarcStartup.cs b/src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarcStartup.cs index 377f0f22..3a05b676 100644 --- a/src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarcStartup.cs +++ b/src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarcStartup.cs @@ -1,13 +1,18 @@ +using System.Security.Policy; using fiskaltrust.Launcher.Common.Configuration; using fiskaltrust.Launcher.Constants; using fiskaltrust.Launcher.Download; using fiskaltrust.Launcher.Extensions; using fiskaltrust.Launcher.Helpers; using fiskaltrust.storage.serialization.V0; +using Microsoft.AspNetCore.Hosting.Server; +using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.Extensions.Hosting.WindowsServices; namespace fiskaltrust.Launcher.ProcessHost { + public record ProcessHostServicePort(int Value); + public class ProcessHostMonarcStartup : BackgroundService { public class AlreadyLoggedException : Exception { } @@ -20,8 +25,9 @@ public class AlreadyLoggedException : Exception { } private readonly ILoggerFactory _loggerFactory; private readonly ILifetime _lifetime; private readonly LauncherExecutablePath _launcherExecutablePath; + private readonly TaskCompletionSource _kestrelReady; - public ProcessHostMonarcStartup(ILoggerFactory loggerFactory, ILogger logger, Dictionary hosts, LauncherConfiguration launcherConfiguration, ftCashBoxConfiguration cashBoxConfiguration, PackageDownloader downloader, ILifetime lifetime, LauncherExecutablePath launcherExecutablePath) + public ProcessHostMonarcStartup(ILoggerFactory loggerFactory, ILogger logger, Dictionary hosts, LauncherConfiguration launcherConfiguration, ftCashBoxConfiguration cashBoxConfiguration, PackageDownloader downloader, ILifetime lifetime, LauncherExecutablePath launcherExecutablePath, IHostApplicationLifetime hostApplicationLifetime, IServer server) { _loggerFactory = loggerFactory; _logger = logger; @@ -31,14 +37,31 @@ public ProcessHostMonarcStartup(ILoggerFactory loggerFactory, ILogger(); + + hostApplicationLifetime.ApplicationStarted.Register(() => + { + try + { + _kestrelReady.TrySetResult(new Uri(server.Features.Get()!.Addresses!.First())); + } + catch (Exception e) + { + _kestrelReady.TrySetException(e); + } + }); } protected override async Task ExecuteAsync(CancellationToken cancellationToken) { _lifetime.ApplicationLifetime.ApplicationStopping.Register(() => _logger.LogInformation("Shutting down launcher.")); + cancellationToken.Register(() => _kestrelReady.TrySetCanceled()); + StartupLogging(); + _downloader.CopyPackagesToCache(); + try { foreach (var scu in _cashBoxConfiguration.ftSignaturCreationDevices) @@ -116,6 +139,21 @@ private async Task StartProcessHostMonarch(PackageConfiguration configuration, P throw new AlreadyLoggedException(); } + if (_launcherConfiguration.LauncherPort == 0) + { + try + { + var url = await _kestrelReady.Task.ConfigureAwait(false); + _launcherConfiguration.LauncherPort = url.Port; + _logger.LogInformation("ProcessHostService running on {url}", url); + } + catch (Exception e) + { + _logger.LogError(e, "Could not get Kestrel port."); + throw new AlreadyLoggedException(); + } + } + var monarch = new ProcessHostMonarch( _loggerFactory.CreateLogger(), _launcherConfiguration, From afe5d87b518d80a72cea286854d8571dc216a730 Mon Sep 17 00:00:00 2001 From: Paul Volavsek Date: Fri, 3 Nov 2023 15:53:02 +0100 Subject: [PATCH 2/5] update docs --- README.md | 71 +++++++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 10dbcf7a..30d65bb4 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ The configuration file should contain the following config keys: "ftCashBoxId": "", // string "accessToken": "", // string - "launcherPort": "", // int (default: 5050) + "launcherPort": "", // int (default: 0) "serviceFolder": "", // string (default-windows: "C:/ProgramData/fiskaltrust", default-linux: "/var/lib/fiskaltrust", default-macos: "/Library/Application Support/fiskaltrust") "sandbox": "", // bool (default: true) "useOffline": "", // bool (default: false) @@ -117,35 +117,35 @@ All of these config keys can be overridden using the corresponding cli arguments The `run` command of the fiskaltrust.Launcher is used to execute the launcher, providing users with various options to configure its behaviour and logging details. -| Option | Description | Default | -|---------------------------------------------------------------|----------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------| -| `--cashbox-id ` | Specifies the ID of the cashbox. | | -| `--access-token ` | Token used for authentication. | | -| `--sandbox` | Enables sandbox mode. | `false` | -| `--log-folder ` | Path to the folder where logs will be saved. | `"/logs"` | -| `--log-level ` | Determines the logging level. Accepts values like Critical, Debug, etc. | `"Information"` | -| `--launcher-configuration-file ` | Path to the launcher configuration file. | `"launcher.configuration.json"` | -| `--legacy-configuration-file ` | Path to the legacy configuration file. | `"fiskaltrust.exe.config"` | -| `--merge-legacy-config-if-exists` | If set, merges legacy configuration if it exists. | `true` | -| `--launcher-port ` | Specifies the port which the launcher will use for internal communication. | `5050` | -| `--use-offline` | Enables offline mode. | `false` | -| `--service-folder ` | Path to the service folder. | windows: `"C:/ProgramData/fiskaltrust"`
linux: `"/var/lib/fiskaltrust"`
macos: `"/Library/Application Support/fiskaltrust"` | -| `--configuration-url ` | URL to fetch the configuration from. | `"https://configuration[-sandbox].fiskaltrust.cloud"` | -| `--packages-url ` | URL to fetch packages from. | `"https://packages-2-0[-sandbox].fiskaltrust.cloud"` | -| `--package-cache ` | Cache directory for the packages. | `"/cache"` | -| `--helipad-url ` | URL for the helipad. | `"https://helipad[-sandbox].fiskaltrust.cloud"` | -| `--download-timeout-sec ` | Timeout for downloads in seconds. | `15` | -| `--download-retry ` | Number of times to retry a failed download. | `1` | -| `--ssl-validation` | Validates SSL certificates. | `true` | -| `--proxy ` | Proxy server details. | | -| `--processhost-ping-period-sec ` | Ping period for the process host in seconds. | `10` | -| `--cashbox-configuration-file ` | Path to the cashbox configuration file. | `""/service/Configuration-.json"` | -| `--tls-certificate-path ` | Path to the TLS certificate. | | -| `--tls-certificate-base64 ` | Base64 encoded TLS certificate. | | -| `--tls-certificate-password ` | Password for the TLS certificate. | | -| `--use-http-sys-binding ` | Uses HTTP sys binding. | `false` | -| `--use-legacy-data-protection ` | Enables use of legacy data protection. | `false` | -| `-?` , `-h` , `--help` | Displays help and usage information. | | +| Option | Description | Default | +|---------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------| +| `--cashbox-id ` | Specifies the ID of the cashbox. | | +| `--access-token ` | Token used for authentication. | | +| `--sandbox` | Enables sandbox mode. | `false` | +| `--log-folder ` | Path to the folder where logs will be saved. | `"/logs"` | +| `--log-level ` | Determines the logging level. Accepts values like Critical, Debug, etc. | `"Information"` | +| `--launcher-configuration-file ` | Path to the launcher configuration file. | `"launcher.configuration.json"` | +| `--legacy-configuration-file ` | Path to the legacy configuration file. | `"fiskaltrust.exe.config"` | +| `--merge-legacy-config-if-exists` | If set, merges legacy configuration if it exists. | `true` | +| `--launcher-port ` | Specifies the port which the launcher will use for internal communication. A dynamic binding is used by default. | `0` | +| `--use-offline` | Enables offline mode. | `false` | +| `--service-folder ` | Path to the service folder. | windows: `"C:/ProgramData/fiskaltrust"`
linux: `"/var/lib/fiskaltrust"`
macos: `"/Library/Application Support/fiskaltrust"` | +| `--configuration-url ` | URL to fetch the configuration from. | `"https://configuration[-sandbox].fiskaltrust.cloud"` | +| `--packages-url ` | URL to fetch packages from. | `"https://packages-2-0[-sandbox].fiskaltrust.cloud"` | +| `--package-cache ` | Cache directory for the packages. | `"/cache"` | +| `--helipad-url ` | URL for the helipad. | `"https://helipad[-sandbox].fiskaltrust.cloud"` | +| `--download-timeout-sec ` | Timeout for downloads in seconds. | `15` | +| `--download-retry ` | Number of times to retry a failed download. | `1` | +| `--ssl-validation` | Validates SSL certificates. | `true` | +| `--proxy ` | Proxy server details. | | +| `--processhost-ping-period-sec ` | Ping period for the process host in seconds. | `10` | +| `--cashbox-configuration-file ` | Path to the cashbox configuration file. | `""/service/Configuration-.json"` | +| `--tls-certificate-path ` | Path to the TLS certificate. | | +| `--tls-certificate-base64 ` | Base64 encoded TLS certificate. | | +| `--tls-certificate-password ` | Password for the TLS certificate. | | +| `--use-http-sys-binding ` | Uses HTTP sys binding. | `false` | +| `--use-legacy-data-protection ` | Enables use of legacy data protection. | `false` | +| `-?` , `-h` , `--help` | Displays help and usage information. | | ## `config` @@ -229,13 +229,13 @@ When using VS Code, please ensure that the following command line parameters are **Q:** Which market can test the launcher 2.0? -**A:** Right now only the German market can test the launcher 2.0. It is possible for everyone to register to the German sandbox and test the launcher 2.0. Also, we are working on making the launcher available for all market. +**A:** Right now only the German and Italian market can test the launcher 2.0. It is possible for everyone to register to the German/Italian sandbox and test the launcher 2.0. Also, we are working on making the launcher available for all market. --- **Q:** Is it possible to update the launcher version (e.g. from 1.3 to 2.0)? -**A:** It is possible to switch the launcher version from 1.3 to 2.0 using the version Launcher 2.0-Public Preview 3 and later versions. +**A:** It is possible to switch the launcher version from 1.3 to 2.0 using the version Launcher 2.0.0-rc.3 and later versions. --- @@ -250,15 +250,8 @@ HttpSysBinding has some limitations: * The launcher may need to be run as an administrator * No Tls certificates can be set ---- - -**Q:** The Launcher fails with the messages `Host ... has shutdown.` and `Restarting ...` . - -**A:** The Launcher could probably not bind to the configured `launcherPort` . Try setting another port in the configuration. This is a [known issue](https://github.com/fiskaltrust/middleware-launcher/issues/98) that will be fixed in a future version. - ## Known Issues -* For multiple Launcher installations on the same maching the `launcherPort` configuration parameter needs to be set to a different port for each running launcher. ([#98](https://github.com/fiskaltrust/middleware-launcher/issues/98)) * The Launcher has access problems when writing to the keyring on linux if run as a service. The launcher configuration parameter `useLegacyDataProtection` needs to be set to `true` as a workaround. ([#100](https://github.com/fiskaltrust/middleware-launcher/issues/100) ## Contributing From d227a8d5b9bec7f89ae1210be6494a8aa1632eed Mon Sep 17 00:00:00 2001 From: Paul Volavsek Date: Fri, 3 Nov 2023 15:58:04 +0100 Subject: [PATCH 3/5] update version file --- version.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.json b/version.json index 6243f34a..1ba953eb 100644 --- a/version.json +++ b/version.json @@ -1,5 +1,5 @@ { - "version": "2.0.0-rc.12", + "version": "2.0.0-rc.13", "releaseBranches": [ "^refs/tags/v\\d+(?:\\.\\d+)*(?:-.*)?$" ] From 88b7f3fa50f715b5da53b128f7e3d7a2f3cb29e0 Mon Sep 17 00:00:00 2001 From: Paul Volavsek Date: Fri, 3 Nov 2023 16:25:57 +0100 Subject: [PATCH 4/5] dont throw when cancellation is requested --- src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarcStartup.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarcStartup.cs b/src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarcStartup.cs index 3a05b676..bcedc333 100644 --- a/src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarcStartup.cs +++ b/src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarcStartup.cs @@ -149,6 +149,7 @@ private async Task StartProcessHostMonarch(PackageConfiguration configuration, P } catch (Exception e) { + if (cancellationToken.IsCancellationRequested) { return; } _logger.LogError(e, "Could not get Kestrel port."); throw new AlreadyLoggedException(); } From 8b06f003ad5526b1c4d1b4bd7feaffd7b5daa15f Mon Sep 17 00:00:00 2001 From: Paul Volavsek Date: Fri, 3 Nov 2023 16:30:00 +0100 Subject: [PATCH 5/5] change location of reading the port --- .../ProcessHost/ProcessHostMonarcStartup.cs | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarcStartup.cs b/src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarcStartup.cs index bcedc333..8efcd720 100644 --- a/src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarcStartup.cs +++ b/src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarcStartup.cs @@ -60,6 +60,22 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken) StartupLogging(); + if (_launcherConfiguration.LauncherPort == 0) + { + try + { + var url = await _kestrelReady.Task.ConfigureAwait(false); + _launcherConfiguration.LauncherPort = url.Port; + _logger.LogInformation("ProcessHostService running on {url}", url); + } + catch (Exception e) + { + if (cancellationToken.IsCancellationRequested) { return; } + _logger.LogError(e, "Could not get Kestrel port."); + throw new AlreadyLoggedException(); + } + } + _downloader.CopyPackagesToCache(); try @@ -139,22 +155,6 @@ private async Task StartProcessHostMonarch(PackageConfiguration configuration, P throw new AlreadyLoggedException(); } - if (_launcherConfiguration.LauncherPort == 0) - { - try - { - var url = await _kestrelReady.Task.ConfigureAwait(false); - _launcherConfiguration.LauncherPort = url.Port; - _logger.LogInformation("ProcessHostService running on {url}", url); - } - catch (Exception e) - { - if (cancellationToken.IsCancellationRequested) { return; } - _logger.LogError(e, "Could not get Kestrel port."); - throw new AlreadyLoggedException(); - } - } - var monarch = new ProcessHostMonarch( _loggerFactory.CreateLogger(), _launcherConfiguration,