// Copyright 2026 Kryptkeeper Authors // // SPDX-License-Identifier: Apache-2.0 package agentinstall import ( "context" "encoding/base64" "encoding/binary" "fmt" "html" "io/fs" "regexp" "strings" "unicode/utf16" ) // W18-02: an Executor backed by WinRM, so the IIS installer can drive a // real Windows host. // // iis_windows.go was complete and unreachable: it takes an Executor, the // only Executor was SSH, and agentdiscovery's WSManShell does not satisfy // the interface — it exposes Run(ctx, cmd, args) (string, error) and has // no file operations at all. This is the adapter that closes that gap. // // Two things it deliberately does NOT paper over: // // 1. WSManShell.Run returns stdout and reports an error only for // transport/SOAP failures — the remote EXIT CODE is discarded. A // failed `netsh http add sslcert` would otherwise read as success and // the installer would report an install that never happened. Every // command here is wrapped so the exit status comes back and a // non-zero one becomes a Go error. // 2. Windows has no POSIX file modes. WriteFile takes an fs.FileMode // because the interface says so; it is ignored, and that is stated // rather than simulated with a bogus icacls translation. // winRMShell is the slice of agentdiscovery.WSManShell this adapter needs. // Declared here so tests substitute a fake and CI needs no Windows host; // *WSManShell satisfies it structurally. type winRMShell interface { RunExit(ctx context.Context, command string, args ...string) (stdout, stderr string, exitCode int, err error) } // WinRMExecutor implements Executor over a WinRM shell. type WinRMExecutor struct { shell winRMShell } func NewWinRMExecutor(shell winRMShell) *WinRMExecutor { return &WinRMExecutor{shell: shell} } // Run executes a command and returns its combined output. The exit code // comes from WS-Man's own CommandState report, so a native command runs // exactly as given and a `powershell -Command