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

Added Domain Sockets and Named Pipes Support to ProcessHostService #123

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ public void Decrypt(IDataProtector dataProtector)

return null;
}
public bool UseDomainSockets { get; init; }
public string? DomainSocketPath { get; init; }
public bool UseNamedPipes { get; init; }
public string? NamedPipeName { get; init; }
}

public record LauncherConfigurationInCashBoxConfiguration
Expand All @@ -349,4 +353,4 @@ public record LauncherConfigurationInCashBoxConfiguration
return configuration;
}
}
}
}
36 changes: 24 additions & 12 deletions src/fiskaltrust.Launcher/Commands/HostCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public HostCommand() : base("host")
AddOption(new Option<string>("--debugging"));
AddOption(new Option<string>("--launcher-configuration"));
AddOption(new Option<bool>("--no-process-host-service", getDefaultValue: () => false));
AddOption(new Option<bool>("--use-domain-sockets"));
AddOption(new Option<string>("--domain-socket-path"));
AddOption(new Option<bool>("--use-named-pipes"));
AddOption(new Option<string?>("--named-pipe-name"));
}
}

Expand All @@ -41,14 +45,19 @@ public class HostCommandHandler : ICommandHandler
public string PlebeianConfiguration { get; set; } = null!;
public bool NoProcessHostService { get; set; }
public bool Debugging { get; set; }

public bool UseDomainSockets { get; }
public string? DomainSocketPath { get; }

private readonly CancellationToken _cancellationToken;
private readonly LauncherExecutablePath _launcherExecutablePath;

public HostCommandHandler(IHostApplicationLifetime lifetime, LauncherExecutablePath launcherExecutablePath)
public HostCommandHandler(IHostApplicationLifetime lifetime, LauncherExecutablePath launcherExecutablePath, bool useDomainSockets, string? domainSocketPath)
{
_cancellationToken = lifetime.ApplicationStopping;
_launcherExecutablePath = launcherExecutablePath;
UseDomainSockets = useDomainSockets;
DomainSocketPath = domainSocketPath;
}

public async Task<int> InvokeAsync(InvocationContext context)
Expand All @@ -61,9 +70,17 @@ public async Task<int> InvokeAsync(InvocationContext context)
}
}

var launcherConfiguration = Common.Configuration.LauncherConfiguration.Deserialize(System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(LauncherConfiguration)));

var plebeianConfiguration = Configuration.PlebeianConfiguration.Deserialize(System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(PlebeianConfiguration)));
var launcherConfigurationBase64Decoded = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(LauncherConfiguration));
var launcherConfiguration = Common.Configuration.LauncherConfiguration.Deserialize(launcherConfigurationBase64Decoded);

launcherConfiguration = launcherConfiguration with
{
UseDomainSockets = UseDomainSockets,
DomainSocketPath = UseDomainSockets ? DomainSocketPath ?? throw new InvalidOperationException("Domain socket path must be provided when using domain sockets.") : null
};

var plebeianConfigurationBase64Decoded = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(PlebeianConfiguration));
var plebeianConfiguration = Configuration.PlebeianConfiguration.Deserialize(plebeianConfigurationBase64Decoded);

var cashboxConfiguration = CashBoxConfigurationExt.Deserialize(await File.ReadAllTextAsync(launcherConfiguration.CashboxConfigurationFile!));

Expand Down Expand Up @@ -96,11 +113,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
.UseSerilog()
.ConfigureServices(services =>
{
services.Configure<HostOptions>(opts =>
{
opts.ShutdownTimeout = TimeSpan.FromSeconds(30);
opts.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.StopHost;
});
services.Configure<HostOptions>(opts => opts.ShutdownTimeout = TimeSpan.FromSeconds(30));
services.AddSingleton(_ => launcherConfiguration);
services.AddSingleton(_ => packageConfiguration);
services.AddSingleton(_ => plebeianConfiguration);
Expand Down Expand Up @@ -155,7 +168,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
{
Log.Error(e, "Could not load {Type}.", nameof(IMiddlewareBootstrapper));
throw;
} // Will also be detected and logged propperly later
}
});

try
Expand All @@ -165,7 +178,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
}
catch (Exception e)
{
Log.Error(e, "An unhandled exception occured.");
Log.Error(e, "An unhandled exception occurred.");
throw;
}
finally
Expand Down Expand Up @@ -211,4 +224,3 @@ private static Dictionary<string, object> ProcessPackageConfiguration(Dictionary
}
}
}

