0b85bd5e8a
Every served install script (Windows/WinPE PowerShell 5.1, Linux and macOS bash) now looks at the device before acting: a healthy, current agent enrolled with this server is left alone (Already Installed), an older one is upgraded in place with its enrollment kept, a failed one (service missing, disabled, stopped or crash-looping, watchdog down, binary missing or broken, Windows Installer registration or cache gone) is restarted through the agent's own initialize and, if that does not help, reinstalled (MSI repair REINSTALL=ALL REINSTALLMODE=vamus, cache restored from the kept package, uninstall+install with ProgramData and the registry key set aside as a last resort; Linux/macOS move files in one by one and re-register the unit or daemon). The bootstrap token is used only when there is no usable credential or the agent points at another server, never printed, and masked out of failure messages. One setup at a time, leftovers of interrupted runs cleared. Each run ends with one outcome line and exits 1 on failure. The agent's status verb now prints a JSON line with version, server URL, credential state (valid/missing/unreadable/other-device) and whether a bootstrap token is pending, which the scripts read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
148 lines
6.1 KiB
C#
148 lines
6.1 KiB
C#
using System.Text.Json;
|
|
using Agent.Commands;
|
|
using Agent.Configuration;
|
|
using Agent.Enrollment;
|
|
using Shared.Infrastructure.Constants;
|
|
using Xunit;
|
|
|
|
namespace Agent.Tests;
|
|
|
|
/// <summary>
|
|
/// The enrollment half of <c>status</c>, which the install scripts read to decide
|
|
/// between leaving a device alone, repairing it and re-enrolling it.
|
|
/// </summary>
|
|
public class AgentHealthTests
|
|
{
|
|
private const string ClientId = "client-a";
|
|
private static readonly Guid EnrollmentId = Guid.Parse("5b0e5a62-2f1d-4c3e-9a77-0c4d1b2e3f40");
|
|
|
|
private readonly InMemoryRegistryStore registry = new();
|
|
private readonly FixedCredentialStore credentials = new();
|
|
|
|
[Fact]
|
|
public void AnEnrolledAgentOfThisHardwareIsValid()
|
|
{
|
|
registry.SetString(Branding.AgentRegistryKey, AgentConfigurationKeys.ServerUrl, " https://control.example.com/ ");
|
|
credentials.Current = Credential(ClientId);
|
|
|
|
var report = Read(fileExists: true, clientId: ClientId);
|
|
|
|
Assert.Equal(AgentHealth.CredentialValid, report.Credential);
|
|
Assert.Equal("https://control.example.com/", report.ServerUrl);
|
|
Assert.Equal(EnrollmentId.ToString("D"), report.EnrollmentId);
|
|
Assert.False(report.BootstrapTokenPending);
|
|
Assert.Equal("1.4.0", report.Version);
|
|
}
|
|
|
|
[Fact]
|
|
public void NoCredentialFileIsMissingAndAPlacedTokenIsPending()
|
|
{
|
|
registry.SetString(Branding.AgentRegistryKey, AgentConfigurationKeys.BootstrapToken, "secret");
|
|
|
|
var report = Read(fileExists: false, clientId: ClientId);
|
|
|
|
Assert.Equal(AgentHealth.CredentialMissing, report.Credential);
|
|
Assert.Null(report.ServerUrl);
|
|
Assert.Null(report.EnrollmentId);
|
|
Assert.True(report.BootstrapTokenPending);
|
|
}
|
|
|
|
[Fact]
|
|
public void AFileThatDoesNotLoadIsUnreadable()
|
|
{
|
|
credentials.Current = null;
|
|
|
|
Assert.Equal(AgentHealth.CredentialUnreadable, Read(fileExists: true, clientId: ClientId).Credential);
|
|
}
|
|
|
|
[Fact]
|
|
public void ACredentialOfOtherHardwareIsAnotherDevice()
|
|
{
|
|
credentials.Current = Credential("client-b");
|
|
|
|
Assert.Equal(AgentHealth.CredentialOtherDevice, Read(fileExists: true, clientId: ClientId).Credential);
|
|
}
|
|
|
|
[Fact]
|
|
public void WithoutAHardwareIdentityTheCredentialIsJudgedOnItsOwn()
|
|
{
|
|
credentials.Current = Credential("client-b");
|
|
|
|
Assert.Equal(AgentHealth.CredentialValid, Read(fileExists: true, clientId: null).Credential);
|
|
}
|
|
|
|
[Fact]
|
|
public void AnExpiredCredentialIsStillValidBecauseItRenewsItself()
|
|
{
|
|
credentials.Current = Credential(ClientId) with { ExpiresUtc = DateTimeOffset.UtcNow.AddDays(-3) };
|
|
|
|
Assert.Equal(AgentHealth.CredentialValid, Read(fileExists: true, clientId: ClientId).Credential);
|
|
}
|
|
|
|
[Fact]
|
|
public void GroupPolicyDecidesTheServer()
|
|
{
|
|
registry.SetString(Branding.AgentRegistryKey, AgentConfigurationKeys.ServerUrl, "https://local.example.com");
|
|
registry.SetString(Branding.AgentPolicyRegistryKey, AgentConfigurationKeys.ServerUrl, "https://policy.example.com");
|
|
|
|
Assert.Equal("https://policy.example.com", Read(fileExists: false, clientId: ClientId).ServerUrl);
|
|
}
|
|
|
|
[Fact]
|
|
public void TheJsonLineCarriesEveryFieldButNeverTheToken()
|
|
{
|
|
registry.SetString(Branding.AgentRegistryKey, AgentConfigurationKeys.ServerUrl, "https://control.example.com:8443/a+b");
|
|
registry.SetString(Branding.AgentRegistryKey, AgentConfigurationKeys.BootstrapToken, "very-secret-token");
|
|
credentials.Current = Credential(ClientId) with { ExpiresUtc = new DateTimeOffset(2027, 1, 2, 3, 4, 5, TimeSpan.Zero) };
|
|
|
|
var json = AgentHealth.Json("ExecuteRAgent", installed: true, "Running", "Automatic", Read(fileExists: true, clientId: ClientId));
|
|
|
|
Assert.DoesNotContain("very-secret-token", json);
|
|
Assert.DoesNotContain("\n", json);
|
|
// Shell scripts read fields with sed: the URL stays as written, no \u escapes.
|
|
Assert.Contains("\"serverUrl\":\"https://control.example.com:8443/a+b\"", json);
|
|
Assert.Contains("\"credential\":\"valid\"", json);
|
|
Assert.Contains("\"bootstrapTokenPending\":true", json);
|
|
using var document = JsonDocument.Parse(json);
|
|
var root = document.RootElement;
|
|
Assert.Equal("ExecuteRAgent", root.GetProperty("service").GetString());
|
|
Assert.True(root.GetProperty("installed").GetBoolean());
|
|
Assert.Equal("Running", root.GetProperty("state").GetString());
|
|
Assert.Equal("Automatic", root.GetProperty("startType").GetString());
|
|
Assert.Equal("1.4.0", root.GetProperty("version").GetString());
|
|
Assert.Equal(EnrollmentId.ToString("D"), root.GetProperty("enrollmentId").GetString());
|
|
Assert.Equal("2027-01-02T03:04:05.0000000+00:00", root.GetProperty("credentialExpiresUtc").GetString());
|
|
}
|
|
|
|
[Fact]
|
|
public void AnUnenrolledAgentWritesNulls()
|
|
{
|
|
var json = AgentHealth.Json("executer-agent", installed: false, "inactive", startType: null, Read(fileExists: false, clientId: ClientId));
|
|
|
|
using var document = JsonDocument.Parse(json);
|
|
var root = document.RootElement;
|
|
Assert.False(root.TryGetProperty("startType", out _));
|
|
Assert.Equal(JsonValueKind.Null, root.GetProperty("serverUrl").ValueKind);
|
|
Assert.Equal(JsonValueKind.Null, root.GetProperty("enrollmentId").ValueKind);
|
|
Assert.Equal("missing", root.GetProperty("credential").GetString());
|
|
}
|
|
|
|
private AgentHealth.Report Read(bool fileExists, string? clientId)
|
|
=> AgentHealth.Read(
|
|
new AgentConfigurationLoader(registry, allowEnvironment: false).Load(), credentials, fileExists, () => clientId, "1.4.0");
|
|
|
|
private static AgentCredentials Credential(string clientId)
|
|
=> new(AgentCredentials.CurrentVersion, clientId, EnrollmentId, "jwt", "seed", DateTimeOffset.UtcNow.AddDays(30));
|
|
|
|
private sealed class FixedCredentialStore : ICredentialStore
|
|
{
|
|
public AgentCredentials? Current { get; set; }
|
|
|
|
public AgentCredentials? Load() => Current;
|
|
|
|
public void Save(AgentCredentials credentials) => Current = credentials;
|
|
|
|
public void Delete() => Current = null;
|
|
}
|
|
}
|