Issue #21: implement worker bootstrap and options
This commit is contained in:
@@ -1 +0,0 @@
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace MxGateway.Worker.Bootstrap;
|
||||
|
||||
public sealed class EnvironmentVariableWorkerEnvironment : IWorkerEnvironment
|
||||
{
|
||||
public string? GetEnvironmentVariable(string name)
|
||||
{
|
||||
return Environment.GetEnvironmentVariable(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace MxGateway.Worker.Bootstrap;
|
||||
|
||||
public interface IWorkerEnvironment
|
||||
{
|
||||
string? GetEnvironmentVariable(string name);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MxGateway.Worker.Bootstrap;
|
||||
|
||||
public interface IWorkerLogger
|
||||
{
|
||||
void Information(string eventName, IReadOnlyDictionary<string, object?> fields);
|
||||
|
||||
void Error(string eventName, IReadOnlyDictionary<string, object?> fields);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace MxGateway.Worker.Bootstrap;
|
||||
|
||||
public sealed class WorkerBootstrapResult
|
||||
{
|
||||
private WorkerBootstrapResult(
|
||||
WorkerExitCode exitCode,
|
||||
WorkerOptions? options,
|
||||
IReadOnlyList<string> errors)
|
||||
{
|
||||
ExitCode = exitCode;
|
||||
Options = options;
|
||||
Errors = errors;
|
||||
}
|
||||
|
||||
public WorkerExitCode ExitCode { get; }
|
||||
|
||||
public WorkerOptions? Options { get; }
|
||||
|
||||
public IReadOnlyList<string> Errors { get; }
|
||||
|
||||
public bool Succeeded => ExitCode == WorkerExitCode.Success;
|
||||
|
||||
public static WorkerBootstrapResult Success(WorkerOptions options)
|
||||
{
|
||||
return new WorkerBootstrapResult(WorkerExitCode.Success, options, []);
|
||||
}
|
||||
|
||||
public static WorkerBootstrapResult Failure(WorkerExitCode exitCode, IEnumerable<string> errors)
|
||||
{
|
||||
return new WorkerBootstrapResult(exitCode, null, errors.ToArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace MxGateway.Worker.Bootstrap;
|
||||
|
||||
public sealed class WorkerConsoleLogger : IWorkerLogger
|
||||
{
|
||||
private readonly TextWriter _writer;
|
||||
|
||||
public WorkerConsoleLogger(TextWriter writer)
|
||||
{
|
||||
_writer = writer ?? throw new ArgumentNullException(nameof(writer));
|
||||
}
|
||||
|
||||
public void Information(string eventName, IReadOnlyDictionary<string, object?> fields)
|
||||
{
|
||||
Write("Information", eventName, fields);
|
||||
}
|
||||
|
||||
public void Error(string eventName, IReadOnlyDictionary<string, object?> fields)
|
||||
{
|
||||
Write("Error", eventName, fields);
|
||||
}
|
||||
|
||||
private void Write(
|
||||
string level,
|
||||
string eventName,
|
||||
IReadOnlyDictionary<string, object?> fields)
|
||||
{
|
||||
Dictionary<string, object?> redactedFields = WorkerLogRedactor.RedactFields(fields);
|
||||
string fieldText = string.Join(
|
||||
" ",
|
||||
redactedFields.Select(field => $"{field.Key}={FormatValue(field.Value)}"));
|
||||
|
||||
_writer.WriteLine($"level={level} event={eventName} {fieldText}".TrimEnd());
|
||||
}
|
||||
|
||||
private static string FormatValue(object? value)
|
||||
{
|
||||
return value?.ToString() ?? string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace MxGateway.Worker.Bootstrap;
|
||||
|
||||
public enum WorkerExitCode
|
||||
{
|
||||
Success = 0,
|
||||
UnexpectedFailure = 1,
|
||||
InvalidArguments = 2,
|
||||
InvalidProtocolVersion = 3,
|
||||
MissingNonce = 4,
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MxGateway.Worker.Bootstrap;
|
||||
|
||||
public static class WorkerLogRedactor
|
||||
{
|
||||
public const string RedactedValue = "[redacted]";
|
||||
|
||||
private static readonly string[] SensitiveFieldNameParts =
|
||||
[
|
||||
"nonce",
|
||||
"secret",
|
||||
"password",
|
||||
"token",
|
||||
"credential",
|
||||
"apikey",
|
||||
"api_key",
|
||||
];
|
||||
|
||||
public static Dictionary<string, object?> RedactFields(IReadOnlyDictionary<string, object?> fields)
|
||||
{
|
||||
Dictionary<string, object?> redactedFields = [];
|
||||
|
||||
foreach (KeyValuePair<string, object?> field in fields)
|
||||
{
|
||||
redactedFields[field.Key] = RedactValue(field.Key, field.Value);
|
||||
}
|
||||
|
||||
return redactedFields;
|
||||
}
|
||||
|
||||
public static object? RedactValue(string fieldName, object? value)
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (string sensitiveFieldNamePart in SensitiveFieldNameParts)
|
||||
{
|
||||
if (fieldName.IndexOf(sensitiveFieldNamePart, StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
{
|
||||
return RedactedValue;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace MxGateway.Worker.Bootstrap;
|
||||
|
||||
public sealed class WorkerOptions
|
||||
{
|
||||
public const string NonceEnvironmentVariableName = "MXGATEWAY_WORKER_NONCE";
|
||||
|
||||
public WorkerOptions(
|
||||
string sessionId,
|
||||
string pipeName,
|
||||
uint protocolVersion,
|
||||
string nonce)
|
||||
{
|
||||
SessionId = sessionId;
|
||||
PipeName = pipeName;
|
||||
ProtocolVersion = protocolVersion;
|
||||
Nonce = nonce;
|
||||
}
|
||||
|
||||
public string SessionId { get; }
|
||||
|
||||
public string PipeName { get; }
|
||||
|
||||
public uint ProtocolVersion { get; }
|
||||
|
||||
public string Nonce { get; }
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MxGateway.Contracts;
|
||||
|
||||
namespace MxGateway.Worker.Bootstrap;
|
||||
|
||||
public sealed class WorkerOptionsParser
|
||||
{
|
||||
private const string SessionIdOptionName = "--session-id";
|
||||
private const string PipeNameOptionName = "--pipe-name";
|
||||
private const string ProtocolVersionOptionName = "--protocol-version";
|
||||
|
||||
private readonly IWorkerEnvironment _environment;
|
||||
|
||||
public WorkerOptionsParser(IWorkerEnvironment environment)
|
||||
{
|
||||
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
|
||||
}
|
||||
|
||||
public WorkerBootstrapResult Parse(string[] args)
|
||||
{
|
||||
if (args is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(args));
|
||||
}
|
||||
|
||||
Dictionary<string, string> values = new(StringComparer.OrdinalIgnoreCase);
|
||||
List<string> errors = [];
|
||||
|
||||
for (int index = 0; index < args.Length; index++)
|
||||
{
|
||||
string arg = args[index];
|
||||
if (!IsKnownOption(arg))
|
||||
{
|
||||
errors.Add($"Unknown option '{arg}'.");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (index + 1 >= args.Length || args[index + 1].StartsWith("--", StringComparison.Ordinal))
|
||||
{
|
||||
errors.Add($"Option '{arg}' requires a value.");
|
||||
continue;
|
||||
}
|
||||
|
||||
values[arg] = args[index + 1];
|
||||
index++;
|
||||
}
|
||||
|
||||
string? sessionId = ReadRequired(values, SessionIdOptionName, errors);
|
||||
string? pipeName = ReadRequired(values, PipeNameOptionName, errors);
|
||||
string? protocolVersionText = ReadRequired(values, ProtocolVersionOptionName, errors);
|
||||
|
||||
if (errors.Count > 0)
|
||||
{
|
||||
return WorkerBootstrapResult.Failure(WorkerExitCode.InvalidArguments, errors);
|
||||
}
|
||||
|
||||
if (!uint.TryParse(protocolVersionText, out uint protocolVersion)
|
||||
|| protocolVersion != GatewayContractInfo.WorkerProtocolVersion)
|
||||
{
|
||||
return WorkerBootstrapResult.Failure(
|
||||
WorkerExitCode.InvalidProtocolVersion,
|
||||
[$"Unsupported protocol version '{protocolVersionText}'."]);
|
||||
}
|
||||
|
||||
string? nonce = _environment.GetEnvironmentVariable(WorkerOptions.NonceEnvironmentVariableName);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(nonce))
|
||||
{
|
||||
return WorkerBootstrapResult.Failure(
|
||||
WorkerExitCode.MissingNonce,
|
||||
["Required worker nonce environment variable is missing."]);
|
||||
}
|
||||
|
||||
return WorkerBootstrapResult.Success(new WorkerOptions(
|
||||
sessionId!,
|
||||
pipeName!,
|
||||
protocolVersion,
|
||||
nonce!));
|
||||
}
|
||||
|
||||
private static string? ReadRequired(
|
||||
IReadOnlyDictionary<string, string> values,
|
||||
string optionName,
|
||||
List<string> errors)
|
||||
{
|
||||
if (!values.TryGetValue(optionName, out string value)
|
||||
|| string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
errors.Add($"Required option '{optionName}' is missing.");
|
||||
return null;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
private static bool IsKnownOption(string optionName)
|
||||
{
|
||||
return optionName is SessionIdOptionName or PipeNameOptionName or ProtocolVersionOptionName;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user