diff --git a/.env.example b/.env.example index 90f1d1a..28e51f1 100644 --- a/.env.example +++ b/.env.example @@ -92,6 +92,11 @@ FORWARD_LIMIT=1 TLS_SELF_SIGNED=0 TLS_CERT_DIR= +# ── Execution history ────────────────────────────────────────────────────── +# Newest jobs kept per device (older jobs and their stored output are +# pruned). 0 disables pruning. +JOB_HISTORY_LIMIT=200 + # ── Initial local admin (first startup only) ─────────────────────────────── INITIAL_ADMIN_USERNAME=admin # Generate: openssl rand -base64 24 diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index e0d219a..953e0d7 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -112,12 +112,12 @@ jobs: -Runtimes win-x64,win-arm64,linux-x64,linux-arm64,osx-x64,osx-arm64 $stage = 'artifacts/agent-package' New-Item -ItemType Directory -Force $stage | Out-Null - $msi = "artifacts/Agent-${{ steps.ver.outputs.version }}.msi" - $zip = "artifacts/Agent-${{ steps.ver.outputs.version }}-win-x64.zip" + $msi = "artifacts/ExecuteR-Agent-${{ steps.ver.outputs.version }}.msi" + $zip = "artifacts/ExecuteR-Agent-${{ steps.ver.outputs.version }}-win-x64.zip" if (Test-Path $msi) { Copy-Item $msi "$stage/Agent.msi" } if (Test-Path $zip) { Copy-Item $zip "$stage/Agent.zip" } foreach ($rid in 'linux-x64','linux-arm64','osx-x64','osx-arm64') { - $unix = "artifacts/Agent-${{ steps.ver.outputs.version }}-$rid.zip" + $unix = "artifacts/ExecuteR-Agent-${{ steps.ver.outputs.version }}-$rid.zip" if (Test-Path $unix) { Copy-Item $unix "$stage/Agent-$rid.zip" } } '${{ steps.ver.outputs.version }}' | Set-Content "$stage/VERSION" -NoNewline @@ -143,12 +143,12 @@ jobs: with: name: binaries path: | - artifacts/Agent-*.zip - artifacts/Agent-*.msi - artifacts/Agent-*-checksums.txt - artifacts/Server-*.zip - artifacts/Server-*.msi - artifacts/Server-*-checksums.txt + artifacts/ExecuteR-Agent-*.zip + artifacts/ExecuteR-Agent-*.msi + artifacts/ExecuteR-Agent-*-checksums.txt + artifacts/ExecuteR-Server-*.zip + artifacts/ExecuteR-Server-*.msi + artifacts/ExecuteR-Server-*-checksums.txt retention-days: 7 images: diff --git a/Directory.Build.props b/Directory.Build.props index 872b1d3..375fc30 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -11,7 +11,12 @@ $(UnifiedVersion) $(UnifiedVersion) $(UnifiedVersion) - $(UnifiedVersion) + + $(UnifiedVersion)-$(GitCommitShort) + $(UnifiedVersion) + false + ExecuteR diff --git a/README.md b/README.md index f2f464e..9f3d8ab 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ updates with **no SignalR, SSE, or polling**. (x64 + arm64, .NET 10), running as a Windows service / systemd unit / launchd daemon. Deterministic hardware identity, secure one-time bootstrap, hardware inventory, in-process PowerShell (System.Management.Automation) - plus PowerShell 5 and Bash engines, ConPTY remote shell (Windows), - presigned-URL file delivery. + plus PowerShell 5 and Bash engines, interactive remote shell (ConPTY on + Windows, forkpty on Linux/macOS), presigned-URL file delivery. - **Backend** — ASP.NET Core control plane: REST API, SQLite, local + OIDC auth, RBAC, NATS operator/JWT issuance, JetStream command dispatch, live event relay. @@ -327,7 +327,8 @@ docker compose up -d --build For a quick spin-up without containers, one self-contained `ExecuteR-Server` binary serves the WebUI (static SPA), the API, the `/messaging/*` WebSocket relay, and supervises a `nats-server` sidecar. CI publishes -`Server--win-x64.zip`, `…-linux-x64.zip` **and `Server-.msi`** +`ExecuteR-Server--win-x64.zip`, `…-linux-x64.zip` **and +`ExecuteR-Server-.msi`** (build locally with `./scripts/Package-Server.ps1`): ```text @@ -399,26 +400,21 @@ packages — lives inside its own directory, so instances never share state. ## Networking and TLS -ExecuteR serves **plain HTTP only — SSL/TLS termination is not supported in -the service itself and will not be added**. Terminating TLS inside every -application is yesterday's pattern; the state of the art is a dedicated edge -appliance or service that terminates TLS once, renews certificates -automatically (ACME/Let's Encrypt) and gives you layer-7 visibility — access -logs, request metrics, rate limiting, WAF hooks — for everything behind it: +ExecuteR encrypts its own listener **by default** with a self-managed +certificate chain — see [Self-Signed TLS](#self-signed-tls-on-by-default) +below. Public TLS termination still belongs at a dedicated edge: it +terminates TLS once with real certificates, renews them automatically via +ACME, and gives you layer-7 visibility — access logs, request metrics, rate +limiting, WAF hooks — for everything behind it. -| Edge | Notes | -|------|-------| -| [Caddy](https://caddyserver.com) | Automatic HTTPS out of the box; single binary, runs fine on **Windows** or Linux | -| [nginx](https://nginx.org) + [Nginx UI](https://nginxui.com) | The classic; Nginx UI adds a web console with ACME renewal and also installs on **Windows** | -| [HAProxy](https://www.haproxy.org) | High-throughput L4/L7 balancing and fine-grained traffic policy | -| [Zoraxy](https://zoraxy.aroz.org) | All-in-one reverse proxy with a friendly UI | +The recommended edge is **[Nginx UI](https://nginxui.com)** — a web console +for nginx with ACME renewal built in, and it installs on **Windows** too. -Put any of these in front on `:443` and forward to ExecuteR over HTTP. -Because forwarded headers are trusted from private address space by default -(next section), this works with zero ExecuteR-side configuration — the edge -handles certificates and encryption, ExecuteR handles the rest. For the -standalone Windows server the shortest path is installing Caddy (or Nginx UI -if you want a console) on the same host and proxying `localhost:9080`. +Put it in front on `:443` and forward the routes below. Because forwarded +headers are trusted from private address space by default (next section), +this works with zero ExecuteR-side configuration. For the standalone +Windows server the shortest path is installing Nginx UI on the same host +and proxying `localhost:9080`. ### Self-Signed TLS (on by default) @@ -535,7 +531,9 @@ instead of the registry, credentials under `/var/lib/executer` (mode 600). On startup the agent idempotently installs PowerShell 7 when missing (Homebrew on macOS, the official install script on Linux) — scripts run via the embedded PowerShell SDK either way, so this only matters for operator convenience. -Remote shell is Windows-only for now. +Remote shell works on every platform — ConPTY on Windows, a forkpty-backed +PTY (with a real controlling terminal, so job control and Ctrl-C behave) on +Linux and macOS. --- @@ -1050,7 +1048,7 @@ unchanged build is skipped (the object key is content-addressed by hash). For a local build without CI: ```powershell -./scripts/Package-Agent.ps1 # writes artifacts/Agent-.msi +./scripts/Package-Agent.ps1 # writes artifacts/ExecuteR-Agent-.msi # then mount that directory at /data/agent, or set AGENT_PACKAGE_PATH ``` diff --git a/docs/screenshots/add-device.png b/docs/screenshots/add-device.png index 8d20fe0..69d2211 100644 Binary files a/docs/screenshots/add-device.png and b/docs/screenshots/add-device.png differ diff --git a/docs/screenshots/dashboard-light.png b/docs/screenshots/dashboard-light.png index fa1e7ae..4ceef89 100644 Binary files a/docs/screenshots/dashboard-light.png and b/docs/screenshots/dashboard-light.png differ diff --git a/docs/screenshots/dashboard.png b/docs/screenshots/dashboard.png index a60ac1a..cde1910 100644 Binary files a/docs/screenshots/dashboard.png and b/docs/screenshots/dashboard.png differ diff --git a/docs/screenshots/device-detail.png b/docs/screenshots/device-detail.png index 51a187f..f5537a6 100644 Binary files a/docs/screenshots/device-detail.png and b/docs/screenshots/device-detail.png differ diff --git a/docs/screenshots/device-execution-history.png b/docs/screenshots/device-execution-history.png new file mode 100644 index 0000000..3c058c2 Binary files /dev/null and b/docs/screenshots/device-execution-history.png differ diff --git a/docs/screenshots/devices.png b/docs/screenshots/devices.png index 06a867f..e535f0d 100644 Binary files a/docs/screenshots/devices.png and b/docs/screenshots/devices.png differ diff --git a/docs/screenshots/functions.png b/docs/screenshots/functions.png index 5c2beca..6d3cee0 100644 Binary files a/docs/screenshots/functions.png and b/docs/screenshots/functions.png differ diff --git a/docs/screenshots/jobs.png b/docs/screenshots/jobs.png index 72add6d..38f9d96 100644 Binary files a/docs/screenshots/jobs.png and b/docs/screenshots/jobs.png differ diff --git a/docs/screenshots/script-execution.png b/docs/screenshots/script-execution.png index 918f91b..b9f5da6 100644 Binary files a/docs/screenshots/script-execution.png and b/docs/screenshots/script-execution.png differ diff --git a/docs/screenshots/settings.png b/docs/screenshots/settings.png index 125c98a..1ca4925 100644 Binary files a/docs/screenshots/settings.png and b/docs/screenshots/settings.png differ diff --git a/scripts/Package-Agent.ps1 b/scripts/Package-Agent.ps1 index 22449c0..f27dc8e 100644 --- a/scripts/Package-Agent.ps1 +++ b/scripts/Package-Agent.ps1 @@ -47,10 +47,15 @@ foreach ($rid in $Runtimes) { -p:PublishSingleFile=true ` -p:IncludeNativeLibrariesForSelfExtract=true ` -p:EnableCompressionInSingleFile=true ` + -p:DebugType=None ` "-p:UnifiedVersion=$Version" ` + "-p:GitCommitShort=$(git -C $repoRoot rev-parse --short HEAD 2>$null)" ` -o $ridPublishDir if ($LASTEXITCODE -ne 0) { throw "Agent publish for $rid failed ($LASTEXITCODE)." } + # Shipping bundles carry no debug symbols. + Get-ChildItem $ridPublishDir -Filter '*.pdb' -Recurse | Remove-Item -Force + # A recognizable binary name on unix; the MSI harvest expects Agent.exe. if ($rid -notlike 'win-*') { Rename-Item (Join-Path $ridPublishDir 'Agent') 'executer-agent' @@ -58,7 +63,7 @@ foreach ($rid in $Runtimes) { Copy-Item $modulesStage (Join-Path $ridPublishDir 'Modules') -Recurse - $zipPath = Join-Path $artifacts "Agent-$Version-$rid.zip" + $zipPath = Join-Path $artifacts "ExecuteR-Agent-$Version-$rid.zip" if (Test-Path $zipPath) { Remove-Item $zipPath -Force } Compress-Archive -Path (Join-Path $ridPublishDir '*') -DestinationPath $zipPath Write-Host "Created $zipPath" @@ -85,15 +90,15 @@ if (-not $SkipMsi -and (Test-Path $wixProject) -and (Test-Path $publishDir)) { $msi = Get-ChildItem (Join-Path $repoRoot 'src/Installer/bin/Release') -Filter '*.msi' -Recurse | Sort-Object LastWriteTimeUtc -Descending | Select-Object -First 1 if ($msi) { - $msiTarget = Join-Path $artifacts "Agent-$Version.msi" + $msiTarget = Join-Path $artifacts "ExecuteR-Agent-$Version.msi" Copy-Item $msi.FullName $msiTarget -Force Write-Host "Created $msiTarget" } } # Checksums for release attachment. -$checksums = Join-Path $artifacts "Agent-$Version-checksums.txt" -Get-ChildItem $artifacts -File | Where-Object { $_.Name -like "Agent-$Version*" -and $_.Extension -ne '.txt' } | ForEach-Object { +$checksums = Join-Path $artifacts "ExecuteR-Agent-$Version-checksums.txt" +Get-ChildItem $artifacts -File | Where-Object { $_.Name -like "ExecuteR-Agent-$Version*" -and $_.Extension -ne '.txt' } | ForEach-Object { "{0} {1}" -f (Get-FileHash $_.FullName -Algorithm SHA256).Hash.ToLowerInvariant(), $_.Name } | Set-Content $checksums -Encoding utf8NoBOM Write-Host "Created $checksums" diff --git a/scripts/Package-Server.ps1 b/scripts/Package-Server.ps1 index 2efa32d..4126f66 100644 --- a/scripts/Package-Server.ps1 +++ b/scripts/Package-Server.ps1 @@ -59,10 +59,16 @@ foreach ($rid in $Runtimes) { dotnet publish (Join-Path $repoRoot 'src/Backend/Backend.csproj') ` -c Release -r $rid --self-contained ` -p:PublishSingleFile=true ` + -p:DebugType=None ` -p:UnifiedVersion=$Version ` + "-p:GitCommitShort=$(git -C $repoRoot rev-parse --short HEAD 2>$null)" ` -o $publishDir if ($LASTEXITCODE -ne 0) { throw "dotnet publish for $rid failed ($LASTEXITCODE)." } + # Shipping bundles carry no debug symbols or build-time asset manifests. + Get-ChildItem $publishDir -Filter '*.pdb' -Recurse | Remove-Item -Force + Remove-Item (Join-Path $publishDir 'Backend.staticwebassets.endpoints.json') -ErrorAction SilentlyContinue + # Single-file apphosts tolerate renames; ship a recognizable binary name. if ($rid -like 'win-*') { Rename-Item (Join-Path $publishDir 'Backend.exe') 'ExecuteR-Server.exe' @@ -77,13 +83,13 @@ foreach ($rid in $Runtimes) { # ── Bundle the agent installers so URL install works out of the box ───── $agentStage = Join-Path $publishDir 'agent-package' New-Item -ItemType Directory -Force $agentStage | Out-Null - $agentMsi = Join-Path $artifacts "Agent-$Version.msi" - $agentZip = Join-Path $artifacts "Agent-$Version-win-x64.zip" + $agentMsi = Join-Path $artifacts "ExecuteR-Agent-$Version.msi" + $agentZip = Join-Path $artifacts "ExecuteR-Agent-$Version-win-x64.zip" $staged = $false if (Test-Path $agentMsi) { Copy-Item $agentMsi (Join-Path $agentStage 'Agent.msi'); $staged = $true } if (Test-Path $agentZip) { Copy-Item $agentZip (Join-Path $agentStage 'Agent.zip'); $staged = $true } foreach ($unixRid in 'linux-x64', 'linux-arm64', 'osx-x64', 'osx-arm64') { - $unixZip = Join-Path $artifacts "Agent-$Version-$unixRid.zip" + $unixZip = Join-Path $artifacts "ExecuteR-Agent-$Version-$unixRid.zip" if (Test-Path $unixZip) { Copy-Item $unixZip (Join-Path $agentStage "Agent-$unixRid.zip"); $staged = $true } } if ($staged) { @@ -167,7 +173,7 @@ TRUSTED_HOSTS, TRUST_PROXY=0 to disable. "@ # ── Zip it ─────────────────────────────────────────────────────────────── - $zipPath = Join-Path $artifacts "Server-$Version-$rid.zip" + $zipPath = Join-Path $artifacts "ExecuteR-Server-$Version-$rid.zip" if (Test-Path $zipPath) { Remove-Item $zipPath -Force } Compress-Archive -Path (Join-Path $publishDir '*') -DestinationPath $zipPath Write-Host "Created $zipPath" @@ -194,17 +200,17 @@ if (-not $SkipMsi -and (Test-Path $serverWix) -and (Test-Path $winPublish)) { $msi = Get-ChildItem (Join-Path $repoRoot 'src/Installer.Server/bin/Release') -Filter '*.msi' -Recurse | Sort-Object LastWriteTimeUtc -Descending | Select-Object -First 1 if ($msi) { - $msiTarget = Join-Path $artifacts "Server-$Version.msi" + $msiTarget = Join-Path $artifacts "ExecuteR-Server-$Version.msi" Copy-Item $msi.FullName $msiTarget -Force Write-Host "Created $msiTarget" } } # Checksums for release attachment. -$checksums = Join-Path $artifacts "Server-$Version-checksums.txt" -Get-ChildItem $artifacts -File | Where-Object { $_.Name -like "Server-$Version*" -and $_.Extension -ne '.txt' } | ForEach-Object { +$checksums = Join-Path $artifacts "ExecuteR-Server-$Version-checksums.txt" +Get-ChildItem $artifacts -File | Where-Object { $_.Name -like "ExecuteR-Server-$Version*" -and $_.Extension -ne '.txt' } | ForEach-Object { "{0} {1}" -f (Get-FileHash $_.FullName -Algorithm SHA256).Hash.ToLowerInvariant(), $_.Name } | Set-Content $checksums -Encoding utf8NoBOM Write-Host "Created $checksums" -Get-ChildItem $artifacts -Filter "Server-$Version-*.zip" | Select-Object Name, Length +Get-ChildItem $artifacts -Filter "ExecuteR-Server-$Version-*.zip" | Select-Object Name, Length diff --git a/scripts/Publish-Release.ps1 b/scripts/Publish-Release.ps1 index 31b8540..978920e 100644 --- a/scripts/Publish-Release.ps1 +++ b/scripts/Publish-Release.ps1 @@ -53,9 +53,9 @@ catch { $artifacts = Join-Path $repoRoot 'artifacts' $assets = Get-ChildItem $artifacts -File | - Where-Object { $_.Name -like "Agent-$Version*" -or $_.Name -like "Server-$Version*" } + Where-Object { $_.Name -like "ExecuteR-Agent-$Version*" -or $_.Name -like "ExecuteR-Server-$Version*" } if (-not $assets) { - throw "No artifacts matching Agent-$Version* or Server-$Version* under $artifacts." + throw "No artifacts matching ExecuteR-Agent-$Version* or ExecuteR-Server-$Version* under $artifacts." } foreach ($asset in $assets) { diff --git a/src/Agent/Agent.csproj b/src/Agent/Agent.csproj index aaf8311..8b6e8f6 100644 --- a/src/Agent/Agent.csproj +++ b/src/Agent/Agent.csproj @@ -4,10 +4,12 @@ Exe + inventory, systemd/launchd, forkpty remote shell). --> net10.0-windows;net10.0 Agent Agent + ExecuteR Agent + ExecuteR Agent win-x64;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64 true diff --git a/src/Agent/Service/AgentHost.cs b/src/Agent/Service/AgentHost.cs index d8cf7c9..7a90b26 100644 --- a/src/Agent/Service/AgentHost.cs +++ b/src/Agent/Service/AgentHost.cs @@ -37,7 +37,7 @@ public static class AgentHost var logSettings = LogSettings.FromConfiguration( builder.Configuration, LogDefaults.Agent, - defaultDirectory: Path.Combine(Branding.AgentProgramDataDirectory, "logs")) with + defaultDirectory: Path.Combine(AppContext.BaseDirectory, "Logs")) with { // The agent's level comes from its own precedence chain // (GPO > registry > default), not from the environment. diff --git a/src/Agent/Shell/ConPtySession.cs b/src/Agent/Shell/ConPtySession.cs index d8fe430..7c9989c 100644 --- a/src/Agent/Shell/ConPtySession.cs +++ b/src/Agent/Shell/ConPtySession.cs @@ -7,9 +7,9 @@ namespace Agent.Shell; /// /// A live ConPTY-backed shell process. Terminal output is delivered via -/// ; input is written with . +/// ; input is written with . /// -public sealed class ConPtySession : IDisposable +public sealed class ConPtySession : IPtySession { private IntPtr pseudoConsole = IntPtr.Zero; private IntPtr attributeList = IntPtr.Zero; @@ -28,8 +28,14 @@ public sealed class ConPtySession : IDisposable /// Raised when the shell process exits. public event Action? Exited; - public void Start(string commandLine, int cols, int rows) + public void Start(string fileName, string[] args, int cols, int rows) { + // CreateProcessW takes a single command line: quote the executable and + // append the arguments verbatim (our args carry no embedded spaces). + var commandLine = args.Length == 0 + ? $"\"{fileName}\"" + : $"\"{fileName}\" {string.Join(' ', args)}"; + // Two pipes: one the PTY reads input from, one it writes output to. if (!CreatePipe(out var inputRead, out var inputWrite, IntPtr.Zero, 0)) { diff --git a/src/Agent/Shell/IPtySession.cs b/src/Agent/Shell/IPtySession.cs new file mode 100644 index 0000000..9cf1921 --- /dev/null +++ b/src/Agent/Shell/IPtySession.cs @@ -0,0 +1,22 @@ +namespace Agent.Shell; + +/// +/// A live pseudo-terminal-backed shell process. Windows uses ConPTY +/// (); Linux/macOS use forkpty +/// (UnixPtySession). Output arrives via ; +/// input is written with . +/// +internal interface IPtySession : IDisposable +{ + /// Raised with raw terminal output bytes as they arrive. + event Action>? OutputReceived; + + /// Raised when the shell process exits. + event Action? Exited; + + void Start(string fileName, string[] args, int cols, int rows); + + Task WriteAsync(ReadOnlyMemory data, CancellationToken cancellationToken); + + void Resize(int cols, int rows); +} diff --git a/src/Agent/Shell/ShellCommandListener.cs b/src/Agent/Shell/ShellCommandListener.cs index 4f93e08..359ff18 100644 --- a/src/Agent/Shell/ShellCommandListener.cs +++ b/src/Agent/Shell/ShellCommandListener.cs @@ -10,7 +10,8 @@ namespace Agent.Shell; /// /// Subscribes to Core NATS shell commands (open/input/resize/close) and drives /// the . Shell traffic stays on Core NATS — never -/// JetStream. Honors the AllowRemoteShell policy. +/// JetStream. Honors the AllowRemoteShell policy. The PTY backend is +/// ConPTY on Windows and forkpty on Linux/macOS. /// public sealed class ShellCommandListener( NatsConnection connection, @@ -18,21 +19,10 @@ public sealed class ShellCommandListener( AgentConfiguration configuration, ILogger logger) : IDisposable { -#if WINDOWS_AGENT private readonly ShellManager manager = new(publisher, logger); -#endif public async Task RunAsync(CancellationToken cancellationToken) { -#if !WINDOWS_AGENT - // The interactive shell is ConPTY-based; unsupported off Windows. - logger.LogInformation("Remote shell is not supported on this platform; not listening for shell commands"); - _ = connection; - _ = publisher; - _ = configuration; - await Task.CompletedTask; - return; -#else if (!configuration.AllowRemoteShell.Value) { logger.LogInformation("Remote shell disabled by policy; not listening for shell commands"); @@ -58,10 +48,8 @@ public sealed class ShellCommandListener( logger.LogWarning(ex, "Failed to handle shell command on {Subject}", msg.Subject); } } -#endif } -#if WINDOWS_AGENT private async Task HandleAsync(MessageEnvelope? envelope, CancellationToken cancellationToken) { if (envelope is null) @@ -109,12 +97,5 @@ public sealed class ShellCommandListener( } } -#endif - - public void Dispose() - { -#if WINDOWS_AGENT - manager.Dispose(); -#endif - } + public void Dispose() => manager.Dispose(); } diff --git a/src/Agent/Shell/ShellManager.cs b/src/Agent/Shell/ShellManager.cs index 3b667e1..76ede54 100644 --- a/src/Agent/Shell/ShellManager.cs +++ b/src/Agent/Shell/ShellManager.cs @@ -1,4 +1,3 @@ -#if WINDOWS_AGENT using Agent.Messaging; using Microsoft.Extensions.Logging; using Shared.Contracts.Messages; @@ -9,9 +8,10 @@ using Shared.Infrastructure.Nats; namespace Agent.Shell; /// -/// Manages interactive ConPTY shell sessions for one agent. MVP allows a -/// single active session per device; opening a new one closes the previous. -/// Output is published as base64 over Core NATS with a monotonic sequence. +/// Manages interactive PTY shell sessions for one agent — ConPTY on Windows, +/// forkpty on Linux/macOS. A single active session per device; opening a new +/// one closes the previous. Output is published as base64 over Core NATS with +/// a monotonic sequence. /// public sealed class ShellManager(AgentPublisher publisher, ILogger logger) : IDisposable { @@ -21,7 +21,7 @@ public sealed class ShellManager(AgentPublisher publisher, ILogger logger) : IDi private sealed class ActiveShell { public required Guid SessionId { get; init; } - public required ConPtySession Pty { get; init; } + public required IPtySession Pty { get; init; } public required Timer IdleTimer { get; init; } public required Timer MaxLifetimeTimer { get; init; } public long Sequence; @@ -32,8 +32,8 @@ public sealed class ShellManager(AgentPublisher publisher, ILogger logger) : IDi { CloseCurrent("superseded by new session"); - var shell = ResolveShellCommand(); - var pty = new ConPtySession(); + var (fileName, args) = ResolveShellCommand(); + var pty = CreatePty(); Guid sessionId = command.SessionId; var active = new ActiveShell @@ -54,9 +54,9 @@ public sealed class ShellManager(AgentPublisher publisher, ILogger logger) : IDi try { - pty.Start(shell, Math.Max(command.Cols, 1), Math.Max(command.Rows, 1)); + pty.Start(fileName, args, Math.Max(command.Cols, 1), Math.Max(command.Rows, 1)); PublishState(sessionId, ShellSessionState.Open, null); - logger.LogInformation("Shell session {SessionId} opened ({Shell})", sessionId, shell); + logger.LogInformation("Shell session {SessionId} opened ({Shell})", sessionId, fileName); } catch (Exception ex) { @@ -173,22 +173,49 @@ public sealed class ShellManager(AgentPublisher publisher, ILogger logger) : IDi _ = reason; } + private static IPtySession CreatePty() => +#if WINDOWS_AGENT + new ConPtySession(); +#else + new UnixPtySession(); +#endif + +#if WINDOWS_AGENT /// Prefers PowerShell 7 (pwsh) when present, else Windows PowerShell. - private static string ResolveShellCommand() + private static (string FileName, string[] Args) ResolveShellCommand() { var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); var pwsh = Path.Combine(programFiles, "PowerShell", "7", "pwsh.exe"); if (File.Exists(pwsh)) { - return $"\"{pwsh}\" -NoLogo"; + return (pwsh, ["-NoLogo"]); } var windowsPowerShell = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.System), "WindowsPowerShell", "v1.0", "powershell.exe"); - return $"\"{windowsPowerShell}\" -NoLogo"; + return (windowsPowerShell, ["-NoLogo"]); } +#else + /// Honors $SHELL, then falls back to bash/zsh/sh. + private static (string FileName, string[] Args) ResolveShellCommand() + { + var candidates = new[] + { + Environment.GetEnvironmentVariable("SHELL"), + "/bin/bash", "/bin/zsh", "/bin/sh", + }; + foreach (var candidate in candidates) + { + if (!string.IsNullOrEmpty(candidate) && File.Exists(candidate)) + { + return (candidate, []); + } + } + + return ("/bin/sh", []); + } +#endif public void Dispose() => CloseCurrent("agent shutting down"); } -#endif diff --git a/src/Agent/Shell/UnixPtySession.cs b/src/Agent/Shell/UnixPtySession.cs new file mode 100644 index 0000000..e544d59 --- /dev/null +++ b/src/Agent/Shell/UnixPtySession.cs @@ -0,0 +1,329 @@ +#if !WINDOWS_AGENT +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +namespace Agent.Shell; + +/// +/// A live forkpty-backed shell on Linux/macOS. forkpty performs +/// openpty + fork + setsid + TIOCSCTTY + dup2 in one native call, so the +/// child gets a proper controlling terminal (job control, Ctrl-C, and TTY +/// detection all work). Terminal output is delivered via +/// ; input is written with +/// . +/// +internal sealed class UnixPtySession : IPtySession +{ + private int masterFd = -1; + private int childPid = -1; + private FileStream? io; + private readonly CancellationTokenSource lifetime = new(); + private bool disposed; + + public event Action>? OutputReceived; + public event Action? Exited; + + public void Start(string fileName, string[] args, int cols, int rows) + { + // Everything the forked child touches must be prepared HERE: after + // fork the child has a single thread and any managed allocation, + // JIT or lock acquisition can deadlock. The child therefore only + // calls execve/_exit, both passed pre-marshaled pointers. + var argv = BuildStringArray(fileName, args); + var envp = BuildEnvironment(); + var pathPtr = Marshal.StringToHGlobalAnsi(fileName); + + var winPtr = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.StructureToPtr( + new WinSize + { + ws_row = (ushort)Math.Clamp(rows, 1, ushort.MaxValue), + ws_col = (ushort)Math.Clamp(cols, 1, ushort.MaxValue), + }, + winPtr, false); + var masterPtr = Marshal.AllocHGlobal(sizeof(int)); + + // Pre-JIT the child's only calls so no compilation happens post-fork. + TryPrepare(nameof(Native.execve)); + TryPrepare(nameof(Native._exit)); + + var pid = Native.forkpty(masterPtr, IntPtr.Zero, IntPtr.Zero, winPtr); + if (pid == 0) + { + // CHILD — exec only, no managed work. + Native.execve(pathPtr, argv, envp); + Native._exit(127); + return; + } + + try + { + if (pid < 0) + { + throw new InvalidOperationException( + $"forkpty failed (errno {Marshal.GetLastPInvokeError()})."); + } + + childPid = pid; + masterFd = Marshal.ReadInt32(masterPtr); + io = new FileStream(new SafeFileHandle((IntPtr)masterFd, ownsHandle: true), FileAccess.ReadWrite); + + _ = Task.Run(PumpOutputAsync); + _ = Task.Run(WatchProcessAsync); + } + finally + { + FreeStringArray(argv); + FreeStringArray(envp); + Marshal.FreeHGlobal(pathPtr); + Marshal.FreeHGlobal(winPtr); + Marshal.FreeHGlobal(masterPtr); + } + } + + public async Task WriteAsync(ReadOnlyMemory data, CancellationToken cancellationToken) + { + if (io is null) + { + return; + } + + await io.WriteAsync(data, cancellationToken); + await io.FlushAsync(cancellationToken); + } + + public void Resize(int cols, int rows) + { + if (masterFd < 0) + { + return; + } + + var win = new WinSize + { + ws_row = (ushort)Math.Clamp(rows, 1, ushort.MaxValue), + ws_col = (ushort)Math.Clamp(cols, 1, ushort.MaxValue), + }; + _ = Native.ioctl(masterFd, Native.TIOCSWINSZ, ref win); + } + + private async Task PumpOutputAsync() + { + var buffer = new byte[4096]; + try + { + while (!lifetime.IsCancellationRequested) + { + var read = await io!.ReadAsync(buffer, lifetime.Token); + if (read == 0) + { + break; + } + + OutputReceived?.Invoke(buffer.AsMemory(0, read).ToArray()); + } + } + catch (Exception) + { + // EIO on the master when the slave closes (child exited), or the + // stream was disposed on shutdown. + } + } + + private async Task WatchProcessAsync() + { + try + { + while (!lifetime.IsCancellationRequested) + { + if (Native.waitpid(childPid, out _, Native.WNOHANG) == childPid) + { + break; + } + + await Task.Delay(250, lifetime.Token); + } + } + catch (OperationCanceledException) + { + return; + } + + Exited?.Invoke(); + } + + public void Dispose() + { + if (disposed) + { + return; + } + + disposed = true; + lifetime.Cancel(); + + // Closing the master hangs up the slave (SIGHUP → the shell exits); + // send SIGTERM as well in case it ignores the hangup, then reap. + if (childPid > 0) + { + _ = Native.kill(childPid, Native.SIGTERM); + } + + try + { + io?.Dispose(); + } + catch (IOException) + { + // Master already closed. + } + + if (childPid > 0) + { + _ = Native.waitpid(childPid, out _, 0); + } + + lifetime.Dispose(); + } + + private static void TryPrepare(string method) + { + try + { + RuntimeHelpers.PrepareMethod(typeof(Native).GetMethod(method)!.MethodHandle); + } + catch (Exception) + { + // Best effort; the signatures are fully blittable so the stubs + // are trivial even if preparation is skipped. + } + } + + /// Null-terminated array of ANSI char* : { argv0, args..., NULL }. + private static IntPtr BuildStringArray(string first, string[] rest) + { + var items = new List { first }; + items.AddRange(rest); + return Allocate(items); + } + + private static IntPtr BuildEnvironment() + { + var items = new List(); + var hasTerm = false; + foreach (System.Collections.DictionaryEntry entry in Environment.GetEnvironmentVariables()) + { + var key = (string)entry.Key; + items.Add($"{key}={entry.Value}"); + if (string.Equals(key, "TERM", StringComparison.Ordinal)) + { + hasTerm = true; + } + } + + if (!hasTerm) + { + items.Add("TERM=xterm-256color"); + } + + return Allocate(items); + } + + private static IntPtr Allocate(List items) + { + var array = Marshal.AllocHGlobal(IntPtr.Size * (items.Count + 1)); + for (var i = 0; i < items.Count; i++) + { + Marshal.WriteIntPtr(array, i * IntPtr.Size, Marshal.StringToHGlobalAnsi(items[i])); + } + + Marshal.WriteIntPtr(array, items.Count * IntPtr.Size, IntPtr.Zero); + return array; + } + + private static void FreeStringArray(IntPtr array) + { + if (array == IntPtr.Zero) + { + return; + } + + for (var i = 0; ; i++) + { + var ptr = Marshal.ReadIntPtr(array, i * IntPtr.Size); + if (ptr == IntPtr.Zero) + { + break; + } + + Marshal.FreeHGlobal(ptr); + } + + Marshal.FreeHGlobal(array); + } + + [StructLayout(LayoutKind.Sequential)] + private struct WinSize + { + public ushort ws_row; + public ushort ws_col; + public ushort ws_xpixel; + public ushort ws_ypixel; + } + + private static class Native + { + // forkpty lives in libutil on Linux and libSystem (libc) on macOS. + private const string PtyLib = "executer-pty"; + + public const int WNOHANG = 1; + public const int SIGTERM = 15; + + // TIOCSWINSZ differs per platform (_IOW('t', 103, struct winsize)). + public static readonly nuint TIOCSWINSZ = + OperatingSystem.IsMacOS() ? 0x80087467u : 0x5414u; + + static Native() + { + NativeLibrary.SetDllImportResolver(typeof(Native).Assembly, (name, _, _) => + { + if (name != PtyLib) + { + return IntPtr.Zero; + } + + foreach (var candidate in OperatingSystem.IsMacOS() + ? ["libSystem.dylib", "libc"] + : new[] { "libutil.so.1", "libutil.so", "libc" }) + { + if (NativeLibrary.TryLoad(candidate, out var handle)) + { + return handle; + } + } + + return IntPtr.Zero; + }); + } + + [DllImport(PtyLib, SetLastError = true)] + public static extern int forkpty(IntPtr amaster, IntPtr name, IntPtr termp, IntPtr winp); + + [DllImport("libc", SetLastError = true)] + public static extern int execve(IntPtr path, IntPtr argv, IntPtr envp); + + [DllImport("libc", EntryPoint = "_exit")] + public static extern void _exit(int status); + + [DllImport("libc", SetLastError = true)] + public static extern int ioctl(int fd, nuint request, ref WinSize winsize); + + [DllImport("libc", SetLastError = true)] + public static extern int waitpid(int pid, out int status, int options); + + [DllImport("libc", SetLastError = true)] + public static extern int kill(int pid, int sig); + } +} +#endif diff --git a/src/Backend/Api/BootstrapTokenEndpoints.cs b/src/Backend/Api/BootstrapTokenEndpoints.cs index 6e5cbb8..f1b0a46 100644 --- a/src/Backend/Api/BootstrapTokenEndpoints.cs +++ b/src/Backend/Api/BootstrapTokenEndpoints.cs @@ -24,9 +24,19 @@ public static class BootstrapTokenEndpoints HttpContext context, CancellationToken cancellationToken) { - var lifetime = request?.ValidMinutes is > 0 - ? TimeSpan.FromMinutes(request.ValidMinutes.Value) - : ProtocolDefaults.BootstrapTokenDefaultLifetime; + // 0 disables expiry entirely (pipelines and pre-staged installers); + // absent keeps the 24-hour default. Revocation always applies. + if (request?.ValidMinutes is < 0) + { + return Results.BadRequest(new { error = "validMinutes must be 0 (no expiry) or positive." }); + } + + var expiresUtc = request?.ValidMinutes switch + { + 0 => DateTimeOffset.MaxValue, + > 0 => DateTimeOffset.UtcNow + TimeSpan.FromMinutes(request.ValidMinutes.Value), + _ => DateTimeOffset.UtcNow + ProtocolDefaults.BootstrapTokenDefaultLifetime, + }; // 0 means unlimited uses (expiry and revocation still apply). if (request?.MaxUses is < 0 or > 9999) @@ -45,7 +55,7 @@ public static class BootstrapTokenEndpoints Id = Guid.NewGuid(), TokenHash = TokenHashing.Sha256Hex(token), CreatedUtc = DateTimeOffset.UtcNow, - ExpiresUtc = DateTimeOffset.UtcNow + lifetime, + ExpiresUtc = expiresUtc, MaxUses = maxUses, Note = request?.Note, CreatedBy = actor, diff --git a/src/Backend/Auth/AuthEndpoints.cs b/src/Backend/Auth/AuthEndpoints.cs index b0f9774..37268d3 100644 --- a/src/Backend/Auth/AuthEndpoints.cs +++ b/src/Backend/Auth/AuthEndpoints.cs @@ -41,11 +41,15 @@ public static class AuthEndpoints app.MapGet(ApiRoutes.AuthOidcCallback, () => Results.Redirect("/")).AllowAnonymous(); } + /// The built-in default; signing in with it forces a reset. + private const string DefaultAdminPassword = "admin"; + private static async Task LoginAsync( LoginRequest request, UserService users, RoleCache roles, AuditService audit, + Backend.Data.AppDbContext db, HttpContext context, CancellationToken cancellationToken) { @@ -59,9 +63,11 @@ public static class AuthEndpoints return Results.Unauthorized(); } - // Valid credentials, but a defaulted password: no session until a real - // one is set (POST /api/auth/change-password completes the sign-in). - if (user.MustChangePassword) + // The reset presents only while the password is still the built-in + // default — a custom password chosen at install signs straight in, + // whatever the flag says (POST /api/auth/change-password completes + // the sign-in when the reset does apply). + if (user.MustChangePassword && request.Password == DefaultAdminPassword) { await audit.LogAsync(AuditCategories.Auth, "login-password-change-required", actor: user.Username, remoteIp: remoteIp, cancellationToken: cancellationToken); @@ -70,6 +76,14 @@ public static class AuthEndpoints MustChangePassword: true)); } + if (user.MustChangePassword) + { + // Stale flag with a non-default password: clear it. + var tracked = await db.Users.SingleAsync(u => u.Id == user.Id, cancellationToken); + tracked.MustChangePassword = false; + await db.SaveChangesAsync(cancellationToken); + } + var session = await IssueSessionAsync(user, roles, context, cancellationToken); await audit.LogAsync(AuditCategories.Auth, "login-succeeded", actor: user.Username, remoteIp: remoteIp, cancellationToken: cancellationToken); diff --git a/src/Backend/Backend.csproj b/src/Backend/Backend.csproj index 9378987..ea166cb 100644 --- a/src/Backend/Backend.csproj +++ b/src/Backend/Backend.csproj @@ -4,6 +4,8 @@ net10.0 Backend backend-dev-secrets + ExecuteR Server + ExecuteR Server diff --git a/src/Backend/Data/DevSeeder.cs b/src/Backend/Data/DevSeeder.cs index 2dbd076..08b2ffc 100644 --- a/src/Backend/Data/DevSeeder.cs +++ b/src/Backend/Data/DevSeeder.cs @@ -24,6 +24,9 @@ public static class DevSeeder ("PC-ENG-014", "Dell Inc.", "OptiPlex 7090", "Windows 11 Pro", TimeSpan.FromSeconds(12), new[] { "10.20.4.14" }, new[] { "engineering", "workstation" }), ("PC-FIN-003", "LENOVO", "ThinkCentre M90q", "Windows 11 Enterprise", TimeSpan.FromMinutes(3), new[] { "10.20.6.3" }, new[] { "finance", "workstation" }), ("LAB-VM-27", "Microsoft Corporation", "Virtual Machine", "Windows Server 2022", TimeSpan.FromMinutes(20), new[] { "10.30.9.27" }, new[] { "lab", "server" }), + ("web-prod-01", "QEMU", "Standard PC (Q35)", "Ubuntu 22.04.4 LTS", TimeSpan.FromSeconds(28), new[] { "10.40.1.11" }, new[] { "production", "server" }), + ("build-arm-02", "Raspberry Pi Foundation", "Raspberry Pi 5", "Debian GNU/Linux 12", TimeSpan.FromMinutes(6), new[] { "10.40.2.2" }, new[] { "ci", "server" }), + ("MBP-DESIGN-9", "Apple Inc.", "MacBookPro18,3", "macOS 14.5 Sonoma", TimeSpan.FromMinutes(1), new[] { "10.20.8.9" }, new[] { "design", "workstation" }), }; var i = 0; @@ -127,46 +130,101 @@ public static class DevSeeder await db.SaveChangesAsync(cancellationToken); } - var firstDevice = db.Devices.First(); - var script = db.Scripts.FirstOrDefault(); - if (script is not null) + // A few scripts across engines so the dashboard's engine mix, top + // scripts and drill-ins have something to show. + var scripts = new List(); + var scriptSeeds = new[] { - var jobs = new[] + ("Collect event logs", "PowerShell"), + ("Patch & reboot", "PowerShell"), + ("Rotate log files", "Bash"), + ("Inventory installed apps", "WindowsPowerShell"), + ("Restart web service", "Bash"), + }; + foreach (var (name, language) in scriptSeeds) + { + if (db.Scripts.Any(s => s.Name == name)) { - (JobState.Succeeded, now.AddMinutes(-8), now.AddMinutes(-7), 0), - (JobState.Running, now.AddMinutes(-1), (DateTimeOffset?)null, (int?)null), - (JobState.Failed, now.AddHours(-2), now.AddHours(-2).AddSeconds(4), 1), - }; + continue; + } - foreach (var (state, started, completed, exit) in jobs) + var script = new ScriptEntity { - var job = new JobEntity + Id = Guid.NewGuid(), + Name = name, + Description = "Seeded sample script.", + Language = language, + Content = language == "Bash" ? "#!/usr/bin/env bash\necho ok\n" : "Write-Output 'ok'\n", + CreatedUtc = now.AddDays(-20), + UpdatedUtc = now.AddDays(-20), + CreatedBy = "seed", + UpdatedBy = "seed", + }; + db.Scripts.Add(script); + scripts.Add(script); + } + + await db.SaveChangesAsync(cancellationToken); + scripts = db.Scripts.ToList(); + + var allDevices = db.Devices.ToList(); + if (scripts.Count > 0 && allDevices.Count > 0) + { + // Deterministic spread over the last 14 days: mostly successes, + // a realistic minority of failures and a couple in flight. + var rng = new Random(1742); + var exitPool = new[] { 0, 0, 0, 0, 0, 1, 2, 3, 1 }; + for (var n = 0; n < 90; n++) + { + var device = allDevices[rng.Next(allDevices.Count)]; + var script = scripts[rng.Next(scripts.Count)]; + var created = now.AddDays(-rng.Next(0, 14)) + .AddHours(-rng.Next(0, 24)) + .AddMinutes(-rng.Next(0, 60)); + + var roll = rng.NextDouble(); + JobState state; + int? exit; + if (roll < 0.72) + { + state = JobState.Succeeded; + exit = 0; + } + else if (roll < 0.9) + { + state = JobState.Failed; + exit = exitPool[rng.Next(exitPool.Length)] is var e && e == 0 ? 1 : e; + } + else if (roll < 0.96) + { + state = JobState.Running; + exit = null; + } + else + { + state = JobState.TimedOut; + exit = null; + } + + DateTimeOffset? started = state == JobState.Queued ? null : created.AddSeconds(1); + DateTimeOffset? completed = state is JobState.Succeeded or JobState.Failed or JobState.TimedOut + ? created.AddSeconds(rng.Next(2, 40)) + : null; + + db.Jobs.Add(new JobEntity { JobId = Guid.NewGuid(), - ClientId = firstDevice.ClientId, + ClientId = device.ClientId, ScriptId = script.Id, ScriptName = script.Name, State = state, - CreatedUtc = started.AddSeconds(-1), + CreatedUtc = created, StartedUtc = started, CompletedUtc = completed, ExitCode = exit, RequestedBy = "admin", OutputSummary = state == JobState.Failed ? "Access to the path is denied." : null, - }; - db.Jobs.Add(job); - - for (var s = 1; s <= 5; s++) - { - db.JobOutput.Add(new JobOutputEntity - { - JobId = job.JobId, - Sequence = s, - Stream = OutputStreamKind.Output, - TimestampUtc = started.AddSeconds(s), - Text = $"Completed step {s}", - }); - } + }); } await db.SaveChangesAsync(cancellationToken); diff --git a/src/Backend/Enrollment/InstallEndpoints.cs b/src/Backend/Enrollment/InstallEndpoints.cs index d75ed6a..6528af8 100644 --- a/src/Backend/Enrollment/InstallEndpoints.cs +++ b/src/Backend/Enrollment/InstallEndpoints.cs @@ -160,7 +160,7 @@ public static class InstallEndpoints {{(skipVerify ? PsSkipVerify : "")}} $server = '{{baseUrl}}' $token = '{{token}}' - $dest = Join-Path $env:ProgramFiles 'ExecuteR Agent' + $dest = Join-Path $env:ProgramFiles 'ExecuteR\Agent' $zip = Join-Path $env:TEMP ('ExecuteRAgent-' + [guid]::NewGuid().ToString('N') + '.zip') try { Invoke-WebRequest -Uri '{{packageUrl}}' -OutFile $zip -UseBasicParsing diff --git a/src/Backend/Jobs/DashboardEndpoints.cs b/src/Backend/Jobs/DashboardEndpoints.cs new file mode 100644 index 0000000..f0c4e9e --- /dev/null +++ b/src/Backend/Jobs/DashboardEndpoints.cs @@ -0,0 +1,142 @@ +using Backend.Auth; +using Backend.Data; +using Microsoft.EntityFrameworkCore; +using Shared.Contracts.Api; +using Shared.Contracts.Models; +using Shared.Infrastructure.Constants; +using Shared.Infrastructure.Grouping; + +namespace Backend.Jobs; + +/// +/// The dashboard overview: one aggregate read across devices, jobs and +/// scripts. Everything is computed from stored rows — no new bookkeeping. +/// +public static class DashboardEndpoints +{ + public static void Map(WebApplication app) + { + app.MapGet(ApiRoutes.Dashboard, GetAsync).RequirePermission(Permissions.DevicesView); + } + + private static async Task GetAsync(AppDbContext db, CancellationToken cancellationToken) + { + var now = DateTimeOffset.UtcNow; + + var devices = await db.Devices.AsNoTracking() + .Select(d => new { d.LastSeenUtc, d.OsCaption }) + .ToListAsync(cancellationToken); + + var statuses = devices + .Select(d => ProtocolDefaults.CalculateStatus(d.LastSeenUtc, now)) + .ToList(); + + var byPlatform = devices + .GroupBy(d => DeviceFieldBag.DerivePlatform(d.OsCaption) ?? "unknown") + .Select(g => new CountPoint(g.Key, g.Count())) + .OrderByDescending(p => p.Count) + .ToList(); + + // Bound the window so the dashboard stays quick on busy fleets. + var since = now.AddDays(-30); + var jobs = await db.Jobs.AsNoTracking() + .Where(j => j.CreatedUtc >= since) + .Select(j => new + { + j.JobId, j.ScriptId, j.ScriptName, j.ClientId, j.State, + j.ExitCode, j.CreatedUtc, + }) + .ToListAsync(cancellationToken); + + var succeeded = jobs.Count(j => j.State == JobState.Succeeded); + var failed = jobs.Count(j => j.State is JobState.Failed or JobState.TimedOut); + var inProgress = jobs.Count(j => j.State is JobState.Queued or JobState.Dispatched or JobState.Running); + var finished = succeeded + failed; + + var byState = jobs + .GroupBy(j => j.State) + .Select(g => new CountPoint(g.Key.ToString(), g.Count())) + .OrderByDescending(p => p.Count) + .ToList(); + + var byExitCode = jobs + .Where(j => j.ExitCode is not null) + .GroupBy(j => j.ExitCode!.Value) + .Select(g => new CountPoint(g.Key == 0 ? "0 (ok)" : g.Key.ToString(), g.Count())) + .OrderByDescending(p => p.Count) + .Take(6) + .ToList(); + + // Engine mix: join each job's script to its language (deleted scripts + // fall into "Unknown"). + var languages = await db.Scripts.AsNoTracking() + .Select(s => new { s.Id, s.Language }) + .ToDictionaryAsync(s => s.Id, s => s.Language, cancellationToken); + var byEngine = jobs + .GroupBy(j => languages.GetValueOrDefault(j.ScriptId) is { Length: > 0 } lang ? lang : "Unknown") + .Select(g => new CountPoint(g.Key, g.Count())) + .OrderByDescending(p => p.Count) + .ToList(); + + var hostnames = await db.Devices.AsNoTracking() + .ToDictionaryAsync(d => d.ClientId, d => d.Hostname, cancellationToken); + var topTargets = jobs + .GroupBy(j => hostnames.GetValueOrDefault(j.ClientId, j.ClientId)) + .Select(g => new CountPoint(g.Key, g.Count())) + .OrderByDescending(p => p.Count) + .Take(6) + .ToList(); + + var topScripts = jobs + .GroupBy(j => j.ScriptName) + .Select(g => new CountPoint(g.Key, g.Count())) + .OrderByDescending(p => p.Count) + .Take(6) + .ToList(); + + // 14-day timeline (oldest → newest) of succeeded / failed / other. + var timeline = new List(); + for (var day = 13; day >= 0; day--) + { + var date = now.AddDays(-day).Date; + var dayJobs = jobs.Where(j => j.CreatedUtc.UtcDateTime.Date == date).ToList(); + timeline.Add(new DayPoint( + date.ToString("MMM d"), + dayJobs.Count(j => j.State == JobState.Succeeded), + dayJobs.Count(j => j.State is JobState.Failed or JobState.TimedOut), + dayJobs.Count(j => j.State is not (JobState.Succeeded or JobState.Failed or JobState.TimedOut)))); + } + + var recentIds = jobs + .OrderByDescending(j => j.CreatedUtc) + .Take(8) + .Select(j => j.JobId) + .ToList(); + var recentRuns = await db.Jobs.AsNoTracking() + .Where(j => recentIds.Contains(j.JobId)) + .OrderByDescending(j => j.CreatedUtc) + .Select(j => new JobDto( + j.JobId, j.ClientId, hostnames.GetValueOrDefault(j.ClientId), j.ScriptId, j.ScriptName, + j.State, j.CreatedUtc, j.StartedUtc, j.CompletedUtc, j.ExitCode, j.RequestedBy, j.OutputSummary)) + .ToListAsync(cancellationToken); + + return Results.Ok(new DashboardDto( + DevicesTotal: devices.Count, + DevicesOnline: statuses.Count(s => s == DeviceStatus.Online), + DevicesStale: statuses.Count(s => s == DeviceStatus.Stale), + DevicesOffline: statuses.Count(s => s == DeviceStatus.Offline), + DevicesByPlatform: byPlatform, + JobsTotal: jobs.Count, + JobsSucceeded: succeeded, + JobsFailed: failed, + JobsInProgress: inProgress, + SuccessRate: finished == 0 ? 0 : Math.Round(100.0 * succeeded / finished, 1), + JobsByState: byState, + JobsByExitCode: byExitCode, + JobsByEngine: byEngine, + TopTargets: topTargets, + TopScripts: topScripts, + Timeline: timeline, + RecentRuns: recentRuns)); + } +} diff --git a/src/Backend/Jobs/JobEndpoints.cs b/src/Backend/Jobs/JobEndpoints.cs index 05bcfa7..f737419 100644 --- a/src/Backend/Jobs/JobEndpoints.cs +++ b/src/Backend/Jobs/JobEndpoints.cs @@ -22,7 +22,7 @@ public static class JobEndpoints } private static async Task ListAsync( - AppDbContext db, string? clientId, int? limit, CancellationToken cancellationToken) + AppDbContext db, string? clientId, Guid? scriptId, int? limit, CancellationToken cancellationToken) { var query = db.Jobs.AsNoTracking().OrderByDescending(j => j.CreatedUtc).AsQueryable(); if (!string.IsNullOrEmpty(clientId)) @@ -30,6 +30,11 @@ public static class JobEndpoints query = query.Where(j => j.ClientId == clientId); } + if (scriptId is { } sid) + { + query = query.Where(j => j.ScriptId == sid); + } + var jobs = await query.Take(Math.Clamp(limit ?? 100, 1, 500)).ToListAsync(cancellationToken); var hostnames = await db.Devices.AsNoTracking() .Where(d => jobs.Select(j => j.ClientId).Contains(d.ClientId)) diff --git a/src/Backend/Jobs/JobRunner.cs b/src/Backend/Jobs/JobRunner.cs index 4b5ce63..f7b80f7 100644 --- a/src/Backend/Jobs/JobRunner.cs +++ b/src/Backend/Jobs/JobRunner.cs @@ -32,7 +32,8 @@ public sealed class JobRunner( CommandDispatcher dispatcher, BrowserEventBroadcaster broadcaster, SecretsProviderService secretsService, - Backend.Files.S3PresignService s3) + Backend.Files.S3PresignService s3, + IConfiguration configuration) { /// /// Composes the prelude, resolves secrets and presigns file bindings. @@ -138,9 +139,40 @@ public sealed class JobRunner( }; db.Jobs.Add(job); await db.SaveChangesAsync(cancellationToken); + await PruneHistoryAsync(clientId, cancellationToken); return job; } + /// + /// Execution history retention: keeps the newest JOB_HISTORY_LIMIT jobs + /// per device (default 200, 0 disables pruning) and drops the stored + /// output of anything older. + /// + private async Task PruneHistoryAsync(string clientId, CancellationToken cancellationToken) + { + var limit = int.TryParse(configuration["JOB_HISTORY_LIMIT"], out var configured) && configured >= 0 + ? configured + : 200; + if (limit == 0) + { + return; + } + + var doomed = await db.Jobs.AsNoTracking() + .Where(j => j.ClientId == clientId) + .OrderByDescending(j => j.CreatedUtc) + .Skip(limit) + .Select(j => j.JobId) + .ToListAsync(cancellationToken); + if (doomed.Count == 0) + { + return; + } + + await db.JobOutput.Where(o => doomed.Contains(o.JobId)).ExecuteDeleteAsync(cancellationToken); + await db.Jobs.Where(j => doomed.Contains(j.JobId)).ExecuteDeleteAsync(cancellationToken); + } + /// Publishes the durable command and broadcasts the activity item. public async Task DispatchAsync( PreparedScript prepared, diff --git a/src/Backend/Program.cs b/src/Backend/Program.cs index 050a887..0b91fc4 100644 --- a/src/Backend/Program.cs +++ b/src/Backend/Program.cs @@ -387,6 +387,7 @@ DeviceEndpoints.Map(app); ScriptEndpoints.Map(app); FunctionEndpoints.Map(app); JobEndpoints.Map(app); +DashboardEndpoints.Map(app); ScheduleEndpoints.Map(app); BootstrapTokenEndpoints.Map(app); MessagingEndpoints.Map(app); diff --git a/src/Backend/Standalone/ServerCli.cs b/src/Backend/Standalone/ServerCli.cs index e4d55e6..9055e03 100644 --- a/src/Backend/Standalone/ServerCli.cs +++ b/src/Backend/Standalone/ServerCli.cs @@ -113,7 +113,7 @@ public static class ServerCli var secretsDir = Path.Combine(baseDir, "secrets", "nats"); var dataDir = Path.Combine(baseDir, "data"); Directory.CreateDirectory(natsDir); - Directory.CreateDirectory(dataDir); + Directory.CreateDirectory(Path.Combine(dataDir, "db")); // The sidecar binary: packaged bundles ship it; a bare binary (MSI or // direct download) fetches it here so setup works the same everywhere. @@ -154,7 +154,9 @@ public static class ServerCli http: 127.0.0.1:{{natsMonitorPort}} jetstream { - store_dir: "../data/jetstream" + # nats-server appends "jetstream" itself: this lands in + # ../data/jetstream. + store_dir: "../data" } websocket { @@ -180,9 +182,9 @@ public static class ServerCli File.WriteAllText(appsettings, $$""" { "Urls": "http://{{bindAddress}}:{{httpPort}}", - "DATABASE_PATH": "{{Path.Combine(dataDir, "app.db").Replace('\\', '/')}}", + "DATABASE_PATH": "{{Path.Combine(dataDir, "db", "app.db").Replace('\\', '/')}}", "DATAPROTECTION_PATH": "{{Path.Combine(dataDir, "keys").Replace('\\', '/')}}", - "LOG_PATH": "{{Path.Combine(dataDir, "logs").Replace('\\', '/')}}", + "LOG_PATH": "{{Path.Combine(baseDir, "logs").Replace('\\', '/')}}", "AGENT_PACKAGE_PATH": "{{Path.Combine(baseDir, "agent-package").Replace('\\', '/')}}", "NATS_URL": "nats://127.0.0.1:{{natsPort}}", "NATS_SIDECAR": "1", diff --git a/src/Installer.Server/Product.wxs b/src/Installer.Server/Product.wxs index fe2e116..bb5d571 100644 --- a/src/Installer.Server/Product.wxs +++ b/src/Installer.Server/Product.wxs @@ -73,7 +73,9 @@ Return="ignore" /> - + + + - + Text="Skip TLS verification (self-signed server; traffic stays encrypted)" /> Jobs, IReadOnlyList SkippedOrphaned); +/// A labeled count used by the dashboard charts. +public sealed record CountPoint(string Label, int Count); + +/// One day of the execution timeline. +public sealed record DayPoint(string Date, int Succeeded, int Failed, int Other); + +/// +/// Aggregated control-plane overview for the dashboard. Everything is derived +/// from the jobs, devices and scripts already stored — one read per load. +/// +public sealed record DashboardDto( + int DevicesTotal, + int DevicesOnline, + int DevicesStale, + int DevicesOffline, + IReadOnlyList DevicesByPlatform, + int JobsTotal, + int JobsSucceeded, + int JobsFailed, + int JobsInProgress, + double SuccessRate, + IReadOnlyList JobsByState, + IReadOnlyList JobsByExitCode, + IReadOnlyList JobsByEngine, + IReadOnlyList TopTargets, + IReadOnlyList TopScripts, + IReadOnlyList Timeline, + IReadOnlyList RecentRuns); + /// /// A reusable cadence object. Scripts honor a schedule through assignments — /// the schedule itself carries no script or target. diff --git a/src/WebUI/src/lib/api/client.ts b/src/WebUI/src/lib/api/client.ts index 77619cc..df73d8d 100644 --- a/src/WebUI/src/lib/api/client.ts +++ b/src/WebUI/src/lib/api/client.ts @@ -11,6 +11,7 @@ import type { GroupRuleSet, GroupRunDto, JobDto, + DashboardDto, JobOutputRecordDto, AgentPackageStatusDto, AuthConfigDto, @@ -184,10 +185,13 @@ export const api = { deleteFunction: (id: string) => request(`/api/functions/${id}`, { method: 'DELETE' }), // Jobs - jobs: (clientId?: string, limit?: number) => { + dashboard: () => request('/api/dashboard'), + + jobs: (clientId?: string, limit?: number, scriptId?: string) => { const params = new URLSearchParams(); if (clientId) params.set('clientId', clientId); if (limit) params.set('limit', String(limit)); + if (scriptId) params.set('scriptId', scriptId); const qs = params.size > 0 ? `?${params}` : ''; return request(`/api/jobs${qs}`); }, diff --git a/src/WebUI/src/lib/api/types.ts b/src/WebUI/src/lib/api/types.ts index b9b38d6..414050a 100644 --- a/src/WebUI/src/lib/api/types.ts +++ b/src/WebUI/src/lib/api/types.ts @@ -359,6 +359,38 @@ export interface SchedulePreviewDto { nextRunsUtc: string[]; } +export interface CountPoint { + label: string; + count: number; +} + +export interface DayPoint { + date: string; + succeeded: number; + failed: number; + other: number; +} + +export interface DashboardDto { + devicesTotal: number; + devicesOnline: number; + devicesStale: number; + devicesOffline: number; + devicesByPlatform: CountPoint[]; + jobsTotal: number; + jobsSucceeded: number; + jobsFailed: number; + jobsInProgress: number; + successRate: number; + jobsByState: CountPoint[]; + jobsByExitCode: CountPoint[]; + jobsByEngine: CountPoint[]; + topTargets: CountPoint[]; + topScripts: CountPoint[]; + timeline: DayPoint[]; + recentRuns: JobDto[]; +} + export interface ServerInfoDto { baseUrl: string; fromEnvironment: boolean; diff --git a/src/WebUI/src/lib/components/AddDeviceModal.svelte b/src/WebUI/src/lib/components/AddDeviceModal.svelte index e253f45..3471b49 100644 --- a/src/WebUI/src/lib/components/AddDeviceModal.svelte +++ b/src/WebUI/src/lib/components/AddDeviceModal.svelte @@ -12,6 +12,7 @@ let scope = $state('single'); let multiUses = $state(1); let unlimited = $state(false); + let noExpiry = $state(false); let token = $state(''); let tokenId = $state(''); let baseUrl = $state(''); @@ -58,7 +59,7 @@ } const created = await api.createBootstrapToken({ - validMinutes: 24 * 60, + validMinutes: scope === 'multi' && noExpiry ? 0 : 24 * 60, maxUses: effectiveUses, note: scope === 'single' @@ -88,6 +89,9 @@ function setScope(next: Scope) { if (scope === next) return; scope = next; + // A single device is a one-time enrollment — no-expiry is a + // multi-device convenience only. + if (scope === 'single') noExpiry = false; limitsChanged(); } @@ -181,6 +185,13 @@ {/if} + {#if scope === 'multi'} + + {/if}