From 786df2b90ca9982ed0c743b74b07ee69c7671fb0 Mon Sep 17 00:00:00 2001 From: Daniel Kuschny Date: Fri, 12 Jan 2024 14:11:14 +0100 Subject: [PATCH] Improve some logs and allow disabling of signing --- SigningServer.Client/Program.cs | 6 ++++++ SigningServer.Client/SigningClientConfiguration.cs | 2 ++ SigningServer.ClientCore/SigningClient.cs | 6 +++--- .../SigningClientConfiguration.cs | 12 +++++++++++- SigningServer.ClientCore/SigningClientRunner.cs | 6 ++++++ SigningServer.StandaloneClient/Program.cs | 6 ++++++ .../StandaloneSigningClientConfiguration.cs | 2 ++ 7 files changed, 36 insertions(+), 4 deletions(-) diff --git a/SigningServer.Client/Program.cs b/SigningServer.Client/Program.cs index c4e8a42..9b42470 100644 --- a/SigningServer.Client/Program.cs +++ b/SigningServer.Client/Program.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Configuration.EnvironmentVariables; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -31,6 +32,11 @@ private static async Task Main(string[] args) }) .ConfigureAppConfiguration(config => { + foreach (var envSources in config.Sources.OfType().ToArray()) + { + config.Sources.Remove(envSources); + } + config.AddEnvironmentVariables("SIGNINGSERVER_CLIENT_"); config.AddJsonFile("config.json", optional: true); }) .ConfigureServices(services => diff --git a/SigningServer.Client/SigningClientConfiguration.cs b/SigningServer.Client/SigningClientConfiguration.cs index b71024d..ab06c3e 100644 --- a/SigningServer.Client/SigningClientConfiguration.cs +++ b/SigningServer.Client/SigningClientConfiguration.cs @@ -14,6 +14,8 @@ public class SigningClientConfiguration : SigningClientConfigurationBase /// public string SigningServer { get; set; } = string.Empty; + public override string CredentialInfo => Username; + /// /// The username for authentication and cerificate selection. /// diff --git a/SigningServer.ClientCore/SigningClient.cs b/SigningServer.ClientCore/SigningClient.cs index b423ca3..faf0b53 100644 --- a/SigningServer.ClientCore/SigningClient.cs +++ b/SigningServer.ClientCore/SigningClient.cs @@ -131,7 +131,7 @@ await File.WriteAllBytesAsync(Configuration.LoadCertificatePath!, var error = $"Certificate Loading Failed with error '{responseDto.ErrorMessage}'"; throw new SigningFailedException(error); case LoadCertificateResponseStatus.CertificateNotLoadedUnauthorized: - Logger.LogError("The specified username and password are not recognized on the server"); + Logger.LogError("The specified username and password are not recognized on the server ({Status}, {Username})", responseDto.Status, Configuration.CredentialInfo); throw new UnauthorizedAccessException(); default: throw new ArgumentOutOfRangeException(); @@ -194,7 +194,7 @@ await File.WriteAllBytesAsync(signatureFile, Convert.FromBase64String(responseDt $"Signing Failed with error '{responseDto.ErrorMessage}' (sign time: {responseDto.SignTimeInMilliseconds:0}ms)"; throw new SigningFailedException(error); case SignHashResponseStatus.HashNotSignedUnauthorized: - Logger.LogError("The specified username and password are not recognized on the server"); + Logger.LogError("The specified username and password are not recognized on the server ({Status}, {Username})", responseDto.Status, Configuration.CredentialInfo); throw new UnauthorizedAccessException(); default: throw new ArgumentOutOfRangeException(); @@ -341,7 +341,7 @@ void WriteResponseInfo() $"Signing Failed with error '{errorMessage}' (upload time: {uploadTime.TotalMilliseconds:0}ms, sign time: {signTime.TotalMilliseconds:0}ms)"; throw new SigningFailedException(error); case SignFileResponseStatus.FileNotSignedUnauthorized: - Logger.LogError("The specified username and password are not recognized on the server"); + Logger.LogError("The specified username and password are not recognized on the server ({Status}, {Username})", status, Configuration.CredentialInfo); throw new UnauthorizedAccessException(); default: throw new ArgumentOutOfRangeException(); diff --git a/SigningServer.ClientCore/SigningClientConfiguration.cs b/SigningServer.ClientCore/SigningClientConfiguration.cs index f8e55e5..1923e1d 100644 --- a/SigningServer.ClientCore/SigningClientConfiguration.cs +++ b/SigningServer.ClientCore/SigningClientConfiguration.cs @@ -11,8 +11,18 @@ namespace SigningServer.ClientCore; /// /// Represents the signing client /// -public class SigningClientConfigurationBase +public abstract class SigningClientConfigurationBase { + /// + /// Whether to execute signing or not, useful if you have to enable/disable signing temporarily. + /// + public bool IsSigningDisabled { get; set; } + + /// + /// Gets the credential info to use for authentication and certificate selection. + /// + public abstract string CredentialInfo { get; } + /// /// Whether to overwrite existing signatures or fail when signatures are present. /// diff --git a/SigningServer.ClientCore/SigningClientRunner.cs b/SigningServer.ClientCore/SigningClientRunner.cs index 815efd8..3394f48 100644 --- a/SigningServer.ClientCore/SigningClientRunner.cs +++ b/SigningServer.ClientCore/SigningClientRunner.cs @@ -37,6 +37,12 @@ public async Task RunAsync() return; } + if (configuration.IsSigningDisabled) + { + _logger.LogWarning("Signing was disabled by configuration"); + return; + } + foreach (var source in configuration.Sources) { if (!File.Exists(source) && !Directory.Exists(source)) diff --git a/SigningServer.StandaloneClient/Program.cs b/SigningServer.StandaloneClient/Program.cs index bd82ccb..4088e5f 100644 --- a/SigningServer.StandaloneClient/Program.cs +++ b/SigningServer.StandaloneClient/Program.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Configuration.EnvironmentVariables; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -32,6 +33,11 @@ private static async Task Main(string[] args) }) .ConfigureAppConfiguration(config => { + foreach (var envSources in config.Sources.OfType().ToArray()) + { + config.Sources.Remove(envSources); + } + config.AddEnvironmentVariables("SIGNINGSERVER_CLIENT_"); config.AddJsonFile("config.json", optional: true); }) .ConfigureServices(services => diff --git a/SigningServer.StandaloneClient/StandaloneSigningClientConfiguration.cs b/SigningServer.StandaloneClient/StandaloneSigningClientConfiguration.cs index 9f1d085..2085df5 100644 --- a/SigningServer.StandaloneClient/StandaloneSigningClientConfiguration.cs +++ b/SigningServer.StandaloneClient/StandaloneSigningClientConfiguration.cs @@ -21,6 +21,8 @@ public class StandaloneSigningClientConfiguration : SigningClientConfigurationBa { public ServerType ServerType { get; set; } + public override string CredentialInfo => "local"; + /// /// A RFC-3161 compliant timestamping server which should be used. ///