From 5d7d021af10677dba1a4ca1eae6c843189e0f9ebc37f0ec55979f24c2d3a8d62 Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Sat, 31 Oct 2026 19:30:08 -0400 Subject: [PATCH 01/20] feat(removal): contracts for removing devices and short-lived agent credentials The remove command and its events, removal states and reasons, the console API, how long a removal shows in the Devices list before only its filter does, the agent credential's lifetime and the 410 answer for a removed device, and the removal the watchdog carries out. Co-Authored-By: Claude Opus 5.5 --- src/Shared.Contracts/Api/ApiRoutes.cs | 6 ++ src/Shared.Contracts/Api/DeviceRemovalDtos.cs | 43 +++++++++ .../Messages/DeviceRemovalPayloads.cs | 23 +++++ src/Shared.Contracts/Messages/MessageTypes.cs | 3 + src/Shared.Contracts/Models/DeviceRemovals.cs | 91 +++++++++++++++++++ src/Shared.Contracts/Models/WatchdogFiles.cs | 2 + .../Serialization/WatchdogJsonContext.cs | 1 + .../Nats/JetStreamDefaults.cs | 1 + .../Nats/NatsSubjects.cs | 1 + 9 files changed, 171 insertions(+) create mode 100644 src/Shared.Contracts/Api/DeviceRemovalDtos.cs create mode 100644 src/Shared.Contracts/Messages/DeviceRemovalPayloads.cs create mode 100644 src/Shared.Contracts/Models/DeviceRemovals.cs diff --git a/src/Shared.Contracts/Api/ApiRoutes.cs b/src/Shared.Contracts/Api/ApiRoutes.cs index 44f0881..e9d0944 100644 --- a/src/Shared.Contracts/Api/ApiRoutes.cs +++ b/src/Shared.Contracts/Api/ApiRoutes.cs @@ -83,6 +83,11 @@ public static class ApiRoutes /// POST: cancel an upgrade that has not been sent yet. public const string AgentUpgradeCancel = "/api/agent-upgrades/{id}/cancel"; public const string AgentUpgradeSettings = "/api/settings/agent-upgrades"; + + /// GET (?state=, ?clientId=, ?limit=): removals, newest first. POST: remove devices. + public const string DeviceRemovals = "/api/device-removals"; + /// POST: stop trying to uninstall; the device stays blocked. + public const string DeviceRemovalStop = "/api/device-removals/{id}/stop"; /// GET (anonymous, ranges): one catalog package by SHA-256; what an upgrade command's DownloadUrl points at. public const string AgentPackageBySha = "/api/agent-packages/{sha256}"; @@ -128,6 +133,7 @@ public static class ApiRoutes public static string Device(string clientId) => $"/api/devices/{Uri.EscapeDataString(clientId)}"; public static string DeviceInventory(string clientId) => $"/api/devices/{Uri.EscapeDataString(clientId)}/inventory"; public static string DeviceHistory(string clientId) => $"/api/devices/{Uri.EscapeDataString(clientId)}/history"; + public static string DeviceRemovalStop(Guid id) => $"/api/device-removals/{id:D}/stop"; public static string AgentVersion(string version) => $"/api/agent-versions/{Uri.EscapeDataString(version)}"; public static string AgentUpgradeCancel(Guid id) => $"/api/agent-upgrades/{id:D}/cancel"; public static string AgentPackageBySha(string sha256) => $"/api/agent-packages/{Uri.EscapeDataString(sha256)}"; diff --git a/src/Shared.Contracts/Api/DeviceRemovalDtos.cs b/src/Shared.Contracts/Api/DeviceRemovalDtos.cs new file mode 100644 index 0000000..546c090 --- /dev/null +++ b/src/Shared.Contracts/Api/DeviceRemovalDtos.cs @@ -0,0 +1,43 @@ +namespace Shared.Contracts.Api; + +/// +/// POST /api/device-removals: remove these devices and every device in these +/// groups (nested groups and folders included). Each leaves the system at once; +/// its agent is uninstalled now if online, or when it next returns. +/// +public sealed record RemoveDevicesRequest( + IReadOnlyList? ClientIds = null, + IReadOnlyList? GroupIds = null, + string? Reason = null); + +/// +/// One device's removal. ShowInDeviceList is true for the first +/// DeviceRemovalDefaults.VisibleInDeviceList after UpdatedUtc; after +/// that only the Removals filter shows it. +/// +public sealed record DeviceRemovalDto( + Guid Id, + string ClientId, + string? Hostname, + string? OsCaption, + string? AgentVersion, + string State, + string? Reason, + string? Detail, + int Attempts, + string? RequestedBy, + string? Note, + DateTimeOffset RequestedUtc, + DateTimeOffset UpdatedUtc, + DateTimeOffset? SentUtc, + DateTimeOffset? LastSeenUtc, + DateTimeOffset? FinishedUtc, + bool ShowInDeviceList); + +/// A device a removal request left out, and why. +public sealed record DeviceRemovalSkipDto(string ClientId, string? Hostname, string Reason); + +/// What a removal request did. +public sealed record DeviceRemovalBatchDto( + IReadOnlyList Removals, + IReadOnlyList Skipped); diff --git a/src/Shared.Contracts/Messages/DeviceRemovalPayloads.cs b/src/Shared.Contracts/Messages/DeviceRemovalPayloads.cs new file mode 100644 index 0000000..85aebdf --- /dev/null +++ b/src/Shared.Contracts/Messages/DeviceRemovalPayloads.cs @@ -0,0 +1,23 @@ +namespace Shared.Contracts.Messages; + +/// +/// Removes the agent from the device; durable command on +/// agent.<clientId>.command.remove. The agent acts only when +/// is the enrollment it holds, so a stale +/// command can never uninstall a device that was enrolled again since. +/// +public sealed record AgentRemoveCommandPayload(Guid RemovalId, Guid EnrollmentId); + +/// +/// Progress of a removal, published by the agent on +/// agent.<clientId>.event with message type . +/// is uninstalling or failed (DeviceRemovalStates): +/// after a successful uninstall nobody is left to say so. +/// +public sealed record AgentRemovalEventPayload( + Guid RemovalId, + string ClientId, + string State, + string? Reason, + string? Detail, + DateTimeOffset TimestampUtc); diff --git a/src/Shared.Contracts/Messages/MessageTypes.cs b/src/Shared.Contracts/Messages/MessageTypes.cs index 69bed98..85f3ff5 100644 --- a/src/Shared.Contracts/Messages/MessageTypes.cs +++ b/src/Shared.Contracts/Messages/MessageTypes.cs @@ -11,11 +11,14 @@ public static class MessageTypes public const string AgentEvent = "agent.event"; /// Upgrade progress or outcome on the agent event subject (). public const string AgentUpgrade = "agent.upgrade"; + /// Removal progress on the agent event subject (). + public const string AgentRemoval = "agent.removal"; public const string CommandInventory = "command.inventory"; public const string CommandScript = "command.script"; public const string CommandFileDownload = "command.file.download"; public const string CommandAgentUpgrade = "command.agent.upgrade"; + public const string CommandAgentRemove = "command.agent.remove"; public const string CommandShellOpen = "command.shell.open"; public const string CommandShellInput = "command.shell.input"; public const string CommandShellResize = "command.shell.resize"; diff --git a/src/Shared.Contracts/Models/DeviceRemovals.cs b/src/Shared.Contracts/Models/DeviceRemovals.cs new file mode 100644 index 0000000..d6a93d5 --- /dev/null +++ b/src/Shared.Contracts/Models/DeviceRemovals.cs @@ -0,0 +1,91 @@ +namespace Shared.Contracts.Models; + +/// +/// States of one device's removal. The device row is gone from the first moment; +/// this record stands in for it until the agent has gone, and stays afterwards +/// as the history. A removal waits for as long as the device stays away. +/// +public static class DeviceRemovalStates +{ + /// Waiting for the device to come back (or for the next send). + public const string Waiting = "waiting"; + /// The remove command was sent; no answer yet. + public const string Sent = "sent"; + /// The agent accepted and is uninstalling (it told us, or its credential renewal was refused). + public const string Uninstalling = "uninstalling"; + /// The agent has gone: it uninstalled, or went silent for good after saying it was uninstalling. + public const string Removed = "removed"; + /// The uninstall failed on the device; the agent says why and the server asks again when it next returns. + public const string Failed = "failed"; + /// An operator stopped trying. The device stays blocked; its agent stays installed. + public const string Stopped = "stopped"; + + /// Not finished: the scheduler still acts on these. + public static readonly IReadOnlySet Open = new HashSet(StringComparer.Ordinal) + { Waiting, Sent, Uninstalling, Failed }; +} + +/// Why a removal failed, or what the agent could not do. +public static class DeviceRemovalReasons +{ + /// The agent is too old to take a remove command: uninstall it by hand, or upgrade it first. + public const string AgentTooOld = "agent-too-old"; + /// The agent cannot remove itself here (foreground or development run, container). + public const string Unsupported = "unsupported"; + /// An agent upgrade is in progress; the server asks again. + public const string Busy = "busy"; + /// Windows Installer failed to uninstall the agent. + public const string Msi = "msi"; + /// Files or services could not be removed. + public const string Uninstall = "uninstall"; + /// The command's enrollment is not the one this agent holds (a stale command); it was ignored. + public const string StaleEnrollment = "stale-enrollment"; +} + +/// Timings shared by the server's removal scheduler and the console. +public static class DeviceRemovalDefaults +{ + /// + /// How long a removal shows in the default Devices list after its last + /// change. After that only the Removals status filter shows it, so the main + /// list stays uncluttered while nothing is lost. + /// + public static readonly TimeSpan VisibleInDeviceList = TimeSpan.FromDays(7); + /// Silence after "uninstalling" that counts as gone: nobody is left to report success. + public static readonly TimeSpan UninstallSilence = TimeSpan.FromMinutes(15); + /// The first agent release that understands the remove command and renews its credential. + public const string MinimumAgentVersion = "1.1.0"; +} + +/// +/// The agent's lifetime on its NATS credential. It renews over HTTPS at half +/// life and at every start (older non-expiring credentials are replaced then), +/// so a removed device is cut off within a day: renewal answers 410 Gone. +/// +public static class AgentCredentialDefaults +{ + public static readonly TimeSpan Lifetime = TimeSpan.FromHours(24); + /// Problem type of the 410 answer: the agent then uninstalls itself. + public const string RemovedProblemType = "urn:problem:device-removed"; +} + +/// +/// A removal handed from the agent to the watchdog, in +/// <data>/upgrade/removal.json. The watchdog runs it from a detached +/// copy of itself outside the install and data folders, since it wipes both. +/// +public sealed record WatchdogRemoval( + Guid RemovalId, + string InstallKind, + string Phase, + int Attempts, + string? LastError, + DateTimeOffset RequestedUtc); + +/// Phases of . +public static class WatchdogRemovalPhases +{ + public const string Requested = "requested"; + public const string Uninstalling = "uninstalling"; + public const string Failed = "failed"; +} diff --git a/src/Shared.Contracts/Models/WatchdogFiles.cs b/src/Shared.Contracts/Models/WatchdogFiles.cs index f5c8219..e354c42 100644 --- a/src/Shared.Contracts/Models/WatchdogFiles.cs +++ b/src/Shared.Contracts/Models/WatchdogFiles.cs @@ -29,6 +29,8 @@ public static class WatchdogFiles /// MSI upgrades: the runner's journal (), beside handover.json. public const string MsiJournal = "msi.json"; + /// A removal handed to the watchdog (), beside handover.json. + public const string Removal = "removal.json"; /// MSI upgrades: upgrade/msi/ holds the MSIs being installed and the runner's copy of the watchdog binary. public const string MsiFolder = "msi"; /// diff --git a/src/Shared.Contracts/Serialization/WatchdogJsonContext.cs b/src/Shared.Contracts/Serialization/WatchdogJsonContext.cs index dbeb2f1..84a17ad 100644 --- a/src/Shared.Contracts/Serialization/WatchdogJsonContext.cs +++ b/src/Shared.Contracts/Serialization/WatchdogJsonContext.cs @@ -20,4 +20,5 @@ namespace Shared.Contracts.Serialization; [JsonSerializable(typeof(WatchdogJournal))] [JsonSerializable(typeof(WatchdogOutcome))] [JsonSerializable(typeof(WatchdogMsiJournal))] +[JsonSerializable(typeof(WatchdogRemoval))] public sealed partial class WatchdogJsonContext : JsonSerializerContext; diff --git a/src/Shared.Infrastructure/Nats/JetStreamDefaults.cs b/src/Shared.Infrastructure/Nats/JetStreamDefaults.cs index 2a345ac..06dd369 100644 --- a/src/Shared.Infrastructure/Nats/JetStreamDefaults.cs +++ b/src/Shared.Infrastructure/Nats/JetStreamDefaults.cs @@ -15,6 +15,7 @@ public static class JetStreamDefaults "agent.*.command.inventory", "agent.*.command.file", "agent.*.command.upgrade", + "agent.*.command.remove", ]; public const string JobEventsStreamName = "JOB_EVENTS"; diff --git a/src/Shared.Infrastructure/Nats/NatsSubjects.cs b/src/Shared.Infrastructure/Nats/NatsSubjects.cs index cc00979..3250828 100644 --- a/src/Shared.Infrastructure/Nats/NatsSubjects.cs +++ b/src/Shared.Infrastructure/Nats/NatsSubjects.cs @@ -18,6 +18,7 @@ public static class NatsSubjects public static string AgentCommandScript(string clientId) => $"agent.{SubjectIdentifier.Require(clientId)}.command.script"; public static string AgentCommandFile(string clientId) => $"agent.{SubjectIdentifier.Require(clientId)}.command.file"; public static string AgentCommandUpgrade(string clientId) => $"agent.{SubjectIdentifier.Require(clientId)}.command.upgrade"; + public static string AgentCommandRemove(string clientId) => $"agent.{SubjectIdentifier.Require(clientId)}.command.remove"; public static string AgentCommandShellOpen(string clientId) => $"agent.{SubjectIdentifier.Require(clientId)}.command.shell.open"; public static string AgentCommandShellInput(string clientId) => $"agent.{SubjectIdentifier.Require(clientId)}.command.shell.input"; public static string AgentCommandShellResize(string clientId) => $"agent.{SubjectIdentifier.Require(clientId)}.command.shell.resize"; -- 2.52.0 From d1525f8b3db2d5dac393dd230d07705f18f1c3f3aee60b0ea7820b86e1d418f7 Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Sat, 31 Oct 2026 19:57:41 -0400 Subject: [PATCH 02/20] feat(removal): remove devices from the system, and chase their agents POST /api/device-removals takes devices and groups (nested included) under a new devices.remove permission. Each device's row goes in one save with its group memberships and trigger runs; its own schedules are disabled and unstarted jobs cancelled. A DeviceRemovals row stands in for it and stays as the history, so old runs keep their names and a removed device keeps its trail. The groups it was in are kept there too, which is how a scoped role keeps seeing its removals. A scheduler sends the remove command when the device is seen online, marks agents too old to take it, retries, and closes a removal after silence following "uninstalling", clearing the durable consumer and command subjects first so a device enrolled again never replays them. Sightings from a client id with no row update the removal instead of being dropped, and never bring the row back. Co-Authored-By: Claude Opus 5.5 --- src/Backend/Api/DeviceEndpoints.cs | 22 +- src/Backend/Api/DeviceHistory.cs | 20 + src/Backend/Auth/DeviceScopeService.cs | 8 + src/Backend/Auth/Permissions.cs | 2 + src/Backend/Data/AppDbContext.cs | 26 +- src/Backend/Data/Entities.cs | 61 + .../20261031234300_DeviceRemovals.Designer.cs | 1301 +++++++++++++++++ .../Postgres/20261031234300_DeviceRemovals.cs | 67 + .../PostgresAppDbContextModelSnapshot.cs | 83 ++ .../20261031234307_DeviceRemovals.Designer.cs | 1301 +++++++++++++++++ .../20261031234307_DeviceRemovals.cs | 67 + .../SqlServerAppDbContextModelSnapshot.cs | 83 ++ .../20261031234253_DeviceRemovals.Designer.cs | 1288 ++++++++++++++++ .../Sqlite/20261031234253_DeviceRemovals.cs | 67 + .../Sqlite/SqliteAppDbContextModelSnapshot.cs | 83 ++ src/Backend/Jobs/CommandDispatcher.cs | 12 + src/Backend/Jobs/JobEndpoints.cs | 13 +- src/Backend/Messaging/MessagingWorkers.cs | 24 +- src/Backend/Nats/JetStreamProvisioner.cs | 30 + src/Backend/Program.cs | 5 + .../Removals/DeviceRemovalEndpoints.cs | 189 +++ .../Removals/DeviceRemovalScheduler.cs | 288 ++++ src/Backend/Removals/DeviceRemovalService.cs | 393 +++++ src/Backend/Upgrades/AgentUpgradeEndpoints.cs | 13 +- 24 files changed, 5435 insertions(+), 11 deletions(-) create mode 100644 src/Backend/Data/Migrations/Postgres/20261031234300_DeviceRemovals.Designer.cs create mode 100644 src/Backend/Data/Migrations/Postgres/20261031234300_DeviceRemovals.cs create mode 100644 src/Backend/Data/Migrations/SqlServer/20261031234307_DeviceRemovals.Designer.cs create mode 100644 src/Backend/Data/Migrations/SqlServer/20261031234307_DeviceRemovals.cs create mode 100644 src/Backend/Data/Migrations/Sqlite/20261031234253_DeviceRemovals.Designer.cs create mode 100644 src/Backend/Data/Migrations/Sqlite/20261031234253_DeviceRemovals.cs create mode 100644 src/Backend/Removals/DeviceRemovalEndpoints.cs create mode 100644 src/Backend/Removals/DeviceRemovalScheduler.cs create mode 100644 src/Backend/Removals/DeviceRemovalService.cs diff --git a/src/Backend/Api/DeviceEndpoints.cs b/src/Backend/Api/DeviceEndpoints.cs index 97c1120..90d7c67 100644 --- a/src/Backend/Api/DeviceEndpoints.cs +++ b/src/Backend/Api/DeviceEndpoints.cs @@ -98,7 +98,11 @@ public static class DeviceEndpoints await groups.GetGroupsForDeviceAsync(clientId, cancellationToken))); } - /// The device's phase trail (enrolled, renamed, OS changed), newest first. + /// + /// The device's phase trail (enrolled, renamed, OS changed), newest first. A + /// removed device has no row but keeps its trail, within the scope its removal + /// recorded. + /// private static async Task GetHistoryAsync( string clientId, int? limit, AppDbContext db, DeviceScopeService scope, HttpContext context, CancellationToken cancellationToken) @@ -108,8 +112,10 @@ public static class DeviceEndpoints return Results.BadRequest(); } - if (!await scope.IsAllowedAsync(context.User, clientId, cancellationToken) - || !await db.Devices.AnyAsync(d => d.ClientId == clientId, cancellationToken)) + var reachable = await db.Devices.AnyAsync(d => d.ClientId == clientId, cancellationToken) + ? await scope.IsAllowedAsync(context.User, clientId, cancellationToken) + : await IsRemovedWithinScopeAsync(clientId, db, scope, context, cancellationToken); + if (!reachable) { return Results.NotFound(); } @@ -125,6 +131,16 @@ public static class DeviceEndpoints return Results.Ok(events); } + private static async Task IsRemovedWithinScopeAsync( + string clientId, AppDbContext db, DeviceScopeService scope, HttpContext context, CancellationToken cancellationToken) + { + var scopeGroupIds = await scope.GetScopeGroupIdsAsync(context.User, cancellationToken); + var removals = await db.DeviceRemovals.AsNoTracking() + .Where(r => r.ClientId == clientId) + .ToListAsync(cancellationToken); + return removals.Any(r => Removals.DeviceRemovalService.IsVisibleTo(r, scopeGroupIds)); + } + private static async Task RefreshInventoryAsync( string clientId, AppDbContext db, diff --git a/src/Backend/Api/DeviceHistory.cs b/src/Backend/Api/DeviceHistory.cs index 7add844..9bec005 100644 --- a/src/Backend/Api/DeviceHistory.cs +++ b/src/Backend/Api/DeviceHistory.cs @@ -23,6 +23,13 @@ public static class DeviceHistory /// An upgrade failed and the agent stayed where it was (detail carries the reason). public const string AgentUpgradeFailed = "agent-upgrade-failed"; + /// An operator removed the device; its agent uninstalls now or when it returns. + public const string RemovalRequested = "device-removal-requested"; + /// The agent has gone, or the device enrolled again and the removal closed. + public const string Removed = "device-removed"; + /// An operator stopped trying: the device stays blocked, its agent stays installed. + public const string RemovalStopped = "device-removal-stopped"; + /// /// Applies a reported hostname and (when ) OS /// to the device, recording a rename / OS change against the previous @@ -77,6 +84,19 @@ public static class DeviceHistory public static void RecordAgentUpgrade(AppDbContext db, string clientId, string action, string detail) => Record(db, clientId, action, detail, DateTimeOffset.UtcNow); + /// Records a removal request, with who asked and their note. + public static void RecordRemovalRequested(AppDbContext db, string clientId, string? requestedBy, string? note) + => Record(db, clientId, RemovalRequested, + $"By {requestedBy ?? "unknown"}{(string.IsNullOrWhiteSpace(note) ? string.Empty : $" · {note}")}", DateTimeOffset.UtcNow); + + /// Records the end of a removal. + public static void RecordRemoved(AppDbContext db, string clientId, string detail) + => Record(db, clientId, Removed, detail, DateTimeOffset.UtcNow); + + /// Records an operator stopping a removal. + public static void RecordRemovalStopped(AppDbContext db, string clientId, string? stoppedBy) + => Record(db, clientId, RemovalStopped, $"By {stoppedBy ?? "unknown"}", DateTimeOffset.UtcNow); + /// "Windows PE 10.0.26100"; just the caption when the version is unknown. public static string Describe(string? caption, string? version) { diff --git a/src/Backend/Auth/DeviceScopeService.cs b/src/Backend/Auth/DeviceScopeService.cs index 45618ac..3c6d1f3 100644 --- a/src/Backend/Auth/DeviceScopeService.cs +++ b/src/Backend/Auth/DeviceScopeService.cs @@ -46,6 +46,14 @@ public sealed class DeviceScopeService( return allowed = clientIds; } + /// + /// The groups the caller's roles are limited to; null when unrestricted. A + /// removed device is in no group any more, so its removal is matched on these. + /// + public async Task?> GetScopeGroupIdsAsync( + ClaimsPrincipal user, CancellationToken cancellationToken) + => (await permissions.ResolveAccessAsync(user, cancellationToken)).ScopeGroupIds; + /// True when the caller may act on the device. public async Task IsAllowedAsync( ClaimsPrincipal user, string clientId, CancellationToken cancellationToken) diff --git a/src/Backend/Auth/Permissions.cs b/src/Backend/Auth/Permissions.cs index ec9aa0d..51b40d0 100644 --- a/src/Backend/Auth/Permissions.cs +++ b/src/Backend/Auth/Permissions.cs @@ -14,6 +14,7 @@ public static class Permissions public const string DevicesView = "devices.view"; public const string DevicesManage = "devices.manage"; + public const string DevicesRemove = "devices.remove"; public const string GroupsView = "groups.view"; public const string GroupsManage = "groups.manage"; public const string ScriptsView = "scripts.view"; @@ -38,6 +39,7 @@ public static class Permissions [ new(DevicesView, "View devices", "See the device list, details and inventory."), new(DevicesManage, "Manage devices", "Refresh inventory and edit device tags."), + new(DevicesRemove, "Remove devices", "Remove devices from the system and uninstall their agents."), ]), new("Groups", [ diff --git a/src/Backend/Data/AppDbContext.cs b/src/Backend/Data/AppDbContext.cs index 9c6a8bd..a08812c 100644 --- a/src/Backend/Data/AppDbContext.cs +++ b/src/Backend/Data/AppDbContext.cs @@ -33,6 +33,7 @@ public class AppDbContext(DbContextOptions options) : DbContext(op public DbSet SecretsProviders => Set(); public DbSet AgentPackages => Set(); public DbSet AgentUpgrades => Set(); + public DbSet DeviceRemovals => Set(); /// /// SQLite cannot ORDER BY / compare DateTimeOffset natively, so it stores @@ -66,7 +67,7 @@ public class AppDbContext(DbContextOptions options) : DbContext(op } /// - /// Every write to a trigger or an agent upgrade gets a fresh stamp; the + /// Every write to a trigger, an agent upgrade or a removal gets a fresh stamp; the /// update is conditioned on the stamp it was read with, so a concurrent /// write surfaces as DbUpdateConcurrencyException instead of silently winning. /// @@ -87,6 +88,14 @@ public class AppDbContext(DbContextOptions options) : DbContext(op entry.Entity.ConcurrencyStamp = Guid.NewGuid(); } } + + foreach (var entry in ChangeTracker.Entries()) + { + if (entry.State is EntityState.Added or EntityState.Modified) + { + entry.Entity.ConcurrencyStamp = Guid.NewGuid(); + } + } } protected override void OnModelCreating(ModelBuilder modelBuilder) @@ -345,5 +354,20 @@ public class AppDbContext(DbContextOptions options) : DbContext(op entity.Property(e => e.ConcurrencyStamp).IsConcurrencyToken(); }); + modelBuilder.Entity(entity => + { + entity.ToTable("DeviceRemovals"); + entity.HasKey(e => e.Id); + entity.Property(e => e.State).HasMaxLength(16); + entity.Property(e => e.Reason).HasMaxLength(32); + entity.Property(e => e.Detail).HasMaxLength(2000); + entity.Property(e => e.RequestedBy).HasMaxLength(256); + entity.Property(e => e.Note).HasMaxLength(500); + // Enrollment, renewal and sightings look up by client id; the scheduler sweeps by state. + entity.HasIndex(e => e.ClientId); + entity.HasIndex(e => e.State); + entity.HasIndex(e => e.RequestedUtc); + entity.Property(e => e.ConcurrencyStamp).IsConcurrencyToken(); + }); } } diff --git a/src/Backend/Data/Entities.cs b/src/Backend/Data/Entities.cs index a0aad4e..9982f19 100644 --- a/src/Backend/Data/Entities.cs +++ b/src/Backend/Data/Entities.cs @@ -561,6 +561,67 @@ public sealed class AgentUpgradeEntity public Guid ConcurrencyStamp { get; set; } } +/// +/// One device's removal. The device row is gone from the moment it is requested; +/// this stands in for it until the agent has gone, and stays afterwards as the +/// history (old runs still find the name here, and bootstrap the date of removal). +/// A client id has at most one open removal: one is only added as the device row +/// is deleted, and enrolling again closes it as the row comes back. +/// +public sealed class DeviceRemovalEntity +{ + public Guid Id { get; set; } + public required string ClientId { get; set; } + + /// The enrolled key: a credential renewal must still prove possession of it. + public required string NkeyPublic { get; set; } + + /// Carried by the remove command; the agent ignores one for another enrollment. + public Guid EnrollmentId { get; set; } + + public string? Hostname { get; set; } + public string? OsCaption { get; set; } + + /// Running version: as removed, then from presence. + public string? AgentVersion { get; set; } + + /// A DeviceRemovalStates value. + public required string State { get; set; } + + /// A DeviceRemovalReasons value. + public string? Reason { get; set; } + + public string? Detail { get; set; } + + /// Times the command was handed to JetStream. + public int Attempts { get; set; } + + public string? RequestedBy { get; set; } + public string? Note { get; set; } + public DateTimeOffset RequestedUtc { get; set; } + + /// Last change of state; sightings move only . + public DateTimeOffset UpdatedUtc { get; set; } + + public DateTimeOffset? SentUtc { get; set; } + public DateTimeOffset? LastSeenUtc { get; set; } + public DateTimeOffset? FinishedUtc { get; set; } + + /// + /// JSON array of the groups the device was in, nesting included, when it was + /// removed: a scoped role's device scope is its groups, and it keeps seeing + /// the removals of the devices that were in them. + /// + public string? GroupIdsJson { get; set; } + + /// + /// Renewed on every save (AppDbContext): the scheduler, the event consumer, + /// credential renewal and an operator's stop may touch the same row on + /// different nodes, and the later write is refused instead of silently winning. + /// + public Guid ConcurrencyStamp { get; set; } +} + public sealed class ShellSessionEntity { public Guid Id { get; set; } diff --git a/src/Backend/Data/Migrations/Postgres/20261031234300_DeviceRemovals.Designer.cs b/src/Backend/Data/Migrations/Postgres/20261031234300_DeviceRemovals.Designer.cs new file mode 100644 index 0000000..e79c789 --- /dev/null +++ b/src/Backend/Data/Migrations/Postgres/20261031234300_DeviceRemovals.Designer.cs @@ -0,0 +1,1301 @@ +// +using System; +using Backend.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Backend.Data.Migrations.Postgres +{ + [DbContext(typeof(PostgresAppDbContext))] + [Migration("20261031234300_DeviceRemovals")] + partial class DeviceRemovals + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "10.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Backend.Data.AgentPackageEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("FileName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("ImportedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("PackageType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("character varying(32)"); + + b.Property("S3Key") + .HasMaxLength(512) + .HasColumnType("character varying(512)"); + + b.Property("Sha256") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("SizeBytes") + .HasColumnType("bigint"); + + b.Property("StoragePath") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("character varying(512)"); + + b.Property("Version") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.HasKey("Id"); + + b.HasIndex("Sha256"); + + b.HasIndex("Version", "PackageType") + .IsUnique(); + + b.ToTable("AgentPackages", (string)null); + }); + + modelBuilder.Entity("Backend.Data.AgentUpgradeEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Attempts") + .HasColumnType("integer"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("text"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("uuid"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Detail") + .HasMaxLength(2000) + .HasColumnType("character varying(2000)"); + + b.Property("FinishedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("FromVersion") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("LastEventUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("LastLogJson") + .HasColumnType("text"); + + b.Property("PackageId") + .HasColumnType("uuid"); + + b.Property("PackageType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("character varying(32)"); + + b.Property("Reason") + .HasMaxLength(32) + .HasColumnType("character varying(32)"); + + b.Property("RequestedBy") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("SentUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Sha256") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("Source") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("State") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("TargetVersion") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.HasKey("Id"); + + b.HasIndex("CreatedUtc"); + + b.HasIndex("State"); + + b.HasIndex("ClientId", "State"); + + b.ToTable("AgentUpgrades", (string)null); + }); + + modelBuilder.Entity("Backend.Data.AppSettingEntity", b => + { + b.Property("Key") + .HasColumnType("text"); + + b.Property("Etag") + .HasColumnType("text"); + + b.Property("IsSecret") + .HasColumnType("boolean"); + + b.Property("UpdatedBy") + .HasColumnType("text"); + + b.Property("UpdatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("Key"); + + b.ToTable("AppSettings", (string)null); + }); + + modelBuilder.Entity("Backend.Data.AuditEventEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Action") + .IsRequired() + .HasColumnType("text"); + + b.Property("Actor") + .HasColumnType("text"); + + b.Property("Category") + .IsRequired() + .HasColumnType("text"); + + b.Property("ClientId") + .HasColumnType("text"); + + b.Property("Detail") + .HasColumnType("text"); + + b.Property("RemoteIp") + .HasColumnType("text"); + + b.Property("TimestampUtc") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("TimestampUtc"); + + b.ToTable("AuditEvents", (string)null); + }); + + modelBuilder.Entity("Backend.Data.BootstrapTokenEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedBy") + .HasColumnType("text"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("ExpiresUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("MaxUses") + .HasColumnType("integer"); + + b.Property("Note") + .HasColumnType("text"); + + b.Property("Revoked") + .HasColumnType("boolean"); + + b.Property("TokenHash") + .IsRequired() + .HasColumnType("text"); + + b.Property("Uses") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("TokenHash") + .IsUnique(); + + b.ToTable("BootstrapTokens", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceEntity", b => + { + b.Property("ClientId") + .HasColumnType("text"); + + b.Property("AgentInstall") + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("AgentRuntime") + .HasMaxLength(32) + .HasColumnType("character varying(32)"); + + b.Property("AgentVersion") + .HasColumnType("text"); + + b.Property("BootstrapTokenId") + .HasColumnType("uuid"); + + b.Property("EnrolledUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("EnrollmentId") + .HasColumnType("uuid"); + + b.Property("Hostname") + .IsRequired() + .HasColumnType("text"); + + b.Property("IpAddressesJson") + .HasColumnType("text"); + + b.Property("IsOrphaned") + .HasColumnType("boolean"); + + b.Property("LastSeenUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Manufacturer") + .HasColumnType("text"); + + b.Property("Model") + .HasColumnType("text"); + + b.Property("NkeyPublic") + .IsRequired() + .HasColumnType("text"); + + b.Property("OrphanedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("OsCaption") + .HasColumnType("text"); + + b.Property("OsVersion") + .HasColumnType("text"); + + b.Property("PublicIp") + .HasColumnType("text"); + + b.Property("ReadyUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("TagsJson") + .HasColumnType("text"); + + b.HasKey("ClientId"); + + b.HasIndex("Hostname"); + + b.ToTable("Devices", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceGroupEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Description") + .HasColumnType("text"); + + b.Property("Kind") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("ParentId") + .HasColumnType("uuid"); + + b.Property("RulesJson") + .HasColumnType("text"); + + b.Property("UpdatedBy") + .HasColumnType("text"); + + b.Property("UpdatedUtc") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("DeviceGroups", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceGroupMemberEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AddedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("text"); + + b.Property("GroupId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("GroupId", "ClientId") + .IsUnique(); + + b.ToTable("DeviceGroupMembers", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceInventoryEntity", b => + { + b.Property("ClientId") + .HasColumnType("text"); + + b.Property("ReportJson") + .IsRequired() + .HasColumnType("text"); + + b.Property("UpdatedUtc") + .HasColumnType("timestamp with time zone"); + + b.HasKey("ClientId"); + + b.ToTable("DeviceInventory", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceRemovalEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AgentVersion") + .HasColumnType("text"); + + b.Property("Attempts") + .HasColumnType("integer"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("text"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("uuid"); + + b.Property("Detail") + .HasMaxLength(2000) + .HasColumnType("character varying(2000)"); + + b.Property("EnrollmentId") + .HasColumnType("uuid"); + + b.Property("FinishedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("GroupIdsJson") + .HasColumnType("text"); + + b.Property("Hostname") + .HasColumnType("text"); + + b.Property("LastSeenUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("NkeyPublic") + .IsRequired() + .HasColumnType("text"); + + b.Property("Note") + .HasMaxLength(500) + .HasColumnType("character varying(500)"); + + b.Property("OsCaption") + .HasColumnType("text"); + + b.Property("Reason") + .HasMaxLength(32) + .HasColumnType("character varying(32)"); + + b.Property("RequestedBy") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("RequestedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("SentUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("State") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("UpdatedUtc") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("RequestedUtc"); + + b.HasIndex("State"); + + b.ToTable("DeviceRemovals", (string)null); + }); + + modelBuilder.Entity("Backend.Data.FunctionModuleEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Content") + .IsRequired() + .HasColumnType("text"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Description") + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("UpdatedBy") + .HasColumnType("text"); + + b.Property("UpdatedUtc") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("Name"); + + b.ToTable("FunctionModules", (string)null); + }); + + modelBuilder.Entity("Backend.Data.JobEntity", b => + { + b.Property("JobId") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("text"); + + b.Property("CompletedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("ExitCode") + .HasColumnType("integer"); + + b.Property("OutputSummary") + .HasColumnType("text"); + + b.Property("RequestedBy") + .HasColumnType("text"); + + b.Property("RunId") + .HasColumnType("uuid"); + + b.Property("ScriptId") + .HasColumnType("uuid"); + + b.Property("ScriptName") + .IsRequired() + .HasColumnType("text"); + + b.Property("StartedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("State") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("JobId"); + + b.HasIndex("ClientId"); + + b.HasIndex("CreatedUtc"); + + b.HasIndex("RunId"); + + b.HasIndex("ScriptId", "RunId"); + + b.ToTable("Jobs", (string)null); + }); + + modelBuilder.Entity("Backend.Data.JobOutputEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("JobId") + .HasColumnType("uuid"); + + b.Property("Sequence") + .HasColumnType("bigint"); + + b.Property("Stream") + .IsRequired() + .HasColumnType("text"); + + b.Property("Text") + .IsRequired() + .HasColumnType("text"); + + b.Property("TimestampUtc") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("JobId", "Sequence") + .IsUnique(); + + b.ToTable("JobOutput", (string)null); + }); + + modelBuilder.Entity("Backend.Data.LocalGroupEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Description") + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("UpdatedUtc") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("LocalGroups", (string)null); + }); + + modelBuilder.Entity("Backend.Data.LocalGroupMemberEntity", b => + { + b.Property("GroupId") + .HasColumnType("uuid"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("GroupId", "UserId"); + + b.HasIndex("UserId"); + + b.ToTable("LocalGroupMembers", (string)null); + }); + + modelBuilder.Entity("Backend.Data.RoleBindingEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedBy") + .HasColumnType("text"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("PrincipalId") + .IsRequired() + .HasMaxLength(450) + .HasColumnType("character varying(450)"); + + b.Property("PrincipalKind") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("character varying(32)"); + + b.Property("PrincipalName") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.HasIndex("PrincipalKind", "PrincipalId", "RoleId") + .IsUnique(); + + b.ToTable("RoleBindings", (string)null); + }); + + modelBuilder.Entity("Backend.Data.RoleEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Description") + .HasColumnType("text"); + + b.Property("DeviceScopeGroupIdsJson") + .HasColumnType("text"); + + b.Property("IsSystem") + .HasColumnType("boolean"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("OidcClaimValuesJson") + .HasColumnType("text"); + + b.Property("PermissionsJson") + .HasColumnType("text"); + + b.Property("UpdatedBy") + .HasColumnType("text"); + + b.Property("UpdatedUtc") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("Roles", (string)null); + }); + + modelBuilder.Entity("Backend.Data.RunEntity", b => + { + b.Property("RunId") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("OnlineOnly") + .HasColumnType("boolean"); + + b.Property("RequestedBy") + .HasColumnType("text"); + + b.Property("ScriptId") + .HasColumnType("uuid"); + + b.Property("ScriptName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)"); + + b.Property("SkippedJson") + .IsRequired() + .HasColumnType("text"); + + b.Property("Source") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("character varying(32)"); + + b.Property("TargetsLabel") + .HasMaxLength(400) + .HasColumnType("character varying(400)"); + + b.Property("TriggerId") + .HasColumnType("uuid"); + + b.HasKey("RunId"); + + b.HasIndex("CreatedUtc"); + + b.HasIndex("ScriptId"); + + b.ToTable("Runs", (string)null); + }); + + modelBuilder.Entity("Backend.Data.ScriptEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Content") + .IsRequired() + .HasColumnType("text"); + + b.Property("CreatedBy") + .HasColumnType("text"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Description") + .HasColumnType("text"); + + b.Property("FileBindingsJson") + .HasColumnType("text"); + + b.Property("FunctionModuleIdsJson") + .HasColumnType("text"); + + b.Property("Language") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("ParametersJson") + .HasColumnType("text"); + + b.Property("SecretBindingsJson") + .HasColumnType("text"); + + b.Property("SecretsVariableName") + .HasColumnType("text"); + + b.Property("UpdatedBy") + .HasColumnType("text"); + + b.Property("UpdatedUtc") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("Name"); + + b.ToTable("Scripts", (string)null); + }); + + modelBuilder.Entity("Backend.Data.ScriptTriggerEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ArgumentsJson") + .HasColumnType("text"); + + b.Property("ClientId") + .HasColumnType("text"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("uuid"); + + b.Property("CreatedBy") + .HasColumnType("text"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("CronExpression") + .HasColumnType("text"); + + b.Property("Enabled") + .HasColumnType("boolean"); + + b.Property("GroupId") + .HasColumnType("uuid"); + + b.Property("LastResult") + .HasColumnType("text"); + + b.Property("LastRunId") + .HasColumnType("uuid"); + + b.Property("LastRunUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("NextRunUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("OncePerDevice") + .HasColumnType("boolean"); + + b.Property("RunOnceUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("ScriptId") + .HasColumnType("uuid"); + + b.Property("TimeZoneId") + .IsRequired() + .HasColumnType("text"); + + b.Property("TriggerType") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("NextRunUtc"); + + b.HasIndex("TriggerType"); + + b.ToTable("ScriptTriggers", (string)null); + }); + + modelBuilder.Entity("Backend.Data.ScriptTriggerRunEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("text"); + + b.Property("FiredUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("JobId") + .HasColumnType("uuid"); + + b.Property("TriggerId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("TriggerId", "ClientId") + .IsUnique(); + + b.ToTable("ScriptTriggerRuns", (string)null); + }); + + modelBuilder.Entity("Backend.Data.SecretsProviderEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConfigProtected") + .IsRequired() + .HasColumnType("text"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Kind") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("UpdatedBy") + .HasColumnType("text"); + + b.Property("UpdatedUtc") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("SecretsProviders", (string)null); + }); + + modelBuilder.Entity("Backend.Data.SessionEntity", b => + { + b.Property("Id") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("AbsoluteExpiresUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("ClientAddress") + .HasColumnType("text"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("CsrfHash") + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("ExpiresUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("GroupsJson") + .HasColumnType("text"); + + b.Property("LastSeenUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Provider") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("RevokedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("SecretHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("UserAgent") + .HasMaxLength(512) + .HasColumnType("character varying(512)"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("AbsoluteExpiresUtc"); + + b.HasIndex("UserId"); + + b.ToTable("Sessions", (string)null); + }); + + modelBuilder.Entity("Backend.Data.ShellSessionEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("text"); + + b.Property("CloseReason") + .HasColumnType("text"); + + b.Property("ClosedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("RequestedBy") + .HasColumnType("text"); + + b.Property("State") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.ToTable("ShellSessions", (string)null); + }); + + modelBuilder.Entity("Backend.Data.SignInRequestEntity", b => + { + b.Property("Id") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("CodeVerifier") + .HasColumnType("text"); + + b.Property("CompletedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Detail") + .HasMaxLength(2000) + .HasColumnType("character varying(2000)"); + + b.Property("ExpiresUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Nonce") + .HasColumnType("text"); + + b.Property("Provider") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("ReturnUrl") + .HasColumnType("text"); + + b.Property("SettingsEtag") + .HasColumnType("text"); + + b.Property("Status") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("StepUpSessionId") + .HasColumnType("text"); + + b.Property("TestSessionId") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("ExpiresUtc"); + + b.HasIndex("TestSessionId"); + + b.ToTable("SignInRequests", (string)null); + }); + + modelBuilder.Entity("Backend.Data.SignInThrottleEntity", b => + { + b.Property("Key") + .HasMaxLength(300) + .HasColumnType("character varying(300)"); + + b.Property("BlockedUntilUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Failures") + .HasColumnType("integer"); + + b.Property("FirstFailureUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("LastFailureUtc") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Key"); + + b.ToTable("SignInThrottles", (string)null); + }); + + modelBuilder.Entity("Backend.Data.StepUpEntity", b => + { + b.Property("SessionId") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("Method") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("VerifiedUtc") + .HasColumnType("timestamp with time zone"); + + b.HasKey("SessionId"); + + b.ToTable("StepUps", (string)null); + }); + + modelBuilder.Entity("Backend.Data.UserEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Description") + .HasColumnType("text"); + + b.Property("DisplayName") + .HasColumnType("text"); + + b.Property("Email") + .HasColumnType("text"); + + b.Property("Enabled") + .HasColumnType("boolean"); + + b.Property("ExternalId") + .HasMaxLength(450) + .HasColumnType("character varying(450)"); + + b.Property("IsBreakGlass") + .HasColumnType("boolean"); + + b.Property("Kind") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("LastSignInUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("LegacyRole") + .HasMaxLength(32) + .HasColumnType("character varying(32)"); + + b.Property("LegacyRoleId") + .HasColumnType("uuid"); + + b.Property("MustChangePassword") + .HasColumnType("boolean"); + + b.Property("PasswordChangedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("UpdatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("UserName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("WindowsGroupsJson") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("Kind", "ExternalId"); + + b.HasIndex("Kind", "UserName") + .IsUnique(); + + b.ToTable("Users", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceInventoryEntity", b => + { + b.HasOne("Backend.Data.DeviceEntity", null) + .WithOne("Inventory") + .HasForeignKey("Backend.Data.DeviceInventoryEntity", "ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Backend.Data.LocalGroupMemberEntity", b => + { + b.HasOne("Backend.Data.LocalGroupEntity", null) + .WithMany() + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Backend.Data.UserEntity", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Backend.Data.RoleBindingEntity", b => + { + b.HasOne("Backend.Data.RoleEntity", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Backend.Data.SessionEntity", b => + { + b.HasOne("Backend.Data.UserEntity", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("Backend.Data.DeviceEntity", b => + { + b.Navigation("Inventory"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/Backend/Data/Migrations/Postgres/20261031234300_DeviceRemovals.cs b/src/Backend/Data/Migrations/Postgres/20261031234300_DeviceRemovals.cs new file mode 100644 index 0000000..a1e4e74 --- /dev/null +++ b/src/Backend/Data/Migrations/Postgres/20261031234300_DeviceRemovals.cs @@ -0,0 +1,67 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Backend.Data.Migrations.Postgres +{ + /// + public partial class DeviceRemovals : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "DeviceRemovals", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + ClientId = table.Column(type: "text", nullable: false), + NkeyPublic = table.Column(type: "text", nullable: false), + EnrollmentId = table.Column(type: "uuid", nullable: false), + Hostname = table.Column(type: "text", nullable: true), + OsCaption = table.Column(type: "text", nullable: true), + AgentVersion = table.Column(type: "text", nullable: true), + State = table.Column(type: "character varying(16)", maxLength: 16, nullable: false), + Reason = table.Column(type: "character varying(32)", maxLength: 32, nullable: true), + Detail = table.Column(type: "character varying(2000)", maxLength: 2000, nullable: true), + Attempts = table.Column(type: "integer", nullable: false), + RequestedBy = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + Note = table.Column(type: "character varying(500)", maxLength: 500, nullable: true), + RequestedUtc = table.Column(type: "timestamp with time zone", nullable: false), + UpdatedUtc = table.Column(type: "timestamp with time zone", nullable: false), + SentUtc = table.Column(type: "timestamp with time zone", nullable: true), + LastSeenUtc = table.Column(type: "timestamp with time zone", nullable: true), + FinishedUtc = table.Column(type: "timestamp with time zone", nullable: true), + GroupIdsJson = table.Column(type: "text", nullable: true), + ConcurrencyStamp = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_DeviceRemovals", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_DeviceRemovals_ClientId", + table: "DeviceRemovals", + column: "ClientId"); + + migrationBuilder.CreateIndex( + name: "IX_DeviceRemovals_RequestedUtc", + table: "DeviceRemovals", + column: "RequestedUtc"); + + migrationBuilder.CreateIndex( + name: "IX_DeviceRemovals_State", + table: "DeviceRemovals", + column: "State"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "DeviceRemovals"); + } + } +} diff --git a/src/Backend/Data/Migrations/Postgres/PostgresAppDbContextModelSnapshot.cs b/src/Backend/Data/Migrations/Postgres/PostgresAppDbContextModelSnapshot.cs index bd04ae8..5c7aff5 100644 --- a/src/Backend/Data/Migrations/Postgres/PostgresAppDbContextModelSnapshot.cs +++ b/src/Backend/Data/Migrations/Postgres/PostgresAppDbContextModelSnapshot.cs @@ -417,6 +417,89 @@ namespace Backend.Data.Migrations.Postgres b.ToTable("DeviceInventory", (string)null); }); + modelBuilder.Entity("Backend.Data.DeviceRemovalEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AgentVersion") + .HasColumnType("text"); + + b.Property("Attempts") + .HasColumnType("integer"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("text"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("uuid"); + + b.Property("Detail") + .HasMaxLength(2000) + .HasColumnType("character varying(2000)"); + + b.Property("EnrollmentId") + .HasColumnType("uuid"); + + b.Property("FinishedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("GroupIdsJson") + .HasColumnType("text"); + + b.Property("Hostname") + .HasColumnType("text"); + + b.Property("LastSeenUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("NkeyPublic") + .IsRequired() + .HasColumnType("text"); + + b.Property("Note") + .HasMaxLength(500) + .HasColumnType("character varying(500)"); + + b.Property("OsCaption") + .HasColumnType("text"); + + b.Property("Reason") + .HasMaxLength(32) + .HasColumnType("character varying(32)"); + + b.Property("RequestedBy") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("RequestedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("SentUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("State") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("UpdatedUtc") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("RequestedUtc"); + + b.HasIndex("State"); + + b.ToTable("DeviceRemovals", (string)null); + }); + modelBuilder.Entity("Backend.Data.FunctionModuleEntity", b => { b.Property("Id") diff --git a/src/Backend/Data/Migrations/SqlServer/20261031234307_DeviceRemovals.Designer.cs b/src/Backend/Data/Migrations/SqlServer/20261031234307_DeviceRemovals.Designer.cs new file mode 100644 index 0000000..b01c72e --- /dev/null +++ b/src/Backend/Data/Migrations/SqlServer/20261031234307_DeviceRemovals.Designer.cs @@ -0,0 +1,1301 @@ +// +using System; +using Backend.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Backend.Data.Migrations.SqlServer +{ + [DbContext(typeof(SqlServerAppDbContext))] + [Migration("20261031234307_DeviceRemovals")] + partial class DeviceRemovals + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "10.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Backend.Data.AgentPackageEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("FileName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("nvarchar(128)"); + + b.Property("ImportedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("PackageType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("nvarchar(32)"); + + b.Property("S3Key") + .HasMaxLength(512) + .HasColumnType("nvarchar(512)"); + + b.Property("Sha256") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + + b.Property("SizeBytes") + .HasColumnType("bigint"); + + b.Property("StoragePath") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("nvarchar(512)"); + + b.Property("Version") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + + b.HasKey("Id"); + + b.HasIndex("Sha256"); + + b.HasIndex("Version", "PackageType") + .IsUnique(); + + b.ToTable("AgentPackages", (string)null); + }); + + modelBuilder.Entity("Backend.Data.AgentUpgradeEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("Attempts") + .HasColumnType("int"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Detail") + .HasMaxLength(2000) + .HasColumnType("nvarchar(2000)"); + + b.Property("FinishedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("FromVersion") + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + + b.Property("LastEventUtc") + .HasColumnType("datetimeoffset"); + + b.Property("LastLogJson") + .HasColumnType("nvarchar(max)"); + + b.Property("PackageId") + .HasColumnType("uniqueidentifier"); + + b.Property("PackageType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("nvarchar(32)"); + + b.Property("Reason") + .HasMaxLength(32) + .HasColumnType("nvarchar(32)"); + + b.Property("RequestedBy") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property("SentUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Sha256") + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + + b.Property("Source") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("nvarchar(16)"); + + b.Property("State") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("nvarchar(16)"); + + b.Property("TargetVersion") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + + b.HasKey("Id"); + + b.HasIndex("CreatedUtc"); + + b.HasIndex("State"); + + b.HasIndex("ClientId", "State"); + + b.ToTable("AgentUpgrades", (string)null); + }); + + modelBuilder.Entity("Backend.Data.AppSettingEntity", b => + { + b.Property("Key") + .HasColumnType("nvarchar(450)"); + + b.Property("Etag") + .HasColumnType("nvarchar(max)"); + + b.Property("IsSecret") + .HasColumnType("bit"); + + b.Property("UpdatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Value") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Key"); + + b.ToTable("AppSettings", (string)null); + }); + + modelBuilder.Entity("Backend.Data.AuditEventEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Action") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Actor") + .HasColumnType("nvarchar(max)"); + + b.Property("Category") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ClientId") + .HasColumnType("nvarchar(max)"); + + b.Property("Detail") + .HasColumnType("nvarchar(max)"); + + b.Property("RemoteIp") + .HasColumnType("nvarchar(max)"); + + b.Property("TimestampUtc") + .HasColumnType("datetimeoffset"); + + b.HasKey("Id"); + + b.HasIndex("TimestampUtc"); + + b.ToTable("AuditEvents", (string)null); + }); + + modelBuilder.Entity("Backend.Data.BootstrapTokenEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("ExpiresUtc") + .HasColumnType("datetimeoffset"); + + b.Property("MaxUses") + .HasColumnType("int"); + + b.Property("Note") + .HasColumnType("nvarchar(max)"); + + b.Property("Revoked") + .HasColumnType("bit"); + + b.Property("TokenHash") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("Uses") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("TokenHash") + .IsUnique(); + + b.ToTable("BootstrapTokens", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceEntity", b => + { + b.Property("ClientId") + .HasColumnType("nvarchar(450)"); + + b.Property("AgentInstall") + .HasMaxLength(16) + .HasColumnType("nvarchar(16)"); + + b.Property("AgentRuntime") + .HasMaxLength(32) + .HasColumnType("nvarchar(32)"); + + b.Property("AgentVersion") + .HasColumnType("nvarchar(max)"); + + b.Property("BootstrapTokenId") + .HasColumnType("uniqueidentifier"); + + b.Property("EnrolledUtc") + .HasColumnType("datetimeoffset"); + + b.Property("EnrollmentId") + .HasColumnType("uniqueidentifier"); + + b.Property("Hostname") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("IpAddressesJson") + .HasColumnType("nvarchar(max)"); + + b.Property("IsOrphaned") + .HasColumnType("bit"); + + b.Property("LastSeenUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Manufacturer") + .HasColumnType("nvarchar(max)"); + + b.Property("Model") + .HasColumnType("nvarchar(max)"); + + b.Property("NkeyPublic") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OrphanedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("OsCaption") + .HasColumnType("nvarchar(max)"); + + b.Property("OsVersion") + .HasColumnType("nvarchar(max)"); + + b.Property("PublicIp") + .HasColumnType("nvarchar(max)"); + + b.Property("ReadyUtc") + .HasColumnType("datetimeoffset"); + + b.Property("TagsJson") + .HasColumnType("nvarchar(max)"); + + b.HasKey("ClientId"); + + b.HasIndex("Hostname"); + + b.ToTable("Devices", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceGroupEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("Kind") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("ParentId") + .HasColumnType("uniqueidentifier"); + + b.Property("RulesJson") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedUtc") + .HasColumnType("datetimeoffset"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("DeviceGroups", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceGroupMemberEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("AddedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("GroupId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("GroupId", "ClientId") + .IsUnique(); + + b.ToTable("DeviceGroupMembers", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceInventoryEntity", b => + { + b.Property("ClientId") + .HasColumnType("nvarchar(450)"); + + b.Property("ReportJson") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedUtc") + .HasColumnType("datetimeoffset"); + + b.HasKey("ClientId"); + + b.ToTable("DeviceInventory", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceRemovalEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("AgentVersion") + .HasColumnType("nvarchar(max)"); + + b.Property("Attempts") + .HasColumnType("int"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("uniqueidentifier"); + + b.Property("Detail") + .HasMaxLength(2000) + .HasColumnType("nvarchar(2000)"); + + b.Property("EnrollmentId") + .HasColumnType("uniqueidentifier"); + + b.Property("FinishedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("GroupIdsJson") + .HasColumnType("nvarchar(max)"); + + b.Property("Hostname") + .HasColumnType("nvarchar(max)"); + + b.Property("LastSeenUtc") + .HasColumnType("datetimeoffset"); + + b.Property("NkeyPublic") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Note") + .HasMaxLength(500) + .HasColumnType("nvarchar(500)"); + + b.Property("OsCaption") + .HasColumnType("nvarchar(max)"); + + b.Property("Reason") + .HasMaxLength(32) + .HasColumnType("nvarchar(32)"); + + b.Property("RequestedBy") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property("RequestedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("SentUtc") + .HasColumnType("datetimeoffset"); + + b.Property("State") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("nvarchar(16)"); + + b.Property("UpdatedUtc") + .HasColumnType("datetimeoffset"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("RequestedUtc"); + + b.HasIndex("State"); + + b.ToTable("DeviceRemovals", (string)null); + }); + + modelBuilder.Entity("Backend.Data.FunctionModuleEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("Content") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("UpdatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedUtc") + .HasColumnType("datetimeoffset"); + + b.HasKey("Id"); + + b.HasIndex("Name"); + + b.ToTable("FunctionModules", (string)null); + }); + + modelBuilder.Entity("Backend.Data.JobEntity", b => + { + b.Property("JobId") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("CompletedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("ExitCode") + .HasColumnType("int"); + + b.Property("OutputSummary") + .HasColumnType("nvarchar(max)"); + + b.Property("RequestedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("RunId") + .HasColumnType("uniqueidentifier"); + + b.Property("ScriptId") + .HasColumnType("uniqueidentifier"); + + b.Property("ScriptName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("StartedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("State") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("JobId"); + + b.HasIndex("ClientId"); + + b.HasIndex("CreatedUtc"); + + b.HasIndex("RunId"); + + b.HasIndex("ScriptId", "RunId"); + + b.ToTable("Jobs", (string)null); + }); + + modelBuilder.Entity("Backend.Data.JobOutputEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("JobId") + .HasColumnType("uniqueidentifier"); + + b.Property("Sequence") + .HasColumnType("bigint"); + + b.Property("Stream") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Text") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("TimestampUtc") + .HasColumnType("datetimeoffset"); + + b.HasKey("Id"); + + b.HasIndex("JobId", "Sequence") + .IsUnique(); + + b.ToTable("JobOutput", (string)null); + }); + + modelBuilder.Entity("Backend.Data.LocalGroupEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("nvarchar(128)"); + + b.Property("UpdatedUtc") + .HasColumnType("datetimeoffset"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("LocalGroups", (string)null); + }); + + modelBuilder.Entity("Backend.Data.LocalGroupMemberEntity", b => + { + b.Property("GroupId") + .HasColumnType("uniqueidentifier"); + + b.Property("UserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("GroupId", "UserId"); + + b.HasIndex("UserId"); + + b.ToTable("LocalGroupMembers", (string)null); + }); + + modelBuilder.Entity("Backend.Data.RoleBindingEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("PrincipalId") + .IsRequired() + .HasMaxLength(450) + .HasColumnType("nvarchar(450)"); + + b.Property("PrincipalKind") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("nvarchar(32)"); + + b.Property("PrincipalName") + .HasColumnType("nvarchar(max)"); + + b.Property("RoleId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.HasIndex("PrincipalKind", "PrincipalId", "RoleId") + .IsUnique(); + + b.ToTable("RoleBindings", (string)null); + }); + + modelBuilder.Entity("Backend.Data.RoleEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("DeviceScopeGroupIdsJson") + .HasColumnType("nvarchar(max)"); + + b.Property("IsSystem") + .HasColumnType("bit"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("OidcClaimValuesJson") + .HasColumnType("nvarchar(max)"); + + b.Property("PermissionsJson") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedUtc") + .HasColumnType("datetimeoffset"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("Roles", (string)null); + }); + + modelBuilder.Entity("Backend.Data.RunEntity", b => + { + b.Property("RunId") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("OnlineOnly") + .HasColumnType("bit"); + + b.Property("RequestedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("ScriptId") + .HasColumnType("uniqueidentifier"); + + b.Property("ScriptName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("SkippedJson") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Source") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("nvarchar(32)"); + + b.Property("TargetsLabel") + .HasMaxLength(400) + .HasColumnType("nvarchar(400)"); + + b.Property("TriggerId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("RunId"); + + b.HasIndex("CreatedUtc"); + + b.HasIndex("ScriptId"); + + b.ToTable("Runs", (string)null); + }); + + modelBuilder.Entity("Backend.Data.ScriptEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("Content") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("CreatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("FileBindingsJson") + .HasColumnType("nvarchar(max)"); + + b.Property("FunctionModuleIdsJson") + .HasColumnType("nvarchar(max)"); + + b.Property("Language") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("ParametersJson") + .HasColumnType("nvarchar(max)"); + + b.Property("SecretBindingsJson") + .HasColumnType("nvarchar(max)"); + + b.Property("SecretsVariableName") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedUtc") + .HasColumnType("datetimeoffset"); + + b.HasKey("Id"); + + b.HasIndex("Name"); + + b.ToTable("Scripts", (string)null); + }); + + modelBuilder.Entity("Backend.Data.ScriptTriggerEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("ArgumentsJson") + .HasColumnType("nvarchar(max)"); + + b.Property("ClientId") + .HasColumnType("nvarchar(max)"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("CronExpression") + .HasColumnType("nvarchar(max)"); + + b.Property("Enabled") + .HasColumnType("bit"); + + b.Property("GroupId") + .HasColumnType("uniqueidentifier"); + + b.Property("LastResult") + .HasColumnType("nvarchar(max)"); + + b.Property("LastRunId") + .HasColumnType("uniqueidentifier"); + + b.Property("LastRunUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Name") + .HasColumnType("nvarchar(max)"); + + b.Property("NextRunUtc") + .HasColumnType("datetimeoffset"); + + b.Property("OncePerDevice") + .HasColumnType("bit"); + + b.Property("RunOnceUtc") + .HasColumnType("datetimeoffset"); + + b.Property("ScriptId") + .HasColumnType("uniqueidentifier"); + + b.Property("TimeZoneId") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("TriggerType") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.HasKey("Id"); + + b.HasIndex("NextRunUtc"); + + b.HasIndex("TriggerType"); + + b.ToTable("ScriptTriggers", (string)null); + }); + + modelBuilder.Entity("Backend.Data.ScriptTriggerRunEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("FiredUtc") + .HasColumnType("datetimeoffset"); + + b.Property("JobId") + .HasColumnType("uniqueidentifier"); + + b.Property("TriggerId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("TriggerId", "ClientId") + .IsUnique(); + + b.ToTable("ScriptTriggerRuns", (string)null); + }); + + modelBuilder.Entity("Backend.Data.SecretsProviderEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("ConfigProtected") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Kind") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("UpdatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedUtc") + .HasColumnType("datetimeoffset"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("SecretsProviders", (string)null); + }); + + modelBuilder.Entity("Backend.Data.SessionEntity", b => + { + b.Property("Id") + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + + b.Property("AbsoluteExpiresUtc") + .HasColumnType("datetimeoffset"); + + b.Property("ClientAddress") + .HasColumnType("nvarchar(max)"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("CsrfHash") + .HasMaxLength(128) + .HasColumnType("nvarchar(128)"); + + b.Property("ExpiresUtc") + .HasColumnType("datetimeoffset"); + + b.Property("GroupsJson") + .HasColumnType("nvarchar(max)"); + + b.Property("LastSeenUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Provider") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("nvarchar(16)"); + + b.Property("RevokedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("SecretHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("nvarchar(128)"); + + b.Property("UserAgent") + .HasMaxLength(512) + .HasColumnType("nvarchar(512)"); + + b.Property("UserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("AbsoluteExpiresUtc"); + + b.HasIndex("UserId"); + + b.ToTable("Sessions", (string)null); + }); + + modelBuilder.Entity("Backend.Data.ShellSessionEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("CloseReason") + .HasColumnType("nvarchar(max)"); + + b.Property("ClosedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("RequestedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("State") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.ToTable("ShellSessions", (string)null); + }); + + modelBuilder.Entity("Backend.Data.SignInRequestEntity", b => + { + b.Property("Id") + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + + b.Property("CodeVerifier") + .HasColumnType("nvarchar(max)"); + + b.Property("CompletedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Detail") + .HasMaxLength(2000) + .HasColumnType("nvarchar(2000)"); + + b.Property("ExpiresUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Nonce") + .HasColumnType("nvarchar(max)"); + + b.Property("Provider") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("nvarchar(16)"); + + b.Property("ReturnUrl") + .HasColumnType("nvarchar(max)"); + + b.Property("SettingsEtag") + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("nvarchar(16)"); + + b.Property("StepUpSessionId") + .HasColumnType("nvarchar(max)"); + + b.Property("TestSessionId") + .HasColumnType("nvarchar(450)"); + + b.HasKey("Id"); + + b.HasIndex("ExpiresUtc"); + + b.HasIndex("TestSessionId"); + + b.ToTable("SignInRequests", (string)null); + }); + + modelBuilder.Entity("Backend.Data.SignInThrottleEntity", b => + { + b.Property("Key") + .HasMaxLength(300) + .HasColumnType("nvarchar(300)"); + + b.Property("BlockedUntilUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Failures") + .HasColumnType("int"); + + b.Property("FirstFailureUtc") + .HasColumnType("datetimeoffset"); + + b.Property("LastFailureUtc") + .HasColumnType("datetimeoffset"); + + b.HasKey("Key"); + + b.ToTable("SignInThrottles", (string)null); + }); + + modelBuilder.Entity("Backend.Data.StepUpEntity", b => + { + b.Property("SessionId") + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + + b.Property("Method") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("nvarchar(16)"); + + b.Property("VerifiedUtc") + .HasColumnType("datetimeoffset"); + + b.HasKey("SessionId"); + + b.ToTable("StepUps", (string)null); + }); + + modelBuilder.Entity("Backend.Data.UserEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("DisplayName") + .HasColumnType("nvarchar(max)"); + + b.Property("Email") + .HasColumnType("nvarchar(max)"); + + b.Property("Enabled") + .HasColumnType("bit"); + + b.Property("ExternalId") + .HasMaxLength(450) + .HasColumnType("nvarchar(450)"); + + b.Property("IsBreakGlass") + .HasColumnType("bit"); + + b.Property("Kind") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("nvarchar(16)"); + + b.Property("LastSignInUtc") + .HasColumnType("datetimeoffset"); + + b.Property("LegacyRole") + .HasMaxLength(32) + .HasColumnType("nvarchar(32)"); + + b.Property("LegacyRoleId") + .HasColumnType("uniqueidentifier"); + + b.Property("MustChangePassword") + .HasColumnType("bit"); + + b.Property("PasswordChangedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("PasswordHash") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("UserName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property("WindowsGroupsJson") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("Kind", "ExternalId"); + + b.HasIndex("Kind", "UserName") + .IsUnique(); + + b.ToTable("Users", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceInventoryEntity", b => + { + b.HasOne("Backend.Data.DeviceEntity", null) + .WithOne("Inventory") + .HasForeignKey("Backend.Data.DeviceInventoryEntity", "ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Backend.Data.LocalGroupMemberEntity", b => + { + b.HasOne("Backend.Data.LocalGroupEntity", null) + .WithMany() + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Backend.Data.UserEntity", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Backend.Data.RoleBindingEntity", b => + { + b.HasOne("Backend.Data.RoleEntity", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Backend.Data.SessionEntity", b => + { + b.HasOne("Backend.Data.UserEntity", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("Backend.Data.DeviceEntity", b => + { + b.Navigation("Inventory"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/Backend/Data/Migrations/SqlServer/20261031234307_DeviceRemovals.cs b/src/Backend/Data/Migrations/SqlServer/20261031234307_DeviceRemovals.cs new file mode 100644 index 0000000..5169e6a --- /dev/null +++ b/src/Backend/Data/Migrations/SqlServer/20261031234307_DeviceRemovals.cs @@ -0,0 +1,67 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Backend.Data.Migrations.SqlServer +{ + /// + public partial class DeviceRemovals : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "DeviceRemovals", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + ClientId = table.Column(type: "nvarchar(450)", nullable: false), + NkeyPublic = table.Column(type: "nvarchar(max)", nullable: false), + EnrollmentId = table.Column(type: "uniqueidentifier", nullable: false), + Hostname = table.Column(type: "nvarchar(max)", nullable: true), + OsCaption = table.Column(type: "nvarchar(max)", nullable: true), + AgentVersion = table.Column(type: "nvarchar(max)", nullable: true), + State = table.Column(type: "nvarchar(16)", maxLength: 16, nullable: false), + Reason = table.Column(type: "nvarchar(32)", maxLength: 32, nullable: true), + Detail = table.Column(type: "nvarchar(2000)", maxLength: 2000, nullable: true), + Attempts = table.Column(type: "int", nullable: false), + RequestedBy = table.Column(type: "nvarchar(256)", maxLength: 256, nullable: true), + Note = table.Column(type: "nvarchar(500)", maxLength: 500, nullable: true), + RequestedUtc = table.Column(type: "datetimeoffset", nullable: false), + UpdatedUtc = table.Column(type: "datetimeoffset", nullable: false), + SentUtc = table.Column(type: "datetimeoffset", nullable: true), + LastSeenUtc = table.Column(type: "datetimeoffset", nullable: true), + FinishedUtc = table.Column(type: "datetimeoffset", nullable: true), + GroupIdsJson = table.Column(type: "nvarchar(max)", nullable: true), + ConcurrencyStamp = table.Column(type: "uniqueidentifier", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_DeviceRemovals", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_DeviceRemovals_ClientId", + table: "DeviceRemovals", + column: "ClientId"); + + migrationBuilder.CreateIndex( + name: "IX_DeviceRemovals_RequestedUtc", + table: "DeviceRemovals", + column: "RequestedUtc"); + + migrationBuilder.CreateIndex( + name: "IX_DeviceRemovals_State", + table: "DeviceRemovals", + column: "State"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "DeviceRemovals"); + } + } +} diff --git a/src/Backend/Data/Migrations/SqlServer/SqlServerAppDbContextModelSnapshot.cs b/src/Backend/Data/Migrations/SqlServer/SqlServerAppDbContextModelSnapshot.cs index df884c8..d87d97c 100644 --- a/src/Backend/Data/Migrations/SqlServer/SqlServerAppDbContextModelSnapshot.cs +++ b/src/Backend/Data/Migrations/SqlServer/SqlServerAppDbContextModelSnapshot.cs @@ -417,6 +417,89 @@ namespace Backend.Data.Migrations.SqlServer b.ToTable("DeviceInventory", (string)null); }); + modelBuilder.Entity("Backend.Data.DeviceRemovalEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("AgentVersion") + .HasColumnType("nvarchar(max)"); + + b.Property("Attempts") + .HasColumnType("int"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("uniqueidentifier"); + + b.Property("Detail") + .HasMaxLength(2000) + .HasColumnType("nvarchar(2000)"); + + b.Property("EnrollmentId") + .HasColumnType("uniqueidentifier"); + + b.Property("FinishedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("GroupIdsJson") + .HasColumnType("nvarchar(max)"); + + b.Property("Hostname") + .HasColumnType("nvarchar(max)"); + + b.Property("LastSeenUtc") + .HasColumnType("datetimeoffset"); + + b.Property("NkeyPublic") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Note") + .HasMaxLength(500) + .HasColumnType("nvarchar(500)"); + + b.Property("OsCaption") + .HasColumnType("nvarchar(max)"); + + b.Property("Reason") + .HasMaxLength(32) + .HasColumnType("nvarchar(32)"); + + b.Property("RequestedBy") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property("RequestedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("SentUtc") + .HasColumnType("datetimeoffset"); + + b.Property("State") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("nvarchar(16)"); + + b.Property("UpdatedUtc") + .HasColumnType("datetimeoffset"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("RequestedUtc"); + + b.HasIndex("State"); + + b.ToTable("DeviceRemovals", (string)null); + }); + modelBuilder.Entity("Backend.Data.FunctionModuleEntity", b => { b.Property("Id") diff --git a/src/Backend/Data/Migrations/Sqlite/20261031234253_DeviceRemovals.Designer.cs b/src/Backend/Data/Migrations/Sqlite/20261031234253_DeviceRemovals.Designer.cs new file mode 100644 index 0000000..f23faa2 --- /dev/null +++ b/src/Backend/Data/Migrations/Sqlite/20261031234253_DeviceRemovals.Designer.cs @@ -0,0 +1,1288 @@ +// +using System; +using Backend.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Backend.Data.Migrations.Sqlite +{ + [DbContext(typeof(SqliteAppDbContext))] + [Migration("20261031234253_DeviceRemovals")] + partial class DeviceRemovals + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.10"); + + modelBuilder.Entity("Backend.Data.AgentPackageEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("FileName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ImportedUtc") + .HasColumnType("INTEGER"); + + b.Property("PackageType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("S3Key") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("Sha256") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("StoragePath") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("Version") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("Sha256"); + + b.HasIndex("Version", "PackageType") + .IsUnique(); + + b.ToTable("AgentPackages", (string)null); + }); + + modelBuilder.Entity("Backend.Data.AgentUpgradeEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("Attempts") + .HasColumnType("INTEGER"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("Detail") + .HasMaxLength(2000) + .HasColumnType("TEXT"); + + b.Property("FinishedUtc") + .HasColumnType("INTEGER"); + + b.Property("FromVersion") + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("LastEventUtc") + .HasColumnType("INTEGER"); + + b.Property("LastLogJson") + .HasColumnType("TEXT"); + + b.Property("PackageId") + .HasColumnType("TEXT"); + + b.Property("PackageType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("Reason") + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("RequestedBy") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("SentUtc") + .HasColumnType("INTEGER"); + + b.Property("Sha256") + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("Source") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("TEXT"); + + b.Property("State") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("TEXT"); + + b.Property("TargetVersion") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("CreatedUtc"); + + b.HasIndex("State"); + + b.HasIndex("ClientId", "State"); + + b.ToTable("AgentUpgrades", (string)null); + }); + + modelBuilder.Entity("Backend.Data.AppSettingEntity", b => + { + b.Property("Key") + .HasColumnType("TEXT"); + + b.Property("Etag") + .HasColumnType("TEXT"); + + b.Property("IsSecret") + .HasColumnType("INTEGER"); + + b.Property("UpdatedBy") + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("INTEGER"); + + b.Property("Value") + .HasColumnType("TEXT"); + + b.HasKey("Key"); + + b.ToTable("AppSettings", (string)null); + }); + + modelBuilder.Entity("Backend.Data.AuditEventEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Action") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Actor") + .HasColumnType("TEXT"); + + b.Property("Category") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ClientId") + .HasColumnType("TEXT"); + + b.Property("Detail") + .HasColumnType("TEXT"); + + b.Property("RemoteIp") + .HasColumnType("TEXT"); + + b.Property("TimestampUtc") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("TimestampUtc"); + + b.ToTable("AuditEvents", (string)null); + }); + + modelBuilder.Entity("Backend.Data.BootstrapTokenEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("CreatedBy") + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("ExpiresUtc") + .HasColumnType("INTEGER"); + + b.Property("MaxUses") + .HasColumnType("INTEGER"); + + b.Property("Note") + .HasColumnType("TEXT"); + + b.Property("Revoked") + .HasColumnType("INTEGER"); + + b.Property("TokenHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Uses") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("TokenHash") + .IsUnique(); + + b.ToTable("BootstrapTokens", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceEntity", b => + { + b.Property("ClientId") + .HasColumnType("TEXT"); + + b.Property("AgentInstall") + .HasMaxLength(16) + .HasColumnType("TEXT"); + + b.Property("AgentRuntime") + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("AgentVersion") + .HasColumnType("TEXT"); + + b.Property("BootstrapTokenId") + .HasColumnType("TEXT"); + + b.Property("EnrolledUtc") + .HasColumnType("INTEGER"); + + b.Property("EnrollmentId") + .HasColumnType("TEXT"); + + b.Property("Hostname") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("IpAddressesJson") + .HasColumnType("TEXT"); + + b.Property("IsOrphaned") + .HasColumnType("INTEGER"); + + b.Property("LastSeenUtc") + .HasColumnType("INTEGER"); + + b.Property("Manufacturer") + .HasColumnType("TEXT"); + + b.Property("Model") + .HasColumnType("TEXT"); + + b.Property("NkeyPublic") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("OrphanedUtc") + .HasColumnType("INTEGER"); + + b.Property("OsCaption") + .HasColumnType("TEXT"); + + b.Property("OsVersion") + .HasColumnType("TEXT"); + + b.Property("PublicIp") + .HasColumnType("TEXT"); + + b.Property("ReadyUtc") + .HasColumnType("INTEGER"); + + b.Property("TagsJson") + .HasColumnType("TEXT"); + + b.HasKey("ClientId"); + + b.HasIndex("Hostname"); + + b.ToTable("Devices", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceGroupEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("Description") + .HasColumnType("TEXT"); + + b.Property("Kind") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ParentId") + .HasColumnType("TEXT"); + + b.Property("RulesJson") + .HasColumnType("TEXT"); + + b.Property("UpdatedBy") + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("DeviceGroups", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceGroupMemberEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AddedUtc") + .HasColumnType("INTEGER"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("GroupId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("GroupId", "ClientId") + .IsUnique(); + + b.ToTable("DeviceGroupMembers", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceInventoryEntity", b => + { + b.Property("ClientId") + .HasColumnType("TEXT"); + + b.Property("ReportJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("INTEGER"); + + b.HasKey("ClientId"); + + b.ToTable("DeviceInventory", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceRemovalEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AgentVersion") + .HasColumnType("TEXT"); + + b.Property("Attempts") + .HasColumnType("INTEGER"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("TEXT"); + + b.Property("Detail") + .HasMaxLength(2000) + .HasColumnType("TEXT"); + + b.Property("EnrollmentId") + .HasColumnType("TEXT"); + + b.Property("FinishedUtc") + .HasColumnType("INTEGER"); + + b.Property("GroupIdsJson") + .HasColumnType("TEXT"); + + b.Property("Hostname") + .HasColumnType("TEXT"); + + b.Property("LastSeenUtc") + .HasColumnType("INTEGER"); + + b.Property("NkeyPublic") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Note") + .HasMaxLength(500) + .HasColumnType("TEXT"); + + b.Property("OsCaption") + .HasColumnType("TEXT"); + + b.Property("Reason") + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("RequestedBy") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("RequestedUtc") + .HasColumnType("INTEGER"); + + b.Property("SentUtc") + .HasColumnType("INTEGER"); + + b.Property("State") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("RequestedUtc"); + + b.HasIndex("State"); + + b.ToTable("DeviceRemovals", (string)null); + }); + + modelBuilder.Entity("Backend.Data.FunctionModuleEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("Content") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("Description") + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UpdatedBy") + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("Name"); + + b.ToTable("FunctionModules", (string)null); + }); + + modelBuilder.Entity("Backend.Data.JobEntity", b => + { + b.Property("JobId") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("CompletedUtc") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("ExitCode") + .HasColumnType("INTEGER"); + + b.Property("OutputSummary") + .HasColumnType("TEXT"); + + b.Property("RequestedBy") + .HasColumnType("TEXT"); + + b.Property("RunId") + .HasColumnType("TEXT"); + + b.Property("ScriptId") + .HasColumnType("TEXT"); + + b.Property("ScriptName") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("INTEGER"); + + b.Property("State") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("JobId"); + + b.HasIndex("ClientId"); + + b.HasIndex("CreatedUtc"); + + b.HasIndex("RunId"); + + b.HasIndex("ScriptId", "RunId"); + + b.ToTable("Jobs", (string)null); + }); + + modelBuilder.Entity("Backend.Data.JobOutputEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("JobId") + .HasColumnType("TEXT"); + + b.Property("Sequence") + .HasColumnType("INTEGER"); + + b.Property("Stream") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Text") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("TimestampUtc") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("JobId", "Sequence") + .IsUnique(); + + b.ToTable("JobOutput", (string)null); + }); + + modelBuilder.Entity("Backend.Data.LocalGroupEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("Description") + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("LocalGroups", (string)null); + }); + + modelBuilder.Entity("Backend.Data.LocalGroupMemberEntity", b => + { + b.Property("GroupId") + .HasColumnType("TEXT"); + + b.Property("UserId") + .HasColumnType("TEXT"); + + b.HasKey("GroupId", "UserId"); + + b.HasIndex("UserId"); + + b.ToTable("LocalGroupMembers", (string)null); + }); + + modelBuilder.Entity("Backend.Data.RoleBindingEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("CreatedBy") + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("PrincipalId") + .IsRequired() + .HasMaxLength(450) + .HasColumnType("TEXT"); + + b.Property("PrincipalKind") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("PrincipalName") + .HasColumnType("TEXT"); + + b.Property("RoleId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.HasIndex("PrincipalKind", "PrincipalId", "RoleId") + .IsUnique(); + + b.ToTable("RoleBindings", (string)null); + }); + + modelBuilder.Entity("Backend.Data.RoleEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("Description") + .HasColumnType("TEXT"); + + b.Property("DeviceScopeGroupIdsJson") + .HasColumnType("TEXT"); + + b.Property("IsSystem") + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("OidcClaimValuesJson") + .HasColumnType("TEXT"); + + b.Property("PermissionsJson") + .HasColumnType("TEXT"); + + b.Property("UpdatedBy") + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("Roles", (string)null); + }); + + modelBuilder.Entity("Backend.Data.RunEntity", b => + { + b.Property("RunId") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("OnlineOnly") + .HasColumnType("INTEGER"); + + b.Property("RequestedBy") + .HasColumnType("TEXT"); + + b.Property("ScriptId") + .HasColumnType("TEXT"); + + b.Property("ScriptName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("TEXT"); + + b.Property("SkippedJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Source") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("TargetsLabel") + .HasMaxLength(400) + .HasColumnType("TEXT"); + + b.Property("TriggerId") + .HasColumnType("TEXT"); + + b.HasKey("RunId"); + + b.HasIndex("CreatedUtc"); + + b.HasIndex("ScriptId"); + + b.ToTable("Runs", (string)null); + }); + + modelBuilder.Entity("Backend.Data.ScriptEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("Content") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("CreatedBy") + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("Description") + .HasColumnType("TEXT"); + + b.Property("FileBindingsJson") + .HasColumnType("TEXT"); + + b.Property("FunctionModuleIdsJson") + .HasColumnType("TEXT"); + + b.Property("Language") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ParametersJson") + .HasColumnType("TEXT"); + + b.Property("SecretBindingsJson") + .HasColumnType("TEXT"); + + b.Property("SecretsVariableName") + .HasColumnType("TEXT"); + + b.Property("UpdatedBy") + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("Name"); + + b.ToTable("Scripts", (string)null); + }); + + modelBuilder.Entity("Backend.Data.ScriptTriggerEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("ArgumentsJson") + .HasColumnType("TEXT"); + + b.Property("ClientId") + .HasColumnType("TEXT"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("TEXT"); + + b.Property("CreatedBy") + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("CronExpression") + .HasColumnType("TEXT"); + + b.Property("Enabled") + .HasColumnType("INTEGER"); + + b.Property("GroupId") + .HasColumnType("TEXT"); + + b.Property("LastResult") + .HasColumnType("TEXT"); + + b.Property("LastRunId") + .HasColumnType("TEXT"); + + b.Property("LastRunUtc") + .HasColumnType("INTEGER"); + + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("NextRunUtc") + .HasColumnType("INTEGER"); + + b.Property("OncePerDevice") + .HasColumnType("INTEGER"); + + b.Property("RunOnceUtc") + .HasColumnType("INTEGER"); + + b.Property("ScriptId") + .HasColumnType("TEXT"); + + b.Property("TimeZoneId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("TriggerType") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("NextRunUtc"); + + b.HasIndex("TriggerType"); + + b.ToTable("ScriptTriggers", (string)null); + }); + + modelBuilder.Entity("Backend.Data.ScriptTriggerRunEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("FiredUtc") + .HasColumnType("INTEGER"); + + b.Property("JobId") + .HasColumnType("TEXT"); + + b.Property("TriggerId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("TriggerId", "ClientId") + .IsUnique(); + + b.ToTable("ScriptTriggerRuns", (string)null); + }); + + modelBuilder.Entity("Backend.Data.SecretsProviderEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("ConfigProtected") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("Kind") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UpdatedBy") + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("SecretsProviders", (string)null); + }); + + modelBuilder.Entity("Backend.Data.SessionEntity", b => + { + b.Property("Id") + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("AbsoluteExpiresUtc") + .HasColumnType("INTEGER"); + + b.Property("ClientAddress") + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("CsrfHash") + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ExpiresUtc") + .HasColumnType("INTEGER"); + + b.Property("GroupsJson") + .HasColumnType("TEXT"); + + b.Property("LastSeenUtc") + .HasColumnType("INTEGER"); + + b.Property("Provider") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("TEXT"); + + b.Property("RevokedUtc") + .HasColumnType("INTEGER"); + + b.Property("SecretHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("UserAgent") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("UserId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AbsoluteExpiresUtc"); + + b.HasIndex("UserId"); + + b.ToTable("Sessions", (string)null); + }); + + modelBuilder.Entity("Backend.Data.ShellSessionEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("CloseReason") + .HasColumnType("TEXT"); + + b.Property("ClosedUtc") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("RequestedBy") + .HasColumnType("TEXT"); + + b.Property("State") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.ToTable("ShellSessions", (string)null); + }); + + modelBuilder.Entity("Backend.Data.SignInRequestEntity", b => + { + b.Property("Id") + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("CodeVerifier") + .HasColumnType("TEXT"); + + b.Property("CompletedUtc") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("Detail") + .HasMaxLength(2000) + .HasColumnType("TEXT"); + + b.Property("ExpiresUtc") + .HasColumnType("INTEGER"); + + b.Property("Nonce") + .HasColumnType("TEXT"); + + b.Property("Provider") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("TEXT"); + + b.Property("ReturnUrl") + .HasColumnType("TEXT"); + + b.Property("SettingsEtag") + .HasColumnType("TEXT"); + + b.Property("Status") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("TEXT"); + + b.Property("StepUpSessionId") + .HasColumnType("TEXT"); + + b.Property("TestSessionId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("ExpiresUtc"); + + b.HasIndex("TestSessionId"); + + b.ToTable("SignInRequests", (string)null); + }); + + modelBuilder.Entity("Backend.Data.SignInThrottleEntity", b => + { + b.Property("Key") + .HasMaxLength(300) + .HasColumnType("TEXT"); + + b.Property("BlockedUntilUtc") + .HasColumnType("INTEGER"); + + b.Property("Failures") + .HasColumnType("INTEGER"); + + b.Property("FirstFailureUtc") + .HasColumnType("INTEGER"); + + b.Property("LastFailureUtc") + .HasColumnType("INTEGER"); + + b.HasKey("Key"); + + b.ToTable("SignInThrottles", (string)null); + }); + + modelBuilder.Entity("Backend.Data.StepUpEntity", b => + { + b.Property("SessionId") + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("Method") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("TEXT"); + + b.Property("VerifiedUtc") + .HasColumnType("INTEGER"); + + b.HasKey("SessionId"); + + b.ToTable("StepUps", (string)null); + }); + + modelBuilder.Entity("Backend.Data.UserEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("Description") + .HasColumnType("TEXT"); + + b.Property("DisplayName") + .HasColumnType("TEXT"); + + b.Property("Email") + .HasColumnType("TEXT"); + + b.Property("Enabled") + .HasColumnType("INTEGER"); + + b.Property("ExternalId") + .HasMaxLength(450) + .HasColumnType("TEXT"); + + b.Property("IsBreakGlass") + .HasColumnType("INTEGER"); + + b.Property("Kind") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("TEXT"); + + b.Property("LastSignInUtc") + .HasColumnType("INTEGER"); + + b.Property("LegacyRole") + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("LegacyRoleId") + .HasColumnType("TEXT"); + + b.Property("MustChangePassword") + .HasColumnType("INTEGER"); + + b.Property("PasswordChangedUtc") + .HasColumnType("INTEGER"); + + b.Property("PasswordHash") + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("INTEGER"); + + b.Property("UserName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("WindowsGroupsJson") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("Kind", "ExternalId"); + + b.HasIndex("Kind", "UserName") + .IsUnique(); + + b.ToTable("Users", (string)null); + }); + + modelBuilder.Entity("Backend.Data.DeviceInventoryEntity", b => + { + b.HasOne("Backend.Data.DeviceEntity", null) + .WithOne("Inventory") + .HasForeignKey("Backend.Data.DeviceInventoryEntity", "ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Backend.Data.LocalGroupMemberEntity", b => + { + b.HasOne("Backend.Data.LocalGroupEntity", null) + .WithMany() + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Backend.Data.UserEntity", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Backend.Data.RoleBindingEntity", b => + { + b.HasOne("Backend.Data.RoleEntity", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Backend.Data.SessionEntity", b => + { + b.HasOne("Backend.Data.UserEntity", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("Backend.Data.DeviceEntity", b => + { + b.Navigation("Inventory"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/Backend/Data/Migrations/Sqlite/20261031234253_DeviceRemovals.cs b/src/Backend/Data/Migrations/Sqlite/20261031234253_DeviceRemovals.cs new file mode 100644 index 0000000..784f0fd --- /dev/null +++ b/src/Backend/Data/Migrations/Sqlite/20261031234253_DeviceRemovals.cs @@ -0,0 +1,67 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Backend.Data.Migrations.Sqlite +{ + /// + public partial class DeviceRemovals : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "DeviceRemovals", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + ClientId = table.Column(type: "TEXT", nullable: false), + NkeyPublic = table.Column(type: "TEXT", nullable: false), + EnrollmentId = table.Column(type: "TEXT", nullable: false), + Hostname = table.Column(type: "TEXT", nullable: true), + OsCaption = table.Column(type: "TEXT", nullable: true), + AgentVersion = table.Column(type: "TEXT", nullable: true), + State = table.Column(type: "TEXT", maxLength: 16, nullable: false), + Reason = table.Column(type: "TEXT", maxLength: 32, nullable: true), + Detail = table.Column(type: "TEXT", maxLength: 2000, nullable: true), + Attempts = table.Column(type: "INTEGER", nullable: false), + RequestedBy = table.Column(type: "TEXT", maxLength: 256, nullable: true), + Note = table.Column(type: "TEXT", maxLength: 500, nullable: true), + RequestedUtc = table.Column(type: "INTEGER", nullable: false), + UpdatedUtc = table.Column(type: "INTEGER", nullable: false), + SentUtc = table.Column(type: "INTEGER", nullable: true), + LastSeenUtc = table.Column(type: "INTEGER", nullable: true), + FinishedUtc = table.Column(type: "INTEGER", nullable: true), + GroupIdsJson = table.Column(type: "TEXT", nullable: true), + ConcurrencyStamp = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_DeviceRemovals", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_DeviceRemovals_ClientId", + table: "DeviceRemovals", + column: "ClientId"); + + migrationBuilder.CreateIndex( + name: "IX_DeviceRemovals_RequestedUtc", + table: "DeviceRemovals", + column: "RequestedUtc"); + + migrationBuilder.CreateIndex( + name: "IX_DeviceRemovals_State", + table: "DeviceRemovals", + column: "State"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "DeviceRemovals"); + } + } +} diff --git a/src/Backend/Data/Migrations/Sqlite/SqliteAppDbContextModelSnapshot.cs b/src/Backend/Data/Migrations/Sqlite/SqliteAppDbContextModelSnapshot.cs index bf26e1b..a5b6450 100644 --- a/src/Backend/Data/Migrations/Sqlite/SqliteAppDbContextModelSnapshot.cs +++ b/src/Backend/Data/Migrations/Sqlite/SqliteAppDbContextModelSnapshot.cs @@ -408,6 +408,89 @@ namespace Backend.Data.Migrations.Sqlite b.ToTable("DeviceInventory", (string)null); }); + modelBuilder.Entity("Backend.Data.DeviceRemovalEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AgentVersion") + .HasColumnType("TEXT"); + + b.Property("Attempts") + .HasColumnType("INTEGER"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("TEXT"); + + b.Property("Detail") + .HasMaxLength(2000) + .HasColumnType("TEXT"); + + b.Property("EnrollmentId") + .HasColumnType("TEXT"); + + b.Property("FinishedUtc") + .HasColumnType("INTEGER"); + + b.Property("GroupIdsJson") + .HasColumnType("TEXT"); + + b.Property("Hostname") + .HasColumnType("TEXT"); + + b.Property("LastSeenUtc") + .HasColumnType("INTEGER"); + + b.Property("NkeyPublic") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Note") + .HasMaxLength(500) + .HasColumnType("TEXT"); + + b.Property("OsCaption") + .HasColumnType("TEXT"); + + b.Property("Reason") + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("RequestedBy") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("RequestedUtc") + .HasColumnType("INTEGER"); + + b.Property("SentUtc") + .HasColumnType("INTEGER"); + + b.Property("State") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("RequestedUtc"); + + b.HasIndex("State"); + + b.ToTable("DeviceRemovals", (string)null); + }); + modelBuilder.Entity("Backend.Data.FunctionModuleEntity", b => { b.Property("Id") diff --git a/src/Backend/Jobs/CommandDispatcher.cs b/src/Backend/Jobs/CommandDispatcher.cs index 02aed0e..2ab66ba 100644 --- a/src/Backend/Jobs/CommandDispatcher.cs +++ b/src/Backend/Jobs/CommandDispatcher.cs @@ -31,6 +31,18 @@ public sealed class CommandDispatcher(NatsHub hub, ILogger lo return await PublishAsync(NatsSubjects.AgentCommandUpgrade(clientId), envelope, cancellationToken); } + public async Task DispatchAgentRemoveAsync( + string clientId, AgentRemoveCommandPayload command, CancellationToken cancellationToken) + { + var envelope = MessageEnvelope.Create( + MessageTypes.CommandAgentRemove, + BackendSource, + command, + new EndpointRef(EndpointKinds.Agent, clientId)); + + return await PublishAsync(NatsSubjects.AgentCommandRemove(clientId), envelope, cancellationToken); + } + public async Task DispatchScriptAsync( string clientId, ScriptCommandPayload command, CancellationToken cancellationToken) { diff --git a/src/Backend/Jobs/JobEndpoints.cs b/src/Backend/Jobs/JobEndpoints.cs index 49ae055..1e89065 100644 --- a/src/Backend/Jobs/JobEndpoints.cs +++ b/src/Backend/Jobs/JobEndpoints.cs @@ -407,13 +407,22 @@ public static class JobEndpoints return Results.Ok(records); } - private static Task> HostnamesAsync( + private static async Task> HostnamesAsync( AppDbContext db, IReadOnlyCollection jobs, CancellationToken cancellationToken) { var clientIds = jobs.Select(j => j.ClientId).Distinct().ToList(); - return db.Devices.AsNoTracking() + var hostnames = await db.Devices.AsNoTracking() .Where(d => clientIds.Contains(d.ClientId)) .ToDictionaryAsync(d => d.ClientId, d => d.Hostname, cancellationToken); + + // Old runs of a removed device keep its name. + foreach (var (clientId, hostname) in await Removals.DeviceRemovalService.RemovedHostnamesAsync( + db, clientIds.Where(id => !hostnames.ContainsKey(id)), cancellationToken)) + { + hostnames[clientId] = hostname; + } + + return hostnames; } private static async Task CreateAsync( diff --git a/src/Backend/Messaging/MessagingWorkers.cs b/src/Backend/Messaging/MessagingWorkers.cs index b9fbbba..bad17e3 100644 --- a/src/Backend/Messaging/MessagingWorkers.cs +++ b/src/Backend/Messaging/MessagingWorkers.cs @@ -92,7 +92,9 @@ public sealed class MessagingWorkers( var device = await db.Devices.SingleOrDefaultAsync(d => d.ClientId == payload.ClientId, cancellationToken); if (device is null) { - continue; // Presence from a device we have no enrollment for. + // No enrollment: a removal may be waiting for this device; otherwise dropped. + await Removals.DeviceRemovalEvents.NoteSightingAsync(scope.ServiceProvider, msg.Subject, payload.ClientId, payload.AgentVersion, cancellationToken); + continue; } if (device.IsOrphaned) @@ -161,6 +163,7 @@ public sealed class MessagingWorkers( var device = await db.Devices.SingleOrDefaultAsync(d => d.ClientId == clientId, cancellationToken); if (device is null) { + await Removals.DeviceRemovalEvents.NoteSightingAsync(scope.ServiceProvider, msg.Subject, clientId, null, cancellationToken); continue; } @@ -208,8 +211,8 @@ public sealed class MessagingWorkers( /// /// Agent lifecycle events. A "ready" notice means the agent is connected /// and consuming job commands, which fires OnAgentReady script triggers. - /// Agent upgrade progress and outcomes share the subject, told apart by - /// the envelope's message type. + /// Agent upgrade and removal progress share the subject, told apart by the + /// envelope's message type. /// private async Task ConsumeAgentEventsAsync(CancellationToken cancellationToken) { @@ -230,6 +233,20 @@ public sealed class MessagingWorkers( continue; } + if (envelope?.MessageType == MessageTypes.AgentRemoval) + { + try + { + await Removals.DeviceRemovalEvents.HandleAsync(scopeFactory, msg.Subject, envelope, cancellationToken); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + logger.LogError(ex, "Agent removal event from {Subject} failed", msg.Subject); + } + + continue; + } + AgentEventPayload? payload = null; try { @@ -253,6 +270,7 @@ public sealed class MessagingWorkers( var device = await db.Devices.SingleOrDefaultAsync(d => d.ClientId == payload.ClientId, cancellationToken); if (device is null) { + await Removals.DeviceRemovalEvents.NoteSightingAsync(scope.ServiceProvider, msg.Subject, payload.ClientId, null, cancellationToken); continue; } diff --git a/src/Backend/Nats/JetStreamProvisioner.cs b/src/Backend/Nats/JetStreamProvisioner.cs index ccf3d2e..f917d3b 100644 --- a/src/Backend/Nats/JetStreamProvisioner.cs +++ b/src/Backend/Nats/JetStreamProvisioner.cs @@ -74,4 +74,34 @@ public sealed class JetStreamProvisioner(NatsHub hub, ILogger + /// Deletes one agent's durable command consumer and purges its command + /// subjects. The stream keeps acked messages until they age out, and a new + /// consumer delivers everything still there, so without this a device enrolled + /// again would replay its old commands. + /// + public async Task RemoveAgentCommandsAsync(string clientId, CancellationToken cancellationToken) + { + if (!hub.IsConfigured) + { + return; + } + + var js = new NatsJSContext(hub.Require()); + try + { + await js.DeleteConsumerAsync( + JetStreamDefaults.CommandStreamName, JetStreamDefaults.AgentCommandConsumerName(clientId), cancellationToken); + } + catch (NatsJSApiException ex) when (ex.Error.Code == 404) + { + // Already gone. + } + + var stream = await js.GetStreamAsync(JetStreamDefaults.CommandStreamName, cancellationToken: cancellationToken); + await stream.PurgeAsync(new StreamPurgeRequest { Filter = NatsSubjects.AgentCommandWildcard(clientId) }, cancellationToken); + + logger.LogInformation("Durable command consumer and commands cleared for {ClientId}", clientId); + } } diff --git a/src/Backend/Program.cs b/src/Backend/Program.cs index 38aecde..90b2b77 100644 --- a/src/Backend/Program.cs +++ b/src/Backend/Program.cs @@ -164,6 +164,9 @@ builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddSingleton(sp => sp.GetRequiredService()); +// Device removals: the tombstones and the sender their scheduler (with the schedules) uses. +builder.Services.AddScoped(); +builder.Services.AddSingleton(); if (builder.Configuration["AGENT_CATALOG_SYNC"] != "0") { builder.Services.AddHostedService(); @@ -189,6 +192,7 @@ if (builder.Configuration["SCHEDULE_WORKER"] != "0") { builder.Services.AddHostedService(); builder.Services.AddHostedService(); + builder.Services.AddHostedService(); } builder.Services.AddHostedService(sp => sp.GetRequiredService()); @@ -403,6 +407,7 @@ GroupEndpoints.Map(app); TriggerEndpoints.Map(app); SettingsEndpoints.Map(app); Backend.Upgrades.AgentUpgradeEndpoints.Map(app); +Backend.Removals.DeviceRemovalEndpoints.Map(app); Backend.Secrets.SecretsEndpoints.Map(app); if (staticUiPath is not null) diff --git a/src/Backend/Removals/DeviceRemovalEndpoints.cs b/src/Backend/Removals/DeviceRemovalEndpoints.cs new file mode 100644 index 0000000..4cdba29 --- /dev/null +++ b/src/Backend/Removals/DeviceRemovalEndpoints.cs @@ -0,0 +1,189 @@ +using Backend.Api; +using Backend.Auth; +using Backend.Data; +using Backend.Groups; +using Backend.Messaging; +using Backend.Security; +using Microsoft.EntityFrameworkCore; +using Shared.Contracts.Api; +using Shared.Contracts.Messages; +using Shared.Contracts.Models; +using Shared.Infrastructure.Nats; + +namespace Backend.Removals; + +/// Removing devices: the request, the list behind the Devices page, and Stop Trying. +public static class DeviceRemovalEndpoints +{ + public const int DefaultListLimit = 200; + + public static void Map(WebApplication app) + { + app.MapGet(ApiRoutes.DeviceRemovals, ListAsync).RequirePermission(Permissions.DevicesView); + app.MapPost(ApiRoutes.DeviceRemovals, RemoveAsync).RequirePermission(Permissions.DevicesRemove); + app.MapPost(ApiRoutes.DeviceRemovalStop, StopAsync).RequirePermission(Permissions.DevicesRemove); + } + + private static async Task ListAsync( + string? state, string? clientId, int? limit, AppDbContext db, DeviceScopeService scope, HttpContext context, + CancellationToken cancellationToken) + { + var query = db.DeviceRemovals.AsNoTracking(); + if (!string.IsNullOrWhiteSpace(state)) + { + query = query.Where(r => r.State == state); + } + + if (!string.IsNullOrWhiteSpace(clientId)) + { + query = query.Where(r => r.ClientId == clientId); + } + + var take = Math.Clamp(limit ?? DefaultListLimit, 1, 5000); + query = query.OrderByDescending(r => r.RequestedUtc); + + // A scoped role sees removals by the groups kept on each row: they are + // JSON, so the page is cut after this filter for such a role. + var scopeGroupIds = await scope.GetScopeGroupIdsAsync(context.User, cancellationToken); + var rows = scopeGroupIds is null + ? await query.Take(take).ToListAsync(cancellationToken) + : (await query.ToListAsync(cancellationToken)) + .Where(r => DeviceRemovalService.IsVisibleTo(r, scopeGroupIds)) + .Take(take) + .ToList(); + + var now = DateTimeOffset.UtcNow; + return Results.Ok(rows.Select(r => DeviceRemovalService.ToDto(r, now)).ToList()); + } + + private static async Task RemoveAsync( + RemoveDevicesRequest request, AppDbContext db, DeviceRemovalService removals, DeviceGroupService groups, + DeviceScopeService scope, AuditService audit, BrowserEventBroadcaster broadcaster, HttpContext context, + CancellationToken cancellationToken) + { + var named = (request.ClientIds ?? []).Distinct(StringComparer.Ordinal).ToList(); + var groupIds = (request.GroupIds ?? []).Distinct().ToList(); + if (named.Count == 0 && groupIds.Count == 0) + { + return Results.BadRequest(new { error = "Choose at least one device or group." }); + } + + if (named.Any(id => !SubjectIdentifier.IsValid(id))) + { + return Results.BadRequest(new { error = "Invalid client id." }); + } + + var selected = await db.DeviceGroups.AsNoTracking() + .Where(g => groupIds.Contains(g.Id)) + .ToListAsync(cancellationToken); + if (selected.Count < groupIds.Count) + { + return Results.NotFound(new { error = groupIds.Count == 1 ? "Group not found." : "Some groups no longer exist." }); + } + + var fromGroups = new HashSet(StringComparer.Ordinal); + foreach (var group in selected) + { + fromGroups.UnionWith(await groups.GetMembersAsync(group, cancellationToken)); + } + + // A scoped role only reaches the devices inside its own scope. A device it + // named outside that reads the same whether it exists or not; one a group + // brought in is not its to know about, as with agent upgrades. + var allowed = await scope.GetAllowedClientIdsAsync(context.User, cancellationToken); + var skipped = new List(); + var targets = new HashSet(StringComparer.Ordinal); + foreach (var clientId in named) + { + if (allowed is null || allowed.Contains(clientId)) + { + targets.Add(clientId); + } + else + { + skipped.Add(new DeviceRemovalSkipDto(clientId, null, DeviceRemovalService.SkipOutOfScope)); + } + } + + targets.UnionWith(allowed is null ? fromGroups : fromGroups.Where(allowed.Contains)); + + var note = string.IsNullOrWhiteSpace(request.Reason) ? null : request.Reason.Trim(); + if (note is { Length: > DeviceRemovalService.NoteMaxLength }) + { + note = note[..DeviceRemovalService.NoteMaxLength]; + } + + var actor = context.User.Identity?.Name; + var (removed, unknown) = await removals.RemoveAsync(targets.Order(StringComparer.Ordinal).ToList(), note, actor, cancellationToken); + skipped.AddRange(unknown); + + var left = string.Join(' ', skipped.GroupBy(s => s.Reason).Select(g => $"{g.Key}={g.Count()}")); + await audit.LogAsync(AuditCategories.Admin, "device-removal-requested", actor, + clientId: removed.Count == 1 ? removed[0].ClientId : null, + detail: $"removed={removed.Count}{(left.Length > 0 ? " skipped: " + left : string.Empty)}", + cancellationToken: cancellationToken); + + if (removed.Count > 0) + { + await broadcaster.BroadcastActivityAsync(new ActivityEventPayload( + DeviceRemovalService.ActivityKind, + removed.Count == 1 ? $"{removed[0].Hostname} removal requested" : $"Removal requested for {removed.Count} devices", + removed.Count == 1 ? removed[0].ClientId : null, null, DateTimeOffset.UtcNow), cancellationToken); + } + + var now = DateTimeOffset.UtcNow; + return Results.Ok(new DeviceRemovalBatchDto( + removed.OrderBy(r => r.Hostname, StringComparer.OrdinalIgnoreCase).Select(r => DeviceRemovalService.ToDto(r, now)).ToList(), + skipped)); + } + + /// + /// Stops chasing a device: it stays blocked and its agent stays installed. + /// A remove command still waiting for it is purged so it cannot arrive later; + /// that is best effort, since a command left behind expires with the stream. + /// + private static async Task StopAsync( + Guid id, AppDbContext db, DeviceRemovalService removals, IDeviceRemovalSender sender, DeviceScopeService scope, + AuditService audit, HttpContext context, ILogger logger, CancellationToken cancellationToken) + { + var row = await db.DeviceRemovals.SingleOrDefaultAsync(r => r.Id == id, cancellationToken); + if (row is null || !DeviceRemovalService.IsVisibleTo(row, await scope.GetScopeGroupIdsAsync(context.User, cancellationToken))) + { + return Results.NotFound(); + } + + if (!DeviceRemovalStates.Open.Contains(row.State)) + { + return Results.Conflict(new { error = $"This removal has already finished; it is {row.State}." }); + } + + var now = DateTimeOffset.UtcNow; + row.State = DeviceRemovalStates.Stopped; + row.FinishedUtc = now; + row.UpdatedUtc = now; + var actor = context.User.Identity?.Name; + DeviceHistory.RecordRemovalStopped(db, row.ClientId, actor); + try + { + await db.SaveChangesAsync(cancellationToken); + } + catch (DbUpdateConcurrencyException) + { + return Results.Conflict(new { error = "The removal changed meanwhile. Try again." }); + } + + try + { + await sender.ClearCommandsAsync(row.ClientId, cancellationToken); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + logger.LogWarning(ex, "Could not clear the commands of device {ClientId} after its removal stopped", row.ClientId); + } + + await audit.LogAsync(AuditCategories.Admin, "device-removal-stopped", actor, clientId: row.ClientId, + detail: $"removal={row.Id}", cancellationToken: cancellationToken); + await removals.AnnounceAsync(row, cancellationToken); + return Results.Ok(DeviceRemovalService.ToDto(row, now)); + } +} diff --git a/src/Backend/Removals/DeviceRemovalScheduler.cs b/src/Backend/Removals/DeviceRemovalScheduler.cs new file mode 100644 index 0000000..68c8fcd --- /dev/null +++ b/src/Backend/Removals/DeviceRemovalScheduler.cs @@ -0,0 +1,288 @@ +using Backend.Api; +using Backend.Data; +using Backend.Jobs; +using Backend.Nats; +using Microsoft.EntityFrameworkCore; +using Shared.Contracts.Messages; +using Shared.Contracts.Models; +using Shared.Infrastructure.Constants; + +namespace Backend.Removals; + +/// Reaches an agent for its removal (the seam the tests record through). +public interface IDeviceRemovalSender +{ + /// Publishes the remove command; false when messaging is down. + Task SendRemoveAsync(string clientId, AgentRemoveCommandPayload command, CancellationToken cancellationToken); + + /// + /// Deletes the agent's durable consumer and purges its command subjects, so + /// a device enrolled again never replays old commands. Throws when messaging + /// is down and something may be left behind. + /// + Task ClearCommandsAsync(string clientId, CancellationToken cancellationToken); +} + +public sealed class DeviceRemovalSender(CommandDispatcher dispatcher, JetStreamProvisioner provisioner, ILogger logger) + : IDeviceRemovalSender +{ + public async Task SendRemoveAsync(string clientId, AgentRemoveCommandPayload command, CancellationToken cancellationToken) + { + try + { + // A removal reopened after it closed has lost its consumer. + await provisioner.EnsureAgentConsumerAsync(clientId, cancellationToken); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + logger.LogWarning(ex, "Could not ensure the command consumer for {ClientId}", clientId); + return false; + } + + return await dispatcher.DispatchAgentRemoveAsync(clientId, command, cancellationToken); + } + + public Task ClearCommandsAsync(string clientId, CancellationToken cancellationToken) + => provisioner.RemoveAgentCommandsAsync(clientId, cancellationToken); +} + +/// What one scheduler pass did (for tests and logs). +public sealed record RemovalPass(int Sent, int Finished); + +/// +/// Drives open removals every 15 seconds: +/// +/// Sends the remove command to a device seen online, not more often than +/// . An agent too old to take it is marked and stays +/// open: the server keeps blocking it. +/// A command nobody answered goes back to waiting after +/// , for when the device next returns. +/// An agent that said it was uninstalling and then went silent counts as +/// gone, since nobody is left to report success: its consumer and commands are +/// cleared and the removal closes. One that keeps talking was not uninstalled, +/// and goes back to waiting. +/// +/// Like the schedule worker it runs on the node with SCHEDULE_WORKER on; each +/// row's write is guarded by its concurrency stamp, so a second scheduler, an +/// event on another node or an operator's stop can never double-send a row. +/// +public sealed class DeviceRemovalScheduler( + IServiceScopeFactory scopeFactory, + IDeviceRemovalSender sender, + ILogger logger) : BackgroundService +{ + public static readonly TimeSpan Tick = TimeSpan.FromSeconds(15); + public static readonly TimeSpan ResendAfter = TimeSpan.FromMinutes(5); + public static readonly TimeSpan ReportDeadline = TimeSpan.FromMinutes(30); + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + while (!stoppingToken.IsCancellationRequested) + { + try + { + await RunPassAsync(DateTimeOffset.UtcNow, stoppingToken); + } + catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) + { + return; + } + catch (Exception ex) + { + logger.LogError(ex, "Device removal pass failed"); + } + + try + { + await Task.Delay(Tick, stoppingToken); + } + catch (OperationCanceledException) + { + return; + } + } + } + + internal async Task RunPassAsync(DateTimeOffset now, CancellationToken cancellationToken) + { + using var scope = scopeFactory.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + var removals = scope.ServiceProvider.GetRequiredService(); + + var open = DeviceRemovalStates.Open.ToList(); + var rows = await db.DeviceRemovals.Where(r => open.Contains(r.State)).ToListAsync(cancellationToken); + + var sent = 0; + var finished = 0; + var messagingDown = false; + foreach (var row in rows) + { + switch (row.State) + { + case DeviceRemovalStates.Uninstalling: + finished += await AdvanceUninstallingAsync(db, removals, row, now, cancellationToken) ? 1 : 0; + break; + + case DeviceRemovalStates.Sent: + if (now - (row.SentUtc ?? row.UpdatedUtc) > ReportDeadline) + { + row.State = DeviceRemovalStates.Waiting; + row.UpdatedUtc = now; + if (await TrySaveAsync(db, row, cancellationToken)) + { + await removals.AnnounceAsync(row, cancellationToken); + } + } + + break; + + case DeviceRemovalStates.Waiting or DeviceRemovalStates.Failed when !messagingDown: + switch (await TrySendAsync(db, removals, row, now, cancellationToken)) + { + case SendResult.Sent: + sent++; + break; + case SendResult.MessagingDown: + messagingDown = true; + logger.LogWarning("Removal {RemovalId} could not be sent; retrying next pass", row.Id); + break; + } + + break; + } + } + + return new RemovalPass(sent, finished); + } + + private enum SendResult + { + NotDue, + Sent, + MessagingDown, + } + + private async Task TrySendAsync( + AppDbContext db, DeviceRemovalService removals, DeviceRemovalEntity row, DateTimeOffset now, CancellationToken cancellationToken) + { + // Presence brings the running version, so an agent upgraded by hand gets past this. + var tooOld = !string.IsNullOrWhiteSpace(row.AgentVersion) + && AgentVersions.Compare(row.AgentVersion, DeviceRemovalDefaults.MinimumAgentVersion) < 0; + if (tooOld) + { + if (row.Reason != DeviceRemovalReasons.AgentTooOld) + { + row.Reason = DeviceRemovalReasons.AgentTooOld; + row.Detail = $"The agent runs {row.AgentVersion}; removing it needs {DeviceRemovalDefaults.MinimumAgentVersion} or later."; + row.UpdatedUtc = now; + if (await TrySaveAsync(db, row, cancellationToken)) + { + await removals.AnnounceAsync(row, cancellationToken); + } + } + + return SendResult.NotDue; + } + + var online = ProtocolDefaults.CalculateStatus(row.LastSeenUtc, now) == DeviceStatus.Online; + if (!online + || string.IsNullOrWhiteSpace(row.AgentVersion) + || (row.SentUtc is { } lastSent && now - lastSent < ResendAfter)) + { + return SendResult.NotDue; + } + + // Mark it sent first: a row is never handed to JetStream twice. + var (state, reason, detail, sentUtc, updatedUtc) = (row.State, row.Reason, row.Detail, row.SentUtc, row.UpdatedUtc); + row.State = DeviceRemovalStates.Sent; + row.Reason = null; + row.Detail = null; + row.SentUtc = now; + row.UpdatedUtc = now; + row.Attempts++; + if (!await TrySaveAsync(db, row, cancellationToken)) + { + return SendResult.NotDue; // Stopped or taken meanwhile. + } + + if (!await sender.SendRemoveAsync(row.ClientId, new AgentRemoveCommandPayload(row.Id, row.EnrollmentId), cancellationToken)) + { + // Messaging is down: as it was, the next pass tries again. + (row.State, row.Reason, row.Detail, row.SentUtc, row.UpdatedUtc) = (state, reason, detail, sentUtc, updatedUtc); + row.Attempts--; + await TrySaveAsync(db, row, cancellationToken); + return SendResult.MessagingDown; + } + + await removals.AnnounceAsync(row, cancellationToken); + return SendResult.Sent; + } + + /// True when the removal closed. + private async Task AdvanceUninstallingAsync( + AppDbContext db, DeviceRemovalService removals, DeviceRemovalEntity row, DateTimeOffset now, CancellationToken cancellationToken) + { + // UpdatedUtc is when it said so: presence moves only LastSeenUtc. + if (row.LastSeenUtc is { } seen && seen - row.UpdatedUtc > DeviceRemovalDefaults.UninstallSilence) + { + row.State = DeviceRemovalStates.Waiting; + row.Detail = "The agent kept reporting after it said it was uninstalling."; + row.UpdatedUtc = now; + if (await TrySaveAsync(db, row, cancellationToken)) + { + await removals.AnnounceAsync(row, cancellationToken); + } + + return false; + } + + var lastSign = row.LastSeenUtc is { } last && last > row.UpdatedUtc ? last : row.UpdatedUtc; + if (now - lastSign <= DeviceRemovalDefaults.UninstallSilence) + { + return false; + } + + try + { + await sender.ClearCommandsAsync(row.ClientId, cancellationToken); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + // Close it only once nothing can replay; the next pass tries again. + logger.LogWarning(ex, "Could not clear the commands of removed device {ClientId}", row.ClientId); + return false; + } + + row.State = DeviceRemovalStates.Removed; + row.FinishedUtc = now; + row.UpdatedUtc = now; + DeviceHistory.RecordRemoved(db, row.ClientId, "The agent said it was uninstalling, then went silent."); + if (!await TrySaveAsync(db, row, cancellationToken)) + { + return false; + } + + await removals.AnnounceAsync(row, cancellationToken); + return true; + } + + /// Saves one row's change; false (the row re-read, its history dropped) when it changed elsewhere first. + private static async Task TrySaveAsync(AppDbContext db, DeviceRemovalEntity row, CancellationToken cancellationToken) + { + try + { + await db.SaveChangesAsync(cancellationToken); + return true; + } + catch (DbUpdateConcurrencyException) + { + foreach (var added in db.ChangeTracker.Entries().Where(e => e.State == EntityState.Added).ToList()) + { + added.State = EntityState.Detached; + } + + await db.Entry(row).ReloadAsync(cancellationToken); + return false; + } + } +} diff --git a/src/Backend/Removals/DeviceRemovalService.cs b/src/Backend/Removals/DeviceRemovalService.cs new file mode 100644 index 0000000..b3f3a0a --- /dev/null +++ b/src/Backend/Removals/DeviceRemovalService.cs @@ -0,0 +1,393 @@ +using Backend.Api; +using Backend.Data; +using Backend.Groups; +using Backend.Messaging; +using Backend.Security; +using Backend.Upgrades; +using Microsoft.EntityFrameworkCore; +using Shared.Contracts.Api; +using Shared.Contracts.Messages; +using Shared.Contracts.Models; +using Shared.Contracts.Serialization; +using Shared.Infrastructure.Nats; + +namespace Backend.Removals; + +/// +/// Removing devices: the tombstone that stands in for a deleted device row, the +/// agent's sightings and events, and the device-history trail. The scheduler +/// sends what is open here; nothing is sent from this class. +/// +public sealed class DeviceRemovalService( + AppDbContext db, + DeviceGroupService groups, + BrowserEventBroadcaster broadcaster, + ILogger logger) +{ + /// Activity kind consoles refresh devices and removals on. + public const string ActivityKind = "device-removal"; + + public const string SkipOutOfScope = "out-of-scope"; + public const string SkipUnknown = "unknown"; + + public const int NoteMaxLength = 500; + private const int DetailMaxLength = 2000; + private const int ReasonMaxLength = 32; + + /// States an agent may report. + private static readonly IReadOnlySet AgentStates = new HashSet(StringComparer.Ordinal) + { + DeviceRemovalStates.Uninstalling, DeviceRemovalStates.Failed, + }; + + public static DeviceRemovalDto ToDto(DeviceRemovalEntity row, DateTimeOffset now) => new( + row.Id, row.ClientId, row.Hostname, row.OsCaption, row.AgentVersion, row.State, row.Reason, row.Detail, + row.Attempts, row.RequestedBy, row.Note, row.RequestedUtc, row.UpdatedUtc, row.SentUtc, row.LastSeenUtc, + row.FinishedUtc, ShowInDeviceList(row, now)); + + public static bool ShowInDeviceList(DeviceRemovalEntity row, DateTimeOffset now) + => now - row.UpdatedUtc < DeviceRemovalDefaults.VisibleInDeviceList; + + /// + /// For each device: its tombstone in, its row and everything that would + /// reattach to a returning client id out, in one save. A device that is + /// already gone (a second request racing this one) is skipped as unknown. + /// + public async Task<(List Removed, List Skipped)> RemoveAsync( + IReadOnlyList clientIds, string? note, string? requestedBy, CancellationToken cancellationToken) + { + var removed = new List(); + var skipped = new List(); + if (clientIds.Count == 0) + { + return (removed, skipped); + } + + // Read while the rows exist: a dynamic group's rules need them. Group ids + // are what lets a scoped role keep seeing the removals of its devices. + var groupsOf = await EffectiveGroupsAsync(clientIds, cancellationToken); + foreach (var clientId in clientIds) + { + if (await TryRemoveOneAsync(clientId, groupsOf.GetValueOrDefault(clientId), note, requestedBy, cancellationToken) is { } row) + { + removed.Add(row); + } + else + { + skipped.Add(new DeviceRemovalSkipDto(clientId, null, SkipUnknown)); + } + } + + return (removed, skipped); + } + + private async Task>> EffectiveGroupsAsync( + IReadOnlyCollection clientIds, CancellationToken cancellationToken) + { + var result = new Dictionary>(StringComparer.Ordinal); + if (!await db.DeviceGroups.AnyAsync(cancellationToken)) + { + return result; + } + + var wanted = clientIds.ToHashSet(StringComparer.Ordinal); + var membership = await groups.GetMembershipAsync(cancellationToken); + foreach (var group in membership.Groups) + { + foreach (var clientId in membership.Effective(group.Id).Where(wanted.Contains)) + { + if (!result.TryGetValue(clientId, out var list)) + { + result[clientId] = list = []; + } + + list.Add(group.Id); + } + } + + return result; + } + + private async Task TryRemoveOneAsync( + string clientId, List? groupIds, string? note, string? requestedBy, CancellationToken cancellationToken) + { + for (var attempt = 0; attempt < 3; attempt++) + { + var device = await db.Devices.SingleOrDefaultAsync(d => d.ClientId == clientId, cancellationToken); + if (device is null) + { + return null; + } + + var now = DateTimeOffset.UtcNow; + var row = new DeviceRemovalEntity + { + Id = Guid.NewGuid(), + ClientId = clientId, + NkeyPublic = device.NkeyPublic, + EnrollmentId = device.EnrollmentId, + Hostname = device.Hostname, + OsCaption = device.OsCaption, + AgentVersion = device.AgentVersion, + State = DeviceRemovalStates.Waiting, + RequestedBy = requestedBy, + Note = note, + RequestedUtc = now, + UpdatedUtc = now, + LastSeenUtc = device.LastSeenUtc, + GroupIdsJson = groupIds is { Count: > 0 } ? JsonDefaults.Serialize(groupIds) : null, + }; + db.DeviceRemovals.Add(row); + await StageDependentsAsync(clientId, now, cancellationToken); + db.Devices.Remove(device); + DeviceHistory.RecordRemovalRequested(db, clientId, requestedBy, note); + + try + { + await db.SaveChangesAsync(cancellationToken); + return row; + } + catch (DbUpdateConcurrencyException) + { + // A schedule fired or the device went meanwhile: read it all again. + db.ChangeTracker.Clear(); + } + } + + logger.LogWarning("Device {ClientId} kept changing; its removal was left out", clientId); + return null; + } + + /// + /// What references the client id without a foreign key. Group memberships + /// would attach to the device again if the id returns; device-specific + /// schedules would keep firing (disabled, as when their group is deleted); + /// trigger runs would stop on-registration triggers firing after a legitimate + /// re-enrollment. Jobs that never started would sit queued forever. Finished + /// jobs, their output and the audit trail stay as history. + /// + private async Task StageDependentsAsync(string clientId, DateTimeOffset now, CancellationToken cancellationToken) + { + db.DeviceGroupMembers.RemoveRange( + await db.DeviceGroupMembers.Where(m => m.ClientId == clientId).ToListAsync(cancellationToken)); + db.ScriptTriggerRuns.RemoveRange( + await db.ScriptTriggerRuns.Where(r => r.ClientId == clientId).ToListAsync(cancellationToken)); + + foreach (var trigger in await db.ScriptTriggers.Where(t => t.ClientId == clientId && t.Enabled).ToListAsync(cancellationToken)) + { + trigger.Enabled = false; + trigger.LastResult = "Disabled: its target device was removed."; + } + + var unstarted = await db.Jobs + .Where(j => j.ClientId == clientId && (j.State == JobState.Queued || j.State == JobState.Dispatched)) + .ToListAsync(cancellationToken); + foreach (var job in unstarted) + { + job.State = JobState.Cancelled; + job.CompletedUtc = now; + job.OutputSummary = "Cancelled: the device was removed."; + } + } + + /// The newest removal of a client id, tracked; older ones are history only. + public Task LatestAsync(string clientId, CancellationToken cancellationToken) + => db.DeviceRemovals + .Where(r => r.ClientId == clientId) + .OrderByDescending(r => r.RequestedUtc) + .FirstOrDefaultAsync(cancellationToken); + + /// + /// Presence, inventory or a ready notice from a client id with no device + /// row. An open removal notes the sighting (the scheduler sends on it), so + /// the device row is never resurrected. An agent still talking after its + /// removal closed as removed was not gone after all: back to waiting, with + /// its command consumer restored when the next command is sent. A stopped + /// removal stays stopped. True when a removal took the sighting. + /// + public async Task NoteSightingAsync( + string? subjectClientId, string clientId, string? agentVersion, CancellationToken cancellationToken) + { + if (subjectClientId is not null && !string.Equals(subjectClientId, clientId, StringComparison.Ordinal)) + { + return false; + } + + var row = await LatestAsync(clientId, cancellationToken); + if (row is null || row.State == DeviceRemovalStates.Stopped) + { + return false; + } + + var now = DateTimeOffset.UtcNow; + var reopened = row.State == DeviceRemovalStates.Removed; + row.LastSeenUtc = now; + row.AgentVersion = string.IsNullOrWhiteSpace(agentVersion) ? row.AgentVersion : agentVersion; + if (reopened) + { + row.State = DeviceRemovalStates.Waiting; + row.FinishedUtc = null; + row.UpdatedUtc = now; + row.Detail = "The device came back after it was marked as removed."; + } + + try + { + await db.SaveChangesAsync(cancellationToken); + } + catch (DbUpdateConcurrencyException) + { + // The scheduler or an event got there first; the next sighting is seconds away. + db.ChangeTracker.Clear(); + return true; + } + + if (reopened) + { + await AnnounceAsync(row, cancellationToken); + } + + return true; + } + + /// + /// Applies an agent's removal event. Ignored: unknown ids, another device's + /// removal, states only the server sets, and anything after the removal + /// closed. True when the row changed. + /// + public async Task ApplyEventAsync( + string? subjectClientId, AgentRemovalEventPayload payload, CancellationToken cancellationToken) + { + if (!SubjectIdentifier.IsValid(payload.ClientId) + || (subjectClientId is not null && !string.Equals(subjectClientId, payload.ClientId, StringComparison.Ordinal)) + || !AgentStates.Contains(payload.State)) + { + return false; + } + + for (var attempt = 0; attempt < 3; attempt++) + { + var row = await db.DeviceRemovals.SingleOrDefaultAsync(r => r.Id == payload.RemovalId, cancellationToken); + if (row is null + || !string.Equals(row.ClientId, payload.ClientId, StringComparison.Ordinal) + || !DeviceRemovalStates.Open.Contains(row.State)) + { + return false; + } + + var now = DateTimeOffset.UtcNow; + row.State = payload.State; + row.Reason = Truncate(payload.Reason, ReasonMaxLength); + row.Detail = Truncate(payload.Detail, DetailMaxLength); + row.UpdatedUtc = now; + row.LastSeenUtc = now; + + try + { + await db.SaveChangesAsync(cancellationToken); + } + catch (DbUpdateConcurrencyException) + { + db.ChangeTracker.Clear(); + continue; + } + + await AnnounceAsync(row, cancellationToken); + return true; + } + + logger.LogWarning("Removal {RemovalId} kept changing; event {State} dropped", payload.RemovalId, payload.State); + return false; + } + + /// Tells consoles a removal changed, with a line for the activity feed. + public ValueTask AnnounceAsync(DeviceRemovalEntity row, CancellationToken cancellationToken) + => broadcaster.BroadcastActivityAsync(new ActivityEventPayload( + ActivityKind, Describe(row), row.ClientId, null, DateTimeOffset.UtcNow), cancellationToken); + + private static string Describe(DeviceRemovalEntity row) + { + var name = row.Hostname ?? row.ClientId; + return row.State switch + { + DeviceRemovalStates.Sent => $"{name} remove command sent", + DeviceRemovalStates.Uninstalling => $"{name} agent uninstalling", + DeviceRemovalStates.Removed => $"{name} agent removed", + DeviceRemovalStates.Failed => $"{name} agent could not uninstall ({row.Reason ?? "unknown"})", + DeviceRemovalStates.Stopped => $"{name} removal stopped", + _ => $"{name} removal waiting for the device", + }; + } + + private static string? Truncate(string? value, int length) + => value is not null && value.Length > length ? value[..length] : value; + + /// Names of removed devices, for what outlives them (old runs, old upgrades); the newest removal wins. + public static async Task> RemovedHostnamesAsync( + AppDbContext db, IEnumerable clientIds, CancellationToken cancellationToken) + { + var ids = clientIds.Distinct(StringComparer.Ordinal).ToList(); + if (ids.Count == 0) + { + return new Dictionary(StringComparer.Ordinal); + } + + var rows = await db.DeviceRemovals.AsNoTracking() + .Where(r => ids.Contains(r.ClientId) && r.Hostname != null) + .OrderBy(r => r.RequestedUtc) + .Select(r => new { r.ClientId, r.Hostname }) + .ToListAsync(cancellationToken); + + var names = new Dictionary(StringComparer.Ordinal); + foreach (var row in rows) + { + names[row.ClientId] = row.Hostname!; + } + + return names; + } + + /// + /// True when the caller's roles may see this removal: an unscoped role sees + /// all, a scoped role those of devices that were in one of its scope groups + /// when they were removed. + /// + public static bool IsVisibleTo(DeviceRemovalEntity row, IReadOnlyList? scopeGroupIds) + { + if (scopeGroupIds is null) + { + return true; + } + + if (string.IsNullOrEmpty(row.GroupIdsJson)) + { + return false; + } + + return (JsonDefaults.Deserialize>(row.GroupIdsJson) ?? []).Intersect(scopeGroupIds).Any(); + } +} + +/// Removal events on the agent event subject (MessageTypes.AgentRemoval). +public static class DeviceRemovalEvents +{ + public static async Task HandleAsync( + IServiceScopeFactory scopeFactory, string? subject, MessageEnvelope envelope, CancellationToken cancellationToken) + { + var payload = envelope.GetPayload(); + if (payload is null) + { + return; + } + + using var scope = scopeFactory.CreateScope(); + await scope.ServiceProvider.GetRequiredService() + .ApplyEventAsync(AgentUpgradeEvents.SubjectClientId(subject), payload, cancellationToken); + } + + /// A sighting from a client id with no device row; comes with presence. + public static Task NoteSightingAsync( + IServiceProvider services, string? subject, string clientId, string? agentVersion, CancellationToken cancellationToken) + => services.GetRequiredService() + .NoteSightingAsync(AgentUpgradeEvents.SubjectClientId(subject), clientId, agentVersion, cancellationToken); +} diff --git a/src/Backend/Upgrades/AgentUpgradeEndpoints.cs b/src/Backend/Upgrades/AgentUpgradeEndpoints.cs index 686a91d..9210283 100644 --- a/src/Backend/Upgrades/AgentUpgradeEndpoints.cs +++ b/src/Backend/Upgrades/AgentUpgradeEndpoints.cs @@ -240,13 +240,22 @@ public static class AgentUpgradeEndpoints return Results.Ok(AgentUpgradeService.ToDto(row, hostname)); } - private static Task> HostnamesAsync( + private static async Task> HostnamesAsync( AppDbContext db, IEnumerable clientIds, CancellationToken cancellationToken) { var ids = clientIds.Distinct(StringComparer.Ordinal).ToList(); - return db.Devices.AsNoTracking() + var hostnames = await db.Devices.AsNoTracking() .Where(d => ids.Contains(d.ClientId)) .ToDictionaryAsync(d => d.ClientId, d => d.Hostname, cancellationToken); + + // Old upgrades of a removed device keep its name. + foreach (var (clientId, hostname) in await Removals.DeviceRemovalService.RemovedHostnamesAsync( + db, ids.Where(id => !hostnames.ContainsKey(id)), cancellationToken)) + { + hostnames[clientId] = hostname; + } + + return hostnames; } private static async Task GetSettingsAsync( -- 2.52.0 From f8abdda4a6157271932f8fcc97001e839d868474dcb3a00ff3b869507434ecd5 Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Sat, 31 Oct 2026 19:57:41 -0400 Subject: [PATCH 03/20] feat(removal): short-lived agent credentials, and enrollment answers a removal Agents from the minimum version get 24 h credentials, renewed over HTTPS; older ones keep a non-expiring credential since they could never renew. A renewal for a removed device proves the enrolled key first, then answers 410 (open and closed removals) or a plain 401 (stopped: Stop Trying means the agent stays installed). Tokens created at or before a removal no longer enroll that device; a newer one closes the removal and clears old commands before creating a fresh row. Co-Authored-By: Claude Opus 5.5 --- src/Backend/Enrollment/EnrollmentEndpoints.cs | 186 +++++++++++++++--- src/Backend/Nats/NatsCredentialIssuer.cs | 15 +- 2 files changed, 168 insertions(+), 33 deletions(-) diff --git a/src/Backend/Enrollment/EnrollmentEndpoints.cs b/src/Backend/Enrollment/EnrollmentEndpoints.cs index 152e230..7aedfc3 100644 --- a/src/Backend/Enrollment/EnrollmentEndpoints.cs +++ b/src/Backend/Enrollment/EnrollmentEndpoints.cs @@ -15,7 +15,9 @@ namespace Backend.Enrollment; /// Agent bootstrap and credential refresh. Bootstrap validates and consumes a /// one-time token, binds the agent's public NKey to the device and returns a /// scoped NATS user JWT. Refresh proves possession of the enrolled NKey by -/// verifying a signature over {clientId}|{timestampUtc:O}. +/// verifying a signature over {clientId}|{timestampUtc:O}. Both answer +/// to a device's removal: tokens from before it no longer enroll the device, and +/// a renewal is told the device has gone. /// public static class EnrollmentEndpoints { @@ -39,6 +41,7 @@ public static class EnrollmentEndpoints JetStreamProvisioner provisioner, Messaging.BrowserEventBroadcaster broadcaster, Groups.ScriptTriggerService triggers, + Removals.IDeviceRemovalSender removalSender, AuditService audit, HttpContext context, ILoggerFactory loggerFactory, @@ -88,6 +91,45 @@ public static class EnrollmentEndpoints return Results.Unauthorized(); } + // A removed device comes back only on a token made after its removal, so + // the install it already has, or a GPO still pushing an old one, cannot. + var removal = await db.DeviceRemovals + .Where(r => r.ClientId == request.ClientId) + .OrderByDescending(r => r.RequestedUtc) + .FirstOrDefaultAsync(cancellationToken); + if (removal is not null && token.CreatedUtc <= removal.RequestedUtc) + { + await audit.LogAsync(AuditCategories.Enrollment, "bootstrap-rejected", + clientId: request.ClientId, detail: $"token predates the device's removal {removal.Id:D}", remoteIp: remoteIp, + cancellationToken: cancellationToken); + logger.LogWarning("Bootstrap rejected for removed client {ClientId} from {RemoteIp}", request.ClientId, remoteIp); + return Results.Unauthorized(); + } + + // Enrolling again: nothing queued for the old enrollment may replay, and + // the agent retries when the messaging system could not make sure of that. + if (removal is not null && device is null) + { + try + { + await removalSender.ClearCommandsAsync(request.ClientId, cancellationToken); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + logger.LogWarning(ex, "Could not clear old commands of re-enrolling client {ClientId}", request.ClientId); + return Results.Problem(statusCode: StatusCodes.Status503ServiceUnavailable, title: "Messaging unavailable."); + } + + if (Shared.Contracts.Models.DeviceRemovalStates.Open.Contains(removal.State)) + { + removal.State = Shared.Contracts.Models.DeviceRemovalStates.Removed; + removal.Detail = "The device enrolled again."; + removal.FinishedUtc = now; + removal.UpdatedUtc = now; + Api.DeviceHistory.RecordRemoved(db, request.ClientId, "The device enrolled again on a new token before its agent uninstalled."); + } + } + // Idempotent enrollment by client id: re-enrollment rebinds the NKey. var enrollmentId = Guid.NewGuid(); @@ -146,7 +188,12 @@ public static class EnrollmentEndpoints device.BootstrapTokenId = token.Id; - var userJwt = issuer.IssueAgentCredential(request.ClientId, request.NkeyPublic); + // An agent too old to renew keeps a credential that never expires: one + // that expired would strand it, and it cannot take a remove command either. + DateTimeOffset? credentialExpires = RenewsItsCredential(request.AgentVersion) + ? now + Shared.Contracts.Models.AgentCredentialDefaults.Lifetime + : null; + var userJwt = issuer.IssueAgentCredential(request.ClientId, request.NkeyPublic, credentialExpires); await db.SaveChangesAsync(cancellationToken); await audit.LogAsync(AuditCategories.Enrollment, "bootstrap-succeeded", @@ -179,9 +226,12 @@ public static class EnrollmentEndpoints await triggers.FireAsync( Shared.Contracts.Models.ScriptTriggerType.OnRegistration, request.ClientId, cancellationToken); - return Results.Ok(new BootstrapResponse(enrollmentId, request.ClientId, userJwt, CredentialExpiresUtc: null)); + return Results.Ok(new BootstrapResponse(enrollmentId, request.ClientId, userJwt, credentialExpires)); } + private static bool RenewsItsCredential(string? agentVersion) + => Shared.Contracts.Models.AgentVersions.Compare(agentVersion, Shared.Contracts.Models.DeviceRemovalDefaults.MinimumAgentVersion) >= 0; + private static bool IsValidUserPublicKey(string candidate) { try @@ -199,6 +249,7 @@ public static class EnrollmentEndpoints CredentialRefreshRequest request, AppDbContext db, NatsCredentialIssuer issuer, + Removals.DeviceRemovalService removals, AuditService audit, HttpContext context, CancellationToken cancellationToken) @@ -209,8 +260,12 @@ public static class EnrollmentEndpoints } var device = await db.Devices.SingleOrDefaultAsync(d => d.ClientId == request.ClientId, cancellationToken); - if (device is null - || !string.Equals(device.NkeyPublic, request.NkeyPublic, StringComparison.Ordinal) + if (device is null) + { + return await RefuseRemovedAsync(request, db, removals, audit, context, cancellationToken); + } + + if (!string.Equals(device.NkeyPublic, request.NkeyPublic, StringComparison.Ordinal) || !issuer.IsConfigured) { return Results.Unauthorized(); @@ -226,32 +281,11 @@ public static class EnrollmentEndpoints return Results.Unauthorized(); } - var skew = DateTimeOffset.UtcNow - request.TimestampUtc; - if (skew.Duration() > RefreshTimestampTolerance) + if (!await ProvesKeyAsync(request, device.NkeyPublic, audit, context, cancellationToken)) { return Results.Unauthorized(); } - byte[] signature; - try - { - signature = Convert.FromBase64String(request.SignatureBase64); - } - catch (FormatException) - { - return Results.Unauthorized(); - } - - var payload = Encoding.UTF8.GetBytes($"{request.ClientId}|{request.TimestampUtc:O}"); - using var verifier = KeyPair.FromPublicKey(device.NkeyPublic); - if (!verifier.Verify(payload, signature)) - { - await audit.LogAsync(AuditCategories.Enrollment, "credential-refresh-rejected", - clientId: request.ClientId, detail: "signature verification failed", - remoteIp: ClientIp.EffectiveText(context), cancellationToken: cancellationToken); - return Results.Unauthorized(); - } - // A verified refresh is the freshest sighting of the agent's public IP. var publicIp = ClientIp.EffectiveText(context); if (!string.Equals(device.PublicIp, publicIp, StringComparison.Ordinal)) @@ -260,7 +294,101 @@ public static class EnrollmentEndpoints await db.SaveChangesAsync(cancellationToken); } - var userJwt = issuer.IssueAgentCredential(request.ClientId, request.NkeyPublic); - return Results.Ok(new CredentialRefreshResponse(userJwt, CredentialExpiresUtc: null)); + // Only agents that renew call this, so the new credential always expires. + var expires = DateTimeOffset.UtcNow + Shared.Contracts.Models.AgentCredentialDefaults.Lifetime; + var userJwt = issuer.IssueAgentCredential(request.ClientId, request.NkeyPublic, expires); + return Results.Ok(new CredentialRefreshResponse(userJwt, expires)); + } + + /// True when the request is fresh and signed with the enrolled key. + private static async Task ProvesKeyAsync( + CredentialRefreshRequest request, string nkeyPublic, AuditService audit, HttpContext context, + CancellationToken cancellationToken) + { + var skew = DateTimeOffset.UtcNow - request.TimestampUtc; + if (skew.Duration() > RefreshTimestampTolerance) + { + return false; + } + + byte[] signature; + try + { + signature = Convert.FromBase64String(request.SignatureBase64); + } + catch (FormatException) + { + return false; + } + + var payload = Encoding.UTF8.GetBytes($"{request.ClientId}|{request.TimestampUtc:O}"); + using var verifier = KeyPair.FromPublicKey(nkeyPublic); + if (verifier.Verify(payload, signature)) + { + return true; + } + + await audit.LogAsync(AuditCategories.Enrollment, "credential-refresh-rejected", + clientId: request.ClientId, detail: "signature verification failed", + remoteIp: ClientIp.EffectiveText(context), cancellationToken: cancellationToken); + return false; + } + + /// + /// A renewal for a client id with no device row. Only the agent holding the + /// enrolled key learns anything: everyone else gets the plain 401 an unknown + /// id gets. An open removal answers 410 and the agent uninstalls itself, which + /// also reaches a device that came back too briefly for a command. So does a + /// closed one, as an agent that returns from the dead should still go. A + /// stopped removal gets the plain 401: Stop Trying means its agent stays + /// installed and stays blocked, and 410 would uninstall it after all. + /// + private static async Task RefuseRemovedAsync( + CredentialRefreshRequest request, AppDbContext db, Removals.DeviceRemovalService removals, AuditService audit, + HttpContext context, CancellationToken cancellationToken) + { + var removal = await removals.LatestAsync(request.ClientId, cancellationToken); + if (removal is null + || !string.Equals(removal.NkeyPublic, request.NkeyPublic, StringComparison.Ordinal) + || !await ProvesKeyAsync(request, removal.NkeyPublic, audit, context, cancellationToken)) + { + return Results.Unauthorized(); + } + + var remoteIp = ClientIp.EffectiveText(context); + if (removal.State == Shared.Contracts.Models.DeviceRemovalStates.Stopped) + { + await audit.LogAsync(AuditCategories.Enrollment, "credential-refresh-refused", + clientId: request.ClientId, detail: "removal stopped: the device stays blocked", remoteIp: remoteIp, + cancellationToken: cancellationToken); + return Results.Unauthorized(); + } + + if (Shared.Contracts.Models.DeviceRemovalStates.Open.Contains(removal.State)) + { + var now = DateTimeOffset.UtcNow; + removal.State = Shared.Contracts.Models.DeviceRemovalStates.Uninstalling; + removal.Reason = null; + removal.UpdatedUtc = now; + removal.LastSeenUtc = now; + try + { + await db.SaveChangesAsync(cancellationToken); + await removals.AnnounceAsync(removal, cancellationToken); + } + catch (DbUpdateConcurrencyException) + { + // Changed on another node meanwhile; the answer is the same, and the event or next renewal says so again. + db.ChangeTracker.Clear(); + } + } + + await audit.LogAsync(AuditCategories.Enrollment, "credential-refresh-refused", + clientId: request.ClientId, detail: $"device removed ({removal.Id:D})", remoteIp: remoteIp, + cancellationToken: cancellationToken); + return Results.Problem( + statusCode: StatusCodes.Status410Gone, + type: Shared.Contracts.Models.AgentCredentialDefaults.RemovedProblemType, + title: "This device was removed."); } } diff --git a/src/Backend/Nats/NatsCredentialIssuer.cs b/src/Backend/Nats/NatsCredentialIssuer.cs index 1b1efb1..d9db27b 100644 --- a/src/Backend/Nats/NatsCredentialIssuer.cs +++ b/src/Backend/Nats/NatsCredentialIssuer.cs @@ -7,9 +7,10 @@ namespace Backend.Nats; /// /// Issues scoped NATS user JWTs signed with the account signing key -/// (NATS_ACCOUNT_SIGNING_SEED). Agents get long-lived credentials restricted -/// to their own client id; browsers get short-lived credentials restricted to -/// their session subjects. The signing seed never appears in logs or responses. +/// (NATS_ACCOUNT_SIGNING_SEED). Agents get credentials restricted to their own +/// client id, expiring within a day for agents that renew them; browsers get +/// short-lived credentials restricted to their session subjects. The signing +/// seed never appears in logs or responses. /// public sealed class NatsCredentialIssuer : IDisposable { @@ -30,12 +31,18 @@ public sealed class NatsCredentialIssuer : IDisposable public bool IsConfigured => signingKey is not null; - public string IssueAgentCredential(string clientId, string userPublicKey) + /// Null only for agents too old to renew a credential. + public string IssueAgentCredential(string clientId, string userPublicKey, DateTimeOffset? expiresUtc) { var (key, account) = RequireConfiguration(); var claims = NatsJwt.NewUserClaims(userPublicKey); claims.Name = $"agent-{clientId}"; + if (expiresUtc is { } expires) + { + claims.Expires = expires; + } + claims.User.IssuerAccount = account; claims.User.Pub.Allow = [.. NatsSubjects.AgentPublishPermissions(clientId)]; claims.User.Sub.Allow = [.. NatsSubjects.AgentSubscribePermissions(clientId)]; -- 2.52.0 From 8ff23db6c9676da8192a88424601edb0beec6d0c4c06d7c843cfc1c077c1fdcb Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Sat, 31 Oct 2026 19:57:41 -0400 Subject: [PATCH 04/20] feat(removal): never move an agent below the first version that renews An agent that renews holds a credential that expires; an older version could not renew it and would strand within a day. Upgrades, roll backs and the scheduler leave such a device out as credential-downgrade. Exactly the minimum is fine. Co-Authored-By: Claude Opus 5.5 --- src/Backend/Upgrades/AgentUpgradeScheduler.cs | 10 +++++++++ src/Backend/Upgrades/AgentUpgradeService.cs | 22 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/Backend/Upgrades/AgentUpgradeScheduler.cs b/src/Backend/Upgrades/AgentUpgradeScheduler.cs index c149bb4..2515d24 100644 --- a/src/Backend/Upgrades/AgentUpgradeScheduler.cs +++ b/src/Backend/Upgrades/AgentUpgradeScheduler.cs @@ -237,6 +237,16 @@ public sealed class AgentUpgradeScheduler( continue; } + // Queued before the device moved past the minimum version, or by a + // request that raced it: an older agent could not renew its credential. + if (AgentUpgradeService.IsCredentialDowngrade(runningVersion(row.ClientId), row.TargetVersion)) + { + upgrades.Finish(row, AgentUpgradeStates.Cancelled, AgentUpgradeService.SkipCredentialDowngrade, + $"The device runs {runningVersion(row.ClientId)}, past the oldest version that renews its credential.", now); + await TrySaveRowAsync(db, row, cancellationToken); + continue; + } + // Mark it sent first: a row is never handed to JetStream twice. row.State = AgentUpgradeStates.Sent; row.SentUtc = now; diff --git a/src/Backend/Upgrades/AgentUpgradeService.cs b/src/Backend/Upgrades/AgentUpgradeService.cs index fd7c427..7aa34fe 100644 --- a/src/Backend/Upgrades/AgentUpgradeService.cs +++ b/src/Backend/Upgrades/AgentUpgradeService.cs @@ -26,6 +26,20 @@ public sealed class AgentUpgradeService( public const string SkipOrphaned = SkipReasons.Orphaned; public const string SkipNoPrevious = "no-previous"; + /// + /// The target is older than the first agent that renews its credential, and + /// the device already holds a credential that expires: it would strand within a day. + /// + public const string SkipCredentialDowngrade = "credential-downgrade"; + + /// + /// True when a device on must not go to + /// : exactly the minimum version still renews. + /// + public static bool IsCredentialDowngrade(string? running, string? target) + => AgentVersions.Compare(running, DeviceRemovalDefaults.MinimumAgentVersion) >= 0 + && AgentVersions.Compare(target, DeviceRemovalDefaults.MinimumAgentVersion) < 0; + /// Detail of a row the scheduler closed because presence already showed the target version. public const string PresenceConfirmed = "Confirmed by presence: the device reports the target version."; @@ -181,6 +195,10 @@ public sealed class AgentUpgradeService( // Already there (or, without an explicit version, ahead of the newest held). skip = AgentUpgradeReasons.Already; } + else if (IsCredentialDowngrade(device.AgentVersion, package.Version)) + { + skip = SkipCredentialDowngrade; + } } if (skip is not null) @@ -231,6 +249,10 @@ public sealed class AgentUpgradeService( { skip = AgentUpgradeReasons.Already; } + else if (IsCredentialDowngrade(device.AgentVersion, from)) + { + skip = SkipCredentialDowngrade; + } else if ((package = await catalog.FindAsync(from, types[device.ClientId]!, cancellationToken)) is null) { skip = SkipNoPrevious; -- 2.52.0 From e8326cb1da2f3bf56e26a614a2dde8ade00fdf2517e4314f9976236438cbe1a8 Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Sat, 31 Oct 2026 19:57:42 -0400 Subject: [PATCH 05/20] test(removal): removal requests, scheduler, enrollment and the upgrade guard Co-Authored-By: Claude Opus 5.5 --- tests/Backend.Tests/DeviceRemovalTests.cs | 834 ++++++++++++++++++++++ 1 file changed, 834 insertions(+) create mode 100644 tests/Backend.Tests/DeviceRemovalTests.cs diff --git a/tests/Backend.Tests/DeviceRemovalTests.cs b/tests/Backend.Tests/DeviceRemovalTests.cs new file mode 100644 index 0000000..a939f47 --- /dev/null +++ b/tests/Backend.Tests/DeviceRemovalTests.cs @@ -0,0 +1,834 @@ +using System.Net; +using System.Net.Http.Json; +using System.Text; +using System.Text.Json; +using Backend.Data; +using Backend.Removals; +using Backend.Upgrades; +using Microsoft.AspNetCore.TestHost; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging.Abstractions; +using NATS.Jwt; +using NATS.NKeys; +using Shared.Contracts.Api; +using Shared.Contracts.Messages; +using Shared.Contracts.Models; +using Shared.Contracts.Serialization; +using Shared.Infrastructure.Security; +using Xunit; +using static Backend.Tests.AgentUpgradeTestKit; + +namespace Backend.Tests; + +/// Records removal commands and cleared consumers instead of touching NATS. +internal sealed class RecordingRemovalSender : IDeviceRemovalSender +{ + public List<(string ClientId, AgentRemoveCommandPayload Command)> Sent { get; } = []; + public List Cleared { get; } = []; + public bool FailSend { get; set; } + public bool FailClear { get; set; } + + public Task SendRemoveAsync(string clientId, AgentRemoveCommandPayload command, CancellationToken cancellationToken) + { + if (FailSend) + { + return Task.FromResult(false); + } + + Sent.Add((clientId, command)); + return Task.FromResult(true); + } + + public Task ClearCommandsAsync(string clientId, CancellationToken cancellationToken) + { + if (FailClear) + { + throw new InvalidOperationException("Messaging is down."); + } + + Cleared.Add(clientId); + return Task.CompletedTask; + } +} + +internal static class DeviceRemovalTestKit +{ + public static DeviceRemovalScheduler RemovalScheduler(this BackendTestFixture fixture, IDeviceRemovalSender sender) + => new(fixture.Services.GetRequiredService(), sender, NullLogger.Instance); + + public static Task RemovalAsync(this BackendTestFixture fixture, Guid id) + => fixture.WithDbAsync(db => db.DeviceRemovals.AsNoTracking().SingleAsync(r => r.Id == id)); + + public static Task EditRemovalAsync(this BackendTestFixture fixture, Guid id, Action edit) + => fixture.WithDbAsync(async db => + { + edit(await db.DeviceRemovals.SingleAsync(r => r.Id == id)); + await db.SaveChangesAsync(); + }); + + public static async Task RemoveAsync(this HttpClient client, params string[] clientIds) + { + var response = await client.PostAsJsonAsync(ApiRoutes.DeviceRemovals, new RemoveDevicesRequest(clientIds), JsonDefaults.Options); + Assert.True(response.IsSuccessStatusCode, await response.Content.ReadAsStringAsync()); + return await response.ReadAsync(); + } + + public static async Task SightAsync(this BackendTestFixture fixture, string clientId, string? version = null, string? subject = null) + { + using var scope = fixture.Services.CreateScope(); + return await scope.ServiceProvider.GetRequiredService() + .NoteSightingAsync(subject ?? clientId, clientId, version, CancellationToken.None); + } + + public static async Task RemovalEventAsync( + this BackendTestFixture fixture, Guid id, string clientId, string state, string? reason = null, string? subject = null) + { + using var scope = fixture.Services.CreateScope(); + return await scope.ServiceProvider.GetRequiredService().ApplyEventAsync( + subject ?? clientId, + new AgentRemovalEventPayload(id, clientId, state, reason, "detail", DateTimeOffset.UtcNow), + CancellationToken.None); + } +} + +/// The request: what leaves with the device, what stays, who may ask. +public class DeviceRemovalRequestTests(BackendTestFixture fixture) : IClassFixture +{ + [Fact] + public async Task RemovingADeviceTakesItsDependentsAndKeepsItsHistory() + { + var clientId = await fixture.SeedAgentAsync("REMOVE-1", "1.1.0", osCaption: "Windows 11"); + var scriptId = await fixture.SeedScriptAsync($"remove-script-{Guid.NewGuid():N}"); + var (groupId, deviceTrigger, runId, finishedJob, queuedJob) = await fixture.WithDbAsync(async db => + { + var group = new DeviceGroupEntity + { + Id = Guid.NewGuid(), Name = $"remove-{Guid.NewGuid():N}", Kind = GroupKind.Static, + CreatedUtc = DateTimeOffset.UtcNow, UpdatedUtc = DateTimeOffset.UtcNow, + }; + db.DeviceGroups.Add(group); + db.DeviceGroupMembers.Add(new DeviceGroupMemberEntity { GroupId = group.Id, ClientId = clientId, AddedUtc = DateTimeOffset.UtcNow }); + var report = new InventoryReport( + new InventoryIdentity(clientId, "REMOVE-1", null, null, null, null, null, null, false, "X64"), + new InventoryOs("Windows 11", "10.0", null, "X64", null, null, null), + new InventoryCpu(null, 4, 4), [], 0, [], [], [], DateTimeOffset.UtcNow); + db.DeviceInventory.Add(new DeviceInventoryEntity { ClientId = clientId, ReportJson = JsonDefaults.Serialize(report), UpdatedUtc = DateTimeOffset.UtcNow }); + var trigger = new ScriptTriggerEntity + { + Id = Guid.NewGuid(), ScriptId = scriptId, ClientId = clientId, TriggerType = ScriptTriggerType.Scheduled, + CronExpression = "0 0 * * *", CreatedUtc = DateTimeOffset.UtcNow, + }; + db.ScriptTriggers.Add(trigger); + var run = new ScriptTriggerRunEntity { TriggerId = trigger.Id, ClientId = clientId, JobId = Guid.NewGuid(), FiredUtc = DateTimeOffset.UtcNow }; + db.ScriptTriggerRuns.Add(run); + var done = new JobEntity { JobId = Guid.NewGuid(), ClientId = clientId, ScriptId = scriptId, ScriptName = "s", State = JobState.Succeeded, CreatedUtc = DateTimeOffset.UtcNow }; + var queued = new JobEntity { JobId = Guid.NewGuid(), ClientId = clientId, ScriptId = scriptId, ScriptName = "s", State = JobState.Queued, CreatedUtc = DateTimeOffset.UtcNow }; + db.Jobs.AddRange(done, queued); + await db.SaveChangesAsync(); + return (group.Id, trigger.Id, run.Id, done.JobId, queued.JobId); + }); + + using var admin = await fixture.SignedInAsync("removal-admin"); + var batch = await admin.RemoveAsync(clientId); + + var removal = Assert.Single(batch.Removals); + Assert.Empty(batch.Skipped); + Assert.Equal(DeviceRemovalStates.Waiting, removal.State); + Assert.Equal("REMOVE-1", removal.Hostname); + Assert.Equal("Windows 11", removal.OsCaption); + Assert.Equal("1.1.0", removal.AgentVersion); + Assert.Equal("removal-admin", removal.RequestedBy); + Assert.True(removal.ShowInDeviceList); + + await fixture.WithDbAsync(async db => + { + Assert.False(await db.Devices.AnyAsync(d => d.ClientId == clientId)); + Assert.False(await db.DeviceInventory.AnyAsync(i => i.ClientId == clientId)); + Assert.False(await db.DeviceGroupMembers.AnyAsync(m => m.GroupId == groupId)); + Assert.False(await db.ScriptTriggerRuns.AnyAsync(r => r.Id == runId)); + + var trigger = await db.ScriptTriggers.SingleAsync(t => t.Id == deviceTrigger); + Assert.False(trigger.Enabled); + Assert.Contains("removed", trigger.LastResult); + + Assert.Equal(JobState.Succeeded, (await db.Jobs.SingleAsync(j => j.JobId == finishedJob)).State); + Assert.Equal(JobState.Cancelled, (await db.Jobs.SingleAsync(j => j.JobId == queuedJob)).State); + }); + + // Old runs keep the name; the device's trail stays reachable. + var job = await admin.GetAsync($"/api/jobs/{finishedJob:D}/output/download"); + job.EnsureSuccessStatusCode(); + Assert.Contains("REMOVE-1", job.Content.Headers.ContentDisposition?.FileName ?? job.Content.Headers.ContentDisposition?.FileNameStar); + + var history = await (await admin.GetAsync(ApiRoutes.For.DeviceHistory(clientId))).ReadAsync>(); + Assert.Contains(history, e => e.Action == Backend.Api.DeviceHistory.RemovalRequested); + + // The detail is gone, and a second request finds nothing. + Assert.Equal(HttpStatusCode.NotFound, (await admin.GetAsync(ApiRoutes.For.Device(clientId))).StatusCode); + var again = await admin.RemoveAsync(clientId); + Assert.Empty(again.Removals); + Assert.Equal(DeviceRemovalService.SkipUnknown, Assert.Single(again.Skipped).Reason); + } + + [Fact] + public async Task HistoryOfANeverEnrolledDeviceIsStillNotFound() + { + using var admin = await fixture.SignedInAsync("removal-admin"); + var response = await admin.GetAsync(ApiRoutes.For.DeviceHistory("never-enrolled-device")); + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); + } + + [Fact] + public async Task RemovingAGroupReachesItsNestedGroups() + { + var top = await fixture.SeedAgentAsync("NEST-TOP", "1.1.0"); + var nested = await fixture.SeedAgentAsync("NEST-CHILD", "1.1.0"); + var outside = await fixture.SeedAgentAsync("NEST-OUT", "1.1.0"); + var parentId = await fixture.WithDbAsync(async db => + { + var parent = new DeviceGroupEntity + { + Id = Guid.NewGuid(), Name = $"parent-{Guid.NewGuid():N}", Kind = GroupKind.Static, + CreatedUtc = DateTimeOffset.UtcNow, UpdatedUtc = DateTimeOffset.UtcNow, + }; + var child = new DeviceGroupEntity + { + Id = Guid.NewGuid(), Name = $"child-{Guid.NewGuid():N}", Kind = GroupKind.Static, ParentId = parent.Id, + CreatedUtc = DateTimeOffset.UtcNow, UpdatedUtc = DateTimeOffset.UtcNow, + }; + db.DeviceGroups.AddRange(parent, child); + db.DeviceGroupMembers.Add(new DeviceGroupMemberEntity { GroupId = parent.Id, ClientId = top, AddedUtc = DateTimeOffset.UtcNow }); + db.DeviceGroupMembers.Add(new DeviceGroupMemberEntity { GroupId = child.Id, ClientId = nested, AddedUtc = DateTimeOffset.UtcNow }); + await db.SaveChangesAsync(); + return parent.Id; + }); + + using var admin = await fixture.SignedInAsync("removal-admin"); + var response = await admin.PostAsJsonAsync(ApiRoutes.DeviceRemovals, + new RemoveDevicesRequest(GroupIds: [parentId], Reason: " retired "), JsonDefaults.Options); + var batch = await response.ReadAsync(); + + Assert.Equal(["NEST-CHILD", "NEST-TOP"], batch.Removals.Select(r => r.Hostname)); + Assert.All(batch.Removals, r => Assert.Equal("retired", r.Note)); + Assert.Empty(batch.Skipped); + Assert.True(await fixture.WithDbAsync(db => db.Devices.AnyAsync(d => d.ClientId == outside))); + + Assert.Equal(HttpStatusCode.NotFound, (await admin.PostAsJsonAsync(ApiRoutes.DeviceRemovals, + new RemoveDevicesRequest(GroupIds: [Guid.NewGuid()]), JsonDefaults.Options)).StatusCode); + Assert.Equal(HttpStatusCode.BadRequest, (await admin.PostAsJsonAsync(ApiRoutes.DeviceRemovals, + new RemoveDevicesRequest(), JsonDefaults.Options)).StatusCode); + } + + [Fact] + public async Task AScopedRoleRemovesAndSeesOnlyItsOwnDevices() + { + var mine = await fixture.SeedAgentAsync("SCOPE-MINE", "1.1.0"); + var theirs = await fixture.SeedAgentAsync("SCOPE-THEIRS", "1.1.0"); + using var scoped = await fixture.ScopedSignedInAsync("removal-scoped", mine); + using var elsewhere = await fixture.ScopedSignedInAsync("removal-elsewhere", theirs); + using var admin = await fixture.SignedInAsync("removal-admin"); + + // Outside the scope reads the same whether or not the device exists. + var batch = await scoped.RemoveAsync(mine, theirs, "no-such-device-anywhere"); + Assert.Equal("SCOPE-MINE", Assert.Single(batch.Removals).Hostname); + Assert.Equal(2, batch.Skipped.Count); + Assert.All(batch.Skipped, s => + { + Assert.Equal(DeviceRemovalService.SkipOutOfScope, s.Reason); + Assert.Null(s.Hostname); + }); + Assert.True(await fixture.WithDbAsync(db => db.Devices.AnyAsync(d => d.ClientId == theirs))); + + var removalId = batch.Removals[0].Id; + + // The group kept on the removal keeps it in scope, with the device out of the group. + var seen = await (await scoped.GetAsync(ApiRoutes.DeviceRemovals)).ReadAsync>(); + Assert.Contains(seen, r => r.Id == removalId); + Assert.Equal(HttpStatusCode.OK, (await scoped.GetAsync(ApiRoutes.For.DeviceHistory(mine))).StatusCode); + + var notSeen = await (await elsewhere.GetAsync(ApiRoutes.DeviceRemovals)).ReadAsync>(); + Assert.DoesNotContain(notSeen, r => r.Id == removalId); + Assert.Equal(HttpStatusCode.NotFound, (await elsewhere.GetAsync(ApiRoutes.For.DeviceHistory(mine))).StatusCode); + Assert.Equal(HttpStatusCode.NotFound, (await elsewhere.PostAsync(ApiRoutes.For.DeviceRemovalStop(removalId), null)).StatusCode); + + var all = await (await admin.GetAsync(ApiRoutes.DeviceRemovals)).ReadAsync>(); + Assert.Contains(all, r => r.Id == removalId); + } + + [Fact] + public async Task RemovingNeedsItsOwnPermission() + { + var clientId = await fixture.SeedAgentAsync("PERM-1", "1.1.0"); + using var op = await fixture.SignedInAsync("removal-operator", "Operator"); + using var viewer = await fixture.SignedInAsync("removal-viewer", "Viewer"); + using var admin = await fixture.SignedInAsync("removal-admin"); + + foreach (var client in new[] { op, viewer }) + { + Assert.Equal(HttpStatusCode.Forbidden, (await client.PostAsJsonAsync(ApiRoutes.DeviceRemovals, + new RemoveDevicesRequest([clientId]), JsonDefaults.Options)).StatusCode); + Assert.Equal(HttpStatusCode.Forbidden, (await client.PostAsync(ApiRoutes.For.DeviceRemovalStop(Guid.NewGuid()), null)).StatusCode); + Assert.Equal(HttpStatusCode.OK, (await client.GetAsync(ApiRoutes.DeviceRemovals)).StatusCode); + } + + Assert.Single((await admin.RemoveAsync(clientId)).Removals); + } + + [Fact] + public async Task ListFiltersAndShowsARemovalInTheDeviceListForSevenDays() + { + var clientId = await fixture.SeedAgentAsync("LIST-1", "1.1.0"); + using var admin = await fixture.SignedInAsync("removal-admin"); + var id = (await admin.RemoveAsync(clientId)).Removals[0].Id; + + async Task ReadAsync(string query) + => Assert.Single(await (await admin.GetAsync($"{ApiRoutes.DeviceRemovals}?{query}")).ReadAsync>()); + + Assert.Equal(id, (await ReadAsync($"clientId={clientId}&state=waiting")).Id); + Assert.Empty(await (await admin.GetAsync($"{ApiRoutes.DeviceRemovals}?clientId={clientId}&state=removed")).ReadAsync>()); + + var justInside = DeviceRemovalDefaults.VisibleInDeviceList - TimeSpan.FromMinutes(1); + await fixture.EditRemovalAsync(id, r => r.UpdatedUtc = DateTimeOffset.UtcNow - justInside); + Assert.True((await ReadAsync($"clientId={clientId}")).ShowInDeviceList); + + var justPast = DeviceRemovalDefaults.VisibleInDeviceList + TimeSpan.FromMinutes(1); + await fixture.EditRemovalAsync(id, r => r.UpdatedUtc = DateTimeOffset.UtcNow - justPast); + Assert.False((await ReadAsync($"clientId={clientId}")).ShowInDeviceList); + + // Exactly at the edge it has dropped out. + var row = await fixture.RemovalAsync(id); + Assert.False(DeviceRemovalService.ShowInDeviceList(row, row.UpdatedUtc + DeviceRemovalDefaults.VisibleInDeviceList)); + } + + [Fact] + public async Task StoppingEndsAnOpenRemovalOnly() + { + var clientId = await fixture.SeedAgentAsync("STOP-1", "1.1.0"); + using var admin = await fixture.SignedInAsync("removal-admin"); + var id = (await admin.RemoveAsync(clientId)).Removals[0].Id; + + var stopped = await (await admin.PostAsync(ApiRoutes.For.DeviceRemovalStop(id), null)).ReadAsync(); + Assert.Equal(DeviceRemovalStates.Stopped, stopped.State); + Assert.NotNull(stopped.FinishedUtc); + Assert.Equal(HttpStatusCode.Conflict, (await admin.PostAsync(ApiRoutes.For.DeviceRemovalStop(id), null)).StatusCode); + Assert.Equal(HttpStatusCode.NotFound, (await admin.PostAsync(ApiRoutes.For.DeviceRemovalStop(Guid.NewGuid()), null)).StatusCode); + + // It stays blocked and is left alone: no sightings, no commands, no events. + Assert.False(await fixture.SightAsync(clientId, "1.1.0")); + Assert.False(await fixture.RemovalEventAsync(id, clientId, DeviceRemovalStates.Uninstalling)); + var sender = new RecordingRemovalSender(); + await fixture.RemovalScheduler(sender).RunPassAsync(DateTimeOffset.UtcNow, CancellationToken.None); + Assert.DoesNotContain(sender.Sent, s => s.ClientId == clientId); + Assert.Equal(DeviceRemovalStates.Stopped, (await fixture.RemovalAsync(id)).State); + } +} + +/// The scheduler: sending, an agent too old, events, closing. +public class DeviceRemovalSchedulerTests(BackendTestFixture fixture) : IClassFixture +{ + private async Task<(string ClientId, Guid Id)> RemovedAsync(string hostname, string? version = "1.1.0", bool online = true) + { + await fixture.WithDbAsync(db => db.DeviceRemovals.ExecuteDeleteAsync()); + var clientId = await fixture.SeedAgentAsync(hostname, version, online: online); + using var admin = await fixture.SignedInAsync("scheduler-admin"); + return (clientId, (await admin.RemoveAsync(clientId)).Removals[0].Id); + } + + [Fact] + public async Task SendsOnlyWhenTheDeviceWasSeenOnlineAndNotTooOften() + { + var (clientId, id) = await RemovedAsync("SEND-1", online: false); + var sender = new RecordingRemovalSender(); + var scheduler = fixture.RemovalScheduler(sender); + var now = DateTimeOffset.UtcNow; + + Assert.Equal(0, (await scheduler.RunPassAsync(now, CancellationToken.None)).Sent); + Assert.Equal(DeviceRemovalStates.Waiting, (await fixture.RemovalAsync(id)).State); + + // It returns: its enrollment id goes with the command. + Assert.True(await fixture.SightAsync(clientId, "1.2.0")); + now = DateTimeOffset.UtcNow; + Assert.Equal(1, (await scheduler.RunPassAsync(now, CancellationToken.None)).Sent); + var row = await fixture.RemovalAsync(id); + Assert.Equal(DeviceRemovalStates.Sent, row.State); + Assert.Equal(1, row.Attempts); + Assert.Equal("1.2.0", row.AgentVersion); + var (sentTo, command) = Assert.Single(sender.Sent); + Assert.Equal(clientId, sentTo); + Assert.Equal(new AgentRemoveCommandPayload(id, row.EnrollmentId), command); + + // Sent: not sent again. + Assert.Equal(0, (await scheduler.RunPassAsync(now.AddSeconds(15), CancellationToken.None)).Sent); + + // A failure within the back-off waits for it; after it, the server asks again. + Assert.True(await fixture.RemovalEventAsync(id, clientId, DeviceRemovalStates.Failed, DeviceRemovalReasons.Busy)); + await fixture.EditRemovalAsync(id, r => r.LastSeenUtc = now.AddMinutes(4)); + Assert.Equal(0, (await scheduler.RunPassAsync(now.AddMinutes(4), CancellationToken.None)).Sent); + await fixture.EditRemovalAsync(id, r => r.LastSeenUtc = now.AddMinutes(5)); + Assert.Equal(1, (await scheduler.RunPassAsync(now.AddMinutes(5), CancellationToken.None)).Sent); + row = await fixture.RemovalAsync(id); + Assert.Equal(2, row.Attempts); + Assert.Null(row.Reason); + } + + [Fact] + public async Task ACommandNobodyAnsweredGoesBackToWaiting() + { + var (_, id) = await RemovedAsync("SEND-2"); + var sender = new RecordingRemovalSender(); + var scheduler = fixture.RemovalScheduler(sender); + var now = DateTimeOffset.UtcNow; + await scheduler.RunPassAsync(now, CancellationToken.None); + + await scheduler.RunPassAsync(now + DeviceRemovalScheduler.ReportDeadline, CancellationToken.None); + Assert.Equal(DeviceRemovalStates.Sent, (await fixture.RemovalAsync(id)).State); + + await scheduler.RunPassAsync(now + DeviceRemovalScheduler.ReportDeadline + TimeSpan.FromSeconds(1), CancellationToken.None); + Assert.Equal(DeviceRemovalStates.Waiting, (await fixture.RemovalAsync(id)).State); + } + + [Fact] + public async Task WithMessagingDownNothingIsMarkedSent() + { + var (_, id) = await RemovedAsync("SEND-3"); + var sender = new RecordingRemovalSender { FailSend = true }; + var scheduler = fixture.RemovalScheduler(sender); + + Assert.Equal(0, (await scheduler.RunPassAsync(DateTimeOffset.UtcNow, CancellationToken.None)).Sent); + var row = await fixture.RemovalAsync(id); + Assert.Equal(DeviceRemovalStates.Waiting, row.State); + Assert.Equal(0, row.Attempts); + Assert.Null(row.SentUtc); + + sender.FailSend = false; + Assert.Equal(1, (await scheduler.RunPassAsync(DateTimeOffset.UtcNow, CancellationToken.None)).Sent); + } + + [Theory] + [InlineData("1.0.9")] + [InlineData("2026.10.23.1400")] // The calendar era sorts before every semantic version. + public async Task AnAgentTooOldIsNeverSentTheCommandAndStaysOpen(string version) + { + var (clientId, id) = await RemovedAsync("OLD-1", version); + var sender = new RecordingRemovalSender(); + var scheduler = fixture.RemovalScheduler(sender); + + await scheduler.RunPassAsync(DateTimeOffset.UtcNow, CancellationToken.None); + Assert.Empty(sender.Sent); + var row = await fixture.RemovalAsync(id); + Assert.Equal(DeviceRemovalStates.Waiting, row.State); + Assert.Equal(DeviceRemovalReasons.AgentTooOld, row.Reason); + + // Upgraded by hand: the command goes out and the reason clears. + await fixture.SightAsync(clientId, DeviceRemovalDefaults.MinimumAgentVersion); + await scheduler.RunPassAsync(DateTimeOffset.UtcNow, CancellationToken.None); + Assert.Single(sender.Sent); + row = await fixture.RemovalAsync(id); + Assert.Equal(DeviceRemovalStates.Sent, row.State); + Assert.Null(row.Reason); + } + + [Fact] + public async Task EventsAreCheckedAgainstTheirDevice() + { + var (clientId, id) = await RemovedAsync("EVENT-1"); + + Assert.False(await fixture.RemovalEventAsync(id, clientId, DeviceRemovalStates.Uninstalling, subject: "someone-else")); + Assert.False(await fixture.RemovalEventAsync(id, "someone-else", DeviceRemovalStates.Uninstalling)); + Assert.False(await fixture.RemovalEventAsync(Guid.NewGuid(), clientId, DeviceRemovalStates.Uninstalling)); + Assert.False(await fixture.RemovalEventAsync(id, clientId, DeviceRemovalStates.Removed)); + Assert.Equal(DeviceRemovalStates.Waiting, (await fixture.RemovalAsync(id)).State); + + Assert.True(await fixture.RemovalEventAsync(id, clientId, DeviceRemovalStates.Failed, new string('x', 100))); + var failed = await fixture.RemovalAsync(id); + Assert.Equal(DeviceRemovalStates.Failed, failed.State); + Assert.Equal(32, failed.Reason!.Length); + Assert.Equal("detail", failed.Detail); + + Assert.True(await fixture.RemovalEventAsync(id, clientId, DeviceRemovalStates.Uninstalling)); + var uninstalling = await fixture.RemovalAsync(id); + Assert.Equal(DeviceRemovalStates.Uninstalling, uninstalling.State); + Assert.Null(uninstalling.Reason); + } + + [Fact] + public async Task SilenceAfterUninstallingClosesItAndClearsItsCommands() + { + var (clientId, id) = await RemovedAsync("GONE-1"); + await fixture.RemovalEventAsync(id, clientId, DeviceRemovalStates.Uninstalling); + var said = (await fixture.RemovalAsync(id)).UpdatedUtc; + var sender = new RecordingRemovalSender(); + var scheduler = fixture.RemovalScheduler(sender); + + Assert.Equal(0, (await scheduler.RunPassAsync(said + DeviceRemovalDefaults.UninstallSilence, CancellationToken.None)).Finished); + Assert.Empty(sender.Cleared); + + // Messaging down: it stays open until nothing can replay. + var later = said + DeviceRemovalDefaults.UninstallSilence + TimeSpan.FromSeconds(1); + sender.FailClear = true; + Assert.Equal(0, (await scheduler.RunPassAsync(later, CancellationToken.None)).Finished); + Assert.Equal(DeviceRemovalStates.Uninstalling, (await fixture.RemovalAsync(id)).State); + + sender.FailClear = false; + Assert.Equal(1, (await scheduler.RunPassAsync(later, CancellationToken.None)).Finished); + Assert.Equal([clientId], sender.Cleared); + var row = await fixture.RemovalAsync(id); + Assert.Equal(DeviceRemovalStates.Removed, row.State); + Assert.Equal(later, row.FinishedUtc); + + var history = await fixture.HistoryAsync(clientId); + Assert.Contains(history, e => e.Action == Backend.Api.DeviceHistory.Removed); + } + + [Fact] + public async Task AnAgentStillTalkingAfterSayingItWasUninstallingGoesBackToWaiting() + { + var (clientId, id) = await RemovedAsync("ALIVE-1"); + await fixture.RemovalEventAsync(id, clientId, DeviceRemovalStates.Uninstalling); + var said = (await fixture.RemovalAsync(id)).UpdatedUtc; + var scheduler = fixture.RemovalScheduler(new RecordingRemovalSender()); + + // A sighting inside the window only delays the close. + await fixture.EditRemovalAsync(id, r => r.LastSeenUtc = said + TimeSpan.FromMinutes(10)); + Assert.Equal(0, (await scheduler.RunPassAsync(said + TimeSpan.FromMinutes(20), CancellationToken.None)).Finished); + Assert.Equal(DeviceRemovalStates.Uninstalling, (await fixture.RemovalAsync(id)).State); + + // One past it means the agent did not go. + await fixture.EditRemovalAsync(id, r => r.LastSeenUtc = said + DeviceRemovalDefaults.UninstallSilence + TimeSpan.FromSeconds(1)); + await scheduler.RunPassAsync(said + TimeSpan.FromMinutes(16), CancellationToken.None); + Assert.Equal(DeviceRemovalStates.Waiting, (await fixture.RemovalAsync(id)).State); + } + + [Fact] + public async Task ASightingNeverBringsTheDeviceRowBack() + { + var (clientId, id) = await RemovedAsync("GHOST-1", online: false); + var before = await fixture.RemovalAsync(id); + + Assert.False(await fixture.SightAsync(clientId, "9.9.9", subject: "someone-else")); + Assert.Equal("1.1.0", (await fixture.RemovalAsync(id)).AgentVersion); + + Assert.True(await fixture.SightAsync(clientId, "1.3.0")); + Assert.False(await fixture.WithDbAsync(db => db.Devices.AnyAsync(d => d.ClientId == clientId))); + var seen = await fixture.RemovalAsync(id); + Assert.Equal("1.3.0", seen.AgentVersion); + Assert.True(seen.LastSeenUtc > before.LastSeenUtc); + Assert.Equal(before.UpdatedUtc, seen.UpdatedUtc); // Sightings do not keep it in the device list. + + // A sighting without a version (inventory, ready) keeps the one known. + Assert.True(await fixture.SightAsync(clientId)); + Assert.Equal("1.3.0", (await fixture.RemovalAsync(id)).AgentVersion); + + // Nothing is waiting for a device nobody removed. + Assert.False(await fixture.SightAsync("never-enrolled-device", "1.3.0")); + } + + [Fact] + public async Task AnAgentBackAfterItsRemovalClosedIsChasedAgain() + { + var (clientId, id) = await RemovedAsync("BACK-1"); + await fixture.RemovalEventAsync(id, clientId, DeviceRemovalStates.Uninstalling); + var said = (await fixture.RemovalAsync(id)).UpdatedUtc; + var sender = new RecordingRemovalSender(); + var scheduler = fixture.RemovalScheduler(sender); + await scheduler.RunPassAsync(said + DeviceRemovalDefaults.UninstallSilence + TimeSpan.FromSeconds(1), CancellationToken.None); + Assert.Equal(DeviceRemovalStates.Removed, (await fixture.RemovalAsync(id)).State); + + Assert.True(await fixture.SightAsync(clientId, "1.1.0")); + var row = await fixture.RemovalAsync(id); + Assert.Equal(DeviceRemovalStates.Waiting, row.State); + Assert.Null(row.FinishedUtc); + + Assert.Equal(1, (await scheduler.RunPassAsync(DateTimeOffset.UtcNow, CancellationToken.None)).Sent); + } +} + +/// Renewal and enrollment answering to a removal, and the credential lifetime. +public class DeviceRemovalEnrollmentTests(BackendTestFixture fixture) : IClassFixture +{ + private async Task CreateTokenAsync(DateTimeOffset? created = null) + { + var token = TokenGenerator.CreateUrlSafeToken(32); + await fixture.WithDbAsync(async db => + { + db.BootstrapTokens.Add(new BootstrapTokenEntity + { + Id = Guid.NewGuid(), + TokenHash = TokenHashing.Sha256Hex(token), + CreatedUtc = created ?? DateTimeOffset.UtcNow, + ExpiresUtc = DateTimeOffset.UtcNow.AddDays(1), + MaxUses = 0, + }); + await db.SaveChangesAsync(); + }); + return token; + } + + private static Task BootstrapAsync(HttpClient client, string token, string clientId, KeyPair key, string version) + => client.PostAsJsonAsync(ApiRoutes.Bootstrap, + new BootstrapRequest(token, clientId, key.GetPublicKey(), version, new EnrollDeviceInfo("ENROLLED", "Dell", "OptiPlex", "Windows 11", "10.0")), + JsonDefaults.Options); + + private static Task RefreshAsync(HttpClient client, string clientId, KeyPair key, string? publicKey = null) + { + var timestamp = DateTimeOffset.UtcNow; + var signature = new byte[64]; + key.Sign(Encoding.UTF8.GetBytes($"{clientId}|{timestamp:O}"), signature); + return client.PostAsJsonAsync(ApiRoutes.RefreshCredential, + new CredentialRefreshRequest(clientId, publicKey ?? key.GetPublicKey(), timestamp, Convert.ToBase64String(signature)), + JsonDefaults.Options); + } + + private static string NewClientId() => Guid.NewGuid().ToString("N"); + + private async Task<(string ClientId, KeyPair Key, Guid RemovalId)> EnrollAndRemoveAsync(HttpClient anonymous) + { + var clientId = NewClientId(); + var key = KeyPair.CreatePair(PrefixByte.User); + (await BootstrapAsync(anonymous, await CreateTokenAsync(), clientId, key, "1.1.0")).EnsureSuccessStatusCode(); + using var admin = await fixture.SignedInAsync("enrollment-admin"); + return (clientId, key, (await admin.RemoveAsync(clientId)).Removals[0].Id); + } + + [Fact] + public async Task RenewalAnswers410OnlyToTheHolderOfTheEnrolledKey() + { + using var client = fixture.CreateClient(); + var (clientId, key, id) = await EnrollAndRemoveAsync(client); + using var _ = key; + + // Forged: as unknown as a device that never existed, with nothing changed. + using var other = KeyPair.CreatePair(PrefixByte.User); + var forged = await RefreshAsync(client, clientId, other, key.GetPublicKey()); + var wrongKey = await RefreshAsync(client, clientId, other); + var unknown = await RefreshAsync(client, NewClientId(), other); + foreach (var refused in new[] { forged, wrongKey, unknown }) + { + Assert.Equal(HttpStatusCode.Unauthorized, refused.StatusCode); + Assert.Empty(await refused.Content.ReadAsStringAsync()); + } + + Assert.Equal(DeviceRemovalStates.Waiting, (await fixture.RemovalAsync(id)).State); + + var gone = await RefreshAsync(client, clientId, key); + Assert.Equal(HttpStatusCode.Gone, gone.StatusCode); + using (var problem = JsonDocument.Parse(await gone.Content.ReadAsStringAsync())) + { + Assert.Equal(AgentCredentialDefaults.RemovedProblemType, problem.RootElement.GetProperty("type").GetString()); + } + + var row = await fixture.RemovalAsync(id); + Assert.Equal(DeviceRemovalStates.Uninstalling, row.State); + Assert.NotNull(row.LastSeenUtc); + } + + [Fact] + public async Task AClosedRemovalAnswers410AndAStoppedOneA401() + { + using var client = fixture.CreateClient(); + var (clientId, key, id) = await EnrollAndRemoveAsync(client); + using var _ = key; + + await fixture.EditRemovalAsync(id, r => r.State = DeviceRemovalStates.Removed); + Assert.Equal(HttpStatusCode.Gone, (await RefreshAsync(client, clientId, key)).StatusCode); + Assert.Equal(DeviceRemovalStates.Removed, (await fixture.RemovalAsync(id)).State); + + await fixture.EditRemovalAsync(id, r => r.State = DeviceRemovalStates.Stopped); + var stopped = await RefreshAsync(client, clientId, key); + Assert.Equal(HttpStatusCode.Unauthorized, stopped.StatusCode); + Assert.Empty(await stopped.Content.ReadAsStringAsync()); + Assert.Equal(DeviceRemovalStates.Stopped, (await fixture.RemovalAsync(id)).State); + } + + [Fact] + public async Task ATokenFromBeforeTheRemovalIsRefused() + { + using var client = fixture.CreateClient(); + var oldToken = await CreateTokenAsync(DateTimeOffset.UtcNow.AddHours(-1)); + var (clientId, key, id) = await EnrollAndRemoveAsync(client); + using var _ = key; + var requested = (await fixture.RemovalAsync(id)).RequestedUtc; + var sameInstant = await CreateTokenAsync(requested); + + using var fresh = KeyPair.CreatePair(PrefixByte.User); + foreach (var token in new[] { oldToken, sameInstant }) + { + var response = await BootstrapAsync(client, token, clientId, fresh, "1.1.0"); + Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + Assert.Empty(await response.Content.ReadAsStringAsync()); + } + + Assert.False(await fixture.WithDbAsync(db => db.Devices.AnyAsync(d => d.ClientId == clientId))); + Assert.True(await fixture.WithDbAsync(db => db.AuditEvents.AnyAsync( + e => e.Action == "bootstrap-rejected" && e.ClientId == clientId && e.Detail!.Contains("removal")))); + Assert.Equal(DeviceRemovalStates.Waiting, (await fixture.RemovalAsync(id)).State); + + // Other devices are not affected. + using var other = KeyPair.CreatePair(PrefixByte.User); + Assert.Equal(HttpStatusCode.OK, (await BootstrapAsync(client, oldToken, NewClientId(), other, "1.1.0")).StatusCode); + } + + [Fact] + public async Task ATokenFromAfterTheRemovalEnrollsAgainOnAFreshRow() + { + var sender = new RecordingRemovalSender(); + using var factory = fixture.WithWebHostBuilder(b => b.ConfigureTestServices( + s => s.AddSingleton(sender))); + using var client = factory.CreateClient(); + + var (clientId, key, id) = await EnrollAndRemoveAsync(client); + using var _ = key; + var oldEnrollment = (await fixture.RemovalAsync(id)).EnrollmentId; + + // Messaging down: refused, so nothing old can replay. + sender.FailClear = true; + using var fresh = KeyPair.CreatePair(PrefixByte.User); + Assert.Equal(HttpStatusCode.ServiceUnavailable, (await BootstrapAsync(client, await CreateTokenAsync(), clientId, fresh, "1.1.0")).StatusCode); + Assert.False(await fixture.WithDbAsync(db => db.Devices.AnyAsync(d => d.ClientId == clientId))); + sender.FailClear = false; + + var response = await BootstrapAsync(client, await CreateTokenAsync(), clientId, fresh, "1.1.0"); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal([clientId], sender.Cleared); + + var body = await response.ReadAsync(); + Assert.NotEqual(oldEnrollment, body.EnrollmentId); + var device = await fixture.WithDbAsync(db => db.Devices.AsNoTracking().SingleAsync(d => d.ClientId == clientId)); + Assert.Equal(fresh.GetPublicKey(), device.NkeyPublic); + + var row = await fixture.RemovalAsync(id); + Assert.Equal(DeviceRemovalStates.Removed, row.State); + Assert.NotNull(row.FinishedUtc); + + // Back in service: the old removal no longer answers renewals, and a sighting is the device's own. + Assert.Equal(HttpStatusCode.OK, (await RefreshAsync(client, clientId, fresh)).StatusCode); + } + + [Theory] + [InlineData("1.1.0", true)] + [InlineData("1.1.0.5", true)] + [InlineData("1.2.0", true)] + [InlineData("1.0.9", false)] + [InlineData("2026.10.23.1400", false)] + [InlineData("0.0.0.0", false)] + public async Task BootstrapGivesAnExpiringCredentialOnlyToAgentsThatRenew(string version, bool expires) + { + using var client = fixture.CreateClient(); + using var key = KeyPair.CreatePair(PrefixByte.User); + var before = DateTimeOffset.UtcNow; + + var body = await (await BootstrapAsync(client, await CreateTokenAsync(), NewClientId(), key, version)).ReadAsync(); + var claims = NatsJwt.DecodeUserClaims(body.UserJwt); + + if (expires) + { + Assert.NotNull(body.CredentialExpiresUtc); + Assert.InRange(body.CredentialExpiresUtc!.Value, before + AgentCredentialDefaults.Lifetime, DateTimeOffset.UtcNow + AgentCredentialDefaults.Lifetime); + Assert.NotNull(claims.Expires); + } + else + { + Assert.Null(body.CredentialExpiresUtc); + Assert.Null(claims.Expires); + } + } + + [Fact] + public async Task RenewalAlwaysGivesAnExpiringCredential() + { + using var client = fixture.CreateClient(); + var clientId = NewClientId(); + using var key = KeyPair.CreatePair(PrefixByte.User); + (await BootstrapAsync(client, await CreateTokenAsync(), clientId, key, "1.1.0")).EnsureSuccessStatusCode(); + var before = DateTimeOffset.UtcNow; + + var body = await (await RefreshAsync(client, clientId, key)).ReadAsync(); + + Assert.NotNull(body.CredentialExpiresUtc); + Assert.InRange(body.CredentialExpiresUtc!.Value, before + AgentCredentialDefaults.Lifetime, DateTimeOffset.UtcNow + AgentCredentialDefaults.Lifetime); + Assert.NotNull(NatsJwt.DecodeUserClaims(body.UserJwt).Expires); + } +} + +/// Agents that renew a credential never go to a version that cannot. +public class CredentialDowngradeTests(BackendTestFixture fixture) : IClassFixture +{ + private const string Below = "1.0.5"; + private const string Minimum = DeviceRemovalDefaults.MinimumAgentVersion; + private const string Above = "1.2.0"; + + [Theory] + [InlineData(Above, Below, true)] + [InlineData(Minimum, Below, true)] + [InlineData(Above, "2026.10.23.1400", true)] + [InlineData(Above, Minimum, false)] + [InlineData(Minimum, Above, false)] + [InlineData(Below, Minimum, false)] + [InlineData(Below, "1.0.1", false)] + public void TheRuleIsAboutCrossingTheMinimum(string running, string target, bool downgrade) + => Assert.Equal(downgrade, AgentUpgradeService.IsCredentialDowngrade(running, target)); + + [Fact] + public async Task AnUpgradeRequestLeavesOutADeviceThatWouldLoseItsCredential() + { + await fixture.ResetAsync(); + await fixture.SeedPackageAsync(Below); + await fixture.SeedPackageAsync(Minimum); + await fixture.SeedPackageAsync(Above); + var newer = await fixture.SeedAgentAsync("GUARD-NEW", Above); + var older = await fixture.SeedAgentAsync("GUARD-OLD", "1.0.1"); + using var admin = await fixture.SignedInAsync("guard-admin"); + + var toBelow = await (await admin.PostAsJsonAsync(ApiRoutes.AgentUpgrades, + new StartAgentUpgradeRequest([newer, older], null, Below), JsonDefaults.Options)).ReadAsync(); + Assert.Equal(older, Assert.Single(toBelow.Upgrades).ClientId); + var skip = Assert.Single(toBelow.Skipped); + Assert.Equal(newer, skip.ClientId); + Assert.Equal(AgentUpgradeService.SkipCredentialDowngrade, skip.Reason); + + // Exactly the minimum still renews. + var toMinimum = await (await admin.PostAsJsonAsync(ApiRoutes.AgentUpgrades, + new StartAgentUpgradeRequest([newer], null, Minimum), JsonDefaults.Options)).ReadAsync(); + Assert.Equal(newer, Assert.Single(toMinimum.Upgrades).ClientId); + } + + [Fact] + public async Task ARollBackLeavesOutADeviceThatWouldLoseItsCredential() + { + await fixture.ResetAsync(); + await fixture.SeedPackageAsync(Below); + await fixture.SeedPackageAsync(Minimum); + await fixture.SeedPackageAsync(Above); + var across = await fixture.SeedAgentAsync("ROLL-ACROSS", Minimum); + await fixture.SeedUpgradeAsync(across, Below, Minimum, AgentUpgradeStates.Succeeded); + var within = await fixture.SeedAgentAsync("ROLL-WITHIN", Above); + await fixture.SeedUpgradeAsync(within, Minimum, Above, AgentUpgradeStates.Succeeded); + using var admin = await fixture.SignedInAsync("guard-admin"); + + var batch = await (await admin.PostAsJsonAsync(ApiRoutes.AgentUpgradesRollback, + new RollBackAgentRequest([across, within]), JsonDefaults.Options)).ReadAsync(); + + Assert.Equal(within, Assert.Single(batch.Upgrades).ClientId); + var skip = Assert.Single(batch.Skipped); + Assert.Equal(across, skip.ClientId); + Assert.Equal(AgentUpgradeService.SkipCredentialDowngrade, skip.Reason); + } + + [Fact] + public async Task TheSchedulerClosesARowThatWouldNowLoseTheCredential() + { + await fixture.ResetAsync(); + var package = await fixture.SeedPackageAsync(Below); + var device = await fixture.SeedAgentAsync("GUARD-QUEUED", Above); + var row = await fixture.SeedUpgradeAsync(device, "1.0.1", Below, AgentUpgradeStates.Queued, packageId: package.Id); + var sender = new RecordingUpgradeSender(); + + await fixture.Scheduler(sender).RunPassAsync(DateTimeOffset.UtcNow, CancellationToken.None); + + Assert.Empty(sender.Sent); + var closed = await fixture.UpgradeAsync(row.Id); + Assert.Equal(AgentUpgradeStates.Cancelled, closed.State); + Assert.Equal(AgentUpgradeService.SkipCredentialDowngrade, closed.Reason); + } +} -- 2.52.0 From 70a3931361465d459a6e2a4c21f64018528b69e0b99a4e0fe67c408c0f35be42 Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Sat, 31 Oct 2026 19:57:42 -0400 Subject: [PATCH 06/20] feat(console): remove agents, and show removals in the Devices list Remove Agent(s) on the device menus and Remove Agents on groups, with a confirm dialog and a See Why result. Recent removals sit dimmed in the plain list with their state and Stop Trying; a Removals status filter shows them all. A removal's row opens the history the device left behind. Co-Authored-By: Claude Opus 5.5 --- src/WebUI/src/api/client.ts | 8 + src/WebUI/src/api/types.ts | 42 +++ src/WebUI/src/components/DataTable.tsx | 5 +- src/WebUI/src/pages/devices/detail.tsx | 2 +- src/WebUI/src/pages/devices/index.tsx | 280 ++++++++++++++---- .../src/sections/devices/DeviceHistory.tsx | 7 +- .../src/sections/devices/RemovalStateChip.tsx | 40 +++ .../src/sections/devices/removalLabels.ts | 45 +++ .../src/sections/devices/useDeviceActions.tsx | 9 +- .../sections/devices/useDeviceRemovals.tsx | 175 +++++++++++ .../src/sections/groups/useGroupActions.tsx | 15 + .../src/sections/upgrades/upgradeLabels.ts | 6 +- src/WebUI/src/utils/deviceActions.tsx | 14 +- 13 files changed, 576 insertions(+), 72 deletions(-) create mode 100644 src/WebUI/src/sections/devices/RemovalStateChip.tsx create mode 100644 src/WebUI/src/sections/devices/removalLabels.ts create mode 100644 src/WebUI/src/sections/devices/useDeviceRemovals.tsx diff --git a/src/WebUI/src/api/client.ts b/src/WebUI/src/api/client.ts index 0b9bb70..f38abdb 100644 --- a/src/WebUI/src/api/client.ts +++ b/src/WebUI/src/api/client.ts @@ -10,6 +10,8 @@ import type { DashboardDto, DeviceDetailDto, DeviceDto, + DeviceRemovalBatchDto, + DeviceRemovalDto, DeviceGroupDto, ExecutionDetailDto, ExecutionSummaryDto, @@ -37,6 +39,7 @@ import type { SecretsProviderDto, SecretsProvidersResponse, SecretsTestResultDto, + RemoveDevicesBody, ServerInfoDto, StartAgentUpgradeBody, TriggerRunResultDto @@ -206,6 +209,9 @@ const enc = encodeURIComponent; const upgradeQuery = ({ clientId, open, limit }: AgentUpgradeQuery) => qs({ clientId, open: open ? 'true' : undefined, limit }); +/** Every removal the page may show (the API's upper limit): the Devices list and its Removals filter share this read. */ +export const removalsKey = '/api/device-removals?limit=5000'; + /** Every REST call the console makes. Reads also have SWR keys (see `keys`). */ export const api = { // Devices @@ -227,6 +233,8 @@ export const api = { rollBackAgent: (body: RollBackAgentBody) => request('/api/agent-upgrades/rollback', json('POST', body)), /** Only an upgrade still queued can be cancelled. */ cancelAgentUpgrade: (id: string) => request(`/api/agent-upgrades/${enc(id)}/cancel`, { method: 'POST' }), + removeDevices: (body: RemoveDevicesBody) => request('/api/device-removals', json('POST', body)), + stopDeviceRemoval: (id: string) => request(`/api/device-removals/${enc(id)}/stop`, { method: 'POST' }), agentUpgradeSettings: () => request('/api/settings/agent-upgrades'), saveAgentUpgradeSettings: (body: AgentUpgradeSettingsDto) => request('/api/settings/agent-upgrades', json('PUT', body)), diff --git a/src/WebUI/src/api/types.ts b/src/WebUI/src/api/types.ts index c265d50..58c45a5 100644 --- a/src/WebUI/src/api/types.ts +++ b/src/WebUI/src/api/types.ts @@ -230,6 +230,48 @@ export interface AgentUpgradeBatchDto { skipped: AgentUpgradeSkipDto[]; } +export type DeviceRemovalState = 'waiting' | 'sent' | 'uninstalling' | 'removed' | 'failed' | 'stopped'; + +/** One device's removal; it stands in for the device row, and stays as the history. */ +export interface DeviceRemovalDto { + id: string; + clientId: string; + hostname?: string | null; + osCaption?: string | null; + agentVersion?: string | null; + state: DeviceRemovalState; + reason?: string | null; + detail?: string | null; + attempts: number; + requestedBy?: string | null; + note?: string | null; + requestedUtc: string; + updatedUtc: string; + sentUtc?: string | null; + lastSeenUtc?: string | null; + finishedUtc?: string | null; + showInDeviceList: boolean; +} + +export interface RemoveDevicesBody { + clientIds?: string[]; + groupIds?: string[]; + /** The operator's note. */ + reason?: string | null; +} + +/** A device a request left out: unknown or out-of-scope. */ +export interface DeviceRemovalSkipDto { + clientId: string; + hostname?: string | null; + reason: string; +} + +export interface DeviceRemovalBatchDto { + removals: DeviceRemovalDto[]; + skipped: DeviceRemovalSkipDto[]; +} + export interface AgentUpgradeSettingsDto { automaticUpgrades: boolean; /** Queued online devices start downloading within this many minutes (1–30), in paced batches. */ diff --git a/src/WebUI/src/components/DataTable.tsx b/src/WebUI/src/components/DataTable.tsx index 0c8d897..4b320eb 100644 --- a/src/WebUI/src/components/DataTable.tsx +++ b/src/WebUI/src/components/DataTable.tsx @@ -60,6 +60,8 @@ interface Props { rowLabel?: (row: T) => string; /** Marks the row currently open in a side panel. */ activeRowId?: string; + /** Dims rows that only stand in for something (not selectable, fewer actions). */ + dimmedRow?: (row: T) => boolean; minWidth?: number; } @@ -93,6 +95,7 @@ export default function DataTable({ dense = true, rowLabel, activeRowId, + dimmedRow, minWidth = 720 }: Props) { const [sort, setSort] = useState(defaultSort); @@ -207,7 +210,7 @@ export default function DataTable({ hover selected={isChosen || activeRowId === id} onClick={onRowClick ? () => onRowClick(row) : undefined} - sx={{ cursor: onRowClick ? 'pointer' : 'default' }} + sx={{ cursor: onRowClick ? 'pointer' : 'default', ...(dimmedRow?.(row) && { '& > td': { opacity: 0.65 } }) }} > {selection && ( e.stopPropagation()}> diff --git a/src/WebUI/src/pages/devices/detail.tsx b/src/WebUI/src/pages/devices/detail.tsx index 300bf05..405b186 100644 --- a/src/WebUI/src/pages/devices/detail.tsx +++ b/src/WebUI/src/pages/devices/detail.tsx @@ -71,7 +71,7 @@ export default function DeviceDetailPage() { () => mutate(), (event) => event.clientId === clientId ); - const actions = useDeviceActions({ onDone: () => void mutate() }); + const actions = useDeviceActions({ onDone: () => void mutate(), onRemoved: () => navigate('/devices') }); const tab = (TABS.find((t) => t.id === params.get('tab'))?.id ?? 'overview') as TabId; const device = detail?.device; diff --git a/src/WebUI/src/pages/devices/index.tsx b/src/WebUI/src/pages/devices/index.tsx index dca19b5..356ef54 100644 --- a/src/WebUI/src/pages/devices/index.tsx +++ b/src/WebUI/src/pages/devices/index.tsx @@ -5,6 +5,10 @@ import { Link as RouterLink, useNavigate, useSearchParams } from 'react-router-d import Box from '@mui/material/Box'; import Button from '@mui/material/Button'; import Chip from '@mui/material/Chip'; +import Dialog from '@mui/material/Dialog'; +import DialogActions from '@mui/material/DialogActions'; +import DialogContent from '@mui/material/DialogContent'; +import DialogTitle from '@mui/material/DialogTitle'; import FormControlLabel from '@mui/material/FormControlLabel'; import Grid from '@mui/material/Grid'; import InputAdornment from '@mui/material/InputAdornment'; @@ -29,7 +33,11 @@ import OsIcon, { isWinPe, platformOf, WinPeChip } from 'components/OsIcon'; import PageHeader from 'components/PageHeader'; import { DeviceStatusChip } from 'components/StatusChip'; import AddDeviceDialog from 'sections/devices/AddDeviceDialog'; +import DeviceHistory from 'sections/devices/DeviceHistory'; +import { isOpenRemoval, isRemovalActivity } from 'sections/devices/removalLabels'; +import RemovalStateChip from 'sections/devices/RemovalStateChip'; import useDeviceActions from 'sections/devices/useDeviceActions'; +import useDeviceRemovals from 'sections/devices/useDeviceRemovals'; import GroupTreePanel, { ALL_NODE, UNGROUPED_NODE } from 'sections/groups/GroupTreePanel'; import useGroupActions from 'sections/groups/useGroupActions'; import { isUpgradeActivity, newestFor, versionSortKey } from 'sections/upgrades/agentVersions'; @@ -38,10 +46,10 @@ import useApi from 'hooks/useApi'; import useAutomation from 'hooks/useAutomation'; import useLiveRefresh from 'hooks/useLiveRefresh'; import useSession, { useGate } from 'hooks/useSession'; -import { keys } from 'api/client'; +import { keys, removalsKey } from 'api/client'; import { plural } from 'utils/format'; import { ancestors, devicesInGroup, ungroupedDevices } from 'utils/groupTree'; -import type { AgentUpgradeDto, AgentVersionDto, DeviceDto, DeviceGroupDto } from 'api/types'; +import type { AgentUpgradeDto, AgentVersionDto, DeviceDto, DeviceGroupDto, DeviceRemovalDto } from 'api/types'; // assets import CalendarOutlined from '@ant-design/icons/CalendarOutlined'; @@ -51,13 +59,33 @@ import PlayCircleOutlined from '@ant-design/icons/PlayCircleOutlined'; import PlusOutlined from '@ant-design/icons/PlusOutlined'; import SearchOutlined from '@ant-design/icons/SearchOutlined'; -type StatusFilter = 'all' | 'online' | 'stale' | 'offline' | 'orphaned'; +type StatusFilter = 'all' | 'online' | 'stale' | 'offline' | 'orphaned' | 'removals'; + +/** A row of the table: a device, or a removal standing in for one. */ +type ListRow = DeviceDto & { removal?: DeviceRemovalDto }; + +const removalRow = (removal: DeviceRemovalDto): ListRow => ({ + clientId: removal.clientId, + hostname: removal.hostname ?? removal.clientId, + osCaption: removal.osCaption ?? undefined, + agentVersion: removal.agentVersion ?? undefined, + ipAddresses: [], + status: 'offline', + isOrphaned: false, + lastSeenUtc: removal.lastSeenUtc ?? undefined, + enrolledUtc: removal.requestedUtc, + removal +}); + +const listRowId = (row: ListRow) => (row.removal ? `removal:${row.removal.id}` : row.clientId); const KIND_LABEL = { static: 'Static Group', dynamic: 'Dynamic Group', folder: 'Folder' } as const; -function matches(device: DeviceDto, query: string): boolean { +function matches(device: ListRow, query: string): boolean { return [ device.hostname, + device.removal?.requestedBy, + device.removal?.note, device.clientId, device.manufacturer, device.model, @@ -92,12 +120,14 @@ export default function DevicesPage() { refreshInterval: 60_000 }); const openByDevice = useMemo(() => new Map((openUpgrades ?? []).map((u) => [u.clientId, u])), [openUpgrades]); + const { data: removals, mutate: refreshRemovals } = useApi(removalsKey, { refreshInterval: 60_000 }); // Presence and inventory changes arrive as activity carrying a client id. useLiveRefresh( () => Promise.all([refreshDevices(), refreshGroups()]), (event) => Boolean(event.clientId) ); useLiveRefresh(() => refreshUpgrades(), isUpgradeActivity); + useLiveRefresh(() => Promise.all([refreshRemovals(), refreshDevices(), refreshGroups()]), isRemovalActivity); const selectedId = params.get('group') ?? ALL_NODE; const search = params.get('q') ?? ''; @@ -120,6 +150,7 @@ export default function DevicesPage() { const [menu, setMenu] = useState<{ anchor: HTMLElement; devices: DeviceDto[] } | null>(null); const [groupMenu, setGroupMenu] = useState(null); const [addOpen, setAddOpen] = useState(false); + const [historyOf, setHistoryOf] = useState(null); const selectedGroup = groups?.find((g) => g.id === selectedId) ?? null; const groupActions = useGroupActions({ onDeleted: () => setParam('group', null) }); @@ -127,6 +158,9 @@ export default function DevicesPage() { staticGroup: selectedGroup?.kind === 'static' ? selectedGroup : null, onDone: () => setChosen(new Set()) }); + const removalActions = useDeviceRemovals(); + const canRemove = can('devices.remove'); + const showingRemovals = status === 'removals'; const inScope = useMemo(() => { const list = devices ?? []; @@ -135,33 +169,54 @@ export default function DevicesPage() { return devicesInGroup(list, groups ?? [], selectedId, nested); }, [devices, groups, selectedId, nested]); - const rows = useMemo(() => { + const rows = useMemo(() => { const query = search.trim().toLowerCase(); - return inScope.filter((d) => { - if (query && !matches(d, query)) return false; - if (status === 'orphaned' ? !d.isOrphaned : status !== 'all' && (d.status !== status || d.isOrphaned)) return false; - if (platform !== 'all' && platformOf(d.osCaption) !== platform) return false; - return true; - }); - }, [inScope, search, status, platform]); + const passes = (d: ListRow) => (!query || matches(d, query)) && (platform === 'all' || platformOf(d.osCaption) === platform); + if (showingRemovals) return (removals ?? []).map(removalRow).filter(passes); - const chosenRows = rows.filter((d) => chosen.has(d.clientId)); + const listed = inScope.filter((d) => { + if (status === 'orphaned' ? !d.isOrphaned : status !== 'all' && (d.status !== status || d.isOrphaned)) return false; + return passes(d); + }); + // Recent removals sit in the plain list; a removed device has no groups, and one enrolled again has its own row. + if (status !== 'all' || selectedId !== ALL_NODE) return listed; + const enrolled = new Set((devices ?? []).map((d) => d.clientId)); + return [ + ...listed, + ...(removals ?? []) + .filter((r) => r.showInDeviceList && !enrolled.has(r.clientId)) + .map(removalRow) + .filter(passes) + ]; + }, [devices, inScope, removals, search, showingRemovals, status, platform, selectedId]); + + const chosenRows = rows.filter((d) => !d.removal && chosen.has(d.clientId)); const groupName = (id: string) => groups?.find((g) => g.id === id)?.name ?? '…'; - const title = - selectedId === ALL_NODE ? 'All Devices' : selectedId === UNGROUPED_NODE ? 'Ungrouped Devices' : (selectedGroup?.name ?? 'Group'); + const title = showingRemovals + ? 'Removals' + : selectedId === ALL_NODE + ? 'All Devices' + : selectedId === UNGROUPED_NODE + ? 'Ungrouped Devices' + : (selectedGroup?.name ?? 'Group'); const trail = selectedGroup ? [...ancestors(groups ?? [], selectedGroup.id).reverse(), selectedGroup] : []; const online = inScope.filter((d) => d.status === 'online').length; - const columns: Column[] = [ - { - id: 'hostname', - header: 'Name', - sortValue: (d) => d.hostname, - cell: (d) => ( - - - + // A removal has no device page; its row opens the history it left behind. + const nameColumn: Column = { + id: 'hostname', + header: 'Name', + sortValue: (d) => d.hostname, + cell: (d) => ( + + + + {d.removal ? ( + + {d.hostname} + + ) : ( {d.hostname} - - {d.clientId} - - + )} + + {d.clientId} + + + ) + }; + + const stopTrying = (removal: DeviceRemovalDto) => ( + ) => { + e.stopPropagation(); + removalActions.stop(removal); + }} + > + Stop Trying + + ); + + const removalColumns: Column[] = [ + nameColumn, + { + id: 'state', + header: 'State', + sortValue: (d) => d.removal?.state, + cell: (d) => + }, + { id: 'requestedBy', header: 'Requested By', sortValue: (d) => d.removal?.requestedBy, cell: (d) => d.removal?.requestedBy ?? '—' }, + { + id: 'requested', + header: 'Requested', + nowrap: true, + sortValue: (d) => d.removal?.requestedUtc, + cell: (d) => + }, + { + id: 'finished', + header: 'Finished', + nowrap: true, + sortValue: (d) => d.removal?.finishedUtc, + cell: (d) => + }, + { + id: 'detail', + header: 'Detail', + cell: (d) => ( + + {[d.removal?.note, d.removal?.detail].filter(Boolean).join(' · ') || '—'} + ) }, + { + id: 'actions', + header: '', + align: 'right', + nowrap: true, + cell: (d) => (d.removal && isOpenRemoval(d.removal.state) ? stopTrying(d.removal) : null) + } + ]; + + const columns: Column[] = [ + nameColumn, { id: 'status', header: 'Status', - sortValue: (d) => (d.isOrphaned ? 'z' : d.status), - cell: (d) => + sortValue: (d) => d.removal?.state ?? (d.isOrphaned ? 'z' : d.status), + cell: (d) => (d.removal ? : ) }, { id: 'os', @@ -292,7 +406,7 @@ export default function DevicesPage() { cell: (d) => ( @@ -310,19 +424,24 @@ export default function DevicesPage() { header: '', width: 56, align: 'right', - cell: (d) => ( - ) => { - e.stopPropagation(); - setMenu({ anchor: e.currentTarget, devices: [d] }); - }} - > - - - ) + cell: (d) => + d.removal ? ( + isOpenRemoval(d.removal.state) ? ( + stopTrying(d.removal) + ) : null + ) : ( + ) => { + e.stopPropagation(); + setMenu({ anchor: e.currentTarget, devices: [d] }); + }} + > + + + ) } ]; @@ -386,6 +505,11 @@ export default function DevicesPage() { Stale Offline Orphaned + + + Removals + + Linux macOS - {selectedGroup && ( + {selectedGroup && !showingRemovals && ( setParam('nested', e.target.checked ? null : '0')} />} label={ @@ -473,16 +597,23 @@ export default function DevicesPage() { {title} - {selectedGroup && } + {selectedGroup && !showingRemovals && } - {plural(inScope.length, 'device')}, {online} online - {selectedGroup?.description ? ` · ${selectedGroup.description}` : ''} + {showingRemovals ? ( + plural(removals?.length ?? 0, 'removal') + ) : ( + <> + {plural(inScope.length, 'device')}, {online} online + {selectedGroup?.description ? ` · ${selectedGroup.description}` : ''} + + )} } secondary={ - selectedGroup && ( + selectedGroup && + !showingRemovals && ( d.clientId} + rowId={listRowId} loading={loading} refreshing={validating && !loading} error={error?.message} toolbar={toolbar} - defaultSort={{ id: 'hostname', direction: 'asc' }} + defaultSort={showingRemovals ? { id: 'requested', direction: 'desc' } : { id: 'hostname', direction: 'asc' }} rowLabel={(d) => d.hostname} - onRowClick={(d) => navigate(`/devices/${encodeURIComponent(d.clientId)}`)} + onRowClick={(d) => (d.removal ? setHistoryOf(d) : navigate(`/devices/${encodeURIComponent(d.clientId)}`))} minWidth={1100} - selection={{ - selected: chosen, - onChange: (ids, on) => - setChosen((current) => { - const next = new Set(current); - for (const id of ids) { - if (on) next.add(id); - else next.delete(id); + dimmedRow={(d) => !showingRemovals && Boolean(d.removal)} + selection={ + showingRemovals + ? undefined + : { + selected: chosen, + selectable: (d) => !d.removal, + onChange: (ids, on) => + setChosen((current) => { + const next = new Set(current); + for (const id of ids) { + if (on) next.add(id); + else next.delete(id); + } + return next; + }) } - return next; - }) - }} + } empty={ - (devices?.length ?? 0) === 0 ? ( + showingRemovals ? ( + + ) : (devices?.length ?? 0) === 0 ? ( setGroupMenu(null)} entries={groupMenuEntries} />} {deviceActions.elements} {groupActions.elements} + {removalActions.elements} + setHistoryOf(null)} maxWidth="md" fullWidth> + {historyOf?.hostname} + {historyOf && } + + + + setAddOpen(false)} /> diff --git a/src/WebUI/src/sections/devices/DeviceHistory.tsx b/src/WebUI/src/sections/devices/DeviceHistory.tsx index eb1933a..07b81a2 100644 --- a/src/WebUI/src/sections/devices/DeviceHistory.tsx +++ b/src/WebUI/src/sections/devices/DeviceHistory.tsx @@ -20,7 +20,10 @@ const EVENTS: Record = { 'device-os-changed': { label: 'OS Changed', color: 'primary' }, 'agent-upgraded': { label: 'Agent Upgraded', color: 'success' }, 'agent-rolled-back': { label: 'Agent Rolled Back', color: 'warning' }, - 'agent-upgrade-failed': { label: 'Agent Upgrade Failed', color: 'error' } + 'agent-upgrade-failed': { label: 'Agent Upgrade Failed', color: 'error' }, + 'device-removal-requested': { label: 'Removal Requested', color: 'warning' }, + 'device-removed': { label: 'Removed', color: 'secondary' }, + 'device-removal-stopped': { label: 'Removal Stopped', color: 'warning' } }; // ==============================|| DEVICE - HISTORY ||============================== // @@ -65,7 +68,7 @@ export default function DeviceHistory({ clientId }: { clientId: string }) { title={ <> Device History - + } > diff --git a/src/WebUI/src/sections/devices/RemovalStateChip.tsx b/src/WebUI/src/sections/devices/RemovalStateChip.tsx new file mode 100644 index 0000000..2c865d4 --- /dev/null +++ b/src/WebUI/src/sections/devices/RemovalStateChip.tsx @@ -0,0 +1,40 @@ +// material-ui +import Chip from '@mui/material/Chip'; +import Stack from '@mui/material/Stack'; +import Tooltip from '@mui/material/Tooltip'; +import Typography from '@mui/material/Typography'; + +// project imports +import Dot from 'components/@extended/Dot'; +import { REMOVAL_REASONS, REMOVAL_STATES } from './removalLabels'; +import type { DeviceRemovalDto } from 'api/types'; + +// ==============================|| REMOVAL STATE CHIP ||============================== // + +/** A removal's state, explaining itself on hover, and under it what stands in its way when something does. */ +export default function RemovalStateChip({ removal }: { removal: DeviceRemovalDto }) { + const known = REMOVAL_STATES[removal.state] ?? { label: removal.state, help: '', tone: 'secondary' as const }; + const why = removal.reason ? REMOVAL_REASONS[removal.reason] : undefined; + const chip = ( + } + sx={{ fontWeight: 500 }} + /> + ); + return ( + + {known.help ? {chip} : chip} + {removal.reason && ( + + + {why?.label ?? removal.reason} + + + )} + + ); +} diff --git a/src/WebUI/src/sections/devices/removalLabels.ts b/src/WebUI/src/sections/devices/removalLabels.ts new file mode 100644 index 0000000..2faedb5 --- /dev/null +++ b/src/WebUI/src/sections/devices/removalLabels.ts @@ -0,0 +1,45 @@ +// types +import type { ActivityEventPayload, DeviceRemovalState } from 'api/types'; + +type Tone = 'success' | 'warning' | 'error' | 'info' | 'primary' | 'secondary'; + +/** Label, hover text and tone per removal state (one map, so a state always reads the same). */ +export const REMOVAL_STATES: Record = { + waiting: { label: 'Waiting for Device', help: 'The agent uninstalls when the device next comes online.', tone: 'secondary' }, + sent: { label: 'Sent', help: 'The remove command was sent, waiting for the agent to answer.', tone: 'info' }, + uninstalling: { label: 'Uninstalling', help: 'The agent is uninstalling itself.', tone: 'primary' }, + removed: { label: 'Removed', help: 'The agent has gone.', tone: 'success' }, + failed: { + label: "Couldn't Uninstall", + help: 'The uninstall failed on the device; the server asks again while it is online.', + tone: 'error' + }, + stopped: { label: 'Stopped', help: 'Nobody is trying any more. The device stays blocked and its agent stays installed.', tone: 'warning' } +}; + +/** States still being worked on: only these can be stopped. */ +export const OPEN_REMOVAL_STATES: DeviceRemovalState[] = ['waiting', 'sent', 'uninstalling', 'failed']; + +export const isOpenRemoval = (state: DeviceRemovalState) => OPEN_REMOVAL_STATES.includes(state); + +/** Why a removal could not go ahead. */ +export const REMOVAL_REASONS: Record = { + 'agent-too-old': { + label: 'Uninstall It by Hand or Upgrade It First', + help: 'This agent is too old to take a remove command; the server keeps blocking it.' + }, + unsupported: { label: "Can't Remove Itself Here", help: 'A foreground or development run, or a container. Uninstall it by hand.' }, + busy: { label: 'Upgrade in Progress', help: 'An agent upgrade is under way; the server asks again.' }, + msi: { label: 'Windows Installer Failed', help: 'Windows Installer did not uninstall the agent; the server asks again.' }, + uninstall: { label: "Files Couldn't Be Removed", help: 'Files or services could not be removed; the server asks again.' }, + 'stale-enrollment': { label: 'Another Enrollment', help: 'The agent holds a different enrollment, so it ignored the command.' } +}; + +/** Why a request left a device out. */ +export const REMOVAL_SKIPS: Record = { + unknown: { label: 'Not Found', help: 'No such device; it may have been removed already.' }, + 'out-of-scope': { label: 'Outside Your Scope', help: 'Your roles do not reach this device.' } +}; + +/** The console refreshes devices and removals on this activity. */ +export const isRemovalActivity = (event: ActivityEventPayload) => event.kind === 'device-removal'; diff --git a/src/WebUI/src/sections/devices/useDeviceActions.tsx b/src/WebUI/src/sections/devices/useDeviceActions.tsx index 4384874..e76a454 100644 --- a/src/WebUI/src/sections/devices/useDeviceActions.tsx +++ b/src/WebUI/src/sections/devices/useDeviceActions.tsx @@ -15,6 +15,7 @@ import ConfirmDialog from 'components/ConfirmDialog'; import { MenuEntry } from 'components/ActionMenu'; import HoverHelp from 'components/HoverHelp'; import GroupEditor from 'sections/groups/GroupEditor'; +import useDeviceRemovals from 'sections/devices/useDeviceRemovals'; import useAgentUpgrades, { UpgradeTarget } from 'sections/upgrades/useAgentUpgrades'; import useApi from 'hooks/useApi'; import useAutomation from 'hooks/useAutomation'; @@ -38,8 +39,11 @@ const upgradeTarget = (devices: DeviceDto[]): UpgradeTarget => ({ /** * Everything the device "…" and bulk Actions menus can do. Render `elements` * once; `menuFor(devices)` builds the entries for one device or a selection. + * `onRemoved` runs after a removal (`onDone` when not given). */ -export default function useDeviceActions(options: { staticGroup?: DeviceGroupDto | null; onDone?: () => void } = {}) { +export default function useDeviceActions( + options: { staticGroup?: DeviceGroupDto | null; onDone?: () => void; onRemoved?: () => void } = {} +) { const navigate = useNavigate(); const { can } = useSession(); const { openRun, openSchedule } = useAutomation(); @@ -53,6 +57,7 @@ export default function useDeviceActions(options: { staticGroup?: DeviceGroupDto const [removing, setRemoving] = useState(null); const upgrades = useAgentUpgrades(); + const removals = useDeviceRemovals({ onRemoved: options.onRemoved ?? options.onDone }); const refresh = () => Promise.all([mutate(keys.devices), mutate(keys.groups)]); const staticGroups = flattenTree(buildGroupTree(groups ?? [])).filter((n) => n.group.kind === 'static'); @@ -89,6 +94,7 @@ export default function useDeviceActions(options: { staticGroup?: DeviceGroupDto refreshInventory: () => void refreshInventory(devices), upgradeAgent: () => upgrades.upgrade(upgradeTarget(devices)), rollBackAgent: () => upgrades.rollBack(upgradeTarget(devices)), + removeAgent: () => removals.remove(upgradeTarget(devices)), details: () => navigate(`/devices/${encodeURIComponent(devices[0].clientId)}`), history: () => navigate(`/jobs?clientId=${encodeURIComponent(devices[0].clientId)}`), copyId: () => @@ -104,6 +110,7 @@ export default function useDeviceActions(options: { staticGroup?: DeviceGroupDto const elements = ( <> {upgrades.elements} + {removals.elements} + 'device' in name + ? `Remove the Agent from ${name.device}?` + : 'devices' in name + ? `Remove the Agents from ${plural(name.devices, 'Device')}?` + : `Remove the Agents in “${name.group}”?`; + +const refresh = () => Promise.all([mutate(keys.devices), mutate(keys.groups), mutate(removalsKey)]); + +// ==============================|| DEVICE REMOVALS (DIALOGS) ||============================== // + +/** + * Remove Agent… and Stop Trying with their dialogs and result. Render `elements` + * once; `remove(target)` and `stop(removal)` open the dialogs. `onRemoved` runs + * when a request removed at least one device. + */ +export default function useDeviceRemovals(options: { onRemoved?: () => void } = {}) { + const [removing, setRemoving] = useState(null); + const [stopping, setStopping] = useState(null); + const [note, setNote] = useState(''); + const [skipped, setSkipped] = useState(null); + + const report = (result: DeviceRemovalBatchDto) => { + const done = result.removals.length; + const out = result.skipped.length; + const message = + done === 0 + ? `No Removal Requested; ${plural(out, 'Device')} Left Out` + : `Removal Requested for ${plural(done, 'Device')}${out > 0 ? `; ${out} Left Out` : ''}`; + enqueueSnackbar(message, { + variant: done === 0 ? 'warning' : out > 0 ? 'info' : 'success', + autoHideDuration: out > 0 ? 10_000 : undefined, + action: + out > 0 + ? (key) => ( + + ) + : undefined + }); + }; + + const remove = (target: RemovalTarget) => { + setNote(''); + setRemoving(target); + }; + + const elements = ( + <> + + Uninstalls the agent. + + + } + confirmLabel="Remove" + destructive + maxWidth="sm" + onClose={() => setRemoving(null)} + onConfirm={async () => { + const result = await api.removeDevices({ + clientIds: removing!.clientIds, + groupIds: removing!.groupIds, + reason: note.trim() || null + }); + await refresh(); + report(result); + if (result.removals.length > 0) options.onRemoved?.(); + }} + > + + Note + + + } + value={note} + onChange={(e) => setNote(e.target.value)} + slotProps={{ htmlInput: { maxLength: 500 } }} + /> + + + + It stays blocked. + + + } + confirmLabel="Stop Trying" + destructive + onClose={() => setStopping(null)} + onConfirm={async () => { + await api.stopDeviceRemoval(stopping!.id); + await mutate(removalsKey); + }} + /> + + setSkipped(null)} maxWidth="sm" fullWidth> + {plural(skipped?.length ?? 0, 'Device')} Left Out + + + {(skipped ?? []).map((s) => ( + + + {s.hostname ?? s.clientId} + + + + + + ))} + + + + + + + + ); + + return { remove, stop: setStopping as (removal: DeviceRemovalDto) => void, elements }; +} diff --git a/src/WebUI/src/sections/groups/useGroupActions.tsx b/src/WebUI/src/sections/groups/useGroupActions.tsx index e4ce945..7ea3f96 100644 --- a/src/WebUI/src/sections/groups/useGroupActions.tsx +++ b/src/WebUI/src/sections/groups/useGroupActions.tsx @@ -12,6 +12,7 @@ import { mutate } from 'swr'; import GroupEditor, { GroupPreset } from './GroupEditor'; import ConfirmDialog from 'components/ConfirmDialog'; import { MenuEntry } from 'components/ActionMenu'; +import useDeviceRemovals from 'sections/devices/useDeviceRemovals'; import useAgentUpgrades from 'sections/upgrades/useAgentUpgrades'; import useApi from 'hooks/useApi'; import useAutomation from 'hooks/useAutomation'; @@ -27,6 +28,7 @@ import CloudSyncOutlined from '@ant-design/icons/CloudSyncOutlined'; import CopyOutlined from '@ant-design/icons/CopyOutlined'; import DeleteOutlined from '@ant-design/icons/DeleteOutlined'; import DesktopOutlined from '@ant-design/icons/DesktopOutlined'; +import DisconnectOutlined from '@ant-design/icons/DisconnectOutlined'; import EditOutlined from '@ant-design/icons/EditOutlined'; import PlayCircleOutlined from '@ant-design/icons/PlayCircleOutlined'; import PlusOutlined from '@ant-design/icons/PlusOutlined'; @@ -49,6 +51,7 @@ export function groupMenu( viewDevices: () => void; upgradeAgents: () => void; rollBackAgents: () => void; + removeAgents: () => void; remove: () => void; } ): MenuEntry[] { @@ -91,6 +94,15 @@ export function groupMenu( disabledReason: manageDevices, onSelect: handlers.rollBackAgents }, + { + kind: 'item', + id: 'remove-agents', + label: 'Remove Agents…', + icon: , + destructive: true, + disabledReason: can('devices.remove') ? empty : NEEDS('Remove Devices'), + onSelect: handlers.removeAgents + }, { kind: 'divider' }, { kind: 'item', id: 'new', label: 'New Group Here…', icon: , disabledReason: manage, onSelect: handlers.newChild }, { kind: 'item', id: 'edit', label: 'Edit…', icon: , disabledReason: manage, onSelect: handlers.edit }, @@ -147,6 +159,7 @@ export default function useGroupActions( const [moving, setMoving] = useState<{ group: DeviceGroupDto; parent: DeviceGroupDto | null } | null>(null); const upgrades = useAgentUpgrades(); + const removals = useDeviceRemovals(); const refresh = () => Promise.all([mutate(keys.groups), mutate(keys.devices)]); @@ -197,6 +210,7 @@ export default function useGroupActions( viewDevices: () => navigate(`/devices?group=${group.id}`), upgradeAgents: () => upgrades.upgrade({ groupIds: [group.id], name: { group: groupPath(groups ?? [], group.id) } }), rollBackAgents: () => upgrades.rollBack({ groupIds: [group.id], name: { group: groupPath(groups ?? [], group.id) } }), + removeAgents: () => removals.remove({ groupIds: [group.id], name: { group: groupPath(groups ?? [], group.id) } }), remove: () => setDeleting(group) }); @@ -226,6 +240,7 @@ export default function useGroupActions( const elements = ( <> {upgrades.elements} + {removals.elements} setEditor(null)} diff --git a/src/WebUI/src/sections/upgrades/upgradeLabels.ts b/src/WebUI/src/sections/upgrades/upgradeLabels.ts index 85f10c3..a268e8c 100644 --- a/src/WebUI/src/sections/upgrades/upgradeLabels.ts +++ b/src/WebUI/src/sections/upgrades/upgradeLabels.ts @@ -57,7 +57,11 @@ export const UPGRADE_REASONS: Record = cancelled: { label: 'Cancelled', help: 'Cancelled before it was sent.' }, 'no-previous': { label: 'No Previous Version', help: 'No earlier upgrade to go back from.' }, expired: { label: 'Expired', help: 'Queued for 7 days without the device coming back.' }, - orphaned: { label: 'Orphaned Device', help: 'The device has been offline past the orphan window.' } + orphaned: { label: 'Orphaned Device', help: 'The device has been offline past the orphan window.' }, + 'credential-downgrade': { + label: 'Too Old to Renew Its Credential', + help: 'This version is older than the first agent that renews its credential, and a device past that would be cut off within a day.' + } }; export const reasonLabel = (reason?: string | null) => (reason ? (UPGRADE_REASONS[reason]?.label ?? reason) : undefined); diff --git a/src/WebUI/src/utils/deviceActions.tsx b/src/WebUI/src/utils/deviceActions.tsx index a8152ac..0a59844 100644 --- a/src/WebUI/src/utils/deviceActions.tsx +++ b/src/WebUI/src/utils/deviceActions.tsx @@ -9,6 +9,7 @@ import CalendarOutlined from '@ant-design/icons/CalendarOutlined'; import CloudSyncOutlined from '@ant-design/icons/CloudSyncOutlined'; import CodeOutlined from '@ant-design/icons/CodeOutlined'; import CopyOutlined from '@ant-design/icons/CopyOutlined'; +import DeleteOutlined from '@ant-design/icons/DeleteOutlined'; import EyeOutlined from '@ant-design/icons/EyeOutlined'; import HistoryOutlined from '@ant-design/icons/HistoryOutlined'; import MinusCircleOutlined from '@ant-design/icons/MinusCircleOutlined'; @@ -27,6 +28,7 @@ export interface DeviceActionHandlers { refreshInventory: () => void; upgradeAgent: () => void; rollBackAgent: () => void; + removeAgent: () => void; details: () => void; history: () => void; copyId: () => void; @@ -160,6 +162,16 @@ export function deviceActionMenu( }, { kind: 'item' as const, id: 'copy', label: 'Copy Client ID', icon: , onSelect: handlers.copyId } ] - : []) + : []), + { kind: 'divider' }, + { + kind: 'item', + id: 'remove-agent', + label: one ? 'Remove Agent…' : 'Remove Agents…', + icon: , + destructive: true, + disabledReason: can('devices.remove') ? undefined : needs('Remove Devices'), + onSelect: handlers.removeAgent + } ]; } -- 2.52.0 From 2f424feba860257c4aa9940a67d367647dde80a44212d10baa2e0e92953fdfe2 Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Sat, 31 Oct 2026 19:57:42 -0400 Subject: [PATCH 07/20] docs: removing devices Co-Authored-By: Claude Opus 5.5 --- README.md | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 90 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 22416dc..c99beeb 100644 --- a/README.md +++ b/README.md @@ -53,12 +53,13 @@ updates with **no SignalR, SSE, or polling**. 25. [Database providers](#database-providers) 26. [Logging](#logging) 27. [Device lifecycle: reconnect or orphan](#device-lifecycle-reconnect-or-orphan) -28. [Agent upgrades from the server](#agent-upgrades-from-the-server) -29. [S3 file delivery](#s3-file-delivery) -30. [CI/CD and GITEA_* variables](#cicd-and-gitea_-variables) -31. [Versioning](#versioning) -32. [Troubleshooting](#troubleshooting) -33. [Licence](#licence) +28. [Removing devices](#removing-devices) +29. [Agent upgrades from the server](#agent-upgrades-from-the-server) +30. [S3 file delivery](#s3-file-delivery) +31. [CI/CD and GITEA_* variables](#cicd-and-gitea_-variables) +32. [Versioning](#versioning) +33. [Troubleshooting](#troubleshooting) +34. [Licence](#licence) --- @@ -1460,6 +1461,89 @@ the device keeps its history, groups and tags. --- +## Removing devices + +**Remove Agent…** on a device (row menu, bulk Actions, device page) or **Remove +Agents…** on a group or folder (nested groups included) takes the device out of +the system at once and uninstalls its agent now, or whenever it next comes back. +It needs the `devices.remove` permission, which Admin has and Operator and Viewer +do not. Scoped roles reach only the devices in their scope. + +The device row goes in the same transaction as its group memberships and trigger +runs; schedules aimed at that device alone are disabled and jobs that never +started are cancelled. Finished jobs, their output and the device's history stay, +and old runs keep its name. What stands in for the row is a **removal**, kept +forever as the history. + +```mermaid +flowchart TD + R["Remove Agent: device row deleted, removal waiting"] --> S{"Seen online?"} + S -- "No, however long" --> S + S -- "Yes, agent 1.1.0 or later" --> C["Remove command sent (at most every 5 min)"] + S -- "Yes, older agent" --> O["Agent too old: stays open, stays blocked"] + C --> U["Agent reports uninstalling"] + C -- "It failed" --> F["Couldn't Uninstall: asked again"] + C -- "No answer for 30 min" --> S + F --> S + U -- "Silent for 15 min" --> D["Removed: consumer deleted, commands purged"] + U -- "Still reporting after 15 min" --> S + D -- "Reports again" --> S +``` + +**Online.** The next scheduler pass (every 15 seconds, on the node with +`SCHEDULE_WORKER` on) sends the command. Its enrollment id must match what the +agent holds, so a stale command can never uninstall a device enrolled again since. +After a successful uninstall nobody is left to report success, so 15 minutes of +silence after "uninstalling" counts as gone. + +**Offline.** A removal waits for as long as the device stays away. Presence, +inventory and ready notices from a client id with no device row only update the +removal; they never bring the row back. + +**Credentials.** Agents from 1.1.0 on hold a NATS credential that lasts 24 hours, +renewed over HTTPS at half life and at every start. A renewal for a removed device +answers `410` with problem type `urn:problem:device-removed`, and the agent +uninstalls itself, so a device that slips past the command is still cut off within +a day. Only the holder of the enrolled key learns this: a bad signature gets the +same plain `401` as an id nobody knows. Older agents never renew and could not +survive an expiring credential, so enrollment still gives them one that never +expires. To keep that true an agent on 1.1.0 or later is never upgraded or rolled +back to anything older (it is left out as `credential-downgrade`); exactly 1.1.0 +is fine. + +**Tokens.** A bootstrap token created at or before the removal no longer enrolls +that device, whatever its other limits say, so the install it has and a GPO still +pushing an old token cannot bring it back. The response is the plain `401` of any +refused token, with the reason in the audit trail. A token created after the +removal enrolls it on a fresh device row: an open removal closes as removed, and +its old command consumer and commands are cleared first, so nothing old replays. + +**In the Devices list.** A removal shows in the list for 7 days after its last +change: dimmed, with its state, not selectable and not counted in the totals. After +that the list stays uncluttered, and the **Removals** option of the Status filter +shows every removal whatever its age, with who asked, when, and what happened. +Clicking a removal opens the device's history. `GET /api/device-removals` takes +`state`, `clientId` and `limit` (default 200). Scoped roles see the removals of +devices that were in one of their scope groups when they were removed; a device +that was in none is visible to unscoped roles only. + +**Stop Trying.** `POST /api/device-removals/{id}/stop` on an open removal ends the +chase: the device stays blocked, its pending command is purged and its agent stays +installed, cut off when its credential expires (a renewal gets a plain `401`, not +`410`, because `410` would uninstall it after all). Enrolling it again needs a new +token. + +**Agents too old.** An agent below 1.1.0 cannot take the command or renew. Its +removal stays open marked *Uninstall It by Hand or Upgrade It First*, and since +its credential never expires the server cannot cut it off from NATS: it can keep +connecting, but the server ignores everything it reports. Once such an agent reports +1.1.0 or later the command goes out as usual. + +The audit trail keeps `device-removal-requested`, `device-removed` and +`device-removal-stopped`. The project wiki is updated separately. + +--- + ## Agent installer delivery **The installer ships inside the backend image — no manual seeding.** The CI -- 2.52.0 From c2b6a9baedc14a5ff17c02f25442e910a61c52d2f174e76cc2d6e7e5289ee5af Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Sat, 31 Oct 2026 20:09:10 -0400 Subject: [PATCH 08/20] feat(watchdog): the detached removal runner, purge and upgrade exclusion On removal.json the watchdog starts a runner copied outside the install and data folders. It uninstalls through MsiConfigureProductExW or removes the services through the service manager, then verifies and finishes the purge. Three attempts, restoring the services after each; failed is left in removal.json for the agent to report. Neither a new upgrade nor the supervisor touches a removal. Co-Authored-By: Claude Opus 5.5 --- src/Agent.Watchdog/AgentSupervisor.cs | 9 + src/Agent.Watchdog/Msi/WindowsInstaller.cs | 30 ++ src/Agent.Watchdog/Platform/ServiceInterop.cs | 33 ++ .../Removal/RemovalCoordinator.cs | 124 +++++ src/Agent.Watchdog/Removal/RemovalPlatform.cs | 484 ++++++++++++++++++ src/Agent.Watchdog/Removal/RemovalPurge.cs | 247 +++++++++ src/Agent.Watchdog/Removal/RemovalRunner.cs | 344 +++++++++++++ .../Removal/RemovalRunnerLauncher.cs | 211 ++++++++ src/Agent.Watchdog/UpgradeApplier.cs | 13 + src/Agent.Watchdog/WatchdogCli.cs | 7 +- src/Agent.Watchdog/WatchdogOptions.cs | 3 + src/Agent.Watchdog/WatchdogService.cs | 14 +- src/Shared.Contracts/Models/DeviceRemovals.cs | 19 +- src/Shared.Contracts/Models/WatchdogFiles.cs | 6 + tests/Agent.Watchdog.Tests/MsiUpgradeTests.cs | 4 + 15 files changed, 1545 insertions(+), 3 deletions(-) create mode 100644 src/Agent.Watchdog/Removal/RemovalCoordinator.cs create mode 100644 src/Agent.Watchdog/Removal/RemovalPlatform.cs create mode 100644 src/Agent.Watchdog/Removal/RemovalPurge.cs create mode 100644 src/Agent.Watchdog/Removal/RemovalRunner.cs create mode 100644 src/Agent.Watchdog/Removal/RemovalRunnerLauncher.cs diff --git a/src/Agent.Watchdog/AgentSupervisor.cs b/src/Agent.Watchdog/AgentSupervisor.cs index 746ce87..7388875 100644 --- a/src/Agent.Watchdog/AgentSupervisor.cs +++ b/src/Agent.Watchdog/AgentSupervisor.cs @@ -25,6 +25,15 @@ public sealed class AgentSupervisor(WatchdogOptions options, IAgentService agent public async Task CheckAsync(CancellationToken cancellationToken) { + // A removal stops the agent on purpose; only one that failed hands it back. + if (WatchdogFileStore.Read(options.RemovalPath, WatchdogJsonContext.Default.WatchdogRemoval) is { Phase: not Shared.Contracts.Models.WatchdogRemovalPhases.Failed }) + { + runningSince = null; + failedSince = null; + Note("A removal is under way; the agent is left to it"); + return; + } + var status = await agent.QueryAsync(cancellationToken); var now = clock.UtcNow; diff --git a/src/Agent.Watchdog/Msi/WindowsInstaller.cs b/src/Agent.Watchdog/Msi/WindowsInstaller.cs index c0cf237..c1b5e30 100644 --- a/src/Agent.Watchdog/Msi/WindowsInstaller.cs +++ b/src/Agent.Watchdog/Msi/WindowsInstaller.cs @@ -17,6 +17,12 @@ public interface IWindowsInstaller /// MsiInstallProductW: installs the package and returns Windows Installer's own code (what msiexec would exit with). int InstallProduct(string package, string commandLine); + + /// MsiConfigureProductExW with INSTALLSTATE_ABSENT: uninstalls the product and returns Windows Installer's own code. + int UninstallProduct(string productCode, string commandLine); + + /// MsiEnumRelatedProductsW: the product code installed under the UpgradeCode, or null. + string? RelatedProduct(string upgradeCode); } /// Windows Installer's answers an MSI run acts on. @@ -37,6 +43,8 @@ public static class MsiExitCodes public const int PackageInvalid = 1620; /// ERROR_PRODUCT_VERSION: a newer version is installed (a downgrade without ALLOWDOWNGRADE=1). public const int ProductVersion = 1638; + /// ERROR_UNKNOWN_PRODUCT: the product is not installed (an uninstall that has nothing to do). + public const int UnknownProduct = 1605; /// Done, done with a restart pending, or done with a restart started. public static bool Succeeded(int code) => code is Success or SuccessRebootRequired or SuccessRebootInitiated; @@ -51,6 +59,7 @@ public static class MsiExitCodes InstallFailure => "a fatal error during installation", PackageInvalid => "the package could not be opened", ProductVersion => "another version of the product is already installed", + UnknownProduct => "the product is not installed", 1619 => "the package could not be opened", 1625 => "the installation is forbidden by system policy", 1633 => "the package is not supported on this platform", @@ -64,6 +73,12 @@ public sealed partial class WindowsInstallerApi : IWindowsInstaller /// INSTALLUILEVEL_NONE. private const int InstallUiLevelNone = 2; + /// INSTALLLEVEL_DEFAULT. + private const int InstallLevelDefault = 0; + + /// INSTALLSTATE_ABSENT: Windows Installer sets REMOVE=ALL, as for msiexec's /x. + private const int InstallStateAbsent = 2; + /// /// msiexec's /l*v (msi.h INSTALLLOGMODE_*): FATALEXIT 0x1, ERROR 0x2, /// WARNING 0x4, USER 0x8, INFO 0x10, RESOLVESOURCE 0x40, OUTOFDISKSPACE 0x80, @@ -89,6 +104,21 @@ public sealed partial class WindowsInstallerApi : IWindowsInstaller public int InstallProduct(string package, string commandLine) => (int)MsiInstallProductW(package, commandLine); + public int UninstallProduct(string productCode, string commandLine) + => (int)MsiConfigureProductExW(productCode, InstallLevelDefault, InstallStateAbsent, commandLine); + + public unsafe string? RelatedProduct(string upgradeCode) + { + var buffer = stackalloc char[39]; + return MsiEnumRelatedProductsW(upgradeCode, 0, 0, buffer) == 0 ? new string(buffer, 0, 38) : null; + } + + [LibraryImport("msi.dll", StringMarshalling = StringMarshalling.Utf16)] + private static partial uint MsiConfigureProductExW(string product, int installLevel, int installState, string commandLine); + + [LibraryImport("msi.dll", StringMarshalling = StringMarshalling.Utf16)] + private static unsafe partial uint MsiEnumRelatedProductsW(string upgradeCode, uint reserved, uint index, char* productCode); + [LibraryImport("msi.dll")] private static partial int MsiSetInternalUI(int level, nint window); diff --git a/src/Agent.Watchdog/Platform/ServiceInterop.cs b/src/Agent.Watchdog/Platform/ServiceInterop.cs index f5ae041..1f7e86d 100644 --- a/src/Agent.Watchdog/Platform/ServiceInterop.cs +++ b/src/Agent.Watchdog/Platform/ServiceInterop.cs @@ -12,7 +12,20 @@ internal static partial class ServiceInterop internal const uint SC_MANAGER_CONNECT = 0x1; internal const uint SC_MANAGER_ALL_ACCESS = 0xF003F; internal const uint SERVICE_QUERY_STATUS = 0x4; + internal const uint SERVICE_START = 0x10; + internal const uint SERVICE_STOP = 0x20; + internal const uint DELETE = 0x10000; internal const uint SERVICE_ALL_ACCESS = 0xF01FF; + internal const uint SERVICE_CONTROL_STOP = 0x1; + internal const uint SERVICE_STOPPED = 0x1; + internal const uint SERVICE_START_PENDING = 0x2; + internal const uint SERVICE_STOP_PENDING = 0x3; + internal const uint SERVICE_RUNNING = 0x4; + internal const int ERROR_SERVICE_ALREADY_RUNNING = 1056; + internal const int ERROR_SERVICE_DOES_NOT_EXIST = 1060; + internal const int ERROR_SERVICE_CANNOT_ACCEPT_CTRL = 1061; + internal const int ERROR_SERVICE_NOT_ACTIVE = 1062; + internal const int ERROR_SERVICE_MARKED_FOR_DELETE = 1072; internal const uint SERVICE_WIN32_OWN_PROCESS = 0x10; internal const uint SERVICE_AUTO_START = 0x2; internal const uint SERVICE_ERROR_NORMAL = 0x1; @@ -62,6 +75,18 @@ internal static partial class ServiceInterop public uint dwServiceFlags; } + [StructLayout(LayoutKind.Sequential)] + internal struct SERVICE_STATUS + { + public uint dwServiceType; + public uint dwCurrentState; + public uint dwControlsAccepted; + public uint dwWin32ExitCode; + public uint dwServiceSpecificExitCode; + public uint dwCheckPoint; + public uint dwWaitHint; + } + [LibraryImport("advapi32.dll", EntryPoint = "OpenSCManagerW", StringMarshalling = StringMarshalling.Utf16, SetLastError = true)] internal static partial IntPtr OpenSCManager(string? machineName, string? databaseName, uint access); @@ -119,6 +144,14 @@ internal static partial class ServiceInterop [return: MarshalAs(UnmanagedType.Bool)] internal static partial bool CloseServiceHandle(IntPtr handle); + [LibraryImport("advapi32.dll", EntryPoint = "StartServiceW", StringMarshalling = StringMarshalling.Utf16, SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static partial bool StartService(IntPtr service, int argumentCount, IntPtr arguments); + + [LibraryImport("advapi32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static partial bool ControlService(IntPtr service, uint control, out SERVICE_STATUS status); + internal static Win32Exception LastError(string operation) => new(Marshal.GetLastPInvokeError(), $"{operation} failed"); diff --git a/src/Agent.Watchdog/Removal/RemovalCoordinator.cs b/src/Agent.Watchdog/Removal/RemovalCoordinator.cs new file mode 100644 index 0000000..b7810ad --- /dev/null +++ b/src/Agent.Watchdog/Removal/RemovalCoordinator.cs @@ -0,0 +1,124 @@ +using Shared.Contracts.Models; +using Shared.Contracts.Serialization; + +namespace Agent.Watchdog.Removal; + +/// +/// The watchdog's side of a removal: on removal.json it starts the detached +/// runner and leaves it to it. The runner stops and removes this watchdog, so a +/// runner that died can only be noticed if the removal failed and the watchdog is +/// still (or again) here; it is then started again, at most +/// times. An upgrade under way finishes first; the agent refuses a removal +/// during one, so that takes a command and a hand-over crossing. +/// +public sealed class RemovalCoordinator(WatchdogOptions options, IRemovalRunnerLauncher? launcher, IAgentService agent, IWatchdogClock clock, WatchdogLog log) +{ + public const int MaxLaunches = 5; + + /// A runner this watchdog just started is given this long to take its lock. + public static readonly TimeSpan LaunchGrace = TimeSpan.FromMinutes(1); + + private DateTimeOffset launchedUtc = DateTimeOffset.MinValue; + private int launches; + private bool waitingNoted; + + /// True while the removal owns this pass: no upgrade step and no supervision. + public async Task PassAsync(CancellationToken cancellationToken) + { + if (!File.Exists(options.RemovalPath)) + { + launches = 0; + return false; + } + + var removal = WatchdogFileStore.Read(options.RemovalPath, WatchdogJsonContext.Default.WatchdogRemoval); + if (removal is null) + { + log.Error($"{options.RemovalPath} does not read; it is removed and nothing was changed"); + WatchdogFileStore.TryDelete(options.RemovalPath); + return false; + } + + if (removal.Phase == WatchdogRemovalPhases.Failed) + { + // Over; the agent reports it and deletes the file. + return false; + } + + if (File.Exists(options.JournalPath) || File.Exists(options.MsiJournalPath)) + { + if (!waitingNoted) + { + log.Warn($"Removal {removal.RemovalId} waits for the upgrade under way to end"); + waitingNoted = true; + } + + return false; + } + + waitingNoted = false; + if (removal.RunnerDirectory is { } directory + && (RemovalRunLock.IsHeld(Path.Combine(directory, WatchdogRemovalFiles.RunnerLock)) || clock.UtcNow - launchedUtc < LaunchGrace)) + { + return true; + } + + if (launcher is null) + { + await GiveUpAsync(removal, "This watchdog runs in its development mode and cannot remove the agent.", cancellationToken); + return false; + } + + if (removal.Attempts >= WatchdogRemovalFiles.MaxAttempts || launches >= MaxLaunches) + { + await GiveUpAsync(removal, removal.LastError ?? $"The removal runner stopped before the removal ended, {launches} times.", cancellationToken); + return false; + } + + if (removal.RunnerDirectory is { } previous) + { + SwapPaths.TryDeleteDirectory(previous); + } + + log.BeginRun(); + log.Info($"Removal {removal.RemovalId} handed over ({removal.InstallKind} install); starting the runner"); + var (runnerDirectory, problem) = launcher.Prepare(removal.RemovalId); + if (problem is null) + { + WatchdogFileStore.Write(options.RemovalPath, + removal with { Phase = WatchdogRemovalPhases.Uninstalling, RunnerDirectory = runnerDirectory }, + WatchdogJsonContext.Default.WatchdogRemoval); + problem = launcher.Start(removal.RemovalId, runnerDirectory!); + } + + launches++; + if (problem is not null) + { + await GiveUpAsync(removal, $"The removal runner did not start: {problem}.", cancellationToken); + return false; + } + + launchedUtc = clock.UtcNow; + return true; + } + + private async Task GiveUpAsync(WatchdogRemoval removal, string why, CancellationToken cancellationToken) + { + log.Error($"Removal {removal.RemovalId} gave up: {why}"); + WatchdogFileStore.Write(options.RemovalPath, + removal with { Phase = WatchdogRemovalPhases.Failed, LastError = why }, + WatchdogJsonContext.Default.WatchdogRemoval); + try + { + var status = await agent.QueryAsync(cancellationToken); + if (status.State != AgentRunState.NotInstalled && !status.IsRunning) + { + await agent.StartAsync(cancellationToken); + } + } + catch (Exception exception) when (exception is not OperationCanceledException || !cancellationToken.IsCancellationRequested) + { + log.Warn("The agent could not be started", exception); + } + } +} diff --git a/src/Agent.Watchdog/Removal/RemovalPlatform.cs b/src/Agent.Watchdog/Removal/RemovalPlatform.cs new file mode 100644 index 0000000..ec9414a --- /dev/null +++ b/src/Agent.Watchdog/Removal/RemovalPlatform.cs @@ -0,0 +1,484 @@ +using System.ComponentModel; +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; +using System.Security.Cryptography.X509Certificates; +using System.Text.Json.Nodes; +using Agent.Watchdog.Msi; +using Agent.Watchdog.Platform; +using Microsoft.Win32; +using Shared.Contracts.Models; +using Shared.Infrastructure.Constants; +using static Agent.Watchdog.Platform.ServiceInterop; + +namespace Agent.Watchdog.Removal; + +/// The services a portable install's removal takes out (an MSI's are the MSI's to remove); replaced in tests. +public interface IRemovalServices +{ + Task StopWatchdogAsync(CancellationToken cancellationToken); + + Task StartWatchdogAsync(CancellationToken cancellationToken); + + /// Removes the agent's and the watchdog's registrations; nothing to do for one already gone. + Task RemoveAsync(CancellationToken cancellationToken); +} + +/// +/// Hidden entry point of the runner, for both binaries that have it: the runner +/// uses no agent code and no watchdog-only code, so the agent can fall back on it +/// when no watchdog can take a removal. Logs to its own folder, since the agent's +/// log folder goes with the install. +/// +public static class RemovalHost +{ + public static async Task RunAsync(Guid removalId, string[] args) + { + string? Value(string name) + { + for (var i = 0; i < args.Length - 1; i++) + { + if (string.Equals(args[i], name, StringComparison.OrdinalIgnoreCase)) + { + return args[i + 1]; + } + } + + return null; + } + + if (Value("--install-dir") is not { } install || Value("--data-dir") is not { } data || Value("--runner-dir") is not { } runnerDirectory) + { + Console.Error.WriteLine("Usage: --install-dir --data-dir --runner-dir "); + return 1; + } + + var options = new WatchdogOptions + { + InstallDirectory = Path.GetFullPath(install), + DataDirectory = Path.GetFullPath(data), + SelfPath = null, + }; + var log = new WatchdogLog(runnerDirectory, console: false, name: RemovalRunner.LogFileName); + var agentPath = Path.Combine(options.InstallDirectory, options.AgentExecutable); + + IAgentService agent; + IRemovalServices services; + IRemovalMachine machine; + IWindowsInstaller? installer = null; + if (OperatingSystem.IsWindows()) + { + agent = new ScmService(Branding.AgentServiceName, agentPath, log); + services = new WindowsRemovalServices( + (ScmService)agent, + new ScmService(Branding.WatchdogServiceName, Path.Combine(options.InstallDirectory, Branding.WatchdogExecutableName), log)); + machine = new WindowsRemovalMachine(log); + installer = new WindowsInstallerApi(); + } + else if (OperatingSystem.IsMacOS()) + { + agent = new LaunchdAgentService(Branding.AgentLaunchdLabel); + services = new UnixRemovalServices(); + machine = new UnixRemovalMachine(options, log); + } + else + { + agent = new SystemdAgentService(Branding.AgentSystemdUnit); + services = new UnixRemovalServices(); + machine = new UnixRemovalMachine(options, log); + } + + var clock = SystemClock.Instance; + var purge = new RemovalPurge(options, machine, clock, log, runnerDirectory); + var runner = new RemovalRunner(options, agent, services, installer, purge, clock, log, runnerDirectory); + return await runner.RunAsync(removalId, CancellationToken.None); + } +} + +/// A Windows service through the SCM's own APIs (never sc.exe, and none of ServiceController, which the agent's linux/macOS build lacks). +[SupportedOSPlatform("windows")] +public sealed class ScmService(string serviceName, string executablePath, WatchdogLog log) : IAgentService +{ + private static readonly TimeSpan StateChangeTimeout = TimeSpan.FromSeconds(60); + + public Task QueryAsync(CancellationToken cancellationToken) + { + if (QueryStatus(serviceName) is not { } status) + { + return Task.FromResult(AgentServiceStatus.NotInstalled); + } + + var state = status.dwCurrentState switch + { + SERVICE_STOPPED => AgentRunState.Stopped, + SERVICE_START_PENDING => AgentRunState.Starting, + SERVICE_STOP_PENDING => AgentRunState.Stopping, + _ => AgentRunState.Running, + }; + return Task.FromResult(new AgentServiceStatus(state, true, false)); + } + + public async Task StartAsync(CancellationToken cancellationToken) + { + WithService(SERVICE_START, service => + { + if (!StartService(service, 0, IntPtr.Zero) && Marshal.GetLastPInvokeError() != ERROR_SERVICE_ALREADY_RUNNING) + { + throw LastError($"StartService({serviceName})"); + } + }); + await WaitForAsync(SERVICE_RUNNING, cancellationToken); + } + + public async Task StopAsync(CancellationToken cancellationToken) + { + WithService(SERVICE_STOP, service => + { + if (!ControlService(service, SERVICE_CONTROL_STOP, out _) + && Marshal.GetLastPInvokeError() is not (ERROR_SERVICE_NOT_ACTIVE or ERROR_SERVICE_CANNOT_ACCEPT_CTRL)) + { + throw LastError($"ControlService({serviceName})"); + } + }); + await WaitForAsync(SERVICE_STOPPED, cancellationToken); + await StopLeftoversAsync(cancellationToken); + } + + /// Removes the registration: gone once the last handle on it closes. + public void Delete() => WithService(DELETE, service => + { + if (!DeleteService(service) && Marshal.GetLastPInvokeError() != ERROR_SERVICE_MARKED_FOR_DELETE) + { + throw LastError($"DeleteService({serviceName})"); + } + + log.Info($"Service {serviceName} removed"); + }); + + /// Runs on the open service; a service that is not there is nothing to do. + private void WithService(uint access, Action action) + { + var scm = OpenSCManager(null, null, SC_MANAGER_CONNECT); + if (scm == IntPtr.Zero) + { + throw LastError("OpenSCManager"); + } + + try + { + var service = OpenService(scm, serviceName, access); + if (service == IntPtr.Zero) + { + if (Marshal.GetLastPInvokeError() == ERROR_SERVICE_DOES_NOT_EXIST) + { + return; + } + + throw LastError($"OpenService({serviceName})"); + } + + try + { + action(service); + } + finally + { + CloseServiceHandle(service); + } + } + finally + { + CloseServiceHandle(scm); + } + } + + private async Task WaitForAsync(uint state, CancellationToken cancellationToken) + { + var deadline = DateTime.UtcNow + StateChangeTimeout; + while (QueryStatus(serviceName) is { } status && status.dwCurrentState != state) + { + if (DateTime.UtcNow >= deadline) + { + throw new System.TimeoutException($"{serviceName} did not reach state {state} within {StateChangeTimeout.TotalSeconds:0} s"); + } + + await Task.Delay(250, cancellationToken); + } + } + + /// The service process can outlive its stop report, and the agent's own children hold its binary: wait for them, then end what is left. + private async Task StopLeftoversAsync(CancellationToken cancellationToken) + { + var name = Path.GetFileNameWithoutExtension(executablePath); + var deadline = DateTime.UtcNow.AddSeconds(10); + while (true) + { + var leftovers = Process.GetProcessesByName(name).Where(IsThisBinary).ToList(); + if (leftovers.Count == 0) + { + return; + } + + var late = DateTime.UtcNow >= deadline; + foreach (var process in leftovers) + { + try + { + if (late) + { + log.Warn($"Ending process {process.Id} of {name} that outlived its service"); + process.Kill(entireProcessTree: true); + } + } + catch (Exception exception) when (exception is InvalidOperationException or Win32Exception) + { + } + finally + { + process.Dispose(); + } + } + + if (late) + { + return; + } + + await Task.Delay(500, cancellationToken); + } + } + + private bool IsThisBinary(Process process) + { + try + { + return string.Equals(process.MainModule?.FileName, executablePath, StringComparison.OrdinalIgnoreCase); + } + catch (Exception exception) when (exception is InvalidOperationException or Win32Exception) + { + return false; + } + } +} + +[SupportedOSPlatform("windows")] +public sealed class WindowsRemovalServices(ScmService agent, ScmService watchdog) : IRemovalServices +{ + public Task StopWatchdogAsync(CancellationToken cancellationToken) => watchdog.StopAsync(cancellationToken); + + public async Task StartWatchdogAsync(CancellationToken cancellationToken) + { + if ((await watchdog.QueryAsync(cancellationToken)) is { IsRunning: false, State: not AgentRunState.NotInstalled }) + { + await watchdog.StartAsync(cancellationToken); + } + } + + public Task RemoveAsync(CancellationToken cancellationToken) + { + agent.Delete(); + watchdog.Delete(); + return Task.CompletedTask; + } +} + +/// The normal registry key, the Local Machine certificate stores, and restart-time deletes. +[SupportedOSPlatform("windows")] +public sealed partial class WindowsRemovalMachine(WatchdogLog log) : IRemovalMachine +{ + private const uint MoveFileDelayUntilReboot = 0x4; + + public void RemoveConfiguration() => Registry.LocalMachine.DeleteSubKeyTree(Branding.AgentRegistryKey, throwOnMissingSubKey: false); + + public bool ConfigurationRemains + { + get + { + using var key = Registry.LocalMachine.OpenSubKey(Branding.AgentRegistryKey); + return key is not null; + } + } + + public void RemoveManagedCa(IReadOnlyCollection thumbprints) + { + foreach (var name in new[] { StoreName.Root, StoreName.CertificateAuthority }) + { + using var store = new X509Store(name, StoreLocation.LocalMachine); + store.Open(OpenFlags.ReadWrite); + foreach (var thumbprint in thumbprints) + { + foreach (var certificate in store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validOnly: false)) + { + store.Remove(certificate); + log.Info($"Removed the server CA {certificate.Subject} from LocalMachine\\{name}"); + } + } + } + } + + public void DeleteAtRestart(string path) + { + if (!MoveFileExW(path, null, MoveFileDelayUntilReboot)) + { + log.Warn($"{path} could not be scheduled for deletion at the next restart (error {Marshal.GetLastPInvokeError()})"); + } + } + + /// Held by the running binary until it exits; its log stays for support until the restart that deletes it. + public void RemoveRunnerDirectory(string directory) + { + try + { + foreach (var file in Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories)) + { + DeleteAtRestart(file); + } + + DeleteAtRestart(directory); + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException) + { + log.Warn($"{directory} could not be scheduled for deletion at the next restart", exception); + } + } + + [LibraryImport("kernel32.dll", StringMarshalling = StringMarshalling.Utf16, SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + private static partial bool MoveFileExW(string existingFileName, string? newFileName, uint flags); +} + +/// Both units or daemons, run from a runner that is outside either's cgroup or process group. +public sealed class UnixRemovalServices : IRemovalServices +{ + public Task StopWatchdogAsync(CancellationToken cancellationToken) => Task.Run(UnixWatchdogRegistration.Stop, cancellationToken); + + public Task StartWatchdogAsync(CancellationToken cancellationToken) => Task.Run(UnixWatchdogRegistration.Start, cancellationToken); + + public Task RemoveAsync(CancellationToken cancellationToken) => Task.Run(() => + { + if (OperatingSystem.IsMacOS()) + { + Command.Run("launchctl", "bootout", $"system/{Branding.AgentLaunchdLabel}"); + DeleteIfThere($"/Library/LaunchDaemons/{Branding.AgentLaunchdLabel}.plist"); + } + else + { + Command.Run("systemctl", "disable", "--now", Branding.AgentSystemdUnit); + DeleteIfThere($"/etc/systemd/system/{Branding.AgentSystemdUnit}.service"); + } + + UnixWatchdogRegistration.Remove(); + }, cancellationToken); + + private static void DeleteIfThere(string path) + { + if (File.Exists(path)) + { + File.Delete(path); + } + } +} + +/// agent.json (its policy section stays), the PATH link, and the trust anchors ServerTrust wrote. +public sealed class UnixRemovalMachine(WatchdogOptions options, WatchdogLog log) : IRemovalMachine +{ + private const string PathLink = "/usr/local/bin/" + Branding.AgentUnixExecutableName; + private const string AnchorName = "executer-server-ca.crt"; + private const string DebianAnchors = "/usr/local/share/ca-certificates/"; + private const string RedHatAnchors = "/etc/pki/ca-trust/source/anchors/"; + + private static string PolicySection => Branding.AgentPolicyRegistryKey.Replace('\\', '/'); + + public void RemoveConfiguration() + { + var path = Branding.AgentUnixConfigFile; + if (new FileInfo(PathLink).LinkTarget is { } target + && (SwapPaths.IsInside(target, options.InstallDirectory) || target.Contains("/executer/", StringComparison.Ordinal))) + { + File.Delete(PathLink); + } + + if (File.Exists(path)) + { + var kept = new JsonObject(); + try + { + if (JsonNode.Parse(File.ReadAllText(path)) is JsonObject root + && root.FirstOrDefault(p => string.Equals(p.Key, PolicySection, StringComparison.OrdinalIgnoreCase)) is { Value: JsonObject policy } section) + { + kept[section.Key] = policy.DeepClone(); + } + } + catch (System.Text.Json.JsonException) + { + log.Warn($"{path} does not read; it goes whole"); + } + + if (kept.Count > 0) + { + File.WriteAllText(path, kept.ToJsonString()); + return; + } + + File.Delete(path); + } + + if (Path.GetDirectoryName(path) is { } folder) + { + SwapPaths.TryDeleteIfEmpty(folder); + } + } + + public bool ConfigurationRemains + { + get + { + if (!File.Exists(Branding.AgentUnixConfigFile)) + { + return false; + } + + try + { + // Only the policy section may stay. + return JsonNode.Parse(File.ReadAllText(Branding.AgentUnixConfigFile)) is not JsonObject root + || root.Any(p => !string.Equals(p.Key, PolicySection, StringComparison.OrdinalIgnoreCase)); + } + catch (System.Text.Json.JsonException) + { + return true; + } + } + } + + public void RemoveManagedCa(IReadOnlyCollection thumbprints) + { + if (OperatingSystem.IsMacOS()) + { + foreach (var thumbprint in thumbprints) + { + Command.Run("security", "delete-certificate", "-Z", thumbprint, "/Library/Keychains/System.keychain"); + } + + return; + } + + foreach (var (folder, refresh) in new[] { (DebianAnchors, "update-ca-certificates"), (RedHatAnchors, "update-ca-trust") }) + { + var anchor = folder + AnchorName; + if (File.Exists(anchor)) + { + File.Delete(anchor); + Command.Run(refresh); + log.Info($"Removed the server CA {anchor}"); + } + } + } + + public void DeleteAtRestart(string path) + { + // Unlinking works here even for a file in use; a sweep already tried. + } + + public void RemoveRunnerDirectory(string directory) => SwapPaths.TryDeleteDirectory(directory); +} diff --git a/src/Agent.Watchdog/Removal/RemovalPurge.cs b/src/Agent.Watchdog/Removal/RemovalPurge.cs new file mode 100644 index 0000000..66d86e1 --- /dev/null +++ b/src/Agent.Watchdog/Removal/RemovalPurge.cs @@ -0,0 +1,247 @@ +using Shared.Contracts.Models; +using Shared.Infrastructure.Constants; + +namespace Agent.Watchdog.Removal; + +/// What a removal wipes outside the install and data folders; replaced in tests. +public interface IRemovalMachine +{ + /// + /// The agent's configuration and runtime state: the normal registry key tree + /// (agent.json and the PATH link on linux/macOS). The policy key is + /// group policy's and stays. + /// + void RemoveConfiguration(); + + bool ConfigurationRemains { get; } + + /// The server CA certificates the agent installed itself (). + void RemoveManagedCa(IReadOnlyCollection thumbprints); + + /// Deletes the file or empty folder at the next restart (nothing to do on linux/macOS, where nothing is locked). + void DeleteAtRestart(string path); + + /// The runner's folder, which holds the running binary: now where that works, otherwise at the next restart. + void RemoveRunnerDirectory(string directory); +} + +/// +/// Verifies and finishes the wipe, whichever way the product went. The MSI's +/// purge-credentials custom action runs with Return="ignore", so +/// its success proves nothing: credentials.dat, the configuration, the +/// data and install folders (Logs with them) are each checked and removed +/// here; what is still locked after a minute goes at the next restart. +/// +public sealed class RemovalPurge(WatchdogOptions options, IRemovalMachine machine, IWatchdogClock clock, WatchdogLog log, string runnerDirectory) +{ + public const int Sweeps = 7; + public static readonly TimeSpan SweepDelay = TimeSpan.FromSeconds(10); + + private readonly List thumbprints = []; + + private string CredentialPath => Path.Combine(options.DataDirectory, Branding.CredentialFileName); + + private string ManagedCaPath => Path.Combine(options.DataDirectory, WatchdogFiles.ManagedCa); + + private string ManagedCaCopy => Path.Combine(runnerDirectory, WatchdogFiles.ManagedCa); + + /// Takes what the purge needs later from the data folder, into the runner's folder (a resumed runner finds it there). + public void Snapshot() + { + try + { + if (File.Exists(ManagedCaPath)) + { + File.Copy(ManagedCaPath, ManagedCaCopy, overwrite: true); + } + + thumbprints.Clear(); + if (File.Exists(ManagedCaCopy)) + { + thumbprints.AddRange(File.ReadAllLines(ManagedCaCopy).Select(l => l.Trim()).Where(l => l.Length > 0)); + } + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException) + { + log.Warn("The list of installed server CAs could not be read; they stay", exception); + } + } + + /// Why the install folder must not be wiped; null when it may. Checked before a portable install's services go. + public string? InstallFolderRefusal() + { + if (IsProtectedFolder(options.InstallDirectory)) + { + return $"The agent runs from {options.InstallDirectory}, which is not a folder of its own; it was left alone."; + } + + return File.Exists(Path.Combine(options.InstallDirectory, options.AgentExecutable)) + ? null + : $"{options.InstallDirectory} holds no {options.AgentExecutable}; it was left alone."; + } + + /// Removes what is left; what stays after every sweep is listed and goes at the next restart. + public async Task> FinishAsync(CancellationToken cancellationToken) + { + var left = new List(); + for (var sweep = 1; sweep <= Sweeps; sweep++) + { + left = Sweep(); + if (left.Count == 0) + { + return left; + } + + if (sweep < Sweeps) + { + log.Info($"Still there (sweep {sweep} of {Sweeps}): {string.Join("; ", left)}"); + await clock.DelayAsync(SweepDelay, cancellationToken); + } + } + + foreach (var folder in new[] { options.DataDirectory, options.InstallDirectory }) + { + if (!IsProtectedFolder(folder)) + { + ScheduleAtRestart(folder); + } + } + + return left; + } + + public void RemoveRunnerDirectory(string directory) => machine.RemoveRunnerDirectory(directory); + + private List Sweep() + { + var left = new List(); + + // First and on its own: nothing else on the device can authenticate as it. + Try("credentials", () => + { + if (File.Exists(CredentialPath)) + { + File.SetAttributes(CredentialPath, FileAttributes.Normal); + File.Delete(CredentialPath); + } + }, left); + Try("configuration", machine.RemoveConfiguration, left); + if (thumbprints.Count > 0) + { + Try("server CA", () => machine.RemoveManagedCa(thumbprints), left); + } + + Try(options.DataDirectory, () => DeleteFolder(options.DataDirectory), left); + Try(options.InstallDirectory, () => DeleteFolder(options.InstallDirectory), left); + if (Path.GetDirectoryName(Path.TrimEndingDirectorySeparator(options.InstallDirectory)) is { } parent + && !IsProtectedFolder(parent) + && string.Equals(Path.GetFileName(parent), Branding.ProductKey, StringComparison.OrdinalIgnoreCase)) + { + // %ProgramFiles%\ExecuteR, /opt/executer: only ever ours, when nothing else is in it. + SwapPaths.TryDeleteIfEmpty(parent); + } + + if (File.Exists(CredentialPath) && !left.Contains("credentials")) + { + left.Add("credentials"); + } + + if (machine.ConfigurationRemains && !left.Contains("configuration")) + { + left.Add("configuration"); + } + + return left; + } + + private void Try(string what, Action action, List left) + { + try + { + action(); + } + catch (Exception exception) when (exception is not OperationCanceledException) + { + log.Warn($"{what} could not be removed", exception); + left.Add(what); + } + } + + private static void DeleteFolder(string folder) + { + if (!Directory.Exists(folder)) + { + return; + } + + if (IsProtectedFolder(folder)) + { + throw new InvalidOperationException($"{folder} is not a folder of the agent's own"); + } + + foreach (var file in Directory.EnumerateFiles(folder, "*", SearchOption.AllDirectories)) + { + // Directory.Delete stops at a read-only file on Windows. + if ((File.GetAttributes(file) & FileAttributes.ReadOnly) != 0) + { + File.SetAttributes(file, FileAttributes.Normal); + } + } + + Directory.Delete(folder, recursive: true); + } + + private void ScheduleAtRestart(string folder) + { + try + { + if (!Directory.Exists(folder)) + { + return; + } + + foreach (var file in Directory.EnumerateFiles(folder, "*", SearchOption.AllDirectories)) + { + machine.DeleteAtRestart(file); + } + + // Deepest first: a folder goes only once it is empty. + foreach (var directory in Directory.EnumerateDirectories(folder, "*", SearchOption.AllDirectories).OrderByDescending(d => d.Length)) + { + machine.DeleteAtRestart(directory); + } + + machine.DeleteAtRestart(folder); + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException) + { + log.Warn($"{folder} could not be scheduled for the next restart", exception); + } + } + + /// A root, or a folder the system or other software shares: a portable agent unpacked straight into one never takes it along. + public static bool IsProtectedFolder(string folder) + { + var full = Path.TrimEndingDirectorySeparator(Path.GetFullPath(folder)); + if (string.IsNullOrEmpty(Path.GetFileName(full))) + { + return true; + } + + IEnumerable shared = OperatingSystem.IsWindows() + ? + [ + Environment.GetFolderPath(Environment.SpecialFolder.Windows), + Environment.GetFolderPath(Environment.SpecialFolder.System), + Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), + Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), + Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles), + Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), + Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), + Path.Combine(Path.GetPathRoot(full) ?? "", "Users"), + ] + : ["/bin", "/boot", "/dev", "/etc", "/home", "/lib", "/lib64", "/opt", "/proc", "/root", "/run", "/sbin", "/sys", "/tmp", "/usr", "/usr/bin", + "/usr/lib", "/usr/local", "/usr/local/bin", "/usr/sbin", "/var", "/var/lib", "/var/tmp", "/Applications", "/Library", "/System", "/Users"]; + return shared.Any(path => path.Length > 0 && SwapPaths.Comparer.Equals(Path.TrimEndingDirectorySeparator(path), full)); + } +} diff --git a/src/Agent.Watchdog/Removal/RemovalRunner.cs b/src/Agent.Watchdog/Removal/RemovalRunner.cs new file mode 100644 index 0000000..e493071 --- /dev/null +++ b/src/Agent.Watchdog/Removal/RemovalRunner.cs @@ -0,0 +1,344 @@ +using Agent.Watchdog.Msi; +using Shared.Contracts.Models; +using Shared.Contracts.Serialization; +using Shared.Infrastructure.Constants; + +namespace Agent.Watchdog.Removal; + +/// Timings of a removal; shortened in tests. +public sealed record RemovalRunnerSettings +{ + /// Between tries while Windows Installer is busy with another installation (1618). + public TimeSpan BusyRetryDelay { get; init; } = TimeSpan.FromSeconds(30); + + /// How long a busy Windows Installer is waited out within one attempt. + public TimeSpan BusyRetryFor { get; init; } = TimeSpan.FromMinutes(10); + + /// The longest one attempt's uninstall may take (real time: Windows Installer's call cannot be cut short). + public TimeSpan AttemptTimeout { get; init; } = WatchdogFiles.MsiInstallTimeout; +} + +/// +/// The detached removal runner (ExecuteR-Watchdog.exe remove-run <id>, or +/// ExecuteR-Agent.exe remove-self <id> where no watchdog can take it), +/// started from a copy of its binary outside the install and data folders, since +/// it wipes both. It owns the removal in removal.json while it holds +/// removal.lock in its own folder: +/// +/// stop the agent; +/// MSI install: Windows Installer in process, by product code +/// (REBOOT=ReallySuppress, no UI, verbose log in the runner's folder); +/// 0, 3010, 1641 and 1605 (already gone) are success, 1618 is tried again every +/// 30 s for up to 10 minutes. Portable install: the watchdog and agent services +/// go through the service manager; +/// the purge is verified and finished (): whatever +/// the MSI's own purge left is removed, and what is locked goes at the next restart. +/// +/// A failed attempt starts the services again and the next one follows 5 minutes +/// later, three in all; then the phase says failed, with the reason, for the +/// agent to report, and the device carries on. Unlike this +/// runner writes no outcome and verifies no health: on success nobody is left. +/// +public sealed class RemovalRunner( + WatchdogOptions options, + IAgentService agent, + IRemovalServices services, + IWindowsInstaller? installer, + RemovalPurge purge, + IWatchdogClock clock, + WatchdogLog log, + string runnerDirectory, + RemovalRunnerSettings? settings = null) +{ + public const string UninstallProperties = "REBOOT=ReallySuppress"; + public const string LogFileName = "removal"; + + private readonly RemovalRunnerSettings timings = settings ?? new RemovalRunnerSettings(); + + private string LockPath => Path.Combine(runnerDirectory, WatchdogRemovalFiles.RunnerLock); + + /// Runs (or resumes) the removal; 0 unless removal.json does not name it. + public async Task RunAsync(Guid removalId, CancellationToken cancellationToken) + { + bool gone; + using (var held = RemovalRunLock.TryHold(LockPath)) + { + if (held is null) + { + log.Info($"Another runner owns removal {removalId}; this one exits"); + return 0; + } + + var removal = WatchdogFileStore.Read(options.RemovalPath, WatchdogJsonContext.Default.WatchdogRemoval); + if (removal is null || removal.RemovalId != removalId) + { + log.Warn($"Removal {removalId} is not what {options.RemovalPath} names; nothing to do"); + return removal is null ? 1 : 0; + } + + log.BeginRun(); + log.Info($"Runner {Environment.ProcessId} owns removal {removalId} ({removal.InstallKind} install, {removal.Attempts} attempts so far)"); + + // The MSI's own purge takes the data folder this reads. + purge.Snapshot(); + + gone = await AttemptAllAsync(removal, cancellationToken); + if (gone) + { + var left = await purge.FinishAsync(cancellationToken); + log.Info(left.Count == 0 + ? $"Removal {removalId} is complete; nothing is left" + : $"Removal {removalId} is complete, except what goes at the next restart: {string.Join("; ", left)}"); + } + } + + purge.RemoveRunnerDirectory(runnerDirectory); + return 0; + } + + /// True once the product and its services are gone; false when every attempt failed. + private async Task AttemptAllAsync(WatchdogRemoval removal, CancellationToken cancellationToken) + { + while (true) + { + if (removal.Attempts >= WatchdogRemovalFiles.MaxAttempts) + { + Finish(removal with { LastError = removal.LastError ?? "The removal runner stopped before it ended." }); + return false; + } + + removal = removal with { Phase = WatchdogRemovalPhases.Uninstalling, Attempts = removal.Attempts + 1 }; + Write(removal); + log.Info($"Removing the agent (attempt {removal.Attempts} of {WatchdogRemovalFiles.MaxAttempts})"); + + string? problem; + try + { + problem = await AttemptAsync(removal, cancellationToken); + } + catch (Exception exception) when (exception is not OperationCanceledException) + { + problem = $"The removal failed ({exception.GetBaseException().Message})."; + } + + if (problem is null) + { + return true; + } + + log.Error($"Attempt {removal.Attempts} failed: {problem}"); + await RestoreAsync(cancellationToken); + + removal = removal with { LastError = problem }; + if (removal.Attempts >= WatchdogRemovalFiles.MaxAttempts) + { + Finish(removal); + return false; + } + + Write(removal); + log.Info($"The next attempt follows in {WatchdogRemovalFiles.AttemptDelay.TotalMinutes:0} minutes; the agent runs meanwhile"); + await clock.DelayAsync(WatchdogRemovalFiles.AttemptDelay, cancellationToken); + } + } + + /// One attempt; null when the agent is gone, otherwise why not. + private async Task AttemptAsync(WatchdogRemoval removal, CancellationToken cancellationToken) + { + try + { + await agent.StopAsync(cancellationToken); + } + catch (Exception exception) when (exception is not OperationCanceledException) + { + // Windows Installer and the service manager both stop it themselves. + log.Warn("The agent did not stop", exception); + } + + var product = removal.InstallKind == AgentInstallKinds.Msi && installer is not null + ? installer.RelatedProduct(Branding.AgentMsiUpgradeCode) + : null; + return product is not null + ? await UninstallMsiAsync(removal, product) + : await RemovePortableAsync(removal, cancellationToken); + } + + private async Task UninstallMsiAsync(WatchdogRemoval removal, string product) + { + var logPath = MsiLogPath(removal.RemovalId); + log.Info($"Uninstalling {product} through Windows Installer ({UninstallProperties}); its log is {logPath}"); + var giveUp = clock.UtcNow + timings.BusyRetryFor; + while (true) + { + var answer = await UninstallOnceAsync(product, logPath); + if (answer is not { } code) + { + return $"Windows Installer did not finish within {timings.AttemptTimeout.TotalMinutes:0} minutes; its log is {logPath}."; + } + + if (code == MsiExitCodes.InstallerBusy) + { + if (clock.UtcNow + timings.BusyRetryDelay > giveUp) + { + return $"Another installation kept Windows Installer busy (1618) for {timings.BusyRetryFor.TotalMinutes:0} minutes."; + } + + log.Warn($"Windows Installer is busy with another installation (1618); trying again in {timings.BusyRetryDelay.TotalSeconds:0} s"); + await clock.DelayAsync(timings.BusyRetryDelay, CancellationToken.None); + continue; + } + + if (!MsiExitCodes.Succeeded(code) && code != MsiExitCodes.UnknownProduct) + { + return $"Windows Installer ended with {code} ({MsiExitCodes.Describe(code)}); its log is {logPath}."; + } + + log.Info($"Windows Installer ended with {code} ({MsiExitCodes.Describe(code)})"); + if (installer!.RelatedProduct(Branding.AgentMsiUpgradeCode) is { } still) + { + return $"Windows Installer ended with {code}, but {still} is still installed."; + } + + return null; + } + } + + /// + /// Windows Installer's answer, or null when it gave none within the timeout. + /// The three calls run on one background thread of their own; one cut off by + /// the timeout ends with the process. + /// + private async Task UninstallOnceAsync(string product, string logPath) + { + var uninstall = Task.Factory.StartNew(() => + { + installer!.SetSilentUI(); + installer.EnableLog(logPath); + return installer.UninstallProduct(product, UninstallProperties); + }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); + + try + { + return await uninstall.WaitAsync(timings.AttemptTimeout); + } + catch (TimeoutException) + { + return null; + } + } + + private async Task RemovePortableAsync(WatchdogRemoval removal, CancellationToken cancellationToken) + { + if (purge.InstallFolderRefusal() is { } refusal) + { + return refusal; + } + + log.Info($"Removing the services of the install in {options.InstallDirectory}"); + await services.StopWatchdogAsync(cancellationToken); + await services.RemoveAsync(cancellationToken); + log.Info(removal.InstallKind == AgentInstallKinds.Msi + ? "Windows Installer no longer knows the product; its services were removed directly" + : "The agent and watchdog services are removed"); + return null; + } + + /// Whatever the failed attempt stopped runs again, the watchdog first. + private async Task RestoreAsync(CancellationToken cancellationToken) + { + try + { + await services.StartWatchdogAsync(cancellationToken); + } + catch (Exception exception) when (exception is not OperationCanceledException) + { + log.Warn("The watchdog service could not be started", exception); + } + + try + { + var status = await agent.QueryAsync(cancellationToken); + if (status.State != AgentRunState.NotInstalled && !status.IsRunning) + { + await agent.StartAsync(cancellationToken); + } + } + catch (Exception exception) when (exception is not OperationCanceledException) + { + log.Warn("The agent could not be started", exception); + } + } + + /// Failed for good: the agent reports it from removal.json; the logs stay where an operator looks. + private void Finish(WatchdogRemoval removal) + { + log.Error($"Removal {removal.RemovalId} gave up after {removal.Attempts} attempts: {removal.LastError}"); + Write(removal with { Phase = WatchdogRemovalPhases.Failed }); + foreach (var file in new[] { Path.Combine(runnerDirectory, LogFileName + ".log"), MsiLogPath(removal.RemovalId) }) + { + try + { + if (File.Exists(file)) + { + Directory.CreateDirectory(options.LogDirectory); + File.Copy(file, Path.Combine(options.LogDirectory, $"agent-removal-{removal.RemovalId:N}-{Path.GetFileName(file)}"), overwrite: true); + } + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException) + { + log.Warn($"{file} could not be kept", exception); + } + } + } + + private string MsiLogPath(Guid removalId) => Path.Combine(runnerDirectory, $"agent-msi-uninstall-{removalId:N}.log"); + + private void Write(WatchdogRemoval removal) + => WatchdogFileStore.Write(options.RemovalPath, removal, WatchdogJsonContext.Default.WatchdogRemoval); +} + +/// +/// removal.lock in the runner's own folder, held open with no sharing for +/// as long as it lives: released when the process ends however it ends, so a lock +/// nobody holds is a runner that is gone. +/// +public static class RemovalRunLock +{ + public static FileStream? TryHold(string path) + { + try + { + return new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); + } + catch (IOException) + { + return null; + } + } + + public static bool IsHeld(string path) + { + if (!File.Exists(path)) + { + return false; + } + + try + { + using var probe = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None); + return false; + } + catch (FileNotFoundException) + { + return false; + } + catch (IOException) + { + return true; + } + catch (UnauthorizedAccessException) + { + return false; + } + } +} diff --git a/src/Agent.Watchdog/Removal/RemovalRunnerLauncher.cs b/src/Agent.Watchdog/Removal/RemovalRunnerLauncher.cs new file mode 100644 index 0000000..9742db3 --- /dev/null +++ b/src/Agent.Watchdog/Removal/RemovalRunnerLauncher.cs @@ -0,0 +1,211 @@ +using System.ComponentModel; +using System.Globalization; +using System.Runtime.Versioning; +using System.Security.AccessControl; +using System.Security.Principal; +using Agent.Watchdog.Msi; +using Agent.Watchdog.Platform; +using Shared.Contracts.Models; +using Shared.Infrastructure.Constants; + +namespace Agent.Watchdog.Removal; + +/// Starts the removal runner apart from the service that asks for it (the runner removes that service); replaced in tests. +public interface IRemovalRunnerLauncher +{ + /// A new folder outside the install and data folders, with a copy of this binary in it; or why not. + (string? RunnerDirectory, string? Problem) Prepare(Guid removalId); + + /// Starts the copy; null when it started, otherwise why not. + string? Start(Guid removalId, string runnerDirectory); +} + +/// The launcher for this platform; shared with the agent, which has it as its fallback when no watchdog can take a removal. +public static class RemovalRunnerLauncher +{ + public static IRemovalRunnerLauncher Create(WatchdogOptions options, WatchdogLog log, string verb) + { + if (OperatingSystem.IsWindows()) + { + return new WindowsRemovalRunnerLauncher(options, log, verb); + } + + return new UnixRemovalRunnerLauncher(options, log, verb); + } + + internal static string[] Arguments(string verb, Guid removalId, WatchdogOptions options, string runnerDirectory) => + [ + verb, removalId.ToString("D"), + "--install-dir", options.InstallDirectory, + "--data-dir", options.DataDirectory, + "--runner-dir", runnerDirectory, + ]; + + internal static string CopySelf(WatchdogOptions options, string runnerDirectory) + { + var self = options.SelfPath is { } path && File.Exists(path) + ? path + : throw new FileNotFoundException("this process has no binary of its own to run from"); + var copy = Path.Combine(runnerDirectory, Path.GetFileName(self)); + File.Copy(self, copy, overwrite: true); + SwapPaths.MakeExecutable(copy); + return copy; + } +} + +/// +/// %SystemRoot%\Temp\ExecuteR-<id>-<random>\: anyone may add to a folder +/// created under that Temp by default, and this one runs as SYSTEM and loads +/// system DLLs from its own folder, so it is created closed (SYSTEM and +/// Administrators only), under a name no one can have prepared, and its owner is +/// checked before the binary goes in. Started detached: stopping both services +/// never takes it down. +/// +[SupportedOSPlatform("windows")] +public sealed class WindowsRemovalRunnerLauncher(WatchdogOptions options, WatchdogLog log, string verb) : IRemovalRunnerLauncher +{ + public (string? RunnerDirectory, string? Problem) Prepare(Guid removalId) + { + var directory = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Temp", + $"{Branding.ProductKey}-{removalId:N}-{Random.Shared.Next():x8}"); + try + { + var folder = new DirectoryInfo(directory); + if (folder.Exists) + { + return (null, $"{directory} already exists"); + } + + folder.Create(ClosedFolder()); + var owner = folder.GetAccessControl().GetOwner(typeof(SecurityIdentifier)); + using var identity = WindowsIdentity.GetCurrent(); + if (owner is not SecurityIdentifier sid + || !(sid.IsWellKnown(WellKnownSidType.LocalSystemSid) || sid.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid) || sid == identity.User)) + { + return (null, $"{directory} belongs to someone else"); + } + + RemovalRunnerLauncher.CopySelf(options, directory); + return (directory, null); + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException) + { + return (null, $"its binary could not be copied to {directory} ({exception.Message})"); + } + } + + public string? Start(Guid removalId, string runnerDirectory) + { + var copy = Path.Combine(runnerDirectory, Path.GetFileName(options.SelfPath!)); + try + { + var processId = DetachedProcess.Start(copy, RemovalRunnerLauncher.Arguments(verb, removalId, options, runnerDirectory), runnerDirectory); + log.Info($"Removal runner started (process {processId}) from {copy}"); + return null; + } + catch (Win32Exception exception) + { + return exception.Message; + } + } + + private static DirectorySecurity ClosedFolder() + { + var security = new DirectorySecurity(); + security.SetAccessRuleProtection(isProtected: true, preserveInheritance: false); + foreach (var account in new[] { WellKnownSidType.LocalSystemSid, WellKnownSidType.BuiltinAdministratorsSid }) + { + security.AddAccessRule(new FileSystemAccessRule( + new SecurityIdentifier(account, null), + FileSystemRights.FullControl, + InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, + PropagationFlags.None, + AccessControlType.Allow)); + } + + return security; + } +} + +/// +/// <parent of data>/executer-removal-<random>/ (mode 700; /var/lib, which +/// unlike a temp folder is not mounted noexec), started through the service +/// manager, since a plain child dies with the service that started it: systemd +/// kills the service's whole cgroup, launchd its process group. Linux: a transient +/// unit (systemd-run). macOS: a one-shot daemon from a plist in that folder. +/// +public sealed class UnixRemovalRunnerLauncher(WatchdogOptions options, WatchdogLog log, string verb) : IRemovalRunnerLauncher +{ + public (string? RunnerDirectory, string? Problem) Prepare(Guid removalId) + { + if (OperatingSystem.IsWindows()) + { + return (null, "not on this platform"); + } + + var parent = Path.GetDirectoryName(Path.TrimEndingDirectorySeparator(options.DataDirectory)); + var directory = Path.Combine(parent ?? "/var/lib", $"executer-removal-{Random.Shared.Next():x8}"); + try + { + Directory.CreateDirectory(directory, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute); + RemovalRunnerLauncher.CopySelf(options, directory); + return (directory, null); + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException) + { + return (null, $"its binary could not be copied to {directory} ({exception.Message})"); + } + } + + public string? Start(Guid removalId, string runnerDirectory) + { + var copy = Path.Combine(runnerDirectory, Path.GetFileName(options.SelfPath!)); + var arguments = RemovalRunnerLauncher.Arguments(verb, removalId, options, runnerDirectory); + try + { + var (code, output) = OperatingSystem.IsMacOS() ? StartLaunchd(copy, arguments, removalId, runnerDirectory) : StartSystemd(copy, arguments, removalId); + if (code != 0) + { + return output.Trim(); + } + + log.Info($"Removal runner started from {copy}"); + return null; + } + catch (Exception exception) when (exception is Win32Exception or InvalidOperationException or IOException) + { + return exception.Message; + } + } + + private static (int Code, string Output) StartSystemd(string copy, string[] arguments, Guid removalId) + => Command.Run("systemd-run", [$"--unit={Branding.AgentSystemdUnit}-removal-{removalId:N}", "--collect", copy, .. arguments]); + + private static (int Code, string Output) StartLaunchd(string copy, string[] arguments, Guid removalId, string runnerDirectory) + { + // Booted from outside /Library/LaunchDaemons: gone at the next restart, with nothing to clean up. + var label = string.Create(CultureInfo.InvariantCulture, $"{Branding.AgentLaunchdLabel}.removal.{removalId:N}"); + var strings = string.Concat(new[] { copy }.Concat(arguments).Select(a => $"{System.Security.SecurityElement.Escape(a)}")); + var plist = Path.Combine(runnerDirectory, label + ".plist"); + File.WriteAllText(plist, $""" + + + + + Label{label} + ProgramArguments + {strings} + RunAtLoad + AbandonProcessGroup + + + """); + if (!OperatingSystem.IsWindows()) + { + File.SetUnixFileMode(plist, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.GroupRead | UnixFileMode.OtherRead); + } + + return Command.Run("launchctl", "bootstrap", "system", plist); + } +} diff --git a/src/Agent.Watchdog/UpgradeApplier.cs b/src/Agent.Watchdog/UpgradeApplier.cs index c501f13..b04503b 100644 --- a/src/Agent.Watchdog/UpgradeApplier.cs +++ b/src/Agent.Watchdog/UpgradeApplier.cs @@ -309,6 +309,19 @@ public sealed class UpgradeApplier( } var handover = WatchdogFileStore.Read(options.HandoverPath, WatchdogJsonContext.Default.WatchdogHandover); + if (handover is not null && File.Exists(options.RemovalPath)) + { + // The device is being removed: an upgrade that crossed it never starts. + log.Warn($"Upgrade {handover.UpgradeId} is set aside: the agent is being removed"); + WatchdogFileStore.TryDelete(options.HandoverPath); + WatchdogFileStore.Write(options.OutcomePath, + new WatchdogOutcome(handover.UpgradeId, AgentUpgradeStates.Failed, AgentUpgradeReasons.Busy, handover.Version, handover.FromVersion, [], + clock.UtcNow, "The agent is being removed from this device; nothing was changed."), + WatchdogJsonContext.Default.WatchdogOutcome); + finished.Add(handover.UpgradeId); + return null; + } + if (handover is null) { var id = WatchdogFileStore.ReadUpgradeId(options.HandoverPath); diff --git a/src/Agent.Watchdog/WatchdogCli.cs b/src/Agent.Watchdog/WatchdogCli.cs index c57c829..0999449 100644 --- a/src/Agent.Watchdog/WatchdogCli.cs +++ b/src/Agent.Watchdog/WatchdogCli.cs @@ -60,6 +60,9 @@ public static class WatchdogCli && Guid.TryParse(args[1], out var upgradeId): return await RunMsiAsync(upgradeId, args[2..]); + case WatchdogRemovalFiles.RunVerb when args.Length > 1 && Guid.TryParse(args[1], out var removalId): + return await Removal.RemovalHost.RunAsync(removalId, args[2..]); + case null or "help" or "-h" or "--help" or "/?": PrintUsage(); return command is null ? 1 : 0; @@ -140,7 +143,9 @@ public static class WatchdogCli agent, clock, log); var applier = new UpgradeApplier(options, agent, clock, log, new ProcessSmokeTest(), restart, msi); var supervisor = new AgentSupervisor(options, agent, clock, log); - var service = new WatchdogService(options, applier, supervisor, clock, log); + var removal = new Removal.RemovalCoordinator(options, + processMode ? null : Removal.RemovalRunnerLauncher.Create(options, log, WatchdogRemovalFiles.RunVerb), agent, clock, log); + var service = new WatchdogService(options, applier, supervisor, clock, log, removal); if (processMode) { diff --git a/src/Agent.Watchdog/WatchdogOptions.cs b/src/Agent.Watchdog/WatchdogOptions.cs index 9a02f8e..893f543 100644 --- a/src/Agent.Watchdog/WatchdogOptions.cs +++ b/src/Agent.Watchdog/WatchdogOptions.cs @@ -63,6 +63,9 @@ public sealed record WatchdogOptions public string MsiRunnerLockPath => Path.Combine(UpgradeDirectory, WatchdogMsiFiles.RunnerLock); + /// A removal the agent handed over: while it is there no new upgrade starts, and until it failed the agent is not supervised. + public string RemovalPath => Path.Combine(UpgradeDirectory, WatchdogFiles.Removal); + /// Windows Installer's verbose log of one MSI run, beside the agent's own logs. public string MsiLogPath(string version, Guid upgradeId) => Path.Combine(LogDirectory, $"agent-msi-{version.Split('+')[0]}-{upgradeId:N}.log"); diff --git a/src/Agent.Watchdog/WatchdogService.cs b/src/Agent.Watchdog/WatchdogService.cs index 051d5ad..226d742 100644 --- a/src/Agent.Watchdog/WatchdogService.cs +++ b/src/Agent.Watchdog/WatchdogService.cs @@ -5,7 +5,13 @@ namespace Agent.Watchdog; /// left half done and tidies the install folder; then, every poll, it carries an /// upgrade on when one is waiting and otherwise supervises the agent. /// -public sealed class WatchdogService(WatchdogOptions options, UpgradeApplier applier, AgentSupervisor supervisor, IWatchdogClock clock, WatchdogLog log) +public sealed class WatchdogService( + WatchdogOptions options, + UpgradeApplier applier, + AgentSupervisor supervisor, + IWatchdogClock clock, + WatchdogLog log, + Removal.RemovalCoordinator? removal = null) { /// Runs until cancelled, or until the watchdog replaced itself (true: exit so the new binary takes over). public async Task RunAsync(CancellationToken cancellationToken) @@ -29,6 +35,12 @@ public sealed class WatchdogService(WatchdogOptions options, UpgradeApplier appl { try { + if (removal is not null && await removal.PassAsync(cancellationToken)) + { + await clock.DelayAsync(options.PollInterval, cancellationToken); + continue; + } + switch (await applier.PassAsync(cancellationToken)) { case PassResult.RestartRequested: diff --git a/src/Shared.Contracts/Models/DeviceRemovals.cs b/src/Shared.Contracts/Models/DeviceRemovals.cs index d6a93d5..6800ae1 100644 --- a/src/Shared.Contracts/Models/DeviceRemovals.cs +++ b/src/Shared.Contracts/Models/DeviceRemovals.cs @@ -80,7 +80,24 @@ public sealed record WatchdogRemoval( string Phase, int Attempts, string? LastError, - DateTimeOffset RequestedUtc); + DateTimeOffset RequestedUtc, + string? RunnerDirectory = null); + +/// How a removal runs on the device, shared by the agent, the watchdog and the runner. +public static class WatchdogRemovalFiles +{ + /// Hidden watchdog verb of the detached runner: remove-run <id> --install-dir … --data-dir … --runner-dir …. + public const string RunVerb = "remove-run"; + /// The same runner in the agent's binary, for a device where no watchdog can take the removal. + public const string AgentRunVerb = "remove-self"; + /// In : held open (no sharing) by the runner for as long as it runs. + public const string RunnerLock = "removal.lock"; + /// Attempts at uninstalling before the removal ends failed. + public const int MaxAttempts = 3; + public static readonly TimeSpan AttemptDelay = TimeSpan.FromMinutes(5); + /// A removal the watchdog has not taken by then: the agent starts the runner itself. + public static readonly TimeSpan PickupDeadline = TimeSpan.FromMinutes(2); +} /// Phases of . public static class WatchdogRemovalPhases diff --git a/src/Shared.Contracts/Models/WatchdogFiles.cs b/src/Shared.Contracts/Models/WatchdogFiles.cs index e354c42..c2639e8 100644 --- a/src/Shared.Contracts/Models/WatchdogFiles.cs +++ b/src/Shared.Contracts/Models/WatchdogFiles.cs @@ -40,6 +40,12 @@ public static class WatchdogFiles /// later upgrades brought; the newest 3 are kept. /// public const string KeptPackages = "packages"; + /// + /// Beside credentials.dat, one thumbprint per line: the server CA + /// certificates the agent itself added to the machine's trust store, the only + /// ones a removal takes out (the same CA may be there for the server's own sake). + /// + public const string ManagedCa = "managed-ca.txt"; public const int KeptPackagesMax = 3; /// The longest an MSI install (or the reinstall of the previous one) may run. public static readonly TimeSpan MsiInstallTimeout = TimeSpan.FromMinutes(30); diff --git a/tests/Agent.Watchdog.Tests/MsiUpgradeTests.cs b/tests/Agent.Watchdog.Tests/MsiUpgradeTests.cs index 7d9272a..d98d57c 100644 --- a/tests/Agent.Watchdog.Tests/MsiUpgradeTests.cs +++ b/tests/Agent.Watchdog.Tests/MsiUpgradeTests.cs @@ -25,6 +25,10 @@ public sealed class FakeWindowsInstaller(WatchdogFixture fixture) : IWindowsInst /// Set to block every install until released (a hung Windows Installer). public ManualResetEventSlim? Block { get; set; } + public int UninstallProduct(string productCode, string commandLine) => throw new InvalidOperationException("an upgrade uninstalls nothing"); + + public string? RelatedProduct(string upgradeCode) => null; + public void SetSilentUI() => SilentCalls++; public void EnableLog(string logPath) -- 2.52.0 From 1fc4569910cd33b61d6f00d0ff029e182091e8117ef37b935fcc6a41df7ae74f Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Sat, 31 Oct 2026 20:09:11 -0400 Subject: [PATCH 09/20] feat(agent): renew the credential over HTTPS and remove the agent on command or 410 A background service renews at every start and at half life, NATS connected or not; NATS reads the current credential at each connect. Nothing is replaced while an upgrade could roll back, and a 410 from an unverified server uninstalls nothing. The remove command checks its enrollment and refuses foreground runs and upgrades in progress; a device with no watchdog runs the same runner from a copy of the agent. Co-Authored-By: Claude Opus 5.5 --- src/Agent/Agent.csproj | 16 + src/Agent/Commands/AgentCli.cs | 5 + src/Agent/Commands/CommandProcessor.cs | 7 + src/Agent/Enrollment/CredentialRenewal.cs | 404 ++++++++++++++++++ src/Agent/Enrollment/CredentialStore.cs | 47 +- src/Agent/Enrollment/ServerTrust.cs | 24 +- .../Messaging/AgentNatsOptionsFactory.cs | 12 +- src/Agent/Service/AgentHost.cs | 45 ++ src/Agent/Service/AgentSession.cs | 17 +- src/Agent/Service/AgentWorker.cs | 3 +- src/Agent/Upgrade/AgentRemoval.cs | 295 +++++++++++++ src/Agent/Upgrade/AgentUpgradeHandler.cs | 11 +- src/Agent/Upgrade/UpgradeFolders.cs | 7 + .../JobPipelineIntegrationTests.cs | 20 +- 14 files changed, 894 insertions(+), 19 deletions(-) create mode 100644 src/Agent/Enrollment/CredentialRenewal.cs create mode 100644 src/Agent/Upgrade/AgentRemoval.cs diff --git a/src/Agent/Agent.csproj b/src/Agent/Agent.csproj index 5fa4593..40ea1f9 100644 --- a/src/Agent/Agent.csproj +++ b/src/Agent/Agent.csproj @@ -60,6 +60,22 @@ + + + + + + + + + + + + +