Skip to content

Commit

Permalink
Improve some logs and allow disabling of signing
Browse files Browse the repository at this point in the history
  • Loading branch information
kusc-leica committed Jan 12, 2024
1 parent 08de130 commit 786df2b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
6 changes: 6 additions & 0 deletions SigningServer.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -31,6 +32,11 @@ private static async Task Main(string[] args)
})
.ConfigureAppConfiguration(config =>
{
foreach (var envSources in config.Sources.OfType<EnvironmentVariablesConfigurationSource>().ToArray())
{
config.Sources.Remove(envSources);
}
config.AddEnvironmentVariables("SIGNINGSERVER_CLIENT_");
config.AddJsonFile("config.json", optional: true);
})
.ConfigureServices(services =>
Expand Down
2 changes: 2 additions & 0 deletions SigningServer.Client/SigningClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class SigningClientConfiguration : SigningClientConfigurationBase
/// </summary>
public string SigningServer { get; set; } = string.Empty;

public override string CredentialInfo => Username;

/// <summary>
/// The username for authentication and cerificate selection.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions SigningServer.ClientCore/SigningClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 11 additions & 1 deletion SigningServer.ClientCore/SigningClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ namespace SigningServer.ClientCore;
/// <summary>
/// Represents the signing client
/// </summary>
public class SigningClientConfigurationBase
public abstract class SigningClientConfigurationBase
{
/// <summary>
/// Whether to execute signing or not, useful if you have to enable/disable signing temporarily.
/// </summary>
public bool IsSigningDisabled { get; set; }

/// <summary>
/// Gets the credential info to use for authentication and certificate selection.
/// </summary>
public abstract string CredentialInfo { get; }

/// <summary>
/// Whether to overwrite existing signatures or fail when signatures are present.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions SigningServer.ClientCore/SigningClientRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions SigningServer.StandaloneClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,6 +33,11 @@ private static async Task Main(string[] args)
})
.ConfigureAppConfiguration(config =>
{
foreach (var envSources in config.Sources.OfType<EnvironmentVariablesConfigurationSource>().ToArray())
{
config.Sources.Remove(envSources);
}
config.AddEnvironmentVariables("SIGNINGSERVER_CLIENT_");
config.AddJsonFile("config.json", optional: true);
})
.ConfigureServices(services =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class StandaloneSigningClientConfiguration : SigningClientConfigurationBa
{
public ServerType ServerType { get; set; }

public override string CredentialInfo => "local";

/// <summary>
/// A RFC-3161 compliant timestamping server which should be used.
/// </summary>
Expand Down

0 comments on commit 786df2b

Please sign in to comment.