feat(apikey): add --pepper-stdin and warn on --pepper (mxaccessgw#194)

--pepper on the command line lands in shell history and the process list.
Add --pepper-stdin (first stdin line), warn on stderr when --pepper is used,
and reject both together. Documented in GatewayProcessDesign.md.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 12:27:37 -05:00
parent be0d585f8c
commit 417b9e5a19
6 changed files with 119 additions and 9 deletions
@@ -0,0 +1,60 @@
using ZB.MOM.WW.MxGateway.Server.Security.Authentication;
namespace ZB.MOM.WW.MxGateway.Tests.Security.Authentication;
public sealed class ApiKeyAdminPepperResolverTests
{
private static ApiKeyAdminCommand Parse(params string[] extra)
{
ApiKeyAdminParseResult result = ApiKeyAdminCommandLineParser.Parse(["apikey", "list-keys", .. extra]);
Assert.Null(result.Error);
return result.Command!;
}
/// <summary>--pepper-stdin parses without a value and reads the pepper from stdin, with no warning.</summary>
[Fact]
public void Resolve_PepperStdin_ReadsFirstLine_NoWarning()
{
ApiKeyAdminCommand command = Parse("--pepper-stdin");
StringWriter error = new();
string? pepper = ApiKeyAdminPepperResolver.Resolve(command, new StringReader("s3cret\nignored\n"), error);
Assert.True(command.PepperFromStdin);
Assert.Null(command.Pepper);
Assert.Equal("s3cret", pepper);
Assert.Equal(string.Empty, error.ToString());
}
/// <summary>--pepper still works but warns on stderr without echoing the secret.</summary>
[Fact]
public void Resolve_PepperFlag_WarnsWithoutEchoingSecret()
{
ApiKeyAdminCommand command = Parse("--pepper", "s3cret");
StringWriter error = new();
string? pepper = ApiKeyAdminPepperResolver.Resolve(command, new StringReader(string.Empty), error);
Assert.Equal("s3cret", pepper);
Assert.Contains("--pepper-stdin", error.ToString());
Assert.DoesNotContain("s3cret", error.ToString());
}
/// <summary>No pepper option leaves configuration untouched and stays silent.</summary>
[Fact]
public void Resolve_NoPepper_ReturnsNull_Silent()
{
StringWriter error = new();
Assert.Null(ApiKeyAdminPepperResolver.Resolve(Parse(), new StringReader("x"), error));
Assert.Equal(string.Empty, error.ToString());
}
/// <summary>Supplying both pepper sources is rejected.</summary>
[Fact]
public void Parse_PepperAndPepperStdin_Fails()
{
ApiKeyAdminParseResult result = ApiKeyAdminCommandLineParser.Parse(
["apikey", "list-keys", "--pepper", "a", "--pepper-stdin"]);
Assert.NotNull(result.Error);
}
}