From 6998411e1172da10ddafdc714fc95a29517b3b9df3140903780c66f2a59d482b Mon Sep 17 00:00:00 2001 From: gsadmin Date: Sun, 9 Aug 2026 15:57:13 -0400 Subject: [PATCH 1/4] Clean server uninstall: purge data, keys and self-signed certs [skip ci] On Windows the managed chain is located in the machine certificate stores by subject and reused across reinstalls; a plain uninstall left the certs and the generated data/logs/secrets/appsettings behind. New `ExecuteR-Server.exe purge [--yes]` forgets the chain (removes root/intermediate/leaf from the stores) and deletes the generated runtime state, keeping MSI-owned files. The server MSI adds a PURGEDATA property, an uninstall-page checkbox ("Also remove all data, keys and self-signed certificates") spliced into the maintenance flow, and a deferred CA that runs purge --yes before RemoveFiles when checked. A plain uninstall still keeps everything for a resume. Verified: purge deletes data/logs/secrets/appsettings/nats.conf and keeps nats-server.exe; MSI builds clean with the property, CA and PurgeDlg present; solution builds, 99 backend tests pass. Co-Authored-By: Claude Fable 5 --- README.md | 13 ++++- src/Backend/Program.cs | 7 +++ src/Backend/Standalone/ServerCli.cs | 73 +++++++++++++++++++++++++++++ src/Backend/Tls/TlsCleanup.cs | 17 +++++++ src/Backend/Tls/TlsPersistence.cs | 30 ++++++++++++ src/Installer.Server/Product.wxs | 54 +++++++++++++++++++++ 6 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 src/Backend/Tls/TlsCleanup.cs diff --git a/README.md b/README.md index 9f3d8ab..28ccf68 100644 --- a/README.md +++ b/README.md @@ -363,8 +363,17 @@ 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. Upgrades never overwrite existing configuration, and uninstall stops and -removes the service. The MSI installs exactly one instance — multi-instance -boxes use the ZIP with distinct ports and service names. +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 +`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 +installs exactly one instance — multi-instance boxes use the ZIP with +distinct ports and service names. Bundles ship the **agent installers** (`agent-package/` with `Agent.msi`, `Agent.zip`, the unix zips and `VERSION`), so the URL install command works diff --git a/src/Backend/Program.cs b/src/Backend/Program.cs index 0b91fc4..8ce6b2c 100644 --- a/src/Backend/Program.cs +++ b/src/Backend/Program.cs @@ -53,6 +53,13 @@ if (args.Length > 0 && args[0] == "service") return Backend.Standalone.ServerCli.RunService(args[1..]); } +// CLI mode: clean-uninstall — forget the self-signed chain and delete +// generated runtime state (invoked by the MSI when PURGEDATA=1). +if (args.Length > 0 && args[0] == "purge") +{ + return Backend.Standalone.ServerCli.RunPurge(args[1..]); +} + // Standalone bundle: when setup generated an appsettings.json next to the // binary, anchor the content root there so the server behaves identically // however it is launched (console from any directory, SCM, systemd). diff --git a/src/Backend/Standalone/ServerCli.cs b/src/Backend/Standalone/ServerCli.cs index 9055e03..dd42e0b 100644 --- a/src/Backend/Standalone/ServerCli.cs +++ b/src/Backend/Standalone/ServerCli.cs @@ -14,6 +14,7 @@ namespace Backend.Standalone; /// 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 /// /// The same binary is the server: running it with no arguments starts the /// control plane (UI + API + WebSocket relay + optional NATS sidecar). @@ -362,6 +363,78 @@ public static class ServerCli }; } + /// + /// Clean-uninstall: forget the managed self-signed chain from the Windows + /// certificate stores and delete the runtime state `setup` generated + /// (appsettings.json, data, logs, secrets, nats.conf). MSI-owned files + /// (the binary, wwwroot, nats-server, agent-package) are left for the + /// installer to remove. Interactive callers are prompted unless --yes. + /// + public static int RunPurge(string[] args) + { + var assumeYes = args.Contains("--yes") || args.Contains("-y"); + if (!assumeYes) + { + Console.Write("Remove all ExecuteR data, keys and self-signed certificates? [y/N] "); + var answer = Console.ReadLine()?.Trim(); + if (answer is not ("y" or "Y" or "yes" or "YES")) + { + Console.WriteLine("Purge cancelled."); + return 0; + } + } + + var removed = Backend.Tls.TlsCleanup.RemoveWindowsChain(); + if (removed > 0) + { + Console.WriteLine($"Removed {removed} self-signed certificate(s) from the machine stores."); + } + + var baseDir = AppContext.BaseDirectory; + foreach (var dir in new[] { "data", "logs", "secrets" }) + { + DeleteDirectory(Path.Combine(baseDir, dir)); + } + + foreach (var file in new[] { "appsettings.json", Path.Combine("nats", "nats.conf") }) + { + var path = Path.Combine(baseDir, file); + if (File.Exists(path)) + { + try + { + File.Delete(path); + Console.WriteLine($"Deleted {path}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Could not delete {path}: {ex.Message}"); + } + } + } + + Console.WriteLine("Purge complete."); + return 0; + } + + private static void DeleteDirectory(string path) + { + if (!Directory.Exists(path)) + { + return; + } + + try + { + Directory.Delete(path, recursive: true); + Console.WriteLine($"Deleted {path}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Could not delete {path}: {ex.Message}"); + } + } + private static int InstallWindows(string exe, string name) { // Idempotent: create may fail when the service exists (repair or MSI diff --git a/src/Backend/Tls/TlsCleanup.cs b/src/Backend/Tls/TlsCleanup.cs new file mode 100644 index 0000000..bf472be --- /dev/null +++ b/src/Backend/Tls/TlsCleanup.cs @@ -0,0 +1,17 @@ +using System.Runtime.Versioning; + +namespace Backend.Tls; + +/// Clean-uninstall helper: forget the managed self-signed chain. +public static class TlsCleanup +{ + /// + /// Removes the managed self-signed chain from the Windows machine + /// certificate stores. No-op off Windows. Returns the count removed. + /// + public static int RemoveWindowsChain() + => OperatingSystem.IsWindows() ? RemoveWindowsChainCore() : 0; + + [SupportedOSPlatform("windows")] + private static int RemoveWindowsChainCore() => WindowsStoreTlsPersistence.RemoveManagedChain(); +} diff --git a/src/Backend/Tls/TlsPersistence.cs b/src/Backend/Tls/TlsPersistence.cs index a83220d..7be213a 100644 --- a/src/Backend/Tls/TlsPersistence.cs +++ b/src/Backend/Tls/TlsPersistence.cs @@ -193,6 +193,36 @@ internal sealed class WindowsStoreTlsPersistence(string mirrorDirectory) : ITlsP return persisted; } + /// + /// Removes the managed self-signed chain (by subject) from all three + /// machine stores — the clean-uninstall counterpart to Save. Returns the + /// number of certificates removed; best-effort per store. + /// + public static int RemoveManagedChain() + { + var removed = 0; + foreach (var role in new[] { TlsRole.Root, TlsRole.Intermediate, TlsRole.Leaf }) + { + try + { + using var store = new X509Store(StoreFor(role), StoreLocation.LocalMachine); + store.Open(OpenFlags.ReadWrite); + foreach (var existing in store.Certificates.Find( + X509FindType.FindBySubjectDistinguishedName, TlsSubjects.For(role), validOnly: false)) + { + store.Remove(existing); + removed++; + } + } + catch (Exception) + { + // Best effort — a locked or missing store must not fail uninstall. + } + } + + return removed; + } + public void Complete(X509Certificate2 root, X509Certificate2 intermediate, X509Certificate2 leaf) { // SChannel serves the chain from the stores; the PEM mirror exists diff --git a/src/Installer.Server/Product.wxs b/src/Installer.Server/Product.wxs index bb5d571..a1a0897 100644 --- a/src/Installer.Server/Product.wxs +++ b/src/Installer.Server/Product.wxs @@ -125,6 +125,22 @@ Impersonate="no" Return="ignore" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + system + pinned + insecure + + + diff --git a/src/Installer/Policies/en-US/ExecuteR.adml b/src/Installer/Policies/en-US/ExecuteR.adml index ecd6225..693098f 100644 --- a/src/Installer/Policies/en-US/ExecuteR.adml +++ b/src/Installer/Policies/en-US/ExecuteR.adml @@ -113,6 +113,20 @@ Do not enable this for servers reached through a proxy with a publicly trusted c The agent reads configuration at service start; restart the "ExecuteR Agent" service (ExecuteRAgent) to apply a change immediately. + Server certificate trust mode + How the agent decides whether to trust the ExecuteR server's certificate. + +System (default): validate against the OS trust store — for a server reached through a reverse proxy with a publicly trusted certificate. + +Pinned: fetch the server's managed self-signed CA (root + intermediate), pin it, and validate against it — refreshing automatically when the server rotates its chain, so trust never lapses. Use this for a self-signed server without a proxy. + +Insecure: accept any certificate (the connection is still encrypted, but the certificate is not verified). Prefer Pinned. + +The agent reads configuration at service start; restart the "ExecuteR Agent" service (ExecuteRAgent) to apply a change immediately. + System trust store (default) + Pinned self-signed CA (auto-renewed) + Insecure (accept any) + Outbound proxy URL Routes the agent's outbound traffic (enrollment HTTPS and the persistent WebSocket connection) through an explicit proxy, for example http://proxy.example.com:8080. @@ -152,6 +166,9 @@ The agent reads configuration at service start; restart the "ExecuteR Agent" ser + + Trust mode: + diff --git a/src/Installer/Product.wxs b/src/Installer/Product.wxs index a503faf..db3e0d4 100644 --- a/src/Installer/Product.wxs +++ b/src/Installer/Product.wxs @@ -76,6 +76,12 @@ + + + + @@ -149,6 +155,10 @@ + + + diff --git a/src/Shared.Contracts/Api/ApiRoutes.cs b/src/Shared.Contracts/Api/ApiRoutes.cs index 4aa4bd1..0eb49da 100644 --- a/src/Shared.Contracts/Api/ApiRoutes.cs +++ b/src/Shared.Contracts/Api/ApiRoutes.cs @@ -65,6 +65,9 @@ public static class ApiRoutes public const string Dashboard = "/api/dashboard"; + /// Public self-signed CA chain (root + intermediate) for agent trust. + public const string EnrollmentCaChain = "/api/enrollment/ca"; + public const string Schedules = "/api/schedules"; public const string Schedule = "/api/schedules/{id}"; public const string SchedulePreview = "/api/schedules/preview"; diff --git a/src/WebUI/src/lib/components/AddDeviceModal.svelte b/src/WebUI/src/lib/components/AddDeviceModal.svelte index 3471b49..80f6881 100644 --- a/src/WebUI/src/lib/components/AddDeviceModal.svelte +++ b/src/WebUI/src/lib/components/AddDeviceModal.svelte @@ -194,8 +194,8 @@ {/if} diff --git a/src/WebUI/src/routes/settings/+page.svelte b/src/WebUI/src/routes/settings/+page.svelte index a93f727..7472cac 100644 --- a/src/WebUI/src/routes/settings/+page.svelte +++ b/src/WebUI/src/routes/settings/+page.svelte @@ -136,7 +136,7 @@
{installCommand}
diff --git a/src/WebUI/vite.config.ts b/src/WebUI/vite.config.ts index f7e5391..4b6a030 100644 --- a/src/WebUI/vite.config.ts +++ b/src/WebUI/vite.config.ts @@ -22,7 +22,7 @@ export default defineConfig({ }) ], server: { - // Local development against `docker compose -f docker-compose.yml -f docker-compose.dev.yml up` + // Local development against `docker compose -f IaC/compose/docker-compose.yml -f IaC/compose/docker-compose.dev.yml up` // (or a locally running backend + NATS). Production is same-origin via the reverse proxy. proxy: { '/api': { diff --git a/tests/Backend.Tests/InstallScriptTests.cs b/tests/Backend.Tests/InstallScriptTests.cs index c565283..cace03c 100644 --- a/tests/Backend.Tests/InstallScriptTests.cs +++ b/tests/Backend.Tests/InstallScriptTests.cs @@ -161,20 +161,31 @@ public class InstallScriptTests(BackendTestFixture fixture) : IClassFixture Date: Sun, 9 Aug 2026 17:42:18 -0400 Subject: [PATCH 3/4] Self-signed CA: install into OS trust (not pinned); richer cert SANs; bind mounts [skip ci] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Agent trust for self-signed servers no longer pins/forces — pinning breaks if a reverse proxy with a real cert is added later. Instead the CA is installed into the OS trust store (ServerCaAutoInstall) so ordinary system validation trusts a direct/local self-signed server, and a proxy with a real cert later just validates normally with the self-signed CA sitting unused. Replaces the pinned TlsTrustMode: agent config ServerCaAutoInstall, ServerTrust.FetchAndInstallCaAsync (Windows stores / update-ca-certificates / macOS keychain), CaRefreshService keeps it current across renewals, configure --install-server-ca, agent MSI CAINSTALL, ADMX toggle, install scripts. Server certificate SANs now cover the hostname, hostname. for every adapter suffix plus the global domain, every non-link-local IP on every adapter, and localhost/loopback (verified live). Swarm stack switches named volumes to STACK_BINDMOUNTROOT bind mounts (shared storage across nodes), matching the compose conventions. Verified: solution builds, Backend 100 / Agent 60 tests, svelte-check clean, agent MSI builds, self-signed boot emits the broadened SANs. Co-Authored-By: Claude Fable 5 --- IaC/swarm/README.md | 9 +- IaC/swarm/executer-stack.yml | 20 +- README.md | 29 ++- src/Agent/Commands/AgentCli.cs | 22 +- src/Agent/Configuration/AgentConfiguration.cs | 20 +- .../Configuration/AgentConfigurationKeys.cs | 13 +- src/Agent/Configuration/AgentTlsTrust.cs | 15 -- src/Agent/Enrollment/EnrollmentService.cs | 21 +- src/Agent/Enrollment/ServerTrust.cs | 208 +++++++++--------- .../Messaging/AgentNatsOptionsFactory.cs | 15 +- src/Agent/Service/CaRefreshService.cs | 18 +- src/Backend/Enrollment/InstallEndpoints.cs | 11 +- src/Backend/Tls/TlsSettings.cs | 70 +++++- src/Installer/Policies/ExecuteR.admx | 17 +- src/Installer/Policies/en-US/ExecuteR.adml | 16 +- src/Installer/Product.wxs | 24 +- .../src/lib/components/AddDeviceModal.svelte | 2 +- src/WebUI/src/routes/settings/+page.svelte | 2 +- tests/Backend.Tests/InstallScriptTests.cs | 10 +- 19 files changed, 282 insertions(+), 260 deletions(-) delete mode 100644 src/Agent/Configuration/AgentTlsTrust.cs diff --git a/IaC/swarm/README.md b/IaC/swarm/README.md index 475a45f..7f0bc87 100644 --- a/IaC/swarm/README.md +++ b/IaC/swarm/README.md @@ -29,10 +29,11 @@ docker service scale executer_API=5 executer_WEB=3 - **`API` replicas** run `SCHEDULE_WORKER=0`; the single **`WORKER`** replica runs `SCHEDULE_WORKER=1`. Keep `WORKER` at replicas: 1 — it owns the scheduler. Everything else scales freely. -- **Shared key ring**: the `keyring` volume is mounted by every `API` and - `WORKER` task. It **must** be network storage — set - `SHARED_VOLUME_DRIVER` to a CephFS/MicroCeph or NFS volume driver. - A `local` driver only works on a single node. +- **Shared key ring**: every `API` and `WORKER` task bind-mounts + `${STACK_BINDMOUNTROOT}/${STACK_NAME}/API/Keys`. On a multi-node swarm + `STACK_BINDMOUNTROOT` **must** be shared storage mounted at the same path + on every node (CephFS/MicroCeph, NFS) — a node-local path only works on a + single node. The same applies to the Postgres and JetStream bind mounts. - **Postgres** backs all backends. Pin it to a node with `executer.db=true` and durable storage, or point `DATABASE_CONNECTION_STRING` at a managed database and drop the `DB` service. diff --git a/IaC/swarm/executer-stack.yml b/IaC/swarm/executer-stack.yml index 5d3f46c..56e830f 100644 --- a/IaC/swarm/executer-stack.yml +++ b/IaC/swarm/executer-stack.yml @@ -27,14 +27,10 @@ networks: internal: true attachable: true -volumes: - # Shared across nodes — back with a CephFS/NFS volume driver, NOT local. - keyring: - driver: ${SHARED_VOLUME_DRIVER:-local} - pgdata: - driver: ${SHARED_VOLUME_DRIVER:-local} - jetstream: - driver: ${SHARED_VOLUME_DRIVER:-local} +# Bind mounts under STACK_BINDMOUNTROOT (per the stack conventions). For a +# multi-node swarm this root MUST be shared storage mounted at the same path +# on every node (CephFS/MicroCeph, NFS) — the key ring especially is read by +# every API/WORKER task wherever it lands. services: DB: @@ -45,7 +41,7 @@ services: POSTGRES_USER: executer POSTGRES_PASSWORD: '${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}' volumes: - - 'pgdata:/var/lib/postgresql/data:rw' + - '${STACK_BINDMOUNTROOT:-/mnt/data/docker/stacks}/${STACK_NAME:-stk-executer-00001}/DB/Data:/var/lib/postgresql/data:rw' deploy: replicas: 1 placement: @@ -66,7 +62,7 @@ services: - '--routes=nats://tasks.NATS:6222' - '--http_port=8222' volumes: - - 'jetstream:/data:rw' + - '${STACK_BINDMOUNTROOT:-/mnt/data/docker/stacks}/${STACK_NAME:-stk-executer-00001}/NATS/JetStream:/data:rw' deploy: replicas: 3 restart_policy: { condition: any } @@ -94,7 +90,7 @@ services: # The scheduler runs only on the dedicated worker below. SCHEDULE_WORKER: '0' volumes: - - 'keyring:/keys:rw' + - '${STACK_BINDMOUNTROOT:-/mnt/data/docker/stacks}/${STACK_NAME:-stk-executer-00001}/API/Keys:/keys:rw' deploy: replicas: ${API_REPLICAS:-3} restart_policy: { condition: any } @@ -114,7 +110,7 @@ services: # The single scheduler owner. Keep this at exactly one replica. SCHEDULE_WORKER: '1' volumes: - - 'keyring:/keys:rw' + - '${STACK_BINDMOUNTROOT:-/mnt/data/docker/stacks}/${STACK_NAME:-stk-executer-00001}/API/Keys:/keys:rw' deploy: replicas: 1 restart_policy: { condition: any } diff --git a/README.md b/README.md index 04976ae..4ded054 100644 --- a/README.md +++ b/README.md @@ -465,21 +465,20 @@ 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 the self-signed chain automatically, and stay trusting -across renewals** — no operator step. When the server serves its chain -directly, the Add-Device modal (and Settings token panel) pre-select -**Trust self-signed certificate**, which sets the agent to **pinned** trust -(`TlsTrustMode=pinned`). On first start the agent fetches the CA from -`/api/enrollment/ca`, pins it, installs it into the host trust store, and -validates every connection (enrollment HTTPS and the NATS WebSocket) -against it — real validation, not skip-verify. A background service -re-pins the CA periodically, so when the server rotates its chain agents -pick up the new CA before trust lapses. `TlsTrustMode` is GPO-manageable -(ADMX `Server certificate trust mode`, policy key beats local config), -`configure --trust-mode pinned` sets it by hand, and the agent MSI accepts -`TRUSTMODE=pinned`. `system` (validate against the OS store, for a real -edge certificate) is the default; `insecure` / `TlsSkipVerify=1` remain as -an accept-any escape hatch. +**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. --- diff --git a/src/Agent/Commands/AgentCli.cs b/src/Agent/Commands/AgentCli.cs index 6be71ee..1fa8cb0 100644 --- a/src/Agent/Commands/AgentCli.cs +++ b/src/Agent/Commands/AgentCli.cs @@ -118,7 +118,6 @@ public static class AgentCli string? url = null; string? token = null; string? proxy = null; - string? trustMode = null; for (var i = 0; i < args.Length - 1; i++) { switch (args[i]) @@ -132,16 +131,14 @@ public static class AgentCli case "--proxy": proxy = args[++i]; break; - case "--trust-mode": - trustMode = args[++i]; - break; } } + var installServerCa = args.Contains("--install-server-ca"); var skipTlsVerify = args.Contains("--skip-tls-verify"); - if (url is null && token is null && proxy is null && trustMode is null && !skipTlsVerify) + if (url is null && token is null && proxy is null && !installServerCa && !skipTlsVerify) { - Console.Error.WriteLine("Usage: configure --url [--token ] [--proxy ] [--trust-mode system|pinned|insecure] [--skip-tls-verify]"); + Console.Error.WriteLine("Usage: configure --url [--token ] [--proxy ] [--install-server-ca] [--skip-tls-verify]"); return 1; } @@ -163,17 +160,18 @@ public static class AgentCli registry.WriteString(Branding.AgentRegistryKey, Agent.Configuration.AgentConfigurationKeys.ProxyUrl, proxy); } - if (trustMode is not null) + if (installServerCa) { - // pinned = fetch and validate against the server's self-signed CA - // (auto-refreshed); system = OS trust store; insecure = accept any. - registry.WriteString(Branding.AgentRegistryKey, Agent.Configuration.AgentConfigurationKeys.TlsTrustMode, trustMode.Trim().ToLowerInvariant()); + // Install the server's self-signed CA into the OS trust store (and + // keep it current) so system validation trusts a direct/local + // connection. Not pinning — a real cert behind a proxy still works. + registry.WriteDword(Branding.AgentRegistryKey, Agent.Configuration.AgentConfigurationKeys.ServerCaAutoInstall, 1); } if (skipTlsVerify) { - // Back-compat escape hatch: accept any certificate (still - // encrypted). Prefer --trust-mode pinned for self-signed servers. + // Escape hatch: accept any certificate (still encrypted). Prefer + // --install-server-ca for self-signed servers. registry.WriteDword(Branding.AgentRegistryKey, Agent.Configuration.AgentConfigurationKeys.TlsSkipVerify, 1); } diff --git a/src/Agent/Configuration/AgentConfiguration.cs b/src/Agent/Configuration/AgentConfiguration.cs index 1eabe78..2eb0cc8 100644 --- a/src/Agent/Configuration/AgentConfiguration.cs +++ b/src/Agent/Configuration/AgentConfiguration.cs @@ -21,17 +21,13 @@ public sealed class AgentConfiguration public required ConfigurationValue TlsSkipVerify { get; init; } public required ConfigurationValue ProxyUrl { get; init; } - /// Raw TlsTrustMode string; defaults are resolved by . - public ConfigurationValue TlsTrustModeRaw { get; init; } = new(null, ConfigurationSource.Default); - - /// Effective trust mode (raw value, else derived from TlsSkipVerify). - public AgentTlsTrust Trust => TlsTrustModeRaw.Value?.Trim().ToLowerInvariant() switch - { - "pinned" => AgentTlsTrust.Pinned, - "insecure" => AgentTlsTrust.Insecure, - "system" => AgentTlsTrust.System, - _ => TlsSkipVerify.Value ? AgentTlsTrust.Insecure : AgentTlsTrust.System, - }; + /// + /// Install the server's managed self-signed CA into the OS trust store so + /// ordinary system validation succeeds on a direct/local connection. Not + /// pinning and not forced — a later reverse proxy with a real certificate + /// still validates normally and the self-signed CA just goes unused. + /// + public ConfigurationValue ServerCaAutoInstall { get; init; } = new(false, ConfigurationSource.Default); /// Parsed ServerURL; null when unset or invalid. public ServerUrl? ServerUrl => @@ -62,7 +58,7 @@ public sealed class AgentConfigurationLoader(IRegistryStore registry, bool allow AllowScriptExecution = ResolveBool(AgentConfigurationKeys.AllowScriptExecution, defaultValue: true), TlsSkipVerify = ResolveBool(AgentConfigurationKeys.TlsSkipVerify, defaultValue: false), ProxyUrl = ResolveString(AgentConfigurationKeys.ProxyUrl, AgentConfigurationKeys.EnvironmentNames.ProxyUrl, null), - TlsTrustModeRaw = ResolveString(AgentConfigurationKeys.TlsTrustMode, AgentConfigurationKeys.EnvironmentNames.TlsTrustMode, null), + ServerCaAutoInstall = ResolveBool(AgentConfigurationKeys.ServerCaAutoInstall, defaultValue: false), }; } diff --git a/src/Agent/Configuration/AgentConfigurationKeys.cs b/src/Agent/Configuration/AgentConfigurationKeys.cs index 41adca0..36f7568 100644 --- a/src/Agent/Configuration/AgentConfigurationKeys.cs +++ b/src/Agent/Configuration/AgentConfigurationKeys.cs @@ -21,14 +21,12 @@ public static class AgentConfigurationKeys public const string TlsSkipVerify = "TlsSkipVerify"; /// - /// Trust mode for the server connection: "system" (validate against the - /// OS trust store — the default, for real certs behind a proxy), - /// "pinned" (fetch the server's managed self-signed CA and validate - /// against it, refreshing across renewals), or "insecure" (accept any — - /// same as TlsSkipVerify). Unset falls back to insecure when - /// TlsSkipVerify is 1, else system. + /// DWORD 1: fetch the server's managed self-signed CA and install it into + /// the OS trust store (and keep it current across renewals) so system + /// validation trusts a direct/local self-signed server. Not pinning — + /// a proxy with a real certificate added later still validates normally. /// - public const string TlsTrustMode = "TlsTrustMode"; + public const string ServerCaAutoInstall = "ServerCaAutoInstall"; /// /// Explicit outbound proxy URL (http://proxy:8080). Unset, the agent @@ -44,7 +42,6 @@ public static class AgentConfigurationKeys public const string BootstrapToken = "AGENT_BOOTSTRAP_TOKEN"; public const string LogLevel = "AGENT_LOG_LEVEL"; public const string ProxyUrl = "AGENT_PROXY_URL"; - public const string TlsTrustMode = "AGENT_TLS_TRUST_MODE"; } } diff --git a/src/Agent/Configuration/AgentTlsTrust.cs b/src/Agent/Configuration/AgentTlsTrust.cs deleted file mode 100644 index 3da594b..0000000 --- a/src/Agent/Configuration/AgentTlsTrust.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Agent.Configuration; - -/// How the agent decides whether to trust the server's certificate. -public enum AgentTlsTrust -{ - /// Validate against the OS trust store (default; real edge certs). - System, - - /// Pin and validate against the server's managed self-signed CA, - /// refreshed across renewals so trust never lapses. - Pinned, - - /// Accept any certificate (still encrypted; trust not verified). - Insecure, -} diff --git a/src/Agent/Enrollment/EnrollmentService.cs b/src/Agent/Enrollment/EnrollmentService.cs index 9567308..7697b54 100644 --- a/src/Agent/Enrollment/EnrollmentService.cs +++ b/src/Agent/Enrollment/EnrollmentService.cs @@ -80,27 +80,22 @@ public sealed class EnrollmentService( AgentVersion: BuildVersion.Resolve(typeof(EnrollmentService).Assembly), Device: ReadDeviceInfo()); - // Trust mode (registry/GPO): pinned validates against the server's - // managed self-signed CA — fetch+pin it first if we haven't yet, so - // enrollment itself is validated. Insecure accepts any (still - // encrypted); system uses the OS trust store. - var trust = configuration.Trust; - if (trust == AgentTlsTrust.Pinned && !ServerTrust.HasPinnedCa) + // Self-signed servers: make the CA available to the OS trust store + // first so ordinary system validation succeeds here — not pinned, so + // a proxy with a real certificate added later still validates. + if (configuration.ServerCaAutoInstall.Value) { - await ServerTrust.FetchAndPinAsync(serverUrl, HttpClient.DefaultProxy, logger, cancellationToken); + await ServerTrust.FetchAndInstallCaAsync(serverUrl, HttpClient.DefaultProxy, logger, cancellationToken); } + // TlsSkipVerify is the accept-any escape hatch; otherwise validate + // against the OS trust store (which now contains the CA above). using var handler = new HttpClientHandler(); - if (trust == AgentTlsTrust.Insecure) + if (configuration.TlsSkipVerify.Value) { handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; } - else if (trust == AgentTlsTrust.Pinned) - { - handler.ServerCertificateCustomValidationCallback = - (_, cert, _, _) => ServerTrust.ValidatePinned(cert); - } using var http = new HttpClient(handler); var endpoint = serverUrl.ApiEndpoint(ApiRoutes.Bootstrap); diff --git a/src/Agent/Enrollment/ServerTrust.cs b/src/Agent/Enrollment/ServerTrust.cs index d0adb6a..a8f32d8 100644 --- a/src/Agent/Enrollment/ServerTrust.cs +++ b/src/Agent/Enrollment/ServerTrust.cs @@ -1,84 +1,32 @@ +using System.Diagnostics; using System.Net; -using System.Net.Security; +using System.Runtime.Versioning; using System.Security.Cryptography.X509Certificates; using Microsoft.Extensions.Logging; using Shared.Contracts.Api; -using Shared.Infrastructure.Constants; using Shared.Infrastructure.Routing; namespace Agent.Enrollment; /// -/// Server-certificate trust for the agent. In pinned mode the agent fetches -/// the server's managed self-signed CA (root + intermediate) once, stores it, -/// and validates every connection against it — so a self-signed server is -/// trusted for real (not skip-verify) and stays trusted across renewals when -/// refreshes the pinned copy. -/// The public certs come from GET /api/enrollment/ca. +/// 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 +/// 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. /// public static class ServerTrust { - /// Where the pinned CA chain (PEM) lives on disk. - public static string PinnedCaPath { get; } = - Path.Combine(Branding.AgentProgramDataDirectory, "server-ca.pem"); - - /// True once a CA chain has been pinned. - public static bool HasPinnedCa => File.Exists(PinnedCaPath); - - /// Validates a server certificate against the pinned CA chain. - public static bool ValidatePinned(X509Certificate2? serverCertificate) - { - if (serverCertificate is null || !File.Exists(PinnedCaPath)) - { - return false; - } - - X509Certificate2Collection anchors = []; - try - { - anchors.ImportFromPemFile(PinnedCaPath); - } - catch (Exception) - { - return false; - } - - using var chain = new X509Chain(); - chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust; - chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; - foreach (var anchor in anchors) - { - // Self-issued = root (trust anchor); the rest are intermediates. - if (anchor.SubjectName.RawData.AsSpan().SequenceEqual(anchor.IssuerName.RawData)) - { - chain.ChainPolicy.CustomTrustStore.Add(anchor); - } - else - { - chain.ChainPolicy.ExtraStore.Add(anchor); - } - } - - return chain.Build(serverCertificate); - } - - /// The RemoteCertificateValidationCallback for the given trust mode. - public static RemoteCertificateValidationCallback? ValidationCallback(Agent.Configuration.AgentTlsTrust trust) - => trust switch - { - Agent.Configuration.AgentTlsTrust.Insecure => (_, _, _, _) => true, - Agent.Configuration.AgentTlsTrust.Pinned => (_, cert, _, _) => - ValidatePinned(cert is null ? null : new X509Certificate2(cert)), - _ => null, - }; - /// - /// Fetches the server's CA chain and pins it if changed. The fetch itself - /// accepts any certificate (we are retrieving public CA certs to establish - /// trust), then validates the payload really is PEM certificates. Returns - /// true when a chain is now pinned. + /// Fetches the server's CA chain and installs it into the OS trust store + /// (idempotent, best-effort). The fetch accepts any certificate — it is + /// retrieving public CA certs to establish trust — then verifies the + /// payload is real PEM certificates before installing. Returns true when + /// a chain was fetched. /// - public static async Task FetchAndPinAsync( + public static async Task FetchAndInstallCaAsync( ServerUrl serverUrl, IWebProxy? proxy, ILogger logger, CancellationToken cancellationToken) { using var handler = new HttpClientHandler @@ -93,58 +41,50 @@ public static class ServerTrust using var http = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(30) }; var endpoint = serverUrl.ApiEndpoint(ApiRoutes.EnrollmentCaChain); + string pem; + X509Certificate2Collection chain = []; try { using var response = await http.GetAsync(endpoint, cancellationToken); if (response.StatusCode == HttpStatusCode.NotFound) { - // Server is not self-signed — nothing to pin. - return false; + return false; // Server is not self-signed — nothing to install. } response.EnsureSuccessStatusCode(); - var pem = await response.Content.ReadAsStringAsync(cancellationToken); - - X509Certificate2Collection parsed = []; - parsed.ImportFromPem(pem); - if (parsed.Count == 0) + pem = await response.Content.ReadAsStringAsync(cancellationToken); + chain.ImportFromPem(pem); + if (chain.Count == 0) { logger.LogWarning("Server CA response contained no certificates"); return false; } - - var existing = File.Exists(PinnedCaPath) ? await File.ReadAllTextAsync(PinnedCaPath, cancellationToken) : null; - if (!string.Equals(existing, pem, StringComparison.Ordinal)) - { - Directory.CreateDirectory(Path.GetDirectoryName(PinnedCaPath)!); - await File.WriteAllTextAsync(PinnedCaPath, pem, cancellationToken); - logger.LogInformation("Pinned server CA updated ({Count} certificate(s))", parsed.Count); - TryInstallOsTrust(parsed, logger); - } - - return true; } catch (Exception ex) { logger.LogWarning(ex, "Could not fetch the server CA from {Endpoint}", endpoint); - return File.Exists(PinnedCaPath); + return false; } + + if (OperatingSystem.IsWindows()) + { + InstallWindows(chain, logger); + } + else if (OperatingSystem.IsMacOS()) + { + InstallMac(pem, logger); + } + else + { + InstallLinux(pem, logger); + } + + return true; } - /// - /// Best-effort: also add the CA to the Windows machine trust stores so - /// host tooling (browsers, curl) trusts the server too. The agent's own - /// connections rely on the pinned file, so failure here is non-fatal. On - /// Linux/macOS the bootstrap installer seeds the OS store; the pinned file - /// keeps the agent itself current thereafter. - /// - private static void TryInstallOsTrust(X509Certificate2Collection chain, ILogger logger) + [SupportedOSPlatform("windows")] + private static void InstallWindows(X509Certificate2Collection chain, ILogger logger) { - if (!OperatingSystem.IsWindows()) - { - return; - } - try { foreach (var cert in chain) @@ -162,12 +102,78 @@ public static class ServerTrust } } - store.Add(cert); + if (store.Certificates.Find(X509FindType.FindByThumbprint, cert.Thumbprint, validOnly: false).Count == 0) + { + store.Add(cert); + logger.LogInformation("Installed server CA into LocalMachine\\{Store}: {Subject}", + selfIssued ? "Root" : "CA", cert.Subject); + } } } catch (Exception ex) { - logger.LogDebug(ex, "Could not update the Windows trust store (pinned file still applies)"); + logger.LogWarning(ex, "Could not install the server CA into the Windows trust store"); + } + } + + private static void InstallLinux(string pem, ILogger logger) + { + try + { + // Debian/Ubuntu vs RHEL anchor locations. + if (Directory.Exists("/usr/local/share/ca-certificates")) + { + File.WriteAllText("/usr/local/share/ca-certificates/executer-server-ca.crt", pem); + RunBestEffort("update-ca-certificates", logger); + } + else if (Directory.Exists("/etc/pki/ca-trust/source/anchors")) + { + File.WriteAllText("/etc/pki/ca-trust/source/anchors/executer-server-ca.crt", pem); + RunBestEffort("update-ca-trust", logger); + } + else + { + logger.LogDebug("No known CA anchor directory found; skipping OS trust install"); + } + } + catch (Exception ex) + { + logger.LogWarning(ex, "Could not install the server CA into the Linux trust store"); + } + } + + private static void InstallMac(string pem, ILogger logger) + { + try + { + var path = Path.Combine(Path.GetTempPath(), "executer-server-ca.pem"); + File.WriteAllText(path, pem); + RunBestEffort("security", logger, + "add-trusted-cert", "-d", "-r", "trustRoot", "-k", "/Library/Keychains/System.keychain", path); + File.Delete(path); + } + catch (Exception ex) + { + logger.LogWarning(ex, "Could not install the server CA into the macOS keychain"); + } + } + + private static void RunBestEffort(string file, ILogger logger, params string[] args) + { + try + { + var psi = new ProcessStartInfo(file) { RedirectStandardError = true, RedirectStandardOutput = true }; + foreach (var arg in args) + { + psi.ArgumentList.Add(arg); + } + + using var process = Process.Start(psi); + process?.WaitForExit(15000); + } + catch (Exception ex) + { + logger.LogDebug(ex, "Trust-store refresh command '{File}' did not run", file); } } } diff --git a/src/Agent/Messaging/AgentNatsOptionsFactory.cs b/src/Agent/Messaging/AgentNatsOptionsFactory.cs index 0f4bfa1..4e5f7e6 100644 --- a/src/Agent/Messaging/AgentNatsOptionsFactory.cs +++ b/src/Agent/Messaging/AgentNatsOptionsFactory.cs @@ -37,16 +37,15 @@ public static class AgentNatsOptionsFactory LoggerFactory = loggerFactory, }; - var trust = configuration.Trust; - if (trust == Agent.Configuration.AgentTlsTrust.Insecure) + // TlsSkipVerify accepts any cert (still encrypted); otherwise the OS + // trust store validates the wss server cert — which contains the + // self-signed CA when ServerCaAutoInstall installed it. + var skipVerify = configuration.TlsSkipVerify.Value; + if (skipVerify) { options = options with { TlsOpts = NatsTlsOpts.Default with { InsecureSkipVerify = true } }; } - // pinned validates the wss server cert against the pinned CA; insecure - // accepts any; system defers to the OS trust store (callback stays null). - var validate = Agent.Enrollment.ServerTrust.ValidationCallback(trust); - options = options with { WebSocketOpts = NatsWebSocketOpts.Default with @@ -58,9 +57,9 @@ public static class AgentNatsOptionsFactory // HttpClient.DefaultProxy on its own. clientOptions.Proxy = System.Net.Http.HttpClient.DefaultProxy; - if (validate is not null) + if (skipVerify) { - clientOptions.RemoteCertificateValidationCallback = validate; + clientOptions.RemoteCertificateValidationCallback = (_, _, _, _) => true; } return ValueTask.CompletedTask; diff --git a/src/Agent/Service/CaRefreshService.cs b/src/Agent/Service/CaRefreshService.cs index 4e525bf..3053bb8 100644 --- a/src/Agent/Service/CaRefreshService.cs +++ b/src/Agent/Service/CaRefreshService.cs @@ -6,20 +6,20 @@ using Microsoft.Extensions.Logging; namespace Agent.Service; /// -/// Keeps the pinned server CA current in pinned-trust mode: fetches the -/// managed self-signed chain on start and every few hours, re-pinning when it -/// changes. This is what makes server-side renewals invisible to operators — -/// when the chain rotates, agents pick up the new CA before trust lapses. -/// Idle in system / insecure trust modes. +/// Keeps the server's self-signed CA present in the OS trust store when +/// ServerCaAutoInstall is set: installs it on start and re-installs every few +/// hours, so a server-side chain rotation stays trusted without operator +/// action. It never forces validation against the CA — a proxy with a real +/// certificate added later is unaffected. Idle otherwise. /// public sealed class CaRefreshService( AgentConfigurationLoader loader, ILogger logger) : BackgroundService { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - if (loader.Load().Trust != AgentTlsTrust.Pinned) + if (!loader.Load().ServerCaAutoInstall.Value) { - logger.LogDebug("CA refresh idle: trust mode is not 'pinned'"); + logger.LogDebug("Server CA auto-install idle (not enabled)"); return; } @@ -29,9 +29,9 @@ public sealed class CaRefreshService( do { var configuration = loader.Load(); - if (configuration.Trust == AgentTlsTrust.Pinned && configuration.ServerUrl is { } serverUrl) + if (configuration.ServerCaAutoInstall.Value && configuration.ServerUrl is { } serverUrl) { - await ServerTrust.FetchAndPinAsync( + await ServerTrust.FetchAndInstallCaAsync( serverUrl, System.Net.Http.HttpClient.DefaultProxy, logger, stoppingToken); } } diff --git a/src/Backend/Enrollment/InstallEndpoints.cs b/src/Backend/Enrollment/InstallEndpoints.cs index 88e687b..d1a54e3 100644 --- a/src/Backend/Enrollment/InstallEndpoints.cs +++ b/src/Backend/Enrollment/InstallEndpoints.cs @@ -120,8 +120,9 @@ public static class InstallEndpoints // 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 set to pinned trust — it fetches, pins and installs the CA - // on first start and refreshes it across renewals. + // 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. private static string BuildMsiScript(string baseUrl, string token, string packageUrl, bool selfSigned) => $$""" #Requires -RunAsAdministrator $ErrorActionPreference = 'Stop' @@ -132,7 +133,7 @@ public static class InstallEndpoints $msi = Join-Path $env:TEMP ('ExecuteRAgent-' + [guid]::NewGuid().ToString('N') + '.msi') try { Invoke-WebRequest -Uri '{{packageUrl}}' -OutFile $msi -UseBasicParsing - $arguments = @('/i', ('"' + $msi + '"'), '/qn', "SERVERURL=$server", "BOOTSTRAPTOKEN=$token"{{(selfSigned ? ", 'TRUSTMODE=pinned'" : "")}}) + $arguments = @('/i', ('"' + $msi + '"'), '/qn', "SERVERURL=$server", "BOOTSTRAPTOKEN=$token"{{(selfSigned ? ", 'CAINSTALL=1'" : "")}}) $p = Start-Process msiexec.exe -ArgumentList $arguments -Wait -PassThru if ($p.ExitCode -ne 0) { throw "msiexec exited with $($p.ExitCode)" } Write-Host 'ExecuteR Agent installed. It will enroll and start automatically.' @@ -172,7 +173,7 @@ public static class InstallEndpoints fi chmod +x "$dest/executer-agent" {{(selfSigned ? "# Trust the server's managed self-signed CA for host tooling too.\n curl -fsSLk \"" + baseUrl + ApiRoutes.EnrollmentCaChain + "\" -o /tmp/executer-ca.pem 2>/dev/null && {\n if [ -d /usr/local/share/ca-certificates ]; then cp /tmp/executer-ca.pem /usr/local/share/ca-certificates/executer-ca.crt && update-ca-certificates >/dev/null 2>&1 || true;\n elif [ -d /etc/pki/ca-trust/source/anchors ]; then cp /tmp/executer-ca.pem /etc/pki/ca-trust/source/anchors/executer-ca.crt && update-ca-trust >/dev/null 2>&1 || true; fi; rm -f /tmp/executer-ca.pem; }" : "")}} - "$dest/executer-agent" configure --url "$server" --token "$token"{{(selfSigned ? " --trust-mode pinned" : "")}} + "$dest/executer-agent" configure --url "$server" --token "$token"{{(selfSigned ? " --install-server-ca" : "")}} "$dest/executer-agent" initialize echo 'ExecuteR Agent installed. It will enroll and start automatically.' """; @@ -195,7 +196,7 @@ public static class InstallEndpoints New-Item -Path 'HKLM:\SOFTWARE\ExecuteR' -Force | Out-Null Set-ItemProperty -Path 'HKLM:\SOFTWARE\ExecuteR' -Name 'ServerURL' -Value $server Set-ItemProperty -Path 'HKLM:\SOFTWARE\ExecuteR' -Name 'BootstrapToken' -Value $token - {{(selfSigned ? "Set-ItemProperty -Path 'HKLM:\\SOFTWARE\\ExecuteR' -Name 'TlsTrustMode' -Value 'pinned'" : "")}} + {{(selfSigned ? "Set-ItemProperty -Path 'HKLM:\\SOFTWARE\\ExecuteR' -Name 'ServerCaAutoInstall' -Value 1 -Type DWord" : "")}} & $exe initialize Write-Host 'ExecuteR Agent installed and starting.' } diff --git a/src/Backend/Tls/TlsSettings.cs b/src/Backend/Tls/TlsSettings.cs index 19034c1..e27f6dd 100644 --- a/src/Backend/Tls/TlsSettings.cs +++ b/src/Backend/Tls/TlsSettings.cs @@ -50,19 +50,21 @@ public sealed class TlsSettings var dnsNames = new List { "localhost" }; var ipAddresses = new List { IPAddress.Loopback, IPAddress.IPv6Loopback }; - // Verification is off at the proxy, so the SAN set only needs to be - // friendly for direct browsing: hostname, FQDN and the bind address. + // Cover every way this host is reachable directly: the hostname, the + // hostname under each adapter's connection-specific DNS suffix, the + // resolved FQDN, every IP on every adapter, plus localhost/loopback. + string? hostName = null; try { - var host = Dns.GetHostName(); - AddDns(dnsNames, host); + hostName = Dns.GetHostName(); + AddDns(dnsNames, hostName); try { - AddDns(dnsNames, Dns.GetHostEntry(host).HostName); + AddDns(dnsNames, Dns.GetHostEntry(hostName).HostName); } catch (Exception) { - // No resolvable FQDN — hostname and localhost suffice. + // No resolvable FQDN — hostname and suffix-derived names suffice. } } catch (Exception) @@ -70,6 +72,8 @@ public sealed class TlsSettings // Hostname unavailable; localhost suffices. } + AddNetworkNamesAndAddresses(dnsNames, ipAddresses, hostName); + foreach (var url in (configuration["urls"] ?? string.Empty) .Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) { @@ -104,6 +108,60 @@ public sealed class TlsSettings }; } + /// + /// Walks every operational network adapter: adds each adapter's DNS suffix + /// (and hostname.suffix), the global domain name, and every unicast IP. + /// + private static void AddNetworkNamesAndAddresses( + List dnsNames, List ipAddresses, string? hostName) + { + try + { + var domain = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName; + if (!string.IsNullOrWhiteSpace(domain) && hostName is { Length: > 0 }) + { + AddDns(dnsNames, $"{hostName}.{domain}"); + } + } + catch (Exception) + { + // Global properties unavailable on this host. + } + + try + { + foreach (var nic in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()) + { + if (nic.OperationalStatus != System.Net.NetworkInformation.OperationalStatus.Up) + { + continue; + } + + var props = nic.GetIPProperties(); + if (hostName is { Length: > 0 } && !string.IsNullOrWhiteSpace(props.DnsSuffix)) + { + AddDns(dnsNames, $"{hostName}.{props.DnsSuffix}"); + } + + foreach (var unicast in props.UnicastAddresses) + { + var ip = unicast.Address; + // Skip link-local (fe80::) — noisy and not a stable SAN. + if (ip.IsIPv6LinkLocal || ipAddresses.Contains(ip)) + { + continue; + } + + ipAddresses.Add(ip); + } + } + } + catch (Exception) + { + // Adapter enumeration unavailable; the base SANs still apply. + } + } + private static void AddDns(List names, string candidate) { if (!string.IsNullOrWhiteSpace(candidate) diff --git a/src/Installer/Policies/ExecuteR.admx b/src/Installer/Policies/ExecuteR.admx index a21077f..4275078 100644 --- a/src/Installer/Policies/ExecuteR.admx +++ b/src/Installer/Policies/ExecuteR.admx @@ -143,20 +143,15 @@ - - + + valueName="ServerCaAutoInstall"> - - - system - pinned - insecure - - + + diff --git a/src/Installer/Policies/en-US/ExecuteR.adml b/src/Installer/Policies/en-US/ExecuteR.adml index 693098f..6bbd12d 100644 --- a/src/Installer/Policies/en-US/ExecuteR.adml +++ b/src/Installer/Policies/en-US/ExecuteR.adml @@ -113,19 +113,16 @@ Do not enable this for servers reached through a proxy with a publicly trusted c The agent reads configuration at service start; restart the "ExecuteR Agent" service (ExecuteRAgent) to apply a change immediately. - Server certificate trust mode - How the agent decides whether to trust the ExecuteR server's certificate. + Install the server's self-signed CA + Installs the ExecuteR server's managed self-signed CA (root + intermediate) into the machine trust store, and keeps it current when the server rotates its chain. -System (default): validate against the OS trust store — for a server reached through a reverse proxy with a publicly trusted certificate. +If you enable this policy, ordinary system certificate validation trusts a direct or local connection to a self-signed server. It is not certificate pinning and it is not forced: if the server is later reached through a reverse proxy presenting a publicly trusted certificate, that certificate validates normally and the installed self-signed CA simply goes unused. -Pinned: fetch the server's managed self-signed CA (root + intermediate), pin it, and validate against it — refreshing automatically when the server rotates its chain, so trust never lapses. Use this for a self-signed server without a proxy. +If you disable or do not configure this policy, the agent does not install the CA. Use this for a self-signed server without a proxy; leave it off when a real edge certificate is in front. -Insecure: accept any certificate (the connection is still encrypted, but the certificate is not verified). Prefer Pinned. +For an accept-any escape hatch (no validation at all), see "Skip TLS certificate verification" instead. The agent reads configuration at service start; restart the "ExecuteR Agent" service (ExecuteRAgent) to apply a change immediately. - System trust store (default) - Pinned self-signed CA (auto-renewed) - Insecure (accept any) Outbound proxy URL Routes the agent's outbound traffic (enrollment HTTPS and the persistent WebSocket connection) through an explicit proxy, for example http://proxy.example.com:8080. @@ -166,9 +163,6 @@ The agent reads configuration at service start; restart the "ExecuteR Agent" ser - - Trust mode: - diff --git a/src/Installer/Product.wxs b/src/Installer/Product.wxs index db3e0d4..17521cc 100644 --- a/src/Installer/Product.wxs +++ b/src/Installer/Product.wxs @@ -5,7 +5,7 @@ - - + + + @@ -155,9 +157,9 @@ - + + Name="ServerCaAutoInstall" Type="integer" Value="[CAINSTALL]" KeyPath="yes" /> @@ -232,9 +234,9 @@ Text="Blank keeps values already configured (upgrades), or set later via registry / Group Policy. The service enrolls once both values are present. Silent: SERVERURL=... BOOTSTRAPTOKEN=..." /> - + Trust self-signed certificate - (self-signed server without a proxy — the agent pins the CA and auto-renews) + (self-signed server without a proxy — installs its CA so system trust works)
diff --git a/src/WebUI/src/routes/settings/+page.svelte b/src/WebUI/src/routes/settings/+page.svelte index 7472cac..2afdf7c 100644 --- a/src/WebUI/src/routes/settings/+page.svelte +++ b/src/WebUI/src/routes/settings/+page.svelte @@ -136,7 +136,7 @@
{installCommand}
diff --git a/tests/Backend.Tests/InstallScriptTests.cs b/tests/Backend.Tests/InstallScriptTests.cs index cace03c..d6a6870 100644 --- a/tests/Backend.Tests/InstallScriptTests.cs +++ b/tests/Backend.Tests/InstallScriptTests.cs @@ -163,20 +163,20 @@ public class InstallScriptTests(BackendTestFixture fixture) : IClassFixture Date: Sun, 9 Aug 2026 17:47:18 -0400 Subject: [PATCH 4/4] Self-signed cert SANs include the public IP (cloud metadata + echo) [skip ci] Enumerating adapters only sees a cloud VM's private/NAT IP, so the public address (Azure/GCP/AWS metadata service, then an api.ipify.org echo fallback) is discovered and added to the SANs, plus a configured PUBLIC_SERVER_URL host. Best-effort and time-bounded (3s cap), and gated on self-signed TLS being enabled so it makes no network calls in tests or when TLS is off. Verified: the public IP lands in the cert. Co-Authored-By: Claude Fable 5 --- src/Backend/Tls/TlsSettings.cs | 130 ++++++++++++++++++++++++++++++++- 1 file changed, 129 insertions(+), 1 deletion(-) diff --git a/src/Backend/Tls/TlsSettings.cs b/src/Backend/Tls/TlsSettings.cs index e27f6dd..d679c37 100644 --- a/src/Backend/Tls/TlsSettings.cs +++ b/src/Backend/Tls/TlsSettings.cs @@ -43,6 +43,7 @@ public sealed class TlsSettings public static TlsSettings FromConfiguration(IConfiguration configuration) { + var enabled = configuration["TLS_SELF_SIGNED"] != "0"; var certDir = configuration["TLS_CERT_DIR"]; var dataDir = Path.GetDirectoryName(Path.GetFullPath( configuration["DATABASE_PATH"] is { Length: > 0 } db ? db : "data/app.db")) ?? "data"; @@ -74,6 +75,33 @@ public sealed class TlsSettings AddNetworkNamesAndAddresses(dnsNames, ipAddresses, hostName); + // A cloud VM's public IP lives on the provider's edge, not the NIC — + // discover it (metadata service, then an echo) so direct HTTPS to the + // 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) + { + AddPublicIpAddresses(ipAddresses); + } + + // A configured public URL's host (name or IP) belongs in the SANs too. + if (configuration["PUBLIC_SERVER_URL"] is { Length: > 0 } publicUrl + && Uri.TryCreate(publicUrl, UriKind.Absolute, out var publicUri)) + { + if (IPAddress.TryParse(publicUri.Host, out var publicIp)) + { + if (!ipAddresses.Contains(publicIp)) + { + ipAddresses.Add(publicIp); + } + } + else + { + AddDns(dnsNames, publicUri.Host); + } + } + foreach (var url in (configuration["urls"] ?? string.Empty) .Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) { @@ -100,7 +128,7 @@ public sealed class TlsSettings return new TlsSettings { - Enabled = configuration["TLS_SELF_SIGNED"] != "0", + Enabled = enabled, CertificateDirectory = certDir is { Length: > 0 } ? certDir : Path.Combine(dataDir, "tls"), UseWindowsStore = OperatingSystem.IsWindows() && certDir is not { Length: > 0 }, DnsNames = dnsNames, @@ -162,6 +190,106 @@ public sealed class TlsSettings } } + /// + /// Best-effort public IP discovery so a cloud instance's public address is + /// in the cert. Tries the local cloud metadata services (Azure/GCP/AWS — + /// fast-fail off-cloud) then a public echo, within a small overall budget. + /// + private static void AddPublicIpAddresses(List ipAddresses) + { + try + { + foreach (var raw in DiscoverPublicIpsAsync().GetAwaiter().GetResult()) + { + if (IPAddress.TryParse(raw, out var ip) && !ipAddresses.Contains(ip) + && !IsPrivateOrReserved(ip)) + { + ipAddresses.Add(ip); + } + } + } + catch (Exception) + { + // Never let discovery block or fail certificate generation. + } + } + + private static async Task> DiscoverPublicIpsAsync() + { + using var budget = new CancellationTokenSource(TimeSpan.FromSeconds(3)); + var found = new List(); + + async Task GetAsync(string url, (string, string)? header, TimeSpan timeout) + { + try + { + using var http = new HttpClient { Timeout = timeout }; + using var request = new HttpRequestMessage(HttpMethod.Get, url); + if (header is { } h) + { + request.Headers.TryAddWithoutValidation(h.Item1, h.Item2); + } + + using var response = await http.SendAsync(request, budget.Token); + if (!response.IsSuccessStatusCode) + { + return null; + } + + return (await response.Content.ReadAsStringAsync(budget.Token)).Trim().Trim('"'); + } + catch (Exception) + { + return null; + } + } + + // Local metadata services fail fast when this isn't that cloud. + var azure = await GetAsync( + "http://169.254.169.254/metadata/instance/network/interface/0/ipv4/ipAddress/0/publicIpAddress?api-version=2021-02-01&format=text", + ("Metadata", "true"), TimeSpan.FromMilliseconds(800)); + if (!string.IsNullOrWhiteSpace(azure)) { found.Add(azure); } + + var gcp = await GetAsync( + "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip", + ("Metadata-Flavor", "Google"), TimeSpan.FromMilliseconds(800)); + if (!string.IsNullOrWhiteSpace(gcp)) { found.Add(gcp); } + + var aws = await GetAsync( + "http://169.254.169.254/latest/meta-data/public-ipv4", null, TimeSpan.FromMilliseconds(800)); + if (!string.IsNullOrWhiteSpace(aws)) { found.Add(aws); } + + // Provider-agnostic fallback if metadata yielded nothing. + if (found.Count == 0) + { + var echo = await GetAsync("https://api.ipify.org", null, TimeSpan.FromSeconds(1.5)); + if (!string.IsNullOrWhiteSpace(echo)) { found.Add(echo); } + } + + return found; + } + + /// True for loopback, link-local, and RFC1918/CGNAT/ULA ranges. + private static bool IsPrivateOrReserved(IPAddress ip) + { + if (IPAddress.IsLoopback(ip) || ip.IsIPv6LinkLocal || ip.IsIPv6SiteLocal || ip.IsIPv6UniqueLocal) + { + return true; + } + + if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) + { + var b = ip.GetAddressBytes(); + return b[0] == 10 + || (b[0] == 172 && b[1] >= 16 && b[1] <= 31) + || (b[0] == 192 && b[1] == 168) + || (b[0] == 169 && b[1] == 254) // link-local / APIPA + || (b[0] == 100 && b[1] >= 64 && b[1] <= 127); // CGNAT + } + + return false; + } + private static void AddDns(List names, string candidate) { if (!string.IsNullOrWhiteSpace(candidate) -- 2.52.0