29 changes: 26 additions & 3 deletions src/fiskaltrust.Launcher/Services/HostingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ public async Task<WebApplication> HostService<T>(Uri uri, HostingType hostingTyp
{
var builder = WebApplication.CreateBuilder();

// Configure Serilog for logging
builder.Host.UseSerilog((_, __, loggerConfiguration) =>
loggerConfiguration
.AddLoggingConfiguration(_launcherConfiguration, aspLogging: true)
.WriteTo.GrpcSink(_packageConfiguration, _processHostService));

// Add HTTP logging if the log level is set to Debug or lower
if (_launcherConfiguration.LogLevel <= LogLevel.Debug)
{
builder.Services.AddHttpLogging(options =>
Expand All @@ -71,9 +73,10 @@ public async Task<WebApplication> HostService<T>(Uri uri, HostingType hostingTyp
HttpLoggingFields.ResponseStatusCode |
HttpLoggingFields.ResponseBody);
}
WebApplication app;

WebApplication app;

// Check if UseHttpSysBinding is enabled and log warnings if necessary
if (_launcherConfiguration.UseHttpSysBinding!.Value)
{
const string message = $"The configuration parameter {{parametername}} will be ignored because {nameof(_launcherConfiguration.UseHttpSysBinding)} is enabled.";
Expand All @@ -98,6 +101,7 @@ public async Task<WebApplication> HostService<T>(Uri uri, HostingType hostingTyp
}
}

// Create the appropriate host based on the hosting type
switch (hostingType)
{
case HostingType.REST:
Expand All @@ -117,6 +121,7 @@ public async Task<WebApplication> HostService<T>(Uri uri, HostingType hostingTyp
throw new NotImplementedException();
}

// Use HTTP logging if the log level is set to Debug or lower
if (_launcherConfiguration.LogLevel <= LogLevel.Debug)
{
app.UseHttpLogging();
Expand All @@ -130,6 +135,7 @@ public async Task<WebApplication> HostService<T>(Uri uri, HostingType hostingTyp

private WebApplication CreateRestHost<T>(WebApplicationBuilder builder, Uri uri, T instance, Action<WebApplication> addEndpoints)
{
// Configure JSON options
builder.Services.Configure<JsonOptions>(options =>
{
options.SerializerOptions.NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString;
Expand All @@ -140,6 +146,7 @@ private WebApplication CreateRestHost<T>(WebApplicationBuilder builder, Uri uri,
options.SerializerOptions.DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull;
});

// Configure Kestrel server binding
builder.WebHost.ConfigureBinding(uri, listenOptions => ConfigureTls(listenOptions), isHttps: !string.IsNullOrEmpty(_launcherConfiguration.TlsCertificatePath) || !string.IsNullOrEmpty(_launcherConfiguration.TlsCertificateBase64), allowSynchronousIO: true, useHttpSys: _launcherConfiguration.UseHttpSysBinding!.Value);

var app = builder.Build();
Expand Down Expand Up @@ -239,7 +246,23 @@ private WebApplication CreateGrpcHost<T>(WebApplicationBuilder builder, Uri uri,
_logger.LogWarning($"{nameof(_launcherConfiguration.UseHttpSysBinding)} is not supported for grpc.");
}

builder.WebHost.BindKestrel(uri, listenOptions => ConfigureTls(listenOptions), false, HttpProtocols.Http2);
// Support for domain sockets/namespots
if (_launcherConfiguration.UseDomainSockets)
{
builder.WebHost.UseKestrel(options =>
{
options.ListenUnixSocket(_launcherConfiguration.DomainSocketPath!, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http2;
ConfigureTls(listenOptions);
});
});
}
else
{
builder.WebHost.BindKestrel(uri, listenOptions => ConfigureTls(listenOptions), false, HttpProtocols.Http2);
}

builder.Services.AddCodeFirstGrpc(options => options.EnableDetailedErrors = true);
builder.Services.AddSingleton(instance);

Expand Down Expand Up @@ -339,4 +362,4 @@ private static ConfigureWebHostBuilder BindHttpSys(this ConfigureWebHostBuilder
return builder;
}
}
}
}
Loading