From b711b71f92447ade8895e13caf701466d6aa96ddfe2c00151cabf59ec85f63bd Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Wed, 4 Nov 2026 19:57:19 -0500 Subject: [PATCH 01/22] feat(groups): nested rule groups, rule validation and field value suggestions The rule set is now the outermost group: it holds conditions of its own beside nested groups, with its own All/Any and Not, and groups nest to eight levels. Rule sets stored in the older two-level shape (blocks of conditions) read, save and evaluate exactly as before; rules stay JSON in RulesJson, so no migration. Saving or previewing a rule checks it by row path: nesting depth, a 200-condition cap, a field on every row, patterns compiled with .NET regex semantics and CIDR ranges that parse. The field catalogue gains a type, fixed choices and a short description for the console's picker. GET /api/groups/fields/values?field=&prefix=&limit= returns a field's distinct values with device counts (groups.manage, limited to the caller's device scope, 25 by default and 100 at most). The preview and members endpoints now also stay inside the caller's device scope. Co-Authored-By: Claude Opus 5.5 --- src/Backend/Groups/DeviceGroupService.cs | 53 ++++ src/Backend/Groups/GroupEndpoints.cs | 92 ++++++- src/Shared.Contracts/Api/ApiRoutes.cs | 3 + src/Shared.Contracts/Api/ResourceDtos.cs | 6 + src/Shared.Contracts/Models/GroupRules.cs | 134 +++++++--- .../Grouping/RuleEvaluator.cs | 22 +- .../Grouping/RuleValidator.cs | 113 ++++++++ tests/Backend.Tests/GroupRuleEndpointTests.cs | 185 +++++++++++++ tests/Shared.Tests/RuleNestingTests.cs | 242 ++++++++++++++++++ 9 files changed, 795 insertions(+), 55 deletions(-) create mode 100644 src/Shared.Infrastructure/Grouping/RuleValidator.cs create mode 100644 tests/Backend.Tests/GroupRuleEndpointTests.cs create mode 100644 tests/Shared.Tests/RuleNestingTests.cs diff --git a/src/Backend/Groups/DeviceGroupService.cs b/src/Backend/Groups/DeviceGroupService.cs index fb6f19f..56e1e43 100644 --- a/src/Backend/Groups/DeviceGroupService.cs +++ b/src/Backend/Groups/DeviceGroupService.cs @@ -223,6 +223,59 @@ public sealed class DeviceGroupService(AppDbContext db, InventoryReportCache inv .ToList(); } + /// + /// The distinct values a rule field holds across the given devices (every + /// device when is null), read from the same field + /// bag rules evaluate, so a suggestion always matches as shown. Values that + /// differ only in case are one value, as rules compare them. Most-held + /// first, at most , optionally only those starting with + /// (ignoring case). + /// + public async Task> KnownValuesAsync( + string field, IReadOnlySet? allowed, string? prefix, int limit, CancellationToken cancellationToken) + { + var devices = await db.Devices.AsNoTracking().ToListAsync(cancellationToken); + if (allowed is not null) + { + devices = devices.Where(d => allowed.Contains(d.ClientId)).ToList(); + } + + if (devices.Count == 0) + { + return []; + } + + var inventory = await GetAllInventoryAsync(cancellationToken); + var now = DateTimeOffset.UtcNow; + var counts = new Dictionary(StringComparer.OrdinalIgnoreCase); + var start = prefix?.Trim(); + foreach (var device in devices) + { + if (!BuildFields(device, inventory.GetValueOrDefault(device.ClientId), now).TryGet(field, out var values)) + { + continue; + } + + // A device holding a value twice (two adapters, one DNS server) counts once for it. + foreach (var value in values.Select(v => v.Trim()).Distinct(StringComparer.OrdinalIgnoreCase)) + { + if (value.Length == 0 || (!string.IsNullOrEmpty(start) && !value.StartsWith(start, StringComparison.OrdinalIgnoreCase))) + { + continue; + } + + counts[value] = counts.TryGetValue(value, out var seen) ? (seen.Value, seen.Devices + 1) : (value, 1); + } + } + + return counts.Values + .OrderByDescending(v => v.Devices) + .ThenBy(v => v.Value, StringComparer.OrdinalIgnoreCase) + .Take(limit) + .Select(v => new GroupFieldValueDto(v.Value, v.Devices)) + .ToList(); + } + /// /// Groups a single device belongs to: the ones it is directly in, plus /// their ancestors (marked inherited, since nesting includes children). diff --git a/src/Backend/Groups/GroupEndpoints.cs b/src/Backend/Groups/GroupEndpoints.cs index fa51935..0da2f38 100644 --- a/src/Backend/Groups/GroupEndpoints.cs +++ b/src/Backend/Groups/GroupEndpoints.cs @@ -6,12 +6,18 @@ using Shared.Contracts.Api; using Shared.Contracts.Models; using Shared.Contracts.Serialization; using Shared.Infrastructure.Constants; +using Shared.Infrastructure.Grouping; using Shared.Infrastructure.Nats; namespace Backend.Groups; public static class GroupEndpoints { + /// Value suggestions: how many by default, at most, and the longest prefix accepted. + private const int DefaultValueLimit = 25; + private const int MaxValueLimit = 100; + private const int MaxValuePrefixLength = 200; + public static void Map(WebApplication app) { app.MapGet(ApiRoutes.Groups, ListAsync).RequirePermission(Permissions.GroupsView); @@ -27,6 +33,8 @@ public static class GroupEndpoints app.MapPost(ApiRoutes.GroupPreview, PreviewAsync).RequirePermission(Permissions.GroupsView); app.MapGet(ApiRoutes.GroupFields, () => Results.Ok(GroupFields.All)) .RequirePermission(Permissions.GroupsView); + // Suggestions serve the rule editor, so only those who may edit rules read device values through it. + app.MapGet(ApiRoutes.GroupFieldValues, FieldValuesAsync).RequirePermission(Permissions.GroupsManage); app.MapPut(ApiRoutes.DeviceTags, UpdateTagsAsync).RequirePermission(Permissions.DevicesManage); } @@ -67,6 +75,11 @@ public static class GroupEndpoints return Results.BadRequest(new { error = parentError }); } + if (RulesProblem(request) is { } rulesProblem) + { + return rulesProblem; + } + var now = DateTimeOffset.UtcNow; var entity = new DeviceGroupEntity { @@ -104,6 +117,11 @@ public static class GroupEndpoints return Results.BadRequest(new { error = parentError }); } + if (RulesProblem(request) is { } rulesProblem) + { + return rulesProblem; + } + // Only static groups keep an explicit list; switching kind drops it so // a later switch back does not resurrect stale members. if (request.Kind != GroupKind.Static && entity.Kind == GroupKind.Static) @@ -203,9 +221,13 @@ public static class GroupEndpoints return Results.NoContent(); } - /// Devices in the group, including those of its nested groups. + /// + /// Devices in the group, including those of its nested groups. A scoped + /// role sees only the members inside its device scope. + /// private static async Task MembersAsync( - Guid id, AppDbContext db, DeviceGroupService groups, CancellationToken cancellationToken) + Guid id, AppDbContext db, DeviceGroupService groups, DeviceScopeService scope, HttpContext context, + CancellationToken cancellationToken) { var entity = await db.DeviceGroups.AsNoTracking().SingleOrDefaultAsync(g => g.Id == id, cancellationToken); if (entity is null) @@ -213,7 +235,13 @@ public static class GroupEndpoints return Results.NotFound(); } - var clientIds = await groups.GetMembersAsync(entity, cancellationToken); + IEnumerable members = await groups.GetMembersAsync(entity, cancellationToken); + if (await scope.GetAllowedClientIdsAsync(context.User, cancellationToken) is { } allowed) + { + members = members.Where(allowed.Contains); + } + + var clientIds = members.ToList(); var devices = await db.Devices.AsNoTracking() .Where(d => clientIds.Contains(d.ClientId)) .OrderBy(d => d.Hostname) @@ -281,13 +309,28 @@ public static class GroupEndpoints return Results.NoContent(); } - /// Evaluates a candidate rule set without saving it (live preview). + /// + /// Evaluates a candidate rule set without saving it (live preview). A + /// scoped role sees only the matches inside its device scope. + /// private static async Task PreviewAsync( - PreviewGroupRequest request, AppDbContext db, DeviceGroupService groups, CancellationToken cancellationToken) + PreviewGroupRequest request, AppDbContext db, DeviceGroupService groups, DeviceScopeService scope, + HttpContext context, CancellationToken cancellationToken) { - var clientIds = await groups.EvaluateAsync(request.Rules, cancellationToken); + if (ValidationProblem(request.Rules) is { } rulesProblem) + { + return rulesProblem; + } + + IEnumerable clientIds = await groups.EvaluateAsync(request.Rules, cancellationToken); + if (await scope.GetAllowedClientIdsAsync(context.User, cancellationToken) is { } allowed) + { + clientIds = clientIds.Where(allowed.Contains); + } + + var matched = clientIds.ToList(); var devices = await db.Devices.AsNoTracking() - .Where(d => clientIds.Contains(d.ClientId)) + .Where(d => matched.Contains(d.ClientId)) .OrderBy(d => d.Hostname) .ToListAsync(cancellationToken); @@ -295,6 +338,41 @@ public static class GroupEndpoints return Results.Ok(devices.Select(d => Api.DeviceEndpoints.ToDto(d, now)).ToList()); } + /// + /// Suggestions for a rule's value: the field's distinct values on the + /// devices the caller may see, those starting with . + /// + private static async Task FieldValuesAsync( + string? field, string? prefix, int? limit, DeviceGroupService groups, DeviceScopeService scope, + HttpContext context, CancellationToken cancellationToken) + { + if (GroupFields.Find(field) is not { } descriptor) + { + return Results.BadRequest(new { error = "Unknown field." }); + } + + if (prefix is { Length: > MaxValuePrefixLength }) + { + return Results.BadRequest(new { error = $"Type at most {MaxValuePrefixLength} characters." }); + } + + var allowed = await scope.GetAllowedClientIdsAsync(context.User, cancellationToken); + return Results.Ok(await groups.KnownValuesAsync( + descriptor.Field, allowed, prefix, Math.Clamp(limit ?? DefaultValueLimit, 1, MaxValueLimit), cancellationToken)); + } + + /// The rules' problems by row path, when a dynamic group is saved with rules that break the limits. + private static IResult? RulesProblem(SaveDeviceGroupRequest request) + => request.Kind == GroupKind.Dynamic ? ValidationProblem(request.Rules) : null; + + private static IResult? ValidationProblem(GroupRuleSet? rules) + { + var errors = RuleValidator.Validate(rules); + return errors.Count == 0 + ? null + : Results.ValidationProblem(errors, detail: errors.Values.First()[0], title: "The rule needs a change."); + } + private static async Task UpdateTagsAsync( string clientId, UpdateDeviceTagsRequest request, AppDbContext db, CancellationToken cancellationToken) { diff --git a/src/Shared.Contracts/Api/ApiRoutes.cs b/src/Shared.Contracts/Api/ApiRoutes.cs index fd3ef99..349dbf8 100644 --- a/src/Shared.Contracts/Api/ApiRoutes.cs +++ b/src/Shared.Contracts/Api/ApiRoutes.cs @@ -61,6 +61,9 @@ public static class ApiRoutes public const string GroupMove = "/api/groups/{id}/move"; public const string GroupPreview = "/api/groups/preview"; public const string GroupFields = "/api/groups/fields"; + + /// Known values of one rule field (?field=&prefix=&limit=), from the caller's devices. + public const string GroupFieldValues = "/api/groups/fields/values"; public const string DeviceTags = "/api/devices/{clientId}/tags"; public const string Triggers = "/api/triggers"; diff --git a/src/Shared.Contracts/Api/ResourceDtos.cs b/src/Shared.Contracts/Api/ResourceDtos.cs index 72d9428..97c9ed2 100644 --- a/src/Shared.Contracts/Api/ResourceDtos.cs +++ b/src/Shared.Contracts/Api/ResourceDtos.cs @@ -58,6 +58,12 @@ public sealed record UpdateGroupMembersRequest( /// POST /api/groups/preview request body: evaluate rules without saving. public sealed record PreviewGroupRequest(GroupRuleSet Rules); +/// +/// One known value of a rule field (GET /api/groups/fields/values), with how +/// many of the caller's devices hold it now. +/// +public sealed record GroupFieldValueDto(string Value, int Devices); + /// PUT /api/devices/{clientId}/tags request body. public sealed record UpdateDeviceTagsRequest(IReadOnlyList Tags); diff --git a/src/Shared.Contracts/Models/GroupRules.cs b/src/Shared.Contracts/Models/GroupRules.cs index 148063a..294f711 100644 --- a/src/Shared.Contracts/Models/GroupRules.cs +++ b/src/Shared.Contracts/Models/GroupRules.cs @@ -57,8 +57,10 @@ public sealed record RuleCondition( bool Negate = false); /// -/// A group of conditions (and optionally nested groups) combined with AND/OR. -/// inverts the whole group. +/// A group of conditions and nested groups combined with AND/OR (All of / Any +/// of). inverts the whole group. A group with neither +/// conditions nor groups matches nothing. Writes are held to +/// . /// public sealed record RuleGroup( RuleCombine Combine = RuleCombine.And, @@ -67,57 +69,117 @@ public sealed record RuleGroup( bool Negate = false); /// -/// A dynamic group's membership rules: one or more condition groups combined -/// with AND/OR. +/// A dynamic group's membership rule: the outermost group. It holds conditions +/// and nested groups like any , with its own All/Any +/// () and Not (). Rule sets +/// saved before it held conditions of its own carry only +/// , and read and evaluate exactly as they always did. +/// An empty rule set matches no devices (safer than every device). /// public sealed record GroupRuleSet( RuleCombine Combine = RuleCombine.And, - IReadOnlyList? Groups = null) + IReadOnlyList? Groups = null, + IReadOnlyList? Conditions = null, + bool Negate = false) { public static GroupRuleSet Empty { get; } = new(RuleCombine.And, []); + + /// The rule set as the group it is, so one walk serves both. + public RuleGroup AsGroup() => new(Combine, Conditions, Groups, Negate); } -/// Field names usable in rules, with the kind of comparison they support. +/// Bounds on a rule set, enforced when one is saved or previewed. +public static class GroupRuleLimits +{ + /// Levels of grouping, the rule itself being the first: the rule, its groups, theirs… + public const int MaxDepth = 8; + + /// Conditions in one rule set, every level together. + public const int MaxConditions = 200; +} + +/// What a field holds: it decides the operators and the value input the console offers. +public enum GroupFieldType +{ + Text, + Number, + + /// Ordered as versions (1.10 after 1.9) by the comparison operators. + Version, + + /// IP addresses: the subnet (CIDR) operators apply. + Address, + + /// "true" or "false". + Boolean, + + /// One of a fixed set (). + Choice, +} + +/// +/// A field usable in rules. , +/// and drive the console's field picker, +/// operator list and value input; and +/// stay for older readers. +/// public sealed record GroupFieldDescriptor( string Field, string DisplayName, string Category, bool Numeric = false, - bool MultiValue = false); + bool MultiValue = false, + GroupFieldType Type = GroupFieldType.Text, + IReadOnlyList? Choices = null, + string? Description = null); /// The catalogue of rule-addressable device fields (also drives the UI). public static class GroupFields { + private static GroupFieldDescriptor Number(string field, string name, string category, string description) + => new(field, name, category, Numeric: true, Type: GroupFieldType.Number, Description: description); + + private static GroupFieldDescriptor Addresses(string field, string name, string description) + => new(field, name, "Network", MultiValue: true, Type: GroupFieldType.Address, Description: description); + public static readonly IReadOnlyList All = [ - new("hostname", "Hostname", "Identity"), - new("clientId", "Client ID", "Identity"), - new("status", "Status (online/stale/offline)", "Identity"), - new("tags", "Tags", "Identity", MultiValue: true), - new("agentVersion", "Agent version", "Identity"), - new("manufacturer", "Manufacturer", "Hardware"), - new("model", "Model", "Hardware"), - new("serialNumber", "Serial number", "Hardware"), - new("biosVersion", "BIOS version", "Hardware"), - new("systemType", "System type", "Hardware"), - new("domain", "Domain / workgroup", "Identity"), - new("partOfDomain", "Domain joined", "Identity"), - new("os.platform", "Platform (windows/linux/macos)", "Operating system"), - new("os.caption", "OS name", "Operating system"), - new("os.version", "OS version", "Operating system"), - new("os.build", "OS build", "Operating system"), - new("os.architecture", "OS architecture", "Operating system"), - new("os.edition", "OS edition", "Operating system"), - new("cpu.name", "CPU name", "Hardware"), - new("cpu.cores", "CPU cores", "Hardware", Numeric: true), - new("cpu.logicalProcessors", "CPU logical processors", "Hardware", Numeric: true), - new("memory.totalGb", "Memory total (GB)", "Hardware", Numeric: true), - new("disk.count", "Disk count", "Storage", Numeric: true), - new("disk.totalGb", "Disk total (GB)", "Storage", Numeric: true), - new("volume.freeGb", "Smallest volume free (GB)", "Storage", Numeric: true), - new("network.ipv4", "IPv4 address", "Network", MultiValue: true), - new("network.mac", "MAC address", "Network", MultiValue: true), - new("network.dns", "DNS server", "Network", MultiValue: true), - new("network.interface", "Interface name", "Network", MultiValue: true), + new("hostname", "Hostname", "Identity", Description: "The device's computer name."), + new("clientId", "Client ID", "Identity", Description: "The agent's enrolled identity."), + new("status", "Status", "Identity", Type: GroupFieldType.Choice, Choices: ["online", "stale", "offline"], + Description: "Online, stale or offline, from when the agent was last heard."), + new("tags", "Tags", "Identity", MultiValue: true, Description: "Tags set on the device; matches when any tag does."), + new("agentVersion", "Agent version", "Identity", Type: GroupFieldType.Version, + Description: "Compared as versions: 1.10 is newer than 1.9."), + new("domain", "Domain / workgroup", "Identity", Description: "The domain or workgroup the device is in."), + new("partOfDomain", "Domain joined", "Identity", Type: GroupFieldType.Boolean, Choices: ["true", "false"], + Description: "Whether the device is joined to a domain."), + new("manufacturer", "Manufacturer", "Hardware", Description: "System manufacturer, as the firmware reports it."), + new("model", "Model", "Hardware", Description: "System model."), + new("serialNumber", "Serial number", "Hardware", Description: "System serial number."), + new("biosVersion", "BIOS version", "Hardware", Description: "Firmware version."), + new("systemType", "System type", "Hardware", Description: "x64-based PC, ARM64-based PC…"), + new("cpu.name", "CPU name", "Hardware", Description: "Processor model."), + Number("cpu.cores", "CPU cores", "Hardware", "Physical processor cores."), + Number("cpu.logicalProcessors", "CPU logical processors", "Hardware", "Logical processors (threads)."), + Number("memory.totalGb", "Memory total (GB)", "Hardware", "Installed memory, in GB."), + new("os.platform", "Platform", "Operating system", Type: GroupFieldType.Choice, Choices: ["windows", "linux", "macos"], + Description: "Windows, Linux or macOS."), + new("os.caption", "OS name", "Operating system", Description: "Windows 11 Pro, Ubuntu 24.04 LTS…"), + new("os.version", "OS version", "Operating system", Description: "10.0.26100, 24.04…"), + new("os.build", "OS build", "Operating system", Description: "The OS build."), + new("os.architecture", "OS architecture", "Operating system", Description: "64-bit, ARM64…"), + new("os.edition", "OS edition", "Operating system", Description: "Professional, Enterprise, Server…"), + Number("disk.count", "Disk count", "Storage", "Physical disks."), + Number("disk.totalGb", "Disk total (GB)", "Storage", "Every disk together, in GB."), + Number("volume.freeGb", "Smallest volume free (GB)", "Storage", "Free space on the fullest volume, in GB."), + Addresses("network.ipv4", "IPv4 address", "Any of the device's IPv4 addresses."), + new("network.mac", "MAC address", "Network", MultiValue: true, Description: "Any adapter's MAC address."), + Addresses("network.dns", "DNS server", "Any DNS server an adapter uses."), + new("network.interface", "Interface name", "Network", MultiValue: true, Description: "Any adapter's interface name."), ]; + + /// The descriptor for a field name, matched as rules match it (ignoring case); null when unknown. + public static GroupFieldDescriptor? Find(string? field) + => All.FirstOrDefault(f => string.Equals(f.Field, field, StringComparison.OrdinalIgnoreCase)); } diff --git a/src/Shared.Infrastructure/Grouping/RuleEvaluator.cs b/src/Shared.Infrastructure/Grouping/RuleEvaluator.cs index ebc4239..97f6bcc 100644 --- a/src/Shared.Infrastructure/Grouping/RuleEvaluator.cs +++ b/src/Shared.Infrastructure/Grouping/RuleEvaluator.cs @@ -22,17 +22,14 @@ public static class RuleEvaluator => op is RuleOperator.NotEquals or RuleOperator.NotContains or RuleOperator.NotIn or RuleOperator.NotExists or RuleOperator.NotInCidr; + /// + /// The rule set is the outermost group: its own conditions and groups, + /// combined and optionally negated. A rule set saved with only groups (no + /// conditions of its own, never negated) evaluates as it always has. + /// public static bool Matches(GroupRuleSet? ruleSet, DeviceFieldBag fields) - { - var groups = ruleSet?.Groups; - if (groups is null || groups.Count == 0) - { - // A dynamic group with no rules matches nothing (safer than everything). - return false; - } - - return Combine(ruleSet!.Combine, groups.Select(g => Matches(g, fields))); - } + // A dynamic group with no rules matches nothing (safer than everything). + => ruleSet is not null && Matches(ruleSet.AsGroup(), fields); public static bool Matches(RuleGroup group, DeviceFieldBag fields) { @@ -50,6 +47,7 @@ public static class RuleEvaluator if (results.Count == 0) { + // Empty matches nothing, negated or not. return false; } @@ -181,7 +179,7 @@ public static class RuleEvaluator /// "10.30.9.27/24" means the same network as "10.30.9.0/24" (the strict /// BCL parser rejects non-zero host bits). /// - private static bool TryParseNetwork(string cidr, out IPNetwork network) + internal static bool TryParseNetwork(string cidr, out IPNetwork network) { network = default; var slash = cidr.IndexOf('/'); @@ -261,6 +259,6 @@ public static class RuleEvaluator }; } - private static IEnumerable SplitList(string value) + internal static IEnumerable SplitList(string value) => value.Split([',', ';', '\n'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); } diff --git a/src/Shared.Infrastructure/Grouping/RuleValidator.cs b/src/Shared.Infrastructure/Grouping/RuleValidator.cs new file mode 100644 index 0000000..2adc2bf --- /dev/null +++ b/src/Shared.Infrastructure/Grouping/RuleValidator.cs @@ -0,0 +1,113 @@ +using System.Net; +using System.Text.RegularExpressions; +using Shared.Contracts.Models; + +namespace Shared.Infrastructure.Grouping; + +/// +/// Checks a rule set before it is saved or previewed: nesting within +/// , at most +/// conditions, a field on every +/// condition, and a value the operator can use (a pattern .NET compiles, CIDR +/// ranges or addresses that parse). Problems are keyed by row path as the +/// console's builder numbers its rows: within a group its conditions come +/// first, then its groups, from 1; 2.1 is the first row of the group in +/// row 2; the rule's own is "". A rule set saved before still loads and +/// evaluates as it did; only one whose pattern or range never matched anything +/// (it could not be read) is asked to change when saved again. +/// +public static class RuleValidator +{ + public static Dictionary Validate(GroupRuleSet? rules) + { + var errors = new Dictionary(StringComparer.Ordinal); + if (rules is null) + { + return errors; + } + + var conditions = 0; + Walk(rules.AsGroup(), path: string.Empty, depth: 1, errors, ref conditions); + if (conditions > GroupRuleLimits.MaxConditions) + { + Add(errors, string.Empty, $"A rule holds at most {GroupRuleLimits.MaxConditions} conditions; this one has {conditions}."); + } + + return errors; + } + + /// Why the condition's value cannot work with its operator, or null when it can. + public static string? ValueProblem(RuleCondition condition) + { + var value = condition.Value ?? string.Empty; + switch (condition.Operator) + { + case RuleOperator.Regex: + try + { + _ = new Regex(value, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250)); + return null; + } + catch (ArgumentException ex) + { + // .NET says where and what: "Invalid pattern '(a' at offset 2. Not enough )'s." + return ex.Message; + } + + case RuleOperator.InCidr: + case RuleOperator.NotInCidr: + var items = RuleEvaluator.SplitList(value).ToList(); + if (items.Count == 0) + { + return "Enter a range such as 10.0.0.0/8, or an address."; + } + + var bad = items.FirstOrDefault(item => item.Contains('/') + ? !RuleEvaluator.TryParseNetwork(item, out _) + : !IPAddress.TryParse(item, out _)); + return bad is null ? null : $"“{bad}” is not a CIDR range or an IP address."; + + default: + return null; + } + } + + private static void Walk(RuleGroup group, string path, int depth, Dictionary errors, ref int conditions) + { + if (depth > GroupRuleLimits.MaxDepth) + { + // Below the limit is not walked: the one message says what to change. + Add(errors, path, $"Groups nest at most {GroupRuleLimits.MaxDepth} levels deep, the rule included."); + return; + } + + var row = 0; + foreach (var condition in group.Conditions ?? []) + { + row++; + conditions++; + if (string.IsNullOrWhiteSpace(condition?.Field)) + { + Add(errors, Child(path, row), "Choose a field."); + } + else if (ValueProblem(condition) is { } problem) + { + Add(errors, Child(path, row), problem); + } + } + + foreach (var child in group.Groups ?? []) + { + row++; + if (child is not null) + { + Walk(child, Child(path, row), depth + 1, errors, ref conditions); + } + } + } + + private static string Child(string path, int row) => path.Length == 0 ? $"{row}" : $"{path}.{row}"; + + private static void Add(Dictionary errors, string path, string message) + => errors[path] = errors.TryGetValue(path, out var existing) ? [.. existing, message] : [message]; +} diff --git a/tests/Backend.Tests/GroupRuleEndpointTests.cs b/tests/Backend.Tests/GroupRuleEndpointTests.cs new file mode 100644 index 0000000..8f0bb30 --- /dev/null +++ b/tests/Backend.Tests/GroupRuleEndpointTests.cs @@ -0,0 +1,185 @@ +using System.Net; +using System.Net.Http.Json; +using System.Text.Json; +using Backend.Data; +using Shared.Contracts.Api; +using Shared.Contracts.Models; +using Shared.Contracts.Serialization; +using Xunit; + +namespace Backend.Tests; + +/// +/// The condition builder's server side: nested rules saved and evaluated, +/// rule problems refused by row path, value suggestions and previews held to +/// the caller's device scope, and rule sets stored in the older shape still +/// matching as before. +/// +public class GroupRuleEndpointTests(BackendTestFixture fixture) : IClassFixture +{ + [Fact] + public async Task ANestedRuleSavesReadsBackAndMatches() + { + using var client = await fixture.SignedInAsync("rule-admin"); + await fixture.SeedDeviceAsync("NESTED-1", tags: ["nested-web"]); + await fixture.SeedDeviceAsync("NESTED-2", tags: ["nested-db"]); + await fixture.SeedDeviceAsync("NESTED-3", tags: ["nested-web", "nested-lab"]); + await fixture.SeedDeviceAsync("OTHER-4", tags: ["nested-web"]); + + // hostname starts with NESTED- and (tag web or tag db) and not (tag lab) + var rules = new GroupRuleSet( + RuleCombine.And, + Groups: + [ + new RuleGroup(RuleCombine.Or, [Tag("nested-web"), Tag("nested-db")]), + new RuleGroup(RuleCombine.And, [Tag("nested-lab")], Negate: true), + ], + Conditions: [new RuleCondition("hostname", RuleOperator.StartsWith, "NESTED-")]); + + var created = await client.PostAsJsonAsync(ApiRoutes.Groups, + new SaveDeviceGroupRequest("Nested", null, GroupKind.Dynamic, rules), JsonDefaults.Options); + created.EnsureSuccessStatusCode(); + var group = (await created.Content.ReadFromJsonAsync(JsonDefaults.Options))!; + + Assert.Equal(2, group.MemberCount); + Assert.Equal(rules.Combine, group.Rules!.Combine); + Assert.Equal("hostname", Assert.Single(group.Rules.Conditions!).Field); + Assert.True(group.Rules.Groups![1].Negate); + + var members = await client.GetFromJsonAsync>(ApiRoutes.For.GroupMembers(group.Id), JsonDefaults.Options); + Assert.Equal(["NESTED-1", "NESTED-2"], members!.Select(d => d.Hostname).Order()); + } + + [Fact] + public async Task ARuleSetStoredInTheOlderShapeStillMatchesAsBefore() + { + using var client = await fixture.SignedInAsync("rule-admin"); + await fixture.SeedDeviceAsync("LEGACY-1", tags: ["legacy-a"]); + await fixture.SeedDeviceAsync("LEGACY-2", tags: ["legacy-b"]); + await fixture.SeedDeviceAsync("LEGACY-3", tags: ["legacy-a", "legacy-off"]); + + // As the two-level builder saved it: blocks of conditions, combined; one block negated. + const string json = """ + {"combine":"and","groups":[ + {"combine":"or","conditions":[{"field":"tags","operator":"equals","value":"legacy-a","negate":false},{"field":"tags","operator":"equals","value":"legacy-b","negate":false}],"negate":false}, + {"combine":"and","conditions":[{"field":"tags","operator":"equals","value":"legacy-off","negate":false}],"negate":true}]} + """; + var id = Guid.NewGuid(); + await fixture.WithDbAsync(async db => + { + db.DeviceGroups.Add(new DeviceGroupEntity + { + Id = id, Name = "Legacy", Kind = GroupKind.Dynamic, RulesJson = json, + CreatedUtc = DateTimeOffset.UtcNow, UpdatedUtc = DateTimeOffset.UtcNow, + }); + await db.SaveChangesAsync(); + }); + + var group = await client.GetFromJsonAsync(ApiRoutes.For.Group(id), JsonDefaults.Options); + Assert.Equal(2, group!.MemberCount); + Assert.Null(group.Rules!.Conditions); + Assert.False(group.Rules.Negate); + Assert.Equal(2, group.Rules.Groups!.Count); + + // Saved back unchanged through the API, it still validates and matches the same devices. + var saved = await client.PutAsJsonAsync(ApiRoutes.For.Group(id), + new SaveDeviceGroupRequest("Legacy", null, GroupKind.Dynamic, group.Rules), JsonDefaults.Options); + saved.EnsureSuccessStatusCode(); + var members = await client.GetFromJsonAsync>(ApiRoutes.For.GroupMembers(id), JsonDefaults.Options); + Assert.Equal(["LEGACY-1", "LEGACY-2"], members!.Select(d => d.Hostname).Order()); + } + + [Fact] + public async Task ABadPatternOrRangeIsRefusedByRow() + { + using var client = await fixture.SignedInAsync("rule-admin"); + var rules = new GroupRuleSet( + RuleCombine.And, + Groups: [new RuleGroup(RuleCombine.Or, [new RuleCondition("network.ipv4", RuleOperator.InCidr, "10.0.0.0/8, 10.1.2.300/24")])], + Conditions: [new RuleCondition("hostname", RuleOperator.Regex, "^PC-(\\d+$")]); + + var save = await client.PostAsJsonAsync(ApiRoutes.Groups, + new SaveDeviceGroupRequest("Bad rule", null, GroupKind.Dynamic, rules), JsonDefaults.Options); + var preview = await client.PostAsJsonAsync(ApiRoutes.GroupPreview, new PreviewGroupRequest(rules), JsonDefaults.Options); + + foreach (var response in new[] { save, preview }) + { + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + var errors = await ErrorsAsync(response); + Assert.Equal(["1", "2.1"], errors.Keys.Order()); + Assert.Contains("10.1.2.300/24", errors["2.1"][0]); + } + + // .NET pattern syntax a browser would reject is fine here. + var dotnet = await client.PostAsJsonAsync(ApiRoutes.GroupPreview, new PreviewGroupRequest(new GroupRuleSet( + Conditions: [new RuleCondition("hostname", RuleOperator.Regex, "(?i)^pc-\\d+$")])), JsonDefaults.Options); + Assert.Equal(HttpStatusCode.OK, dotnet.StatusCode); + } + + [Fact] + public async Task ValueSuggestionsComeFromTheCallersDevices() + { + var a = await fixture.SeedDeviceAsync("VALS-1", tags: ["vals-alpha"]); + var b = await fixture.SeedDeviceAsync("VALS-2", tags: ["vals-alpha", "vals-beta"]); + await fixture.SeedDeviceAsync("VALS-3", tags: ["vals-gamma", "other-vals"]); + + using var admin = await fixture.SignedInAsync("rule-admin"); + Assert.Equal( + [("vals-alpha", 2), ("vals-beta", 1), ("vals-gamma", 1)], + await ValuesAsync(admin, "tags", "VALS-")); + Assert.Equal([("vals-alpha", 2)], await ValuesAsync(admin, "tags", "vals-", limit: 1)); + Assert.Contains(("VALS-3", 1), await ValuesAsync(admin, "hostname", "vals-")); + + using var scoped = await fixture.ScopedSignedInAsync("rule-scoped", a, b); + Assert.Equal([("vals-alpha", 2), ("vals-beta", 1)], await ValuesAsync(scoped, "tags", "vals-")); + + var unknown = await admin.GetAsync($"{ApiRoutes.GroupFieldValues}?field=nope"); + Assert.Equal(HttpStatusCode.BadRequest, unknown.StatusCode); + + using var viewer = await fixture.SignedInAsync("rule-viewer", "Viewer"); + var refused = await viewer.GetAsync($"{ApiRoutes.GroupFieldValues}?field=tags"); + Assert.Equal(HttpStatusCode.Forbidden, refused.StatusCode); + } + + [Fact] + public async Task PreviewAndMembersStayInsideTheCallersScope() + { + var inside = await fixture.SeedDeviceAsync("SCOPE-IN", tags: ["scope-rule"]); + await fixture.SeedDeviceAsync("SCOPE-OUT", tags: ["scope-rule"]); + var rules = new GroupRuleSet(Conditions: [Tag("scope-rule")]); + + using var admin = await fixture.SignedInAsync("rule-admin"); + var created = await admin.PostAsJsonAsync(ApiRoutes.Groups, + new SaveDeviceGroupRequest("Scope rule", null, GroupKind.Dynamic, rules), JsonDefaults.Options); + var group = (await created.Content.ReadFromJsonAsync(JsonDefaults.Options))!; + Assert.Equal(2, (await PreviewAsync(admin, rules)).Count); + + using var scoped = await fixture.ScopedSignedInAsync("rule-scope", inside); + Assert.Equal(["SCOPE-IN"], (await PreviewAsync(scoped, rules)).Select(d => d.Hostname)); + var members = await scoped.GetFromJsonAsync>(ApiRoutes.For.GroupMembers(group.Id), JsonDefaults.Options); + Assert.Equal(["SCOPE-IN"], members!.Select(d => d.Hostname)); + } + + private static RuleCondition Tag(string tag) => new("tags", RuleOperator.Equals, tag); + + private static async Task> ValuesAsync(HttpClient client, string field, string prefix, int? limit = null) + { + var url = $"{ApiRoutes.GroupFieldValues}?field={Uri.EscapeDataString(field)}&prefix={Uri.EscapeDataString(prefix)}" + + (limit is { } l ? $"&limit={l}" : string.Empty); + var values = await client.GetFromJsonAsync>(url, JsonDefaults.Options); + return values!.Select(v => (v.Value, v.Devices)).ToList(); + } + + private static async Task> PreviewAsync(HttpClient client, GroupRuleSet rules) + { + var response = await client.PostAsJsonAsync(ApiRoutes.GroupPreview, new PreviewGroupRequest(rules), JsonDefaults.Options); + response.EnsureSuccessStatusCode(); + return (await response.Content.ReadFromJsonAsync>(JsonDefaults.Options))!; + } + + private static async Task> ErrorsAsync(HttpResponseMessage response) + { + using var body = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); + return body.RootElement.GetProperty("errors").Deserialize>()!; + } +} diff --git a/tests/Shared.Tests/RuleNestingTests.cs b/tests/Shared.Tests/RuleNestingTests.cs new file mode 100644 index 0000000..8570aee --- /dev/null +++ b/tests/Shared.Tests/RuleNestingTests.cs @@ -0,0 +1,242 @@ +using System.Text.Json.Nodes; +using Shared.Contracts.Models; +using Shared.Contracts.Serialization; +using Shared.Infrastructure.Grouping; +using Xunit; + +namespace Shared.Tests; + +/// +/// Rules as the condition builder writes them now (conditions and groups at +/// every level, a Not on the rule itself), and proof that rule sets saved in +/// the older two-level shape read, save and match exactly as before. +/// +public class RuleNestingTests +{ + /// Rule sets as earlier versions stored them: the rule holds only blocks, each block only conditions. + public static readonly TheoryData LegacyRuleSets = + [ + // The seeded platform groups. + """{"combine":"and","groups":[{"combine":"and","conditions":[{"field":"os.platform","operator":"equals","value":"windows","negate":false}],"negate":false}]}""", + """{"combine":"and","groups":[{"combine":"and","conditions":[{"field":"os.platform","operator":"equals","value":"windows","negate":false},{"field":"os.caption","operator":"notContains","value":"Server","negate":false}],"negate":false}]}""", + // The development seed: (Dell or Lenovo) and (16 GB and a workstation tag). + """{"combine":"and","groups":[{"combine":"or","conditions":[{"field":"manufacturer","operator":"equals","value":"Dell Inc.","negate":false},{"field":"manufacturer","operator":"equals","value":"LENOVO","negate":false}],"negate":false},{"combine":"and","conditions":[{"field":"memory.totalGb","operator":"greaterOrEqual","value":"16","negate":false},{"field":"tags","operator":"contains","value":"workstation","negate":false}],"negate":false}]}""", + // Blocks combined with Any, a negated block, a negated condition, lists and ranges. + """{"combine":"or","groups":[{"combine":"and","conditions":[{"field":"status","operator":"equals","value":"offline","negate":false}],"negate":true},{"combine":"or","conditions":[{"field":"hostname","operator":"in","value":"PC-001, PC-002;KIOSK-9","negate":false},{"field":"network.ipv4","operator":"inCidr","value":"10.20.0.0/16, 192.168.1.0/24","negate":true}],"negate":false}]}""", + // An empty block among others, and a value-less operator. + """{"combine":"and","groups":[{"combine":"and","conditions":[],"negate":false},{"combine":"and","conditions":[{"field":"tags","operator":"exists","negate":false}],"negate":false}]}""", + """{"combine":"or","groups":[{"combine":"and","conditions":[],"negate":false},{"combine":"and","conditions":[{"field":"tags","operator":"notExists","negate":false}],"negate":false}]}""", + // No blocks at all, and a block nested inside a block (the contract always allowed it). + """{"combine":"and","groups":[]}""", + """{"combine":"and"}""", + """{"combine":"and","groups":[{"combine":"and","conditions":[{"field":"manufacturer","operator":"startsWith","value":"Dell","negate":false}],"groups":[{"combine":"or","conditions":[{"field":"cpu.cores","operator":"greaterThan","value":"4","negate":false},{"field":"agentVersion","operator":"lessThan","value":"1.4","negate":false}],"negate":true}],"negate":false}]}""", + ]; + + [Theory] + [MemberData(nameof(LegacyRuleSets))] + public void ALegacyRuleSetMatchesTheSameDevicesAsBefore(string json) + { + var rules = JsonDefaults.Deserialize(json)!; + + foreach (var (name, bag) in Fleet()) + { + Assert.True(LegacyMatches(rules, bag) == RuleEvaluator.Matches(rules, bag), $"{name}: {json}"); + } + } + + [Theory] + [MemberData(nameof(LegacyRuleSets))] + public void ALegacyRuleSetSavesBackWithoutLoss(string json) + { + var rules = JsonDefaults.Deserialize(json)!; + var saved = JsonNode.Parse(JsonDefaults.Serialize(rules))!.AsObject(); + + // Saving adds only the rule's own Not, off; every block and condition is kept as it was. + Assert.False(saved["negate"]!.GetValue()); + Assert.Null(saved["conditions"]); + saved.Remove("negate"); + Assert.True(JsonNode.DeepEquals(JsonNode.Parse(json), saved), saved.ToJsonString()); + + var again = JsonDefaults.Deserialize(saved.ToJsonString())!; + foreach (var (_, bag) in Fleet()) + { + Assert.Equal(RuleEvaluator.Matches(rules, bag), RuleEvaluator.Matches(again, bag)); + } + } + + [Theory] + [MemberData(nameof(LegacyRuleSets))] + public void ALegacyRuleSetIsStillValid(string json) + => Assert.Empty(RuleValidator.Validate(JsonDefaults.Deserialize(json))); + + [Fact] + public void TheRuleHoldsConditionsOfItsOwn() + { + var rules = new GroupRuleSet( + RuleCombine.And, + Groups: [new RuleGroup(RuleCombine.Or, [Is("status", "offline"), Is("tags", "kiosk")])], + Conditions: [Is("manufacturer", "Dell Inc.")]); + + Assert.True(RuleEvaluator.Matches(rules, Bag(tags: ["kiosk"]))); + Assert.False(RuleEvaluator.Matches(rules, Bag(tags: ["finance"]))); + Assert.False(RuleEvaluator.Matches(rules, Bag(manufacturer: "LENOVO", tags: ["kiosk"]))); + // Conditions alone, no groups. + Assert.True(RuleEvaluator.Matches(new GroupRuleSet(Conditions: [Is("hostname", "PC-001")]), Bag())); + } + + [Fact] + public void NotOnTheRuleInvertsIt() + { + var rules = new GroupRuleSet(RuleCombine.Or, Conditions: [Is("status", "offline"), Is("tags", "kiosk")], Negate: true); + + Assert.True(RuleEvaluator.Matches(rules, Bag(tags: ["finance"]))); + Assert.False(RuleEvaluator.Matches(rules, Bag(tags: ["kiosk"]))); + } + + [Fact] + public void AnEmptyRuleMatchesNothingEvenNegated() + { + Assert.False(RuleEvaluator.Matches(new GroupRuleSet(Negate: true), Bag())); + Assert.False(RuleEvaluator.Matches(new GroupRuleSet(Groups: [new RuleGroup(Negate: true)]), Bag())); + } + + [Fact] + public void GroupsNestToTheLimitAndEvaluateAtEveryLevel() + { + // hostname = PC-001 and not (tags has lab or (manufacturer = LENOVO and (cores > 16 or status = offline))) + var innermost = new RuleGroup(RuleCombine.Or, [ + new RuleCondition("cpu.cores", RuleOperator.GreaterThan, "16"), + Is("status", "offline"), + ]); + var middle = new RuleGroup(RuleCombine.And, [Is("manufacturer", "LENOVO")], [innermost]); + var outer = new RuleGroup(RuleCombine.Or, [Is("tags", "lab")], [middle], Negate: true); + var rules = new GroupRuleSet(RuleCombine.And, [outer], [Is("hostname", "PC-001")]); + + Assert.True(RuleEvaluator.Matches(rules, Bag())); + Assert.False(RuleEvaluator.Matches(rules, Bag(tags: ["lab"]))); + Assert.True(RuleEvaluator.Matches(rules, Bag(manufacturer: "LENOVO"))); + Assert.False(RuleEvaluator.Matches(rules, Bag(manufacturer: "LENOVO", status: DeviceStatus.Offline))); + Assert.Empty(RuleValidator.Validate(rules)); + } + + [Fact] + public void NestingPastTheLimitIsRefusedWhereItHappens() + { + // The rule is level 1; each group nests one level deeper, always as row 2 (after one condition). + RuleGroup Level(int remaining) + => new(RuleCombine.And, [Is("hostname", "PC-001")], remaining > 0 ? [Level(remaining - 1)] : null); + + var atLimit = new GroupRuleSet(RuleCombine.And, [Level(GroupRuleLimits.MaxDepth - 2)], [Is("hostname", "PC-001")]); + var pastLimit = new GroupRuleSet(RuleCombine.And, [Level(GroupRuleLimits.MaxDepth - 1)], [Is("hostname", "PC-001")]); + + Assert.Empty(RuleValidator.Validate(atLimit)); + var errors = RuleValidator.Validate(pastLimit); + var path = string.Join('.', Enumerable.Repeat("2", GroupRuleLimits.MaxDepth)); + Assert.Equal([path], errors.Keys); + Assert.Contains($"{GroupRuleLimits.MaxDepth} levels", errors[path][0]); + } + + [Fact] + public void RowsAreNumberedConditionsFirstThenGroups() + { + var rules = new GroupRuleSet( + RuleCombine.And, + Groups: [new RuleGroup(RuleCombine.And, [Is("hostname", "a"), new RuleCondition(" ", RuleOperator.Equals, "b")])], + Conditions: [new RuleCondition("", RuleOperator.Exists)]); + + var errors = RuleValidator.Validate(rules); + + Assert.Equal(["1", "2.2"], errors.Keys.Order()); + Assert.Equal("Choose a field.", errors["2.2"][0]); + } + + [Theory] + [InlineData(RuleOperator.Regex, "^PC-\\d+$", true)] + [InlineData(RuleOperator.Regex, "(?i)^pc-(?\\d+)$", true)] // .NET syntax a browser would refuse + [InlineData(RuleOperator.Regex, "^PC-(\\d+$", false)] + [InlineData(RuleOperator.Regex, "[z-a]", false)] + [InlineData(RuleOperator.InCidr, "10.0.0.0/8, 192.168.1.7", true)] + [InlineData(RuleOperator.InCidr, "10.30.9.27/24", true)] // host bits are tolerated, as evaluation does + [InlineData(RuleOperator.NotInCidr, "fd00::/8", true)] + [InlineData(RuleOperator.InCidr, "10.0.0.0/33", false)] + [InlineData(RuleOperator.InCidr, "10.0.0.0/8, office", false)] + [InlineData(RuleOperator.NotInCidr, " ", false)] + [InlineData(RuleOperator.Contains, "(", true)] // only patterns are read as patterns + public void AValueTheOperatorCannotReadIsRefused(RuleOperator op, string value, bool valid) + { + var rules = new GroupRuleSet(Conditions: [new RuleCondition("network.ipv4", op, value)]); + + var errors = RuleValidator.Validate(rules); + + Assert.Equal(valid, errors.Count == 0); + if (!valid) + { + Assert.Equal(["1"], errors.Keys); + } + } + + [Fact] + public void TooManyConditionsAreRefusedOnTheRule() + { + var many = Enumerable.Range(0, GroupRuleLimits.MaxConditions + 1).Select(i => Is("hostname", $"PC-{i}")).ToList(); + var rules = new GroupRuleSet(RuleCombine.Or, [new RuleGroup(RuleCombine.Or, many)]); + + Assert.Equal([""], RuleValidator.Validate(rules).Keys); + } + + [Fact] + public void EveryFieldDescribesItsType() + { + Assert.All(GroupFields.All, f => Assert.False(string.IsNullOrWhiteSpace(f.Description))); + Assert.All(GroupFields.All.Where(f => f.Type is GroupFieldType.Choice or GroupFieldType.Boolean), + f => Assert.NotEmpty(f.Choices!)); + Assert.All(GroupFields.All.Where(f => f.Type == GroupFieldType.Number), f => Assert.True(f.Numeric)); + Assert.Equal(GroupFields.All.Count, GroupFields.All.Select(f => f.Field).Distinct(StringComparer.OrdinalIgnoreCase).Count()); + Assert.Same(GroupFields.All.Single(f => f.Field == "os.platform"), GroupFields.Find("OS.Platform")); + } + + /// The rule set's evaluation before the rule held conditions of its own: its blocks, combined. + private static bool LegacyMatches(GroupRuleSet ruleSet, DeviceFieldBag fields) + { + var groups = ruleSet.Groups; + if (groups is null || groups.Count == 0) + { + return false; + } + + var results = groups.Select(g => RuleEvaluator.Matches(g, fields)); + return ruleSet.Combine == RuleCombine.Or ? results.Any(r => r) : results.All(r => r); + } + + private static RuleCondition Is(string field, string value) => new(field, RuleOperator.Equals, value); + + private static IEnumerable<(string Name, DeviceFieldBag Bag)> Fleet() + { + yield return ("dell workstation", Bag(tags: ["workstation"])); + yield return ("lenovo offline", Bag(manufacturer: "LENOVO", status: DeviceStatus.Offline)); + yield return ("server", Bag(os: "Windows Server 2022 Datacenter", memoryBytes: 8L << 30)); + yield return ("ubuntu kiosk", Bag(os: "Ubuntu 24.04 LTS", hostname: "KIOSK-9", tags: ["kiosk"], ips: ["192.168.1.40"])); + yield return ("hp untagged", Bag(manufacturer: "HP", hostname: "PC-002", cores: 2, ips: ["172.16.0.5"])); + yield return ("no inventory", DeviceFieldBag.Build("c9", "NEW-1", DeviceStatus.Stale, "1.3.0", null, null, "Windows 11 Pro")); + } + + private static DeviceFieldBag Bag( + string manufacturer = "Dell Inc.", + string os = "Windows 11 Pro", + int cores = 8, + long memoryBytes = 34_359_738_368, + string[]? ips = null, + DeviceStatus status = DeviceStatus.Online, + string[]? tags = null, + string hostname = "PC-001") + => DeviceFieldBag.Build("c1", hostname, status, "1.4.2", tags, new InventoryReport( + new InventoryIdentity("c1", hostname, manufacturer, "OptiPlex 7090", "SN1", "1.2.3", "BB1", "CORP", true, "x64-based PC"), + new InventoryOs(os, "10.0.26100", "26100", "64-bit", null, null, "Professional"), + new InventoryCpu("Intel Core i7", cores * 2, cores), + [], + memoryBytes, + [new InventoryDisk("SSD", "S1", 1_000_000_000_000, "NVMe")], + [new InventoryVolume("C:", "NTFS", 1_000_000_000_000, 500_000_000_000)], + [new InventoryNetworkAdapter("NIC", "00:11:22:33:44:55", ips ?? ["10.20.4.14"], [], ["10.20.0.1"], ["10.20.0.10"], InterfaceName: "eth0")], + DateTimeOffset.UtcNow)); +} -- 2.52.0 From bf55c6fc84e9da25d2c68e5ae59813b14cef093b1f6485b2858af77bda68bc73 Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Wed, 4 Nov 2026 19:58:54 -0500 Subject: [PATCH 02/22] fix(install): install scripts hand out the newest agent The package route served the first Agent.msi it found, AGENT_PACKAGE_PATH before the build baked into the image, and offload reused whatever S3 key was saved once. An old package left in a volume or uploaded once was installed forever, and every new device was upgraded straight after. It now serves the catalog's newest build for the platform (presigned when offloaded), unless this node holds a newer one. Local lookup picks the newest copy by VERSION marker, and the catalog imports every copy it finds. Co-Authored-By: Claude Opus 5.5 --- src/Backend/Enrollment/InstallEndpoints.cs | 38 +++++++++++++--- src/Backend/Files/AgentPackageService.cs | 50 ++++++++++++++-------- tests/Backend.Tests/AgentUpgradeTests.cs | 22 ++++++++++ 3 files changed, 85 insertions(+), 25 deletions(-) diff --git a/src/Backend/Enrollment/InstallEndpoints.cs b/src/Backend/Enrollment/InstallEndpoints.cs index 15dcb60..e649f71 100644 --- a/src/Backend/Enrollment/InstallEndpoints.cs +++ b/src/Backend/Enrollment/InstallEndpoints.cs @@ -1,6 +1,7 @@ using Backend.Proxy; using Backend.Security; using Shared.Contracts.Api; +using Shared.Contracts.Models; namespace Backend.Enrollment; @@ -86,22 +87,44 @@ public static class InstallEndpoints return Results.Text(script, "text/plain; charset=utf-8"); } + /// + /// The newest agent there is: the catalog's newest build for the platform + /// unless this node holds a newer one than the catalog has caught up with. + /// A new device then starts on the version upgrades would take it to, + /// instead of an old package left in a volume and upgraded straight after. + /// private static async Task GetAgentPackage( - Files.AgentPackageService packages, string? type, CancellationToken cancellationToken) + Files.AgentPackageService packages, Upgrades.AgentCatalogService catalog, Upgrades.AgentCatalogStorage storage, + string? type, CancellationToken cancellationToken) { var packageType = Files.AgentPackageService.NormalizeType(type); var fileName = Files.AgentPackageService.FileNameFor(packageType); + var path = packages.ResolveLocalPath(packageType); + var localVersion = path is null ? null : Files.AgentPackageService.ReadVersionMarker(Path.GetDirectoryName(path)); - // When S3 offload is on, hand the endpoint a presigned URL so the - // transfer never touches the control plane. Falls through to local - // delivery if presigning fails for any reason. + if ((await catalog.NewestByTypeAsync(cancellationToken)).GetValueOrDefault(packageType) is { } newest + && (path is null || !AgentVersions.IsNewer(localVersion, newest.Version))) + { + // Offloaded: a presigned URL, so the transfer never touches the control plane. + if (newest.S3Key is { } key && await packages.TryPresignOffloadedAsync(key, cancellationToken) is { } url) + { + return Results.Redirect(url, permanent: false); + } + + var catalogued = storage.FullPath(newest.StoragePath); + if (File.Exists(catalogued)) + { + return Results.File(catalogued, ContentTypeFor(packageType), fileName, enableRangeProcessing: true); + } + } + + // Nothing catalogued yet (an unversioned build, or the first sync still running). var offloadUrl = await packages.TryGetOffloadUrlAsync(packageType, cancellationToken); if (offloadUrl is not null) { return Results.Redirect(offloadUrl, permanent: false); } - var path = packages.ResolveLocalPath(packageType); if (path is null) { return Results.NotFound(new @@ -112,11 +135,12 @@ public static class InstallEndpoints }); } - var contentType = packageType == "msi" ? "application/x-msi" : "application/zip"; var stream = File.OpenRead(path); - return Results.Stream(stream, contentType, fileDownloadName: fileName, enableRangeProcessing: true); + return Results.Stream(stream, ContentTypeFor(packageType), fileDownloadName: fileName, enableRangeProcessing: true); } + private static string ContentTypeFor(string packageType) => packageType == "msi" ? "application/x-msi" : "application/zip"; + private static string ResolveBaseUrl(HttpContext context, IConfiguration configuration) => Backend.Proxy.PublicUrl.Resolve(context, configuration); diff --git a/src/Backend/Files/AgentPackageService.cs b/src/Backend/Files/AgentPackageService.cs index 214c5ef..57267bc 100644 --- a/src/Backend/Files/AgentPackageService.cs +++ b/src/Backend/Files/AgentPackageService.cs @@ -2,18 +2,22 @@ using System.Security.Cryptography; using Amazon.S3; using Amazon.S3.Model; using Shared.Contracts.Api; +using Shared.Contracts.Models; namespace Backend.Files; /// /// Locates the agent installer that the URL-install flow hands out. /// -/// Search order: +/// Places searched: /// 1. AGENT_PACKAGE_PATH (compose defaults it to /data/agent, which is a /// host bind mount) — dropping a package there delivers an out-of-band /// agent update with no new backend image or restart /// 2. /opt/executer/agent — baked into the backend image by CI /// 3. /data/agent — fallback when AGENT_PACKAGE_PATH points elsewhere +/// The newest by VERSION marker wins, so a package left behind in a volume +/// never shadows the newer one a later image brings; between equal versions +/// the earlier place wins. /// /// CI copies the Windows-built package into the image, so a stock deployment /// serves the agent with no manual step. When S3 offload is enabled the same @@ -55,22 +59,32 @@ public sealed class AgentPackageService( yield return SeededPath; } - /// Full path to the package on local disk, or null when absent. - public string? ResolveLocalPath(string packageType) + /// Full path to the newest package on local disk, or null when absent. + public string? ResolveLocalPath(string packageType) => LocalPackages(packageType).FirstOrDefault().Path; + + /// Every copy of the package on local disk, newest first; one without a VERSION marker counts as oldest. + private List<(string Path, string? Version)> LocalPackages(string packageType) { var fileName = FileNameFor(packageType); - foreach (var directory in CandidateDirectories()) - { - var path = Path.Combine(directory, fileName); - if (File.Exists(path)) - { - return path; - } - } - - return null; + return CandidateDirectories() + .Select(directory => Path.GetFullPath(Path.Combine(directory, fileName))) + .Distinct(OperatingSystem.IsWindows() ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal) + .Where(File.Exists) + .Select(path => (Path: path, Version: ReadVersionMarker(Path.GetDirectoryName(path)))) + // Stable: equal versions keep the search order. + .OrderByDescending(package => package.Version, MarkedVersions) + .ToList(); } + private static readonly IComparer MarkedVersions = Comparer.Create( + (left, right) => (AgentVersions.IsValid(left), AgentVersions.IsValid(right)) switch + { + (true, true) => AgentVersions.Compare(left, right), + (true, false) => 1, + (false, true) => -1, + _ => 0, + }); + public async Task GetStatusAsync(string packageType, CancellationToken cancellationToken) { var offloadEnabled = await s3Settings.GetAgentOffloadEnabledAsync(cancellationToken); @@ -108,16 +122,16 @@ public sealed class AgentPackageService( /// /// The upgradable packages visible right now (the MSI, the portable zip and - /// the Unix builds), each with the version from its VERSION marker: - /// what the agent catalog imports. + /// the Unix builds) in every place searched, each with the version from + /// its VERSION marker: what the agent catalog imports. /// public IEnumerable<(string PackageType, string Path, string? Version)> LocateUpgradePackages() { - foreach (var packageType in Shared.Contracts.Models.AgentPackageTypes.Upgradable) + foreach (var packageType in AgentPackageTypes.Upgradable) { - if (ResolveLocalPath(packageType) is { } path) + foreach (var (path, version) in LocalPackages(packageType)) { - yield return (packageType, path, ReadVersionMarker(System.IO.Path.GetDirectoryName(path))); + yield return (packageType, path, version); } } } diff --git a/tests/Backend.Tests/AgentUpgradeTests.cs b/tests/Backend.Tests/AgentUpgradeTests.cs index 363b116..ec01c46 100644 --- a/tests/Backend.Tests/AgentUpgradeTests.cs +++ b/tests/Backend.Tests/AgentUpgradeTests.cs @@ -143,6 +143,28 @@ public class AgentCatalogTests(BackendTestFixture fixture) : IClassFixtureStarting upgrades and roll backs, cancelling, listing. -- 2.52.0 From 624f768478f6f75170c3e376c4c53fa04abea1afbc8238eda33b5f2fa8f5ecb9 Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Wed, 4 Nov 2026 19:58:55 -0500 Subject: [PATCH 03/22] feat(devices): removed devices leave at once; right-click a device - A removed device leaves the Devices list immediately; the Removals filter keeps the history. ShowInDeviceList and the 7-day window are gone. - An agent too old to take the remove command, or one that can't uninstall itself (foreground, container), no longer keeps its removal open: it is removed from the system straight away, its commands cleared, the agent left on the device ("Agent Left on Device"). Its later reports don't reopen it; enrolling again still needs a new token. - Right-click a device row for its actions menu; on a chosen row it acts on every chosen device. Co-Authored-By: Claude Opus 5.5 --- .../Removals/DeviceRemovalEndpoints.cs | 8 +- .../Removals/DeviceRemovalScheduler.cs | 57 ++++++++++---- src/Backend/Removals/DeviceRemovalService.cs | 16 ++-- src/Shared.Contracts/Api/DeviceRemovalDtos.cs | 9 +-- src/Shared.Contracts/Models/DeviceRemovals.cs | 10 +-- src/WebUI/src/api/types.ts | 1 - src/WebUI/src/components/DataTable.tsx | 11 +++ src/WebUI/src/pages/devices/index.tsx | 74 +++++++++---------- .../src/sections/devices/removalLabels.ts | 9 ++- tests/Backend.Tests/DeviceRemovalTests.cs | 54 ++++++++------ 10 files changed, 147 insertions(+), 102 deletions(-) diff --git a/src/Backend/Removals/DeviceRemovalEndpoints.cs b/src/Backend/Removals/DeviceRemovalEndpoints.cs index 4cdba29..f01d3fe 100644 --- a/src/Backend/Removals/DeviceRemovalEndpoints.cs +++ b/src/Backend/Removals/DeviceRemovalEndpoints.cs @@ -52,8 +52,7 @@ public static class DeviceRemovalEndpoints .Take(take) .ToList(); - var now = DateTimeOffset.UtcNow; - return Results.Ok(rows.Select(r => DeviceRemovalService.ToDto(r, now)).ToList()); + return Results.Ok(rows.Select(DeviceRemovalService.ToDto).ToList()); } private static async Task RemoveAsync( @@ -131,9 +130,8 @@ public static class DeviceRemovalEndpoints 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(), + removed.OrderBy(r => r.Hostname, StringComparer.OrdinalIgnoreCase).Select(DeviceRemovalService.ToDto).ToList(), skipped)); } @@ -184,6 +182,6 @@ public static class DeviceRemovalEndpoints 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)); + return Results.Ok(DeviceRemovalService.ToDto(row)); } } diff --git a/src/Backend/Removals/DeviceRemovalScheduler.cs b/src/Backend/Removals/DeviceRemovalScheduler.cs index 68c8fcd..52daff2 100644 --- a/src/Backend/Removals/DeviceRemovalScheduler.cs +++ b/src/Backend/Removals/DeviceRemovalScheduler.cs @@ -53,8 +53,9 @@ 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. +/// . An agent too old to take it, or one that can't +/// uninstall itself there, is removed from the system at once and left on the +/// device. /// 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 @@ -165,22 +166,20 @@ public sealed class DeviceRemovalScheduler( 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. + // Nothing to ask: the device is gone from the system all the same, and only its agent stays. 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); - } - } + await CloseUnremovableAsync(db, removals, row, now, DeviceRemovalReasons.AgentTooOld, + $"The agent runs {row.AgentVersion}, too old to uninstall itself; it stays on the device.", cancellationToken); + return SendResult.NotDue; + } + if (row is { State: DeviceRemovalStates.Failed, Reason: DeviceRemovalReasons.Unsupported }) + { + await CloseUnremovableAsync(db, removals, row, now, DeviceRemovalReasons.Unsupported, + row.Detail ?? "The agent can't uninstall itself here; it stays on the device.", cancellationToken); return SendResult.NotDue; } @@ -218,6 +217,38 @@ public sealed class DeviceRemovalScheduler( return SendResult.Sent; } + /// + /// Closes the removal of an agent that can't take it away itself: removed + /// from the system, its commands cleared, the agent left where it is. Its + /// later sightings don't reopen it, and enrolling again still needs a new token. + /// + private async Task CloseUnremovableAsync( + AppDbContext db, DeviceRemovalService removals, DeviceRemovalEntity row, DateTimeOffset now, + string reason, string detail, CancellationToken cancellationToken) + { + 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; + } + + row.State = DeviceRemovalStates.Removed; + row.Reason = reason; + row.Detail = detail; + row.FinishedUtc = now; + row.UpdatedUtc = now; + DeviceHistory.RecordRemoved(db, row.ClientId, detail); + if (await TrySaveAsync(db, row, cancellationToken)) + { + await removals.AnnounceAsync(row, cancellationToken); + } + } + /// True when the removal closed. private async Task AdvanceUninstallingAsync( AppDbContext db, DeviceRemovalService removals, DeviceRemovalEntity row, DateTimeOffset now, CancellationToken cancellationToken) diff --git a/src/Backend/Removals/DeviceRemovalService.cs b/src/Backend/Removals/DeviceRemovalService.cs index 6cb34e0..813632f 100644 --- a/src/Backend/Removals/DeviceRemovalService.cs +++ b/src/Backend/Removals/DeviceRemovalService.cs @@ -40,13 +40,15 @@ public sealed class DeviceRemovalService( DeviceRemovalStates.Uninstalling, DeviceRemovalStates.Failed, }; - public static DeviceRemovalDto ToDto(DeviceRemovalEntity row, DateTimeOffset now) => new( + public static DeviceRemovalDto ToDto(DeviceRemovalEntity row) => 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)); + row.FinishedUtc); - public static bool ShowInDeviceList(DeviceRemovalEntity row, DateTimeOffset now) - => now - row.UpdatedUtc < DeviceRemovalDefaults.VisibleInDeviceList; + /// Removed from the system with its agent left installed: too old, or unable to uninstall itself there. + public static bool LeftOnDevice(DeviceRemovalEntity row) + => row.State == DeviceRemovalStates.Removed + && row.Reason is DeviceRemovalReasons.AgentTooOld or DeviceRemovalReasons.Unsupported; /// /// For each device: its tombstone in, its row and everything that would @@ -203,7 +205,8 @@ public sealed class DeviceRemovalService( /// 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. + /// removal stays stopped, and so does one whose agent was left on the + /// device. True when a removal took the sighting. /// public async Task NoteSightingAsync( string? subjectClientId, string clientId, string? agentVersion, CancellationToken cancellationToken) @@ -220,7 +223,8 @@ public sealed class DeviceRemovalService( } var now = DateTimeOffset.UtcNow; - var reopened = row.State == DeviceRemovalStates.Removed; + // One removed while its agent stayed on the device is expected to keep talking. + var reopened = row.State == DeviceRemovalStates.Removed && !LeftOnDevice(row); row.LastSeenUtc = now; row.AgentVersion = string.IsNullOrWhiteSpace(agentVersion) ? row.AgentVersion : agentVersion; if (reopened) diff --git a/src/Shared.Contracts/Api/DeviceRemovalDtos.cs b/src/Shared.Contracts/Api/DeviceRemovalDtos.cs index 546c090..6a6ab7b 100644 --- a/src/Shared.Contracts/Api/DeviceRemovalDtos.cs +++ b/src/Shared.Contracts/Api/DeviceRemovalDtos.cs @@ -10,11 +10,7 @@ public sealed record RemoveDevicesRequest( 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. -/// +/// One device's removal: what the Removals filter lists, the device itself having left the list. public sealed record DeviceRemovalDto( Guid Id, string ClientId, @@ -31,8 +27,7 @@ public sealed record DeviceRemovalDto( DateTimeOffset UpdatedUtc, DateTimeOffset? SentUtc, DateTimeOffset? LastSeenUtc, - DateTimeOffset? FinishedUtc, - bool ShowInDeviceList); + DateTimeOffset? FinishedUtc); /// A device a removal request left out, and why. public sealed record DeviceRemovalSkipDto(string ClientId, string? Hostname, string Reason); diff --git a/src/Shared.Contracts/Models/DeviceRemovals.cs b/src/Shared.Contracts/Models/DeviceRemovals.cs index 6800ae1..6c01a6a 100644 --- a/src/Shared.Contracts/Models/DeviceRemovals.cs +++ b/src/Shared.Contracts/Models/DeviceRemovals.cs @@ -28,9 +28,9 @@ public static class DeviceRemovalStates /// 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. + /// The agent is too old to take a remove command: removed from the system at once, the agent left on the device. public const string AgentTooOld = "agent-too-old"; - /// The agent cannot remove itself here (foreground or development run, container). + /// The agent cannot remove itself here (foreground or development run, container): removed from the system, the agent left on the device. public const string Unsupported = "unsupported"; /// An agent upgrade is in progress; the server asks again. public const string Busy = "busy"; @@ -45,12 +45,6 @@ public static class DeviceRemovalReasons /// 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. diff --git a/src/WebUI/src/api/types.ts b/src/WebUI/src/api/types.ts index 1e990ee..42e0ff5 100644 --- a/src/WebUI/src/api/types.ts +++ b/src/WebUI/src/api/types.ts @@ -270,7 +270,6 @@ export interface DeviceRemovalDto { sentUtc?: string | null; lastSeenUtc?: string | null; finishedUtc?: string | null; - showInDeviceList: boolean; } export interface RemoveDevicesBody { diff --git a/src/WebUI/src/components/DataTable.tsx b/src/WebUI/src/components/DataTable.tsx index 4b320eb..c500103 100644 --- a/src/WebUI/src/components/DataTable.tsx +++ b/src/WebUI/src/components/DataTable.tsx @@ -48,6 +48,8 @@ interface Props { error?: string; empty?: ReactNode; onRowClick?: (row: T) => void; + /** Right-click on a row; the browser's own menu is suppressed only when this is set. */ + onRowContextMenu?: (row: T, position: { top: number; left: number }) => void; selection?: Selection; /** Filters and bulk actions, above the header row. */ toolbar?: ReactNode; @@ -87,6 +89,7 @@ export default function DataTable({ error, empty, onRowClick, + onRowContextMenu, selection, toolbar, defaultSort, @@ -210,6 +213,14 @@ export default function DataTable({ hover selected={isChosen || activeRowId === id} onClick={onRowClick ? () => onRowClick(row) : undefined} + onContextMenu={ + onRowContextMenu + ? (e) => { + e.preventDefault(); + onRowContextMenu(row, { top: e.clientY, left: e.clientX }); + } + : undefined + } sx={{ cursor: onRowClick ? 'pointer' : 'default', ...(dimmedRow?.(row) && { '& > td': { opacity: 0.65 } }) }} > {selection && ( diff --git a/src/WebUI/src/pages/devices/index.tsx b/src/WebUI/src/pages/devices/index.tsx index 356ef54..07d7f30 100644 --- a/src/WebUI/src/pages/devices/index.tsx +++ b/src/WebUI/src/pages/devices/index.tsx @@ -147,7 +147,8 @@ export default function DevicesPage() { ); const [chosen, setChosen] = useState>(new Set()); - const [menu, setMenu] = useState<{ anchor: HTMLElement; devices: DeviceDto[] } | null>(null); + // Opened by a row's "…" button, the Actions button, or a right-click (a point). + const [menu, setMenu] = useState<{ anchor?: HTMLElement; position?: { top: number; left: number }; devices: DeviceDto[] } | null>(null); const [groupMenu, setGroupMenu] = useState(null); const [addOpen, setAddOpen] = useState(false); const [historyOf, setHistoryOf] = useState(null); @@ -174,23 +175,14 @@ export default function DevicesPage() { const passes = (d: ListRow) => (!query || matches(d, query)) && (platform === 'all' || platformOf(d.osCaption) === platform); if (showingRemovals) return (removals ?? []).map(removalRow).filter(passes); - const listed = inScope.filter((d) => { + // A removed device leaves the list at once; the Removals filter keeps its history. + return 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]); + }, [inScope, removals, search, showingRemovals, status, platform]); - const chosenRows = rows.filter((d) => !d.removal && chosen.has(d.clientId)); + const chosenRows = rows.filter((d) => chosen.has(d.clientId)); const groupName = (id: string) => groups?.find((g) => g.id === id)?.name ?? '…'; const title = showingRemovals @@ -296,8 +288,8 @@ export default function DevicesPage() { { id: 'status', header: 'Status', - sortValue: (d) => d.removal?.state ?? (d.isOrphaned ? 'z' : d.status), - cell: (d) => (d.removal ? : ) + sortValue: (d) => (d.isOrphaned ? 'z' : d.status), + cell: (d) => }, { id: 'os', @@ -406,7 +398,7 @@ export default function DevicesPage() { cell: (d) => ( @@ -424,24 +416,19 @@ export default function DevicesPage() { header: '', width: 56, align: 'right', - cell: (d) => - d.removal ? ( - isOpenRemoval(d.removal.state) ? ( - stopTrying(d.removal) - ) : null - ) : ( - ) => { - e.stopPropagation(); - setMenu({ anchor: e.currentTarget, devices: [d] }); - }} - > - - - ) + cell: (d) => ( + ) => { + e.stopPropagation(); + setMenu({ anchor: e.currentTarget, devices: [d] }); + }} + > + + + ) } ]; @@ -663,14 +650,18 @@ export default function DevicesPage() { defaultSort={showingRemovals ? { id: 'requested', direction: 'desc' } : { id: 'hostname', direction: 'asc' }} rowLabel={(d) => d.hostname} onRowClick={(d) => (d.removal ? setHistoryOf(d) : navigate(`/devices/${encodeURIComponent(d.clientId)}`))} + // A right-click on a chosen row acts on every chosen device, as the Actions button does. + onRowContextMenu={ + showingRemovals + ? undefined + : (d, position) => setMenu({ position, devices: chosen.has(d.clientId) && chosenRows.length > 1 ? chosenRows : [d] }) + } minWidth={1100} - 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); @@ -718,7 +709,14 @@ export default function DevicesPage() { - {menu && setMenu(null)} entries={deviceActions.menuFor(menu.devices)} />} + {menu && ( + setMenu(null)} + entries={deviceActions.menuFor(menu.devices)} + /> + )} {groupMenu && setGroupMenu(null)} entries={groupMenuEntries} />} {deviceActions.elements} {groupActions.elements} diff --git a/src/WebUI/src/sections/devices/removalLabels.ts b/src/WebUI/src/sections/devices/removalLabels.ts index 2faedb5..4f9b47d 100644 --- a/src/WebUI/src/sections/devices/removalLabels.ts +++ b/src/WebUI/src/sections/devices/removalLabels.ts @@ -25,10 +25,13 @@ export const isOpenRemoval = (state: DeviceRemovalState) => OPEN_REMOVAL_STATES. /** 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.' + label: 'Agent Left on Device', + help: 'Too old to uninstall itself. The device is gone from the system; its agent can no longer enroll on an old token.' + }, + unsupported: { + label: 'Agent Left on Device', + help: "A foreground or development run, or a container, can't uninstall itself. The device is gone from the system." }, - 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.' }, diff --git a/tests/Backend.Tests/DeviceRemovalTests.cs b/tests/Backend.Tests/DeviceRemovalTests.cs index 23a363e..0ee304d 100644 --- a/tests/Backend.Tests/DeviceRemovalTests.cs +++ b/tests/Backend.Tests/DeviceRemovalTests.cs @@ -139,7 +139,6 @@ public class DeviceRemovalRequestTests(BackendTestFixture fixture) : IClassFixtu 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 => { @@ -288,17 +287,9 @@ public class DeviceRemovalRequestTests(BackendTestFixture fixture) : IClassFixtu 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)); + // However long ago, the Removals filter still lists it. + await fixture.EditRemovalAsync(id, r => r.UpdatedUtc = DateTimeOffset.UtcNow - TimeSpan.FromDays(400)); + Assert.Equal(id, (await ReadAsync($"clientId={clientId}")).Id); } [Fact] @@ -408,25 +399,46 @@ public class DeviceRemovalSchedulerTests(BackendTestFixture fixture) : IClassFix [Theory] [InlineData("1.0.9")] [InlineData("2026.10.23.1400")] // The calendar era sorts before every semantic version. - public async Task AnAgentTooOldIsNeverSentTheCommandAndStaysOpen(string version) + public async Task AnAgentTooOldIsRemovedFromTheSystemAndLeftOnTheDevice(string version) { var (clientId, id) = await RemovedAsync("OLD-1", version); - var sender = new RecordingRemovalSender(); + var sender = new RecordingRemovalSender { FailClear = true }; var scheduler = fixture.RemovalScheduler(sender); + // Messaging down: it stays open until nothing can replay. + await scheduler.RunPassAsync(DateTimeOffset.UtcNow, CancellationToken.None); + Assert.Equal(DeviceRemovalStates.Waiting, (await fixture.RemovalAsync(id)).State); + + sender.FailClear = false; await scheduler.RunPassAsync(DateTimeOffset.UtcNow, CancellationToken.None); Assert.Empty(sender.Sent); + Assert.Contains(clientId, sender.Cleared); var row = await fixture.RemovalAsync(id); - Assert.Equal(DeviceRemovalStates.Waiting, row.State); + Assert.Equal(DeviceRemovalStates.Removed, row.State); Assert.Equal(DeviceRemovalReasons.AgentTooOld, row.Reason); + Assert.NotNull(row.FinishedUtc); - // Upgraded by hand: the command goes out and the reason clears. - await fixture.SightAsync(clientId, DeviceRemovalDefaults.MinimumAgentVersion); + // The agent stayed, so it keeps reporting: that does not reopen the removal. + Assert.True(await fixture.SightAsync(clientId, version)); 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); + Assert.Equal(DeviceRemovalStates.Removed, (await fixture.RemovalAsync(id)).State); + Assert.Empty(sender.Sent); + Assert.False(await fixture.WithDbAsync(db => db.Devices.AnyAsync(d => d.ClientId == clientId))); + } + + [Fact] + public async Task AnAgentThatCantUninstallItselfIsRemovedFromTheSystem() + { + var (clientId, id) = await RemovedAsync("CONTAINER-1"); + Assert.True(await fixture.RemovalEventAsync(id, clientId, DeviceRemovalStates.Failed, DeviceRemovalReasons.Unsupported)); + var sender = new RecordingRemovalSender(); + + await fixture.RemovalScheduler(sender).RunPassAsync(DateTimeOffset.UtcNow, CancellationToken.None); + Assert.Empty(sender.Sent); + var row = await fixture.RemovalAsync(id); + Assert.Equal(DeviceRemovalStates.Removed, row.State); + Assert.Equal(DeviceRemovalReasons.Unsupported, row.Reason); + Assert.Equal("detail", row.Detail); } [Fact] -- 2.52.0 From 95e6bd4b02b947c3e55b04b7159155832d4ad69cb4c7b7e505088d67ff6e0f6b Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Wed, 4 Nov 2026 20:00:59 -0500 Subject: [PATCH 04/22] chore: publisher is Endexa Solutions Assembly Company and the agent installer's Manufacturer (what Programs and Features shows as the publisher). Copyright and LICENSE unchanged. Co-Authored-By: Claude Opus 5.5 --- Directory.Build.props | 2 +- src/Installer/Product.wxs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index d5a1bf1..2fd38a3 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -23,7 +23,7 @@ $(UnifiedVersion)+$(GitCommitShort) $(UnifiedVersion) false - ExecuteR + Endexa Solutions Copyright (c) 2026 Grace Solutions. All rights reserved. diff --git a/src/Installer/Product.wxs b/src/Installer/Product.wxs index d07e6d2..8a43942 100644 --- a/src/Installer/Product.wxs +++ b/src/Installer/Product.wxs @@ -22,7 +22,7 @@ Date: Wed, 4 Nov 2026 20:05:12 -0500 Subject: [PATCH 05/22] feat(s3): reveal the built-in store's keys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The built-in store generates its keys on first start and keeps them encrypted in the settings; until now nothing could show them. Settings › Storage gains Reveal Keys (endpoint, bucket, access and secret key, each copyable), from an admin-only, audited endpoint GET /api/settings/s3/built-in/credentials. A shared store's keys stay with the deployment that sets them. Co-Authored-By: Claude Opus 5.5 --- src/Backend/Settings/SettingsEndpoints.cs | 31 ++++++++++- src/Shared.Contracts/Api/ApiRoutes.cs | 1 + src/Shared.Contracts/Api/ResourceDtos.cs | 10 +++- src/WebUI/src/api/client.ts | 2 + src/WebUI/src/api/types.ts | 9 +++ .../src/sections/settings/StorageSettings.tsx | 55 ++++++++++++++++++- tests/Backend.Tests/EmbeddedS3StoreTests.cs | 41 +++++++++++++- 7 files changed, 145 insertions(+), 4 deletions(-) diff --git a/src/Backend/Settings/SettingsEndpoints.cs b/src/Backend/Settings/SettingsEndpoints.cs index e1bb96e..0f88300 100644 --- a/src/Backend/Settings/SettingsEndpoints.cs +++ b/src/Backend/Settings/SettingsEndpoints.cs @@ -17,6 +17,7 @@ public static class SettingsEndpoints app.MapGet(ApiRoutes.S3Settings, GetS3Async).RequirePermission(Permissions.SettingsManage); app.MapPut(ApiRoutes.S3Settings, SaveS3Async).RequirePermission(Permissions.SettingsManage); app.MapPost(ApiRoutes.S3SettingsTest, TestS3Async).RequirePermission(Permissions.SettingsManage); + app.MapGet(ApiRoutes.S3EmbeddedCredentials, RevealEmbeddedAsync).RequirePermission(Permissions.SettingsManage); app.MapGet(ApiRoutes.AgentPackageStatus, AgentStatusAsync).RequirePermission(Permissions.SettingsManage); app.MapPost(ApiRoutes.AgentPackageOffload, OffloadAsync).RequirePermission(Permissions.SettingsManage); @@ -40,7 +41,7 @@ public static class SettingsEndpoints private static async Task GetS3Async(S3SettingsService settings, EmbeddedS3Store embedded, CancellationToken cancellationToken) { - // The built-in store's credentials are never shown: the form's fields are always the external store's. + // The form's fields are always the external store's; the built-in store's keys come only from the audited reveal. var effective = await settings.GetEffectiveAsync(cancellationToken); var external = await settings.GetExternalAsync(cancellationToken); return Results.Ok(new S3SettingsDto( @@ -61,6 +62,34 @@ public static class SettingsEndpoints Embedded: embedded.Allowed ? embedded.Status() : null)); } + /// + /// The built-in store's keys, on demand. While it runs they are the keys it + /// runs with (generated once, or the deployment's for a shared store); + /// before that, the generated ones saved for it. Each reveal is audited. + /// + private static async Task RevealEmbeddedAsync( + S3SettingsService settings, EmbeddedS3Store embedded, AuditService audit, HttpContext context, CancellationToken cancellationToken) + { + if (!embedded.Allowed) + { + return Results.NotFound(); + } + + var connection = embedded.Connection; + var keys = connection is not null + ? (connection.AccessKey, connection.SecretKey) + : await settings.GetEmbeddedCredentialsAsync(cancellationToken); + if (keys is not { } found) + { + return Results.Conflict(new { error = "The built-in store has not started yet, so it has no keys." }); + } + + await audit.LogAsync(AuditCategories.Admin, "s3-built-in-keys-revealed", context.User.Identity?.Name, + cancellationToken: cancellationToken); + return Results.Ok(new S3EmbeddedCredentialsDto( + connection?.Endpoint ?? "", EmbeddedS3Store.Bucket, EmbeddedS3Store.Region, found.AccessKey, found.SecretKey)); + } + private static async Task SaveS3Async( S3SettingsDto request, S3SettingsService settings, diff --git a/src/Shared.Contracts/Api/ApiRoutes.cs b/src/Shared.Contracts/Api/ApiRoutes.cs index fd3ef99..cce5034 100644 --- a/src/Shared.Contracts/Api/ApiRoutes.cs +++ b/src/Shared.Contracts/Api/ApiRoutes.cs @@ -77,6 +77,7 @@ public static class ApiRoutes public const string ServerInfo = "/api/settings/server"; public const string S3Settings = "/api/settings/s3"; public const string S3SettingsTest = "/api/settings/s3/test"; + public const string S3EmbeddedCredentials = "/api/settings/s3/built-in/credentials"; public const string S3Objects = "/api/s3/objects"; public const string S3ObjectContent = "/api/s3/objects/content"; public const string S3ObjectStat = "/api/s3/objects/stat"; diff --git a/src/Shared.Contracts/Api/ResourceDtos.cs b/src/Shared.Contracts/Api/ResourceDtos.cs index 72d9428..4ccb46e 100644 --- a/src/Shared.Contracts/Api/ResourceDtos.cs +++ b/src/Shared.Contracts/Api/ResourceDtos.cs @@ -460,10 +460,18 @@ public sealed record S3EmbeddedStatusDto( long? FreeBytes, string? LastError); +/// +/// GET /api/settings/s3/built-in/credentials: the built-in store's keys, for an +/// S3 client on the server itself (the store listens on loopback, or inside the +/// stack). Admin-only and audited; nothing else ever returns them. +/// +public sealed record S3EmbeddedCredentialsDto(string Endpoint, string Bucket, string Region, string AccessKey, string SecretKey); + /// /// GET/PUT /api/settings/s3. The secret key is never returned; a non-null /// value on PUT replaces it, null leaves the stored secret untouched. The -/// fields are the external store's: the built-in one's are never shown. +/// fields are the external store's: the built-in one's keys come only from +/// . /// public sealed record S3SettingsDto( bool Enabled, diff --git a/src/WebUI/src/api/client.ts b/src/WebUI/src/api/client.ts index 6cee26b..aa3a93a 100644 --- a/src/WebUI/src/api/client.ts +++ b/src/WebUI/src/api/client.ts @@ -3,6 +3,7 @@ import type { AgentUpgradeBatchDto, AgentUpgradeDto, AgentUpgradeSettingsDto, + S3EmbeddedCredentialsDto, AgentVersionDto, BootstrapTokenDto, BrowserCredentialResponse, @@ -329,6 +330,7 @@ export const api = { s3Settings: () => request('/api/settings/s3'), saveS3Settings: (body: S3SettingsDto) => request('/api/settings/s3', json('PUT', body)), testS3Settings: (body: S3SettingsDto) => request('/api/settings/s3/test', json('POST', body)), + revealS3EmbeddedCredentials: () => request('/api/settings/s3/built-in/credentials'), agentPackage: (type: 'msi' | 'zip' = 'msi') => request(`/api/settings/agent-package?type=${type}`), offloadAgentPackage: (type: 'msi' | 'zip' = 'msi') => request<{ message: string; status: AgentPackageStatusDto }>(`/api/settings/agent-package/offload?type=${type}`, { method: 'POST' }), diff --git a/src/WebUI/src/api/types.ts b/src/WebUI/src/api/types.ts index 42e0ff5..92a374b 100644 --- a/src/WebUI/src/api/types.ts +++ b/src/WebUI/src/api/types.ts @@ -138,6 +138,15 @@ export interface S3SettingsDto { embedded?: S3EmbeddedStatusDto; } +/** The built-in store's keys, from the audited reveal only. */ +export interface S3EmbeddedCredentialsDto { + endpoint: string; + bucket: string; + region: string; + accessKey: string; + secretKey: string; +} + export interface S3EmbeddedStatusDto { /** off | unavailable | starting | running | failed */ state: string; diff --git a/src/WebUI/src/sections/settings/StorageSettings.tsx b/src/WebUI/src/sections/settings/StorageSettings.tsx index f35e476..bcc5894 100644 --- a/src/WebUI/src/sections/settings/StorageSettings.tsx +++ b/src/WebUI/src/sections/settings/StorageSettings.tsx @@ -34,11 +34,13 @@ import { SETTINGS_KEYS, SETTINGS_PERMISSION, SETTINGS_PERMISSION_LABEL } from '. // assets import CloudUploadOutlined from '@ant-design/icons/CloudUploadOutlined'; +import EyeInvisibleOutlined from '@ant-design/icons/EyeInvisibleOutlined'; +import EyeOutlined from '@ant-design/icons/EyeOutlined'; import SaveOutlined from '@ant-design/icons/SaveOutlined'; import ThunderboltOutlined from '@ant-design/icons/ThunderboltOutlined'; // types -import type { AgentPackageStatusDto, S3SettingsDto, S3TestResultDto } from 'api/types'; +import type { AgentPackageStatusDto, S3EmbeddedCredentialsDto, S3SettingsDto, S3TestResultDto } from 'api/types'; type PackageType = 'msi' | 'zip'; @@ -98,6 +100,9 @@ export default function StorageSettings() { const [actionError, setActionError] = useState(''); const [offloadError, setOffloadError] = useState(''); const [test, setTest] = useState(null); + // The built-in store's keys, fetched only when asked for (each reveal is audited). + const [keys, setKeys] = useState(null); + const [revealing, setRevealing] = useState(false); if (loading) return ; if (error || !form) return {errorMessage(error, 'Could not load the storage settings.')}; @@ -161,6 +166,30 @@ export default function StorageSettings() { } }; + const toggleKeys = async () => { + if (keys) { + setKeys(null); + return; + } + setRevealing(true); + try { + setKeys(await api.revealS3EmbeddedCredentials()); + } catch (e) { + enqueueSnackbar(errorMessage(e, 'Could not reveal the keys.'), { variant: 'error' }); + } finally { + setRevealing(false); + } + }; + + const revealed = (value: string, label: string) => ( + <> + + {value} + + + + ); + const saveButton = ( } onClick={save} disabled={busy !== ''}> {busy === 'save' ? 'Saving…' : 'Save Changes'} @@ -244,6 +273,30 @@ export default function StorageSettings() { A Service of This Stack )} + {!store?.shared && ( + + : } + onClick={toggleKeys} + disabled={revealing} + > + {keys ? 'Hide Keys' : 'Reveal Keys'} + + + + )} + {keys && ( + <> + {keys.endpoint && {revealed(keys.endpoint, 'Endpoint')}} + {revealed(keys.bucket, 'Bucket')} + {revealed(keys.accessKey, 'Access Key')} + {revealed(keys.secretKey, 'Secret Key')} + + )} {store?.usedBytes != null && ( {store.usedBytes === 0 ? '0 B' : formatBytes(store.usedBytes)} diff --git a/tests/Backend.Tests/EmbeddedS3StoreTests.cs b/tests/Backend.Tests/EmbeddedS3StoreTests.cs index ab4a7d2..e09195d 100644 --- a/tests/Backend.Tests/EmbeddedS3StoreTests.cs +++ b/tests/Backend.Tests/EmbeddedS3StoreTests.cs @@ -2,6 +2,7 @@ using System.Net; using System.Net.Http.Json; using System.Security.Cryptography; using Backend.Files; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Shared.Contracts.Api; using Xunit; @@ -108,7 +109,7 @@ public sealed class EmbeddedS3StoreTests : IClassFixture(ct); Assert.True(test!.Ok, string.Join("; ", test.Steps.Select(s => $"{s.Name}: {s.Detail}"))); + // Its generated keys come out only on request, and they are the ones it runs with. + var keys = (await admin.GetFromJsonAsync(ApiRoutes.S3EmbeddedCredentials, ct))!; + Assert.Equal(EmbeddedS3Store.Bucket, keys.Bucket); + Assert.StartsWith("http://127.0.0.1:", keys.Endpoint); + Assert.Equal(20, keys.AccessKey.Length); + Assert.Equal(40, keys.SecretKey.Length); + using (var direct = S3ClientFactory.Create(new S3Options + { + Enabled = true, Endpoint = keys.Endpoint, Bucket = keys.Bucket, AccessKey = keys.AccessKey, SecretKey = keys.SecretKey, + })) + { + Assert.Contains((await direct.ListBucketsAsync(ct)).Buckets, b => b.BucketName == EmbeddedS3Store.Bucket); + } + + using (var scope = services.CreateScope()) + { + Assert.True(await scope.ServiceProvider.GetRequiredService() + .AuditEvents.AnyAsync(e => e.Action == "s3-built-in-keys-revealed", ct)); + } + // Small and multipart, both whole. var small = RandomNumberGenerator.GetBytes(1024); var large = RandomNumberGenerator.GetBytes(40 * 1024 * 1024); @@ -501,5 +522,23 @@ public sealed class S3StoreChoiceTests Assert.Equal(HttpStatusCode.Forbidden, (await viewer.PostAsJsonAsync(ApiRoutes.S3ObjectFolder, new S3FolderRequest("a/"), ct)).StatusCode); Assert.Equal(HttpStatusCode.Forbidden, (await viewer.PostAsJsonAsync(ApiRoutes.S3ObjectStat, new S3StatRequest(["a.bin"]), ct)).StatusCode); Assert.Equal(HttpStatusCode.Forbidden, (await viewer.GetAsync($"{ApiRoutes.S3ObjectLink}?key=a.bin", ct)).StatusCode); + Assert.Equal(HttpStatusCode.Forbidden, (await viewer.GetAsync(ApiRoutes.S3EmbeddedCredentials, ct)).StatusCode); + } + + [Fact] + public async Task There_are_no_built_in_keys_to_reveal_before_it_starts_or_without_it() + { + var ct = TestContext.Current.CancellationToken; + using (var server = Server()) + { + var admin = await server.SignedInAsync("s3-admin"); + Assert.Equal(HttpStatusCode.Conflict, (await admin.GetAsync(ApiRoutes.S3EmbeddedCredentials, ct)).StatusCode); + } + + using (var server = Server(embedded: "0")) + { + var admin = await server.SignedInAsync("s3-admin"); + Assert.Equal(HttpStatusCode.NotFound, (await admin.GetAsync(ApiRoutes.S3EmbeddedCredentials, ct)).StatusCode); + } } } -- 2.52.0 From 7a1dede5e9569fd404a20fda492c15a55c3ddb1603517d4b2025dd5ca11532bc Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Wed, 4 Nov 2026 20:07:58 -0500 Subject: [PATCH 06/22] refactor(console): one code workspace for the script and function editors The Scripts code tab and the Functions editor now share CodeWorkspace (toolbar with language, IntelliSense and Pop Out/Restore, full-window mode with its own Save, and the Monaco editor with its status line), plus the editing session pieces: SaveButton (Ctrl+S hint, Saving...), UnsavedChip, useSaveShortcut and useUnsavedChangesGuard (in-console navigation and tab close/reload). Functions gain Ctrl+S, Pop Out, the IntelliSense switch, change bars on new modules, and a guard on leaving the page, browser back/forward and module switches (the page's own switch dialog is gone). IntelliSense is now an editor preference shared by both, carrying over the old setting. The device file dialog reuses the shared Ctrl+S hook. Co-Authored-By: Claude Opus 5.5 --- .../components/code-editor/CodeWorkspace.tsx | 139 ++++++++++++ .../src/components/code-editor/SaveButton.tsx | 40 ++++ src/WebUI/src/components/code-editor/prefs.ts | 9 +- .../src/components/code-editor/session.tsx | 89 ++++++++ src/WebUI/src/pages/functions/index.tsx | 42 +--- src/WebUI/src/pages/scripts/editor.tsx | 213 +++--------------- .../src/sections/devices/TextFileDialog.tsx | 15 +- .../src/sections/functions/FunctionEditor.tsx | 59 +++-- 8 files changed, 350 insertions(+), 256 deletions(-) create mode 100644 src/WebUI/src/components/code-editor/CodeWorkspace.tsx create mode 100644 src/WebUI/src/components/code-editor/SaveButton.tsx create mode 100644 src/WebUI/src/components/code-editor/session.tsx diff --git a/src/WebUI/src/components/code-editor/CodeWorkspace.tsx b/src/WebUI/src/components/code-editor/CodeWorkspace.tsx new file mode 100644 index 0000000..b4d9ef5 --- /dev/null +++ b/src/WebUI/src/components/code-editor/CodeWorkspace.tsx @@ -0,0 +1,139 @@ +import { ReactNode, useEffect, useState } from 'react'; + +// material-ui +import Box from '@mui/material/Box'; +import Button from '@mui/material/Button'; +import FormControlLabel from '@mui/material/FormControlLabel'; +import Stack from '@mui/material/Stack'; +import Switch from '@mui/material/Switch'; +import Tooltip from '@mui/material/Tooltip'; +import Typography from '@mui/material/Typography'; + +// project imports +import CodeEditor, { type CodeLanguage } from 'components/CodeEditor'; +import { setEditorPref, useEditorPrefs } from './prefs'; +import SaveButton, { type SaveControl } from './SaveButton'; + +// assets +import FullscreenExitOutlined from '@ant-design/icons/FullscreenExitOutlined'; +import FullscreenOutlined from '@ant-design/icons/FullscreenOutlined'; + +// types +import type { CompletionContext } from 'utils/monaco/powershell-language'; + +const DEFAULT_HINT = 'Context-aware completions, hover cards and checks for cmdlets, parameters and variables.'; + +interface Props { + value: string; + onChange: (value: string) => void; + language: CodeLanguage; + readOnly?: boolean; + /** The saved text: change bars in the gutter and Compare with Saved. */ + original?: string; + /** Names the code can use besides its own ($Param, $Secrets.X, attached functions). */ + completionContext?: CompletionContext; + /** What is being edited, shown above the code while it fills the window. */ + title: string; + /** Hover text for the IntelliSense switch. */ + intellisenseHint?: string; + /** Height in the page (CSS length or px); popped out, it fills the window. */ + height?: number | string; + /** Extra controls at the start of the toolbar's right-hand group. */ + toolbar?: ReactNode; + /** Save, shown in the toolbar while the editor fills the window and hides the page's own. */ + save?: SaveControl; +} + +// ==============================|| CODE WORKSPACE ||============================== // + +/** The code pane every script and function editor shares: toolbar, IntelliSense, pop-out and the Monaco editor. */ +export default function CodeWorkspace({ + value, + onChange, + language, + readOnly = false, + original, + completionContext, + title, + intellisenseHint = DEFAULT_HINT, + height = 'max(380px, 56vh)', + toolbar, + save +}: Props) { + const prefs = useEditorPrefs(); + const [fullscreen, setFullscreen] = useState(false); + const powershell = language === 'powershell'; + + useEffect(() => { + if (!fullscreen) return; + const onKey = (event: KeyboardEvent) => event.key === 'Escape' && setFullscreen(false); + window.addEventListener('keydown', onKey); + return () => window.removeEventListener('keydown', onKey); + }, [fullscreen]); + + return ( + theme.zIndex.modal, + bgcolor: 'background.paper', + p: 2, + display: 'flex', + flexDirection: 'column', + gap: 1.5 + } + : { display: 'flex', flexDirection: 'column', gap: 1.5 } + } + > + + + {fullscreen ? `${title} · ` : ''} + {language === 'shell' ? 'bash' : language} + + + {toolbar} + + setEditorPref('intellisense', !prefs.intellisense)} + /> + } + label={IntelliSense} + sx={{ mr: 0 }} + /> + + + + + {fullscreen && save && } + + + + + + + ); +} diff --git a/src/WebUI/src/components/code-editor/SaveButton.tsx b/src/WebUI/src/components/code-editor/SaveButton.tsx new file mode 100644 index 0000000..a8077af --- /dev/null +++ b/src/WebUI/src/components/code-editor/SaveButton.tsx @@ -0,0 +1,40 @@ +// material-ui +import Tooltip from '@mui/material/Tooltip'; + +// project imports +import GatedButton from 'components/GatedButton'; + +// assets +import SaveOutlined from '@ant-design/icons/SaveOutlined'; + +export interface SaveControl { + onSave: () => void; + saving: boolean; + /** Nothing to save yet (or not ready). */ + disabled: boolean; + /** Why this person cannot save (undefined when they can). */ + gate?: string; +} + +// ==============================|| CODE EDITOR - SAVE BUTTON ||============================== // + +/** The editor's Save, in the page header and in the toolbar when the editor fills the window. */ +export default function SaveButton({ control, size }: { control: SaveControl; size?: 'small' | 'medium' }) { + const { onSave, saving, disabled, gate } = control; + return ( + + + } + gate={gate} + disabled={disabled || saving} + onClick={onSave} + > + {saving ? 'Saving…' : 'Save'} + + + + ); +} diff --git a/src/WebUI/src/components/code-editor/prefs.ts b/src/WebUI/src/components/code-editor/prefs.ts index 6d2a34b..5b91296 100644 --- a/src/WebUI/src/components/code-editor/prefs.ts +++ b/src/WebUI/src/components/code-editor/prefs.ts @@ -5,14 +5,19 @@ export interface EditorPrefs { minimap: boolean; wordWrap: boolean; stickyScroll: boolean; + /** Completions, hover cards and context-aware checks (PowerShell). */ + intellisense: boolean; } const KEY = 'editor-prefs'; -const DEFAULTS: EditorPrefs = { minimap: false, wordWrap: false, stickyScroll: true }; +const DEFAULTS: EditorPrefs = { minimap: false, wordWrap: false, stickyScroll: true, intellisense: true }; +/** Where IntelliSense was remembered before it joined these. */ +const LEGACY_INTELLISENSE_KEY = 'editor-intellisense'; function read(): EditorPrefs { try { - return { ...DEFAULTS, ...JSON.parse(localStorage.getItem(KEY) ?? '{}') }; + const intellisense = localStorage.getItem(LEGACY_INTELLISENSE_KEY) !== '0'; + return { ...DEFAULTS, intellisense, ...JSON.parse(localStorage.getItem(KEY) ?? '{}') }; } catch { return DEFAULTS; } diff --git a/src/WebUI/src/components/code-editor/session.tsx b/src/WebUI/src/components/code-editor/session.tsx new file mode 100644 index 0000000..47097f0 --- /dev/null +++ b/src/WebUI/src/components/code-editor/session.tsx @@ -0,0 +1,89 @@ +import { ReactNode, useCallback, useEffect, useRef } from 'react'; +import { BlockerFunction, Location, useBlocker } from 'react-router-dom'; + +// material-ui +import Chip from '@mui/material/Chip'; + +// project imports +import ConfirmDialog from 'components/ConfirmDialog'; + +/** Ctrl+S / Cmd+S runs `save` (also from inside the code editor) instead of saving the page. */ +export function useSaveShortcut(save: () => void) { + const saveRef = useRef(save); + useEffect(() => { + saveRef.current = save; + }); + useEffect(() => { + const onKey = (event: KeyboardEvent) => { + if ((event.ctrlKey || event.metaKey) && !event.altKey && event.key.toLowerCase() === 's') { + event.preventDefault(); + saveRef.current(); + } + }; + window.addEventListener('keydown', onKey, true); + return () => window.removeEventListener('keydown', onKey, true); + }, []); +} + +const leavesPath = (current: Location, next: Location) => current.pathname !== next.pathname; + +/** + * Asks before leaving with unsaved changes: navigations in the console and closing or reloading the tab. + * `leaves` says which navigations leave the document (by default, any change of path). + * Call `allowLeave` just before navigating away on purpose (after a save, copy or delete). + */ +export function useUnsavedChangesGuard( + dirty: boolean, + leaves: (current: Location, next: Location) => boolean = leavesPath +): { allowLeave: () => void; dialog: ReactNode } { + const dirtyRef = useRef(dirty); + const leavesRef = useRef(leaves); + const leavingRef = useRef(false); + const proceedingRef = useRef(false); + useEffect(() => { + dirtyRef.current = dirty; + leavesRef.current = leaves; + }); + const blocker = useBlocker( + useCallback( + ({ currentLocation, nextLocation }) => dirtyRef.current && !leavingRef.current && leavesRef.current(currentLocation, nextLocation), + [] + ) + ); + useEffect(() => { + if (!dirty) return; + const warn = (event: BeforeUnloadEvent) => event.preventDefault(); + window.addEventListener('beforeunload', warn); + return () => window.removeEventListener('beforeunload', warn); + }, [dirty]); + + const allowLeave = useCallback(() => { + leavingRef.current = true; + }, []); + + const dialog = ( + { + proceedingRef.current = true; + blocker.proceed?.(); + }} + onClose={() => { + // After Discard the blocker has already moved on; only Cancel resets it. + if (!proceedingRef.current) blocker.reset?.(); + proceedingRef.current = false; + }} + /> + ); + + return { allowLeave, dialog }; +} + +/** The marker beside an editor's title while it has unsaved edits. */ +export function UnsavedChip({ dirty }: { dirty: boolean }) { + return dirty ? : null; +} diff --git a/src/WebUI/src/pages/functions/index.tsx b/src/WebUI/src/pages/functions/index.tsx index 2d2dd5f..66d5878 100644 --- a/src/WebUI/src/pages/functions/index.tsx +++ b/src/WebUI/src/pages/functions/index.tsx @@ -14,7 +14,6 @@ import Stack from '@mui/material/Stack'; import TextField from '@mui/material/TextField'; // project imports -import ConfirmDialog from 'components/ConfirmDialog'; import { RelativeTime } from 'components/DateTime'; import EmptyState from 'components/EmptyState'; import GatedButton from 'components/GatedButton'; @@ -46,9 +45,6 @@ export default function FunctionsPage() { const { data: modules, error, loading } = useApi(keys.functions); const [search, setSearch] = useState(''); - const [dirty, setDirty] = useState(false); - // A switch waiting on "discard unsaved changes?" (undefined: none pending). - const [pending, setPending] = useState(undefined); const sorted = useMemo(() => [...(modules ?? [])].sort((a, b) => a.name.localeCompare(b.name)), [modules]); const visible = useMemo(() => { @@ -66,36 +62,16 @@ export default function FunctionsPage() { return next; }); - /** Switches module, asking first when the open one has unsaved edits. */ + /** Switches module (the open editor asks first when it has unsaved edits). */ const select = (id: string | null) => { - if (id === selectedId) return; - if (dirty) setPending(id); - else go(id); + if (id !== selectedId) go(id); }; let editor; if (selectedId === NEW) { - editor = ( - m.name)} - gate={gate} - onSelect={go} - onDirtyChange={setDirty} - /> - ); + editor = m.name)} gate={gate} onSelect={go} />; } else if (selected) { - editor = ( - m.name)} - gate={gate} - onSelect={go} - onDirtyChange={setDirty} - /> - ); + editor = m.name)} gate={gate} onSelect={go} />; } else if (selectedId && loading) { editor = ( @@ -184,16 +160,6 @@ export default function FunctionsPage() { {editor} - - go(pending ?? null)} - onClose={() => setPending(undefined)} - /> ); } diff --git a/src/WebUI/src/pages/scripts/editor.tsx b/src/WebUI/src/pages/scripts/editor.tsx index 2f8b7be..89c8d87 100644 --- a/src/WebUI/src/pages/scripts/editor.tsx +++ b/src/WebUI/src/pages/scripts/editor.tsx @@ -1,21 +1,18 @@ -import { MouseEvent, ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react'; -import { BlockerFunction, Link as RouterLink, useBlocker, useNavigate, useParams, useSearchParams } from 'react-router-dom'; +import { MouseEvent, ReactNode, useMemo, useState } from 'react'; +import { Link as RouterLink, useNavigate, useParams, useSearchParams } from 'react-router-dom'; // material-ui import Alert from '@mui/material/Alert'; import Box from '@mui/material/Box'; import Button from '@mui/material/Button'; import Chip from '@mui/material/Chip'; -import FormControlLabel from '@mui/material/FormControlLabel'; import Grid from '@mui/material/Grid'; import MenuItem from '@mui/material/MenuItem'; import Skeleton from '@mui/material/Skeleton'; import Stack from '@mui/material/Stack'; -import Switch from '@mui/material/Switch'; import Tab from '@mui/material/Tab'; import Tabs from '@mui/material/Tabs'; import TextField from '@mui/material/TextField'; -import Tooltip from '@mui/material/Tooltip'; import Typography from '@mui/material/Typography'; // third-party @@ -25,7 +22,9 @@ import { mutate } from 'swr'; // project imports import IconButton from 'components/@extended/IconButton'; import ActionMenu from 'components/ActionMenu'; -import CodeEditor from 'components/CodeEditor'; +import CodeWorkspace from 'components/code-editor/CodeWorkspace'; +import SaveButton, { type SaveControl } from 'components/code-editor/SaveButton'; +import { UnsavedChip, useSaveShortcut, useUnsavedChangesGuard } from 'components/code-editor/session'; import ConfirmDialog from 'components/ConfirmDialog'; import { RelativeTime } from 'components/DateTime'; import EmptyState from 'components/EmptyState'; @@ -60,12 +59,9 @@ import useLiveRefresh from 'hooks/useLiveRefresh'; // assets import CalendarOutlined from '@ant-design/icons/CalendarOutlined'; -import FullscreenExitOutlined from '@ant-design/icons/FullscreenExitOutlined'; -import FullscreenOutlined from '@ant-design/icons/FullscreenOutlined'; import MoreOutlined from '@ant-design/icons/MoreOutlined'; import PlayCircleOutlined from '@ant-design/icons/PlayCircleOutlined'; import PlusOutlined from '@ant-design/icons/PlusOutlined'; -import SaveOutlined from '@ant-design/icons/SaveOutlined'; // types import type { FunctionModuleDto, ScriptDto, ScriptTriggerDto } from 'api/types'; @@ -73,15 +69,6 @@ import type { CompletionContext } from 'utils/monaco/powershell-language'; import type { EditorTab, ScriptDraft, ScriptEngine } from 'sections/scripts/scriptModel'; const TABS: EditorTab[] = ['code', 'parameters', 'functions', 'secrets', 'files', 'executions', 'schedules']; -const INTELLISENSE_KEY = 'editor-intellisense'; - -function readIntellisense(): boolean { - try { - return localStorage.getItem(INTELLISENSE_KEY) !== '0'; - } catch { - return true; - } -} /** Tab label with a count, red when something in the tab needs fixing. */ function TabLabel({ label, count, problem }: { label: string; count?: number; problem?: boolean }) { @@ -148,8 +135,6 @@ function ScriptEditor({ id }: { id: string }) { const [saveError, setSaveError] = useState(''); const [menuAnchor, setMenuAnchor] = useState(null); const [deleting, setDeleting] = useState(false); - const [fullscreen, setFullscreen] = useState(false); - const [intellisense, setIntellisense] = useState(readIntellisense); const requestedTab = params.get('tab') as EditorTab | null; const tab: EditorTab = @@ -168,26 +153,8 @@ function ScriptEditor({ id }: { id: string }) { const edit = (patch: Partial) => setDraft((current) => (current ? { ...current, ...patch } : current)); - // ── Leaving with unsaved changes ────────────────────────────────────── - const dirtyRef = useRef(dirty); - // Set before navigating away on purpose (after a save, duplicate or delete). - const leavingRef = useRef(false); - const proceedingRef = useRef(false); - useEffect(() => { - dirtyRef.current = dirty; - }, [dirty]); - const blocker = useBlocker( - useCallback( - ({ currentLocation, nextLocation }) => dirtyRef.current && !leavingRef.current && currentLocation.pathname !== nextLocation.pathname, - [] - ) - ); - useEffect(() => { - if (!dirty) return; - const warn = (event: BeforeUnloadEvent) => event.preventDefault(); - window.addEventListener('beforeunload', warn); - return () => window.removeEventListener('beforeunload', warn); - }, [dirty]); + // Leaving with unsaved changes asks first (tab switches stay on the page). + const guard = useUnsavedChangesGuard(dirty); /** Keeps only the tab when moving to another script. */ const tabSearch = tab === 'code' ? '' : `?tab=${tab}`; @@ -213,7 +180,7 @@ function ScriptEditor({ id }: { id: string }) { const created = await api.createScript(saveBody(draft)); await mutate(keys.script(created.id), created, { revalidate: false }); await mutate(keys.scripts); - leavingRef.current = true; + guard.allowLeave(); enqueueSnackbar('Script created.', { variant: 'success' }); navigate(`/scripts/${created.id}${tabSearch}`, { replace: true }); return created.id; @@ -235,28 +202,13 @@ function ScriptEditor({ id }: { id: string }) { } }; - // Ctrl+S / Cmd+S saves (also from inside the code editor). - const saveRef = useRef(save); - useEffect(() => { - saveRef.current = save; - }); - useEffect(() => { - const onKey = (event: KeyboardEvent) => { - if ((event.ctrlKey || event.metaKey) && !event.altKey && event.key.toLowerCase() === 's') { - event.preventDefault(); - void saveRef.current(); - } - }; - window.addEventListener('keydown', onKey, true); - return () => window.removeEventListener('keydown', onKey, true); - }, []); - - useEffect(() => { - if (!fullscreen) return; - const onKey = (event: KeyboardEvent) => event.key === 'Escape' && setFullscreen(false); - window.addEventListener('keydown', onKey); - return () => window.removeEventListener('keydown', onKey); - }, [fullscreen]); + useSaveShortcut(() => void save()); + const saveControl: SaveControl = { + onSave: () => void save(), + saving, + disabled: !draft || (!isNew && !dirty), + gate: gates.manage + }; /** Runs and schedules use the stored script, so unsaved edits are saved first. */ const runNow = async () => { @@ -280,7 +232,7 @@ function ScriptEditor({ id }: { id: string }) { const created = await api.createScript(duplicateBody(draft)); await mutate(keys.script(created.id), created, { revalidate: false }); await mutate(keys.scripts); - leavingRef.current = true; + guard.allowLeave(); navigate(`/scripts/${created.id}${tabSearch}`); enqueueSnackbar('Duplicated — you are editing the copy.', { variant: 'success' }); } catch (e) { @@ -314,16 +266,6 @@ function ScriptEditor({ id }: { id: string }) { // The saved code, for change bars and Compare with Saved. const savedContent = useMemo(() => (baseline ? (JSON.parse(baseline) as ScriptDraft).content : undefined), [baseline]); - const toggleIntellisense = () => - setIntellisense((on) => { - try { - localStorage.setItem(INTELLISENSE_KEY, on ? '0' : '1'); - } catch { - // Storage unavailable; the choice lasts for this page only. - } - return !on; - }); - // ── Render ──────────────────────────────────────────────────────────── const notFound = loadError instanceof ApiError && loadError.status === 404; const title = draft?.name.trim() || (isNew ? 'New Script' : (script?.name ?? 'Script')); @@ -358,7 +300,7 @@ function ScriptEditor({ id }: { id: string }) { } + adornment={} description={ isNew ? ( 'Write the code, then add its inputs.' @@ -403,21 +345,7 @@ function ScriptEditor({ id }: { id: string }) { )} } - primaryAction={ - - - } - gate={gates.manage} - disabled={!draft || saving || (!isNew && !dirty)} - onClick={() => void save()} - > - {saving ? 'Saving…' : 'Save'} - - - - } + primaryAction={} /> {saveError && ( @@ -512,79 +440,19 @@ function ScriptEditor({ id }: { id: string }) { {tab === 'code' && ( - theme.zIndex.modal, - bgcolor: 'background.paper', - p: 2, - display: 'flex', - flexDirection: 'column', - gap: 1.5 - } - : { p: { xs: 2, md: 2.5 }, display: 'flex', flexDirection: 'column', gap: 1.5 } - } - > - - - {fullscreen ? `${title} · ` : ''} - {draft.engine === 'Bash' ? 'bash' : 'powershell'} - - - - } - label={IntelliSense} - sx={{ mr: 0 }} - /> - - - - - {fullscreen && ( - } - gate={gates.manage} - disabled={saving || (!isNew && !dirty)} - onClick={() => void save()} - > - Save - - )} - - - - edit({ content })} - language={editorLanguage(draft.engine)} - readOnly={readOnly} - height={fullscreen ? '100%' : 'max(380px, 56vh)'} - intellisense={intellisense && draft.engine !== 'Bash'} - completionContext={completionContext} - original={savedContent} - /> - - + + edit({ content })} + language={editorLanguage(draft.engine)} + readOnly={readOnly} + original={savedContent} + completionContext={completionContext} + title={title} + intellisenseHint="Context-aware completions, hover cards and checks for cmdlets, parameters, variables, secrets and attached functions." + save={saveControl} + /> + )} {tab === 'parameters' && ( @@ -694,28 +562,13 @@ function ScriptEditor({ id }: { id: string }) { onConfirm={async () => { await api.deleteScript(id); await mutate(keys.scripts); - leavingRef.current = true; + guard.allowLeave(); enqueueSnackbar('Script deleted.', { variant: 'success' }); navigate('/scripts'); }} /> - { - proceedingRef.current = true; - blocker.proceed?.(); - }} - onClose={() => { - // After Discard the blocker has already moved on; only Cancel resets it. - if (!proceedingRef.current) blocker.reset?.(); - proceedingRef.current = false; - }} - /> + {guard.dialog} ); } diff --git a/src/WebUI/src/sections/devices/TextFileDialog.tsx b/src/WebUI/src/sections/devices/TextFileDialog.tsx index dd29789..22735d8 100644 --- a/src/WebUI/src/sections/devices/TextFileDialog.tsx +++ b/src/WebUI/src/sections/devices/TextFileDialog.tsx @@ -17,6 +17,7 @@ import Typography from '@mui/material/Typography'; // project imports import CodeEditor from 'components/CodeEditor'; +import { useSaveShortcut } from 'components/code-editor/session'; import ConfirmDialog from 'components/ConfirmDialog'; import GatedButton from 'components/GatedButton'; import HoverHelp from 'components/HoverHelp'; @@ -111,17 +112,9 @@ export default function TextFileDialog({ clientId, hostname, path, name, saveGat [clientId, encoding, file, lineEndings, onSaved, path, text] ); - // Ctrl+S saves rather than offering to save the console's page. - useEffect(() => { - const onKey = (event: KeyboardEvent) => { - if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 's') { - event.preventDefault(); - if (dirty && !readOnly && !saving) void save(); - } - }; - window.addEventListener('keydown', onKey); - return () => window.removeEventListener('keydown', onKey); - }, [dirty, readOnly, save, saving]); + useSaveShortcut(() => { + if (dirty && !readOnly && !saving) void save(); + }); const close = () => (dirty ? setLeaving(true) : onClose()); diff --git a/src/WebUI/src/sections/functions/FunctionEditor.tsx b/src/WebUI/src/sections/functions/FunctionEditor.tsx index 3a207b2..da985a3 100644 --- a/src/WebUI/src/sections/functions/FunctionEditor.tsx +++ b/src/WebUI/src/sections/functions/FunctionEditor.tsx @@ -1,17 +1,18 @@ -import { useEffect, useState } from 'react'; +import { useState } from 'react'; import { mutate } from 'swr'; import { enqueueSnackbar } from 'notistack'; // material-ui import Alert from '@mui/material/Alert'; -import Chip from '@mui/material/Chip'; import Grid from '@mui/material/Grid'; import Stack from '@mui/material/Stack'; import TextField from '@mui/material/TextField'; import Typography from '@mui/material/Typography'; // project imports -import CodeEditor from 'components/CodeEditor'; +import CodeWorkspace from 'components/code-editor/CodeWorkspace'; +import SaveButton, { type SaveControl } from 'components/code-editor/SaveButton'; +import { UnsavedChip, useSaveShortcut, useUnsavedChangesGuard } from 'components/code-editor/session'; import ConfirmDialog from 'components/ConfirmDialog'; import { RelativeTime } from 'components/DateTime'; import GatedButton from 'components/GatedButton'; @@ -21,9 +22,9 @@ import { api, errorMessage, keys } from 'api/client'; // assets import CopyOutlined from '@ant-design/icons/CopyOutlined'; import DeleteOutlined from '@ant-design/icons/DeleteOutlined'; -import SaveOutlined from '@ant-design/icons/SaveOutlined'; // types +import type { Location } from 'react-router-dom'; import type { FunctionModuleDto } from 'api/types'; interface Props { @@ -35,7 +36,6 @@ interface Props { gate?: string; /** Switches the page to another module (null: none). */ onSelect: (id: string | null) => void; - onDirtyChange: (dirty: boolean) => void; } interface Draft { @@ -56,10 +56,14 @@ const copyName = (base: string, existing: string[]) => { return candidate; }; +/** The page shows one module per `?id=`: switching module leaves this one as much as leaving the page. */ +const leavesModule = (current: Location, next: Location) => + current.pathname !== next.pathname || new URLSearchParams(current.search).get('id') !== new URLSearchParams(next.search).get('id'); + // ==============================|| FUNCTION EDITOR ||============================== // /** Name, description and code of one function module. Mount with `key` per module so the draft resets. */ -export default function FunctionEditor({ module, existingNames, gate, onSelect, onDirtyChange }: Props) { +export default function FunctionEditor({ module, existingNames, gate, onSelect }: Props) { const [saved, setSaved] = useState(() => module ? { name: module.name, description: module.description ?? '', content: module.content } : NEW_MODULE ); @@ -69,14 +73,12 @@ export default function FunctionEditor({ module, existingNames, gate, onSelect, const [confirmDelete, setConfirmDelete] = useState(false); const dirty = draft.name !== saved.name || draft.description !== saved.description || draft.content !== saved.content; - useEffect(() => { - onDirtyChange(dirty); - return () => onDirtyChange(false); - }, [dirty, onDirtyChange]); + const guard = useUnsavedChangesGuard(dirty, leavesModule); const update = (patch: Partial) => setDraft((current) => ({ ...current, ...patch })); const save = async () => { + if (busy || gate || (module && !dirty)) return; if (!draft.name.trim()) { setError('Name is required.'); return; @@ -93,6 +95,7 @@ export default function FunctionEditor({ module, existingNames, gate, onSelect, } else { const created = await api.createFunction(body); await mutate(keys.functions); + guard.allowLeave(); onSelect(created.id); } enqueueSnackbar('Saved', { variant: 'success' }); @@ -103,6 +106,9 @@ export default function FunctionEditor({ module, existingNames, gate, onSelect, } }; + useSaveShortcut(() => void save()); + const saveControl: SaveControl = { onSave: () => void save(), saving: busy, disabled: Boolean(module) && !dirty, gate }; + /** Copies the current editor state into a new module and switches to it. */ const clone = async () => { setBusy(true); @@ -114,6 +120,7 @@ export default function FunctionEditor({ module, existingNames, gate, onSelect, content: draft.content }); await mutate(keys.functions); + guard.allowLeave(); onSelect(created.id); enqueueSnackbar('Cloned — you are editing the copy.', { variant: 'success' }); } catch (e) { @@ -125,19 +132,22 @@ export default function FunctionEditor({ module, existingNames, gate, onSelect, const remove = async () => { await api.deleteFunction(module!.id); + guard.allowLeave(); await mutate(keys.functions); onSelect(null); enqueueSnackbar(`Deleted ${module!.name}`, { variant: 'success' }); }; + const title = module ? saved.name : 'New Function'; + return ( - {module ? saved.name : 'New Function'} + {title} - {module && dirty && } + } subheader={ @@ -167,20 +177,16 @@ export default function FunctionEditor({ module, existingNames, gate, onSelect, Clone )} - } - onClick={save} - disabled={busy || (Boolean(module) && !dirty)} - > - {busy ? 'Saving…' : 'Save'} - + } > - {error && {error}} + {error && ( + setError('')}> + {error} + + )} - update({ content })} language="powershell" readOnly={Boolean(gate)} - original={module ? saved.content : undefined} - height="max(420px, calc(100vh - 420px))" + original={saved.content} + title={draft.name.trim() || title} + height="max(420px, calc(100vh - 460px))" + save={saveControl} /> @@ -226,6 +234,7 @@ export default function FunctionEditor({ module, existingNames, gate, onSelect, onClose={() => setConfirmDelete(false)} /> )} + {guard.dialog} ); } -- 2.52.0 From a2c96407b8c13a9f8fb9351b56c4eec59e2b811653027ccfd50559c04677ffca Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Wed, 4 Nov 2026 20:09:39 -0500 Subject: [PATCH 07/22] feat(groups): condition builder with Add Condition and Add Group The rule editor follows StifleR's builder: the rule and every group pick All of or Any of with a Not, and hold conditions and nested groups, with Add Condition and Add Group inside each (Add Group stops at eight levels, with the reason on hover). Add Block is gone. Fields are picked from a searchable list headed by category; operators fit the field's type; values are suggested from what the caller's devices report (fetched while the list is open, as typing pauses), fixed choices are a select, lists are chips, and patterns get their own input. Problems show on their row: missing values and unreadable numbers from the console, patterns (.NET semantics) and CIDR ranges from the server through the live preview. The preview's count opens a table of every match. A rule saved as one block opens flat, matching the same devices; the Groups page describes nested rules. Co-Authored-By: Claude Opus 5.5 --- src/WebUI/src/api/client.ts | 4 + src/WebUI/src/api/types.ts | 19 + src/WebUI/src/pages/groups/index.tsx | 37 +- src/WebUI/src/sections/groups/GroupEditor.tsx | 69 +- src/WebUI/src/sections/groups/RuleBuilder.tsx | 785 ++++++++++++------ .../src/sections/groups/RuleMatchesDialog.tsx | 87 ++ src/WebUI/src/sections/groups/ruleModel.ts | 177 ++++ 7 files changed, 889 insertions(+), 289 deletions(-) create mode 100644 src/WebUI/src/sections/groups/RuleMatchesDialog.tsx create mode 100644 src/WebUI/src/sections/groups/ruleModel.ts diff --git a/src/WebUI/src/api/client.ts b/src/WebUI/src/api/client.ts index 6cee26b..448f214 100644 --- a/src/WebUI/src/api/client.ts +++ b/src/WebUI/src/api/client.ts @@ -19,6 +19,7 @@ import type { ExecutionSummaryDto, FunctionModuleDto, GroupFieldDescriptor, + GroupFieldValueDto, GroupRuleSet, GroupRunDto, JobDto, @@ -303,6 +304,9 @@ export const api = { request(`/api/groups/${id}/members`, json('POST', { add, remove })), previewGroup: (rules: GroupRuleSet) => request('/api/groups/preview', json('POST', { rules })), groupFields: () => request('/api/groups/fields'), + /** Values devices in the caller's scope report for a rule field, most common first. */ + groupFieldValues: (field: string, prefix: string, signal?: AbortSignal, limit = 25) => + request(`/api/groups/fields/values${qs({ field, prefix, limit })}`, { signal }), // Schedules and event triggers triggers: () => request('/api/triggers'), diff --git a/src/WebUI/src/api/types.ts b/src/WebUI/src/api/types.ts index 1e990ee..f13ba3d 100644 --- a/src/WebUI/src/api/types.ts +++ b/src/WebUI/src/api/types.ts @@ -71,17 +71,36 @@ export interface RuleGroup { negate?: boolean; } +/** + * The rule itself: the outermost group, with conditions and groups of its own. + * Rules saved before it held conditions carry only `groups`. + */ export interface GroupRuleSet { combine: RuleCombine; groups?: RuleGroup[]; + conditions?: RuleCondition[]; + negate?: boolean; } +/** What a rule field holds: it picks the operators and the value input. */ +export type GroupFieldType = 'text' | 'number' | 'version' | 'address' | 'boolean' | 'choice'; + export interface GroupFieldDescriptor { field: string; displayName: string; category: string; numeric: boolean; multiValue: boolean; + type?: GroupFieldType; + /** Every value the field can hold, when that is fixed. */ + choices?: string[]; + description?: string; +} + +/** GET /api/groups/fields/values: one value devices report for a field, and on how many of them. */ +export interface GroupFieldValueDto { + value: string; + devices: number; } export interface DeviceGroupDto { diff --git a/src/WebUI/src/pages/groups/index.tsx b/src/WebUI/src/pages/groups/index.tsx index 11ca6d5..2c9ffdb 100644 --- a/src/WebUI/src/pages/groups/index.tsx +++ b/src/WebUI/src/pages/groups/index.tsx @@ -22,6 +22,7 @@ import PageHeader from 'components/PageHeader'; import Properties from 'components/Properties'; import GroupTreePanel, { ALL_NODE } from 'sections/groups/GroupTreePanel'; import useGroupActions from 'sections/groups/useGroupActions'; +import { describeRules, ruleIsEmpty } from 'sections/groups/ruleModel'; import ScheduleTable from 'sections/schedules/ScheduleTable'; import useApi from 'hooks/useApi'; import useAutomation from 'hooks/useAutomation'; @@ -30,7 +31,7 @@ import useSession, { useGate } from 'hooks/useSession'; import { keys } from 'api/client'; import { plural } from 'utils/format'; import { devicesInGroup, groupPath } from 'utils/groupTree'; -import type { DeviceDto, DeviceGroupDto, GroupFieldDescriptor, GroupRuleSet, RuleCondition, ScriptTriggerDto } from 'api/types'; +import type { DeviceDto, DeviceGroupDto, GroupFieldDescriptor, ScriptTriggerDto } from 'api/types'; // assets import ApartmentOutlined from '@ant-design/icons/ApartmentOutlined'; @@ -42,38 +43,6 @@ import PlusOutlined from '@ant-design/icons/PlusOutlined'; const KIND_LABEL = { static: 'Static', dynamic: 'Dynamic', folder: 'Folder' } as const; -const OPERATOR_TEXT: Record = { - equals: 'is', - notEquals: 'is not', - contains: 'contains', - notContains: 'does not contain', - startsWith: 'starts with', - endsWith: 'ends with', - regex: 'matches', - in: 'is one of', - notIn: 'is none of', - greaterThan: '>', - greaterOrEqual: '≥', - lessThan: '<', - lessOrEqual: '≤', - exists: 'has a value', - notExists: 'has no value', - inCidr: 'is in', - notInCidr: 'is not in' -}; - -/** Rules in plain words, one line per block ("Tags is web AND OS name contains Server"). */ -function describeRules(rules: GroupRuleSet | undefined, fields: GroupFieldDescriptor[]): string[] { - const label = (c: RuleCondition) => fields.find((f) => f.field === c.field)?.displayName ?? c.field; - return (rules?.groups ?? []).map((block) => { - const parts = (block.conditions ?? []).map( - (c) => `${c.negate ? 'NOT ' : ''}${label(c)} ${OPERATOR_TEXT[c.operator] ?? c.operator}${c.value ? ` “${c.value}”` : ''}` - ); - const text = parts.length > 0 ? parts.join(block.combine === 'and' ? ' AND ' : ' OR ') : '(no conditions)'; - return block.negate ? `NOT (${text})` : text; - }); -} - // ==============================|| GROUPS ||============================== // export default function GroupsPage() { @@ -319,7 +288,7 @@ export default function GroupsPage() { )} ))} - {(group.rules?.groups ?? []).length === 0 && ( + {ruleIsEmpty(group.rules) && ( No rules; matches no devices. diff --git a/src/WebUI/src/sections/groups/GroupEditor.tsx b/src/WebUI/src/sections/groups/GroupEditor.tsx index 324d1f6..4f7ef35 100644 --- a/src/WebUI/src/sections/groups/GroupEditor.tsx +++ b/src/WebUI/src/sections/groups/GroupEditor.tsx @@ -18,11 +18,13 @@ import { mutate } from 'swr'; // project imports import RuleBuilder from './RuleBuilder'; +import RuleMatchesDialog from './RuleMatchesDialog'; +import { checkRules, normalizeRules } from './ruleModel'; import HoverHelp from 'components/HoverHelp'; import Sheet from 'components/Sheet'; import { DeviceStatusChip } from 'components/StatusChip'; import useApi from 'hooks/useApi'; -import { api, errorMessage, keys } from 'api/client'; +import { api, ApiError, errorMessage, fieldErrors, keys } from 'api/client'; import { buildGroupTree, canNestUnder, flattenTree, groupPath } from 'utils/groupTree'; import { plural } from 'utils/format'; import type { DeviceDto, DeviceGroupDto, GroupFieldDescriptor, GroupKind, GroupRuleSet } from 'api/types'; @@ -51,9 +53,15 @@ const KIND_HELP: Record = { const EMPTY_RULES: GroupRuleSet = { combine: 'and', groups: [] }; -/** Live rule preview: how many devices match, and a few of them (debounced). */ +/** + * Live rule preview: how many devices match, and a few of them (debounced). The server checks the rule as it previews it (patterns + * with .NET semantics), so a refusal carries its problems by row path. + */ function useRulePreview(rules: GroupRuleSet, active: boolean) { - const [state, setState] = useState<{ devices: DeviceDto[] | null; error?: string }>({ devices: null }); + const [state, setState] = useState<{ devices: DeviceDto[] | null; error?: string; problems: Record }>({ + devices: null, + problems: {} + }); const key = JSON.stringify(rules); useEffect(() => { @@ -62,8 +70,12 @@ function useRulePreview(rules: GroupRuleSet, active: boolean) { const timer = window.setTimeout(() => { api .previewGroup(JSON.parse(key) as GroupRuleSet) - .then((devices) => !cancelled && setState({ devices })) - .catch((e) => !cancelled && setState({ devices: null, error: errorMessage(e) })); + .then((devices) => !cancelled && setState({ devices, problems: {} })) + .catch( + (e) => + !cancelled && + setState({ devices: null, error: errorMessage(e), problems: e instanceof ApiError && e.status === 400 ? fieldErrors(e) : {} }) + ); }, 400); return () => { cancelled = true; @@ -90,6 +102,10 @@ export default function GroupEditor({ open, onClose, group, preset, onSaved }: P const [initialMembers, setInitialMembers] = useState([]); const [busy, setBusy] = useState(false); const [error, setError] = useState(''); + // Rows not filled in are flagged once a save is tried; the server's verdict on a save, until the rule changes. + const [showIncomplete, setShowIncomplete] = useState(false); + const [saveProblems, setSaveProblems] = useState>({}); + const [matchesOpen, setMatchesOpen] = useState(false); // Static members are seeded from the device list once per opening; later // refreshes of that list (live presence, focus) must not wipe edits. const membersSeeded = useRef(false); @@ -103,11 +119,13 @@ export default function GroupEditor({ open, onClose, group, preset, onSaved }: P membersSeeded.current = false; setCreatedId(null); setError(''); + setShowIncomplete(false); + setSaveProblems({}); setName(group?.name ?? ''); setDescription(group?.description ?? ''); setParentId(group ? (group.parentId ?? null) : (preset?.parentId ?? null)); setKind(group?.kind ?? preset?.kind ?? (preset?.memberIds ? 'static' : 'dynamic')); - setRules(group?.rules ?? EMPTY_RULES); + setRules(normalizeRules(group?.rules ?? EMPTY_RULES)); setMembers(preset?.memberIds ?? []); setInitialMembers([]); // eslint-disable-next-line react-hooks/exhaustive-deps @@ -122,6 +140,15 @@ export default function GroupEditor({ open, onClose, group, preset, onSaved }: P }, [open, group, devices]); const preview = useRulePreview(rules, open && kind === 'dynamic'); + const checked = useMemo(() => checkRules(rules, fields ?? []), [rules, fields]); + // The console's own checks first, then the server's (patterns, limits) from the last preview or save. + const ruleProblems = { ...saveProblems, ...preview.problems, ...(showIncomplete ? checked.incomplete : {}), ...checked.errors }; + const ruleBlocked = + Object.keys(checked.errors).length + Object.keys(checked.incomplete).length + Object.keys(preview.problems).length > 0; + const editRules = (next: GroupRuleSet) => { + setRules(next); + setSaveProblems({}); + }; const parentOptions = useMemo( () => flattenTree(buildGroupTree(groups ?? [])).filter(({ group: g }) => !group || canNestUnder(groups ?? [], group.id, g.id)), @@ -133,6 +160,10 @@ export default function GroupEditor({ open, onClose, group, preset, onSaved }: P const save = async () => { if (!name.trim()) return setError('Give the group a name.'); + if (kind === 'dynamic' && ruleBlocked) { + setShowIncomplete(true); + return setError('Some rule rows need a change.'); + } setBusy(true); setError(''); try { @@ -156,6 +187,7 @@ export default function GroupEditor({ open, onClose, group, preset, onSaved }: P onSaved?.(saved); onClose(); } catch (e) { + setSaveProblems(e instanceof ApiError && e.status === 400 ? fieldErrors(e) : {}); setError(errorMessage(e, 'The group could not be saved.')); } finally { setBusy(false); @@ -224,9 +256,13 @@ export default function GroupEditor({ open, onClose, group, preset, onSaved }: P {kind === 'dynamic' && ( Rules - + - {preview.error ? ( + {Object.keys(preview.problems).length > 0 ? ( + + No preview until the highlighted rows change. + + ) : preview.error ? ( {preview.error} @@ -236,13 +272,25 @@ export default function GroupEditor({ open, onClose, group, preset, onSaved }: P ) : ( <> - Matches {plural(preview.devices.length, 'device')} now + + + Matches {plural(preview.devices.length, 'device')} now + + + {preview.devices.length > 0 && ( + + )} + {preview.devices.length > 0 && ( {preview.devices.slice(0, 12).map((d) => ( ))} - {preview.devices.length > 12 && } + {preview.devices.length > 12 && ( + setMatchesOpen(true)} /> + )} )} @@ -301,6 +349,7 @@ export default function GroupEditor({ open, onClose, group, preset, onSaved }: P {error && {error}} + setMatchesOpen(false)} devices={preview.devices ?? []} /> ); } diff --git a/src/WebUI/src/sections/groups/RuleBuilder.tsx b/src/WebUI/src/sections/groups/RuleBuilder.tsx index 0063a89..d32e0de 100644 --- a/src/WebUI/src/sections/groups/RuleBuilder.tsx +++ b/src/WebUI/src/sections/groups/RuleBuilder.tsx @@ -1,284 +1,579 @@ +import { HTMLAttributes, Key, useEffect, useMemo, useState } from 'react'; + // material-ui +import Autocomplete from '@mui/material/Autocomplete'; import Box from '@mui/material/Box'; import Button from '@mui/material/Button'; -import Checkbox from '@mui/material/Checkbox'; -import Chip from '@mui/material/Chip'; -import FormControlLabel from '@mui/material/FormControlLabel'; -import ListSubheader from '@mui/material/ListSubheader'; import MenuItem from '@mui/material/MenuItem'; import Stack from '@mui/material/Stack'; import TextField from '@mui/material/TextField'; +import ToggleButton from '@mui/material/ToggleButton'; +import ToggleButtonGroup from '@mui/material/ToggleButtonGroup'; import Tooltip from '@mui/material/Tooltip'; import Typography from '@mui/material/Typography'; // project imports import IconButton from 'components/@extended/IconButton'; import HoverHelp from 'components/HoverHelp'; -import type { GroupFieldDescriptor, GroupRuleSet, RuleCondition, RuleGroup, RuleOperator } from 'api/types'; +import { api } from 'api/client'; +import { plural } from 'utils/format'; +import type { + GroupFieldDescriptor, + GroupFieldValueDto, + GroupRuleSet, + RuleCombine, + RuleCondition, + RuleGroup, + RuleOperator +} from 'api/types'; +import { + childPath, + fieldType, + fromGroup, + isListOperator, + joinList, + MAX_RULE_DEPTH, + needsValue, + newCondition, + operatorsFor, + splitList, + toGroup +} from './ruleModel'; // assets -import CloseOutlined from '@ant-design/icons/CloseOutlined'; +import DeleteOutlined from '@ant-design/icons/DeleteOutlined'; import PlusOutlined from '@ant-design/icons/PlusOutlined'; -interface OperatorInfo { - value: RuleOperator; - label: string; - needsValue: boolean; - /** Which fields offer it. */ - applies: 'any' | 'text' | 'numeric' | 'address'; -} - -const OPERATORS: OperatorInfo[] = [ - { value: 'equals', label: 'equals', needsValue: true, applies: 'any' }, - { value: 'notEquals', label: 'does not equal', needsValue: true, applies: 'any' }, - { value: 'contains', label: 'contains', needsValue: true, applies: 'text' }, - { value: 'notContains', label: 'does not contain', needsValue: true, applies: 'text' }, - { value: 'startsWith', label: 'starts with', needsValue: true, applies: 'text' }, - { value: 'endsWith', label: 'ends with', needsValue: true, applies: 'text' }, - { value: 'regex', label: 'matches regex', needsValue: true, applies: 'text' }, - { value: 'in', label: 'is one of', needsValue: true, applies: 'any' }, - { value: 'notIn', label: 'is none of', needsValue: true, applies: 'any' }, - { value: 'greaterThan', label: '>', needsValue: true, applies: 'numeric' }, - { value: 'greaterOrEqual', label: '≥', needsValue: true, applies: 'numeric' }, - { value: 'lessThan', label: '<', needsValue: true, applies: 'numeric' }, - { value: 'lessOrEqual', label: '≤', needsValue: true, applies: 'numeric' }, - { value: 'exists', label: 'has a value', needsValue: false, applies: 'any' }, - { value: 'notExists', label: 'has no value', needsValue: false, applies: 'any' }, - { value: 'inCidr', label: 'is in CIDR range', needsValue: true, applies: 'address' }, - { value: 'notInCidr', label: 'is not in CIDR range', needsValue: true, applies: 'address' } -]; - -const isAddressField = (field?: GroupFieldDescriptor) => Boolean(field && /ipv4|ipaddress|address/i.test(field.field)); - -function operatorsFor(field?: GroupFieldDescriptor): OperatorInfo[] { - return OPERATORS.filter( - (op) => - op.applies === 'any' || - (op.applies === 'numeric' && field?.numeric) || - (op.applies === 'text' && !field?.numeric) || - (op.applies === 'address' && isAddressField(field)) - ); -} - -const needsValue = (op: RuleOperator) => OPERATORS.find((o) => o.value === op)?.needsValue ?? true; - -const newCondition = (): RuleCondition => ({ field: 'hostname', operator: 'contains', value: '' }); - interface Props { rules: GroupRuleSet; fields: GroupFieldDescriptor[]; onChange: (rules: GroupRuleSet) => void; + /** Problems by row path (`2.1`); the rule's own under ''. */ + errors?: Record; disabled?: boolean; } // ==============================|| DYNAMIC GROUP RULE BUILDER ||============================== // -export default function RuleBuilder({ rules, fields, onChange, disabled }: Props) { - const groups = rules.groups ?? []; - const categories = [...new Set(fields.map((f) => f.category))]; - const fieldFor = (name: string) => fields.find((f) => f.field === name); - - const setGroups = (next: RuleGroup[]) => onChange({ ...rules, groups: next }); - const updateGroup = (index: number, patch: Partial) => setGroups(groups.map((g, i) => (i === index ? { ...g, ...patch } : g))); - const updateCondition = (gi: number, ci: number, patch: Partial) => - updateGroup(gi, { conditions: (groups[gi].conditions ?? []).map((c, i) => (i === ci ? { ...c, ...patch } : c)) }); - - const placeholder = (condition: RuleCondition) => { - if (condition.operator === 'in' || condition.operator === 'notIn') return 'a, b, c'; - if (condition.operator === 'regex') return '^PC-\\d+$'; - if (condition.operator === 'inCidr' || condition.operator === 'notInCidr') return '10.0.0.0/8, 192.168.1.0/24'; - return fieldFor(condition.field)?.numeric ? 'Number' : 'Value'; - }; +/** + * The rule is a group: All of or Any of its conditions and groups, optionally negated. Groups hold conditions and groups in turn, and + * Add Condition and Add Group sit inside each, so where a row lands is seen before it is added. + */ +export default function RuleBuilder({ rules, fields, onChange, errors = {}, disabled = false }: Props) { + // The catalog in category order, so the picker can head each category once. + const sorted = useMemo(() => { + const order = [...new Set(fields.map((f) => f.category))]; + return [...fields].sort((a, b) => order.indexOf(a.category) - order.indexOf(b.category)); + }, [fields]); return ( - - - - A device matches when - - onChange({ ...rules, combine: e.target.value as GroupRuleSet['combine'] })} - disabled={disabled} - sx={{ minWidth: 180 }} - > - every block matches - any block matches - - - - + onChange(fromGroup(next))} + fields={sorted} + errors={errors} + disabled={disabled} + depth={1} + path="" + /> + ); +} - {groups.length === 0 && ( - - No rules; matches no devices. +interface BlockProps { + node: RuleGroup; + onChange: (next: RuleGroup) => void; + onRemove?: () => void; + fields: GroupFieldDescriptor[]; + errors: Record; + disabled: boolean; + depth: number; + path: string; +} + +function Block({ node, onChange, onRemove, fields, errors, disabled, depth, path }: BlockProps) { + const conditions = node.conditions ?? []; + const groups = node.groups ?? []; + const nested = depth > 1; + const label = nested ? `Group ${path}` : 'Rule'; + const atLimit = depth >= MAX_RULE_DEPTH; + + const setCondition = (index: number, next: RuleCondition | null) => + onChange({ + ...node, + conditions: next === null ? conditions.filter((_, i) => i !== index) : conditions.map((c, i) => (i === index ? next : c)) + }); + const setGroup = (index: number, next: RuleGroup | null) => + onChange({ ...node, groups: next === null ? groups.filter((_, i) => i !== index) : groups.map((g, i) => (i === index ? next : g)) }); + const firstField = fields.find((f) => f.field === 'hostname') ?? fields[0]; + + return ( + + + + {nested ? 'Group' : 'Match'} + + combine && onChange({ ...node, combine })} + aria-label={`${label}: match`} + > + All of + Any of + + + onChange({ ...node, negate: !node.negate })} + aria-label={`${label}: not`} + > + Not + + + {onRemove && ( + + + + + + + + )} + + {errors[path] && ( + + {errors[path]} )} - {groups.map((group, gi) => ( - { + const rowPath = childPath(path, index + 1); + return ( + setCondition(index, next)} + onRemove={() => setCondition(index, null)} + fields={fields} + error={errors[rowPath]} + disabled={disabled} + name={`Row ${rowPath}`} + /> + ); + })} + {groups.map((group, index) => { + const rowPath = childPath(path, conditions.length + index + 1); + return ( + setGroup(index, next)} + onRemove={() => setGroup(index, null)} + fields={fields} + errors={errors} + disabled={disabled} + depth={depth + 1} + path={rowPath} + /> + ); + })} + {conditions.length === 0 && groups.length === 0 && ( + + {nested ? 'Empty; matches nothing.' : 'No conditions; matches no devices.'} + + )} + + + + + - setGroups(groups.filter((_, i) => i !== gi))} + startIcon={} + disabled={disabled || atLimit} + onClick={() => + onChange({ + ...node, + groups: [...groups, { combine: 'or', conditions: [newCondition(firstField)], groups: [], negate: false }] + }) + } > - - - - - - {(group.conditions ?? []).map((condition, ci) => { - const field = fieldFor(condition.field); - const operators = operatorsFor(field); - return ( - - { - const nextField = fieldFor(e.target.value); - // Keep the operator only if the new field still offers it. - const keep = operatorsFor(nextField).some((o) => o.value === condition.operator); - updateCondition(gi, ci, { field: e.target.value, operator: keep ? condition.operator : 'equals' }); - }} - sx={{ minWidth: 220 }} - > - {categories.flatMap((category) => [ - {category}, - ...fields - .filter((f) => f.category === category) - .map((f) => ( - - {f.displayName} - - )) - ])} - - o.value === condition.operator) ? condition.operator : ''} - onChange={(e) => updateCondition(gi, ci, { operator: e.target.value as RuleOperator })} - disabled={disabled} - sx={{ minWidth: 170 }} - > - {operators.map((op) => ( - - {op.label} - - ))} - - {needsValue(condition.operator) ? ( - - Value - - - ) : ( - 'Value' - ) - } - value={condition.value ?? ''} - placeholder={placeholder(condition)} - onChange={(e) => updateCondition(gi, ci, { value: e.target.value })} - disabled={disabled} - sx={{ flex: 1, minWidth: 160 }} - /> - ) : ( - - )} - - updateCondition(gi, ci, { negate: e.target.checked })} - /> - } - label="Not" - disabled={disabled} - /> - - updateGroup(gi, { conditions: (group.conditions ?? []).filter((_, i) => i !== ci) })} - > - - - - ); - })} - {(group.conditions ?? []).length === 0 && ( - - No conditions; this block matches nothing. - - )} - - - ))} + Add Group + + + + ); } + +// ==============================|| ONE CONDITION ||============================== // + +interface RowProps { + value: RuleCondition; + onChange: (next: RuleCondition) => void; + onRemove: () => void; + fields: GroupFieldDescriptor[]; + error?: string; + disabled: boolean; + /** Names the row for screen readers: "Row 2.1". */ + name: string; +} + +function ConditionRow({ value, onChange, onRemove, fields, error, disabled, name }: RowProps) { + const field = fields.find((f) => f.field.toLowerCase() === value.field.toLowerCase()); + // A field the catalog no longer lists still shows, by its name, so the row reads as saved. + const options = + field || !value.field + ? fields + : [...fields, { field: value.field, displayName: value.field, category: 'Other', numeric: false, multiValue: false }]; + // Null (no field yet) keeps the picker controlled; it shows empty. + const chosen = (options.find((f) => f.field.toLowerCase() === value.field.toLowerCase()) ?? null) as GroupFieldDescriptor; + const operators = operatorsFor(field, value.operator); + + const setField = (next: GroupFieldDescriptor) => { + if (next.field === value.field) return; + // The operator stays if the new field offers it; the value belongs to the old field. + const keep = operatorsFor(next).some((o) => o.value === value.operator); + onChange({ ...value, field: next.field, operator: keep ? value.operator : operatorsFor(next)[0].value, value: '' }); + }; + + // The value carries over: a single value becomes the list's first, and a list keeps its first when one value is wanted. + const setOperator = (operator: RuleOperator) => + onChange({ + ...value, + operator, + value: !needsValue(operator) + ? undefined + : isListOperator(value.operator) && !isListOperator(operator) + ? (splitList(value.value)[0] ?? '') + : (value.value ?? '') + }); + + return ( + + + + size="small" + disableClearable + disabled={disabled} + options={options} + value={chosen} + groupBy={(f) => f.category} + getOptionLabel={(f) => f.displayName} + isOptionEqualToValue={(a, b) => a.field === b.field} + onChange={(_, next) => next && setField(next)} + renderOption={({ key, ...props }, f) => ( +
  • + + + {f.displayName} + + {f.description && ( + + {f.description} + + )} + +
  • + )} + renderInput={(params) => ( + + )} + slotProps={{ popper: { sx: { minWidth: 300 } } }} + sx={{ flex: { md: '0 0 210px' }, minWidth: 0 }} + /> + setOperator(e.target.value as RuleOperator)} + slotProps={{ htmlInput: { 'aria-label': `${name} operator` } }} + sx={{ flex: { md: '0 0 150px' } }} + > + {operators.map((op) => ( + + {op.label} + + ))} + + + {needsValue(value.operator) && ( + + )} + + + + onChange({ ...value, negate: !value.negate })} + aria-label={`${name}: not`} + > + Not + + + + + + + + + + +
    + {error && ( + + {error} + + )} +
    + ); +} + +// ==============================|| THE VALUE ||============================== // + +interface ValueProps { + field?: GroupFieldDescriptor; + value: RuleCondition; + onChange: (next: RuleCondition) => void; + error?: string; + disabled: boolean; + name: string; +} + +const PLACEHOLDERS: Partial> = { regex: '^PC-\\d+$', inCidr: '10.0.0.0/8', notInCidr: '10.0.0.0/8' }; + +/** + * Values the devices report for a field, starting with what is typed: asked for only while the list is open, a moment after typing + * stops. Failures leave the list empty; a value can always be typed. + */ +function useFieldValues(field: string | undefined, prefix: string, active: boolean) { + const [state, setState] = useState<{ key: string; values: GroupFieldValueDto[] }>({ key: '', values: [] }); + const [loading, setLoading] = useState(false); + const key = `${field}\n${prefix}`; + + useEffect(() => { + if (!active || !field) return undefined; + const controller = new AbortController(); + const timer = window.setTimeout(() => { + setLoading(true); + api + .groupFieldValues(field, prefix.trim(), controller.signal) + .then((values) => setState({ key, values })) + .catch(() => !controller.signal.aborted && setState({ key, values: [] })) + .finally(() => !controller.signal.aborted && setLoading(false)); + }, 250); + return () => { + window.clearTimeout(timer); + controller.abort(); + }; + }, [field, prefix, active, key]); + + return { values: state.values, loading }; +} + +function ValueEditor({ field, value, onChange, error, disabled, name }: ValueProps) { + const type = fieldType(field); + const list = isListOperator(value.operator); + const choices = field?.choices ?? []; + const [open, setOpen] = useState(false); + const [typed, setTyped] = useState(''); + const fixed = (type === 'choice' || type === 'boolean') && choices.length > 0; + const pattern = value.operator === 'regex'; + const live = useFieldValues(field?.field, list ? typed : (value.value ?? ''), open && !fixed && !pattern); + const counts = new Map(live.values.map((v) => [v.value, v.devices])); + const label = list ? 'Values' : 'Value'; + const inputLabel = (text: string) => + field?.multiValue ? ( + <> + {text} + + + ) : ( + text + ); + + const renderOption = (props: HTMLAttributes & { key: Key }, option: string) => { + const { key, ...rest } = props; + const devices = counts.get(option); + return ( +
  • + + + {option} + + {devices !== undefined && ( + + {plural(devices, 'device')} + + )} + +
  • + ); + }; + + if (pattern) { + return ( + + Pattern + + + } + value={value.value ?? ''} + placeholder={PLACEHOLDERS.regex} + error={Boolean(error)} + disabled={disabled} + onChange={(e) => onChange({ ...value, value: e.target.value })} + slotProps={{ htmlInput: { 'aria-label': `${name} pattern`, spellCheck: false, style: { fontFamily: 'var(--font-mono)' } } }} + /> + ); + } + + if (fixed && !list) { + // A value a saved rule holds outside the choices stays, so the row reads as saved. + const options = value.value && !choices.includes(value.value) ? [...choices, value.value] : choices; + return ( + onChange({ ...value, value: e.target.value })} + slotProps={{ htmlInput: { 'aria-label': `${name} value` } }} + > + {options.map((c) => ( + + {c} + + ))} + + ); + } + + const suggestions = fixed ? choices : live.values.map((v) => v.value); + + if (list) { + const values = splitList(value.value); + const add = (text: string) => { + if (text.trim()) onChange({ ...value, value: joinList([...values, text]) }); + setTyped(''); + }; + return ( + + multiple + freeSolo={!fixed} + size="small" + disabled={disabled} + options={suggestions} + value={values} + loading={live.loading} + open={open} + onOpen={() => setOpen(true)} + onClose={() => setOpen(false)} + filterOptions={(o) => (fixed ? o : o.filter((v) => !values.some((x) => x.toLowerCase() === v.toLowerCase())))} + inputValue={typed} + onInputChange={(_, text, reason) => reason !== 'reset' && setTyped(text)} + onChange={(_, next) => { + onChange({ ...value, value: joinList(next) }); + setTyped(''); + }} + // A value typed but not yet followed by Enter is kept, not lost, when the row loses focus. + onBlur={() => !fixed && add(typed)} + renderOption={renderOption} + renderInput={(params) => ( + 0 ? undefined : (PLACEHOLDERS[value.operator] ?? 'Each followed by Enter')} + slotProps={{ ...params.slotProps, htmlInput: { ...params.slotProps.htmlInput, 'aria-label': `${name} values` } }} + /> + )} + /> + ); + } + + return ( + + freeSolo + size="small" + disabled={disabled} + options={suggestions} + loading={live.loading} + open={open} + onOpen={() => setOpen(true)} + onClose={() => setOpen(false)} + filterOptions={(o) => o} + value={value.value ?? ''} + inputValue={value.value ?? ''} + onChange={(_, chosen) => onChange({ ...value, value: chosen ?? '' })} + onInputChange={(_, text, reason) => { + // A reset only echoes the value back; it never clears a value the row already holds (a rule opened for editing). + if (reason !== 'reset' || text) onChange({ ...value, value: text }); + }} + renderOption={renderOption} + renderInput={(params) => ( + + )} + /> + ); +} diff --git a/src/WebUI/src/sections/groups/RuleMatchesDialog.tsx b/src/WebUI/src/sections/groups/RuleMatchesDialog.tsx new file mode 100644 index 0000000..12fea74 --- /dev/null +++ b/src/WebUI/src/sections/groups/RuleMatchesDialog.tsx @@ -0,0 +1,87 @@ +// material-ui +import Button from '@mui/material/Button'; +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 Stack from '@mui/material/Stack'; +import Typography from '@mui/material/Typography'; + +// project imports +import DataTable, { Column } from 'components/DataTable'; +import { RelativeTime } from 'components/DateTime'; +import OsIcon from 'components/OsIcon'; +import { DeviceStatusChip } from 'components/StatusChip'; +import { plural } from 'utils/format'; +import type { DeviceDto } from 'api/types'; + +const COLUMNS: Column[] = [ + { id: 'hostname', header: 'Hostname', cell: (d) => d.hostname, sortValue: (d) => d.hostname, nowrap: true }, + { + id: 'status', + header: 'Status', + cell: (d) => , + sortValue: (d) => d.status + }, + { + id: 'os', + header: 'OS', + cell: (d) => ( + + + {d.osCaption ?? '—'} + + ), + sortValue: (d) => d.osCaption + }, + { id: 'model', header: 'Model', cell: (d) => d.model ?? '—', sortValue: (d) => d.model }, + { + id: 'lastSeen', + header: 'Last Seen', + cell: (d) => (d.lastSeenUtc ? : '—'), + sortValue: (d) => d.lastSeenUtc, + nowrap: true + } +]; + +interface Props { + open: boolean; + onClose: () => void; + devices: DeviceDto[]; +} + +// ==============================|| WHAT THE RULE MATCHES ||============================== // + +/** Every device the rule being edited matches now, as the live preview last read it. */ +export default function RuleMatchesDialog({ open, onClose, devices }: Props) { + return ( + + + + + Matching Devices + + + {plural(devices.length, 'device')} now + + + + + + columns={COLUMNS} + rows={devices} + rowId={(d) => d.clientId} + defaultSort={{ id: 'hostname', direction: 'asc' }} + empty="Nothing matches now." + pageSize={25} + dense + /> + + + + + + ); +} diff --git a/src/WebUI/src/sections/groups/ruleModel.ts b/src/WebUI/src/sections/groups/ruleModel.ts new file mode 100644 index 0000000..d02f41b --- /dev/null +++ b/src/WebUI/src/sections/groups/ruleModel.ts @@ -0,0 +1,177 @@ +// The dynamic group rule as the condition builder edits it: operators per field type, row paths, checks and the rule in plain words. + +import type { GroupFieldDescriptor, GroupFieldType, GroupRuleSet, RuleCondition, RuleGroup, RuleOperator } from 'api/types'; + +/** Levels of grouping, the rule itself being the first (the server's GroupRuleLimits.MaxDepth). */ +export const MAX_RULE_DEPTH = 8; + +export interface OperatorInfo { + value: RuleOperator; + label: string; + needsValue: boolean; + /** The value is a list ("a, b, c"), edited as chips. */ + list?: boolean; +} + +export const OPERATORS: Record = { + equals: { value: 'equals', label: 'is', needsValue: true }, + notEquals: { value: 'notEquals', label: 'is not', needsValue: true }, + contains: { value: 'contains', label: 'contains', needsValue: true }, + notContains: { value: 'notContains', label: 'does not contain', needsValue: true }, + startsWith: { value: 'startsWith', label: 'starts with', needsValue: true }, + endsWith: { value: 'endsWith', label: 'ends with', needsValue: true }, + regex: { value: 'regex', label: 'matches', needsValue: true }, + in: { value: 'in', label: 'is any of', needsValue: true, list: true }, + notIn: { value: 'notIn', label: 'is none of', needsValue: true, list: true }, + greaterThan: { value: 'greaterThan', label: '>', needsValue: true }, + greaterOrEqual: { value: 'greaterOrEqual', label: '≥', needsValue: true }, + lessThan: { value: 'lessThan', label: '<', needsValue: true }, + lessOrEqual: { value: 'lessOrEqual', label: '≤', needsValue: true }, + exists: { value: 'exists', label: 'has a value', needsValue: false }, + notExists: { value: 'notExists', label: 'has no value', needsValue: false }, + inCidr: { value: 'inCidr', label: 'is in subnet', needsValue: true, list: true }, + notInCidr: { value: 'notInCidr', label: 'is not in subnet', needsValue: true, list: true } +}; + +const PRESENCE: RuleOperator[] = ['exists', 'notExists']; +const TEXT: RuleOperator[] = ['equals', 'notEquals', 'contains', 'notContains', 'startsWith', 'endsWith', 'regex', 'in', 'notIn']; +const ORDERED: RuleOperator[] = ['greaterThan', 'greaterOrEqual', 'lessThan', 'lessOrEqual']; + +/** The operators each kind of field offers, most useful first. */ +const BY_TYPE: Record = { + text: [...TEXT, ...PRESENCE], + number: ['equals', 'notEquals', ...ORDERED, 'in', 'notIn', ...PRESENCE], + version: ['equals', 'notEquals', ...ORDERED, 'startsWith', 'in', 'notIn', ...PRESENCE], + address: ['inCidr', 'notInCidr', ...TEXT, ...PRESENCE], + choice: ['equals', 'notEquals', 'in', 'notIn', ...PRESENCE], + boolean: ['equals', 'notEquals'] +}; + +export const fieldType = (field?: GroupFieldDescriptor): GroupFieldType => field?.type ?? (field?.numeric ? 'number' : 'text'); + +/** The field's operators; one a saved rule already uses stays offered, so the row reads as it was saved. */ +export function operatorsFor(field: GroupFieldDescriptor | undefined, current?: RuleOperator): OperatorInfo[] { + const ops = BY_TYPE[fieldType(field)]; + return [...ops, ...(current && !ops.includes(current) ? [current] : [])].map((op) => OPERATORS[op]); +} + +export const needsValue = (op: RuleOperator) => OPERATORS[op]?.needsValue ?? true; +export const isListOperator = (op: RuleOperator) => Boolean(OPERATORS[op]?.list); + +/** List values travel as one string; the server splits on commas, semicolons and new lines. */ +export const splitList = (value?: string) => + (value ?? '') + .split(/[,;\n]/) + .map((v) => v.trim()) + .filter(Boolean); +export const joinList = (values: string[]) => [...new Set(values.map((v) => v.trim()).filter(Boolean))].join(', '); + +export function newCondition(field?: GroupFieldDescriptor): RuleCondition { + return { field: field?.field ?? 'hostname', operator: operatorsFor(field)[0]?.value ?? 'equals', value: '' }; +} + +// ==============================|| THE RULE AS ONE GROUP ||============================== // + +/** The rule is the outermost group: the builder edits both through one shape. */ +export const toGroup = (rules: GroupRuleSet): RuleGroup => ({ + combine: rules.combine, + conditions: rules.conditions ?? [], + groups: rules.groups ?? [], + negate: Boolean(rules.negate) +}); + +export const fromGroup = (group: RuleGroup): GroupRuleSet => ({ + combine: group.combine, + groups: group.groups ?? [], + ...((group.conditions ?? []).length > 0 ? { conditions: group.conditions } : {}), + ...(group.negate ? { negate: true } : {}) +}); + +/** + * A rule saved by the two-level builder as one block, opened flat: the block's All/Any, conditions and groups become the rule's own. + * It matches the same devices (one block combined with nothing else is the block), so saving it back changes nothing that matters. + */ +export function normalizeRules(rules: GroupRuleSet): GroupRuleSet { + const groups = rules.groups ?? []; + if ((rules.conditions ?? []).length > 0 || rules.negate || groups.length !== 1 || groups[0].negate) return rules; + const [only] = groups; + return fromGroup({ combine: only.combine, conditions: only.conditions ?? [], groups: only.groups ?? [], negate: false }); +} + +export const ruleIsEmpty = (rules?: GroupRuleSet) => !rules || ((rules.conditions ?? []).length === 0 && (rules.groups ?? []).length === 0); + +/** Rows are numbered within their group, conditions first and then groups, from 1: `2.1` is the first row of the group in row 2. */ +export const childPath = (path: string, row: number) => (path ? `${path}.${row}` : `${row}`); + +// ==============================|| CHECKS ||============================== // + +const isNumber = (text: string) => text.trim() !== '' && Number.isFinite(Number(text)); + +/** + * What the console can tell about a row without asking: a missing field or value, or a number that cannot be read. Patterns and CIDR + * ranges are the server's to judge (it reads them as evaluation does), through the live preview. + */ +function rowProblem(condition: RuleCondition, field: GroupFieldDescriptor | undefined, fieldsKnown: boolean) { + if (!condition.field || (fieldsKnown && !field)) return { message: 'Choose a field.', incomplete: true }; + if (!needsValue(condition.operator)) return null; + const values = isListOperator(condition.operator) ? splitList(condition.value) : [condition.value ?? '']; + if (values.length === 0 || values.every((v) => v.trim() === '')) return { message: 'Enter a value.', incomplete: true }; + if (fieldType(field) === 'number' && condition.operator !== 'regex') { + const bad = values.find((v) => !isNumber(v)); + return bad ? { message: `“${bad}” is not a number.`, incomplete: false } : null; + } + return null; +} + +export interface RuleProblems { + /** Shown as soon as they appear: a value that cannot work. */ + errors: Record; + /** Shown once a save is tried: rows not filled in yet. */ + incomplete: Record; +} + +/** Problems by row path. Patterns (.NET semantics) and ranges are checked by the server, through the live preview. */ +export function checkRules(rules: GroupRuleSet, fields: GroupFieldDescriptor[]): RuleProblems { + const problems: RuleProblems = { errors: {}, incomplete: {} }; + const byName = new Map(fields.map((f) => [f.field.toLowerCase(), f])); + const walk = (group: RuleGroup, path: string) => { + const conditions = group.conditions ?? []; + conditions.forEach((c, i) => { + const problem = rowProblem(c, byName.get(c.field?.toLowerCase()), fields.length > 0); + if (problem) (problem.incomplete ? problems.incomplete : problems.errors)[childPath(path, i + 1)] = problem.message; + }); + (group.groups ?? []).forEach((g, i) => walk(g, childPath(path, conditions.length + i + 1))); + }; + walk(toGroup(rules), ''); + return problems; +} + +// ==============================|| IN PLAIN WORDS ||============================== // + +function describeCondition(condition: RuleCondition, fields: GroupFieldDescriptor[]) { + const name = fields.find((f) => f.field.toLowerCase() === condition.field.toLowerCase())?.displayName ?? condition.field; + const value = needsValue(condition.operator) && condition.value ? ` “${condition.value}”` : ''; + return `${condition.negate ? 'NOT ' : ''}${name} ${OPERATORS[condition.operator]?.label ?? condition.operator}${value}`; +} + +function describeGroup(group: RuleGroup, fields: GroupFieldDescriptor[]): string { + const parts = [ + ...(group.conditions ?? []).map((c) => describeCondition(c, fields)), + ...(group.groups ?? []).map((g) => `(${describeGroup(g, fields)})`) + ]; + const text = parts.length > 0 ? parts.join(group.combine === 'or' ? ' OR ' : ' AND ') : '(nothing)'; + return group.negate ? `NOT (${text})` : text; +} + +/** + * The rule in plain words, one line per row of the rule itself ("Tags is “web”", "OS name contains “Server” OR (…)"), to be joined by + * the rule's AND or OR; a negated rule reads as one line. + */ +export function describeRules(rules: GroupRuleSet | undefined, fields: GroupFieldDescriptor[]): string[] { + if (!rules || ruleIsEmpty(rules)) return []; + if (rules.negate) return [describeGroup(toGroup(rules), fields)]; + return [ + ...(rules.conditions ?? []).map((c) => describeCondition(c, fields)), + ...(rules.groups ?? []).map((g) => describeGroup(g, fields)) + ]; +} -- 2.52.0 From bede763ea1e5c644fe2842c1a2343d0304543f97770117c56b0fee558d8dce8e Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Wed, 4 Nov 2026 20:13:45 -0500 Subject: [PATCH 08/22] feat(secrets): a built-in vault every server has A secrets provider kind "builtin" that is always listed, under a fixed id and with nothing to configure, so script bindings, the picker's folder and name listings and dispatch-time resolution work without an external vault. Secrets live in the application database (VaultSecrets, VaultFolders): folder and name in clear for listing, the value encrypted with the server's Data Protection key ring, which replicas already share. Uniqueness is case-insensitive on every provider through upper-cased key columns; the version doubles as the concurrency token, so concurrent saves from two replicas conflict instead of one winning. The console API browses folders, creates, edits, renames and moves secrets and folders, and deletes them; values only leave through a separate reveal call that takes the new secrets.reveal permission. Every change and reveal is audited under "secrets", never with a value. Co-Authored-By: Claude Opus 5.5 --- src/Backend/Auth/Permissions.cs | 6 +- src/Backend/Data/AppDbContext.cs | 24 + src/Backend/Data/Entities.cs | 33 + .../20261105011039_BuiltInVault.Designer.cs | 1416 +++++++++++++++++ .../Postgres/20261105011039_BuiltInVault.cs | 73 + .../PostgresAppDbContextModelSnapshot.cs | 84 + .../20261105011046_BuiltInVault.Designer.cs | 1416 +++++++++++++++++ .../SqlServer/20261105011046_BuiltInVault.cs | 73 + .../SqlServerAppDbContextModelSnapshot.cs | 84 + .../20261105011032_BuiltInVault.Designer.cs | 1403 ++++++++++++++++ .../Sqlite/20261105011032_BuiltInVault.cs | 73 + .../Sqlite/SqliteAppDbContextModelSnapshot.cs | 84 + src/Backend/Program.cs | 6 +- src/Backend/Secrets/BuiltInVaultDriver.cs | 72 + src/Backend/Secrets/ISecretsProviderDriver.cs | 7 +- src/Backend/Secrets/SecretsEndpoints.cs | 18 +- src/Backend/Secrets/SecretsProviderService.cs | 35 +- src/Backend/Secrets/VaultEndpoints.cs | 140 ++ src/Backend/Secrets/VaultService.cs | 491 ++++++ src/Backend/Security/AuditService.cs | 3 + src/Shared.Contracts/Api/ApiRoutes.cs | 9 + src/Shared.Contracts/Api/ResourceDtos.cs | 46 +- src/Shared.Contracts/Models/SecretBinding.cs | 13 + tests/Backend.Tests/VaultTests.cs | 277 ++++ 24 files changed, 5873 insertions(+), 13 deletions(-) create mode 100644 src/Backend/Data/Migrations/Postgres/20261105011039_BuiltInVault.Designer.cs create mode 100644 src/Backend/Data/Migrations/Postgres/20261105011039_BuiltInVault.cs create mode 100644 src/Backend/Data/Migrations/SqlServer/20261105011046_BuiltInVault.Designer.cs create mode 100644 src/Backend/Data/Migrations/SqlServer/20261105011046_BuiltInVault.cs create mode 100644 src/Backend/Data/Migrations/Sqlite/20261105011032_BuiltInVault.Designer.cs create mode 100644 src/Backend/Data/Migrations/Sqlite/20261105011032_BuiltInVault.cs create mode 100644 src/Backend/Secrets/BuiltInVaultDriver.cs create mode 100644 src/Backend/Secrets/VaultEndpoints.cs create mode 100644 src/Backend/Secrets/VaultService.cs create mode 100644 tests/Backend.Tests/VaultTests.cs diff --git a/src/Backend/Auth/Permissions.cs b/src/Backend/Auth/Permissions.cs index 54b223f..3bdf44d 100644 --- a/src/Backend/Auth/Permissions.cs +++ b/src/Backend/Auth/Permissions.cs @@ -27,6 +27,7 @@ public static class Permissions public const string AutomationManage = "automation.manage"; public const string SecretsUse = "secrets.use"; public const string SecretsManage = "secrets.manage"; + public const string SecretsReveal = "secrets.reveal"; public const string ShellUse = "shell.use"; public const string FilesSend = "files.send"; public const string FilesBrowse = "files.browse"; @@ -70,8 +71,9 @@ public static class Permissions ]), new("Secrets", [ - new(SecretsUse, "Use secrets", "Browse provider paths and bind secrets into scripts."), - new(SecretsManage, "Manage secret providers", "Configure and test secrets provider connections."), + new(SecretsUse, "Use secrets", "Browse provider paths and the built-in vault, and bind secrets into scripts."), + new(SecretsManage, "Manage secrets", "Configure and test secrets provider connections, and edit the built-in vault."), + new(SecretsReveal, "Reveal secrets", "Show built-in vault values in the console. Every reveal is audited."), ]), new("Remote access", [ diff --git a/src/Backend/Data/AppDbContext.cs b/src/Backend/Data/AppDbContext.cs index 7dafe08..afe88f9 100644 --- a/src/Backend/Data/AppDbContext.cs +++ b/src/Backend/Data/AppDbContext.cs @@ -31,6 +31,8 @@ public class AppDbContext(DbContextOptions options) : DbContext(op public DbSet ScriptTriggerRuns => Set(); public DbSet AppSettings => Set(); public DbSet SecretsProviders => Set(); + public DbSet VaultSecrets => Set(); + public DbSet VaultFolders => Set(); public DbSet AgentPackages => Set(); public DbSet AgentUpgrades => Set(); public DbSet DeviceRemovals => Set(); @@ -325,6 +327,28 @@ public class AppDbContext(DbContextOptions options) : DbContext(op entity.HasIndex(e => e.Name).IsUnique(); }); + modelBuilder.Entity(entity => + { + entity.ToTable("VaultSecrets"); + entity.HasKey(e => e.Id); + entity.Property(e => e.Path).HasMaxLength(512); + entity.Property(e => e.PathKey).HasMaxLength(512); + entity.Property(e => e.Name).HasMaxLength(200); + entity.Property(e => e.NameKey).HasMaxLength(200); + // Replicas creating the same secret at once race on this. + entity.HasIndex(e => new { e.PathKey, e.NameKey }).IsUnique(); + entity.Property(e => e.Version).IsConcurrencyToken(); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("VaultFolders"); + entity.HasKey(e => e.Id); + entity.Property(e => e.Path).HasMaxLength(512); + entity.Property(e => e.PathKey).HasMaxLength(512); + entity.HasIndex(e => e.PathKey).IsUnique(); + }); + modelBuilder.Entity(entity => { entity.ToTable("AgentPackages"); diff --git a/src/Backend/Data/Entities.cs b/src/Backend/Data/Entities.cs index ef4df3e..2e09ff4 100644 --- a/src/Backend/Data/Entities.cs +++ b/src/Backend/Data/Entities.cs @@ -364,6 +364,39 @@ public sealed class SecretsProviderEntity public string? UpdatedBy { get; set; } } +/// +/// A secret in the built-in vault. Folder and name are clear text so the +/// vault can be listed; the value is Data Protection-encrypted. The key +/// columns are the upper-cased folder and name, so uniqueness and lookups are +/// case-insensitive on every database provider. +/// +public sealed class VaultSecretEntity +{ + public Guid Id { get; set; } + /// Normalized folder, "/" or "/a/b". + public required string Path { get; set; } + public required string PathKey { get; set; } + public required string Name { get; set; } + public required string NameKey { get; set; } + public required string ValueProtected { get; set; } + /// Starts at 1 and counts value changes; also the concurrency token. + public int Version { get; set; } + public DateTimeOffset CreatedUtc { get; set; } + public string? CreatedBy { get; set; } + public DateTimeOffset UpdatedUtc { get; set; } + public string? UpdatedBy { get; set; } +} + +/// A folder created in the built-in vault before (or without) holding secrets. +public sealed class VaultFolderEntity +{ + public Guid Id { get; set; } + public required string Path { get; set; } + public required string PathKey { get; set; } + public DateTimeOffset CreatedUtc { get; set; } + public string? CreatedBy { get; set; } +} + /// /// Operator-editable application settings that override environment defaults. /// Values marked secret are encrypted at rest with ASP.NET Data Protection and diff --git a/src/Backend/Data/Migrations/Postgres/20261105011039_BuiltInVault.Designer.cs b/src/Backend/Data/Migrations/Postgres/20261105011039_BuiltInVault.Designer.cs new file mode 100644 index 0000000..cb8fe7d --- /dev/null +++ b/src/Backend/Data/Migrations/Postgres/20261105011039_BuiltInVault.Designer.cs @@ -0,0 +1,1416 @@ +// +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("20261105011039_BuiltInVault")] + partial class BuiltInVault + { + /// + 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("NotBeforeUtc") + .HasColumnType("timestamp with time zone"); + + 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("ErrorId") + .HasMaxLength(300) + .HasColumnType("character varying(300)"); + + b.Property("ErrorMessage") + .HasMaxLength(2000) + .HasColumnType("character varying(2000)"); + + b.Property("ErrorType") + .HasMaxLength(300) + .HasColumnType("character varying(300)"); + + b.Property("ExitCode") + .HasColumnType("integer"); + + b.Property("NotBeforeUtc") + .HasColumnType("timestamp with time zone"); + + 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.HasIndex("State", "NotBeforeUtc"); + + 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("SnapshotProtected") + .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("StaggerBatchSize") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasDefaultValue(1); + + b.Property("StaggerSeconds") + .HasColumnType("integer"); + + 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.VaultFolderEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedBy") + .HasColumnType("text"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Path") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("character varying(512)"); + + b.Property("PathKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("character varying(512)"); + + b.HasKey("Id"); + + b.HasIndex("PathKey") + .IsUnique(); + + b.ToTable("VaultFolders", (string)null); + }); + + modelBuilder.Entity("Backend.Data.VaultSecretEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedBy") + .HasColumnType("text"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)"); + + b.Property("NameKey") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)"); + + b.Property("Path") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("character varying(512)"); + + b.Property("PathKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("character varying(512)"); + + b.Property("UpdatedBy") + .HasColumnType("text"); + + b.Property("UpdatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("ValueProtected") + .IsRequired() + .HasColumnType("text"); + + b.Property("Version") + .IsConcurrencyToken() + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("PathKey", "NameKey") + .IsUnique(); + + b.ToTable("VaultSecrets", (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/20261105011039_BuiltInVault.cs b/src/Backend/Data/Migrations/Postgres/20261105011039_BuiltInVault.cs new file mode 100644 index 0000000..c4d3250 --- /dev/null +++ b/src/Backend/Data/Migrations/Postgres/20261105011039_BuiltInVault.cs @@ -0,0 +1,73 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Backend.Data.Migrations.Postgres +{ + /// + public partial class BuiltInVault : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "VaultFolders", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Path = table.Column(type: "character varying(512)", maxLength: 512, nullable: false), + PathKey = table.Column(type: "character varying(512)", maxLength: 512, nullable: false), + CreatedUtc = table.Column(type: "timestamp with time zone", nullable: false), + CreatedBy = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_VaultFolders", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "VaultSecrets", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Path = table.Column(type: "character varying(512)", maxLength: 512, nullable: false), + PathKey = table.Column(type: "character varying(512)", maxLength: 512, nullable: false), + Name = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), + NameKey = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), + ValueProtected = table.Column(type: "text", nullable: false), + Version = table.Column(type: "integer", nullable: false), + CreatedUtc = table.Column(type: "timestamp with time zone", nullable: false), + CreatedBy = table.Column(type: "text", nullable: true), + UpdatedUtc = table.Column(type: "timestamp with time zone", nullable: false), + UpdatedBy = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_VaultSecrets", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_VaultFolders_PathKey", + table: "VaultFolders", + column: "PathKey", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_VaultSecrets_PathKey_NameKey", + table: "VaultSecrets", + columns: new[] { "PathKey", "NameKey" }, + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "VaultFolders"); + + migrationBuilder.DropTable( + name: "VaultSecrets"); + } + } +} diff --git a/src/Backend/Data/Migrations/Postgres/PostgresAppDbContextModelSnapshot.cs b/src/Backend/Data/Migrations/Postgres/PostgresAppDbContextModelSnapshot.cs index ce17ca2..34b2f1d 100644 --- a/src/Backend/Data/Migrations/Postgres/PostgresAppDbContextModelSnapshot.cs +++ b/src/Backend/Data/Migrations/Postgres/PostgresAppDbContextModelSnapshot.cs @@ -1275,6 +1275,90 @@ namespace Backend.Data.Migrations.Postgres b.ToTable("Users", (string)null); }); + modelBuilder.Entity("Backend.Data.VaultFolderEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedBy") + .HasColumnType("text"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Path") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("character varying(512)"); + + b.Property("PathKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("character varying(512)"); + + b.HasKey("Id"); + + b.HasIndex("PathKey") + .IsUnique(); + + b.ToTable("VaultFolders", (string)null); + }); + + modelBuilder.Entity("Backend.Data.VaultSecretEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedBy") + .HasColumnType("text"); + + b.Property("CreatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)"); + + b.Property("NameKey") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)"); + + b.Property("Path") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("character varying(512)"); + + b.Property("PathKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("character varying(512)"); + + b.Property("UpdatedBy") + .HasColumnType("text"); + + b.Property("UpdatedUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("ValueProtected") + .IsRequired() + .HasColumnType("text"); + + b.Property("Version") + .IsConcurrencyToken() + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("PathKey", "NameKey") + .IsUnique(); + + b.ToTable("VaultSecrets", (string)null); + }); + modelBuilder.Entity("Backend.Data.DeviceInventoryEntity", b => { b.HasOne("Backend.Data.DeviceEntity", null) diff --git a/src/Backend/Data/Migrations/SqlServer/20261105011046_BuiltInVault.Designer.cs b/src/Backend/Data/Migrations/SqlServer/20261105011046_BuiltInVault.Designer.cs new file mode 100644 index 0000000..5bf738e --- /dev/null +++ b/src/Backend/Data/Migrations/SqlServer/20261105011046_BuiltInVault.Designer.cs @@ -0,0 +1,1416 @@ +// +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("20261105011046_BuiltInVault")] + partial class BuiltInVault + { + /// + 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("NotBeforeUtc") + .HasColumnType("datetimeoffset"); + + 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("ErrorId") + .HasMaxLength(300) + .HasColumnType("nvarchar(300)"); + + b.Property("ErrorMessage") + .HasMaxLength(2000) + .HasColumnType("nvarchar(2000)"); + + b.Property("ErrorType") + .HasMaxLength(300) + .HasColumnType("nvarchar(300)"); + + b.Property("ExitCode") + .HasColumnType("int"); + + b.Property("NotBeforeUtc") + .HasColumnType("datetimeoffset"); + + 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(450)"); + + b.HasKey("JobId"); + + b.HasIndex("ClientId"); + + b.HasIndex("CreatedUtc"); + + b.HasIndex("RunId"); + + b.HasIndex("ScriptId", "RunId"); + + b.HasIndex("State", "NotBeforeUtc"); + + 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("SnapshotProtected") + .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("StaggerBatchSize") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasDefaultValue(1); + + b.Property("StaggerSeconds") + .HasColumnType("int"); + + 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.VaultFolderEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Path") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("nvarchar(512)"); + + b.Property("PathKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("nvarchar(512)"); + + b.HasKey("Id"); + + b.HasIndex("PathKey") + .IsUnique(); + + b.ToTable("VaultFolders", (string)null); + }); + + modelBuilder.Entity("Backend.Data.VaultSecretEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("NameKey") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("Path") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("nvarchar(512)"); + + b.Property("PathKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("nvarchar(512)"); + + b.Property("UpdatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("ValueProtected") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Version") + .IsConcurrencyToken() + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PathKey", "NameKey") + .IsUnique(); + + b.ToTable("VaultSecrets", (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/20261105011046_BuiltInVault.cs b/src/Backend/Data/Migrations/SqlServer/20261105011046_BuiltInVault.cs new file mode 100644 index 0000000..36669d3 --- /dev/null +++ b/src/Backend/Data/Migrations/SqlServer/20261105011046_BuiltInVault.cs @@ -0,0 +1,73 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Backend.Data.Migrations.SqlServer +{ + /// + public partial class BuiltInVault : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "VaultFolders", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + Path = table.Column(type: "nvarchar(512)", maxLength: 512, nullable: false), + PathKey = table.Column(type: "nvarchar(512)", maxLength: 512, nullable: false), + CreatedUtc = table.Column(type: "datetimeoffset", nullable: false), + CreatedBy = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_VaultFolders", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "VaultSecrets", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + Path = table.Column(type: "nvarchar(512)", maxLength: 512, nullable: false), + PathKey = table.Column(type: "nvarchar(512)", maxLength: 512, nullable: false), + Name = table.Column(type: "nvarchar(200)", maxLength: 200, nullable: false), + NameKey = table.Column(type: "nvarchar(200)", maxLength: 200, nullable: false), + ValueProtected = table.Column(type: "nvarchar(max)", nullable: false), + Version = table.Column(type: "int", nullable: false), + CreatedUtc = table.Column(type: "datetimeoffset", nullable: false), + CreatedBy = table.Column(type: "nvarchar(max)", nullable: true), + UpdatedUtc = table.Column(type: "datetimeoffset", nullable: false), + UpdatedBy = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_VaultSecrets", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_VaultFolders_PathKey", + table: "VaultFolders", + column: "PathKey", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_VaultSecrets_PathKey_NameKey", + table: "VaultSecrets", + columns: new[] { "PathKey", "NameKey" }, + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "VaultFolders"); + + migrationBuilder.DropTable( + name: "VaultSecrets"); + } + } +} diff --git a/src/Backend/Data/Migrations/SqlServer/SqlServerAppDbContextModelSnapshot.cs b/src/Backend/Data/Migrations/SqlServer/SqlServerAppDbContextModelSnapshot.cs index c017ae2..0ec52b4 100644 --- a/src/Backend/Data/Migrations/SqlServer/SqlServerAppDbContextModelSnapshot.cs +++ b/src/Backend/Data/Migrations/SqlServer/SqlServerAppDbContextModelSnapshot.cs @@ -1275,6 +1275,90 @@ namespace Backend.Data.Migrations.SqlServer b.ToTable("Users", (string)null); }); + modelBuilder.Entity("Backend.Data.VaultFolderEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Path") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("nvarchar(512)"); + + b.Property("PathKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("nvarchar(512)"); + + b.HasKey("Id"); + + b.HasIndex("PathKey") + .IsUnique(); + + b.ToTable("VaultFolders", (string)null); + }); + + modelBuilder.Entity("Backend.Data.VaultSecretEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("CreatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("NameKey") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("Path") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("nvarchar(512)"); + + b.Property("PathKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("nvarchar(512)"); + + b.Property("UpdatedBy") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedUtc") + .HasColumnType("datetimeoffset"); + + b.Property("ValueProtected") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Version") + .IsConcurrencyToken() + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PathKey", "NameKey") + .IsUnique(); + + b.ToTable("VaultSecrets", (string)null); + }); + modelBuilder.Entity("Backend.Data.DeviceInventoryEntity", b => { b.HasOne("Backend.Data.DeviceEntity", null) diff --git a/src/Backend/Data/Migrations/Sqlite/20261105011032_BuiltInVault.Designer.cs b/src/Backend/Data/Migrations/Sqlite/20261105011032_BuiltInVault.Designer.cs new file mode 100644 index 0000000..cc4dd3f --- /dev/null +++ b/src/Backend/Data/Migrations/Sqlite/20261105011032_BuiltInVault.Designer.cs @@ -0,0 +1,1403 @@ +// +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("20261105011032_BuiltInVault")] + partial class BuiltInVault + { + /// + 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("NotBeforeUtc") + .HasColumnType("INTEGER"); + + 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("ErrorId") + .HasMaxLength(300) + .HasColumnType("TEXT"); + + b.Property("ErrorMessage") + .HasMaxLength(2000) + .HasColumnType("TEXT"); + + b.Property("ErrorType") + .HasMaxLength(300) + .HasColumnType("TEXT"); + + b.Property("ExitCode") + .HasColumnType("INTEGER"); + + b.Property("NotBeforeUtc") + .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.HasIndex("State", "NotBeforeUtc"); + + 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("SnapshotProtected") + .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("StaggerBatchSize") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER") + .HasDefaultValue(1); + + b.Property("StaggerSeconds") + .HasColumnType("INTEGER"); + + 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.VaultFolderEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("CreatedBy") + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("Path") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("PathKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("PathKey") + .IsUnique(); + + b.ToTable("VaultFolders", (string)null); + }); + + modelBuilder.Entity("Backend.Data.VaultSecretEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("CreatedBy") + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("TEXT"); + + b.Property("NameKey") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("TEXT"); + + b.Property("Path") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("PathKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("UpdatedBy") + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("INTEGER"); + + b.Property("ValueProtected") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Version") + .IsConcurrencyToken() + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("PathKey", "NameKey") + .IsUnique(); + + b.ToTable("VaultSecrets", (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/20261105011032_BuiltInVault.cs b/src/Backend/Data/Migrations/Sqlite/20261105011032_BuiltInVault.cs new file mode 100644 index 0000000..2602102 --- /dev/null +++ b/src/Backend/Data/Migrations/Sqlite/20261105011032_BuiltInVault.cs @@ -0,0 +1,73 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Backend.Data.Migrations.Sqlite +{ + /// + public partial class BuiltInVault : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "VaultFolders", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + Path = table.Column(type: "TEXT", maxLength: 512, nullable: false), + PathKey = table.Column(type: "TEXT", maxLength: 512, nullable: false), + CreatedUtc = table.Column(type: "INTEGER", nullable: false), + CreatedBy = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_VaultFolders", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "VaultSecrets", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + Path = table.Column(type: "TEXT", maxLength: 512, nullable: false), + PathKey = table.Column(type: "TEXT", maxLength: 512, nullable: false), + Name = table.Column(type: "TEXT", maxLength: 200, nullable: false), + NameKey = table.Column(type: "TEXT", maxLength: 200, nullable: false), + ValueProtected = table.Column(type: "TEXT", nullable: false), + Version = table.Column(type: "INTEGER", nullable: false), + CreatedUtc = table.Column(type: "INTEGER", nullable: false), + CreatedBy = table.Column(type: "TEXT", nullable: true), + UpdatedUtc = table.Column(type: "INTEGER", nullable: false), + UpdatedBy = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_VaultSecrets", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_VaultFolders_PathKey", + table: "VaultFolders", + column: "PathKey", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_VaultSecrets_PathKey_NameKey", + table: "VaultSecrets", + columns: new[] { "PathKey", "NameKey" }, + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "VaultFolders"); + + migrationBuilder.DropTable( + name: "VaultSecrets"); + } + } +} diff --git a/src/Backend/Data/Migrations/Sqlite/SqliteAppDbContextModelSnapshot.cs b/src/Backend/Data/Migrations/Sqlite/SqliteAppDbContextModelSnapshot.cs index b0a8335..54d86ea 100644 --- a/src/Backend/Data/Migrations/Sqlite/SqliteAppDbContextModelSnapshot.cs +++ b/src/Backend/Data/Migrations/Sqlite/SqliteAppDbContextModelSnapshot.cs @@ -1262,6 +1262,90 @@ namespace Backend.Data.Migrations.Sqlite b.ToTable("Users", (string)null); }); + modelBuilder.Entity("Backend.Data.VaultFolderEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("CreatedBy") + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("Path") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("PathKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("PathKey") + .IsUnique(); + + b.ToTable("VaultFolders", (string)null); + }); + + modelBuilder.Entity("Backend.Data.VaultSecretEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("CreatedBy") + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("TEXT"); + + b.Property("NameKey") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("TEXT"); + + b.Property("Path") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("PathKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("UpdatedBy") + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("INTEGER"); + + b.Property("ValueProtected") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Version") + .IsConcurrencyToken() + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("PathKey", "NameKey") + .IsUnique(); + + b.ToTable("VaultSecrets", (string)null); + }); + modelBuilder.Entity("Backend.Data.DeviceInventoryEntity", b => { b.HasOne("Backend.Data.DeviceEntity", null) diff --git a/src/Backend/Program.cs b/src/Backend/Program.cs index f16583f..b1abd37 100644 --- a/src/Backend/Program.cs +++ b/src/Backend/Program.cs @@ -183,10 +183,13 @@ if (Backend.Standalone.NatsSidecar.IsEnabled(builder.Configuration)) builder.Services.AddHostedService(sp => sp.GetRequiredService()); } -// Secrets providers: pluggable drivers; add implementations here. +// Secrets providers: pluggable drivers; add implementations here. The +// built-in vault is one of them, kept in the application database. builder.Services.AddHttpClient(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddSingleton(); builder.Services.AddHttpContextAccessor(); builder.Services.AddSingleton(sp => new Backend.DeviceFiles.NatsDeviceFileClient(() => sp.GetRequiredService().Connection)); @@ -421,6 +424,7 @@ SettingsEndpoints.Map(app); Backend.Upgrades.AgentUpgradeEndpoints.Map(app); Backend.Removals.DeviceRemovalEndpoints.Map(app); Backend.Secrets.SecretsEndpoints.Map(app); +Backend.Secrets.VaultEndpoints.Map(app); if (staticUiPath is not null) { diff --git a/src/Backend/Secrets/BuiltInVaultDriver.cs b/src/Backend/Secrets/BuiltInVaultDriver.cs new file mode 100644 index 0000000..80bb51d --- /dev/null +++ b/src/Backend/Secrets/BuiltInVaultDriver.cs @@ -0,0 +1,72 @@ +using Shared.Contracts.Api; +using Shared.Contracts.Models; + +namespace Backend.Secrets; + +/// +/// The built-in vault as a provider kind, so script bindings, the picker's +/// listings and dispatch-time resolution treat it like any external store. +/// It has no config: the secrets live in this server's database. +/// +public sealed class BuiltInVaultDriver(IServiceScopeFactory scopes) : ISecretsProviderDriver +{ + public string Kind => BuiltInVault.Kind; + + public string DisplayName => BuiltInVault.DisplayName; + + public IReadOnlyList SecretFields { get; } = []; + + public IReadOnlyList Fields { get; } = []; + + public async Task> TestAsync(string configJson, CancellationToken cancellationToken) + { + var steps = new List(); + await using var scope = scopes.CreateAsyncScope(); + var vault = scope.ServiceProvider.GetRequiredService(); + + try + { + steps.Add(new S3TestStepDto("database", true, $"{await vault.CountAsync(cancellationToken)} secret(s)")); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + steps.Add(new S3TestStepDto("database", false, ex.Message)); + return steps; + } + + try + { + steps.Add(vault.KeyRingWorks() + ? new S3TestStepDto("encryption", true, "key ring round trip ok") + : new S3TestStepDto("encryption", false, "key ring round trip returned a different value")); + } + catch (Exception ex) + { + steps.Add(new S3TestStepDto("encryption", false, ex.Message)); + } + + return steps; + } + + public Task> ListSecretNamesAsync(string configJson, string? path, CancellationToken cancellationToken) + => WithVaultAsync(vault => vault.NamesAsync(path, cancellationToken)); + + public Task> ListFolderPathsAsync(string configJson, string? path, CancellationToken cancellationToken) + => WithVaultAsync(vault => vault.FolderPathsAsync(path, cancellationToken)); + + public Task GetSecretAsync(string configJson, string secretName, string? path, CancellationToken cancellationToken) + => WithVaultAsync(vault => vault.GetValueAsync(path, secretName, cancellationToken)); + + private async Task WithVaultAsync(Func> action) + { + await using var scope = scopes.CreateAsyncScope(); + try + { + return await action(scope.ServiceProvider.GetRequiredService()); + } + catch (VaultException ex) + { + throw new SecretResolutionException(ex.Message, ex); + } + } +} diff --git a/src/Backend/Secrets/ISecretsProviderDriver.cs b/src/Backend/Secrets/ISecretsProviderDriver.cs index 3391dea..b546b52 100644 --- a/src/Backend/Secrets/ISecretsProviderDriver.cs +++ b/src/Backend/Secrets/ISecretsProviderDriver.cs @@ -3,9 +3,10 @@ using Shared.Contracts.Api; namespace Backend.Secrets; /// -/// One kind of external secrets store. Drivers are stateless singletons; the -/// per-provider connection settings arrive as the decrypted driver-specific -/// config JSON. Register additional implementations to add providers. +/// One kind of secrets store, external or the built-in vault. Drivers are +/// stateless singletons; the per-provider connection settings arrive as the +/// decrypted driver-specific config JSON. Register additional implementations +/// to add providers. /// public interface ISecretsProviderDriver { diff --git a/src/Backend/Secrets/SecretsEndpoints.cs b/src/Backend/Secrets/SecretsEndpoints.cs index 3c2787a..b785c64 100644 --- a/src/Backend/Secrets/SecretsEndpoints.cs +++ b/src/Backend/Secrets/SecretsEndpoints.cs @@ -1,6 +1,7 @@ using Backend.Auth; using Backend.Security; using Shared.Contracts.Api; +using Shared.Contracts.Models; namespace Backend.Secrets; @@ -59,7 +60,7 @@ public static class SecretsEndpoints { var providers = await service.ListAsync(cancellationToken); var kinds = service.Drivers - .Select(d => new { kind = d.Kind, displayName = d.DisplayName, fields = d.Fields }) + .Select(d => new { kind = d.Kind, displayName = d.DisplayName, fields = d.Fields, builtIn = d.Kind == BuiltInVault.Kind }) .ToList(); return Results.Ok(new { providers, kinds }); } @@ -96,6 +97,11 @@ public static class SecretsEndpoints return problem; } + if (SecretsProviderService.IsBuiltIn(id)) + { + return Results.BadRequest(new { error = "The built-in vault has no settings to change." }); + } + var entity = await service.FindAsync(id, cancellationToken); if (entity is null) { @@ -117,6 +123,11 @@ public static class SecretsEndpoints HttpContext context, CancellationToken cancellationToken) { + if (SecretsProviderService.IsBuiltIn(id)) + { + return Results.BadRequest(new { error = "The built-in vault cannot be deleted." }); + } + var entity = await service.FindAsync(id, cancellationToken); if (entity is null) { @@ -188,6 +199,11 @@ public static class SecretsEndpoints return Results.BadRequest(new { error = $"Unknown provider kind “{request.Kind}”." }); } + if (string.Equals(request.Kind, BuiltInVault.Kind, StringComparison.OrdinalIgnoreCase)) + { + return Results.BadRequest(new { error = "The built-in vault is always there; it cannot be added." }); + } + return null; } } diff --git a/src/Backend/Secrets/SecretsProviderService.cs b/src/Backend/Secrets/SecretsProviderService.cs index 5e2e6fc..b77b069 100644 --- a/src/Backend/Secrets/SecretsProviderService.cs +++ b/src/Backend/Secrets/SecretsProviderService.cs @@ -12,7 +12,8 @@ namespace Backend.Secrets; /// /// Stores secrets provider configurations (encrypted at rest with Data /// Protection) and resolves script secret bindings server-side, so agents -/// and browsers never hold vault credentials or contact the vault. +/// and browsers never hold vault credentials or contact the vault. The +/// built-in vault is always listed, under a fixed id, without a stored row. /// public sealed class SecretsProviderService( AppDbContext db, @@ -26,16 +27,31 @@ public sealed class SecretsProviderService( public ISecretsProviderDriver? Driver(string kind) => drivers.FirstOrDefault(d => string.Equals(d.Kind, kind, StringComparison.OrdinalIgnoreCase)); + /// Every provider, the built-in vault first (so it is the picker's default). public async Task> ListAsync(CancellationToken cancellationToken) { var rows = await db.SecretsProviders.AsNoTracking() .OrderBy(p => p.Name) .ToListAsync(cancellationToken); - return rows.Select(ToDto).ToList(); + return rows.Prepend(BuiltIn()).Select(ToDto).ToList(); } - public Task FindAsync(Guid id, CancellationToken cancellationToken) - => db.SecretsProviders.SingleOrDefaultAsync(p => p.Id == id, cancellationToken); + /// A stored provider, or the built-in vault's stand-in (never tracked, never saved). + public async Task FindAsync(Guid id, CancellationToken cancellationToken) + => IsBuiltIn(id) ? BuiltIn() : await db.SecretsProviders.SingleOrDefaultAsync(p => p.Id == id, cancellationToken); + + public static bool IsBuiltIn(Guid id) => id == BuiltInVault.ProviderId; + + /// The built-in vault has no provider row: every server has it, with nothing to configure. + private static SecretsProviderEntity BuiltIn() => new() + { + Id = BuiltInVault.ProviderId, + Name = BuiltInVault.DisplayName, + Kind = BuiltInVault.Kind, + ConfigProtected = string.Empty, + CreatedUtc = DateTimeOffset.UnixEpoch, + UpdatedUtc = DateTimeOffset.UnixEpoch, + }; public async Task CreateAsync( SaveSecretsProviderRequest request, string? actor, CancellationToken cancellationToken) @@ -99,6 +115,10 @@ public sealed class SecretsProviderService( var providers = await db.SecretsProviders.AsNoTracking() .Where(p => providerIds.Contains(p.Id)) .ToDictionaryAsync(p => p.Id, cancellationToken); + if (providerIds.Any(IsBuiltIn)) + { + providers[BuiltInVault.ProviderId] = BuiltIn(); + } var configCache = new Dictionary(); var result = new List(bindings.Count); @@ -156,11 +176,16 @@ public sealed class SecretsProviderService( .ToList(); return new SecretsProviderDto( - entity.Id, entity.Name, entity.Kind, sanitized, set, entity.UpdatedUtc, entity.UpdatedBy); + entity.Id, entity.Name, entity.Kind, sanitized, set, entity.UpdatedUtc, entity.UpdatedBy, IsBuiltIn(entity.Id)); } private Dictionary DecryptConfig(SecretsProviderEntity entity) { + if (IsBuiltIn(entity.Id)) + { + return new Dictionary(StringComparer.OrdinalIgnoreCase); + } + try { var json = protector.Unprotect(entity.ConfigProtected); diff --git a/src/Backend/Secrets/VaultEndpoints.cs b/src/Backend/Secrets/VaultEndpoints.cs new file mode 100644 index 0000000..29c601c --- /dev/null +++ b/src/Backend/Secrets/VaultEndpoints.cs @@ -0,0 +1,140 @@ +using Backend.Auth; +using Backend.Security; +using Shared.Contracts.Api; + +namespace Backend.Secrets; + +/// +/// The built-in vault's console API. Listings carry names and folders only; +/// a value leaves the server through the reveal call alone, which needs its +/// own permission and is audited. Every change is audited too, never with +/// the value. +/// +public static class VaultEndpoints +{ + public static void Map(WebApplication app) + { + app.MapGet(ApiRoutes.Vault, ListAsync).RequirePermission(Permissions.SecretsUse); + app.MapPost(ApiRoutes.VaultFolders, CreateFolderAsync).RequirePermission(Permissions.SecretsManage); + app.MapDelete(ApiRoutes.VaultFolders, DeleteFolderAsync).RequirePermission(Permissions.SecretsManage); + app.MapPost(ApiRoutes.VaultFolderMove, MoveFolderAsync).RequirePermission(Permissions.SecretsManage); + app.MapPost(ApiRoutes.VaultSecrets, CreateAsync).RequirePermission(Permissions.SecretsManage); + app.MapPut(ApiRoutes.VaultSecret, UpdateAsync).RequirePermission(Permissions.SecretsManage); + app.MapDelete(ApiRoutes.VaultSecret, DeleteAsync).RequirePermission(Permissions.SecretsManage); + app.MapPost(ApiRoutes.VaultSecretReveal, RevealAsync).RequirePermission(Permissions.SecretsReveal); + } + + private static Task ListAsync(string? path, VaultService vault, CancellationToken cancellationToken) + => GuardAsync(async () => Results.Ok(await vault.ListAsync(path, cancellationToken))); + + private static Task CreateFolderAsync( + VaultFolderRequest request, VaultService vault, AuditService audit, HttpContext context, + CancellationToken cancellationToken) + => GuardAsync(async () => + { + var path = await vault.CreateFolderAsync(request.Path, Actor(context), cancellationToken); + await LogAsync(audit, context, "vault-folder-created", path, cancellationToken); + return Results.Ok(new VaultFolderDto(path, path[(path.LastIndexOf('/') + 1)..])); + }); + + private static Task MoveFolderAsync( + MoveVaultFolderRequest request, VaultService vault, AuditService audit, HttpContext context, + CancellationToken cancellationToken) + => GuardAsync(async () => + { + var (from, to, count) = await vault.MoveFolderAsync(request.From, request.To, Actor(context), cancellationToken); + await LogAsync(audit, context, "vault-folder-moved", $"{from} → {to} ({count} secret(s))", cancellationToken); + return Results.Ok(new VaultFolderDto(to, to[(to.LastIndexOf('/') + 1)..])); + }); + + private static Task DeleteFolderAsync( + string? path, VaultService vault, AuditService audit, HttpContext context, CancellationToken cancellationToken) + => GuardAsync(async () => + { + var (folder, count) = await vault.DeleteFolderAsync(path ?? string.Empty, cancellationToken); + await LogAsync(audit, context, "vault-folder-deleted", $"{folder} ({count} secret(s))", cancellationToken); + return Results.NoContent(); + }); + + private static Task CreateAsync( + SaveVaultSecretRequest request, VaultService vault, AuditService audit, HttpContext context, + CancellationToken cancellationToken) + => GuardAsync(async () => + { + var dto = await vault.CreateAsync(request, Actor(context), cancellationToken); + await LogAsync(audit, context, "vault-secret-created", Describe(dto), cancellationToken); + return Results.Created(ApiRoutes.For.VaultSecret(dto.Id), dto); + }); + + private static Task UpdateAsync( + Guid id, SaveVaultSecretRequest request, VaultService vault, AuditService audit, HttpContext context, + CancellationToken cancellationToken) + => GuardAsync(async () => + { + var entity = await vault.FindAsync(id, cancellationToken); + if (entity is null) + { + return Results.NotFound(); + } + + var change = await vault.UpdateAsync(entity, request, Actor(context), cancellationToken); + var detail = Describe(change.Secret) + + (change.ValueChanged ? ", value changed" : string.Empty) + + (change.MovedFrom is { } before ? $", moved from {before}" : string.Empty); + await LogAsync(audit, context, "vault-secret-updated", detail, cancellationToken); + return Results.Ok(change.Secret); + }); + + private static Task DeleteAsync( + Guid id, VaultService vault, AuditService audit, HttpContext context, CancellationToken cancellationToken) + => GuardAsync(async () => + { + var entity = await vault.FindAsync(id, cancellationToken); + if (entity is null) + { + return Results.NotFound(); + } + + var detail = Describe(VaultService.ToDto(entity)); + await vault.DeleteAsync(entity, cancellationToken); + await LogAsync(audit, context, "vault-secret-deleted", detail, cancellationToken); + return Results.NoContent(); + }); + + private static Task RevealAsync( + Guid id, VaultService vault, AuditService audit, HttpContext context, CancellationToken cancellationToken) + => GuardAsync(async () => + { + var entity = await vault.FindAsync(id, cancellationToken); + if (entity is null) + { + return Results.NotFound(); + } + + var revealed = vault.Reveal(entity); + // Audited before the value leaves: a reveal that cannot be recorded does not happen. + await LogAsync(audit, context, "vault-secret-revealed", Describe(VaultService.ToDto(entity)), cancellationToken); + context.Response.Headers.CacheControl = "no-store"; + return Results.Ok(revealed); + }); + + private static string? Actor(HttpContext context) => context.User.Identity?.Name; + + private static string Describe(VaultSecretDto secret) => $"{VaultPaths.Join(secret.Path, secret.Name)} v{secret.Version}"; + + private static Task LogAsync(AuditService audit, HttpContext context, string action, string detail, CancellationToken cancellationToken) + => audit.LogAsync(AuditCategories.Secrets, action, Actor(context), detail: detail, + remoteIp: Proxy.ClientIp.EffectiveText(context), cancellationToken: cancellationToken); + + private static async Task GuardAsync(Func> action) + { + try + { + return await action(); + } + catch (VaultException ex) + { + return Results.Json(new { error = ex.Message }, statusCode: ex.StatusCode); + } + } +} diff --git a/src/Backend/Secrets/VaultService.cs b/src/Backend/Secrets/VaultService.cs new file mode 100644 index 0000000..92b7d56 --- /dev/null +++ b/src/Backend/Secrets/VaultService.cs @@ -0,0 +1,491 @@ +using System.Security.Cryptography; +using Backend.Data; +using Microsoft.AspNetCore.DataProtection; +using Microsoft.EntityFrameworkCore; +using Shared.Contracts.Api; + +namespace Backend.Secrets; + +/// A vault request the caller can fix. Message is value-free. +public sealed class VaultException(string message, int statusCode = StatusCodes.Status400BadRequest) + : Exception(message) +{ + public int StatusCode { get; } = statusCode; +} + +/// Folder and name rules for the built-in vault. +public static class VaultPaths +{ + public const int MaxPathLength = 512; + public const int MaxSegmentLength = 100; + public const int MaxNameLength = 200; + public const int MaxValueLength = 64 * 1024; + + /// "/" or "/a/b": slashes collapsed, segments trimmed, no "." or "..". + public static string NormalizePath(string? path) + { + var segments = (path ?? string.Empty) + .Split('/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + foreach (var segment in segments) + { + if (segment is "." or "..") + { + throw new VaultException("A folder cannot be named “.” or “..”."); + } + + if (segment.Length > MaxSegmentLength) + { + throw new VaultException($"Folder names are at most {MaxSegmentLength} characters."); + } + + if (segment.Any(char.IsControl)) + { + throw new VaultException("Folder names cannot hold control characters."); + } + } + + var normalized = "/" + string.Join('/', segments); + return normalized.Length <= MaxPathLength + ? normalized + : throw new VaultException($"Folder paths are at most {MaxPathLength} characters."); + } + + public static string NormalizeName(string? name) + { + var trimmed = (name ?? string.Empty).Trim(); + if (trimmed.Length == 0) + { + throw new VaultException("A secret name is required."); + } + + if (trimmed.Length > MaxNameLength) + { + throw new VaultException($"Secret names are at most {MaxNameLength} characters."); + } + + return trimmed.Contains('/') || trimmed.Any(char.IsControl) + ? throw new VaultException("Secret names cannot hold “/” or control characters.") + : trimmed; + } + + /// Case-insensitive key; upper-casing keeps the length, so prefixes line up with the original. + public static string Key(string value) => value.ToUpperInvariant(); + + public static string Join(string folder, string child) => folder == "/" ? "/" + child : $"{folder}/{child}"; + + /// Whether is or inside it. + public static bool IsWithin(string pathKey, string folderKey) + => folderKey == "/" || pathKey == folderKey || pathKey.StartsWith(folderKey + "/", StringComparison.Ordinal); +} + +/// What an update changed, for the audit trail (never the value). +public sealed record VaultSecretChange(VaultSecretDto Secret, bool ValueChanged, string? MovedFrom); + +/// +/// The built-in vault: secrets in the application database, values encrypted +/// with the server's Data Protection key ring (shared by every replica), so +/// any node reads what another wrote. Folders exist explicitly (created +/// empty) or implicitly (a secret's path); both list the same way. +/// +public sealed class VaultService(AppDbContext db, IDataProtectionProvider protectionProvider) +{ + private readonly IDataProtector protector = protectionProvider.CreateProtector("Backend.Vault.v1"); + + public async Task ListAsync(string? path, CancellationToken cancellationToken) + { + var folder = await CanonicalAsync(VaultPaths.NormalizePath(path), cancellationToken); + var key = VaultPaths.Key(folder); + var secrets = await db.VaultSecrets.AsNoTracking() + .Where(s => s.PathKey == key) + .ToListAsync(cancellationToken); + return new VaultListingDto( + folder, + await ChildFoldersAsync(folder, cancellationToken), + secrets.OrderBy(s => s.Name, StringComparer.OrdinalIgnoreCase).Select(ToDto).ToList()); + } + + public async Task> NamesAsync(string? path, CancellationToken cancellationToken) + { + var key = VaultPaths.Key(VaultPaths.NormalizePath(path)); + var names = await db.VaultSecrets.AsNoTracking() + .Where(s => s.PathKey == key) + .Select(s => s.Name) + .ToListAsync(cancellationToken); + return names.Order(StringComparer.OrdinalIgnoreCase).ToList(); + } + + public async Task> FolderPathsAsync(string? path, CancellationToken cancellationToken) + { + var folder = await CanonicalAsync(VaultPaths.NormalizePath(path), cancellationToken); + return (await ChildFoldersAsync(folder, cancellationToken)).Select(f => f.Path).ToList(); + } + + /// One value, for a script binding. Callers must never log the result. + public async Task GetValueAsync(string? path, string name, CancellationToken cancellationToken) + { + var folder = VaultPaths.NormalizePath(path); + var secretName = VaultPaths.NormalizeName(name); + var (pathKey, nameKey) = (VaultPaths.Key(folder), VaultPaths.Key(secretName)); + var entity = await db.VaultSecrets.AsNoTracking() + .SingleOrDefaultAsync(s => s.PathKey == pathKey && s.NameKey == nameKey, cancellationToken) + ?? throw new VaultException($"No secret “{secretName}” in “{folder}”.", StatusCodes.Status404NotFound); + return Unprotect(entity); + } + + public Task FindAsync(Guid id, CancellationToken cancellationToken) + => db.VaultSecrets.SingleOrDefaultAsync(s => s.Id == id, cancellationToken); + + public Task CountAsync(CancellationToken cancellationToken) => db.VaultSecrets.CountAsync(cancellationToken); + + public async Task CreateAsync(SaveVaultSecretRequest request, string? actor, CancellationToken cancellationToken) + { + var folder = await CanonicalAsync(VaultPaths.NormalizePath(request.Path), cancellationToken); + var name = VaultPaths.NormalizeName(request.Name); + var value = RequireValue(request.Value); + await EnsureFreeAsync(folder, name, null, cancellationToken); + + var now = DateTimeOffset.UtcNow; + var entity = new VaultSecretEntity + { + Id = Guid.NewGuid(), + Path = folder, + PathKey = VaultPaths.Key(folder), + Name = name, + NameKey = VaultPaths.Key(name), + ValueProtected = protector.Protect(value), + Version = 1, + CreatedUtc = now, + CreatedBy = actor, + UpdatedUtc = now, + UpdatedBy = actor, + }; + db.VaultSecrets.Add(entity); + await SaveAsync(cancellationToken); + return ToDto(entity); + } + + /// Renames, moves and/or replaces the value. Every save counts as a new version. + public async Task UpdateAsync( + VaultSecretEntity entity, SaveVaultSecretRequest request, string? actor, CancellationToken cancellationToken) + { + if (request.Version is { } expected && expected != entity.Version) + { + throw Changed(); + } + + var folder = await CanonicalAsync(VaultPaths.NormalizePath(request.Path), cancellationToken); + var name = VaultPaths.NormalizeName(request.Name); + var value = request.Value is null ? null : RequireValue(request.Value); + await EnsureFreeAsync(folder, name, entity.Id, cancellationToken); + + var before = VaultPaths.Join(entity.Path, entity.Name); + entity.Path = folder; + entity.PathKey = VaultPaths.Key(folder); + entity.Name = name; + entity.NameKey = VaultPaths.Key(name); + if (value is not null) + { + entity.ValueProtected = protector.Protect(value); + } + + entity.Version++; + entity.UpdatedUtc = DateTimeOffset.UtcNow; + entity.UpdatedBy = actor; + await SaveAsync(cancellationToken); + + var after = VaultPaths.Join(folder, name); + return new VaultSecretChange(ToDto(entity), value is not null, before == after ? null : before); + } + + public async Task DeleteAsync(VaultSecretEntity entity, CancellationToken cancellationToken) + { + db.VaultSecrets.Remove(entity); + await SaveAsync(cancellationToken); + } + + /// The value, for a person allowed to see it. The caller audits the reveal. + public RevealedVaultSecretDto Reveal(VaultSecretEntity entity) => new(Unprotect(entity), entity.Version); + + public async Task CreateFolderAsync(string path, string? actor, CancellationToken cancellationToken) + { + var folder = await CanonicalAsync(VaultPaths.NormalizePath(path), cancellationToken); + if (folder == "/") + { + throw new VaultException("A folder name is required."); + } + + if (await FolderExistsAsync(VaultPaths.Key(folder), cancellationToken)) + { + throw new VaultException($"“{folder}” already exists.", StatusCodes.Status409Conflict); + } + + db.VaultFolders.Add(new VaultFolderEntity + { + Id = Guid.NewGuid(), + Path = folder, + PathKey = VaultPaths.Key(folder), + CreatedUtc = DateTimeOffset.UtcNow, + CreatedBy = actor, + }); + await SaveAsync(cancellationToken); + return folder; + } + + /// + /// Renames or moves a folder with everything in it. Refused when a secret + /// of the same name already lives at the destination. Returns the folder's + /// new path and how many secrets moved. + /// + public async Task<(string From, string To, int Secrets)> MoveFolderAsync( + string from, string to, string? actor, CancellationToken cancellationToken) + { + var source = await CanonicalAsync(VaultPaths.NormalizePath(from), cancellationToken); + var target = VaultPaths.NormalizePath(to); + if (source == "/" || target == "/") + { + throw new VaultException("The top of the vault cannot be moved or replaced."); + } + + // Adopt the casing of the destination's existing parents, but keep the + // last segment as typed: a case-only rename is a rename. + var cut = target.LastIndexOf('/'); + var parent = await CanonicalAsync(cut == 0 ? "/" : target[..cut], cancellationToken); + target = VaultPaths.Join(parent, target[(cut + 1)..]); + + var (sourceKey, targetKey) = (VaultPaths.Key(source), VaultPaths.Key(target)); + if (sourceKey != targetKey && VaultPaths.IsWithin(targetKey, sourceKey)) + { + throw new VaultException("A folder cannot move inside itself."); + } + + if (!await FolderExistsAsync(sourceKey, cancellationToken)) + { + throw new VaultException($"“{source}” does not exist.", StatusCodes.Status404NotFound); + } + + var secrets = await Within(db.VaultSecrets, sourceKey).ToListAsync(cancellationToken); + var folders = await WithinFolders(sourceKey).ToListAsync(cancellationToken); + string Rebase(string path) => target + path[source.Length..]; + + if (sourceKey != targetKey) + { + var taken = (await Within(db.VaultSecrets.AsNoTracking(), targetKey) + .Select(s => new { s.PathKey, s.NameKey, s.Path, s.Name }) + .ToListAsync(cancellationToken)) + .ToDictionary(s => (s.PathKey, s.NameKey), s => VaultPaths.Join(s.Path, s.Name)); + var clash = secrets.FirstOrDefault(s => taken.ContainsKey((VaultPaths.Key(Rebase(s.Path)), s.NameKey))); + if (clash is not null) + { + throw new VaultException( + $"“{taken[(VaultPaths.Key(Rebase(clash.Path)), clash.NameKey)]}” already exists.", + StatusCodes.Status409Conflict); + } + } + + var now = DateTimeOffset.UtcNow; + foreach (var secret in secrets) + { + secret.Path = Rebase(secret.Path); + secret.PathKey = VaultPaths.Key(secret.Path); + secret.Version++; + secret.UpdatedUtc = now; + secret.UpdatedBy = actor; + } + + // Folder rows merge into any already at the destination. + var existing = sourceKey == targetKey + ? [] + : (await WithinFolders(targetKey).Select(f => f.PathKey).ToListAsync(cancellationToken)).ToHashSet(); + foreach (var folder in folders) + { + var path = Rebase(folder.Path); + if (existing.Contains(VaultPaths.Key(path))) + { + db.VaultFolders.Remove(folder); + continue; + } + + folder.Path = path; + folder.PathKey = VaultPaths.Key(path); + } + + await SaveAsync(cancellationToken); + return (source, target, secrets.Count); + } + + /// Deletes a folder and every secret in it. Returns the folder's path and how many secrets went. + public async Task<(string Path, int Secrets)> DeleteFolderAsync(string path, CancellationToken cancellationToken) + { + var folder = await CanonicalAsync(VaultPaths.NormalizePath(path), cancellationToken); + if (folder == "/") + { + throw new VaultException("The top of the vault cannot be deleted."); + } + + var key = VaultPaths.Key(folder); + var secrets = await Within(db.VaultSecrets, key).ToListAsync(cancellationToken); + var folders = await WithinFolders(key).ToListAsync(cancellationToken); + if (secrets.Count == 0 && folders.Count == 0) + { + throw new VaultException($"“{folder}” does not exist.", StatusCodes.Status404NotFound); + } + + db.VaultSecrets.RemoveRange(secrets); + db.VaultFolders.RemoveRange(folders); + await SaveAsync(cancellationToken); + return (folder, secrets.Count); + } + + /// Round-trips a value through the key ring, for the provider test. + public bool KeyRingWorks() + { + var probe = Guid.NewGuid().ToString("N"); + return protector.Unprotect(protector.Protect(probe)) == probe; + } + + public static VaultSecretDto ToDto(VaultSecretEntity entity) => new( + entity.Id, entity.Path, entity.Name, entity.Version, + entity.CreatedUtc, entity.CreatedBy, entity.UpdatedUtc, entity.UpdatedBy); + + private string Unprotect(VaultSecretEntity entity) + { + try + { + return protector.Unprotect(entity.ValueProtected); + } + catch (CryptographicException) + { + throw new VaultException( + $"“{VaultPaths.Join(entity.Path, entity.Name)}” could not be decrypted: this server's key ring does not hold its key.", + StatusCodes.Status500InternalServerError); + } + } + + private static string RequireValue(string? value) + { + if (value is null) + { + throw new VaultException("A value is required."); + } + + return value.Length <= VaultPaths.MaxValueLength + ? value + : throw new VaultException($"Values are at most {VaultPaths.MaxValueLength / 1024} KiB."); + } + + private async Task EnsureFreeAsync(string folder, string name, Guid? self, CancellationToken cancellationToken) + { + var (pathKey, nameKey) = (VaultPaths.Key(folder), VaultPaths.Key(name)); + if (await db.VaultSecrets.AnyAsync( + s => s.PathKey == pathKey && s.NameKey == nameKey && s.Id != self, cancellationToken)) + { + throw new VaultException($"“{VaultPaths.Join(folder, name)}” already exists.", StatusCodes.Status409Conflict); + } + } + + /// Saves, turning a lost race with another replica into a value-free conflict. + private async Task SaveAsync(CancellationToken cancellationToken) + { + try + { + await db.SaveChangesAsync(cancellationToken); + } + catch (DbUpdateConcurrencyException) + { + throw Changed(); + } + catch (DbUpdateException) + { + // The unique keys: another node saved the same folder or name first. + throw new VaultException("Something with that name was saved at the same time. Reload and retry.", + StatusCodes.Status409Conflict); + } + } + + private static VaultException Changed() + => new("Changed by someone else since it was opened. Reload and retry.", StatusCodes.Status409Conflict); + + /// A folder exists while a folder row or a secret is at or below it. + private async Task FolderExistsAsync(string key, CancellationToken cancellationToken) + => key == "/" + || await WithinFolders(key).AnyAsync(cancellationToken) + || await Within(db.VaultSecrets, key).AnyAsync(cancellationToken); + + private static IQueryable Within(IQueryable secrets, string key) + { + var below = key + "/"; + return secrets.Where(s => s.PathKey == key || s.PathKey.StartsWith(below)); + } + + private IQueryable WithinFolders(string key) + { + var below = key + "/"; + return db.VaultFolders.Where(f => f.PathKey == key || f.PathKey.StartsWith(below)); + } + + /// The folders directly inside , explicit and implied. + private async Task> ChildFoldersAsync(string folder, CancellationToken cancellationToken) + { + var key = VaultPaths.Key(folder); + var below = key == "/" ? "/" : key + "/"; + var explicitPaths = await db.VaultFolders.AsNoTracking() + .Where(f => f.PathKey.StartsWith(below)) + .Select(f => f.Path) + .ToListAsync(cancellationToken); + var secretPaths = await db.VaultSecrets.AsNoTracking() + .Where(s => s.PathKey.StartsWith(below)) + .Select(s => s.Path) + .Distinct() + .ToListAsync(cancellationToken); + + var children = new Dictionary(StringComparer.Ordinal); + foreach (var path in explicitPaths.Concat(secretPaths)) + { + if (path.Length <= below.Length || !VaultPaths.Key(path).StartsWith(below, StringComparison.Ordinal)) + { + continue; + } + + var segment = path[below.Length..].Split('/')[0]; + children.TryAdd(VaultPaths.Key(segment), new VaultFolderDto(VaultPaths.Join(folder, segment), segment)); + } + + return children.Values.OrderBy(f => f.Name, StringComparer.OrdinalIgnoreCase).ToList(); + } + + /// + /// The path with the casing of the folders it already passes through, so + /// "/prod/db" lands in an existing "/Prod" instead of beside it. + /// + private async Task CanonicalAsync(string path, CancellationToken cancellationToken) + { + if (path == "/") + { + return path; + } + + var segments = path.Split('/', StringSplitOptions.RemoveEmptyEntries); + var top = "/" + VaultPaths.Key(segments[0]); + var known = (await WithinFolders(top).AsNoTracking().Select(f => f.Path).ToListAsync(cancellationToken)) + .Concat(await Within(db.VaultSecrets.AsNoTracking(), top).Select(s => s.Path).Distinct().ToListAsync(cancellationToken)); + + string[] best = []; + foreach (var candidate in known) + { + var parts = candidate.Split('/', StringSplitOptions.RemoveEmptyEntries); + var shared = 0; + while (shared < parts.Length && shared < segments.Length + && VaultPaths.Key(parts[shared]) == VaultPaths.Key(segments[shared])) + { + shared++; + } + + if (shared > best.Length) + { + best = parts[..shared]; + } + } + + return "/" + string.Join('/', best.Concat(segments.Skip(best.Length))); + } +} diff --git a/src/Backend/Security/AuditService.cs b/src/Backend/Security/AuditService.cs index 7b831fa..9eb0ffa 100644 --- a/src/Backend/Security/AuditService.cs +++ b/src/Backend/Security/AuditService.cs @@ -41,6 +41,9 @@ public static class AuditCategories public const string Settings = "settings"; public const string Automation = "automation"; + /// The built-in vault: secrets created, changed, moved, deleted and revealed (never values). + public const string Secrets = "secrets"; + /// A device's own trail: enrolled, renamed, OS changed (see DeviceHistory). public const string Device = "device"; } diff --git a/src/Shared.Contracts/Api/ApiRoutes.cs b/src/Shared.Contracts/Api/ApiRoutes.cs index fd3ef99..c9dfe8d 100644 --- a/src/Shared.Contracts/Api/ApiRoutes.cs +++ b/src/Shared.Contracts/Api/ApiRoutes.cs @@ -74,6 +74,13 @@ public static class ApiRoutes public const string SecretsProviderNames = "/api/secrets/providers/{id}/secret-names"; public const string SecretsProviderFolders = "/api/secrets/providers/{id}/folders"; + public const string Vault = "/api/secrets/vault"; + public const string VaultFolders = "/api/secrets/vault/folders"; + public const string VaultFolderMove = "/api/secrets/vault/folders/move"; + public const string VaultSecrets = "/api/secrets/vault/secrets"; + public const string VaultSecret = "/api/secrets/vault/secrets/{id}"; + public const string VaultSecretReveal = "/api/secrets/vault/secrets/{id}/reveal"; + public const string ServerInfo = "/api/settings/server"; public const string S3Settings = "/api/settings/s3"; public const string S3SettingsTest = "/api/settings/s3/test"; @@ -173,6 +180,8 @@ public static class ApiRoutes public static string TriggerRun(Guid id) => $"/api/triggers/{id:D}/run"; public static string SecretsProvider(Guid id) => $"/api/secrets/providers/{id:D}"; public static string SecretsProviderNames(Guid id) => $"/api/secrets/providers/{id:D}/secret-names"; + public static string VaultSecret(Guid id) => $"/api/secrets/vault/secrets/{id:D}"; + public static string VaultSecretReveal(Guid id) => $"/api/secrets/vault/secrets/{id:D}/reveal"; public static string Job(Guid jobId) => $"/api/jobs/{jobId:D}"; public static string JobOutput(Guid jobId) => $"/api/jobs/{jobId:D}/output"; public static string JobOutputDownload(Guid jobId) => $"/api/jobs/{jobId:D}/output/download"; diff --git a/src/Shared.Contracts/Api/ResourceDtos.cs b/src/Shared.Contracts/Api/ResourceDtos.cs index 72d9428..156c34e 100644 --- a/src/Shared.Contracts/Api/ResourceDtos.cs +++ b/src/Shared.Contracts/Api/ResourceDtos.cs @@ -285,7 +285,8 @@ public sealed record SecretsProviderDto( IReadOnlyDictionary Config, IReadOnlyList SecretFieldsSet, DateTimeOffset UpdatedUtc, - string? UpdatedBy); + string? UpdatedBy, + bool BuiltIn = false); /// /// POST/PUT /api/secrets/providers body. Empty/null secret config fields keep @@ -305,6 +306,49 @@ public sealed record TestSecretsProviderRequest( /// Result of a secrets provider connectivity test. public sealed record SecretsTestResultDto(bool Ok, IReadOnlyList Steps); +/// +/// One secret in the built-in vault: name, folder and history, never the +/// value (that takes a separate, audited reveal). +/// +public sealed record VaultSecretDto( + Guid Id, + string Path, + string Name, + int Version, + DateTimeOffset CreatedUtc, + string? CreatedBy, + DateTimeOffset UpdatedUtc, + string? UpdatedBy); + +/// A child folder of the browsed path. +public sealed record VaultFolderDto(string Path, string Name); + +/// GET /api/secrets/vault: one level of the built-in vault. +public sealed record VaultListingDto( + string Path, + IReadOnlyList Folders, + IReadOnlyList Secrets); + +/// +/// POST/PUT /api/secrets/vault/secrets body. On update a null value keeps the +/// stored one (so a path or name change alone never needs the value), and +/// Version, when given, must match the stored version. +/// +public sealed record SaveVaultSecretRequest( + string Path, + string Name, + string? Value = null, + int? Version = null); + +/// POST /api/secrets/vault/folders body. +public sealed record VaultFolderRequest(string Path); + +/// POST /api/secrets/vault/folders/move body: renames or moves a folder with everything in it. +public sealed record MoveVaultFolderRequest(string From, string To); + +/// POST /api/secrets/vault/secrets/{id}/reveal response. +public sealed record RevealedVaultSecretDto(string Value, int Version); + /// Reusable PowerShell function module (prepended to scripts that reference it). public sealed record FunctionModuleDto( Guid Id, diff --git a/src/Shared.Contracts/Models/SecretBinding.cs b/src/Shared.Contracts/Models/SecretBinding.cs index ff796a9..06a86b0 100644 --- a/src/Shared.Contracts/Models/SecretBinding.cs +++ b/src/Shared.Contracts/Models/SecretBinding.cs @@ -14,3 +14,16 @@ public sealed record SecretBinding( Guid ProviderId, string SecretName, string? Path = null); + +/// +/// The built-in vault: a provider every server has, with no configuration and +/// no stored provider row. Bindings reference it by this fixed id. +/// +public static class BuiltInVault +{ + public const string Kind = "builtin"; + + public const string DisplayName = "Built-in Vault"; + + public static readonly Guid ProviderId = new("5ec7e700-0000-4000-8000-000000000001"); +} diff --git a/tests/Backend.Tests/VaultTests.cs b/tests/Backend.Tests/VaultTests.cs new file mode 100644 index 0000000..e18bd26 --- /dev/null +++ b/tests/Backend.Tests/VaultTests.cs @@ -0,0 +1,277 @@ +using System.Net; +using System.Net.Http.Json; +using Backend.Data; +using Backend.Jobs; +using Backend.Secrets; +using Backend.Security; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Shared.Contracts.Api; +using Shared.Contracts.Models; +using Shared.Contracts.Serialization; +using Xunit; + +namespace Backend.Tests; + +/// The built-in vault: storage, the console API, permissions, audit, and script bindings through it. +public class VaultTests(BackendTestFixture fixture) : IClassFixture +{ + // Fake values only: what matters is that they never show up where they must not. + private const string Value = "fake-vault-value-1"; + private const string OtherValue = "fake-vault-value-2"; + + private static async Task CreateAsync(HttpClient client, string path, string name, string value = Value) + { + var response = await client.PostAsJsonAsync(ApiRoutes.VaultSecrets, new SaveVaultSecretRequest(path, name, value), JsonDefaults.Options); + Assert.Equal(HttpStatusCode.Created, response.StatusCode); + return (await response.Content.ReadFromJsonAsync(JsonDefaults.Options))!; + } + + private static async Task ListAsync(HttpClient client, string path) + => (await client.GetFromJsonAsync($"{ApiRoutes.Vault}?path={Uri.EscapeDataString(path)}", JsonDefaults.Options))!; + + private static Task RevealAsync(HttpClient client, Guid id) + => client.PostAsync(ApiRoutes.For.VaultSecretReveal(id), null); + + private Task> AuditAsync(string action) + => fixture.WithDbAsync(db => db.AuditEvents.AsNoTracking() + .Where(e => e.Category == AuditCategories.Secrets && e.Action == action).ToListAsync()); + + /// A signed-in client whose only role grants exactly . + private async Task SignedInWithAsync(string username, params string[] permissions) + { + var roleId = await fixture.WithDbAsync(async db => + { + var role = new RoleEntity + { + Id = Guid.NewGuid(), Name = $"vault-{Guid.NewGuid():N}", + PermissionsJson = JsonDefaults.Serialize(permissions), + CreatedUtc = DateTimeOffset.UtcNow, UpdatedUtc = DateTimeOffset.UtcNow, + }; + db.Roles.Add(role); + await db.SaveChangesAsync(); + return role.Id; + }); + return await fixture.SignedInAsync(username, roleId: roleId); + } + + [Fact] + public async Task SecretsAreCreatedListedChangedAndDeleted() + { + using var admin = await fixture.SignedInAsync("vault-crud"); + var created = await CreateAsync(admin, "/crud//apps/ ", "API_KEY"); + Assert.Equal("/crud/apps", created.Path); + Assert.Equal(1, created.Version); + Assert.Equal("vault-crud", created.CreatedBy); + + var top = await ListAsync(admin, "/crud"); + Assert.Contains(top.Folders, f => f.Path == "/crud/apps" && f.Name == "apps"); + var listing = await ListAsync(admin, "/crud/apps"); + Assert.Equal(["API_KEY"], listing.Secrets.Select(s => s.Name)); + // Listings never carry the value. + Assert.DoesNotContain(Value, await admin.GetStringAsync($"{ApiRoutes.Vault}?path=/crud/apps")); + + // Rename and move without the value keeps it. + var moved = await admin.PutAsJsonAsync(ApiRoutes.For.VaultSecret(created.Id), + new SaveVaultSecretRequest("/crud/other", "API_TOKEN", Version: 1), JsonDefaults.Options); + moved.EnsureSuccessStatusCode(); + var movedDto = (await moved.Content.ReadFromJsonAsync(JsonDefaults.Options))!; + Assert.Equal(("/crud/other", "API_TOKEN", 2), (movedDto.Path, movedDto.Name, movedDto.Version)); + var kept = await (await RevealAsync(admin, created.Id)).Content.ReadFromJsonAsync(JsonDefaults.Options); + Assert.Equal(Value, kept!.Value); + + // A new value is a new version. + (await admin.PutAsJsonAsync(ApiRoutes.For.VaultSecret(created.Id), + new SaveVaultSecretRequest("/crud/other", "API_TOKEN", OtherValue), JsonDefaults.Options)).EnsureSuccessStatusCode(); + var changed = await (await RevealAsync(admin, created.Id)).Content.ReadFromJsonAsync(JsonDefaults.Options); + Assert.Equal((OtherValue, 3), (changed!.Value, changed.Version)); + + // A save against a version someone else already replaced is refused. + var stale = await admin.PutAsJsonAsync(ApiRoutes.For.VaultSecret(created.Id), + new SaveVaultSecretRequest("/crud/other", "API_TOKEN", Value, Version: 1), JsonDefaults.Options); + Assert.Equal(HttpStatusCode.Conflict, stale.StatusCode); + + Assert.Equal(HttpStatusCode.NoContent, (await admin.DeleteAsync(ApiRoutes.For.VaultSecret(created.Id))).StatusCode); + Assert.Empty((await ListAsync(admin, "/crud/other")).Secrets); + Assert.Equal(HttpStatusCode.NotFound, (await admin.DeleteAsync(ApiRoutes.For.VaultSecret(created.Id))).StatusCode); + + // Every change is audited, by path and name, never with the value. + foreach (var action in new[] { "vault-secret-created", "vault-secret-updated", "vault-secret-deleted", "vault-secret-revealed" }) + { + var events = await AuditAsync(action); + Assert.Contains(events, e => e.Actor == "vault-crud" && e.Detail!.Contains("API_")); + Assert.All(events, e => Assert.DoesNotContain("fake-vault-value", e.Detail)); + } + } + + [Fact] + public async Task NamesAreUniqueRegardlessOfCaseAndFoldersKeepTheirCasing() + { + using var admin = await fixture.SignedInAsync("vault-case"); + await CreateAsync(admin, "/Case", "Token"); + + var duplicate = await admin.PostAsJsonAsync(ApiRoutes.VaultSecrets, new SaveVaultSecretRequest("/case", "TOKEN", Value), JsonDefaults.Options); + Assert.Equal(HttpStatusCode.Conflict, duplicate.StatusCode); + + // A lower-cased path lands in the existing folder instead of beside it. + var sibling = await CreateAsync(admin, "/case", "Other"); + Assert.Equal("/Case", sibling.Path); + Assert.Single((await ListAsync(admin, "/")).Folders, f => f.Name.Equals("case", StringComparison.OrdinalIgnoreCase)); + + var bad = await admin.PostAsJsonAsync(ApiRoutes.VaultSecrets, new SaveVaultSecretRequest("/a/../b", "X", Value), JsonDefaults.Options); + Assert.Equal(HttpStatusCode.BadRequest, bad.StatusCode); + } + + [Fact] + public async Task ValuesAreEncryptedAtRestAndNamesAreNot() + { + using var admin = await fixture.SignedInAsync("vault-rest"); + var created = await CreateAsync(admin, "/rest", "DB_PASSWORD"); + + var stored = await fixture.WithDbAsync(db => db.VaultSecrets.AsNoTracking().SingleAsync(s => s.Id == created.Id)); + Assert.Equal(("/rest", "DB_PASSWORD"), (stored.Path, stored.Name)); + Assert.DoesNotContain(Value, stored.ValueProtected); + Assert.NotEqual(Value, stored.ValueProtected); + Assert.True(stored.ValueProtected.Length > Value.Length); + } + + [Fact] + public async Task RevealTakesItsOwnPermission() + { + using var admin = await fixture.SignedInAsync("vault-reveal-admin"); + var created = await CreateAsync(admin, "/reveal", "SIGNING_KEY"); + + using var manager = await SignedInWithAsync("vault-manager", "secrets.use", "secrets.manage"); + Assert.Single((await ListAsync(manager, "/reveal")).Secrets); + Assert.Equal(HttpStatusCode.Forbidden, (await RevealAsync(manager, created.Id)).StatusCode); + + using var user = await SignedInWithAsync("vault-user", "secrets.use"); + var write = await user.PostAsJsonAsync(ApiRoutes.VaultSecrets, new SaveVaultSecretRequest("/reveal", "NEW", Value), JsonDefaults.Options); + Assert.Equal(HttpStatusCode.Forbidden, write.StatusCode); + Assert.Equal(HttpStatusCode.Forbidden, (await user.DeleteAsync(ApiRoutes.For.VaultSecret(created.Id))).StatusCode); + + using var revealer = await SignedInWithAsync("vault-revealer", "secrets.reveal"); + var revealed = await RevealAsync(revealer, created.Id); + revealed.EnsureSuccessStatusCode(); + Assert.Equal("no-store", revealed.Headers.CacheControl?.ToString()); + Assert.Equal(Value, (await revealed.Content.ReadFromJsonAsync(JsonDefaults.Options))!.Value); + + var reveals = await AuditAsync("vault-secret-revealed"); + Assert.Contains(reveals, e => e.Actor == "vault-revealer" && e.Detail == "/reveal/SIGNING_KEY v1"); + // Refused reveals are not recorded as reveals. + Assert.DoesNotContain(reveals, e => e.Actor == "vault-manager"); + } + + [Fact] + public async Task FoldersAreCreatedMovedAndDeletedWithWhatIsInThem() + { + using var admin = await fixture.SignedInAsync("vault-folders"); + (await admin.PostAsJsonAsync(ApiRoutes.VaultFolders, new VaultFolderRequest("/fold/empty"), JsonDefaults.Options)).EnsureSuccessStatusCode(); + Assert.Contains((await ListAsync(admin, "/fold")).Folders, f => f.Name == "empty"); + var again = await admin.PostAsJsonAsync(ApiRoutes.VaultFolders, new VaultFolderRequest("/FOLD/EMPTY"), JsonDefaults.Options); + Assert.Equal(HttpStatusCode.Conflict, again.StatusCode); + + var secret = await CreateAsync(admin, "/fold/src/deep", "A"); + await CreateAsync(admin, "/fold/dst/deep", "B"); + await CreateAsync(admin, "/fold/clash/deep", "A"); + + // Moving onto a folder holding the same name is refused, and moves nothing. + var refused = await admin.PostAsJsonAsync(ApiRoutes.VaultFolderMove, new MoveVaultFolderRequest("/fold/src", "/fold/clash"), JsonDefaults.Options); + Assert.Equal(HttpStatusCode.Conflict, refused.StatusCode); + var inside = await admin.PostAsJsonAsync(ApiRoutes.VaultFolderMove, new MoveVaultFolderRequest("/fold/src", "/fold/src/deep/x"), JsonDefaults.Options); + Assert.Equal(HttpStatusCode.BadRequest, inside.StatusCode); + Assert.Single((await ListAsync(admin, "/fold/src/deep")).Secrets); + + (await admin.PostAsJsonAsync(ApiRoutes.VaultFolderMove, new MoveVaultFolderRequest("/fold/src", "/fold/dst"), JsonDefaults.Options)).EnsureSuccessStatusCode(); + Assert.Equal(["A", "B"], (await ListAsync(admin, "/fold/dst/deep")).Secrets.Select(s => s.Name)); + Assert.DoesNotContain((await ListAsync(admin, "/fold")).Folders, f => f.Name == "src"); + var revealed = await (await RevealAsync(admin, secret.Id)).Content.ReadFromJsonAsync(JsonDefaults.Options); + Assert.Equal(Value, revealed!.Value); + + // A case-only rename is a rename. + (await admin.PostAsJsonAsync(ApiRoutes.VaultFolderMove, new MoveVaultFolderRequest("/fold/dst", "/fold/Dst"), JsonDefaults.Options)).EnsureSuccessStatusCode(); + Assert.Contains((await ListAsync(admin, "/fold")).Folders, f => f.Name == "Dst"); + + Assert.Equal(HttpStatusCode.NoContent, (await admin.DeleteAsync($"{ApiRoutes.VaultFolders}?path=/fold/Dst")).StatusCode); + Assert.Empty((await ListAsync(admin, "/fold/Dst/deep")).Secrets); + Assert.Contains(await AuditAsync("vault-folder-deleted"), e => e.Detail == "/fold/Dst (2 secret(s))"); + Assert.Equal(HttpStatusCode.BadRequest, (await admin.DeleteAsync($"{ApiRoutes.VaultFolders}?path=/")).StatusCode); + } + + [Fact] + public async Task TheBuiltInVaultIsAlwaysAProviderAndCannotBeChanged() + { + using var admin = await fixture.SignedInAsync("vault-provider"); + await CreateAsync(admin, "/picker/sub", "PICKED"); + var builtIn = BuiltInVault.ProviderId; + + var list = await admin.GetFromJsonAsync(ApiRoutes.SecretsProviders, JsonDefaults.Options); + var first = list!.Providers[0]; + Assert.Equal((builtIn, BuiltInVault.Kind, true), (first.Id, first.Kind, first.BuiltIn)); + + // The picker's listings go through the provider routes, like any external store. + var folders = await admin.GetFromJsonAsync>($"/api/secrets/providers/{builtIn:D}/folders?path=/picker", JsonDefaults.Options); + Assert.Equal(["/picker/sub"], folders); + var names = await admin.GetFromJsonAsync>($"{ApiRoutes.For.SecretsProviderNames(builtIn)}?path=/picker/sub", JsonDefaults.Options); + Assert.Equal(["PICKED"], names); + + var test = await admin.PostAsJsonAsync(ApiRoutes.SecretsProviderTest, + new TestSecretsProviderRequest(BuiltInVault.Kind, [], builtIn), JsonDefaults.Options); + Assert.True((await test.Content.ReadFromJsonAsync(JsonDefaults.Options))!.Ok); + + var save = new SaveSecretsProviderRequest("Another", BuiltInVault.Kind, []); + Assert.Equal(HttpStatusCode.BadRequest, (await admin.PostAsJsonAsync(ApiRoutes.SecretsProviders, save, JsonDefaults.Options)).StatusCode); + Assert.Equal(HttpStatusCode.BadRequest, (await admin.PutAsJsonAsync(ApiRoutes.For.SecretsProvider(builtIn), save, JsonDefaults.Options)).StatusCode); + Assert.Equal(HttpStatusCode.BadRequest, (await admin.DeleteAsync(ApiRoutes.For.SecretsProvider(builtIn))).StatusCode); + } + + [Fact] + public async Task ScriptBindingsResolveThroughTheBuiltInVault() + { + using var admin = await fixture.SignedInAsync("vault-binding"); + await CreateAsync(admin, "/apps/billing", "API_KEY"); + await CreateAsync(admin, "/", "ROOT_TOKEN", OtherValue); + + var bindings = new List + { + new("ApiKey", BuiltInVault.ProviderId, "api_key", "/Apps/Billing"), + new("Root", BuiltInVault.ProviderId, "ROOT_TOKEN"), + }; + + using (var scope = fixture.Services.CreateScope()) + { + var resolved = await scope.ServiceProvider.GetRequiredService() + .ResolveAsync(bindings, CancellationToken.None); + Assert.Equal([("ApiKey", Value), ("Root", OtherValue)], resolved.Select(s => (s.Name, s.Value))); + + var missing = await Assert.ThrowsAsync(() => + scope.ServiceProvider.GetRequiredService().ResolveAsync( + [new SecretBinding("Gone", BuiltInVault.ProviderId, "NOPE", "/apps")], CancellationToken.None)); + Assert.Contains("Gone", missing.Message); + Assert.Contains(BuiltInVault.DisplayName, missing.Message); + } + + // End to end: a run delivers the values with the command, resolved at dispatch. + fixture.Scripts.Accept = true; + var script = await admin.PostAsJsonAsync(ApiRoutes.Scripts, + new SaveScriptRequest("vault-bound", null, "Write-Output $Secrets.Count", SecretBindings: bindings), JsonDefaults.Options); + script.EnsureSuccessStatusCode(); + var scriptId = (await script.Content.ReadFromJsonAsync(JsonDefaults.Options))!.Id; + var device = await fixture.SeedDeviceAsync("VAULT-BOUND"); + + (await admin.PostAsJsonAsync(ApiRoutes.Jobs, new CreateJobRequest(null, scriptId, ClientIds: [device]), JsonDefaults.Options)) + .EnsureSuccessStatusCode(); + + lock (fixture.Scripts.Sent) + { + var sent = Assert.Single(fixture.Scripts.Sent, s => s.ClientId == device); + Assert.Equal([("ApiKey", Value), ("Root", OtherValue)], sent.Command.Secrets!.Select(s => (s.Name, s.Value))); + } + + // The job record never holds the value. + var stored = await fixture.WithDbAsync(db => db.Jobs.AsNoTracking().Where(j => j.ClientId == device).ToListAsync()); + Assert.DoesNotContain(stored, j => JsonDefaults.Serialize(j).Contains("fake-vault-value")); + } + + private sealed record ProvidersResponse(List Providers); +} -- 2.52.0 From 47238a5613cbfb82f0eb8443133e02c2f5bec9c6077cbf47b8c9633046ea9e3d Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Wed, 4 Nov 2026 20:23:07 -0500 Subject: [PATCH 09/22] feat(secrets): manage the built-in vault from the console A Vault page (Automation) browses the built-in vault folder by folder: new folders and secrets, edit, rename and move (a secret through its folder and name, a folder with everything in it), and delete. Values stay masked; the eye button reveals one through its own audited call and hides it again after 30 seconds or when the folder is left, with a copy button while shown. Writes are gated on Manage Secrets and reveal on Reveal Secrets, each explaining itself on hover when missing. Settings, Secrets Providers lists the built-in vault as a row that opens the vault instead of an editor, keeps it out of the kinds a provider can be added as, and gates its actions on Manage Secrets, as the API already did. Co-Authored-By: Claude Opus 5.5 --- src/WebUI/src/api/client.ts | 16 +- src/WebUI/src/api/types.ts | 42 ++ src/WebUI/src/menu-items/index.ts | 2 + src/WebUI/src/pages/vault/index.tsx | 17 + src/WebUI/src/routes/index.tsx | 2 + .../src/sections/secrets/VaultBrowser.tsx | 570 ++++++++++++++++++ .../sections/settings/SecretsProviders.tsx | 56 +- 7 files changed, 684 insertions(+), 21 deletions(-) create mode 100644 src/WebUI/src/pages/vault/index.tsx create mode 100644 src/WebUI/src/sections/secrets/VaultBrowser.tsx diff --git a/src/WebUI/src/api/client.ts b/src/WebUI/src/api/client.ts index 6cee26b..665ee79 100644 --- a/src/WebUI/src/api/client.ts +++ b/src/WebUI/src/api/client.ts @@ -46,7 +46,11 @@ import type { RemoveDevicesBody, ServerInfoDto, StartAgentUpgradeBody, - TriggerRunResultDto + TriggerRunResultDto, + RevealedVaultSecretDto, + SaveVaultSecretBody, + VaultFolderDto, + VaultSecretDto } from './types'; export interface AgentUpgradeQuery { @@ -344,6 +348,16 @@ export const api = { secretNames: (providerId: string, path?: string) => request(`/api/secrets/providers/${providerId}/secret-names${qs({ path })}`), secretFolders: (providerId: string, path?: string) => request(`/api/secrets/providers/${providerId}/folders${qs({ path })}`), + // Built-in vault + createVaultFolder: (path: string) => request('/api/secrets/vault/folders', json('POST', { path })), + moveVaultFolder: (from: string, to: string) => request('/api/secrets/vault/folders/move', json('POST', { from, to })), + deleteVaultFolder: (path: string) => request(`/api/secrets/vault/folders${qs({ path })}`, { method: 'DELETE' }), + createVaultSecret: (body: SaveVaultSecretBody) => request('/api/secrets/vault/secrets', json('POST', body)), + updateVaultSecret: (id: string, body: SaveVaultSecretBody) => + request(`/api/secrets/vault/secrets/${id}`, json('PUT', body)), + deleteVaultSecret: (id: string) => request(`/api/secrets/vault/secrets/${id}`, { method: 'DELETE' }), + revealVaultSecret: (id: string) => request(`/api/secrets/vault/secrets/${id}/reveal`, { method: 'POST' }), + // Server info (public base URL for generated commands) serverInfo: () => request('/api/settings/server'), diff --git a/src/WebUI/src/api/types.ts b/src/WebUI/src/api/types.ts index 1e990ee..35d414b 100644 --- a/src/WebUI/src/api/types.ts +++ b/src/WebUI/src/api/types.ts @@ -580,12 +580,54 @@ export interface SecretsProviderDto { secretFieldsSet: string[]; updatedUtc: string; updatedBy?: string; + /** The built-in vault: always listed first, nothing to configure, cannot be edited or deleted. */ + builtIn?: boolean; } export interface SecretsProviderKindDto { kind: string; displayName: string; fields: { name: string; label: string; secret: boolean; placeholder?: string }[]; + /** Not offered when adding a provider: the built-in vault is always there. */ + builtIn?: boolean; +} + +// ── Built-in vault ──────────────────────────────────────────────────── + +/** A vault secret's name, folder and history; the value takes a separate, audited reveal. */ +export interface VaultSecretDto { + id: string; + path: string; + name: string; + version: number; + createdUtc: string; + createdBy?: string; + updatedUtc: string; + updatedBy?: string; +} + +export interface VaultFolderDto { + path: string; + name: string; +} + +export interface VaultListingDto { + path: string; + folders: VaultFolderDto[]; + secrets: VaultSecretDto[]; +} + +/** On update, an omitted value keeps the stored one; `version` refuses a save over someone else's. */ +export interface SaveVaultSecretBody { + path: string; + name: string; + value?: string; + version?: number; +} + +export interface RevealedVaultSecretDto { + value: string; + version: number; } export interface SecretsProvidersResponse { diff --git a/src/WebUI/src/menu-items/index.ts b/src/WebUI/src/menu-items/index.ts index 6a71738..9b7cfb4 100644 --- a/src/WebUI/src/menu-items/index.ts +++ b/src/WebUI/src/menu-items/index.ts @@ -7,6 +7,7 @@ import DashboardOutlined from '@ant-design/icons/DashboardOutlined'; import DesktopOutlined from '@ant-design/icons/DesktopOutlined'; import FunctionOutlined from '@ant-design/icons/FunctionOutlined'; import HistoryOutlined from '@ant-design/icons/HistoryOutlined'; +import LockOutlined from '@ant-design/icons/LockOutlined'; import SafetyCertificateOutlined from '@ant-design/icons/SafetyCertificateOutlined'; import SettingOutlined from '@ant-design/icons/SettingOutlined'; @@ -42,6 +43,7 @@ const menuItems: NavGroupType[] = [ children: [ { id: 'scripts', title: 'Scripts', url: '/scripts', icon: CodeOutlined, permission: 'scripts.view', permissionLabel: 'View Scripts' }, { id: 's3', title: 'S3 Files', url: '/s3', icon: CloudOutlined, permission: 'scripts.manage', permissionLabel: 'Manage Scripts' }, + { id: 'vault', title: 'Vault', url: '/vault', icon: LockOutlined, permission: 'secrets.use', permissionLabel: 'Use Secrets' }, { id: 'functions', title: 'Functions', diff --git a/src/WebUI/src/pages/vault/index.tsx b/src/WebUI/src/pages/vault/index.tsx new file mode 100644 index 0000000..edbb493 --- /dev/null +++ b/src/WebUI/src/pages/vault/index.tsx @@ -0,0 +1,17 @@ +// project imports +import MainCard from 'components/MainCard'; +import PageHeader from 'components/PageHeader'; +import VaultBrowser from 'sections/secrets/VaultBrowser'; + +// ==============================|| VAULT ||============================== // + +export default function VaultPage() { + return ( + <> + + + + + + ); +} diff --git a/src/WebUI/src/routes/index.tsx b/src/WebUI/src/routes/index.tsx index ef36cd0..4a41b4a 100644 --- a/src/WebUI/src/routes/index.tsx +++ b/src/WebUI/src/routes/index.tsx @@ -16,6 +16,7 @@ const Groups = Loadable(lazy(() => import('pages/groups'))); const Scripts = Loadable(lazy(() => import('pages/scripts'))); const ScriptEditor = Loadable(lazy(() => import('pages/scripts/editor'))); const S3Files = Loadable(lazy(() => import('pages/s3'))); +const Vault = Loadable(lazy(() => import('pages/vault'))); const Functions = Loadable(lazy(() => import('pages/functions'))); const Schedules = Loadable(lazy(() => import('pages/schedules'))); const Jobs = Loadable(lazy(() => import('pages/jobs'))); @@ -60,6 +61,7 @@ const router = createBrowserRouter([ { path: 'scripts', element: }, { path: 'scripts/:id', element: }, { path: 's3', element: }, + { path: 'vault', element: }, { path: 'functions', element: }, { path: 'schedules', element: }, { path: 'jobs', element: }, diff --git a/src/WebUI/src/sections/secrets/VaultBrowser.tsx b/src/WebUI/src/sections/secrets/VaultBrowser.tsx new file mode 100644 index 0000000..bd34199 --- /dev/null +++ b/src/WebUI/src/sections/secrets/VaultBrowser.tsx @@ -0,0 +1,570 @@ +import { MouseEvent, useEffect, useRef, useState } from 'react'; +import { useSearchParams } from 'react-router-dom'; +import { enqueueSnackbar } from 'notistack'; + +// material-ui +import Alert from '@mui/material/Alert'; +import Box from '@mui/material/Box'; +import Breadcrumbs from '@mui/material/Breadcrumbs'; +import Button from '@mui/material/Button'; +import Grid from '@mui/material/Grid'; +import InputAdornment from '@mui/material/InputAdornment'; +import Link from '@mui/material/Link'; +import Stack from '@mui/material/Stack'; +import TextField from '@mui/material/TextField'; +import Tooltip from '@mui/material/Tooltip'; +import Typography from '@mui/material/Typography'; + +// project imports +import ActionMenu, { MenuEntry } from 'components/ActionMenu'; +import ConfirmDialog from 'components/ConfirmDialog'; +import CopyButton from 'components/CopyButton'; +import DataTable, { Column } from 'components/DataTable'; +import EmptyState from 'components/EmptyState'; +import GatedButton from 'components/GatedButton'; +import IconButton from 'components/@extended/IconButton'; +import Sheet from 'components/Sheet'; +import { RelativeTime } from 'components/DateTime'; +import { api, errorMessage } from 'api/client'; +import useApi from 'hooks/useApi'; +import { useGate } from 'hooks/useSession'; +import Field from 'sections/settings/Field'; + +// assets +import DeleteOutlined from '@ant-design/icons/DeleteOutlined'; +import EditOutlined from '@ant-design/icons/EditOutlined'; +import EyeInvisibleOutlined from '@ant-design/icons/EyeInvisibleOutlined'; +import EyeOutlined from '@ant-design/icons/EyeOutlined'; +import FolderAddOutlined from '@ant-design/icons/FolderAddOutlined'; +import FolderOpenOutlined from '@ant-design/icons/FolderOpenOutlined'; +import FolderOutlined from '@ant-design/icons/FolderOutlined'; +import KeyOutlined from '@ant-design/icons/KeyOutlined'; +import MoreOutlined from '@ant-design/icons/MoreOutlined'; +import PlusOutlined from '@ant-design/icons/PlusOutlined'; +import SaveOutlined from '@ant-design/icons/SaveOutlined'; + +// types +import type { VaultFolderDto, VaultListingDto, VaultSecretDto } from 'api/types'; + +/** A revealed value goes back to dots after this long. */ +const REVEAL_MS = 30_000; + +const mono = { fontFamily: 'var(--font-mono)' }; + +type Row = { kind: 'folder'; folder: VaultFolderDto } | { kind: 'secret'; secret: VaultSecretDto }; + +/** Null while closed; `secret` is set when editing a saved one. */ +type Editor = { secret?: VaultSecretDto } | null; + +type FolderDialog = { mode: 'new' } | { mode: 'move'; folder: VaultFolderDto } | null; + +type Removal = { kind: 'folder'; folder: VaultFolderDto } | { kind: 'secret'; secret: VaultSecretDto } | null; + +const vaultKey = (path: string) => `/api/secrets/vault?${new URLSearchParams({ path })}`; + +const join = (folder: string, child: string) => (folder === '/' ? `/${child}` : `${folder}/${child}`); + +// ==============================|| BUILT-IN VAULT ||============================== // + +/** Browse the built-in vault folder by folder, change what is in it, and reveal values one at a time. */ +export default function VaultBrowser() { + const [params, setParams] = useSearchParams(); + const path = params.get('path') || '/'; + const { data, error, loading, validating, mutate } = useApi(vaultKey(path)); + const gate = useGate('secrets.manage', 'Manage Secrets'); + const revealGate = useGate('secrets.reveal', 'Reveal Secrets'); + + // Revealed values belong to the folder they were revealed in and go when it is left. + const [reveals, setReveals] = useState<{ path: string; values: Record }>({ path, values: {} }); + const revealed = reveals.path === path ? reveals.values : {}; + const [revealing, setRevealing] = useState(''); + const timers = useRef(new Map()); + const [notice, setNotice] = useState(''); + + const [editor, setEditor] = useState(null); + const [formFolder, setFormFolder] = useState('/'); + const [formName, setFormName] = useState(''); + const [formValue, setFormValue] = useState(''); + const [showValue, setShowValue] = useState(false); + const [formError, setFormError] = useState(''); + const [busy, setBusy] = useState(false); + + const [folderDialog, setFolderDialog] = useState(null); + const [folderInput, setFolderInput] = useState(''); + const [removal, setRemoval] = useState(null); + const [menu, setMenu] = useState<{ anchor: HTMLElement; row: Row } | null>(null); + + const editing = editor?.secret; + + const hide = (id: string) => { + window.clearTimeout(timers.current.get(id)); + timers.current.delete(id); + setReveals((current) => { + const values = { ...current.values }; + delete values[id]; + return { path: current.path, values }; + }); + }; + + useEffect(() => { + const pending = timers.current; + return () => pending.forEach((timer) => window.clearTimeout(timer)); + }, []); + + const open = (next: string) => + setParams( + (prev) => { + const updated = new URLSearchParams(prev); + if (next === '/') updated.delete('path'); + else updated.set('path', next); + return updated; + }, + { replace: false } + ); + + const reveal = async (secret: VaultSecretDto) => { + setRevealing(secret.id); + setNotice(''); + try { + const { value } = await api.revealVaultSecret(secret.id); + setReveals((current) => ({ path, values: { ...(current.path === path ? current.values : {}), [secret.id]: value } })); + window.clearTimeout(timers.current.get(secret.id)); + timers.current.set( + secret.id, + window.setTimeout(() => hide(secret.id), REVEAL_MS) + ); + } catch (e) { + setNotice(errorMessage(e, 'The value could not be revealed.')); + } finally { + setRevealing(''); + } + }; + + const openCreate = () => { + setEditor({}); + setFormFolder(path); + setFormName(''); + setFormValue(''); + setShowValue(false); + setFormError(''); + }; + + const openEdit = (secret: VaultSecretDto) => { + setEditor({ secret }); + setFormFolder(secret.path); + setFormName(secret.name); + setFormValue(''); + setShowValue(false); + setFormError(''); + }; + + const close = () => { + if (busy) return; + setEditor(null); + }; + + const save = async () => { + setBusy(true); + setFormError(''); + try { + const body = { + path: formFolder, + name: formName, + value: editing && formValue === '' ? undefined : formValue, + version: editing?.version + }; + const saved = editing ? await api.updateVaultSecret(editing.id, body) : await api.createVaultSecret(body); + if (editing) hide(editing.id); + await mutate(); + setEditor(null); + enqueueSnackbar(saved.path === path ? (editing ? 'Secret saved' : 'Secret added') : `Saved to ${saved.path}`, { variant: 'success' }); + } catch (e) { + setFormError(errorMessage(e, 'Save failed.')); + } finally { + setBusy(false); + } + }; + + const saveFolder = async () => { + if (!folderDialog) return; + if (folderDialog.mode === 'new') { + await api.createVaultFolder(join(path, folderInput.trim())); + enqueueSnackbar('Folder added', { variant: 'success' }); + } else { + const moved = await api.moveVaultFolder(folderDialog.folder.path, folderInput.trim()); + enqueueSnackbar(`Moved to ${moved.path}`, { variant: 'success' }); + } + await mutate(); + }; + + const remove = async () => { + if (!removal) return; + if (removal.kind === 'folder') await api.deleteVaultFolder(removal.folder.path); + else { + await api.deleteVaultSecret(removal.secret.id); + hide(removal.secret.id); + if (editing?.id === removal.secret.id) setEditor(null); + } + await mutate(); + enqueueSnackbar(removal.kind === 'folder' ? 'Folder deleted' : 'Secret deleted', { variant: 'success' }); + }; + + const menuEntries = (row: Row): MenuEntry[] => + row.kind === 'folder' + ? [ + { kind: 'item', id: 'open', label: 'Open', icon: , onSelect: () => open(row.folder.path) }, + { + kind: 'item', + id: 'move', + label: 'Rename or Move', + icon: , + disabledReason: gate, + onSelect: () => { + setFolderInput(row.folder.path); + setFolderDialog({ mode: 'move', folder: row.folder }); + } + }, + { kind: 'divider' }, + { + kind: 'item', + id: 'delete', + label: 'Delete', + icon: , + destructive: true, + disabledReason: gate, + onSelect: () => setRemoval({ kind: 'folder', folder: row.folder }) + } + ] + : [ + { kind: 'item', id: 'edit', label: 'Edit, Rename or Move', icon: , onSelect: () => openEdit(row.secret) }, + { kind: 'divider' }, + { + kind: 'item', + id: 'delete', + label: 'Delete', + icon: , + destructive: true, + disabledReason: gate, + onSelect: () => setRemoval({ kind: 'secret', secret: row.secret }) + } + ]; + + const rows: Row[] | undefined = data && [ + ...data.folders.map((folder): Row => ({ kind: 'folder', folder })), + ...data.secrets.map((secret): Row => ({ kind: 'secret', secret })) + ]; + + const columns: Column[] = [ + { + id: 'name', + header: 'Name', + cell: (row) => ( + + + {row.kind === 'folder' ? : } + + + {row.kind === 'folder' ? row.folder.name : row.secret.name} + + + ) + }, + { + id: 'value', + header: 'Value', + help: 'Masked until revealed. Each reveal is fetched on its own, audited, and hidden again after 30 seconds.', + cell: (row) => { + if (row.kind === 'folder') return null; + const id = row.secret.id; + const value = revealed[id]; + return ( + e.stopPropagation()}> + + {value ?? '••••••••'} + + {value === undefined ? ( + + + reveal(row.secret)} + > + + + + + ) : ( + <> + + hide(id)}> + + + + + + )} + + ); + } + }, + { + id: 'version', + header: 'Version', + help: 'Counts saved changes.', + width: 90, + cell: (row) => (row.kind === 'secret' ? `v${row.secret.version}` : null) + }, + { + id: 'updated', + header: 'Updated', + nowrap: true, + cell: (row) => + row.kind === 'secret' ? ( + + + {row.secret.updatedBy && ( + + by {row.secret.updatedBy} + + )} + + ) : null + }, + { + id: 'actions', + header: '', + align: 'right', + width: 60, + cell: (row) => ( + ) => { + e.stopPropagation(); + setMenu({ anchor: e.currentTarget, row }); + }} + > + + + ) + } + ]; + + const steps = path + .split('/') + .filter(Boolean) + .map((label, i, all) => ({ label, path: `/${all.slice(0, i + 1).join('/')}` })); + + const addButton = ( + } onClick={openCreate}> + New Secret + + ); + + const toolbar = ( + + + {steps.length === 0 ? ( + Vault + ) : ( + open('/')}> + Vault + + )} + {steps.map((s, i) => + i === steps.length - 1 ? ( + + {s.label} + + ) : ( + open(s.path)}> + {s.label} + + ) + )} + + } + onClick={() => { + setFolderInput(''); + setFolderDialog({ mode: 'new' }); + }} + > + New Folder + + {addButton} + + ); + + return ( + <> + {notice && ( + setNotice('')}> + {notice} + + )} + (row.kind === 'folder' ? `folder:${row.folder.path}` : row.secret.id)} + loading={loading} + refreshing={validating} + error={error ? errorMessage(error, 'The vault could not be read.') : undefined} + onRowClick={(row) => (row.kind === 'folder' ? open(row.folder.path) : openEdit(row.secret))} + activeRowId={editing?.id} + toolbar={toolbar} + paged={false} + empty={ + } + title={path === '/' ? 'No Secrets Yet' : 'Nothing Here'} + action={path === '/' ? addButton : undefined} + /> + } + /> + + + + } + onClick={save} + disabled={busy || !formName.trim() || (!editing && formValue === '')} + > + {busy ? 'Saving…' : 'Save Changes'} + + + } + > + + + + setFormFolder(e.target.value)} + slotProps={{ htmlInput: { style: mono } }} + /> + + + + + setFormName(e.target.value)} + slotProps={{ htmlInput: { style: mono } }} + /> + + + + + setFormValue(e.target.value)} + sx={{ '& textarea': { ...mono, WebkitTextSecurity: showValue ? 'none' : 'disc' } }} + slotProps={{ + htmlInput: { spellCheck: false }, + input: { + endAdornment: ( + + + setShowValue(!showValue)} + > + {showValue ? : } + + + + ) + } + }} + /> + + + + {formError && ( + + {formError} + + )} + + + setMenu(null)} /> + + setFolderDialog(null)} + > + setFolderInput(e.target.value)} + slotProps={{ htmlInput: { style: mono } }} + /> + + + + {removal?.kind === 'folder' ? removal.folder.path : removal ? join(removal.secret.path, removal.secret.name) : ''} + + } + confirmLabel="Delete" + destructive + onConfirm={remove} + onClose={() => setRemoval(null)} + /> + + ); +} diff --git a/src/WebUI/src/sections/settings/SecretsProviders.tsx b/src/WebUI/src/sections/settings/SecretsProviders.tsx index 42e06e9..d8c122c 100644 --- a/src/WebUI/src/sections/settings/SecretsProviders.tsx +++ b/src/WebUI/src/sections/settings/SecretsProviders.tsx @@ -1,4 +1,5 @@ import { MouseEvent, useState } from 'react'; +import { useNavigate } from 'react-router-dom'; import { enqueueSnackbar } from 'notistack'; // material-ui @@ -29,11 +30,12 @@ import useApi from 'hooks/useApi'; import { useGate } from 'hooks/useSession'; import Field from './Field'; import TestSteps from './TestSteps'; -import { SETTINGS_KEYS, SETTINGS_PERMISSION, SETTINGS_PERMISSION_LABEL } from './keys'; +import { SETTINGS_KEYS } from './keys'; // assets import DeleteOutlined from '@ant-design/icons/DeleteOutlined'; import EditOutlined from '@ant-design/icons/EditOutlined'; +import FolderOpenOutlined from '@ant-design/icons/FolderOpenOutlined'; import MoreOutlined from '@ant-design/icons/MoreOutlined'; import PlusOutlined from '@ant-design/icons/PlusOutlined'; import SafetyCertificateOutlined from '@ant-design/icons/SafetyCertificateOutlined'; @@ -52,9 +54,12 @@ type Editor = { provider?: SecretsProviderDto } | null; export default function SecretsProviders() { const { data, error, loading, validating, mutate } = useApi(SETTINGS_KEYS.secretsProviders); - const gate = useGate(SETTINGS_PERMISSION, SETTINGS_PERMISSION_LABEL); + const gate = useGate('secrets.manage', 'Manage Secrets'); + const navigate = useNavigate(); const providers = data?.providers; - const kinds = data?.kinds ?? []; + const allKinds = data?.kinds ?? []; + // The built-in vault is always listed and is never added. + const kinds = allKinds.filter((k) => !k.builtIn); const [editor, setEditor] = useState(null); const [formName, setFormName] = useState(''); @@ -74,7 +79,8 @@ export default function SecretsProviders() { const editing = editor?.provider; const kindDef = kinds.find((k) => k.kind === formKind); - const kindName = (kind: string) => kinds.find((k) => k.kind === kind)?.displayName ?? kind; + const kindName = (kind: string) => allKinds.find((k) => k.kind === kind)?.displayName ?? kind; + const openRow = (provider: SecretsProviderDto) => (provider.builtIn ? navigate('/vault') : openEdit(provider)); const openCreate = () => { setEditor({}); @@ -153,7 +159,12 @@ export default function SecretsProviders() { enqueueSnackbar('Provider deleted', { variant: 'success' }); }; - const menuEntries = (provider: SecretsProviderDto): MenuEntry[] => [ + const menuEntries = (provider: SecretsProviderDto): MenuEntry[] => + provider.builtIn + ? [{ kind: 'item', id: 'open', label: 'Open Vault', icon: , onSelect: () => navigate('/vault') }] + : providerEntries(provider); + + const providerEntries = (provider: SecretsProviderDto): MenuEntry[] => [ { kind: 'item', id: 'edit', label: 'Edit', icon: , onSelect: () => openEdit(provider) }, { kind: 'divider' }, { @@ -178,9 +189,11 @@ export default function SecretsProviders() { { id: 'credentials', header: 'Credentials', - help: 'Whether the secret connection fields hold a stored value.', + help: 'Whether the secret connection fields hold a stored value. The built-in vault needs none.', cell: (p) => - p.secretFieldsSet.length > 0 ? ( + p.builtIn ? ( + + ) : p.secretFieldsSet.length > 0 ? ( ) : ( @@ -190,16 +203,19 @@ export default function SecretsProviders() { id: 'updated', header: 'Updated', nowrap: true, - cell: (p) => ( - - - {p.updatedBy && ( - - by {p.updatedBy} - - )} - - ), + cell: (p) => + p.builtIn ? ( + — + ) : ( + + + {p.updatedBy && ( + + by {p.updatedBy} + + )} + + ), sortValue: (p) => p.updatedUtc }, { @@ -259,10 +275,10 @@ export default function SecretsProviders() { title={ <> Secrets Providers - + } - subheader="External vaults for scripts." + subheader="Vaults for scripts." secondary={addButton} content={false} > @@ -273,7 +289,7 @@ export default function SecretsProviders() { loading={loading} refreshing={validating} error={error ? errorMessage(error, 'Could not load the secrets providers.') : undefined} - onRowClick={openEdit} + onRowClick={openRow} activeRowId={editing?.id} defaultSort={{ id: 'name', direction: 'asc' }} paged={false} -- 2.52.0 From 86dd6fa40527590cfb025077ce5ab01c85abc21078ab456b087663bc47a7806f Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Wed, 4 Nov 2026 20:23:07 -0500 Subject: [PATCH 10/22] docs(secrets): the built-in vault Co-Authored-By: Claude Opus 5.5 --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 387bf96..6918de2 100644 --- a/README.md +++ b/README.md @@ -1210,6 +1210,19 @@ the standard module path and resolve in subsequent jobs. ## Secrets providers and $Secrets +Every server has a **built-in vault**, with nothing to set up: it is always the +first provider, so scripts can bind secrets out of the box. Manage it under +**Vault** (nav): folders and secrets, create, edit, rename, move and delete. +Secrets live in the application database, folder and name in clear for +listing, the **value encrypted with the server's Data Protection key ring** +(shared by replicas through `DATAPROTECTION_PATH`, so every node reads what +another wrote). Values are masked; the eye button fetches one with a separate +call that needs the **Reveal secrets** (`secrets.reveal`) permission, kept +apart from **Manage secrets** (`secrets.manage`, which edits the vault). Every +create, change, move, delete and reveal is audited under `secrets`, never with +the value. Each save bumps the secret's version, and a save over a version +someone else already replaced is refused. + Configure external vaults under **Settings → Secrets providers** (Infisical first; the driver interface is pluggable). Connection details are stored as a **Data Protection-encrypted blob**, secret config fields never return to the -- 2.52.0 From 39441eced918c78faae3695c7b761b84a4ec8a163ac1a5f20bad2ec7f6ddda37 Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Wed, 4 Nov 2026 20:42:38 -0500 Subject: [PATCH 11/22] feat(auth): record a generated first-start admin password and add reset-admin An empty INITIAL_ADMIN_PASSWORD now means generate and record: the one-time password goes to /initial-admin-password.txt (Administrators, SYSTEM and the server's own account only; mode 600 elsewhere) and the log only says where. The file is removed once the password is changed or reset. reset-admin (alias auth-reset) reads the standalone appsettings.json, so it reaches the same database with the service running or stopped, takes --user/--password, turns the account back on and is audited. The compose scripts stop generating the password into .env. Co-Authored-By: Claude Opus 5.5 --- .env.example | 31 +++- scripts/New-LocalSecrets.ps1 | 29 ++-- scripts/Package-Server.ps1 | 5 +- scripts/deploy.sh | 22 +-- secrets/README.md | 2 +- src/Backend/Auth/AuthResetCli.cs | 72 ++++++---- src/Backend/Auth/SignIn/BreakGlass.cs | 56 ++++++-- .../Auth/SignIn/InitialAdminPasswordFile.cs | 98 +++++++++++++ src/Backend/Auth/SignIn/SignInService.cs | 5 + tests/Backend.Tests/InitialAdminTests.cs | 133 ++++++++++++++++++ 10 files changed, 392 insertions(+), 61 deletions(-) create mode 100644 src/Backend/Auth/SignIn/InitialAdminPasswordFile.cs create mode 100644 tests/Backend.Tests/InitialAdminTests.cs diff --git a/.env.example b/.env.example index 5cbc929..8da1d6d 100644 --- a/.env.example +++ b/.env.example @@ -3,7 +3,8 @@ #============================================================================= # # CREDENTIALS: -# Initial admin: INITIAL_ADMIN_USERNAME / INITIAL_ADMIN_PASSWORD (below) +# Initial admin: generated at first start into /data/initial-admin-password.txt +# (INITIAL_ADMIN_PASSWORD below sets one instead) # NATS seeds: generated into secrets/nats by scripts/New-LocalSecrets.ps1 # # ACCESS URL: set PUBLIC_SERVER_URL to the public https URL served by your @@ -11,7 +12,7 @@ # # PRODUCTION SETUP: # 1. cp .env.example .env (or run scripts/New-LocalSecrets.ps1) -# 2. Fill INITIAL_ADMIN_PASSWORD and NATS_ACCOUNT_SIGNING_SEED +# 2. Fill NATS_ACCOUNT_SIGNING_SEED (and INITIAL_ADMIN_PASSWORD only to choose it) # 3. docker compose -f IaC/compose/docker-compose.yml up -d --build (or ./scripts/deploy.sh) # #============================================================================= @@ -95,19 +96,39 @@ FORWARD_LIMIT=1 # your organization's security standards. The chain is PEM files under # /tls (TLS_CERT_DIR overrides; keep it on a persisted volume, and # switch the proxy upstream + health checks to https accordingly). +# The root and intermediate alone (no keys) are also written to +# /ca-certificates as .cer and .pem, for Group Policy or a proxy. TLS_SELF_SIGNED=0 TLS_CERT_DIR= +# Your own certificate instead (turns HTTPS on by itself; hot-reloaded within +# a minute when the file is replaced): TLS_CERT_SOURCE=file with +# TLS_CERT_FILE a PFX/P12 (TLS_CERT_PASSWORD) or a PEM (a fullchain PEM +# brings its intermediates) with TLS_CERT_KEY_FILE, mounted read-only into the +# container. On Windows, TLS_CERT_SOURCE=store + TLS_CERT_THUMBPRINT serves a +# LocalMachine\My certificate. When its chain is not publicly trusted (an +# enterprise CA) agents are handed that chain, pinned to the token, as with the +# self-signed one; TLS_DISTRIBUTE_CA=1/0 overrides that decision. +TLS_CERT_SOURCE= +TLS_CERT_FILE= +TLS_CERT_KEY_FILE= +TLS_CERT_PASSWORD= +TLS_DISTRIBUTE_CA= + # ── Execution history ────────────────────────────────────────────────────── # Newest jobs kept per device (older jobs and their stored output are # pruned). 0 disables pruning. JOB_HISTORY_LIMIT=200 # ── Break-glass account (first startup only) ─────────────────────────────── -# The one recovery built-in account. Left empty, a one-time password is -# generated and written once to the server log. Recover with `auth-reset`. +# The one recovery built-in account. Left empty (the default), a one-time +# password is generated at the first start and written to +# /data/initial-admin-password.txt (mode 600, never the log); it must be +# changed at the first sign-in, and the file goes once it is. Read it with +# docker exec EXECUTER-API-00001 cat /data/initial-admin-password.txt +# Locked out or lost: docker exec EXECUTER-API-00001 dotnet Backend.dll reset-admin +# Set a password here only to choose it yourself (existing deployments keep theirs). INITIAL_ADMIN_USERNAME=admin -# Generate: openssl rand -base64 24 INITIAL_ADMIN_PASSWORD= # ── Sign-in ──────────────────────────────────────────────────────────────── diff --git a/scripts/New-LocalSecrets.ps1 b/scripts/New-LocalSecrets.ps1 index 7b92880..9c01fed 100644 --- a/scripts/New-LocalSecrets.ps1 +++ b/scripts/New-LocalSecrets.ps1 @@ -1,7 +1,9 @@ <# .SYNOPSIS Generates strong local development secrets: -- .env (from .env.example) with a random initial admin password +- .env (from .env.example); INITIAL_ADMIN_PASSWORD stays empty, so the + backend generates the one-time admin password at its first start and + records it in its data volume (/data/initial-admin-password.txt) - Real Ed25519 NATS operator/account/signing NKeys and JWTs via the backend's nats-auth CLI (NATS.NKeys / NATS.Jwt — no fake seeds) - secrets/nats/resolver.conf for the NATS container @@ -36,18 +38,17 @@ $accountPublic = (Get-Content (Join-Path $natsSecretsDir 'account.pub') -Raw).Tr $operatorPublic = (Get-Content (Join-Path $natsSecretsDir 'operator.pub') -Raw).Trim() # ── .env ────────────────────────────────────────────────────────────────── -function New-RandomPassword { - param([int]$Length = 24) - $alphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789!@#%^*-_=+' - $bytes = [byte[]]::new($Length) - [System.Security.Cryptography.RandomNumberGenerator]::Fill($bytes) - -join ($bytes | ForEach-Object { $alphabet[$_ % $alphabet.Length] }) +# INITIAL_ADMIN_PASSWORD is left as .env.example has it (empty): generate and +# record at the first start. An existing .env's value is kept on -Force, so a +# deployment that chose its own keeps it. +$existingAdminPassword = if (Test-Path $envFile) { + (Get-Content $envFile | Where-Object { $_ -match '^INITIAL_ADMIN_PASSWORD=' } | Select-Object -First 1) -replace '^INITIAL_ADMIN_PASSWORD=', '' } -$adminPassword = New-RandomPassword - $content = Get-Content $envExample -$content = $content -replace '^INITIAL_ADMIN_PASSWORD=.*$', "INITIAL_ADMIN_PASSWORD=$adminPassword" +if ($existingAdminPassword) { + $content = $content -replace '^INITIAL_ADMIN_PASSWORD=.*$', "INITIAL_ADMIN_PASSWORD=$existingAdminPassword" +} $content = $content -replace '^NATS_ACCOUNT_SIGNING_SEED=.*$', "NATS_ACCOUNT_SIGNING_SEED=$accountSigningSeed" $content = $content -replace '^NATS_ACCOUNT_PUBLIC_KEY=.*$', "NATS_ACCOUNT_PUBLIC_KEY=$accountPublic" $content = $content -replace '^NATS_OPERATOR_PUBLIC_KEY=.*$', "NATS_OPERATOR_PUBLIC_KEY=$operatorPublic" @@ -57,5 +58,11 @@ Set-Content -Path $envFile -Value $content -Encoding utf8NoBOM Write-Host '' Write-Host "Wrote $envFile" -Write-Host "Initial admin: admin / $adminPassword" +if ($existingAdminPassword) { + Write-Host 'Initial admin: admin with the INITIAL_ADMIN_PASSWORD kept from the existing .env' +} +else { + Write-Host 'Initial admin: admin, one-time password recorded at the first start:' + Write-Host ' docker exec EXECUTER-API-00001 cat /data/initial-admin-password.txt' +} Write-Host 'NATS material written below secrets/nats (seeds are SECRET).' diff --git a/scripts/Package-Server.ps1 b/scripts/Package-Server.ps1 index 52d8eeb..bd4dbdf 100644 --- a/scripts/Package-Server.ps1 +++ b/scripts/Package-Server.ps1 @@ -5,8 +5,9 @@ WebUI (static SPA), the API, the /messaging WebSocket relay, and supervising a bundled nats-server sidecar. Produces a zip per platform. Quick start on the target machine: - unzip, run `ExecuteRServer setup`, set INITIAL_ADMIN_PASSWORD in - appsettings.json, then run the binary (or `ExecuteRServer service install`). + unzip, run `ExecuteR-Server setup`, then run the binary (or + `ExecuteR-Server service install`); the first start records a one-time + admin password in data/initial-admin-password.txt (administrators only). .EXAMPLE ./scripts/Package-Server.ps1 # win-x64 + linux-x64 diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 99f6d9d..b7c288e 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -109,11 +109,10 @@ cd "$STACK_DIR" if [[ ! -f .env ]]; then log "Creating .env from .env.example" cp .env.example .env - - # Generate the initial admin password so the stack is usable immediately. - ADMIN_PASSWORD="$(openssl rand -base64 24 2>/dev/null | tr -d '\n/+=' | cut -c1-24)" - [[ -n "$ADMIN_PASSWORD" ]] && sed -i "s|^INITIAL_ADMIN_PASSWORD=.*|INITIAL_ADMIN_PASSWORD=$ADMIN_PASSWORD|" .env - GENERATED_ADMIN_PASSWORD="$ADMIN_PASSWORD" + # INITIAL_ADMIN_PASSWORD stays empty: the backend generates the one-time + # admin password at its first start and records it in its data volume + # (/data/initial-admin-password.txt, mode 600), never in .env or the log. + NEW_ENV=1 fi # shellcheck disable=SC1091 @@ -257,8 +256,15 @@ echo " WebUI: http://:${WEBUI_PORT:-13080}" echo " API: http://:${BACKEND_PORT:-19080}/api/health" echo " NATS WS: ws://:${NATS_WS_PORT:-19222}" [[ $WITH_PROXY -eq 1 ]] && echo " Proxy: https://${PUBLIC_HOSTNAME}:${PROXY_HTTPS_PORT:-18443}" -if [[ -n "${GENERATED_ADMIN_PASSWORD:-}" ]]; then +if [[ -z "${INITIAL_ADMIN_PASSWORD:-}" ]]; then + # Only the command is printed: the password stays in the data volume + # (mode 600), out of this terminal's scrollback and any CI log. echo - echo " Initial admin: ${INITIAL_ADMIN_USERNAME:-admin} / ${GENERATED_ADMIN_PASSWORD}" - echo " (also stored in .env — change it after first sign-in)" + echo " Initial admin: ${INITIAL_ADMIN_USERNAME:-admin}, with the one-time password the first start recorded:" + echo " docker exec EXECUTER-API-00001 cat /data/initial-admin-password.txt" + echo " A new password is required at the first sign-in. Locked out later:" + echo " docker exec EXECUTER-API-00001 dotnet Backend.dll reset-admin" +elif [[ -n "${NEW_ENV:-}" ]]; then + echo + echo " Initial admin: ${INITIAL_ADMIN_USERNAME:-admin} with INITIAL_ADMIN_PASSWORD from .env" fi diff --git a/secrets/README.md b/secrets/README.md index 676c331..5064d2b 100644 --- a/secrets/README.md +++ b/secrets/README.md @@ -30,7 +30,7 @@ uses at runtime to issue agent and browser user JWTs. If you prefer ## Required secrets (production) -- `INITIAL_ADMIN_PASSWORD` — first-boot local admin password +- `INITIAL_ADMIN_PASSWORD` — optional first-boot admin password; left empty, the backend generates one and records it in its data folder (`initial-admin-password.txt`, administrators only) - `NATS_OPERATOR_SEED` — operator identity (kept offline after setup) - `NATS_ACCOUNT_SEED` — application account identity - `NATS_ACCOUNT_SIGNING_SEED` — given to the backend for user JWT issuance diff --git a/src/Backend/Auth/AuthResetCli.cs b/src/Backend/Auth/AuthResetCli.cs index 48fceda..684e3b3 100644 --- a/src/Backend/Auth/AuthResetCli.cs +++ b/src/Backend/Auth/AuthResetCli.cs @@ -5,27 +5,54 @@ using Microsoft.AspNetCore.DataProtection; namespace Backend.Auth; /// -/// Lockout recovery, run on the server (or inside the container): +/// Lockout recovery, run on the server (or inside the container), with the service running or stopped: /// -/// dotnet Backend.dll auth-reset Built-In sign-in on, OIDC redirect off, and a new one-time -/// password for the break-glass account (created if missing) -/// dotnet Backend.dll auth-reset --user jdoe a new one-time password for that built-in account instead -/// dotnet Backend.dll auth-reset --password 'secret' that password rather than a generated one (still changed at sign-in) +/// ExecuteR-Server reset-admin Built-In sign-in on, OIDC redirect off, and a new one-time +/// password for the break-glass account (created if missing) +/// ExecuteR-Server reset-admin --user jdoe a new one-time password for that built-in account instead +/// ExecuteR-Server reset-admin --password 'secret' that password rather than a generated one (still changed at sign-in) /// -/// So an identity provider that is down, misconfigured or has revoked the -/// client never locks anyone out for good. AUTH_FORCE_LOCAL=1 offers the -/// built-in form at startup without a database write. Exit codes: 0 done, -/// 1 failed, 2 no such account. +/// (auth-reset is the same verb; containers run dotnet Backend.dll reset-admin.) The account is turned +/// back on, its sign-in back-off cleared and its sessions ended; the reset is audited. The new password is printed +/// here only. So an identity provider that is down, misconfigured or has revoked the client, or a lost first-start +/// password, never locks anyone out for good. AUTH_FORCE_LOCAL=1 offers the built-in form at startup without a +/// database write. Exit codes: 0 done, 1 failed, 2 no such account. /// public static class AuthResetCli { + public static readonly string[] Verbs = ["reset-admin", "auth-reset"]; + + /// + /// The server's own settings: the standalone server's appsettings.json beside the binary (where setup wrote the + /// database and data paths), then the environment, as the server itself reads them. + /// + public static IConfiguration LoadConfiguration(string? baseDirectory = null) + => new ConfigurationBuilder() + .AddJsonFile(Path.Combine(baseDirectory ?? AppContext.BaseDirectory, "appsettings.json"), optional: true, reloadOnChange: false) + .AddEnvironmentVariables() + .Build(); + public static async Task RunAsync(string[] args, IConfiguration configuration) { + if (args.Contains("--help") || args.Contains("-h") || args.Contains("/?")) + { + Console.WriteLine("Usage: reset-admin [--user ] [--password ]"); + Console.WriteLine(" Gives the break-glass account (or --user) a new password that must be changed at the next sign-in,"); + Console.WriteLine(" turns it back on and restores Built-In sign-in. Without --password one is generated and printed."); + return 0; + } + var userName = ArgValue(args, "--user") ?? ArgValue(args, "--admin"); var password = ArgValue(args, "--password"); + if (password is { Length: > PasswordHashing.MaxLength }) + { + Console.Error.WriteLine($"--password may be at most {PasswordHashing.MaxLength} characters."); + return 1; + } var services = new ServiceCollection(); - services.AddLogging(b => b.AddSimpleConsole()); + // Warnings only: the output is read by a person, and the password line must not drown in query logging. + services.AddLogging(b => b.AddSimpleConsole().SetMinimumLevel(LogLevel.Warning)); services.AddSingleton(configuration); services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(DataProtectionPath(configuration))); services.AddApplicationDatabase(DatabaseOptions.FromConfiguration(configuration)); @@ -41,24 +68,20 @@ public static class AuthResetCli var row = await SettingsDocuments.LoadAsync(db, AuthenticationMethods.Key, CancellationToken.None); var before = SettingsDocuments.Parse(row?.Value, AuthenticationMethods.Defaults); SettingsDocuments.Stage(db, row, AuthenticationMethods.Key, row is null ? null : before, before with { BuiltIn = true, OidcRedirect = false }, - "auth-reset", always: true); + "reset-admin", always: true); await db.SaveChangesAsync(); Console.WriteLine("Built-In sign-in is on and OIDC redirect is off."); } - var (user, generated) = await provider.GetRequiredService().ResetAsync(userName, "auth-reset", CancellationToken.None); - if (!string.IsNullOrEmpty(password)) + var (user, generated) = await provider.GetRequiredService().ResetAsync(userName, "reset-admin", CancellationToken.None, password); + Console.WriteLine(string.IsNullOrEmpty(password) + ? $"{user.UserName}: one-time password {generated} (a new one is required at the next sign-in)." + : $"{user.UserName}: password set; a new one is required at the next sign-in."); + + // A first-start record no longer holds a working password. + if (user.IsBreakGlass && InitialAdminPasswordFile.Delete(InitialAdminPasswordFile.PathFor(configuration))) { - using var scope = provider.CreateScope(); - var db = scope.ServiceProvider.GetRequiredService(); - var tracked = await db.Users.FindAsync(user.Id); - tracked!.PasswordHash = PasswordHashing.Hash(password); - await db.SaveChangesAsync(); - Console.WriteLine($"{user.UserName}: password set; it must be changed at the next sign-in."); - } - else - { - Console.WriteLine($"{user.UserName}: one-time password {generated} (change it at the next sign-in)."); + Console.WriteLine($"Removed the first-start record {InitialAdminPasswordFile.PathFor(configuration)}."); } Console.WriteLine("No restart needed; sign in at /sign-in."); @@ -71,7 +94,8 @@ public static class AuthResetCli } catch (Exception exception) { - Console.Error.WriteLine($"auth-reset failed: {exception.Message}"); + Console.Error.WriteLine($"reset-admin failed: {exception.Message}"); + Console.Error.WriteLine("Run it as administrator (root) on the server, where the server's data folder can be written."); return 1; } } diff --git a/src/Backend/Auth/SignIn/BreakGlass.cs b/src/Backend/Auth/SignIn/BreakGlass.cs index dd6fcca..467e23b 100644 --- a/src/Backend/Auth/SignIn/BreakGlass.cs +++ b/src/Backend/Auth/SignIn/BreakGlass.cs @@ -9,9 +9,10 @@ namespace Backend.Auth; /// The one break-glass built-in account: full access through no binding, kept /// for recovery, audited on every use. Created on the first start of an empty /// database: with INITIAL_ADMIN_USERNAME/INITIAL_ADMIN_PASSWORD when the -/// deployment set them, otherwise as admin with a generated password -/// written once to the log. The auth-reset verb gives it a new one-time -/// password. +/// deployment set them, otherwise with a generated password that must be changed +/// at the first sign-in, recorded in the data folder for administrators only +/// (). The reset-admin verb (also +/// auth-reset) gives it a new one-time password. /// public sealed class BreakGlass(IServiceScopeFactory scopes) { @@ -84,10 +85,11 @@ public sealed class BreakGlass(IServiceScopeFactory scopes) /// /// Gives a built-in account (the break-glass account when - /// is null, creating it if missing) a new generated password that must be - /// changed at the next sign-in, clears its back-off and ends its sessions. + /// is null, creating it if missing) a new password that must be changed at the + /// next sign-in (, or a generated one), turns it back + /// on, clears its back-off and ends its sessions. /// - public async Task<(UserEntity User, string Password)> ResetAsync(string? userName, string actor, CancellationToken cancellationToken) + public async Task<(UserEntity User, string Password)> ResetAsync(string? userName, string actor, CancellationToken cancellationToken, string? password = null) { using var scope = scopes.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); @@ -100,7 +102,7 @@ public sealed class BreakGlass(IServiceScopeFactory scopes) } var now = DateTimeOffset.UtcNow; - var password = PasswordHashing.Generate(); + password = string.IsNullOrEmpty(password) ? PasswordHashing.Generate() : password; if (user is null) { var taken = await db.Users.AnyAsync(u => u.Kind == SignInProviders.Local && u.UserName == DefaultUserName, cancellationToken); @@ -157,11 +159,29 @@ public sealed class IdentityStartup(IServiceScopeFactory scopes, BreakGlass brea var userName = configuration["INITIAL_ADMIN_USERNAME"]; var generated = await breakGlass.EnsureAsync(userName, configuration["INITIAL_ADMIN_PASSWORD"], configuration["INITIAL_ADMIN_MUST_CHANGE"] is "1" or "true", cancellationToken); + var record = InitialAdminPasswordFile.PathFor(configuration); if (generated is not null) { - logger.LogWarning( - "First start: sign in as {User} with the one-time password {Password} and choose a new one. This is the only place it is shown; if it is lost, run the auth-reset verb on this server.", - string.IsNullOrWhiteSpace(userName) ? BreakGlass.DefaultUserName : userName.Trim(), generated); + // An empty INITIAL_ADMIN_PASSWORD means generate and record: the password goes to a file only administrators can + // open, never to the log, which is shipped, bundled and read far more widely than the data folder. + var name = string.IsNullOrWhiteSpace(userName) ? BreakGlass.DefaultUserName : userName.Trim(); + try + { + InitialAdminPasswordFile.Write(record, name, generated, DateTimeOffset.UtcNow); + logger.LogWarning( + "First start: the one-time password for {User} is in {Path} (administrators only); a new one is required at the first sign-in. Lost? Run reset-admin on this server.", + name, record); + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException or PlatformNotSupportedException) + { + logger.LogError(exception, + "First start: the one-time password for {User} could not be written to {Path}. Run reset-admin on this server to set a new one.", + name, record); + } + } + else + { + await ForgetChangedPasswordAsync(record, cancellationToken); } await UpgradeUsersAsync(userName, cancellationToken); @@ -170,6 +190,22 @@ public sealed class IdentityStartup(IServiceScopeFactory scopes, BreakGlass brea access.Invalidate(); } + /// The recorded one-time password goes once the break-glass account has chosen its own. + private async Task ForgetChangedPasswordAsync(string record, CancellationToken cancellationToken) + { + if (!File.Exists(record)) + { + return; + } + + using var scope = scopes.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + if (!await db.Users.AnyAsync(u => u.IsBreakGlass && u.MustChangePassword, cancellationToken) && InitialAdminPasswordFile.Delete(record)) + { + logger.LogInformation("Removed {Path}: the one-time password in it has been changed.", record); + } + } + private async Task UpgradeUsersAsync(string? initialAdmin, CancellationToken cancellationToken) { using var scope = scopes.CreateScope(); diff --git a/src/Backend/Auth/SignIn/InitialAdminPasswordFile.cs b/src/Backend/Auth/SignIn/InitialAdminPasswordFile.cs new file mode 100644 index 0000000..0ae2669 --- /dev/null +++ b/src/Backend/Auth/SignIn/InitialAdminPasswordFile.cs @@ -0,0 +1,98 @@ +using System.Runtime.Versioning; +using System.Security.AccessControl; +using System.Security.Principal; +using Backend.Upgrades; + +namespace Backend.Auth; + +/// +/// Where a generated first-start password is recorded: initial-admin-password.txt in the data folder. +/// On Windows only BUILTIN\Administrators, SYSTEM and the account the server runs as can open it (inheritance off); +/// elsewhere it is mode 600, owned by the server's account. The general log only says where it is, so log +/// shipping, support bundles and docker logs never carry the password. The file is removed once the +/// password has been changed, or replaced by a reset-admin. +/// +public static class InitialAdminPasswordFile +{ + public const string FileName = "initial-admin-password.txt"; + + public static string PathFor(IConfiguration configuration) => Path.Combine(AgentCatalogStorage.DataRoot(configuration), FileName); + + public static void Write(string path, string userName, string password, DateTimeOffset createdUtc) + { + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + if (File.Exists(path)) + { + File.Delete(path); + } + + var content = $""" + ExecuteR one-time administrator sign-in, created {createdUtc:u} + User: {userName} + Password: {password} + + A new password is required at the first sign-in; this file is deleted once it has been changed. + Lost or locked out? Run as administrator on the server: ExecuteR-Server reset-admin + + """; + + using var stream = OperatingSystem.IsWindows() ? CreateWindows(path) : CreateUnix(path); + using var writer = new StreamWriter(stream); + writer.Write(content.ReplaceLineEndings()); + } + + /// Removes the record; true when there was one. + public static bool Delete(string path) + { + try + { + if (!File.Exists(path)) + { + return false; + } + + File.Delete(path); + return true; + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException) + { + return false; + } + } + + [SupportedOSPlatform("windows")] + private static FileStream CreateWindows(string path) + { + var security = new FileSecurity(); + security.SetAccessRuleProtection(isProtected: true, preserveInheritance: false); + var sids = new List + { + new(WellKnownSidType.BuiltinAdministratorsSid, null), + new(WellKnownSidType.LocalSystemSid, null), + }; + + // The account the server runs as already owns the database; without it a non-elevated development run could not read its own record. + if (WindowsIdentity.GetCurrent().User is { } self && !sids.Contains(self)) + { + sids.Add(self); + } + + foreach (var sid in sids) + { + security.AddAccessRule(new FileSystemAccessRule(sid, FileSystemRights.FullControl, AccessControlType.Allow)); + } + + // Created with the protected ACL in one step, so the password is never readable through an inherited ACE. + return new FileInfo(path).Create(FileMode.CreateNew, FileSystemRights.Write | FileSystemRights.ReadData, FileShare.None, 4096, FileOptions.None, security); + } + + [UnsupportedOSPlatform("windows")] + private static FileStream CreateUnix(string path) + => new(path, new FileStreamOptions + { + Mode = FileMode.CreateNew, + Access = FileAccess.Write, + Share = FileShare.None, + UnixCreateMode = UnixFileMode.UserRead | UnixFileMode.UserWrite, + }); +} diff --git a/src/Backend/Auth/SignIn/SignInService.cs b/src/Backend/Auth/SignIn/SignInService.cs index d7031fe..c70d9cb 100644 --- a/src/Backend/Auth/SignIn/SignInService.cs +++ b/src/Backend/Auth/SignIn/SignInService.cs @@ -235,6 +235,11 @@ public sealed class SignInService( await db.SaveChangesAsync(cancellationToken); await sessions.RevokeAllAsync(user.Id, caller.SessionId, cancellationToken); await AuditAsync(context, "auth.password.change", SignInProviders.Local, user.UserName, "succeeded", null, cancellationToken); + if (user.IsBreakGlass) + { + // The recorded one-time password is spent now. + InitialAdminPasswordFile.Delete(InitialAdminPasswordFile.PathFor(scope.ServiceProvider.GetRequiredService())); + } var updated = caller with { PasswordChangeRequired = false }; context.SetCaller(updated); diff --git a/tests/Backend.Tests/InitialAdminTests.cs b/tests/Backend.Tests/InitialAdminTests.cs new file mode 100644 index 0000000..098164a --- /dev/null +++ b/tests/Backend.Tests/InitialAdminTests.cs @@ -0,0 +1,133 @@ +using System.Net.Http.Json; +using System.Security.AccessControl; +using System.Security.Principal; +using System.Text.RegularExpressions; +using Backend.Auth; +using Backend.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Shared.Contracts.Api; +using Shared.Contracts.Serialization; +using Xunit; + +namespace Backend.Tests; + +/// +/// No admin password in the installer: the first start generates one, records it where only administrators can +/// read it, and requires a new one at the first sign-in; reset-admin recovers a lost or locked-out account. +/// +public partial class InitialAdminPasswordTests(BackendTestFixture fixture) : IClassFixture +{ + private string RecordPath => Path.Combine(fixture.DataPath, InitialAdminPasswordFile.FileName); + + [Fact] + public async Task TheGeneratedPasswordIsRecordedForAdministratorsAndMustBeChangedAtFirstSignIn() + { + using var _ = fixture.CreateClient(); + var record = File.ReadAllText(RecordPath); + var password = PasswordLine().Match(record).Groups[1].Value; + Assert.Contains($"User: {BreakGlass.DefaultUserName}", record); + Assert.True(password.Length >= 16, "a strong generated password"); + + using var client = fixture.CreateClient(); + (await client.SignInAsync(BreakGlass.DefaultUserName, password)).EnsureSuccessStatusCode(); + var session = await client.GetFromJsonAsync(ApiRoutes.AuthSession, JsonDefaults.Options); + Assert.True(session!.BreakGlass); + Assert.True(session.PasswordChangeRequired); + + if (OperatingSystem.IsWindows()) + { + AssertAdministratorsOnly(RecordPath); + } + else + { + Assert.Equal(UnixFileMode.UserRead | UnixFileMode.UserWrite, File.GetUnixFileMode(RecordPath)); + } + } + + [System.Runtime.Versioning.SupportedOSPlatform("windows")] + private static void AssertAdministratorsOnly(string path) + { + var acl = new FileInfo(path).GetAccessControl(); + Assert.True(acl.AreAccessRulesProtected, "inheritance is off"); + var allowed = new HashSet(); + foreach (FileSystemAccessRule rule in acl.GetAccessRules(true, true, typeof(SecurityIdentifier))) + { + allowed.Add((SecurityIdentifier)rule.IdentityReference); + } + + Assert.Contains(new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null), allowed); + Assert.Contains(new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null), allowed); + Assert.DoesNotContain(new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), allowed); + Assert.DoesNotContain(new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null), allowed); + Assert.DoesNotContain(new SecurityIdentifier(WellKnownSidType.WorldSid, null), allowed); + allowed.ExceptWith([ + new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null), + new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null), + WindowsIdentity.GetCurrent().User!, + ]); + Assert.Empty(allowed); + } + + [GeneratedRegex(@"Password: (\S+)")] + private static partial Regex PasswordLine(); +} + +/// reset-admin, run on the server with the service running or stopped. +public class ResetAdminTests(BackendTestFixture fixture) : IClassFixture +{ + private string RecordPath => Path.Combine(fixture.DataPath, InitialAdminPasswordFile.FileName); + + [Fact] + public async Task ResetAdminSetsAPasswordReEnablesTheAccountAndAuditsIt() + { + using var _ = fixture.CreateClient(); + await fixture.WithDbAsync(db => db.Users.Where(u => u.IsBreakGlass).ExecuteUpdateAsync(set => set.SetProperty(u => u.Enabled, false))); + var password = "Reset-" + Guid.NewGuid().ToString("N"); + + var exit = await AuthResetCli.RunAsync(["--password", password], CliConfiguration()); + + Assert.Equal(0, exit); + using var client = fixture.CreateClient(); + (await client.SignInAsync(BreakGlass.DefaultUserName, password)).EnsureSuccessStatusCode(); + var session = await client.GetFromJsonAsync(ApiRoutes.AuthSession, JsonDefaults.Options); + Assert.True(session!.PasswordChangeRequired); + Assert.True(await fixture.WithDbAsync(db => db.AuditEvents.AnyAsync(a => a.Action == "identity.break-glass.reset" && a.Actor == "reset-admin"))); + Assert.False(File.Exists(RecordPath), "the first-start record no longer holds a working password"); + } + + [Fact] + public async Task ResetAdminRefusesAnAccountThatDoesNotExist() + { + using var _ = fixture.CreateClient(); + + Assert.Equal(2, await AuthResetCli.RunAsync(["--user", "nobody-here"], CliConfiguration())); + } + + [Fact] + public void ResetAdminReadsTheStandaloneSettingsBesideTheBinary() + { + var folder = Path.Combine(Path.GetTempPath(), "executer-reset-admin-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(folder); + try + { + File.WriteAllText(Path.Combine(folder, "appsettings.json"), """{ "DATABASE_PATH": "C:/ExecuteR/data/db/app.db", "DATA_PATH": "C:/ExecuteR/data" }"""); + + var configuration = AuthResetCli.LoadConfiguration(folder); + + Assert.Equal("C:/ExecuteR/data/db/app.db", configuration["DATABASE_PATH"]); + Assert.Equal("C:/ExecuteR/data", configuration["DATA_PATH"]); + } + finally + { + Directory.Delete(folder, recursive: true); + } + } + + private IConfiguration CliConfiguration() => new ConfigurationBuilder().AddInMemoryCollection(new Dictionary + { + ["DATABASE_PATH"] = fixture.DatabasePath, + ["DATA_PATH"] = fixture.DataPath, + ["DATAPROTECTION_PATH"] = Path.Combine(fixture.DataPath, "keys"), + }).Build(); +} -- 2.52.0 From bae67a9ffc0e6ce3452d3373632951f9eef9dd273f879cdec9f668c827ea3565 Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Wed, 4 Nov 2026 20:42:38 -0500 Subject: [PATCH 12/22] feat(tls): serve a certificate from the Windows store or a file TLS_CERT_SOURCE=store (TLS_CERT_THUMBPRINT, LocalMachine\My, renewals with the same subject and issuer followed) or file (TLS_CERT_FILE PFX/PEM, TLS_CERT_KEY_FILE, TLS_CERT_PASSWORD), hot-reloaded within a minute; the managed self-signed chain stays the default. Each handshake carries the intermediates. A chain that is not publicly trusted is handed to agents like the managed one (GET /api/enrollment/ca without the leaf, its root's fingerprint in tokens); TLS_DISTRIBUTE_CA overrides. Root and intermediate go to /ca-certificates as .cer/.pem for Group Policy. setup takes --tls-source/--tls-thumbprint/--tls-cert-file/--tls-key-file/--tls-password, checks the certificate, writes values JSON-escaped and restricts appsettings.json. The install scripts validate first and pin the CA to the token only on a trust failure. Co-Authored-By: Claude Opus 5.5 --- IaC/compose/docker-compose.yml | 9 +- src/Backend/Api/BootstrapTokenEndpoints.cs | 8 +- src/Backend/Enrollment/InstallEndpoints.cs | 199 +++++++-- src/Backend/Program.cs | 52 ++- src/Backend/Standalone/ServerCli.cs | 209 ++++++++- src/Backend/Tls/ProvidedTlsCertificate.cs | 399 ++++++++++++++++++ src/Backend/Tls/SelfSignedTlsManager.cs | 54 ++- src/Backend/Tls/ServerTlsSource.cs | 178 ++++++++ src/Backend/Tls/TlsRenewalService.cs | 16 +- src/Backend/Tls/TlsSettings.cs | 115 ++++- .../CertificateTrustScriptTests.cs | 165 ++++++++ .../ProvidedTlsCertificateTests.cs | 322 ++++++++++++++ tests/Backend.Tests/SelfSignedTlsTests.cs | 34 ++ tests/Backend.Tests/TestPki.cs | 85 ++++ 14 files changed, 1722 insertions(+), 123 deletions(-) create mode 100644 src/Backend/Tls/ProvidedTlsCertificate.cs create mode 100644 src/Backend/Tls/ServerTlsSource.cs create mode 100644 tests/Backend.Tests/CertificateTrustScriptTests.cs create mode 100644 tests/Backend.Tests/ProvidedTlsCertificateTests.cs create mode 100644 tests/Backend.Tests/TestPki.cs diff --git a/IaC/compose/docker-compose.yml b/IaC/compose/docker-compose.yml index 2f89fbf..480fee8 100644 --- a/IaC/compose/docker-compose.yml +++ b/IaC/compose/docker-compose.yml @@ -4,7 +4,8 @@ #============================================================================= # # CREDENTIALS: -# Initial admin: INITIAL_ADMIN_USERNAME / INITIAL_ADMIN_PASSWORD (.env) +# Initial admin: generated at first start into /data/initial-admin-password.txt +# (or INITIAL_ADMIN_USERNAME / INITIAL_ADMIN_PASSWORD in .env) # NATS decentralized auth: seeds generated by scripts/New-LocalSecrets.ps1 # # PRODUCTION SETUP: @@ -117,6 +118,12 @@ services: TRUSTED_HOSTS: '${TRUSTED_HOSTS:-}' TLS_SELF_SIGNED: '${TLS_SELF_SIGNED:-0}' TLS_CERT_DIR: '${TLS_CERT_DIR:-}' + # Your own certificate (file only in a container): mount it read-only and point TLS_CERT_FILE at it. + TLS_CERT_SOURCE: '${TLS_CERT_SOURCE:-}' + TLS_CERT_FILE: '${TLS_CERT_FILE:-}' + TLS_CERT_KEY_FILE: '${TLS_CERT_KEY_FILE:-}' + TLS_CERT_PASSWORD: '${TLS_CERT_PASSWORD:-}' + TLS_DISTRIBUTE_CA: '${TLS_DISTRIBUTE_CA:-}' FORWARD_LIMIT: '${FORWARD_LIMIT:-1}' PUBLIC_SERVER_URL: '${PUBLIC_SERVER_URL:-}' INITIAL_ADMIN_USERNAME: '${INITIAL_ADMIN_USERNAME:-}' diff --git a/src/Backend/Api/BootstrapTokenEndpoints.cs b/src/Backend/Api/BootstrapTokenEndpoints.cs index d08b879..3c8757a 100644 --- a/src/Backend/Api/BootstrapTokenEndpoints.cs +++ b/src/Backend/Api/BootstrapTokenEndpoints.cs @@ -48,13 +48,15 @@ public static class BootstrapTokenEndpoints var maxUses = request?.MaxUses ?? ProtocolDefaults.BootstrapTokenDefaultMaxUses; // Plaintext exists exactly once: in this response. Only the hash is stored. - // When the server runs the managed self-signed chain, append its CA - // fingerprint (token = "secret.fp"): the agent pins the CA it fetches on + // When the server hands agents its issuing chain (the managed self-signed + // chain, or a provided certificate from a CA that is not publicly + // trusted), append that root's fingerprint (token = "secret.fp"): the + // agent pins the CA it fetches on // first contact to this value, so an on-path attacker cannot substitute a // CA. The fp travels wherever the token does — no extra plumbing — and the // hash covers the whole string, so enrollment validation is unchanged. var token = TokenGenerator.CreateUrlSafeToken(ProtocolDefaults.BootstrapTokenEntropyBytes); - var caFingerprint = context.RequestServices.GetService()?.CaFingerprint(); + var caFingerprint = context.RequestServices.GetService()?.CaFingerprint(); if (caFingerprint is not null) { token += "." + caFingerprint; diff --git a/src/Backend/Enrollment/InstallEndpoints.cs b/src/Backend/Enrollment/InstallEndpoints.cs index 15dcb60..b21da09 100644 --- a/src/Backend/Enrollment/InstallEndpoints.cs +++ b/src/Backend/Enrollment/InstallEndpoints.cs @@ -31,16 +31,18 @@ public static class InstallEndpoints } /// - /// Serves the managed self-signed CA chain (root + intermediate, public - /// certs only) so agents can trust the server across renewals. This is the - /// self-signed signal too: a 200 (a chain) means the server runs self-signed - /// TLS and agents should auto-trust it; a 404 means it is not self-signed - /// (a real edge certificate validates on its own — nothing to distribute). - /// The chain is not pinned; it is installed into the OS trust store. + /// Serves the issuing chain of the certificate this server presents + /// (intermediates + root, public certs only, never the leaf): the managed + /// self-signed chain, or a provided certificate's chain when that is not + /// publicly trusted (an enterprise CA). A 200 means agents that fail to + /// validate the server may anchor trust in it (pinned to the token's + /// fingerprint, then installed into the OS trust store); a 404 means there + /// is nothing to distribute (plain HTTP, or a publicly trusted chain that + /// validates on its own). /// private static IResult GetCaChain(HttpContext context) { - var manager = context.RequestServices.GetService(); + var manager = context.RequestServices.GetService(); var pem = manager?.CaChainPem(); return pem is null ? Results.NotFound() @@ -57,15 +59,15 @@ public static class InstallEndpoints var baseUrl = ResolveBaseUrl(context, configuration); - // The only thing the script needs from the server is whether its own - // download must skip verification: true when this request arrived over - // the self-signed listener directly (no proxy in front), so the agent's - // download from the same URL succeeds. A proxied request (real cert) - // needs no skip. Trust itself is handled by the agent at enrollment: it - // always probes GET /api/enrollment/ca (accept-any) and installs the CA - // when self-signed (404 = not self-signed = nothing to do). Where TLS - // terminates follows the URL, which keeps this flexible. - var skipDownloadVerify = tls.IsSelfSignedDirect(context); + // The package download trusts the server the way the agent does: normal + // validation first, and only when that fails, the CA the server + // publishes (GET /api/enrollment/ca), checked against the fingerprint + // the token carries. A token without one (made while the server served + // no chain of its own) keeps the old rule: skip verification only when + // this request arrived over this server's own private-chain listener + // directly, so the download from the same URL succeeds. + var fingerprint = TokenFingerprint(token); + var skipDownloadVerify = fingerprint is null && tls.IsSelfSignedDirect(context); // Token is embedded so the operator can paste one command; knowledge of a // valid token is itself the enrollment gate. The Windows scripts run in @@ -77,10 +79,10 @@ public static class InstallEndpoints var zipUrl = $"{baseUrl}{ApiRoutes.AgentPackage}?type=zip"; var script = type?.ToLowerInvariant() switch { - "linux" => BuildUnixScript(baseUrl, token!, "linux", skipDownloadVerify), - "osx" or "mac" or "macos" or "darwin" => BuildUnixScript(baseUrl, token!, "osx", skipDownloadVerify), - "zip" => BuildZipScript(baseUrl, token!, zipUrl, skipDownloadVerify), - _ => BuildMsiScript(baseUrl, token!, $"{baseUrl}{ApiRoutes.AgentPackage}?type=msi", zipUrl, skipDownloadVerify), + "linux" => BuildUnixScript(baseUrl, token!, "linux", skipDownloadVerify, fingerprint), + "osx" or "mac" or "macos" or "darwin" => BuildUnixScript(baseUrl, token!, "osx", skipDownloadVerify, fingerprint), + "zip" => BuildZipScript(baseUrl, token!, zipUrl, skipDownloadVerify, fingerprint), + _ => BuildMsiScript(baseUrl, token!, $"{baseUrl}{ApiRoutes.AgentPackage}?type=msi", zipUrl, skipDownloadVerify, fingerprint), }; return Results.Text(script, "text/plain; charset=utf-8"); @@ -132,23 +134,24 @@ public static class InstallEndpoints "[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }"; // NOTE: $$""" — interpolations are {{ }}, single braces are literal PowerShell. - // selfSigned: the server serves its managed self-signed chain directly, so - // the initial download skips verification (the cert isn't trusted yet) and - // the agent is told to install the server CA into the OS trust store (and - // keep it current) — system validation then trusts the server, without - // pinning, so a proxy with a real cert added later still works. + // The download validates normally and, only when the certificate is not + // trusted, pins the server's CA to the token's fingerprint (PsDownload); + // the agent then installs that CA into the OS trust store at enrollment, so + // system validation trusts the server and a proxy with a real cert added + // later still works. // The install runs in place: no auto-elevation (the self-relaunch tripped // AV/EDR ClickFix heuristics), no -EncodedCommand. The operator runs it from // an elevated PowerShell — the Add device modal states this prerequisite. // Windows PE (the MiniNT key) has no Windows Installer, so there the MSI // script takes the portable path instead — same service, same enrollment. private static string BuildMsiScript( - string baseUrl, string token, string packageUrl, string zipUrl, bool skipDownloadVerify) => $$""" + string baseUrl, string token, string packageUrl, string zipUrl, bool skipDownloadVerify, string? fingerprint) => $$""" $ErrorActionPreference = 'Stop' {{PsDownloadTls}} {{(skipDownloadVerify ? PsAcceptCert : "")}} $server = '{{baseUrl}}' $token = '{{token}}' + {{PsDownload(fingerprint)}} if (Test-Path '{{PsWinPeKey}}') { Write-Host 'Windows PE detected: installing ExecuteR Agent (portable)...' {{PortableInstall(zipUrl)}} @@ -157,9 +160,7 @@ public static class InstallEndpoints Write-Host 'Installing ExecuteR Agent...' $msi = Join-Path $env:TEMP ('ExecuteRAgent-' + [guid]::NewGuid().ToString('N') + '.msi') try { - $wc = New-Object System.Net.WebClient - $wc.DownloadFile('{{packageUrl}}', $msi) - $wc.Dispose() + Save-ExecuteRFile '{{packageUrl}}' $msi $arguments = @('/i', ('"' + $msi + '"'), '/qn', "SERVERURL=$server", "BOOTSTRAPTOKEN=$token") $p = Start-Process msiexec.exe -ArgumentList $arguments -Wait -PassThru if ($p.ExitCode -ne 0) { throw "msiexec exited with $($p.ExitCode)" } @@ -189,9 +190,7 @@ public static class InstallEndpoints $zip = Join-Path $env:TEMP ('ExecuteRAgent-' + [guid]::NewGuid().ToString('N') + '.zip') $unpacked = $zip + '.d' try { - $wc = New-Object System.Net.WebClient - $wc.DownloadFile('{{packageUrl}}', $zip) - $wc.Dispose() + Save-ExecuteRFile '{{packageUrl}}' $zip Add-Type -AssemblyName System.IO.Compression.FileSystem if (Test-Path $dest) { [System.IO.Compression.ZipFile]::ExtractToDirectory($zip, $unpacked) @@ -224,7 +223,7 @@ public static class InstallEndpoints // ReplaceLineEndings("\n"): the .cs source is checked out with CRLF on // Windows, but a bash script with CRLF fails hard when piped to bash // (`set -euo pipefail\r` -> ": invalid option namepefail"). Force LF. - private static string BuildUnixScript(string baseUrl, string token, string os, bool skipDownloadVerify) => ($$""" + private static string BuildUnixScript(string baseUrl, string token, string os, bool skipDownloadVerify, string? fingerprint) => ($$""" #!/usr/bin/env bash set -euo pipefail if [ "$(id -u)" -ne 0 ]; then echo 'Run with sudo/root.' >&2; exit 1; fi @@ -239,8 +238,9 @@ public static class InstallEndpoints dest=/opt/executer/agent tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT + {{ShDownload(fingerprint, skipDownloadVerify)}} echo "Installing ExecuteR Agent ({{os}}-${arch})..." - curl -fsSL{{(skipDownloadVerify ? "k" : "")}} "$pkg" -o "$tmp/agent.zip" + download "$pkg" "$tmp/agent.zip" mkdir -p "$dest" if command -v unzip >/dev/null 2>&1; then unzip -oq "$tmp/agent.zip" -d "$dest" @@ -253,13 +253,142 @@ public static class InstallEndpoints echo 'ExecuteR Agent installed. It will enroll and start automatically.' """).ReplaceLineEndings("\n"); - private static string BuildZipScript(string baseUrl, string token, string packageUrl, bool skipDownloadVerify) => $$""" + private static string BuildZipScript(string baseUrl, string token, string packageUrl, bool skipDownloadVerify, string? fingerprint) => $$""" $ErrorActionPreference = 'Stop' Write-Host 'Installing ExecuteR Agent (portable)...' {{PsDownloadTls}} {{(skipDownloadVerify ? PsAcceptCert : "")}} $server = '{{baseUrl}}' $token = '{{token}}' + {{PsDownload(fingerprint)}} {{PortableInstall(packageUrl)}} """; + + /// + /// The CA fingerprint a bootstrap token carries ("secret.fp", 64 hex + /// characters, as the agent reads it), or null. + /// + internal static string? TokenFingerprint(string? token) + { + var dot = token?.LastIndexOf('.') ?? -1; + if (token is null || dot < 0) + { + return null; + } + + var fingerprint = token[(dot + 1)..]; + return fingerprint.Length == 64 && fingerprint.All(Uri.IsHexDigit) ? fingerprint.ToLowerInvariant() : null; + } + + /// + /// Save-ExecuteRFile: a WebClient download. With a fingerprint in the token + /// it validates normally first (the outer one-liner's accept-any is undone) + /// and only when the certificate is not trusted fetches the server's CA, + /// finds the root whose SubjectPublicKeyInfo hashes to the fingerprint + /// (parsed from the DER, as Windows PowerShell 5.1's .NET has no SPKI + /// export), and accepts the server only through a chain that ends at that + /// root — extra certificates in the response cannot become anchors. The + /// agent installs the CA into the trust store itself when it enrolls. + /// + internal static string PsDownload(string? fingerprint) => fingerprint is null + ? """ + function Save-ExecuteRFile([string]$Url, [string]$Path) { + $wc = New-Object System.Net.WebClient + try { $wc.DownloadFile($Url, $Path) } finally { $wc.Dispose() } + } + """ + : $$""" + [System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null + $global:ExecuteRCaPin = '{{fingerprint}}' + $global:ExecuteRCa = $null + function Get-ExecuteRDerHeader([byte[]]$d, [int]$p) { + $l = [int]$d[$p + 1] + if ($l -lt 128) { return @(2, $l) } + $n = $l - 128; $l = 0 + for ($i = 0; $i -lt $n; $i++) { $l = $l * 256 + $d[$p + 2 + $i] } + return @((2 + $n), $l) + } + function Get-ExecuteRSpkiHash([byte[]]$d) { + $h = Get-ExecuteRDerHeader $d 0; $p = $h[0] + $h = Get-ExecuteRDerHeader $d $p; $p += $h[0] + if ($d[$p] -eq 0xA0) { $h = Get-ExecuteRDerHeader $d $p; $p += $h[0] + $h[1] } + for ($k = 0; $k -lt 5; $k++) { $h = Get-ExecuteRDerHeader $d $p; $p += $h[0] + $h[1] } + $h = Get-ExecuteRDerHeader $d $p + $sha = [System.Security.Cryptography.SHA256]::Create() + try { -join ($sha.ComputeHash($d, $p, $h[0] + $h[1]) | ForEach-Object { $_.ToString('x2') }) } finally { $sha.Dispose() } + } + function Use-ExecuteRCa { + [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true } + $wc = New-Object System.Net.WebClient + try { $pem = $wc.DownloadString($server + '{{ApiRoutes.EnrollmentCaChain}}') } + finally { $wc.Dispose(); [System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null } + $cas = @() + foreach ($m in [regex]::Matches($pem, '-----BEGIN CERTIFICATE-----([A-Za-z0-9+/=\s]+)-----END CERTIFICATE-----')) { + $cas += New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 (,[Convert]::FromBase64String(($m.Groups[1].Value -replace '\s', ''))) + } + $anchor = @($cas | Where-Object { (Get-ExecuteRSpkiHash $_.RawData) -eq $global:ExecuteRCaPin }) + if ($anchor.Count -eq 0) { throw 'The server CA does not match the bootstrap token; refusing to continue (possible interception).' } + $global:ExecuteRCa = @{ Anchor = $anchor[0].Thumbprint; Certificates = $cas } + [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { + param($sender, $certificate, $chain, $errors) + if ($errors -eq [System.Net.Security.SslPolicyErrors]::None) { return $true } + if ($errors -ne [System.Net.Security.SslPolicyErrors]::RemoteCertificateChainErrors) { return $false } + $c = New-Object System.Security.Cryptography.X509Certificates.X509Chain + $c.ChainPolicy.RevocationMode = [System.Security.Cryptography.X509Certificates.X509RevocationMode]::NoCheck + $c.ChainPolicy.VerificationFlags = [System.Security.Cryptography.X509Certificates.X509VerificationFlags]::AllowUnknownCertificateAuthority + foreach ($ca in $global:ExecuteRCa.Certificates) { [void]$c.ChainPolicy.ExtraStore.Add($ca) } + if (-not $c.Build((New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 (,$certificate.GetRawCertData())))) { return $false } + return $c.ChainElements[$c.ChainElements.Count - 1].Certificate.Thumbprint -eq $global:ExecuteRCa.Anchor + } + } + function Save-ExecuteRFile([string]$Url, [string]$Path) { + $wc = New-Object System.Net.WebClient + try { $wc.DownloadFile($Url, $Path) } + catch { + $e = $_.Exception + while ($e -and -not ($e -is [System.Net.WebException])) { $e = $e.InnerException } + if ($global:ExecuteRCa -or -not $e -or ($e.Status -ne [System.Net.WebExceptionStatus]::TrustFailure -and $e.Status -ne [System.Net.WebExceptionStatus]::SecureChannelFailure)) { throw } + Write-Host 'The server certificate is not trusted here yet: checking its CA against the bootstrap token...' + Use-ExecuteRCa + $wc.DownloadFile($Url, $Path) + } + finally { $wc.Dispose() } + } + """; + + /// + /// download URL FILE for the Unix script: the same rule with curl and + /// openssl. The CA file handed to curl holds only the pinned root and the + /// certificates that verify against it. + /// + internal static string ShDownload(string? fingerprint, bool skipDownloadVerify) => fingerprint is null + ? $$""" + download() { curl -fsSL{{(skipDownloadVerify ? "k" : "")}} "$1" -o "$2"; } + """ + : $$""" + pin='{{fingerprint}}' + cafile="$tmp/server-ca.pem" + download() { + local rc=0 anchor='' f + if [ -s "$cafile" ]; then curl -fsSL --cacert "$cafile" "$1" -o "$2"; return; fi + curl -fsSL "$1" -o "$2" || rc=$? + [ "$rc" -eq 0 ] && return 0 + [ "$rc" -eq 60 ] || [ "$rc" -eq 51 ] || [ "$rc" -eq 35 ] || return "$rc" + command -v openssl >/dev/null 2>&1 || { echo 'The server certificate is not trusted here, and openssl is needed to check its CA against the bootstrap token.' >&2; return 1; } + echo 'The server certificate is not trusted here yet: checking its CA against the bootstrap token...' + curl -fsSLk "$server{{ApiRoutes.EnrollmentCaChain}}" -o "$tmp/fetched-ca.pem" + awk -v dir="$tmp" '/-----BEGIN CERTIFICATE-----/{n++} n{print > (dir "/fetched-ca-" n ".pem")}' "$tmp/fetched-ca.pem" + for f in "$tmp"/fetched-ca-*.pem; do + [ -e "$f" ] || continue + if [ "$(openssl x509 -in "$f" -noout -pubkey | openssl pkey -pubin -outform DER | openssl dgst -sha256 -r | cut -d' ' -f1)" = "$pin" ]; then anchor="$f"; fi + done + [ -n "$anchor" ] || { echo 'The server CA does not match the bootstrap token; refusing to continue (possible interception).' >&2; return 1; } + cat "$anchor" > "$cafile" + for f in "$tmp"/fetched-ca-*.pem; do + [ "$f" = "$anchor" ] && continue + if openssl verify -CAfile "$anchor" -untrusted "$tmp/fetched-ca.pem" "$f" >/dev/null 2>&1; then cat "$f" >> "$cafile"; fi + done + curl -fsSL --cacert "$cafile" "$1" -o "$2" + } + """; } diff --git a/src/Backend/Program.cs b/src/Backend/Program.cs index f16583f..0633923 100644 --- a/src/Backend/Program.cs +++ b/src/Backend/Program.cs @@ -35,11 +35,12 @@ if (args.Length > 0 && args[0] == "nats-auth") return NatsAuthCli.Run(args[1..]); } -// CLI mode: lockout recovery — disable OIDC and restore local sign-in. -if (args.Length > 0 && args[0] == "auth-reset") +// CLI mode: lockout recovery — a new one-time admin password, local sign-in +// restored. Reads the standalone appsettings.json like the server does, so it +// reaches the same database whether the service is running or stopped. +if (args.Length > 0 && AuthResetCli.Verbs.Contains(args[0])) { - var cliConfig = new ConfigurationBuilder().AddEnvironmentVariables().Build(); - return await AuthResetCli.RunAsync(args[1..], cliConfig); + return await AuthResetCli.RunAsync(args[1..], AuthResetCli.LoadConfiguration()); } // CLI mode: standalone single-binary server management. @@ -95,22 +96,32 @@ builder.Services.ConfigureHttpJsonOptions(options => var proxySettings = ProxySettings.FromConfiguration(builder.Configuration); builder.Services.AddSingleton(proxySettings); -// ── Built-in encryption (self-signed, on by default) ───────────────────── +// ── Built-in encryption (self-signed by default, or a provided certificate) ─ // The listener serves HTTPS with a locally managed chain (root > // intermediate > leaf, auto-renewed, hot-swapped) so the proxy-to-server -// hop cannot be sniffed; TLS_SELF_SIGNED=0 opts out. Proxies either skip -// verification (the hop is still encrypted — the roots just aren't -// distributed) or import root+intermediate. Public TLS stays at the edge. +// hop cannot be sniffed; TLS_SELF_SIGNED=0 opts out. TLS_CERT_SOURCE=store +// (TLS_CERT_THUMBPRINT) or file (TLS_CERT_FILE) serves the administrator's +// own certificate instead, hot-reloaded the same way. Agents are handed the +// issuing chain unless it is publicly trusted. Public TLS stays at the edge. var tlsSettings = TlsSettings.FromConfiguration(builder.Configuration); builder.Services.AddSingleton(tlsSettings); -SelfSignedTlsManager? tlsManager = null; +IServerTlsSource? tlsSource = null; if (tlsSettings.Enabled) { - tlsManager = new SelfSignedTlsManager(tlsSettings); - builder.Services.AddSingleton(tlsManager); + if (tlsSettings.Source == TlsCertificateSource.SelfSigned) + { + var tlsManager = new SelfSignedTlsManager(tlsSettings); + builder.Services.AddSingleton(tlsManager); + tlsSource = tlsManager; + } + else + { + tlsSource = new ProvidedTlsCertificate(tlsSettings); + } + + builder.Services.AddSingleton(tlsSource); builder.Services.AddHostedService(); - builder.WebHost.ConfigureKestrel(kestrel => kestrel.ConfigureHttpsDefaults(https => - https.ServerCertificateSelector = (_, _) => tlsManager.ServerCertificate)); + builder.WebHost.ConfigureKestrel(kestrel => kestrel.ConfigureHttpsDefaults(https => ServerTls.UseSource(https, tlsSource))); var listenUrls = builder.Configuration["urls"] is { Length: > 0 } configured ? configured : "http://0.0.0.0:8080"; @@ -234,16 +245,19 @@ builder.Services.AddRateLimiter(options => var app = builder.Build(); -// The chain must exist before Kestrel accepts the first TLS handshake. -if (tlsManager is not null) +// The certificate must be loaded before Kestrel accepts the first TLS +// handshake; a provided one that cannot be loaded stops the server here with +// the reason. +if (tlsSource is not null) { - tlsManager.EnsureChain(app.Logger); - if (tlsManager.UsesWindowsStore) + tlsSource.Refresh(app.Logger); + if (tlsSource is SelfSignedTlsManager { UsesWindowsStore: true }) { app.Logger.LogInformation( - "Built-in TLS: serving HTTPS with the managed self-signed chain from the Windows certificate stores (proxies: skip verification or import the root+intermediate)"); + "Built-in TLS: serving HTTPS with the managed self-signed chain from the Windows certificate stores (proxies: skip verification or import the root+intermediate from {Distribution})", + tlsSettings.DistributionDirectory); } - else + else if (tlsSource is SelfSignedTlsManager) { app.Logger.LogInformation( "Built-in TLS: serving HTTPS with the managed self-signed chain in {Directory} (proxies: skip verification or pin fullchain.crt)", diff --git a/src/Backend/Standalone/ServerCli.cs b/src/Backend/Standalone/ServerCli.cs index 9fabd9b..c31dcd1 100644 --- a/src/Backend/Standalone/ServerCli.cs +++ b/src/Backend/Standalone/ServerCli.cs @@ -9,15 +9,22 @@ namespace Backend.Standalone; /// /// Standalone-server commands for the combined single-binary deployment: /// -/// setup [--admin-user U] [--admin-password P] [--tls 0] +/// setup [--tls 0] [--tls-source self-signed|store|file] +/// [--tls-thumbprint T] [--tls-cert-file F] [--tls-key-file K] [--tls-password P] +/// [--admin-user U] [--admin-password P] /// — generate NATS keys + sidecar config + appsettings, /// downloading nats-server for this platform if absent /// service install — register as a Windows service / systemd unit /// service uninstall — stop and remove the service /// purge [--yes] — remove the self-signed chain + generated state +/// reset-admin [--user U] [--password P] +/// — lockout recovery (AuthResetCli): a new one-time password /// /// The same binary is the server: running it with no arguments starts the -/// control plane (UI + API + WebSocket relay + optional NATS sidecar). +/// control plane (UI + API + WebSocket relay + optional NATS sidecar). With no +/// admin password given, the first start generates one, records it in +/// data/initial-admin-password.txt for administrators only, and requires a new +/// one at the first sign-in. /// public static class ServerCli { @@ -28,13 +35,22 @@ public static class ServerCli public static async Task RunSetupAsync(string[] args) { - // Bare `setup` fully configures the server: admin/admin unless the - // flags override — and the defaulted password forces a reset prompt - // on the first sign-in. Port flags let several instances share a box - // (one directory per instance; pair with `service install --name`). + // Bare `setup` fully configures the server. The admin password is left + // empty: the first start generates one, records it where only + // administrators can read it and forces a new one at the first sign-in + // (--admin-password still sets one, for scripted installs; the literal + // old default "admin" forces the reset too). Port flags let several + // instances share a box (one directory per instance; pair with + // `service install --name`). var adminUser = "admin"; - var adminPassword = "admin"; - var passwordDefaulted = true; + var adminPassword = ""; + var passwordDefaulted = false; + // TLS source: the managed self-signed chain unless a certificate is given. + string? tlsSourceText = null; + string? tlsThumbprint = null; + string? tlsCertFile = null; + string? tlsKeyFile = null; + string? tlsPassword = null; var httpPort = 9080; var bindAddress = "0.0.0.0"; var natsPort = 4222; @@ -90,14 +106,34 @@ public static class ServerCli natsBind = natsCandidate; break; case "--admin-user": - adminUser = args[++i]; + adminUser = args[++i] is { Length: > 0 } user ? user : "admin"; break; case "--admin-password": adminPassword = args[++i]; - // The MSI passes the flag even when the wizard default was - // kept: a literal "admin" password always forces the reset. + // A silent install that still passes the old wizard default + // (admin) gets the reset forced at the first sign-in. passwordDefaulted = adminPassword == "admin"; break; + case "--tls-source": + case "--ts": + tlsSourceText = args[++i]; + break; + case "--tls-thumbprint": + case "--tt": + tlsThumbprint = args[++i]; + break; + case "--tls-cert-file": + case "--tf": + tlsCertFile = args[++i]; + break; + case "--tls-key-file": + case "--tk": + tlsKeyFile = args[++i]; + break; + case "--tls-password": + case "--tp": + tlsPassword = args[++i]; + break; // Short aliases keep the MSI's custom-action command inside // the 255-character CustomAction table column (ICE03). case "--http-port": @@ -124,6 +160,35 @@ public static class ServerCli selfSignedTls = true; } + // A provided certificate (store or file) turns TLS on by itself and is + // checked now, so a wrong thumbprint, path or password fails setup (and + // the MSI) instead of the first start. + Backend.Tls.TlsCertificateSource tlsSource; + try + { + tlsThumbprint = Backend.Tls.TlsSettings.NormalizeThumbprint(tlsThumbprint); + tlsCertFile = tlsCertFile is { Length: > 0 } ? Path.GetFullPath(tlsCertFile.Trim().Trim('"')) : null; + tlsKeyFile = tlsKeyFile is { Length: > 0 } ? Path.GetFullPath(tlsKeyFile.Trim().Trim('"')) : null; + tlsPassword = tlsPassword is { Length: > 0 } ? tlsPassword : null; + tlsSource = Backend.Tls.TlsSettings.ParseSource(tlsSourceText, tlsThumbprint, tlsCertFile); + } + catch (InvalidOperationException exception) + { + return Fail(exception.Message.Replace("TLS_CERT_SOURCE", "--tls-source")); + } + + if (tlsSource != Backend.Tls.TlsCertificateSource.SelfSigned) + { + var problem = CheckProvidedCertificate(tlsSource, tlsThumbprint, tlsCertFile, tlsKeyFile, tlsPassword); + if (problem is not null) + { + return Fail(problem); + } + } + + var tlsOn = selfSignedTls || tlsSource != Backend.Tls.TlsCertificateSource.SelfSigned; + selfSignedTls &= tlsSource == Backend.Tls.TlsCertificateSource.SelfSigned; + var baseDir = AppContext.BaseDirectory; var natsDir = Path.Combine(baseDir, "nats"); var dataDir = Path.Combine(baseDir, "data"); @@ -161,11 +226,12 @@ public static class ServerCli // The core protocol and monitor listeners are backend-only, so they // always stay on loopback. Only the WebSocket is proxy-facing; its bind // opens with --nats-bind for an edge proxy on another host, and when - // that happens over self-signed TLS it reuses the server's own managed - // leaf (data/tls) so nothing extra is generated. The loopback default - // stays plaintext — the backend relays /messaging/* over loopback. + // that happens over TLS it reuses the server's own leaf (data/tls: the + // managed one, or a provided one mirrored there as PEM) so nothing extra + // is generated. The loopback default stays plaintext — the backend + // relays /messaging/* over loopback. var natsWsExposed = !System.Net.IPAddress.IsLoopback(System.Net.IPAddress.Parse(natsBind)); - var natsWsTls = selfSignedTls && natsWsExposed; + var natsWsTls = tlsOn && natsWsExposed; var natsWsSecurity = natsWsTls ? " tls {\n cert_file: \"../data/tls/server.crt\"\n key_file: \"../data/tls/server.key\"\n }" : " no_tls: true"; @@ -211,9 +277,15 @@ public static class ServerCli { var signingSeed = File.ReadAllText(Path.Combine(secretsDir, "account-signing.nk")).Trim(); var accountPublic = File.ReadAllText(Path.Combine(secretsDir, "account.pub")).Trim(); + var tlsSourceName = tlsSource switch + { + Backend.Tls.TlsCertificateSource.Store => "store", + Backend.Tls.TlsCertificateSource.File => "file", + _ => "self-signed", + }; File.WriteAllText(appsettings, $$""" { - "Urls": "{{(selfSignedTls ? "https" : "http")}}://{{bindAddress}}:{{httpPort}}", + "Urls": "{{(tlsOn ? "https" : "http")}}://{{bindAddress}}:{{httpPort}}", "DATA_PATH": "{{dataDir.Replace('\\', '/')}}", "DATABASE_PATH": "{{Path.Combine(dataDir, "db", "app.db").Replace('\\', '/')}}", "DATAPROTECTION_PATH": "{{Path.Combine(dataDir, "keys").Replace('\\', '/')}}", @@ -226,11 +298,17 @@ public static class ServerCli "NATS_MONITOR_URL": "http://127.0.0.1:{{natsMonitorPort}}", "NATS_ACCOUNT_SIGNING_SEED": "{{signingSeed}}", "NATS_ACCOUNT_PUBLIC_KEY": "{{accountPublic}}", - "INITIAL_ADMIN_USERNAME": "{{adminUser}}", - "INITIAL_ADMIN_PASSWORD": "{{adminPassword}}", + "INITIAL_ADMIN_USERNAME": {{Json(adminUser)}}, + "INITIAL_ADMIN_PASSWORD": {{Json(adminPassword)}}, "INITIAL_ADMIN_MUST_CHANGE": "{{(passwordDefaulted ? "1" : "0")}}", "TLS_SELF_SIGNED": "{{(selfSignedTls ? "1" : "0")}}", + "TLS_CERT_SOURCE": "{{tlsSourceName}}", + "TLS_CERT_THUMBPRINT": {{Json(tlsThumbprint)}}, + "TLS_CERT_FILE": {{Json(tlsCertFile?.Replace('\\', '/'))}}, + "TLS_CERT_KEY_FILE": {{Json(tlsKeyFile?.Replace('\\', '/'))}}, + "TLS_CERT_PASSWORD": {{Json(tlsPassword)}}, + "TLS_DISTRIBUTE_CA": "", "TLS_CERT_DIR": "", "PUBLIC_SERVER_URL": "", "TRUST_PROXY": "1", @@ -238,15 +316,29 @@ public static class ServerCli "TRUSTED_HOSTS": "" } """); + ProtectSettingsFile(appsettings); Console.WriteLine($"Wrote {appsettings}"); } Console.WriteLine(); Console.WriteLine("Setup complete."); - Console.WriteLine(passwordDefaulted - ? $" - First sign-in: {adminUser} / admin — a new password is required immediately." - : $" - First sign-in: {adminUser} / ."); - var scheme = selfSignedTls ? "https" : "http"; + Console.WriteLine(adminPassword.Length == 0 + ? $" - First sign-in: {adminUser} with the one-time password the first start writes to {Path.Combine(dataDir, Backend.Auth.InitialAdminPasswordFile.FileName)} (administrators only); a new one is required immediately. Locked out? `reset-admin`." + : passwordDefaulted + ? $" - First sign-in: {adminUser} / admin — a new password is required immediately." + : $" - First sign-in: {adminUser} / ."); + if (tlsSource != Backend.Tls.TlsCertificateSource.SelfSigned) + { + Console.WriteLine(tlsSource == Backend.Tls.TlsCertificateSource.Store + ? $" - TLS: the certificate {tlsThumbprint} from LocalMachine\\My (renewals with the same subject and issuer are followed)." + : $" - TLS: the certificate in {tlsCertFile} (replace the file to renew; it is reloaded within a minute)."); + if (natsWsTls && tlsSource == Backend.Tls.TlsCertificateSource.Store) + { + Console.WriteLine(" - The exposed NATS WebSocket serves the same certificate only when its private key is exportable."); + } + } + + var scheme = tlsOn ? "https" : "http"; Console.WriteLine($" - UI + API on {scheme}://{bindAddress}:{httpPort} (NATS on {natsBind}: {natsPort}/{natsWsPort}/{natsMonitorPort})."); if (selfSignedTls) { @@ -254,6 +346,7 @@ public static class ServerCli Console.WriteLine(" verification (not insecure — the roots just aren't distributed) or"); Console.WriteLine(" import the root+intermediate to establish trust (re-import when they"); Console.WriteLine(" rotate at expiry). Follow your organization's security standards."); + Console.WriteLine($" Root and intermediate for Group Policy: {Path.Combine(dataDir, Backend.Tls.TlsSettings.DistributionFolderName)}."); } Console.WriteLine(" - Run the binary directly, or `service install` to register the Windows service."); @@ -261,6 +354,78 @@ public static class ServerCli return 0; } + /// A JSON string literal (quotes and escapes included); null is "". + private static string Json(string? value) => System.Text.Json.JsonSerializer.Serialize(value ?? string.Empty); + + /// Loads the certificate the way the server will; the reason it cannot be used, or null. + private static string? CheckProvidedCertificate( + Backend.Tls.TlsCertificateSource source, string? thumbprint, string? certificateFile, string? keyFile, string? password) + { + try + { + var settings = new Backend.Tls.TlsSettings + { + Enabled = true, + Source = source, + StoreThumbprint = thumbprint, + CertificateFile = certificateFile, + KeyFile = keyFile, + CertificatePassword = password, + CertificateDirectory = Path.GetTempPath(), + UseWindowsStore = false, + }; + new Backend.Tls.ProvidedTlsCertificate(settings).Refresh(Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance); + return null; + } + catch (Exception exception) when (exception is InvalidOperationException or IOException or UnauthorizedAccessException + or System.Security.Cryptography.CryptographicException or ArgumentException) + { + return $"The TLS certificate cannot be used: {exception.Message}"; + } + } + + /// + /// appsettings.json carries the NATS signing seed and any certificate + /// password: on Windows only Administrators, SYSTEM (the service) and the + /// account running setup may open it. Elsewhere it is mode 600. + /// + private static void ProtectSettingsFile(string path) + { + try + { + if (OperatingSystem.IsWindows()) + { + var security = new System.Security.AccessControl.FileSecurity(); + security.SetAccessRuleProtection(isProtected: true, preserveInheritance: false); + var sids = new List + { + new(System.Security.Principal.WellKnownSidType.BuiltinAdministratorsSid, null), + new(System.Security.Principal.WellKnownSidType.LocalSystemSid, null), + }; + if (System.Security.Principal.WindowsIdentity.GetCurrent().User is { } self && !sids.Contains(self)) + { + sids.Add(self); + } + + foreach (var sid in sids) + { + security.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule( + sid, System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow)); + } + + new FileInfo(path).SetAccessControl(security); + } + else + { + File.SetUnixFileMode(path, UnixFileMode.UserRead | UnixFileMode.UserWrite); + } + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException or InvalidOperationException) + { + Console.Error.WriteLine($"Could not restrict access to {path}: {exception.Message}"); + } + } + private static int ParsePort(string value, string flag) => int.TryParse(value, out var port) && port is >= 1 and <= 65535 ? port diff --git a/src/Backend/Tls/ProvidedTlsCertificate.cs b/src/Backend/Tls/ProvidedTlsCertificate.cs new file mode 100644 index 0000000..db8e609 --- /dev/null +++ b/src/Backend/Tls/ProvidedTlsCertificate.cs @@ -0,0 +1,399 @@ +using System.Net.Security; +using System.Runtime.Versioning; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; + +namespace Backend.Tls; + +/// +/// Serves a certificate the administrator provides instead of the managed +/// chain: one in LocalMachine\My by thumbprint (Windows), or a file — a +/// PFX/P12 (TLS_CERT_PASSWORD) or a PEM certificate with its key (in the same +/// file or TLS_CERT_KEY_FILE; a fullchain PEM brings its intermediates). +/// Checked every minute: a replaced file, or a store certificate renewed by +/// auto-enrollment (the configured one archived or about to expire, its +/// successor with the same subject and issuer), is picked up without a +/// restart. A chain that is not publicly trusted (an enterprise CA) is +/// distributed the same way as the managed one: GET /api/enrollment/ca serves +/// it without the leaf, tokens carry its root's fingerprint, and the +/// distribution folder holds it; a publicly trusted chain distributes nothing. +/// +public sealed class ProvidedTlsCertificate : IServerTlsSource +{ + private const string ServerAuthentication = "1.3.6.1.5.5.7.3.1"; + private static readonly TimeSpan RenewalWindow = TimeSpan.FromDays(14); + + private readonly TlsSettings settings; + private readonly StoreLocation storeLocation; + private readonly Func isPublic; + private Served? served; + private string? fileStamp; + private string? lastFailure; + + /// Source, thumbprint or file, password and distribution choice. + /// LocalMachine in service; tests use CurrentUser. + /// Whether a root (and whether the chain built with the system's trust) counts as publicly trusted; tests replace it. + public ProvidedTlsCertificate(TlsSettings settings, StoreLocation storeLocation = StoreLocation.LocalMachine, + Func? isPublic = null) + { + if (settings.Source == TlsCertificateSource.SelfSigned) + { + throw new ArgumentException("A provided certificate needs TLS_CERT_SOURCE store or file.", nameof(settings)); + } + + this.settings = settings; + this.storeLocation = storeLocation; + this.isPublic = isPublic ?? ((root, trusted) => trusted && IsPublicRoot(root)); + } + + private sealed record Served(X509Certificate2 Leaf, SslStreamCertificateContext Context, IReadOnlyList Distributed, string? ChainPem, string? Fingerprint); + + public TimeSpan RefreshInterval => TimeSpan.FromMinutes(1); + + public X509Certificate2? ServerCertificate => Volatile.Read(ref served)?.Leaf; + + public SslStreamCertificateContext? CertificateContext => Volatile.Read(ref served)?.Context; + + public string? CaChainPem() => Volatile.Read(ref served)?.ChainPem; + + public string? CaFingerprint() => Volatile.Read(ref served)?.Fingerprint; + + /// The CA certificates handed to agents (intermediates then root); empty when none are. + public IReadOnlyList DistributedCertificates => Volatile.Read(ref served)?.Distributed ?? []; + + /// + /// Loads the certificate, or picks up a replacement. The first load throws + /// with the reason (the server must not start without its certificate); + /// later failures — a file caught half-written, a key not readable — are + /// logged once and the current certificate stays in service. + /// + public bool Refresh(ILogger logger) + { + var current = Volatile.Read(ref served); + string? stamp = null; + X509Certificate2 leaf; + X509Certificate2Collection extras; + try + { + if (settings.Source == TlsCertificateSource.File) + { + stamp = FileStamp(); + if (current is not null && stamp == fileStamp) + { + return false; + } + + (leaf, extras) = LoadFromFile(); + } + else + { + (leaf, extras) = LoadFromStore(current?.Leaf, logger); + } + + CheckUsable(leaf); + } + catch (Exception exception) when (current is not null + && exception is CryptographicException or IOException or InvalidOperationException or UnauthorizedAccessException) + { + if (exception.Message != lastFailure) + { + lastFailure = exception.Message; + logger.LogWarning("TLS: the provided certificate could not be reloaded ({Reason}); still serving {Thumbprint}", exception.Message, current.Leaf.Thumbprint); + } + + return false; + } + + lastFailure = null; + fileStamp = stamp; + if (current is not null && current.Leaf.Thumbprint == leaf.Thumbprint) + { + return false; + } + + var next = Build(leaf, extras, logger); + Volatile.Write(ref served, next); + logger.LogInformation("TLS: serving {Subject} ({Thumbprint}, valid to {NotAfter:u}) from {Source}{Distribution}", + leaf.Subject, leaf.Thumbprint, leaf.NotAfter.ToUniversalTime(), + settings.Source == TlsCertificateSource.Store ? $"{storeLocation}\\My" : settings.CertificateFile, + next.ChainPem is null ? "; its chain is publicly trusted, so agents are given nothing" : "; agents are given its issuing chain"); + TlsDistribution.Write(settings.DistributionDirectory, next.Distributed, logger); + if (OperatingSystem.IsWindows() && settings.UseWindowsStore && storeLocation == StoreLocation.LocalMachine) + { + InstallIntermediates(next.Context.IntermediateCertificates, logger); + } + + MirrorForNats(leaf, next.Context.IntermediateCertificates, logger); + return true; + } + + private Served Build(X509Certificate2 leaf, X509Certificate2Collection extras, ILogger logger) + { + using var chain = new X509Chain(); + chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; + chain.ChainPolicy.ExtraStore.AddRange(extras); + var trusted = chain.Build(leaf); + var cas = chain.ChainElements.Skip(1).Select(e => X509CertificateLoader.LoadCertificate(e.Certificate.RawData)).ToList(); + var root = cas.LastOrDefault(ServerTls.IsSelfIssued); + var intermediates = cas.Where(c => !ServerTls.IsSelfIssued(c)).ToList(); + var context = SslStreamCertificateContext.Create(leaf, [.. intermediates], offline: true); + + var distribute = settings.DistributeCa ?? (root is not null && !isPublic(root, trusted)); + if (distribute && root is null) + { + logger.LogWarning( + "TLS: the root that issued {Subject} is neither in the certificate file nor in this server's stores, so agents cannot be given it; add the full chain to the file or trust the root here", + leaf.Subject); + distribute = false; + } + + if (!distribute) + { + return new Served(leaf, context, [], null, null); + } + + List distributed = [.. intermediates, root!]; + return new Served(leaf, context, distributed, ServerTls.ChainPem(distributed), ServerTls.Fingerprint(root!)); + } + + private static void CheckUsable(X509Certificate2 leaf) + { + if (!leaf.HasPrivateKey) + { + throw new InvalidOperationException($"The certificate {leaf.Thumbprint} has no private key this server can use."); + } + + if (leaf.Extensions.OfType().FirstOrDefault() is { } eku + && !eku.EnhancedKeyUsages.Cast().Any(o => o.Value == ServerAuthentication)) + { + throw new InvalidOperationException($"The certificate {leaf.Thumbprint} is not for server authentication (its Enhanced Key Usage lacks {ServerAuthentication})."); + } + } + + private string FileStamp() + { + var files = new[] { settings.CertificateFile!, settings.KeyFile }.Where(f => f is { Length: > 0 }).Select(f => new FileInfo(f!)).ToList(); + foreach (var file in files.Where(f => !f.Exists)) + { + throw new FileNotFoundException($"The certificate file {file.FullName} does not exist.", file.FullName); + } + + return string.Join('|', files.Select(f => $"{f.FullName}:{f.Length}:{f.LastWriteTimeUtc.Ticks}")); + } + + private (X509Certificate2 Leaf, X509Certificate2Collection Extras) LoadFromFile() + { + var path = settings.CertificateFile!; + X509Certificate2Collection all; + X509Certificate2 leaf; + if (IsPem(path)) + { + var keyPath = settings.KeyFile is { Length: > 0 } key ? key : path; + var pem = settings.CertificatePassword is { } password + ? X509Certificate2.CreateFromEncryptedPemFile(path, password, keyPath) + : X509Certificate2.CreateFromPemFile(path, keyPath); + if (OperatingSystem.IsWindows()) + { + // SChannel refuses ephemeral PEM keys; round-trip through PKCS#12. + using (pem) + { + leaf = X509CertificateLoader.LoadPkcs12(pem.Export(X509ContentType.Pfx), null, X509KeyStorageFlags.Exportable); + } + } + else + { + leaf = pem; + } + + all = []; + all.ImportFromPemFile(path); + } + else + { + // Exportable: the PEM mirror for an exposed NATS WebSocket needs the key. + all = X509CertificateLoader.LoadPkcs12CollectionFromFile(path, settings.CertificatePassword, X509KeyStorageFlags.Exportable); + leaf = all.Cast() + .Where(c => c.HasPrivateKey) + .OrderBy(c => ServerTls.IsCertificateAuthority(c) ? 1 : 0) + .ThenByDescending(c => c.NotAfter) + .FirstOrDefault() + ?? throw new InvalidOperationException($"{path} holds no certificate with a private key."); + } + + X509Certificate2Collection extras = [.. all.Cast().Where(c => c.Thumbprint != leaf.Thumbprint)]; + return (leaf, extras); + } + + private static bool IsPem(string path) + { + if (Path.GetExtension(path).ToLowerInvariant() is ".pfx" or ".p12") + { + return false; + } + + using var stream = File.OpenRead(path); + var head = new byte[Math.Min(4096, stream.Length)]; + stream.ReadExactly(head); + return System.Text.Encoding.ASCII.GetString(head).Contains("-----BEGIN", StringComparison.Ordinal); + } + + /// + /// The configured thumbprint, or its renewal: auto-enrollment archives the + /// old certificate and adds a new one with the same subject and issuer, so + /// when the configured one is archived, missing or inside the renewal + /// window, the newest current certificate with that subject and issuer and + /// a private key is served instead (and the thumbprint to set is logged). + /// + private (X509Certificate2 Leaf, X509Certificate2Collection Extras) LoadFromStore(X509Certificate2? servedLeaf, ILogger logger) + { + var thumbprint = settings.StoreThumbprint!; + using var store = new X509Store(StoreName.My, storeLocation); + store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly | OpenFlags.IncludeArchived); + var now = DateTime.Now; + var configured = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validOnly: false) + .Cast().FirstOrDefault(); + var usable = configured is { HasPrivateKey: true, Archived: false } && configured.NotAfter > now + RenewalWindow; + if (usable) + { + return (configured!, []); + } + + var template = configured ?? servedLeaf; + var renewal = template is null + ? null + : store.Certificates.Cast() + .Where(c => !c.Archived && c.HasPrivateKey && c.Thumbprint != thumbprint + && c.Subject == template.Subject && c.Issuer == template.Issuer + && c.NotBefore <= now && c.NotAfter > (configured?.NotAfter ?? now)) + .MaxBy(c => c.NotAfter); + if (renewal is not null) + { + if (servedLeaf?.Thumbprint != renewal.Thumbprint) + { + logger.LogWarning( + "TLS: certificate {Configured} is {State}; serving its renewal {Renewal} (same subject and issuer). Set TLS_CERT_THUMBPRINT to {Renewal} to make it permanent.", + thumbprint, configured is null ? "missing" : configured.Archived ? "archived" : "about to expire", renewal.Thumbprint, renewal.Thumbprint); + } + + return (renewal, []); + } + + if (configured is { HasPrivateKey: true, Archived: false } && configured.NotAfter > now) + { + return (configured, []); + } + + throw new InvalidOperationException(configured is null + ? $"There is no certificate {thumbprint} in {storeLocation}\\My." + : $"The certificate {thumbprint} in {storeLocation}\\My is {(configured.Archived ? "archived" : !configured.HasPrivateKey ? "without a private key" : "expired")} and has no renewal there."); + } + + /// + /// Publicly trusted: on Windows the root is in the Third-Party Root + /// Certification Authorities store, where Windows Update puts the public + /// roots (an enterprise root is in Trusted Root only); elsewhere it is in + /// the system bundle and not one of the locally added anchors. A wrong + /// "private" only costs agents a CA they already trust; a wrong "public" + /// would leave devices outside the domain unable to connect. + /// + internal static bool IsPublicRoot(X509Certificate2 root) + { + try + { + if (OperatingSystem.IsWindows()) + { + using var store = new X509Store(StoreName.AuthRoot, StoreLocation.LocalMachine); + store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); + return store.Certificates.Find(X509FindType.FindByThumbprint, root.Thumbprint, validOnly: false).Count > 0; + } + + foreach (var directory in new[] { "/usr/local/share/ca-certificates", "/etc/pki/ca-trust/source/anchors", "/etc/ca-certificates/trust-source/anchors" }) + { + if (!Directory.Exists(directory)) + { + continue; + } + + foreach (var file in Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories)) + { + try + { + X509Certificate2Collection anchors = []; + anchors.ImportFromPemFile(file); + if (anchors.Cast().Any(a => a.Thumbprint == root.Thumbprint)) + { + return false; + } + } + catch (Exception) + { + // Not a certificate. + } + } + } + + return true; + } + catch (Exception) + { + return false; + } + } + + [SupportedOSPlatform("windows")] + private static void InstallIntermediates(IEnumerable intermediates, ILogger logger) + { + try + { + using var store = new X509Store(StoreName.CertificateAuthority, StoreLocation.LocalMachine); + store.Open(OpenFlags.ReadWrite); + foreach (var intermediate in intermediates) + { + if (store.Certificates.Find(X509FindType.FindByThumbprint, intermediate.Thumbprint, validOnly: false).Count == 0) + { + store.Add(intermediate); + logger.LogInformation("TLS: added {Subject} to LocalMachine\\CA so the handshake sends the whole chain", intermediate.Subject); + } + } + } + catch (Exception exception) + { + logger.LogDebug(exception, "TLS: intermediates not added to LocalMachine\\CA"); + } + } + + /// An exposed NATS WebSocket serves the same certificate from PEM files (nats.conf names server.crt/server.key). + private void MirrorForNats(X509Certificate2 leaf, IEnumerable intermediates, ILogger logger) + { + if (settings.LeafMirrorDirectory is not { } directory) + { + return; + } + + try + { + string key; + using (var rsa = leaf.GetRSAPrivateKey()) + using (var ecdsa = rsa is null ? leaf.GetECDsaPrivateKey() : null) + { + key = rsa?.ExportPkcs8PrivateKeyPem() ?? ecdsa?.ExportPkcs8PrivateKeyPem() + ?? throw new CryptographicException("Only RSA and ECDSA keys can be mirrored."); + } + + Directory.CreateDirectory(directory); + var keyPath = Path.Combine(directory, "server.key"); + File.WriteAllText(keyPath, key); + if (!OperatingSystem.IsWindows()) + { + File.SetUnixFileMode(keyPath, UnixFileMode.UserRead | UnixFileMode.UserWrite); + } + + File.WriteAllText(Path.Combine(directory, "server.crt"), + leaf.ExportCertificatePem() + '\n' + string.Concat(intermediates.Select(c => c.ExportCertificatePem() + '\n'))); + } + catch (Exception exception) when (exception is CryptographicException or IOException or UnauthorizedAccessException) + { + logger.LogWarning("TLS: the NATS WebSocket cannot serve the provided certificate ({Reason}); its key must be exportable", exception.Message); + } + } +} diff --git a/src/Backend/Tls/SelfSignedTlsManager.cs b/src/Backend/Tls/SelfSignedTlsManager.cs index a5b2e60..8b9d8a0 100644 --- a/src/Backend/Tls/SelfSignedTlsManager.cs +++ b/src/Backend/Tls/SelfSignedTlsManager.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using System.Net.Security; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; @@ -27,16 +28,28 @@ public sealed record TlsLifetimes( /// created on first boot, re-issued ahead of expiry, hot-swapped into the /// live listener through (Kestrel's /// certificate selector reads it on every handshake — renewals need no -/// restart). Windows persists to the machine certificate stores; everywhere -/// else (or with TLS_CERT_DIR set) the chain is PEM files. +/// restart). Windows persists to the machine certificate stores (root in +/// Trusted Roots, intermediate in Intermediate CAs, leaf in Personal), so the +/// server itself trusts the chain; everywhere else (or with TLS_CERT_DIR set) +/// the chain is PEM files. Every settle also refreshes the distribution +/// folder (root + intermediate only) for Group Policy. /// -public sealed class SelfSignedTlsManager +public sealed class SelfSignedTlsManager : IServerTlsSource { private readonly Lock gate = new(); private readonly TlsSettings settings; private readonly TlsLifetimes lifetimes; private readonly ITlsPersistence persistence; private X509Certificate2? leaf; + private SslStreamCertificateContext? context; + private string? chainPem; + private string? fingerprint; + + public TimeSpan RefreshInterval => TimeSpan.FromHours(6); + + public SslStreamCertificateContext? CertificateContext => Volatile.Read(ref context); + + public bool Refresh(ILogger logger) => EnsureChain(logger); public SelfSignedTlsManager(TlsSettings settings, TlsLifetimes? lifetimes = null) { @@ -65,20 +78,7 @@ public sealed class SelfSignedTlsManager /// to agents so they can validate the leaf and keep trusting it across /// renewals. Null before the chain exists. Contains no private keys. /// - public string? CaChainPem() - { - lock (gate) - { - var root = persistence.Load(TlsRole.Root); - var intermediate = persistence.Load(TlsRole.Intermediate); - if (root is null || intermediate is null) - { - return null; - } - - return intermediate.ExportCertificatePem() + '\n' + root.ExportCertificatePem() + '\n'; - } - } + public string? CaChainPem() => Volatile.Read(ref chainPem); /// /// SHA-256 (lowercase hex) of the root CA's public key (SubjectPublicKeyInfo). @@ -88,17 +88,7 @@ public sealed class SelfSignedTlsManager /// in-place root renewal (which re-issues the cert but keeps the key). Null /// before the chain exists. /// - public string? CaFingerprint() - { - lock (gate) - { - var root = persistence.Load(TlsRole.Root); - return root is null - ? null - : Convert.ToHexStringLower( - System.Security.Cryptography.SHA256.HashData(root.PublicKey.ExportSubjectPublicKeyInfo())); - } - } + public string? CaFingerprint() => Volatile.Read(ref fingerprint); /// /// Creates or re-issues whatever links are missing, expiring, or no longer @@ -160,7 +150,15 @@ public sealed class SelfSignedTlsManager } persistence.Complete(root, intermediate, current); + if (changed || Volatile.Read(ref context) is null) + { + Volatile.Write(ref context, SslStreamCertificateContext.Create(current, [intermediate], offline: true)); + } + + Volatile.Write(ref chainPem, ServerTls.ChainPem([intermediate, root])); + Volatile.Write(ref fingerprint, ServerTls.Fingerprint(root)); Volatile.Write(ref leaf, current); + TlsDistribution.Write(settings.DistributionDirectory, [intermediate, root], logger); return changed; } } diff --git a/src/Backend/Tls/ServerTlsSource.cs b/src/Backend/Tls/ServerTlsSource.cs new file mode 100644 index 0000000..218e3f0 --- /dev/null +++ b/src/Backend/Tls/ServerTlsSource.cs @@ -0,0 +1,178 @@ +using System.Net.Security; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using Microsoft.AspNetCore.Server.Kestrel.Https; + +namespace Backend.Tls; + +/// Where the listener's certificate comes from. +public enum TlsCertificateSource +{ + /// The managed self-signed chain (root > intermediate > leaf), the default. + SelfSigned, + + /// A certificate in LocalMachine\My, by thumbprint (Windows). + Store, + + /// A PFX/P12, or a PEM certificate and key. + File, +} + +/// +/// The certificate the listener serves and what agents are given to trust it. +/// Kestrel reads on every handshake, so a +/// renewal or replacement found by needs no restart. +/// +public interface IServerTlsSource +{ + /// The current server certificate; null until the first . + X509Certificate2? ServerCertificate { get; } + + /// The leaf with its intermediates, so the handshake sends the chain. + SslStreamCertificateContext? CertificateContext { get; } + + /// + /// The issuing chain agents are given (intermediates then root, PEM, public + /// certificates only, never the leaf), or null when there is nothing to + /// distribute (a publicly trusted chain): GET /api/enrollment/ca is then 404. + /// + string? CaChainPem(); + + /// + /// SHA-256 (lowercase hex) of the distributed root's SubjectPublicKeyInfo, + /// appended to bootstrap tokens; null with nothing distributed. + /// + string? CaFingerprint(); + + /// How often runs. + TimeSpan RefreshInterval { get; } + + /// Loads, renews or picks up a replaced certificate. Returns true when the served certificate changed. + bool Refresh(ILogger logger); +} + +/// Helpers shared by the certificate sources. +public static class ServerTls +{ + /// Hashes the SubjectPublicKeyInfo, the pin bootstrap tokens carry. + public static string Fingerprint(X509Certificate2 certificate) + => Convert.ToHexStringLower(SHA256.HashData(certificate.PublicKey.ExportSubjectPublicKeyInfo())); + + public static bool IsSelfIssued(X509Certificate2 certificate) + => certificate.SubjectName.RawData.AsSpan().SequenceEqual(certificate.IssuerName.RawData); + + public static bool IsCertificateAuthority(X509Certificate2 certificate) + => certificate.Extensions.OfType().FirstOrDefault() is { CertificateAuthority: true } + || (IsSelfIssued(certificate) && certificate.Extensions.OfType().FirstOrDefault() is null); + + /// Intermediates then root, as GET /api/enrollment/ca serves them. + public static string ChainPem(IEnumerable caCertificates) + => string.Concat(caCertificates.Select(c => c.ExportCertificatePem() + '\n')); + + /// + /// Serves on every HTTPS endpoint. The selector + /// satisfies Kestrel's configuration check; each handshake then takes the + /// current certificate context, which carries the intermediates (a bare + /// certificate only sends what the OS can find in its own stores). + /// + public static void UseSource(HttpsConnectionAdapterOptions https, IServerTlsSource source) + { + https.ServerCertificateSelector = (_, _) => source.ServerCertificate; + https.OnAuthenticate = (_, options) => + { + if (source.CertificateContext is { } context) + { + options.ServerCertificateSelectionCallback = null; + options.ServerCertificate = null; + options.ServerCertificateContext = context; + } + }; + } +} + +/// +/// The distribution folder: the root and intermediate certificates (never +/// the leaf, never a private key) as DER .cer (Group Policy's import format) +/// and PEM, plus ca-chain.pem, rewritten whenever the chain changes so an +/// administrator can push them to devices that do not run the agent, or ahead +/// of it. Emptied when the served chain is publicly trusted. +/// +public static class TlsDistribution +{ + private const string Marker = "ca-chain.pem"; + + /// Writes the files for (intermediates then root); none clears the folder. Returns true when anything changed. + public static bool Write(string? directory, IReadOnlyList caCertificates, ILogger logger) + { + if (directory is null) + { + return false; + } + + try + { + var wanted = new Dictionary(StringComparer.OrdinalIgnoreCase); + var intermediates = caCertificates.Where(c => !ServerTls.IsSelfIssued(c)).ToList(); + for (var i = 0; i < intermediates.Count; i++) + { + var name = intermediates.Count == 1 ? "intermediate-ca" : $"intermediate-ca-{i + 1}"; + wanted[name + ".cer"] = intermediates[i].RawData; + wanted[name + ".pem"] = System.Text.Encoding.ASCII.GetBytes(intermediates[i].ExportCertificatePem() + '\n'); + } + + if (caCertificates.FirstOrDefault(ServerTls.IsSelfIssued) is { } root) + { + wanted["root-ca.cer"] = root.RawData; + wanted["root-ca.pem"] = System.Text.Encoding.ASCII.GetBytes(root.ExportCertificatePem() + '\n'); + } + + if (wanted.Count > 0) + { + wanted[Marker] = System.Text.Encoding.ASCII.GetBytes(ServerTls.ChainPem(caCertificates)); + } + + var changed = false; + if (Directory.Exists(directory)) + { + foreach (var stale in Directory.EnumerateFiles(directory) + .Where(f => IsOurs(Path.GetFileName(f)) && !wanted.ContainsKey(Path.GetFileName(f)))) + { + File.Delete(stale); + changed = true; + } + } + + if (wanted.Count > 0) + { + Directory.CreateDirectory(directory); + } + + foreach (var (name, content) in wanted) + { + var path = Path.Combine(directory, name); + if (!File.Exists(path) || !File.ReadAllBytes(path).AsSpan().SequenceEqual(content)) + { + File.WriteAllBytes(path, content); + changed = true; + } + } + + if (changed && wanted.Count > 0) + { + logger.LogInformation("TLS: CA certificates for distribution (root and intermediate, no keys) are in {Directory}", directory); + } + + return changed; + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException) + { + logger.LogWarning(exception, "TLS: could not refresh the CA certificates in {Directory}", directory); + return false; + } + } + + private static bool IsOurs(string name) + => name.Equals(Marker, StringComparison.OrdinalIgnoreCase) + || ((name.StartsWith("root-ca", StringComparison.OrdinalIgnoreCase) || name.StartsWith("intermediate-ca", StringComparison.OrdinalIgnoreCase)) + && (name.EndsWith(".cer", StringComparison.OrdinalIgnoreCase) || name.EndsWith(".pem", StringComparison.OrdinalIgnoreCase))); +} diff --git a/src/Backend/Tls/TlsRenewalService.cs b/src/Backend/Tls/TlsRenewalService.cs index 8dfadb2..3e2ad93 100644 --- a/src/Backend/Tls/TlsRenewalService.cs +++ b/src/Backend/Tls/TlsRenewalService.cs @@ -1,27 +1,29 @@ namespace Backend.Tls; /// -/// Background renewal for the managed self-signed chain: re-runs the -/// idempotent EnsureChain periodically so the leaf (and eventually the CAs) -/// are re-issued well before expiry and hot-swapped without a restart. +/// Keeps the served certificate current without a restart: re-runs the +/// source's idempotent refresh periodically — the managed chain is re-issued +/// well before expiry (every 6 hours checked), a provided certificate's +/// replacement or renewal is picked up within a minute — and Kestrel takes +/// the new one on the next handshake. /// -public sealed class TlsRenewalService(SelfSignedTlsManager manager, ILogger logger) +public sealed class TlsRenewalService(IServerTlsSource source, ILogger logger) : BackgroundService { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - using var timer = new PeriodicTimer(TimeSpan.FromHours(6)); + using var timer = new PeriodicTimer(source.RefreshInterval); try { while (await timer.WaitForNextTickAsync(stoppingToken)) { try { - manager.EnsureChain(logger); + source.Refresh(logger); } catch (Exception ex) { - logger.LogError(ex, "Self-signed TLS renewal check failed; will retry"); + logger.LogError(ex, "TLS certificate refresh failed; will retry"); } } } diff --git a/src/Backend/Tls/TlsSettings.cs b/src/Backend/Tls/TlsSettings.cs index a0f3b68..4bb3e1d 100644 --- a/src/Backend/Tls/TlsSettings.cs +++ b/src/Backend/Tls/TlsSettings.cs @@ -12,8 +12,52 @@ namespace Backend.Tls; /// public sealed class TlsSettings { + /// The distribution folder's name under the data directory. + public const string DistributionFolderName = "ca-certificates"; + public bool Enabled { get; init; } + /// + /// Where the server certificate comes from (TLS_CERT_SOURCE): the managed + /// self-signed chain (the default), LocalMachine\My by thumbprint + /// (TLS_CERT_THUMBPRINT, Windows), or a file (TLS_CERT_FILE: PFX/PKCS#12 + /// with TLS_CERT_PASSWORD, or PEM with TLS_CERT_KEY_FILE). + /// + public TlsCertificateSource Source { get; init; } = TlsCertificateSource.SelfSigned; + + /// LocalMachine\My thumbprint (Source = Store). + public string? StoreThumbprint { get; init; } + + /// PFX/P12 or PEM certificate file (Source = File). + public string? CertificateFile { get; init; } + + /// PEM private key file; empty when the key is in (or a PFX). + public string? KeyFile { get; init; } + + /// PFX password, or an encrypted PEM key's password. + public string? CertificatePassword { get; init; } + + /// + /// TLS_DISTRIBUTE_CA: whether a provided certificate's issuing chain is + /// handed to agents (GET /api/enrollment/ca, the token fingerprint and the + /// distribution folder). Null decides by itself: only a chain that is not + /// publicly trusted is distributed. + /// + public bool? DistributeCa { get; init; } + + /// + /// The root and intermediate certificates (never the leaf, never a key), + /// as .cer and .pem, for administrators to push by Group Policy. Null + /// writes nothing. Defaults to <data>/ca-certificates. + /// + public string? DistributionDirectory { get; init; } + + /// + /// Where a provided certificate is mirrored as server.crt/server.key PEM for + /// an exposed NATS WebSocket (NATS_TLS_CERT's folder); null mirrors nothing. + /// + public string? LeafMirrorDirectory { get; init; } + /// PEM storage directory (file mode). Defaults to <data>/tls. public required string CertificateDirectory { get; init; } @@ -29,21 +73,61 @@ public sealed class TlsSettings public IReadOnlyList IpAddresses { get; init; } = []; /// - /// True when this request reached the self-signed listener directly — - /// HTTPS served here with no forwarded headers consumed (a proxied - /// request carries X-Original-* after UseForwardedHeaders). Generated - /// install commands then include TLS skip-verify so enrollment works - /// against the undistributed roots. + /// True when this request reached this server's own listener directly and + /// the listener serves a chain devices may not trust yet (the managed + /// self-signed chain, or a provided certificate from a private CA) — HTTPS + /// served here with no forwarded headers consumed (a proxied request + /// carries X-Original-* after UseForwardedHeaders). Generated install + /// commands then accept the certificate for the script fetch; the script + /// and the agent pin the CA to the token's fingerprint from there. /// public bool IsSelfSignedDirect(HttpContext context) => Enabled && context.Request.IsHttps && !context.Request.Headers.ContainsKey("X-Original-Proto") - && !context.Request.Headers.ContainsKey("X-Original-For"); + && !context.Request.Headers.ContainsKey("X-Original-For") + && (context.RequestServices.GetService() is not { } source || source.CaChainPem() is not null); + + /// TLS_CERT_SOURCE: self-signed (or managed), store, file; empty infers it from which certificate setting is set. + public static TlsCertificateSource ParseSource(string? value, string? thumbprint, string? file) + => value?.Trim().Replace("-", string.Empty).Replace("_", string.Empty).ToLowerInvariant() switch + { + null or "" => thumbprint is { Length: > 0 } ? TlsCertificateSource.Store + : file is { Length: > 0 } ? TlsCertificateSource.File + : TlsCertificateSource.SelfSigned, + "selfsigned" or "managed" => TlsCertificateSource.SelfSigned, + "store" or "windowsstore" or "certificatestore" => TlsCertificateSource.Store, + "file" or "pfx" or "pem" => TlsCertificateSource.File, + _ => throw new InvalidOperationException($"TLS_CERT_SOURCE must be self-signed, store or file (got '{value}')."), + }; + + /// Thumbprints copied from the certificate console carry spaces and an invisible left-to-right mark. + public static string? NormalizeThumbprint(string? thumbprint) + => thumbprint is null ? null : new string([.. thumbprint.Where(Uri.IsHexDigit)]).ToUpperInvariant() is { Length: > 0 } clean ? clean : null; public static TlsSettings FromConfiguration(IConfiguration configuration) { - var enabled = configuration["TLS_SELF_SIGNED"] != "0"; + var thumbprint = NormalizeThumbprint(configuration["TLS_CERT_THUMBPRINT"]); + var certificateFile = configuration["TLS_CERT_FILE"] is { Length: > 0 } f ? f.Trim().Trim('"') : null; + var source = ParseSource(configuration["TLS_CERT_SOURCE"], thumbprint, certificateFile); + if (source == TlsCertificateSource.Store && thumbprint is null) + { + throw new InvalidOperationException("TLS_CERT_SOURCE=store needs TLS_CERT_THUMBPRINT (a certificate in LocalMachine\\My)."); + } + + if (source == TlsCertificateSource.Store && !OperatingSystem.IsWindows()) + { + throw new InvalidOperationException("TLS_CERT_SOURCE=store is for Windows; elsewhere use TLS_CERT_FILE."); + } + + if (source == TlsCertificateSource.File && certificateFile is null) + { + throw new InvalidOperationException("TLS_CERT_SOURCE=file needs TLS_CERT_FILE (a PFX/P12, or a PEM with TLS_CERT_KEY_FILE)."); + } + + // A provided certificate turns TLS on by itself; TLS_SELF_SIGNED=0 only opts out of the managed chain. + var enabled = source != TlsCertificateSource.SelfSigned || configuration["TLS_SELF_SIGNED"] != "0"; + var selfSigned = enabled && source == TlsCertificateSource.SelfSigned; var certDir = configuration["TLS_CERT_DIR"]; // Prefer the explicit data root; otherwise fall back to the database's // directory. The DB now lives in data/db, so deriving from it alone @@ -86,7 +170,7 @@ public sealed class TlsSettings // public address validates. Best-effort and time-bounded, and only // when we're actually generating a self-signed cert (never in tests // or when TLS is off) so it makes no network calls otherwise. - if (enabled) + if (selfSigned) { AddPublicIpAddresses(ipAddresses); } @@ -135,6 +219,21 @@ public sealed class TlsSettings return new TlsSettings { Enabled = enabled, + Source = source, + StoreThumbprint = thumbprint, + CertificateFile = certificateFile, + KeyFile = configuration["TLS_CERT_KEY_FILE"] is { Length: > 0 } key ? key.Trim().Trim('"') : null, + CertificatePassword = configuration["TLS_CERT_PASSWORD"] is { Length: > 0 } password ? password : null, + DistributeCa = configuration["TLS_DISTRIBUTE_CA"]?.Trim().ToLowerInvariant() switch + { + "1" or "true" or "yes" => true, + "0" or "false" or "no" => false, + _ => null, + }, + DistributionDirectory = Path.Combine(dataDir, DistributionFolderName), + LeafMirrorDirectory = source != TlsCertificateSource.SelfSigned && configuration["NATS_TLS_CERT"] is { Length: > 0 } natsCertificate + ? Path.GetDirectoryName(Path.GetFullPath(natsCertificate)) + : null, CertificateDirectory = certDir is { Length: > 0 } ? certDir : Path.Combine(dataDir, "tls"), UseWindowsStore = OperatingSystem.IsWindows() && certDir is not { Length: > 0 }, DnsNames = dnsNames, diff --git a/tests/Backend.Tests/CertificateTrustScriptTests.cs b/tests/Backend.Tests/CertificateTrustScriptTests.cs new file mode 100644 index 0000000..5ae0d6e --- /dev/null +++ b/tests/Backend.Tests/CertificateTrustScriptTests.cs @@ -0,0 +1,165 @@ +using System.Diagnostics; +using System.Net; +using System.Net.Http.Json; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using System.Text.Json; +using Backend.Enrollment; +using Xunit; + +namespace Backend.Tests; + +/// The install scripts trust the server as the agent does: normal validation, then the CA pinned to the token. +public class CertificateTrustScriptTests(BackendTestFixture fixture) : IClassFixture +{ + private const string Fingerprint = "0f1e2d3c4b5a69788796a5b4c3d2e1f00f1e2d3c4b5a69788796a5b4c3d2e1f0"; + + [Theory] + [InlineData(null)] + [InlineData("zip")] + public async Task APinnedTokenMakesTheWindowsDownloadValidateFirstAndPinOnlyOnFailure(string? type) + { + using var client = fixture.CreateClient(); + var url = $"/api/enrollment/install?token=secret.{Fingerprint}" + (type is null ? "" : $"&type={type}"); + var script = await (await client.GetAsync(url)).Content.ReadAsStringAsync(); + + // The one-liner's accept-any is undone before anything is downloaded. + var reset = script.IndexOf("ServerCertificateValidationCallback = $null", StringComparison.Ordinal); + var firstDownload = script.IndexOf("Save-ExecuteRFile '", StringComparison.Ordinal); + Assert.True(reset >= 0 && reset < firstDownload, "validation is restored before the first download"); + Assert.Contains($"$global:ExecuteRCaPin = '{Fingerprint}'", script); + Assert.Contains("TrustFailure", script); + Assert.Contains("/api/enrollment/ca", script); + Assert.Contains("-eq $global:ExecuteRCa.Anchor", script); + } + + [Fact] + public async Task APinnedTokenMakesTheUnixDownloadValidateFirstAndPinOnlyOnFailure() + { + using var client = fixture.CreateClient(); + var script = await (await client.GetAsync($"/api/enrollment/install?token=secret.{Fingerprint}&type=linux")).Content.ReadAsStringAsync(); + + Assert.Contains($"pin='{Fingerprint}'", script); + Assert.Contains("download \"$pkg\" \"$tmp/agent.zip\"", script); + Assert.Contains("curl -fsSL \"$1\" -o \"$2\" || rc=$?", script); + Assert.Contains("--cacert \"$cafile\"", script); + Assert.Contains("openssl verify -CAfile \"$anchor\"", script); + Assert.DoesNotContain("curl -fsSLk \"$pkg\"", script); + Assert.DoesNotContain("\r", script); + } + + [Fact] + public void TheTokenFingerprintIsReadAsTheAgentReadsIt() + { + Assert.Equal(Fingerprint, InstallEndpoints.TokenFingerprint($"abc.{Fingerprint.ToUpperInvariant()}")); + Assert.Null(InstallEndpoints.TokenFingerprint("abc")); + Assert.Null(InstallEndpoints.TokenFingerprint("abc.not-hex")); + Assert.Null(InstallEndpoints.TokenFingerprint($"abc.{Fingerprint}00")); + } + + [Fact] + public async Task WindowsPowerShellHashesTheSubjectPublicKeyInfoAsDotNetDoes() + { + var powershell = Path.Combine(Environment.SystemDirectory, @"WindowsPowerShell\v1.0\powershell.exe"); + Assert.SkipUnless(OperatingSystem.IsWindows() && File.Exists(powershell), "Needs Windows PowerShell 5.1."); + using var pki = new TestPki(); + using var rsaLeaf = pki.IssueLeaf(); + using var ecKey = ECDsa.Create(ECCurve.NamedCurves.nistP384); + using var ecRoot = new CertificateRequest("CN=EC Root", ecKey, HashAlgorithmName.SHA384).CreateSelfSigned(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddYears(1)); + var folder = Path.Combine(Path.GetTempPath(), "executer-spki-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(folder); + try + { + var certificates = new[] { pki.Root, pki.Intermediate, rsaLeaf, ecRoot }; + for (var i = 0; i < certificates.Length; i++) + { + File.WriteAllBytes(Path.Combine(folder, $"{i}.cer"), certificates[i].RawData); + } + + var script = Path.Combine(folder, "spki.ps1"); + File.WriteAllText(script, "$server = 'https://unused'\n" + InstallEndpoints.PsDownload(Fingerprint) + "\n" + + $"0..{certificates.Length - 1} | ForEach-Object {{ Get-ExecuteRSpkiHash ([IO.File]::ReadAllBytes((Join-Path '{folder}' \"$_.cer\"))) }}\n"); + using var process = Process.Start(new ProcessStartInfo(powershell, $"-NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"{script}\"") + { + RedirectStandardOutput = true, + RedirectStandardError = true, + })!; + var output = await process.StandardOutput.ReadToEndAsync(); + var errors = await process.StandardError.ReadToEndAsync(); + await process.WaitForExitAsync(); + + Assert.True(process.ExitCode == 0, errors); + Assert.Equal( + certificates.Select(c => Convert.ToHexStringLower(SHA256.HashData(c.PublicKey.ExportSubjectPublicKeyInfo()))), + output.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)); + } + finally + { + Directory.Delete(folder, recursive: true); + } + } +} + +/// +/// A provided certificate from a private CA: the server hands agents that CA (never the leaf) and tokens pin its root, +/// exactly as with the managed chain. +/// +public sealed class ProvidedCertificateEnrollmentTests : IClassFixture, IDisposable +{ + private const string Password = "enrollment-pfx"; + private readonly BackendTestFixture fixture; + private readonly TestPki pki = new("Enrollment Test"); + private readonly string pfx = Path.Combine(Path.GetTempPath(), $"executer-provided-{Guid.NewGuid():N}.pfx"); + + public ProvidedCertificateEnrollmentTests(BackendTestFixture fixture) + { + this.fixture = fixture; + if (!fixture.Settings.ContainsKey("TLS_CERT_FILE")) + { + using var leaf = pki.IssueLeaf(); + pki.WritePfx(pfx, leaf, Password); + fixture.Settings["TLS_CERT_SOURCE"] = "file"; + fixture.Settings["TLS_CERT_FILE"] = pfx; + fixture.Settings["TLS_CERT_PASSWORD"] = Password; + // File mode: never the machine stores from a test run. + fixture.Settings["TLS_CERT_DIR"] = Path.Combine(fixture.DataPath, "tls"); + } + } + + private X509Certificate2 ServedRoot() + { + var collection = X509CertificateLoader.LoadPkcs12CollectionFromFile(fixture.Settings["TLS_CERT_FILE"]!, Password); + return collection.Cast().Single(c => c.Subject == c.Issuer); + } + + [Fact] + public async Task TheCaEndpointServesTheEnterpriseChainWithoutTheLeaf() + { + using var client = fixture.CreateClient(); + var response = await client.GetAsync("/api/enrollment/ca"); + + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + X509Certificate2Collection chain = []; + chain.ImportFromPem(await response.Content.ReadAsStringAsync()); + Assert.Equal(2, chain.Count); + Assert.Equal(ServedRoot().Thumbprint, chain[1].Thumbprint); + Assert.All(chain.Cast(), c => Assert.Contains(c.Extensions.OfType(), b => b.CertificateAuthority)); + } + + [Fact] + public async Task TokensCarryTheEnterpriseRootsFingerprint() + { + using var admin = await fixture.SignedInAsync("provided-tls-admin"); + var response = await admin.PostAsJsonAsync("/api/admin/bootstrap-tokens", new { maxUses = 1, validMinutes = 60 }); + response.EnsureSuccessStatusCode(); + using var body = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); + var token = body.RootElement.GetProperty("token").GetString()!; + + Assert.EndsWith("." + Convert.ToHexStringLower(SHA256.HashData(ServedRoot().PublicKey.ExportSubjectPublicKeyInfo())), token); + } + + public void Dispose() + { + pki.Dispose(); + } +} diff --git a/tests/Backend.Tests/ProvidedTlsCertificateTests.cs b/tests/Backend.Tests/ProvidedTlsCertificateTests.cs new file mode 100644 index 0000000..aa4e495 --- /dev/null +++ b/tests/Backend.Tests/ProvidedTlsCertificateTests.cs @@ -0,0 +1,322 @@ +using System.Net; +using System.Net.Security; +using System.Security.Cryptography.X509Certificates; +using Backend.Tls; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting.Server; +using Microsoft.AspNetCore.Hosting.Server.Features; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Xunit; + +namespace Backend.Tests; + +/// A certificate the administrator provides: from a file or the Windows store, hot-reloaded, its private chain handed to agents. +public sealed class ProvidedTlsCertificateTests : IDisposable +{ + private const string Password = "pfx-test-password"; + private readonly string directory = Path.Combine(Path.GetTempPath(), "executer-provided-tls-tests", Guid.NewGuid().ToString("N")); + private readonly TestPki pki = new(); + + public ProvidedTlsCertificateTests() => Directory.CreateDirectory(directory); + + private string Distribution => Path.Combine(directory, "ca-certificates"); + + public void Dispose() + { + pki.Dispose(); + try + { + Directory.Delete(directory, recursive: true); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + } + } + + private TlsSettings FileSettings(string file, string? key = null, string? password = null, bool? distribute = null) => new() + { + Enabled = true, + Source = TlsCertificateSource.File, + CertificateFile = file, + KeyFile = key, + CertificatePassword = password, + DistributeCa = distribute, + DistributionDirectory = Distribution, + CertificateDirectory = Path.Combine(directory, "tls"), + UseWindowsStore = false, + }; + + [Fact] + public void APfxFromAPrivateCaIsServedAndItsChainWithoutTheLeafGoesToAgents() + { + using var leaf = pki.IssueLeaf(); + var pfx = Path.Combine(directory, "server.pfx"); + pki.WritePfx(pfx, leaf, Password); + var source = new ProvidedTlsCertificate(FileSettings(pfx, password: Password)); + + Assert.True(source.Refresh(NullLogger.Instance)); + + Assert.Equal(leaf.Thumbprint, source.ServerCertificate!.Thumbprint); + Assert.True(source.ServerCertificate.HasPrivateKey); + Assert.Equal([pki.Intermediate.Thumbprint], source.CertificateContext!.IntermediateCertificates.Select(c => c.Thumbprint)); + X509Certificate2Collection served = []; + served.ImportFromPem(source.CaChainPem()!); + Assert.Equal([pki.Intermediate.Thumbprint, pki.Root.Thumbprint], served.Select(c => c.Thumbprint)); + Assert.Equal(pki.RootFingerprint, source.CaFingerprint()); + } + + [Fact] + public void TheDistributionFolderHoldsRootAndIntermediateButNeverTheLeafOrAKey() + { + using var leaf = pki.IssueLeaf(); + var pfx = Path.Combine(directory, "server.pfx"); + pki.WritePfx(pfx, leaf, Password); + + new ProvidedTlsCertificate(FileSettings(pfx, password: Password)).Refresh(NullLogger.Instance); + + var files = Directory.GetFiles(Distribution).Select(Path.GetFileName).Order().ToList(); + Assert.Equal(["ca-chain.pem", "intermediate-ca.cer", "intermediate-ca.pem", "root-ca.cer", "root-ca.pem"], files); + Assert.Equal(pki.Root.Thumbprint, X509CertificateLoader.LoadCertificateFromFile(Path.Combine(Distribution, "root-ca.cer")).Thumbprint); + Assert.Equal(pki.Intermediate.Thumbprint, X509CertificateLoader.LoadCertificateFromFile(Path.Combine(Distribution, "intermediate-ca.cer")).Thumbprint); + foreach (var file in Directory.GetFiles(Distribution)) + { + var bytes = File.ReadAllBytes(file); + Assert.DoesNotContain("PRIVATE KEY", System.Text.Encoding.ASCII.GetString(bytes)); + Assert.False(bytes.AsSpan().IndexOf(leaf.RawData) >= 0, $"{file} carries the leaf"); + } + } + + [Fact] + public void APublicChainGivesAgentsNothingAndEmptiesTheFolder() + { + using var leaf = pki.IssueLeaf(); + var pfx = Path.Combine(directory, "server.pfx"); + pki.WritePfx(pfx, leaf, Password); + new ProvidedTlsCertificate(FileSettings(pfx, password: Password)).Refresh(NullLogger.Instance); + Assert.NotEmpty(Directory.GetFiles(Distribution)); + + var publicSource = new ProvidedTlsCertificate(FileSettings(pfx, password: Password), isPublic: (_, _) => true); + publicSource.Refresh(NullLogger.Instance); + + Assert.Null(publicSource.CaChainPem()); + Assert.Null(publicSource.CaFingerprint()); + Assert.Empty(Directory.GetFiles(Distribution)); + Assert.Equal(leaf.Thumbprint, publicSource.ServerCertificate!.Thumbprint); + } + + [Theory] + [InlineData(false, false)] + [InlineData(true, true)] + public void TlsDistributeCaOverridesTheDecision(bool distribute, bool expected) + { + using var leaf = pki.IssueLeaf(); + var pfx = Path.Combine(directory, "server.pfx"); + pki.WritePfx(pfx, leaf, Password); + + // Classified public, yet TLS_DISTRIBUTE_CA=1 hands the chain out; classified private, yet =0 keeps it back. + var source = new ProvidedTlsCertificate(FileSettings(pfx, password: Password, distribute: distribute), isPublic: (_, _) => !distribute); + source.Refresh(NullLogger.Instance); + + Assert.Equal(expected, source.CaChainPem() is not null); + } + + [Fact] + public void APemFullChainAndKeyAreServedAndAReplacementIsPickedUp() + { + using var first = pki.IssueLeaf("CN=first.test.local"); + var certificate = Path.Combine(directory, "fullchain.pem"); + var key = Path.Combine(directory, "privkey.pem"); + pki.WritePem(certificate, key, first); + var source = new ProvidedTlsCertificate(FileSettings(certificate, key)); + source.Refresh(NullLogger.Instance); + Assert.Equal(first.Thumbprint, source.ServerCertificate!.Thumbprint); + Assert.Equal(pki.RootFingerprint, source.CaFingerprint()); + + Assert.False(source.Refresh(NullLogger.Instance)); + + using var second = pki.IssueLeaf("CN=second.test.local"); + pki.WritePem(certificate, key, second); + File.SetLastWriteTimeUtc(certificate, DateTime.UtcNow.AddSeconds(5)); + + Assert.True(source.Refresh(NullLogger.Instance)); + Assert.Equal(second.Thumbprint, source.ServerCertificate!.Thumbprint); + } + + [Fact] + public void AMissingCertificateStopsTheFirstLoadButABrokenReplacementKeepsTheCurrentOne() + { + var pfx = Path.Combine(directory, "server.pfx"); + var missing = new ProvidedTlsCertificate(FileSettings(pfx, password: Password)); + Assert.Throws(() => missing.Refresh(NullLogger.Instance)); + + using var leaf = pki.IssueLeaf(); + pki.WritePfx(pfx, leaf, Password); + var source = new ProvidedTlsCertificate(FileSettings(pfx, password: Password)); + source.Refresh(NullLogger.Instance); + + File.WriteAllText(pfx, "half-written"); + File.SetLastWriteTimeUtc(pfx, DateTime.UtcNow.AddSeconds(5)); + + Assert.False(source.Refresh(NullLogger.Instance)); + Assert.Equal(leaf.Thumbprint, source.ServerCertificate!.Thumbprint); + } + + [Fact] + public void AWrongPfxPasswordIsRefused() + { + using var leaf = pki.IssueLeaf(); + var pfx = Path.Combine(directory, "server.pfx"); + pki.WritePfx(pfx, leaf, Password); + + Assert.ThrowsAny( + () => new ProvidedTlsCertificate(FileSettings(pfx, password: "wrong")).Refresh(NullLogger.Instance)); + } + + [Fact] + public void AStoreCertificateIsFoundByThumbprintAndItsRenewalFollowed() + { + Assert.SkipUnless(OperatingSystem.IsWindows(), "The certificate store source is Windows only."); + using var first = Persisted(pki.IssueLeaf("CN=store.test.local", days: 60)); + using var renewal = Persisted(pki.IssueLeaf("CN=store.test.local", days: 120)); + using var store = new X509Store(StoreName.My, StoreLocation.CurrentUser); + store.Open(OpenFlags.ReadWrite); + store.Add(first); + try + { + // As copied from the certificate console: spaced, lower case, behind an invisible mark. + var copied = "\u200e" + string.Join(' ', first.Thumbprint.ToLowerInvariant().Chunk(2).Select(c => new string(c))); + var settings = new TlsSettings + { + Enabled = true, + Source = TlsCertificateSource.Store, + StoreThumbprint = TlsSettings.NormalizeThumbprint(copied), + DistributionDirectory = Distribution, + CertificateDirectory = directory, + UseWindowsStore = false, + }; + var source = new ProvidedTlsCertificate(settings, StoreLocation.CurrentUser); + source.Refresh(NullLogger.Instance); + Assert.Equal(first.Thumbprint, source.ServerCertificate!.Thumbprint); + + // Auto-enrollment renews: the new certificate arrives with the same subject and issuer, the old one goes. + store.Add(renewal); + store.Remove(first); + + Assert.True(source.Refresh(NullLogger.Instance)); + Assert.Equal(renewal.Thumbprint, source.ServerCertificate!.Thumbprint); + } + finally + { + foreach (var certificate in new[] { first, renewal }) + { + store.Remove(certificate); + if (OperatingSystem.IsWindows()) + { + (certificate.GetRSAPrivateKey() as System.Security.Cryptography.RSACng)?.Key.Delete(); + } + } + } + } + + [Fact] + public async Task KestrelSendsTheIntermediateAndSwapsInAReplacementWithoutARestart() + { + using var first = pki.IssueLeaf("CN=first.test.local"); + var pfx = Path.Combine(directory, "server.pfx"); + pki.WritePfx(pfx, first, Password); + var source = new ProvidedTlsCertificate(FileSettings(pfx, password: Password)); + source.Refresh(NullLogger.Instance); + + var builder = WebApplication.CreateBuilder(new WebApplicationOptions { ContentRootPath = directory }); + builder.Logging.ClearProviders(); + builder.WebHost.UseKestrel(kestrel => kestrel.ConfigureHttpsDefaults(https => ServerTls.UseSource(https, source))); + builder.WebHost.UseUrls("https://127.0.0.1:0"); + await using var app = builder.Build(); + app.MapGet("/", () => "ok"); + await app.StartAsync(); + var url = app.Services.GetRequiredService().Features.Get()!.Addresses.First(); + + // Trust only the root, as an agent that pinned it does: the path must be completed by what the server sent. + async Task ServedLeafAsync() + { + string? thumbprint = null; + using var handler = new HttpClientHandler + { + ServerCertificateCustomValidationCallback = (_, certificate, presented, _) => + { + using var chain = new X509Chain(); + chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust; + chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; + chain.ChainPolicy.CustomTrustStore.Add(pki.Root); + foreach (var element in presented!.ChainElements) + { + if (element.Certificate.Thumbprint != pki.Root.Thumbprint) + { + chain.ChainPolicy.ExtraStore.Add(element.Certificate); + } + } + + thumbprint = certificate!.Thumbprint; + return chain.Build(certificate); + }, + }; + using var http = new HttpClient(handler); + Assert.Equal("ok", await http.GetStringAsync(url)); + return thumbprint; + } + + Assert.Equal(first.Thumbprint, await ServedLeafAsync()); + + using var second = pki.IssueLeaf("CN=second.test.local"); + pki.WritePfx(pfx, second, Password); + File.SetLastWriteTimeUtc(pfx, DateTime.UtcNow.AddSeconds(5)); + source.Refresh(NullLogger.Instance); + + Assert.Equal(second.Thumbprint, await ServedLeafAsync()); + await app.StopAsync(); + } + + [Fact] + public void FromConfigurationReadsTheCertificateSettings() + { + var file = Settings(new() { ["TLS_CERT_FILE"] = @"C:\certs\server.pfx", ["TLS_CERT_PASSWORD"] = "pw", ["TLS_SELF_SIGNED"] = "0", ["DATA_PATH"] = directory }); + Assert.Equal((true, TlsCertificateSource.File, @"C:\certs\server.pfx", "pw"), (file.Enabled, file.Source, file.CertificateFile, file.CertificatePassword)); + Assert.Equal(Path.Combine(directory, "ca-certificates"), file.DistributionDirectory); + Assert.Null(file.DistributeCa); + + var pem = Settings(new() { ["TLS_CERT_SOURCE"] = "file", ["TLS_CERT_FILE"] = "/etc/ssl/fullchain.pem", ["TLS_CERT_KEY_FILE"] = "/etc/ssl/privkey.pem", ["TLS_DISTRIBUTE_CA"] = "0" }); + Assert.Equal(("/etc/ssl/privkey.pem", false), (pem.KeyFile, pem.DistributeCa)); + + var managed = Settings(new() { ["TLS_SELF_SIGNED"] = "0" }); + Assert.Equal((false, TlsCertificateSource.SelfSigned), (managed.Enabled, managed.Source)); + + Assert.Throws(() => Settings(new() { ["TLS_CERT_SOURCE"] = "file" })); + Assert.Throws(() => Settings(new() { ["TLS_CERT_SOURCE"] = "acme" })); + if (OperatingSystem.IsWindows()) + { + var store = Settings(new() { ["TLS_CERT_THUMBPRINT"] = "ab cd ef 01" }); + Assert.Equal((TlsCertificateSource.Store, "ABCDEF01"), (store.Source, store.StoreThumbprint)); + } + else + { + Assert.Throws(() => Settings(new() { ["TLS_CERT_THUMBPRINT"] = "abcdef01" })); + } + } + + private static TlsSettings Settings(Dictionary values) + => TlsSettings.FromConfiguration(new ConfigurationBuilder().AddInMemoryCollection(values).Build()); + + /// A key the store can keep (an ephemeral one cannot be added). + private static X509Certificate2 Persisted(X509Certificate2 certificate) + { + using (certificate) + { + return X509CertificateLoader.LoadPkcs12(certificate.Export(X509ContentType.Pfx, "x"), "x", X509KeyStorageFlags.UserKeySet | X509KeyStorageFlags.PersistKeySet); + } + } +} diff --git a/tests/Backend.Tests/SelfSignedTlsTests.cs b/tests/Backend.Tests/SelfSignedTlsTests.cs index c8b30d9..a6f2128 100644 --- a/tests/Backend.Tests/SelfSignedTlsTests.cs +++ b/tests/Backend.Tests/SelfSignedTlsTests.cs @@ -247,4 +247,38 @@ public sealed class SelfSignedTlsTests : IDisposable Assert.DoesNotContain(IPAddress.IPv6Any, settings.IpAddresses); Assert.DoesNotContain("wildcard", settings.DnsNames); } + + [Fact] + public void EnsureChain_WritesRootAndIntermediateForDistributionButNotTheLeaf() + { + var distribution = Path.Combine(directory, "ca-certificates"); + var manager = new SelfSignedTlsManager(new TlsSettings + { + Enabled = true, + CertificateDirectory = directory, + DistributionDirectory = distribution, + UseWindowsStore = false, + DnsNames = ["localhost"], + }); + manager.EnsureChain(NullLogger.Instance); + + using var root = X509CertificateLoader.LoadCertificateFromFile(Path.Combine(distribution, "root-ca.cer")); + using var intermediate = X509CertificateLoader.LoadCertificateFromFile(Path.Combine(distribution, "intermediate-ca.cer")); + Assert.Equal(TlsSubjects.Root, root.Subject); + Assert.Equal(TlsSubjects.Intermediate, intermediate.Subject); + Assert.False(root.HasPrivateKey); + Assert.Equal(manager.CaFingerprint(), ServerTls.Fingerprint(root)); + Assert.Equal(manager.CaChainPem(), File.ReadAllText(Path.Combine(distribution, "ca-chain.pem"))); + foreach (var file in Directory.GetFiles(distribution)) + { + var text = File.ReadAllText(file); + Assert.DoesNotContain("PRIVATE KEY", text); + Assert.DoesNotContain(manager.ServerCertificate!.ExportCertificatePem(), text); + } + + // The handshake carries the intermediate too (the platform may add the root, never the leaf). + var sent = manager.CertificateContext!.IntermediateCertificates.Select(c => c.Thumbprint).ToList(); + Assert.Equal(intermediate.Thumbprint, sent[0]); + Assert.DoesNotContain(manager.ServerCertificate!.Thumbprint, sent); + } } diff --git a/tests/Backend.Tests/TestPki.cs b/tests/Backend.Tests/TestPki.cs new file mode 100644 index 0000000..25f9530 --- /dev/null +++ b/tests/Backend.Tests/TestPki.cs @@ -0,0 +1,85 @@ +using System.Net; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; + +namespace Backend.Tests; + +/// A private CA (root > intermediate) issuing TLS server leaves, as an enterprise PKI would. +internal sealed class TestPki : IDisposable +{ + public TestPki(string name = "Test Enterprise") + { + var now = DateTimeOffset.UtcNow; + using var rootKey = RSA.Create(2048); + var rootRequest = new CertificateRequest($"CN={name} Root CA", rootKey, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); + rootRequest.CertificateExtensions.Add(new X509BasicConstraintsExtension(true, false, 0, true)); + rootRequest.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.KeyCertSign | X509KeyUsageFlags.CrlSign, true)); + rootRequest.CertificateExtensions.Add(new X509SubjectKeyIdentifierExtension(rootRequest.PublicKey, false)); + Root = rootRequest.CreateSelfSigned(now.AddDays(-1), now.AddYears(10)); + + using var intermediateKey = RSA.Create(2048); + var intermediateRequest = new CertificateRequest($"CN={name} Issuing CA", intermediateKey, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); + intermediateRequest.CertificateExtensions.Add(new X509BasicConstraintsExtension(true, true, 0, true)); + intermediateRequest.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.KeyCertSign | X509KeyUsageFlags.CrlSign, true)); + intermediateRequest.CertificateExtensions.Add(new X509SubjectKeyIdentifierExtension(intermediateRequest.PublicKey, false)); + intermediateRequest.CertificateExtensions.Add(X509AuthorityKeyIdentifierExtension.CreateFromCertificate(Root, true, false)); + using var issued = intermediateRequest.Create(Root, now.AddDays(-1), now.AddYears(5), Serial()); + Intermediate = issued.CopyWithPrivateKey(intermediateKey); + } + + public X509Certificate2 Root { get; } + + public X509Certificate2 Intermediate { get; } + + public string RootFingerprint => Convert.ToHexStringLower(SHA256.HashData(Root.PublicKey.ExportSubjectPublicKeyInfo())); + + /// A server leaf for localhost/127.0.0.1 with its private key. + public X509Certificate2 IssueLeaf(string subject = "CN=executer.test.local", int days = 90) + { + var now = DateTimeOffset.UtcNow; + using var key = RSA.Create(2048); + var request = new CertificateRequest(subject, key, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); + request.CertificateExtensions.Add(new X509BasicConstraintsExtension(false, false, 0, true)); + request.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.KeyEncipherment, true)); + request.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension([new Oid("1.3.6.1.5.5.7.3.1")], false)); + var san = new SubjectAlternativeNameBuilder(); + san.AddDnsName("localhost"); + san.AddIpAddress(IPAddress.Loopback); + request.CertificateExtensions.Add(san.Build()); + request.CertificateExtensions.Add(X509AuthorityKeyIdentifierExtension.CreateFromCertificate(Intermediate, true, false)); + using var issued = request.Create(Intermediate, now.AddMinutes(-5), now.AddDays(days), Serial()); + return issued.CopyWithPrivateKey(key); + } + + /// The leaf, intermediate and root in one password-protected PFX, as a CA export gives it. + public void WritePfx(string path, X509Certificate2 leaf, string password) + { + X509Certificate2Collection bundle = + [ + leaf, + X509CertificateLoader.LoadCertificate(Intermediate.RawData), + X509CertificateLoader.LoadCertificate(Root.RawData), + ]; + File.WriteAllBytes(path, bundle.Export(X509ContentType.Pkcs12, password)!); + } + + /// fullchain.pem (leaf, intermediate, root) and a separate key, as certbot or an ACME client lays them out. + public void WritePem(string certificatePath, string keyPath, X509Certificate2 leaf) + { + File.WriteAllText(certificatePath, leaf.ExportCertificatePem() + "\n" + Intermediate.ExportCertificatePem() + "\n" + Root.ExportCertificatePem() + "\n"); + File.WriteAllText(keyPath, leaf.GetRSAPrivateKey()!.ExportPkcs8PrivateKeyPem()); + } + + public void Dispose() + { + Root.Dispose(); + Intermediate.Dispose(); + } + + private static byte[] Serial() + { + var serial = RandomNumberGenerator.GetBytes(16); + serial[0] &= 0x7F; + return serial; + } +} -- 2.52.0 From 5a0bd1a7ea332f32694b5847f7fe545667c71346232814017ad7125f32dd02b9 Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Wed, 4 Nov 2026 20:42:39 -0500 Subject: [PATCH 13/22] feat(agent): validate every channel first, then the CA pinned at enrollment First contact keeps only the root matching the token's fingerprint and the CAs that chain to it, so an extra root in the response can no longer reach the trust store. The verified chain is kept as the agent's anchors and backs the HTTPS API, downloads, upgrades, the CA refresh and the NATS WebSocket when OS validation fails on an untrusted chain. Co-Authored-By: Claude Opus 5.5 --- src/Agent/Enrollment/CredentialRenewal.cs | 7 +- src/Agent/Enrollment/EnrollmentService.cs | 11 +- src/Agent/Enrollment/ServerTrust.cs | 69 +++-- src/Agent/Enrollment/ServerTrustAnchors.cs | 205 +++++++++++++++ src/Agent/Files/ServerDownloads.cs | 15 +- .../Messaging/AgentNatsOptionsFactory.cs | 16 +- .../src/sections/devices/AddDeviceDialog.tsx | 2 +- tests/Agent.Tests/ServerTrustAnchorsTests.cs | 237 ++++++++++++++++++ 8 files changed, 509 insertions(+), 53 deletions(-) create mode 100644 src/Agent/Enrollment/ServerTrustAnchors.cs create mode 100644 tests/Agent.Tests/ServerTrustAnchorsTests.cs diff --git a/src/Agent/Enrollment/CredentialRenewal.cs b/src/Agent/Enrollment/CredentialRenewal.cs index 32cd448..5fff827 100644 --- a/src/Agent/Enrollment/CredentialRenewal.cs +++ b/src/Agent/Enrollment/CredentialRenewal.cs @@ -352,11 +352,8 @@ public sealed class CredentialRenewal(CredentialRenewalContext context, ILogger return create(skipVerify); } - var handler = new HttpClientHandler(); - if (skipVerify) - { - handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; - } + // The OS trust store, then the CA pinned at enrollment (ServerTrustAnchors). + var handler = ServerTrustAnchors.Default.CreateHandler(skipVerify); return handler; } diff --git a/src/Agent/Enrollment/EnrollmentService.cs b/src/Agent/Enrollment/EnrollmentService.cs index 4a3c6b0..248a4c4 100644 --- a/src/Agent/Enrollment/EnrollmentService.cs +++ b/src/Agent/Enrollment/EnrollmentService.cs @@ -24,8 +24,11 @@ public sealed class EnrollmentService( RuntimeStateStore runtimeState, IHardwareIdentitySource hardwareIdentity, IInventoryCollector inventory, - ILogger logger) + ILogger logger, + ServerTrustAnchors? anchors = null) { + private readonly ServerTrustAnchors trustAnchors = anchors ?? ServerTrustAnchors.Default; + /// /// Returns valid credentials, either loaded from the protected store or /// obtained by bootstrapping. Null when enrollment is currently impossible @@ -154,7 +157,9 @@ public sealed class EnrollmentService( { try { - using var validating = new HttpClientHandler(); + // Validation as every channel does it: the OS trust store, then the + // CA pinned at an earlier enrollment. + using var validating = trustAnchors.CreateHandler(skipVerify: false); response = await PostBootstrapAsync(validating, endpoint, request, cancellationToken); } catch (HttpRequestException ex) when (ex.HttpRequestError == HttpRequestError.SecureConnectionError) @@ -168,7 +173,7 @@ public sealed class EnrollmentService( } var (status, pinnedChain) = await ServerTrust.FetchAndInstallCaAsync( - serverUrl, HttpClient.DefaultProxy, logger, expectedFingerprint, cancellationToken); + serverUrl, HttpClient.DefaultProxy, logger, expectedFingerprint, cancellationToken, trustAnchors); if (status != ServerCaStatus.Installed) { logger.LogError("Could not anchor server trust ({Status}); refusing to enroll", status); diff --git a/src/Agent/Enrollment/ServerTrust.cs b/src/Agent/Enrollment/ServerTrust.cs index e556f23..e120604 100644 --- a/src/Agent/Enrollment/ServerTrust.cs +++ b/src/Agent/Enrollment/ServerTrust.cs @@ -27,13 +27,16 @@ public enum ServerCaStatus } /// -/// Makes the server's managed self-signed CA available to the OS trust store -/// so ordinary system validation succeeds when the agent connects to the -/// self-signed server directly (e.g. a local URL). It is intentionally NOT +/// Makes the server's CA (its managed self-signed chain, or the private CA +/// behind a certificate the administrator provided) available to the OS trust +/// store so ordinary system validation succeeds when the agent connects to the +/// server directly (e.g. a local URL), and keeps it as the agent's own anchors +/// () for every channel. It is intentionally NOT /// pinned or forced: if a reverse proxy with a real certificate is added /// later, system validation trusts that instead, and the installed /// self-signed CA simply goes unused — nothing to break. Certs come from -/// GET /api/enrollment/ca; a 404 means the server is not self-signed. +/// GET /api/enrollment/ca; a 404 means there is nothing to distribute (a +/// publicly trusted certificate, or plain HTTP). /// public static class ServerTrust { @@ -47,7 +50,8 @@ public static class ServerTrust /// it directly, independent of OS trust-store timing). /// public static async Task<(ServerCaStatus Status, X509Certificate2Collection Chain)> FetchAndInstallCaAsync( - ServerUrl serverUrl, IWebProxy? proxy, ILogger logger, string expectedFingerprint, CancellationToken cancellationToken) + ServerUrl serverUrl, IWebProxy? proxy, ILogger logger, string expectedFingerprint, CancellationToken cancellationToken, + ServerTrustAnchors? anchors = null) { using var handler = new HttpClientHandler { @@ -88,17 +92,31 @@ public static class ServerTrust // Pin on the public key (SubjectPublicKeyInfo), not the certificate, so it // matches a root renewed in place (same key, re-issued cert). Only the root - // carries this key — the intermediate and leaf have their own. - var expected = expectedFingerprint.Trim().ToLowerInvariant(); - var matched = chain.Cast() - .Any(c => Convert.ToHexStringLower(SHA256.HashData(c.PublicKey.ExportSubjectPublicKeyInfo())) == expected); - if (!matched) + // carries this key — the intermediate and leaf have their own. Only that + // root and the CAs that chain to it are kept: the response came over an + // unverified connection, so anything else in it (an extra root added on + // the path) must never reach the trust store. + var pinned = ServerTrustAnchors.PinnedSubset(chain, expectedFingerprint); + if (pinned.Count == 0) { logger.LogError( "Fetched server CA does not match the fingerprint pinned in the bootstrap token; refusing to trust (possible MITM)"); return (ServerCaStatus.FingerprintMismatch, []); } + if (pinned.Count < chain.Count) + { + logger.LogWarning("Ignored {Count} certificate(s) in the server CA response that do not chain to the pinned root", chain.Count - pinned.Count); + } + + Install(pinned, logger); + (anchors ?? ServerTrustAnchors.Default).Save(pinned, logger); + return (ServerCaStatus.Installed, pinned); + } + + private static void Install(X509Certificate2Collection chain, ILogger logger) + { + var pem = string.Concat(chain.Cast().Select(c => c.ExportCertificatePem() + '\n')); if (OperatingSystem.IsWindows()) { InstallWindows(chain, logger); @@ -111,8 +129,6 @@ public static class ServerTrust { InstallLinux(pem, logger); } - - return (ServerCaStatus.Installed, chain); } /// @@ -121,13 +137,18 @@ public static class ServerTrust /// connection — a root renewed in place (same key, longer validity) or a new /// intermediate — is authentic and installed. No pin here: the validated /// transport, not a fingerprint, authenticates it (first contact was pinned at - /// enrollment). A 404 (real certificate) or a validation failure is a harmless - /// no-op. Returns true when a chain was installed. + /// enrollment). The connection validates like every other channel: the OS + /// trust store first, then the anchors kept at enrollment, so a CA that was + /// taken out of the trust store is put back here. A 404 (real certificate) + /// or a validation failure is a harmless no-op. Returns true when a chain + /// was installed. /// public static async Task RefreshCaAsync( - ServerUrl serverUrl, IWebProxy? proxy, ILogger logger, CancellationToken cancellationToken) + ServerUrl serverUrl, IWebProxy? proxy, ILogger logger, CancellationToken cancellationToken, + ServerTrustAnchors? anchors = null) { - using var handler = new HttpClientHandler(); + anchors ??= ServerTrustAnchors.Default; + using var handler = anchors.CreateHandler(skipVerify: false); if (proxy is not null) { handler.Proxy = proxy; @@ -162,19 +183,15 @@ public static class ServerTrust return false; } - if (OperatingSystem.IsWindows()) + X509Certificate2Collection authorities = [.. chain.Cast().Where(c => c.Extensions.OfType().FirstOrDefault() is { CertificateAuthority: true } + || ServerTrustAnchors.IsSelfIssued(c))]; + if (authorities.Count == 0) { - InstallWindows(chain, logger); - } - else if (OperatingSystem.IsMacOS()) - { - InstallMac(pem, chain, logger); - } - else - { - InstallLinux(pem, logger); + return false; } + Install(authorities, logger); + anchors.Save(authorities, logger); return true; } diff --git a/src/Agent/Enrollment/ServerTrustAnchors.cs b/src/Agent/Enrollment/ServerTrustAnchors.cs new file mode 100644 index 0000000..8ad05f2 --- /dev/null +++ b/src/Agent/Enrollment/ServerTrustAnchors.cs @@ -0,0 +1,205 @@ +using System.Linq; +using System.Net.Http; +using System.Net.Security; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using Microsoft.Extensions.Logging; +using Shared.Infrastructure.Constants; + +namespace Agent.Enrollment; + +/// +/// The server CA this agent verified when it enrolled (pinned to the token's +/// fingerprint), kept in the agent's protected data folder so every channel +/// to the server — the HTTPS API, downloads, upgrades, the NATS WebSocket — +/// validates the same way: normal OS validation first, and only when that +/// fails on an untrusted chain, a chain that ends at one of these anchors. +/// The OS trust store holds the same CA (installed at enrollment), so this +/// only matters when that install did not take or was undone (a trust-store +/// policy, an unknown Linux layout), and it keeps the agent connected instead +/// of stranded. Name mismatches and missing certificates are never accepted. +/// +public sealed class ServerTrustAnchors(string path) +{ + public const string FileName = "server-ca.pem"; + + private readonly Lock gate = new(); + private X509Certificate2Collection anchors = []; + private DateTime loadedStamp = DateTime.MinValue; + + /// The agent's own anchors, %ProgramData%\ExecuteR\server-ca.pem (/var/lib/executer elsewhere). + public static ServerTrustAnchors Default { get; } = new(Path.Combine(Branding.AgentProgramDataDirectory, FileName)); + + public string FilePath => path; + + /// The persisted anchors (reloaded when the file changes); empty when none. + public X509Certificate2Collection Load() + { + lock (gate) + { + try + { + var stamp = File.Exists(path) ? File.GetLastWriteTimeUtc(path) : DateTime.MinValue; + if (stamp != loadedStamp) + { + X509Certificate2Collection fresh = []; + if (stamp != DateTime.MinValue) + { + fresh.ImportFromPemFile(path); + } + + anchors = fresh; + loadedStamp = stamp; + } + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or CryptographicException) + { + anchors = []; + } + + return anchors; + } + } + + /// Replaces the anchors with 's CA certificates (public certificates only). + public void Save(IEnumerable chain, ILogger logger) + { + var pem = string.Concat(chain.Where(IsCertificateAuthority).Select(c => c.ExportCertificatePem() + '\n')); + if (pem.Length == 0) + { + return; + } + + try + { + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + if (File.Exists(path) && File.ReadAllText(path) == pem) + { + return; + } + + File.WriteAllText(path, pem); + lock (gate) + { + loadedStamp = DateTime.MinValue; + } + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + logger.LogWarning(ex, "Could not keep the server CA in {Path}; the OS trust store alone validates the server", path); + } + } + + /// + /// Normal validation's verdict when it passed; otherwise, for an untrusted + /// chain only, whether the certificate chains to a pinned anchor. + /// + public bool Validate(X509Certificate2? certificate, X509Chain? presented, SslPolicyErrors errors) + { + if (errors == SslPolicyErrors.None) + { + return true; + } + + if ((errors & ~SslPolicyErrors.RemoteCertificateChainErrors) != 0 || certificate is null) + { + return false; + } + + var known = Load(); + return known.Count > 0 && ChainsTo(certificate, known, presented); + } + + /// For HttpClientHandler.ServerCertificateCustomValidationCallback. + public bool ValidateHttp(HttpRequestMessage request, X509Certificate2? certificate, X509Chain? chain, SslPolicyErrors errors) + => Validate(certificate, chain, errors); + + /// For ClientWebSocketOptions / SslStream RemoteCertificateValidationCallback. + public bool ValidateSocket(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors errors) + => Validate(certificate as X509Certificate2 ?? (certificate is null ? null : X509CertificateLoader.LoadCertificate(certificate.GetRawCertData())), chain, errors); + + /// A handler for the server: accept-any with TlsSkipVerify, else validation with this fallback. + public HttpClientHandler CreateHandler(bool skipVerify) + => new() + { + ServerCertificateCustomValidationCallback = skipVerify + ? HttpClientHandler.DangerousAcceptAnyServerCertificateValidator + : ValidateHttp, + }; + + /// + /// Custom-root validation against : self-issued + /// certificates are the anchors, the rest (and what the server presented) + /// only help build the path. + /// + public static bool ChainsTo(X509Certificate2 certificate, X509Certificate2Collection trusted, X509Chain? presented = null) + { + using var chain = new X509Chain(); + chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust; + chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; + foreach (var ca in trusted) + { + if (IsSelfIssued(ca)) + { + chain.ChainPolicy.CustomTrustStore.Add(ca); + } + else + { + chain.ChainPolicy.ExtraStore.Add(ca); + } + } + + if (presented is not null) + { + foreach (var element in presented.ChainElements) + { + chain.ChainPolicy.ExtraStore.Add(element.Certificate); + } + } + + return chain.ChainPolicy.CustomTrustStore.Count > 0 && chain.Build(certificate); + } + + /// + /// What may be trusted from a fetched chain once the root matching + /// is found: that root, and the CA + /// certificates that chain to it. Anything else in the response — an extra + /// root slipped in by someone on the path, a leaf — is dropped, so it can + /// never become a trust anchor. Empty when no self-issued certificate + /// matches. + /// + public static X509Certificate2Collection PinnedSubset(X509Certificate2Collection fetched, string fingerprint) + { + var expected = fingerprint.Trim().ToLowerInvariant(); + var root = fetched.Cast() + .FirstOrDefault(c => IsSelfIssued(c) && Convert.ToHexStringLower(SHA256.HashData(c.PublicKey.ExportSubjectPublicKeyInfo())) == expected); + if (root is null) + { + return []; + } + + X509Certificate2Collection anchor = [root]; + X509Certificate2Collection result = [root]; + foreach (var candidate in fetched.Cast().Where(c => c.Thumbprint != root.Thumbprint && IsCertificateAuthority(c) && !IsSelfIssued(c))) + { + using var chain = new X509Chain(); + chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust; + chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; + chain.ChainPolicy.CustomTrustStore.AddRange(anchor); + chain.ChainPolicy.ExtraStore.AddRange(fetched); + if (chain.Build(candidate)) + { + result.Add(candidate); + } + } + + return result; + } + + public static bool IsSelfIssued(X509Certificate2 certificate) + => certificate.SubjectName.RawData.AsSpan().SequenceEqual(certificate.IssuerName.RawData); + + private static bool IsCertificateAuthority(X509Certificate2 certificate) + => certificate.Extensions.OfType().FirstOrDefault() is { CertificateAuthority: true } + || (IsSelfIssued(certificate) && !certificate.Extensions.OfType().Any()); +} diff --git a/src/Agent/Files/ServerDownloads.cs b/src/Agent/Files/ServerDownloads.cs index 1126f65..97f5c74 100644 --- a/src/Agent/Files/ServerDownloads.cs +++ b/src/Agent/Files/ServerDownloads.cs @@ -5,8 +5,9 @@ namespace Agent.Files; /// /// Where a download a command names comes from, and how it is fetched. A server path goes to the server this agent /// already talks to, so a link from the built-in S3 store needs no address of its own. An absolute URL is used as is. -/// Trust is the same as enrollment's: OS validation (a self-signed server's CA was installed into the OS store when -/// the agent enrolled), or accept-any when TlsSkipVerify is configured. +/// Trust is the same as enrollment's: OS validation (the server's CA was installed into the OS store when the agent +/// enrolled), then the CA pinned at enrollment (), or accept-any when +/// TlsSkipVerify is configured. /// public sealed class ServerDownloads(ServerUrl? serverUrl, bool tlsSkipVerify) { @@ -39,15 +40,7 @@ public sealed class ServerDownloads(ServerUrl? serverUrl, bool tlsSkipVerify) } public static HttpMessageHandler CreateHandler(bool tlsSkipVerify) - { - var handler = new HttpClientHandler(); - if (tlsSkipVerify) - { - handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; - } - - return handler; - } + => Agent.Enrollment.ServerTrustAnchors.Default.CreateHandler(tlsSkipVerify); /// A link's query is its authorization, which has no place in a job's output. private static string RedactQuery(string url) => url.Split('?')[0]; diff --git a/src/Agent/Messaging/AgentNatsOptionsFactory.cs b/src/Agent/Messaging/AgentNatsOptionsFactory.cs index ae38ee3..90d1a90 100644 --- a/src/Agent/Messaging/AgentNatsOptionsFactory.cs +++ b/src/Agent/Messaging/AgentNatsOptionsFactory.cs @@ -18,7 +18,8 @@ public static class AgentNatsOptionsFactory AgentCredentials credentials, AgentConfiguration configuration, ILoggerFactory loggerFactory, - Func? current = null) + Func? current = null, + ServerTrustAnchors? trustAnchors = null) { var options = NatsOpts.Default with { @@ -44,9 +45,11 @@ public static class AgentNatsOptionsFactory }; // TlsSkipVerify accepts any cert (still encrypted); otherwise the OS - // trust store validates the wss server cert — which trusts the - // self-signed CA the agent auto-installs at enrollment. + // trust store validates the wss server cert — which trusts the CA the + // agent auto-installs at enrollment — and, should that fail, the CA + // pinned at enrollment does (ServerTrustAnchors), as for HTTPS. var skipVerify = configuration.TlsSkipVerify.Value; + var anchors = trustAnchors ?? ServerTrustAnchors.Default; if (skipVerify) { options = options with { TlsOpts = NatsTlsOpts.Default with { InsecureSkipVerify = true } }; @@ -63,10 +66,9 @@ public static class AgentNatsOptionsFactory // HttpClient.DefaultProxy on its own. clientOptions.Proxy = System.Net.Http.HttpClient.DefaultProxy; - if (skipVerify) - { - clientOptions.RemoteCertificateValidationCallback = (_, _, _, _) => true; - } + clientOptions.RemoteCertificateValidationCallback = skipVerify + ? (_, _, _, _) => true + : anchors.ValidateSocket; return ValueTask.CompletedTask; }, diff --git a/src/WebUI/src/sections/devices/AddDeviceDialog.tsx b/src/WebUI/src/sections/devices/AddDeviceDialog.tsx index 0505c05..123fa54 100644 --- a/src/WebUI/src/sections/devices/AddDeviceDialog.tsx +++ b/src/WebUI/src/sections/devices/AddDeviceDialog.tsx @@ -277,7 +277,7 @@ function AddDeviceContent({ open, onClose }: Props) { serverInfo?.fromEnvironment ? 'Set by PUBLIC_SERVER_URL.' : 'Derived from this request (and trusted forwarded headers). Set PUBLIC_SERVER_URL when devices reach the server under a different address.' - }${selfSignedDirect ? ' The server uses its self-signed certificate, so the command skips TLS verification for the download.' : ''}`} + }${selfSignedDirect ? " Devices may not trust this server's certificate yet, so the command accepts it to fetch the install script; the script and the agent then check the server's CA against the token." : ''}`} /> )} diff --git a/tests/Agent.Tests/ServerTrustAnchorsTests.cs b/tests/Agent.Tests/ServerTrustAnchorsTests.cs new file mode 100644 index 0000000..6ce4fe7 --- /dev/null +++ b/tests/Agent.Tests/ServerTrustAnchorsTests.cs @@ -0,0 +1,237 @@ +using System.Net; +using System.Net.Security; +using System.Net.Sockets; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using Agent.Enrollment; +using Microsoft.Extensions.Logging.Abstractions; +using Xunit; + +namespace Agent.Tests; + +/// +/// Every channel validates the server the same way: the OS trust store first, and only on an untrusted chain the CA +/// pinned at enrollment; first contact keeps only the pinned root and what chains to it. +/// +public sealed class ServerTrustAnchorsTests : IDisposable +{ + private readonly string directory = Path.Combine(Path.GetTempPath(), "executer-anchor-tests", Guid.NewGuid().ToString("N")); + private readonly Pki pki = new("Anchor Test"); + + public ServerTrustAnchorsTests() => Directory.CreateDirectory(directory); + + private ServerTrustAnchors Anchors() => new(Path.Combine(directory, ServerTrustAnchors.FileName)); + + public void Dispose() + { + pki.Dispose(); + try + { + Directory.Delete(directory, recursive: true); + } + catch (IOException) + { + } + } + + [Fact] + public void FirstContactKeepsThePinnedRootAndWhatChainsToItOnly() + { + using var rogue = new Pki("Rogue"); + using var leaf = pki.IssueLeaf(); + X509Certificate2Collection fetched = [Public(pki.Intermediate), Public(pki.Root), Public(rogue.Root), Public(rogue.Intermediate), Public(leaf)]; + + var kept = ServerTrustAnchors.PinnedSubset(fetched, pki.RootFingerprint.ToUpperInvariant()); + + Assert.Equal([pki.Root.Thumbprint, pki.Intermediate.Thumbprint], kept.Select(c => c.Thumbprint)); + } + + [Fact] + public void FirstContactKeepsNothingWhenNoRootMatchesTheToken() + { + using var rogue = new Pki("Rogue"); + X509Certificate2Collection fetched = [Public(rogue.Intermediate), Public(rogue.Root)]; + + Assert.Empty(ServerTrustAnchors.PinnedSubset(fetched, pki.RootFingerprint)); + } + + [Fact] + public void TheIntermediateAloneCannotStandInForThePinnedRoot() + { + // A fingerprint that names a non-root certificate anchors nothing: the pin is the root's key. + X509Certificate2Collection fetched = [Public(pki.Intermediate), Public(pki.Root)]; + var intermediateFingerprint = Convert.ToHexStringLower(SHA256.HashData(pki.Intermediate.PublicKey.ExportSubjectPublicKeyInfo())); + + Assert.Empty(ServerTrustAnchors.PinnedSubset(fetched, intermediateFingerprint)); + } + + [Fact] + public void AnUntrustedChainIsAcceptedOnlyThroughThePinnedAnchors() + { + using var leaf = pki.IssueLeaf(); + using var other = new Pki("Other"); + using var stranger = other.IssueLeaf(); + var anchors = Anchors(); + + Assert.False(anchors.Validate(leaf, null, SslPolicyErrors.RemoteCertificateChainErrors), "nothing pinned yet"); + + anchors.Save([Public(pki.Intermediate), Public(pki.Root), Public(leaf)], NullLogger.Instance); + + Assert.True(anchors.Validate(leaf, null, SslPolicyErrors.RemoteCertificateChainErrors)); + Assert.False(anchors.Validate(stranger, null, SslPolicyErrors.RemoteCertificateChainErrors)); + Assert.False(anchors.Validate(leaf, null, SslPolicyErrors.RemoteCertificateChainErrors | SslPolicyErrors.RemoteCertificateNameMismatch)); + Assert.False(anchors.Validate(null, null, SslPolicyErrors.RemoteCertificateNotAvailable)); + Assert.True(anchors.Validate(stranger, null, SslPolicyErrors.None), "what the OS trusts is never second-guessed"); + Assert.True(anchors.ValidateSocket(this, (X509Certificate)Public(leaf), null, SslPolicyErrors.RemoteCertificateChainErrors)); + } + + [Fact] + public void OnlyCaCertificatesAreKeptAndAReplacementIsReadBack() + { + using var leaf = pki.IssueLeaf(); + var anchors = Anchors(); + anchors.Save([Public(pki.Intermediate), Public(pki.Root), Public(leaf)], NullLogger.Instance); + + Assert.Equal([pki.Intermediate.Thumbprint, pki.Root.Thumbprint], anchors.Load().Select(c => c.Thumbprint)); + Assert.DoesNotContain("PRIVATE KEY", File.ReadAllText(anchors.FilePath)); + + using var next = new Pki("Next"); + anchors.Save([Public(next.Root)], NullLogger.Instance); + File.SetLastWriteTimeUtc(anchors.FilePath, DateTime.UtcNow.AddSeconds(5)); + + Assert.Equal([next.Root.Thumbprint], anchors.Load().Select(c => c.Thumbprint)); + } + + [Fact] + public async Task AnHttpsChannelValidatesThroughThePinnedCaWhenTheOsDoesNotTrustIt() + { + using var leaf = pki.IssueLeaf(); + using var server = new TlsServer(leaf, pki.Intermediate); + var anchors = Anchors(); + + using (var unpinned = new HttpClient(anchors.CreateHandler(skipVerify: false))) + { + await Assert.ThrowsAsync(() => unpinned.GetStringAsync(server.Url)); + } + + anchors.Save([Public(pki.Intermediate), Public(pki.Root)], NullLogger.Instance); + using var pinned = new HttpClient(anchors.CreateHandler(skipVerify: false)); + + Assert.Equal("ok", await pinned.GetStringAsync(server.Url)); + } + + private static X509Certificate2 Public(X509Certificate2 certificate) => X509CertificateLoader.LoadCertificate(certificate.RawData); + + /// One HTTPS answer per connection, over SslStream, serving the leaf and its intermediate. + private sealed class TlsServer : IDisposable + { + private readonly TcpListener listener = new(IPAddress.Loopback, 0); + private readonly CancellationTokenSource stop = new(); + private readonly SslStreamCertificateContext context; + + public TlsServer(X509Certificate2 leaf, X509Certificate2 intermediate) + { + // SChannel needs a key it can use (not an ephemeral one). + var usable = X509CertificateLoader.LoadPkcs12(leaf.Export(X509ContentType.Pfx), null, X509KeyStorageFlags.Exportable); + context = SslStreamCertificateContext.Create(usable, [Public(intermediate)], offline: true); + listener.Start(); + Url = $"https://127.0.0.1:{((IPEndPoint)listener.LocalEndpoint).Port}/"; + _ = Task.Run(AcceptAsync); + } + + public string Url { get; } + + private async Task AcceptAsync() + { + while (!stop.IsCancellationRequested) + { + TcpClient client; + try + { + client = await listener.AcceptTcpClientAsync(stop.Token); + } + catch (Exception) + { + return; + } + + _ = Task.Run(async () => + { + using (client) + { + try + { + await using var tls = new SslStream(client.GetStream()); + await tls.AuthenticateAsServerAsync(new SslServerAuthenticationOptions { ServerCertificateContext = context }, stop.Token); + var buffer = new byte[4096]; + _ = await tls.ReadAsync(buffer, stop.Token); + await tls.WriteAsync(Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok"), stop.Token); + } + catch (Exception) + { + // A client that refused the certificate. + } + } + }); + } + } + + public void Dispose() + { + stop.Cancel(); + listener.Stop(); + } + } + + /// A private root > intermediate issuing server leaves. + private sealed class Pki : IDisposable + { + public Pki(string name) + { + var now = DateTimeOffset.UtcNow; + using var rootKey = ECDsa.Create(ECCurve.NamedCurves.nistP256); + var rootRequest = new CertificateRequest($"CN={name} Root", rootKey, HashAlgorithmName.SHA256); + rootRequest.CertificateExtensions.Add(new X509BasicConstraintsExtension(true, false, 0, true)); + rootRequest.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.KeyCertSign, true)); + rootRequest.CertificateExtensions.Add(new X509SubjectKeyIdentifierExtension(rootRequest.PublicKey, false)); + Root = rootRequest.CreateSelfSigned(now.AddDays(-1), now.AddYears(5)); + + using var intermediateKey = ECDsa.Create(ECCurve.NamedCurves.nistP256); + var intermediateRequest = new CertificateRequest($"CN={name} Intermediate", intermediateKey, HashAlgorithmName.SHA256); + intermediateRequest.CertificateExtensions.Add(new X509BasicConstraintsExtension(true, true, 0, true)); + intermediateRequest.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.KeyCertSign, true)); + intermediateRequest.CertificateExtensions.Add(new X509SubjectKeyIdentifierExtension(intermediateRequest.PublicKey, false)); + intermediateRequest.CertificateExtensions.Add(X509AuthorityKeyIdentifierExtension.CreateFromCertificate(Root, true, false)); + using var issued = intermediateRequest.Create(Root, now.AddDays(-1), now.AddYears(2), RandomNumberGenerator.GetBytes(12)); + Intermediate = issued.CopyWithPrivateKey(intermediateKey); + } + + public X509Certificate2 Root { get; } + + public X509Certificate2 Intermediate { get; } + + public string RootFingerprint => Convert.ToHexStringLower(SHA256.HashData(Root.PublicKey.ExportSubjectPublicKeyInfo())); + + public X509Certificate2 IssueLeaf() + { + var now = DateTimeOffset.UtcNow; + using var key = ECDsa.Create(ECCurve.NamedCurves.nistP256); + var request = new CertificateRequest("CN=executer-server", key, HashAlgorithmName.SHA256); + request.CertificateExtensions.Add(new X509BasicConstraintsExtension(false, false, 0, true)); + request.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension([new Oid("1.3.6.1.5.5.7.3.1")], false)); + var san = new SubjectAlternativeNameBuilder(); + san.AddIpAddress(IPAddress.Loopback); + request.CertificateExtensions.Add(san.Build()); + request.CertificateExtensions.Add(X509AuthorityKeyIdentifierExtension.CreateFromCertificate(Intermediate, true, false)); + using var issued = request.Create(Intermediate, now.AddMinutes(-5), now.AddDays(30), RandomNumberGenerator.GetBytes(12)); + return issued.CopyWithPrivateKey(key); + } + + public void Dispose() + { + Root.Dispose(); + Intermediate.Dispose(); + } + } +} -- 2.52.0 From 198df09d413fbcd158e7d125124a8142ea3b8b08eaca1239ce1100d9343198b6 Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Wed, 4 Nov 2026 20:42:39 -0500 Subject: [PATCH 14/22] feat(installer): StifleR-style server wizard without admin fields Welcome, Server Address, Certificate (Self-Signed, Certificate Store, File, None), Ready; upgrades skip straight to Ready. Labels are short with detail in tooltips, stock strings come from ServerUI.wxl. The admin account page is gone: Finish says where the one-time password is; ADMINUSERNAME/ADMINPASSWORD are still honoured from the command line. Secrets are hidden from the log and the setup carrying one runs through WixSilentExec. Publisher is Endexa Solutions. Co-Authored-By: Claude Opus 5.5 --- README.md | 184 ++++++++--- src/Installer.Server/Product.wxs | 294 ++++++------------ src/Installer.Server/Server.Installer.wixproj | 13 + src/Installer.Server/ServerUI.wxl | 16 + src/Installer.Server/ServerUI.wxs | 226 ++++++++++++++ 5 files changed, 485 insertions(+), 248 deletions(-) create mode 100644 src/Installer.Server/ServerUI.wxl create mode 100644 src/Installer.Server/ServerUI.wxs diff --git a/README.md b/README.md index 387bf96..801c28e 100644 --- a/README.md +++ b/README.md @@ -275,7 +275,7 @@ Never commit generated secrets (enforced by `.gitignore`). | Secret | Purpose | |--------|---------| -| `INITIAL_ADMIN_PASSWORD` | First-boot local admin password | +| `INITIAL_ADMIN_PASSWORD` | Optional first-boot admin password; empty generates one into `/initial-admin-password.txt` | | `NATS_OPERATOR_SEED` | NATS operator identity | | `NATS_ACCOUNT_SEED` | Application account identity | | `NATS_ACCOUNT_SIGNING_SEED` | Backend signs user JWTs with this | @@ -349,42 +349,73 @@ relay, and supervises a `nats-server` sidecar. CI publishes ```text ExecuteR-Server setup - Generates NATS keys, the sidecar config and appsettings.json with the - admin/admin sign-in (override with --admin-user/--admin-password) — and + Generates NATS keys, the sidecar config and appsettings.json — and downloads nats-server for this OS/architecture if it is not bundled. -ExecuteR-Server run in the foreground on http://0.0.0.0:9080 + TLS: the managed self-signed chain by default; --tls-source store + --tls-thumbprint (Windows) or --tls-source file --tls-cert-file + [--tls-key-file ] [--tls-password ] serves your own + (checked here, so a wrong path or password fails setup); --tls 0 for HTTP. +ExecuteR-Server run in the foreground on https://0.0.0.0:9080 ExecuteR-Server service install Windows service / systemd unit ExecuteR-Server service uninstall +ExecuteR-Server reset-admin [--user ] [--password ] + Lockout recovery, run elevated on the server (service running or not) ``` -When the password was left at the default, the **first sign-in demands a new -password** before any session is issued — valid credentials return a -must-change signal, and the sign-in page walks straight into a set-new-password -form. Choosing a literal `admin` password (CLI flag or MSI wizard) triggers -the same reset. +**No administrator password is asked for.** At its first start the server +creates the built-in `admin` account with a **generated one-time password** +and writes it to `data\initial-admin-password.txt` (beside the binary; +`/data/initial-admin-password.txt` in the container). Only Administrators, +SYSTEM and the account the server runs as can open that file (inheritance +off; mode 600 off Windows), and the log only says where it is — logs are +shipped and bundled far more widely than the data folder, so the password +never goes there. The **first sign-in demands a new password** before any +session is issued, and the file is deleted once it has been changed. +`--admin-user` / `--admin-password` (MSI `ADMINUSERNAME` / `ADMINPASSWORD`) +still set one for scripted installs; a literal `admin` forces the same reset. ![First sign-in password reset](docs/screenshots/login-reset.png) -The **Windows MSI** wraps the same flow with two wizard pages — the -Administrator account (defaults admin / admin) and **Network settings**: bind -address (0.0.0.0 default, explained inline), the HTTP port (9080), the -three loopback-only NATS ports, and an **Enable Self-Signed TLS** checkbox, -checked by default (see [Networking and TLS](#networking-and-tls) — proxies -skip verification or import the exported roots), each described in place so -a next-next-next install just works. Silent installs pass the same values -as properties (`ADMINUSERNAME ADMINPASSWORD BINDADDRESS HTTPPORT NATSPORT -NATSWSPORT NATSMONITORPORT ENABLETLS`), then the binary's own `setup` + -`service install` run. -The finish page offers to **open the console in the browser** (checked by -default) — a wildcard bind (`0.0.0.0` / `::`) opens `http://localhost:`, -a specific bind address opens that address directly. +**Locked out, or the file is gone?** From an elevated prompt on the server: + +```text +ExecuteR-Server reset-admin new one-time password for the break-glass account, printed here +ExecuteR-Server reset-admin --user jdoe that built-in account instead +ExecuteR-Server reset-admin --password '' that password (still changed at the next sign-in) +``` + +It reads the same `appsettings.json` as the server, so it reaches the same +database whether the service is running or stopped; it turns the account +back on, clears its sign-in back-off, ends its sessions, turns Built-In +sign-in back on (OIDC redirect off), and is audited +(`identity.break-glass.reset`). No restart is needed. In a container: +`docker exec EXECUTER-API-00001 dotnet Backend.dll reset-admin`. +`auth-reset` is the same verb. + +The **Windows MSI** wraps the same flow with two wizard pages, each label +short with the detail in its tooltip: **Server Address** — the port (9080), +Listen On (All Addresses, This Machine Only, or a specific address) and the +three NATS ports with the WebSocket bind — and **Certificate** — +**Self-Signed** (the default, managed and auto-renewed), **Certificate Store** +(a thumbprint from Local Computer › Personal), **File** (a PFX with its +password, or a PEM certificate and key) or **None (HTTP Only)**. A +next-next-next install just works. Silent installs pass the same values as +properties (`BINDADDRESS HTTPPORT NATSPORT NATSWSPORT NATSMONITORPORT +NATSBIND ENABLETLS TLSSOURCE=selfsigned|store|file TLSTHUMBPRINT TLSCERTFILE +TLSKEYFILE TLSCERTPASSWORD`, plus the optional `ADMINUSERNAME ADMINPASSWORD`; +the passwords are hidden from the installer log), then the binary's own +`setup` + `service install` run. The finish page says where the one-time +password is and offers to **open the console in the browser** (checked by +default) — a wildcard bind (`0.0.0.0` / `::`) opens `https://localhost:`, +a specific bind address opens that address directly. An upgrade goes straight +from Welcome to Ready to Install: it keeps the configuration in effect. Upgrades never overwrite existing configuration, and uninstall stops and removes the service. By default a plain uninstall **keeps** the generated data, sign-in and self-signed chain so a reinstall resumes where it left off (the managed chain is located in the machine certificate stores by subject and reused when still valid). For a fully clean removal, tick -**Also remove all data, keys and self-signed certificates** on the -uninstall page — or pass `PURGEDATA=1` to a silent uninstall. That deletes +**Remove All Data, Keys and Self-Signed Certificates** on the Remove Data +page — or pass `PURGEDATA=1` to a silent uninstall. That deletes `appsettings.json`, `data`, `logs` and `secrets` and removes the self-signed root/intermediate/leaf from the Windows stores. The same cleanup is available directly as `ExecuteR-Server.exe purge`. The MSI @@ -401,9 +432,13 @@ The standalone binary is configured through **flat keys in the generated `appsettings.json`** next to the binary, or the same names as **environment variables** (env wins). It honors the default proxy trust (loopback / RFC1918 / CGNAT), so it works behind a typical reverse proxy untouched; `setup` -pre-writes the optional knobs — `TLS_SELF_SIGNED`, `TLS_CERT_DIR`, +pre-writes the optional knobs — `TLS_SELF_SIGNED`, `TLS_CERT_SOURCE`, +`TLS_CERT_THUMBPRINT`, `TLS_CERT_FILE`, `TLS_CERT_KEY_FILE`, +`TLS_CERT_PASSWORD`, `TLS_DISTRIBUTE_CA`, `TLS_CERT_DIR`, `PUBLIC_SERVER_URL`, `TRUSTED_NETWORKS`, `TRUSTED_HOSTS`, `TRUST_PROXY` — as -placeholders ready to fill. +placeholders ready to fill. Because it carries the NATS signing seed and any +certificate password, `setup` restricts `appsettings.json` to Administrators, +SYSTEM and the account that ran it. **Multiple instances on one box**: one directory per instance, distinct ports at setup, distinct service names at install: @@ -447,8 +482,8 @@ The server encrypts its own listener **by default**: HTTPS with a chain it manages itself — root CA → intermediate → leaf, issued on first boot and **auto-renewed** in the background (leaf yearly, re-issued 30 days early and hot-swapped without a restart). Opt out with `TLS_SELF_SIGNED=0`, `setup ---tls 0`, or by unchecking **Enable Self-Signed TLS** in the server MSI -wizard (`ENABLETLS=""` silently). +--tls 0`, or **None (HTTP Only)** on the server MSI's Certificate page +(`ENABLETLS=""` silently). The reverse proxy then has two equally valid options — **pick per your organization's security standards**: @@ -473,20 +508,73 @@ Caddy / Nginx UI / nginx can bind or trust the files directly and stay current. On **Linux/macOS/containers** the PEM files are the primary store (`TLS_CERT_DIR` overrides; keep it on a persisted volume in containers). -**Agents trust a direct self-signed server automatically — without pinning.** -When the server serves its chain directly, the Add-Device modal (and -Settings token panel) pre-select **Trust self-signed certificate**, which -sets `ServerCaAutoInstall`. The agent then fetches the CA from -`/api/enrollment/ca` and **installs it into the OS trust store**, and a -background service keeps it current as the server rotates its chain — so -ordinary system validation trusts the server and renewals never lapse. It -is deliberately *not* pinned or forced: if a reverse proxy with a real -certificate is added later, that certificate validates normally and the -installed self-signed CA simply goes unused — nothing to break. The setting -is GPO-manageable (ADMX `Install the server's self-signed CA`), -`configure --install-server-ca` sets it by hand, and the agent MSI accepts -`CAINSTALL=1`. For an accept-any escape hatch (no validation), `insecure` / -`TlsSkipVerify=1` remains. +**For distribution**, the root and intermediate alone — never the leaf, +never a key — are also written to **`/ca-certificates`** +(`root-ca.cer`/`.pem`, `intermediate-ca.cer`/`.pem`, `ca-chain.pem`) and +rewritten on every renewal, so an administrator can push them by Group +Policy (*Computer Configuration › Windows Settings › Security Settings › +Public Key Policies*: the root into Trusted Root Certification Authorities, +the intermediate into Intermediate Certification Authorities) or to a proxy. + +### Your own certificate + +Instead of the managed chain, the listener can serve a certificate you +provide — from the **Windows certificate store** or a **file** — with the +managed chain staying the default: + +| Setting | `setup` flag | MSI property | | +|---|---|---|---| +| `TLS_CERT_SOURCE` | `--tls-source` | `TLSSOURCE` | `self-signed` (default), `store`, `file` | +| `TLS_CERT_THUMBPRINT` | `--tls-thumbprint` | `TLSTHUMBPRINT` | LocalMachine\My, with its private key (Windows) | +| `TLS_CERT_FILE` | `--tls-cert-file` | `TLSCERTFILE` | a PFX/P12, or a PEM (a fullchain PEM brings its intermediates) | +| `TLS_CERT_KEY_FILE` | `--tls-key-file` | `TLSKEYFILE` | the PEM key, when it is not in `TLS_CERT_FILE` | +| `TLS_CERT_PASSWORD` | `--tls-password` | `TLSCERTPASSWORD` | the PFX password, or an encrypted PEM key's | +| `TLS_DISTRIBUTE_CA` | | | `1`/`0` overrides whether agents are given the issuing chain | + +A provided certificate turns HTTPS on by itself and is **reloaded without a +restart**: a replaced file within a minute, and a store certificate renewed +by auto-enrollment (the configured one archived or within 14 days of expiry, +its successor with the same subject and issuer) is followed, with the new +thumbprint logged. The handshake sends the intermediates. A certificate that +cannot be loaded stops the first start with the reason; a later failed reload +keeps the current one in service. + +When the provided certificate's issuing chain is **not publicly trusted** — +an enterprise CA — it is treated like the managed chain: the server's own +machine stores get the intermediate (Windows), `/api/enrollment/ca` serves +the intermediates and root (never the leaf), bootstrap tokens carry that +root's fingerprint, and `/ca-certificates` holds the same files for +Group Policy. A **publicly trusted** chain (Windows: the root is in the +Third-Party Root Certification Authorities store; elsewhere, in the system +bundle rather than a locally added anchor) distributes nothing: +`/api/enrollment/ca` stays 404 and agents just validate it. + +### How agents trust the server + +**It just works when the agent talks to the server directly.** Every +channel — enrollment, the HTTPS API, downloads and upgrades, and the NATS +WebSocket — first validates the certificate normally; if that succeeds the +agent does nothing more (a public certificate, a proxy's, or a CA already +trusted, for instance by Group Policy). Only when validation fails on an +untrusted chain does the agent use the **fingerprint in the bootstrap token** +(`.`): it fetches the chain from +`/api/enrollment/ca`, keeps only the root matching the fingerprint and the +CAs that chain to it (anything else in the response is dropped), installs +them into the OS trust store (LocalMachine Root / Intermediate CA on Windows, +the system anchors on Linux, the System keychain on macOS), keeps them as its +own anchors (`%ProgramData%\ExecuteR\server-ca.pem`) and connects again with +normal validation. Those anchors back every later connection should the OS +store lose the CA, and a background refresh over the validated connection +keeps them and the store current as the server renews. Name mismatches are +never accepted. The install scripts' package download follows the same +rule: validate first, and only on a trust failure fetch the CA and accept the +server through a chain that ends at the pinned root (PowerShell 5.1, or +curl `--cacert` after an `openssl` check). For an accept-any escape hatch, +`insecure` / `TlsSkipVerify=1` remains. + +Agents enrolled before a switch to a certificate from a different CA cannot +validate the new one through their old anchors: give them a new bootstrap +token (or push the new CA by Group Policy first). --- @@ -981,14 +1069,18 @@ and carry a CSRF token checked on every write. **Break-glass account.** One built-in account always has full access through no binding, and cannot be turned off or removed. On an empty database it is created from `INITIAL_ADMIN_USERNAME` / `INITIAL_ADMIN_PASSWORD`. Without -those, it is `admin` with a one-time password written once to the server log. +a password, it gets a generated one-time password recorded in +`/initial-admin-password.txt` for administrators only (the log says +where, never what), changed at the first sign-in. **Lockout recovery** (an identity-provider outage is never terminal): - `AUTH_FORCE_LOCAL=1` (environment only) always offers the built-in form. -- `dotnet Backend.dll auth-reset` turns Built-In sign-in back on, turns - redirect off, and prints a new one-time password for the break-glass - account. Add `--user ` for another built-in account. +- `ExecuteR-Server reset-admin` (`dotnet Backend.dll reset-admin` in a + container; `auth-reset` is the same verb) turns Built-In sign-in back on, + turns redirect off, turns the account back on, and prints a new one-time + password for the break-glass account. Add `--user ` for another + built-in account, `--password ` to choose it. **Upgrading from an earlier ExecuteR** happens once, at the first start: diff --git a/src/Installer.Server/Product.wxs b/src/Installer.Server/Product.wxs index 8739ba3..eea9e15 100644 --- a/src/Installer.Server/Product.wxs +++ b/src/Installer.Server/Product.wxs @@ -1,30 +1,43 @@ + xmlns:util="http://wixtoolset.org/schemas/v4/wxs/util"> + - -