rename: prefix gateway projects/namespaces with ZB.MOM.WW + sln→slnx
Apply the ZB.MOM.WW. prefix to all gateway-side projects, folders,
.csproj/.sln contents, C# namespaces, using directives, generated proto
C# (csharp_namespace + checked-in generated files), InternalsVisibleTo
attributes, project-name string literals (LoadProject, .sln lookups,
worker exe paths, staticwebassets manifest), and the install/script/doc
references that point at any of the above. Migrate the solution from
.sln to .slnx via `dotnet sln migrate` and delete the old file.
External-runtime identifiers are intentionally NOT prefixed so external
configuration keeps working:
- GatewayMetrics.cs MeterName ("MxGateway.Server")
- DashboardAuthenticationDefaults Scheme/Policy ("MxGateway.Dashboard")
- GatewayRequestLoggingMiddleware logger category ("MxGateway.Request")
- StaRuntime thread name ("MxGateway.Worker.STA")
- appsettings.json root section "MxGateway" + env-var prefix
MxGateway__... and secret-name MxGateway:ApiKeyPepper
- C:\ProgramData\MxGateway\ data dir paths
Also fixes two tests that were not rename-related but became visible
while validating the rename:
- WorkerLiveMxAccessSmokeTests.ShutDownAsync: cancellation that the
gateway service correctly maps to RpcException(Cancelled) per gRPC
convention was being misclassified as a stream fault. Added a sibling
catch on RpcException with StatusCode.Cancelled.
- IntegrationTestEnvironment.ResolveRepositoryRoot: extracted IsRepositoryRoot
and made it accept either a .git marker OR a .sln/.slnx next to src/
so the worker-exe walker works in non-git working copies.
clients/proto/proto-inputs.json's protoRoot updated to point at
src/ZB.MOM.WW.MxGateway.Contracts/Protos.
Verified by `dotnet build` and a full `dotnet test` of the .slnx with
MXGATEWAY_RUN_LIVE_{MXACCESS,LDAP,GALAXY}_TESTS=1:
Tests: 472/472 pass
Worker.Tests: 280/280 pass (4 dev-rig [Fact(Skip=...)] skipped)
IntegrationTests: 18/18 pass
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Worker.Bootstrap;
|
||||
|
||||
/// <summary>
|
||||
/// Worker environment that reads from system environment variables.
|
||||
/// </summary>
|
||||
public sealed class EnvironmentVariableWorkerEnvironment : IWorkerEnvironment
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public string? GetEnvironmentVariable(string name)
|
||||
{
|
||||
return Environment.GetEnvironmentVariable(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace ZB.MOM.WW.MxGateway.Worker.Bootstrap;
|
||||
|
||||
/// <summary>
|
||||
/// Abstracts access to environment variables for the worker.
|
||||
/// </summary>
|
||||
public interface IWorkerEnvironment
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets an environment variable by name.
|
||||
/// </summary>
|
||||
/// <param name="name">Name of the environment variable.</param>
|
||||
/// <returns>The value of the environment variable, or null if not found.</returns>
|
||||
string? GetEnvironmentVariable(string name);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Worker.Bootstrap;
|
||||
|
||||
public interface IWorkerLogger
|
||||
{
|
||||
/// <summary>
|
||||
/// Logs an informational event with fields.
|
||||
/// </summary>
|
||||
/// <param name="eventName">Event name.</param>
|
||||
/// <param name="fields">Event fields.</param>
|
||||
void Information(string eventName, IReadOnlyDictionary<string, object?> fields);
|
||||
|
||||
/// <summary>
|
||||
/// Logs an error event with fields.
|
||||
/// </summary>
|
||||
/// <param name="eventName">Event name.</param>
|
||||
/// <param name="fields">Event fields.</param>
|
||||
void Error(string eventName, IReadOnlyDictionary<string, object?> fields);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Worker.Bootstrap;
|
||||
|
||||
public sealed class WorkerBootstrapResult
|
||||
{
|
||||
private WorkerBootstrapResult(
|
||||
WorkerExitCode exitCode,
|
||||
WorkerOptions? options,
|
||||
IReadOnlyList<string> errors)
|
||||
{
|
||||
ExitCode = exitCode;
|
||||
Options = options;
|
||||
Errors = errors;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the worker process exit code.
|
||||
/// </summary>
|
||||
public WorkerExitCode ExitCode { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the bootstrap options if bootstrap succeeded.
|
||||
/// </summary>
|
||||
public WorkerOptions? Options { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of bootstrap errors if any.
|
||||
/// </summary>
|
||||
public IReadOnlyList<string> Errors { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether bootstrap succeeded.
|
||||
/// </summary>
|
||||
public bool Succeeded => ExitCode == WorkerExitCode.Success;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a successful bootstrap result with the given options.
|
||||
/// </summary>
|
||||
/// <param name="options">Bootstrap options.</param>
|
||||
/// <returns>Successful bootstrap result.</returns>
|
||||
public static WorkerBootstrapResult Success(WorkerOptions options)
|
||||
{
|
||||
return new WorkerBootstrapResult(WorkerExitCode.Success, options, []);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a failed bootstrap result with the given exit code and errors.
|
||||
/// </summary>
|
||||
/// <param name="exitCode">Worker exit code.</param>
|
||||
/// <param name="errors">Bootstrap errors.</param>
|
||||
/// <returns>Failed bootstrap result.</returns>
|
||||
public static WorkerBootstrapResult Failure(WorkerExitCode exitCode, IEnumerable<string> errors)
|
||||
{
|
||||
return new WorkerBootstrapResult(exitCode, null, errors.ToArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Worker.Bootstrap;
|
||||
|
||||
public sealed class WorkerConsoleLogger : IWorkerLogger
|
||||
{
|
||||
private readonly TextWriter _writer;
|
||||
|
||||
/// <summary>Initializes a new worker console logger.</summary>
|
||||
/// <param name="writer">Text writer destination for log output.</param>
|
||||
public WorkerConsoleLogger(TextWriter writer)
|
||||
{
|
||||
_writer = writer ?? throw new ArgumentNullException(nameof(writer));
|
||||
}
|
||||
|
||||
/// <summary>Writes an informational log entry.</summary>
|
||||
/// <param name="eventName">Name of the event being logged.</param>
|
||||
/// <param name="fields">Event fields and values to log.</param>
|
||||
public void Information(string eventName, IReadOnlyDictionary<string, object?> fields)
|
||||
{
|
||||
Write("Information", eventName, fields);
|
||||
}
|
||||
|
||||
/// <summary>Writes an error log entry.</summary>
|
||||
/// <param name="eventName">Name of the event being logged.</param>
|
||||
/// <param name="fields">Event fields and values to log.</param>
|
||||
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,12 @@
|
||||
namespace ZB.MOM.WW.MxGateway.Worker.Bootstrap;
|
||||
|
||||
public enum WorkerExitCode
|
||||
{
|
||||
Success = 0,
|
||||
UnexpectedFailure = 1,
|
||||
InvalidArguments = 2,
|
||||
InvalidProtocolVersion = 3,
|
||||
MissingNonce = 4,
|
||||
PipeConnectionFailed = 5,
|
||||
ProtocolViolation = 6,
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Worker.Bootstrap;
|
||||
|
||||
/// <summary>
|
||||
/// Redacts sensitive fields from worker log messages.
|
||||
/// </summary>
|
||||
public static class WorkerLogRedactor
|
||||
{
|
||||
/// <summary>
|
||||
/// Replacement text for redacted values.
|
||||
/// </summary>
|
||||
public const string RedactedValue = "[redacted]";
|
||||
|
||||
private static readonly string[] SensitiveFieldNameParts =
|
||||
[
|
||||
"nonce",
|
||||
"secret",
|
||||
"password",
|
||||
"token",
|
||||
"credential",
|
||||
"apikey",
|
||||
"api_key",
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Redacts sensitive field values from a log field dictionary.
|
||||
/// </summary>
|
||||
/// <param name="fields">Dictionary of field names and values.</param>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Redacts a single value if its field name contains sensitive keywords.
|
||||
/// </summary>
|
||||
/// <param name="fieldName">Name of the field to check.</param>
|
||||
/// <param name="value">Value to redact if sensitive.</param>
|
||||
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,37 @@
|
||||
namespace ZB.MOM.WW.MxGateway.Worker.Bootstrap;
|
||||
|
||||
/// <summary>Worker bootstrap options passed via environment variables and named pipes.</summary>
|
||||
public sealed class WorkerOptions
|
||||
{
|
||||
/// <summary>Environment variable name for the worker nonce.</summary>
|
||||
public const string NonceEnvironmentVariableName = "MXGATEWAY_WORKER_NONCE";
|
||||
|
||||
/// <summary>Initializes worker options from a bootstrap handshake.</summary>
|
||||
/// <param name="sessionId">Identifier of the session.</param>
|
||||
/// <param name="pipeName">Named pipe name for gateway communication.</param>
|
||||
/// <param name="protocolVersion">Protocol version agreed with the gateway.</param>
|
||||
/// <param name="nonce">Authentication nonce for the handshake.</param>
|
||||
public WorkerOptions(
|
||||
string sessionId,
|
||||
string pipeName,
|
||||
uint protocolVersion,
|
||||
string nonce)
|
||||
{
|
||||
SessionId = sessionId;
|
||||
PipeName = pipeName;
|
||||
ProtocolVersion = protocolVersion;
|
||||
Nonce = nonce;
|
||||
}
|
||||
|
||||
/// <summary>Unique identifier for the gateway session this worker serves.</summary>
|
||||
public string SessionId { get; }
|
||||
|
||||
/// <summary>Named pipe name for communicating with the gateway.</summary>
|
||||
public string PipeName { get; }
|
||||
|
||||
/// <summary>Worker protocol version negotiated with the gateway.</summary>
|
||||
public uint ProtocolVersion { get; }
|
||||
|
||||
/// <summary>Nonce used to authenticate the handshake with the gateway.</summary>
|
||||
public string Nonce { get; }
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ZB.MOM.WW.MxGateway.Contracts;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Worker.Bootstrap;
|
||||
|
||||
/// <summary>
|
||||
/// Parses worker command-line arguments and environment variables.
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the parser with a worker environment.
|
||||
/// </summary>
|
||||
/// <param name="environment">Worker environment for reading configuration.</param>
|
||||
public WorkerOptionsParser(IWorkerEnvironment environment)
|
||||
{
|
||||
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses command-line arguments and returns bootstrap configuration or errors.
|
||||
/// </summary>
|
||||
/// <param name="args">Command-line arguments to parse.</param>
|
||||
/// <returns>Bootstrap result containing configuration or error messages.</returns>
|
||||
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