Files
ExecuteR/tests/Agent.Tests/ClientIdentityTests.cs
gsadmin e72729059b M3: Deterministic identity, secure bootstrap, NATS JWT issuance, WSS connection
Agent:
- CIM hardware identity (System.Management) with placeholder rejection and
  labeled canonical string; SHA-256 lowercase-hex client id; strong-identifier
  requirement
- DPAPI(LocalMachine) credential store with SYSTEM/Administrators-only ACLs;
  versioned encrypted JSON blob; private NKey seed never leaves the endpoint
- Enrollment client: local user NKey generation, HTTPS bootstrap, credential
  persistence, bootstrap token erased from registry after success
- NATS options factory: WSS endpoint from ServerURL, JWT+seed auth from
  protected memory, unlimited reconnect with backoff+jitter; worker now
  orchestrates enroll -> connect -> session with reload of registry config

Backend:
- NatsCredentialIssuer: account-signing-key user JWTs (agent scoped to own
  client id, browser scoped to session subjects, backend full access)
- nats-auth init CLI: real Ed25519 operator/APP/SYS keys + resolver.conf via
  NATS.Jwt/NATS.NKeys (no fake seeds)
- EF Core SQLite schema + InitialCreate migration (Users, Devices,
  DeviceInventory, Scripts, Jobs, JobOutput, BootstrapTokens, AuditEvents,
  ShellSessions, OidcProviders); migrate on startup
- Bootstrap endpoint: hashed one-time tokens, idempotent enrollment, NKey
  binding, rate limiting, auditing; credential refresh via Ed25519 proof of
  possession
- Readiness now verifies database, NATS connection and signing key

Tests: 27 agent (identity, config), 9 backend (bootstrap/refresh via
WebApplicationFactory with real JWT verification)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-06 19:03:39 -04:00

94 lines
3.2 KiB
C#

using Agent.Identity;
using Xunit;
namespace Agent.Tests;
public class ClientIdentityTests
{
private static readonly HardwareIdentity FullIdentity = new(
SystemProductUuid: "4c4c4544-0032-3510-8051-b4c04f4a3532",
BiosSerialNumber: "ABC1234",
BaseboardSerialNumber: "/ABC1234/CNWS123456789/",
IdentifyingNumber: "ABC1234",
Manufacturer: "Dell Inc.",
Model: "OptiPlex 7090");
[Theory]
[InlineData(null, null)]
[InlineData("", null)]
[InlineData(" ", null)]
[InlineData("To Be Filled By O.E.M.", null)]
[InlineData("Default string", null)]
[InlineData("00000000-0000-0000-0000-000000000000", null)]
[InlineData("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF", null)]
[InlineData("0000000", null)]
[InlineData("None", null)]
[InlineData(" abc123 ", "ABC123")]
[InlineData("serial-1", "SERIAL-1")]
public void NormalizationRejectsPlaceholdersAndUppercases(string? input, string? expected)
{
Assert.Equal(expected, ClientIdentityFactory.Normalize(input));
}
[Fact]
public void CanonicalIdentityIsLabeledAndOrdered()
{
var canonical = ClientIdentityFactory.BuildCanonicalIdentity(FullIdentity);
Assert.StartsWith("UUID=4C4C4544-0032-3510-8051-B4C04F4A3532|BIOS=ABC1234|", canonical);
Assert.Contains("|MFR=DELL INC.|MODEL=OPTIPLEX 7090", canonical);
}
[Fact]
public void ClientIdIsDeterministicLowercaseHexSha256()
{
var first = ClientIdentityFactory.ComputeClientId(FullIdentity);
var second = ClientIdentityFactory.ComputeClientId(FullIdentity);
Assert.Equal(first, second);
Assert.Equal(64, first.Length);
Assert.Matches("^[0-9a-f]{64}$", first);
}
[Fact]
public void ClientIdIgnoresVolatileValueChanges()
{
// Same hardware, different hostname/user/IP — not part of the identity at all.
var a = ClientIdentityFactory.ComputeClientId(FullIdentity);
var b = ClientIdentityFactory.ComputeClientId(FullIdentity with { });
Assert.Equal(a, b);
}
[Fact]
public void DifferentHardwareYieldsDifferentClientId()
{
var other = FullIdentity with { SystemProductUuid = "11111111-2222-3333-4444-555555555555" };
Assert.NotEqual(
ClientIdentityFactory.ComputeClientId(FullIdentity),
ClientIdentityFactory.ComputeClientId(other));
}
[Fact]
public void PlaceholderUuidFallsBackToOtherIdentifiers()
{
var identity = FullIdentity with { SystemProductUuid = "03000200-0400-0500-0006-000700080009" };
var canonical = ClientIdentityFactory.BuildCanonicalIdentity(identity);
Assert.StartsWith("UUID=|BIOS=ABC1234|", canonical);
}
[Fact]
public void MissingAllStrongIdentifiersThrows()
{
var junk = new HardwareIdentity(
SystemProductUuid: "00000000-0000-0000-0000-000000000000",
BiosSerialNumber: "To Be Filled By O.E.M.",
BaseboardSerialNumber: null,
IdentifyingNumber: "Default string",
Manufacturer: "Generic",
Model: "Board");
Assert.Throws<InvalidOperationException>(() => ClientIdentityFactory.BuildCanonicalIdentity(junk));
}
}