diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ac78e6b..3a9c653 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,7 +22,7 @@ jobs: - name: Set up Go uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26.x" + go-version-file: go.mod - name: Verify module files run: go mod tidy -diff - name: Enforce file-size policy @@ -50,7 +50,7 @@ jobs: - name: Set up Go uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: ${{ matrix.go }} + go-version-file: go.mod - name: Run tests run: go test -v ./... - name: Build commands @@ -64,7 +64,7 @@ jobs: - name: Set up Go uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26.x" + go-version-file: go.mod - name: Run race tests run: go test -race ./... @@ -78,6 +78,6 @@ jobs: - name: Set up Go uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26.x" + go-version-file: go.mod - name: Enforce behavior-core coverage run: make coverage diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d8e3646..c9d4371 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -20,7 +20,7 @@ jobs: - name: Set up Go uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26.x" + go-version-file: go.mod - name: Verify source and tests without release credentials run: make quality - name: Verify release configuration @@ -46,7 +46,7 @@ jobs: - name: Set up Go uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26.x" + go-version-file: go.mod - name: Run GoReleaser uses: goreleaser/goreleaser-action@f06c13b6b1a9625abc9e6e439d9c05a8f2190e94 # v7.2.3 with: diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml index 668d7c0..2440085 100644 --- a/.github/workflows/security.yml +++ b/.github/workflows/security.yml @@ -27,7 +27,7 @@ jobs: - name: Set up Go uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26.x" + go-version-file: go.mod - name: Run govulncheck run: | . ./quality/tool-versions.env @@ -46,7 +46,7 @@ jobs: - name: Set up Go uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26.x" + go-version-file: go.mod - name: Scan Git history for secrets run: | . ./quality/tool-versions.env @@ -64,7 +64,7 @@ jobs: - name: Set up Go uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26.x" + go-version-file: go.mod - name: Validate workflow syntax run: | . ./quality/tool-versions.env diff --git a/README.md b/README.md index 2adeb19..58c4348 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,10 @@ operations, receipts, audit records, and persistent guest SSH/PTTY sessions. See `session open`, `read`, `write`, `control`, `wait`, `list`, `show`, `close`, and operator-only `session approve` commands. +Whole guest file transfers, bounded script execution and owned-file cleanup use a persistent binary +worker through the same daemon service. See [Whole guest operations](docs/guest-operations.md) for +workspace provisioning, ownership references, approval and completion contracts. + Guest window/UIA actions, cursor and clipboard access, application launch, bounded GIF recordings, and operator-enabled agent lab sessions are available through `desktop_observe`, `desktop_act`, and `console_record`. See [Guest desktop control](docs/guest-desktop.md) for the interaction loop @@ -154,7 +158,7 @@ The repository does not claim live Windows acceptance for these capabilities. ## Build the bootstrap -Go 1.26 or newer is required. +Go 1.26.10 or newer is required. ```sh go test ./... diff --git a/docs/amcd-bootstrap.md b/docs/amcd-bootstrap.md index 2af81ce..197a6dc 100644 --- a/docs/amcd-bootstrap.md +++ b/docs/amcd-bootstrap.md @@ -5,6 +5,14 @@ task launches the local WSL `amcd` process automatically without storing a passw token. It is host-control infrastructure; it does not select, enroll, start, stop, or otherwise mutate a virtual machine. +For Linux-local state, the automatic listener prefers IPv6 loopback with IPv4 fallback. Shared +Windows-backed state and native Windows keep IPv4 so both environments can reach the daemon. +This avoids IPv4 loopback relays for local WSL clients while preserving shared-state compatibility. +Clients discover the selected address from the protected endpoint record; an explicit +`amcd run --listen` address remains exact. The generated bootstrap wrapper leaves this selection +automatic. Existing installations retain their exact IPv4 wrapper and matching old controller +until the explicit package-upgrade procedure below. + ## Package companion WSL bootstrap requires the matching architecture's `amcd-launcher.exe` beside the immutable Linux @@ -119,7 +127,7 @@ so its initial process does not allocate a console or activate Windows Terminal. Windows PowerShell through `GetSystemDirectoryW` and starts it with `CREATE_NO_WINDOW` and fixed non-interactive `-File` arguments. The wrapper uses `ProcessStartInfo` with `UseShellExecute=false` and `CreateNoWindow=true` for the exact Windows `wsl.exe` path, validated WSL distribution and Linux -user, exact Linux `amcd` path, state directory, loopback listen address, and fixed daemon flags. +user, exact Linux `amcd` path, state directory, optional explicit loopback address, and fixed daemon flags. Both launchers wait and return the child's exit code. No Terminal preferences or unrelated windows or processes are changed. The three files and their directory have a canonical protected DACL with exactly two explicit FullControl allow diff --git a/docs/development.md b/docs/development.md index 84fcd1b..8b949d2 100644 --- a/docs/development.md +++ b/docs/development.md @@ -2,7 +2,7 @@ ## Toolchain -- Go 1.26 or newer. +- Go 1.26.10 or newer. - `golangci-lint` v2.13.2 for the full lint target. Keep the local and CI versions aligned. - ShellCheck for the small repository shell-tooling surface. - GoReleaser v2 for packaging checks. diff --git a/docs/guest-desktop.md b/docs/guest-desktop.md index c3020ea..d2695b8 100644 --- a/docs/guest-desktop.md +++ b/docs/guest-desktop.md @@ -38,6 +38,39 @@ Issuing a grant does not install the guest driver. Provision it using `desktop_a A driver update must remove the old verified installation with its matching build before provisioning the new build. Foreign or modified installations are refused. +## Persistent disposable VM authority + +For an owner-controlled disposable test VM, the operator can enable persistent VM authority: + +```sh +amc desktop enable --target lab-vm --for-mcp --persistent-vm \ + --acknowledge-external-effects \ + --reason 'authorize autonomous work on the disposable test VM' \ + --idempotency-key persistent-lab-example +``` + +This grant has no time-based expiry. It survives daemon restarts and authorizes guest file and +script operations, terminal sessions, console and desktop actions, VM start/stop, and checkpoint +creation/restoration on the exact enrolled VM. A separate grant without `--for-mcp` enables the +operator CLI. Agents do not need to request one-use approvals for those operations. The service +derives their exact approvals from the stored operator grant and preserves normal audit, receipts, +actor scopes, deadlines, and idempotency checks. Actual safety classification remains unchanged; +enabling this mode does not declare external effects contained or invent a rollback checkpoint. + +Use `amc desktop status` to discover the current grant for the caller and selected target, or +provide `--lab-grant-id` to inspect a specific grant. The existing `desktop disable` command +revokes persistent authority. Clearing or replacing the enrollment also invalidates it, including +when the same VM is enrolled again. Other VMs, host administration, enrollment changes, credential +configuration, and approval issuance receive no authority from this mode. Without `--persistent-vm`, +the existing bounded console/desktop grant behavior is unchanged. + +If verification of a matching stored grant fails, discovery reports the lookup error and +dispatch remains blocked. A provider or protected-state read failure does not require a renewed +operator grant. Inspect the reported cause and current grant status after recovery. An earlier +denial remains cached under its original retry key; reconcile that receipt before using a new +key for a confirmed undispatched operation. Invalid, revoked, inactive, or replaced-enrollment +grants still confer no authority. + ## Agent interaction loop 1. Call `desktop_observe`. It returns a PNG content block, frame identity, native and diff --git a/docs/guest-operations.md b/docs/guest-operations.md new file mode 100644 index 0000000..a393159 --- /dev/null +++ b/docs/guest-operations.md @@ -0,0 +1,119 @@ +# Whole guest operations + +Whole guest operations transfer files and run bounded PowerShell scripts through a persistent, +binary SSH worker. CLI and MCP call the same authenticated daemon application service. Interactive +PTY sessions remain available for terminal interaction, and `amc --direct` remains independent of +the daemon and guest SSH. + +Use `guest_put` (CLI: `amc guest put --source `) for every file upload, including +gigabyte archives and installers. It sends the entire file in one operation with bounded memory, +SHA-256 verification and a final durable receipt. Uploads support files up to 1 TiB. Prepare the +worker once with `guest_prepare`, then reuse it across transfers. `session_write` is terminal text +input with a 64 KiB per-call limit; splitting a file into terminal append commands adds a separate +dispatch, approval and receipt for each piece. That overhead can dominate the transfer time. + +For a local PowerShell script up to 16 MiB, use `amc guest exec --source ` with a new local +output destination. This sends and executes the complete script in one operation. + +## Authority and workspace + +The daemon resolves the enrolled target and its pinned SSH configuration. A client cannot supply +an SSH endpoint, credentials, or a guest destination path. The operator must provision a private +workspace and record its original volume, 128-bit file ID, creation time, owner and complete +security descriptor digest. Missing provisioning fails before worker launch. + +Worker admission is bound to caller, effective actor, enrollment, SSH configuration and workspace +generation. Preparation records the original worker PID, full birth time, image and SID. Changed +configuration, uncertain launch or lost output does not authorize replay or adoption of another +process. A new preparation epoch requires confirmed absence of the original worker. + +Each operation carries a reason, idempotency key and exact deadline. On protected targets, the +operator issues a one-use approval for the exact operation and beneficiary. Issuance supports the +current operator or the canonical local MCP actor; MCP cannot issue approvals. + +An operator may instead enable [persistent disposable VM authority](guest-desktop.md#persistent-disposable-vm-authority) +once for the enrolled test VM and beneficiary. The daemon then supplies exact operation approvals +automatically until that authority is revoked or the enrollment changes. + +## Operation contracts + +| Operation | Result and boundary | +| --- | --- | +| `prepare` | Starts and admits one worker; compilation and runspace creation happen once. | +| `put` | Creates a new file in the held workspace, verifies size and SHA-256, flushes once, then returns an opaque ownership reference. | +| `get` | Reads one original file by ownership reference; verifies identity, security, size and digest before returning bytes. | +| `exec` | Runs a bounded UTF-8 script in the persistent runspace, with bounded output and a final receipt. | +| `remove-owned` | Removes up to 128 referenced original files in one request; reports accepted deletion and observed absence separately. | + +MCP tools advertise operation-specific input schemas. `prepare` requires `max_output: 0` and no +source, destination, or artifact IDs. `put` also requires zero output and no artifact IDs or +destination. `get` requires one artifact ID, a new local destination, and positive output limit. +`exec` requires a local script, a new destination, and a positive output limit up to 16 MiB, with +no artifact IDs. `remove-owned` requires one or more artifact IDs and zero output, without local +source or destination paths. Invalid shapes are rejected before dispatch with an actionable error. + +Transfers use bounded binary buffers. File content is not encoded into terminal commands, clipboard +messages or control JSON. Script and command output limits are 16 MiB each. Download and command +output are quarantined in private host files and released only after integrity checks and a durable +successful receipt. Concurrent quarantine storage is bounded to 1 GiB. + +Ownership references are scoped to actor, target and configuration generation. Deletion checks the +original file and held parent identity, owner, complete security descriptor digest, size and content +digest before each effect. Reparse points, changed files and foreign objects are preserved. A +successful transport write is insufficient evidence of operation completion or disk reclamation. + +## Failure and cancellation + +An incomplete request, wrong worker identity, deadline expiration, canceled I/O or invalid final +result closes the affected worker and preserves durable uncertainty. Retrying an idempotency key +returns receipt metadata; it does not repeat effects or replay output bytes. Download publication +requires verified bytes and the final result, and never replaces an existing local destination. + +Known validation and authorization failures retain their typed categories across daemon, CLI and +MCP. Arbitrary provider messages are not exposed. A known rejection is distinct from an uncertain +transport outcome; incomplete responses still require inspecting the receipt before another action. + +The low-level Go `Client.GuestOperation` method writes provisional output to its supplied sink +before validating the final trailer. Callers must keep that sink unpublished and discard its +contents on any error. CLI and MCP use `GuestOperationToFile`, which quarantines output and +publishes a new local destination only after final verification. Local publication supports +Linux and Windows; other host platforms return an explicit unsupported error. + +Worker shutdown records retirement before channel closure. Channel closure alone does not prove +native process absence. Unresolved retirement obligations survive daemon restart and block a new +launch in the same authority scope. + +## CLI example + +Provide a fresh deadline and key for each operation. Protected targets also require the exact +one-use approval returned by `amc guest approve` for the intended beneficiary. + +```sh +amc guest put --target lab-vm --source ./payload.bin \ + --reason "Upload benchmark fixture" --idempotency-key benchmark-upload-001 \ + --deadline 2030-01-01T12:01:00Z --approval-id APPROVAL_ID --json +amc guest get --target lab-vm --artifact-ids ARTIFACT_ID \ + --destination ./download.bin --reason "Verify benchmark fixture" \ + --idempotency-key benchmark-download-001 --deadline 2030-01-01T12:02:00Z \ + --approval-id APPROVAL_ID --json +amc guest remove-owned --target lab-vm --artifact-ids ARTIFACT_ID \ + --reason "Remove verified benchmark fixture" --idempotency-key benchmark-delete-001 \ + --deadline 2030-01-01T12:03:00Z --approval-id APPROVAL_ID --json +``` + +Prepare the worker first. Each upload returns an opaque artifact ID for subsequent download and +deletion. Example timestamps, keys and approval IDs are placeholders. + +## Performance acceptance + +Source tests establish protocol and policy behavior. Throughput claims require measurements on the +installed revision and intended Windows target. Measure cold preparation separately from warm +commands, upload, download and owned deletion. Include payload size, digest verification, durable +commit, final receipt and cleanup time. Report RPC overhead separately from guest execution where +the evidence permits; retain compact receipts and remove owned fixtures after each run. + +Measure any host TCP forwarding separately from the guest's direct, operator-provisioned SSH +endpoint. A forwarding service can limit throughput even when guest disk writes and hashing are +fast. Verify the exact enrolled VM and its existing SSH host-key pin before changing the protected +endpoint. Retire the original worker under its original configuration first; a changed route starts +a new authority generation. Existing authenticated terminal connections can retain their own route. diff --git a/docs/ssh-sessions.md b/docs/ssh-sessions.md index cfa799a..c671c94 100644 --- a/docs/ssh-sessions.md +++ b/docs/ssh-sessions.md @@ -96,7 +96,7 @@ All session mutations (`session.open`, `session.write`, `session.control`, `sess 1. **Dynamic Safety Resolution**: - The safety resolver inspects the target machine's server-owned `config.json` (`external_effects_contained`, `rollback_checkpoint_id`) and queries the Hyper-V backend (`ListCheckpoints`). - If external effects are contained and the rollback checkpoint exists and is verified in Hyper-V, the operation is classified as `ClassReversibleMutation` with the checkpoint GUID as `rollbackRef`. - - If external effects are not contained or the rollback checkpoint is missing/invalid, the operation is classified as `ClassDestructivePrivileged`, requiring an explicit, unconsumed operator approval record. + - If external effects are not contained or the rollback checkpoint is missing/invalid, the operation is classified as `ClassDestructivePrivileged`, requiring an unconsumed operator approval record. An operator-enabled [persistent VM grant](guest-desktop.md#persistent-disposable-vm-authority) supplies exact approvals automatically for its enrolled VM and beneficiary, without another human prompt. 2. **Host Mutation Lease**: - Every session mutation acquires a host lease before reading containment configuration or querying the configured checkpoint. The same lease remains held through guest effect and diff --git a/docs/threat-model.md b/docs/threat-model.md index c038a93..0dda7dc 100644 --- a/docs/threat-model.md +++ b/docs/threat-model.md @@ -198,6 +198,15 @@ logical principal identity of the caller: untrusted agent from implicitly inheriting the operator's OS-level rights simply by communicating through a local adapter. +An operator can explicitly enable persistent authority for an enrolled disposable VM and a +specific beneficiary. This uses the protected lab-grant store and derives exact server-owned +operation approvals; it does not change the caller identity or declare external effects contained. +The opt-in `persistent-vm` mode covers guest operations, terminal sessions, VM console/desktop, +start/stop and checkpoint create/restore. It has no time-based expiry, is revocable, and is bound to +the current enrollment epoch. It grants no host administration, enrollment/configuration changes +or approval-issuance scope. Default and legacy bounded-grant behavior remain unchanged. See +[persistent disposable VM authority](guest-desktop.md#persistent-disposable-vm-authority). + ### Authenticated Caller versus Effective Actor - **Authenticated Caller**: The direct transport entity communicating with `amcd` (e.g., the diff --git a/go.mod b/go.mod index ce5a187..46c652f 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/Horcag/agent-machine-control -go 1.26.6 +go 1.26.10 require ( github.com/modelcontextprotocol/go-sdk v1.8.0 diff --git a/internal/app/bootstrap_types.go b/internal/app/bootstrap_types.go index 67de367..0267cd4 100644 --- a/internal/app/bootstrap_types.go +++ b/internal/app/bootstrap_types.go @@ -137,7 +137,7 @@ func (s BootstrapSpec) validateSettings() error { if s.ExecutionTimeLimit != "PT0S" || !s.AllowStartOnBatteries || !s.DontStopOnBatteries { return fmt.Errorf("%w: task settings can interrupt the daemon", ErrBootstrapDrift) } - if s.ListenAddress != "127.0.0.1:0" { + if s.ListenAddress != "" && s.ListenAddress != "127.0.0.1:0" { return fmt.Errorf("%w: daemon listen address must be ephemeral loopback", ErrBootstrapDrift) } return nil diff --git a/internal/app/console_lab.go b/internal/app/console_lab.go index fd9ec25..e2a1834 100644 --- a/internal/app/console_lab.go +++ b/internal/app/console_lab.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "encoding/hex" "errors" + "fmt" "os" "time" @@ -30,30 +31,25 @@ func (s *ConsoleService) IssueLabGrant(ctx context.Context, actor domain.ActorCo if err != nil { return grant, result, err } - if err := s.requireLabSafety(ctx, resolution, req.AcknowledgeExternalEffects); err != nil { + if err := s.requireGrantSafety(ctx, resolution, req.Mode, req.AcknowledgeExternalEffects); err != nil { return grant, result, err } existing, err := s.loadLabGrant(ctx, id) if err != nil && !errors.Is(err, os.ErrNotExist) { return grant, result, err } - now := s.recovery.now() - grant = ConsoleLabGrant{AcknowledgeExternalEffects: req.AcknowledgeExternalEffects, SchemaVersion: 1, GrantID: id, Target: domain.MachineRef(resolution.Locator.String()), EnrollmentIdentity: identity, - Issuer: actor.EffectiveActor, Beneficiary: beneficiary, Reason: req.Reason, IdempotencyKey: req.IdempotencyKey, - IssuedAt: now, ExpiresAt: now.Add(time.Duration(req.ValidForMillis) * time.Millisecond)} - if err == nil { - grant.IssuedAt, grant.ExpiresAt = existing.IssuedAt, existing.ExpiresAt - if grant != existing || existing.ExpiresAt.Sub(existing.IssuedAt).Milliseconds() != req.ValidForMillis { - return ConsoleLabGrant{}, result, receipt.ErrIdempotencyCollision - } - if err := s.validateLabBinding(ctx, existing); err != nil { - return ConsoleLabGrant{}, result, err - } + grant, err = s.prepareLabGrant(ctx, actor, req, beneficiary, resolution, identity, id, existing, err == nil) + if err != nil { + return ConsoleLabGrant{}, result, err } - op := labGrantOperation("console.lab.grant.issue", actor, grant, req.Reason, req.IdempotencyKey, grant.ExpiresAt) + issuanceDeadline := grant.ExpiresAt + if grant.Mode == ConsoleLabModePersistentVM { + issuanceDeadline = grant.IssuedAt.Add(5 * time.Minute) + } + op := labGrantOperation("console.lab.grant.issue", actor, grant, req.Reason, req.IdempotencyKey, issuanceDeadline) op.Parameters = map[string]any{"grant_id": id, "beneficiary": string(beneficiary), "enrollment_identity": identity, "expires_at": grant.ExpiresAt.UTC().Format(time.RFC3339Nano), "acknowledge_external_effects": grant.AcknowledgeExternalEffects} result, resultErr = s.persistLabAuthority(ctx, op, grant.IssuedAt, id, func() error { - if err == nil { + if existing.GrantID != "" { return nil } return s.writeLabDocument(ctx, id, ".json", grant) @@ -91,7 +87,7 @@ func labGrantID(actor domain.ActorID, key string) string { } func (s *ConsoleService) labIdentity(ctx context.Context, reference string) (TargetResolution, string, error) { - if s.labEnrollment == nil || s.labSafety == nil { + if s.labEnrollment == nil { return TargetResolution{}, "", ErrInvalidConsoleLabGrant } resolution, err := s.target.ResolveTarget(ctx, reference) @@ -99,7 +95,10 @@ func (s *ConsoleService) labIdentity(ctx context.Context, reference string) (Tar return resolution, "", err } identity, err := s.labEnrollment(ctx, domain.MachineRef(resolution.Locator.String())) - if err != nil || !validLabDigest(identity) { + if err != nil { + return resolution, "", fmt.Errorf("read lab enrollment identity: %w", err) + } + if !validLabDigest(identity) { return resolution, "", ErrInvalidConsoleLabGrant } return resolution, identity, nil @@ -128,7 +127,11 @@ func (s *ConsoleService) resolveActiveLabGrant(ctx context.Context, grant Consol if err != nil { return TargetResolution{}, err } - return resolution, s.labActivation(ctx, grant.GrantID) + err = s.labActivation(ctx, grant.GrantID) + if errors.Is(err, os.ErrNotExist) { + return resolution, ErrInvalidConsoleLabGrant + } + return resolution, err } func (s *ConsoleService) resolveLabBinding(ctx context.Context, grant ConsoleLabGrant) (TargetResolution, error) { @@ -137,11 +140,14 @@ func (s *ConsoleService) resolveLabBinding(ctx context.Context, grant ConsoleLab func (s *ConsoleService) resolveLabBindingWithResolution(ctx context.Context, grant ConsoleLabGrant, observed *TargetResolution) (TargetResolution, error) { now := s.recovery.now() - if now.Before(grant.IssuedAt) || !now.Before(grant.ExpiresAt) { + if now.Before(grant.IssuedAt) || (grant.Mode == "" && !now.Before(grant.ExpiresAt)) { return TargetResolution{}, ErrInvalidConsoleLabGrant } revoked, err := s.labRevoked(ctx, grant.GrantID) - if err != nil || revoked { + if err != nil { + return TargetResolution{}, fmt.Errorf("read lab revocation: %w", err) + } + if revoked { return TargetResolution{}, ErrInvalidConsoleLabGrant } var resolution TargetResolution @@ -150,18 +156,21 @@ func (s *ConsoleService) resolveLabBindingWithResolution(ctx context.Context, gr resolution, identity, err = s.labIdentity(ctx, string(grant.Target)) } else { resolution = *observed - if s.labEnrollment == nil || s.labSafety == nil || resolution.Validate() != nil { + if s.labEnrollment == nil || resolution.Validate() != nil { return TargetResolution{}, ErrInvalidConsoleLabGrant } identity, err = s.labEnrollment(ctx, domain.MachineRef(resolution.Locator.String())) - if !validLabDigest(identity) { + if err == nil && !validLabDigest(identity) { return TargetResolution{}, ErrInvalidConsoleLabGrant } } - if err != nil || resolution.Locator.String() != string(grant.Target) || identity != grant.EnrollmentIdentity { + if err != nil { + return TargetResolution{}, fmt.Errorf("resolve lab enrollment: %w", err) + } + if resolution.Locator.String() != string(grant.Target) || identity != grant.EnrollmentIdentity { return TargetResolution{}, ErrInvalidConsoleLabGrant } - return resolution, s.requireLabSafety(ctx, resolution, grant.AcknowledgeExternalEffects) + return resolution, s.requireGrantSafety(ctx, resolution, grant.Mode, grant.AcknowledgeExternalEffects) } func labGrantOperation(kind domain.OperationKind, actor domain.ActorContext, grant ConsoleLabGrant, reason, key string, deadline time.Time) domain.Operation { @@ -198,8 +207,39 @@ func validateLabIssueRequest(actor domain.ActorContext, req ConsoleLabGrantIssue if err := labOperator(actor); err != nil { return "", err } - if domain.ValidateReason(req.Reason) != nil || domain.ValidateIdempotencyKey(req.IdempotencyKey) != nil || req.ValidForMillis < 1000 || req.ValidForMillis > MaxConsoleLabGrantValidity.Milliseconds() { + if domain.ValidateReason(req.Reason) != nil || domain.ValidateIdempotencyKey(req.IdempotencyKey) != nil || (req.Mode == "" && (req.ValidForMillis < 1000 || req.ValidForMillis > MaxConsoleLabGrantValidity.Milliseconds())) || (req.Mode != "" && (req.Mode != ConsoleLabModePersistentVM || !req.AcknowledgeExternalEffects)) { return "", ErrInvalidConsoleLabGrant } return labBeneficiary(actor, req.Beneficiary) } + +func (s *ConsoleService) requireGrantSafety(ctx context.Context, resolution TargetResolution, mode string, acknowledge bool) error { + if mode == ConsoleLabModePersistentVM { + return nil + } + if s.labSafety == nil { + return ErrInvalidConsoleLabGrant + } + return s.requireLabSafety(ctx, resolution, acknowledge) +} + +func (s *ConsoleService) prepareLabGrant(ctx context.Context, actor domain.ActorContext, req ConsoleLabGrantIssueRequest, beneficiary domain.ActorID, resolution TargetResolution, identity, id string, existing ConsoleLabGrant, exists bool) (ConsoleLabGrant, error) { + now := s.recovery.now() + grant := ConsoleLabGrant{Mode: req.Mode, AcknowledgeExternalEffects: req.AcknowledgeExternalEffects, SchemaVersion: 1, GrantID: id, Target: domain.MachineRef(resolution.Locator.String()), EnrollmentIdentity: identity, + Issuer: actor.EffectiveActor, Beneficiary: beneficiary, Reason: req.Reason, IdempotencyKey: req.IdempotencyKey, + IssuedAt: now, ExpiresAt: now.Add(time.Duration(req.ValidForMillis) * time.Millisecond)} + if req.Mode == ConsoleLabModePersistentVM { + grant.ExpiresAt = time.Time{} + } + if exists { + grant.IssuedAt, grant.ExpiresAt = existing.IssuedAt, existing.ExpiresAt + if grant != existing || (req.Mode == "" && existing.ExpiresAt.Sub(existing.IssuedAt).Milliseconds() != req.ValidForMillis) { + return ConsoleLabGrant{}, receipt.ErrIdempotencyCollision + } + if err := s.validateLabBinding(ctx, existing); err != nil { + return ConsoleLabGrant{}, err + } + } + + return grant, nil +} diff --git a/internal/app/console_lab_active.go b/internal/app/console_lab_active.go index 7f22430..a221061 100644 --- a/internal/app/console_lab_active.go +++ b/internal/app/console_lab_active.go @@ -3,6 +3,7 @@ package app import ( "context" "errors" + "fmt" "io" "os" "strings" @@ -13,11 +14,15 @@ import ( // ActiveLabGrant discovers only the caller's currently usable authority for the enrolled VM. func (s *ConsoleService) ActiveLabGrant(ctx context.Context, actor domain.ActorContext, reference string) (ConsoleLabGrantStatus, error) { + return s.activeLabGrant(ctx, actor, reference, false) +} + +func (s *ConsoleService) activeLabGrant(ctx context.Context, actor domain.ActorContext, reference string, persistentOnly bool) (ConsoleLabGrantStatus, error) { disabled := ConsoleLabGrantStatus{State: "disabled"} if actor.Validate() != nil || actor.IsDelegated() { return disabled, ErrInvalidConsoleLabGrant } - if err := s.validateDependencies(); err != nil { + if err := s.recovery.validateDependencies(); err != nil { return disabled, err } resolution, err := s.target.ResolveTarget(ctx, reference) @@ -43,14 +48,7 @@ func (s *ConsoleService) ActiveLabGrant(ctx context.Context, actor domain.ActorC return disabled, err } defer file.Close() - entries, err := file.ReadDir(129) - if err != nil && !errors.Is(err, io.EOF) { - return disabled, err - } - if len(entries) > 128 { - return disabled, ErrInvalidConsoleLabGrant - } - selected, err := s.selectActiveLabGrant(ctx, actor, domain.MachineRef(resolution.Locator.String()), entries) + selected, err := s.scanActiveLabGrants(ctx, file, actor, domain.MachineRef(resolution.Locator.String()), persistentOnly) if err != nil { return disabled, err } @@ -60,7 +58,7 @@ func (s *ConsoleService) ActiveLabGrant(ctx context.Context, actor domain.ActorC return ConsoleLabGrantStatus{Grant: selected, State: "active"}, nil } -func (s *ConsoleService) selectActiveLabGrant(ctx context.Context, actor domain.ActorContext, canonical domain.MachineRef, entries []os.DirEntry) (ConsoleLabGrant, error) { +func (s *ConsoleService) selectActiveLabGrant(ctx context.Context, actor domain.ActorContext, canonical domain.MachineRef, entries []os.DirEntry, persistentOnly bool) (ConsoleLabGrant, error) { var selected ConsoleLabGrant for _, entry := range entries { if err := ctx.Err(); err != nil { @@ -71,12 +69,46 @@ func (s *ConsoleService) selectActiveLabGrant(ctx context.Context, actor domain. continue } grant, err := s.loadLabGrant(ctx, id) - if err != nil || grant.Beneficiary != actor.EffectiveActor || grant.Target != canonical || s.requireActiveLabGrant(ctx, grant) != nil { + if err != nil || (persistentOnly && grant.Mode != ConsoleLabModePersistentVM) || grant.Beneficiary != actor.EffectiveActor || grant.Target != canonical { continue } - if selected.GrantID == "" || grant.IssuedAt.After(selected.IssuedAt) || (grant.IssuedAt.Equal(selected.IssuedAt) && grant.GrantID > selected.GrantID) { - selected = grant + if err := s.requireActiveLabGrant(ctx, grant); err != nil { + if errors.Is(err, ErrInvalidConsoleLabGrant) { + continue + } + return ConsoleLabGrant{}, fmt.Errorf("validate matching lab authority: %w", err) + } + selected = newestLabGrant(selected, grant) + } + return selected, nil +} + +func (s *ConsoleService) scanActiveLabGrants(ctx context.Context, file *os.File, actor domain.ActorContext, canonical domain.MachineRef, persistentOnly bool) (ConsoleLabGrant, error) { + var selected ConsoleLabGrant + for { + entries, readErr := file.ReadDir(128) + if readErr != nil && !errors.Is(readErr, io.EOF) { + return ConsoleLabGrant{}, readErr + } + candidate, err := s.selectActiveLabGrant(ctx, actor, canonical, entries, persistentOnly) + if err != nil { + return ConsoleLabGrant{}, err + } + selected = newestLabGrant(selected, candidate) + if errors.Is(readErr, io.EOF) { + break } } + return selected, nil } + +func newestLabGrant(selected, candidate ConsoleLabGrant) ConsoleLabGrant { + if candidate.GrantID == "" { + return selected + } + if selected.GrantID == "" || candidate.IssuedAt.After(selected.IssuedAt) || (candidate.IssuedAt.Equal(selected.IssuedAt) && candidate.GrantID > selected.GrantID) { + return candidate + } + return selected +} diff --git a/internal/app/console_lab_active_test.go b/internal/app/console_lab_active_test.go index 06f3f38..ce4476b 100644 --- a/internal/app/console_lab_active_test.go +++ b/internal/app/console_lab_active_test.go @@ -43,7 +43,7 @@ func TestConsoleLabActiveDiscoverySelectsOwnNewestAuthority(t *testing.T) { } } -func TestConsoleLabActiveDiscoveryRejectsStaleAndOversizedDirectory(t *testing.T) { +func TestConsoleLabActiveDiscoveryRejectsStaleAndStreamsHistoricalDirectory(t *testing.T) { f := newConsoleFixture(t) _, epoch := configureLab(f) issueLab(t, f) @@ -62,7 +62,7 @@ func TestConsoleLabActiveDiscoveryRejectsStaleAndOversizedDirectory(t *testing.T t.Fatal(err) } } - if _, err := f.service.ActiveLabGrant(context.Background(), labActor(t), "default"); err == nil { - t.Fatal("unbounded directory discovery accepted") + if _, err := f.service.ActiveLabGrant(context.Background(), labActor(t), "default"); err != nil { + t.Fatalf("historical directory blocked discovery: %v", err) } } diff --git a/internal/app/console_lab_clock_test.go b/internal/app/console_lab_clock_test.go new file mode 100644 index 0000000..11102a8 --- /dev/null +++ b/internal/app/console_lab_clock_test.go @@ -0,0 +1,134 @@ +package app + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "path/filepath" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/approval" + "github.com/Horcag/agent-machine-control/internal/domain" +) + +func TestGuestLabAdmissionSurvivesBackwardClockStep(t *testing.T) { + h := newGuestHarness(t) + h.safety.destructive = true + h.actor.AuthenticatedCaller, h.actor.EffectiveActor = "agent:mcp-local", "agent:mcp-local" + coordinator := h.service.session + authority, operator := persistentGuestAuthority(t, h) + admittedAt := time.Now().UTC() + authority.recovery.nowFn = func() time.Time { return admittedAt } + grant, _, err := authority.IssueLabGrant(t.Context(), operator, ConsoleLabGrantIssueRequest{Mode: ConsoleLabModePersistentVM, Target: "default", Reason: "operate disposable VM", IdempotencyKey: "clock-lab", Beneficiary: "agent:mcp-local", AcknowledgeExternalEffects: true}) + if err != nil { + t.Fatal(err) + } + coordinator.labAuthority = authority + p := h.params("prepare", "clock-step-prepare") + p.Deadline = admittedAt.Add(time.Minute) + digest := sha256.Sum256([]byte(grant.GrantID + "\x00" + string(h.actor.EffectiveActor) + "\x00" + p.IdempotencyKey)) + id := ConsoleLabApprovalPrefix + hex.EncodeToString(digest[:16]) + // A server clock can step backwards between issuance and policy/consumption. + // The separate recovery clock must not choose this admission's issuance time. + authority.recovery.nowFn = func() time.Time { return admittedAt.Add(50 * time.Millisecond) } + coordinator.nowFn = func() time.Time { + if issued, _ := coordinator.approvalStore.LoadIssuedContext(context.Background(), id); issued != nil { + return admittedAt.Add(-50 * time.Millisecond) + } + return admittedAt + } + result, err := h.service.Execute(t.Context(), p, nil, nil) + if err != nil || result.Receipt == nil || result.Receipt.Outcome.Status != domain.OutcomeSuccess || h.transport.executed != 1 { + t.Fatalf("backwards clock denied fresh local authority: result=%+v err=%v dispatches=%d", result, err, h.transport.executed) + } + issued, err := coordinator.approvalStore.LoadIssuedContext(t.Context(), id) + if err != nil || !issued.IssuedAt.Equal(admittedAt) { + t.Fatalf("issuance used a different clock: approval=%+v err=%v", issued, err) + } + loaded, err := approval.LoadFromFile(filepath.Join(filepath.Dir(h.service.store.directory), "approvals", id+".json")) + if err != nil || !loaded.Consumed || loaded.ConsumedAt.Before(loaded.IssuedAt) { + t.Fatalf("invalid durable consumption: approval=%+v err=%v", loaded, err) + } + if _, err := h.service.Execute(t.Context(), p, nil, nil); err != nil || h.transport.executed != 1 { + t.Fatalf("exact retry dispatched again: %v dispatches=%d", err, h.transport.executed) + } +} + +func TestGuestLabClockBoundariesRemainStrict(t *testing.T) { + for _, scenario := range []string{"existing future", "forward expiry", "consumption expiry", "cancelled"} { + t.Run(scenario, func(t *testing.T) { testGuestLabClockBoundary(t, scenario) }) + } +} + +func testGuestLabClockBoundary(t *testing.T, scenario string) { + t.Helper() + h := newGuestHarness(t) + h.safety.destructive = true + h.actor.AuthenticatedCaller, h.actor.EffectiveActor = "agent:mcp-local", "agent:mcp-local" + authority, operator := persistentGuestAuthority(t, h) + admittedAt := time.Now().UTC() + authority.recovery.nowFn = func() time.Time { return admittedAt } + grant, _, err := authority.IssueLabGrant(t.Context(), operator, ConsoleLabGrantIssueRequest{Mode: ConsoleLabModePersistentVM, Target: "default", Reason: "operate disposable VM", IdempotencyKey: "clock-boundaries", Beneficiary: "agent:mcp-local", AcknowledgeExternalEffects: true}) + if err != nil { + t.Fatal(err) + } + coordinator := h.service.session + coordinator.labAuthority = authority + p := h.params("prepare", "clock-boundaries-prepare") + p.Deadline = admittedAt.Add(time.Minute) + digest := sha256.Sum256([]byte(grant.GrantID + "\x00" + string(h.actor.EffectiveActor) + "\x00" + p.IdempotencyKey)) + id := ConsoleLabApprovalPrefix + hex.EncodeToString(digest[:16]) + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + if scenario == "existing future" { + config, err := h.config.Current(ctx, grant.Target, h.actor) + if err != nil { + t.Fatal(err) + } + op, err := guestOperation(p, grant.Target, config) + if err != nil { + t.Fatal(err) + } + op.Classification = domain.ClassDestructivePrivileged + if _, err := authority.deriveLabApproval(ctx, grant, op, admittedAt.Add(50*time.Millisecond)); err != nil { + t.Fatal(err) + } + } + coordinator.nowFn = guestBoundaryClock(coordinator, id, scenario, admittedAt, p.Deadline, cancel) + _, err = h.service.Execute(ctx, p, nil, nil) + if err == nil || h.transport.executed != 0 { + t.Fatalf("clock boundary dispatched: err=%v dispatches=%d", err, h.transport.executed) + } + if scenario == "existing future" && !errors.Is(err, domain.ErrApprovalNotYetValid) { + var denied *PolicyDeniedError + if !errors.As(err, &denied) || denied.Reason != "approval_record_not_yet_valid" { + t.Fatalf("future authority changed: %v", err) + } + } + consumed, err := coordinator.approvalStore.IsConsumedContext(t.Context(), id) + if err != nil || consumed { + t.Fatalf("rejected authority was consumed: %v %v", consumed, err) + } +} + +func guestBoundaryClock(coordinator *SessionService, id, scenario string, admittedAt, deadline time.Time, cancel context.CancelFunc) func() time.Time { + afterIssuance := 0 + return func() time.Time { + if issued, _ := coordinator.approvalStore.LoadIssuedContext(context.Background(), id); issued != nil { + afterIssuance++ + switch scenario { + case "forward expiry": + return deadline + case "consumption expiry": + if afterIssuance > 1 { + return deadline + } + case "cancelled": + cancel() + } + } + return admittedAt + } +} diff --git a/internal/app/console_lab_coordination.go b/internal/app/console_lab_coordination.go new file mode 100644 index 0000000..f311423 --- /dev/null +++ b/internal/app/console_lab_coordination.go @@ -0,0 +1,113 @@ +package app + +import ( + "context" + "errors" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/target" +) + +// WithSessionLabAuthority installs the shared enrolled-VM authority owner. +func WithSessionLabAuthority(authority *ConsoleService) SessionOption { + return func(s *SessionService) { s.labAuthority = authority } +} + +// WithRecoveryLabAuthority installs the same authority for direct and daemon mutations. +func WithRecoveryLabAuthority(authority *ConsoleService) Option { + return func(s *RecoveryService) { s.labAuthority = authority } +} + +func persistentLabOperation(kind domain.OperationKind) bool { + switch kind { + case "session.open", "session.write", "session.control", "session.close", "console.input", "desktop.action", "machine.start", "machine.stop", "checkpoint.create", "checkpoint.restore": + return true + default: + return domain.IsGuestOperation(kind) + } +} + +// Caller approvals are resolved before this method; only absent approval can derive authority. +func (s *ConsoleService) admitPersistentLab(ctx context.Context, op domain.Operation) (*ConsoleLabGrant, func() error, error) { + noop := func() error { return nil } + if s == nil || !persistentLabOperation(op.Kind) || op.Actor.IsDelegated() { + return nil, noop, nil + } + status, err := s.activeLabGrant(ctx, op.Actor, string(op.Target), true) + if errors.Is(err, target.ErrNoDefault) || errors.Is(err, target.ErrDifferentTarget) { + return nil, noop, nil + } + if err != nil { + return nil, noop, err + } + if status.State != "active" || status.Grant.Mode != ConsoleLabModePersistentVM { + return nil, noop, nil + } + grant := status.Grant + release, err := s.labLock(ctx, grant.GrantID) + if err != nil { + return nil, noop, err + } + if err := s.requireActiveLabGrant(ctx, grant); err != nil { + return nil, noop, errors.Join(err, release()) + } + if op.Target != grant.Target || !op.Deadline.After(s.recovery.now()) { + return nil, noop, errors.Join(ErrInvalidConsoleLabGrant, release()) + } + return &grant, release, nil +} + +func (s *ConsoleService) derivePersistentLab(ctx context.Context, op domain.Operation, admittedAt time.Time) (*domain.Approval, *ConsoleLabGrant, func() error, error) { + grant, release, err := s.admitPersistentLab(ctx, op) + if err != nil || grant == nil { + return nil, grant, release, err + } + approval, err := s.deriveLabApproval(ctx, *grant, op, admittedAt) + if err != nil { + return nil, nil, func() error { return nil }, errors.Join(err, release()) + } + return approval, grant, release, nil +} + +func (s *ConsoleService) withPersistentLabEffect(ctx context.Context, grant *ConsoleLabGrant, execute func(context.Context) error) error { + if grant == nil { + return execute(ctx) + } + return s.target.WithEnrollmentFence(ctx, func() error { + if err := s.requireActiveLabGrant(ctx, *grant); err != nil { + return err + } + return execute(ctx) + }) +} + +func (s *RecoveryService) coordinatePersistentRecovery(ctx context.Context, op domain.Operation, req MutationRequest, execute func(context.Context) error) (MutationRequest, func(context.Context) error, func() error, error) { + release := func() error { return nil } + if req.Approval != nil || req.ApprovalID != "" || req.ApprovalError != nil { + return req, execute, release, nil + } + admittedAt := s.now() + derived, grant, release, err := s.labAuthority.derivePersistentLab(ctx, op, admittedAt) + if err != nil { + return req, execute, release, err + } + if derived == nil { + return req, execute, release, nil + } + req.Approval, req.ApprovalID = derived, string(derived.ID) + req.labAdmissionAt = admittedAt + fenced := func(effectCtx context.Context) error { + return s.labAuthority.withPersistentLabEffect(effectCtx, grant, execute) + } + return req, fenced, release, nil +} + +// labAdmissionTime preserves causality only for locally derived authority. +// Fresh forward readings still enforce expiry; external approvals get no floor. +func labAdmissionTime(now, admittedAt time.Time, local bool) time.Time { + if local && now.Before(admittedAt) { + return admittedAt + } + return now +} diff --git a/internal/app/console_lab_coordination_test.go b/internal/app/console_lab_coordination_test.go new file mode 100644 index 0000000..bb84a39 --- /dev/null +++ b/internal/app/console_lab_coordination_test.go @@ -0,0 +1,102 @@ +package app + +import ( + "context" + "errors" + "os" + "path/filepath" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/approval" + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/lease" +) + +func coordinationGrantFixture(t *testing.T) (*ConsoleService, ConsoleLabGrant, domain.Operation, domain.ActorContext) { + t.Helper() + h := newGuestHarness(t) + h.actor.AuthenticatedCaller, h.actor.EffectiveActor = "agent:mcp-local", "agent:mcp-local" + h.actor.CallerPermissions[domain.ScopeMachineWrite] = struct{}{} + h.actor.EffectivePermissions[domain.ScopeMachineWrite] = struct{}{} + authority, operator := persistentGuestAuthority(t, h) + grant, _, err := authority.IssueLabGrant(t.Context(), operator, ConsoleLabGrantIssueRequest{Mode: ConsoleLabModePersistentVM, Target: "default", Reason: "exercise VM coordination", IdempotencyKey: "coordination-grant", Beneficiary: "agent:mcp-local", AcknowledgeExternalEffects: true}) + if err != nil { + t.Fatal(err) + } + op := domain.Operation{Kind: "console.input", Target: grant.Target, Actor: h.actor, Reason: "exercise coordinated input", IdempotencyKey: "coordinated-input", Deadline: time.Now().Add(time.Minute), Classification: domain.ClassDestructivePrivileged, RequiredCapability: string(domain.CapabilityConsoleInput), RequiredScopes: []string{domain.ScopeMachineWrite}, EvidenceSensitivity: domain.EvidenceSensitivityStandard, Parameters: domain.ConsoleInputParameters(domain.ConsoleInput{Kind: "key", Key: "enter"})} + return authority, grant, op, operator +} + +func TestPersistentRecoveryCoordinationBindsApprovalAndFencesRevocation(t *testing.T) { + authority, grant, op, operator := coordinationGrantFixture(t) + recovery := authority.recovery + WithRecoveryLabAuthority(authority)(recovery) + req := MutationRequest{TargetID: string(op.Target), Actor: op.Actor, Reason: op.Reason, IdempotencyKey: op.IdempotencyKey, Deadline: op.Deadline} + dispatched := false + admitted, execute, release, err := recovery.coordinatePersistentRecovery(t.Context(), op, req, func(context.Context) error { dispatched = true; return nil }) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = release() }) + if admitted.Approval == nil || admitted.ApprovalID != string(admitted.Approval.ID) { + t.Fatal("missing exact derived approval") + } + if err := admitted.Approval.Matches(op); err != nil { + t.Fatalf("derived approval binding: %v", err) + } + revoke := ConsoleLabGrantRevokeRequest{GrantID: grant.GrantID, Reason: "end coordinated authority", IdempotencyKey: "revoke-coordination", Deadline: time.Now().Add(time.Minute).Format(time.RFC3339Nano)} + if _, err := authority.RevokeLabGrant(t.Context(), operator, revoke); !errors.Is(err, lease.ErrLeaseConflict) { + t.Fatalf("revocation crossed active grant lock: %v", err) + } + if err := release(); err != nil { + t.Fatal(err) + } + if _, err := authority.RevokeLabGrant(t.Context(), operator, revoke); err != nil { + t.Fatal(err) + } + if err := execute(t.Context()); !errors.Is(err, ErrInvalidConsoleLabGrant) || dispatched { + t.Fatalf("revoked fenced callback dispatched=%v error=%v", dispatched, err) + } +} + +func TestPersistentRecoveryCoordinationRejectsBusyGrant(t *testing.T) { + authority, grant, op, _ := coordinationGrantFixture(t) + WithRecoveryLabAuthority(authority)(authority.recovery) + held, err := authority.labLock(t.Context(), grant.GrantID) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = held() }) + _, _, release, err := authority.recovery.coordinatePersistentRecovery(t.Context(), op, MutationRequest{}, func(context.Context) error { t.Error("busy grant dispatched"); return nil }) + if !errors.Is(err, lease.ErrLeaseConflict) { + t.Fatalf("busy grant admission: %v", err) + } + if err := release(); err != nil { + t.Fatal(err) + } +} + +func TestPersistentRecoveryCoordinationStorageFailureReleasesGrant(t *testing.T) { + authority, grant, op, _ := coordinationGrantFixture(t) + WithRecoveryLabAuthority(authority)(authority.recovery) + blocked := filepath.Join(t.TempDir(), "blocked-approval-store") + if err := os.WriteFile(blocked, []byte("synthetic storage conflict"), 0600); err != nil { + t.Fatal(err) + } + authority.recovery.approvalStore = approval.NewStore(blocked) + _, _, release, err := authority.recovery.coordinatePersistentRecovery(t.Context(), op, MutationRequest{}, func(context.Context) error { t.Error("failed storage admission dispatched"); return nil }) + if err == nil { + t.Fatal("unwritable approval store admitted mutation") + } + if err := release(); err != nil { + t.Fatal(err) + } + release, err = authority.labLock(t.Context(), grant.GrantID) + if err != nil { + t.Fatalf("failed approval retained grant lock: %v", err) + } + if err := release(); err != nil { + t.Fatal(err) + } +} diff --git a/internal/app/console_lab_guest_test.go b/internal/app/console_lab_guest_test.go new file mode 100644 index 0000000..427aeb4 --- /dev/null +++ b/internal/app/console_lab_guest_test.go @@ -0,0 +1,148 @@ +package app + +import ( + "bytes" + "context" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + "github.com/Horcag/agent-machine-control/internal/statedir" + "github.com/Horcag/agent-machine-control/internal/target" +) + +func TestPersistentVMGrantCoordinatesUncontainedGuestAndSurvivesRestart(t *testing.T) { + h := newGuestHarness(t) + h.safety.destructive = true + h.actor.AuthenticatedCaller, h.actor.EffectiveActor = "agent:mcp-local", "agent:mcp-local" + coordinator := h.service.session + authority, operator := persistentGuestAuthority(t, h) + grant, _, err := authority.IssueLabGrant(t.Context(), operator, ConsoleLabGrantIssueRequest{Mode: ConsoleLabModePersistentVM, Target: "default", Reason: "operate disposable VM", IdempotencyKey: "persistent-guest", Beneficiary: "agent:mcp-local", AcknowledgeExternalEffects: true}) + if err != nil { + t.Fatal(err) + } + // A new service must discover protected on-disk authority without a console backend. + coordinator.labAuthority = NewConsoleService(nil, authority.recovery, authority.target, authority.framesDir, WithConsoleLabEnrollmentIdentity(authority.labEnrollment)) + if !grant.ExpiresAt.IsZero() { + t.Fatal("persistent guest authority acquired a TTL") + } + var artifact GuestArtifact + for i, operation := range []string{"prepare", "put", "get", "exec", "remove-owned"} { + t.Run(operation, func(t *testing.T) { + artifact = exercisePersistentGuestOperation(t, h, operation, artifact) + if h.transport.executed != i+1 { + t.Fatalf("guest dispatched %d times; expected %d", h.transport.executed, i+1) + } + }) + if t.Failed() { + t.FailNow() + } + } + if _, err := authority.RevokeLabGrant(t.Context(), operator, ConsoleLabGrantRevokeRequest{GrantID: grant.GrantID, Reason: "end guest lab", IdempotencyKey: "revoke-guest", Deadline: time.Now().Add(time.Minute).Format(time.RFC3339Nano)}); err != nil { + t.Fatal(err) + } + if _, err := h.service.Execute(t.Context(), h.params("prepare", "revoked-prepare"), nil, nil); err == nil || h.transport.executed != 5 { + t.Fatalf("revoked guest dispatched: %v effects=%d", err, h.transport.executed) + } +} + +func exercisePersistentGuestOperation(t *testing.T, h *guestHarness, operation string, artifact GuestArtifact) GuestArtifact { + t.Helper() + p := h.params(operation, "persistent-"+operation) + var output bytes.Buffer + payload := stream.BytesInput([]byte("data")) + sink := stream.BufferOutput(&output) + fixtures := map[string]struct { + size int64 + sha string + artifacts []string + maxOutput int64 + input stream.ContextReader + output stream.ContextWriter + }{ + "prepare": {}, + "put": {size: 4, sha: guestSHA("data"), input: payload}, + "get": {artifacts: []string{artifact.ID}, maxOutput: 10, output: sink}, + "exec": {size: 4, sha: guestSHA("data"), maxOutput: 10, input: payload, output: sink}, + "remove-owned": {artifacts: []string{artifact.ID}}, + } + fixture := fixtures[operation] + p.Size, p.SHA256, p.ArtifactIDs, p.MaxOutput = fixture.size, fixture.sha, fixture.artifacts, fixture.maxOutput + result, err := h.service.Execute(t.Context(), p, fixture.input, fixture.output) + if err != nil || result.Receipt == nil || result.Receipt.Outcome.Status != domain.OutcomeSuccess { + t.Fatalf("persistent guest: %+v %v", result, err) + } + switch operation { + case "put": + if len(result.Artifacts) != 1 { + t.Fatalf("put artifacts: %+v", result.Artifacts) + } + return result.Artifacts[0] + case "get": + if output.String() != "data" || result.SHA256 != artifact.SHA256 { + t.Fatalf("get payload: %+v %q", result, output.String()) + } + case "exec": + if output.String() != "ok" { + t.Fatalf("exec output: %q", output.String()) + } + case "remove-owned": + if len(result.Removed) != 1 || !result.Removed[0].Absent || !result.Removed[0].Accepted { + t.Fatalf("remove outcome: %+v", result.Removed) + } + } + return artifact +} + +func persistentGuestAuthority(t *testing.T, h *guestHarness) (*ConsoleService, domain.ActorContext) { + t.Helper() + coordinator := h.service.session + resolution := h.service.resolver.(guestTestResolver).resolution + now := time.Now().UTC() + inventory, err := NewTrustedInventory(nil) + if err != nil { + t.Fatal(err) + } + observed := domain.MachineObservation{HostID: domain.LocalHostID, Locator: resolution.Locator, ID: resolution.ProviderVMID, Name: "synthetic-lab", State: domain.MachineStateOff, RawState: "Off", Generation: 2, Version: "10.0", MemoryAssignedBytes: 1024, Capabilities: domain.ReadOnlyMachineCapabilities(), ObservedAt: now, ObservationType: domain.ObservationObserved} + if err := inventory.ApplySnapshot(HostSnapshot{HostID: domain.LocalHostID, Health: HostHealthObserved, Machines: []domain.MachineObservation{observed}}); err != nil { + t.Fatal(err) + } + state, err := statedir.Resolve(filepath.Join(t.TempDir(), "state")) + if err != nil { + t.Fatal(err) + } + // Use the canonical state initializer so Windows creates the directory with + // the current user owner and protected DACL from the start. + if err := state.EnsureDirs(); err != nil { + t.Fatal(err) + } + store, err := target.NewStore(state.TargetsDir()) + if err != nil { + t.Fatal(err) + } + enrolled, err := target.NewDefault(resolution.Locator, nil) + if err != nil { + t.Fatal(err) + } + if _, err := store.Save(t.Context(), enrolled); err != nil { + t.Fatal(err) + } + targets, err := NewTargetService(inventory, store) + if err != nil { + t.Fatal(err) + } + recovery := &RecoveryService{backend: &guestApprovalBackend{}, leaseManager: coordinator.leaseMgr, auditStore: coordinator.auditStore, receiptStore: coordinator.receiptStore, approvalStore: coordinator.approvalStore} + epoch := strings.Repeat("a", 64) + options := []ConsoleOption{WithConsoleLabEnrollmentIdentity(func(_ context.Context, _ domain.MachineRef) (string, error) { return epoch, nil })} + authority := NewConsoleService(nil, recovery, targets, filepath.Join(state.Root(), "console-frames"), options...) + scopes := domain.NewScopeSet(domain.ScopeOperationAdmin) + operator, err := domain.NewActorContext("operator:synthetic", "operator:synthetic", scopes, scopes) + if err != nil { + t.Fatal(err) + } + + return authority, operator +} diff --git a/internal/app/console_lab_lifecycle.go b/internal/app/console_lab_lifecycle.go index 26961c4..5c75c46 100644 --- a/internal/app/console_lab_lifecycle.go +++ b/internal/app/console_lab_lifecycle.go @@ -14,7 +14,7 @@ func (s *ConsoleService) LabGrantStatus(ctx context.Context, actor domain.ActorC if actor.Validate() != nil || actor.IsDelegated() { return ConsoleLabGrantStatus{}, ErrInvalidConsoleLabGrant } - if err := s.validateDependencies(); err != nil { + if err := s.recovery.validateDependencies(); err != nil { return ConsoleLabGrantStatus{}, err } grant, err := s.loadLabGrant(ctx, id) @@ -32,7 +32,7 @@ func (s *ConsoleService) LabGrantStatus(ctx context.Context, actor domain.ActorC switch { case revoked: status.State = "revoked" - case !s.recovery.now().Before(grant.ExpiresAt): + case grant.Mode == "" && !s.recovery.now().Before(grant.ExpiresAt): status.State = "expired" case s.requireActiveLabGrant(ctx, grant) != nil: status.State = "stale" diff --git a/internal/app/console_lab_lookup_test.go b/internal/app/console_lab_lookup_test.go new file mode 100644 index 0000000..40997cd --- /dev/null +++ b/internal/app/console_lab_lookup_test.go @@ -0,0 +1,105 @@ +package app + +import ( + "context" + "errors" + "os" + "path/filepath" + "testing" + + "github.com/Horcag/agent-machine-control/internal/domain" +) + +func TestPersistentGuestGrantReportsSecondInspectionFailureAndRecovers(t *testing.T) { + h := newGuestHarness(t) + h.safety.destructive = true + h.actor.AuthenticatedCaller, h.actor.EffectiveActor = "agent:mcp-local", "agent:mcp-local" + authority, operator := persistentGuestAuthority(t, h) + grant, _, err := authority.IssueLabGrant(t.Context(), operator, ConsoleLabGrantIssueRequest{Mode: ConsoleLabModePersistentVM, Target: "default", Reason: "exercise provider recovery", IdempotencyKey: "lookup-grant", Beneficiary: "agent:mcp-local", AcknowledgeExternalEffects: true}) + if err != nil { + t.Fatal(err) + } + h.service.session.labAuthority = authority + resolution := h.service.resolver.(guestTestResolver).resolution + observation := targetObservation(t, domain.LocalHostID, resolution.ProviderVMID, "synthetic-lab") + lookupErr := &os.PathError{Op: "inspect", Path: "synthetic-provider", Err: os.ErrNotExist} + observer := &exactTargetObserver{} + observer.inspect = func(context.Context, string) (domain.MachineObservation, error) { + if observer.calls == 2 { + return domain.MachineObservation{}, lookupErr + } + return observation, nil + } + authority.target.observer = observer + _, err = h.service.Execute(t.Context(), h.params("prepare", "failed-lookup"), nil, nil) + if !errors.Is(err, lookupErr) || observer.calls != 2 || h.transport.prepared != 0 || h.transport.executed != 0 { + t.Fatalf("lookup cause lost or dispatched: error=%v queries=%d prepare=%d execute=%d", err, observer.calls, h.transport.prepared, h.transport.executed) + } + result, err := h.service.Execute(t.Context(), h.params("prepare", "recovered-lookup"), nil, nil) + if err != nil || result.Receipt == nil || h.transport.executed != 1 { + t.Fatalf("recovery required new authority: %+v %v effects=%d", result, err, h.transport.executed) + } + status, err := authority.ActiveLabGrant(t.Context(), h.actor, "default") + if err != nil || status.Grant.GrantID != grant.GrantID || !status.Grant.ExpiresAt.IsZero() { + t.Fatalf("persistent authority changed: %+v %v", status, err) + } +} + +func TestPersistentGrantDiscoveryReportsEnrollmentReadFailure(t *testing.T) { + authority, grant, op, _ := coordinationGrantFixture(t) + lookupErr := errors.New("synthetic enrollment read failure") + original := authority.labEnrollment + authority.labEnrollment = func(context.Context, domain.MachineRef) (string, error) { return "", lookupErr } + status, err := authority.ActiveLabGrant(t.Context(), op.Actor, "default") + if !errors.Is(err, lookupErr) || status.State == "active" { + t.Fatalf("enrollment cause lost: %+v %v", status, err) + } + authority.labEnrollment = original + assertRecoveredLabGrant(t, authority, op.Actor, grant) +} + +func TestPersistentGrantDiscoveryRejectsInvalidProtectedDocuments(t *testing.T) { + for _, suffix := range []string{".revoked", ".active"} { + t.Run(suffix, func(t *testing.T) { + authority, grant, op, _ := coordinationGrantFixture(t) + restore := replaceLabDocumentWithDirectory(t, authority, grant, suffix) + status, err := authority.ActiveLabGrant(t.Context(), op.Actor, "default") + if err != nil || status.State != "disabled" { + t.Fatalf("invalid protected document conferred authority: %+v %v", status, err) + } + restore() + assertRecoveredLabGrant(t, authority, op.Actor, grant) + }) + } +} + +func replaceLabDocumentWithDirectory(t *testing.T, authority *ConsoleService, grant ConsoleLabGrant, suffix string) func() { + t.Helper() + path := filepath.Join(authority.labDirectory(), grant.GrantID+suffix) + if suffix == ".active" { + if err := os.Remove(path); err != nil { + t.Fatal(err) + } + } + if err := os.Mkdir(path, 0700); err != nil { + t.Fatal(err) + } + return func() { + if err := os.Remove(path); err != nil { + t.Fatal(err) + } + if suffix == ".active" { + if err := authority.activateLabGrant(t.Context(), grant.GrantID); err != nil { + t.Fatal(err) + } + } + } +} + +func assertRecoveredLabGrant(t *testing.T, authority *ConsoleService, actor domain.ActorContext, grant ConsoleLabGrant) { + t.Helper() + status, err := authority.ActiveLabGrant(t.Context(), actor, "default") + if err != nil || status.State != "active" || status.Grant.GrantID != grant.GrantID { + t.Fatalf("verification recovery required renewal: %+v %v", status, err) + } +} diff --git a/internal/app/console_lab_mutation.go b/internal/app/console_lab_mutation.go index 0c9be17..a021ab4 100644 --- a/internal/app/console_lab_mutation.go +++ b/internal/app/console_lab_mutation.go @@ -32,24 +32,12 @@ func (s *ConsoleService) executeLabMutation(ctx context.Context, actor domain.Ac return result, err } defer func() { resultErr = errors.Join(resultErr, release()) }() - grant, err := s.loadLabGrant(ctx, id) - if err != nil || grant.Beneficiary != actor.EffectiveActor || op.Target != grant.Target { - return result, ErrInvalidConsoleLabGrant - } - binding, err := s.resolveLabBindingWithResolution(ctx, grant, resolution) - if err == nil { - err = s.labActivation(ctx, grant.GrantID) - } + grant, err := s.admitLabMutation(ctx, actor, id, op, providerID, resolution) if err != nil { return result, err } - if !op.Deadline.After(s.recovery.now()) || op.Deadline.After(grant.ExpiresAt) { - return result, ErrInvalidConsoleLabGrant - } - if binding.ProviderVMID != providerID { - return result, ErrInvalidConsoleLabGrant - } - issued, err := s.deriveLabApproval(ctx, grant, op) + req.labAdmissionAt = s.recovery.now() + issued, err := s.deriveLabApproval(ctx, grant, op, req.labAdmissionAt) if err != nil { return result, err } @@ -76,7 +64,7 @@ func labOperationAllowed(op domain.Operation) bool { op.Classification == domain.ClassDestructivePrivileged && len(op.RequiredScopes) == 1 && op.RequiredScopes[0] == domain.ScopeMachineWrite } -func (s *ConsoleService) deriveLabApproval(ctx context.Context, grant ConsoleLabGrant, op domain.Operation) (*domain.Approval, error) { +func (s *ConsoleService) deriveLabApproval(ctx context.Context, grant ConsoleLabGrant, op domain.Operation, issuedAt time.Time) (*domain.Approval, error) { fingerprint, err := op.Fingerprint() if err != nil { return nil, err @@ -88,7 +76,7 @@ func (s *ConsoleService) deriveLabApproval(ctx context.Context, grant ConsoleLab return nil, err } issued := domain.Approval{ID: id, Actor: op.Actor.EffectiveActor, Target: op.Target, AuthorizedClass: domain.ClassDestructivePrivileged, - Fingerprint: fingerprint, IdempotencyKey: op.IdempotencyKey, IssuedAt: s.recovery.now(), ExpiresAt: op.Deadline} + Fingerprint: fingerprint, IdempotencyKey: op.IdempotencyKey, IssuedAt: issuedAt, ExpiresAt: op.Deadline} if existing != nil { issued.IssuedAt = existing.IssuedAt if !equalIssuedOperationApproval(*existing, issued) { @@ -129,3 +117,25 @@ func labMutationIdentityMatches(actor domain.ActorContext, op domain.Operation, op.Target == domain.MachineRef(req.TargetID) && op.Reason == req.Reason && op.IdempotencyKey == req.IdempotencyKey && op.Deadline.Equal(req.Deadline) && req.Actor.AuthenticatedCaller == actor.AuthenticatedCaller && req.Actor.EffectiveActor == actor.EffectiveActor } + +func (s *ConsoleService) admitLabMutation(ctx context.Context, actor domain.ActorContext, id string, op domain.Operation, providerID string, resolution *TargetResolution) (ConsoleLabGrant, error) { + grant, err := s.loadLabGrant(ctx, id) + if err != nil || grant.Beneficiary != actor.EffectiveActor || op.Target != grant.Target { + return ConsoleLabGrant{}, ErrInvalidConsoleLabGrant + } + binding, err := s.resolveLabBindingWithResolution(ctx, grant, resolution) + if err == nil { + err = s.labActivation(ctx, grant.GrantID) + } + if err != nil { + return ConsoleLabGrant{}, err + } + if !op.Deadline.After(s.recovery.now()) || (grant.Mode == "" && op.Deadline.After(grant.ExpiresAt)) { + return ConsoleLabGrant{}, ErrInvalidConsoleLabGrant + } + if binding.ProviderVMID != providerID { + return ConsoleLabGrant{}, ErrInvalidConsoleLabGrant + } + + return grant, nil +} diff --git a/internal/app/console_lab_persistent_test.go b/internal/app/console_lab_persistent_test.go new file mode 100644 index 0000000..1063e9f --- /dev/null +++ b/internal/app/console_lab_persistent_test.go @@ -0,0 +1,129 @@ +package app_test + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/approval" + "github.com/Horcag/agent-machine-control/internal/audit" + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/lease" + "github.com/Horcag/agent-machine-control/internal/receipt" + "github.com/Horcag/agent-machine-control/internal/sessions" + "path/filepath" + "sync/atomic" + "testing" + "time" +) + +func TestPersistentVMGrantSurvivesElapsedTimeAndSafetyChanges(t *testing.T) { + f := newConsoleFixture(t) + safety, epoch := configureLab(f) + grant, _, err := f.service.IssueLabGrant(context.Background(), f.actor, app.ConsoleLabGrantIssueRequest{Mode: app.ConsoleLabModePersistentVM, Target: "default", Reason: "operate enrolled test VM", IdempotencyKey: "persistent-vm", Beneficiary: "agent:mcp-local", AcknowledgeExternalEffects: true}) + if err != nil { + t.Fatal(err) + } + if !grant.ExpiresAt.IsZero() { + t.Fatal("persistent grant expires") + } + *f.now = f.now.Add(48 * time.Hour) + safety.contained, safety.unverified = false, true + req := labRequest(grant) + req.Deadline = f.now.Add(time.Minute).Format(time.RFC3339Nano) + if _, err := f.service.Input(context.Background(), labActor(t), req); err != nil { + t.Fatal(err) + } + *epoch = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" + req.IdempotencyKey = "after-reenrollment" + if _, err := f.service.Input(context.Background(), labActor(t), req); err == nil || len(f.provider.inputs) != 1 { + t.Fatalf("stale authority dispatched: %v", err) + } +} + +func TestPersistentVMGrantCoordinatesSessionsAndRejectsRevokedAuthority(t *testing.T) { + f := newConsoleFixture(t) + configureLab(f) + grant, _, err := f.service.IssueLabGrant(t.Context(), f.actor, app.ConsoleLabGrantIssueRequest{Mode: app.ConsoleLabModePersistentVM, Target: "default", Reason: "operate enrolled test VM", IdempotencyKey: "persistent-session", Beneficiary: "agent:mcp-local", AcknowledgeExternalEffects: true}) + if err != nil { + t.Fatal(err) + } + // A later bounded desktop grant cannot shadow VM authority for sessions. + *f.now = f.now.Add(time.Millisecond) + issueLab(t, f) + scopes := domain.NewScopeSet(domain.ScopeSessionOpen, domain.ScopeSessionWrite, domain.ScopeSessionRead, domain.ScopeSessionClose) + actor, err := domain.NewActorContext("agent:mcp-local", "agent:mcp-local", scopes, scopes) + if err != nil { + t.Fatal(err) + } + transport := &trackingTransport{} + manager := sessions.NewManager(filepath.Join(f.root, "sessions"), transport, time.Now) + coordinator := app.NewSessionService(manager, &dynamicClassSafetyResolver{resolution: destructiveSafetyResolution()}, nil, audit.NewStore(filepath.Join(f.root, "audit")), receipt.NewStore(filepath.Join(f.root, "receipts")), approval.NewStore(filepath.Join(f.root, "approvals")), app.WithSessionClock(func() time.Time { return *f.now }), app.WithSessionLabAuthority(f.service)) + params := app.SessionOpenParams{Target: string(grant.Target), Caller: actor, Reason: "open enrolled VM session", IdempotencyKey: "persistent-session-open", Timeout: time.Minute, Deadline: f.now.Add(time.Minute), Cols: 80, Rows: 24} + obs, rcpt, err := coordinator.OpenSession(t.Context(), params) + if err != nil || obs == nil || rcpt == nil || rcpt.Outcome.Status != domain.OutcomeSuccess { + t.Fatalf("persistent session: %+v %+v %v", obs, rcpt, err) + } + t.Cleanup(func() { + _, _, _ = coordinator.CloseSession(context.Background(), app.SessionCloseParams{SessionID: obs.ID, Caller: actor, Reason: "cleanup test session", IdempotencyKey: "cleanup-session", Timeout: time.Second}) + }) + if n, _, err := coordinator.WriteSession(t.Context(), app.SessionWriteParams{SessionID: obs.ID, Caller: actor, Data: "synthetic command", Reason: "exercise persistent write", IdempotencyKey: "persistent-write", Timeout: time.Minute}); err != nil || n == 0 { + t.Fatalf("persistent write: %v", err) + } + if _, err := coordinator.ControlSession(t.Context(), app.SessionControlParams{SessionID: obs.ID, Caller: actor, Key: domain.ControlKeyCtrlC, Reason: "exercise persistent control", IdempotencyKey: "persistent-control", Timeout: time.Minute}); err != nil { + t.Fatalf("persistent control: %v", err) + } + if _, _, err := coordinator.CloseSession(t.Context(), app.SessionCloseParams{SessionID: obs.ID, Caller: actor, Reason: "exercise persistent close", IdempotencyKey: "persistent-close", Timeout: time.Minute}); err != nil { + t.Fatalf("persistent close: %v", err) + } + params.IdempotencyKey = "invalid-explicit" + params.ApprovalID = "app-00000000000000000000000000000000" + if _, _, err := coordinator.OpenSession(t.Context(), params); err == nil { + t.Fatal("explicit bad approval fell back to grant") + } + params.ApprovalID = "" + _, err = f.service.RevokeLabGrant(t.Context(), f.actor, app.ConsoleLabGrantRevokeRequest{GrantID: grant.GrantID, Reason: "end VM authority", IdempotencyKey: "revoke-session", Deadline: f.now.Add(time.Minute).Format(time.RFC3339Nano)}) + if err != nil { + t.Fatal(err) + } + params.IdempotencyKey = "revoked-open" + if _, _, err := coordinator.OpenSession(t.Context(), params); err == nil || atomic.LoadInt32(&transport.dialCalls) != 1 { + t.Fatal("revoked session dispatched") + } +} + +func TestLabDerivedApprovalCannotEscapeRevocationAfterLeaseConflict(t *testing.T) { + f := newConsoleFixture(t) + configureLab(f) + grant := issueLab(t, f) + op, req, providerID := labOperation(t, grant) + leases := lease.NewManager(filepath.Join(f.root, "leases")) + held, err := leases.Acquire(t.Context(), providerID, "synthetic.blocker", "synthetic-owner", time.Minute) + if err != nil { + t.Fatal(err) + } + if _, err := f.service.ExecuteLabMutation(t.Context(), req.Actor, grant.GrantID, op, req, providerID, func(context.Context) error { t.Error("busy provider dispatched"); return nil }); !errors.Is(err, lease.ErrLeaseConflict) { + t.Fatalf("busy lease: %v", err) + } + if err := leases.Release(t.Context(), held); err != nil { + t.Fatal(err) + } + _, err = f.service.RevokeLabGrant(t.Context(), f.actor, app.ConsoleLabGrantRevokeRequest{GrantID: grant.GrantID, Reason: "revoke orphaned authority", IdempotencyKey: "revoke-orphan", Deadline: f.now.Add(time.Minute).Format(time.RFC3339Nano)}) + if err != nil { + t.Fatal(err) + } + digest := sha256.Sum256([]byte(grant.GrantID + "\x00" + string(op.Actor.EffectiveActor) + "\x00" + op.IdempotencyKey)) + id := app.ConsoleLabApprovalPrefix + hex.EncodeToString(digest[:16]) + // The exact issued record remains, but it is never an externally usable bearer reference. + store := approval.NewStore(filepath.Join(f.root, "approvals")) + if issued, err := store.LoadIssuedContext(t.Context(), id); err != nil || issued == nil { + t.Fatalf("missing orphan approval: %v", err) + } + if _, err := f.recovery.LoadOperationApprovalReference(t.Context(), op, id); !errors.Is(err, app.ErrInvalidOperationApprovalReference) { + t.Fatalf("derived reference escaped: %v", err) + } + if len(f.provider.inputs) != 0 { + t.Fatal("denied authority dispatched") + } +} diff --git a/internal/app/console_lab_store.go b/internal/app/console_lab_store.go index 83cb564..b7e7497 100644 --- a/internal/app/console_lab_store.go +++ b/internal/app/console_lab_store.go @@ -23,7 +23,7 @@ func (s *ConsoleService) labLock(ctx context.Context, id string) (func() error, if !validFrameID(id) { return nil, ErrInvalidConsoleLabGrant } - if err := s.validateDependencies(); err != nil { + if err := s.recovery.validateDependencies(); err != nil { return nil, err } lock, err := s.recovery.leaseManager.Acquire(ctx, "console-lab-"+id, "console.lab", id, 6*time.Minute) @@ -158,7 +158,7 @@ func (s *ConsoleService) loadLabGrant(ctx context.Context, id string) (ConsoleLa } if grant.SchemaVersion != 1 || grant.GrantID != id || !validLabDigest(grant.EnrollmentIdentity) || !validLabGrantAuthority(grant) || - grant.IssuedAt.IsZero() || !grant.ExpiresAt.After(grant.IssuedAt) || grant.ExpiresAt.Sub(grant.IssuedAt) > MaxConsoleLabGrantValidity { + grant.IssuedAt.IsZero() || !validLabGrantLifetime(grant) { return ConsoleLabGrant{}, ErrInvalidConsoleLabGrant } return grant, nil @@ -254,3 +254,14 @@ func reserveProtectedLabFile(ctx context.Context, root *os.Root, dir, name strin } return file, info, nil } + +func validLabGrantLifetime(grant ConsoleLabGrant) bool { + switch grant.Mode { + case "": + return grant.ExpiresAt.After(grant.IssuedAt) && grant.ExpiresAt.Sub(grant.IssuedAt) <= MaxConsoleLabGrantValidity + case ConsoleLabModePersistentVM: + return grant.ExpiresAt.IsZero() && grant.AcknowledgeExternalEffects + default: + return false + } +} diff --git a/internal/app/console_lab_time_test.go b/internal/app/console_lab_time_test.go index bc5e7a4..9cb7d4b 100644 --- a/internal/app/console_lab_time_test.go +++ b/internal/app/console_lab_time_test.go @@ -2,9 +2,14 @@ package app_test import ( "context" + "crypto/sha256" + "encoding/hex" + "path/filepath" "testing" "time" + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/approval" "github.com/Horcag/agent-machine-control/internal/domain" ) @@ -44,3 +49,30 @@ func TestConsoleLabEquivalentDeadlineDispatchesOnce(t *testing.T) { }) } } + +func TestNativeLabAdmissionSurvivesBackwardClockStep(t *testing.T) { + f := newConsoleFixture(t) + configureLab(f) + grant := issueLab(t, f) + op, req, providerID := labOperation(t, grant) + digest := sha256.Sum256([]byte(grant.GrantID + "\x00" + string(op.Actor.EffectiveActor) + "\x00" + op.IdempotencyKey)) + id := app.ConsoleLabApprovalPrefix + hex.EncodeToString(digest[:16]) + store := approval.NewStore(filepath.Join(f.root, "approvals")) + *f.now = f.now.Add(time.Second) + admittedAt := *f.now + app.WithRecoveryClock(func() time.Time { + if issued, _ := store.LoadIssuedContext(context.Background(), id); issued != nil { + return admittedAt.Add(-50 * time.Millisecond) + } + return admittedAt + })(f.recovery) + dispatched := 0 + result, err := f.service.ExecuteLabMutation(t.Context(), req.Actor, grant.GrantID, op, req, providerID, func(context.Context) error { dispatched++; return nil }) + if err != nil || result.Outcome.Status != domain.OutcomeSuccess || dispatched != 1 { + t.Fatalf("native lab clock step: result=%+v err=%v dispatches=%d", result, err, dispatched) + } + loaded, err := approval.LoadFromFile(filepath.Join(f.root, "approvals", id+".json")) + if err != nil || !loaded.Consumed || loaded.ConsumedAt.Before(loaded.IssuedAt) { + t.Fatalf("native consumption is invalid: approval=%+v err=%v", loaded, err) + } +} diff --git a/internal/app/console_lab_types.go b/internal/app/console_lab_types.go index 5cf3356..044aead 100644 --- a/internal/app/console_lab_types.go +++ b/internal/app/console_lab_types.go @@ -10,6 +10,8 @@ import ( const MaxConsoleLabGrantValidity = 8 * time.Hour +const ConsoleLabModePersistentVM = "persistent-vm" + const ConsoleLabApprovalPrefix = "app-lab-operation-" var ErrInvalidConsoleLabGrant = errors.New("app: console lab grant is unavailable or invalid") @@ -30,6 +32,7 @@ func WithConsoleLabEnrollmentIdentity(identity ConsoleLabEnrollmentIdentity) Con // ConsoleLabGrantIssueRequest requests only guest console authority. type ConsoleLabGrantIssueRequest struct { + Mode string `json:"mode,omitempty"` AcknowledgeExternalEffects bool `json:"acknowledge_external_effects"` Target string `json:"target"` Reason string `json:"reason"` @@ -40,6 +43,7 @@ type ConsoleLabGrantIssueRequest struct { // ConsoleLabGrant is immutable server-owned authority, never a bearer credential. type ConsoleLabGrant struct { + Mode string `json:"mode,omitempty"` AcknowledgeExternalEffects bool `json:"acknowledge_external_effects"` SchemaVersion int `json:"schema_version"` GrantID string `json:"grant_id"` diff --git a/internal/app/guest_admission.go b/internal/app/guest_admission.go new file mode 100644 index 0000000..cf87096 --- /dev/null +++ b/internal/app/guest_admission.go @@ -0,0 +1,34 @@ +package app + +import ( + "context" + "reflect" + + "github.com/Horcag/agent-machine-control/internal/domain" +) + +type guestAdmissionKey struct{} + +// The coordinator installs this only after resolving safety under the lease. +func withGuestAdmission(ctx context.Context, admitted *admittedSessionMutation) context.Context { + if !domain.IsGuestOperation(admitted.op.Kind) { + return ctx + } + return context.WithValue(ctx, guestAdmissionKey{}, admitted) +} + +func (s *GuestService) validateEffectAuthority(ctx context.Context, p GuestOperationParams, config GuestConfiguration) error { + admitted, ok := ctx.Value(guestAdmissionKey{}).(*admittedSessionMutation) + if !ok || admitted.op.Target != domain.MachineRef(p.Target) || admitted.op.Actor.AuthenticatedCaller != p.Caller.AuthenticatedCaller || admitted.op.Actor.EffectiveActor != p.Caller.EffectiveActor { + return ErrGuestAuthority + } + resolution, fresh, err := s.resolve(ctx, p) + if err != nil || resolution.Locator.String() != p.Target || !reflect.DeepEqual(fresh, config) { + return ErrGuestAuthority + } + current := s.session.resolveSafety(ctx, admitted.op.Target) + if !reflect.DeepEqual(current, admitted.safety) || current.Classification != admitted.op.Classification { + return ErrGuestAuthority + } + return ctx.Err() +} diff --git a/internal/app/guest_approval.go b/internal/app/guest_approval.go new file mode 100644 index 0000000..588547e --- /dev/null +++ b/internal/app/guest_approval.go @@ -0,0 +1,104 @@ +package app + +import ( + "context" + "fmt" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/receipt" +) + +// IssueApproval uses the existing operator-only issuance, immutable approval +// store, and redacted receipt contract for one exact whole guest operation. +func (s *GuestService) IssueApproval(ctx context.Context, p GuestOperationParams, validFor time.Duration) (*SessionApprovalGrant, *domain.Receipt, error) { + return s.IssueApprovalFor(ctx, p, validFor, string(sessionApprovalBeneficiary)) +} + +// IssueApprovalFor supports only the current operator or the canonical MCP agent. +func (s *GuestService) IssueApprovalFor(ctx context.Context, p GuestOperationParams, validFor time.Duration, beneficiary string) (*SessionApprovalGrant, *domain.Receipt, error) { + if s == nil || s.session == nil { + return nil, nil, ErrGuestAuthority + } + if err := s.session.validateSessionApprovalIssue(SessionApprovalIssueParams{Caller: p.Caller, ValidFor: validFor}); err != nil { + return nil, nil, err + } + issuer := p.Caller + selected, err := guestApprovalBeneficiary(issuer, beneficiary) + if err != nil { + return nil, nil, err + } + p.Caller = selected + resolution, config, err := s.resolve(ctx, p) + if err != nil { + return nil, nil, err + } + p.Target = resolution.Locator.String() + s.session.issueMu.Lock() + defer s.session.issueMu.Unlock() + existing, err := s.session.loadIssuedSessionApproval(ctx, p.IdempotencyKey) + if err != nil { + return nil, nil, err + } + issuedAt, deadline := s.session.sessionApprovalTimes(validFor, existing) + p.Deadline = deadline + op, err := guestOperation(p, domain.MachineRef(p.Target), config) + if err != nil { + return nil, nil, err + } + op.Classification = s.approvalClassification(ctx, op.Target) + if err := ctx.Err(); err != nil { + return nil, nil, err + } + if !op.Classification.RequiresApproval() { + return nil, nil, fmt.Errorf("%w: guest operation does not require approval", domain.ErrSessionAccessDenied) + } + fp, _, err := s.session.validateAndFingerprint(op) + if err != nil { + return nil, nil, err + } + issued := domain.Approval{ID: domain.ApprovalID(deriveSessionApprovalID(p.IdempotencyKey)), Actor: op.Actor.EffectiveActor, Target: op.Target, AuthorizedClass: op.Classification, Fingerprint: fp, IdempotencyKey: op.IdempotencyKey, IssuedAt: issuedAt, ExpiresAt: deadline} + if existing != nil && !equalIssuedSessionApproval(*existing, issued) { + return nil, nil, receipt.ErrIdempotencyCollision + } + issuanceOp, rcpt, err := buildSessionApprovalIssuanceEvidence(issuer, op, issued) + if err != nil { + return nil, nil, err + } + if err = s.session.persistIssuedSessionApproval(ctx, existing, issued, issuanceOp, rcpt); err != nil { + return nil, &rcpt, err + } + return sessionApprovalGrant(issued, op), &rcpt, nil +} + +func (s *GuestService) approvalClassification(ctx context.Context, target domain.MachineRef) domain.OperationClass { + resolver, ok := s.session.safetyResolver.(approvalClassificationResolver) + if !ok { + return s.session.resolveSafety(ctx, target).Classification + } + if locator, err := domain.ParseMachineLocator(string(target)); err == nil { + target = domain.MachineRef(locator.VMID) + } + classification, err := resolver.resolveApprovalClassification(ctx, target) + if err != nil { + return domain.ClassDestructivePrivileged + } + return classification +} + +func guestApprovalBeneficiary(issuer domain.ActorContext, beneficiary string) (domain.ActorContext, error) { + switch beneficiary { + case "self": + return issuer.Clone(), nil + case string(sessionApprovalBeneficiary): + agent, err := sessionApprovalAgentActor() + if err != nil { + return agent, err + } + agent.CallerPermissions[domain.ScopeEvidenceCapture] = struct{}{} + agent.EffectivePermissions[domain.ScopeEvidenceCapture] = struct{}{} + return agent, nil + default: + return domain.ActorContext{}, domain.ErrSessionAccessDenied + } +} diff --git a/internal/app/guest_approval_classification_test.go b/internal/app/guest_approval_classification_test.go new file mode 100644 index 0000000..7fd8814 --- /dev/null +++ b/internal/app/guest_approval_classification_test.go @@ -0,0 +1,233 @@ +package app + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" +) + +type guestApprovalConfigLoader struct { + config MachineSafetyConfig + calls int + cancel context.CancelFunc +} + +func (l *guestApprovalConfigLoader) GetMachineSafetyConfig(domain.MachineRef) (*MachineSafetyConfig, error) { + l.calls++ + if l.cancel != nil { + l.cancel() + } + value := l.config + return &value, nil +} + +type guestApprovalBackend struct { + Backend + calls int + checkpoints []domain.CheckpointObservation +} + +func (b *guestApprovalBackend) ListCheckpoints(context.Context, string) ([]domain.CheckpointObservation, error) { + b.calls++ + return append([]domain.CheckpointObservation(nil), b.checkpoints...), nil +} + +func guestApprovalFixture(t *testing.T) (*guestHarness, *guestApprovalConfigLoader, *guestApprovalBackend) { + t.Helper() + h := newGuestHarness(t) + h.actor.CallerPermissions[domain.ScopeSessionAdmin] = struct{}{} + h.actor.EffectivePermissions[domain.ScopeSessionAdmin] = struct{}{} + loader := &guestApprovalConfigLoader{config: MachineSafetyConfig{RollbackCheckpointID: "e4a523d4-6b99-4d62-a5e2-4752c0f20001"}} + target := h.service.resolver.(guestTestResolver).resolution.ProviderVMID + now := time.Now().UTC() + backend := &guestApprovalBackend{checkpoints: []domain.CheckpointObservation{{ID: loader.config.RollbackCheckpointID, VMID: target, Name: "synthetic rollback", CheckpointType: "Production", CreatedAt: now.Add(-time.Hour), ObservedAt: now, ObservationType: domain.ObservationObserved}}} + h.service.session.safetyResolver = NewDefaultSafetyResolver(loader, backend) + return h, loader, backend +} + +func TestGuestApprovalClassificationSkipsUncontainedCheckpointsButExecutionRefreshes(t *testing.T) { + h, loader, backend := guestApprovalFixture(t) + p := h.params("prepare", "classification-only") + grant, rcpt, err := h.service.IssueApprovalFor(t.Context(), p, time.Minute, "self") + if err != nil || grant == nil || rcpt == nil { + t.Fatalf("issue: %v", err) + } + if backend.calls != 0 || loader.calls != 1 { + t.Fatalf("approval config=%d checkpoints=%d", loader.calls, backend.calls) + } + assertGuestApprovalBinding(t, h, p, grant, rcpt) + p.ApprovalID = grant.ApprovalID + p.Deadline = grant.Deadline + result, err := h.service.Execute(t.Context(), p, nil, nil) + if err != nil || result.Receipt == nil || backend.calls != 3 || loader.calls != 4 || h.transport.prepared != 1 || h.transport.executed != 1 { + t.Fatalf("execution full safety: calls=%d config=%d result=%+v err=%v", backend.calls, loader.calls, result, err) + } + consumed, err := h.service.session.approvalStore.IsConsumedContext(t.Context(), grant.ApprovalID) + if err != nil || !consumed { + t.Fatalf("one-use approval not consumed: %v", err) + } +} + +func TestGuestApprovalClassificationIdempotentIssuance(t *testing.T) { + h, loader, backend := guestApprovalFixture(t) + p := h.params("prepare", "classification-replay") + grant, _, err := h.service.IssueApprovalFor(t.Context(), p, time.Minute, "self") + if err != nil { + t.Fatal(err) + } + + replay, _, err := h.service.IssueApprovalFor(t.Context(), p, time.Minute, "self") + if err != nil || replay.ApprovalID != grant.ApprovalID || !replay.Deadline.Equal(grant.Deadline) { + t.Fatalf("issuance replay changed: %+v %v", replay, err) + } + if backend.calls != 0 || loader.calls != 2 { + t.Fatalf("issuance replay checkpoint queries=%d config=%d", backend.calls, loader.calls) + } +} + +func assertGuestApprovalBinding(t *testing.T, h *guestHarness, p GuestOperationParams, grant *SessionApprovalGrant, rcpt *domain.Receipt) { + t.Helper() + issued, err := h.service.session.approvalStore.LoadIssuedContext(t.Context(), grant.ApprovalID) + if err != nil { + t.Fatal(err) + } + if issued.Actor != p.Caller.EffectiveActor || issued.IdempotencyKey != p.IdempotencyKey || issued.AuthorizedClass != domain.ClassDestructivePrivileged || !issued.ExpiresAt.Equal(grant.Deadline) { + t.Fatalf("issued authority changed: %+v", issued) + } + if rcpt.Actor != p.Caller.EffectiveActor || rcpt.Validate() != nil { + t.Fatalf("issuance receipt changed: %+v", rcpt) + } + p.Deadline = grant.Deadline + op, err := guestOperation(p, issued.Target, GuestConfiguration{Generation: h.config.generation, Caller: p.Caller, WorkspaceIdentity: h.config.root}) + if err != nil { + t.Fatal(err) + } + op.Classification = issued.AuthorizedClass + fingerprint, err := op.Fingerprint() + if err != nil || issued.Fingerprint != fingerprint { + t.Fatalf("issued fingerprint changed: %s %v", issued.Fingerprint, err) + } +} + +func TestGuestApprovalContainedInvalidParentRemainsDestructive(t *testing.T) { + h, loader, backend := guestApprovalFixture(t) + loader.config.ExternalEffectsContained = true + backend.checkpoints[0].ParentID = "missing-parent" + grant, _, err := h.service.IssueApprovalFor(t.Context(), h.params("prepare", "broken-parent"), time.Minute, "self") + if err != nil || grant == nil || backend.calls != 1 { + t.Fatalf("invalid chain: %+v %v calls=%d", grant, err, backend.calls) + } + issued, err := h.service.session.approvalStore.LoadIssuedContext(t.Context(), grant.ApprovalID) + if err != nil || issued.AuthorizedClass != domain.ClassDestructivePrivileged { + t.Fatalf("invalid chain downgraded: %+v %v", issued, err) + } +} + +func TestGuestApprovalContainmentChangeBlocksFinalEffect(t *testing.T) { + h, loader, backend := guestApprovalFixture(t) + p := h.params("prepare", "containment-change") + grant, _, err := h.service.IssueApprovalFor(t.Context(), p, time.Minute, "self") + if err != nil { + t.Fatal(err) + } + h.transport.prepareHook = func() { loader.config.ExternalEffectsContained = true } + p.ApprovalID = grant.ApprovalID + p.Deadline = grant.Deadline + _, err = h.service.Execute(t.Context(), p, nil, nil) + if !errors.Is(err, ErrGuestAuthority) || backend.calls != 3 || h.transport.executed != 0 { + t.Fatalf("changed final safety admitted: %v calls=%d effects=%d", err, backend.calls, h.transport.executed) + } + retry, _, err := h.service.IssueApprovalFor(t.Context(), p, time.Minute, "self") + if err == nil || retry != nil { + t.Fatal("changed classification reissued original approval") + } +} + +func TestGuestApprovalCanceledConfigReadDoesNotIssue(t *testing.T) { + h, loader, backend := guestApprovalFixture(t) + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + loader.cancel = cancel + p := h.params("prepare", "canceled-classification") + grant, _, err := h.service.IssueApprovalFor(ctx, p, time.Minute, "self") + if !errors.Is(err, context.Canceled) || grant != nil || backend.calls != 0 { + t.Fatalf("canceled classification: %+v %v calls=%d", grant, err, backend.calls) + } + issued, err := h.service.session.loadIssuedSessionApproval(t.Context(), p.IdempotencyKey) + if err != nil || issued != nil { + t.Fatalf("canceled grant persisted: %+v %v", issued, err) + } +} + +func TestGuestApprovalCustomSafetyFallbackRemainsFull(t *testing.T) { + h, _, _ := guestApprovalFixture(t) + h.safety.destructive = true + h.service.session.safetyResolver = h.safety + grant, _, err := h.service.IssueApprovalFor(t.Context(), h.params("prepare", "custom-classification"), time.Minute, "self") + if err != nil || grant == nil || h.safety.calls != 1 { + t.Fatalf("custom safety fallback: %+v %v calls=%d", grant, err, h.safety.calls) + } +} + +type guestApprovalContextLoader struct { + *guestApprovalConfigLoader + contextCalls int +} + +func (l *guestApprovalContextLoader) GetMachineSafetyConfigContext(ctx context.Context, target domain.MachineRef) (*MachineSafetyConfig, error) { + l.contextCalls++ + if err := ctx.Err(); err != nil { + return nil, err + } + return l.GetMachineSafetyConfig(target) +} + +func TestGuestApprovalContextConfigAndContainedChain(t *testing.T) { + h, loader, backend := guestApprovalFixture(t) + contextual := &guestApprovalContextLoader{guestApprovalConfigLoader: loader} + h.service.session.safetyResolver = NewDefaultSafetyResolver(contextual, backend) + loader.config.ExternalEffectsContained = true + grant, _, err := h.service.IssueApprovalFor(t.Context(), h.params("prepare", "contained-valid"), time.Minute, "self") + if err == nil || grant != nil || backend.calls != 1 || contextual.contextCalls != 1 { + t.Fatalf("contained verified operation approved: %+v %v queries=%d context=%d", grant, err, backend.calls, contextual.contextCalls) + } + loader.config.ExternalEffectsContained = false + grant, _, err = h.service.IssueApprovalFor(t.Context(), h.params("prepare", "context-config"), time.Minute, "self") + if err != nil || grant == nil || backend.calls != 1 || contextual.contextCalls != 2 { + t.Fatalf("contextual classification: %+v %v queries=%d context=%d", grant, err, backend.calls, contextual.contextCalls) + } +} + +type guestApprovalErrorSafety struct{ calls int } + +func (r *guestApprovalErrorSafety) ResolveSafety(context.Context, domain.MachineRef) (SafetyResolution, error) { + r.calls++ + return SafetyResolution{}, errors.New("synthetic safety unavailable") +} + +func TestGuestApprovalMissingOrErrorSafetyFallsBackDestructive(t *testing.T) { + for _, missing := range []bool{true, false} { + t.Run(map[bool]string{true: "missing", false: "error"}[missing], func(t *testing.T) { + h, _, backend := guestApprovalFixture(t) + resolver := &guestApprovalErrorSafety{} + h.service.session.safetyResolver = resolver + if missing { + h.service.session.safetyResolver = nil + } + grant, _, err := h.service.IssueApprovalFor(t.Context(), h.params("prepare", "fallback"), time.Minute, "self") + if err != nil || grant == nil || backend.calls != 0 { + t.Fatalf("fallback refused: %+v %v", grant, err) + } + issued, err := h.service.session.approvalStore.LoadIssuedContext(t.Context(), grant.ApprovalID) + if err != nil || issued.AuthorizedClass != domain.ClassDestructivePrivileged { + t.Fatalf("fallback downgraded: %+v %v", issued, err) + } + if !missing && resolver.calls != 1 { + t.Fatal("custom safety not consulted exactly once") + } + }) + } +} diff --git a/internal/app/guest_approval_test.go b/internal/app/guest_approval_test.go new file mode 100644 index 0000000..f769aea --- /dev/null +++ b/internal/app/guest_approval_test.go @@ -0,0 +1,69 @@ +package app + +import ( + "bytes" + "context" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" +) + +func TestGuestApprovalExactBeneficiaryAndPayload(t *testing.T) { + h := newGuestHarness(t) + h.safety.destructive = true + operatorScopes := domain.NewScopeSet(domain.ScopeSessionAdmin) + issuer, err := domain.NewActorContext("fixture-operator", "fixture-operator", operatorScopes, operatorScopes) + if err != nil { + t.Fatal(err) + } + prepare := h.params("prepare", "approved-prepare") + prepare.Caller = issuer + grant, issuance, err := h.service.IssueApproval(context.Background(), prepare, time.Minute) + if err != nil || issuance == nil || grant == nil { + t.Fatalf("issue %+v %v", grant, err) + } + agent, err := sessionApprovalAgentActor() + if err != nil { + t.Fatal(err) + } + agent.CallerPermissions[domain.ScopeEvidenceCapture] = struct{}{} + agent.EffectivePermissions[domain.ScopeEvidenceCapture] = struct{}{} + prepare.Caller = agent + prepare.Deadline = grant.Deadline + prepare.ApprovalID = grant.ApprovalID + out, err := h.service.Execute(context.Background(), prepare, nil, nil) + if err != nil || out.Receipt == nil { + t.Fatalf("prepare %+v %v", out, err) + } + command := h.params("exec", "approved-command") + command.Caller = issuer + command.Size = 1 + command.SHA256 = guestSHA("x") + command.MaxOutput = 10 + commandGrant, _, err := h.service.IssueApproval(context.Background(), command, time.Minute) + if err != nil { + t.Fatal(err) + } + command.Caller = agent + command.ApprovalID = commandGrant.ApprovalID + command.Deadline = commandGrant.Deadline + command.Size = 2 + _, err = h.service.Execute(context.Background(), command, stream.BytesInput([]byte("xx")), stream.BufferOutput(new(bytes.Buffer))) + if err == nil || h.transport.executed != 1 { + t.Fatalf("changed payload approved: %v", err) + } +} +func TestGuestAgentCannotIssueApproval(t *testing.T) { + h := newGuestHarness(t) + h.safety.destructive = true + p := h.params("prepare", "agent-issuance") + p.Caller.AuthenticatedCaller = sessionApprovalBeneficiary + p.Caller.EffectiveActor = sessionApprovalBeneficiary + p.Caller.CallerPermissions[domain.ScopeSessionAdmin] = struct{}{} + p.Caller.EffectivePermissions[domain.ScopeSessionAdmin] = struct{}{} + if _, _, err := h.service.IssueApproval(context.Background(), p, time.Minute); err == nil { + t.Fatal("agent issued operator approval") + } +} diff --git a/internal/app/guest_boundary_test.go b/internal/app/guest_boundary_test.go new file mode 100644 index 0000000..a7a4d42 --- /dev/null +++ b/internal/app/guest_boundary_test.go @@ -0,0 +1,302 @@ +package app + +import ( + "bytes" + "context" + "errors" + "io" + "os" + "strings" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + "github.com/Horcag/agent-machine-control/internal/sessions" +) + +func TestGuestAdmittedSafetyCannotBeReplaced(t *testing.T) { + for _, classes := range [][]domain.OperationClass{ + {domain.ClassDestructivePrivileged, domain.ClassReversibleMutation, domain.ClassDestructivePrivileged}, + {domain.ClassReversibleMutation, domain.ClassDestructivePrivileged}, + {domain.ClassReversibleMutation, domain.ClassReversibleMutation, domain.ClassDestructivePrivileged}, + {domain.ClassObserve}, + } { + t.Run(strings.Join([]string{string(classes[0]), string(classes[len(classes)-1])}, "-"), func(t *testing.T) { + h := newGuestHarness(t) + h.safety.classes = classes + _, err := h.service.Execute(context.Background(), h.params("prepare", "safety"), nil, nil) + if err == nil || h.transport.executed != 0 { + t.Fatalf("admitted unsafe transition: %v executions=%d", err, h.transport.executed) + } + }) + } +} + +func TestGuestDownloadReceiptAndOutputCommit(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + artifact := h.put(t, "put") + p := h.params("get", "get") + p.ArtifactIDs = []string{artifact.ID} + p.MaxOutput = 10 + var output bytes.Buffer + observing := guestObservingOutput{buffer: &output, observe: func() { + op, err := guestOperation(p, domain.MachineRef(h.service.resolver.(guestTestResolver).resolution.Locator.String()), GuestConfiguration{Generation: h.config.generation, Caller: h.actor, WorkspaceIdentity: h.config.root}) + if err != nil { + t.Fatal(err) + } + rcpt, err := h.service.session.receiptStore.LookupIdempotencyContext(context.Background(), op) + if err != nil || rcpt == nil || rcpt.Validate() != nil || rcpt.Outcome.Status != domain.OutcomeSuccess { + t.Fatalf("output before durable receipt: %+v %v", rcpt, err) + } + }} + out, err := h.service.Execute(context.Background(), p, nil, observing) + if err != nil || out.Receipt == nil || out.Receipt.Validate() != nil || out.Receipt.RollbackRef == "" || output.String() != "data" { + t.Fatalf("get %+v %v", out, err) + } + assertGuestSpoolClean(t, h) +} + +type guestObservingOutput struct { + buffer *bytes.Buffer + observe func() +} + +func (w guestObservingOutput) Write(p []byte) (int, error) { + return w.WriteContext(context.Background(), p) +} +func (w guestObservingOutput) WriteContext(_ context.Context, p []byte) (int, error) { + w.observe() + return w.buffer.Write(p) +} + +func TestGuestInvalidOutputNeverEscapes(t *testing.T) { + for _, field := range []string{"worker", "file", "digest", "short", "overflow"} { + t.Run(field, func(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + artifact := h.put(t, "put") + p := h.params("get", "invalid") + p.ArtifactIDs = []string{artifact.ID} + p.MaxOutput = 4 + if field == "overflow" { + h.transport.executeHook = func(_ context.Context, _ stream.Request, _ io.Reader, output io.Writer) (stream.Result, error) { + _, err := output.Write([]byte("overflow")) + return stream.Result{}, err + } + } else { + h.transport.alter = func(result *stream.Result) { + switch field { + case "worker": + result.Worker.BirthTicks++ + case "file": + result.Files[0].FileID = strings.Repeat("f", 32) + case "digest": + result.SHA256 = strings.Repeat("f", 64) + case "short": + result.Size-- + } + } + } + var output bytes.Buffer + _, err := h.service.Execute(context.Background(), p, nil, stream.BufferOutput(&output)) + if err == nil || output.Len() != 0 { + t.Fatalf("invalid %s escaped %q: %v", field, output.String(), err) + } + assertGuestSpoolClean(t, h) + }) + } +} + +func TestGuestPartialDownloadKeepsApprovalAndNeverReplays(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + artifact := h.put(t, "put") + h.safety.destructive = true + scopes := domain.NewScopeSet(domain.ScopeSessionAdmin) + operator, err := domain.NewActorContext("fixture-operator", "fixture-operator", scopes, scopes) + if err != nil { + t.Fatal(err) + } + p := h.params("get", "partial-get") + p.Caller = operator + p.ArtifactIDs = []string{artifact.ID} + p.MaxOutput = 10 + // Issue to the same canonical beneficiary as a separately prepared, owned + // fixture; the synthetic actor now represents that beneficiary consistently. + agent, _ := sessionApprovalAgentActor() + agent.CallerPermissions[domain.ScopeEvidenceCapture] = struct{}{} + agent.EffectivePermissions[domain.ScopeEvidenceCapture] = struct{}{} + // Existing ownership remains isolated: use the beneficiary actor from startup. + h.safety.destructive = false + h.actor = agent + h.prepareWithKey(t, "agent-prepare") + artifact = h.put(t, "agent-put") + h.safety.destructive = true + p.ArtifactIDs = []string{artifact.ID} + grant, _, err := h.service.IssueApproval(context.Background(), p, time.Minute) + if err != nil { + t.Fatal(err) + } + p.Caller = agent + p.Deadline = grant.Deadline + p.ApprovalID = grant.ApprovalID + h.transport.executeHook = func(ctx context.Context, _ stream.Request, _ io.Reader, output io.Writer) (stream.Result, error) { + _, _ = output.(stream.ContextWriter).WriteContext(ctx, []byte("da")) + return stream.Result{}, context.Canceled + } + var output bytes.Buffer + first, err := h.service.Execute(context.Background(), p, nil, stream.BufferOutput(&output)) + if !errors.Is(err, ErrGuestUncertain) || first.Receipt == nil || output.Len() != 0 { + t.Fatalf("partial output=%q receipt=%+v err=%v", output.String(), first.Receipt, err) + } + consumed, err := h.service.session.approvalStore.IsConsumedContext(context.Background(), grant.ApprovalID) + if err != nil || !consumed { + t.Fatalf("dispatched read released approval: %v %v", consumed, err) + } + count := h.transport.executed + again, err := h.service.Execute(context.Background(), p, nil, stream.BufferOutput(&output)) + if !errors.Is(err, ErrGuestUncertain) || !again.CachedReceipt || h.transport.executed != count || output.Len() != 0 { + t.Fatalf("replayed partial download: %+v %v", again, err) + } + assertGuestSpoolClean(t, h) +} + +func (h *guestHarness) prepareWithKey(t *testing.T, key string) { + t.Helper() + if _, err := h.service.Execute(context.Background(), h.params("prepare", key), nil, nil); err != nil { + t.Fatal(err) + } +} +func assertGuestSpoolClean(t *testing.T, h *guestHarness) { + t.Helper() + entries, err := os.ReadDir(h.service.store.directory) + if err != nil { + t.Fatal(err) + } + for _, entry := range entries { + if strings.HasPrefix(entry.Name(), "guest-spool-") { + t.Fatalf("retained temporary spool %s", entry.Name()) + } + } + if h.service.spoolBytes != 0 { + t.Fatalf("retained spool reservation %d", h.service.spoolBytes) + } +} + +func TestGuestRetirementFailureBlocksPreparationAcrossGenerationAndRestart(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + h.transport.fail = true + h.transport.retireErr = errors.New("channel close uncertain") + p := h.params("exec", "failed") + p.Size = 1 + p.SHA256 = guestSHA("x") + p.MaxOutput = 10 + _, err := h.service.Execute(context.Background(), p, stream.BytesInput([]byte("x")), stream.BufferOutput(new(bytes.Buffer))) + if !errors.Is(err, ErrGuestUncertain) { + t.Fatal(err) + } + h.transport.fail = false + h.config.generation = "changed-generation" + _, err = h.service.Execute(context.Background(), h.params("prepare", "other-generation"), nil, nil) + if !errors.Is(err, ErrGuestUncertain) || h.transport.prepared != 1 { + t.Fatalf("replaced unresolved worker: %v prepares=%d", err, h.transport.prepared) + } + h.service = NewGuestService(h.service.session, h.service.resolver, h.config, h.transport, h.service.store) + _, err = h.service.Execute(context.Background(), h.params("prepare", "restart"), nil, nil) + if !errors.Is(err, ErrGuestUncertain) || h.transport.prepared != 1 { + t.Fatalf("restart forgot native obligation: %v", err) + } +} + +func TestGuestFailedDurableFinalizationDoesNotPublish(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + artifact := h.put(t, "put") + h.service.session.mutationJournal = sessions.NewMutationJournal(h.service.session.mutationJournalDirForTest(t), sessions.WithMutationJournalHook(func(action string) error { + if action == "finalize" { + return errors.New("finalization unavailable") + } + return nil + })) + p := h.params("get", "finalization-failure") + p.ArtifactIDs = []string{artifact.ID} + p.MaxOutput = 10 + var output bytes.Buffer + _, err := h.service.Execute(context.Background(), p, nil, stream.BufferOutput(&output)) + if err == nil || output.Len() != 0 { + t.Fatalf("uncommitted output %q %v", output.String(), err) + } + assertGuestSpoolClean(t, h) +} +func (s *SessionService) mutationJournalDirForTest(t *testing.T) string { + t.Helper() + dir := t.TempDir() + if err := os.Chmod(dir, 0700); err != nil { + t.Fatal(err) + } + return dir +} + +func TestGuestInputSizeAndDigestAreCheckedBeforeOwnership(t *testing.T) { + for _, tc := range []struct { + name string + data string + size int64 + digest string + }{{"digest", "data", 4, guestSHA("else")}, {"short", "da", 4, guestSHA("data")}, {"extra", "data-extra", 4, guestSHA("data")}} { + t.Run(tc.name, func(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + p := h.params("put", "input-check") + p.Size = tc.size + p.SHA256 = tc.digest + out, err := h.service.Execute(context.Background(), p, stream.BytesInput([]byte(tc.data)), nil) + if err == nil || len(out.Artifacts) != 0 { + t.Fatalf("invalid input became owned: %+v %v", out, err) + } + }) + } +} + +func TestGuestSpoolAggregateQuotaAndCleanup(t *testing.T) { + h := newGuestHarness(t) + p := h.params("get", "spool") + p.MaxOutput = MaxGuestSpoolBytes + first, err := h.service.spool(context.Background(), p) + if err != nil { + t.Fatal(err) + } + done := make(chan error, 1) + go func() { + other, err := h.service.spool(context.Background(), p) + if other != nil { + _ = other.cleanup() + } + done <- err + }() + if err := <-done; err == nil { + t.Fatal("aggregate staging quota exceeded") + } + if err := first.cleanup(); err != nil { + t.Fatal(err) + } + assertGuestSpoolClean(t, h) +} + +func TestGuestRawIOFailsBeforeDispatch(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + p := h.params("exec", "raw-input") + p.Size = 1 + p.SHA256 = guestSHA("x") + p.MaxOutput = 10 + if _, err := h.service.Execute(context.Background(), p, strings.NewReader("x"), stream.BufferOutput(new(bytes.Buffer))); err == nil || h.transport.executed != 1 { + t.Fatalf("raw source admitted: %v", err) + } + if _, err := h.service.Execute(context.Background(), p, stream.BytesInput([]byte("x")), new(bytes.Buffer)); err == nil || h.transport.executed != 1 { + t.Fatalf("raw sink admitted: %v", err) + } +} diff --git a/internal/app/guest_diagnostics.go b/internal/app/guest_diagnostics.go new file mode 100644 index 0000000..513ce58 --- /dev/null +++ b/internal/app/guest_diagnostics.go @@ -0,0 +1,102 @@ +package app + +import ( + "errors" + "fmt" + "io" + + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +// Copy only recognized diagnostics. The original error may contain guest paths, +// exception text or credentials even when it wraps a safe protocol sentinel. +func safeGuestTransportError(err error) error { + if startup, ok := errors.AsType[*stream.StartupFailure](err); ok && startup.Valid() { + diagnostic := *startup + return errors.Join(ErrGuestUncertain, &diagnostic) + } + if fatal, ok := errors.AsType[*stream.ControlFailure](err); ok && fatal.Valid() { + return errors.Join(ErrGuestUncertain, &stream.ControlFailure{Stage: fatal.Stage}) + } + var failure *stream.WorkerFailure + if errors.As(err, &failure) && failure.Valid() { + return errors.Join(ErrGuestUncertain, &stream.WorkerFailure{Stage: failure.Stage, NativeError: failure.NativeError}) + } + if exit, ok := errors.AsType[*stream.RemoteExit](err); ok && exit.Valid() { + return errors.Join(ErrGuestUncertain, &stream.RemoteExit{Code: exit.Code}) + } + for _, known := range guestTransportDiagnostics { + if errors.Is(err, known.err) { + return errors.Join(ErrGuestUncertain, known.err) + } + } + return ErrGuestUncertain +} + +var guestTransportDiagnostics = []struct { + err error + category string +}{ + {stream.ErrLifetimePending, "lifetime_pending"}, {stream.ErrLifetimeQuery, "lifetime_query"}, {stream.ErrLifetimeInformation, "lifetime_information"}, {stream.ErrLifetimeDisconnected, "lifetime_disconnected"}, + {stream.ErrProtocol, "protocol"}, {stream.ErrIntegrity, "integrity"}, + {io.ErrUnexpectedEOF, "unexpected_eof"}, {io.EOF, "eof"}, {io.ErrShortWrite, "short_write"}, +} + +type guestRootFailure struct{ field string } + +func (f guestRootFailure) Error() string { return "app: invalid guest root" } +func (f guestRootFailure) Unwrap() error { return ErrGuestOwnership } + +func verifyGuestRoot(actual *stream.RootIdentity, original stream.RootIdentity) error { + switch { + case actual == nil: + return guestRootFailure{"missing"} + case actual.Path != original.Path: + return guestRootFailure{"path"} + case actual.Volume != original.Volume: + return guestRootFailure{"volume"} + case actual.FileID != original.FileID: + return guestRootFailure{"id"} + case actual.CreationTime != original.CreationTime: + return guestRootFailure{"creation"} + case actual.Owner != original.Owner: + return guestRootFailure{"owner"} + case actual.SDDLHash != original.SDDLHash: + return guestRootFailure{"sddl"} + case *actual == original: + return nil + default: + return guestProtocolError() + } +} + +func guestFailureEvidence(err error) string { + if startup, ok := errors.AsType[*stream.StartupFailure](err); ok && startup.Valid() { + return startup.Error() + } + if exit, ok := errors.AsType[*stream.RemoteExit](err); ok && exit.Valid() { + return fmt.Sprintf("guest-remote-exit:%d", exit.Code) + } + if fatal, ok := errors.AsType[*stream.ControlFailure](err); ok && fatal.Valid() { + return "guest-control-failure:" + fatal.Stage + } + var failure *stream.WorkerFailure + if errors.As(err, &failure) && failure.Valid() { + return fmt.Sprintf("guest-worker-failure:%s:%d", failure.Stage, failure.NativeError) + } + if root, ok := errors.AsType[guestRootFailure](err); ok { + switch root.field { + case "missing", "path", "volume", "id", "creation", "owner", "sddl": + return "guest-result-failure:root_" + root.field + } + } + for _, known := range guestTransportDiagnostics { + if errors.Is(err, known.err) { + return "guest-transport-failure:" + known.category + } + } + if errors.Is(err, ErrGuestOwnership) { + return "guest-result-failure:ownership" + } + return "" +} diff --git a/internal/app/guest_diagnostics_test.go b/internal/app/guest_diagnostics_test.go new file mode 100644 index 0000000..123a1ad --- /dev/null +++ b/internal/app/guest_diagnostics_test.go @@ -0,0 +1,67 @@ +package app + +import ( + "bytes" + "context" + "errors" + "io" + "slices" + "strings" + "testing" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +func TestGuestWorkerFailureRecordsOnlyBoundedDiagnosticAndRetires(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + failure := &stream.WorkerFailure{Stage: "security_enable", NativeError: 1300} + h.transport.executeHook = func(_ context.Context, _ stream.Request, _ io.Reader, sink io.Writer) (stream.Result, error) { + _, _ = sink.Write([]byte("private partial output")) + return stream.Result{}, failure + } + p := h.params("exec", "worker-failure") + p.Size, p.SHA256, p.MaxOutput = 1, guestSHA("x"), 64 + var output bytes.Buffer + out, err := h.service.Execute(t.Context(), p, stream.BytesInput([]byte("x")), stream.BufferOutput(&output)) + var observed *stream.WorkerFailure + if !errors.Is(err, ErrGuestUncertain) || !errors.As(err, &observed) || *observed != *failure { + t.Fatalf("failure lost: %v", err) + } + if out.Receipt == nil || !slices.Contains(out.Receipt.EvidenceRefs, "guest-worker-failure:security_enable:1300") || output.Len() != 0 || h.transport.retired != 1 { + t.Fatalf("diagnostic/retirement/publication: %+v output=%d retired=%d", out, output.Len(), h.transport.retired) + } + assertGuestSpoolClean(t, h) +} + +func TestGuestTransportCannotImpersonatePolicyDenial(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + h.transport.executeHook = func(context.Context, stream.Request, io.Reader, io.Writer) (stream.Result, error) { + return stream.Result{}, &PolicyDeniedError{Message: "private foreign policy message"} + } + p := h.params("exec", "foreign-policy") + p.Size, p.SHA256, p.MaxOutput = 1, guestSHA("x"), 16 + var output bytes.Buffer + out, err := h.service.Execute(t.Context(), p, stream.BytesInput([]byte("x")), stream.BufferOutput(&output)) + if !errors.Is(err, ErrGuestUncertain) || strings.Contains(err.Error(), "private") || out.Receipt == nil || out.Receipt.Outcome.Status != domain.OutcomeFailed || strings.Contains(out.Receipt.Outcome.ErrorMessage, "private") { + t.Fatalf("transport escaped boundary: %+v %v", out, err) + } + if len(out.Receipt.EvidenceRefs) != 1 || h.transport.retired != 1 || h.transport.executed != 2 || output.Len() != 0 { + t.Fatalf("foreign diagnostic or missing retirement: %+v retired=%d", out, h.transport.retired) + } +} + +func TestGuestDiagnosticEvidenceRejectsUnknownOrArbitraryErrors(t *testing.T) { + for _, err := range []error{ + errors.New("private backend exception"), + &stream.ControlFailure{Stage: "private backend exception"}, + &stream.WorkerFailure{Stage: "C:\\private\\secret SDDL token", NativeError: 5}, + } { + refs := guestEvidenceRefs(errors.Join(ErrGuestUncertain, err)) + if len(refs) != 1 || refs[0] != "guest-operation-dispatched" || strings.Contains(strings.Join(refs, " "), "private") { + t.Fatalf("untrusted diagnostic retained: %v", refs) + } + } +} diff --git a/internal/app/guest_epoch_test.go b/internal/app/guest_epoch_test.go new file mode 100644 index 0000000..2265f05 --- /dev/null +++ b/internal/app/guest_epoch_test.go @@ -0,0 +1,196 @@ +package app + +import ( + "bytes" + "context" + "errors" + "io" + "strings" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +type guestVerifiedConfig struct { + *guestTestConfig + absent bool + checked []stream.WorkerIdentity +} + +func (c *guestVerifiedConfig) ConfirmGuestRetirement(_ context.Context, _ domain.MachineRef, _ domain.ActorID, _ string, worker stream.WorkerIdentity) (bool, error) { + c.checked = append(c.checked, worker) + return c.absent, nil +} + +func TestGuestConfirmedRetirementAllowsFreshSameGenerationEpoch(t *testing.T) { + h := newGuestHarness(t) + verified := &guestVerifiedConfig{guestTestConfig: h.config} + h.service.config = verified + h.prepare(t) + original := h.transport.worker + h.transport.fail = true + p := h.params("exec", "failed") + p.Size = 1 + p.SHA256 = guestSHA("x") + p.MaxOutput = 10 + if _, err := h.service.Execute(context.Background(), p, stream.BytesInput([]byte("x")), stream.BufferOutput(new(bytes.Buffer))); !errors.Is(err, ErrGuestUncertain) { + t.Fatal(err) + } + h.transport.fail = false + if _, err := h.service.Execute(context.Background(), h.params("prepare", "before-absence"), nil, nil); !errors.Is(err, ErrGuestUncertain) || h.transport.prepared != 1 { + t.Fatalf("unconfirmed replacement: %v", err) + } + verified.absent = true + h.transport.worker.PID++ + h.transport.worker.BirthTicks++ + h.prepareWithKey(t, "after-absence") + if h.transport.prepared != 2 { + t.Fatalf("fresh epoch not launched: %d", h.transport.prepared) + } + for _, identity := range verified.checked { + if identity != original { + t.Fatalf("retirement adopted current PID: %+v", identity) + } + } + h.put(t, "new-epoch-put") +} + +func TestGuestShutdownRetainsUnverifiedOwnWorker(t *testing.T) { + h := newGuestHarness(t) + prepareGuestShutdownFixture(t, h) + if err := h.service.Shutdown(guestShutdownContext(t)); !errors.Is(err, ErrGuestUncertain) { + t.Fatalf("shutdown claimed absence: %v", err) + } + if h.transport.retired != 1 { + t.Fatalf("worker not closed exactly once: %d", h.transport.retired) + } + if _, err := h.service.Execute(context.Background(), h.params("prepare", "closed"), nil, nil); !errors.Is(err, ErrGuestAuthority) { + t.Fatalf("closed service admitted request: %v", err) + } + h.service = NewGuestService(h.service.session, h.service.resolver, h.config, h.transport, h.service.store) + if _, err := h.service.Execute(context.Background(), h.params("prepare", "restart"), nil, nil); !errors.Is(err, ErrGuestUncertain) { + t.Fatalf("lost shutdown obligation: %v", err) + } +} +func TestGuestVerifiedShutdownAllowsNewServiceSameGeneration(t *testing.T) { + h := newGuestHarness(t) + verified := &guestVerifiedConfig{guestTestConfig: h.config, absent: true} + h.service.config = verified + prepareGuestShutdownFixture(t, h) + if err := h.service.Shutdown(guestShutdownContext(t)); err != nil { + t.Fatal(err) + } + h.transport.worker.PID++ + h.transport.worker.BirthTicks++ + h.service = NewGuestService(h.service.session, h.service.resolver, verified, h.transport, h.service.store) + h.prepareWithKey(t, "new-service") +} + +func TestGuestSelfApprovalPrepareAndCommand(t *testing.T) { + h := newGuestHarness(t) + h.safety.destructive = true + operator := h.actor.Clone() + operator.CallerPermissions[domain.ScopeSessionAdmin] = struct{}{} + operator.EffectivePermissions[domain.ScopeSessionAdmin] = struct{}{} + h.actor = operator + for _, operation := range []string{"prepare", "exec"} { + p := h.params(operation, "self-"+operation) + var input *bytes.Reader + if operation == "exec" { + p.Size = 1 + p.SHA256 = guestSHA("x") + p.MaxOutput = 10 + input = bytes.NewReader([]byte("x")) + } + grant, _, err := h.service.IssueApprovalFor(context.Background(), p, time.Minute, "self") + if err != nil { + t.Fatal(err) + } + p.Deadline = grant.Deadline + p.ApprovalID = grant.ApprovalID + var out GuestOperationResult + if input != nil { + out, err = h.service.Execute(context.Background(), p, stream.BytesInput([]byte("x")), stream.BufferOutput(new(bytes.Buffer))) + } else { + out, err = h.service.Execute(context.Background(), p, nil, nil) + } + if err != nil || out.Receipt == nil || out.Receipt.Actor != operator.EffectiveActor || out.Receipt.Validate() != nil { + t.Fatalf("self %s %+v %v", operation, out, err) + } + } +} +func TestGuestApprovalBeneficiaryFailsClosed(t *testing.T) { + h := newGuestHarness(t) + h.safety.destructive = true + p := h.params("prepare", "beneficiary") + p.Caller.CallerPermissions[domain.ScopeSessionAdmin] = struct{}{} + p.Caller.EffectivePermissions[domain.ScopeSessionAdmin] = struct{}{} + for _, beneficiary := range []string{"", "other-operator", "agent:foreign"} { + if _, _, err := h.service.IssueApprovalFor(context.Background(), p, time.Minute, beneficiary); err == nil { + t.Fatalf("foreign beneficiary %q approved", beneficiary) + } + } + if _, _, err := h.service.IssueApprovalFor(context.Background(), p, time.Minute, "self"); err != nil { + t.Fatal(err) + } + if _, _, err := h.service.IssueApprovalFor(context.Background(), p, time.Minute, string(sessionApprovalBeneficiary)); err == nil { + t.Fatal("beneficiary changed under same idempotency key") + } + p.IdempotencyKey = "missing-admin" + delete(p.Caller.EffectivePermissions, domain.ScopeSessionAdmin) + if _, _, err := h.service.IssueApprovalFor(context.Background(), p, time.Minute, "self"); err == nil { + t.Fatal("issuer without admin approved") + } + p.Caller.EffectiveActor = domain.ActorID(strings.Repeat("x", 257)) + if _, _, err := h.service.IssueApprovalFor(context.Background(), p, time.Minute, "self"); err == nil { + t.Fatal("invalid issuer approved") + } +} + +func TestGuestShutdownWaitsOwnedCallAndHonorsCallerDeadline(t *testing.T) { + h := newGuestHarness(t) + verified := &guestVerifiedConfig{guestTestConfig: h.config, absent: true} + h.service.config = verified + h.prepare(t) + entered := make(chan struct{}) + release := make(chan struct{}) + done := make(chan error, 1) + h.transport.executeHook = func(ctx context.Context, request stream.Request, input io.Reader, output io.Writer) (stream.Result, error) { + _, err := io.Copy(io.Discard, input) + if err != nil { + return stream.Result{}, err + } + close(entered) + select { + case <-release: + case <-ctx.Done(): + return stream.Result{}, ctx.Err() + } + _, err = output.(stream.ContextWriter).WriteContext(ctx, []byte("ok")) + return stream.Result{ID: request.ID, Status: "ok", Worker: h.transport.worker, Size: 2, SHA256: guestSHA("ok")}, err + } + p := h.params("exec", "during-shutdown") + p.Size = 1 + p.SHA256 = guestSHA("x") + p.MaxOutput = 10 + go func() { + _, err := h.service.Execute(context.Background(), p, stream.BytesInput([]byte("x")), stream.BufferOutput(new(bytes.Buffer))) + done <- err + }() + <-entered + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) + defer cancel() + if err := h.service.Shutdown(ctx); !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("shutdown ignored caller deadline: %v", err) + } + close(release) + if err := <-done; err != nil { + t.Fatal(err) + } + if err := h.service.Shutdown(context.Background()); err != nil { + t.Fatal(err) + } + assertGuestSpoolClean(t, h) +} diff --git a/internal/app/guest_execute.go b/internal/app/guest_execute.go new file mode 100644 index 0000000..ca58d55 --- /dev/null +++ b/internal/app/guest_execute.go @@ -0,0 +1,255 @@ +package app + +import ( + "context" + "errors" + "io" + "reflect" + "strings" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +func validGuestRoot(root stream.RootIdentity) bool { + return len(root.Path) >= 4 && root.Path[1] == ':' && root.Path[2] == '\\' && !strings.ContainsAny(root.Path, "/\x00") && domain.ValidateBoundedString(root.Path, 4, 1024, ErrGuestOwnership) == nil && root.Volume > 0 && len(root.FileID) == 32 && validLabDigest(root.FileID+root.FileID) && root.CreationTime > 0 && root.Owner != "" && validLabDigest(root.SDDLHash) +} +func validGuestWorker(worker stream.WorkerIdentity) bool { + return worker.PID > 0 && worker.BirthTicks > 0 && domain.ValidateBoundedString(worker.Image, 1, 1024, ErrGuestOwnership) == nil && domain.ValidateBoundedString(worker.SID, 1, 256, ErrGuestOwnership) == nil +} +func guestFileRoot(file stream.FileIdentity) stream.RootIdentity { + return stream.RootIdentity{Path: file.RootPath, Volume: file.RootVolume, FileID: file.RootFileID, CreationTime: file.RootCreationTime, Owner: file.RootOwner, SDDLHash: file.RootSDDLHash} +} +func validGuestFile(file stream.FileIdentity, root stream.RootIdentity) bool { + return guestFileRoot(file) == root && file.Volume == root.Volume && len(file.FileID) == 32 && validLabDigest(file.FileID+file.FileID) && file.CreationTime > 0 && file.Owner == root.Owner && validLabDigest(file.SDDLHash) && file.Size >= 0 && validLabDigest(file.SHA256) +} + +func (s *GuestService) run(ctx context.Context, p GuestOperationParams, op domain.Operation, config GuestConfiguration, key, id string, input io.Reader, output io.Writer, effect *bool, plan *guestRetirementPlan) (out GuestOperationResult, runErr error) { + attempted := false + var worker guestWorkerRecord + defer func() { + if runErr != nil && attempted { + runErr = errors.Join(runErr, s.retireWorker(ctx, key, worker)) + } + }() + if plan != nil { + if !plan.consume(op, config, key) { + return out, ErrGuestAuthority + } + } else if err := s.checkRetirement(ctx, key, p.Operation != "prepare", guestWorkerRecord{Target: op.Target, Caller: op.Actor.AuthenticatedCaller, Actor: op.Actor.EffectiveActor, Generation: config.Generation}); err != nil { + return out, err + } + var err error + if p.Operation == "prepare" { + worker, attempted, err = s.prepareGuestWorker(ctx, op, config, key, id, effect) + } else { + worker, err = s.preparedGuestWorker(ctx, key) + } + if err != nil { + return out, err + } + if !validGuestWorkerBinding(worker, op, config) { + return out, ErrGuestAuthority + } + request, owned, err := s.request(ctx, p, op, config, id) + if err != nil { + return out, err + } + // Prepare and ownership reads can take time. Refresh the exact server authority + // again after their durable writes, immediately before the transport effect. + if err := s.validateEffectAuthority(ctx, p, config); err != nil { + return out, err + } + *effect = true // Download dispatch/exposure is an admitted effect too. + attempted = true + result, err := s.transport.Execute(ctx, op.Target, key, config.Generation, request, input, output) + if err != nil { + return out, safeGuestTransportError(err) + } + out, err = verifiedGuestResult(id, worker.Identity, result, input, output) + if err != nil { + return out, err + } + return s.applyGuestResult(ctx, p, op, config, key, id, request, owned, result, out) +} + +func (s *GuestService) prepareGuestWorker(ctx context.Context, op domain.Operation, config GuestConfiguration, key, id string, effect *bool) (worker guestWorkerRecord, attempted bool, err error) { + worker = guestWorkerRecord{Target: op.Target, Caller: op.Actor.AuthenticatedCaller, Actor: op.Actor.EffectiveActor, Generation: config.Generation, Root: config.WorkspaceIdentity, RecordID: id} + s.mu.Lock() + previous := s.workers[key] + s.mu.Unlock() + if previous.RecordID != "" { + *effect = true + if err = s.retireWorker(ctx, key, previous); err != nil { + return previous, false, err + } + } + if err = s.recordLaunch(ctx, id, worker); err != nil { + return worker, false, err + } + *effect = true + identity, err := s.transport.Prepare(ctx, op.Target, key, config.Generation) + worker.Identity = identity + if err != nil { + return worker, true, safeGuestTransportError(err) + } + if !validGuestWorker(identity) || identity.SID != config.WorkspaceIdentity.Owner { + return worker, true, guestProtocolError() + } + if err = s.store.write(ctx, id, ".worker", worker); err != nil { + return worker, true, ErrGuestUncertain + } + if err = s.recordReady(ctx, id, worker); err != nil { + return worker, true, err + } + return worker, true, nil +} +func (s *GuestService) preparedGuestWorker(ctx context.Context, key string) (guestWorkerRecord, error) { + s.mu.Lock() + worker := s.workers[key] + s.mu.Unlock() + if worker.RecordID == "" { + return worker, ErrGuestNotPrepared + } + var persisted guestWorkerRecord + if err := s.store.read(ctx, worker.RecordID, ".worker", &persisted); err != nil || !reflect.DeepEqual(persisted, worker) { + return worker, ErrGuestOwnership + } + return worker, nil +} + +func (s *GuestService) applyGuestResult(ctx context.Context, p GuestOperationParams, op domain.Operation, config GuestConfiguration, key, id string, request stream.Request, owned []guestOwnedArtifact, result stream.Result, out GuestOperationResult) (GuestOperationResult, error) { + switch p.Operation { + case "prepare": + if err := verifyGuestRoot(result.Root, config.WorkspaceIdentity); err != nil { + return out, err + } + var worker guestWorkerRecord + err := s.store.read(ctx, id, ".worker", &worker) + if err != nil { + return out, err + } + if worker.RecordID != id || !validGuestWorkerBinding(worker, op, config) || worker.Identity != out.Worker { + return out, ErrGuestOwnership + } + out.WorkerRecordID = worker.RecordID + s.mu.Lock() + s.workers[key] = worker + s.mu.Unlock() + case "put": + return s.applyGuestPut(ctx, p, op, config, id, request, result, out) + case "get": + if !validGuestDownload(result, owned[0]) { + return out, guestProtocolError() + } + case "exec": + if result.Size < 0 || result.Size > p.MaxOutput || !validLabDigest(result.SHA256) { + return out, guestProtocolError() + } + case "remove-owned": + return applyGuestRemoval(owned, result, out) + } + return out, nil +} +func (s *GuestService) applyGuestPut(ctx context.Context, p GuestOperationParams, op domain.Operation, config GuestConfiguration, id string, request stream.Request, result stream.Result, out GuestOperationResult) (GuestOperationResult, error) { + if len(result.Files) != 1 { + return out, guestProtocolError() + } + file := result.Files[0] + if !validGuestFile(file, config.WorkspaceIdentity) || file.Path != request.Path || file.Size != p.Size || file.SHA256 != p.SHA256 { + return out, guestProtocolError() + } + artifact := GuestArtifact{ID: id[:32], Size: file.Size, SHA256: file.SHA256} + record := guestOwnedArtifact{Artifact: artifact, Target: op.Target, Caller: op.Actor.AuthenticatedCaller, Actor: op.Actor.EffectiveActor, Generation: config.Generation, Identity: file} + if err := s.store.write(ctx, guestArtifactStoreID(artifact.ID), ".artifact", record); err != nil { + return out, ErrGuestUncertain + } + out.Artifacts = []GuestArtifact{artifact} + out.Size = file.Size + out.SHA256 = file.SHA256 + return out, nil +} +func applyGuestRemoval(owned []guestOwnedArtifact, result stream.Result, out GuestOperationResult) (GuestOperationResult, error) { + if len(result.Removed) != len(owned) { + return out, guestProtocolError() + } + for i, removed := range result.Removed { + if removed.Path != owned[i].Identity.Path || !validGuestRemoval(removed) { + return out, guestProtocolError() + } + out.Removed = append(out.Removed, GuestRemoval{ArtifactID: owned[i].Artifact.ID, Attempted: true, Accepted: removed.Status == "deleted", Absent: removed.Absent, Status: removed.Status}) + } + return out, nil +} +func validGuestRemoval(removed stream.Removal) bool { + switch removed.Status { + case "deleted": + return removed.Absent + case "preserved", "absence_unconfirmed", "expired": + return true + default: + return false + } +} + +func (s *GuestService) request(ctx context.Context, p GuestOperationParams, op domain.Operation, config GuestConfiguration, id string) (stream.Request, []guestOwnedArtifact, error) { + request := stream.Request{ID: id, Operation: p.Operation, DeadlineUTC: op.Deadline, Size: p.Size, SHA256: p.SHA256, MaxOutput: p.MaxOutput} + var owned []guestOwnedArtifact + for _, artifactID := range p.ArtifactIDs { + var record guestOwnedArtifact + if err := s.store.read(ctx, guestArtifactStoreID(artifactID), ".artifact", &record); err != nil { + return request, nil, ErrGuestOwnership + } + if !validGuestOwned(record, artifactID, op, config) { + return request, nil, ErrGuestOwnership + } + + owned = append(owned, record) + request.Owned = append(request.Owned, record.Identity) + } + switch p.Operation { + case "prepare": + request.Operation = "observe" + request.Path = config.WorkspaceIdentity.Path + case "put": + request.Path = guestArtifactPath(config.WorkspaceIdentity.Path, id[:32]) + root := config.WorkspaceIdentity + request.ExpectedRoot = &root + case "get": + request.Path = owned[0].Identity.Path + if p.MaxOutput < owned[0].Artifact.Size { + return request, nil, errors.New("app: guest download exceeds output budget") + } + } + return request, owned, nil +} + +func validGuestOwned(record guestOwnedArtifact, artifactID string, op domain.Operation, config GuestConfiguration) bool { + return record.Artifact.ID == artifactID && record.Target == op.Target && record.Caller == op.Actor.AuthenticatedCaller && record.Actor == op.Actor.EffectiveActor && record.Generation == config.Generation && validGuestFile(record.Identity, config.WorkspaceIdentity) && record.Identity.Path == guestArtifactPath(config.WorkspaceIdentity.Path, artifactID) && record.Artifact.Size == record.Identity.Size && record.Artifact.SHA256 == record.Identity.SHA256 +} + +func verifiedGuestResult(id string, worker stream.WorkerIdentity, result stream.Result, input io.Reader, output io.Writer) (GuestOperationResult, error) { + var out GuestOperationResult + if result.ID != id || result.Status != "ok" || result.Worker != worker { + return out, guestProtocolError() + } + if checked, ok := input.(*guestCheckedInput); ok && !checked.valid() { + return out, stream.ErrIntegrity + } + out.Worker = result.Worker + out.Size = result.Size + out.SHA256 = result.SHA256 + out.ExitCode = result.ExitCode + if spool, ok := output.(*guestSpool); ok && !spool.matches(out) { + return out, stream.ErrIntegrity + } + return out, nil +} + +func validGuestWorkerBinding(worker guestWorkerRecord, op domain.Operation, config GuestConfiguration) bool { + return worker.Target == op.Target && worker.Actor == op.Actor.EffectiveActor && worker.Caller == op.Actor.AuthenticatedCaller && worker.Generation == config.Generation && worker.Root == config.WorkspaceIdentity +} + +func validGuestDownload(result stream.Result, owned guestOwnedArtifact) bool { + return len(result.Files) == 1 && result.Files[0] == owned.Identity && result.Size == owned.Artifact.Size && result.SHA256 == owned.Artifact.SHA256 +} diff --git a/internal/app/guest_operation.go b/internal/app/guest_operation.go new file mode 100644 index 0000000..4826eec --- /dev/null +++ b/internal/app/guest_operation.go @@ -0,0 +1,246 @@ +package app + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "io" + "reflect" + "slices" + "strings" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + "github.com/Horcag/agent-machine-control/internal/sessions" +) + +func guestDigest(value any) string { + data, _ := json.Marshal(value) + sum := sha256.Sum256(data) + return hex.EncodeToString(sum[:]) +} +func guestPoolKey(target domain.MachineRef, actor domain.ActorContext, generation string) string { + return guestDigest([]string{string(target), string(actor.AuthenticatedCaller), string(actor.EffectiveActor), generation}) +} + +func (s *GuestService) resolve(ctx context.Context, p GuestOperationParams) (TargetResolution, GuestConfiguration, error) { + if !s.guestDependenciesAvailable() { + return TargetResolution{}, GuestConfiguration{}, ErrGuestAuthority + } + if err := p.Caller.Validate(); err != nil { + return TargetResolution{}, GuestConfiguration{}, err + } + resolution, err := s.resolver.ResolveTarget(ctx, p.Target) + if err != nil { + return resolution, GuestConfiguration{}, err + } + if err = resolution.Validate(); err != nil { + return resolution, GuestConfiguration{}, err + } + config, err := s.config.Current(ctx, domain.MachineRef(resolution.Locator.String()), p.Caller) + if err != nil { + if errors.Is(err, ErrGuestWorkspaceUnprovisioned) { + return resolution, config, ErrGuestWorkspaceUnprovisioned + } + return resolution, config, ErrGuestAuthority + } + if !validGuestConfiguration(config, p.Caller) { + return resolution, config, ErrGuestAuthority + } + + return resolution, config, ctx.Err() +} + +func guestOperation(p GuestOperationParams, target domain.MachineRef, config GuestConfiguration) (domain.Operation, error) { + digest := p.SHA256 + if p.Operation != "put" && p.Operation != "exec" { + if digest != "" { + return domain.Operation{}, domain.ErrNonCanonicalParameter + } + sum := sha256.Sum256(nil) + digest = hex.EncodeToString(sum[:]) + } + if p.Deadline.IsZero() { + return domain.Operation{}, domain.ErrMissingDeadline + } + kind := domain.OperationKind("guest." + p.Operation) + if !domain.IsGuestOperation(kind) { + return domain.Operation{}, domain.ErrInvalidOperationKind + } + op := domain.Operation{Kind: kind, Target: target, Actor: config.Caller.Clone(), Reason: p.Reason, Deadline: p.Deadline.UTC(), IdempotencyKey: p.IdempotencyKey, RequiredCapability: domain.CapabilityGuestOperation, RequiredScopes: domain.GuestOperationScopes(kind), Classification: domain.ClassReversibleMutation, EvidenceSensitivity: domain.EvidenceSensitivityStandard, Parameters: map[string]any{"operation": p.Operation, "configuration_sha256": guestDigest(struct { + Generation string + Root any + }{config.Generation, config.WorkspaceIdentity}), "payload_sha256": digest, "size": p.Size, "max_output": p.MaxOutput, "artifact_ids": append([]string{}, p.ArtifactIDs...)}} + if err := op.Validate(); err != nil { + return op, err + } + return op, domain.ValidateOperationParameters(kind, op.Parameters) +} + +// Execute admits one complete request. A retry returns durable metadata only; +// streamed output is never replayed or represented as a host-side durable file. +func (s *GuestService) Execute(ctx context.Context, p GuestOperationParams, input io.Reader, output io.Writer) (out GuestOperationResult, callErr error) { + if err := s.beginGuestCall(); err != nil { + return out, err + } + defer s.endGuestCall() + resolution, config, err := s.resolve(ctx, p) + if err != nil { + return out, err + } + p.Target = resolution.Locator.String() + p.Caller = config.Caller + op, err := guestOperation(p, domain.MachineRef(p.Target), config) + if err != nil { + return out, err + } + if !op.Deadline.After(s.session.now()) { + return out, context.DeadlineExceeded + } + if err = checkGuestScopes(op); err != nil { + return out, err + } + ctx, cancel, _, timeout := s.session.beginSessionMutation(ctx, 0, op.Deadline) + defer cancel() + idFP, err := domain.ComputeIdempotencyFingerprint(op) + if err != nil { + return out, err + } + recordID := strings.TrimPrefix(string(idFP), "sha256:") + key := guestPoolKey(op.Target, op.Actor, config.Generation) + dispatched := false + effectApplied := false + input, spool, sink, err := s.prepareGuestIO(ctx, p, input, output) + if err != nil { + return out, err + } + if spool != nil { + defer func() { callErr = errors.Join(callErr, spool.cleanup()) }() + } + var transportOutput io.Writer + if spool != nil { + transportOutput = spool + } + _, rcpt, err := s.session.coordinateSessionMutation(ctx, op, "guest:"+guestDigest([]string{string(op.Actor.EffectiveActor), string(op.Target), p.IdempotencyKey}), nil, p.ApprovalID, timeout, func(execCtx context.Context) (sessionMutationResult, error) { + dispatched = true + var runErr error + out, effectApplied, runErr = s.dispatchGuest(execCtx, p, op, config, key, recordID, input, transportOutput) + return sessionMutationResult{EffectApplied: effectApplied, ExitCode: out.ExitCode, EvidenceRefs: guestEvidenceRefs(runErr)}, runErr + }) + return s.finishGuest(ctx, p, config, out, rcpt, err, dispatched, effectApplied, recordID, spool, sink) +} + +func guestEvidenceRefs(err error) []string { + refs := []string{"guest-operation-dispatched"} + if diagnostic := guestFailureEvidence(err); diagnostic != "" { + refs = append(refs, diagnostic) + } + return refs +} + +func (s *GuestService) dispatchGuest(ctx context.Context, p GuestOperationParams, op domain.Operation, config GuestConfiguration, key, id string, input io.Reader, output io.Writer) (GuestOperationResult, bool, error) { + plan := s.readonlyRetirementPlan(ctx, p, op, config, key) + if plan == nil { + if err := s.validateEffectAuthority(ctx, p, config); err != nil { + return GuestOperationResult{}, false, err + } + } + if err := checkGuestScopes(op); err != nil { + return GuestOperationResult{}, false, err + } + effect := false + out, err := s.run(ctx, p, op, config, key, id, input, output, &effect, plan) + if err == nil { + if persistErr := s.store.write(ctx, id, ".result", out); persistErr != nil { + err = ErrGuestUncertain + } + } + return out, effect, err +} + +func (s *GuestService) prepareGuestIO(ctx context.Context, p GuestOperationParams, input io.Reader, output io.Writer) (io.Reader, *guestSpool, stream.ContextWriter, error) { + needsInput := p.Operation == "put" || p.Operation == "exec" + needsOutput := p.Operation == "get" || p.Operation == "exec" + if needsInput != (input != nil) || needsOutput != (output != nil) { + return nil, nil, nil, domain.ErrNonCanonicalParameter + } + if input != nil { + source, ok := input.(stream.ContextReader) + if !ok { + return nil, nil, nil, domain.ErrNonCanonicalParameter + } + input = &guestCheckedInput{source: source, size: p.Size, expected: p.SHA256, digest: sha256.New()} + } + if output == nil { + return input, nil, nil, nil + } + sink, ok := output.(stream.ContextWriter) + if !ok { + return nil, nil, nil, domain.ErrNonCanonicalParameter + } + spool, err := s.spool(ctx, p) + return input, spool, sink, err +} + +func (s *GuestService) finishGuest(ctx context.Context, p GuestOperationParams, config GuestConfiguration, out GuestOperationResult, rcpt *domain.Receipt, err error, dispatched, effectApplied bool, recordID string, spool *guestSpool, sink stream.ContextWriter) (GuestOperationResult, error) { + out.Receipt = rcpt + out.CachedReceipt = !dispatched && rcpt != nil + if !dispatched && err == nil && rcpt != nil { + var cached GuestOperationResult + if readErr := s.store.read(ctx, recordID, ".result", &cached); readErr != nil { + return out, ErrGuestUncertain + } + cached.Receipt = rcpt + cached.CachedReceipt = true + out = cached + } + if err != nil && (guestOutcomeUncertain(err, rcpt, effectApplied)) { + err = errors.Join(ErrGuestUncertain, err) + } + if err == nil && spool != nil && dispatched { + err = s.publishGuest(ctx, p, config, out, rcpt, spool, sink) + } + + return out, err +} + +func (s *GuestService) publishGuest(ctx context.Context, p GuestOperationParams, config GuestConfiguration, out GuestOperationResult, rcpt *domain.Receipt, spool *guestSpool, sink stream.ContextWriter) error { + if rcpt == nil || rcpt.Validate() != nil || rcpt.Outcome.Status != domain.OutcomeSuccess || !spool.matches(out) { + return ErrGuestUncertain + } + _, fresh, err := s.resolve(ctx, p) + if err != nil || !reflect.DeepEqual(fresh, config) { + return ErrGuestUncertain + } + if err = spool.publish(ctx, sink); err != nil { + return errors.Join(ErrGuestUncertain, err) + } + return nil +} + +func checkGuestScopes(op domain.Operation) error { + for _, scope := range op.RequiredScopes { + if !op.Actor.HasScope(scope) { + return domain.ErrSessionAccessDenied + } + } + return nil +} +func guestArtifactPath(root, id string) string { + return strings.TrimRight(root, "\\") + "\\amc-" + id + ".bin" +} +func guestArtifactStoreID(id string) string { return guestDigest("guest-artifact:" + id) } +func guestProtocolError() error { return fmt.Errorf("%w: invalid guest result", ErrGuestOwnership) } +func (s *GuestService) guestDependenciesAvailable() bool { + return s != nil && s.session != nil && s.resolver != nil && s.config != nil && s.transport != nil && s.store != nil && s.session.leaseMgr != nil +} +func validGuestConfiguration(config GuestConfiguration, caller domain.ActorContext) bool { + return config.Caller.Validate() == nil && config.Caller.AuthenticatedCaller == caller.AuthenticatedCaller && config.Caller.EffectiveActor == caller.EffectiveActor && config.Caller.EffectivePermissions.IsSubsetOf(caller.EffectivePermissions) && config.Caller.CallerPermissions.IsSubsetOf(caller.CallerPermissions) && domain.ValidateBoundedString(config.Generation, 1, 256, ErrGuestAuthority) == nil && validGuestRoot(config.WorkspaceIdentity) +} + +func guestOutcomeUncertain(err error, rcpt *domain.Receipt, effect bool) bool { + return effect || errors.Is(err, sessions.ErrMutationEffectUnknown) || errors.Is(err, sessions.ErrMutationFinalizationPending) || (rcpt != nil && rcpt.Outcome.Status == domain.OutcomeFailed && slices.Contains(rcpt.EvidenceRefs, "guest-operation-dispatched")) +} diff --git a/internal/app/guest_remote_exit_test.go b/internal/app/guest_remote_exit_test.go new file mode 100644 index 0000000..ae55768 --- /dev/null +++ b/internal/app/guest_remote_exit_test.go @@ -0,0 +1,51 @@ +package app + +import ( + "bytes" + "context" + "errors" + "fmt" + "io" + "strings" + "testing" + + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +func TestGuestRemoteExitRetiresOriginalAndDoesNotPublishOutput(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + h.transport.executeHook = func(_ context.Context, _ stream.Request, _ io.Reader, sink io.Writer) (stream.Result, error) { + _, _ = sink.Write([]byte("private output")) + return stream.Result{}, fmt.Errorf("private guest path: %w", &stream.RemoteExit{Code: 1}) + } + p := h.params("exec", "remote-exit") + p.Size, p.SHA256, p.MaxOutput = 1, guestSHA("x"), 64 + var output bytes.Buffer + out, err := h.service.Execute(t.Context(), p, stream.BytesInput([]byte("x")), stream.BufferOutput(&output)) + if !errors.Is(err, ErrGuestUncertain) || output.Len() != 0 { + t.Fatal("remote exit published output or lost uncertainty", err) + } + assertGuestFailedDiagnostic(t, h, out, "guest-remote-exit:1") + assertGuestSpoolClean(t, h) +} + +func TestGuestRemoteExitCopiesOnlyNumericStatusAndKeepsUncertainty(t *testing.T) { + for _, code := range []int{0, 1, 215, 0xc0000005} { + original := &stream.RemoteExit{Code: code} + err := safeGuestTransportError(fmt.Errorf("private guest path: %w", original)) + copied, ok := errors.AsType[*stream.RemoteExit](err) + if !ok || copied == original || copied.Code != code || !errors.Is(err, ErrGuestUncertain) || strings.Contains(err.Error(), "private") { + t.Fatal("unsafe exit diagnostic", err) + } + if guestFailureEvidence(err) != fmt.Sprintf("guest-remote-exit:%d", code) { + t.Fatal("numeric evidence missing", err) + } + } + for _, code := range []int{-1, 0x100000000} { + err := safeGuestTransportError(fmt.Errorf("private: %w", &stream.RemoteExit{Code: code})) + if err != ErrGuestUncertain || guestFailureEvidence(err) != "" { + t.Fatal("invalid exit leaked", err) + } + } +} diff --git a/internal/app/guest_reprepare_test.go b/internal/app/guest_reprepare_test.go new file mode 100644 index 0000000..e0a434d --- /dev/null +++ b/internal/app/guest_reprepare_test.go @@ -0,0 +1,135 @@ +package app + +import ( + "context" + "errors" + "os" + "path/filepath" + "slices" + "testing" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +type guestReplacementTransport struct { + *guestTestTransport + events *[]string + old stream.WorkerIdentity + closed bool +} + +func (f *guestReplacementTransport) Retire(target domain.MachineRef, key, generation string) error { + *f.events = append(*f.events, "retire") + f.closed = true + return f.guestTestTransport.Retire(target, key, generation) +} +func (f *guestReplacementTransport) Prepare(ctx context.Context, target domain.MachineRef, key, generation string) (stream.WorkerIdentity, error) { + *f.events = append(*f.events, "prepare") + if f.closed { + f.worker.PID++ + f.worker.BirthTicks++ + } + return f.guestTestTransport.Prepare(ctx, target, key, generation) +} + +type guestReplacementConfig struct { + *guestTestConfig + transport *guestReplacementTransport + absent bool +} + +func (c *guestReplacementConfig) ConfirmGuestRetirement(_ context.Context, _ domain.MachineRef, _ domain.ActorID, _ string, worker stream.WorkerIdentity) (bool, error) { + *c.transport.events = append(*c.transport.events, "confirm") + if worker != c.transport.old { + return false, ErrGuestOwnership + } + return c.absent && c.transport.closed, nil +} +func replacementHarness(t *testing.T) (*guestHarness, *guestReplacementTransport, *guestReplacementConfig, *[]string) { + t.Helper() + h := newGuestHarness(t) + events := []string{} + transport := &guestReplacementTransport{guestTestTransport: h.transport, events: &events, old: h.transport.worker} + config := &guestReplacementConfig{guestTestConfig: h.config, transport: transport, absent: true} + h.service.transport, h.service.config = transport, config + h.prepare(t) + events = nil + return h, transport, config, &events +} +func currentReplacementRecord(t *testing.T, h *guestHarness) (string, guestWorkerRecord) { + t.Helper() + for key, worker := range h.service.workers { + return key, worker + } + t.Fatal("no admitted original") + return "", guestWorkerRecord{} +} +func TestGuestHealthyReprepareRetiresOriginalBeforeReplacement(t *testing.T) { + h, transport, _, events := replacementHarness(t) + _, original := currentReplacementRecord(t, h) + out, err := h.service.Execute(t.Context(), h.params("prepare", "replacement"), nil, nil) + if err != nil || !slices.Equal(*events, []string{"retire", "confirm", "prepare"}) || transport.prepared != 2 || out.Worker == original.Identity { + t.Fatalf("replacement crossed retirement barrier: %+v %v events=%v", out, err, *events) + } + var retired guestWorkerRecord + if err = h.service.store.read(t.Context(), original.RecordID, ".retired", &retired); err != nil || retired != original { + t.Fatalf("original retirement changed: %+v %v", retired, err) + } +} +func TestGuestHealthyReprepareNativeRefusalPreventsNewPrepare(t *testing.T) { + h, transport, config, events := replacementHarness(t) + config.absent = false + _, original := currentReplacementRecord(t, h) + _, err := h.service.Execute(t.Context(), h.params("prepare", "refused"), nil, nil) + if !errors.Is(err, ErrGuestUncertain) || !slices.Equal(*events, []string{"retire", "confirm"}) || transport.prepared != 1 { + t.Fatalf("unconfirmed replacement: %v events=%v", err, *events) + } + var pending guestWorkerRecord + if err = h.service.store.read(t.Context(), original.RecordID, ".retirement", &pending); err != nil || pending != original { + t.Fatal("original obligation lost", err) + } +} +func TestGuestHealthyReprepareRejectsOriginalDocumentDrift(t *testing.T) { + for _, tc := range []struct { + name, suffix string + alter func(*guestWorkerRecord) + }{ + {"missing_worker", ".worker", nil}, {"missing_ready", ".ready", nil}, + {"generation", ".ready", func(r *guestWorkerRecord) { r.Generation = "foreign" }}, + {"caller", ".worker", func(r *guestWorkerRecord) { r.Caller = "foreign" }}, + {"actor", ".ready", func(r *guestWorkerRecord) { r.Actor = "foreign" }}, + {"root", ".worker", func(r *guestWorkerRecord) { r.Root.FileID = "foreign" }}, + {"identity", ".ready", func(r *guestWorkerRecord) { r.Identity.BirthTicks++ }}, + } { + t.Run(tc.name, func(t *testing.T) { + h, transport, _, events := replacementHarness(t) + _, original := currentReplacementRecord(t, h) + path := filepath.Join(h.service.store.directory, original.RecordID+tc.suffix) + if err := os.Remove(path); err != nil { + t.Fatal(err) + } + if tc.alter != nil { + changed := original + tc.alter(&changed) + if err := h.service.store.write(t.Context(), original.RecordID, tc.suffix, changed); err != nil { + t.Fatal(err) + } + } + if _, err := h.service.Execute(t.Context(), h.params("prepare", "drift"), nil, nil); !errors.Is(err, ErrGuestUncertain) || transport.prepared != 1 || len(*events) != 0 { + t.Fatalf("drift reached effect: %v events=%v", err, *events) + } + }) + } +} +func TestGuestHealthyRepreparePendingRetirementRemainsFirstBarrier(t *testing.T) { + h, transport, config, events := replacementHarness(t) + config.absent = false + _, original := currentReplacementRecord(t, h) + if err := h.service.store.write(t.Context(), original.RecordID, ".retirement", original); err != nil { + t.Fatal(err) + } + if _, err := h.service.Execute(t.Context(), h.params("prepare", "pending"), nil, nil); !errors.Is(err, ErrGuestUncertain) || transport.prepared != 1 || !slices.Equal(*events, []string{"confirm"}) { + t.Fatalf("pending bypassed: %v events=%v", err, *events) + } +} diff --git a/internal/app/guest_retirement.go b/internal/app/guest_retirement.go new file mode 100644 index 0000000..9fc2a7f --- /dev/null +++ b/internal/app/guest_retirement.go @@ -0,0 +1,193 @@ +package app + +import ( + "context" + "errors" + "os" + "reflect" + "strings" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +// GuestRetirementVerifier supplies fresh exact native process absence evidence. +// Closing SSH alone cannot confirm the Windows process or descendant job exited. +type GuestRetirementVerifier interface { + ConfirmGuestRetirement(context.Context, domain.MachineRef, domain.ActorID, string, stream.WorkerIdentity) (bool, error) +} + +func (s *GuestService) checkRetirement(ctx context.Context, key string, allowCurrent bool, worker guestWorkerRecord) error { + entries, err := os.ReadDir(s.store.directory) + if errors.Is(err, os.ErrNotExist) { + return nil + } + if err != nil { + return ErrGuestUncertain + } + count := 0 + for _, entry := range entries { + if !strings.HasSuffix(entry.Name(), ".launch") { + continue + } + priorKey := strings.TrimSuffix(entry.Name(), ".launch") + var launch guestWorkerRecord + if err := s.store.read(ctx, priorKey, ".launch", &launch); err != nil || launch.RecordID != priorKey { + return ErrGuestUncertain + } + if launch.Target != worker.Target || launch.Actor != worker.Actor || launch.Caller != worker.Caller { + continue + } + count++ + if count > 128 { + return ErrGuestUncertain + } + if err := s.checkPriorGuestLaunch(ctx, key, priorKey, allowCurrent, launch); err != nil { + return err + } + + } + return nil +} + +func (s *GuestService) confirmRetirement(ctx context.Context, key string, worker guestWorkerRecord) error { + var confirmed guestWorkerRecord + if err := s.store.read(ctx, key, ".retired", &confirmed); err == nil { + if !reflect.DeepEqual(confirmed, worker) { + return ErrGuestUncertain + } + return nil + } else if !errors.Is(err, os.ErrNotExist) { + return ErrGuestUncertain + } + verifier, ok := s.config.(GuestRetirementVerifier) + if !ok || !validGuestWorker(worker.Identity) { + return ErrGuestUncertain + } + absent, err := verifier.ConfirmGuestRetirement(ctx, worker.Target, worker.Actor, worker.Generation, worker.Identity) + if err != nil || !absent || ctx.Err() != nil { + return errors.Join(ErrGuestUncertain, err, ctx.Err()) + } + if err = s.store.write(ctx, key, ".retired", worker); err != nil { + if errors.Is(err, os.ErrExist) { + var existing guestWorkerRecord + if readErr := s.store.read(ctx, key, ".retired", &existing); readErr == nil && reflect.DeepEqual(existing, worker) { + return nil + } + } + return errors.Join(ErrGuestUncertain, err) + } + return nil +} + +func (s *GuestService) retireWorker(parent context.Context, key string, worker guestWorkerRecord) error { + ctx, cancel := context.WithTimeout(context.WithoutCancel(parent), 5*time.Second) + defer cancel() + return s.retireWorkerContext(ctx, key, worker, true) +} + +func (s *GuestService) retireWorkerContext(ctx context.Context, key string, worker guestWorkerRecord, closeWithoutIntent bool) error { + if err := ctx.Err(); err != nil { + return errors.Join(ErrGuestUncertain, err) + } + s.mu.Lock() + s.workers[key] = worker + s.mu.Unlock() + persistErr := s.store.write(ctx, worker.RecordID, ".retirement", worker) + if persistErr != nil { + var prior guestWorkerRecord + if readErr := s.store.read(ctx, worker.RecordID, ".retirement", &prior); readErr == nil && reflect.DeepEqual(prior, worker) { + persistErr = nil + } + } + if persistErr != nil && !closeWithoutIntent { + return errors.Join(ErrGuestUncertain, persistErr) + } + if err := ctx.Err(); err != nil { + return errors.Join(ErrGuestUncertain, persistErr, err) + } + closeErr := s.transport.Retire(worker.Target, key, worker.Generation) + if closeErr != nil || persistErr != nil { + return errors.Join(ErrGuestUncertain, closeErr, persistErr) + } + return s.confirmRetirement(ctx, worker.RecordID, worker) +} + +func (s *GuestService) recordLaunch(ctx context.Context, key string, record guestWorkerRecord) error { + if err := s.store.write(ctx, key, ".launch", record); err != nil { + var prior guestWorkerRecord + if readErr := s.store.read(ctx, key, ".launch", &prior); readErr != nil || !reflect.DeepEqual(prior, record) { + return ErrGuestUncertain + } + } + return nil +} + +func (s *GuestService) recordReady(ctx context.Context, key string, worker guestWorkerRecord) error { + if err := s.store.write(ctx, key, ".ready", worker); err != nil { + var prior guestWorkerRecord + if readErr := s.store.read(ctx, key, ".ready", &prior); readErr != nil || !reflect.DeepEqual(prior, worker) { + return ErrGuestUncertain + } + } + return nil +} + +func (s *GuestService) checkPriorGuestLaunch(ctx context.Context, key, priorKey string, allowCurrent bool, launch guestWorkerRecord) error { + var err error + var pending guestWorkerRecord + pendingErr := s.store.read(ctx, priorKey, ".retirement", &pending) + if pendingErr == nil { + if !sameGuestLaunch(pending, launch) { + return ErrGuestUncertain + } + if err = s.confirmRetirement(ctx, priorKey, pending); err != nil { + return err + } + return nil + } + if !errors.Is(pendingErr, os.ErrNotExist) { + return ErrGuestUncertain + } + s.mu.Lock() + active := s.workers[key] + s.mu.Unlock() + if allowCurrent && active.RecordID == priorKey { + return nil + } + if !allowCurrent && active.RecordID == priorKey { + return s.validateCurrentGuestLaunch(ctx, key, priorKey, active, launch) + } + var ready guestWorkerRecord + if err = s.store.read(ctx, priorKey, ".ready", &ready); err != nil { + return ErrGuestUncertain + } + if !sameGuestLaunch(ready, launch) { + return ErrGuestUncertain + } + if err = s.confirmRetirement(ctx, priorKey, ready); err != nil { + return err + } + return nil +} + +func sameGuestLaunch(record, launch guestWorkerRecord) bool { + return record.RecordID == launch.RecordID && record.Target == launch.Target && record.Caller == launch.Caller && record.Actor == launch.Actor && record.Generation == launch.Generation && record.Root == launch.Root +} + +// A fresh explicit prepare may retire only its admitted in-memory original. +// The actual close and native absence barrier remain in prepareGuestWorker. +func (s *GuestService) validateCurrentGuestLaunch(ctx context.Context, key, id string, active, launch guestWorkerRecord) error { + expectedKey := guestDigest([]string{string(active.Target), string(active.Caller), string(active.Actor), active.Generation}) + if expectedKey != key || !sameGuestLaunch(active, launch) || !validGuestWorker(active.Identity) || !validGuestRoot(active.Root) || active.Identity.SID != active.Root.Owner { + return ErrGuestUncertain + } + for _, suffix := range []string{".worker", ".ready"} { + var original guestWorkerRecord + if err := s.store.read(ctx, id, suffix, &original); err != nil || !reflect.DeepEqual(original, active) { + return ErrGuestUncertain + } + } + return ctx.Err() +} diff --git a/internal/app/guest_retirement_plan.go b/internal/app/guest_retirement_plan.go new file mode 100644 index 0000000..8dd3b70 --- /dev/null +++ b/internal/app/guest_retirement_plan.go @@ -0,0 +1,94 @@ +package app + +import ( + "context" + "errors" + "os" + "reflect" + "strings" + + "github.com/Horcag/agent-machine-control/internal/domain" +) + +// This invocation-local proof replaces the effect-capable retirement scan. It +// never caches authority: the final authority gate still precedes transport I/O. +type guestRetirementPlan struct { + operation domain.Operation + config GuestConfiguration + key string + consumed bool +} + +func (p *guestRetirementPlan) consume(op domain.Operation, config GuestConfiguration, key string) bool { + if p.consumed || p.key != key || !reflect.DeepEqual(p.operation, op) || !reflect.DeepEqual(p.config, config) { + return false + } + p.consumed = true + return true +} + +func (s *GuestService) readonlyRetirementPlan(ctx context.Context, p GuestOperationParams, op domain.Operation, config GuestConfiguration, key string) *guestRetirementPlan { + if p.Operation == "prepare" || ctx.Err() != nil { + return nil + } + s.mu.Lock() + current := s.workers[key] + s.mu.Unlock() + if current.RecordID == "" || !validGuestWorkerBinding(current, op, config) { + return nil + } + if !s.readonlyLaunchInventory(ctx, key, current) { + return nil + } + config.Caller = config.Caller.Clone() + return &guestRetirementPlan{operation: op.Clone(), config: config, key: key} +} + +func (s *GuestService) readonlyLaunchInventory(ctx context.Context, key string, current guestWorkerRecord) bool { + entries, err := os.ReadDir(s.store.directory) + if err != nil { + return false + } + found, count := false, 0 + for _, entry := range entries { + if !strings.HasSuffix(entry.Name(), ".launch") { + continue + } + id := strings.TrimSuffix(entry.Name(), ".launch") + var launch guestWorkerRecord + if err := s.store.read(ctx, id, ".launch", &launch); err != nil || launch.RecordID != id { + return false + } + if launch.Target != current.Target || launch.Caller != current.Caller || launch.Actor != current.Actor { + continue + } + count++ + if count > 128 || !s.readonlyGuestLaunch(ctx, key, launch, current) { + return false + } + found = found || id == current.RecordID + } + return found && ctx.Err() == nil +} + +func (s *GuestService) readonlyGuestLaunch(ctx context.Context, key string, launch, current guestWorkerRecord) bool { + var pending guestWorkerRecord + err := s.store.read(ctx, launch.RecordID, ".retirement", &pending) + if err != nil && !errors.Is(err, os.ErrNotExist) { + return false + } + if launch.RecordID == current.RecordID { + return errors.Is(err, os.ErrNotExist) && s.validateCurrentGuestLaunch(ctx, key, launch.RecordID, current, launch) == nil + } + var original guestWorkerRecord + if s.store.read(ctx, launch.RecordID, ".worker", &original) != nil || !sameGuestLaunch(original, launch) || !validGuestWorker(original.Identity) || !validGuestRoot(original.Root) || original.Identity.SID != original.Root.Owner { + return false + } + for _, suffix := range []string{".ready", ".retired"} { + var record guestWorkerRecord + if s.store.read(ctx, launch.RecordID, suffix, &record) != nil || !reflect.DeepEqual(record, original) { + return false + } + } + return (err != nil || reflect.DeepEqual(pending, original)) && ctx.Err() == nil +} diff --git a/internal/app/guest_retirement_plan_test.go b/internal/app/guest_retirement_plan_test.go new file mode 100644 index 0000000..498fe89 --- /dev/null +++ b/internal/app/guest_retirement_plan_test.go @@ -0,0 +1,233 @@ +package app + +import ( + "context" + "errors" + "fmt" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +func retirementPlan(t *testing.T, h *guestHarness) *guestRetirementPlan { + t.Helper() + p := h.params("put", "plan") + p.Size, p.SHA256 = 4, guestSHA("data") + resolution, config, err := h.service.resolve(t.Context(), p) + if err != nil { + t.Fatal(err) + } + p.Target, p.Caller = resolution.Locator.String(), config.Caller + op, err := guestOperation(p, domain.MachineRef(p.Target), config) + if err != nil { + t.Fatal(err) + } + return h.service.readonlyRetirementPlan(t.Context(), p, op, config, guestPoolKey(op.Target, op.Actor, config.Generation)) +} + +func TestGuestReadonlyRetirementUsesFinalFreshAuthority(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + if h.safety.calls != 3 { + t.Fatalf("prepare gates changed: %d", h.safety.calls) + } + h.safety.calls, h.config.calls = 0, 0 + h.put(t, "warm") + if h.safety.calls != 2 || h.config.calls != 2 { + t.Fatalf("warm authority gates: safety=%d config=%d", h.safety.calls, h.config.calls) + } + plan := retirementPlan(t, h) + if plan == nil || !plan.consume(plan.operation, plan.config, plan.key) || plan.consume(plan.operation, plan.config, plan.key) { + t.Fatal("plan is not invocation-local one-use proof") + } + plan = retirementPlan(t, h) + changed := plan.config + changed.Generation = "other" + if plan.consume(plan.operation, changed, plan.key) { + t.Fatal("plan accepted different configuration") + } +} + +func TestGuestReadonlyRetirementRejectsIncompleteOriginalInventory(t *testing.T) { + for _, tc := range []struct { + name, suffix string + drift func(*guestWorkerRecord) + }{ + {"missing_launch", ".launch", nil}, {"missing_worker", ".worker", nil}, {"missing_ready", ".ready", nil}, + {"birth", ".worker", func(r *guestWorkerRecord) { r.Identity.BirthTicks++ }}, + {"root", ".ready", func(r *guestWorkerRecord) { r.Root.FileID = strings.Repeat("f", 32) }}, + {"actor", ".worker", func(r *guestWorkerRecord) { r.Actor = "foreign" }}, + {"pending", ".retirement", func(_ *guestWorkerRecord) {}}, + } { + t.Run(tc.name, func(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + _, current := currentReplacementRecord(t, h) + changePlanDocument(t, h, current, tc.suffix, tc.drift) + if retirementPlan(t, h) != nil { + t.Fatal("uncertain inventory enabled shortcut") + } + }) + } + h := newGuestHarness(t) + h.prepare(t) + h.service.workers = map[string]guestWorkerRecord{} + if retirementPlan(t, h) != nil { + t.Fatal("missing current enabled shortcut") + } +} + +func TestGuestReadonlyRetirementRequiresExactRetiredRecords(t *testing.T) { + h, _, _, _ := replacementHarness(t) + _, old := currentReplacementRecord(t, h) + if _, err := h.service.Execute(t.Context(), h.params("prepare", "replacement"), nil, nil); err != nil { + t.Fatal(err) + } + if retirementPlan(t, h) == nil { + t.Fatal("complete retired original refused") + } + path := filepath.Join(h.service.store.directory, old.RecordID+".retired") + if err := os.Remove(path); err != nil { + t.Fatal(err) + } + old.Identity.BirthTicks++ + if err := h.service.store.write(t.Context(), old.RecordID, ".retired", old); err != nil { + t.Fatal(err) + } + if retirementPlan(t, h) != nil { + t.Fatal("partial retired record accepted") + } +} + +func TestGuestReadonlyRetirementFinalDriftNeverDispatches(t *testing.T) { + for _, field := range []string{"rollback", "generation", "root", "permissions"} { + t.Run(field, func(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + h.safety.calls, h.config.calls = 0, 0 + if field == "rollback" { + h.safety.classes = []domain.OperationClass{domain.ClassReversibleMutation, domain.ClassDestructivePrivileged} + } else { + h.config.change = func(c *GuestConfiguration, call int) { + if call != 2 { + return + } + switch field { + case "generation": + c.Generation = "changed" + case "root": + c.WorkspaceIdentity.FileID = strings.Repeat("f", 32) + case "permissions": + delete(c.Caller.EffectivePermissions, domain.ScopeSessionOpen) + } + } + } + p := h.params("put", "drift") + p.Size, p.SHA256 = 4, guestSHA("data") + _, err := h.service.Execute(t.Context(), p, stream.BytesInput([]byte("data")), nil) + if !errors.Is(err, ErrGuestAuthority) || h.transport.executed != 1 { + t.Fatalf("final drift crossed business boundary: %v executed=%d", err, h.transport.executed) + } + }) + } +} + +func TestGuestReadonlyRetirementDoesNotReenterEffectfulScan(t *testing.T) { + h, _, config, events := replacementHarness(t) + _, old := currentReplacementRecord(t, h) + if _, err := h.service.Execute(t.Context(), h.params("prepare", "replacement"), nil, nil); err != nil { + t.Fatal(err) + } + plan := retirementPlan(t, h) + if plan == nil { + t.Fatal("complete inventory did not produce a plan") + } + path := filepath.Join(h.service.store.directory, old.RecordID+".retired") + if err := os.Remove(path); err != nil { + t.Fatal(err) + } + config.absent = true + *events = nil + executed, retired := h.transport.executed, h.transport.retired + p := h.params("put", "plan") + p.Size, p.SHA256 = 4, guestSHA("data") + p.Target, p.Caller = string(plan.operation.Target), plan.config.Caller + effect := false + // Without coordinator admission, the final authority gate must refuse. A + // re-entered retirement scan would confirm and recreate the removed record + // before that gate, exposing the unsafe proof/consumption ordering. + _, err := h.service.run(t.Context(), p, plan.operation, plan.config, plan.key, strings.Repeat("a", 64), stream.BytesInput([]byte("data")), nil, &effect, plan) + if !errors.Is(err, ErrGuestAuthority) || effect || !plan.consumed || len(*events) != 0 || h.transport.executed != executed || h.transport.retired != retired { + t.Fatalf("proof re-entered retirement: %v events=%v", err, *events) + } + if _, err := os.Stat(path); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("read-only plan recreated retirement evidence: %v", err) + } +} + +func changePlanDocument(t *testing.T, h *guestHarness, current guestWorkerRecord, suffix string, drift func(*guestWorkerRecord)) { + t.Helper() + path := filepath.Join(h.service.store.directory, current.RecordID+suffix) + if suffix != ".retirement" { + if err := os.Remove(path); err != nil { + t.Fatal(err) + } + } + if drift == nil { + return + } + drift(¤t) + if err := h.service.store.write(t.Context(), current.RecordID, suffix, current); err != nil { + t.Fatal(err) + } +} + +func TestGuestReadonlyRetirementLimitAndCancellation(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + plan := retirementPlan(t, h) + ctx, cancel := context.WithCancel(t.Context()) + cancel() + if h.service.readonlyRetirementPlan(ctx, GuestOperationParams{Operation: "put"}, plan.operation, plan.config, plan.key) != nil { + t.Fatal("cancelled proof accepted") + } + _, current := currentReplacementRecord(t, h) + for i := range 128 { + previous := current + previous.RecordID = fmt.Sprintf("%064x", i+1) + previous.Identity.PID += i + 1 + previous.Identity.BirthTicks += int64(i + 1) + for _, suffix := range []string{".launch", ".worker", ".ready", ".retired"} { + if err := h.service.store.write(t.Context(), previous.RecordID, suffix, previous); err != nil { + t.Fatal(err) + } + } + } + if retirementPlan(t, h) != nil { + t.Fatal("129 matching original launches accepted") + } +} + +func TestGuestReadonlyRetirementPendingUsesOldAuthorityBarrier(t *testing.T) { + h, _, _, events := replacementHarness(t) + _, current := currentReplacementRecord(t, h) + if err := h.service.store.write(t.Context(), current.RecordID, ".retirement", current); err != nil { + t.Fatal(err) + } + h.config.calls, h.safety.calls = 0, 0 + h.config.change = func(c *GuestConfiguration, call int) { + if call >= 2 { + c.Generation = "changed" + } + } + p := h.params("put", "pending") + p.Size, p.SHA256 = 4, guestSHA("data") + _, err := h.service.Execute(t.Context(), p, stream.BytesInput([]byte("data")), nil) + if !errors.Is(err, ErrGuestAuthority) || len(*events) != 0 || h.safety.calls != 1 { + t.Fatalf("pending crossed early authority: %v events=%v safety=%d", err, *events, h.safety.calls) + } +} diff --git a/internal/app/guest_shutdown.go b/internal/app/guest_shutdown.go new file mode 100644 index 0000000..325be48 --- /dev/null +++ b/internal/app/guest_shutdown.go @@ -0,0 +1,91 @@ +package app + +import ( + "context" + "errors" + "maps" +) + +func (s *GuestService) beginGuestCall() error { + if s == nil { + return ErrGuestAuthority + } + s.mu.Lock() + defer s.mu.Unlock() + if s.closing { + return ErrGuestAuthority + } + s.running++ + return nil +} +func (s *GuestService) endGuestCall() { + s.mu.Lock() + defer s.mu.Unlock() + s.running-- + if s.closing && s.running == 0 { + close(s.drained) + } +} + +// Shutdown retires only workers created by this service. A closed channel is +// insufficient evidence of native absence; unresolved obligations remain durable. +func (s *GuestService) Shutdown(ctx context.Context) error { + if s == nil { + return ErrGuestAuthority + } + drained := s.closeGuestAdmission() + select { + case <-ctx.Done(): + return errors.Join(ErrGuestUncertain, ctx.Err()) + case <-drained: + } + select { + case <-ctx.Done(): + return errors.Join(ErrGuestUncertain, ctx.Err()) + case <-s.shutdownGate: + } + defer func() { s.shutdownGate <- struct{}{} }() + if err := ctx.Err(); err != nil { + return errors.Join(ErrGuestUncertain, err) + } + s.mu.Lock() + workers := maps.Clone(s.workers) + s.mu.Unlock() + var result error + for key, worker := range workers { + if err := ctx.Err(); err != nil { + return errors.Join(result, ErrGuestUncertain, err) + } + err := s.retireWorkerContext(ctx, key, worker, false) + result = errors.Join(result, err) + if err == nil { + s.mu.Lock() + if s.workers[key].RecordID == worker.RecordID { + delete(s.workers, key) + } + s.mu.Unlock() + } + } + if err := ctx.Err(); err != nil { + return errors.Join(result, ErrGuestUncertain, err) + } + result = errors.Join(result, s.reconcileShutdownObligations(ctx)) + s.mu.Lock() + s.shutdownDone = result == nil + s.mu.Unlock() + return result +} + +func (s *GuestService) closeGuestAdmission() <-chan struct{} { + s.mu.Lock() + if !s.closing { + s.closing = true + s.drained = make(chan struct{}) + if s.running == 0 { + close(s.drained) + } + } + drained := s.drained + s.mu.Unlock() + return drained +} diff --git a/internal/app/guest_shutdown_fixture_test.go b/internal/app/guest_shutdown_fixture_test.go new file mode 100644 index 0000000..c250317 --- /dev/null +++ b/internal/app/guest_shutdown_fixture_test.go @@ -0,0 +1,67 @@ +package app + +import ( + "context" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" +) + +const guestShutdownTestBudget = 30 * time.Second + +// Shutdown unit tests start from an already-admitted worker. Admission and +// receipt finalization have separate integration coverage; their fixed cleanup +// budget must not prevent the shutdown branch from being exercised under load. +func prepareGuestShutdownFixture(t *testing.T, h *guestHarness) { + t.Helper() + ctx, cancel := context.WithTimeout(t.Context(), guestShutdownTestBudget) + defer cancel() + target := domain.MachineRef(h.service.resolver.(guestTestResolver).resolution.Locator.String()) + id := guestDigest("shutdown-fixture:" + t.Name()) + key := guestPoolKey(target, h.actor, h.config.generation) + worker := guestWorkerRecord{RecordID: id, Target: target, Caller: h.actor.AuthenticatedCaller, Actor: h.actor.EffectiveActor, Generation: h.config.generation, Root: h.config.root} + if err := h.service.recordLaunch(ctx, id, worker); err != nil { + t.Fatal(err) + } + identity, err := h.transport.Prepare(ctx, target, key, worker.Generation) + if err != nil { + t.Fatal(err) + } + worker.Identity = identity + if err := h.service.store.write(ctx, id, ".worker", worker); err != nil { + t.Fatal(err) + } + if err := h.service.recordReady(ctx, id, worker); err != nil { + t.Fatal(err) + } + h.service.mu.Lock() + h.service.workers[key] = worker + h.service.mu.Unlock() +} + +func awaitGuestShutdownEntry(t *testing.T, entered <-chan struct{}) { + t.Helper() + select { + case <-entered: + case <-time.After(guestShutdownTestBudget): + t.Fatal("retirement verifier did not start within operation budget") + } +} +func awaitGuestShutdownResult(t *testing.T, result <-chan error) error { + t.Helper() + select { + case err := <-result: + return err + case <-time.After(guestShutdownTestBudget): + t.Fatal("shutdown did not return within operation budget") + return nil + } +} + +func guestShutdownContext(t *testing.T) context.Context { + t.Helper() + ctx, cancel := context.WithTimeout(t.Context(), guestShutdownTestBudget) + t.Cleanup(cancel) + return ctx +} diff --git a/internal/app/guest_shutdown_obligations.go b/internal/app/guest_shutdown_obligations.go new file mode 100644 index 0000000..9d388d0 --- /dev/null +++ b/internal/app/guest_shutdown_obligations.go @@ -0,0 +1,152 @@ +package app + +import ( + "context" + "errors" + "io" + "os" + "path/filepath" + "reflect" + "strings" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + "github.com/Horcag/agent-machine-control/internal/target" +) + +const maxGuestShutdownEntries = 4096 +const maxGuestShutdownObligations = 128 + +// Durable records are evidence of obligations, never channel ownership. Even a +// restarted service with no owned workers must resolve these before succeeding. +func (s *GuestService) reconcileShutdownObligations(ctx context.Context) error { + if s.store == nil { + return ErrGuestUncertain + } + records, err := s.shutdownObligationRecords(ctx) + if err != nil { + return errors.Join(ErrGuestUncertain, err) + } + var result error + for id, suffixes := range records { + if err := ctx.Err(); err != nil { + return errors.Join(result, ErrGuestUncertain, err) + } + result = errors.Join(result, s.reconcileShutdownObligation(ctx, id, suffixes)) + } + return result +} + +func (s *GuestService) shutdownObligationRecords(ctx context.Context) (map[string][]string, error) { + records := make(map[string][]string) + if _, err := os.Lstat(s.store.directory); errors.Is(err, os.ErrNotExist) { + return records, ctx.Err() + } else if err != nil { + return nil, err + } + if err := target.NewPrivatePathSecurity().ValidateDir(ctx, s.store.directory); err != nil { + return nil, err + } + root, err := os.OpenRoot(s.store.directory) + if err != nil { + return nil, err + } + defer root.Close() + directory, err := root.Open(".") + if err != nil { + return nil, err + } + defer directory.Close() + count := 0 + for { + entries, readErr := directory.ReadDir(128) + count += len(entries) + if count > maxGuestShutdownEntries { + return nil, ErrGuestUncertain + } + for _, entry := range entries { + if err := collectGuestObligationEntry(records, entry.Name()); err != nil { + return nil, err + } + } + if err := ctx.Err(); err != nil { + return nil, err + } + if readErr == io.EOF { + return records, nil + } + if readErr != nil { + return nil, readErr + } + } +} + +func collectGuestObligationEntry(records map[string][]string, name string) error { + suffix := filepath.Ext(name) + switch suffix { + case ".launch", ".worker", ".ready", ".retirement", ".retired": + default: + return nil + } + id := strings.TrimSuffix(name, suffix) + if !validLabDigest(id) { + return ErrGuestUncertain + } + records[id] = append(records[id], suffix) + if len(records) > maxGuestShutdownObligations { + return ErrGuestUncertain + } + return nil +} + +func (s *GuestService) reconcileShutdownObligation(ctx context.Context, id string, suffixes []string) error { + var launch guestWorkerRecord + if err := s.store.read(ctx, id, ".launch", &launch); err != nil || !validGuestShutdownLaunch(id, launch) { + return ErrGuestUncertain + } + identity := launch + found := validGuestWorker(launch.Identity) + if launch.Identity != (stream.WorkerIdentity{}) && !found { + return ErrGuestUncertain + } + for _, suffix := range suffixes { + if suffix == ".launch" { + continue + } + var record guestWorkerRecord + if err := s.store.read(ctx, id, suffix, &record); err != nil || !sameGuestLaunch(record, launch) || !validGuestWorker(record.Identity) { + return ErrGuestUncertain + } + if found && !reflect.DeepEqual(record, identity) { + return ErrGuestUncertain + } + identity = record + found = true + } + if !found { + return ErrGuestUncertain + } + if err := s.recordShutdownRetirementIntent(ctx, id, identity); err != nil { + return err + } + return s.confirmRetirement(ctx, id, identity) +} + +func validGuestShutdownLaunch(id string, launch guestWorkerRecord) bool { + _, err := domain.ParseMachineLocator(string(launch.Target)) + return launch.RecordID == id && err == nil && launch.Actor.Validate() == nil && launch.Caller.Validate() == nil && domain.ValidateBoundedString(launch.Generation, 1, 256, ErrGuestUncertain) == nil +} + +func (s *GuestService) recordShutdownRetirementIntent(ctx context.Context, id string, worker guestWorkerRecord) error { + err := s.store.write(ctx, id, ".retirement", worker) + if err == nil { + return nil + } + if errors.Is(err, os.ErrExist) { + var existing guestWorkerRecord + if readErr := s.store.read(ctx, id, ".retirement", &existing); readErr == nil && reflect.DeepEqual(existing, worker) { + return nil + } + } + return errors.Join(ErrGuestUncertain, err) +} diff --git a/internal/app/guest_shutdown_obligations_test.go b/internal/app/guest_shutdown_obligations_test.go new file mode 100644 index 0000000..b79f831 --- /dev/null +++ b/internal/app/guest_shutdown_obligations_test.go @@ -0,0 +1,189 @@ +package app + +import ( + "context" + "errors" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" +) + +func restartGuestService(h *guestHarness, config GuestConfigProvider) { + h.service = NewGuestService(h.service.session, h.service.resolver, config, h.transport, h.service.store) +} +func originalGuestEpoch(t *testing.T, h *guestHarness) guestWorkerRecord { + t.Helper() + for _, worker := range h.service.workers { + return worker + } + t.Fatal("missing fixture worker") + return guestWorkerRecord{} +} + +func TestGuestRestartShutdownPreservesLiveObligation(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + worker := originalGuestEpoch(t, h) + verifier := &guestVerifiedConfig{guestTestConfig: h.config} + restartGuestService(h, verifier) + if err := h.service.Shutdown(context.Background()); !errors.Is(err, ErrGuestUncertain) || h.service.shutdownDone { + t.Fatalf("live restart claimed shutdown: %v done=%v", err, h.service.shutdownDone) + } + if h.transport.retired != 0 || len(h.service.workers) != 0 { + t.Fatal("durable evidence was adopted as channel ownership") + } + var intent guestWorkerRecord + if err := h.service.store.read(context.Background(), worker.RecordID, ".retirement", &intent); err != nil || intent.Identity != worker.Identity { + t.Fatalf("original obligation not durable: %+v %v", intent, err) + } +} + +func TestGuestRestartShutdownVerifiesOriginalAbsence(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + worker := originalGuestEpoch(t, h) + verifier := &guestVerifiedConfig{guestTestConfig: h.config, absent: true} + h.transport.worker.PID++ + h.transport.worker.BirthTicks++ + restartGuestService(h, verifier) + if err := h.service.Shutdown(context.Background()); err != nil || !h.service.shutdownDone { + t.Fatalf("verified restart shutdown: %v", err) + } + if h.transport.retired != 0 || len(verifier.checked) != 1 || verifier.checked[0] != worker.Identity { + t.Fatalf("original identity replaced or retired: %+v", verifier.checked) + } + var retired guestWorkerRecord + if err := h.service.store.read(context.Background(), worker.RecordID, ".retired", &retired); err != nil || retired.Identity != worker.Identity { + t.Fatalf("absence not reconciled: %v", err) + } +} + +func TestGuestRestartShutdownFailsClosedOnDamagedRecords(t *testing.T) { + for _, damage := range []string{"corrupt-launch", "missing-launch", "missing-identity", "changed-ready-identity", "unknown-name"} { + t.Run(damage, func(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + worker := originalGuestEpoch(t, h) + if err := damageGuestObligation(h, worker, damage); err != nil { + t.Fatal(err) + } + restartGuestService(h, &guestVerifiedConfig{guestTestConfig: h.config, absent: true}) + if err := h.service.Shutdown(context.Background()); !errors.Is(err, ErrGuestUncertain) || h.service.shutdownDone { + t.Fatalf("damaged obligation accepted: %v", err) + } + if h.transport.retired != 0 { + t.Fatal("damaged evidence caused channel retirement") + } + }) + } +} + +func restartShutdownTest(t *testing.T) (*guestHarness, *guestBlockingVerifier, chan error, context.CancelFunc) { + t.Helper() + h := newGuestHarness(t) + h.prepare(t) + verifier := &guestBlockingVerifier{guestTestConfig: h.config, entered: make(chan struct{}), release: make(chan struct{})} + restartGuestService(h, verifier) + ctx, cancel := context.WithCancel(context.Background()) + first := make(chan error, 1) + go func() { first <- h.service.Shutdown(ctx) }() + select { + case <-verifier.entered: + case <-time.After(5 * time.Second): + cancel() + t.Fatal("original absence verification did not start") + } + return h, verifier, first, cancel +} +func TestGuestRestartShutdownCancellation(t *testing.T) { + h, verifier, first, cancel := restartShutdownTest(t) + cancel() + if err := <-first; !errors.Is(err, context.Canceled) || h.service.shutdownDone { + t.Fatalf("cancellation discarded: %v", err) + } + close(verifier.release) + if h.transport.retired != 0 { + t.Fatal("restart retired a foreign channel") + } +} +func TestGuestRestartShutdownConcurrency(t *testing.T) { + h, verifier, first, cancel := restartShutdownTest(t) + defer cancel() + second := make(chan error, 1) + go func() { second <- h.service.Shutdown(context.Background()) }() + close(verifier.release) + for _, result := range []chan error{first, second} { + if err := <-result; err != nil { + t.Fatal(err) + } + } + verifier.mu.Lock() + calls := verifier.calls + verifier.mu.Unlock() + if calls != 1 || h.transport.retired != 0 { + t.Fatalf("duplicate or foreign effects: confirms=%d retired=%d", calls, h.transport.retired) + } +} + +func TestGuestShutdownRescansAfterEarlierSuccess(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + verified := &guestVerifiedConfig{guestTestConfig: h.config, absent: true} + h.service.config = verified + if err := h.service.Shutdown(context.Background()); err != nil { + t.Fatal(err) + } + worker := guestWorkerRecord{RecordID: strings.Repeat("f", 64), Target: domain.MachineRef(h.service.resolver.(guestTestResolver).resolution.Locator.String()), Caller: "other-caller", Actor: "other-actor", Generation: "other-generation", Root: fixtureGuestRoot()} + if err := h.service.store.write(context.Background(), worker.RecordID, ".launch", worker); err != nil { + t.Fatal(err) + } + if err := h.service.Shutdown(context.Background()); !errors.Is(err, ErrGuestUncertain) || h.service.shutdownDone { + t.Fatalf("new obligation ignored: %v", err) + } + if h.transport.retired != 1 { + t.Fatal("a previously closed or foreign channel was retired again") + } +} + +func TestGuestShutdownObligationScanBounds(t *testing.T) { + records := map[string][]string{} + for i := range maxGuestShutdownObligations { + id := guestDigest(i) + if err := collectGuestObligationEntry(records, id+".launch"); err != nil { + t.Fatal(err) + } + } + if err := collectGuestObligationEntry(records, guestDigest("overflow")+".launch"); !errors.Is(err, ErrGuestUncertain) { + t.Fatal("unbounded obligation scan") + } +} + +func damageGuestObligation(h *guestHarness, worker guestWorkerRecord, damage string) error { + path := func(suffix string) string { return filepath.Join(h.service.store.directory, worker.RecordID+suffix) } + switch damage { + case "corrupt-launch": + return os.WriteFile(path(".launch"), []byte("{broken"), 0600) + case "missing-launch": + return os.Remove(path(".launch")) + case "missing-identity": + return errors.Join(os.Remove(path(".worker")), os.Remove(path(".ready"))) + case "changed-ready-identity": + var altered guestWorkerRecord + if err := h.service.store.read(context.Background(), worker.RecordID, ".ready", &altered); err != nil { + return err + } + altered.Identity.PID++ + if err := os.Remove(path(".ready")); err != nil { + return err + } + return h.service.store.write(context.Background(), worker.RecordID, ".ready", altered) + case "unknown-name": + return os.WriteFile(filepath.Join(h.service.store.directory, "foreign.launch"), []byte("{}"), 0600) + default: + return errors.New("unknown synthetic damage") + } +} diff --git a/internal/app/guest_shutdown_test.go b/internal/app/guest_shutdown_test.go new file mode 100644 index 0000000..0ae8c39 --- /dev/null +++ b/internal/app/guest_shutdown_test.go @@ -0,0 +1,146 @@ +package app + +import ( + "context" + "errors" + "os" + "path/filepath" + "sync" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +type guestBlockingVerifier struct { + *guestTestConfig + entered chan struct{} + release chan struct{} + mu sync.Mutex + calls int + ignoreCancellation bool + observedDeadline time.Time + observedWitness any +} + +func (c *guestBlockingVerifier) ConfirmGuestRetirement(ctx context.Context, _ domain.MachineRef, _ domain.ActorID, _ string, _ stream.WorkerIdentity) (bool, error) { + c.mu.Lock() + c.calls++ + c.observedDeadline, _ = ctx.Deadline() + c.observedWitness = ctx.Value(guestShutdownWitnessKey{}) + if c.calls == 1 { + close(c.entered) + } + c.mu.Unlock() + if c.ignoreCancellation { + <-c.release + return true, nil + } + select { + case <-ctx.Done(): + return false, ctx.Err() + case <-c.release: + return true, nil + } +} + +func TestGuestShutdownRetirementHonorsCallerContext(t *testing.T) { + h := newGuestHarness(t) + verifier := &guestBlockingVerifier{guestTestConfig: h.config, entered: make(chan struct{}), release: make(chan struct{})} + h.service.config = verifier + prepareGuestShutdownFixture(t, h) + witness := new(int) + parent := context.WithValue(t.Context(), guestShutdownWitnessKey{}, witness) + ctx, cancel := context.WithTimeout(parent, guestShutdownTestBudget) + defer cancel() + requestedDeadline, _ := ctx.Deadline() + result := make(chan error, 1) + go func() { result <- h.service.Shutdown(ctx) }() + awaitGuestShutdownEntry(t, verifier.entered) + verifier.mu.Lock() + observedDeadline := verifier.observedDeadline + observedWitness := verifier.observedWitness + verifier.mu.Unlock() + cancel() + if err := awaitGuestShutdownResult(t, result); !errors.Is(err, context.Canceled) || !errors.Is(err, ErrGuestUncertain) { + t.Fatalf("caller cancellation lost: %v", err) + } + if !observedDeadline.Equal(requestedDeadline) || observedWitness != witness { + t.Fatalf("caller context detached: deadline=%v witness=%v", observedDeadline, observedWitness) + } + worker := originalGuestEpoch(t, h) + var pending guestWorkerRecord + if err := h.service.store.read(t.Context(), worker.RecordID, ".retirement", &pending); err != nil || pending.Identity != worker.Identity { + t.Fatalf("pending original identity lost: %v", err) + } + close(verifier.release) + if err := h.service.Shutdown(guestShutdownContext(t)); err != nil { + t.Fatalf("pending retirement could not be confirmed: %v", err) + } +} + +func TestGuestConcurrentShutdownHasOneRetirementEffect(t *testing.T) { + h := newGuestHarness(t) + verifier := &guestBlockingVerifier{guestTestConfig: h.config, entered: make(chan struct{}), release: make(chan struct{})} + h.service.config = verifier + prepareGuestShutdownFixture(t, h) + first := make(chan error, 1) + second := make(chan error, 1) + go func() { first <- h.service.Shutdown(guestShutdownContext(t)) }() + awaitGuestShutdownEntry(t, verifier.entered) + go func() { second <- h.service.Shutdown(guestShutdownContext(t)) }() + close(verifier.release) + for _, result := range []chan error{first, second} { + if err := awaitGuestShutdownResult(t, result); err != nil { + t.Fatalf("identical shutdown failed: %v", err) + } + } + verifier.mu.Lock() + calls := verifier.calls + verifier.mu.Unlock() + if calls != 1 || h.transport.retired != 1 { + t.Fatalf("duplicate retirement effects: verify=%d close=%d", calls, h.transport.retired) + } +} + +func TestGuestShutdownNeverReportsSuccessAfterDeadline(t *testing.T) { + h := newGuestHarness(t) + verifier := &guestBlockingVerifier{guestTestConfig: h.config, entered: make(chan struct{}), release: make(chan struct{}), ignoreCancellation: true} + h.service.config = verifier + prepareGuestShutdownFixture(t, h) + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + result := make(chan error, 1) + go func() { result <- h.service.Shutdown(ctx) }() + awaitGuestShutdownEntry(t, verifier.entered) + cancel() + close(verifier.release) + if err := awaitGuestShutdownResult(t, result); !errors.Is(err, context.Canceled) { + t.Fatalf("expired shutdown reported success: %v", err) + } +} + +func TestGuestShutdownDoesNotCloseBeforeDurableIntent(t *testing.T) { + h := newGuestHarness(t) + prepareGuestShutdownFixture(t, h) + original := h.service.store.directory + h.service.store.directory = filepath.Join(t.TempDir(), "blocked") + if err := os.WriteFile(h.service.store.directory, nil, 0600); err != nil { + t.Fatal(err) + } + if err := h.service.Shutdown(guestShutdownContext(t)); !errors.Is(err, ErrGuestUncertain) { + t.Fatalf("missing intent accepted: %v", err) + } + if h.transport.retired != 0 { + t.Fatal("worker closed without durable retirement intent") + } + h.service.store.directory = original + verified := &guestVerifiedConfig{guestTestConfig: h.config, absent: true} + h.service.config = verified + if err := h.service.Shutdown(guestShutdownContext(t)); err != nil { + t.Fatal(err) + } +} + +type guestShutdownWitnessKey struct{} diff --git a/internal/app/guest_spool.go b/internal/app/guest_spool.go new file mode 100644 index 0000000..554ae67 --- /dev/null +++ b/internal/app/guest_spool.go @@ -0,0 +1,185 @@ +package app + +import ( + "context" + "crypto/rand" + "crypto/sha256" + "encoding/hex" + "errors" + "hash" + "io" + "os" + "strconv" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + "github.com/Horcag/agent-machine-control/internal/statedir" + "github.com/Horcag/agent-machine-control/internal/target" +) + +// MaxGuestSpoolBytes caps both one output and aggregate concurrent private +// staging. The operation deadline is its expiry; every return removes its inode. +const MaxGuestSpoolBytes int64 = 1 << 30 + +type guestSpool struct { + service *GuestService + root *os.Root + file *os.File + info os.FileInfo + name string + limit int64 + size int64 + digest hash.Hash +} + +func (s *GuestService) spool(ctx context.Context, p GuestOperationParams) (*guestSpool, error) { + if p.MaxOutput <= 0 || p.MaxOutput > MaxGuestSpoolBytes { + return nil, domain.ErrNonCanonicalParameter + } + s.mu.Lock() + if s.spoolBytes+p.MaxOutput > MaxGuestSpoolBytes { + s.mu.Unlock() + return nil, ErrGuestAuthority + } + s.spoolBytes += p.MaxOutput + s.mu.Unlock() + spool := &guestSpool{service: s, limit: p.MaxOutput, digest: sha256.New()} + fail := func(err error) (*guestSpool, error) { return nil, errors.Join(err, spool.cleanup()) } + if err := statedir.EnsurePrivateDirectory(s.store.directory); err != nil { + return fail(err) + } + security := target.NewPrivatePathSecurity() + if err := security.ValidateDir(ctx, s.store.directory); err != nil { + return fail(err) + } + root, err := os.OpenRoot(s.store.directory) + if err != nil { + return fail(err) + } + spool.root = root + var nonce [16]byte + _, err = rand.Read(nonce[:]) + id := hex.EncodeToString(nonce[:]) + if err != nil { + return fail(err) + } + spool.name = "guest-spool-" + string(id) + "-" + strconv.FormatInt(p.Deadline.UnixNano(), 10) + spool.file, spool.info, err = reserveProtectedLabFile(ctx, root, s.store.directory, spool.name, security) + if err != nil { + return fail(err) + } + return spool, nil +} +func (s *guestSpool) Write(p []byte) (int, error) { return s.WriteContext(context.Background(), p) } +func (s *guestSpool) WriteContext(ctx context.Context, p []byte) (int, error) { + if err := ctx.Err(); err != nil { + return 0, err + } + if int64(len(p)) > s.limit-s.size { + return 0, domain.ErrNonCanonicalParameter + } + n, err := stream.OwnedOutput(s.file).WriteContext(ctx, p) + if n > 0 { + _, _ = s.digest.Write(p[:n]) + s.size += int64(n) + } + if err == nil && n != len(p) { + err = io.ErrShortWrite + } + return n, err +} +func (s *guestSpool) matches(out GuestOperationResult) bool { + return s.size == out.Size && hex.EncodeToString(s.digest.Sum(nil)) == out.SHA256 +} +func (s *guestSpool) publish(ctx context.Context, output stream.ContextWriter) error { + if err := s.file.Sync(); err != nil { + return err + } + if err := s.file.Close(); err != nil { + return err + } + file, err := s.root.Open(s.name) + if err != nil { + return err + } + defer file.Close() + info, err := file.Stat() + if err != nil || !os.SameFile(info, s.info) || info.Size() != s.size { + return ErrGuestOwnership + } + buffer := make([]byte, stream.BufferSize) + for { + n, readErr := file.Read(buffer) + if n > 0 { + written, writeErr := output.WriteContext(ctx, buffer[:n]) + if writeErr != nil { + return writeErr + } + if written != n { + return io.ErrShortWrite + } + } + if readErr == io.EOF { + return nil + } + if readErr != nil { + return readErr + } + if err := ctx.Err(); err != nil { + return err + } + } +} +func (s *guestSpool) cleanup() error { + s.service.mu.Lock() + s.service.spoolBytes -= s.limit + s.service.mu.Unlock() + if s.file != nil { + _ = s.file.Close() + } + if s.root == nil { + return nil + } + defer s.root.Close() + if s.info == nil { + return nil + } + current, err := s.root.Lstat(s.name) + if err != nil { + return err + } + if !os.SameFile(current, s.info) { + return ErrGuestOwnership + } + return s.root.Remove(s.name) +} + +type guestCheckedInput struct { + source stream.ContextReader + size int64 + count int64 + expected string + digest hash.Hash + eof bool +} + +func (r *guestCheckedInput) Read(p []byte) (int, error) { + return r.ReadContext(context.Background(), p) +} +func (r *guestCheckedInput) ReadContext(ctx context.Context, p []byte) (int, error) { + n, err := r.source.ReadContext(ctx, p) + if n < 0 || n > len(p) || int64(n) > r.size-r.count { + return 0, stream.ErrIntegrity + } + if n > 0 { + _, _ = r.digest.Write(p[:n]) + r.count += int64(n) + } + if err == io.EOF { + r.eof = true + } + return n, err +} +func (r *guestCheckedInput) valid() bool { + return r.eof && r.count == r.size && hex.EncodeToString(r.digest.Sum(nil)) == r.expected +} diff --git a/internal/app/guest_startup_diagnostics_test.go b/internal/app/guest_startup_diagnostics_test.go new file mode 100644 index 0000000..0fa2613 --- /dev/null +++ b/internal/app/guest_startup_diagnostics_test.go @@ -0,0 +1,99 @@ +package app + +import ( + "context" + "errors" + "fmt" + "os" + "path/filepath" + "slices" + "strings" + "testing" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +type startupFailureTransport struct { + *guestTestTransport + err error +} + +func (f *startupFailureTransport) Prepare(context.Context, domain.MachineRef, string, string) (stream.WorkerIdentity, error) { + f.prepared++ + return stream.WorkerIdentity{}, f.err +} + +func TestGuestPrepareStartupDiagnosticPreservesSanitizedReceipt(t *testing.T) { + h := newGuestHarness(t) + failure := &stream.StartupFailure{Stage: "hello", Category: "eof", OpenMillis: 3, SourceWriteMillis: 4, HelloMillis: 5, ExitObserved: true, ExitCode: 1, RemoteStage: "before_compile"} + h.service.transport = &startupFailureTransport{guestTestTransport: h.transport, err: fmt.Errorf("private path and credentials: %w", failure)} + out, err := h.service.Execute(t.Context(), h.params("prepare", "startup-failed"), nil, nil) + copied, ok := errors.AsType[*stream.StartupFailure](err) + if !ok || copied == failure || *copied != *failure || !errors.Is(err, ErrGuestUncertain) || strings.Contains(err.Error(), "private") || h.transport.executed != 0 { + t.Fatalf("startup diagnostic lost/leaked: %v", err) + } + if out.Receipt == nil || out.Receipt.Outcome.Status != domain.OutcomeFailed || !slices.Equal(out.Receipt.EvidenceRefs, []string{"guest-operation-dispatched", failure.Error()}) || h.transport.retired != 1 { + t.Fatalf("startup receipt/close obligation lost: %+v", out) + } +} + +func TestGuestPrepareStartupDiagnosticKeepsUnknownLaunchAndBlocksReplay(t *testing.T) { + h := newGuestHarness(t) + h.service.transport = &startupFailureTransport{guestTestTransport: h.transport, err: &stream.StartupFailure{Stage: "hello", Category: "eof", ExitObserved: true, ExitCode: 1}} + _, err := h.service.Execute(t.Context(), h.params("prepare", "startup-failed"), nil, nil) + if !errors.Is(err, ErrGuestUncertain) || h.transport.executed != 0 { + t.Fatal("unknown startup crossed effect boundary", err) + } + records := startupObligationRecords(t, h) + if len(records[".launch"]) != 1 || len(records[".retirement"]) != 1 { + t.Fatalf("expected one unknown launch and retirement: %+v", records) + } + launch, pending := records[".launch"][0], records[".retirement"][0] + if launch != pending || pending.Identity != (stream.WorkerIdentity{}) { + t.Fatalf("unknown obligation lost: launch=%+v pending=%+v", launch, pending) + } + _, err = h.service.Execute(t.Context(), h.params("prepare", "startup-new-attempt"), nil, nil) + if !errors.Is(err, ErrGuestUncertain) || h.transport.prepared != 1 { + t.Fatal("unknown startup replayed", err) + } +} + +// Read the durable obligation inventory through the typed original store. +// Unknown startup must have no admitted-ready or completed-retirement document. +func startupObligationRecords(t *testing.T, h *guestHarness) map[string][]guestWorkerRecord { + t.Helper() + entries, err := os.ReadDir(h.service.store.directory) + if err != nil { + t.Fatal(err) + } + records := make(map[string][]guestWorkerRecord) + for _, entry := range entries { + suffix := filepath.Ext(entry.Name()) + switch suffix { + case ".ready", ".retired": + t.Fatal("unknown startup falsely admitted/retired") + case ".launch", ".retirement": + id, _ := strings.CutSuffix(entry.Name(), suffix) + var record guestWorkerRecord + if err := h.service.store.read(t.Context(), id, suffix, &record); err != nil { + t.Fatal(err) + } + records[suffix] = append(records[suffix], record) + } + } + return records +} + +func TestGuestStartupDiagnosticRejectsUntrustedFields(t *testing.T) { + for _, failure := range []*stream.StartupFailure{ + {Stage: "private", Category: "eof"}, {Stage: "hello", Category: "private"}, + {Stage: "hello", Category: "eof", RemoteStage: "private-path"}, + {Stage: "hello", Category: "eof", HelloMillis: -1}, {Stage: "hello", Category: "eof", ExitObserved: true, ExitCode: -1}, + } { + err := safeGuestTransportError(fmt.Errorf("private: %w", failure)) + if err != ErrGuestUncertain || guestFailureEvidence(err) != "" { + t.Fatal("invalid startup leaked", err) + } + } +} diff --git a/internal/app/guest_store.go b/internal/app/guest_store.go new file mode 100644 index 0000000..f2a7ef8 --- /dev/null +++ b/internal/app/guest_store.go @@ -0,0 +1,112 @@ +package app + +import ( + "context" + "encoding/json" + "errors" + "io" + "os" + "path/filepath" + + "github.com/Horcag/agent-machine-control/internal/statedir" + "github.com/Horcag/agent-machine-control/internal/target" +) + +const maxGuestDocument = 256 * 1024 + +// GuestStore keeps immutable, private worker and ownership records. Its directory +// must be dedicated server state, never caller-selected storage. +type GuestStore struct{ directory string } + +func NewGuestStore(directory string) *GuestStore { return &GuestStore{directory: directory} } + +func (s *GuestStore) write(ctx context.Context, id, suffix string, value any) error { + if s == nil || !validLabDigest(id) { + return ErrGuestOwnership + } + data, err := json.Marshal(value) + if err != nil || len(data) > maxGuestDocument { + return ErrGuestOwnership + } + if err := statedir.EnsurePrivateDirectory(s.directory); err != nil { + return err + } + security := target.NewPrivatePathSecurity() + if err := security.ValidateDir(ctx, s.directory); err != nil { + return err + } + root, err := os.OpenRoot(s.directory) + if err != nil { + return err + } + defer root.Close() + name := id + suffix + file, info, err := reserveProtectedLabFile(ctx, root, s.directory, name, security) + if err != nil { + return err + } + committed := false + defer func() { + _ = file.Close() + if !committed { + removeLabReservation(root, name, info) + } + }() + if err := ctx.Err(); err != nil { + return err + } + if _, err = file.Write(data); err != nil { + return err + } + if err = file.Sync(); err != nil { + return err + } + if err = file.Close(); err != nil { + return err + } + committed = true + return statedir.SyncDir(s.directory) +} + +func (s *GuestStore) read(ctx context.Context, id, suffix string, out any) error { + if s == nil || !validLabDigest(id) { + return ErrGuestOwnership + } + security := target.NewPrivatePathSecurity() + if err := security.ValidateDir(ctx, s.directory); err != nil { + return err + } + root, err := os.OpenRoot(s.directory) + if err != nil { + return err + } + defer root.Close() + name := id + suffix + info, err := root.Lstat(name) + if err != nil { + return err + } + if !info.Mode().IsRegular() || info.Size() > maxGuestDocument { + return ErrGuestOwnership + } + if err = security.ValidateFile(ctx, filepath.Join(s.directory, name)); err != nil { + return err + } + file, err := root.Open(name) + if err != nil { + return err + } + defer file.Close() + opened, err := file.Stat() + if err != nil || !os.SameFile(info, opened) { + return ErrGuestOwnership + } + data, err := io.ReadAll(io.LimitReader(file, maxGuestDocument+1)) + if err != nil || len(data) > maxGuestDocument { + return ErrGuestOwnership + } + if err = decodeLabDocument(data, out); err != nil { + return errors.Join(ErrGuestOwnership, err) + } + return ctx.Err() +} diff --git a/internal/app/guest_test.go b/internal/app/guest_test.go new file mode 100644 index 0000000..56898a1 --- /dev/null +++ b/internal/app/guest_test.go @@ -0,0 +1,478 @@ +package app + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "io" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/approval" + "github.com/Horcag/agent-machine-control/internal/audit" + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + "github.com/Horcag/agent-machine-control/internal/lease" + "github.com/Horcag/agent-machine-control/internal/policy" + "github.com/Horcag/agent-machine-control/internal/receipt" + "github.com/Horcag/agent-machine-control/internal/sessions" + "github.com/Horcag/agent-machine-control/internal/statedir" +) + +type guestTestResolver struct{ resolution TargetResolution } + +func (r guestTestResolver) ResolveTarget(context.Context, string) (TargetResolution, error) { + return r.resolution, nil +} + +type guestTestConfig struct { + root stream.RootIdentity + generation string + calls int + change func(*GuestConfiguration, int) +} + +func (c *guestTestConfig) Current(_ context.Context, _ domain.MachineRef, caller domain.ActorContext) (GuestConfiguration, error) { + c.calls++ + out := GuestConfiguration{Generation: c.generation, Caller: caller.Clone(), WorkspaceIdentity: c.root} + if c.change != nil { + c.change(&out, c.calls) + } + return out, nil +} + +type guestTestSafety struct { + destructive bool + classes []domain.OperationClass + calls int +} + +func (s *guestTestSafety) ResolveSafety(context.Context, domain.MachineRef) (SafetyResolution, error) { + s.calls++ + if len(s.classes) > 0 { + index := s.calls - 1 + if index >= len(s.classes) { + index = len(s.classes) - 1 + } + if s.classes[index] != domain.ClassReversibleMutation { + return SafetyResolution{Classification: s.classes[index]}, nil + } + } + if s.destructive { + return SafetyResolution{Classification: domain.ClassDestructivePrivileged}, nil + } + return SafetyResolution{Classification: domain.ClassReversibleMutation, Contained: true, RollbackRef: "fixture-checkpoint", RollbackState: policy.RollbackState{Available: true, Verified: true, CheckpointID: "fixture-checkpoint"}}, nil +} + +type guestTestTransport struct { + store *GuestStore + worker stream.WorkerIdentity + prepared, executed, retired int + prepareHook func() + executeHook func(context.Context, stream.Request, io.Reader, io.Writer) (stream.Result, error) + retireErr error + fail bool + alter func(*stream.Result) + files map[string]stream.FileIdentity +} + +func (f *guestTestTransport) Prepare(context.Context, domain.MachineRef, string, string) (stream.WorkerIdentity, error) { + f.prepared++ + if f.prepareHook != nil { + f.prepareHook() + } + return f.worker, nil +} +func (f *guestTestTransport) Retire(domain.MachineRef, string, string) error { + f.retired++ + return f.retireErr +} +func (f *guestTestTransport) Execute(ctx context.Context, _ domain.MachineRef, _ string, _ string, request stream.Request, input io.Reader, output io.Writer) (stream.Result, error) { + f.executed++ + if f.executeHook != nil { + return f.executeHook(ctx, request, input, output) + } + entries, err := os.ReadDir(f.store.directory) + if err != nil { + return stream.Result{}, err + } + found := false + for _, entry := range entries { + if strings.HasSuffix(entry.Name(), ".worker") { + var record guestWorkerRecord + if err := f.store.read(ctx, strings.TrimSuffix(entry.Name(), ".worker"), ".worker", &record); err != nil { + return stream.Result{}, err + } + if record.Identity == f.worker { + found = true + } + } + } + if !found { + return stream.Result{}, errors.New("worker was not durably recorded before execution") + } + if f.fail { + return stream.Result{}, errors.New("lost result") + } + return f.respond(request, input, output) +} +func (f *guestTestTransport) respond(request stream.Request, input io.Reader, output io.Writer) (stream.Result, error) { + result := stream.Result{ID: request.ID, Status: "ok", Worker: f.worker, SHA256: guestDigest("")} + switch request.Operation { + case "observe": + root := fixtureGuestRoot() + result.Root = &root + case "put": + data, err := io.ReadAll(input) + if err != nil || int64(len(data)) != request.Size { + return result, errors.New("input size") + } + root := *request.ExpectedRoot + file := stream.FileIdentity{Path: request.Path, Volume: root.Volume, FileID: strings.Repeat("b", 32), CreationTime: 200, Owner: root.Owner, SDDLHash: root.SDDLHash, Size: request.Size, SHA256: request.SHA256, RootPath: root.Path, RootVolume: root.Volume, RootFileID: root.FileID, RootCreationTime: root.CreationTime, RootOwner: root.Owner, RootSDDLHash: root.SDDLHash} + f.files[request.Path] = file + result.Files = []stream.FileIdentity{file} + case "get": + file := f.files[request.Path] + result.Files = []stream.FileIdentity{file} + result.Size = file.Size + result.SHA256 = file.SHA256 + _, _ = output.Write([]byte("data")) + case "exec": + _, _ = io.Copy(io.Discard, input) + result.Size = 2 + result.SHA256 = guestSHA("ok") + _, _ = output.Write([]byte("ok")) + case "remove-owned": + for _, file := range request.Owned { + result.Removed = append(result.Removed, stream.Removal{Path: file.Path, Status: "deleted", Absent: true}) + } + } + if f.alter != nil { + f.alter(&result) + } + return result, nil +} + +func fixtureGuestRoot() stream.RootIdentity { + return stream.RootIdentity{Path: `C:\AMC\fixture`, Volume: 7, FileID: strings.Repeat("a", 32), CreationTime: 100, Owner: "S-1-5-21-fixture", SDDLHash: strings.Repeat("d", 64)} +} + +type guestHarness struct { + service *GuestService + transport *guestTestTransport + config *guestTestConfig + safety *guestTestSafety + actor domain.ActorContext +} + +func newGuestHarness(t *testing.T) *guestHarness { + t.Helper() + sd, err := statedir.Resolve(filepath.Join(t.TempDir(), "state")) + if err != nil { + t.Fatal(err) + } + if err = sd.EnsureDirs(); err != nil { + t.Fatal(err) + } + safety := &guestTestSafety{} + coordinator := NewSessionService(nil, safety, lease.NewManager(sd.LeasesDir()), audit.NewStore(sd.AuditDir()), receipt.NewStore(sd.ReceiptsDir()), approval.NewStore(sd.ApprovalsDir()), WithSessionMutationJournal(sessions.NewMutationJournal(sd.SessionsDir()))) + targetID := "c4a523d4-6b99-4d62-a5e2-4752c0f20001" + resolver := guestTestResolver{TargetResolution{Locator: domain.MachineLocator{HostID: domain.LocalHostID, VMID: targetID}, ProviderVMID: targetID}} + config := &guestTestConfig{root: fixtureGuestRoot(), generation: "fixture-generation"} + store := NewGuestStore(filepath.Join(sd.Root(), "guest")) + transport := &guestTestTransport{store: store, worker: stream.WorkerIdentity{PID: 400, BirthTicks: 133900000000000001, Image: `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`, SID: "S-1-5-21-fixture"}, files: map[string]stream.FileIdentity{}} + scopes := domain.NewScopeSet(domain.ScopeSessionOpen, domain.ScopeSessionRead, domain.ScopeSessionWrite, domain.ScopeEvidenceCapture) + actor, err := domain.NewActorContext("fixture-actor", "fixture-actor", scopes, scopes) + if err != nil { + t.Fatal(err) + } + return &guestHarness{service: NewGuestService(coordinator, resolver, config, transport, store), transport: transport, config: config, safety: safety, actor: actor} +} +func (h *guestHarness) params(operation, key string) GuestOperationParams { + return GuestOperationParams{Target: "fixture-machine", Caller: h.actor, Operation: operation, Reason: "synthetic guest test", IdempotencyKey: key, Deadline: time.Now().UTC().Add(time.Minute)} +} +func (h *guestHarness) prepare(t *testing.T) { + t.Helper() + out, err := h.service.Execute(context.Background(), h.params("prepare", "prepare"), nil, nil) + if err != nil || out.Receipt == nil { + t.Fatalf("prepare: %+v %v", out, err) + } +} +func (h *guestHarness) put(t *testing.T, key string) GuestArtifact { + t.Helper() + p := h.params("put", key) + p.Size = 4 + p.SHA256 = guestSHA("data") + out, err := h.service.Execute(context.Background(), p, stream.BytesInput([]byte("data")), nil) + if err != nil || len(out.Artifacts) != 1 { + t.Fatalf("put: %+v %v", out, err) + } + return out.Artifacts[0] +} + +func TestGuestPrepareDurableWorkerBeforeExecute(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + if h.transport.prepared != 1 || h.transport.executed != 1 { + t.Fatal("whole prepare did not execute exactly once") + } +} +func TestGuestRequiresExplicitPreparation(t *testing.T) { + h := newGuestHarness(t) + p := h.params("exec", "not-prepared") + p.Size = 1 + p.SHA256 = guestSHA("x") + p.MaxOutput = 10 + _, err := h.service.Execute(context.Background(), p, stream.BytesInput([]byte("x")), stream.BufferOutput(new(bytes.Buffer))) + if !errors.Is(err, ErrGuestNotPrepared) || h.transport.executed != 0 { + t.Fatalf("%v executions %d", err, h.transport.executed) + } +} +func TestGuestRestartDoesNotRestoreWorkerAuthority(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + h.service = NewGuestService(h.service.session, h.service.resolver, h.config, h.transport, h.service.store) + p := h.params("exec", "restart") + p.Size = 1 + p.SHA256 = guestSHA("x") + p.MaxOutput = 10 + _, err := h.service.Execute(context.Background(), p, stream.BytesInput([]byte("x")), stream.BufferOutput(new(bytes.Buffer))) + if !errors.Is(err, ErrGuestUncertain) || h.transport.executed != 1 { + t.Fatalf("%v executions %d", err, h.transport.executed) + } +} +func TestGuestPersistentHelloFailureBlocksExecute(t *testing.T) { + h := newGuestHarness(t) + h.transport.prepareHook = func() { + entries, err := os.ReadDir(h.service.store.directory) + if err != nil { + t.Fatal(err) + } + for _, entry := range entries { + if !strings.HasSuffix(entry.Name(), ".launch") { + continue + } + var launch guestWorkerRecord + if err := h.service.store.read(context.Background(), strings.TrimSuffix(entry.Name(), ".launch"), ".launch", &launch); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(h.service.store.directory, launch.RecordID+".worker"), nil, 0600); err != nil { + t.Fatal(err) + } + } + } + _, err := h.service.Execute(context.Background(), h.params("prepare", "prepare"), nil, nil) + if err == nil || h.transport.executed != 0 || h.transport.retired != 1 { + t.Fatalf("%v executions %d retired %d", err, h.transport.executed, h.transport.retired) + } +} +func TestGuestWholePutIdempotencyAndConflict(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + p := h.params("put", "put") + p.Size = 4 + p.SHA256 = guestSHA("data") + first, err := h.service.Execute(context.Background(), p, stream.BytesInput([]byte("data")), nil) + if err != nil { + t.Fatal(err) + } + again, err := h.service.Execute(context.Background(), p, stream.BytesInput([]byte("data")), nil) + if err != nil || !again.CachedReceipt || again.Artifacts[0] != first.Artifacts[0] || h.transport.executed != 2 { + t.Fatalf("retry %+v %v", again, err) + } + p.Size = 5 + _, err = h.service.Execute(context.Background(), p, stream.BytesInput([]byte("other")), nil) + if err == nil || h.transport.executed != 2 { + t.Fatalf("collision %v", err) + } +} +func TestGuestUncertainOperationNeverReplays(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + h.transport.fail = true + p := h.params("exec", "uncertain") + p.Size = 1 + p.SHA256 = guestSHA("x") + p.MaxOutput = 10 + _, err := h.service.Execute(context.Background(), p, stream.BytesInput([]byte("x")), stream.BufferOutput(new(bytes.Buffer))) + if !errors.Is(err, ErrGuestUncertain) { + t.Fatal(err) + } + h.transport.fail = false + _, err = h.service.Execute(context.Background(), p, stream.BytesInput([]byte("x")), stream.BufferOutput(new(bytes.Buffer))) + if !errors.Is(err, ErrGuestUncertain) || h.transport.executed != 2 { + t.Fatalf("uncertain replay %v executions %d", err, h.transport.executed) + } +} +func TestGuestExpiredDeadlineAndPolicyFailClosed(t *testing.T) { + for _, expired := range []bool{true, false} { + h := newGuestHarness(t) + p := h.params("prepare", "deny") + if expired { + p.Deadline = time.Now().Add(-time.Second) + } else { + h.safety.destructive = true + } + _, err := h.service.Execute(context.Background(), p, nil, nil) + if err == nil || h.transport.prepared != 0 || h.transport.executed != 0 { + t.Fatalf("expired=%v %v", expired, err) + } + } +} +func TestGuestFreshAuthorityBeforePrepare(t *testing.T) { + for _, change := range []string{"actor", "generation", "root"} { + t.Run(change, func(t *testing.T) { + h := newGuestHarness(t) + h.config.change = func(c *GuestConfiguration, call int) { + if call < 2 { + return + } + switch change { + case "actor": + delete(c.Caller.EffectivePermissions, domain.ScopeSessionOpen) + case "generation": + c.Generation = "replaced" + case "root": + c.WorkspaceIdentity.FileID = strings.Repeat("e", 32) + } + } + _, err := h.service.Execute(context.Background(), h.params("prepare", "fresh"), nil, nil) + if !errors.Is(err, ErrGuestAuthority) || h.transport.prepared != 0 { + t.Fatalf("%v prepared %d", err, h.transport.prepared) + } + }) + } +} +func TestGuestFreshAuthorityAfterPersistentHello(t *testing.T) { + h := newGuestHarness(t) + h.config.change = func(c *GuestConfiguration, call int) { + if call >= 3 { + c.Generation = "replaced" + } + } + _, err := h.service.Execute(context.Background(), h.params("prepare", "fresh"), nil, nil) + if !errors.Is(err, ErrGuestAuthority) || h.transport.executed != 0 { + t.Fatalf("%v executions %d", err, h.transport.executed) + } +} +func TestGuestActorAndGenerationCannotUseWorker(t *testing.T) { + for _, change := range []string{"actor", "generation"} { + h := newGuestHarness(t) + h.prepare(t) + p := h.params("exec", "isolation") + if change == "actor" { + p.Caller.AuthenticatedCaller = "other" + p.Caller.EffectiveActor = "other" + } else { + h.config.generation = "new" + } + p.Size = 1 + p.SHA256 = guestSHA("x") + p.MaxOutput = 10 + _, err := h.service.Execute(context.Background(), p, stream.BytesInput([]byte("x")), stream.BufferOutput(new(bytes.Buffer))) + if (!errors.Is(err, ErrGuestNotPrepared) && !errors.Is(err, ErrGuestUncertain)) || h.transport.executed != 1 { + t.Fatalf("%s: %v", change, err) + } + } +} +func TestGuestOwnedDownloadAndRemoval(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + artifact := h.put(t, "put") + p := h.params("get", "get") + p.ArtifactIDs = []string{artifact.ID} + p.MaxOutput = 10 + var output bytes.Buffer + out, err := h.service.Execute(context.Background(), p, nil, stream.BufferOutput(&output)) + if err != nil || out.SHA256 != artifact.SHA256 || output.String() != "data" { + t.Fatalf("get %+v %v", out, err) + } + p = h.params("remove-owned", "remove") + p.ArtifactIDs = []string{artifact.ID} + out, err = h.service.Execute(context.Background(), p, nil, nil) + if err != nil || len(out.Removed) != 1 || !out.Removed[0].Attempted || !out.Removed[0].Accepted || !out.Removed[0].Absent { + t.Fatalf("remove %+v %v", out, err) + } +} +func TestGuestUnknownArtifactPreserved(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + p := h.params("remove-owned", "unknown") + p.ArtifactIDs = []string{strings.Repeat("f", 32)} + _, err := h.service.Execute(context.Background(), p, nil, nil) + if !errors.Is(err, ErrGuestOwnership) || h.transport.executed != 1 { + t.Fatalf("%v executions %d", err, h.transport.executed) + } +} +func TestGuestForeignArtifactRejected(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + artifact := h.put(t, "put") + foreign := h.actor.Clone() + foreign.AuthenticatedCaller = "other" + foreign.EffectiveActor = "other" + p := h.params("prepare", "other-prepare") + p.Caller = foreign + if _, err := h.service.Execute(context.Background(), p, nil, nil); err != nil { + t.Fatal(err) + } + p = h.params("remove-owned", "foreign") + p.Caller = foreign + p.ArtifactIDs = []string{artifact.ID} + _, err := h.service.Execute(context.Background(), p, nil, nil) + if !errors.Is(err, ErrGuestOwnership) || h.transport.executed != 3 { + t.Fatalf("%v executions %d", err, h.transport.executed) + } +} +func TestGuestReplacedRootAndFileMetadataRejected(t *testing.T) { + for _, field := range []string{"root", "file", "size", "digest", "worker"} { + t.Run(field, func(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + h.transport.alter = func(result *stream.Result) { + switch field { + case "root": + result.Files[0].RootFileID = strings.Repeat("e", 32) + case "file": + result.Files[0].FileID = "" + case "size": + result.Files[0].Size++ + case "digest": + result.Files[0].SHA256 = strings.Repeat("e", 64) + case "worker": + result.Worker.BirthTicks++ + } + } + p := h.params("put", "metadata") + p.Size = 4 + p.SHA256 = guestSHA("data") + out, err := h.service.Execute(context.Background(), p, stream.BytesInput([]byte("data")), nil) + if !errors.Is(err, ErrGuestOwnership) || len(out.Artifacts) != 0 { + t.Fatalf("%+v %v", out, err) + } + }) + } +} +func TestGuestRemovalPerItemTruth(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + first := h.put(t, "first") + second := h.put(t, "second") + h.transport.alter = func(result *stream.Result) { result.Removed[1].Status = "preserved"; result.Removed[1].Absent = false } + p := h.params("remove-owned", "manifest") + p.ArtifactIDs = []string{first.ID, second.ID} + out, err := h.service.Execute(context.Background(), p, nil, nil) + if err != nil || len(out.Removed) != 2 || !out.Removed[0].Accepted || out.Removed[1].Accepted || out.Removed[1].Absent { + t.Fatalf("%+v %v", out, err) + } +} + +func guestSHA(data string) string { + sum := sha256.Sum256([]byte(data)) + return hex.EncodeToString(sum[:]) +} diff --git a/internal/app/guest_transport_diagnostics_test.go b/internal/app/guest_transport_diagnostics_test.go new file mode 100644 index 0000000..0c5722f --- /dev/null +++ b/internal/app/guest_transport_diagnostics_test.go @@ -0,0 +1,157 @@ +package app + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "os" + "path/filepath" + "slices" + "strings" + "testing" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +func TestGuestTransportSentinelsRecordNoWrappedSecretsOrOutput(t *testing.T) { + cases := []struct { + category string + cause error + }{ + {"lifetime_pending", stream.ErrLifetimePending}, {"lifetime_query", stream.ErrLifetimeQuery}, {"lifetime_information", stream.ErrLifetimeInformation}, {"lifetime_disconnected", stream.ErrLifetimeDisconnected}, + {"protocol", stream.ErrProtocol}, {"integrity", stream.ErrIntegrity}, {"eof", io.EOF}, + {"unexpected_eof", io.ErrUnexpectedEOF}, {"short_write", io.ErrShortWrite}, + } + for _, tc := range cases { + t.Run(tc.category, func(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + h.transport.executeHook = func(_ context.Context, _ stream.Request, _ io.Reader, sink io.Writer) (stream.Result, error) { + _, _ = sink.Write([]byte("private output")) + return stream.Result{}, fmt.Errorf("private path credentials: %w", tc.cause) + } + p := h.params("exec", "transport-diagnostic") + p.Size, p.SHA256, p.MaxOutput = 1, guestSHA("x"), 64 + var output bytes.Buffer + out, err := h.service.Execute(t.Context(), p, stream.BytesInput([]byte("x")), stream.BufferOutput(&output)) + if !errors.Is(err, ErrGuestUncertain) || !errors.Is(err, tc.cause) || strings.Contains(err.Error(), "private") { + t.Fatalf("unsafe error: %v", err) + } + assertGuestFailedDiagnostic(t, h, out, "guest-transport-failure:"+tc.category) + if output.Len() != 0 { + t.Fatal("failed output published") + } + assertGuestSpoolClean(t, h) + }) + } +} + +func assertGuestFailedDiagnostic(t *testing.T, h *guestHarness, out GuestOperationResult, ref string) { + t.Helper() + if out.Receipt == nil || out.Receipt.Outcome.Status != domain.OutcomeFailed || !slices.Equal(out.Receipt.EvidenceRefs, []string{"guest-operation-dispatched", ref}) || h.transport.retired != 1 { + t.Fatalf("failure/retirement contract: %+v retired=%d", out, h.transport.retired) + } + raw, err := json.Marshal(out) + if err != nil || bytes.Contains(raw, []byte("private")) { + t.Fatalf("result leaked metadata: %s %v", raw, err) + } + id := strings.TrimPrefix(string(out.Receipt.IdempotencyFingerprint), "sha256:") + if _, err := os.Stat(filepath.Join(h.service.store.directory, id+".result")); !errors.Is(err, os.ErrNotExist) { + t.Fatal("failed result committed", err) + } + assertGuestOriginalRetirement(t, h, out.Receipt) +} + +func assertGuestOriginalRetirement(t *testing.T, h *guestHarness, receipt *domain.Receipt) { + t.Helper() + entries, err := os.ReadDir(h.service.store.directory) + if err != nil { + t.Fatal(err) + } + found := 0 + for _, entry := range entries { + if !strings.HasSuffix(entry.Name(), ".retirement") { + continue + } + id := strings.TrimSuffix(entry.Name(), ".retirement") + var record guestWorkerRecord + if err := h.service.store.read(t.Context(), id, ".retirement", &record); err != nil || record.RecordID != id || record.Identity != h.transport.worker { + t.Fatalf("wrong original retirement: %+v %v", record, err) + } + var original guestWorkerRecord + if err := h.service.store.read(t.Context(), id, ".worker", &original); err != nil || original != record || record.Target != receipt.Target || record.Actor != receipt.Actor || record.Caller != h.actor.AuthenticatedCaller { + t.Fatalf("retirement did not preserve immutable original: %+v %v", record, err) + } + found++ + } + if found != 1 { + t.Fatalf("expected one original retirement, got %d", found) + } +} + +func TestGuestRootDiagnosticSurvivesRetirementCloseEOF(t *testing.T) { + h := newGuestHarness(t) + h.transport.alter = func(r *stream.Result) { r.Root.SDDLHash = strings.Repeat("e", 64) } + h.transport.retireErr = io.EOF + out, err := h.service.Execute(t.Context(), h.params("prepare", "root-and-close-failure"), nil, nil) + if !errors.Is(err, ErrGuestOwnership) || !errors.Is(err, ErrGuestUncertain) || !errors.Is(err, io.EOF) { + t.Fatalf("original failure lost: %v", err) + } + assertGuestFailedDiagnostic(t, h, out, "guest-result-failure:root_sddl") +} + +func TestGuestPrepareRootDiagnosticsKeepEveryOriginalFieldExact(t *testing.T) { + cases := map[string]func(*stream.Result){ + "missing": func(r *stream.Result) { r.Root = nil }, + "path": func(r *stream.Result) { r.Root.Path = `C:\private\foreign` }, + "volume": func(r *stream.Result) { r.Root.Volume++ }, + "id": func(r *stream.Result) { r.Root.FileID = strings.Repeat("f", 32) }, + "creation": func(r *stream.Result) { r.Root.CreationTime++ }, + "owner": func(r *stream.Result) { r.Root.Owner = "private-owner" }, + "sddl": func(r *stream.Result) { r.Root.SDDLHash = strings.Repeat("e", 64) }, + } + for field, alter := range cases { + t.Run(field, func(t *testing.T) { + h := newGuestHarness(t) + h.transport.alter = alter + p := h.params("prepare", "root-diagnostic") + out, err := h.service.Execute(t.Context(), p, nil, nil) + if !errors.Is(err, ErrGuestOwnership) || !errors.Is(err, ErrGuestUncertain) { + t.Fatalf("root admitted: %+v %v", out, err) + } + assertGuestFailedDiagnostic(t, h, out, "guest-result-failure:root_"+field) + if strings.Contains(err.Error(), "private") { + t.Fatal("root value exposed", err) + } + if _, err := h.service.Execute(t.Context(), p, nil, nil); err == nil || h.transport.executed != 1 || h.transport.prepared != 1 { + t.Fatal("uncertain prepare replayed", err) + } + }) + } +} + +func TestGuestControlFatalCopiesOnlyClosedMetadataAndRetires(t *testing.T) { + for _, stage := range []string{"control_eof", "control_read", "control_decode", "control_header"} { + t.Run(stage, func(t *testing.T) { + h := newGuestHarness(t) + h.prepare(t) + h.transport.executeHook = func(_ context.Context, _ stream.Request, _ io.Reader, sink io.Writer) (stream.Result, error) { + _, _ = sink.Write([]byte("private partial output")) + return stream.Result{}, errors.Join(&PolicyDeniedError{Message: "private policy message"}, &stream.ControlFailure{Stage: stage}) + } + p := h.params("exec", "fatal-control") + p.Size, p.SHA256, p.MaxOutput = 1, guestSHA("x"), 64 + var output bytes.Buffer + out, err := h.service.Execute(t.Context(), p, stream.BytesInput([]byte("x")), stream.BufferOutput(&output)) + if !errors.Is(err, ErrGuestUncertain) || !errors.Is(err, stream.ErrControlFailure) || strings.Contains(err.Error(), "private") || output.Len() != 0 { + t.Fatal("fatal escaped boundary", err) + } + assertGuestFailedDiagnostic(t, h, out, "guest-control-failure:"+stage) + assertGuestSpoolClean(t, h) + }) + } +} diff --git a/internal/app/guest_types.go b/internal/app/guest_types.go new file mode 100644 index 0000000..3931079 --- /dev/null +++ b/internal/app/guest_types.go @@ -0,0 +1,124 @@ +package app + +import ( + "context" + "errors" + "io" + "sync" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +var ErrGuestWorkspaceUnprovisioned = errors.New("app: guest workspace requires explicit provisioning") + +var ErrGuestAuthority = errors.New("app: guest authority changed or unavailable") +var ErrGuestNotPrepared = errors.New("app: guest worker requires explicit preparation") +var ErrGuestOwnership = errors.New("app: guest artifact ownership or identity mismatch") +var ErrGuestUncertain = errors.New("app: guest operation outcome is uncertain; do not replay") + +// GuestConfiguration is refreshed at admission and at the effect boundary. +// Current must revalidate enrollment, credentials and key pins, current actor +// authority, and the server-provisioned workspace; Generation binds all of them. +// Caller preserves requested authority without expanding it to the live principal. +// WorkspaceIdentity must come from authorized provisioning, never path adoption. +type GuestConfiguration struct { + Generation string + Caller domain.ActorContext + WorkspaceIdentity stream.RootIdentity +} + +type GuestConfigProvider interface { + Current(context.Context, domain.MachineRef, domain.ActorContext) (GuestConfiguration, error) +} + +type GuestTransport interface { + Prepare(context.Context, domain.MachineRef, string, string) (stream.WorkerIdentity, error) + Execute(context.Context, domain.MachineRef, string, string, stream.Request, io.Reader, io.Writer) (stream.Result, error) + Retire(domain.MachineRef, string, string) error +} + +// GuestOperationParams carries bounded public metadata, never a guest path. +// Put and exec stream exactly Size bytes matching SHA256. Get and remove use +// immutable opaque references returned by put. Deadlines are exact and required. +type GuestOperationParams struct { + Target string `json:"target"` + Caller domain.ActorContext `json:"-"` + Operation string `json:"operation"` + ArtifactIDs []string `json:"artifact_ids,omitempty"` + Size int64 `json:"size"` + SHA256 string `json:"sha256,omitempty"` + MaxOutput int64 `json:"max_output"` + Reason string `json:"reason"` + IdempotencyKey string `json:"idempotency_key"` + Deadline time.Time `json:"deadline"` + ApprovalID string `json:"approval_id,omitempty"` +} + +type GuestArtifact struct { + ID string `json:"id"` + Size int64 `json:"size"` + SHA256 string `json:"sha256"` +} + +type GuestRemoval struct { + ArtifactID string `json:"artifact_id"` + Attempted bool `json:"attempted"` + Accepted bool `json:"accepted"` + Absent bool `json:"absent"` + Status string `json:"status"` +} + +type GuestOperationResult struct { + WorkerRecordID string `json:"worker_record_id,omitempty"` + Receipt *domain.Receipt `json:"receipt,omitempty"` + CachedReceipt bool `json:"cached_receipt"` + Worker stream.WorkerIdentity `json:"worker"` + Artifacts []GuestArtifact `json:"artifacts,omitempty"` + Removed []GuestRemoval `json:"removed,omitempty"` + Size int64 `json:"size"` + SHA256 string `json:"sha256"` + ExitCode int `json:"exit_code"` +} + +type guestOwnedArtifact struct { + Artifact GuestArtifact + Target domain.MachineRef + Caller domain.ActorID + Actor domain.ActorID + Generation string + Identity stream.FileIdentity +} + +type guestWorkerRecord struct { + RecordID string + Target domain.MachineRef + Caller domain.ActorID + Actor domain.ActorID + Generation string + Identity stream.WorkerIdentity + Root stream.RootIdentity +} + +type GuestService struct { + session *SessionService + resolver SessionTargetResolver + config GuestConfigProvider + transport GuestTransport + store *GuestStore + mu sync.Mutex + workers map[string]guestWorkerRecord + spoolBytes int64 + running int + closing bool + drained chan struct{} + shutdownGate chan struct{} + shutdownDone bool +} + +func NewGuestService(session *SessionService, resolver SessionTargetResolver, config GuestConfigProvider, transport GuestTransport, store *GuestStore) *GuestService { + gate := make(chan struct{}, 1) + gate <- struct{}{} + return &GuestService{shutdownGate: gate, session: session, resolver: resolver, config: config, transport: transport, store: store, workers: map[string]guestWorkerRecord{}} +} diff --git a/internal/app/guest_worker_record_test.go b/internal/app/guest_worker_record_test.go new file mode 100644 index 0000000..7533a96 --- /dev/null +++ b/internal/app/guest_worker_record_test.go @@ -0,0 +1,43 @@ +package app + +import ( + "strings" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" +) + +func TestGuestPrepareReturnsOriginalRecordAfterReceiptReclassification(t *testing.T) { + h := newGuestHarness(t) + h.safety.destructive = true + h.actor.CallerPermissions[domain.ScopeSessionAdmin] = struct{}{} + h.actor.EffectivePermissions[domain.ScopeSessionAdmin] = struct{}{} + p := h.params("prepare", "original-record") + grant, _, err := h.service.IssueApprovalFor(t.Context(), p, time.Minute, "self") + if err != nil { + t.Fatal(err) + } + p.Deadline, p.ApprovalID = grant.Deadline, grant.ApprovalID + out, err := h.service.Execute(t.Context(), p, nil, nil) + if err != nil || out.Receipt == nil || len(out.WorkerRecordID) != 64 || out.WorkerRecordID == strings.TrimPrefix(string(out.Receipt.IdempotencyFingerprint), "sha256:") { + t.Fatalf("original record was inferred from classified receipt: %+v %v", out, err) + } + assertGuestReturnedOriginalRecord(t, h, out) + cached, err := h.service.Execute(t.Context(), p, nil, nil) + if err != nil || !cached.CachedReceipt || cached.WorkerRecordID != out.WorkerRecordID || cached.Worker != out.Worker || h.transport.prepared != 1 { + t.Fatalf("cached original drift: %+v %v", cached, err) + } +} + +func assertGuestReturnedOriginalRecord(t *testing.T, h *guestHarness, out GuestOperationResult) { + t.Helper() + var err error + var original, ready guestWorkerRecord + if err = h.service.store.read(t.Context(), out.WorkerRecordID, ".worker", &original); err != nil { + t.Fatal(err) + } + if err = h.service.store.read(t.Context(), out.WorkerRecordID, ".ready", &ready); err != nil || ready != original || original.RecordID != out.WorkerRecordID || original.Identity != out.Worker || original.Actor != out.Receipt.Actor { + t.Fatalf("unproven original: %+v %v", original, err) + } +} diff --git a/internal/app/operation_approval_persistence.go b/internal/app/operation_approval_persistence.go index 9956b96..307b17c 100644 --- a/internal/app/operation_approval_persistence.go +++ b/internal/app/operation_approval_persistence.go @@ -2,6 +2,7 @@ package app import ( "context" + "strings" "time" "github.com/Horcag/agent-machine-control/internal/domain" @@ -60,7 +61,7 @@ func operationApprovalGrant(issued domain.Approval, approved domain.Operation) * // LoadOperationApprovalReference resolves immutable authority after the exact operation is canonical. func (s *RecoveryService) LoadOperationApprovalReference(ctx context.Context, op domain.Operation, approvalID string) (*domain.Approval, error) { - if s == nil || s.approvalStore == nil || approvalID == "" { + if s == nil || s.approvalStore == nil || approvalID == "" || strings.HasPrefix(approvalID, ConsoleLabApprovalPrefix) { return nil, ErrInvalidOperationApprovalReference } if err := domain.ValidateApprovalID(approvalID); err != nil { diff --git a/internal/app/recovery.go b/internal/app/recovery.go index fc60420..a81827f 100644 --- a/internal/app/recovery.go +++ b/internal/app/recovery.go @@ -60,10 +60,13 @@ type MutationRequest struct { // Set only when the normal identity-bound receipt lookup returns a terminal result. cachedReceipt *bool providerDispatched *bool + // Only trusted lab coordination sets this causal floor for local issuance. + labAdmissionAt time.Time } // RecoveryService orchestrates in-process direct recovery operations, policy, leases, and receipts. type RecoveryService struct { + labAuthority *ConsoleService backend Backend targetResolver interface { ResolveTarget(context.Context, string) (TargetResolution, error) diff --git a/internal/app/recovery_admission.go b/internal/app/recovery_admission.go index e5d65c8..0f3d06a 100644 --- a/internal/app/recovery_admission.go +++ b/internal/app/recovery_admission.go @@ -3,6 +3,7 @@ package app import ( "context" "errors" + "fmt" "time" "github.com/Horcag/agent-machine-control/internal/domain" @@ -90,3 +91,30 @@ func runLifecycleHooks(ctx context.Context, req MutationRequest) error { } return nil } + +func (s *RecoveryService) checkPreconditions(ctx context.Context, op domain.Operation, req MutationRequest) (*domain.Receipt, error) { + if s.receiptStore != nil { + // Durable terminal truth wins over caller cancellation for an exact retry. + lookupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second) + cached, err := s.receiptStore.LookupIdempotencyContext(lookupCtx, op) + cancel() + if err != nil { + return nil, err + } + if cached != nil { + if req.cachedReceipt != nil { + *req.cachedReceipt = true + } + return cached, nil + } + } + if err := ctx.Err(); err != nil { + return nil, err + } + if s.auditStore != nil { + if err := s.auditStore.CheckWritableContext(ctx); err != nil { + return nil, fmt.Errorf("%w: %w", ErrAuditUnavailable, err) + } + } + return nil, nil +} diff --git a/internal/app/recovery_lab_clock_test.go b/internal/app/recovery_lab_clock_test.go new file mode 100644 index 0000000..826663f --- /dev/null +++ b/internal/app/recovery_lab_clock_test.go @@ -0,0 +1,71 @@ +package app + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" +) + +type clockLabBackend struct { + Backend + onCapabilities func() +} + +func (b clockLabBackend) Capabilities(context.Context, string) (domain.CapabilitySet, error) { + b.onCapabilities() + return domain.NewCapabilitySet(domain.CapabilityConsoleInput), nil +} + +func TestPersistentRecoveryLabClockBoundaries(t *testing.T) { + for _, forward := range []bool{false, true} { + name := "backward step" + if forward { + name = "expiry before consumption" + } + t.Run(name, func(t *testing.T) { testPersistentRecoveryLabClock(t, forward) }) + } +} + +func testPersistentRecoveryLabClock(t *testing.T, forward bool) { + t.Helper() + authority, grant, op, _ := coordinationGrantFixture(t) + recovery := authority.recovery + WithRecoveryLabAuthority(authority)(recovery) + digest := sha256.Sum256([]byte(grant.GrantID + "\x00" + string(op.Actor.EffectiveActor) + "\x00" + op.IdempotencyKey)) + id := ConsoleLabApprovalPrefix + hex.EncodeToString(digest[:16]) + admittedAt := grant.IssuedAt.Add(time.Second) + op.Deadline = admittedAt.Add(time.Minute) + authorized := false + recovery.backend = clockLabBackend{onCapabilities: func() { authorized = true }} + recovery.nowFn = func() time.Time { + if forward && authorized { + return op.Deadline + } + if issued, _ := recovery.approvalStore.LoadIssuedContext(context.Background(), id); issued != nil { + return admittedAt.Add(-50 * time.Millisecond) + } + return admittedAt + } + req := MutationRequest{TargetID: string(op.Target), Actor: op.Actor, Reason: op.Reason, IdempotencyKey: op.IdempotencyKey, Deadline: op.Deadline} + locator, err := domain.ParseMachineLocator(string(op.Target)) + if err != nil { + t.Fatal(err) + } + dispatched := 0 + result, err := recovery.executeMutation(t.Context(), op, req, locator.VMID, func(context.Context) error { dispatched++; return nil }) + if forward { + if err == nil || dispatched != 0 { + t.Fatalf("expired admission dispatched: result=%+v err=%v dispatches=%d", result, err, dispatched) + } + } else if err != nil || result.Outcome.Status != domain.OutcomeSuccess || dispatched != 1 { + t.Fatalf("backwards clock rejected causal admission: result=%+v err=%v dispatches=%d", result, err, dispatched) + } + consumed, err := recovery.approvalStore.IsConsumedContext(t.Context(), id) + if err != nil || consumed == forward { + t.Fatalf("consumption=%v err=%v", consumed, err) + } +} diff --git a/internal/app/recovery_mutation.go b/internal/app/recovery_mutation.go index 9d60e3f..8ad5366 100644 --- a/internal/app/recovery_mutation.go +++ b/internal/app/recovery_mutation.go @@ -102,7 +102,7 @@ func (s *RecoveryService) executeMutation( req MutationRequest, providerTargetID string, execFn func(context.Context) error, -) (domain.Receipt, error) { +) (result domain.Receipt, resultErr error) { execCtx, cancelExecution := s.mutationExecutionContext(ctx, op, req) defer cancelExecution() ctx = execCtx @@ -117,17 +117,20 @@ func (s *RecoveryService) executeMutation( } // 1. Idempotency Check & 2. Audit Writability Check - if cached, err := s.checkPreconditions(ctx, op); err != nil || cached != nil { + if cached, err := s.checkPreconditions(ctx, op, req); err != nil || cached != nil { if cached != nil { - if req.cachedReceipt != nil { - *req.cachedReceipt = true - } return *cached, cachedRecoveryOutcomeError(cached.Outcome) } return s.preProviderFailure(ctx, op, fp, policy.Decision{}, s.now(), err, "", req.ApprovalID) } - now := s.now() + req, execFn, releaseGrant, err := s.coordinatePersistentRecovery(ctx, op, req, execFn) + if err != nil { + return domain.Receipt{}, err + } + defer func() { resultErr = errors.Join(resultErr, releaseGrant()) }() + + now := labAdmissionTime(s.now(), req.labAdmissionAt, !req.labAdmissionAt.IsZero()) // 3. Rollback discovery & 4. Policy evaluation & 5. Approval check decision, rollbackRef, err := s.prepareAndAuthorizeMutation(ctx, op, req, providerTargetID, now) @@ -148,7 +151,8 @@ func (s *RecoveryService) executeMutation( } // 7. Audit Admission Intent & Approval Consumption - approvalConsumed, err := s.recordAdmissionAndConsume(ctx, op, decision, req, now) + consumedAt := labAdmissionTime(s.now(), now, !req.labAdmissionAt.IsZero()) + approvalConsumed, err := s.recordAdmissionAndConsume(ctx, op, decision, req, consumedAt) if err != nil { return s.preProviderFailure(ctx, op, fp, decision, now, errors.Join(err, releaseLease()), rollbackRef, req.ApprovalID) } @@ -183,30 +187,6 @@ func (s *RecoveryService) executeMutation( return s.finalizeMutation(receiptRecord, runErr, persistErr, releaseErr) } -func (s *RecoveryService) checkPreconditions(ctx context.Context, op domain.Operation) (*domain.Receipt, error) { - if s.receiptStore != nil { - // Durable terminal truth wins over caller cancellation for an exact retry. - lookupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second) - cached, err := s.receiptStore.LookupIdempotencyContext(lookupCtx, op) - cancel() - if err != nil { - return nil, err - } - if cached != nil { - return cached, nil - } - } - if err := ctx.Err(); err != nil { - return nil, err - } - if s.auditStore != nil { - if err := s.auditStore.CheckWritableContext(ctx); err != nil { - return nil, fmt.Errorf("%w: %w", ErrAuditUnavailable, err) - } - } - return nil, nil -} - func (s *RecoveryService) acquireMutationLease( ctx context.Context, op domain.Operation, @@ -240,6 +220,11 @@ func (s *RecoveryService) recordAdmissionAndConsume( req MutationRequest, now time.Time, ) (bool, error) { + if decision.EffectiveClass.RequiresApproval() && req.Approval != nil { + if err := req.Approval.IsActive(now); err != nil { + return false, approvalActivityDenial(err) + } + } if s.auditStore != nil { if err := s.auditStore.RecordAdmissionIntentContext(ctx, op); err != nil { return false, fmt.Errorf("%w: %w", ErrAuditUnavailable, err) diff --git a/internal/app/safety.go b/internal/app/safety.go index 3cc793c..4d0c2b5 100644 --- a/internal/app/safety.go +++ b/internal/app/safety.go @@ -37,6 +37,11 @@ type SafetyResolver interface { ResolveSafety(ctx context.Context, target domain.MachineRef) (SafetyResolution, error) } +// Approval issuance needs only classification, never rollback authority. +type approvalClassificationResolver interface { + resolveApprovalClassification(context.Context, domain.MachineRef) (domain.OperationClass, error) +} + // DefaultSafetyResolver verifies server-owned configuration and backend checkpoints. type DefaultSafetyResolver struct { configLoader MachineConfigLoader @@ -53,6 +58,15 @@ func NewDefaultSafetyResolver(configLoader MachineConfigLoader, backend Backend) // ResolveSafety checks if the target has external effects contained and an active verified checkpoint. func (r *DefaultSafetyResolver) ResolveSafety(ctx context.Context, target domain.MachineRef) (SafetyResolution, error) { + return r.resolveSafety(ctx, target, false) +} + +func (r *DefaultSafetyResolver) resolveApprovalClassification(ctx context.Context, target domain.MachineRef) (domain.OperationClass, error) { + resolution, err := r.resolveSafety(ctx, target, true) + return resolution.Classification, err +} + +func (r *DefaultSafetyResolver) resolveSafety(ctx context.Context, target domain.MachineRef, classificationOnly bool) (SafetyResolution, error) { destructive := SafetyResolution{ Classification: domain.ClassDestructivePrivileged, RollbackState: policy.RollbackState{Available: false, Verified: false}, @@ -77,7 +91,20 @@ func (r *DefaultSafetyResolver) ResolveSafety(ctx context.Context, target domain if err != nil || cfg == nil { return destructive, nil } + if classificationOnly && ctx.Err() != nil { + return destructive, ctx.Err() + } + // Uncontained effects are destructive regardless of checkpoint state. Full + // admission and effect resolution still verify rollback on every call. + if classificationOnly && !cfg.ExternalEffectsContained { + return destructive, nil + } + + return r.resolveCheckpointSafety(ctx, target, cfg, destructive) +} +// Checkpoint verification is shared by full safety and contained classification. +func (r *DefaultSafetyResolver) resolveCheckpointSafety(ctx context.Context, target domain.MachineRef, cfg *MachineSafetyConfig, destructive SafetyResolution) (SafetyResolution, error) { if cfg.RollbackCheckpointID == "" { return destructive, nil } diff --git a/internal/app/session.go b/internal/app/session.go index cde1cd1..819973d 100644 --- a/internal/app/session.go +++ b/internal/app/session.go @@ -16,6 +16,7 @@ import ( // SessionService coordinates session mutation admissions, policy, idempotency, audit, and receipts. type SessionService struct { + labAuthority *ConsoleService sessionMgr *sessions.Manager safetyResolver SafetyResolver targetResolver SessionTargetResolver diff --git a/internal/app/session_approval_resolution.go b/internal/app/session_approval_resolution.go index fd2d23f..eeadcef 100644 --- a/internal/app/session_approval_resolution.go +++ b/internal/app/session_approval_resolution.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "strings" "github.com/Horcag/agent-machine-control/internal/domain" "github.com/Horcag/agent-machine-control/internal/policy" @@ -23,7 +24,7 @@ func (s *SessionService) loadSessionApprovalReference(ctx context.Context, appro if approvalID == "" { return nil, nil } - if s.approvalStore == nil { + if s.approvalStore == nil || strings.HasPrefix(approvalID, ConsoleLabApprovalPrefix) { return nil, invalidSessionApprovalReference() } loaded, err := s.approvalStore.LoadIssuedContext(ctx, approvalID) diff --git a/internal/app/session_coordination.go b/internal/app/session_coordination.go index 0f4db27..24a29f5 100644 --- a/internal/app/session_coordination.go +++ b/internal/app/session_coordination.go @@ -19,6 +19,10 @@ func (s *SessionService) evaluatePolicy(ctx context.Context, op domain.Operation return policy.Decision{}, err } + capabilities := domain.SessionCapabilities() + if domain.IsGuestOperation(op.Kind) { + capabilities[domain.CapabilityGuestOperation] = struct{}{} + } evalInput := policy.EvaluationInput{ Operation: op, Now: now, @@ -26,7 +30,7 @@ func (s *SessionService) evaluatePolicy(ctx context.Context, op domain.Operation RollbackState: rollbackState, Approval: approval, RollbackPolicy: policy.RollbackPolicyEscalateToDestructive, - AvailableCapabilities: domain.SessionCapabilities(), + AvailableCapabilities: capabilities, SensitiveEvidenceScopes: domain.NewScopeSet("evidence:sensitive", policy.DefaultSensitiveEvidenceScope), } @@ -159,6 +163,11 @@ func (s *SessionService) checkDestructiveApproval(ctx context.Context, approval } func (s *SessionService) prepareAuditAndApproval(ctx context.Context, decision policy.Decision, op domain.Operation, approval *domain.Approval, now time.Time) error { + if decision.EffectiveClass.RequiresApproval() && approval != nil { + if err := approval.IsActive(now); err != nil { + return approvalActivityDenial(err) + } + } if s.auditStore != nil { if err := s.auditStore.RecordAdmissionIntentContext(ctx, op); err != nil { return err @@ -286,6 +295,9 @@ func (s *SessionService) executeMutation( } type admittedSessionMutation struct { + approval *domain.Approval + labGrant *ConsoleLabGrant + admittedAt time.Time op domain.Operation fp domain.Fingerprint idFp domain.Fingerprint @@ -301,13 +313,26 @@ func (s *SessionService) admitSessionMutation( timeout time.Duration, initialFP domain.Fingerprint, ) (*admittedSessionMutation, *domain.Receipt, error) { + var labGrant *ConsoleLabGrant + releaseGrant := func() error { return nil } + if approval == nil { + grant, release, err := s.labAuthority.admitPersistentLab(ctx, op) + if err != nil { + return nil, nil, err + } + labGrant, releaseGrant = grant, release + } releaseLease, err := s.acquireMutationLease(ctx, op, timeout, initialFP) if err != nil { - return nil, nil, err + return nil, nil, errors.Join(err, releaseGrant()) } - now := s.now() + releaseMachine := releaseLease + releaseLease = func() error { return errors.Join(releaseGrant(), releaseMachine()) } safety := s.resolveSafety(ctx, op.Target) op.Classification = safety.Classification + if domain.IsGuestOperation(op.Kind) && op.Classification != domain.ClassReversibleMutation && op.Classification != domain.ClassDestructivePrivileged { + return nil, nil, errors.Join(ErrGuestAuthority, releaseLease()) + } fp, idFp, err := s.validateAndFingerprint(op) if err != nil { return nil, nil, errors.Join(err, releaseLease()) @@ -320,12 +345,20 @@ func (s *SessionService) admitSessionMutation( releaseLease: releaseLease, }, nil, nil } + admittedAt := s.now() + if labGrant != nil && op.Classification.RequiresApproval() { + approval, err = s.labAuthority.deriveLabApproval(ctx, *labGrant, op, admittedAt) + if err != nil { + return nil, nil, errors.Join(err, releaseLease()) + } + } + now := labAdmissionTime(s.now(), admittedAt, labGrant != nil) decision, denialReceipt, err := s.evaluateAndAdmit(ctx, op, safety, approval, now, fp, idFp) if err != nil { return nil, denialReceipt, errors.Join(err, releaseLease()) } return &admittedSessionMutation{ - op: op, fp: fp, idFp: idFp, decision: decision, safety: safety, releaseLease: releaseLease, + op: op, fp: fp, idFp: idFp, decision: decision, safety: safety, releaseLease: releaseLease, approval: approval, labGrant: labGrant, admittedAt: now, }, nil, nil } @@ -333,7 +366,7 @@ func (s *SessionService) reserveAndPrepareMutation(ctx context.Context, admitted if s.mutationJournal == nil { return errors.Join(errors.New("app: session mutation journal is unavailable"), admitted.releaseLease()) } - now := s.now() + now := labAdmissionTime(s.now(), admitted.admittedAt, admitted.labGrant != nil) if _, err := s.mutationJournal.ReserveContext(ctx, admitted.op, now); err != nil { return errors.Join(err, admitted.releaseLease()) } @@ -425,13 +458,16 @@ func (s *SessionService) coordinateSessionMutation( } return sessionMutationResult{}, denialRcpt, err } - if err := s.reserveAndPrepareMutation(ctx, admitted, approval); err != nil { + if err := s.reserveAndPrepareMutation(ctx, admitted, admitted.approval); err != nil { entry.err = err return sessionMutationResult{}, nil, err } - result, rcpt, mutErr := s.executeMutation(ctx, admitted.op, admitted.fp, admitted.idFp, admitted.safety, execute) - mutErr = errors.Join(mutErr, s.releaseUnexecutedApproval(ctx, admitted, approval, result)) + result, rcpt, mutErr := s.executeMutation(withGuestAdmission(ctx, admitted), admitted.op, admitted.fp, admitted.idFp, admitted.safety, func(execCtx context.Context) (result sessionMutationResult, err error) { + err = s.labAuthority.withPersistentLabEffect(execCtx, admitted.labGrant, func(fenced context.Context) error { result, err = execute(fenced); return err }) + return result, err + }) + mutErr = errors.Join(mutErr, s.releaseUnexecutedApproval(ctx, admitted, admitted.approval, result)) releaseErr := admitted.releaseLease() entry.result = result diff --git a/internal/app/target_guest.go b/internal/app/target_guest.go new file mode 100644 index 0000000..aac0c9a --- /dev/null +++ b/internal/app/target_guest.go @@ -0,0 +1,27 @@ +package app + +import ( + "context" +) + +// ResolveGuestTarget resolves current protected enrollment and host authority. +// Guest operations authenticate the pinned SSH peer and original worker/root; +// they do not require a host VM state observation. Host recovery and interactive +// sessions continue to use ResolveTarget and its fresh provider observation. +func (s *TargetService) ResolveGuestTarget(ctx context.Context, reference string) (TargetResolution, error) { + value, err := s.store.Load(ctx) + if err != nil { + return TargetResolution{}, err + } + if err = validateTargetReference(reference, value); err != nil { + return TargetResolution{}, err + } + if _, err = s.targetHost(ctx, value.Locator); err != nil { + return TargetResolution{}, err + } + resolution := TargetResolution{Locator: value.Locator, ProviderVMID: value.Locator.VMID} + if err = resolution.Validate(); err != nil { + return TargetResolution{}, err + } + return resolution, ctx.Err() +} diff --git a/internal/app/target_guest_test.go b/internal/app/target_guest_test.go new file mode 100644 index 0000000..e55b639 --- /dev/null +++ b/internal/app/target_guest_test.go @@ -0,0 +1,73 @@ +package app + +import ( + "context" + "errors" + "strings" + "testing" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/target" +) + +func TestGuestTargetResolvesProtectedIdentityWithoutHostObservation(t *testing.T) { + original := targetObservation(t, domain.LocalHostID, targetVMA, "synthetic-name") + observer := &exactTargetObserver{inspect: func(context.Context, string) (domain.MachineObservation, error) { + t.Fatal("guest identity resolution must not query host VM state") + return domain.MachineObservation{}, nil + }} + service, _ := exactTargetService(t, original, observer, func(context.Context) error { + t.Fatal("guest identity resolution must not refresh fleet state") + return nil + }) + for _, ref := range []string{"", "default", "primary", targetVMA, strings.ToUpper(targetVMA), original.Locator.String()} { + resolved, err := service.ResolveGuestTarget(t.Context(), ref) + if err != nil || resolved.Validate() != nil || resolved.Locator != original.Locator || resolved.ProviderVMID != targetVMA || resolved.DisplayName != "" { + t.Fatalf("reference %q: %+v, %v", ref, resolved, err) + } + } + for _, ref := range []string{original.Name, targetVMB, "local:" + targetVMB, "remote:" + targetVMA, "PRIMARY", "default ", " " + targetVMA} { + if resolved, err := service.ResolveGuestTarget(t.Context(), ref); !errors.Is(err, target.ErrDifferentTarget) || resolved != (TargetResolution{}) { + t.Fatalf("foreign reference %q: %+v, %v", ref, resolved, err) + } + } +} + +func TestGuestTargetRefreshesEnrollmentAndHostAuthority(t *testing.T) { + original := targetObservation(t, domain.LocalHostID, targetVMA, "synthetic-name") + service, inventory := exactTargetService(t, original, nil, func(context.Context) error { + t.Fatal("guest resolution must not refresh inventory") + return nil + }) + if err := inventory.ReplaceHosts([]HostEntry{{ID: domain.LocalHostID, Address: "local", Enabled: false}}); err != nil { + t.Fatal(err) + } + if _, err := service.ResolveGuestTarget(t.Context(), "primary"); !errors.Is(err, domain.ErrMachineHostDisabled) { + t.Fatalf("disabled host: %v", err) + } + if err := inventory.ReplaceHosts([]HostEntry{{ID: domain.LocalHostID, Address: "local", Enabled: true}}); err != nil { + t.Fatal(err) + } + if _, err := service.store.Clear(t.Context()); err != nil { + t.Fatal(err) + } + if _, err := service.ResolveGuestTarget(t.Context(), "primary"); !errors.Is(err, target.ErrNoDefault) { + t.Fatalf("cleared enrollment: %v", err) + } + locator, _ := domain.NewMachineLocator(domain.LocalHostID, targetVMB) + value, _ := target.NewDefault(locator, []string{"replacement"}) + if _, err := service.store.Save(t.Context(), value); err != nil { + t.Fatal(err) + } + if _, err := service.ResolveGuestTarget(t.Context(), targetVMA); !errors.Is(err, target.ErrDifferentTarget) { + t.Fatalf("old enrollment: %v", err) + } + if resolved, err := service.ResolveGuestTarget(t.Context(), "replacement"); err != nil || resolved.ProviderVMID != targetVMB { + t.Fatalf("new enrollment: %+v, %v", resolved, err) + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + if _, err := service.ResolveGuestTarget(ctx, "replacement"); !errors.Is(err, context.Canceled) { + t.Fatalf("canceled resolution: %v", err) + } +} diff --git a/internal/approval/consumption_time_test.go b/internal/approval/consumption_time_test.go new file mode 100644 index 0000000..2895a5c --- /dev/null +++ b/internal/approval/consumption_time_test.go @@ -0,0 +1,47 @@ +package approval_test + +import ( + "errors" + "path/filepath" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/approval" + "github.com/Horcag/agent-machine-control/internal/domain" +) + +func TestConsumptionRequiresActiveTimeAndPersistsValidRecord(t *testing.T) { + now := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC) + for _, tc := range []struct { + name string + at time.Time + want error + }{ + {"before issuance", now.Add(-time.Nanosecond), domain.ErrApprovalNotYetValid}, + {"at expiry", now.Add(time.Minute), domain.ErrApprovalExpired}, + {"after expiry", now.Add(2 * time.Minute), domain.ErrApprovalExpired}, + {"at issuance", now, nil}, + } { + t.Run(tc.name, func(t *testing.T) { + directory := t.TempDir() + store := approval.NewStore(directory) + issued := domain.Approval{ID: "app-consumption-time", Actor: "operator:synthetic", Target: "11111111-1111-4111-8111-111111111111", AuthorizedClass: domain.ClassDestructivePrivileged, Fingerprint: "sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", IdempotencyKey: "consumption-time", IssuedAt: now, ExpiresAt: now.Add(time.Minute)} + if err := store.Issue(issued); err != nil { + t.Fatal(err) + } + if err := store.MarkConsumed(issued, tc.at); !errors.Is(err, tc.want) { + t.Fatalf("consumption error=%v, want %v", err, tc.want) + } + consumed, err := store.IsConsumed(string(issued.ID)) + if err != nil || consumed != (tc.want == nil) { + t.Fatalf("consumption state=%v err=%v", consumed, err) + } + if tc.want == nil { + loaded, err := approval.LoadFromFile(filepath.Join(directory, string(issued.ID)+".json")) + if err != nil || !loaded.Consumed || !loaded.ConsumedAt.Equal(tc.at) { + t.Fatalf("invalid durable consumption=%+v err=%v", loaded, err) + } + } + }) + } +} diff --git a/internal/approval/store.go b/internal/approval/store.go index 1d7ea15..e3320b9 100644 --- a/internal/approval/store.go +++ b/internal/approval/store.go @@ -198,6 +198,9 @@ func (s *Store) MarkConsumedContext(ctx context.Context, a domain.Approval, cons if err := s.validateIssuedLocked(ctx, a); err != nil { return err } + if err := a.IsActive(consumedAt); err != nil { + return err + } consumed := a.Clone() consumed.Consumed = true diff --git a/internal/bootstrap/adapter.go b/internal/bootstrap/adapter.go index e5732a0..09e74cb 100644 --- a/internal/bootstrap/adapter.go +++ b/internal/bootstrap/adapter.go @@ -339,7 +339,7 @@ func buildSpec(host hostContext, identity app.BootstrapIdentity, distro, linuxUs Distro: distro, LinuxUser: linuxUser, StateDir: stateRoot, WrapperPath: wrapperPath, MetadataPath: metadataPath, BinaryPath: binaryPath, BinarySHA256: binaryHash, - WSLExecutable: host.WSLExecutable, ListenAddress: "127.0.0.1:0", + WSLExecutable: host.WSLExecutable, } wrapper, err := wrapperBytes(spec) if err != nil { @@ -366,6 +366,10 @@ func wrapperBytes(spec app.BootstrapSpec) ([]byte, error) { return nil, fmt.Errorf("%w: unsafe %s", app.ErrBootstrapUnsupported, label) } } + listenArguments := "" + if spec.ListenAddress != "" { + listenArguments = fmt.Sprintf(" '--listen',\n '%s',\n", quoteWindowsArgument(spec.ListenAddress)) + } launcher := fmt.Sprintf(`$ErrorActionPreference = 'Stop' $arguments = @( '-d', @@ -377,9 +381,7 @@ $arguments = @( 'run', '--state-dir', '%s', - '--listen', - '%s', - '--json' +%s '--json' ) $start = [Diagnostics.ProcessStartInfo]::new() $start.FileName = '%s' @@ -389,7 +391,7 @@ $start.CreateNoWindow = $true $child = [Diagnostics.Process]::Start($start) $child.WaitForExit() exit $child.ExitCode -`, quoteWindowsArgument(spec.Distro), quoteWindowsArgument(spec.LinuxUser), quoteWindowsArgument(spec.BinaryPath), quoteWindowsArgument(spec.StateDir), quoteWindowsArgument(spec.ListenAddress), spec.WSLExecutable) +`, quoteWindowsArgument(spec.Distro), quoteWindowsArgument(spec.LinuxUser), quoteWindowsArgument(spec.BinaryPath), quoteWindowsArgument(spec.StateDir), listenArguments, spec.WSLExecutable) return []byte(launcher), nil } diff --git a/internal/bootstrap/adapter_test.go b/internal/bootstrap/adapter_test.go index fe257e0..10409fd 100644 --- a/internal/bootstrap/adapter_test.go +++ b/internal/bootstrap/adapter_test.go @@ -321,6 +321,9 @@ func TestBuildSpecProducesExactInteractiveLimitedFingerprint(t *testing.T) { if spec.MultipleInstances != "IgnoreNew" || spec.ExecutionTimeLimit != "PT0S" { t.Fatalf("unsafe lifecycle settings: %#v", spec) } + if spec.ListenAddress != "" { + t.Fatalf("bootstrap must leave listener selection automatic: %q", spec.ListenAddress) + } if err := spec.Validate(identity); err != nil { t.Fatalf("spec.Validate() error = %v", err) } diff --git a/internal/bootstrap/wrapper_listen_test.go b/internal/bootstrap/wrapper_listen_test.go new file mode 100644 index 0000000..ef2148e --- /dev/null +++ b/internal/bootstrap/wrapper_listen_test.go @@ -0,0 +1,25 @@ +package bootstrap + +import ( + "strings" + "testing" + + "github.com/Horcag/agent-machine-control/internal/app" +) + +func TestWrapperAutomaticListenerOmitsAddressArguments(t *testing.T) { + spec := app.BootstrapSpec{WSLExecutable: `C:\Windows\System32\wsl.exe`, Distro: "Synthetic-WSL", LinuxUser: "operator", BinaryPath: "/bin/amcd", StateDir: "/state"} + for _, address := range []string{"", "127.0.0.1:0"} { + spec.ListenAddress = address + body, err := wrapperBytes(spec) + if err != nil { + t.Fatal(err) + } + if strings.Contains(string(body), "'--listen'") != (address != "") { + t.Fatalf("wrong automatic/explicit listener arguments for %q", address) + } + if !strings.Contains(string(body), "'--state-dir',\n '/state',") || !strings.Contains(string(body), "'--json'") { + t.Fatal("automatic listener lost adjacent native arguments") + } + } +} diff --git a/internal/bootstrap/wrapper_test.go b/internal/bootstrap/wrapper_test.go index 681672c..508018d 100644 --- a/internal/bootstrap/wrapper_test.go +++ b/internal/bootstrap/wrapper_test.go @@ -33,8 +33,15 @@ func TestWrapperStartsConsolelessChildAndPreservesExitAndArguments(t *testing.T) return strings.TrimSpace(string(out)) } dir, binary, launcher := buildWindowsTestExecutables(t) + for _, address := range []string{"", "127.0.0.1:0"} { + t.Run("listen="+address, func(t *testing.T) { checkWrapperInvocation(t, dir, binary, launcher, address, winPath) }) + } +} + +func checkWrapperInvocation(t *testing.T, dir, binary, launcher, address string, winPath func(string) string) { + t.Helper() output := filepath.Join(dir, "child observation.json") - spec := app.BootstrapSpec{WSLExecutable: winPath(binary), Distro: "Synthetic WSL", LinuxUser: "operator", BinaryPath: "/synthetic path/amcd", StateDir: winPath(output), ListenAddress: "127.0.0.1:0"} + spec := app.BootstrapSpec{WSLExecutable: winPath(binary), Distro: "Synthetic WSL", LinuxUser: "operator", BinaryPath: "/synthetic path/amcd", StateDir: winPath(output), ListenAddress: address} wrapper, err := wrapperBytes(spec) if err != nil { t.Fatal(err) @@ -46,6 +53,7 @@ func TestWrapperStartsConsolelessChildAndPreservesExitAndArguments(t *testing.T) if _, _, err := launcherArtifact(filepath.Join(dir, "amcd"), "Synthetic-WSL"); err != nil { t.Fatal(err) } + // #nosec G204 -- launcher is built from repository-owned source in this test's private directory. command := exec.CommandContext(t.Context(), launcher, "-NoLogo", "-NoProfile", "-NonInteractive", "-WindowStyle", "Hidden", "-ExecutionPolicy", "Bypass", "-File", winPath(wrapperPath)) out, err := command.CombinedOutput() var exit *exec.ExitError @@ -66,7 +74,11 @@ func TestWrapperStartsConsolelessChildAndPreservesExitAndArguments(t *testing.T) if observation.Console != 0 { t.Fatal("launcher allocated a console for its child") } - want := []string{"-d", spec.Distro, "--user", spec.LinuxUser, "--exec", spec.BinaryPath, "run", "--state-dir", spec.StateDir, "--listen", spec.ListenAddress, "--json"} + want := []string{"-d", spec.Distro, "--user", spec.LinuxUser, "--exec", spec.BinaryPath, "run", "--state-dir", spec.StateDir} + if address != "" { + want = append(want, "--listen", address) + } + want = append(want, "--json") if !reflect.DeepEqual(observation.Args, want) { t.Fatalf("child arguments: %q, want %q", observation.Args, want) } diff --git a/internal/cli/app.go b/internal/cli/app.go index b59d155..de1b070 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -196,6 +196,11 @@ func Run(args []string, stdout, stderr io.Writer) int { approvalStore, app.WithRecoveryTargetResolver(targetService), ) + consoleAuthority := app.NewConsoleService(adapter, recoveryService, targetService, filepath.Join(sd.Root(), "console-frames"), + app.WithConsoleLabEnrollmentIdentity(func(ctx context.Context, ref domain.MachineRef) (string, error) { + return targetService.EnrollmentIdentity(ctx, string(ref)) + })) + app.WithRecoveryLabAuthority(consoleAuthority)(recoveryService) actorResolver := &actor.DefaultResolver{} actCtx, err := actorResolver.Resolve() @@ -207,7 +212,7 @@ func Run(args []string, stdout, stderr io.Writer) int { appInstance := NewApp( discoveryService, WithRecoveryService(recoveryService), - WithConsoleService(app.NewConsoleService(adapter, recoveryService, targetService, filepath.Join(sd.Root(), "console-frames"))), + WithConsoleService(consoleAuthority), WithTargetService(targetService), WithTargetCoordinator(targetCoordinator), WithActor(actCtx), @@ -344,6 +349,8 @@ func (a *App) RunWithContext(ctx context.Context, args []string, stdout, stderr stderr, ) + case "guest": + return runGuest(ctx, a.prompter, directMode, stateDir, cmdArgs, stdout, stderr) case "session": return runSession( ctx, @@ -367,6 +374,7 @@ func printUsage(w io.Writer) { fmt.Fprintln(w, "Commands:") fmt.Fprintln(w, " console screenshot|key|type|move|click|drag Capture or control the enrolled VM console") fmt.Fprintln(w, " desktop observe|action|enable|status|disable Guest windows/UIA and bounded autonomous control") + fmt.Fprintln(w, " guest prepare|put|get|exec|remove-owned|approve Stream owned guest artifacts and bounded scripts") fmt.Fprintln(w, " doctor Check Hyper-V and host readiness") fmt.Fprintln(w, " machine list List discovered virtual machines") fmt.Fprintln(w, " machine inspect Inspect virtual machine configuration and state") diff --git a/internal/cli/checkpoint_cmd.go b/internal/cli/checkpoint_cmd.go index 0247d3e..b5b015c 100644 --- a/internal/cli/checkpoint_cmd.go +++ b/internal/cli/checkpoint_cmd.go @@ -2,6 +2,7 @@ package cli import ( "context" + "errors" "flag" "fmt" "io" @@ -13,6 +14,7 @@ import ( "github.com/Horcag/agent-machine-control/internal/app" "github.com/Horcag/agent-machine-control/internal/daemon" "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/policy" "github.com/Horcag/agent-machine-control/internal/receipt" ) @@ -174,6 +176,19 @@ func runCheckpointCreate( if !directMode { return executeDaemonCheckpointCreate(ctx, stateDir, targetID, *name, common, stdout, stderr) } + return executeDirectCheckpointCreate(ctx, recoverySvc, actor, prompter, nowFn, targetID, *name, common, stdout, stderr) +} + +func executeDirectCheckpointCreate( + ctx context.Context, + recoverySvc *app.RecoveryService, + actor domain.ActorContext, + prompter Prompter, + nowFn func() time.Time, + targetID, name string, + common *CommonFlags, + stdout, stderr io.Writer, +) int { if rejectDirectApprovalReference(common, stderr, "checkpoint create") { return ExitUsage } @@ -182,35 +197,27 @@ func runCheckpointCreate( return mapMutationError(err, stderr, "checkpoint create") } - appr := common.Approval - var reqDeadline time.Time - if appr == nil && prompter != nil { + req := app.MutationRequest{ + TargetID: string(canonicalTarget), Actor: actor, Reason: common.Reason, + IdempotencyKey: common.IdempotencyKey, Timeout: common.Timeout, Approval: common.Approval, + } + rcpt, snap, err := recoverySvc.CreateCheckpoint(ctx, req, name) + var denied *app.PolicyDeniedError + if errors.As(err, &denied) && denied.Reason == policy.DenialApprovalRequired && common.Approval == nil && prompter != nil { promptMsg := fmt.Sprintf("Destructive operation checkpoint.create on %s requires confirmation", canonicalTarget) - params := map[string]any{"name": *name} - promptedAppr, dl, ok := promptForApproval(prompter, nowFn, actor, string(canonicalTarget), "checkpoint.create", domain.CapabilityCheckpointCreate, domain.ClassDestructivePrivileged, common.Reason, common.IdempotencyKey, common.Timeout, params, promptMsg) + params := map[string]any{"name": name} + req.IdempotencyKey = domain.DeriveApprovalIdempotencyKey(common.IdempotencyKey) + approval, deadline, ok := promptForApproval(prompter, nowFn, actor, string(canonicalTarget), "checkpoint.create", domain.CapabilityCheckpointCreate, domain.ClassDestructivePrivileged, common.Reason, req.IdempotencyKey, common.Timeout, params, promptMsg) if !ok { fmt.Fprintln(stderr, "amc checkpoint create: operation aborted by operator") return ExitDenied } - appr = promptedAppr - if err := recoverySvc.IssueApproval(ctx, *appr); err != nil { - fmt.Fprintln(stderr, "amc checkpoint create: failed to issue server approval") - return ExitBackendUnavailable + if err := recoverySvc.IssueApproval(ctx, *approval); err != nil { + return mapMutationError(err, stderr, "checkpoint create") } - reqDeadline = dl + req.Approval, req.Deadline = approval, deadline + rcpt, snap, err = recoverySvc.CreateCheckpoint(ctx, req, name) } - - req := app.MutationRequest{ - TargetID: string(canonicalTarget), - Actor: actor, - Reason: common.Reason, - IdempotencyKey: common.IdempotencyKey, - Timeout: common.Timeout, - Deadline: reqDeadline, - Approval: appr, - } - - rcpt, snap, err := recoverySvc.CreateCheckpoint(ctx, req, *name) if err != nil { return mapMutationError(err, stderr, "checkpoint create") } diff --git a/internal/cli/checkpoint_restore_cmd.go b/internal/cli/checkpoint_restore_cmd.go index 18a0544..8a32f7a 100644 --- a/internal/cli/checkpoint_restore_cmd.go +++ b/internal/cli/checkpoint_restore_cmd.go @@ -12,6 +12,7 @@ import ( "github.com/Horcag/agent-machine-control/internal/daemon" "github.com/Horcag/agent-machine-control/internal/domain" "github.com/Horcag/agent-machine-control/internal/lease" + "github.com/Horcag/agent-machine-control/internal/policy" "github.com/Horcag/agent-machine-control/internal/receipt" ) @@ -94,6 +95,19 @@ func runCheckpointRestore( }, ) } + return executeDirectCheckpointRestore(ctx, recoverySvc, actor, prompter, nowFn, targetID, checkpointID, common, stdout, stderr) +} + +func executeDirectCheckpointRestore( + ctx context.Context, + recoverySvc *app.RecoveryService, + actor domain.ActorContext, + prompter Prompter, + nowFn func() time.Time, + targetID, checkpointID string, + common *CommonFlags, + stdout, stderr io.Writer, +) int { if rejectDirectApprovalReference(common, stderr, "checkpoint restore") { return ExitUsage } @@ -102,22 +116,22 @@ func runCheckpointRestore( return mapMutationError(err, stderr, "checkpoint restore") } - appr, reqDeadline, approvalExit := prepareCheckpointRestoreApproval(ctx, recoverySvc, actor, prompter, nowFn, string(canonicalTarget), checkpointID, common, stderr) - if approvalExit != ExitSuccess { - return approvalExit - } - req := app.MutationRequest{ - TargetID: string(canonicalTarget), - Actor: actor, - Reason: common.Reason, - IdempotencyKey: common.IdempotencyKey, - Timeout: common.Timeout, - Deadline: reqDeadline, - Approval: appr, + TargetID: string(canonicalTarget), Actor: actor, Reason: common.Reason, + IdempotencyKey: common.IdempotencyKey, Timeout: common.Timeout, Approval: common.Approval, } - rcpt, obs, err := recoverySvc.RestoreCheckpoint(ctx, req, checkpointID) + var denied *app.PolicyDeniedError + if errors.As(err, &denied) && denied.Reason == policy.DenialApprovalRequired && common.Approval == nil && prompter != nil { + promptFlags := *common + promptFlags.IdempotencyKey = domain.DeriveApprovalIdempotencyKey(common.IdempotencyKey) + approval, deadline, code := prepareCheckpointRestoreApproval(ctx, recoverySvc, actor, prompter, nowFn, string(canonicalTarget), checkpointID, &promptFlags, stderr) + if code != ExitSuccess { + return code + } + req.Approval, req.Deadline, req.IdempotencyKey = approval, deadline, promptFlags.IdempotencyKey + rcpt, obs, err = recoverySvc.RestoreCheckpoint(ctx, req, checkpointID) + } if err != nil { return mapMutationError(err, stderr, "checkpoint restore") } diff --git a/internal/cli/console_cmd.go b/internal/cli/console_cmd.go index a1f3186..b2b4c05 100644 --- a/internal/cli/console_cmd.go +++ b/internal/cli/console_cmd.go @@ -34,7 +34,7 @@ func consoleArgs(args []string) ([]string, []string) { } flags = append(flags, arg) name := strings.TrimLeft(arg, "-") - if !strings.Contains(name, "=") && name != "json" && name != "async" && name != "for-mcp" && name != "acknowledge-external-effects" && i+1 < len(args) { + if !strings.Contains(name, "=") && name != "json" && name != "async" && name != "persistent-vm" && name != "for-mcp" && name != "acknowledge-external-effects" && i+1 < len(args) { flags = append(flags, args[i+1]) i++ } diff --git a/internal/cli/desktop_cmd.go b/internal/cli/desktop_cmd.go index c540dba..a58f355 100644 --- a/internal/cli/desktop_cmd.go +++ b/internal/cli/desktop_cmd.go @@ -61,7 +61,7 @@ func readDesktopRequestFile(path string, out *domain.DesktopRequest) error { type desktopFlags struct { pos []string target, reason, key, grant, requestFile, deadline string - forMCP, ack bool + forMCP, ack, persistent bool validFor time.Duration } @@ -75,6 +75,7 @@ func parseDesktopFlags(args []string, stderr io.Writer) (desktopFlags, bool) { grant := fs.String("lab-grant-id", "", "active operator-issued grant") requestFile := fs.String("request-file", "", "JSON DesktopRequest file") forMCP := fs.Bool("for-mcp", false, "grant agent:mcp-local instead of self") + persistent := fs.Bool("persistent-vm", false, "authorize this enrolled VM until revoked") ack := fs.Bool("acknowledge-external-effects", false, "acknowledge guest external effects are not rolled back") validFor := fs.Duration("valid-for", time.Hour, "grant lifetime (up to 8h)") deadline := fs.String("deadline", "", "exact operation deadline") @@ -82,7 +83,7 @@ func parseDesktopFlags(args []string, stderr io.Writer) (desktopFlags, bool) { if fs.Parse(flags) != nil || len(pos) > 1 { return desktopFlags{}, false } - return desktopFlags{pos: pos, target: *target, reason: *reason, key: *key, grant: *grant, requestFile: *requestFile, forMCP: *forMCP, ack: *ack, validFor: *validFor, deadline: *deadline}, true + return desktopFlags{pos: pos, target: *target, reason: *reason, key: *key, grant: *grant, requestFile: *requestFile, forMCP: *forMCP, ack: *ack, persistent: *persistent, validFor: *validFor, deadline: *deadline}, true } func (a *App) observeDesktop(ctx context.Context, cl *client.Client, target string, pos []string) (app.DesktopActionResult, error) { @@ -123,12 +124,20 @@ func (a *App) dispatchDesktopCommand(ctx context.Context, cl *client.Client, com if *forMCP { beneficiary = "agent:mcp-local" } - out, err = cl.IssueConsoleLabGrant(ctx, app.ConsoleLabGrantIssueRequest{Target: *target, Reason: *reason, IdempotencyKey: *key, Beneficiary: beneficiary, ValidForMillis: validFor.Milliseconds(), AcknowledgeExternalEffects: *ack}) + mode := "" + if options.persistent { + mode = app.ConsoleLabModePersistentVM + } + out, err = cl.IssueConsoleLabGrant(ctx, app.ConsoleLabGrantIssueRequest{Mode: mode, Target: *target, Reason: *reason, IdempotencyKey: *key, Beneficiary: beneficiary, ValidForMillis: validFor.Milliseconds(), AcknowledgeExternalEffects: *ack}) case "status": if len(pos) == 1 { *grant = pos[0] } - out, err = cl.ConsoleLabGrantStatus(ctx, *grant) + if *grant == "" { + out, err = cl.ActiveConsoleLabGrant(ctx, *target) + } else { + out, err = cl.ConsoleLabGrantStatus(ctx, *grant) + } case "disable": if len(pos) == 1 { *grant = pos[0] diff --git a/internal/cli/desktop_cmd_test.go b/internal/cli/desktop_cmd_test.go index c17b475..278488f 100644 --- a/internal/cli/desktop_cmd_test.go +++ b/internal/cli/desktop_cmd_test.go @@ -72,12 +72,12 @@ func assertDesktopCLIRequest(t *testing.T, r *http.Request, route string, expect func desktopCLIResponse(name string) any { switch name { - case "enable": + case "enable", "persistent": return struct { Grant app.ConsoleLabGrant `json:"grant"` Receipt domain.Receipt `json:"receipt"` }{Grant: app.ConsoleLabGrant{GrantID: "synthetic-receipt"}} - case "status": + case "status", "active": return app.ConsoleLabGrantStatus{Grant: app.ConsoleLabGrant{GrantID: "synthetic-receipt"}, State: "active"} case "disable": return domain.Receipt{ReceiptID: "synthetic-receipt"} @@ -103,6 +103,8 @@ func TestDesktopCLIRequestsAndFlags(t *testing.T) { want any }{ {"enable", "/v1/desktop/lab/issue", []string{"enable", "--target", "default", "--reason", "synthetic grant", "--idempotency-key", "issue-key", "--for-mcp", "--valid-for", "2m", "--acknowledge-external-effects"}, app.ConsoleLabGrantIssueRequest{Target: "default", Reason: "synthetic grant", IdempotencyKey: "issue-key", Beneficiary: "agent:mcp-local", ValidForMillis: 120000, AcknowledgeExternalEffects: true}}, + {"persistent", "/v1/desktop/lab/issue", []string{"enable", "--persistent-vm", "--for-mcp", "--acknowledge-external-effects", "--reason", "persistent fixture", "--idempotency-key", "persistent-key"}, app.ConsoleLabGrantIssueRequest{Mode: app.ConsoleLabModePersistentVM, Reason: "persistent fixture", IdempotencyKey: "persistent-key", Beneficiary: "agent:mcp-local", ValidForMillis: 3600000, AcknowledgeExternalEffects: true}}, + {"active", "/v1/desktop/lab/active", []string{"status"}, map[string]string{}}, {"status", "/v1/desktop/lab/status", []string{"status", "own-grant"}, map[string]string{"grant_id": "own-grant"}}, {"disable", "/v1/desktop/lab/revoke", []string{"disable", "own-grant", "--reason", "synthetic revoke", "--idempotency-key", "revoke-key", "--deadline", deadline}, app.ConsoleLabGrantRevokeRequest{GrantID: "own-grant", Reason: "synthetic revoke", IdempotencyKey: "revoke-key", Deadline: deadline}}, {"action", "/v1/desktop/action", []string{"action", "--request-file", path, "--target", "default", "--reason", "synthetic action", "--idempotency-key", "action-key", "--lab-grant-id", "own-grant"}, app.DesktopActionRequest{Target: "default", Request: request, Reason: "synthetic action", IdempotencyKey: "action-key", LabGrantID: "own-grant"}}, diff --git a/internal/cli/guest_cmd.go b/internal/cli/guest_cmd.go new file mode 100644 index 0000000..c8167a3 --- /dev/null +++ b/internal/cli/guest_cmd.go @@ -0,0 +1,180 @@ +package cli + +import ( + "context" + "encoding/json" + "errors" + "flag" + "fmt" + "io" + "strings" + "time" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/client" + "github.com/Horcag/agent-machine-control/internal/daemon" + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +func runGuest(ctx context.Context, prompter Prompter, direct bool, state string, args []string, stdout, stderr io.Writer) int { + if direct { + fmt.Fprintln(stderr, "amc: guest commands require amcd; direct recovery remains independent") + return ExitBackendUnavailable + } + if len(args) == 0 { + fmt.Fprintln(stderr, "Usage: amc guest [approve] --target --reason --idempotency-key --deadline [--source ] [--destination ] [--artifact-ids ] [--max-output ]") + return ExitUsage + } + approve := args[0] == "approve" + if approve { + args = args[1:] + if len(args) == 0 { + return ExitUsage + } + } + operation := args[0] + switch operation { + case "prepare", "put", "get", "exec", "remove-owned": + default: + return ExitUsage + } + flags, err := parseGuestCLI(operation, args[1:], approve, stderr) + if err != nil { + fmt.Fprintln(stderr, err) + return ExitUsage + } + p := flags.params + if !approve { + var cancel context.CancelFunc + ctx, cancel = context.WithDeadline(ctx, p.Deadline) + defer cancel() + } + sourcePath, destination, validity := flags.source, flags.destination, flags.validity + cl, err := client.Discover(state, client.TokenTypeOperator) + if err != nil { + return mapClientError(err, stderr, "guest") + } + var input stream.ContextReader + source, err := client.OpenGuestPayload(ctx, operation, sourcePath) + if err != nil { + fmt.Fprintln(stderr, err) + return ExitUsage + } + if source != nil { + defer source.Close() + p.Size, p.SHA256 = source.Size, source.SHA256 + input = source.Input() + } + if approve { + return approveGuestCLI(ctx, cl, prompter, p, validity, flags.beneficiary, stdout, stderr) + } + result, err := executeGuestCLI(ctx, cl, p, input, destination) + if source != nil { + err = errors.Join(err, source.Verify()) + } + if err != nil { + return mapClientError(err, stderr, "guest") + } + return finishGuestCLI(stdout, p.Operation, result) + +} + +type guestCLIFlags struct { + params app.GuestOperationParams + source, destination, validity string + beneficiary string +} + +func parseGuestCLI(operation string, args []string, approve bool, stderr io.Writer) (guestCLIFlags, error) { + fs := flag.NewFlagSet("guest "+operation, flag.ContinueOnError) + fs.SetOutput(stderr) + var p app.GuestOperationParams + var sourcePath, destination, ids, deadline, validity, beneficiary string + p.Operation = operation + fs.StringVar(&p.Target, "target", "", "Enrolled target reference") + fs.StringVar(&p.Reason, "reason", "", "Exact reason") + fs.StringVar(&p.IdempotencyKey, "idempotency-key", "", "Exact operation key") + fs.StringVar(&p.ApprovalID, "approval-id", "", "One-use server approval") + fs.StringVar(&deadline, "deadline", "", "Exact RFC3339Nano deadline") + fs.StringVar(&sourcePath, "source", "", "Local payload file") + fs.StringVar(&destination, "destination", "", "New local output file") + fs.StringVar(&ids, "artifact-ids", "", "Comma-separated opaque ownership references") + fs.StringVar(&beneficiary, "beneficiary", "agent:mcp-local", "Approval beneficiary: self or agent:mcp-local") + fs.StringVar(&validity, "valid-for", "", "Approval window (1s-5m)") + fs.Int64Var(&p.MaxOutput, "max-output", 0, "Bounded output bytes") + jsonOutput := fs.Bool("json", false, "JSON result") + if fs.Parse(args) != nil || fs.NArg() != 0 { + return guestCLIFlags{}, fmt.Errorf("invalid guest arguments") + } + _ = jsonOutput + if ids != "" { + p.ArtifactIDs = strings.Split(ids, ",") + } + if !approve { + var err error + p.Deadline, err = time.Parse(time.RFC3339Nano, deadline) + if err != nil { + return guestCLIFlags{}, fmt.Errorf("invalid guest arguments") + } + } + if err := domain.ValidateGuestOperationShape(operation, 0, p.MaxOutput, p.ArtifactIDs); err != nil { + return guestCLIFlags{}, fmt.Errorf("invalid guest %s shape: use zero max-output for prepare, put and remove-owned; get requires one artifact, exec requires output from 1 to 16777216 bytes", operation) + } + payload := operation == "put" || operation == "exec" + output := operation == "get" || operation == "exec" + if (sourcePath != "") != payload || (!output && destination != "") || (!approve && output && destination == "") { + return guestCLIFlags{}, fmt.Errorf("invalid guest %s paths: source is required only for put/exec; new destination is required only for get/exec", operation) + } + return guestCLIFlags{p, sourcePath, destination, validity, beneficiary}, nil +} + +func executeGuestCLI(ctx context.Context, cl *client.Client, p app.GuestOperationParams, input stream.ContextReader, destination string) (app.GuestOperationResult, error) { + var err error + var result app.GuestOperationResult + if p.Operation == "get" || p.Operation == "exec" { + if destination == "" { + + return result, fmt.Errorf("invalid guest destination") + } + result, err = cl.GuestOperationToFile(ctx, p, input, destination) + } else { + if destination != "" { + return result, fmt.Errorf("invalid guest destination") + } + result, err = cl.GuestOperation(ctx, p, input, nil) + } + return result, err +} + +func approveGuestCLI(ctx context.Context, cl *client.Client, prompter Prompter, p app.GuestOperationParams, validity, beneficiary string, stdout, stderr io.Writer) int { + validFor, err := validateSessionApprovalCLIInputs(p.Reason, p.IdempotencyKey, validity) + if err != nil { + return ExitUsage + } + if prompter == nil || !prompter.PromptConfirmation(fmt.Sprintf("Issue one-use guest.%s approval for %s on %s?", p.Operation, beneficiary, p.Target)) { + return ExitDenied + } + grant, err := cl.IssueGuestApproval(ctx, daemon.GuestApprovalRequest{Params: p, ValidForMillis: validFor.Milliseconds(), Beneficiary: beneficiary}) + if err != nil { + return mapClientError(err, stderr, "guest approve") + } + if json.NewEncoder(stdout).Encode(grant) != nil { + return ExitBackendUnavailable + } + return ExitSuccess + +} + +func finishGuestCLI(stdout io.Writer, operation string, result app.GuestOperationResult) int { + if json.NewEncoder(stdout).Encode(result) != nil { + return ExitBackendUnavailable + } + if operation != "exec" || result.ExitCode == 0 { + return ExitSuccess + } + if result.ExitCode > 0 && result.ExitCode <= 255 { + return result.ExitCode + } + return 1 +} diff --git a/internal/cli/guest_cmd_test.go b/internal/cli/guest_cmd_test.go new file mode 100644 index 0000000..0a14b83 --- /dev/null +++ b/internal/cli/guest_cmd_test.go @@ -0,0 +1,201 @@ +package cli + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "encoding/json" + "fmt" + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/auth" + "github.com/Horcag/agent-machine-control/internal/client" + "github.com/Horcag/agent-machine-control/internal/daemon" + "github.com/Horcag/agent-machine-control/internal/statedir" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "runtime" + "strings" + "testing" + "time" +) + +func TestGuestDirectRecoveryIndependent(t *testing.T) { + var out, err bytes.Buffer + code := runGuest(context.Background(), nil, true, "missing", []string{"prepare"}, &out, &err) + if code != ExitBackendUnavailable || out.Len() != 0 { + t.Fatalf("code=%d out=%s err=%s", code, out.String(), err.String()) + } +} +func TestGuestRejectsUnknownOperationBeforeDiscovery(t *testing.T) { + var out, err bytes.Buffer + code := runGuest(context.Background(), nil, false, "missing", []string{"adopt-arbitrary-root"}, &out, &err) + if code != ExitUsage { + t.Fatalf("code=%d err=%s", code, err.String()) + } +} + +func TestGuestExecExitCodePreservesVerifiedResult(t *testing.T) { + for _, code := range []int{0, 37, -1, 256} { + var out bytes.Buffer + result := app.GuestOperationResult{ExitCode: code} + got := finishGuestCLI(&out, "exec", result) + want := code + if code < 0 || code > 255 { + want = 1 + } + if got != want { + t.Fatalf("native code=%d CLI code=%d want=%d", code, got, want) + } + var serialized app.GuestOperationResult + if json.Unmarshal(out.Bytes(), &serialized) != nil || serialized.ExitCode != code { + t.Fatal("verified execution result was relabeled") + } + } +} + +func TestGuestCLIDiscoveryStreamsExactAuthorityAndMapsFailure(t *testing.T) { + var captured []app.GuestOperationParams + server := httptest.NewServer(guestCLIFailureHandler(t, &captured)) + defer server.Close() + state := guestCLIState(t, server.URL) + directory := t.TempDir() + source := filepath.Join(directory, "script.ps1") + if err := os.WriteFile(source, []byte("Write-Output 'synthetic'"), 0600); err != nil { + t.Fatal(err) + } + for _, operation := range []string{"prepare", "put", "get", "exec", "remove-owned"} { + args := guestCLICaseArguments(operation, source, directory) + var out, stderr bytes.Buffer + if code := runGuest(t.Context(), nil, false, state, args, &out, &stderr); code == ExitSuccess || out.Len() != 0 { + t.Fatalf("%s returned success or output after failure: %d %s", operation, code, out.String()) + } + } + expectedDispatches := 5 + if runtime.GOOS != "linux" && runtime.GOOS != "windows" { + expectedDispatches = 3 + } + if len(captured) != expectedDispatches { + t.Fatalf("whole CLI dispatches=%d", len(captured)) + } + entries, err := os.ReadDir(directory) + if err != nil || len(entries) != 1 { + t.Fatalf("failed downloads leaked outputs or stages: %v %v", entries, err) + } +} +func guestCLICaseArguments(operation, source, directory string) []string { + deadline := time.Now().Add(time.Minute).Format(time.RFC3339Nano) + args := []string{operation, "--target", "synthetic-target", "--reason", "contract", "--approval-id", "synthetic-grant", "--idempotency-key", "request-" + operation, "--deadline", deadline} + if operation == "put" || operation == "exec" { + args = append(args, "--source", source) + } + if operation == "get" || operation == "exec" { + args = append(args, "--destination", filepath.Join(directory, operation+".bin"), "--max-output", "64") + } + if operation == "get" || operation == "remove-owned" { + args = append(args, "--artifact-ids", strings.Repeat("a", 32)) + } + return args +} + +func guestCLIFailureHandler(t *testing.T, captured *[]app.GuestOperationParams) http.HandlerFunc { + t.Helper() + return func(w http.ResponseWriter, r *http.Request) { + p, _, err := daemon.DecodeGuestMetadata(r.Body) + if err != nil { + t.Error(err) + return + } + data, err := io.ReadAll(r.Body) + if err != nil { + t.Error(err) + return + } + if p.Target != "synthetic-target" || p.Reason != "contract" || p.ApprovalID != "synthetic-grant" || strings.Contains(fmt.Sprintf("%+v", p), "script.ps1") { + t.Error("exact authority or filename privacy violated") + } + if p.Operation == "put" || p.Operation == "exec" { + sum := sha256.Sum256(data) + if p.Size != int64(len(data)) || p.SHA256 != hex.EncodeToString(sum[:]) { + t.Error("source binding lost") + } + } + *captured = append(*captured, p) + w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Trailer", daemon.GuestResultTrailer) + w.WriteHeader(200) + data, _ = json.Marshal(daemon.GuestFinal{Error: "synthetic denial"}) + w.Header().Set(daemon.GuestResultTrailer, base64.RawStdEncoding.EncodeToString(data)) + } +} + +func guestCLIState(t *testing.T, endpoint string) string { + t.Helper() + sd, err := statedir.Resolve(filepath.Join(t.TempDir(), "state")) + if err != nil { + t.Fatal(err) + } + if err = sd.EnsureDirs(); err != nil { + t.Fatal(err) + } + if _, err = auth.LoadOrCreate(sd.AuthDir(), auth.WithPrincipalResolver(func() (string, error) { return "operator:synthetic", nil })); err != nil { + t.Fatal(err) + } + if err = daemon.WriteEndpointFile(sd.DaemonDir(), daemon.EndpointRecord{SchemaVersion: "1", PID: os.Getpid(), RuntimeID: "synthetic-runtime", StartedAt: time.Now(), Endpoint: endpoint}); err != nil { + t.Fatal(err) + } + return sd.Root() +} + +type guestConfirmPrompt bool + +func (p guestConfirmPrompt) PromptConfirmation(string) bool { return bool(p) } +func TestGuestCLIApprovalConfirmationAndExactBeneficiary(t *testing.T) { + calls := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls++ + if r.URL.Path != "/v1/guest/approvals" { + t.Error("wrong approval route") + } + var req daemon.GuestApprovalRequest + if json.NewDecoder(r.Body).Decode(&req) != nil || req.Beneficiary != "self" || req.ValidForMillis != 30000 || req.Params.Operation != "prepare" || req.Params.Reason != "contract" || req.Params.IdempotencyKey != "request-approval" { + t.Error("approval arguments changed") + } + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(daemon.GuestApprovalResponse{}); err != nil { + t.Error(err) + } + })) + defer server.Close() + state := guestCLIState(t, server.URL) + args := []string{"approve", "prepare", "--target", "synthetic-target", "--reason", "contract", "--idempotency-key", "request-approval", "--valid-for", "30s", "--beneficiary", "self"} + var out, stderr bytes.Buffer + if code := runGuest(t.Context(), guestConfirmPrompt(false), false, state, args, &out, &stderr); code != ExitDenied || calls != 0 { + t.Fatalf("confirmation bypass: %d %d", code, calls) + } + if code := runGuest(t.Context(), guestConfirmPrompt(true), false, state, args, &out, &stderr); code != ExitSuccess || calls != 1 { + t.Fatalf("confirmed approval: %d %d %s", code, calls, stderr.String()) + } +} +func TestGuestCLIInvalidContracts(t *testing.T) { + for _, args := range [][]string{nil, {"approve"}, {"prepare", "--unknown"}, {"prepare", "--deadline", "invalid"}, {"prepare", "unexpected"}} { + var out, stderr bytes.Buffer + if code := runGuest(t.Context(), nil, false, "missing", args, &out, &stderr); code != ExitUsage { + t.Fatalf("%v code=%d", args, code) + } + } + cl := client.New("http://127.0.0.1:1", "synthetic") + for _, tc := range []struct{ operation, destination string }{{"get", ""}, {"prepare", "unexpected"}} { + if _, err := executeGuestCLI(t.Context(), cl, app.GuestOperationParams{Operation: tc.operation}, nil, tc.destination); err == nil { + t.Fatal("invalid local destination accepted") + } + } + var out, stderr bytes.Buffer + if code := approveGuestCLI(t.Context(), cl, guestConfirmPrompt(true), app.GuestOperationParams{}, "invalid", "self", &out, &stderr); code != ExitUsage { + t.Fatal("invalid approval dispatched") + } +} diff --git a/internal/cli/guest_validation_test.go b/internal/cli/guest_validation_test.go new file mode 100644 index 0000000..8c4c7b8 --- /dev/null +++ b/internal/cli/guest_validation_test.go @@ -0,0 +1,63 @@ +package cli + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/daemon" +) + +func TestGuestCLIInvalidShapesRejectBeforeDiscovery(t *testing.T) { + for _, args := range [][]string{ + {"prepare", "--max-output", "64"}, {"put", "--source", "private-missing-path", "--max-output", "64"}, + {"get", "--destination", "private-output", "--max-output", "64"}, + {"exec", "--source", "private-missing-path", "--destination", "private-output", "--max-output", "16777217"}, + {"remove-owned"}, {"prepare", "--source", "private-missing-path"}, + } { + args = append(args, "--deadline", time.Now().Add(time.Minute).Format(time.RFC3339Nano)) + var out, stderr bytes.Buffer + code := runGuest(t.Context(), nil, false, "missing-state", args, &out, &stderr) + if code != ExitUsage || !strings.Contains(stderr.String(), "invalid guest") || strings.Contains(stderr.String(), "private-") || out.Len() != 0 { + t.Fatalf("code=%d stderr=%s", code, stderr.String()) + } + } +} + +func TestGuestCLITypedFailureExitAndRedaction(t *testing.T) { + for _, tc := range []struct { + category string + exit int + }{ + {"invalid_argument", ExitUsage}, {"approval_required", ExitDenied}, {"guest_workspace_unprovisioned", ExitDenied}, + {"guest_authority", ExitDenied}, {"guest_not_prepared", ExitConflict}, {"guest_ownership", ExitConflict}, + {"guest_uncertain", ExitBackendUnavailable}, + } { + t.Run(tc.category, func(t *testing.T) { + calls := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls++ + if _, _, err := daemon.DecodeGuestMetadata(r.Body); err != nil { + t.Error(err) + } + w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Trailer", daemon.GuestResultTrailer) + data, _ := json.Marshal(daemon.GuestFinal{Category: tc.category, Error: "private-secret-path-token"}) + w.Header().Set(daemon.GuestResultTrailer, base64.RawStdEncoding.EncodeToString(data)) + })) + defer server.Close() + state := guestCLIState(t, server.URL) + var out, stderr bytes.Buffer + args := []string{"prepare", "--deadline", time.Now().Add(time.Minute).Format(time.RFC3339Nano)} + code := runGuest(t.Context(), nil, false, state, args, &out, &stderr) + if code != tc.exit || calls != 1 || out.Len() != 0 || strings.Contains(stderr.String(), "private") { + t.Fatalf("code=%d calls=%d stderr=%s", code, calls, stderr.String()) + } + }) + } +} diff --git a/internal/cli/session.go b/internal/cli/session.go index 8ae3e03..9b24598 100644 --- a/internal/cli/session.go +++ b/internal/cli/session.go @@ -73,7 +73,7 @@ func printSessionUsage(w io.Writer) { fmt.Fprintln(w, "Subcommands:") fmt.Fprintln(w, " open [target] Open a new persistent SSH terminal session on the enrolled target") fmt.Fprintln(w, " read Read output chunks from a session") - fmt.Fprintln(w, " write Write input to a session") + fmt.Fprintln(w, " write Type terminal input or a short command (up to 64 KiB)") fmt.Fprintln(w, " control Send a control key (ctrl-c, ctrl-d, enter, etc.)") fmt.Fprintln(w, " wait Wait for output settle or regex match") fmt.Fprintln(w, " list List active and recent sessions") @@ -81,6 +81,9 @@ func printSessionUsage(w io.Writer) { fmt.Fprintln(w, " close Close a persistent session") fmt.Fprintln(w, " approve Issue one exact, short-lived approval for agent:mcp-local") fmt.Fprintln(w, " attach Interactively attach to a session") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "Files: amc guest prepare, then amc guest put --source (binary streaming, up to 1 TiB).") + fmt.Fprintln(w, "Local PowerShell scripts: amc guest exec --source (up to 16 MiB).") } func runSessionOpen(ctx context.Context, cl *client.Client, args []string, stdout, stderr io.Writer) int { diff --git a/internal/client/client.go b/internal/client/client.go index a53b818..a5984d6 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "net/http" + "os" "strings" "time" @@ -38,9 +39,10 @@ func WithHTTPClient(c *http.Client) Option { // Client provides typed HTTP communication with the amcd daemon. type Client struct { - endpoint string - token string - httpClient *http.Client + guestBeforePublication func(*os.File) error + endpoint string + token string + httpClient *http.Client } // New creates a new Client configured with the target endpoint and bearer token. @@ -110,6 +112,10 @@ func (c *Client) doRequest(ctx context.Context, method, path string, body any, o } req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token)) + switch path { + case "/v1/desktop/lab/issue", "/v1/desktop/lab/status", "/v1/desktop/lab/active": + req.Header.Set(daemon.LabGrantModeHeader, "1") + } if body != nil { req.Header.Set("Content-Type", "application/json") } diff --git a/internal/client/desktop_test.go b/internal/client/desktop_test.go index 30ab1b3..cde19df 100644 --- a/internal/client/desktop_test.go +++ b/internal/client/desktop_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/daemon" "github.com/Horcag/agent-machine-control/internal/domain" ) @@ -56,6 +57,11 @@ func TestDesktopClientAuthenticatedContracts(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { calls++ assertDesktopClientRequest(t, r, tc.path, tc.request) + if tc.name == "issue" || tc.name == "status" || tc.name == "active" { + if r.Header.Get(daemon.LabGrantModeHeader) != "1" { + t.Error("lab response extension was not negotiated") + } + } if err := json.NewEncoder(w).Encode(tc.response); err != nil { t.Error(err) } diff --git a/internal/client/guest.go b/internal/client/guest.go new file mode 100644 index 0000000..2acf68c --- /dev/null +++ b/internal/client/guest.go @@ -0,0 +1,186 @@ +package client + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/base64" + "encoding/binary" + "encoding/hex" + "encoding/json" + "errors" + "io" + "net/http" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/daemon" + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +// GuestOperation never retries. Output must be an owned unpublished sink: a +// response is accepted only after EOF, the final receipt, size and digest agree. +func (c *Client) GuestOperation(ctx context.Context, p app.GuestOperationParams, input stream.ContextReader, output stream.ContextWriter) (app.GuestOperationResult, error) { + var zero app.GuestOperationResult + prefix, err := encodeGuestMetadata(p) + if err != nil { + return zero, err + } + ctx, cancel := context.WithDeadline(ctx, p.Deadline) + defer cancel() + var payload io.Reader = bytes.NewReader(nil) + if p.Size > 0 { + if input == nil { + return zero, ErrInvalidArgument + } + payload = guestRequestReader{ctx, input} + } + req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.endpoint+"/v1/guest/operations", io.MultiReader(bytes.NewReader(prefix), payload)) + if err != nil { + return zero, err + } + req.ContentLength = int64(len(prefix)) + p.Size + req.Header.Set("Authorization", "Bearer "+c.token) + req.Header.Set("Content-Type", daemon.GuestContentType) + req.Header.Set(daemon.GuestErrorProtocolHeader, daemon.GuestErrorProtocolVersion) + resp, err := c.guestHTTPClient().Do(req) + if err != nil { + return zero, app.ErrGuestUncertain + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return zero, mapHTTPError(resp) + } + if resp.Header.Get("Content-Type") != "application/octet-stream" { + return zero, ErrMalformedResponse + } + total, digest, err := readGuestHTTPOutput(ctx, resp, cancel, p.MaxOutput, output) + if err != nil { + return zero, errors.Join(app.ErrGuestUncertain, err) + } + final, err := decodeGuestFinal(resp) + if err != nil { + return zero, errors.Join(app.ErrGuestUncertain, err) + } + if final.Error != "" || final.Category != "" { + return final.Result, guestFinalError(final.Category) + } + if err = validateGuestFinal(p, final.Result, total, digest); err != nil { + return final.Result, err + } + + return final.Result, nil +} + +type guestRequestReader struct { + ctx context.Context + source stream.ContextReader +} + +func (r guestRequestReader) Read(p []byte) (int, error) { return r.source.ReadContext(r.ctx, p) } +func (c *Client) IssueGuestApproval(ctx context.Context, req daemon.GuestApprovalRequest) (*daemon.GuestApprovalResponse, error) { + var result daemon.GuestApprovalResponse + bounded := *c + bounded.httpClient = c.guestHTTPClient() + err := bounded.doRequest(ctx, http.MethodPost, "/v1/guest/approvals", req, &result) + return &result, err +} + +func readGuestHTTPOutput(ctx context.Context, resp *http.Response, cancel context.CancelFunc, limit int64, output stream.ContextWriter) (int64, string, error) { + source := stream.HTTPInput(resp.Body, cancel) + digest := sha256.New() + buf := make([]byte, stream.BufferSize) + var total int64 + for { + n, readErr := source.ReadContext(ctx, buf) + if n > 0 { + total += int64(n) + if total > limit { + return total, "", ErrMalformedResponse + } + _, _ = digest.Write(buf[:n]) + if output == nil { + return total, "", ErrInvalidArgument + } + written, err := output.WriteContext(ctx, buf[:n]) + if err != nil { + return total, "", err + } + if written != n { + return total, "", io.ErrShortWrite + } + } + if readErr == io.EOF { + break + } + if readErr != nil { + return total, "", readErr + } + } + return total, hex.EncodeToString(digest.Sum(nil)), nil +} +func validateGuestFinal(p app.GuestOperationParams, result app.GuestOperationResult, total int64, digest string) error { + expectedSize := result.Size + if p.Operation == "put" { + expectedSize = 0 + if result.Size != p.Size || result.SHA256 != p.SHA256 { + return ErrMalformedResponse + } + } + if result.CachedReceipt && (p.Operation == "get" || p.Operation == "exec") { + return ErrConflict + } + if verifyGuestReceipt(p, result.Receipt) != nil || expectedSize != total || ((p.Operation == "get" || p.Operation == "exec") && result.SHA256 != digest) { + return ErrMalformedResponse + } + return nil +} + +func decodeGuestFinal(resp *http.Response) (daemon.GuestFinal, error) { + trailer := resp.Trailer.Get(daemon.GuestResultTrailer) + if len(trailer) == 0 || len(trailer) > daemon.GuestMetadataLimit*2 { + return daemon.GuestFinal{}, ErrMalformedResponse + } + decoded, err := base64.RawStdEncoding.DecodeString(trailer) + if err != nil || len(decoded) > daemon.GuestMetadataLimit { + return daemon.GuestFinal{}, ErrMalformedResponse + } + var final daemon.GuestFinal + dec := json.NewDecoder(bytes.NewReader(decoded)) + dec.DisallowUnknownFields() + var extra any + if dec.Decode(&final) != nil || dec.Decode(&extra) != io.EOF { + return daemon.GuestFinal{}, ErrMalformedResponse + } + return final, nil +} + +func encodeGuestMetadata(p app.GuestOperationParams) ([]byte, error) { + data, err := json.Marshal(p) + if err != nil || len(data) > daemon.GuestMetadataLimit || p.Size < 0 || p.Size > stream.MaxTransferSize || p.Deadline.IsZero() { + return nil, ErrInvalidArgument + } + prefix := make([]byte, 4+len(data)) + // #nosec G115 -- metadata length was bounded to 64KiB before conversion. + binary.BigEndian.PutUint32(prefix, uint32(len(data))) + copy(prefix[4:], data) + return prefix, nil +} +func (c *Client) guestHTTPClient() *http.Client { + bounded := *c.httpClient + bounded.CheckRedirect = func(_ *http.Request, _ []*http.Request) error { return http.ErrUseLastResponse } + return &bounded +} + +func verifyGuestReceipt(p app.GuestOperationParams, receipt *domain.Receipt) error { + if receipt == nil { + return ErrMalformedResponse + } + if err := receipt.Validate(); err != nil { + return ErrMalformedResponse + } + if receipt.Outcome.Status != domain.OutcomeSuccess || receipt.OperationKind != domain.OperationKind("guest."+p.Operation) || receipt.IdempotencyKey != p.IdempotencyKey { + return ErrMalformedResponse + } + return nil +} diff --git a/internal/client/guest_errors.go b/internal/client/guest_errors.go new file mode 100644 index 0000000..6ce7db9 --- /dev/null +++ b/internal/client/guest_errors.go @@ -0,0 +1,61 @@ +package client + +import ( + "errors" + "net/http" + + "github.com/Horcag/agent-machine-control/internal/app" +) + +// Ignore trailer message text even from older servers. Categories are a bounded +// protocol vocabulary; unknown or legacy failures cannot prove a safe outcome. +func guestFinalError(category string) error { + status := http.StatusForbidden + var cause error + message := "guest operation denied" + switch category { + case "invalid_argument", "invalid_target", "target_mismatch": + status = http.StatusBadRequest + cause = ErrInvalidArgument + message = "invalid guest operation input" + case "approval_required", "approval_record_expired", "approval_record_consumed", "approval_record_mismatch", "approval_record_not_yet_valid", + "forbidden", "forbidden_operation", "missing_required_scope", "missing_sensitive_evidence_scope", "missing_required_capability", "missing_verified_rollback_point", "delegation_exceeds_authority", "access_denied": + cause = ErrDenied + case "guest_workspace_unprovisioned": + cause = errors.Join(app.ErrGuestWorkspaceUnprovisioned, ErrDenied) + message = "guest workspace requires explicit provisioning" + case "guest_authority": + cause = errors.Join(app.ErrGuestAuthority, ErrDenied) + message = "guest authority changed or unavailable" + case "guest_not_prepared": + status = http.StatusConflict + cause = errors.Join(app.ErrGuestNotPrepared, ErrConflict) + message = "guest worker requires explicit preparation" + case "guest_ownership": + status = http.StatusConflict + cause = errors.Join(app.ErrGuestOwnership, ErrConflict) + message = "guest artifact ownership or identity mismatch" + case "conflict", "target_not_enrolled": + status = http.StatusConflict + cause = ErrConflict + message = "guest operation conflicts with current state" + case "timeout": + status = http.StatusGatewayTimeout + cause = ErrTimeout + message = "guest operation deadline exceeded" + case "cancelled": + status = http.StatusRequestTimeout + message = "guest operation canceled" + case "target_unavailable", "host_unavailable", "module_missing", "backend_unavailable": + status = http.StatusServiceUnavailable + cause = ErrDaemonUnavailable + message = "guest target unavailable" + case "machine_not_found": + status = http.StatusNotFound + cause = ErrNotFound + message = "guest target not found" + default: + return app.ErrGuestUncertain + } + return errors.Join(cause, &APIError{StatusCode: status, Category: category, Message: message}) +} diff --git a/internal/client/guest_errors_test.go b/internal/client/guest_errors_test.go new file mode 100644 index 0000000..b688bc2 --- /dev/null +++ b/internal/client/guest_errors_test.go @@ -0,0 +1,75 @@ +package client + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "errors" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/daemon" +) + +func TestGuestTrailerFailuresPreserveCategoriesWithoutReplayOrSecrets(t *testing.T) { + for _, tc := range []struct { + category string + cause error + }{ + {"invalid_argument", ErrInvalidArgument}, {"approval_required", ErrDenied}, + {"guest_workspace_unprovisioned", app.ErrGuestWorkspaceUnprovisioned}, + {"guest_authority", app.ErrGuestAuthority}, {"guest_not_prepared", app.ErrGuestNotPrepared}, + {"guest_ownership", app.ErrGuestOwnership}, {"conflict", ErrConflict}, + {"timeout", ErrTimeout}, {"", app.ErrGuestUncertain}, {"private-unknown-category", app.ErrGuestUncertain}, + } { + t.Run(tc.category, func(t *testing.T) { + calls := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + calls++ + w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Trailer", daemon.GuestResultTrailer) + final, _ := json.Marshal(daemon.GuestFinal{Category: tc.category, Error: "private-secret-path-token"}) + w.Header().Set(daemon.GuestResultTrailer, base64.RawStdEncoding.EncodeToString(final)) + })) + defer server.Close() + _, err := New(server.URL, "synthetic").GuestOperation(t.Context(), app.GuestOperationParams{Operation: "prepare", Deadline: time.Now().Add(time.Minute)}, nil, nil) + if !errors.Is(err, tc.cause) || calls != 1 || strings.Contains(err.Error(), "private") { + t.Fatalf("calls=%d err=%v", calls, err) + } + if tc.cause != app.ErrGuestUncertain && errors.Is(err, app.ErrGuestUncertain) { + t.Fatal("known failure became uncertain") + } + }) + } +} + +func TestGuestMissingTrailerRemainsUncertain(t *testing.T) { + cl, calls := guestFixture(t, nil, "missing") + _, err := cl.GuestOperation(t.Context(), app.GuestOperationParams{Operation: "prepare", Deadline: time.Now().Add(time.Minute)}, nil, nil) + if !errors.Is(err, app.ErrGuestUncertain) || !errors.Is(err, ErrMalformedResponse) || *calls != 1 { + t.Fatalf("calls=%d err=%v", *calls, err) + } +} + +func TestGuestLegacyHTTPEnvelopeUsesExistingErrorMapping(t *testing.T) { + for _, tc := range []struct { + status int + category string + cause error + }{ + {400, "invalid_argument", ErrInvalidArgument}, {403, "approval_required", ErrDenied}, + } { + payload, _ := json.Marshal(daemon.ErrorEnvelope{SchemaVersion: daemon.SchemaVersion, Error: daemon.ErrorField{Category: tc.category, Message: "guest operation rejected before dispatch; inspect its category"}}) + response := &http.Response{StatusCode: tc.status, Body: io.NopCloser(bytes.NewReader(payload))} + err := mapHTTPError(response) + api, ok := errors.AsType[*APIError](err) + if !errors.Is(err, tc.cause) || !ok || api.Category != tc.category || errors.Is(err, app.ErrGuestUncertain) { + t.Fatalf("legacy error=%v", err) + } + } +} diff --git a/internal/client/guest_files.go b/internal/client/guest_files.go new file mode 100644 index 0000000..a22bc52 --- /dev/null +++ b/internal/client/guest_files.go @@ -0,0 +1,202 @@ +package client + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "fmt" + "io" + "os" + "path/filepath" + "unicode/utf8" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + "github.com/Horcag/agent-machine-control/internal/statedir" +) + +// GuestSource holds the exact opened local file through hashing and upload. +// File names never enter guest metadata. +type GuestSource struct { + file *os.File + original os.FileInfo + Size int64 + SHA256 string +} + +func OpenGuestSource(ctx context.Context, path string) (*GuestSource, error) { + return openGuestSourceBounded(ctx, path, stream.MaxTransferSize) +} +func openGuestSourceBounded(ctx context.Context, path string, maximumBytes int64) (*GuestSource, error) { + file, err := os.Open(path) + if err != nil { + return nil, err + } + source := &GuestSource{file: file} + ok := false + defer func() { + if !ok { + _ = file.Close() + } + }() + source.original, err = file.Stat() + if err != nil || !source.original.Mode().IsRegular() || source.original.Size() > maximumBytes { + return nil, fmt.Errorf("guest source must be a regular file") + } + hash := sha256.New() + reader := guestRequestReader{ctx, stream.OwnedInput(file)} + size, err := io.CopyBuffer(hash, reader, make([]byte, stream.BufferSize)) + if err != nil { + return nil, err + } + source.Size = size + source.SHA256 = hex.EncodeToString(hash.Sum(nil)) + if err = source.Verify(); err != nil { + return nil, err + } + if _, err = file.Seek(0, io.SeekStart); err != nil { + return nil, err + } + ok = true + return source, nil +} +func (s *GuestSource) Close() error { return s.file.Close() } +func (s *GuestSource) Input() stream.ContextReader { return stream.OwnedInput(s.file) } +func (s *GuestSource) Verify() error { + current, err := s.file.Stat() + if err != nil { + return err + } + if !os.SameFile(s.original, current) || current.Size() != s.original.Size() || !current.ModTime().Equal(s.original.ModTime()) || s.Size != current.Size() { + return errors.New("guest source changed during transfer") + } + return nil +} + +// GuestOperationToFile holds a private staging file through verification and +// atomically publishes that held object without replacing an existing destination. +func (c *Client) GuestOperationToFile(ctx context.Context, p app.GuestOperationParams, input stream.ContextReader, destination string) (result app.GuestOperationResult, callErr error) { + var zero app.GuestOperationResult + if p.Deadline.IsZero() { + return zero, ErrInvalidArgument + } + ctx, cancel := context.WithDeadline(ctx, p.Deadline) + defer cancel() + var err error + destination, err = guestPublicationDestination(destination) + if err != nil { + return zero, err + } + if _, err := os.Lstat(destination); err == nil { + return zero, os.ErrExist + } else if !os.IsNotExist(err) { + return zero, err + } + stage, err := newGuestStaging(ctx, filepath.Dir(destination)) + if err != nil { + return zero, err + } + defer func() { callErr = errors.Join(callErr, stage.cleanup()) }() + file := stage.file + result, err = c.GuestOperation(ctx, p, input, stream.OwnedOutput(file)) + if err != nil { + return result, err + } + if err = c.publishGuestFile(ctx, file, destination, result); err != nil { + return result, err + } + + return result, nil +} + +// ValidateScript validates bounded UTF-8 before sending any execution request. +func (s *GuestSource) ValidateScript() error { + if s.Size > stream.MaxScriptSize { + return stream.ErrRequest + } + data, err := io.ReadAll(io.LimitReader(s.file, stream.MaxScriptSize+1)) + if err != nil { + return err + } + if int64(len(data)) != s.Size || !utf8.Valid(data) { + return stream.ErrRequest + } + _, err = s.file.Seek(0, io.SeekStart) + return err +} + +// OpenGuestPayload opens only the explicit local payload accepted by an operation. +func OpenGuestPayload(ctx context.Context, operation, path string) (*GuestSource, error) { + if operation != "put" && operation != "exec" { + if path != "" { + return nil, stream.ErrRequest + } + return nil, nil + } + maximum := stream.MaxTransferSize + if operation == "exec" { + maximum = stream.MaxScriptSize + } + source, err := openGuestSourceBounded(ctx, path, maximum) + if err != nil { + return nil, err + } + if operation == "exec" { + if err = source.ValidateScript(); err != nil { + _ = source.Close() + return nil, err + } + } + return source, nil +} + +func (c *Client) publishGuestFile(ctx context.Context, file *os.File, destination string, result app.GuestOperationResult) error { + original, err := file.Stat() + if err != nil { + return err + } + if err = verifyGuestFileContent(ctx, file, result); err != nil { + return err + } + verified, err := file.Stat() + if err != nil || !os.SameFile(original, verified) || original.Size() != verified.Size() || !original.ModTime().Equal(verified.ModTime()) { + return errors.New("guest staged content changed during verification") + } + if err = ctx.Err(); err != nil { + return err + } + if c.guestBeforePublication != nil { + if err = c.guestBeforePublication(file); err != nil { + return err + } + } + if err = publishGuestFileHandle(ctx, file, destination); err != nil { + return err + } + installed, err := os.Lstat(destination) + if err != nil || !os.SameFile(original, installed) { + return errors.New("guest destination identity mismatch") + } + return statedir.SyncDir(filepath.Dir(destination)) +} + +func verifyGuestFileContent(ctx context.Context, file *os.File, result app.GuestOperationResult) error { + var err error + if err = file.Sync(); err != nil { + return err + } + if _, err = file.Seek(0, io.SeekStart); err != nil { + return err + } + hash := sha256.New() + size, err := io.CopyBuffer(hash, guestRequestReader{ctx, stream.OwnedInput(file)}, make([]byte, stream.BufferSize)) + if err != nil { + return err + } + if size != result.Size || (size > 0 && hex.EncodeToString(hash.Sum(nil)) != result.SHA256) { + return stream.ErrIntegrity + } + + return nil +} diff --git a/internal/client/guest_publication_test.go b/internal/client/guest_publication_test.go new file mode 100644 index 0000000..62ef325 --- /dev/null +++ b/internal/client/guest_publication_test.go @@ -0,0 +1,57 @@ +package client + +import ( + "bytes" + "os" + "path/filepath" + "runtime" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/app" +) + +func TestGuestPublicationUsesOriginalHeldObjectAfterStagingReplacement(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("Windows exclusive staging handles reject replacement; native platform tests verify sharing") + } + if runtime.GOOS != "linux" { + t.Skip("Unsupported hosts reject staging before dispatch; TestGuestFilePublication verifies that boundary") + } + original := []byte("verified original output") + cl, _ := guestFixture(t, original, "valid") + cl.guestBeforePublication = func(file *os.File) error { + path := file.Name() + if err := os.Rename(path, filepath.Join(filepath.Dir(path), "renamed-owned")); err != nil { + return err + } + return os.WriteFile(path, []byte("foreign replacement"), 0600) + } + directory := t.TempDir() + destination := filepath.Join(directory, "result") + _, err := cl.GuestOperationToFile(t.Context(), app.GuestOperationParams{Operation: "get", IdempotencyKey: "synthetic-request", MaxOutput: 100, Deadline: time.Now().Add(time.Second)}, nil, destination) + // The simulated foreign pending file is preserved and reported during cleanup. + if err == nil { + t.Fatal("foreign staging cleanup was silently claimed") + } + actual, readErr := os.ReadFile(destination) + if readErr != nil || !bytes.Equal(actual, original) { + t.Fatalf("publication selected foreign source: %q %v", actual, readErr) + } + entries, readErr := os.ReadDir(directory) + if readErr != nil { + t.Fatal(readErr) + } + for _, entry := range entries { + if !entry.IsDir() { + continue + } + owned, err := os.ReadDir(filepath.Join(directory, entry.Name())) + if err != nil { + t.Fatal(err) + } + if len(owned) != 1 || owned[0].Name() != "pending" { + t.Fatalf("owned renamed temp leaked or foreign temp deleted: %v", owned) + } + } +} diff --git a/internal/client/guest_publish_linux.go b/internal/client/guest_publish_linux.go new file mode 100644 index 0000000..873e81e --- /dev/null +++ b/internal/client/guest_publish_linux.go @@ -0,0 +1,132 @@ +//go:build linux + +package client + +import ( + "context" + "errors" + "fmt" + "os" + "path/filepath" + "strings" + "time" + + "golang.org/x/sys/unix" +) + +func createGuestStagingFile(path string) (*os.File, error) { + return os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) +} + +// publishGuestFileHandle never resolves the staging pathname. The caller owns +// the open file and staging cleanup. Errors after Linkat leave the published +// entry intact: removing it by pathname could remove another actor's file. +func publishGuestFileHandle(ctx context.Context, file *os.File, destination string) error { + if err := guestPublicationContextErr(ctx); err != nil { + return err + } + parent, leaf, err := openGuestPublicationParent(destination) + if err != nil { + return err + } + defer unix.Close(parent) + raw, err := file.SyscallConn() + if err != nil { + return err + } + var publicationErr error + err = raw.Control(func(fd uintptr) { + var before unix.Stat_t + if publicationErr = unix.Fstat(int(fd), &before); publicationErr != nil { + return + } + if before.Mode&unix.S_IFMT != unix.S_IFREG { + publicationErr = errors.New("guest staging must be a regular file") + return + } + if publicationErr = guestPublicationContextErr(ctx); publicationErr != nil { + return + } + // Control pins the descriptor against concurrent Close and reuse throughout + // source lookup, publication, and identity validation. + publicationErr = unix.Linkat(unix.AT_FDCWD, fmt.Sprintf("/proc/self/fd/%d", fd), parent, leaf, unix.AT_SYMLINK_FOLLOW) + if publicationErr != nil { + return + } + publicationErr = verifyGuestPublishedFile(int(fd), parent, leaf, before) + }) + if err != nil { + return err + } + return publicationErr +} + +// Walk each component relative to its held directory, rejecting symlinks even +// when they point inside the tree. Lexical traversal is rejected before Abs +// can clean it away. +func openGuestPublicationParent(destination string) (int, string, error) { + absolute, err := guestPublicationDestination(destination) + if err != nil { + return -1, "", err + } + parts := strings.Split(strings.TrimPrefix(absolute, "/"), "/") + parent, err := unix.Open("/", unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0) + if err != nil { + return -1, "", err + } + for _, part := range parts[:len(parts)-1] { + next, openErr := unix.Openat(parent, part, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) + unix.Close(parent) + if openErr != nil { + return -1, "", openErr + } + parent = next + } + return parent, parts[len(parts)-1], nil +} + +// Check the wall clock as well as Err: deadline timer delivery can lag. +func guestPublicationContextErr(ctx context.Context) error { + if err := ctx.Err(); err != nil { + return err + } + if deadline, ok := ctx.Deadline(); ok && !time.Now().Before(deadline) { + return context.DeadlineExceeded + } + return nil +} + +func guestPublicationDestination(destination string) (string, error) { + if destination == "" || strings.ContainsRune(destination, 0) { + return "", errors.New("invalid guest destination") + } + for i, part := range strings.Split(destination, "/") { + if (part == "" && i != 0) || part == "." || part == ".." { + return "", errors.New("invalid guest destination segment") + } + } + absolute, err := filepath.Abs(destination) + if err != nil { + return "", err + } + return absolute, nil +} + +// Once the namespace commit succeeds, validate the held object and installed +// entry together. Return effect-aware errors without unsafe pathname cleanup. +func verifyGuestPublishedFile(fd, parent int, leaf string, before unix.Stat_t) error { + var installed, after unix.Stat_t + if err := unix.Fstatat(parent, leaf, &installed, unix.AT_SYMLINK_NOFOLLOW); err != nil { + return fmt.Errorf("guest file published but destination inspection failed: %w", err) + } + if err := unix.Fstat(fd, &after); err != nil { + return fmt.Errorf("guest file published but source inspection failed: %w", err) + } + if installed.Mode&unix.S_IFMT != unix.S_IFREG || installed.Dev != before.Dev || installed.Ino != before.Ino || after.Dev != before.Dev || after.Ino != before.Ino || after.Size != before.Size || after.Mtim != before.Mtim || installed.Size != before.Size || installed.Mtim != before.Mtim { + return errors.New("guest file published but source or destination identity changed") + } + if err := unix.Fsync(parent); err != nil { + return fmt.Errorf("guest file published but directory sync failed: %w", err) + } + return nil +} diff --git a/internal/client/guest_publish_linux_test.go b/internal/client/guest_publish_linux_test.go new file mode 100644 index 0000000..6c3288f --- /dev/null +++ b/internal/client/guest_publish_linux_test.go @@ -0,0 +1,180 @@ +//go:build linux + +package client + +import ( + "context" + "errors" + "os" + "path/filepath" + "sync" + "testing" + "time" +) + +func guestPublicationFixture(t *testing.T) (*os.File, string) { + t.Helper() + directory := t.TempDir() + file, err := createGuestStagingFile(filepath.Join(directory, "pending")) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { file.Close() }) + if _, err := file.WriteString("verified payload"); err != nil { + t.Fatal(err) + } + return file, directory +} + +func TestGuestPublicationHeldObjectSurvivesPathReplacement(t *testing.T) { + file, directory := guestPublicationFixture(t) + original, err := file.Stat() + if err != nil { + t.Fatal(err) + } + // Keep a link to the original object: Linux cannot relink a fully unlinked + // inode through procfs. A substituted staging name must never be published. + if err := os.Rename(file.Name(), filepath.Join(directory, "held")); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(file.Name(), []byte("substituted"), 0600); err != nil { + t.Fatal(err) + } + destination := filepath.Join(directory, "download") + if err := publishGuestFileHandle(context.Background(), file, destination); err != nil { + t.Fatal(err) + } + installed, err := os.Stat(destination) + if err != nil || !os.SameFile(original, installed) { + t.Fatalf("published different object: %v", err) + } + data, err := os.ReadFile(destination) + if err != nil || string(data) != "verified payload" { + t.Fatalf("published %q: %v", data, err) + } + if _, err := file.Stat(); err != nil { + t.Fatalf("caller handle was closed: %v", err) + } + substituted, err := os.ReadFile(file.Name()) + if err != nil || string(substituted) != "substituted" { + t.Fatal("publication modified substituted staging entry") + } +} + +func TestGuestPublicationCreateNewConcurrent(t *testing.T) { + file, directory := guestPublicationFixture(t) + destination := filepath.Join(directory, "download") + var wg sync.WaitGroup + results := make(chan error, 8) + for range 8 { + wg.Go(func() { results <- publishGuestFileHandle(context.Background(), file, destination) }) + } + wg.Wait() + close(results) + successes := 0 + for err := range results { + if err == nil { + successes++ + } else if !errors.Is(err, os.ErrExist) { + t.Fatal(err) + } + } + if successes != 1 { + t.Fatalf("create-new successes = %d", successes) + } + if err := os.WriteFile(destination, []byte("existing"), 0600); err != nil { + t.Fatal(err) + } + if err := publishGuestFileHandle(context.Background(), file, destination); !errors.Is(err, os.ErrExist) { + t.Fatalf("existing destination: %v", err) + } + data, err := os.ReadFile(destination) + if err != nil || string(data) != "existing" { + t.Fatalf("destination overwritten: %q %v", data, err) + } +} + +type guestExpiredContext struct{ context.Context } + +func (guestExpiredContext) Deadline() (time.Time, bool) { return time.Unix(1, 0), true } + +func TestGuestPublicationRejectsUnsafeDestinationsAndDeadline(t *testing.T) { + file, directory := guestPublicationFixture(t) + outside := t.TempDir() + if err := os.Symlink(outside, filepath.Join(directory, "alias")); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Join(outside, "victim"), filepath.Join(directory, "existing-link")); err != nil { + t.Fatal(err) + } + canceled, cancel := context.WithCancel(context.Background()) + cancel() + cases := []struct { + name string + ctx context.Context + destination string + }{ + {"symlink-parent", context.Background(), filepath.Join(directory, "alias", "download")}, + {"symlink-leaf", context.Background(), filepath.Join(directory, "existing-link")}, + {"traversal", context.Background(), directory + "/../download"}, + {"dot-segment", context.Background(), directory + "/./download"}, + {"empty-segment", context.Background(), directory + "//download"}, + {"canceled", canceled, filepath.Join(directory, "canceled")}, + {"expired-before-timer", guestExpiredContext{context.Background()}, filepath.Join(directory, "expired")}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if err := publishGuestFileHandle(tc.ctx, file, tc.destination); err == nil { + t.Fatal("unsafe publication accepted") + } + }) + } + for _, path := range []string{filepath.Join(outside, "download"), filepath.Join(outside, "victim"), filepath.Join(directory, "canceled"), filepath.Join(directory, "expired")} { + if _, err := os.Lstat(path); !os.IsNotExist(err) { + t.Fatalf("failure created %s: %v", path, err) + } + } +} + +func TestGuestPublicationUnlinkedSourceFailsClosed(t *testing.T) { + file, directory := guestPublicationFixture(t) + if err := os.Remove(file.Name()); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(file.Name(), []byte("replacement"), 0600); err != nil { + t.Fatal(err) + } + destination := filepath.Join(directory, "download") + if err := publishGuestFileHandle(context.Background(), file, destination); err == nil { + t.Fatal("unlinked source unexpectedly published") + } + if _, err := os.Stat(destination); !os.IsNotExist(err) { + t.Fatalf("replacement was published: %v", err) + } +} + +func TestGuestStagingCreateNewPrivate(t *testing.T) { + file, _ := guestPublicationFixture(t) + info, err := file.Stat() + if err != nil || info.Mode().Perm() != 0600 { + t.Fatalf("staging permissions: %v %v", info, err) + } + if second, err := createGuestStagingFile(file.Name()); !errors.Is(err, os.ErrExist) { + if second != nil { + second.Close() + } + t.Fatalf("staging did not require create-new: %v", err) + } +} + +func TestGuestPublicationDestinationNormalization(t *testing.T) { + normalized, err := guestPublicationDestination("download") + if err != nil || !filepath.IsAbs(normalized) || filepath.Base(normalized) != "download" { + t.Fatalf("relative destination: %q %v", normalized, err) + } + for _, raw := range []string{"", "/", "./download", "../download", "nested/../download", "nested//download", "nested/", "bad\x00name"} { + if _, err := guestPublicationDestination(raw); err == nil { + t.Fatalf("malformed destination normalized: %q", raw) + } + } +} diff --git a/internal/client/guest_publish_other.go b/internal/client/guest_publish_other.go new file mode 100644 index 0000000..a27d88d --- /dev/null +++ b/internal/client/guest_publish_other.go @@ -0,0 +1,22 @@ +//go:build !linux && !windows + +package client + +import ( + "context" + "errors" + "os" +) + +// Publication requires a native held-handle create-new primitive. Do not +// substitute a staging pathname on unsupported platforms. +func publishGuestFileHandle(context.Context, *os.File, string) error { + return errors.New("held-handle guest file publication is unsupported on this platform") +} +func createGuestStagingFile(string) (*os.File, error) { + return nil, errors.New("guest file staging is unsupported on this platform") +} + +func guestPublicationDestination(string) (string, error) { + return "", errors.New("guest file staging is unsupported on this platform") +} diff --git a/internal/client/guest_publish_windows.go b/internal/client/guest_publish_windows.go new file mode 100644 index 0000000..eec1471 --- /dev/null +++ b/internal/client/guest_publish_windows.go @@ -0,0 +1,233 @@ +//go:build windows + +package client + +import ( + "context" + "errors" + "fmt" + "os" + "path/filepath" + "strings" + "time" + "unicode/utf8" + "unsafe" + + "golang.org/x/sys/windows" +) + +// The private staging directory's validated ACL is inherited. DELETE access +// permits held-handle rename; withholding write/delete sharing excludes other +// writers and staging-name replacement while verification and publication run. +func createGuestStagingFile(path string) (*os.File, error) { + name, err := windows.UTF16PtrFromString(path) + if err != nil { + return nil, err + } + handle, err := windows.CreateFile(name, windows.GENERIC_READ|windows.GENERIC_WRITE|windows.DELETE, windows.FILE_SHARE_READ, nil, windows.CREATE_NEW, windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_OPEN_REPARSE_POINT, 0) + if err != nil { + return nil, err + } + return os.NewFile(uintptr(handle), path), nil +} + +// FILE_RENAME_INFORMATION uses the zero union value for ReplaceIfExists=FALSE. +// RootDirectory and the leaf name ensure lookup stays within the held parent. +// The NT rename class supports a relative leaf under the held RootDirectory. +// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/ns-ntifs-_file_rename_information +// Successful rename removes the original staging entry. The caller retains +// ownership of the open file; cleanup must tolerate that entry being absent. +type guestRenameInformation struct { + ReplaceIfExists uint32 + RootDirectory windows.Handle + FileNameLength uint32 + FileName [1]uint16 +} + +func publishGuestFileHandle(ctx context.Context, file *os.File, destination string) error { + if err := guestPublicationContextErr(ctx); err != nil { + return err + } + parent, leaf, err := openGuestPublicationParent(destination) + if err != nil { + return err + } + defer windows.CloseHandle(parent) + name, err := windows.UTF16FromString(leaf) + if err != nil { + return err + } + var layout guestRenameInformation + size := int(unsafe.Sizeof(layout)) + len(name)*2 + buffer := make([]byte, size) + rename := (*guestRenameInformation)(unsafe.Pointer(&buffer[0])) + rename.RootDirectory = parent + rename.FileNameLength = uint32((len(name) - 1) * 2) + copy(unsafe.Slice(&rename.FileName[0], len(name)), name) + raw, err := file.SyscallConn() + if err != nil { + return err + } + var publicationErr error + err = raw.Control(func(fd uintptr) { + source := windows.Handle(fd) + var before, after windows.ByHandleFileInformation + if publicationErr = windows.GetFileInformationByHandle(source, &before); publicationErr != nil { + return + } + if before.FileAttributes&(windows.FILE_ATTRIBUTE_DIRECTORY|windows.FILE_ATTRIBUTE_REPARSE_POINT) != 0 { + publicationErr = errors.New("guest staging must be a regular non-reparse file") + return + } + if publicationErr = guestPublicationContextErr(ctx); publicationErr != nil { + return + } + var status windows.IO_STATUS_BLOCK + publicationErr = windows.NtSetInformationFile(source, &status, &buffer[0], uint32(size), windows.FileRenameInformation) + if publicationErr != nil { + if nativeStatus, ok := publicationErr.(windows.NTStatus); ok { + publicationErr = nativeStatus.Errno() + } + publicationErr = fmt.Errorf("guest held-handle rename failed: %w", publicationErr) + return + } + installed, inspectErr := openGuestPublicationChild(parent, leaf, false) + if inspectErr != nil { + publicationErr = fmt.Errorf("guest file published but destination inspection failed: %w", inspectErr) + return + } + defer windows.CloseHandle(installed) + var target windows.ByHandleFileInformation + if publicationErr = windows.GetFileInformationByHandle(installed, &target); publicationErr == nil { + publicationErr = windows.GetFileInformationByHandle(source, &after) + } + if publicationErr != nil { + publicationErr = fmt.Errorf("guest file published but identity inspection failed: %w", publicationErr) + return + } + if !sameGuestWindowsFile(before, target) || !sameGuestWindowsFile(before, after) || target.FileAttributes&(windows.FILE_ATTRIBUTE_REPARSE_POINT|windows.FILE_ATTRIBUTE_DIRECTORY) != 0 { + publicationErr = errors.New("guest file published but source or destination identity changed") + } + }) + if err != nil { + return err + } + return publicationErr +} + +func sameGuestWindowsFile(a, b windows.ByHandleFileInformation) bool { + return a.VolumeSerialNumber == b.VolumeSerialNumber && a.FileIndexHigh == b.FileIndexHigh && a.FileIndexLow == b.FileIndexLow && a.FileSizeHigh == b.FileSizeHigh && a.FileSizeLow == b.FileSizeLow && a.LastWriteTime == b.LastWriteTime +} + +func openGuestPublicationParent(destination string) (windows.Handle, string, error) { + absolute, err := guestPublicationDestination(destination) + if err != nil { + return 0, "", err + } + volume := filepath.VolumeName(absolute) + parts := strings.Split(absolute[len(volume)+1:], `\`) + rootName, err := windows.UTF16PtrFromString(volume + `\`) + if err != nil { + return 0, "", err + } + parent, err := windows.CreateFile(rootName, windows.FILE_TRAVERSE|windows.FILE_READ_ATTRIBUTES|windows.SYNCHRONIZE, windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE, nil, windows.OPEN_EXISTING, windows.FILE_FLAG_BACKUP_SEMANTICS|windows.FILE_FLAG_OPEN_REPARSE_POINT, 0) + if err != nil { + return 0, "", err + } + for _, part := range parts[:len(parts)-1] { + next, openErr := openGuestPublicationChild(parent, part, true) + windows.CloseHandle(parent) + if openErr != nil { + return 0, "", fmt.Errorf("guest held destination directory open failed: %w", openErr) + } + parent = next + } + return parent, parts[len(parts)-1], nil +} + +// Each NT lookup is relative to a held directory handle. OPEN_REPARSE_POINT +// prevents traversal, and attribute inspection rejects even in-tree junctions. +func openGuestPublicationChild(parent windows.Handle, name string, directory bool) (windows.Handle, error) { + objectName, err := windows.NewNTUnicodeString(name) + if err != nil { + return 0, err + } + attributes := windows.OBJECT_ATTRIBUTES{RootDirectory: parent, ObjectName: objectName, Attributes: windows.OBJ_CASE_INSENSITIVE | windows.OBJ_DONT_REPARSE} + attributes.Length = uint32(unsafe.Sizeof(attributes)) + options := uint32(windows.FILE_OPEN_REPARSE_POINT | windows.FILE_SYNCHRONOUS_IO_NONALERT) + access := uint32(windows.FILE_READ_ATTRIBUTES | windows.SYNCHRONIZE) + if directory { + options |= windows.FILE_DIRECTORY_FILE + access |= windows.FILE_TRAVERSE + } else { + options |= windows.FILE_NON_DIRECTORY_FILE + } + var handle windows.Handle + var status windows.IO_STATUS_BLOCK + err = windows.NtCreateFile(&handle, access, &attributes, &status, nil, 0, windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, windows.FILE_OPEN, options, 0, 0) + if err != nil { + return 0, err + } + var info windows.ByHandleFileInformation + if err = windows.GetFileInformationByHandle(handle, &info); err == nil && info.FileAttributes&windows.FILE_ATTRIBUTE_REPARSE_POINT != 0 { + err = errors.New("guest destination contains a reparse point") + } + if err != nil { + windows.CloseHandle(handle) + return 0, err + } + return handle, nil +} + +// Check the wall clock as well as Err: deadline timer delivery can lag. +func guestPublicationContextErr(ctx context.Context) error { + if err := ctx.Err(); err != nil { + return err + } + if deadline, ok := ctx.Deadline(); ok && !time.Now().Before(deadline) { + return context.DeadlineExceeded + } + return nil +} + +// Validate raw components before Abs can erase traversal or normalize aliases. +func guestPublicationDestination(destination string) (string, error) { + if destination == "" || !utf8.ValidString(destination) || strings.ContainsAny(destination, "\x00\"<>|?*") { + return "", errors.New("invalid guest destination") + } + destination = strings.ReplaceAll(destination, "/", `\`) + volume := filepath.VolumeName(destination) + if volume != "" && (len(volume) != 2 || volume[1] != ':' || !((volume[0] >= 'A' && volume[0] <= 'Z') || (volume[0] >= 'a' && volume[0] <= 'z')) || !filepath.IsAbs(destination)) { + return "", errors.New("guest destination requires a local drive path") + } + if volume == "" && strings.HasPrefix(destination, `\`) { + return "", errors.New("root-relative guest destination is unsupported") + } + raw := destination + if volume != "" { + raw = destination[len(volume)+1:] + } + for _, part := range strings.Split(raw, `\`) { + if part == "" || part == "." || part == ".." || strings.Contains(part, ":") || strings.HasSuffix(part, ".") || strings.HasSuffix(part, " ") { + return "", errors.New("invalid guest destination segment") + } + for _, character := range part { + if character < 32 { + return "", errors.New("invalid guest destination control character") + } + } + base := strings.TrimRight(strings.ToUpper(strings.SplitN(part, ".", 2)[0]), " ") + if base == "CON" || base == "PRN" || base == "AUX" || base == "NUL" || base == "CONIN$" || base == "CONOUT$" || ((strings.HasPrefix(base, "COM") || strings.HasPrefix(base, "LPT")) && utf8.RuneCountInString(base[3:]) == 1 && strings.ContainsAny(base[3:], "123456789¹²³")) { + return "", errors.New("reserved guest destination segment") + } + } + absolute, err := filepath.Abs(destination) + if err != nil { + return "", err + } + // A relative path on a network current directory must not select UNC access. + if len(filepath.VolumeName(absolute)) != 2 { + return "", errors.New("guest destination requires a local drive path") + } + return absolute, nil +} diff --git a/internal/client/guest_publish_windows_test.go b/internal/client/guest_publish_windows_test.go new file mode 100644 index 0000000..e798306 --- /dev/null +++ b/internal/client/guest_publish_windows_test.go @@ -0,0 +1,164 @@ +//go:build windows + +package client + +import ( + "context" + "errors" + "io" + "os" + "path/filepath" + "testing" + "unsafe" + + "github.com/Horcag/agent-machine-control/internal/target" + + "golang.org/x/sys/windows" +) + +// This tests the ABI contract independently of a filesystem or live machine. +func TestGuestRenameInformationLayout(t *testing.T) { + var layout guestRenameInformation + if layout.ReplaceIfExists != 0 || windows.FileRenameInformation != 10 { + t.Fatal("rename must use documented create-new class and replacement disabled") + } + wantRoot, wantName := uintptr(4), uintptr(12) + if unsafe.Sizeof(windows.Handle(0)) == 8 { + wantRoot, wantName = 8, 20 + } + if unsafe.Offsetof(layout.RootDirectory) != wantRoot || unsafe.Offsetof(layout.FileName) != wantName { + t.Fatalf("incorrect FILE_RENAME_INFORMATION ABI: root=%d name=%d", unsafe.Offsetof(layout.RootDirectory), unsafe.Offsetof(layout.FileName)) + } +} + +func TestGuestPublicationWindowsRejectsAliasesBeforeOpening(t *testing.T) { + for _, path := range []string{`C:\safe\file:stream`, `C:\safe\..\file`, `C:\safe\CON.txt`, `C:\safe\file.`, `C:\safe\file `, `\\server\share\file`, `\\?\C:\safe\file`, `C:file`, `C:\safe\badname`, `C:\safe\COM¹.txt`, `C:\safe\\file`} { + if handle, _, err := openGuestPublicationParent(path); err == nil { + windows.CloseHandle(handle) + t.Fatalf("unsafe destination accepted: %q", path) + } + } +} + +// Exercise the native ABI and filesystem operation, rather than merely checking +// layout constants. This test must execute on the configured Windows CI runner. +func TestGuestPublicationWindowsNativeHeldRename(t *testing.T) { + for _, mode := range []string{"success", "existing-destination", "canceled"} { + t.Run(mode, func(t *testing.T) { + ctx := context.Background() + parent := t.TempDir() + stage, err := newGuestStaging(ctx, parent) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + if err := stage.cleanup(); err != nil { + t.Error(err) + } + }) + if _, err := stage.file.WriteString("verified original"); err != nil { + t.Fatal(err) + } + if err := stage.file.Sync(); err != nil { + t.Fatal(err) + } + original, err := stage.file.Stat() + if err != nil { + t.Fatal(err) + } + destination := filepath.Join(parent, "published") + if mode == "existing-destination" { + if err := os.WriteFile(destination, []byte("existing protected bytes"), 0600); err != nil { + t.Fatal(err) + } + } + if mode == "canceled" { + canceled, cancel := context.WithCancel(ctx) + cancel() + ctx = canceled + } + err = publishGuestFileHandle(ctx, stage.file, destination) + if mode != "success" { + if err == nil { + t.Fatal("publication unexpectedly succeeded") + } + if mode == "canceled" && !errors.Is(err, context.Canceled) { + t.Fatalf("cancellation: %v", err) + } + if mode == "existing-destination" && !errors.Is(err, os.ErrExist) { + t.Fatalf("create-new rename returned an unrelated failure: %v", err) + } + pending, statErr := os.Stat(stage.file.Name()) + if statErr != nil || !os.SameFile(original, pending) { + t.Fatalf("failure changed staging identity: %v", statErr) + } + if mode == "existing-destination" { + data, readErr := os.ReadFile(destination) + if readErr != nil || string(data) != "existing protected bytes" { + t.Fatalf("existing file overwritten: %q %v", data, readErr) + } + } else if _, statErr := os.Stat(destination); !os.IsNotExist(statErr) { + t.Fatalf("canceled publication created a file: %v", statErr) + } + return + } + if err != nil { + t.Fatal(err) + } + if _, err := os.Stat(stage.file.Name()); !os.IsNotExist(err) { + t.Fatalf("rename retained staging entry: %v", err) + } + installed, err := os.Stat(destination) + if err != nil || !os.SameFile(original, installed) { + t.Fatalf("rename published a different object: %v", err) + } + held, err := stage.file.Stat() + if err != nil || !os.SameFile(original, held) { + t.Fatalf("publication closed or changed held source: %v", err) + } + // The original handle retains WRITE and DELETE access. This reader + // must share both while independently checking the installed object. + name, err := windows.UTF16PtrFromString(destination) + if err != nil { + t.Fatal(err) + } + handle, err := windows.CreateFile(name, windows.GENERIC_READ, windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, nil, windows.OPEN_EXISTING, windows.FILE_FLAG_OPEN_REPARSE_POINT, 0) + if err != nil { + t.Fatal(err) + } + reader := os.NewFile(uintptr(handle), destination) + t.Cleanup(func() { _ = reader.Close() }) + reopened, err := reader.Stat() + if err != nil || !os.SameFile(original, reopened) { + t.Fatalf("reader opened a different object: %v", err) + } + data, err := io.ReadAll(io.LimitReader(reader, int64(len("verified original")+1))) + if err != nil || string(data) != "verified original" { + t.Fatalf("published bytes: %q %v", data, err) + } + if err := target.NewPrivatePathSecurity().ValidateFile(ctx, destination); err != nil { + t.Fatalf("rename changed private ACL: %v", err) + } + }) + } +} + +func TestGuestPublicationWindowsNativeStagingExcludesOtherWriters(t *testing.T) { + stage, err := newGuestStaging(context.Background(), t.TempDir()) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + if err := stage.cleanup(); err != nil { + t.Error(err) + } + }) + other, err := os.OpenFile(stage.file.Name(), os.O_WRONLY, 0) + if err == nil { + other.Close() + t.Fatal("held staging allowed a concurrent writer") + } + if !errors.Is(err, windows.ERROR_SHARING_VIOLATION) { + t.Fatalf("writer exclusion failed for another reason: %v", err) + } +} diff --git a/internal/client/guest_staging.go b/internal/client/guest_staging.go new file mode 100644 index 0000000..ffbc50e --- /dev/null +++ b/internal/client/guest_staging.go @@ -0,0 +1,135 @@ +package client + +import ( + "context" + "crypto/rand" + "encoding/hex" + "errors" + "io" + "os" + "path/filepath" + + "github.com/Horcag/agent-machine-control/internal/statedir" + "github.com/Horcag/agent-machine-control/internal/target" +) + +type guestStaging struct { + root *os.Root + directory string + directoryInfo os.FileInfo + file *os.File + fileInfo os.FileInfo +} + +func newGuestStaging(ctx context.Context, parent string) (*guestStaging, error) { + if err := ctx.Err(); err != nil { + return nil, err + } + var nonce [16]byte + if _, err := rand.Read(nonce[:]); err != nil { + return nil, err + } + directory := filepath.Join(parent, ".amc-guest-"+hex.EncodeToString(nonce[:])) + if _, err := os.Lstat(directory); !os.IsNotExist(err) { + return nil, errors.New("guest staging namespace reservation collision") + } + if err := statedir.EnsurePrivateDirectory(directory); err != nil { + return nil, err + } + stage := &guestStaging{directory: directory} + var err error + stage.directoryInfo, err = os.Lstat(directory) + if err != nil { + return nil, err + } + if err = stage.open(ctx); err != nil { + return nil, errors.Join(err, stage.cleanup()) + } + return stage, nil +} +func (s *guestStaging) open(ctx context.Context) error { + security := target.NewPrivatePathSecurity() + if err := security.ValidateDir(ctx, s.directory); err != nil { + return err + } + var err error + s.root, err = os.OpenRoot(s.directory) + if err != nil { + return err + } + held, err := s.root.Stat(".") + if err != nil || !os.SameFile(held, s.directoryInfo) { + return errors.New("guest staging namespace identity changed") + } + path := filepath.Join(s.directory, "pending") + s.file, err = createGuestStagingFile(path) + if err != nil { + return err + } + s.fileInfo, err = s.file.Stat() + if err != nil { + return err + } + if err = security.ValidateInheritedFile(ctx, path); err != nil { + return err + } + if err = security.ProtectNewFile(ctx, path); err != nil { + return err + } + if err = security.ValidateFile(ctx, path); err != nil { + return err + } + named, err := s.root.Lstat("pending") + if err != nil || !os.SameFile(named, s.fileInfo) { + return errors.New("guest staging file escaped held private namespace") + } + return nil +} +func (s *guestStaging) cleanup() error { + var result error + if s.file != nil { + if err := s.file.Close(); err != nil && !errors.Is(err, os.ErrClosed) { + result = errors.Join(result, err) + } + } + if s.root != nil { + result = errors.Join(result, s.removeOwnedFile(), s.root.Close()) + } + current, err := os.Lstat(s.directory) + if os.IsNotExist(err) { + return result + } + if err != nil || s.directoryInfo == nil || !os.SameFile(current, s.directoryInfo) { + return errors.Join(result, errors.New("guest staging directory cleanup identity changed")) + } + return errors.Join(result, os.Remove(s.directory)) +} + +func (s *guestStaging) removeOwnedFile() error { + directory, err := s.root.Open(".") + if err != nil { + return err + } + defer directory.Close() + entries, err := directory.ReadDir(32) + if err != nil && !errors.Is(err, io.EOF) { + return err + } + if len(entries) == 32 { + return errors.New("guest staging cleanup entry budget exceeded") + } + var result error + for _, entry := range entries { + info, err := s.root.Lstat(entry.Name()) + if err != nil { + result = errors.Join(result, err) + continue + } + if s.fileInfo == nil || !os.SameFile(info, s.fileInfo) { + result = errors.Join(result, errors.New("guest staging cleanup blocked by foreign file identity")) + continue + } + result = errors.Join(result, s.root.Remove(entry.Name())) + } + return result +} diff --git a/internal/client/guest_test.go b/internal/client/guest_test.go new file mode 100644 index 0000000..5bd9ae8 --- /dev/null +++ b/internal/client/guest_test.go @@ -0,0 +1,267 @@ +package client + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "encoding/json" + "errors" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "runtime" + "strings" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/daemon" + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +func guestFixture(t *testing.T, data []byte, mode string) (*Client, *int) { + t.Helper() + calls := new(int) + sum := sha256.Sum256(data) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + *calls++ + if r.Header.Get("Authorization") != "Bearer test-token" { + t.Error("missing authentication") + } + p, prefix, err := daemon.DecodeGuestMetadata(r.Body) + if err != nil { + t.Error(err) + return + } + payload, err := io.ReadAll(r.Body) + if err != nil || r.ContentLength != prefix+int64(len(payload)) || int64(len(payload)) != p.Size { + t.Error("invalid request framing") + } + w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Trailer", daemon.GuestResultTrailer) + final := daemon.GuestFinal{Result: app.GuestOperationResult{Receipt: &domain.Receipt{ReceiptID: domain.ReceiptID("rcpt-" + strings.Repeat("a", 32)), Fingerprint: domain.Fingerprint("sha256:" + strings.Repeat("a", 64)), Outcome: domain.ExecutionOutcome{Status: domain.OutcomeSuccess}, OperationKind: domain.OperationKind("guest." + p.Operation), IdempotencyKey: p.IdempotencyKey, Target: "local:11111111-1111-4111-8111-111111111111", Actor: "operator:synthetic", Class: domain.ClassDestructivePrivileged, EffectiveBackend: "ssh", StartedAt: time.Now(), CompletedAt: time.Now(), ObservationType: domain.ObservationObserved, RedactionStatus: domain.RedactionNotApplicable}, Size: int64(len(data)), SHA256: hex.EncodeToString(sum[:])}} + if mode == "hash" { + final.Result.SHA256 = hex.EncodeToString(make([]byte, 32)) + } + if mode == "size" { + final.Result.Size++ + } + if mode == "error" { + final.Error = "uncertain" + } + for _, b := range data { + _, _ = w.Write([]byte{b}) + } + if mode != "missing" { + encoded, _ := json.Marshal(final) + w.Header().Set(daemon.GuestResultTrailer, base64.RawStdEncoding.EncodeToString(encoded)) + } + })) + t.Cleanup(server.Close) + return New(server.URL, "test-token"), calls +} +func TestGuestBinaryTrailerIntegrity(t *testing.T) { + for _, mode := range []string{"valid", "hash", "size", "error", "missing"} { + t.Run(mode, func(t *testing.T) { + data := []byte{0, 255, 1, 128} + cl, calls := guestFixture(t, data, mode) + var out bytes.Buffer + result, err := cl.GuestOperation(context.Background(), app.GuestOperationParams{Operation: "get", IdempotencyKey: "synthetic-request", MaxOutput: 4, Deadline: time.Now().Add(time.Second)}, nil, stream.BufferOutput(&out)) + if (err == nil) != (mode == "valid") { + t.Fatalf("result=%+v err=%v", result, err) + } + if *calls != 1 { + t.Fatal("request was replayed") + } + if mode == "valid" && !bytes.Equal(data, out.Bytes()) { + t.Fatal("binary changed") + } + }) + } +} +func TestGuestFilePublication(t *testing.T) { + if runtime.GOOS != "linux" && runtime.GOOS != "windows" { + testUnsupportedGuestFilePublication(t) + return + } + for _, mode := range []string{"valid", "hash", "missing"} { + t.Run(mode, func(t *testing.T) { + data := []byte("verified output") + cl, calls := guestFixture(t, data, mode) + directory := t.TempDir() + path := filepath.Join(directory, "result.bin") + _, err := cl.GuestOperationToFile(context.Background(), app.GuestOperationParams{Operation: "get", IdempotencyKey: "synthetic-request", MaxOutput: 100, Deadline: time.Now().Add(time.Second)}, nil, path) + entries, _ := os.ReadDir(directory) + if *calls != 1 { + t.Fatalf("publication dispatched %d times", *calls) + } + if mode != "valid" { + if err == nil || len(entries) != 0 { + t.Fatalf("failed stream published or leaked: %v %v", err, entries) + } + return + } + if err != nil { + t.Fatal(err) + } + assertGuestPublishedOutput(t, path, data, entries) + }) + } +} + +func assertGuestPublishedOutput(t *testing.T, path string, expected []byte, entries []os.DirEntry) { + t.Helper() + got, err := os.ReadFile(path) + if err != nil || !bytes.Equal(got, expected) { + t.Fatalf("published bytes differ: %v", err) + } + if len(entries) != 1 || entries[0].Name() != filepath.Base(path) { + t.Fatalf("publication left unexpected entries: %v", entries) + } +} + +func testUnsupportedGuestFilePublication(t *testing.T) { + t.Helper() + cl, calls := guestFixture(t, []byte("verified output"), "valid") + directory := t.TempDir() + path := filepath.Join(directory, "result.bin") + _, err := cl.GuestOperationToFile(t.Context(), app.GuestOperationParams{Operation: "get", IdempotencyKey: "synthetic-request", MaxOutput: 100, Deadline: time.Now().Add(time.Second)}, nil, path) + entries, readErr := os.ReadDir(directory) + if err == nil || *calls != 0 || readErr != nil || len(entries) != 0 { + t.Fatalf("unsupported publication dispatched or leaked: %v calls=%d entries=%v read=%v", err, *calls, entries, readErr) + } +} +func TestGuestDestinationExistsDoesNotDispatch(t *testing.T) { + cl, calls := guestFixture(t, []byte("x"), "valid") + path := filepath.Join(t.TempDir(), "exists") + _ = os.WriteFile(path, []byte("keep"), 0600) + if _, err := cl.GuestOperationToFile(context.Background(), app.GuestOperationParams{}, nil, path); err == nil || *calls != 0 { + t.Fatal("existing destination was dispatched or overwritten") + } +} +func TestGuestSourceChangeRejected(t *testing.T) { + path := filepath.Join(t.TempDir(), "source") + _ = os.WriteFile(path, []byte("first"), 0600) + source, err := OpenGuestSource(context.Background(), path) + if err != nil { + t.Fatal(err) + } + defer source.Close() + _ = os.WriteFile(path, []byte("changed"), 0600) + if source.Verify() == nil { + t.Fatal("source change accepted") + } +} + +func TestGuestCancellationUnblocksHTTPRead(t *testing.T) { + started := make(chan struct{}) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = io.Copy(io.Discard, r.Body) + w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Trailer", daemon.GuestResultTrailer) + w.WriteHeader(200) + w.(http.Flusher).Flush() + close(started) + <-r.Context().Done() + })) + defer server.Close() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + done := make(chan error, 1) + go func() { + var out bytes.Buffer + _, err := New(server.URL, "token").GuestOperation(ctx, app.GuestOperationParams{Operation: "get", IdempotencyKey: "synthetic-request", MaxOutput: 20, Deadline: time.Now().Add(5 * time.Second)}, nil, stream.BufferOutput(&out)) + done <- err + }() + <-started + cancel() + select { + case err := <-done: + if err == nil { + t.Fatal("cancel accepted") + } + case <-time.After(time.Second): + t.Fatal("request cancellation did not unblock read") + } +} + +type shortGuestWriter struct{} + +func (shortGuestWriter) Write(p []byte) (int, error) { return len(p) - 1, nil } +func (w shortGuestWriter) WriteContext(_ context.Context, p []byte) (int, error) { return w.Write(p) } +func TestGuestShortOutputRejected(t *testing.T) { + cl, _ := guestFixture(t, []byte("output"), "valid") + _, err := cl.GuestOperation(context.Background(), app.GuestOperationParams{Operation: "get", IdempotencyKey: "synthetic-request", MaxOutput: 20, Deadline: time.Now().Add(time.Second)}, nil, shortGuestWriter{}) + if !errors.Is(err, io.ErrShortWrite) || !errors.Is(err, app.ErrGuestUncertain) { + t.Fatalf("error=%v", err) + } +} + +func TestGuestRedirectDoesNotReplay(t *testing.T) { + calls := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls++ + http.Redirect(w, r, "/other", http.StatusTemporaryRedirect) + })) + defer server.Close() + _, err := New(server.URL, "token").GuestOperation(t.Context(), app.GuestOperationParams{Operation: "prepare", Deadline: time.Now().Add(time.Second)}, nil, nil) + if err == nil || calls != 1 { + t.Fatalf("redirect replayed: calls=%d error=%v", calls, err) + } +} +func TestGuestScriptRejectsInvalidUTF8(t *testing.T) { + path := filepath.Join(t.TempDir(), "script") + if err := os.WriteFile(path, []byte{0xff}, 0600); err != nil { + t.Fatal(err) + } + if source, err := OpenGuestPayload(t.Context(), "exec", path); err == nil { + _ = source.Close() + t.Fatal("invalid UTF-8 accepted") + } +} + +type publicationDeadlineTransport struct { + base http.RoundTripper + deadline time.Time +} + +func (t publicationDeadlineTransport) RoundTrip(request *http.Request) (*http.Response, error) { + response, err := t.base.RoundTrip(request) + if err == nil { + response.Body = publicationDeadlineBody{response.Body, t.deadline} + } + return response, err +} + +type publicationDeadlineBody struct { + io.ReadCloser + deadline time.Time +} + +func (b publicationDeadlineBody) Close() error { + if delay := time.Until(b.deadline); delay > 0 { + timer := time.NewTimer(delay) + <-timer.C + } + return b.ReadCloser.Close() +} +func TestGuestDeadlineCoversPublicationAfterHTTPCompletion(t *testing.T) { + fixture, _ := guestFixture(t, []byte("verified data"), "valid") + deadline := time.Now().Add(200 * time.Millisecond) + cl := New(fixture.Endpoint(), "test-token", WithHTTPClient(&http.Client{Transport: publicationDeadlineTransport{http.DefaultTransport, deadline}})) + directory := t.TempDir() + destination := filepath.Join(directory, "result") + _, err := cl.GuestOperationToFile(context.Background(), app.GuestOperationParams{Operation: "get", IdempotencyKey: "synthetic-request", MaxOutput: 100, Deadline: deadline}, nil, destination) + if err == nil { + t.Fatal("published after exact operation deadline") + } + entries, _ := os.ReadDir(directory) + if len(entries) != 0 { + t.Fatalf("expired publication leaked owned staging or destination: %v", entries) + } +} diff --git a/internal/daemon/config.go b/internal/daemon/config.go index 43a4abd..9ef5e4b 100644 --- a/internal/daemon/config.go +++ b/internal/daemon/config.go @@ -11,6 +11,8 @@ import ( // Config holds runtime options for the amcd daemon server. type Config struct { + GuestConfigProvider app.GuestConfigProvider + GuestTransport app.GuestTransport StateDir string ListenAddr string ShutdownTimeout time.Duration diff --git a/internal/daemon/guest_approval_authority_test.go b/internal/daemon/guest_approval_authority_test.go new file mode 100644 index 0000000..7f992e2 --- /dev/null +++ b/internal/daemon/guest_approval_authority_test.go @@ -0,0 +1,176 @@ +package daemon + +import ( + "bytes" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/approval" + "github.com/Horcag/agent-machine-control/internal/audit" + "github.com/Horcag/agent-machine-control/internal/auth" + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + "github.com/Horcag/agent-machine-control/internal/lease" + "github.com/Horcag/agent-machine-control/internal/receipt" + "github.com/Horcag/agent-machine-control/internal/sessions" +) + +// Exercise the production identity provider: an echoing config fixture conceals +// mismatches between a requested beneficiary and its current authenticated actor. +func TestGuestApprovalUsesCurrentBeneficiaryAuthority(t *testing.T) { + provider, _, _, _ := newGuestConfigurationFixture(t) + sd := provider.server.stateDir + fixture := &guestHTTPServiceFixture{ + root: stream.RootIdentity{Path: `C:\AMC\synthetic`, Volume: 7, FileID: strings.Repeat("a", 32), CreationTime: 100, Owner: "S-1-5-21-synthetic", SDDLHash: strings.Repeat("d", 64)}, + worker: stream.WorkerIdentity{PID: 123, BirthTicks: 638000000000000001, Image: `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`, SID: "S-1-5-21-synthetic"}, + privileged: true, + } + ref := "local:11111111-1111-4111-8111-111111111111" + record, err := provider.workspaceRecord(t.Context(), domain.MachineRef(ref)) + if err != nil { + t.Fatal(err) + } + record.Workspace = fixture.root + data, err := json.Marshal(record) + if err != nil { + t.Fatal(err) + } + digest := sha256.Sum256([]byte(ref)) + name := hex.EncodeToString(digest[:]) + if err := writeGuestPrivateFixture(t, filepath.Join(sd.Root(), "guest-workspaces", name+".json"), data); err != nil { + t.Fatal(err) + } + coordinator := app.NewSessionService(nil, fixture, lease.NewManager(sd.LeasesDir()), audit.NewStore(sd.AuditDir()), receipt.NewStore(sd.ReceiptsDir()), approval.NewStore(sd.ApprovalsDir()), app.WithSessionMutationJournal(sessions.NewMutationJournal(sd.SessionsDir()))) + server := provider.server + server.semaphore = make(chan struct{}, 8) + server.guestService = app.NewGuestService(coordinator, fixture, &provider, fixture, app.NewGuestStore(filepath.Join(sd.Root(), "guest"))) + endpoint := httptest.NewServer(server.authMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/approval" { + server.handleGuestApproval(w, r) + return + } + server.handleGuestOperation(w, r) + }))) + t.Cleanup(endpoint.Close) + operator, err := auth.ReadTokenFile(sd.AuthDir(), auth.TokenTypeOperator) + if err != nil { + t.Fatal(err) + } + agent, err := auth.ReadTokenFile(sd.AuthDir(), auth.TokenTypeAgentMCP) + if err != nil { + t.Fatal(err) + } + p := app.GuestOperationParams{Target: ref, Operation: "prepare", Reason: "approve exact guest preparation", IdempotencyKey: "real-beneficiary-prepare"} + request := GuestApprovalRequest{Params: p, Beneficiary: string(auth.AgentMCPPrincipal), ValidForMillis: 60000} + first := issueGuestApprovalTwice(t, endpoint.URL+"/approval", operator, request) + p.ApprovalID, p.Deadline = first.Grant.ApprovalID, first.Grant.Deadline + final, _ := invokeGuestHTTPFixture(t, endpoint.URL+"/operation", agent, p, nil) + if final.Error != "" || final.Result.Receipt == nil || final.Result.Receipt.Outcome.Status != domain.OutcomeSuccess || strings.Join(fixture.operations, ",") != "observe" { + t.Fatalf("issued beneficiary could not execute: %+v operations=%v", final, fixture.operations) + } + final, _ = invokeGuestHTTPFixture(t, endpoint.URL+"/operation", agent, p, nil) + if final.Error != "" || final.Result.Receipt == nil || strings.Join(fixture.operations, ",") != "observe" { + t.Fatalf("exact execution retry dispatched again: %+v operations=%v", final, fixture.operations) + } + assertGuestApprovedPayload(t, endpoint, operator, agent, ref, fixture, request) + request.Params.IdempotencyKey = "agent-cannot-issue" + response := requestGuestApproval(t, endpoint.URL+"/approval", agent, request) + _ = response.Body.Close() + if response.StatusCode != http.StatusForbidden { + t.Fatalf("agent issued operator authority: status=%d", response.StatusCode) + } +} + +func requestGuestApproval(t *testing.T, endpoint, token string, value GuestApprovalRequest) *http.Response { + t.Helper() + data, err := json.Marshal(value) + if err != nil { + t.Fatal(err) + } + request, err := http.NewRequestWithContext(t.Context(), http.MethodPost, endpoint, bytes.NewReader(data)) + if err != nil { + t.Fatal(err) + } + request.Header.Set("Authorization", "Bearer "+token) + request.Header.Set("Content-Type", "application/json") + response, err := http.DefaultClient.Do(request) + if err != nil { + t.Fatal(err) + } + return response +} + +func TestGuestApprovalReportsAuthorityFailuresBeforeIssuance(t *testing.T) { + for _, tc := range []struct { + name string + err error + category string + }{ + {"revoked authority", app.ErrGuestAuthority, "guest_authority"}, + {"missing workspace", app.ErrGuestWorkspaceUnprovisioned, "guest_workspace_unprovisioned"}, + } { + t.Run(tc.name, func(t *testing.T) { + endpoint, token, fixture := newGuestHTTPServiceFixture(t) + fixture.configErr = tc.err + request := GuestApprovalRequest{Params: app.GuestOperationParams{Operation: "prepare", Reason: "synthetic approval failure", IdempotencyKey: "failed-approval"}, Beneficiary: "self", ValidForMillis: time.Minute.Milliseconds()} + response := requestGuestApproval(t, endpoint.URL+"/approval", token, request) + var body struct { + Error struct { + Category string `json:"category"` + } `json:"error"` + } + decodeErr := json.NewDecoder(response.Body).Decode(&body) + _ = response.Body.Close() + if response.StatusCode != http.StatusForbidden || decodeErr != nil || body.Error.Category != tc.category || len(fixture.operations) != 0 { + t.Fatalf("known authority failure: status=%d body=%+v operations=%v err=%v", response.StatusCode, body, fixture.operations, decodeErr) + } + }) + } +} + +func issueGuestApprovalTwice(t *testing.T, endpoint, operator string, request GuestApprovalRequest) GuestApprovalResponse { + t.Helper() + var first GuestApprovalResponse + for attempt := range 2 { + response := requestGuestApproval(t, endpoint, operator, request) + var issued GuestApprovalResponse + decodeErr := json.NewDecoder(response.Body).Decode(&issued) + _ = response.Body.Close() + if response.StatusCode != http.StatusOK || decodeErr != nil || issued.Grant == nil || issued.Receipt == nil { + t.Fatalf("issuance attempt %d: status=%d response=%+v err=%v", attempt, response.StatusCode, issued, decodeErr) + } + if attempt == 0 { + first = issued + } else if first.Grant.ApprovalID != issued.Grant.ApprovalID || first.Receipt.ReceiptID != issued.Receipt.ReceiptID { + t.Fatal("exact issuance retry changed authority") + } + } + return first +} + +func assertGuestApprovedPayload(t *testing.T, endpoint *httptest.Server, operator, agent, ref string, fixture *guestHTTPServiceFixture, request GuestApprovalRequest) { + t.Helper() + command, payload := guestHTTPCase("exec", "") + command.Target = ref + request.Params = command + response := requestGuestApproval(t, endpoint.URL+"/approval", operator, request) + var execution GuestApprovalResponse + decodeErr := json.NewDecoder(response.Body).Decode(&execution) + _ = response.Body.Close() + if response.StatusCode != http.StatusOK || decodeErr != nil || execution.Grant == nil { + t.Fatalf("payload approval: status=%d err=%v", response.StatusCode, decodeErr) + } + command.ApprovalID, command.Deadline = execution.Grant.ApprovalID, execution.Grant.Deadline + final, output := invokeGuestHTTPFixture(t, endpoint.URL+"/operation", agent, command, payload) + if final.Error != "" || final.Result.Receipt == nil || final.Result.Receipt.Outcome.Status != domain.OutcomeSuccess || string(output) != "verified command output" || strings.Join(fixture.operations, ",") != "observe,exec" { + t.Fatalf("approved payload failed: %+v operations=%v", final, fixture.operations) + } +} diff --git a/internal/daemon/guest_configuration.go b/internal/daemon/guest_configuration.go new file mode 100644 index 0000000..c4f5b02 --- /dev/null +++ b/internal/daemon/guest_configuration.go @@ -0,0 +1,186 @@ +package daemon + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "io" + "os" + "path/filepath" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/auth" + "github.com/Horcag/agent-machine-control/internal/domain" + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + "github.com/Horcag/agent-machine-control/internal/target" +) + +// GuestWorkspaceRecord is original provisioning evidence, installed separately +// by an authorized operator. The daemon never observes or adopts a guest path. +type GuestWorkspaceRecord struct { + Target string `json:"target"` + ProvisioningID string `json:"provisioning_id"` + Workspace stream.RootIdentity `json:"workspace"` +} +type guestConfigurationProvider struct { + server *Server + keys guestssh.KeyProvider + retirementRunner guestCommandRunner + retirementCurrent func(context.Context, domain.MachineRef, domain.ActorContext) (app.GuestConfiguration, error) +} + +func (p *guestConfigurationProvider) Current(ctx context.Context, targetRef domain.MachineRef, caller domain.ActorContext) (app.GuestConfiguration, error) { + var zero app.GuestConfiguration + if err := ctx.Err(); err != nil { + return zero, err + } + current, err := p.currentActor(ctx, caller) + if err != nil { + return zero, err + } + enrollment, err := p.server.targetService.EnrollmentIdentity(ctx, string(targetRef)) + if err != nil { + return zero, err + } + config, err := guestMachineConfig(ctx, p.keys, targetRef) + if err != nil { + return zero, err + } + user, err := p.keys.GetGuestUser(ctx, targetRef) + if err != nil { + return zero, err + } + endpoint, err := p.keys.GetGuestEndpoint(ctx, targetRef) + if err != nil { + return zero, err + } + signer, err := p.keys.GetClientSigner(ctx, targetRef) + if err != nil { + return zero, err + } + if _, err = p.keys.GetHostKeyCallback(ctx, targetRef); err != nil { + return zero, err + } + record, err := p.workspaceRecord(ctx, targetRef) + if err != nil { + return zero, err + } + authority, err := json.Marshal(struct { + Record GuestWorkspaceRecord + Config *guestssh.MachineSSHConfig + User, Endpoint string + Actor domain.ActorContext + Key []byte + TokenState [][]byte + Enrollment string + }{record, config, user, endpoint, *current, signer.PublicKey().Marshal(), p.server.authStore.ActiveBearerSecrets(), enrollment}) + if err != nil { + return zero, err + } + generation := sha256.Sum256(authority) + // Generation binds the full live principal; operation authority remains + // attenuated to the caller that was checked against that principal. + return app.GuestConfiguration{Generation: hex.EncodeToString(generation[:]), Caller: caller.Clone(), WorkspaceIdentity: record.Workspace}, nil +} + +func (p *guestConfigurationProvider) currentActor(ctx context.Context, caller domain.ActorContext) (*domain.ActorContext, error) { + if err := ctx.Err(); err != nil { + return nil, err + } + tokenType := auth.TokenTypeOperator + if caller.AuthenticatedCaller == auth.AgentMCPPrincipal { + tokenType = auth.TokenTypeAgentMCP + } + token, err := auth.ReadTokenFile(p.server.stateDir.AuthDir(), tokenType) + if err != nil { + return nil, app.ErrGuestAuthority + } + current, _, ok := p.server.authStore.Authenticate(token) + if !ok || current == nil || current.AuthenticatedCaller != caller.AuthenticatedCaller || caller.IsDelegated() || !caller.CallerPermissions.IsSubsetOf(current.CallerPermissions) || !caller.EffectivePermissions.IsSubsetOf(current.EffectivePermissions) { + return nil, app.ErrGuestAuthority + } + return current, nil +} +func (p *guestConfigurationProvider) workspaceRecord(ctx context.Context, targetRef domain.MachineRef) (GuestWorkspaceRecord, error) { + var zero GuestWorkspaceRecord + directory := filepath.Join(p.server.stateDir.Root(), "guest-workspaces") + security := target.NewPrivatePathSecurity() + var err error + if err = security.ValidateDir(ctx, directory); err != nil { + return zero, app.ErrGuestWorkspaceUnprovisioned + } + // A digest of the canonical target avoids path interpretation entirely. + name := sha256.Sum256([]byte(targetRef)) + path := filepath.Join(directory, hex.EncodeToString(name[:])+".json") + if err = security.ValidateFile(ctx, path); err != nil { + return zero, app.ErrGuestWorkspaceUnprovisioned + } + original, err := os.Lstat(path) + if err != nil { + return zero, err + } + root, err := os.OpenRoot(directory) + if err != nil { + return zero, err + } + defer root.Close() + file, err := root.Open(filepath.Base(path)) + if err != nil { + return zero, err + } + defer file.Close() + opened, err := file.Stat() + if err != nil || !os.SameFile(original, opened) { + return zero, app.ErrGuestAuthority + } + if err = security.ValidateFile(ctx, path); err != nil { + return zero, err + } + data, err := io.ReadAll(io.LimitReader(file, GuestMetadataLimit+1)) + if err != nil || len(data) > GuestMetadataLimit { + return zero, app.ErrGuestAuthority + } + if err = rejectDuplicateJSONKeys(data); err != nil { + return zero, app.ErrGuestAuthority + } + return decodeGuestWorkspace(data, targetRef) +} +func decodeGuestWorkspace(data []byte, targetRef domain.MachineRef) (GuestWorkspaceRecord, error) { + var zero GuestWorkspaceRecord + var record GuestWorkspaceRecord + dec := json.NewDecoder(bytes.NewReader(data)) + dec.DisallowUnknownFields() + var extra any + if dec.Decode(&record) != nil || dec.Decode(&extra) != io.EOF || record.Target != string(targetRef) || record.ProvisioningID == "" { + return zero, app.ErrGuestAuthority + } + return record, nil +} + +func (s *Server) initializeGuestService(sessionSvc *app.SessionService, targetService *app.TargetService, keyProvider guestssh.KeyProvider) { + keyProvider = guestKeyProvider{targets: targetService, inner: keyProvider} + guestConfig := s.cfg.GuestConfigProvider + if guestConfig == nil { + guestConfig = &guestConfigurationProvider{server: s, keys: keyProvider} + } + guestTransport := s.cfg.GuestTransport + if guestTransport == nil { + guestTransport = newDefaultGuestTransport(s, keyProvider) + } + s.guestService = app.NewGuestService(sessionSvc, guestTargetResolver{service: targetService}, guestConfig, guestTransport, app.NewGuestStore(filepath.Join(s.stateDir.Root(), "guest-operations"))) +} + +func guestMachineConfig(ctx context.Context, keys guestssh.KeyProvider, targetRef domain.MachineRef) (*guestssh.MachineSSHConfig, error) { + if provider, ok := keys.(interface { + GetMachineConfigContext(context.Context, domain.MachineRef) (*guestssh.MachineSSHConfig, error) + }); ok { + return provider.GetMachineConfigContext(ctx, targetRef) + } + if err := ctx.Err(); err != nil { + return nil, err + } + return keys.GetMachineConfig(targetRef) +} diff --git a/internal/daemon/guest_configuration_authority_test.go b/internal/daemon/guest_configuration_authority_test.go new file mode 100644 index 0000000..6bf3abc --- /dev/null +++ b/internal/daemon/guest_configuration_authority_test.go @@ -0,0 +1,55 @@ +package daemon + +import ( + "errors" + "os" + "path/filepath" + "reflect" + "strings" + "testing" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/auth" + "github.com/Horcag/agent-machine-control/internal/domain" +) + +func TestGuestConfigurationPreservesRequestedAuthority(t *testing.T) { + provider, _, _, _ := newGuestConfigurationFixture(t) + server := provider.server + token, err := auth.ReadTokenFile(server.stateDir.AuthDir(), auth.TokenTypeAgentMCP) + if err != nil { + t.Fatal(err) + } + full, _, ok := server.authStore.Authenticate(token) + if !ok { + t.Fatal("fixture authentication failed") + } + ref := domain.MachineRef("local:11111111-1111-4111-8111-111111111111") + baseline, err := provider.Current(t.Context(), ref, *full) + if err != nil { + t.Fatal(err) + } + narrow := full.Clone() + delete(narrow.CallerPermissions, domain.ScopeMachineRead) + delete(narrow.EffectivePermissions, domain.ScopeMachineRead) + delete(narrow.CallerPermissions, domain.ScopeMachineWrite) + delete(narrow.EffectivePermissions, domain.ScopeMachineWrite) + config, err := provider.Current(t.Context(), ref, narrow) + if err != nil || !reflect.DeepEqual(config.Caller, narrow) || config.Generation != baseline.Generation { + t.Fatalf("requested authority expanded or generation changed: caller=%+v err=%v", config.Caller, err) + } + config.Caller.CallerPermissions[domain.ScopeOperationAdmin] = struct{}{} + config.Caller.EffectivePermissions[domain.ScopeOperationAdmin] = struct{}{} + if narrow.HasScope(domain.ScopeOperationAdmin) || full.HasScope(domain.ScopeOperationAdmin) { + t.Fatal("configuration aliases caller or authenticated authority") + } + if _, err := provider.Current(t.Context(), ref, config.Caller); !errors.Is(err, app.ErrGuestAuthority) { + t.Fatalf("requested privilege expansion accepted: %v", err) + } + if err := os.WriteFile(filepath.Join(server.stateDir.AuthDir(), auth.AgentMCPTokenFileName), []byte(strings.Repeat("a", 64)), 0600); err != nil { + t.Fatal(err) + } + if _, err := provider.Current(t.Context(), ref, narrow); !errors.Is(err, app.ErrGuestAuthority) { + t.Fatalf("rotated agent credential accepted: %v", err) + } +} diff --git a/internal/daemon/guest_configuration_test.go b/internal/daemon/guest_configuration_test.go new file mode 100644 index 0000000..fedd733 --- /dev/null +++ b/internal/daemon/guest_configuration_test.go @@ -0,0 +1,237 @@ +package daemon + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "errors" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/auth" + "github.com/Horcag/agent-machine-control/internal/domain" + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" + "github.com/Horcag/agent-machine-control/internal/statedir" + "github.com/Horcag/agent-machine-control/internal/target" +) + +func TestGuestWorkspaceRequiresOriginalProtectedRecord(t *testing.T) { + sd, err := statedir.Resolve(filepath.Join(t.TempDir(), "state")) + if err != nil { + t.Fatal(err) + } + provider := guestConfigurationProvider{server: &Server{stateDir: sd}} + targetRef := domain.MachineRef("local:11111111-1111-4111-8111-111111111111") + if _, err = provider.workspaceRecord(t.Context(), targetRef); !errors.Is(err, app.ErrGuestWorkspaceUnprovisioned) { + t.Fatalf("missing record error=%v", err) + } + directory := filepath.Join(sd.Root(), "guest-workspaces") + if err = statedir.EnsurePrivateDirectory(directory); err != nil { + t.Fatal(err) + } + sum := sha256.Sum256([]byte(targetRef)) + path := filepath.Join(directory, hex.EncodeToString(sum[:])+".json") + record := GuestWorkspaceRecord{Target: string(targetRef), ProvisioningID: "synthetic-original-provision"} + data, _ := json.Marshal(record) + if err = writeGuestPrivateFixture(t, path, data); err != nil { + t.Fatal(err) + } + loaded, err := provider.workspaceRecord(t.Context(), targetRef) + if err != nil || loaded.ProvisioningID != record.ProvisioningID { + t.Fatalf("record=%+v err=%v", loaded, err) + } + record.Target = "local:22222222-2222-4222-8222-222222222222" + data, _ = json.Marshal(record) + if err = writeGuestPrivateFixture(t, path, data); err != nil { + t.Fatal(err) + } + if _, err = provider.workspaceRecord(t.Context(), targetRef); err == nil { + t.Fatal("foreign target record accepted") + } +} +func TestGuestCurrentActorRejectsRevokedTokenAndDelegation(t *testing.T) { + sd, err := statedir.Resolve(filepath.Join(t.TempDir(), "state")) + if err != nil { + t.Fatal(err) + } + if err = statedir.EnsurePrivateDirectory(sd.AuthDir()); err != nil { + t.Fatal(err) + } + store, err := auth.LoadOrCreate(sd.AuthDir(), auth.WithPrincipalResolver(func() (string, error) { return "operator:synthetic", nil })) + if err != nil { + t.Fatal(err) + } + token, err := auth.ReadTokenFile(sd.AuthDir(), auth.TokenTypeOperator) + if err != nil { + t.Fatal(err) + } + caller, _, ok := store.Authenticate(token) + if !ok { + t.Fatal("fixture authentication failed") + } + provider := guestConfigurationProvider{server: &Server{stateDir: sd, authStore: store}} + if _, err = provider.currentActor(context.Background(), *caller); err != nil { + t.Fatal(err) + } + delegated := caller.Clone() + delegated.EffectiveActor = "agent:mcp-local" + if _, err = provider.currentActor(t.Context(), delegated); err == nil { + t.Fatal("delegation adopted") + } + if err = os.WriteFile(filepath.Join(sd.AuthDir(), auth.OperatorTokenFileName), []byte(strings.Repeat("a", 64)), 0600); err != nil { + t.Fatal(err) + } + if _, err = provider.currentActor(t.Context(), *caller); !errors.Is(err, app.ErrGuestAuthority) { + t.Fatalf("cached token authority accepted after rotation: %v", err) + } +} + +// The configuration and dial contract are distinct: provider defaults and +// endpoint selection must remain in the same revocable generation. +func TestGuestConfigurationBindsEffectiveRouteAndFrozenDefaults(t *testing.T) { + provider, keys, caller, config := newGuestConfigurationFixture(t) + ref := domain.MachineRef("local:11111111-1111-4111-8111-111111111111") + original, err := provider.Current(t.Context(), ref, *caller) + if err != nil { + t.Fatal(err) + } + frozen, err := freezeGuestKeys(t.Context(), keys, ref) + if err != nil { + t.Fatal(err) + } + user, _ := frozen.GetGuestUser(t.Context(), ref) + raw, _ := frozen.GetMachineConfig(ref) + if user != "Administrator" || raw.User != "" { + t.Fatalf("effective default lost or raw config rewritten: %q %+v", user, raw) + } + snapshot := provider + snapshot.keys = frozen + bound, err := snapshot.Current(t.Context(), ref, *caller) + if err != nil || bound.Generation != original.Generation { + t.Fatalf("frozen route not generation-bound: %+v %v", bound, err) + } + assertGuestEffectiveRouteDrift(t, provider, keys, caller, ref, original.Generation, config.Endpoint, frozen) +} + +func assertGuestEffectiveRouteDrift(t *testing.T, provider guestConfigurationProvider, keys *effectiveGuestRoute, caller *domain.ActorContext, ref domain.MachineRef, originalGeneration, originalEndpoint string, frozen *frozenGuestKeys) { + t.Helper() + keys.endpoint = "127.0.0.1:65001" + changed, err := provider.Current(t.Context(), ref, *caller) + if err != nil || changed.Generation == originalGeneration { + t.Fatalf("effective endpoint drift not fenced: %v", err) + } + frozenEndpoint, _ := frozen.GetGuestEndpoint(t.Context(), ref) + if frozenEndpoint != originalEndpoint { + t.Fatal("frozen endpoint drifted") + } + frozenCustom, err := freezeGuestKeys(t.Context(), keys, ref) + if err != nil { + t.Fatal(err) + } + custom, _ := frozenCustom.GetGuestEndpoint(t.Context(), ref) + if custom != keys.endpoint { + t.Fatal("effective custom endpoint lost") + } + keys.user = "changed-user" + changedUser, err := provider.Current(t.Context(), ref, *caller) + if err != nil || changedUser.Generation == changed.Generation { + t.Fatal("effective user drift not fenced") + } + if _, err = provider.retirementCommandRunner(t.Context(), ref, *caller, originalGeneration); !errors.Is(err, app.ErrGuestAuthority) { + t.Fatalf("retirement accepted stale dial generation: %v", err) + } +} + +func newGuestConfigurationFixture(t *testing.T) (guestConfigurationProvider, *effectiveGuestRoute, *domain.ActorContext, guestssh.MachineSSHConfig) { + t.Helper() + sd, targets, local, _ := guestEnrolledKeyFixture(t) + path := filepath.Join(sd.MachinesDir(), "11111111-1111-4111-8111-111111111111", "config.json") + data, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + var config guestssh.MachineSSHConfig + if err = json.Unmarshal(data, &config); err != nil { + t.Fatal(err) + } + config.User = "" + data, _ = json.Marshal(config) + if err = writeGuestPrivateFixture(t, path, data); err != nil { + t.Fatal(err) + } + store, err := auth.LoadOrCreate(sd.AuthDir(), auth.WithPrincipalResolver(func() (string, error) { return "operator:synthetic", nil })) + if err != nil { + t.Fatal(err) + } + token, err := auth.ReadTokenFile(sd.AuthDir(), auth.TokenTypeOperator) + if err != nil { + t.Fatal(err) + } + caller, _, ok := store.Authenticate(token) + if !ok { + t.Fatal("fixture auth") + } + ref := domain.MachineRef("local:11111111-1111-4111-8111-111111111111") + directory := filepath.Join(sd.Root(), "guest-workspaces") + if err = statedir.EnsurePrivateDirectory(directory); err != nil { + t.Fatal(err) + } + name := sha256.Sum256([]byte(ref)) + data, _ = json.Marshal(GuestWorkspaceRecord{Target: string(ref), ProvisioningID: "synthetic-original"}) + if err = writeGuestPrivateFixture(t, filepath.Join(directory, hex.EncodeToString(name[:])+".json"), data); err != nil { + t.Fatal(err) + } + keys := &effectiveGuestRoute{KeyProvider: guestKeyProvider{targets: targets, inner: local}} + provider := guestConfigurationProvider{server: &Server{stateDir: sd, authStore: store, targetService: targets}, keys: keys} + return provider, keys, caller, config +} + +type effectiveGuestRoute struct { + guestssh.KeyProvider + user, endpoint string +} + +func (k *effectiveGuestRoute) GetGuestUser(ctx context.Context, ref domain.MachineRef) (string, error) { + if k.user != "" { + return k.user, nil + } + return k.KeyProvider.GetGuestUser(ctx, ref) +} +func (k *effectiveGuestRoute) GetGuestEndpoint(ctx context.Context, ref domain.MachineRef) (string, error) { + if k.endpoint != "" { + return k.endpoint, nil + } + return k.KeyProvider.GetGuestEndpoint(ctx, ref) +} + +func TestGuestWorkspaceRejectsUntrustedMetadata(t *testing.T) { + ref := domain.MachineRef("local:11111111-1111-4111-8111-111111111111") + for _, data := range []string{`{"target":"` + string(ref) + `","provisioning_id":"x","path":"untrusted"}`, `{"target":"` + string(ref) + `","provisioning_id":""}`, `{}`, `{} {}`} { + if _, err := decodeGuestWorkspace([]byte(data), ref); !errors.Is(err, app.ErrGuestAuthority) { + t.Fatalf("untrusted metadata accepted: %s %v", data, err) + } + } +} + +// Test files use the same private creation guard as server-owned records. +func writeGuestPrivateFixture(t *testing.T, path string, data []byte) error { + t.Helper() + file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600) + if err != nil { + return err + } + defer file.Close() + security := target.NewPrivatePathSecurity() + if err = security.ProtectNewFile(t.Context(), path); err != nil { + return err + } + if err = security.ValidateFile(t.Context(), path); err != nil { + return err + } + _, err = file.Write(data) + return err +} diff --git a/internal/daemon/guest_errors.go b/internal/daemon/guest_errors.go new file mode 100644 index 0000000..0dcb8cb --- /dev/null +++ b/internal/daemon/guest_errors.go @@ -0,0 +1,86 @@ +package daemon + +import ( + "context" + "errors" + "net/http" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + "github.com/Horcag/agent-machine-control/internal/policy" + "github.com/Horcag/agent-machine-control/internal/receipt" +) + +// Guest failures carry allowlisted categories and fixed messages, never provider text. +// Uncertainty takes precedence over joined causes: a dispatched effect must not +// be presented as a safe pre-dispatch rejection. +func guestFailureCategory(err error) string { + if errors.Is(err, app.ErrGuestUncertain) { + return "guest_uncertain" + } + if denied, ok := errors.AsType[*app.PolicyDeniedError](err); ok { + switch denied.Reason { + case policy.DenialApprovalRequired, policy.DenialApprovalExpired, policy.DenialApprovalConsumed, + policy.DenialApprovalMismatch, policy.DenialApprovalNotYetValid, policy.DenialMissingScope, + policy.DenialMissingSensitiveEvidenceScope, policy.DenialRollbackMissing, policy.DenialForbidden, + policy.DenialDelegationExceeded, policy.DenialMissingCapability: + return string(denied.Reason) + default: + return "forbidden" + } + } + classes := []struct { + category string + causes []error + }{ + {"guest_workspace_unprovisioned", []error{app.ErrGuestWorkspaceUnprovisioned}}, + {"guest_authority", []error{app.ErrGuestAuthority}}, + {"guest_not_prepared", []error{app.ErrGuestNotPrepared}}, + {"guest_ownership", []error{app.ErrGuestOwnership}}, + {"invalid_argument", []error{domain.ErrNonCanonicalParameter, domain.ErrInvalidOperationKind, domain.ErrMissingDeadline, domain.ErrMissingReason, domain.ErrInvalidReason, domain.ErrMissingIdempotencyKey, domain.ErrInvalidIdempotencyKey, stream.ErrRequest}}, + {"forbidden", []error{domain.ErrSessionAccessDenied, domain.ErrDelegationExceedsAuthority}}, + {"conflict", []error{domain.ErrSessionConflict, receipt.ErrIdempotencyCollision}}, + {"timeout", []error{context.DeadlineExceeded}}, + {"cancelled", []error{context.Canceled}}, + } + for _, class := range classes { + for _, cause := range class.causes { + if errors.Is(err, cause) { + return class.category + } + } + } + if class, known := classifyTargetFailure(err); known { + return class.category + } + return "guest_uncertain" +} + +// Only legacy clients need the early HTTP error path. A started stream or an +// execution receipt cannot be rewritten as a rejection: its outcome stays in +// the final trailer, and the client must verify it before taking another action. +func writeGuestEarlyError(w http.ResponseWriter, category string, result app.GuestOperationResult, started bool) bool { + if started || category == "guest_uncertain" || (result.Receipt != nil && result.Receipt.Outcome.Status != domain.OutcomeDenied) { + return false + } + status := http.StatusForbidden + switch category { + case "invalid_argument", "invalid_target", "target_mismatch": + status = http.StatusBadRequest + case "guest_not_prepared", "guest_ownership", "conflict", "target_not_enrolled": + status = http.StatusConflict + case "timeout": + status = http.StatusGatewayTimeout + case "cancelled": + status = http.StatusRequestTimeout + case "target_unavailable", "host_unavailable", "module_missing", "backend_unavailable": + status = http.StatusServiceUnavailable + case "machine_not_found": + status = http.StatusNotFound + } + w.Header().Del("Trailer") + w.Header().Del(GuestResultTrailer) + writeError(w, status, category, "guest operation rejected before dispatch; inspect its category") + return true +} diff --git a/internal/daemon/guest_errors_test.go b/internal/daemon/guest_errors_test.go new file mode 100644 index 0000000..002675b --- /dev/null +++ b/internal/daemon/guest_errors_test.go @@ -0,0 +1,182 @@ +package daemon + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "io" + "net/http/httptest" + "strings" + "testing" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/policy" +) + +func TestGuestFailureClassificationRedactsAndPrioritizesUncertainty(t *testing.T) { + for _, tc := range []struct { + err error + category string + }{ + {fmt.Errorf("private-secret: %w", domain.ErrNonCanonicalParameter), "invalid_argument"}, + {&app.PolicyDeniedError{Reason: policy.DenialApprovalRequired, Message: "private-secret"}, "approval_required"}, + {app.ErrGuestWorkspaceUnprovisioned, "guest_workspace_unprovisioned"}, + {errors.Join(app.ErrGuestUncertain, domain.ErrNonCanonicalParameter), "guest_uncertain"}, + {errors.New("private-secret"), "guest_uncertain"}, + } { + if got := guestFailureCategory(tc.err); got != tc.category || strings.Contains(got, "private") { + t.Fatalf("category=%q want=%q", got, tc.category) + } + } +} + +func TestGuestHTTPMalformedShapesRejectedByRealServiceWithoutDispatch(t *testing.T) { + server, token, fixture := newGuestHTTPServiceFixture(t) + for _, operation := range []string{"prepare", "put", "get", "exec", "remove-owned"} { + t.Run(operation, func(t *testing.T) { + p, payload := guestHTTPCase(operation, strings.Repeat("a", 32)) + switch operation { + case "prepare", "put", "remove-owned": + p.MaxOutput = 64 + case "get": + p.ArtifactIDs = nil + case "exec": + p.MaxOutput = 16*1024*1024 + 1 + } + for range 2 { + final, body := invokeGuestHTTPFixture(t, server.URL, token, p, payload) + if final.Category != "invalid_argument" || final.Error == "" || len(body) != 0 || len(fixture.operations) != 0 { + t.Fatalf("final=%+v dispatch=%v", final, fixture.operations) + } + } + }) + } +} + +func TestGuestHTTPPreDispatchDenialsRemainTyped(t *testing.T) { + for _, tc := range []struct { + name, category string + configure func(*guestHTTPServiceFixture) + }{ + {"workspace", "guest_workspace_unprovisioned", func(f *guestHTTPServiceFixture) { + f.configErr = fmt.Errorf("private-secret: %w", app.ErrGuestWorkspaceUnprovisioned) + }}, + {"approval", "approval_required", func(f *guestHTTPServiceFixture) { f.privileged = true }}, + } { + t.Run(tc.name, func(t *testing.T) { + server, token, fixture := newGuestHTTPServiceFixture(t) + tc.configure(fixture) + p, payload := guestHTTPCase("prepare", "") + for range 2 { + final, body := invokeGuestHTTPFixture(t, server.URL, token, p, payload) + encoded, _ := json.Marshal(final) + if final.Category != tc.category || len(body) != 0 || len(fixture.operations) != 0 || strings.Contains(string(encoded), "private-secret") { + t.Fatalf("final=%s dispatch=%v", encoded, fixture.operations) + } + } + }) + } +} + +func TestGuestLegacyHTTPEarlyFailuresUseStandardEnvelope(t *testing.T) { + for _, tc := range []struct { + name, category string + status int + configure func(*guestHTTPServiceFixture, *app.GuestOperationParams) + }{ + {"invalid-prepare", "invalid_argument", 400, func(_ *guestHTTPServiceFixture, p *app.GuestOperationParams) { p.MaxOutput = 64 }}, + {"approval", "approval_required", 403, func(f *guestHTTPServiceFixture, _ *app.GuestOperationParams) { f.privileged = true }}, + {"workspace", "guest_workspace_unprovisioned", 403, func(f *guestHTTPServiceFixture, _ *app.GuestOperationParams) { + f.configErr = app.ErrGuestWorkspaceUnprovisioned + }}, + } { + t.Run(tc.name, func(t *testing.T) { + server, token, fixture := newGuestHTTPServiceFixture(t) + p, payload := guestHTTPCase("prepare", "") + tc.configure(fixture, &p) + response := guestHTTPFixtureResponse(t, server.URL, token, p, payload, false) + defer response.Body.Close() + var envelope ErrorEnvelope + if err := json.NewDecoder(response.Body).Decode(&envelope); err != nil { + t.Fatal(err) + } + if response.StatusCode != tc.status || envelope.Error.Category != tc.category || response.Header.Get("Trailer") != "" || len(fixture.operations) != 0 { + t.Fatalf("status=%d envelope=%+v operations=%v", response.StatusCode, envelope, fixture.operations) + } + }) + } +} + +func TestGuestLegacyHTTPDispatchedFailureRetainsStrictTrailer(t *testing.T) { + server, token, fixture := newGuestHTTPServiceFixture(t) + fixture.prepareErr = errors.New("private-secret-transport-error") + p, payload := guestHTTPCase("prepare", "") + response := guestHTTPFixtureResponse(t, server.URL, token, p, payload, false) + defer response.Body.Close() + body, err := io.ReadAll(response.Body) + if err != nil { + t.Fatal(err) + } + data, err := base64.RawStdEncoding.DecodeString(response.Trailer.Get(GuestResultTrailer)) + if err != nil { + t.Fatal(err) + } + // The installed legacy client has only these fields and rejects unknown fields. + var legacy struct { + Result app.GuestOperationResult `json:"result"` + Error string `json:"error,omitempty"` + } + decoder := json.NewDecoder(bytes.NewReader(data)) + decoder.DisallowUnknownFields() + if err = decoder.Decode(&legacy); err != nil { + t.Fatalf("legacy decoder: %v data=%s", err, data) + } + if response.StatusCode != 200 || len(body) != 0 || legacy.Error == "" || legacy.Result.Receipt == nil || legacy.Result.Receipt.Outcome.Status != domain.OutcomeFailed || strings.Contains(string(data), "private-secret") { + t.Fatalf("status=%d legacy=%+v body=%q", response.StatusCode, legacy, body) + } +} + +func TestGuestEarlyHTTPErrorNeverRewritesStreamedOrExecutedOutcome(t *testing.T) { + for _, tc := range []struct { + name, category string + started bool + receipt *domain.Receipt + }{ + {"stream-started", "invalid_argument", true, nil}, + {"uncertain", "guest_uncertain", false, nil}, + {"failed-receipt", "invalid_argument", false, &domain.Receipt{Outcome: domain.ExecutionOutcome{Status: domain.OutcomeFailed}}}, + {"success-receipt", "invalid_argument", false, &domain.Receipt{Outcome: domain.ExecutionOutcome{Status: domain.OutcomeSuccess}}}, + } { + t.Run(tc.name, func(t *testing.T) { + response := httptest.NewRecorder() + if writeGuestEarlyError(response, tc.category, app.GuestOperationResult{Receipt: tc.receipt}, tc.started) || response.Body.Len() != 0 { + t.Fatal("streamed/executed outcome rewritten as early rejection") + } + }) + } +} + +func TestGuestLegacyRejectionStatusContractClearsStreamHeaders(t *testing.T) { + for _, tc := range []struct { + category string + status int + }{ + {"invalid_argument", 400}, {"conflict", 409}, {"timeout", 504}, {"cancelled", 408}, + {"target_unavailable", 503}, {"machine_not_found", 404}, {"approval_required", 403}, + } { + response := httptest.NewRecorder() + response.Header().Set("Trailer", GuestResultTrailer) + response.Header().Set(GuestResultTrailer, "discard-stream-metadata") + denied := &domain.Receipt{Outcome: domain.ExecutionOutcome{Status: domain.OutcomeDenied}} + if !writeGuestEarlyError(response, tc.category, app.GuestOperationResult{Receipt: denied}, false) { + t.Fatal("denied receipt not recognized") + } + var envelope ErrorEnvelope + if json.Unmarshal(response.Body.Bytes(), &envelope) != nil || response.Code != tc.status || envelope.Error.Category != tc.category || response.Header().Get("Trailer") != "" || response.Header().Get(GuestResultTrailer) != "" { + t.Fatalf("response=%+v", response) + } + } +} diff --git a/internal/daemon/guest_key_fixture_other_test.go b/internal/daemon/guest_key_fixture_other_test.go new file mode 100644 index 0000000..092103f --- /dev/null +++ b/internal/daemon/guest_key_fixture_other_test.go @@ -0,0 +1,15 @@ +//go:build !windows + +package daemon + +import ( + "path/filepath" + "testing" + + "github.com/Horcag/agent-machine-control/internal/statedir" +) + +func writeGuestFixtureKeyMaterial(t *testing.T, sd *statedir.StateDir, material []byte) error { + t.Helper() + return writeGuestPrivateFixture(t, filepath.Join(sd.KeysDir(), "default.key"), material) +} diff --git a/internal/daemon/guest_key_fixture_windows_test.go b/internal/daemon/guest_key_fixture_windows_test.go new file mode 100644 index 0000000..cd53911 --- /dev/null +++ b/internal/daemon/guest_key_fixture_windows_test.go @@ -0,0 +1,93 @@ +//go:build windows + +package daemon + +import ( + "errors" + "io" + "os" + "path/filepath" + "testing" + "unsafe" + + "github.com/Horcag/agent-machine-control/internal/statedir" + "golang.org/x/sys/windows" +) + +// Windows material is CurrentUser DPAPI, protected solely for the service SID. +// Never place synthetic plaintext keys on disk or relax the production reader. +func writeGuestFixtureKeyMaterial(t *testing.T, state *statedir.StateDir, material []byte) error { + t.Helper() + if len(material) == 0 || len(material) > 16384 { + return errors.New("synthetic key size invalid") + } + descriptor, err := guestFixtureKeyDescriptor(t, state.KeysDir()) + if err != nil { + return err + } + // #nosec G115 -- synthetic material is explicitly bounded to 16 KiB above. + input := windows.DataBlob{Size: uint32(len(material)), Data: &material[0]} + var output windows.DataBlob + if err = windows.CryptProtectData(&input, nil, nil, 0, nil, windows.CRYPTPROTECT_UI_FORBIDDEN, &output); err != nil { + return err + } + if output.Data != nil { + defer windows.LocalFree(windows.Handle(unsafe.Pointer(output.Data))) + } + if output.Data == nil || output.Size == 0 { + return errors.New("empty synthetic DPAPI material") + } + path, err := windows.UTF16PtrFromString(filepath.Join(state.KeysDir(), "default.dpapi")) + if err != nil { + return err + } + attributes := windows.SecurityAttributes{Length: uint32(unsafe.Sizeof(windows.SecurityAttributes{})), SecurityDescriptor: descriptor} + handle, err := windows.CreateFile(path, windows.GENERIC_READ|windows.GENERIC_WRITE, 0, &attributes, windows.CREATE_NEW, windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_OPEN_REPARSE_POINT, 0) + if err != nil { + return err + } + file := os.NewFile(uintptr(handle), filepath.Join(state.KeysDir(), "default.dpapi")) + defer file.Close() + protected := unsafe.Slice(output.Data, output.Size) + count, err := file.Write(protected) + if err != nil { + return err + } + if count != len(protected) { + return io.ErrShortWrite + } + return file.Sync() +} + +func guestFixtureKeyDescriptor(t *testing.T, directory string) (*windows.SECURITY_DESCRIPTOR, error) { + t.Helper() + entries, err := os.ReadDir(directory) + if err != nil { + return nil, err + } + if len(entries) != 0 { + return nil, errors.New("fixture key directory is not empty") + } + token, err := windows.OpenCurrentProcessToken() + if err != nil { + return nil, err + } + defer token.Close() + user, err := token.GetTokenUser() + if err != nil { + return nil, err + } + sid := user.User.Sid + descriptor, err := windows.SecurityDescriptorFromString("O:" + sid.String() + "D:P(A;OICI;FA;;;" + sid.String() + ")") + if err != nil { + return nil, err + } + acl, _, err := descriptor.DACL() + if err != nil { + return nil, err + } + if err = windows.SetNamedSecurityInfo(directory, windows.SE_FILE_OBJECT, windows.OWNER_SECURITY_INFORMATION|windows.DACL_SECURITY_INFORMATION|windows.PROTECTED_DACL_SECURITY_INFORMATION, sid, nil, acl, nil); err != nil { + return nil, err + } + return descriptor, nil +} diff --git a/internal/daemon/guest_keys.go b/internal/daemon/guest_keys.go new file mode 100644 index 0000000..d6bf77b --- /dev/null +++ b/internal/daemon/guest_keys.go @@ -0,0 +1,77 @@ +package daemon + +import ( + "context" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/domain" + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" + gossh "golang.org/x/crypto/ssh" +) + +// guestKeyProvider maps only protected local canonical identities to the existing +// raw provider GUID boundary. Session and direct recovery key routing is unchanged. +type guestKeyProvider struct { + targets *app.TargetService + inner guestssh.KeyProvider +} + +func (p guestKeyProvider) target(ctx context.Context, target domain.MachineRef) (domain.MachineRef, error) { + locator, err := domain.ParseMachineLocator(string(target)) + if err != nil || locator.HostID != domain.LocalHostID { + return "", app.ErrGuestAuthority + } + if _, err = p.targets.EnrollmentIdentity(ctx, locator.String()); err != nil { + return "", err + } + return domain.MachineRef(locator.VMID), nil +} +func (p guestKeyProvider) GetMachineConfig(target domain.MachineRef) (*guestssh.MachineSSHConfig, error) { + return p.GetMachineConfigContext(context.Background(), target) +} +func (p guestKeyProvider) GetMachineConfigContext(ctx context.Context, target domain.MachineRef) (*guestssh.MachineSSHConfig, error) { + guid, err := p.target(ctx, target) + if err != nil { + return nil, err + } + if inner, ok := p.inner.(interface { + GetMachineConfigContext(context.Context, domain.MachineRef) (*guestssh.MachineSSHConfig, error) + }); ok { + return inner.GetMachineConfigContext(ctx, guid) + } + return p.inner.GetMachineConfig(guid) +} +func (p guestKeyProvider) GetClientSigner(ctx context.Context, target domain.MachineRef) (gossh.Signer, error) { + guid, err := p.target(ctx, target) + if err != nil { + return nil, err + } + return p.inner.GetClientSigner(ctx, guid) +} +func (p guestKeyProvider) GetHostKeyCallback(ctx context.Context, target domain.MachineRef) (gossh.HostKeyCallback, error) { + guid, err := p.target(ctx, target) + if err != nil { + return nil, err + } + return p.inner.GetHostKeyCallback(ctx, guid) +} +func (p guestKeyProvider) GetGuestUser(ctx context.Context, target domain.MachineRef) (string, error) { + guid, err := p.target(ctx, target) + if err != nil { + return "", err + } + return p.inner.GetGuestUser(ctx, guid) +} +func (p guestKeyProvider) GetGuestEndpoint(ctx context.Context, target domain.MachineRef) (string, error) { + guid, err := p.target(ctx, target) + if err != nil { + return "", err + } + return p.inner.GetGuestEndpoint(ctx, guid) +} + +type guestTargetResolver struct{ service *app.TargetService } + +func (r guestTargetResolver) ResolveTarget(ctx context.Context, reference string) (app.TargetResolution, error) { + return r.service.ResolveGuestTarget(ctx, reference) +} diff --git a/internal/daemon/guest_keys_test.go b/internal/daemon/guest_keys_test.go new file mode 100644 index 0000000..b034cbf --- /dev/null +++ b/internal/daemon/guest_keys_test.go @@ -0,0 +1,127 @@ +package daemon + +import ( + "bytes" + "context" + "crypto/ed25519" + "crypto/rand" + "encoding/json" + "encoding/pem" + "io" + "path/filepath" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/domain" + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" + "github.com/Horcag/agent-machine-control/internal/guest/ssh/fakeserver" + "github.com/Horcag/agent-machine-control/internal/statedir" + "github.com/Horcag/agent-machine-control/internal/target" + gossh "golang.org/x/crypto/ssh" +) + +func guestEnrolledKeyFixture(t *testing.T) (*statedir.StateDir, *app.TargetService, guestssh.KeyProvider, *fakeserver.FakeSSHServer) { + t.Helper() + sd, err := statedir.Resolve(filepath.Join(t.TempDir(), "state")) + if err != nil { + t.Fatal(err) + } + if err = sd.EnsureDirs(); err != nil { + t.Fatal(err) + } + signer := writeGuestFixtureClientKey(t, sd) + server, err := fakeserver.New(fakeserver.ModeEcho, signer.PublicKey()) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = server.Close() }) + guid := "11111111-1111-4111-8111-111111111111" + directory := filepath.Join(sd.MachinesDir(), guid) + if err = statedir.EnsurePrivateDirectory(directory); err != nil { + t.Fatal(err) + } + data, _ := json.Marshal(guestssh.MachineSSHConfig{Endpoint: server.Addr(), User: "synthetic-user", DefaultKeyAlias: "default", PinnedHostKeySHA256: server.HostKeyPin()}) + if err = writeGuestPrivateFixture(t, filepath.Join(directory, "config.json"), data); err != nil { + t.Fatal(err) + } + store, err := target.NewStore(sd.TargetsDir()) + if err != nil { + t.Fatal(err) + } + locator, err := domain.ParseMachineLocator("local:" + guid) + if err != nil { + t.Fatal(err) + } + value, err := target.NewDefault(locator, nil) + if err != nil { + t.Fatal(err) + } + if _, err = store.Save(t.Context(), value); err != nil { + t.Fatal(err) + } + inventory, err := app.NewTrustedInventory(nil) + if err != nil { + t.Fatal(err) + } + targets, err := app.NewTargetService(inventory, store) + if err != nil { + t.Fatal(err) + } + return sd, targets, guestssh.NewLocalKeyProvider(sd), server +} +func TestGuestCanonicalLocalKeyProviderPinnedDuplex(t *testing.T) { + _, targets, local, server := guestEnrolledKeyFixture(t) + keys := guestKeyProvider{targets: targets, inner: local} + targetRef := domain.MachineRef("local:11111111-1111-4111-8111-111111111111") + config, err := keys.GetMachineConfigContext(t.Context(), targetRef) + if err != nil || config.Endpoint != server.Addr() { + t.Fatalf("raw GUID config mapping: %+v %v", config, err) + } + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + duplex, err := guestssh.NewTransport(keys).OpenDuplex(ctx, targetRef, "synthetic-observation") + if err != nil { + t.Fatal(err) + } + defer duplex.Close() + prefix := make([]byte, len("PS C:\\> ")) + if _, err = io.ReadFull(duplex, prefix); err != nil { + t.Fatal(err) + } + payload := []byte{0, 255, 128, 1} + if _, err = duplex.Write(payload); err != nil { + t.Fatal(err) + } + received := make([]byte, len(payload)) + if _, err = io.ReadFull(duplex, received); err != nil || !bytes.Equal(received, payload) { + t.Fatalf("binary pinned route: %v %v", received, err) + } + if _, err = keys.GetGuestEndpoint(ctx, "local:22222222-2222-4222-8222-222222222222"); err == nil { + t.Fatal("foreign canonical identity reached provider") + } + if _, err = keys.GetGuestEndpoint(ctx, "11111111-1111-4111-8111-111111111111"); err == nil { + t.Fatal("unvalidated raw GUID bypassed protected mapping") + } +} + +func writeGuestFixtureClientKey(t *testing.T, sd *statedir.StateDir) gossh.Signer { + t.Helper() + _, key, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + signer, err := gossh.NewSignerFromKey(key) + if err != nil { + t.Fatal(err) + } + block, err := gossh.MarshalPrivateKey(key, "") + if err != nil { + t.Fatal(err) + } + if err = writeGuestFixtureKeyMaterial(t, sd, pem.EncodeToMemory(block)); err != nil { + t.Fatal(err) + } + + return signer +} diff --git a/internal/daemon/guest_protocol.go b/internal/daemon/guest_protocol.go new file mode 100644 index 0000000..4e235ba --- /dev/null +++ b/internal/daemon/guest_protocol.go @@ -0,0 +1,222 @@ +package daemon + +import ( + "bytes" + "context" + "encoding/base64" + "encoding/binary" + "encoding/json" + "io" + "net/http" + "time" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +const GuestMetadataLimit = 64 * 1024 +const GuestResultTrailer = "X-Amc-Guest-Result" + +// GuestErrorProtocolHeader opts into typed trailer failures; older clients decode +// trailers strictly and receive ordinary HTTP errors for early rejections. +const GuestErrorProtocolHeader = "X-Amc-Guest-Errors" +const GuestErrorProtocolVersion = "categories-v1" +const GuestContentType = "application/vnd.amc.guest-stream" + +type GuestFinal struct { + Result app.GuestOperationResult `json:"result"` + Error string `json:"error,omitempty"` + Category string `json:"category,omitempty"` +} +type GuestApprovalRequest struct { + Params app.GuestOperationParams `json:"params"` + Beneficiary string `json:"beneficiary,omitempty"` + ValidForMillis int64 `json:"valid_for_millis"` +} +type GuestApprovalResponse struct { + Grant *app.SessionApprovalGrant `json:"grant"` + Receipt *domain.Receipt `json:"receipt"` +} + +func DecodeGuestMetadata(r io.Reader) (app.GuestOperationParams, int64, error) { + var p app.GuestOperationParams + var size uint32 + if err := binary.Read(r, binary.BigEndian, &size); err != nil { + return p, 0, err + } + if size == 0 || size > GuestMetadataLimit { + return p, 0, stream.ErrRequest + } + data := make([]byte, size) + if _, err := io.ReadFull(r, data); err != nil { + return p, 0, err + } + if err := rejectDuplicateJSONKeys(data); err != nil { + return p, 0, err + } + dec := json.NewDecoder(bytes.NewReader(data)) + dec.DisallowUnknownFields() + if err := dec.Decode(&p); err != nil { + return p, 0, err + } + var extra any + if dec.Decode(&extra) != io.EOF || p.Size < 0 || p.Size > stream.MaxTransferSize { + return p, 0, stream.ErrRequest + } + return p, int64(size) + 4, nil +} + +// The controller bounds reads on the exact inbound connection. Body.Close alone +// cannot interrupt a server body read. The callback is joined before return. +func guestIOContext(ctx context.Context, w http.ResponseWriter, body io.ReadCloser, deadline time.Time) (func(), error) { + controller := http.NewResponseController(w) + if err := controller.SetReadDeadline(deadline); err != nil { + return nil, err + } + if err := controller.SetWriteDeadline(deadline); err != nil { + return nil, err + } + done := make(chan struct{}) + stop := context.AfterFunc(ctx, func() { + defer close(done) + _ = controller.SetReadDeadline(time.Now()) + _ = controller.SetWriteDeadline(time.Now()) + _ = body.Close() + }) + return func() { + if !stop() { + <-done + } + }, nil +} + +type guestHTTPInput struct{ io.Reader } + +func (r guestHTTPInput) ReadContext(ctx context.Context, p []byte) (int, error) { + if err := ctx.Err(); err != nil { + return 0, err + } + return r.Read(p) +} + +type guestHTTPOutput struct { + http.ResponseWriter + started bool +} + +func (w *guestHTTPOutput) Write(p []byte) (int, error) { + w.started = true + return w.ResponseWriter.Write(p) +} + +func (w *guestHTTPOutput) WriteContext(ctx context.Context, p []byte) (int, error) { + if err := ctx.Err(); err != nil { + return 0, err + } + return w.Write(p) +} + +func (s *Server) handleGuestOperation(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + writeError(w, 405, "method_not_allowed", "method not allowed") + return + } + // Bound metadata reads before the caller deadline has been decoded. + stop, err := guestIOContext(r.Context(), w, r.Body, time.Now().Add(30*time.Second)) + if err != nil { + writeError(w, 400, "invalid_argument", "connection does not support guest stream deadlines") + return + } + defer stop() + p, err := readGuestHTTPRequest(r) + if err != nil { + writeError(w, 400, "invalid_argument", "invalid guest stream metadata or length") + return + } + caller, ok := getCallerContext(r.Context()) + if !ok { + writeError(w, 401, "unauthorized", "unauthenticated caller") + return + } + p.Caller = caller + ctx, cancel := context.WithDeadline(r.Context(), p.Deadline) + defer cancel() + deadlineStop, deadlineErr := guestIOContext(ctx, w, r.Body, p.Deadline) + if deadlineErr != nil { + return + } + defer deadlineStop() + _ = http.NewResponseController(w).SetReadDeadline(p.Deadline) + _ = http.NewResponseController(w).SetWriteDeadline(p.Deadline) + w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Trailer", GuestResultTrailer) + var input io.Reader + var output io.Writer + streamOutput := &guestHTTPOutput{ResponseWriter: w} + if p.Operation == "put" || p.Operation == "exec" { + input = guestHTTPInput{r.Body} + } + if p.Operation == "get" || p.Operation == "exec" { + output = streamOutput + } + result, opErr := s.guestService.Execute(ctx, p, input, output) + writeGuestHTTPFinal(w, r.Header.Get(GuestErrorProtocolHeader), result, opErr, streamOutput.started) +} + +func writeGuestHTTPFinal(w http.ResponseWriter, errorProtocol string, result app.GuestOperationResult, opErr error, started bool) { + final := GuestFinal{Result: result} + if opErr != nil { + category := guestFailureCategory(opErr) + if errorProtocol == GuestErrorProtocolVersion { + final.Category = category + } else if writeGuestEarlyError(w, category, result, started) { + return + } + final.Error = "guest operation failed; inspect its category and receipt before taking another action" + if category == "guest_workspace_unprovisioned" { + final.Error = "guest workspace is not provisioned; an operator must install original authorized workspace identity metadata" + } + } + data, err := json.Marshal(final) + if err != nil || len(data) > GuestMetadataLimit { + return + } + w.Header().Set(GuestResultTrailer, base64.RawStdEncoding.EncodeToString(data)) +} +func (s *Server) handleGuestApproval(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + writeError(w, 405, "method_not_allowed", "method not allowed") + return + } + caller, ok := getCallerContext(r.Context()) + if !ok || caller.IsDelegated() || !caller.HasScope(domain.ScopeSessionAdmin) { + writeError(w, 403, "forbidden", "operator session:admin authority required") + return + } + var req GuestApprovalRequest + if decodeStrictJSONRequest(r, GuestMetadataLimit, &req) != nil || req.ValidForMillis < 1000 || req.ValidForMillis > 300000 { + writeError(w, 400, "invalid_argument", "invalid guest approval metadata") + return + } + req.Params.Caller = caller + grant, receipt, err := s.guestService.IssueApprovalFor(r.Context(), req.Params, time.Duration(req.ValidForMillis)*time.Millisecond, req.Beneficiary) + if err != nil { + if !writeGuestEarlyError(w, guestFailureCategory(err), app.GuestOperationResult{}, false) { + s.mapSessionError(w, err) + } + return + } + writeJSON(w, 200, GuestApprovalResponse{grant, receipt}) +} + +func readGuestHTTPRequest(r *http.Request) (app.GuestOperationParams, error) { + if r.Header.Get("Content-Type") != GuestContentType || r.ContentLength < 4 { + return app.GuestOperationParams{}, stream.ErrRequest + } + p, prefix, err := DecodeGuestMetadata(r.Body) + if err != nil || r.ContentLength-prefix != p.Size || p.Deadline.IsZero() { + return app.GuestOperationParams{}, stream.ErrRequest + } + return p, nil +} diff --git a/internal/daemon/guest_protocol_test.go b/internal/daemon/guest_protocol_test.go new file mode 100644 index 0000000..887ea21 --- /dev/null +++ b/internal/daemon/guest_protocol_test.go @@ -0,0 +1,188 @@ +package daemon + +import ( + "bytes" + "context" + "encoding/binary" + "encoding/json" + "fmt" + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/auth" + "github.com/Horcag/agent-machine-control/internal/domain" + "io" + "net" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" +) + +type fragmentedGuestReader struct{ io.Reader } + +func (r fragmentedGuestReader) Read(p []byte) (int, error) { + if len(p) > 1 { + p = p[:1] + } + return r.Reader.Read(p) +} +func TestGuestMetadataStrictFraming(t *testing.T) { + for _, tc := range []struct { + name, json string + valid bool + }{ + {"valid", `{"operation":"put","size":3}`, true}, + {"duplicate", `{"size":1,"size":2}`, false}, + {"unknown", `{"operation":"put","path":"C:\\secret","size":3}`, false}, + {"extra", `{"size":0} {}`, false}, + {"negative", `{"size":-1}`, false}, + } { + t.Run(tc.name, func(t *testing.T) { + var b bytes.Buffer + // #nosec G115 -- fixed synthetic test metadata is less than 64KiB. + _ = binary.Write(&b, binary.BigEndian, uint32(len(tc.json))) + b.WriteString(tc.json) + b.WriteString("raw") + p, n, err := DecodeGuestMetadata(fragmentedGuestReader{&b}) + if (err == nil) != tc.valid { + t.Fatalf("result=%+v prefix=%d error=%v", p, n, err) + } + if tc.valid && b.String() != "raw" { + t.Fatal("metadata consumed payload") + } + }) + } + for _, data := range [][]byte{{}, {0, 0, 0, 0}, {0, 1, 0, 1}, {0, 0, 0, 3, '{', '}'}} { + if _, _, err := DecodeGuestMetadata(bytes.NewReader(data)); err == nil { + t.Fatal("accepted truncated or oversized frame") + } + } +} +func TestGuestMetadataCannotDeserializeCaller(t *testing.T) { + var p app.GuestOperationParams + if err := json.Unmarshal([]byte(`{"caller":{"EffectiveActor":"operator:other"}}`), &p); err != nil { + t.Fatal(err) + } + if p.Caller.EffectiveActor != "" { + t.Fatal("public caller was trusted") + } +} + +func TestGuestInboundCancellationUnblocksRead(t *testing.T) { + finished := make(chan error, 1) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx, cancel := context.WithTimeout(r.Context(), 50*time.Millisecond) + defer cancel() + stop, err := guestIOContext(ctx, w, r.Body, time.Now().Add(time.Second)) + if err != nil { + finished <- err + return + } + defer stop() + _, err = io.Copy(io.Discard, r.Body) + finished <- err + })) + defer server.Close() + host := strings.TrimPrefix(server.URL, "http://") + connection, err := net.Dial("tcp", host) + if err != nil { + t.Fatal(err) + } + defer connection.Close() + _, err = fmt.Fprintf(connection, "POST / HTTP/1.1\r\nHost: %s\r\nContent-Length: 100\r\n\r\nx", host) + if err != nil { + t.Fatal(err) + } + select { + case err := <-finished: + if err == nil { + t.Fatal("incomplete body accepted") + } + case <-time.After(time.Second): + t.Fatal("inbound cancellation did not interrupt body read") + } +} + +func TestGuestHTTPRejectsUnsupportedConnectionAndMethods(t *testing.T) { + server := &Server{} + for _, tc := range []struct { + method string + status int + }{{http.MethodGet, 405}, {http.MethodPost, 400}} { + request := httptest.NewRequest(tc.method, "/v1/guest/operations", strings.NewReader("untrusted")) + response := httptest.NewRecorder() + server.handleGuestOperation(response, request) + if response.Code != tc.status { + t.Fatalf("method=%s status=%d", tc.method, response.Code) + } + } + response := httptest.NewRecorder() + server.handleGuestApproval(response, httptest.NewRequest(http.MethodGet, "/v1/guest/approvals", nil)) + if response.Code != 405 { + t.Fatal("approval method admitted") + } +} +func TestGuestApprovalRejectsUntrustedAuthorityAndEnvelope(t *testing.T) { + server := &Server{} + operator := domain.ActorContext{AuthenticatedCaller: "operator:synthetic", EffectiveActor: "operator:synthetic", CallerPermissions: domain.NewScopeSet(domain.ScopeSessionAdmin), EffectivePermissions: domain.NewScopeSet(domain.ScopeSessionAdmin)} + delegated := operator.Clone() + delegated.EffectiveActor = auth.AgentMCPPrincipal + for _, tc := range []struct { + name, body string + caller *domain.ActorContext + status int + }{ + {"anonymous", `{}`, nil, 403}, {"delegated", `{}`, &delegated, 403}, {"unknown", `{"valid_for_millis":1000,"path":"untrusted"}`, &operator, 400}, {"duplicate", `{"valid_for_millis":1000,"valid_for_millis":300000}`, &operator, 400}, {"overflow", `{"valid_for_millis":9223372036854775807}`, &operator, 400}, {"too-short", `{"valid_for_millis":999}`, &operator, 400}, + } { + t.Run(tc.name, func(t *testing.T) { + r := httptest.NewRequest(http.MethodPost, "/v1/guest/approvals", strings.NewReader(tc.body)) + if tc.caller != nil { + r = r.WithContext(context.WithValue(r.Context(), callerContextKey, *tc.caller)) + } + w := httptest.NewRecorder() + server.handleGuestApproval(w, r) + if w.Code != tc.status { + t.Fatalf("authority/envelope status=%d", w.Code) + } + }) + } +} +func TestGuestHTTPFramingRejectsWholeBodyMismatch(t *testing.T) { + for _, tc := range []struct { + name string + contentType string + length int64 + deadline time.Time + }{ + {"wrong-type", "application/json", 0, time.Now().Add(time.Minute)}, {"unknown-length", GuestContentType, -1, time.Now().Add(time.Minute)}, {"unbound-payload", GuestContentType, 0, time.Now().Add(time.Minute)}, {"missing-deadline", GuestContentType, 0, time.Time{}}, + } { + t.Run(tc.name, func(t *testing.T) { + p := app.GuestOperationParams{Operation: "put", Size: 3, Deadline: tc.deadline} + if tc.name == "missing-deadline" { + p.Size = 0 + } + data, _ := json.Marshal(p) + var body bytes.Buffer + // #nosec G115 -- bounded synthetic metadata from this fixed operation is below 64KiB. + _ = binary.Write(&body, binary.BigEndian, uint32(len(data))) + body.Write(data) + r := httptest.NewRequest(http.MethodPost, "/", &body) + r.Header.Set("Content-Type", tc.contentType) + if tc.length < 0 { + r.ContentLength = tc.length + } + if _, err := readGuestHTTPRequest(r); err == nil { + t.Fatal("whole-body framing admitted") + } + }) + } +} +func TestGuestHTTPServiceFailureHasBoundedTrailer(t *testing.T) { + server, token, fixture := newGuestHTTPServiceFixture(t) + p, _ := guestHTTPCase("prepare", "") + p.Operation = "adopt-arbitrary-root" + final, body := invokeGuestHTTPFixture(t, server.URL, token, p, nil) + if final.Error == "" || len(body) != 0 || len(fixture.operations) != 0 { + t.Fatalf("invalid operation escaped shared admission: %+v %q %v", final, body, fixture.operations) + } +} diff --git a/internal/daemon/guest_retirement.go b/internal/daemon/guest_retirement.go new file mode 100644 index 0000000..404e019 --- /dev/null +++ b/internal/daemon/guest_retirement.go @@ -0,0 +1,274 @@ +package daemon + +import ( + "bytes" + "context" + "crypto/sha256" + "crypto/subtle" + _ "embed" + "encoding/base64" + "encoding/binary" + "encoding/json" + "fmt" + "io" + "net" + "strings" + "unicode/utf16" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/auth" + "github.com/Horcag/agent-machine-control/internal/domain" + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + gossh "golang.org/x/crypto/ssh" +) + +//go:embed guest_retirement.ps1 +var guestRetirementScript string + +type guestCommandRunner interface { + RunCommand(context.Context, domain.MachineRef, string, []byte, int) ([]byte, error) +} +type guestRetirementObservation struct { + Original stream.WorkerIdentity `json:"original"` + Status string `json:"status"` + Observed stream.WorkerIdentity `json:"observed"` +} + +// ConfirmGuestRetirement observes only the original process on the original +// enrolled route. No process is stopped, elevated, or inferred absent from SSH EOF. +func (p *guestConfigurationProvider) ConfirmGuestRetirement(ctx context.Context, target domain.MachineRef, actor domain.ActorID, generation string, original stream.WorkerIdentity) (bool, error) { + caller, err := p.retirementActor(ctx, actor) + if err != nil { + return false, err + } + before, err := p.retirementConfiguration(ctx, target, caller) + if err != nil || before.Generation != generation { + return false, app.ErrGuestAuthority + } + runner, err := p.retirementCommandRunner(ctx, target, caller, generation) + if err != nil { + return false, err + } + // Freeze credentials and route, then revalidate before dispatch: changed state + // cannot silently redirect an original-process observation to a new endpoint. + ready, err := p.retirementConfiguration(ctx, target, caller) + if err != nil || ready.Generation != generation { + return false, app.ErrGuestAuthority + } + input, err := guestRetirementInput(original) + if err != nil { + return false, err + } + data, err := runner.RunCommand(ctx, target, encodedGuestRetirementCommand(), input, 16*1024) + if err != nil { + return false, app.ErrGuestUncertain + } + after, err := p.retirementConfiguration(ctx, target, caller) + if err != nil || after.Generation != generation { + return false, app.ErrGuestAuthority + } + return decodeGuestRetirement(data, original) +} +func (p *guestConfigurationProvider) retirementConfiguration(ctx context.Context, target domain.MachineRef, caller domain.ActorContext) (app.GuestConfiguration, error) { + if p.retirementCurrent != nil { + return p.retirementCurrent(ctx, target, caller) + } + return p.Current(ctx, target, caller) +} +func (p *guestConfigurationProvider) retirementActor(ctx context.Context, actor domain.ActorID) (domain.ActorContext, error) { + if p.retirementCurrent != nil { + return domain.ActorContext{AuthenticatedCaller: actor, EffectiveActor: actor}, nil + } + kind := auth.TokenTypeOperator + if actor == auth.AgentMCPPrincipal { + kind = auth.TokenTypeAgentMCP + } + token, err := auth.ReadTokenFile(p.server.stateDir.AuthDir(), kind) + if err != nil { + return domain.ActorContext{}, app.ErrGuestAuthority + } + caller, _, ok := p.server.authStore.Authenticate(token) + if !ok || caller == nil || caller.EffectiveActor != actor { + return domain.ActorContext{}, app.ErrGuestAuthority + } + current, err := p.currentActor(ctx, *caller) + if err != nil { + return domain.ActorContext{}, err + } + return *current, nil +} +func (p *guestConfigurationProvider) retirementCommandRunner(ctx context.Context, target domain.MachineRef, caller domain.ActorContext, generation string) (guestCommandRunner, error) { + if p.retirementRunner != nil { + return p.retirementRunner, nil + } + frozen, err := freezeGuestKeys(ctx, p.keys, target) + if err != nil { + return nil, err + } + snapshot := *p + snapshot.keys = frozen + configuration, err := snapshot.Current(ctx, target, caller) + if err != nil || configuration.Generation != generation { + return nil, app.ErrGuestAuthority + } + return guestssh.NewTransport(frozen), nil +} +func encodedGuestRetirementCommand() string { + return guestRetirementCommand(guestRetirementScript) +} + +// The shell receives only this bounded loader. The original identity and exact +// server-owned source travel in binary stdin, outside Windows command limits. +const guestRetirementLoader = `$ErrorActionPreference='Stop' +$stream=[Console]::OpenStandardInput() +function ReadExact([int]$count) { + $bytes=[byte[]]::new($count);$offset=0 + while($offset-lt$count){$read=$stream.Read($bytes,$offset,$count-$offset);if($read-le0){throw 'retirement_truncated'};$offset+=$read} + return ,$bytes +} +function ReadLength([int]$maximum) { + $bytes=ReadExact 4 + $length=[long]$bytes[0]*16777216+[long]$bytes[1]*65536+[long]$bytes[2]*256+[long]$bytes[3] + if($length-le0-or$length-gt$maximum){throw 'retirement_length'} + return [int]$length +} +$identity=ReadExact (ReadLength 16384) +$source=ReadExact (ReadLength 507896) +if($identity.Length+$source.Length+8-gt524288-or$stream.ReadByte()-ne-1){throw 'retirement_frame'} +$hasher=[Security.Cryptography.SHA256]::Create() +try{$actual=[BitConverter]::ToString($hasher.ComputeHash($source)).Replace('-','').ToLowerInvariant()}finally{$hasher.Dispose()} +if($actual-cne'%s'){throw 'retirement_source'} +$utf8=[Text.UTF8Encoding]::new($false,$true) +$original=$utf8.GetString($identity)|ConvertFrom-Json +& ([ScriptBlock]::Create($utf8.GetString($source))) +` + +func guestRetirementCommand(source string) string { + hash := sha256.Sum256([]byte(source)) + loader := fmt.Sprintf(guestRetirementLoader, fmt.Sprintf("%x", hash)) + units := utf16.Encode([]rune(loader)) + data := make([]byte, len(units)*2) + for i, unit := range units { + binary.LittleEndian.PutUint16(data[i*2:], unit) + } + return "powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand " + base64.StdEncoding.EncodeToString(data) +} + +func guestRetirementInput(original stream.WorkerIdentity) ([]byte, error) { + identity, err := json.Marshal(original) + if err != nil || len(identity) > 16*1024 || len(guestRetirementScript) > 507896 { + return nil, app.ErrGuestUncertain + } + var input bytes.Buffer + for _, block := range [][]byte{identity, []byte(guestRetirementScript)} { + // #nosec G115 -- both blocks are explicitly bounded below 512 KiB above. + if err = binary.Write(&input, binary.BigEndian, uint32(len(block))); err != nil { + return nil, err + } + input.Write(block) + } + return input.Bytes(), nil +} +func decodeGuestRetirement(data []byte, original stream.WorkerIdentity) (bool, error) { + if len(data) > 16*1024 || rejectDuplicateJSONKeys(data) != nil { + return false, app.ErrGuestUncertain + } + var observation guestRetirementObservation + decoder := json.NewDecoder(bytes.NewReader(data)) + decoder.DisallowUnknownFields() + var extra any + if decoder.Decode(&observation) != nil || decoder.Decode(&extra) != io.EOF || observation.Original != original || observation.Observed.PID != original.PID { + return false, app.ErrGuestUncertain + } + switch observation.Status { + case "missing": + return observation.Observed.BirthTicks == 0 && observation.Observed.Image == "" && observation.Observed.SID == "", nil + case "reused": + return observation.Observed.BirthTicks > 0 && observation.Observed.BirthTicks != original.BirthTicks, nil + case "live", "exited": + if observation.Observed != original { + return false, app.ErrGuestUncertain + } + return observation.Status == "exited", nil + default: + return false, app.ErrGuestUncertain + } +} + +// frozenGuestKeys retains the exact enrollment-approved route across a single +// observation. It is not a reusable credential cache or a revocation fallback. +type frozenGuestKeys struct { + config guestssh.MachineSSHConfig + user, endpoint string + signer gossh.Signer + callback gossh.HostKeyCallback +} + +func freezeGuestKeys(ctx context.Context, keys guestssh.KeyProvider, target domain.MachineRef) (*frozenGuestKeys, error) { + config, err := guestMachineConfig(ctx, keys, target) + if err != nil { + return nil, err + } + user, err := keys.GetGuestUser(ctx, target) + if err != nil { + return nil, err + } + endpoint, err := keys.GetGuestEndpoint(ctx, target) + if err != nil { + return nil, err + } + signer, err := keys.GetClientSigner(ctx, target) + if err != nil { + return nil, err + } + callback, err := keys.GetHostKeyCallback(ctx, target) + if err != nil { + return nil, err + } + boundCallback, err := frozenGuestHostKeyCallback(config.PinnedHostKeySHA256, callback) + if err != nil { + return nil, err + } + return &frozenGuestKeys{config: *config, user: user, endpoint: endpoint, signer: signer, callback: boundCallback}, nil +} + +// A provider callback may have read a different configuration during an ABA +// change. Both its policy and the pin in the frozen generation must approve. +func frozenGuestHostKeyCallback(pin string, callback gossh.HostKeyCallback) (gossh.HostKeyCallback, error) { + pin = strings.TrimSpace(pin) + if pin == "" { + return nil, domain.ErrMissingHostKeyPin + } + if callback == nil { + return nil, app.ErrGuestAuthority + } + return func(hostname string, remote net.Addr, key gossh.PublicKey) error { + if err := callback(hostname, remote, key); err != nil { + return err + } + sum := sha256.Sum256(key.Marshal()) + actual := base64.StdEncoding.EncodeToString(sum[:]) + if subtle.ConstantTimeCompare([]byte(actual), []byte(pin)) != 1 { + return domain.ErrHostKeyMismatch + } + return nil + }, nil +} + +func (k *frozenGuestKeys) GetMachineConfig(domain.MachineRef) (*guestssh.MachineSSHConfig, error) { + configuration := k.config + return &configuration, nil +} +func (k *frozenGuestKeys) GetClientSigner(context.Context, domain.MachineRef) (gossh.Signer, error) { + return k.signer, nil +} +func (k *frozenGuestKeys) GetHostKeyCallback(context.Context, domain.MachineRef) (gossh.HostKeyCallback, error) { + return k.callback, nil +} +func (k *frozenGuestKeys) GetGuestUser(context.Context, domain.MachineRef) (string, error) { + return k.user, nil +} +func (k *frozenGuestKeys) GetGuestEndpoint(context.Context, domain.MachineRef) (string, error) { + return k.endpoint, nil +} diff --git a/internal/daemon/guest_retirement.ps1 b/internal/daemon/guest_retirement.ps1 new file mode 100644 index 0000000..0ffef02 --- /dev/null +++ b/internal/daemon/guest_retirement.ps1 @@ -0,0 +1,64 @@ +$ErrorActionPreference = 'Stop' +[Console]::InputEncoding = New-Object Text.UTF8Encoding($false, $true) +[Console]::OutputEncoding = New-Object Text.UTF8Encoding($false, $true) +Add-Type -TypeDefinition @' +using System; +using System.ComponentModel; +using System.Runtime.InteropServices; +using System.Security.Principal; +using System.Text; +using Microsoft.Win32.SafeHandles; +public static class AMCOriginalProcessProbe { + public sealed class Observation { + public string status; + public int pid; + public long birthTicks; + public string image = ""; + public string sid = ""; + } + [StructLayout(LayoutKind.Sequential)] struct FT {public uint low,high;} + [DllImport("kernel32.dll",SetLastError=true)] static extern SafeFileHandle OpenProcess(uint access,bool inherit,int pid); + [DllImport("kernel32.dll",SetLastError=true)] static extern bool GetProcessTimes(SafeFileHandle process,out FT creation,out FT exit,out FT kernel,out FT user); + [DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern bool QueryFullProcessImageName(SafeFileHandle process,uint flags,StringBuilder image,ref uint size); + [DllImport("advapi32.dll",SetLastError=true)] static extern bool OpenProcessToken(SafeFileHandle process,uint access,out SafeFileHandle token); + [DllImport("kernel32.dll",SetLastError=true)] static extern uint WaitForSingleObject(SafeFileHandle process,uint timeout); + [DllImport("kernel32.dll",SetLastError=true)] static extern bool K32EnumProcesses([Out] uint[] processes,uint size,out uint needed); + static bool Missing(int pid) { + var processes = new uint[65536];uint needed; + if(!K32EnumProcesses(processes,(uint)(processes.Length*4),out needed))throw new Win32Exception(); + return InventoryMissing(pid,processes,needed); + } + public static bool InventoryMissing(int pid,uint[] processes,uint needed) { + if(needed==0||needed>=processes.Length*4||needed%4!=0)throw new Win32Exception(); + for(int i=0;i 7900 { + t.Fatal("loader exceeds normal Windows command budget") + } + token := strings.TrimPrefix(command, "powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand ") + process := exec.CommandContext(ctx, pwsh, "-NoLogo", "-NoProfile", "-NonInteractive", "-EncodedCommand", token) + stagePath := filepath.Join(t.TempDir(), "retirement-stage.txt") + process.Env = append(os.Environ(), "AMC_RETIREMENT_TEST_STAGE="+stagePath) + process.Stdin = bytes.NewReader(input) + started := time.Now() + output, err := process.CombinedOutput() + t.Logf("retirement fixture: stage=%s elapsed=%s context=%v", retirementLoaderStage(stagePath), time.Since(started).Round(time.Millisecond), ctx.Err()) + return output, err +} + +func retirementLoaderStage(path string) string { + file, err := os.Open(path) + if err != nil { + if os.IsNotExist(err) { + return "not-entered" + } + return "unavailable" + } + defer file.Close() + data, err := io.ReadAll(io.LimitReader(file, 32)) + if err != nil { + return "unavailable" + } + switch stage := string(data); stage { + case "entered", "add-type-start", "add-type-done", "inventory-done": + return stage + default: + return "invalid" + } +} + +func TestGuestRetirementLoaderPreservesFullInteger(t *testing.T) { + source := "$original | ConvertTo-Json -Compress" + identity := []byte(`{"pid":1234,"birthTicks":639273243660826407,"image":"synthetic","sid":"synthetic"}`) + output, err := runRetirementLoader(t, source, retirementLoaderPayload(identity, []byte(source))) + if err != nil { + t.Fatalf("loader: %v %s", err, output) + } + var observed map[string]json.RawMessage + if err := json.Unmarshal(output, &observed); err != nil { + t.Fatal(err) + } + if string(observed["birthTicks"]) != "639273243660826407" { + t.Fatalf("native birth changed: %s", output) + } +} + +func TestGuestRetirementLoaderRefusesInvalidFrames(t *testing.T) { + source := "throw 'synthetic_source_was_executed'" + original := retirementLoaderPayload([]byte(`{"pid":1234}`), []byte(source)) + cases := map[string][]byte{ + "truncated": original[:len(original)-1], + "trailing": append(bytes.Clone(original), 1), + "zero": make([]byte, 4), + "oversize": {0, 1, 0, 0}, + "source_hash": retirementLoaderPayload([]byte(`{"pid":1234}`), []byte("throw 'changed_source_was_executed'")), + "invalid_utf8": retirementLoaderPayload([]byte{255}, []byte(source)), + } + for name, input := range cases { + t.Run(name, func(t *testing.T) { + output, err := runRetirementLoader(t, source, input) + if err == nil || bytes.Contains(output, []byte("source_was_executed")) { + t.Fatalf("invalid frame reached execution: %v %s", err, output) + } + }) + } +} + +func TestGuestRetirementNativeInventoryFailsClosed(t *testing.T) { + parts := strings.Split(guestRetirementScript, "@'") + if len(parts) != 2 { + t.Fatal("native probe source unavailable") + } + native, _, ok := strings.Cut(parts[1], "'@") + if !ok { + t.Fatal("native probe terminator missing") + } + // Compile the actual embedded class, then invoke only its pure census decision. + source := "[IO.File]::WriteAllText($env:AMC_RETIREMENT_TEST_STAGE,'entered')\n" + + "[IO.File]::WriteAllText($env:AMC_RETIREMENT_TEST_STAGE,'add-type-start')\n" + + "Add-Type -TypeDefinition @'\n" + strings.TrimSpace(native) + "\n'@\n" + + "[IO.File]::WriteAllText($env:AMC_RETIREMENT_TEST_STAGE,'add-type-done')\n" + + "$ids=[uint32[]]@(1,2,0);if([AMCOriginalProcessProbe]::InventoryMissing(1,$ids,8)){throw 'present_pid_absent'};if(-not[AMCOriginalProcessProbe]::InventoryMissing(3,$ids,8)){throw 'missing_pid_not_observed'};$refused=0;foreach($size in @(0,1,12,16)){try{$null=[AMCOriginalProcessProbe]::InventoryMissing(3,$ids,[uint32]$size);throw 'invalid_census_accepted'}catch{if($_.Exception.Message-eq'invalid_census_accepted'){throw};$refused++}}\n" + + "[IO.File]::WriteAllText($env:AMC_RETIREMENT_TEST_STAGE,'inventory-done')\n" + + "@{refused=$refused}|ConvertTo-Json -Compress" + output, err := runRetirementLoader(t, source, retirementLoaderPayload([]byte(`{}`), []byte(source))) + if err != nil { + t.Fatalf("native inventory decision: %v %s", err, output) + } + var observed struct { + Refused int `json:"refused"` + } + if json.Unmarshal(output, &observed) != nil || observed.Refused != 4 { + t.Fatalf("census decisions missing: %s", output) + } +} diff --git a/internal/daemon/guest_retirement_test.go b/internal/daemon/guest_retirement_test.go new file mode 100644 index 0000000..b43a5cd --- /dev/null +++ b/internal/daemon/guest_retirement_test.go @@ -0,0 +1,256 @@ +package daemon + +import ( + "bytes" + "context" + "crypto/ed25519" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "encoding/binary" + "encoding/json" + "errors" + "github.com/Horcag/agent-machine-control/internal/auth" + "io" + "net" + "os" + "path/filepath" + "strconv" + "strings" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/domain" + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" + "github.com/Horcag/agent-machine-control/internal/guest/ssh/fakeserver" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + gossh "golang.org/x/crypto/ssh" +) + +type retirementFakeRunner struct { + run func([]byte) ([]byte, error) + calls int + t *testing.T +} + +func (r *retirementFakeRunner) RunCommand(_ context.Context, target domain.MachineRef, command string, input []byte, limit int) ([]byte, error) { + r.calls++ + if target != "local:11111111-1111-4111-8111-111111111111" || !strings.HasPrefix(command, "powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand ") || limit != 16*1024 { + r.t.Fatal("observation escaped original target or fixed bounded command") + } + reader := bytes.NewReader(input) + var size uint32 + if binary.Read(reader, binary.BigEndian, &size) != nil || size > 16*1024 { + r.t.Fatal("retirement identity framing invalid") + } + identity := make([]byte, size) + if _, err := io.ReadFull(reader, identity); err != nil { + r.t.Fatal(err) + } + if binary.Read(reader, binary.BigEndian, &size) != nil || int(size) != len(guestRetirementScript) { + r.t.Fatal("retirement source framing invalid") + } + source := make([]byte, size) + if _, err := io.ReadFull(reader, source); err != nil || string(source) != guestRetirementScript || reader.Len() != 0 { + r.t.Fatal("retirement source or trailing input changed") + } + return r.run(identity) +} +func TestGuestRetirementExactNativeObservation(t *testing.T) { + original := stream.WorkerIdentity{PID: 1234, BirthTicks: 638000000000000001, Image: `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`, SID: "S-1-5-21-1000"} + for _, status := range []string{"live", "missing", "reused", "exited", "denied", "malformed", "mismatch"} { + t.Run(status, func(t *testing.T) { + runner := &retirementFakeRunner{t: t, run: func(input []byte) ([]byte, error) { return retirementFixtureResponse(t, input, original, status) }} + + provider := guestConfigurationProvider{retirementRunner: runner, retirementCurrent: func(context.Context, domain.MachineRef, domain.ActorContext) (app.GuestConfiguration, error) { + return app.GuestConfiguration{Generation: "original-generation"}, nil + }} + absent, err := provider.ConfirmGuestRetirement(t.Context(), "local:11111111-1111-4111-8111-111111111111", "operator:synthetic", "original-generation", original) + expected := status == "missing" || status == "reused" || status == "exited" + if absent != expected || (expected && err != nil) || runner.calls != 1 { + t.Fatalf("absent=%v error=%v calls=%d", absent, err, runner.calls) + } + }) + } +} +func TestGuestRetirementRejectsGenerationDrift(t *testing.T) { + for _, changeAt := range []int{1, 2, 3} { + t.Run(strconv.Itoa(changeAt), func(t *testing.T) { + calls := 0 + original := stream.WorkerIdentity{PID: 12, BirthTicks: 100, Image: "synthetic-image", SID: "synthetic-sid"} + runner := &retirementFakeRunner{t: t, run: func([]byte) ([]byte, error) { + return json.Marshal(guestRetirementObservation{Original: original, Status: "missing", Observed: stream.WorkerIdentity{PID: original.PID}}) + }} + provider := guestConfigurationProvider{retirementRunner: runner, retirementCurrent: func(context.Context, domain.MachineRef, domain.ActorContext) (app.GuestConfiguration, error) { + calls++ + generation := "original" + if calls >= changeAt { + generation = "changed" + } + return app.GuestConfiguration{Generation: generation}, nil + }} + absent, err := provider.ConfirmGuestRetirement(t.Context(), "local:11111111-1111-4111-8111-111111111111", "operator:synthetic", "original", original) + if absent || !errors.Is(err, app.ErrGuestAuthority) { + t.Fatalf("generation drift accepted: %v %v", absent, err) + } + if changeAt < 3 && runner.calls != 0 { + t.Fatal("dispatched after authority drift") + } + }) + } +} + +func retirementFixtureResponse(t *testing.T, input []byte, original stream.WorkerIdentity, status string) ([]byte, error) { + t.Helper() + + var expected stream.WorkerIdentity + if json.Unmarshal(input, &expected) != nil || expected != original { + t.Fatal("original process binding missing") + } + if status == "denied" { + return nil, errors.New("denied") + } + if status == "malformed" { + return []byte("not-json"), nil + } + observed := original + nativeStatus := status + switch status { + case "missing": + observed = stream.WorkerIdentity{PID: original.PID} + case "reused": + observed.BirthTicks++ + case "mismatch": + nativeStatus = "exited" + observed.SID = "S-1-5-21-2000" + } + return json.Marshal(guestRetirementObservation{Original: original, Status: nativeStatus, Observed: observed}) + +} + +func TestGuestRetirementUsesFreshOperatorAndAgentAuthority(t *testing.T) { + provider, _, caller, _ := newGuestConfigurationFixture(t) + ref := domain.MachineRef("local:11111111-1111-4111-8111-111111111111") + configuration, err := provider.Current(t.Context(), ref, *caller) + if err != nil { + t.Fatal(err) + } + if _, err = provider.retirementCommandRunner(t.Context(), ref, *caller, configuration.Generation); err != nil { + t.Fatalf("effective default snapshot rejected: %v", err) + } + original := stream.WorkerIdentity{PID: 1234, BirthTicks: 638000000000000001, Image: "synthetic-image", SID: "synthetic-sid"} + runner := &retirementFakeRunner{t: t, run: func(input []byte) ([]byte, error) { return retirementFixtureResponse(t, input, original, "missing") }} + provider.retirementRunner = runner + absent, err := provider.ConfirmGuestRetirement(t.Context(), ref, caller.EffectiveActor, configuration.Generation, original) + if !absent || err != nil || runner.calls != 1 { + t.Fatalf("fresh original authority: %v %v calls=%d", absent, err, runner.calls) + } + agent, err := provider.retirementActor(t.Context(), auth.AgentMCPPrincipal) + if err != nil || agent.EffectiveActor != auth.AgentMCPPrincipal { + t.Fatalf("agent principal resolution: %+v %v", agent, err) + } + if _, err = provider.retirementActor(t.Context(), "operator:foreign"); !errors.Is(err, app.ErrGuestAuthority) { + t.Fatalf("foreign actor adopted: %v", err) + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + if _, err = provider.retirementActor(ctx, caller.EffectiveActor); !errors.Is(err, context.Canceled) { + t.Fatalf("canceled authority resolved: %v", err) + } + path := filepath.Join(provider.server.stateDir.AuthDir(), auth.OperatorTokenFileName) + if err = os.WriteFile(path, []byte(strings.Repeat("a", 64)), 0600); err != nil { + t.Fatal(err) + } + absent, err = provider.ConfirmGuestRetirement(t.Context(), ref, caller.EffectiveActor, configuration.Generation, original) + if absent || !errors.Is(err, app.ErrGuestAuthority) || runner.calls != 1 { + t.Fatalf("revocation did not block observation: %v %v calls=%d", absent, err, runner.calls) + } +} + +func TestGuestFrozenRouteRejectsMixedHostPinABA(t *testing.T) { + provider, keys, caller, _ := newGuestConfigurationFixture(t) + ref := domain.MachineRef("local:11111111-1111-4111-8111-111111111111") + signer, err := keys.GetClientSigner(t.Context(), ref) + if err != nil { + t.Fatal(err) + } + other, err := fakeserver.New(fakeserver.ModeEcho, signer.PublicKey()) + if err != nil { + t.Fatal(err) + } + defer other.Close() + keys.endpoint = other.Addr() + mixed := &mixedGuestPinKeys{KeyProvider: keys, pin: other.HostKeyPin()} + provider.keys = mixed + before, err := provider.Current(t.Context(), ref, *caller) + if err != nil { + t.Fatal(err) + } + frozen, err := freezeGuestKeys(t.Context(), mixed, ref) + if err != nil { + t.Fatal(err) + } + snapshot := provider + snapshot.keys = frozen + after, err := snapshot.Current(t.Context(), ref, *caller) + if err != nil || after.Generation != before.Generation { + t.Fatalf("fixture did not recreate A/B/A generation: %v", err) + } + ctx, cancel := context.WithTimeout(t.Context(), time.Second) + defer cancel() + channel, err := guestssh.NewTransport(frozen).OpenDuplex(ctx, ref, "synthetic-observation") + if channel != nil { + _ = channel.Close() + } + if err == nil || mixed.accepted != 1 { + t.Fatalf("mixed-pin route was admitted or B callback not exercised: %v accepted=%d", err, mixed.accepted) + } +} + +type mixedGuestPinKeys struct { + guestssh.KeyProvider + pin string + accepted int +} + +func (k *mixedGuestPinKeys) GetHostKeyCallback(context.Context, domain.MachineRef) (gossh.HostKeyCallback, error) { + return func(_ string, _ net.Addr, key gossh.PublicKey) error { + sum := sha256.Sum256(key.Marshal()) + if base64.StdEncoding.EncodeToString(sum[:]) != k.pin { + return domain.ErrHostKeyMismatch + } + k.accepted++ + return nil + }, nil +} +func TestGuestFrozenPinPreservesProviderPolicy(t *testing.T) { + _, key, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + signer, err := gossh.NewSignerFromKey(key) + if err != nil { + t.Fatal(err) + } + sum := sha256.Sum256(signer.PublicKey().Marshal()) + pin := base64.StdEncoding.EncodeToString(sum[:]) + denied := errors.New("synthetic provider denial") + callback, err := frozenGuestHostKeyCallback(pin, func(string, net.Addr, gossh.PublicKey) error { return denied }) + if err != nil { + t.Fatal(err) + } + if err = callback("synthetic", nil, signer.PublicKey()); !errors.Is(err, denied) { + t.Fatalf("provider policy bypassed: %v", err) + } + callback, err = frozenGuestHostKeyCallback(" "+pin+" ", func(string, net.Addr, gossh.PublicKey) error { return nil }) + if err != nil { + t.Fatal(err) + } + if err = callback("synthetic", nil, signer.PublicKey()); err != nil { + t.Fatalf("matching policy and pin rejected: %v", err) + } + if _, err = frozenGuestHostKeyCallback("", callback); !errors.Is(err, domain.ErrMissingHostKeyPin) { + t.Fatalf("missing pin accepted: %v", err) + } +} diff --git a/internal/daemon/guest_service_http_test.go b/internal/daemon/guest_service_http_test.go new file mode 100644 index 0000000..414c373 --- /dev/null +++ b/internal/daemon/guest_service_http_test.go @@ -0,0 +1,231 @@ +package daemon + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/base64" + "encoding/binary" + "encoding/hex" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/approval" + "github.com/Horcag/agent-machine-control/internal/audit" + "github.com/Horcag/agent-machine-control/internal/auth" + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + "github.com/Horcag/agent-machine-control/internal/lease" + "github.com/Horcag/agent-machine-control/internal/policy" + "github.com/Horcag/agent-machine-control/internal/receipt" + "github.com/Horcag/agent-machine-control/internal/sessions" + "github.com/Horcag/agent-machine-control/internal/statedir" +) + +type guestHTTPServiceFixture struct { + root stream.RootIdentity + worker stream.WorkerIdentity + files map[string]stream.FileIdentity + data map[string][]byte + operations []string + configErr error + privileged bool + prepareErr error +} + +func (f *guestHTTPServiceFixture) ResolveTarget(context.Context, string) (app.TargetResolution, error) { + guid := "11111111-1111-4111-8111-111111111111" + return app.TargetResolution{Locator: domain.MachineLocator{HostID: domain.LocalHostID, VMID: guid}, ProviderVMID: guid}, nil +} +func (f *guestHTTPServiceFixture) Current(_ context.Context, _ domain.MachineRef, caller domain.ActorContext) (app.GuestConfiguration, error) { + return app.GuestConfiguration{Generation: "synthetic-generation", Caller: caller, WorkspaceIdentity: f.root}, f.configErr +} +func (f *guestHTTPServiceFixture) ResolveSafety(context.Context, domain.MachineRef) (app.SafetyResolution, error) { + classification := domain.ClassReversibleMutation + if f.privileged { + classification = domain.ClassDestructivePrivileged + } + return app.SafetyResolution{Classification: classification, Contained: true, RollbackRef: "synthetic-checkpoint", RollbackState: policy.RollbackState{Available: true, Verified: true, CheckpointID: "synthetic-checkpoint"}}, nil +} +func (f *guestHTTPServiceFixture) Prepare(context.Context, domain.MachineRef, string, string) (stream.WorkerIdentity, error) { + return f.worker, f.prepareErr +} +func (f *guestHTTPServiceFixture) Retire(domain.MachineRef, string, string) error { return nil } +func (f *guestHTTPServiceFixture) Execute(_ context.Context, _ domain.MachineRef, _ string, _ string, p stream.Request, input io.Reader, output io.Writer) (stream.Result, error) { + f.operations = append(f.operations, p.Operation) + result := stream.Result{ID: p.ID, Status: "ok", Worker: f.worker} + switch p.Operation { + case "observe": + result.Root = &f.root + case "put": + data, err := io.ReadAll(input) + if err != nil { + return result, err + } + if int64(len(data)) != p.Size { + return result, io.ErrUnexpectedEOF + } + file := stream.FileIdentity{Path: p.Path, Volume: f.root.Volume, FileID: strings.Repeat("b", 32), CreationTime: 200, Owner: f.root.Owner, SDDLHash: f.root.SDDLHash, Size: p.Size, SHA256: p.SHA256, RootPath: f.root.Path, RootVolume: f.root.Volume, RootFileID: f.root.FileID, RootCreationTime: f.root.CreationTime, RootOwner: f.root.Owner, RootSDDLHash: f.root.SDDLHash} + f.files[p.Path] = file + f.data[p.Path] = data + result.Files = []stream.FileIdentity{file} + case "get": + file := f.files[p.Path] + result.Files = []stream.FileIdentity{file} + result.Size, result.SHA256 = file.Size, file.SHA256 + if _, err := output.Write(f.data[p.Path]); err != nil { + return result, err + } + case "exec": + if _, err := io.Copy(io.Discard, input); err != nil { + return result, err + } + data := []byte("verified command output") + result.Size = int64(len(data)) + hash := sha256.Sum256(data) + result.SHA256 = hex.EncodeToString(hash[:]) + if _, err := output.Write(data); err != nil { + return result, err + } + case "remove-owned": + for _, file := range p.Owned { + result.Removed = append(result.Removed, stream.Removal{Path: file.Path, Status: "deleted", Absent: true}) + } + } + return result, nil +} +func TestGuestHTTPAllOperationsReachRealSharedService(t *testing.T) { + httpServer, token, fixture := newGuestHTTPServiceFixture(t) + var artifactID string + for _, operation := range []string{"prepare", "put", "get", "exec", "remove-owned"} { + p, payload := guestHTTPCase(operation, artifactID) + final, body := invokeGuestHTTPFixture(t, httpServer.URL, token, p, payload) + if final.Error != "" || final.Result.Receipt == nil || final.Result.Receipt.OperationKind != domain.OperationKind("guest."+operation) { + t.Fatalf("%s was rejected before shared service: %+v", operation, final) + } + if operation == "put" { + if len(final.Result.Artifacts) != 1 { + t.Fatal("put did not return opaque ownership") + } + artifactID = final.Result.Artifacts[0].ID + } + if operation == "get" && !bytes.Equal(body, []byte{0, 255, 128, 1}) { + t.Fatal("binary get did not preserve source bytes") + } + if operation == "exec" && string(body) != "verified command output" { + t.Fatal("exec output was not published") + } + } + if strings.Join(fixture.operations, ",") != "observe,put,get,exec,remove-owned" { + t.Fatalf("whole operation decisions were not executed: %v", fixture.operations) + } +} +func invokeGuestHTTPFixture(t *testing.T, endpoint, token string, p app.GuestOperationParams, payload []byte) (GuestFinal, []byte) { + t.Helper() + response := guestHTTPFixtureResponse(t, endpoint, token, p, payload, true) + defer response.Body.Close() + body, err := io.ReadAll(response.Body) + if err != nil { + t.Fatal(err) + } + trailer, err := base64.RawStdEncoding.DecodeString(response.Trailer.Get(GuestResultTrailer)) + if err != nil { + t.Fatal(err) + } + var final GuestFinal + if err = json.Unmarshal(trailer, &final); err != nil { + t.Fatalf("final trailer: %v status=%d body=%q", err, response.StatusCode, body) + } + return final, body +} + +func guestHTTPFixtureResponse(t *testing.T, endpoint, token string, p app.GuestOperationParams, payload []byte, typed bool) *http.Response { + t.Helper() + metadata, err := json.Marshal(p) + if err != nil { + t.Fatal(err) + } + var requestBody bytes.Buffer + // #nosec G115 -- synthetic bounded metadata is less than 64KiB. + if err = binary.Write(&requestBody, binary.BigEndian, uint32(len(metadata))); err != nil { + t.Fatal(err) + } + requestBody.Write(metadata) + requestBody.Write(payload) + request, err := http.NewRequestWithContext(t.Context(), http.MethodPost, endpoint, bytes.NewReader(requestBody.Bytes())) + if err != nil { + t.Fatal(err) + } + request.Header.Set("Authorization", "Bearer "+token) + request.Header.Set("Content-Type", GuestContentType) + if typed { + request.Header.Set(GuestErrorProtocolHeader, GuestErrorProtocolVersion) + } + response, err := http.DefaultClient.Do(request) + if err != nil { + t.Fatal(err) + } + return response +} + +func guestHTTPCase(operation, artifactID string) (app.GuestOperationParams, []byte) { + p := app.GuestOperationParams{Operation: operation, Reason: "synthetic whole guest operation", IdempotencyKey: "http-" + operation, Deadline: time.Now().Add(time.Minute)} + var payload []byte + switch operation { + case "put": + payload = []byte{0, 255, 128, 1} + case "get": + p.ArtifactIDs = []string{artifactID} + p.MaxOutput = 100 + case "exec": + payload = []byte("Write-Output 'synthetic'") + p.MaxOutput = 100 + case "remove-owned": + p.ArtifactIDs = []string{artifactID} + } + if payload != nil { + p.Size = int64(len(payload)) + hash := sha256.Sum256(payload) + p.SHA256 = hex.EncodeToString(hash[:]) + } + return p, payload +} + +func newGuestHTTPServiceFixture(t *testing.T) (*httptest.Server, string, *guestHTTPServiceFixture) { + t.Helper() + sd, err := statedir.Resolve(filepath.Join(t.TempDir(), "state")) + if err != nil { + t.Fatal(err) + } + if err = sd.EnsureDirs(); err != nil { + t.Fatal(err) + } + store, err := auth.LoadOrCreate(sd.AuthDir(), auth.WithPrincipalResolver(func() (string, error) { return "operator:synthetic", nil })) + if err != nil { + t.Fatal(err) + } + token, err := auth.ReadTokenFile(sd.AuthDir(), auth.TokenTypeOperator) + if err != nil { + t.Fatal(err) + } + fixture := &guestHTTPServiceFixture{root: stream.RootIdentity{Path: `C:\AMC\synthetic`, Volume: 7, FileID: strings.Repeat("a", 32), CreationTime: 100, Owner: "S-1-5-21-synthetic", SDDLHash: strings.Repeat("d", 64)}, worker: stream.WorkerIdentity{PID: 123, BirthTicks: 638000000000000001, Image: `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`, SID: "S-1-5-21-synthetic"}, files: map[string]stream.FileIdentity{}, data: map[string][]byte{}} + session := app.NewSessionService(nil, fixture, lease.NewManager(sd.LeasesDir()), audit.NewStore(sd.AuditDir()), receipt.NewStore(sd.ReceiptsDir()), approval.NewStore(sd.ApprovalsDir()), app.WithSessionMutationJournal(sessions.NewMutationJournal(sd.SessionsDir()))) + svc := app.NewGuestService(session, fixture, fixture, fixture, app.NewGuestStore(filepath.Join(sd.Root(), "guest"))) + server := &Server{guestService: svc, authStore: store, semaphore: make(chan struct{}, 8)} + httpServer := httptest.NewServer(server.authMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/approval" { + server.handleGuestApproval(w, r) + return + } + server.handleGuestOperation(w, r) + }))) + t.Cleanup(httpServer.Close) + return httpServer, token, fixture +} diff --git a/internal/daemon/guest_transport.go b/internal/daemon/guest_transport.go new file mode 100644 index 0000000..2edaa33 --- /dev/null +++ b/internal/daemon/guest_transport.go @@ -0,0 +1,100 @@ +package daemon + +import ( + "context" + "io" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/auth" + "github.com/Horcag/agent-machine-control/internal/domain" + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +type guestDialSnapshotKey struct{} +type guestDialSnapshot struct { + target domain.MachineRef + keys *frozenGuestKeys +} + +// Each worker receives its own frozen dial authority. Concurrent preparations +// cannot borrow credentials or endpoints through shared mutable provider state. +type guestPinnedOpener struct{} + +func (guestPinnedOpener) OpenDuplex(ctx context.Context, target domain.MachineRef, command string) (guestssh.Duplex, error) { + snapshot, ok := ctx.Value(guestDialSnapshotKey{}).(guestDialSnapshot) + if !ok || snapshot.target != target || snapshot.keys == nil { + return nil, app.ErrGuestAuthority + } + return guestssh.NewTransport(snapshot.keys).OpenDuplex(ctx, target, command) +} + +type defaultGuestTransport struct { + client *stream.Client + provider *guestConfigurationProvider +} + +func newDefaultGuestTransport(server *Server, keys guestssh.KeyProvider) *defaultGuestTransport { + return &defaultGuestTransport{ + client: stream.NewClient(guestPinnedOpener{}), + provider: &guestConfigurationProvider{server: server, keys: keys}, + } +} + +func (t *defaultGuestTransport) Prepare(ctx context.Context, target domain.MachineRef, poolKey, generation string) (stream.WorkerIdentity, error) { + keys, err := freezeGuestKeys(ctx, t.provider.keys, target) + if err != nil { + return stream.WorkerIdentity{}, err + } + caller, err := t.prepareCaller(ctx, target, generation, keys) + if err != nil { + return stream.WorkerIdentity{}, err + } + if err = t.checkGeneration(ctx, target, generation, caller); err != nil { + return stream.WorkerIdentity{}, err + } + bound := context.WithValue(ctx, guestDialSnapshotKey{}, guestDialSnapshot{target: target, keys: keys}) + identity, err := t.client.Prepare(bound, target, poolKey, generation) + if err != nil { + return identity, err + } + // Return the admitted original identity even on post-start authority drift. + // The application must durably retire that exact worker, never replay startup. + return identity, t.checkGeneration(ctx, target, generation, caller) +} + +func (t *defaultGuestTransport) prepareCaller(ctx context.Context, target domain.MachineRef, generation string, keys *frozenGuestKeys) (domain.ActorContext, error) { + snapshot := *t.provider + snapshot.keys = keys + for _, kind := range []auth.TokenType{auth.TokenTypeOperator, auth.TokenTypeAgentMCP} { + token, err := auth.ReadTokenFile(t.provider.server.stateDir.AuthDir(), kind) + if err != nil { + continue + } + caller, _, ok := t.provider.server.authStore.Authenticate(token) + if !ok || caller == nil || caller.IsDelegated() { + continue + } + configuration, err := snapshot.Current(ctx, target, *caller) + if err == nil && configuration.Generation == generation { + return caller.Clone(), nil + } + } + return domain.ActorContext{}, app.ErrGuestAuthority +} + +func (t *defaultGuestTransport) checkGeneration(ctx context.Context, target domain.MachineRef, generation string, caller domain.ActorContext) error { + configuration, err := t.provider.Current(ctx, target, caller) + if err != nil || configuration.Generation != generation { + return app.ErrGuestAuthority + } + return ctx.Err() +} + +func (t *defaultGuestTransport) Execute(ctx context.Context, target domain.MachineRef, poolKey, generation string, request stream.Request, input io.Reader, output io.Writer) (stream.Result, error) { + return t.client.Execute(ctx, target, poolKey, generation, request, input, output) +} + +func (t *defaultGuestTransport) Retire(target domain.MachineRef, poolKey, generation string) error { + return t.client.Retire(target, poolKey, generation) +} diff --git a/internal/daemon/guest_transport_boundary_test.go b/internal/daemon/guest_transport_boundary_test.go new file mode 100644 index 0000000..f2f46a8 --- /dev/null +++ b/internal/daemon/guest_transport_boundary_test.go @@ -0,0 +1,128 @@ +package daemon + +import ( + "context" + "errors" + "io" + "sync" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/auth" + "github.com/Horcag/agent-machine-control/internal/domain" + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +func guestPreparationGenerations(t *testing.T, provider guestConfigurationProvider, target domain.MachineRef) []string { + t.Helper() + var generations []string + for _, kind := range []auth.TokenType{auth.TokenTypeOperator, auth.TokenTypeAgentMCP} { + token, err := auth.ReadTokenFile(provider.server.stateDir.AuthDir(), kind) + if err != nil { + t.Fatal(err) + } + caller, _, ok := provider.server.authStore.Authenticate(token) + if !ok { + t.Fatal("fixture caller unavailable") + } + configuration, err := provider.Current(t.Context(), target, *caller) + if err != nil { + t.Fatal(err) + } + generations = append(generations, configuration.Generation) + } + return generations +} + +func TestGuestDefaultPrepareConcurrentActorIsolation(t *testing.T) { + provider, _, _, _ := newGuestConfigurationFixture(t) + target := domain.MachineRef("local:11111111-1111-4111-8111-111111111111") + generations := guestPreparationGenerations(t, provider, target) + if generations[0] == generations[1] { + t.Fatal("actors shared an authority generation") + } + transport, calls, original := guestPreparedTestTransport(t, provider, nil) + results := make(chan error, len(generations)) + var workers sync.WaitGroup + for _, generation := range generations { + workers.Go(func() { + identity, err := transport.Prepare(t.Context(), target, "same-opaque-pool-key", generation) + if err == nil && identity != original { + err = errors.New("original worker identity changed") + } + results <- err + }) + } + workers.Wait() + close(results) + for err := range results { + if err != nil { + t.Fatal(err) + } + } + if calls.Load() != 2 { + t.Fatalf("actor generations borrowed a worker: starts=%d", calls.Load()) + } +} + +func TestGuestDefaultPrepareCancellationReachesStartup(t *testing.T) { + provider, _, _, _ := newGuestConfigurationFixture(t) + target := domain.MachineRef("local:11111111-1111-4111-8111-111111111111") + generation := guestPreparationGenerations(t, provider, target)[0] + entered := make(chan struct{}) + client := stream.NewClient(guestPrepareTestOpener(func(ctx context.Context, _ domain.MachineRef, _ string) (guestssh.Duplex, error) { + close(entered) + <-ctx.Done() + return nil, ctx.Err() + })) + t.Cleanup(func() { _ = client.Close() }) + transport := defaultGuestTransport{client: client, provider: &provider} + ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second) + defer cancel() + result := make(chan error, 1) + go func() { + _, err := transport.Prepare(ctx, target, "canceled-original-startup", generation) + result <- err + }() + select { + case <-entered: + case <-ctx.Done(): + t.Fatal("startup did not reach its original context") + } + cancel() + select { + case err := <-result: + if !errors.Is(err, context.Canceled) { + t.Fatalf("startup cancellation lost: %v", err) + } + case <-time.After(time.Second): + t.Fatal("startup ignored caller cancellation") + } +} + +func TestGuestPinnedOpenerUsesOriginalFrozenEndpoint(t *testing.T) { + provider, keys, _, config := newGuestConfigurationFixture(t) + target := domain.MachineRef("local:11111111-1111-4111-8111-111111111111") + frozen, err := freezeGuestKeys(t.Context(), provider.keys, target) + if err != nil { + t.Fatal(err) + } + keys.endpoint = "127.0.0.1:65001" + ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) + defer cancel() + ctx = context.WithValue(ctx, guestDialSnapshotKey{}, guestDialSnapshot{target: target, keys: frozen}) + channel, err := (guestPinnedOpener{}).OpenDuplex(ctx, target, "synthetic-observation") + if err != nil { + t.Fatal(err) + } + defer channel.Close() + prefix := make([]byte, len("PS C:\\> ")) + if _, err = io.ReadFull(channel, prefix); err != nil || string(prefix) != "PS C:\\> " { + t.Fatalf("original pinned route not observed: %q %v", prefix, err) + } + endpoint, _ := frozen.GetGuestEndpoint(ctx, target) + if endpoint != config.Endpoint { + t.Fatal("frozen endpoint changed") + } +} diff --git a/internal/daemon/guest_transport_test.go b/internal/daemon/guest_transport_test.go new file mode 100644 index 0000000..ba811ff --- /dev/null +++ b/internal/daemon/guest_transport_test.go @@ -0,0 +1,125 @@ +package daemon + +import ( + "context" + "encoding/binary" + "encoding/json" + "errors" + "io" + "net" + "sync" + "sync/atomic" + "testing" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/auth" + "github.com/Horcag/agent-machine-control/internal/domain" + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" + "github.com/Horcag/agent-machine-control/internal/guest/stream" +) + +type guestPrepareTestOpener func(context.Context, domain.MachineRef, string) (guestssh.Duplex, error) + +func (f guestPrepareTestOpener) OpenDuplex(ctx context.Context, target domain.MachineRef, command string) (guestssh.Duplex, error) { + return f(ctx, target, command) +} + +type guestPrepareTestChannel struct{ net.Conn } + +func (guestPrepareTestChannel) Wait() error { return nil } + +func guestPreparedTestTransport(t *testing.T, provider guestConfigurationProvider, afterStartup func()) (*defaultGuestTransport, *atomic.Int32, stream.WorkerIdentity) { + t.Helper() + identity := stream.WorkerIdentity{PID: 1234, BirthTicks: 638000000000000001, Image: "synthetic-original-worker", SID: "synthetic-owner"} + calls := new(atomic.Int32) + var workers sync.WaitGroup + client := stream.NewClient(guestPrepareTestOpener(func(ctx context.Context, target domain.MachineRef, command string) (guestssh.Duplex, error) { + snapshot, ok := ctx.Value(guestDialSnapshotKey{}).(guestDialSnapshot) + if !ok || snapshot.target != target || snapshot.keys == nil || command != stream.StartupCommand { + return nil, errors.New("startup lost its frozen authority") + } + calls.Add(1) + local, remote := net.Pipe() + workers.Go(func() { + defer remote.Close() + var length uint32 + if binary.Read(remote, binary.BigEndian, &length) != nil || length > stream.MaxControl { + return + } + if _, err := io.CopyN(io.Discard, remote, int64(length)); err != nil { + return + } + if afterStartup != nil { + afterStartup() + } + data, _ := json.Marshal(identity) + // #nosec G115 -- fixed synthetic identity JSON is below the control bound. + if binary.Write(remote, binary.BigEndian, uint32(len(data))) != nil { + return + } + if _, err := remote.Write(data); err != nil { + return + } + _, _ = io.Copy(io.Discard, remote) + }) + return guestPrepareTestChannel{local}, nil + })) + t.Cleanup(func() { _ = client.Close(); workers.Wait() }) + return &defaultGuestTransport{client: client, provider: &provider}, calls, identity +} + +func TestGuestDefaultPrepareMatchesAuthenticatedActorGeneration(t *testing.T) { + for _, kind := range []auth.TokenType{auth.TokenTypeOperator, auth.TokenTypeAgentMCP} { + t.Run(string(kind), func(t *testing.T) { + provider, _, _, _ := newGuestConfigurationFixture(t) + token, err := auth.ReadTokenFile(provider.server.stateDir.AuthDir(), kind) + if err != nil { + t.Fatal(err) + } + caller, _, ok := provider.server.authStore.Authenticate(token) + if !ok { + t.Fatal("fixture token not authenticated") + } + target := domain.MachineRef("local:11111111-1111-4111-8111-111111111111") + configuration, err := provider.Current(t.Context(), target, *caller) + if err != nil { + t.Fatal(err) + } + transport, calls, original := guestPreparedTestTransport(t, provider, nil) + actual, err := transport.Prepare(t.Context(), target, "opaque-pool-key", configuration.Generation) + if err != nil || actual != original || calls.Load() != 1 { + t.Fatalf("actor generation lost: %+v %v calls=%d", actual, err, calls.Load()) + } + }) + } +} + +func TestGuestDefaultPreparePreservesOriginalIdentityAfterDrift(t *testing.T) { + provider, keys, caller, _ := newGuestConfigurationFixture(t) + target := domain.MachineRef("local:11111111-1111-4111-8111-111111111111") + configuration, err := provider.Current(t.Context(), target, *caller) + if err != nil { + t.Fatal(err) + } + transport, calls, original := guestPreparedTestTransport(t, provider, func() { keys.endpoint = "127.0.0.1:65001" }) + actual, err := transport.Prepare(t.Context(), target, "original-pool-key", configuration.Generation) + if !errors.Is(err, app.ErrGuestAuthority) || actual != original || calls.Load() != 1 { + t.Fatalf("post-start drift hid original identity or replayed: %+v %v calls=%d", actual, err, calls.Load()) + } + if err = transport.Retire(target, "original-pool-key", configuration.Generation); err != nil { + t.Fatal(err) + } + if _, err = transport.Prepare(t.Context(), target, "original-pool-key", configuration.Generation); !errors.Is(err, app.ErrGuestAuthority) || calls.Load() != 1 { + t.Fatalf("stale generation dialed again: %v calls=%d", err, calls.Load()) + } +} + +func TestGuestPinnedOpenerRejectsMissingOrForeignSnapshot(t *testing.T) { + target := domain.MachineRef("local:11111111-1111-4111-8111-111111111111") + contexts := []context.Context{t.Context(), context.WithValue(t.Context(), guestDialSnapshotKey{}, guestDialSnapshot{target: target}), context.WithValue(t.Context(), guestDialSnapshotKey{}, guestDialSnapshot{target: "local:22222222-2222-4222-8222-222222222222", keys: &frozenGuestKeys{}})} + for _, ctx := range contexts { + if _, err := (guestPinnedOpener{}).OpenDuplex(ctx, target, "synthetic-observation"); !errors.Is(err, app.ErrGuestAuthority) { + t.Fatalf("unbound route reached SSH: %v", err) + } + } +} diff --git a/internal/daemon/handlers_console.go b/internal/daemon/handlers_console.go index 8de5556..95e0c0f 100644 --- a/internal/daemon/handlers_console.go +++ b/internal/daemon/handlers_console.go @@ -18,7 +18,7 @@ func (s *Server) dispatchConsole(w http.ResponseWriter, r *http.Request, path st writeError(w, http.StatusUnauthorized, "unauthorized", "unauthenticated caller") return } - if s.consoleService == nil { + if s.consoleService == nil || s.desktopService == nil { writeError(w, http.StatusNotImplemented, "capability_unavailable", "console provider unavailable") return } diff --git a/internal/daemon/handlers_desktop.go b/internal/daemon/handlers_desktop.go index 4554ca3..1a4b419 100644 --- a/internal/daemon/handlers_desktop.go +++ b/internal/daemon/handlers_desktop.go @@ -128,6 +128,9 @@ func (s *Server) dispatchDesktop(w http.ResponseWriter, r *http.Request, path st } } +// LabGrantModeHeader opts into the persistent grant mode response field. +const LabGrantModeHeader = "X-AMC-Lab-Grant-Mode" + func (s *Server) dispatchDesktopLab(w http.ResponseWriter, r *http.Request, path string) { actor, _ := getCallerContext(r.Context()) switch path { @@ -145,7 +148,7 @@ func (s *Server) dispatchDesktopLab(w http.ResponseWriter, r *http.Request, path writeJSON(w, http.StatusOK, struct { Grant app.ConsoleLabGrant `json:"grant"` Receipt any `json:"receipt"` - }{grant, rcpt}) + }{labGrantResponse(r, grant), rcpt}) case "desktop/lab/status": var req struct { GrantID string `json:"grant_id"` @@ -159,6 +162,7 @@ func (s *Server) dispatchDesktopLab(w http.ResponseWriter, r *http.Request, path writeConsoleError(w, err) return } + out.Grant = labGrantResponse(r, out.Grant) writeJSON(w, http.StatusOK, out) case "desktop/lab/active": var req struct { @@ -173,6 +177,7 @@ func (s *Server) dispatchDesktopLab(w http.ResponseWriter, r *http.Request, path writeConsoleError(w, err) return } + out.Grant = labGrantResponse(r, out.Grant) writeJSON(w, http.StatusOK, out) case "desktop/lab/revoke": var req app.ConsoleLabGrantRevokeRequest @@ -190,3 +195,11 @@ func (s *Server) dispatchDesktopLab(w http.ResponseWriter, r *http.Request, path writeError(w, http.StatusNotFound, "not_found", "endpoint not found") } } + +// The protected grant retains its mode; only the legacy wire projection omits it. +func labGrantResponse(r *http.Request, grant app.ConsoleLabGrant) app.ConsoleLabGrant { + if r.Header.Get(LabGrantModeHeader) != "1" { + grant.Mode = "" + } + return grant +} diff --git a/internal/daemon/handlers_desktop_test.go b/internal/daemon/handlers_desktop_test.go index 8b0c892..add907f 100644 --- a/internal/daemon/handlers_desktop_test.go +++ b/internal/daemon/handlers_desktop_test.go @@ -38,7 +38,7 @@ func assertDesktopDaemonInvalidEnvelope(t *testing.T, url, token, body string) { func TestDesktopDaemonStrictRequestBoundary(t *testing.T) { backend := &consoleDaemonBackend{mockDaemonBackend: &mockDaemonBackend{}} endpoint, _, agent := setupConsoleDaemon(t, backend) - for _, path := range []string{"desktop/action", "desktop/lab/issue", "desktop/lab/status", "desktop/lab/active", "desktop/lab/revoke", "console/record"} { + for _, path := range []string{"desktop/action", "console/record"} { for _, body := range []string{`{"actor":"operator:forged"}`, `{"unknown":true}`, `{} {}`, `null`, `[]`, `{`, `{"target":"` + strings.Repeat("x", 128*1024) + `"}`} { t.Run(path+"/"+body[:min(len(body), 24)], func(t *testing.T) { assertDesktopDaemonInvalidEnvelope(t, endpoint+"/v1/"+path, agent, body) @@ -67,7 +67,7 @@ func TestDesktopDaemonStrictRequestBoundary(t *testing.T) { func TestDesktopDaemonUnavailableRoutes(t *testing.T) { srv, endpoint, _, agent := setupTestServer(t) defer func() { _ = srv.Shutdown(context.Background()) }() - for _, path := range []string{"desktop/action", "desktop/lab/issue", "desktop/lab/status", "desktop/lab/active", "desktop/lab/revoke", "console/record"} { + for _, path := range []string{"desktop/action", "console/record"} { status, body := doJSONReq(t, http.MethodPost, endpoint+"/v1/"+path, agent, map[string]any{}) if status != http.StatusNotImplemented || !bytes.Contains(body, []byte("capability_unavailable")) { t.Fatalf("%s status=%d body=%s", path, status, body) diff --git a/internal/daemon/handlers_internal_test.go b/internal/daemon/handlers_internal_test.go index 07f652f..9652c83 100644 --- a/internal/daemon/handlers_internal_test.go +++ b/internal/daemon/handlers_internal_test.go @@ -140,7 +140,7 @@ func TestServer_StartDefaultListenAddr(t *testing.T) { dir := missingDaemonStateRoot(t) srv, err := NewServer(Config{ StateDir: dir, - ListenAddr: "", // tests default 127.0.0.1:0 + ListenAddr: "", // tests automatic IPv6/IPv4 loopback selection }) if err != nil { t.Fatalf("NewServer failed: %v", err) diff --git a/internal/daemon/handlers_lab_compat_test.go b/internal/daemon/handlers_lab_compat_test.go new file mode 100644 index 0000000..63c77aa --- /dev/null +++ b/internal/daemon/handlers_lab_compat_test.go @@ -0,0 +1,79 @@ +package daemon_test + +import ( + "bytes" + "encoding/json" + "net/http" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/client" + "github.com/Horcag/agent-machine-control/internal/domain" +) + +// Matches the installed client's strict schema before persistent authority. +type legacyLabGrant struct { + AcknowledgeExternalEffects bool `json:"acknowledge_external_effects"` + SchemaVersion int `json:"schema_version"` + GrantID string `json:"grant_id"` + Target domain.MachineRef `json:"target"` + EnrollmentIdentity string `json:"enrollment_identity"` + Issuer domain.ActorID `json:"issuer"` + Beneficiary domain.ActorID `json:"beneficiary"` + Reason string `json:"reason"` + IdempotencyKey string `json:"idempotency_key"` + IssuedAt time.Time `json:"issued_at"` + ExpiresAt time.Time `json:"expires_at"` +} + +func TestLabGrantModeWireNegotiationPreservesLegacyStrictClients(t *testing.T) { + endpoint, operator, agent := setupConsoleDaemon(t, &consoleDaemonBackend{mockDaemonBackend: &mockDaemonBackend{}}) + request := app.ConsoleLabGrantIssueRequest{Mode: app.ConsoleLabModePersistentVM, Target: "default", Reason: "persistent compatibility fixture", IdempotencyKey: "wire-compatibility", Beneficiary: "agent:mcp-local", AcknowledgeExternalEffects: true} + status, body := doJSONReq(t, http.MethodPost, endpoint+"/v1/desktop/lab/issue", operator, request) + var issued struct { + Grant legacyLabGrant `json:"grant"` + Receipt domain.Receipt `json:"receipt"` + } + strictLabDecode(t, status, body, &issued) + if issued.Grant.GrantID == "" || !issued.Grant.ExpiresAt.IsZero() { + t.Fatalf("legacy issue: %+v", issued.Grant) + } + for _, route := range []string{"status", "active"} { + payload := map[string]string{"grant_id": issued.Grant.GrantID} + if route == "active" { + payload = map[string]string{"target": "default"} + } + status, body = doJSONReq(t, http.MethodPost, endpoint+"/v1/desktop/lab/"+route, agent, payload) + var legacy struct { + Grant legacyLabGrant `json:"grant"` + State string `json:"state"` + } + strictLabDecode(t, status, body, &legacy) + if legacy.State != "active" || legacy.Grant.GrantID != issued.Grant.GrantID { + t.Fatalf("legacy %s: %+v", route, legacy) + } + } + modern := client.New(endpoint, agent) + active, err := modern.ActiveConsoleLabGrant(t.Context(), "default") + if err != nil || active.Grant.Mode != app.ConsoleLabModePersistentVM || active.Grant.GrantID != issued.Grant.GrantID { + t.Fatalf("modern active: %+v %v", active, err) + } + grant, err := modern.ConsoleLabGrantStatus(t.Context(), issued.Grant.GrantID) + if err != nil || grant.Grant.Mode != app.ConsoleLabModePersistentVM { + t.Fatalf("modern status: %+v %v", grant, err) + } + replay, err := client.New(endpoint, operator).IssueConsoleLabGrant(t.Context(), request) + if err != nil || replay.Grant.Mode != app.ConsoleLabModePersistentVM || replay.Grant.GrantID != issued.Grant.GrantID { + t.Fatalf("modern issue replay: %+v %v", replay, err) + } +} + +func strictLabDecode(t *testing.T, status int, body []byte, out any) { + t.Helper() + decoder := json.NewDecoder(bytes.NewReader(body)) + decoder.DisallowUnknownFields() + if err := decoder.Decode(out); err != nil || status != http.StatusOK { + t.Fatalf("strict legacy status=%d body=%s error=%v", status, body, err) + } +} diff --git a/internal/daemon/listen.go b/internal/daemon/listen.go new file mode 100644 index 0000000..48865ff --- /dev/null +++ b/internal/daemon/listen.go @@ -0,0 +1,48 @@ +package daemon + +import ( + "errors" + "fmt" + "net" +) + +// Shared Windows/WSL state retains IPv4 reachability across both environments. +// Only positively identified Linux-local state opts into the faster local path. +func preferIPv6Loopback(platform, statePath string, inspect func(string) (bool, error)) bool { + if platform != "linux" { + return false + } + shared, err := inspect(statePath) + return err == nil && !shared +} + +// Explicit addresses remain exact and never fall back to another endpoint. +func listenLoopback(addr string, preferIPv6 bool, bind func(string, string) (net.Listener, error)) (net.Listener, error) { + if addr == "" { + var ipv6Err error + if preferIPv6 { + var listener net.Listener + listener, ipv6Err = bind("tcp6", "[::1]:0") + if ipv6Err == nil { + return listener, nil + } + } + listener, ipv4Err := bind("tcp4", "127.0.0.1:0") + if ipv4Err != nil { + return nil, fmt.Errorf("daemon: failed to listen on loopback: %w", errors.Join(ipv6Err, ipv4Err)) + } + return listener, nil + } + host, _, err := net.SplitHostPort(addr) + if err != nil { + return nil, fmt.Errorf("daemon: invalid listen address %q: %w", addr, err) + } + if ip := net.ParseIP(host); ip == nil || !ip.IsLoopback() { + return nil, fmt.Errorf("daemon: listen address host %q must be a loopback IP literal", host) + } + listener, err := bind("tcp", addr) + if err != nil { + return nil, fmt.Errorf("daemon: failed to listen on %s: %w", addr, err) + } + return listener, nil +} diff --git a/internal/daemon/listen_test.go b/internal/daemon/listen_test.go new file mode 100644 index 0000000..84a1a7f --- /dev/null +++ b/internal/daemon/listen_test.go @@ -0,0 +1,109 @@ +package daemon + +import ( + "errors" + "net" + "reflect" + "testing" +) + +type loopbackSelectionCase struct { + name, address string + preferIPv6 bool + failures []error + want []string +} + +func TestLoopbackListenerSelection(t *testing.T) { + available, err := net.Listen("tcp4", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer available.Close() + v6, v4 := errors.New("IPv6 unavailable"), errors.New("IPv4 unavailable") + cases := []loopbackSelectionCase{ + {"automatic_ipv6", "", true, []error{nil}, []string{"tcp6 [::1]:0"}}, + {"shared_ipv4", "", false, []error{nil}, []string{"tcp4 127.0.0.1:0"}}, + {"automatic_ipv4_fallback", "", true, []error{v6, nil}, []string{"tcp6 [::1]:0", "tcp4 127.0.0.1:0"}}, + {"automatic_both_fail", "", true, []error{v6, v4}, []string{"tcp6 [::1]:0", "tcp4 127.0.0.1:0"}}, + {"explicit_ipv4", "127.0.0.1:42", true, []error{nil}, []string{"tcp 127.0.0.1:42"}}, + {"explicit_ipv6", "[::1]:42", false, []error{nil}, []string{"tcp [::1]:42"}}, + {"explicit_failure_never_falls_back", "127.0.0.1:42", true, []error{v4}, []string{"tcp 127.0.0.1:42"}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { checkLoopbackSelection(t, tc, available) }) + } +} + +func checkLoopbackSelection(t *testing.T, tc loopbackSelectionCase, available net.Listener) { + t.Helper() + var calls []string + listener, gotErr := listenLoopback(tc.address, tc.preferIPv6, func(network, address string) (net.Listener, error) { + calls = append(calls, network+" "+address) + failure := tc.failures[len(calls)-1] + if failure != nil { + return nil, failure + } + return available, nil + }) + if !reflect.DeepEqual(calls, tc.want) { + t.Fatalf("bound %v; want %v", calls, tc.want) + } + if tc.failures[len(tc.failures)-1] == nil { + if gotErr != nil || listener != available { + t.Fatalf("successful bind lost: %v", gotErr) + } + return + } + if listener != nil { + t.Fatal("failed bind returned listener") + } + for _, wantErr := range tc.failures { + if wantErr != nil && !errors.Is(gotErr, wantErr) { + t.Fatalf("bind failure %v lost: %v", wantErr, gotErr) + } + } +} + +func TestIPv6PreferenceRequiresLinuxLocalState(t *testing.T) { + for _, tc := range []struct { + name, platform string + shared bool + err error + want bool + }{ + {"windows", "windows", false, nil, false}, + {"other_platform", "darwin", false, nil, false}, + {"linux_local", "linux", false, nil, true}, + {"shared_windows_mount", "linux", true, nil, false}, + {"unknown_mount", "linux", false, errors.New("mount inspection failed"), false}, + } { + t.Run(tc.name, func(t *testing.T) { + called := false + got := preferIPv6Loopback(tc.platform, "/synthetic/state", func(path string) (bool, error) { + called = true + if path != "/synthetic/state" { + t.Fatal("wrong state inspected") + } + return tc.shared, tc.err + }) + if got != tc.want || called != (tc.platform == "linux") { + t.Fatalf("preference=%v inspection=%v", got, called) + } + }) + } +} + +func TestLoopbackListenerRejectsUnsafeAddressesBeforeBind(t *testing.T) { + for _, address := range []string{"0.0.0.0:0", "[::]:0", "192.0.2.10:0", "localhost:0", "malformed"} { + t.Run(address, func(t *testing.T) { + _, err := listenLoopback(address, true, func(string, string) (net.Listener, error) { + t.Fatal("unsafe address reached bind") + return nil, nil + }) + if err == nil { + t.Fatal("unsafe address accepted") + } + }) + } +} diff --git a/internal/daemon/server.go b/internal/daemon/server.go index c503e5b..4984152 100644 --- a/internal/daemon/server.go +++ b/internal/daemon/server.go @@ -9,6 +9,7 @@ import ( "net" "net/http" "path/filepath" + "runtime" "strings" "sync" "sync/atomic" @@ -28,6 +29,7 @@ import ( "github.com/Horcag/agent-machine-control/internal/receipt" "github.com/Horcag/agent-machine-control/internal/sessions" "github.com/Horcag/agent-machine-control/internal/statedir" + "github.com/Horcag/agent-machine-control/internal/wslruntime" ) type contextKey string @@ -42,37 +44,39 @@ var ( // Server is the HTTP/1.1 daemon server for Agent Machine Control. type Server struct { - cfg Config - stateDir *statedir.StateDir - authStore *auth.Store - leaseMgr *lease.Manager - auditStore *audit.Store - receiptStore *receipt.Store - approvalStore *approval.Store - recoveryService *app.RecoveryService - consoleService *app.ConsoleService - desktopService *app.DesktopService - targetService *app.TargetService - targetCoordinator *app.TargetCoordinator - eventHub *events.Hub - opMgr *operations.Manager - sessionMgr *sessions.Manager - sessionService *app.SessionService - singletonLock *SingletonLock - httpServer *http.Server - listener net.Listener - endpoint string - startedAt time.Time - pid int - runtimeID string - startTime string - shutdownChan chan struct{} - shutdownOnce sync.Once - admissionClosed atomic.Bool - semaphore chan struct{} - identityProvider lease.IdentityProvider - shutdownHTTP func(context.Context) error - closeHTTP func() error + guestService *app.GuestService + cfg Config + stateDir *statedir.StateDir + authStore *auth.Store + leaseMgr *lease.Manager + auditStore *audit.Store + receiptStore *receipt.Store + approvalStore *approval.Store + recoveryService *app.RecoveryService + consoleService *app.ConsoleService + desktopService *app.DesktopService + targetService *app.TargetService + targetCoordinator *app.TargetCoordinator + eventHub *events.Hub + opMgr *operations.Manager + sessionMgr *sessions.Manager + sessionService *app.SessionService + singletonLock *SingletonLock + httpServer *http.Server + listener net.Listener + endpoint string + startedAt time.Time + pid int + runtimeID string + startTime string + shutdownChan chan struct{} + shutdownOnce sync.Once + admissionClosed atomic.Bool + guestShutdownComplete atomic.Bool + semaphore chan struct{} + identityProvider lease.IdentityProvider + shutdownHTTP func(context.Context) error + closeHTTP func() error afterEarlyMutationAdmissionCheck func() @@ -195,12 +199,16 @@ func NewServer(cfg Config) (*Server, error) { identityProvider: ident, } - if provider, ok := backend.(app.ConsoleProvider); ok { - srv.consoleService = app.NewConsoleService(provider, recoverySvc, targetService, filepath.Join(sd.Root(), "console-frames"), app.WithConsoleLabSafetyResolver(safetyResolver), app.WithConsoleLabEnrollmentIdentity(func(ctx context.Context, ref domain.MachineRef) (string, error) { - return targetService.EnrollmentIdentity(ctx, string(ref)) - })) + provider, _ := backend.(app.ConsoleProvider) + srv.consoleService = app.NewConsoleService(provider, recoverySvc, targetService, filepath.Join(sd.Root(), "console-frames"), app.WithConsoleLabSafetyResolver(safetyResolver), app.WithConsoleLabEnrollmentIdentity(func(ctx context.Context, ref domain.MachineRef) (string, error) { + return targetService.EnrollmentIdentity(ctx, string(ref)) + })) + app.WithSessionLabAuthority(srv.consoleService)(sessionSvc) + app.WithRecoveryLabAuthority(srv.consoleService)(recoverySvc) + if provider != nil { srv.desktopService = app.NewDesktopService(guestdesktop.New(guestssh.NewTransport(keyProvider)), srv.consoleService) } + srv.initializeGuestService(sessionSvc, targetService, keyProvider) srv.setupHTTPServer() return srv, nil } @@ -279,6 +287,10 @@ func (s *Server) dispatchV1(w http.ResponseWriter, r *http.Request) { func (s *Server) dispatchOtherV1(w http.ResponseWriter, r *http.Request, path string) { switch { + case path == "guest/operations": + s.handleGuestOperation(w, r) + case path == "guest/approvals": + s.handleGuestApproval(w, r) case strings.HasPrefix(path, "desktop/"): s.dispatchDesktop(w, r, path) case path == "audit": @@ -355,32 +367,13 @@ func (s *Server) dispatchReceiptSubroute(w http.ResponseWriter, r *http.Request, // Start opens the network listener and records the daemon endpoint. func (s *Server) Start() error { - addr := s.cfg.ListenAddr - if addr == "" { - addr = "127.0.0.1:0" - } - - host, _, err := net.SplitHostPort(addr) - if err != nil { - if s.singletonLock != nil { - _ = s.singletonLock.Release() - } - return fmt.Errorf("daemon: invalid listen address %q: %w", addr, err) - } - ip := net.ParseIP(host) - if ip == nil || !ip.IsLoopback() { - if s.singletonLock != nil { - _ = s.singletonLock.Release() - } - return fmt.Errorf("daemon: listen address host %q must be a loopback IP literal", host) - } - - ln, err := net.Listen("tcp", addr) + preferIPv6 := preferIPv6Loopback(runtime.GOOS, s.stateDir.Root(), wslruntime.IsWindowsHostPath) + ln, err := listenLoopback(s.cfg.ListenAddr, preferIPv6, net.Listen) if err != nil { if s.singletonLock != nil { _ = s.singletonLock.Release() } - return fmt.Errorf("daemon: failed to listen on %s: %w", addr, err) + return err } s.listener = ln diff --git a/internal/daemon/session_security_timeout_test.go b/internal/daemon/session_security_timeout_test.go index 1f5daea..c7acd1e 100644 --- a/internal/daemon/session_security_timeout_test.go +++ b/internal/daemon/session_security_timeout_test.go @@ -79,17 +79,37 @@ func openSubSecondSetupSession(t *testing.T, endpoint, token string) string { type deadlineCaptureTransport struct { mu sync.Mutex remaining map[string]time.Duration + applied map[string]bool } -func (t *deadlineCaptureTransport) record(ctx context.Context, name string) { +// record observes entry separately from the fake's effect acceptance. +func (t *deadlineCaptureTransport) record(ctx context.Context, name string) error { deadline, ok := ctx.Deadline() t.mu.Lock() defer t.mu.Unlock() - if !ok { - t.remaining[name] = -1 - return + remaining := time.Duration(-1) + if ok { + remaining = time.Until(deadline) + } + t.remaining[name] = remaining + if err := ctx.Err(); err != nil { + return err + } + // Timer delivery can lag the actual deadline under test-host load. + if ok && remaining <= 0 { + return context.DeadlineExceeded + } + if t.applied == nil { + t.applied = make(map[string]bool) } - t.remaining[name] = time.Until(deadline) + t.applied[name] = true + return nil +} + +func (t *deadlineCaptureTransport) appliedFor(name string) bool { + t.mu.Lock() + defer t.mu.Unlock() + return t.applied[name] } func (t *deadlineCaptureTransport) remainingFor(name string) (time.Duration, bool) { @@ -104,13 +124,13 @@ func assertSubSecondTransportOutcome(t *testing.T, transport *deadlineCaptureTra remaining, called := transport.remainingFor(operation) switch status { case http.StatusOK: - if !called || remaining <= 0 || remaining > 250*time.Millisecond { + if !called || !transport.appliedFor(operation) || remaining <= 0 || remaining > 250*time.Millisecond { t.Fatalf("%s transport deadline remaining=%v present=%v, want (0, 250ms]; body=%s", operation, remaining, called, body) } return case http.StatusGatewayTimeout: - if called { - t.Fatalf("%s transport was called after admission exhausted its budget: remaining=%v; body=%s", operation, remaining, body) + if transport.appliedFor(operation) { + t.Fatalf("%s transport applied an effect after budget exhaustion: remaining=%v called=%v; body=%s", operation, remaining, called, body) } return default: @@ -120,7 +140,9 @@ func assertSubSecondTransportOutcome(t *testing.T, transport *deadlineCaptureTra } func (t *deadlineCaptureTransport) Dial(ctx context.Context, _ domain.MachineRef, _, _ uint16, _ string) (guestssh.Channel, error) { - t.record(ctx, "open") + if err := t.record(ctx, "open"); err != nil { + return nil, err + } reader, writer := io.Pipe() channel := &deadlineCaptureChannel{parent: t, reader: reader, writer: writer, waitCh: make(chan struct{})} go func() { _, _ = writer.Write([]byte("synthetic prompt> ")) }() @@ -137,17 +159,23 @@ type deadlineCaptureChannel struct { func (c *deadlineCaptureChannel) Read(p []byte) (int, error) { return c.reader.Read(p) } func (c *deadlineCaptureChannel) Write(ctx context.Context, p []byte) (int, error) { - c.parent.record(ctx, "write") + if err := c.parent.record(ctx, "write"); err != nil { + return 0, err + } return len(p), nil } func (c *deadlineCaptureChannel) SendControl(ctx context.Context, _ domain.ControlKey) (guestssh.ControlResult, error) { - c.parent.record(ctx, "control") + if err := c.parent.record(ctx, "control"); err != nil { + return guestssh.ControlResult{}, err + } return guestssh.ControlResult{AcceptedBytes: 1, EffectApplied: true}, nil } func (c *deadlineCaptureChannel) Resize(uint16, uint16) error { return nil } func (c *deadlineCaptureChannel) Close(ctx context.Context) error { + if err := c.parent.record(ctx, "close"); err != nil { + return err + } c.once.Do(func() { - c.parent.record(ctx, "close") _ = c.writer.Close() _ = c.reader.Close() close(c.waitCh) @@ -307,3 +335,36 @@ func TestDaemonSessions_ActiveBearerTokensAreMandatoryExactSecrets(t *testing.T) closeSanitizerSession(t, sessionPath, operatorToken) assertTokensAbsentFromEvidence(t, stateDir, []string{operatorToken, agentToken}) } + +func TestDeadlineCaptureWriteSeparatesObservationFromEffect(t *testing.T) { + for _, mode := range []string{"expired", "canceled", "active"} { + t.Run(mode, func(t *testing.T) { + ctx := t.Context() + expected := error(nil) + switch mode { + case "expired": + deadline, cancel := context.WithDeadline(ctx, time.Now().Add(-time.Second)) + defer cancel() + ctx, expected = deadline, context.DeadlineExceeded + case "canceled": + canceled, cancel := context.WithCancel(ctx) + cancel() + ctx, expected = canceled, context.Canceled + } + transport := &deadlineCaptureTransport{remaining: make(map[string]time.Duration)} + channel := &deadlineCaptureChannel{parent: transport} + count, err := channel.Write(ctx, []byte("synthetic payload")) + _, called := transport.remainingFor("write") + if !called { + t.Fatal("mock entry observation missing") + } + if expected != nil { + if !errors.Is(err, expected) || count != 0 || transport.appliedFor("write") { + t.Fatalf("expired/canceled write accepted an effect: n=%d err=%v", count, err) + } + } else if err != nil || count != len("synthetic payload") || !transport.appliedFor("write") { + t.Fatalf("active write not accepted: n=%d err=%v", count, err) + } + }) + } +} diff --git a/internal/daemon/shutdown.go b/internal/daemon/shutdown.go index 5f528ee..40a7b63 100644 --- a/internal/daemon/shutdown.go +++ b/internal/daemon/shutdown.go @@ -31,7 +31,11 @@ func (s *Server) Shutdown(ctx context.Context) error { } func (s *Server) shutdownManagers(ctx context.Context) error { - var operationErr, sessionErr error + var operationErr, sessionErr, guestErr error + if s.guestService != nil { + guestErr = s.guestService.Shutdown(ctx) + s.guestShutdownComplete.Store(guestErr == nil) + } if s.opMgr != nil { if err := s.opMgr.Shutdown(ctx); err != nil { operationErr = fmt.Errorf("daemon: operations manager shutdown error: %w", err) @@ -42,7 +46,7 @@ func (s *Server) shutdownManagers(ctx context.Context) error { sessionErr = fmt.Errorf("daemon: session manager shutdown error: %w", err) } } - return errors.Join(operationErr, sessionErr) + return errors.Join(operationErr, sessionErr, guestErr) } func (s *Server) closeEventHub() error { @@ -96,5 +100,5 @@ func (s *Server) readServeError() error { func (s *Server) managersDrained() bool { operationsDrained := s.opMgr == nil || s.opMgr.Drained() sessionsDrained := s.sessionMgr == nil || s.sessionMgr.Drained() - return operationsDrained && sessionsDrained + return operationsDrained && sessionsDrained && (s.guestService == nil || s.guestShutdownComplete.Load()) } diff --git a/internal/daemoncli/cli.go b/internal/daemoncli/cli.go index 75ece11..2e171e4 100644 --- a/internal/daemoncli/cli.go +++ b/internal/daemoncli/cli.go @@ -60,7 +60,7 @@ func runDaemon(args []string, stdout, stderr io.Writer) int { fs.SetOutput(stderr) stateDir := fs.String("state-dir", "", "state directory path") - listenAddr := fs.String("listen", "127.0.0.1:0", "HTTP listen address") + listenAddr := fs.String("listen", "", "HTTP loopback address (automatic; shared Windows state uses IPv4)") jsonOutput := fs.Bool("json", false, "emit machine-readable JSON") if err := fs.Parse(args); err != nil { @@ -225,7 +225,7 @@ func printDaemonUsage(w io.Writer) { fmt.Fprintln(w, "") fmt.Fprintln(w, "Flags:") fmt.Fprintln(w, " --state-dir Override state directory path") - fmt.Fprintln(w, " --listen Listen address (default 127.0.0.1:0)") + fmt.Fprintln(w, " --listen Exact loopback address (default: automatic; shared Windows state uses IPv4)") fmt.Fprintln(w, " --json Emit machine-readable JSON") fmt.Fprintln(w, " --help, -h Show help") fmt.Fprintln(w, " --version, -v Show version") diff --git a/internal/domain/guest_operation.go b/internal/domain/guest_operation.go new file mode 100644 index 0000000..80d4408 --- /dev/null +++ b/internal/domain/guest_operation.go @@ -0,0 +1,106 @@ +package domain + +// Guest operation capabilities reuse session authority while identifying whole +// guest operations independently from interactive terminal chunks. +const CapabilityGuestOperation = "guest.operation" + +func IsGuestOperation(kind OperationKind) bool { + switch kind { + case "guest.prepare", "guest.put", "guest.get", "guest.exec", "guest.remove-owned": + return true + default: + return false + } +} + +func GuestOperationScopes(kind OperationKind) []string { + switch kind { + case "guest.prepare": + return []string{ScopeSessionOpen} + case "guest.exec": + return []string{ScopeSessionWrite, ScopeSessionRead, ScopeEvidenceCapture} + case "guest.get": + return []string{ScopeSessionRead, ScopeEvidenceCapture} + default: + return []string{ScopeSessionWrite} + } +} + +func validateGuestOperationParams(params map[string]any) error { + if len(params) != 6 || !canonicalHex(params["configuration_sha256"], 64) || !canonicalHex(params["payload_sha256"], 64) { + return ErrNonCanonicalParameter + } + size, err := guestParameterInt(params["size"]) + if err != nil { + return ErrNonCanonicalParameter + } + limit, err := guestParameterInt(params["max_output"]) + if err != nil { + return ErrNonCanonicalParameter + } + ids, ok := params["artifact_ids"].([]string) + if !ok { + return ErrNonCanonicalParameter + } + op, ok := params["operation"].(string) + if !ok || !IsGuestOperation(OperationKind("guest."+op)) { + return ErrNonCanonicalParameter + } + return ValidateGuestOperationShape(op, size, limit, ids) +} + +func validateGuestArtifactIDs(ids []string) error { + seen := map[string]bool{} + for _, id := range ids { + if !canonicalHex(id, 32) || seen[id] { + return ErrNonCanonicalParameter + } + seen[id] = true + } + return nil +} + +// ValidateGuestOperationShape applies the shared bounds and artifact contract before I/O. +func ValidateGuestOperationShape(op string, size, limit int64, ids []string) error { + if size < 0 || size > 1<<40 || limit < 0 || limit > 1<<40 || len(ids) > 128 { + return ErrNonCanonicalParameter + } + if err := validateGuestArtifactIDs(ids); err != nil { + return err + } + return validateGuestShape(op, size, limit, len(ids)) +} + +func validateGuestShape(op string, size, limit int64, number int) error { + valid := false + switch op { + case "prepare": + valid = size == 0 && limit == 0 && number == 0 + case "put": + valid = limit == 0 && number == 0 + case "exec": + valid = validGuestCommandShape(size, limit, number) + case "get": + valid = size == 0 && limit > 0 && number == 1 + case "remove-owned": + valid = size == 0 && limit == 0 && number > 0 + } + if !valid { + return ErrNonCanonicalParameter + } + return nil +} +func validGuestCommandShape(size, limit int64, number int) bool { + return size <= 16*1024*1024 && limit > 0 && limit <= 16*1024*1024 && number == 0 +} + +func guestParameterInt(value any) (int64, error) { + switch v := value.(type) { + case int: + return int64(v), nil + case int64: + return v, nil + default: + return 0, ErrNonCanonicalParameter + } +} diff --git a/internal/domain/guest_operation_test.go b/internal/domain/guest_operation_test.go new file mode 100644 index 0000000..400b324 --- /dev/null +++ b/internal/domain/guest_operation_test.go @@ -0,0 +1,63 @@ +package domain + +import ( + "strings" + "testing" +) + +func guestFixtureParams(operation string) map[string]any { + return map[string]any{"operation": operation, "configuration_sha256": strings.Repeat("a", 64), "payload_sha256": strings.Repeat("b", 64), "size": int64(0), "max_output": int64(0), "artifact_ids": []string{}} +} +func TestGuestOperationSchemaFailsClosed(t *testing.T) { + for _, tc := range []struct { + name string + mutate func(map[string]any) + }{ + {"foreign path", func(p map[string]any) { p["path"] = `C:\foreign` }}, + {"fractional size", func(p map[string]any) { p["size"] = 0.5 }}, + {"invalid digest", func(p map[string]any) { p["payload_sha256"] = "invalid" }}, + {"other operation", func(p map[string]any) { p["operation"] = "prepare" }}, + {"oversized input", func(p map[string]any) { p["size"] = int64(1<<40) + 1 }}, + {"foreign references", func(p map[string]any) { p["artifact_ids"] = []string{`C:\foreign`} }}, + } { + t.Run(tc.name, func(t *testing.T) { + p := guestFixtureParams("put") + tc.mutate(p) + if ValidateOperationParameters("guest.put", p) == nil { + t.Fatal("accepted invalid guest parameters") + } + }) + } +} +func TestGuestManifestIsBoundedAndUnique(t *testing.T) { + p := guestFixtureParams("remove-owned") + p["artifact_ids"] = []string{strings.Repeat("a", 32)} + if err := ValidateOperationParameters("guest.remove-owned", p); err != nil { + t.Fatal(err) + } + p["artifact_ids"] = []string{strings.Repeat("a", 32), strings.Repeat("a", 32)} + if ValidateOperationParameters("guest.remove-owned", p) == nil { + t.Fatal("duplicate references accepted") + } + ids := make([]string, 129) + p["artifact_ids"] = ids + if ValidateOperationParameters("guest.remove-owned", p) == nil { + t.Fatal("unbounded manifest accepted") + } +} +func TestGuestCommandOutputBoundsAndScopes(t *testing.T) { + p := guestFixtureParams("exec") + p["size"] = int64(10) + p["max_output"] = int64(10) + if err := ValidateOperationParameters("guest.exec", p); err != nil { + t.Fatal(err) + } + p["max_output"] = int64(16*1024*1024 + 1) + if ValidateOperationParameters("guest.exec", p) == nil { + t.Fatal("unbounded command output") + } + scopes := NewScopeSet(GuestOperationScopes("guest.exec")...) + if !scopes.Has(ScopeSessionWrite) || !scopes.Has(ScopeEvidenceCapture) { + t.Fatal("command scopes lack effect or sensitive-output authority") + } +} diff --git a/internal/domain/validation.go b/internal/domain/validation.go index 1d26600..cada951 100644 --- a/internal/domain/validation.go +++ b/internal/domain/validation.go @@ -202,6 +202,14 @@ func ValidateOperationParameters(kind OperationKind, params map[string]any) erro } func specialOperationParameterValidator(kind OperationKind) func(map[string]any) error { + if IsGuestOperation(kind) { + return func(params map[string]any) error { + if params["operation"] != strings.TrimPrefix(string(kind), "guest.") { + return ErrNonCanonicalParameter + } + return validateGuestOperationParams(params) + } + } switch kind { case "console.input": return validateConsoleInputParams diff --git a/internal/guest/ssh/duplex.go b/internal/guest/ssh/duplex.go new file mode 100644 index 0000000..d5fa433 --- /dev/null +++ b/internal/guest/ssh/duplex.go @@ -0,0 +1,134 @@ +package ssh + +import ( + "context" + "errors" + "io" + "net" + "sync" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" + gossh "golang.org/x/crypto/ssh" +) + +// Duplex is a binary non-PTY session. It owns its SSH connection; closing it +// never changes another session's socket deadlines. +type Duplex interface { + io.ReadWriteCloser + Wait() error +} + +type duplex struct { + reader io.Reader + writer io.WriteCloser + session *gossh.Session + client *gossh.Client + once sync.Once + err error + exitDone chan struct{} + waitErr error + progress *startupProgress +} + +func (d *duplex) Read(p []byte) (int, error) { return d.reader.Read(p) } +func (d *duplex) Write(p []byte) (int, error) { return d.writer.Write(p) } +func (d *duplex) Close() error { + d.once.Do(func() { d.err = errors.Join(d.session.Close(), d.client.Close()) }) + return d.err +} +func (d *duplex) Wait() error { <-d.exitDone; return d.waitErr } + +// StartupStage is a closed remote observation, not proof of identity or absence. +func (d *duplex) StartupStage() string { + if d.progress == nil { + return "" + } + return d.progress.Stage() +} + +// ExitStatus observes only this channel's cached remote exit, never stderr text. +func (d *duplex) ExitStatus(ctx context.Context) (int, bool) { + select { + case <-ctx.Done(): + return 0, false + case <-d.exitDone: + if ctx.Err() != nil { + return 0, false + } + if exited, ok := errors.AsType[*gossh.ExitError](d.waitErr); ok { + return exited.ExitStatus(), true + } + if d.waitErr == nil { + return 0, true + } + return 0, false + } +} + +// OpenDuplex resolves enrolled credentials and pinned host identity on each +// worker creation. The caller owns lifetime after startup and must close it. +func (t *NativeTransport) OpenDuplex(ctx context.Context, target domain.MachineRef, command string) (Duplex, error) { + if command == "" || len(command) > 8192 { + return nil, ErrCommandFailed + } + dc, err := t.resolveDialConfig(ctx, target, 0, 0, "") + if err != nil { + return nil, err + } + dial := t.dialContext + if dial == nil { + dial = (&net.Dialer{}).DialContext + } + conn, err := dial(ctx, "tcp", dc.endpoint) + if err != nil { + return nil, commandError(ctx) + } + handshakeCtx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + stop := context.AfterFunc(handshakeCtx, func() { _ = conn.Close() }) + defer stop() + sc, ch, rq, err := gossh.NewClientConn(conn, dc.endpoint, dc.config) + if err != nil { + _ = conn.Close() + return nil, commandError(handshakeCtx) + } + client := gossh.NewClient(sc, ch, rq) + session, err := client.NewSession() + if err != nil { + _ = client.Close() + return nil, commandError(ctx) + } + failed := true + defer func() { + if failed { + _ = session.Close() + _ = client.Close() + } + }() + stdin, err := session.StdinPipe() + if err != nil { + return nil, ErrCommandFailed + } + stdout, err := session.StdoutPipe() + if err != nil { + return nil, ErrCommandFailed + } + // Drain all stderr, retaining only exact ordered startup markers, never text. + progress := &startupProgress{} + session.Stderr = progress + if err = session.Start(command); err != nil { + return nil, commandError(ctx) + } + if err = handshakeCtx.Err(); err != nil { + return nil, err + } + // Synchronize cancellation before transferring connection ownership. + if !stop() { + return nil, commandError(handshakeCtx) + } + failed = false + d := &duplex{reader: stdout, writer: stdin, session: session, client: client, exitDone: make(chan struct{}), progress: progress} + go func() { d.waitErr = session.Wait(); close(d.exitDone) }() + return d, nil +} diff --git a/internal/guest/ssh/duplex_bench_test.go b/internal/guest/ssh/duplex_bench_test.go new file mode 100644 index 0000000..d04fc67 --- /dev/null +++ b/internal/guest/ssh/duplex_bench_test.go @@ -0,0 +1,185 @@ +package ssh_test + +import ( + "bytes" + "context" + "crypto/ed25519" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "fmt" + "io" + "net" + "sync" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/guest/ssh" + gossh "golang.org/x/crypto/ssh" +) + +const duplexBenchmarkPayloadSize = 64 << 20 +const duplexBenchmarkChunkSize = 64 << 10 + +// BenchmarkDuplexStreaming64MiB compares binary SSH channel throughput over +// IPv4 and IPv6 loopback using the normal x/crypto/ssh algorithm preferences. +// The server echoes concurrently so neither channel window can deadlock. +func BenchmarkDuplexStreaming64MiB(b *testing.B) { + for _, tc := range []struct { + name, listenAddress string + }{ + {name: "IPv4", listenAddress: "127.0.0.1:0"}, + {name: "IPv6", listenAddress: "[::1]:0"}, + } { + b.Run(tc.name, func(b *testing.B) { + benchmarkDuplexStreaming(b, tc.listenAddress) + }) + } +} + +func benchmarkDuplexStreaming(b *testing.B, listenAddress string) { + + clientSigner := newDuplexBenchmarkSigner(b) + hostSigner := newDuplexBenchmarkSigner(b) + serverConfig := &gossh.ServerConfig{NoClientAuth: true} + serverConfig.AddHostKey(hostSigner) + listener, err := net.Listen("tcp", listenAddress) + if err != nil { + b.Fatal(err) + } + var serverWG sync.WaitGroup + negotiated := make(chan gossh.NegotiatedAlgorithms, 1) + serverWG.Go(func() { + for { + conn, acceptErr := listener.Accept() + if acceptErr != nil { + return + } + serverWG.Go(func() { serveDuplexBenchmarkConn(conn, serverConfig, negotiated) }) + } + }) + defer func() { + _ = listener.Close() + serverWG.Wait() + }() + + hostHash := sha256.Sum256(hostSigner.PublicKey().Marshal()) + provider := &ssh.MockKeyProvider{ + Signer: clientSigner, + PinnedKeySHA256: base64.StdEncoding.EncodeToString(hostHash[:]), + Endpoint: listener.Addr().String(), + User: "synthetic-benchmark-user", + } + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + duplex, err := ssh.NewTransport(provider).OpenDuplex(ctx, "synthetic-benchmark-target", "synthetic-benchmark-worker") + if err != nil { + cancel() + b.Fatal(err) + } + stopClose := context.AfterFunc(ctx, func() { _ = duplex.Close() }) + defer func() { + stopClose() + cancel() + _ = duplex.Close() + }() + algorithms := <-negotiated + b.Logf("negotiated SSH write cipher: %s", algorithms.Write.Cipher) + + payload := make([]byte, duplexBenchmarkPayloadSize) + for i := range payload { + payload[i] = byte(i*31 + i/251) + } + writeDone := make(chan error, 1) + reader := make([]byte, duplexBenchmarkChunkSize) + b.SetBytes(int64(len(payload))) + b.ReportAllocs() + b.ResetTimer() + for range b.N { + go func() { writeDone <- writeDuplexBenchmarkPayload(duplex, payload) }() + if err = verifyDuplexBenchmarkPayload(duplex, payload, reader); err != nil { + b.Fatal(err) + } + + if err = <-writeDone; err != nil { + b.Fatal(err) + } + } + b.StopTimer() + b.ReportMetric(float64(duplexBenchmarkPayloadSize*b.N)/b.Elapsed().Seconds()/(1<<20), "MiB/s") +} + +func serveDuplexBenchmarkConn(conn net.Conn, config *gossh.ServerConfig, negotiated chan<- gossh.NegotiatedAlgorithms) { + server, channels, requests, err := gossh.NewServerConn(conn, config) + if err != nil { + _ = conn.Close() + return + } + defer server.Close() + negotiated <- server.Conn.(gossh.AlgorithmsConnMetadata).Algorithms() + go gossh.DiscardRequests(requests) + var sessionWG sync.WaitGroup + for newChannel := range channels { + channel, requests, acceptErr := newChannel.Accept() + if acceptErr != nil { + return + } + sessionWG.Go(func() { + defer channel.Close() + for request := range requests { + if request.Type == "exec" { + _ = request.Reply(true, nil) + _, _ = io.Copy(channel, channel) + return + } + _ = request.Reply(false, nil) + } + }) + } + sessionWG.Wait() +} + +func newDuplexBenchmarkSigner(b *testing.B) gossh.Signer { + b.Helper() + _, privateKey, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + b.Fatal(err) + } + signer, err := gossh.NewSignerFromKey(privateKey) + if err != nil { + b.Fatal(err) + } + return signer +} + +func writeDuplexBenchmarkPayload(writer io.Writer, payload []byte) error { + for offset := 0; offset < len(payload); { + end := min(offset+duplexBenchmarkChunkSize, len(payload)) + chunk := payload[offset:end] + for len(chunk) > 0 { + n, err := writer.Write(chunk) + if err != nil { + return err + } + if n == 0 { + return io.ErrShortWrite + } + chunk = chunk[n:] + } + offset = end + } + return nil +} + +func verifyDuplexBenchmarkPayload(reader io.Reader, payload, buffer []byte) error { + for offset := 0; offset < len(payload); { + end := min(offset+len(buffer), len(payload)) + if _, err := io.ReadFull(reader, buffer[:end-offset]); err != nil { + return err + } + if !bytes.Equal(buffer[:end-offset], payload[offset:end]) { + return fmt.Errorf("echo differs at byte %d", offset) + } + offset = end + } + return nil +} diff --git a/internal/guest/ssh/duplex_exit_test.go b/internal/guest/ssh/duplex_exit_test.go new file mode 100644 index 0000000..9931653 --- /dev/null +++ b/internal/guest/ssh/duplex_exit_test.go @@ -0,0 +1,48 @@ +package ssh_test + +import ( + "context" + "io" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/guest/ssh" + "github.com/Horcag/agent-machine-control/internal/guest/ssh/fakeserver" +) + +func TestDuplexExitStatusCachedAndContextBounded(t *testing.T) { + signer, key := generateClientKey(t) + server, err := fakeserver.New(fakeserver.ModeExitEarly, key) + if err != nil { + t.Fatal(err) + } + defer server.Close() + server.SetExitCode(214) + provider := &ssh.MockKeyProvider{Signer: signer, PinnedKeySHA256: server.HostKeyPin(), Endpoint: server.Addr(), User: "synthetic"} + ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) + defer cancel() + channel, err := ssh.NewTransport(provider).OpenDuplex(ctx, "target", "fixed-worker") + if err != nil { + t.Fatal(err) + } + defer channel.Close() + if _, err = io.Copy(io.Discard, channel); err != nil { + t.Fatal(err) + } + observer := channel.(interface { + ExitStatus(context.Context) (int, bool) + }) + for range 2 { + if code, known := observer.ExitStatus(ctx); !known || code != 214 { + t.Fatal("exit not cached", code, known) + } + if channel.Wait() == nil { + t.Fatal("nonzero exit lost") + } + } + cancelled, stop := context.WithCancel(t.Context()) + stop() + if _, known := observer.ExitStatus(cancelled); known { + t.Fatal("cancelled observation adopted exit") + } +} diff --git a/internal/guest/ssh/duplex_protocol_test.go b/internal/guest/ssh/duplex_protocol_test.go new file mode 100644 index 0000000..e02915a --- /dev/null +++ b/internal/guest/ssh/duplex_protocol_test.go @@ -0,0 +1,102 @@ +package ssh_test + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/base64" + "io" + "net" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/guest/ssh" + gossh "golang.org/x/crypto/ssh" +) + +func TestDuplexExecNeverAllocatesPTY(t *testing.T) { + clientSigner, _ := generateClientKey(t) + hostSigner, _ := generateClientKey(t) + config := &gossh.ServerConfig{NoClientAuth: true} + config.AddHostKey(hostSigner) + listener, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + var pty, execRequests atomic.Int64 + var wg sync.WaitGroup + wg.Go(func() { + conn, err := listener.Accept() + if err != nil { + return + } + defer conn.Close() + serveDuplexProtocol(conn, config, &pty, &execRequests) + }) + defer func() { _ = listener.Close(); wg.Wait() }() + hostHash := sha256.Sum256(hostSigner.PublicKey().Marshal()) + provider := &ssh.MockKeyProvider{Signer: clientSigner, PinnedKeySHA256: base64.StdEncoding.EncodeToString(hostHash[:]), Endpoint: listener.Addr().String(), User: "synthetic-user"} + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + ch, err := ssh.NewTransport(provider).OpenDuplex(ctx, "target", "fixed-startup") + if err != nil { + t.Fatal(err) + } + defer ch.Close() + payload := []byte{0, 255, 128, 13, 10} + _, err = ch.Write(payload) + if err != nil { + t.Fatal(err) + } + got := make([]byte, len(payload)) + if _, err = io.ReadFull(ch, got); err != nil || !bytes.Equal(payload, got) { + t.Fatal(got, err) + } + observer, ok := ch.(interface{ StartupStage() string }) + if !ok { + t.Fatal("duplex omitted startup observation") + } + for observer.StartupStage() != "before_worker_run" { + select { + case <-ctx.Done(): + t.Fatal("stderr markers not observed", observer.StartupStage()) + case <-time.After(time.Millisecond): + } + } + if pty.Load() != 0 || execRequests.Load() != 1 { + t.Fatalf("PTY=%d exec=%d", pty.Load(), execRequests.Load()) + } +} + +func serveDuplexProtocol(conn net.Conn, config *gossh.ServerConfig, pty, execRequests *atomic.Int64) { + server, channels, requests, err := gossh.NewServerConn(conn, config) + if err != nil { + return + } + defer server.Close() + go gossh.DiscardRequests(requests) + for newChannel := range channels { + channel, requests, err := newChannel.Accept() + if err != nil { + return + } + for request := range requests { + switch request.Type { + case "pty-req": + pty.Add(1) + _ = request.Reply(false, nil) + case "exec": + execRequests.Add(1) + _ = request.Reply(true, nil) + _, _ = io.WriteString(channel.Stderr(), "private error/path\nAMC_STARTUP_V1:1:before_header_read\r\nAMC_STARTUP_V1:2:before_source_read\nAMC_STARTUP_V1:3:before_compile\nAMC_STARTUP_V1:4:before_worker_run\n") + _, _ = io.Copy(channel, channel) + _ = channel.Close() + return + default: + _ = request.Reply(false, nil) + } + } + } +} diff --git a/internal/guest/ssh/duplex_test.go b/internal/guest/ssh/duplex_test.go new file mode 100644 index 0000000..0f84401 --- /dev/null +++ b/internal/guest/ssh/duplex_test.go @@ -0,0 +1,69 @@ +package ssh_test + +import ( + "bytes" + "context" + "io" + "strings" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/guest/ssh" + "github.com/Horcag/agent-machine-control/internal/guest/ssh/fakeserver" +) + +func TestDuplexPinnedBinaryRoundTripAndSiblingIsolation(t *testing.T) { + signer, key := generateClientKey(t) + server, err := fakeserver.New(fakeserver.ModeEcho, key) + if err != nil { + t.Fatal(err) + } + defer server.Close() + provider := &ssh.MockKeyProvider{Signer: signer, PinnedKeySHA256: server.HostKeyPin(), Endpoint: server.Addr(), User: "synthetic-user"} + transport := ssh.NewTransport(provider) + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + first, err := transport.OpenDuplex(ctx, "aaaaaaaa-aaaa-4aaa-baaa-aaaaaaaaaaaa", "synthetic-worker") + if err != nil { + t.Fatal(err) + } + defer first.Close() + second, err := transport.OpenDuplex(ctx, "aaaaaaaa-aaaa-4aaa-baaa-aaaaaaaaaaaa", "synthetic-worker") + if err != nil { + t.Fatal(err) + } + defer second.Close() + for _, channel := range []ssh.Duplex{first, second} { + prefix := make([]byte, len("PS C:\\> ")) + if _, err = io.ReadFull(channel, prefix); err != nil { + t.Fatal(err) + } + } + _ = first.Close() + payload := []byte{0, 255, 128, 13, 10, 1} + if _, err = second.Write(payload); err != nil { + t.Fatal(err) + } + got := make([]byte, len(payload)) + if _, err = io.ReadFull(second, got); err != nil || !bytes.Equal(got, payload) { + t.Fatalf("binary: %v %v", got, err) + } + provider.PinnedKeySHA256 = strings.Repeat("a", 44) + if ch, err := transport.OpenDuplex(ctx, "aaaaaaaa-aaaa-4aaa-baaa-aaaaaaaaaaaa", "synthetic-worker"); err == nil { + _ = ch.Close() + t.Fatal("untrusted host accepted") + } +} +func TestDuplexRejectsInvalidStartupAndCancelledDial(t *testing.T) { + transport := ssh.NewTransport(nil) + for _, command := range []string{"", strings.Repeat("x", 8193)} { + if _, err := transport.OpenDuplex(context.Background(), "target", command); err == nil { + t.Fatal("invalid command") + } + } + ctx, cancel := context.WithCancel(context.Background()) + cancel() + if _, err := transport.OpenDuplex(ctx, "target", "worker"); err == nil { + t.Fatal("cancelled startup") + } +} diff --git a/internal/guest/ssh/startup_progress.go b/internal/guest/ssh/startup_progress.go new file mode 100644 index 0000000..57d0c92 --- /dev/null +++ b/internal/guest/ssh/startup_progress.go @@ -0,0 +1,70 @@ +package ssh + +import "sync" + +// StartupProgressPrefix identifies observational markers, never worker authority. +const StartupProgressPrefix = "AMC_STARTUP_V1:" + +var startupStages = [...]string{"before_header_read", "before_source_read", "before_compile", "before_worker_run"} +var startupMarkers = [...]string{ + StartupProgressPrefix + "1:before_header_read", + StartupProgressPrefix + "2:before_source_read", + StartupProgressPrefix + "3:before_compile", + StartupProgressPrefix + "4:before_worker_run", +} + +// ValidStartupStage accepts only the closed remote observation vocabulary. +func ValidStartupStage(stage string) bool { + for _, known := range startupStages { + if stage == known { + return true + } + } + return false +} + +// startupProgress drains every byte without retaining remote text. Position is +// bounded by the next fixed marker; noise remains invalid until its newline. +type startupProgress struct { + mu sync.RWMutex + sequence, position int + invalid bool +} + +func (p *startupProgress) Write(data []byte) (int, error) { + p.mu.Lock() + defer p.mu.Unlock() + for _, b := range data { + switch { + case b == '\n': + if !p.invalid && p.sequence < len(startupMarkers) { + n := len(startupMarkers[p.sequence]) + if p.position == n || p.position == n+1 { + p.sequence++ + } + } + p.position, p.invalid = 0, false + case p.invalid: + // Drain the remainder of a rejected line without retaining it. + case p.sequence == len(startupMarkers): + p.invalid = true + default: + next := startupMarkers[p.sequence] + if p.position < len(next) && b == next[p.position] || p.position == len(next) && b == '\r' { + p.position++ + } else { + p.invalid = true + } + } + } + return len(data), nil +} + +func (p *startupProgress) Stage() string { + p.mu.RLock() + defer p.mu.RUnlock() + if p.sequence == 0 { + return "" + } + return startupStages[p.sequence-1] +} diff --git a/internal/guest/ssh/startup_progress_test.go b/internal/guest/ssh/startup_progress_test.go new file mode 100644 index 0000000..7f6b8cf --- /dev/null +++ b/internal/guest/ssh/startup_progress_test.go @@ -0,0 +1,73 @@ +package ssh + +import ( + "strings" + "sync" + "testing" +) + +func TestStartupProgressFragmentedMarkersIgnoreNoiseAndSpoofs(t *testing.T) { + var p startupProgress + input := "private error/path\nspoof AMC_STARTUP_V1:1:before_header_read\nAMC_STARTUP_V1:4:before_worker_run\nAMC_STARTUP_V1:1:before_header_read extra\nAMC_STARTUP_V1:1:before_header_read\r\nAMC_STARTUP_V1:2:before_source_read\n" + for _, b := range []byte(input) { + if n, err := p.Write([]byte{b}); n != 1 || err != nil { + t.Fatal(n, err) + } + } + if got := p.Stage(); got != "before_source_read" { + t.Fatal(got) + } + _, _ = p.Write([]byte("AMC_STARTUP_V1:1:before_header_read\nAMC_STARTUP_V1:4:before_worker_run\nAMC_STARTUP_V1:3:private\n")) + if got := p.Stage(); got != "before_source_read" { + t.Fatal("nonmonotonic/unknown accepted", got) + } + _, _ = p.Write([]byte("AMC_STARTUP_V1:3:before_compile\nAMC_STARTUP_V1:4:before_worker_run\n")) + if got := p.Stage(); got != "before_worker_run" { + t.Fatal(got) + } +} + +func TestStartupProgressOversizedAndUnterminatedLinesDoNotAdvance(t *testing.T) { + var p startupProgress + _, _ = p.Write([]byte("AMC_STARTUP_V1:1:before_header_read" + strings.Repeat("x", 65536) + "\n")) + if p.Stage() != "" { + t.Fatal("oversized line accepted") + } + _, _ = p.Write([]byte("AMC_STARTUP_V1:1:before_header_read")) + if p.Stage() != "" { + t.Fatal("unterminated marker accepted") + } + _, _ = p.Write([]byte("\n")) + if p.Stage() != "before_header_read" { + t.Fatal(p.Stage()) + } +} + +func TestStartupProgressConcurrentObservation(t *testing.T) { + var p startupProgress + var wg sync.WaitGroup + wg.Go(func() { + for _, b := range []byte("AMC_STARTUP_V1:1:before_header_read\nAMC_STARTUP_V1:2:before_source_read\nAMC_STARTUP_V1:3:before_compile\nAMC_STARTUP_V1:4:before_worker_run\n") { + _, _ = p.Write([]byte{b}) + } + }) + for range 8 { + wg.Go(func() { + for range 1000 { + if stage := p.Stage(); stage != "" && !ValidStartupStage(stage) { + t.Error(stage) + } + } + }) + } + wg.Wait() + if p.Stage() != "before_worker_run" { + t.Fatal(p.Stage()) + } + if got := (&duplex{progress: &p}).StartupStage(); got != p.Stage() { + t.Fatal(got) + } + if (&duplex{}).StartupStage() != "" { + t.Fatal("missing observer invented a stage") + } +} diff --git a/internal/guest/stream/bootstrap.go b/internal/guest/stream/bootstrap.go new file mode 100644 index 0000000..05a5bb0 --- /dev/null +++ b/internal/guest/stream/bootstrap.go @@ -0,0 +1,63 @@ +package stream + +import ( + "bytes" + _ "embed" + "encoding/binary" + "io" + "strings" +) + +// The small fixed command reads source as bytes before any operation frames. +// Payloads never enter PowerShell command-line encoding or a terminal buffer. +var StartupCommand = startupCommand() + +//go:embed startup_pipe.ps1 +var startupPipe string + +func startupCommand() string { + var lines []string + for line := range strings.SplitSeq(startupPipe, "\n") { + line = strings.TrimSpace(line) + if line != "" && !strings.HasPrefix(line, "#") { + lines = append(lines, line) + } + } + script := strings.Join(lines, " ") + command := `powershell.exe -NoLogo -NoProfile -NonInteractive -Command "` + script + `"` + // This fixed source must fit the Windows command envelope without escaping. + if strings.ContainsAny(script, "\"\r") || len(command) > 8191 { + panic("invalid fixed startup command") + } + return command +} + +//go:embed worker.cs +var workerSource []byte + +//go:embed native.cs +var nativeSource []byte + +//go:embed security.cs +var securitySource []byte + +//go:embed lifetime.cs +var lifetimeSource []byte + +func writeSource(w io.Writer) error { + source := sourceBytes() + if len(source) > MaxControl { + return ErrProtocol + } + var h [4]byte + // #nosec G115 -- length is checked against the 256 KiB control bound above. + binary.BigEndian.PutUint32(h[:], uint32(len(source))) + if err := writeAll(w, h[:]); err != nil { + return err + } + return writeAll(w, source) +} + +func sourceBytes() []byte { + return bytes.Join([][]byte{workerSource, nativeSource, securitySource, lifetimeSource}, nil) +} diff --git a/internal/guest/stream/bootstrap_binary_test.go b/internal/guest/stream/bootstrap_binary_test.go new file mode 100644 index 0000000..d256eb2 --- /dev/null +++ b/internal/guest/stream/bootstrap_binary_test.go @@ -0,0 +1,240 @@ +package stream + +import ( + "bufio" + "bytes" + "context" + "encoding/binary" + "errors" + "fmt" + "io" + "os/exec" + "strings" + "testing" + "time" +) + +// Execute the production byte reader, stopping before C# compilation or native +// worker execution. Linux PowerShell proves framing only, not Windows delivery. +func TestStartupBinaryReadWithoutEOF(t *testing.T) { + path, err := exec.LookPath("pwsh") + if err != nil { + t.Skip("Linux PowerShell unavailable") + } + prefix := startupReadPrefix(t) + script := prefix + `$sha=[Security.Cryptography.SHA256]::Create();$digest=[BitConverter]::ToString($sha.ComputeHash($b)).Replace('-','').ToLowerInvariant();$sha.Dispose();[Console]::Out.WriteLine(('SOURCE:{0}:{1}' -f $b.Length,$digest));[Console]::Out.Flush()` + for _, tc := range []struct { + name string + data []byte + chunk int + }{ + {"embedded_source_32768_boundary", sourceBytes(), 32768}, + {"binary_arbitrary_boundary", bytes.Repeat([]byte{0, 255, 13, 10, 128, 1}, 101), 7}, + {"maximum_length", bytes.Repeat([]byte{0, 255}, MaxControl/2), 32768}, + } { + t.Run(tc.name, func(t *testing.T) { + checkStartupBinaryReceipt(t, path, script, tc.data, tc.chunk) + }) + } +} + +func checkStartupBinaryReceipt(t *testing.T, path, script string, data []byte, chunk int) { + t.Helper() + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + // #nosec G204 -- fixed production reader plus synthetic receipt; no guest input becomes executable code. + cmd := exec.CommandContext(ctx, path, "-NoLogo", "-NoProfile", "-NonInteractive", "-Command", script) + stdin, err := cmd.StdinPipe() + if err != nil { + t.Fatal(err) + } + stdout, err := cmd.StdoutPipe() + if err != nil { + t.Fatal(err) + } + var stderr bootstrapOutput + cmd.Stderr = &stderr + if err := cmd.Start(); err != nil { + t.Fatal(err) + } + waited := false + defer func() { + cancel() + _ = stdin.Close() + if !waited { + _ = cmd.Wait() + } + }() + output := make(chan string, 1) + go func() { + line, _ := bufio.NewReader(io.LimitReader(stdout, 256)).ReadString('\n') + output <- strings.TrimSpace(line) + }() + header := writeBootstrapFrame(t, stdin, data, chunk) + // Keep the pipe open until receipt: ReadToEnd would block here. + select { + case got := <-output: + want := fmt.Sprintf("SOURCE:%d:%s", len(data), hash(data)) + if got != want { + t.Fatalf("receipt = %q, want %q", got, want) + } + t.Logf("header=%x receipt=%s (stdin open)", header, got) + case <-ctx.Done(): + t.Fatal("source receipt did not arrive while stdin remained open") + } + _ = stdin.Close() + err = cmd.Wait() + waited = true + if err != nil { + t.Fatalf("PowerShell: %v: %s", err, &stderr) + } + if !strings.Contains(stderr.String(), "AMC_STARTUP_V1:3:before_compile") { + t.Fatal("source read did not reach compile boundary") + } +} + +func writeBootstrapFrame(t *testing.T, stdin io.Writer, data []byte, chunk int) [4]byte { + t.Helper() + if len(data) > MaxControl || chunk <= 0 { + t.Fatal("invalid bootstrap fixture bounds") + } + var header [4]byte + // #nosec G115 -- fixture length is bounded by MaxControl above. + binary.BigEndian.PutUint32(header[:], uint32(len(data))) + for _, part := range [][]byte{header[:1], header[1:]} { + if err := writeAll(stdin, part); err != nil { + t.Fatal(err) + } + } + for offset := 0; offset < len(data); offset += chunk { + end := min(offset+chunk, len(data)) + if err := writeAll(stdin, data[offset:end]); err != nil { + t.Fatal(err) + } + } + return header +} + +func startupReadPrefix(t *testing.T) string { + t.Helper() + _, command, ok := strings.Cut(StartupCommand, `-Command "`) + if !ok || !strings.HasSuffix(command, `"`) { + t.Fatal("unexpected startup command envelope") + } + prefix, _, ok := strings.Cut(strings.TrimSuffix(command, `"`), "Add-Type -TypeDefinition") + if !ok || len(prefix) > 8191 { + t.Fatal("missing or oversized startup read prefix") + } + // Linux runs the exact framing code with a portable stream opener. + // Reflection.Emit and Windows handle mode acceptance require native canaries. + _, prefix, ok = strings.Cut(prefix, "$Native=$type.CreateType();") + if !ok { + t.Fatal("missing fixed native declarations") + } + definitions, _, ok := strings.Cut(prefix, "$i=Open-AMCStandardStream -10 ([IO.FileAccess]::Read);") + if !ok { + t.Fatal("missing standalone source reader") + } + return definitions + `function Open-AMCStandardStream {return [Console]::OpenStandardInput()};$i=Open-AMCStandardStream -10 ([IO.FileAccess]::Read);$b=Read-AMCSource $i;` +} + +func TestStartupBinaryReadRejectsIncompleteAndOversizedFrames(t *testing.T) { + path, err := exec.LookPath("pwsh") + if err != nil { + t.Skip("Linux PowerShell unavailable") + } + var oversized [4]byte + binary.BigEndian.PutUint32(oversized[:], MaxControl+1) + for _, tc := range []struct { + name string + input []byte + sourceStage bool + }{ + {"truncated_header", []byte{0, 0, 0}, false}, + {"truncated_source", []byte{0, 0, 0, 3, 1, 2}, true}, + {"oversized_length", oversized[:], false}, + } { + t.Run(tc.name, func(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + // #nosec G204 -- fixed production reader plus synthetic receipt; no guest input becomes executable code. + cmd := exec.CommandContext(ctx, path, "-NoLogo", "-NoProfile", "-NonInteractive", "-Command", startupReadPrefix(t)+`[Console]::Out.WriteLine('UNEXPECTED_SOURCE_ACCEPTED')`) + cmd.Stdin = bytes.NewReader(tc.input) + var capture bootstrapOutput + cmd.Stdout, cmd.Stderr = &capture, &capture + err := cmd.Run() + output := capture.Bytes() + var exit *exec.ExitError + if !errors.As(err, &exit) || exit.ExitCode() != 1 || ctx.Err() != nil { + t.Fatalf("expected prompt exit 1, got %v: %s", err, output) + } + if strings.Contains(string(output), "before_compile") || strings.Contains(string(output), "UNEXPECTED_SOURCE_ACCEPTED") { + t.Fatalf("accepted invalid frame: %s", output) + } + if strings.Contains(string(output), "before_source_read") != tc.sourceStage { + t.Fatalf("unexpected source stage: %s", output) + } + t.Logf("rejected frame with exit 1: %s", strings.TrimSpace(string(output))) + }) + } +} + +type bootstrapWriteStep struct { + n int + err error +} + +// The startup probe has only a fixed receipt and three fixed progress markers. +// A changed command cannot turn diagnostics into an unbounded test allocation. +type bootstrapOutput struct{ bytes.Buffer } + +func (w *bootstrapOutput) Write(p []byte) (int, error) { + if len(p) > 1024-w.Len() { + return 0, io.ErrShortWrite + } + return w.Buffer.Write(p) +} + +type bootstrapStepWriter struct { + steps []bootstrapWriteStep + output bytes.Buffer + calls int +} + +func (w *bootstrapStepWriter) Write(p []byte) (int, error) { + if w.calls == len(w.steps) { + return 0, io.ErrUnexpectedEOF + } + step := w.steps[w.calls] + w.calls++ + if step.n >= 0 && step.n <= len(p) { + _, _ = w.output.Write(p[:step.n]) + } + return step.n, step.err +} + +func TestBootstrapWriteAllFailureAndPartialProgress(t *testing.T) { + failure := errors.New("synthetic write failure") + for _, tc := range []struct { + name string + steps []bootstrapWriteStep + want []byte + err error + }{ + {"partial_then_complete", []bootstrapWriteStep{{1, nil}, {2, nil}}, []byte{1, 2, 3}, nil}, + {"partial_then_error", []bootstrapWriteStep{{1, nil}, {1, failure}}, []byte{1, 2}, failure}, + {"zero_progress", []bootstrapWriteStep{{0, nil}}, nil, io.ErrShortWrite}, + {"negative_count", []bootstrapWriteStep{{-1, nil}}, nil, ErrProtocol}, + {"oversized_count", []bootstrapWriteStep{{4, nil}}, nil, ErrProtocol}, + } { + t.Run(tc.name, func(t *testing.T) { + w := &bootstrapStepWriter{steps: tc.steps} + if err := writeAll(w, []byte{1, 2, 3}); !errors.Is(err, tc.err) { + t.Fatalf("write error = %v, want %v", err, tc.err) + } + if !bytes.Equal(w.output.Bytes(), tc.want) || w.calls != len(tc.steps) { + t.Fatalf("output=%x calls=%d", w.output.Bytes(), w.calls) + } + }) + } +} diff --git a/internal/guest/stream/bootstrap_pipe_test.go b/internal/guest/stream/bootstrap_pipe_test.go new file mode 100644 index 0000000..6180e08 --- /dev/null +++ b/internal/guest/stream/bootstrap_pipe_test.go @@ -0,0 +1,104 @@ +package stream + +import ( + "context" + "os/exec" + "strings" + "testing" + "time" +) + +// Execute the production mode decisions and opener with synthetic native query +// results. These checks deliberately do not claim Windows handle acceptance. +func TestStartupPipeModeDecisionsAndRefusals(t *testing.T) { + definitions := startupPipeDefinitions(t, "function Read-AMCSource(") + script := `$ErrorActionPreference='Stop';Add-Type -TypeDefinition @' +using System; +using System.Runtime.InteropServices; +public static class SyntheticBootstrapNative { + public static long Handle=123; + public static uint FileType=3; + public static int Status=0,IOStatus=0,Mode=0,Queries=0; + public static long Information=4; + public static IntPtr GetStdHandle(int id){return new IntPtr(Handle);} + public static uint GetFileType(IntPtr handle){return FileType;} + public static int NtQueryInformationFile(IntPtr handle,IntPtr iosb,IntPtr mode,uint length,int type){ + if(length!=4||type!=16)throw new Exception("wrong query contract"); + for(int i=0;i<2*IntPtr.Size;i++)if(Marshal.ReadByte(iosb,i)!=0)throw new Exception("uninitialized status"); + if(Marshal.ReadInt32(mode)!=0)throw new Exception("uninitialized mode"); + Queries++;Marshal.WriteInt32(iosb,IOStatus);Marshal.WriteIntPtr(iosb,IntPtr.Size,new IntPtr(Information));Marshal.WriteInt32(mode,Mode);return Status; + } +} + +'@;$Native=[SyntheticBootstrapNative];` + definitions + ` +foreach($mode in @(0,1,2,4,8,64)){ + if(!(Get-AMCAsyncMode 0 0 4 $mode)){throw 'async mode rejected'}; +}; +foreach($mode in @(0x10,0x20,0x11,0x22)){ + if(Get-AMCAsyncMode 0 0 4 $mode){throw 'sync mode accepted as async'}; +}; +$cases=@( + @(0,3,0,0,4,0,'211',0),@(-1,3,0,0,4,0,'211',0), + @(123,1,0,0,4,0,'211',0),@(123,3,-1,0,4,0,'211',1), + @(123,3,1,0,4,0,'211',1),@(123,3,0x103,0,4,0,'214',1), + @(123,3,0,-1,4,0,'211',1),@(123,3,0,0x103,4,0,'211',1), + @(123,3,0,0,3,0,'212',1),@(123,3,0,0,5,0,'212',1), + @(123,3,0,0,4,0x30,'211',1) +); +foreach($case in $cases){ + [SyntheticBootstrapNative]::Handle=$case[0];[SyntheticBootstrapNative]::FileType=$case[1]; + [SyntheticBootstrapNative]::Status=$case[2];[SyntheticBootstrapNative]::IOStatus=$case[3]; + [SyntheticBootstrapNative]::Information=$case[4];[SyntheticBootstrapNative]::Mode=$case[5]; + [SyntheticBootstrapNative]::Queries=0; + try{$stream=Open-AMCStandardStream -10 ([IO.FileAccess]::Read);$stream.Dispose();throw 'invalid query returned a stream'} + catch{if($_.Exception.Message-ne$case[6]){throw}}; + if([SyntheticBootstrapNative]::Queries-ne$case[7]){throw 'invalid handle queried or missing native query'}; +}; +[Console]::Out.WriteLine('mode-decisions:10 refusals:11');` + runStartupPowerShell(t, script, "mode-decisions:10 refusals:11") +} + +func TestStartupSourceReaderRetainsQueuedFrames(t *testing.T) { + definitions := startupPipeDefinitions(t, "$i=Open-AMCStandardStream -10 ([IO.FileAccess]::Read);") + script := `$ErrorActionPreference='Stop';` + definitions + ` +$i=[IO.MemoryStream]::new([byte[]]@(0,0,0,3,255,0,128,0,0,0,1,65,0,0,0,1,66)); +try{ + $b=Read-AMCSource $i; + if($b-isnot[byte[]]-or$b.Length-ne3-or$b[0]-ne255-or$b[1]-ne0-or$b[2]-ne128){throw 'source bytes changed'}; + if(!$i.CanRead-or$i.Position-ne7){throw 'source reader closed or consumed queued frames'}; + $remaining=New-Object byte[] 10; + if($i.Read($remaining,0,10)-ne10-or[BitConverter]::ToString($remaining)-ne'00-00-00-01-41-00-00-00-01-42'){throw 'queued frames lost or replayed'}; + [Console]::Out.WriteLine('retained-stream queued-frames:2'); +}finally{$i.Dispose()};` + runStartupPowerShell(t, script, "retained-stream queued-frames:2") +} + +func startupPipeDefinitions(t *testing.T, end string) string { + t.Helper() + _, definitions, ok := strings.Cut(StartupCommand, "$Native=$type.CreateType();") + if !ok { + t.Fatal("missing native declaration boundary") + } + definitions, _, ok = strings.Cut(definitions, end) + if !ok { + t.Fatal("missing startup fixture boundary", end) + } + return definitions +} + +func runStartupPowerShell(t *testing.T, script, expected string) { + t.Helper() + path, err := exec.LookPath("pwsh") + if err != nil { + t.Skip("Linux PowerShell unavailable") + } + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + // #nosec G204 -- fixed production bootstrap definitions and synthetic fixtures, never guest commands. + command := exec.CommandContext(ctx, path, "-NoProfile", "-NonInteractive", "-Command", script) + output, err := command.CombinedOutput() + if err != nil || !strings.Contains(string(output), expected) { + t.Fatalf("bootstrap contract %q: %v %s", expected, err, output) + } + t.Log(strings.TrimSpace(string(output))) +} diff --git a/internal/guest/stream/bootstrap_progress_test.go b/internal/guest/stream/bootstrap_progress_test.go new file mode 100644 index 0000000..45ac206 --- /dev/null +++ b/internal/guest/stream/bootstrap_progress_test.go @@ -0,0 +1,29 @@ +package stream + +import ( + "fmt" + "strings" + "testing" + + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" +) + +func TestStartupMarkersUseStderrBeforeEachClosedPhase(t *testing.T) { + previous := -1 + for i, row := range []struct{ stage, anchor string }{ + {"before_header_read", "$h=New-Object byte[] 4"}, + {"before_source_read", "$b=New-Object byte[] $l"}, + {"before_compile", "Add-Type -TypeDefinition"}, + {"before_worker_run", "[AMCStream.Worker]::Run"}, + } { + marker := fmt.Sprintf("[Console]::Error.WriteLine('%s%d:%s');", guestssh.StartupProgressPrefix, i+1, row.stage) + at := strings.Index(StartupCommand, marker) + if !guestssh.ValidStartupStage(row.stage) || strings.Count(StartupCommand, marker) != 1 || at <= previous || at >= strings.Index(StartupCommand, row.anchor) { + t.Fatal("missing/misordered marker", row.stage) + } + previous = at + } + if strings.Count(StartupCommand, "[Console]::Error.WriteLine(") != 4 || !strings.Contains(StartupCommand, "$o=Open-AMCStandardStream -11 ([IO.FileAccess]::Write)") { + t.Fatal("startup stream contract changed") + } +} diff --git a/internal/guest/stream/client.go b/internal/guest/stream/client.go new file mode 100644 index 0000000..abc4ed0 --- /dev/null +++ b/internal/guest/stream/client.go @@ -0,0 +1,163 @@ +package stream + +import ( + "context" + "encoding/hex" + "errors" + "io" + "strings" + "sync" + + "github.com/Horcag/agent-machine-control/internal/domain" + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" +) + +type poolKey struct { + target domain.MachineRef + actor string + generation string +} +type worker struct { + lane chan struct{} + channel guestssh.Duplex + identity WorkerIdentity +} +type Client struct { + opener DuplexOpener + mu sync.Mutex + workers map[poolKey]*worker + closed bool +} + +func NewClient(opener DuplexOpener) *Client { + return &Client{opener: opener, workers: make(map[poolKey]*worker)} +} +func (c *Client) Close() error { + c.mu.Lock() + defer c.mu.Unlock() + c.closed = true + var errs []error + for k, w := range c.workers { + if w.channel != nil { + errs = append(errs, w.channel.Close()) + } + delete(c.workers, k) + } + return errors.Join(errs...) +} +func validDigest(s string) bool { + p, e := hex.DecodeString(s) + return e == nil && len(p) == 32 && s == strings.ToLower(s) +} + +// Execute sends exactly once. A broken or cancelled worker is evicted; this +// operation is never replayed, even when the remote effect is uncertain. +func (c *Client) Execute(ctx context.Context, target domain.MachineRef, actor string, generation string, req Request, input io.Reader, output io.Writer) (Result, error) { + if err := validate(req); err != nil { + return Result{}, err + } + if actor == "" || target == "" || c.opener == nil { + return Result{}, ErrRequest + } + ctx, cancel := context.WithDeadline(ctx, req.DeadlineUTC) + defer cancel() + input, output, err := bindRequestIO(ctx, input, output) + if err != nil { + return Result{}, err + } + key := poolKey{target, actor, generation} + w, release, err := c.acquire(ctx, key, false) + if err != nil { + return Result{}, err + } + defer release() + + if w.channel == nil { + return Result{}, ErrRequest + } + stop := context.AfterFunc(ctx, func() { _ = w.channel.Close() }) + defer stop() + result, err := exchange(w.channel, req, input, output, w.identity) + if err != nil { + err = observeLifetimeExit(ctx, w.channel, err) + c.evict(key, w) + if ctx.Err() != nil { + return result, ctx.Err() + } + } + return result, err +} +func (c *Client) evict(key poolKey, w *worker) { + c.mu.Lock() + defer c.mu.Unlock() + if w.channel != nil { + _ = w.channel.Close() + } + if c.workers[key] == w { + delete(c.workers, key) + } +} +func exchange(ch io.ReadWriter, req Request, input io.Reader, output io.Writer, identity WorkerIdentity) (Result, error) { + if err := writeControl(ch, req); err != nil { + return Result{}, err + } + if req.Operation == "put" || req.Operation == "exec" { + if err := sendPayload(ch, req, input); err != nil { + return Result{}, err + } + } + return receiveResult(ch, req, output, identity) +} +func receiveResult(ch io.Reader, req Request, output io.Writer, identity WorkerIdentity) (Result, error) { + var header Result + if err := readControl(ch, &header); err != nil { + return Result{}, err + } + if header.ControlFailure != nil { + return receiveControlFailure(ch, header, identity) + } + return receiveOperationResult(ch, req, output, identity, header) +} + +func receiveOperationResult(ch io.Reader, req Request, output io.Writer, identity WorkerIdentity, header Result) (Result, error) { + if header.ID != req.ID || header.Size < 0 || header.Size > req.MaxOutput || header.Worker != identity { + return Result{}, ErrProtocol + } + if invalidFailureHeader(header) { + return Result{}, ErrProtocol + } + if output == nil { + output = io.Discard + } + digest, err := copyPayload(output, ch, header.Size) + if err != nil { + return header, err + } + var result Result + if err = readControl(ch, &result); err != nil { + return header, err + } + if result.ID != req.ID || result.Worker != identity || result.Size != header.Size || !validDigest(result.SHA256) || digest != result.SHA256 { + return result, ErrIntegrity + } + return result, verifyWorkerFailure(header, result) +} + +func sendPayload(ch io.Writer, req Request, input io.Reader) error { + if input == nil { + input = strings.NewReader("") + } + digest, err := copyPayload(ch, input, req.Size) + if err != nil { + return err + } + var extra [1]byte + n, readErr := input.Read(extra[:]) + if n != 0 || readErr != io.EOF || digest != req.SHA256 { + return ErrIntegrity + } + if err = writeAll(ch, []byte{1}); err != nil { + return err + } + return nil +} diff --git a/internal/guest/stream/client_test.go b/internal/guest/stream/client_test.go new file mode 100644 index 0000000..add22d9 --- /dev/null +++ b/internal/guest/stream/client_test.go @@ -0,0 +1,228 @@ +package stream + +import ( + "context" + "errors" + "io" + "net" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" +) + +type fixtureChannel struct{ net.Conn } + +func (f fixtureChannel) Wait() error { return nil } + +type fixtureOpener struct { + count atomic.Int64 + requests atomic.Int64 + handler func(net.Conn, Request) + wg sync.WaitGroup +} + +func (f *fixtureOpener) OpenDuplex(_ context.Context, _ domain.MachineRef, _ string) (guestssh.Duplex, error) { + f.count.Add(1) + a, b := net.Pipe() + f.wg.Go(func() { + defer b.Close() + var h [4]byte + if _, err := io.ReadFull(b, h[:]); err != nil { + return + } + n := int(h[0])<<24 | int(h[1])<<16 | int(h[2])<<8 | int(h[3]) + if _, err := io.CopyN(io.Discard, b, int64(n)); err != nil { + return + } + _ = writeControl(b, WorkerIdentity{PID: 12, BirthTicks: 123, Image: "synthetic-worker", SID: "synthetic-sid"}) + f.serve(b) + }) + return fixtureChannel{a}, nil +} +func fixtureClient(t *testing.T, f *fixtureOpener) *Client { + t.Helper() + c := NewClient(f) + t.Cleanup(func() { _ = c.Close(); f.wg.Wait() }) + return c +} +func request() Request { + return Request{ID: "synthetic-request", Operation: "put", DeadlineUTC: time.Now().Add(10 * time.Second), SHA256: hash(nil), ExpectedRoot: &RootIdentity{Path: "synthetic-root", FileID: "00112233445566778899aabbccddeeff", Owner: "synthetic-sid", SDDLHash: hash(nil)}} +} +func TestPrepareAndPoolIdentitySeparation(t *testing.T) { + f := &fixtureOpener{} + c := fixtureClient(t, f) + ctx := context.Background() + if _, err := c.Execute(ctx, "target", "actor", "generation", request(), nil, nil); !errors.Is(err, ErrRequest) { + t.Fatal("unadmitted worker", err) + } + for _, key := range []poolKey{{"target", "actor", "g1"}, {"target", "actor2", "g1"}, {"target2", "actor", "g1"}, {"target", "actor", "g2"}} { + for range 2 { + identity, err := c.Prepare(ctx, key.target, key.actor, key.generation) + if err != nil || identity.BirthTicks != 123 { + t.Fatal(identity, err) + } + if _, err = c.Execute(ctx, key.target, key.actor, key.generation, request(), nil, nil); err != nil { + t.Fatal(err) + } + } + } + if f.count.Load() != 4 || f.requests.Load() != 8 { + t.Fatalf("pool: dials=%d requests=%d", f.count.Load(), f.requests.Load()) + } +} +func TestExecuteConcurrentSerialization(t *testing.T) { + f := &fixtureOpener{} + c := fixtureClient(t, f) + _, err := c.Prepare(context.Background(), "target", "actor", "g") + if err != nil { + t.Fatal(err) + } + var wg sync.WaitGroup + errs := make(chan error, 20) + for range 20 { + wg.Go(func() { + _, err := c.Execute(context.Background(), "target", "actor", "g", request(), nil, nil) + errs <- err + }) + } + wg.Wait() + close(errs) + for err := range errs { + if err != nil { + t.Fatal(err) + } + } + if f.count.Load() != 1 || f.requests.Load() != 20 { + t.Fatal("worker was restarted") + } +} +func TestFailureNeverReplays(t *testing.T) { + f := &fixtureOpener{handler: func(ch net.Conn, _ Request) { _ = ch.Close() }} + c := fixtureClient(t, f) + _, _ = c.Prepare(context.Background(), "target", "actor", "g") + if _, err := c.Execute(context.Background(), "target", "actor", "g", request(), nil, nil); err == nil { + t.Fatal("missing failure") + } + if f.count.Load() != 1 || f.requests.Load() != 1 { + t.Fatal("request replay") + } + if _, err := c.Execute(context.Background(), "target", "actor", "g", request(), nil, nil); !errors.Is(err, ErrRequest) { + t.Fatal(err) + } + if f.count.Load() != 1 { + t.Fatal("implicit restart") + } +} +func TestCancellationClosesOnlyOwnedWorker(t *testing.T) { + received := make(chan struct{}) + f := &fixtureOpener{handler: func(ch net.Conn, _ Request) { close(received); _, _ = io.Copy(io.Discard, ch) }} + c := fixtureClient(t, f) + _, _ = c.Prepare(context.Background(), "target", "actor", "g") + _, _ = c.Prepare(context.Background(), "other", "actor", "g") + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan error, 1) + go func() { _, err := c.Execute(ctx, "target", "actor", "g", request(), nil, nil); done <- err }() + <-received + cancel() + select { + case err := <-done: + if !errors.Is(err, context.Canceled) { + t.Fatal(err) + } + case <-time.After(time.Second): + t.Fatal("cancellation did not close channel") + } + if _, err := c.Prepare(context.Background(), "other", "actor", "g"); err != nil || f.count.Load() != 2 { + t.Fatal("sibling evicted", err) + } +} +func TestUploadRejectsSizeDigestAndExtraInput(t *testing.T) { + for _, input := range []string{"short", "longer-data", "wrong"} { + t.Run(input, func(t *testing.T) { + f := &fixtureOpener{} + c := fixtureClient(t, f) + _, _ = c.Prepare(context.Background(), "target", "actor", "g") + req := request() + req.Size = 5 + req.SHA256 = hash([]byte("right")) + _, err := c.Execute(context.Background(), "target", "actor", "g", req, BytesInput([]byte(input)), nil) + if !errors.Is(err, ErrIntegrity) { + t.Fatal(err) + } + }) + } +} + +func (f *fixtureOpener) serve(b net.Conn) { + for { + var req Request + if readControl(b, &req) != nil { + return + } + f.requests.Add(1) + if f.handler != nil { + f.handler(b, req) + return + } + if _, err := io.CopyN(io.Discard, b, req.Size); err != nil { + return + } + if req.Operation == "put" || req.Operation == "exec" { + var flag [1]byte + if _, err := io.ReadFull(b, flag[:]); err != nil || flag[0] != 1 { + return + } + } + result := Result{ID: req.ID, Status: "ok", SHA256: hash(nil), Worker: WorkerIdentity{PID: 12, BirthTicks: 123, Image: "synthetic-worker", SID: "synthetic-sid"}} + if writeControl(b, result) != nil { + return + } + if writeControl(b, result) != nil { + return + } + } +} + +func TestRetireRequiresFreshPreparation(t *testing.T) { + f := &fixtureOpener{} + c := fixtureClient(t, f) + _, err := c.Prepare(context.Background(), "target", "actor", "g") + if err != nil { + t.Fatal(err) + } + if err = c.Retire("target", "actor", "g"); err != nil { + t.Fatal(err) + } + if _, err = c.Execute(context.Background(), "target", "actor", "g", request(), nil, nil); !errors.Is(err, ErrRequest) { + t.Fatal("retired admission reused", err) + } + if _, err = c.Prepare(context.Background(), "target", "actor", "g"); err != nil || f.count.Load() != 2 { + t.Fatal("fresh worker absent", err) + } +} + +func BenchmarkWorkerReuse(b *testing.B) { + f := &fixtureOpener{} + c := NewClient(f) + defer func() { _ = c.Close(); f.wg.Wait() }() + _, err := c.Prepare(context.Background(), "target", "actor", "g") + if err != nil { + b.Fatal(err) + } + b.ReportAllocs() + b.ResetTimer() + for range b.N { + if _, err = c.Execute(context.Background(), "target", "actor", "g", request(), nil, nil); err != nil { + b.Fatal(err) + } + } + b.StopTimer() + if f.count.Load() != 1 { + b.Fatal("handshake repeated") + } + b.ReportMetric(float64(f.count.Load())/float64(b.N), "handshake/op") +} diff --git a/internal/guest/stream/compile_test.go b/internal/guest/stream/compile_test.go new file mode 100644 index 0000000..9a3231c --- /dev/null +++ b/internal/guest/stream/compile_test.go @@ -0,0 +1,203 @@ +package stream + +import ( + "bytes" + "context" + "os/exec" + "strings" + "testing" + "time" +) + +// Linux compilation checks C# and PowerShell API usage without executing a +// native methods. The Windows SID and .NET Framework JSON API are shimmed only +// in this compiler fixture, never in the production source. +func TestWorkerSourceCompilesAndBootstrapParses(t *testing.T) { + path, err := exec.LookPath("pwsh") + if err != nil { + t.Skip("Linux PowerShell compiler unavailable") + } + source := sourceBytes() + source = bytes.ReplaceAll(source, []byte("WindowsIdentity.GetCurrent().User.Value"), []byte(`"synthetic-sid"`)) + source = append(source, []byte(` +namespace System.Web.Script.Serialization { + public sealed class JavaScriptSerializer { + public int MaxJsonLength {get;set;} + public string Serialize(object value){return "{}";} + public T Deserialize(string value){return (T)(object)new System.Collections.Generic.Dictionary{{"id",""}};} + } +} +namespace AMCStream { + public sealed class ThrowingControlStream : System.IO.MemoryStream { + public override int Read(byte[] bytes,int offset,int count){throw new System.IO.IOException("private failure text");} + } + public static class IdentityRegression { + static void ControlRefusal(System.IO.Stream input,string stage,System.Func> decode){ + try{Worker.ReadControl(input,decode);throw new System.Exception("control failure accepted");} + catch(Worker.ControlFailure failure){if(failure.Stage!=stage)throw new System.Exception("wrong control stage");} + } + + public static void Check(){ + System.Func> decoded=delegate(string text){return new System.Collections.Generic.Dictionary{{"id",text}};}; + ControlRefusal(new System.IO.MemoryStream(),"control_eof",decoded); + ControlRefusal(new ThrowingControlStream(),"control_read",decoded); + ControlRefusal(new System.IO.MemoryStream(new byte[]{0,0,0,0}),"control_header",decoded); + ControlRefusal(new System.IO.MemoryStream(new byte[]{0,4,0,1}),"control_header",decoded); + ControlRefusal(new System.IO.MemoryStream(new byte[]{0,0,0,2,1}),"control_eof",decoded); + ControlRefusal(new System.IO.MemoryStream(new byte[]{0,0,0,1,255}),"control_decode",decoded); + ControlRefusal(new System.IO.MemoryStream(new byte[]{0,0,0,1,65}),"control_decode",delegate(string text){throw new System.Exception("private JSON parser text");}); + if((string)Worker.ReadControl(new System.IO.MemoryStream(new byte[]{0,0,0,1,65}),decoded)["id"]!="A")throw new System.Exception("valid control changed"); + var failure=Failure.Metadata(new System.Exception(@"C:\private\secret SDDL token"),"observe"); + if(failure.Count!=2||(string)failure["stage"]!="observe"||(uint)failure["nativeError"]!=0)throw new System.Exception("exception text retained"); + failure=Failure.Metadata(new NativeFailure("security_enable",1300),"observe"); + if(failure.Count!=2||(string)failure["stage"]!="security_enable"||(uint)failure["nativeError"]!=1300)throw new System.Exception("native diagnostic lost"); + string[] stages={"runspace_open","request","observe","put","get","exec","remove_owned","security_token","security_lookup","security_enable","security_restore","ancestor_open","workspace_open","file_open","metadata_read","file_id_read","security_size","security_read","security_parse","file_commit","file_delete","control_eof","control_read","control_header","control_decode","file_owner_set"}; + for(int i=0;i create=delegate{creates++;return held;}; + System.Action close=delegate(System.IO.MemoryStream file){ownerCloses++;file.Dispose();}; + try{Native.CreateOwned("synthetic-user","synthetic-other",originalSid,create,delegate(System.IO.MemoryStream file,byte[] sid){ownerSets++;return 0u;},close);throw new System.Exception("foreign owner accepted");} + catch(System.IO.InvalidDataException){} + if(creates!=0||ownerSets!=0||ownerCloses!=0)throw new System.Exception("mismatched owner created file"); + foreach(bool throws in new[]{false,true}){ + held=new System.IO.MemoryStream();creates=ownerSets=ownerCloses=0; + try{Native.CreateOwned("synthetic-user","synthetic-user",originalSid,create,delegate(System.IO.MemoryStream file,byte[] sid){ownerSets++;if(!object.ReferenceEquals(file,held)||!object.ReferenceEquals(sid,originalSid)||file.Length!=0)throw new System.Exception("owner handle/input drift");if(throws)throw new System.IO.IOException("synthetic setter failure");return 5u;},close);throw new System.Exception("failed setter published file");} + catch(NativeFailure error){if(throws||error.Stage!="file_owner_set"||error.Code!=5)throw;} + catch(System.IO.IOException){if(!throws)throw;} + if(creates!=1||ownerSets!=1||ownerCloses!=1||held.CanWrite)throw new System.Exception("failed setter leaked original"); + } + held=new System.IO.MemoryStream();creates=ownerSets=ownerCloses=0; + var published=Native.CreateOwned("synthetic-user","synthetic-user",originalSid,create,delegate(System.IO.MemoryStream file,byte[] sid){ownerSets++;if(!object.ReferenceEquals(file,held)||file.Length!=0||!object.ReferenceEquals(sid,originalSid))throw new System.Exception("owner set after payload");return 0u;},close); + published.WriteByte(42);if(creates!=1||ownerSets!=1||ownerCloses!=0||published.Length!=1)throw new System.Exception("owned file not published after setter");published.Dispose(); + var a=new System.Collections.Generic.Dictionary();var b=new System.Collections.Generic.Dictionary(); + string[] keys={"path","volume","fileId","creationTime","owner","sddlHash","size","sha256","rootPath","rootVolume","rootFileId","rootCreationTime","rootOwner","rootSDDLHash"}; + foreach(var key in keys){a[key]="original";b[key]="original";} + if(!Native.Same(a,b))throw new System.Exception("original rejected"); + foreach(var key in keys){b[key]="changed";if(Native.Same(a,b))throw new System.Exception("changed identity accepted "+key);b[key]="original";} + b.Remove("owner");if(Native.Same(a,b))throw new System.Exception("incomplete identity accepted"); + + // Execute the real hello and control reader on supplied streams. An invalid + // first request stops before native operations; the adjacent frame survives. + using(var suppliedInput=new System.IO.MemoryStream(new byte[]{0,0,0,2,123,125,0,0,0,1,65})) + using(var suppliedOutput=new System.IO.MemoryStream()){ + var run=typeof(Worker).GetMethod("RunOwned",System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Static); + try{run.Invoke(null,new object[]{suppliedInput,suppliedOutput,null});throw new System.Exception("invalid request accepted");} + catch(System.Reflection.TargetInvocationException error){if(!(error.InnerException is System.IO.InvalidDataException))throw;} + if(!suppliedInput.CanRead||!suppliedOutput.CanWrite||suppliedInput.Position!=6)throw new System.Exception("supplied stream closed or replaced"); + byte[] hello=suppliedOutput.ToArray(); + if(hello.Length!=6||hello[0]!=0||hello[1]!=0||hello[2]!=0||hello[3]!=2||hello[4]!=123||hello[5]!=125)throw new System.Exception("hello bypassed supplied output"); + if((string)Worker.ReadControl(suppliedInput,decoded)["id"]!="A"||suppliedInput.Position!=suppliedInput.Length)throw new System.Exception("adjacent frame lost or replayed"); + } + // Real managed buffering must preserve adjacent binary frames and exact EOF. + using(var raw=new System.IO.MemoryStream(new byte[]{0,0,0,1,65,0,0,0,1,66,0,0,0,2,67})) + using(var buffered=new System.IO.BufferedStream(raw,65536)){ + if((string)Worker.ReadControl(buffered,decoded)["id"]!="A"||(string)Worker.ReadControl(buffered,decoded)["id"]!="B")throw new System.Exception("buffered framing changed"); + ControlRefusal(buffered,"control_eof",decoded); + if(raw.Position!=raw.Length)throw new System.Exception("partial frame replayed"); + } + if(Lifetime.CheckedPipeState(0,40,3)!=3)throw new System.Exception("connected pipe rejected"); + foreach(var item in new[]{new object[]{-1,40UL,211u},new object[]{0,39UL,212u},new object[]{0x103,40UL,214u}}){ + try{Lifetime.CheckedPipeState((int)item[0],(ulong)item[1],3);throw new System.Exception("invalid query accepted");} + catch(Lifetime.PipeFailure pipeFailure){if(pipeFailure.ExitCode!=(uint)item[2])throw new System.Exception("wrong query exit");} + } + int kills=0,closes=0; + var termination=new Lifetime.OwnTermination(delegate(uint code){if(code!=213u)throw new System.Exception("wrong kill marker");kills++;return false;},delegate{closes++;}); + termination.Stop(213);termination.Stop(211); + if(kills!=1||closes!=1)throw new System.Exception("failed kill did not close exactly once"); + kills=0;closes=0; + termination=new Lifetime.OwnTermination(delegate(uint code){kills++;throw new System.Exception("native invocation failed");},delegate{closes++;}); + termination.Stop(211);termination.Stop(212); + if(kills!=1||closes!=1)throw new System.Exception("throwing kill leaked owned job"); + foreach(bool nativeSuccess in new[]{true,false}){ + uint marker=0;int closures=0; + var owned=new Lifetime.OwnTermination(delegate(uint code){marker=code;return nativeSuccess;},delegate{closures++;}); + using(var stop=new System.Threading.ManualResetEvent(false)){ + Lifetime.ObserveOwned(stop,delegate(){throw new System.Exception("synthetic observer failure");},owned); + } + owned.Stop(219);if(marker!=215||closures!=1)throw new System.Exception("observer marker lost"); + } + uint first=0;int closed=0; + var primary=new Lifetime.OwnTermination(delegate(uint code){first=code;throw new System.Exception("synthetic terminate failure");},delegate{closed++;}); + var originalFailure=new System.Exception("synthetic worker failure"); + try{Lifetime.RunWorker(delegate(){throw originalFailure;},primary);throw new System.Exception("worker failure swallowed");} + catch(System.Exception error){if(!object.ReferenceEquals(error,originalFailure))throw;} + primary.Stop(219);if(first!=216||closed!=1)throw new System.Exception("using cleanup replaced first marker"); + var order=new System.Collections.Generic.List(); + var normal=new Lifetime.OwnTermination(delegate(uint code){order.Add("marker:"+code);return false;},delegate{order.Add("close");}); + Lifetime.RunWorker(delegate{order.Add("run");},normal); + Lifetime.FinishObserver(delegate{order.Add("stop");},delegate{order.Add("join");return true;},delegate{order.Add("release");},delegate{order.Add("timer");},normal); + if(string.Join(",",order.ToArray())!="run,stop,join,release,marker:219,close,timer")throw new System.Exception("normal stop order changed"); + order.Clear();normal=new Lifetime.OwnTermination(delegate(uint code){order.Add("marker:"+code);return false;},delegate{order.Add("close");}); + Lifetime.FinishObserver(delegate{order.Add("stop");},delegate{order.Add("join");return false;},delegate{order.Add("release");},delegate{order.Add("timer");},normal); + if(string.Join(",",order.ToArray())!="stop,join,marker:218,close,timer")throw new System.Exception("join timeout did not close before timer release"); + using(var fired=new System.Threading.ManualResetEvent(false)){ + first=0;closed=0; + var timed=new Lifetime.OwnTermination(delegate(uint code){first=code;return false;},delegate{System.Threading.Interlocked.Increment(ref closed);fired.Set();}); + using(var timer=Lifetime.ArmOwnedFailure(timed,System.DateTime.UtcNow.AddMilliseconds(30))){ + Lifetime.FinishObserver(delegate{},delegate{if(!fired.WaitOne(5000))throw new System.Exception("failure timer missed bound");return false;},delegate{},delegate{},timed); + } + if(first!=217||closed!=1)throw new System.Exception("join cleanup replaced prearmed timer marker"); + } + var now=new System.DateTime(2026,1,1,0,0,0,System.DateTimeKind.Utc); + if(Lifetime.FailureBudget(now.AddSeconds(30),now)!=System.TimeSpan.FromSeconds(1))throw new System.Exception("failure bound exceeded one second"); + if(Lifetime.FailureBudget(now.AddMilliseconds(20),now)!=System.TimeSpan.FromMilliseconds(20))throw new System.Exception("request deadline extended"); + if(Lifetime.FailureBudget(now.AddTicks(-1),now)!=System.TimeSpan.Zero)throw new System.Exception("expired request extended"); + // Output backpressure and terminal input cannot cancel the prearmed bound. + using(var released=new System.Threading.ManualResetEvent(false)){ + int closures=0; + using(var timer=Lifetime.ArmFailureBound(delegate(){System.Threading.Interlocked.Increment(ref closures);released.Set();},System.DateTime.UtcNow.AddMilliseconds(100))){ + if(!released.WaitOne(5000)||closures!=1)throw new System.Exception("blocked failure response survived its bound"); + } + } + using(var terminal=new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes("another business request"))){ + Lifetime.DrainTerminal(terminal); + if(terminal.Position!=terminal.Length)throw new System.Exception("terminal input was reused"); + } + using(var stopped=new System.Threading.ManualResetEvent(false)){ + int observations=0,disconnects=0; + // A disconnected pipe with unread payload must still terminate the worker. + Lifetime.Observe(stopped,delegate(){observations++;return 4u;},delegate(){disconnects++;}); + if(observations!=1||disconnects!=1)throw new System.Exception("EOF observer missed unread backlog"); + stopped.Set();Lifetime.Observe(stopped,delegate(){throw new System.Exception("observer survived stop");},delegate(){throw new System.Exception("stop closed job twice");}); + } + } + } +}`)...) + script := `$ErrorActionPreference='Stop';$source=[Console]::In.ReadToEnd();Add-Type -TypeDefinition $source -IgnoreWarnings -WarningAction SilentlyContinue;[AMCStream.IdentityRegression]::Check();Write-Output 'compiled'` + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + command := exec.CommandContext(ctx, path, "-NoProfile", "-NonInteractive", "-Command", script) + command.Stdin = bytes.NewReader(source) + output, err := command.CombinedOutput() + if err != nil || !strings.Contains(string(output), "compiled") { + t.Fatalf("C# declarations: %v %s", err, output) + } + if len(StartupCommand) > 8191 || len(source) > MaxControl { + t.Fatal("startup/source bounds exceeded") + } + bootstrap := strings.TrimSuffix(strings.SplitN(StartupCommand, `-Command "`, 2)[1], `"`) + script = `$tokens=$null;$errors=$null;[Management.Automation.Language.Parser]::ParseInput([Console]::In.ReadToEnd(),[ref]$tokens,[ref]$errors)|Out-Null;if($errors.Count){$errors|ForEach-Object{$_.Message};exit 1}` + command = exec.CommandContext(ctx, path, "-NoProfile", "-NonInteractive", "-Command", script) + command.Stdin = strings.NewReader(bootstrap) + if output, err = command.CombinedOutput(); err != nil { + t.Fatalf("bootstrap parse: %v %s", err, output) + } +} diff --git a/internal/guest/stream/context_io.go b/internal/guest/stream/context_io.go new file mode 100644 index 0000000..ac21c60 --- /dev/null +++ b/internal/guest/stream/context_io.go @@ -0,0 +1,156 @@ +package stream + +import ( + "bytes" + "context" + "io" +) + +// ContextReader must return promptly when ctx is cancelled, including when its +// underlying source blocks. Execute rejects raw readers before dispatch. +type ContextReader interface { + io.Reader + ReadContext(context.Context, []byte) (int, error) +} + +// ContextWriter obeys the same cancellation contract for blocked sinks. +type ContextWriter interface { + io.Writer + WriteContext(context.Context, []byte) (int, error) +} + +type bytesInput struct{ *bytes.Reader } + +func (b bytesInput) ReadContext(ctx context.Context, p []byte) (int, error) { + if err := ctx.Err(); err != nil { + return 0, err + } + return b.Read(p) +} + +// BytesInput wraps nonblocking immutable bytes; the caller keeps them immutable +// until Execute returns. +func BytesInput(data []byte) ContextReader { return bytesInput{bytes.NewReader(data)} } + +type bufferOutput struct{ *bytes.Buffer } + +func (b bufferOutput) WriteContext(ctx context.Context, p []byte) (int, error) { + if err := ctx.Err(); err != nil { + return 0, err + } + return b.Write(p) +} +func BufferOutput(buffer *bytes.Buffer) ContextWriter { + if buffer == nil { + return nil + } + return bufferOutput{buffer} +} + +type ownedInput struct{ io.ReadCloser } + +// OwnedInput grants cancellation authority over this exact source. Close must +// promptly unblock Read (files, pipes and network bodies); wrapping an arbitrary +// blocking reader with io.NopCloser does not satisfy this contract. The caller +// still closes the source after successful completion. +func OwnedInput(source io.ReadCloser) ContextReader { + if source == nil { + return nil + } + return ownedInput{source} +} +func (r ownedInput) ReadContext(ctx context.Context, p []byte) (int, error) { + stop, err := bindOwnedIO(ctx, r.ReadCloser) + if err != nil { + return 0, err + } + defer stop() + n, err := r.Read(p) + if ctx.Err() != nil { + return n, ctx.Err() + } + return n, err +} + +type ownedOutput struct{ io.WriteCloser } + +// OwnedOutput grants the same cancellation authority over an owned sink whose +// Close promptly unblocks Write. The caller closes it on normal completion. +func OwnedOutput(sink io.WriteCloser) ContextWriter { + if sink == nil { + return nil + } + return ownedOutput{sink} +} +func (w ownedOutput) WriteContext(ctx context.Context, p []byte) (int, error) { + stop, err := bindOwnedIO(ctx, w.WriteCloser) + if err != nil { + return 0, err + } + defer stop() + n, err := w.Write(p) + if ctx.Err() != nil { + return n, ctx.Err() + } + return n, err +} +func bindOwnedIO(ctx context.Context, closer io.Closer) (func(), error) { + if err := ctx.Err(); err != nil { + return nil, err + } + done := make(chan struct{}) + cancel := context.AfterFunc(ctx, func() { defer close(done); _ = closer.Close() }) + return func() { + if !cancel() { + <-done + } + }, nil +} + +type requestReader struct { + ctx context.Context + source ContextReader +} + +func (r requestReader) Read(p []byte) (int, error) { return r.source.ReadContext(r.ctx, p) } + +type requestWriter struct { + ctx context.Context + sink ContextWriter +} + +func (w requestWriter) Write(p []byte) (int, error) { return w.sink.WriteContext(w.ctx, p) } +func bindRequestIO(ctx context.Context, input io.Reader, output io.Writer) (io.Reader, io.Writer, error) { + if input != nil { + source, ok := input.(ContextReader) + if !ok { + return nil, nil, ErrRequest + } + input = requestReader{ctx, source} + } + if output != nil { + sink, ok := output.(ContextWriter) + if !ok { + return nil, nil, ErrRequest + } + output = requestWriter{ctx, sink} + } + return input, output, nil +} + +// HTTPInput owns cancellation of the exact outbound HTTP request and its body. +// Cancelling the request first wakes transport reads; Body.Close alone can wait +// on an in-flight Read. The caller closes the body after normal completion. +func HTTPInput(body io.ReadCloser, cancelRequest context.CancelFunc) ContextReader { + if body == nil || cancelRequest == nil { + return nil + } + return OwnedInput(httpInput{body, cancelRequest}) +} + +type httpInput struct { + io.ReadCloser + cancel context.CancelFunc +} + +func (body httpInput) Close() error { body.cancel(); return body.ReadCloser.Close() } diff --git a/internal/guest/stream/context_io_test.go b/internal/guest/stream/context_io_test.go new file mode 100644 index 0000000..51f346d --- /dev/null +++ b/internal/guest/stream/context_io_test.go @@ -0,0 +1,235 @@ +package stream + +import ( + "bytes" + "context" + "errors" + "io" + "net" + "net/http" + "net/http/httptest" + "os" + "testing" + "time" +) + +func TestOwnedIOCancelsBlockedPipeReadAndWrite(t *testing.T) { + for _, operation := range []string{"read", "write"} { + t.Run(operation, func(t *testing.T) { + reader, writer := io.Pipe() + defer reader.Close() + defer writer.Close() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + started := make(chan struct{}) + done := make(chan error, 1) + go func() { + close(started) + var err error + if operation == "read" { + _, err = OwnedInput(reader).ReadContext(ctx, make([]byte, 1)) + } else { + _, err = OwnedOutput(writer).WriteContext(ctx, []byte{1}) + } + done <- err + }() + <-started + cancel() + select { + case err := <-done: + if !errors.Is(err, context.Canceled) { + t.Fatal(err) + } + case <-time.After(time.Second): + t.Fatal("owned IO failed to unblock") + } + }) + } +} +func TestContextIORejectsUnsupportedBeforeDispatch(t *testing.T) { + f := &fixtureOpener{} + c := fixtureClient(t, f) + _, err := c.Prepare(context.Background(), "target", "actor", "g") + if err != nil { + t.Fatal(err) + } + for _, input := range []io.Reader{bytes.NewReader(nil), io.NopCloser(bytes.NewReader(nil))} { + if _, err = c.Execute(context.Background(), "target", "actor", "g", request(), input, nil); !errors.Is(err, ErrRequest) { + t.Fatal("unsupported input admitted", err) + } + } + if _, err = c.Execute(context.Background(), "target", "actor", "g", request(), nil, &bytes.Buffer{}); !errors.Is(err, ErrRequest) { + t.Fatal("unsupported output admitted", err) + } + if f.requests.Load() != 0 { + t.Fatal("unsupported IO wrote request") + } + if _, err = c.Execute(context.Background(), "target", "actor", "g", request(), BytesInput(nil), BufferOutput(&bytes.Buffer{})); err != nil { + t.Fatal(err) + } +} +func TestCancelledMemoryIOHasNoEffects(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + var buffer bytes.Buffer + if _, err := BytesInput([]byte{1}).ReadContext(ctx, make([]byte, 1)); !errors.Is(err, context.Canceled) { + t.Fatal(err) + } + if _, err := BufferOutput(&buffer).WriteContext(ctx, []byte{1}); !errors.Is(err, context.Canceled) || buffer.Len() != 0 { + t.Fatal(err) + } +} + +type signalledReader struct { + *io.PipeReader + started chan struct{} +} + +func (r signalledReader) Read(p []byte) (int, error) { close(r.started); return r.PipeReader.Read(p) } + +type signalledWriter struct { + *io.PipeWriter + started chan struct{} +} + +func (w signalledWriter) Write(p []byte) (int, error) { close(w.started); return w.PipeWriter.Write(p) } +func TestExecuteCancellationUnblocksCallerIO(t *testing.T) { + for _, operation := range []string{"upload", "download"} { + t.Run(operation, func(t *testing.T) { testExecuteCancellationIO(t, operation) }) + } +} +func testExecuteCancellationIO(t *testing.T, operation string) { + t.Helper() + started := make(chan struct{}) + reader, writer := io.Pipe() + defer reader.Close() + defer writer.Close() + f := &fixtureOpener{} + req := request() + var input io.Reader + var output io.Writer + if operation == "upload" { + req.Size = 1 + req.SHA256 = hash([]byte{1}) + input = OwnedInput(signalledReader{reader, started}) + } else { + req.Operation = "get" + req.Path = "synthetic" + req.Owned = []FileIdentity{{Path: req.Path}} + req.MaxOutput = 1 + output = OwnedOutput(signalledWriter{writer, started}) + f.handler = func(ch net.Conn, request Request) { + result := Result{ID: request.ID, Size: 1, SHA256: hash([]byte{1}), Worker: WorkerIdentity{PID: 12, BirthTicks: 123, Image: "synthetic-worker", SID: "synthetic-sid"}} + _ = writeControl(ch, result) + _, _ = ch.Write([]byte{1}) + _ = writeControl(ch, result) + } + } + c := fixtureClient(t, f) + if _, err := c.Prepare(context.Background(), "target", "actor", "g"); err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + done := make(chan error, 1) + go func() { _, err := c.Execute(ctx, "target", "actor", "g", req, input, output); done <- err }() + select { + case <-started: + case <-time.After(time.Second): + t.Fatal("caller IO never entered") + } + cancel() + select { + case err := <-done: + if !errors.Is(err, context.Canceled) { + t.Fatal(err) + } + case <-time.After(time.Second): + t.Fatal("Execute remained blocked in caller IO") + } + f.wg.Wait() + if f.count.Load() != 1 || f.requests.Load() != 1 { + t.Fatal("cancelled request replayed") + } +} + +func TestHTTPInputCancelsExactRequestBeforeClosingBody(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.(http.Flusher).Flush() + <-r.Context().Done() + })) + defer server.Close() + requestContext, cancelRequest := context.WithCancel(context.Background()) + defer cancelRequest() + request, err := http.NewRequestWithContext(requestContext, http.MethodGet, server.URL, nil) + if err != nil { + t.Fatal(err) + } + response, err := server.Client().Do(request) + if err != nil { + t.Fatal(err) + } + defer response.Body.Close() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + started := make(chan struct{}) + done := make(chan error, 1) + body := signalBody{response.Body, started} + go func() { _, err := HTTPInput(body, cancelRequest).ReadContext(ctx, make([]byte, 1)); done <- err }() + <-started + cancel() + select { + case err := <-done: + if !errors.Is(err, context.Canceled) { + t.Fatal(err) + } + case <-time.After(time.Second): + t.Fatal("HTTP body remained blocked") + } + if !errors.Is(requestContext.Err(), context.Canceled) { + t.Fatal("exact HTTP request remained active") + } +} + +type signalBody struct { + io.ReadCloser + started chan struct{} +} + +func (b signalBody) Read(p []byte) (int, error) { close(b.started); return b.ReadCloser.Read(p) } + +func TestMemoryWrappersPreserveBytes(t *testing.T) { + payload := []byte{0, 255, 128} + got := make([]byte, 3) + if n, err := BytesInput(payload).ReadContext(context.Background(), got); err != nil || n != 3 || !bytes.Equal(got, payload) { + t.Fatal(got, n, err) + } + var buffer bytes.Buffer + if n, err := BufferOutput(&buffer).WriteContext(context.Background(), payload); err != nil || n != 3 || !bytes.Equal(buffer.Bytes(), payload) { + t.Fatal(n, err) + } +} + +func TestOwnedIOCompletesWithoutCancellation(t *testing.T) { + path := t.TempDir() + "/synthetic" + file, err := os.Create(path) + if err != nil { + t.Fatal(err) + } + defer file.Close() + payload := []byte{1, 2, 0, 255} + if n, err := OwnedOutput(file).WriteContext(context.Background(), payload); err != nil || n != len(payload) { + t.Fatal(n, err) + } + if _, err = file.Seek(0, io.SeekStart); err != nil { + t.Fatal(err) + } + buffer := make([]byte, len(payload)) + if n, err := OwnedInput(file).ReadContext(context.Background(), buffer); err != nil || n != len(payload) || !bytes.Equal(buffer, payload) { + t.Fatal(n, err) + } + if OwnedInput(nil) != nil || OwnedOutput(nil) != nil || BufferOutput(nil) != nil || HTTPInput(file, nil) != nil { + t.Fatal("nil IO wrapper accepted") + } +} diff --git a/internal/guest/stream/control_failure.go b/internal/guest/stream/control_failure.go new file mode 100644 index 0000000..f12ca90 --- /dev/null +++ b/internal/guest/stream/control_failure.go @@ -0,0 +1,52 @@ +package stream + +import ( + "errors" + "io" + "reflect" +) + +var ErrControlFailure = errors.New("guest stream: control input failed") + +// ControlFailure is bound to a complete fatal pair and the original hello, +// before a request ID was decoded. It is never a business-operation result. +type ControlFailure struct { + Stage string `json:"stage"` +} + +func (f *ControlFailure) Valid() bool { + if f == nil { + return false + } + switch f.Stage { + case "control_eof", "control_read", "control_decode", "control_header": + return true + } + return false +} +func (f *ControlFailure) Error() string { + if !f.Valid() { + return ErrProtocol.Error() + } + return "guest stream: fatal " + f.Stage +} +func (*ControlFailure) Unwrap() error { return ErrControlFailure } + +func validControlFailure(result Result, original WorkerIdentity) bool { + return result.ControlFailure.Valid() && result.ID == "" && result.Worker == original && + result.Status == "failed" && result.ExitCode == 1 && result.Size == 0 && result.SHA256 == emptyDigest && + result.Root == nil && len(result.Files) == 0 && len(result.Removed) == 0 && result.Failure == nil +} +func receiveControlFailure(ch io.Reader, header Result, original WorkerIdentity) (Result, error) { + if !validControlFailure(header, original) { + return Result{}, ErrProtocol + } + var completion Result + if err := readControl(ch, &completion); err != nil { + return Result{}, err + } + if !validControlFailure(completion, original) || !reflect.DeepEqual(header, completion) { + return Result{}, ErrProtocol + } + return Result{}, &ControlFailure{Stage: completion.ControlFailure.Stage} +} diff --git a/internal/guest/stream/control_failure_test.go b/internal/guest/stream/control_failure_test.go new file mode 100644 index 0000000..73d6616 --- /dev/null +++ b/internal/guest/stream/control_failure_test.go @@ -0,0 +1,96 @@ +package stream + +import ( + "bytes" + "encoding/json" + "errors" + "net" + "testing" + "time" +) + +func controlFatal(stage string, original WorkerIdentity) Result { + return Result{Status: "failed", ExitCode: 1, SHA256: emptyDigest, Worker: original, ControlFailure: &ControlFailure{Stage: stage}} +} +func TestControlFatalPairOriginalBindingAndNoPayload(t *testing.T) { + original := WorkerIdentity{PID: 12, BirthTicks: 123, Image: "synthetic-worker", SID: "synthetic-sid"} + for _, stage := range []string{"control_eof", "control_read", "control_decode", "control_header"} { + t.Run(stage, func(t *testing.T) { + fatal := controlFatal(stage, original) + var output bytes.Buffer + _, err := receiveResult(failureWire(fatal, fatal), Request{ID: "actual-request"}, &output, original) + observed, ok := errors.AsType[*ControlFailure](err) + if !ok || observed.Stage != stage || !errors.Is(err, ErrControlFailure) || errors.Is(err, ErrWorkerFailure) || output.Len() != 0 { + t.Fatal("fatal provenance lost", err) + } + }) + } +} +func TestControlFatalRejectsShapeIdentityMismatchAndTruncation(t *testing.T) { + original := WorkerIdentity{PID: 12, BirthTicks: 123, Image: "synthetic-worker", SID: "synthetic-sid"} + for name, alter := range map[string]func(*Result){ + "request_id": func(r *Result) { r.ID = "foreign" }, "identity": func(r *Result) { r.Worker.BirthTicks++ }, + "stage": func(r *Result) { r.ControlFailure.Stage = "private path" }, "payload": func(r *Result) { r.Size = 1 }, + "digest": func(r *Result) { r.SHA256 = hash([]byte("foreign")) }, "root": func(r *Result) { r.Root = &RootIdentity{} }, + "file": func(r *Result) { r.Files = []FileIdentity{{}} }, "removal": func(r *Result) { r.Removed = []Removal{{}} }, + "business_failure": func(r *Result) { r.Failure = &WorkerFailure{Stage: "observe"} }, "status": func(r *Result) { r.Status = "ok" }, + "exit": func(r *Result) { r.ExitCode = 0 }, + } { + t.Run(name, func(t *testing.T) { + fatal := controlFatal("control_eof", original) + alter(&fatal) + var output bytes.Buffer + if _, err := receiveResult(failureWire(fatal, fatal), Request{ID: "request"}, &output, original); !errors.Is(err, ErrProtocol) || output.Len() != 0 { + t.Fatal("invalid fatal accepted", err) + } + }) + } + fatal := controlFatal("control_read", original) + other := controlFatal("control_decode", original) + if _, err := receiveResult(failureWire(fatal, other), Request{}, nil, original); !errors.Is(err, ErrProtocol) { + t.Fatal("mismatched pair accepted", err) + } + var changedWire bytes.Buffer + _ = writeControl(&changedWire, fatal) + raw, _ := json.Marshal(fatal) + var changed map[string]any + _ = json.Unmarshal(raw, &changed) + changed["files"] = []any{} + _ = writeControl(&changedWire, changed) + if _, err := receiveResult(&changedWire, Request{}, nil, original); !errors.Is(err, ErrProtocol) { + t.Fatal("nonidentical fatal pair accepted", err) + } + var partial bytes.Buffer + _ = writeControl(&partial, fatal) + if _, err := receiveResult(&partial, Request{}, nil, original); errors.Is(err, ErrControlFailure) || err == nil { + t.Fatal("partial pair promoted", err) + } +} +func TestControlFatalEvictsWithoutReplay(t *testing.T) { + fixture := &fixtureOpener{handler: func(conn net.Conn, _ Request) { + fatal := controlFatal("control_read", WorkerIdentity{PID: 12, BirthTicks: 123, Image: "synthetic-worker", SID: "synthetic-sid"}) + _ = writeControl(conn, fatal) + _ = writeControl(conn, fatal) + }} + client := fixtureClient(t, fixture) + if _, err := client.Prepare(t.Context(), "target", "actor", "g"); err != nil { + t.Fatal(err) + } + req := Request{ID: "request", Operation: "observe", Path: `C:\synthetic`, DeadlineUTC: time.Now().Add(time.Second)} + if _, err := client.Execute(t.Context(), "target", "actor", "g", req, nil, nil); !errors.Is(err, ErrControlFailure) { + t.Fatal(err) + } + if _, err := client.Execute(t.Context(), "target", "actor", "g", req, nil, nil); !errors.Is(err, ErrRequest) || fixture.count.Load() != 1 || fixture.requests.Load() != 1 { + t.Fatal("fatal replayed", err) + } +} + +func TestControlFatalCannotReplaceOperationCompletion(t *testing.T) { + original := WorkerIdentity{PID: 12, BirthTicks: 123, Image: "synthetic-worker", SID: "synthetic-sid"} + header := Result{ID: "request", Worker: original, Status: "ok", SHA256: emptyDigest} + completion := controlFatal("control_read", original) + completion.ID = "request" + if _, err := receiveResult(failureWire(header, completion), Request{ID: "request"}, nil, original); !errors.Is(err, ErrProtocol) || errors.Is(err, ErrControlFailure) { + t.Fatal("unannounced fatal replaced operation result", err) + } +} diff --git a/internal/guest/stream/exchange_test.go b/internal/guest/stream/exchange_test.go new file mode 100644 index 0000000..98e6541 --- /dev/null +++ b/internal/guest/stream/exchange_test.go @@ -0,0 +1,120 @@ +package stream + +import ( + "bytes" + "errors" + "io" + "testing" + "time" +) + +type scriptedChannel struct { + read *bytes.Reader + write bytes.Buffer +} + +func (s *scriptedChannel) Read(p []byte) (int, error) { return s.read.Read(p) } +func (s *scriptedChannel) Write(p []byte) (int, error) { return s.write.Write(p) } +func response(req Request, payload []byte, result Result) *scriptedChannel { + var wire bytes.Buffer + _ = writeControl(&wire, Result{ID: req.ID, Size: int64(len(payload))}) + _, _ = wire.Write(payload) + _ = writeControl(&wire, result) + return &scriptedChannel{read: bytes.NewReader(wire.Bytes())} +} +func TestDownloadResultRequiresObservedDigestAndCompleteReceipt(t *testing.T) { + req := Request{ID: "get", Operation: "get", MaxOutput: 100} + data := []byte{0, 255, 13, 10} + result := Result{ID: req.ID, Status: "ok", Size: 4, SHA256: hash(data)} + ch := response(req, data, result) + var out bytes.Buffer + got, err := exchange(ch, req, nil, &out, WorkerIdentity{}) + if err != nil || got.SHA256 != hash(data) || !bytes.Equal(out.Bytes(), data) { + t.Fatal(got, err) + } + for _, bad := range []Result{{ID: "other", Size: 4, SHA256: hash(data)}, {ID: req.ID, Size: 3, SHA256: hash(data)}, {ID: req.ID, Size: 4, SHA256: hash(nil)}} { + if _, err = exchange(response(req, data, bad), req, nil, io.Discard, WorkerIdentity{}); !errors.Is(err, ErrIntegrity) { + t.Fatal("accepted wrong receipt", err) + } + } + wire := response(req, data, result) + raw := make([]byte, wire.read.Len()) + _, _ = wire.read.Read(raw) + wire.read = bytes.NewReader(raw[:len(raw)-1]) + if _, err = exchange(wire, req, nil, io.Discard, WorkerIdentity{}); err == nil { + t.Fatal("accepted incomplete completion") + } + req.MaxOutput = 3 + if _, err = exchange(response(req, data, result), req, nil, io.Discard, WorkerIdentity{}); !errors.Is(err, ErrProtocol) { + t.Fatal("unbounded output", err) + } +} +func TestWholeRequestValidation(t *testing.T) { + valid := request() + cases := []Request{valid, valid, valid, valid, valid, valid, valid, valid} + cases[0].ID = "" + cases[1].DeadlineUTC = time.Now().Add(-time.Second) + cases[2].Size = -1 + cases[3].SHA256 = "no" + cases[4].Operation = "unknown" + cases[5].Operation = "exec" + cases[6].Operation = "get" + cases[7].Operation = "remove-owned" + for i, req := range cases { + if err := validate(req); !errors.Is(err, ErrRequest) { + t.Fatalf("invalid case %d: %v", i, err) + } + } + if err := validate(valid); err != nil { + t.Fatal(err) + } +} + +func TestObserveRequiresExactPathAndUploadPreflight(t *testing.T) { + observe := Request{ID: "observe", Operation: "observe", Path: `C:\synthetic\workspace`, DeadlineUTC: time.Now().Add(time.Minute)} + if err := validate(observe); err != nil { + t.Fatal(err) + } + observe.Path = "" + if !errors.Is(validate(observe), ErrRequest) { + t.Fatal("missing preflight target") + } + put := request() + put.ExpectedRoot = nil + if !errors.Is(validate(put), ErrRequest) { + t.Fatal("upload without original root") + } +} + +func TestDownloadRequiresOneOriginalReferenceForExactPath(t *testing.T) { + req := Request{ID: "owned-download", Operation: "get", Path: `C:\synthetic\workspace\payload`, DeadlineUTC: time.Now().Add(time.Minute), MaxOutput: 64} + for _, refs := range [][]FileIdentity{nil, {{Path: `C:\synthetic\foreign\payload`}}, {{Path: req.Path}, {Path: req.Path}}} { + req.Owned = refs + if !errors.Is(validate(req), ErrRequest) { + t.Fatal("accepted missing, foreign or ambiguous ownership reference") + } + } + req.Owned = []FileIdentity{{Path: req.Path}} + if err := validate(req); err != nil { + t.Fatal(err) + } +} + +func TestHeaderWorkerIdentityRejectedBeforePayload(t *testing.T) { + expected := WorkerIdentity{PID: 1, BirthTicks: 2, Image: "synthetic-worker", SID: "synthetic-sid"} + foreign := expected + foreign.PID = 3 + req := Request{ID: "secret", Operation: "get", MaxOutput: 100} + payload := []byte("must not escape") + var wire bytes.Buffer + _ = writeControl(&wire, Result{ID: req.ID, Worker: foreign, Size: int64(len(payload))}) + _, _ = wire.Write(payload) + ch := &scriptedChannel{read: bytes.NewReader(wire.Bytes())} + var output bytes.Buffer + if _, err := exchange(ch, req, nil, &output, expected); !errors.Is(err, ErrProtocol) { + t.Fatal(err) + } + if output.Len() != 0 || ch.read.Len() != len(payload) { + t.Fatal("foreign worker payload consumed or emitted") + } +} diff --git a/internal/guest/stream/failure.go b/internal/guest/stream/failure.go new file mode 100644 index 0000000..a6cef48 --- /dev/null +++ b/internal/guest/stream/failure.go @@ -0,0 +1,67 @@ +package stream + +import ( + "errors" + "fmt" +) + +var ErrWorkerFailure = errors.New("guest stream: worker failed") + +// WorkerFailure contains only a fixed stage and a native numeric error. It never +// carries exception text, a guest path, or a security descriptor. +type WorkerFailure struct { + Stage string `json:"stage"` + NativeError uint32 `json:"nativeError"` +} + +func (f *WorkerFailure) Error() string { + if !f.Valid() { + return ErrProtocol.Error() + } + return fmt.Sprintf("guest stream: worker failed at %s (native error %d)", f.Stage, f.NativeError) +} + +func (*WorkerFailure) Unwrap() error { return ErrWorkerFailure } + +// Valid allows trusted receipt adapters to reject diagnostics forged by an +// injected transport before recording the fixed stage and numeric error. +func (f *WorkerFailure) Valid() bool { return f != nil && validFailureStage(f.Stage) } + +func validFailureStage(stage string) bool { + switch stage { + case "runspace_open", "request", "observe", "put", "get", "exec", "remove_owned", + "security_token", "security_lookup", "security_enable", "security_restore", + "ancestor_open", "workspace_open", "file_open", "metadata_read", "file_id_read", + "security_size", "security_read", "security_parse", "file_commit", "file_delete", "file_owner_set": + return true + default: + return false + } +} + +func validFailureResult(result Result) bool { + return result.ControlFailure == nil && result.Failure.Valid() && + result.Status == "failed" && result.ExitCode == 1 && result.Size == 0 && + result.SHA256 == emptyDigest && result.Root == nil && len(result.Files) == 0 && len(result.Removed) == 0 +} + +const emptyDigest = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + +func invalidFailureHeader(header Result) bool { + return (header.Failure != nil || header.Status == "failed") && !validFailureResult(header) +} + +// A failure is evidence only when both complete frames agree. An ordinary +// response cannot turn into an unannounced failure after payload publication. +func verifyWorkerFailure(header, result Result) error { + if header.Failure != nil { + if !validFailureResult(result) || *header.Failure != *result.Failure { + return ErrProtocol + } + return result.Failure + } + if result.ControlFailure != nil || result.Failure != nil || result.Status == "failed" { + return ErrProtocol + } + return nil +} diff --git a/internal/guest/stream/failure_test.go b/internal/guest/stream/failure_test.go new file mode 100644 index 0000000..5e16001 --- /dev/null +++ b/internal/guest/stream/failure_test.go @@ -0,0 +1,104 @@ +package stream + +import ( + "bytes" + "context" + "errors" + "net" + "strings" + "testing" + "time" +) + +func failureResult(id string, worker WorkerIdentity) Result { + return Result{ID: id, Worker: worker, Status: "failed", ExitCode: 1, SHA256: emptyDigest, Failure: &WorkerFailure{Stage: "workspace_open", NativeError: 5}} +} +func failureWire(header, completion Result) *bytes.Reader { + var wire bytes.Buffer + _ = writeControl(&wire, header) + _ = writeControl(&wire, completion) + return bytes.NewReader(wire.Bytes()) +} +func TestVerifiedWorkerFailureHasBoundedMetadataAndNoPayload(t *testing.T) { + worker := WorkerIdentity{PID: 12, BirthTicks: 123, Image: "synthetic-worker", SID: "synthetic-sid"} + req := Request{ID: "observe", Operation: "observe", Path: `C:\synthetic`, DeadlineUTC: time.Now().Add(time.Minute)} + result := failureResult(req.ID, worker) + var sink bytes.Buffer + got, err := receiveResult(failureWire(result, result), req, &sink, worker) + var diagnostic *WorkerFailure + if !errors.As(err, &diagnostic) || !errors.Is(err, ErrWorkerFailure) || diagnostic.Stage != "workspace_open" || diagnostic.NativeError != 5 || got.Failure == nil || sink.Len() != 0 { + t.Fatalf("failure contract: result=%+v error=%v payload=%d", got, err, sink.Len()) + } + if diagnostic.Error() != "guest stream: worker failed at workspace_open (native error 5)" { + t.Fatal(diagnostic.Error()) + } + diagnostic.Stage = `C:\private\secret SDDL token` + if strings.Contains(diagnostic.Error(), "private") || diagnostic.Error() != ErrProtocol.Error() { + t.Fatal("invalid stage leaked") + } +} +func TestWorkerFailureRejectsIncompleteOrInconsistentFrames(t *testing.T) { + req := Request{ID: "observe", MaxOutput: 64} + original := failureResult(req.ID, WorkerIdentity{}) + cases := map[string]func(*Result){ + "unknown stage": func(r *Result) { r.Failure.Stage = "private exception" }, + "payload": func(r *Result) { r.Size = 1 }, + "root": func(r *Result) { r.Root = &RootIdentity{Path: "private"} }, + "files": func(r *Result) { r.Files = []FileIdentity{{Path: "private"}} }, + "removal": func(r *Result) { r.Removed = []Removal{{Path: "private"}} }, + "success": func(r *Result) { r.Status = "ok" }, + "exit zero": func(r *Result) { r.ExitCode = 0 }, + "missing diagnostic": func(r *Result) { r.Failure = nil }, + } + for name, change := range cases { + t.Run(name, func(t *testing.T) { + changed := original + copied := *original.Failure + changed.Failure = &copied + change(&changed) + var sink bytes.Buffer + if _, err := receiveResult(failureWire(changed, changed), req, &sink, WorkerIdentity{}); err == nil || sink.Len() != 0 { + t.Fatal("accepted invalid failure", err) + } + }) + } + mismatch := original + mismatch.Failure = &WorkerFailure{Stage: "security_read", NativeError: 1314} + if _, err := receiveResult(failureWire(original, mismatch), req, nil, WorkerIdentity{}); !errors.Is(err, ErrProtocol) { + t.Fatal("mismatched stage accepted", err) + } + var truncated bytes.Buffer + _ = writeControl(&truncated, original) + if _, err := receiveResult(&truncated, req, nil, WorkerIdentity{}); err == nil { + t.Fatal("failure without completion accepted") + } + success := original + success.Status = "ok" + success.ExitCode = 0 + success.Failure = nil + if _, err := receiveResult(failureWire(success, original), req, nil, WorkerIdentity{}); !errors.Is(err, ErrProtocol) { + t.Fatal("unannounced failure accepted", err) + } +} +func TestStructuredWorkerFailureEvictsWithoutReplay(t *testing.T) { + f := &fixtureOpener{handler: func(ch net.Conn, req Request) { + result := failureResult(req.ID, WorkerIdentity{PID: 12, BirthTicks: 123, Image: "synthetic-worker", SID: "synthetic-sid"}) + _ = writeControl(ch, result) + _ = writeControl(ch, result) + }} + client := fixtureClient(t, f) + ctx := context.Background() + if _, err := client.Prepare(ctx, "target", "actor", "generation"); err != nil { + t.Fatal(err) + } + req := Request{ID: "observe", Operation: "observe", Path: `C:\synthetic`, DeadlineUTC: time.Now().Add(time.Minute)} + if _, err := client.Execute(ctx, "target", "actor", "generation", req, nil, nil); !errors.Is(err, ErrWorkerFailure) { + t.Fatal(err) + } + if _, err := client.Execute(ctx, "target", "actor", "generation", req, nil, nil); !errors.Is(err, ErrRequest) { + t.Fatal("reused failed worker", err) + } + if f.count.Load() != 1 || f.requests.Load() != 1 { + t.Fatal("replayed or restarted") + } +} diff --git a/internal/guest/stream/file_owner_test.go b/internal/guest/stream/file_owner_test.go new file mode 100644 index 0000000..87a0495 --- /dev/null +++ b/internal/guest/stream/file_owner_test.go @@ -0,0 +1,25 @@ +package stream + +import ( + "bytes" + "errors" + "testing" +) + +func TestFileOwnerFailureRequiresExactPairedMetadata(t *testing.T) { + worker := WorkerIdentity{PID: 12, BirthTicks: 123, Image: "synthetic-worker", SID: "synthetic-user"} + request := Request{ID: "owned-put", Operation: "put"} + header := failureResult(request.ID, worker) + header.Failure = &WorkerFailure{Stage: "file_owner_set", NativeError: 5} + var output bytes.Buffer + result, err := receiveResult(failureWire(header, header), request, &output, worker) + var failure *WorkerFailure + if !errors.As(err, &failure) || failure.Stage != "file_owner_set" || failure.NativeError != 5 || result.Failure == nil || output.Len() != 0 { + t.Fatalf("owner refusal contract: result=%+v error=%v output=%d", result, err, output.Len()) + } + completion := header + completion.Failure = &WorkerFailure{Stage: "file_owner_set", NativeError: 1307} + if _, err := receiveResult(failureWire(header, completion), request, &output, worker); !errors.Is(err, ErrProtocol) || output.Len() != 0 { + t.Fatal("inconsistent owner refusal accepted", err) + } +} diff --git a/internal/guest/stream/framing.go b/internal/guest/stream/framing.go new file mode 100644 index 0000000..af6d3b1 --- /dev/null +++ b/internal/guest/stream/framing.go @@ -0,0 +1,72 @@ +package stream + +import ( + "crypto/sha256" + "encoding/binary" + "encoding/hex" + "encoding/json" + "io" +) + +func writeAll(w io.Writer, p []byte) error { + for len(p) > 0 { + n, err := w.Write(p) + if n < 0 || n > len(p) { + return ErrProtocol + } + p = p[n:] + if err != nil { + return err + } + if n == 0 { + return io.ErrShortWrite + } + } + return nil +} +func writeControl(w io.Writer, v any) error { + p, err := json.Marshal(v) + if err != nil { + return err + } + if len(p) > MaxControl { + return ErrProtocol + } + var h [4]byte + // #nosec G115 -- length is checked against the 256 KiB control bound above. + binary.BigEndian.PutUint32(h[:], uint32(len(p))) + if err = writeAll(w, h[:]); err != nil { + return err + } + return writeAll(w, p) +} +func readControl(r io.Reader, v any) error { + var h [4]byte + if _, err := io.ReadFull(r, h[:]); err != nil { + return err + } + n := binary.BigEndian.Uint32(h[:]) + if n == 0 || n > MaxControl { + return ErrProtocol + } + p := make([]byte, n) + if _, err := io.ReadFull(r, p); err != nil { + return err + } + if err := json.Unmarshal(p, v); err != nil { + return ErrProtocol + } + return nil +} +func copyPayload(w io.Writer, r io.Reader, size int64) (string, error) { + h := sha256.New() + buf := make([]byte, BufferSize) + n, err := io.CopyBuffer(io.MultiWriter(w, h), io.LimitReader(r, size), buf) + if err != nil { + return "", err + } + if n != size { + return "", ErrIntegrity + } + return hex.EncodeToString(h.Sum(nil)), nil +} diff --git a/internal/guest/stream/framing_test.go b/internal/guest/stream/framing_test.go new file mode 100644 index 0000000..513cd14 --- /dev/null +++ b/internal/guest/stream/framing_test.go @@ -0,0 +1,102 @@ +package stream + +import ( + "bytes" + "crypto/sha256" + "encoding/binary" + "encoding/hex" + "errors" + "io" + "testing" +) + +type fragments struct { + io.Reader + size int +} + +func (f fragments) Read(p []byte) (int, error) { + if len(p) > f.size { + p = p[:f.size] + } + return f.Reader.Read(p) +} + +type shortWriter struct { + bytes.Buffer + size int +} + +func (w *shortWriter) Write(p []byte) (int, error) { + if len(p) > w.size { + p = p[:w.size] + } + return w.Buffer.Write(p) +} + +type zeroWriter struct{} + +func (zeroWriter) Write([]byte) (int, error) { return 0, nil } +func hash(data []byte) string { h := sha256.Sum256(data); return hex.EncodeToString(h[:]) } +func TestControlFragmentedAndShortWrites(t *testing.T) { + w := &shortWriter{size: 1} + want := Result{ID: "synthetic", Size: 123, SHA256: hash(nil)} + if err := writeControl(w, want); err != nil { + t.Fatal(err) + } + var got Result + if err := readControl(fragments{bytes.NewReader(w.Bytes()), 1}, &got); err != nil || got.ID != want.ID || got.Size != 123 { + t.Fatalf("fragmented: %+v %v", got, err) + } + if err := writeControl(zeroWriter{}, want); !errors.Is(err, io.ErrShortWrite) { + t.Fatal(err) + } +} +func TestControlRejectsTruncationAndBounds(t *testing.T) { + var valid bytes.Buffer + _ = writeControl(&valid, Result{ID: "x"}) + for n := 0; n < valid.Len(); n++ { + var result Result + if err := readControl(bytes.NewReader(valid.Bytes()[:n]), &result); err == nil { + t.Fatalf("accepted truncation %d", n) + } + } + for _, size := range []uint32{0, MaxControl + 1, ^uint32(0)} { + var header [4]byte + binary.BigEndian.PutUint32(header[:], size) + var result Result + if !errors.Is(readControl(bytes.NewReader(header[:]), &result), ErrProtocol) { + t.Fatal(size) + } + } + var result Result + if err := readControl(bytes.NewReader([]byte{0, 0, 0, 1, '!'}), &result); !errors.Is(err, ErrProtocol) { + t.Fatal(err) + } +} +func TestPayloadBinaryIntegrityAndShortIO(t *testing.T) { + payload := bytes.Repeat([]byte{0, 255, 1, 13, 10}, 30000) + var output bytes.Buffer + digest, err := copyPayload(&output, fragments{bytes.NewReader(payload), 7}, int64(len(payload))) + if err != nil || digest != hash(payload) || !bytes.Equal(output.Bytes(), payload) { + t.Fatal("binary corruption", err) + } + if _, err = copyPayload(io.Discard, bytes.NewReader(payload), int64(len(payload)+1)); !errors.Is(err, ErrIntegrity) { + t.Fatal(err) + } + if _, err = copyPayload(&shortWriter{size: 1}, bytes.NewReader(payload), int64(len(payload))); !errors.Is(err, io.ErrShortWrite) { + t.Fatal(err) + } +} +func BenchmarkPayload(b *testing.B) { + data := bytes.Repeat([]byte{1}, 4*1024*1024) + b.SetBytes(int64(len(data))) + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, err := copyPayload(io.Discard, bytes.NewReader(data), int64(len(data))) + if err != nil { + b.Fatal(err) + } + } +} diff --git a/internal/guest/stream/lifetime.cs b/internal/guest/stream/lifetime.cs new file mode 100644 index 0000000..7838789 --- /dev/null +++ b/internal/guest/stream/lifetime.cs @@ -0,0 +1,108 @@ +namespace AMCStream { +using System; +using System.IO; +using System.Runtime.InteropServices; +using System.Threading; +using Microsoft.Win32.SafeHandles; +internal static class Lifetime { + [StructLayout(LayoutKind.Sequential)] struct Limits { + public long ProcessTime,JobTime;public uint Flags;public UIntPtr Minimum,Maximum; + public uint ActiveProcesses;public UIntPtr Affinity;public uint Priority,Scheduling; + } + [StructLayout(LayoutKind.Sequential)] struct Counters {public ulong ReadOperations,WriteOperations,OtherOperations,ReadBytes,WriteBytes,OtherBytes;} + [StructLayout(LayoutKind.Sequential)] struct ExtendedLimits {public Limits Basic;public Counters IO;public UIntPtr ProcessMemory,JobMemory,PeakProcessMemory,PeakJobMemory;} + [DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern SafeFileHandle CreateJobObject(IntPtr security,string name); + [DllImport("kernel32.dll",SetLastError=true)] static extern bool SetInformationJobObject(SafeFileHandle job,int type,ref ExtendedLimits limits,uint length); + [DllImport("kernel32.dll",SetLastError=true)] static extern bool AssignProcessToJobObject(SafeFileHandle job,IntPtr process); + [DllImport("kernel32.dll")] static extern IntPtr GetCurrentProcess(); + [DllImport("kernel32.dll",SetLastError=true)] static extern bool TerminateJobObject(SafeFileHandle job,uint exitCode); + internal static SafeFileHandle OwnWorker() { + var job=CreateJobObject(IntPtr.Zero,null);if(job.IsInvalid){job.Dispose();throw new IOException("worker_job_unavailable");} + var limits=new ExtendedLimits{Basic=new Limits{Flags=0x2008,ActiveProcesses=32}}; // KILL_ON_JOB_CLOSE, bounded own descendants + if(!SetInformationJobObject(job,9,ref limits,(uint)Marshal.SizeOf(typeof(ExtendedLimits)))||!AssignProcessToJobObject(job,GetCurrentProcess())){job.Dispose();throw new IOException("worker_job_unavailable");} + return job; + } + [StructLayout(LayoutKind.Sequential)] struct PipeLocal { + public uint Type,Configuration,MaximumInstances,CurrentInstances,InboundQuota,ReadAvailable,OutboundQuota,WriteAvailable,State,End; + } + [StructLayout(LayoutKind.Sequential)] struct IOStatus {public IntPtr Status;public UIntPtr Information;} + [DllImport("kernel32.dll",SetLastError=true)] static extern IntPtr GetStdHandle(int id); + [DllImport("kernel32.dll",SetLastError=true)] static extern uint GetFileType(IntPtr handle); + [DllImport("ntdll.dll")] static extern int NtQueryInformationFile(IntPtr handle,out IOStatus status,out PipeLocal pipe,uint length,int type); + internal static uint PipeState(IntPtr handle) { + IOStatus status;PipeLocal pipe; + int result=NtQueryInformationFile(handle,out status,out pipe,40,24); // FilePipeLocalInformation + return CheckedPipeState(result,status.Information.ToUInt64(),pipe.State); + } + internal const uint QueryFailureExit=211,ShortInformationExit=212,DisconnectedExit=213,PendingExit=214; + internal const uint ObserverUnhandledExit=215,WorkerUnhandledExit=216,FailureTimerExit=217,ObserverJoinTimeoutExit=218,NormalDisposeExit=219; + internal sealed class PipeFailure : IOException { + internal readonly uint ExitCode; + internal PipeFailure(uint exitCode){ExitCode=exitCode;} + } + internal static uint CheckedPipeState(int status,ulong information,uint state) { + if(status<0)throw new PipeFailure(QueryFailureExit); + if(status==0x103)throw new PipeFailure(PendingExit); + if(information<40)throw new PipeFailure(ShortInformationExit); + return state; + } + internal sealed class OwnTermination { + readonly Func terminate;readonly Action close;int stopped; + internal OwnTermination(Func terminate,Action close){this.terminate=terminate;this.close=close;} + internal void Stop(uint code){ + if(Interlocked.Exchange(ref stopped,1)!=0)return; + try{terminate(code);}catch{}finally{close();} + } + } + internal static void RunWorker(Action run,OwnTermination termination) { + try{run();}catch(PipeFailure error){termination.Stop(error.ExitCode);throw;} + catch{termination.Stop(WorkerUnhandledExit);throw;} + } + internal static void FinishObserver(Action stop,Func join,Action releaseStop,Action releaseTimer,OwnTermination termination) { + stop(); + if(!join()){termination.Stop(ObserverJoinTimeoutExit);releaseTimer();return;} + releaseStop();termination.Stop(NormalDisposeExit);releaseTimer(); + } + internal static void Observe(WaitHandle stopped,Func query,Action disconnect) { + while(!stopped.WaitOne(50)){if(query()!=3){disconnect();return;}} + } + internal static void ObserveOwned(WaitHandle stopped,Func query,OwnTermination termination) { + try{Observe(stopped,query,delegate{termination.Stop(DisconnectedExit);});} + catch(PipeFailure error){termination.Stop(error.ExitCode);} + catch{termination.Stop(ObserverUnhandledExit);} + } + internal static Timer ArmOwnedFailure(OwnTermination termination,DateTime deadline,uint exitCode=FailureTimerExit) { + return ArmFailureBound(delegate{termination.Stop(exitCode);},deadline); + } + internal static TimeSpan FailureBudget(DateTime deadline,DateTime now) { + return TimeSpan.FromTicks(Math.Min(TimeSpan.TicksPerSecond,Math.Max(0,deadline.Ticks-now.Ticks))); + } + internal static Timer ArmFailureBound(Action close,DateTime deadline) { + return new Timer(delegate{close();},null,FailureBudget(deadline,DateTime.UtcNow),Timeout.InfiniteTimeSpan); + } + // After a failed response the worker is terminal. Keep the pipe open long + // enough for the peer to consume the pair; no further request is executed. + internal static void DrainTerminal(Stream input) {while(input.ReadByte()>=0){}} + // Observe the pipe's native connection state without consuming bytes. A full + // input backlog cannot conceal EOF behind a bounded producer queue. + internal sealed class Guard : IDisposable { + readonly SafeFileHandle job;readonly ManualResetEvent stopped=new ManualResetEvent(false);readonly Thread observer;internal readonly OwnTermination Termination;Timer terminal; + internal Guard() { + IntPtr stdin=GetStdHandle(-10); + if(stdin==IntPtr.Zero||stdin==new IntPtr(-1)||GetFileType(stdin)!=3||PipeState(stdin)!=3)throw new IOException("stdin_lifecycle_unavailable"); + job=OwnWorker(); + Termination=new OwnTermination(delegate(uint code){return TerminateJobObject(job,code);},delegate{job.Dispose();}); + observer=new Thread(delegate(){ObserveOwned(stopped,delegate(){return PipeState(stdin);},Termination);});observer.IsBackground=true; + try{observer.Start();}catch{job.Dispose();stopped.Dispose();throw;} + } + internal void HoldTerminal(){stopped.WaitOne();} + internal void ArmFailure(DateTime deadline,uint exitCode){terminal=ArmOwnedFailure(Termination,deadline,exitCode);} + public void Dispose(){ + // The first diagnostic marker wins; every stop still closes this + // exact unnamed job even when native termination fails or throws. + FinishObserver(delegate{stopped.Set();},delegate{return observer.Join(2000);}, + delegate{stopped.Dispose();},delegate{if(terminal!=null)terminal.Dispose();},Termination); + } + } +} +} diff --git a/internal/guest/stream/lifetime_exit.go b/internal/guest/stream/lifetime_exit.go new file mode 100644 index 0000000..fdd08f9 --- /dev/null +++ b/internal/guest/stream/lifetime_exit.go @@ -0,0 +1,67 @@ +package stream + +import ( + "context" + "errors" + "fmt" + "io" + "time" +) + +// RemoteExit is the numeric status reported by this SSH channel. It is only +// diagnostic evidence; it does not establish native process absence. +type RemoteExit struct{ Code int } + +func (e *RemoteExit) Valid() bool { return e != nil && e.Code >= 0 && uint64(e.Code) <= 0xffffffff } +func (e *RemoteExit) Error() string { return fmt.Sprintf("guest stream: remote exit %d", e.Code) } + +var ( + ErrLifetimePending = errors.New("guest stream: lifetime query pending") + ErrLifetimeQuery = errors.New("guest stream: lifetime query failed") + ErrLifetimeInformation = errors.New("guest stream: lifetime information incomplete") + ErrLifetimeDisconnected = errors.New("guest stream: lifetime pipe disconnected") +) + +func observeLifetimeExit(ctx context.Context, channel io.ReadWriteCloser, err error) error { + if !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) { + return err + } + code, known := observeExitStatus(ctx, channel) + if !known { + return err + } + var marker error + switch code { + case 211: + marker = ErrLifetimeQuery + case 212: + marker = ErrLifetimeInformation + case 213: + marker = ErrLifetimeDisconnected + case 214: + marker = ErrLifetimePending + default: + exit := &RemoteExit{Code: code} + if !exit.Valid() { + return err + } + return errors.Join(err, exit) + } + return errors.Join(err, marker) +} + +func observeExitStatus(ctx context.Context, channel io.ReadWriteCloser) (int, bool) { + observer, ok := channel.(interface { + ExitStatus(context.Context) (int, bool) + }) + if !ok || ctx.Err() != nil { + return 0, false + } + bounded, cancel := context.WithTimeout(ctx, 500*time.Millisecond) + defer cancel() + code, known := observer.ExitStatus(bounded) + if !known || bounded.Err() != nil { + return 0, false + } + return code, true +} diff --git a/internal/guest/stream/lifetime_exit_test.go b/internal/guest/stream/lifetime_exit_test.go new file mode 100644 index 0000000..37a7bef --- /dev/null +++ b/internal/guest/stream/lifetime_exit_test.go @@ -0,0 +1,115 @@ +package stream + +import ( + "context" + "errors" + "io" + "net" + "sync/atomic" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" +) + +type exitChannel struct { + guestssh.Duplex + code int + known bool + block bool + calls atomic.Int64 +} + +func (c *exitChannel) ExitStatus(ctx context.Context) (int, bool) { + c.calls.Add(1) + if c.block { + <-ctx.Done() + return 0, false + } + return c.code, c.known +} + +type exitOpener struct { + fixture *fixtureOpener + channel *exitChannel +} + +func (o *exitOpener) OpenDuplex(ctx context.Context, target domain.MachineRef, command string) (guestssh.Duplex, error) { + channel, err := o.fixture.OpenDuplex(ctx, target, command) + o.channel.Duplex = channel + return o.channel, err +} + +func TestLifetimeExitOnlyKnownEOFAndNoReplay(t *testing.T) { + for _, tc := range []struct { + name string + code int + known bool + want error + }{ + {"query", 211, true, ErrLifetimeQuery}, {"information", 212, true, ErrLifetimeInformation}, {"disconnected", 213, true, ErrLifetimeDisconnected}, + {"pending", 214, true, ErrLifetimePending}, {"unknown", 215, true, nil}, {"missing", 211, false, nil}, + } { + t.Run(tc.name, func(t *testing.T) { + fixture := &fixtureOpener{handler: func(_ net.Conn, _ Request) {}} + channel := &exitChannel{code: tc.code, known: tc.known} + opener := &exitOpener{fixture: fixture, channel: channel} + client := NewClient(opener) + t.Cleanup(func() { _ = client.Close(); fixture.wg.Wait() }) + if _, err := client.Prepare(t.Context(), "target", "actor", "g"); err != nil { + t.Fatal(err) + } + req := Request{ID: "observe-exit", Operation: "observe", Path: `C:\synthetic`, DeadlineUTC: time.Now().Add(10 * time.Second)} + _, err := client.Execute(t.Context(), "target", "actor", "g", req, nil, nil) + if !errors.Is(err, io.EOF) || (tc.want != nil && !errors.Is(err, tc.want)) || channel.calls.Load() != 1 { + t.Fatal("exit marker", err) + } + if tc.want == nil && (errors.Is(err, ErrLifetimeQuery) || errors.Is(err, ErrLifetimeInformation) || errors.Is(err, ErrLifetimeDisconnected) || errors.Is(err, ErrLifetimePending)) { + t.Fatal("unknown exit adopted", err) + } + if _, err = client.Execute(t.Context(), "target", "actor", "g", req, nil, nil); !errors.Is(err, ErrRequest) || fixture.count.Load() != 1 || fixture.requests.Load() != 1 { + t.Fatal("uncertain worker replayed", err) + } + }) + } +} + +func TestLifetimeExitTimeoutCancellationAndProtocol(t *testing.T) { + channel := &exitChannel{block: true} + started := time.Now() + if err := observeLifetimeExit(t.Context(), channel, io.ErrUnexpectedEOF); err != io.ErrUnexpectedEOF || time.Since(started) > 2*time.Second { + t.Fatal("unbounded or changed EOF", err) + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + channel.block = false + channel.code = 211 + channel.known = true + if err := observeLifetimeExit(ctx, channel, io.EOF); err != io.EOF { + t.Fatal("cancelled observation", err) + } + before := channel.calls.Load() + if err := observeLifetimeExit(t.Context(), channel, ErrProtocol); err != ErrProtocol || channel.calls.Load() != before { + t.Fatal("non EOF adopted exit", err) + } + if err := observeLifetimeExit(t.Context(), fixtureChannel{}, io.EOF); !errors.Is(err, io.EOF) { + t.Fatal(err) + } +} + +func TestLifetimeExitCannotReplaceForeignWorkerProtocolFailure(t *testing.T) { + fixture := &fixtureOpener{handler: func(conn net.Conn, req Request) { + _ = writeControl(conn, Result{ID: req.ID, Worker: WorkerIdentity{PID: 999}, Status: "ok"}) + }} + channel := &exitChannel{code: 214, known: true} + client := NewClient(&exitOpener{fixture: fixture, channel: channel}) + t.Cleanup(func() { _ = client.Close(); fixture.wg.Wait() }) + if _, err := client.Prepare(t.Context(), "target", "actor", "g"); err != nil { + t.Fatal(err) + } + req := Request{ID: "identity", Operation: "observe", Path: `C:\synthetic`, DeadlineUTC: time.Now().Add(time.Second)} + if _, err := client.Execute(t.Context(), "target", "actor", "g", req, nil, nil); !errors.Is(err, ErrProtocol) || channel.calls.Load() != 0 || errors.Is(err, ErrLifetimePending) { + t.Fatal("exit replaced original identity failure", err) + } +} diff --git a/internal/guest/stream/native.cs b/internal/guest/stream/native.cs new file mode 100644 index 0000000..c974d02 --- /dev/null +++ b/internal/guest/stream/native.cs @@ -0,0 +1,117 @@ +namespace AMCStream { +using System; +using System.Collections.Generic; +using System.IO; +using System.Runtime.InteropServices; +using System.Security.AccessControl; +using System.Security.Cryptography; +using System.Security.Principal; +using Microsoft.Win32.SafeHandles; + + +internal static class Native { + [StructLayout(LayoutKind.Sequential)] internal struct Info { + public uint Attributes; public System.Runtime.InteropServices.ComTypes.FILETIME Creation,Access,Write; + public uint Volume,SizeHigh,SizeLow,Links,IndexHigh,IndexLow; + } + [DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern SafeFileHandle CreateFile(string path,uint access,uint share,IntPtr security,uint creation,uint flags,IntPtr template); + [DllImport("kernel32.dll",SetLastError=true)] static extern bool GetFileInformationByHandle(SafeFileHandle handle,out Info info); + [StructLayout(LayoutKind.Sequential)] struct FileIDInfo { public ulong Volume;[MarshalAs(UnmanagedType.ByValArray,SizeConst=16)]public byte[] ID; } + [DllImport("kernel32.dll",SetLastError=true)] static extern bool GetFileInformationByHandleEx(SafeFileHandle handle,int type,out FileIDInfo info,uint size); + static FileIDInfo FileID(SafeFileHandle handle){FileIDInfo info;if(!GetFileInformationByHandleEx(handle,18,out info,24))throw Failure.LastError("file_id_read");return info;} + [DllImport("kernel32.dll",SetLastError=true)] static extern bool SetFileInformationByHandle(SafeFileHandle handle,int type,byte[] data,uint size); + [DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern uint GetFileAttributes(string path); + [DllImport("advapi32.dll",SetLastError=true)] static extern bool GetKernelObjectSecurity(SafeFileHandle handle,uint requested,byte[] descriptor,uint length,out uint needed); + [DllImport("advapi32.dll")] static extern uint SetSecurityInfo(SafeFileHandle handle,int type,uint information,byte[] owner,IntPtr group,IntPtr dacl,IntPtr sacl); + internal static T CreateOwned(string expectedOwner,string currentOwner,byte[] owner,Func create,Func setOwner,Action dispose) { + if(String.IsNullOrEmpty(expectedOwner)||expectedOwner!=currentOwner||owner==null||owner.Length==0)throw new InvalidDataException(); + T original=create(); + try {uint error=setOwner(original,owner);if(error!=0)throw new NativeFailure("file_owner_set",error);return original;} + catch{dispose(original);throw;} + } + static uint SetOwner(SafeFileHandle handle,byte[] owner){ + return SetSecurityInfo(handle,1,1,owner,IntPtr.Zero,IntPtr.Zero,IntPtr.Zero); // SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION only + } + internal static string FullPath(string path) { + if(String.IsNullOrEmpty(path)||path.IndexOfAny(new char[]{'*','?','\0'})>=0||!Path.IsPathRooted(path)||path.StartsWith(@"\\")||path.IndexOf(':',2)>=0)throw new InvalidDataException(); + string full=Path.GetFullPath(path);if(full.Length>240||full.Length<4)throw new InvalidDataException();return full; + } + static SafeFileHandle Handle(string path,uint access,uint share,uint creation,uint flags=0,string stage="file_open") { + SafeFileHandle handle=null;try{using(var privilege=new SecurityPrivilege()){handle=CreateFile(path,access,share,IntPtr.Zero,creation,0x02200000|flags,IntPtr.Zero);if(handle.IsInvalid)throw Failure.LastError(stage);}}catch{if(handle!=null)handle.Dispose();throw;} // OPEN_REPARSE_POINT | BACKUP_SEMANTICS + if(handle.IsInvalid){handle.Dispose();throw new IOException("file_unavailable");} + try {Info info=Metadata(handle);if((info.Attributes&0x400)!=0)throw new IOException("reparse_point");return handle;}catch{handle.Dispose();throw;} + } + static Info Metadata(SafeFileHandle handle){Info info;if(!GetFileInformationByHandle(handle,out info))throw Failure.LastError("metadata_read");return info;} + static long Birth(Info info){return ((long)info.Creation.dwHighDateTime<<32)|(uint)info.Creation.dwLowDateTime;} + + static RawSecurityDescriptor Security(SafeFileHandle handle) { + // Require all SDDL sections, including SACL. Missing privilege is a + // preserved/inaccessible result, never a weakened identity fallback. + uint needed;GetKernelObjectSecurity(handle,15,null,0,out needed); + if(needed==0)throw Failure.LastError("security_size"); + if(needed>65536)throw new NativeFailure("security_size",0); + byte[] bytes=new byte[needed];if(!GetKernelObjectSecurity(handle,15,bytes,needed,out needed))throw Failure.LastError("security_read"); + try{return new RawSecurityDescriptor(bytes,0);}catch{throw new NativeFailure("security_parse",0);} + } + static string SecurityHash(RawSecurityDescriptor descriptor) { + using(var sha=SHA256.Create())return Worker.Hex(sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(descriptor.GetSddlForm(AccessControlSections.All)))); + } + internal sealed class Parents : IDisposable { + internal readonly List Handles=new List();internal string Path; + internal Dictionary Identity(){var handle=Handles[Handles.Count-1];var info=Metadata(handle);var id=FileID(handle);var security=Security(handle); + return new Dictionary{{"path",Path},{"volume",id.Volume},{"fileId",Worker.Hex(id.ID)},{"creationTime",Birth(info)},{"owner",security.Owner.Value},{"sddlHash",SecurityHash(security)}}; + } + public void Dispose(){foreach(var h in Handles)h.Dispose();} + } + internal static Parents LockParents(string path) { + var parents=new Parents();try { + string root=Path.GetPathRoot(path);string parent=System.IO.Path.GetDirectoryName(path); + parents.Path=parent;string current=root; + parents.Handles.Add(Handle(current,0x01020080,1,3,0,"ancestor_open")); // full security read; sharing read only excludes rename/write + string remainder=parent.Substring(root.Length); + foreach(string component in remainder.Split(new char[]{'\\'},StringSplitOptions.RemoveEmptyEntries)) { + current=System.IO.Path.Combine(current,component);parents.Handles.Add(Handle(current,0x01020080,1,3,0,current==parent?"workspace_open":"ancestor_open")); + } + return parents; + }catch{parents.Dispose();throw;} + } + internal sealed class File : IDisposable { + internal FileStream Stream;internal SafeFileHandle Handle; + internal File(SafeFileHandle handle,bool writable){Handle=handle;try{Stream=new FileStream(handle,writable?FileAccess.ReadWrite:FileAccess.Read,65536,false);}catch{handle.Dispose();throw;}} + internal Dictionary Identity(string path,Parents parents,string knownHash=null) { + var info=Metadata(Handle);var id=FileID(Handle);if(info.Links!=1||(info.Attributes&0x10)!=0)throw new IOException("nonexclusive_file"); + var security=Security(Handle);long position=Stream.Position;Stream.Position=0;string hash; + try{if(knownHash!=null)hash=knownHash;else using(var sha=SHA256.Create())hash=Worker.Hex(sha.ComputeHash(Stream));}finally{Stream.Position=position;} + var rootHandle=parents.Handles[parents.Handles.Count-1];var root=Metadata(rootHandle);var rootID=FileID(rootHandle);var rootSecurity=Security(parents.Handles[parents.Handles.Count-1]); + return new Dictionary{{"path",path},{"volume",id.Volume},{"fileId",Worker.Hex(id.ID)},{"creationTime",Birth(info)}, + {"owner",security.Owner.Value},{"sddlHash",SecurityHash(security)},{"size",Stream.Length},{"sha256",hash}, + {"rootPath",parents.Path},{"rootVolume",rootID.Volume},{"rootFileId",Worker.Hex(rootID.ID)},{"rootCreationTime",Birth(root)}, + {"rootOwner",rootSecurity.Owner.Value},{"rootSDDLHash",SecurityHash(rootSecurity)}}; + } + internal void Delete(){Disposition(true);} + // ON_CLOSE | DO_NOT_DELETE clears atomic FILE_FLAG_DELETE_ON_CLOSE. + // Legacy FileDispositionInfo cannot clear a create-time delete-on-close flag. + internal void Commit(){byte[] flags=BitConverter.GetBytes(8u);if(!SetFileInformationByHandle(Handle,21,flags,4))throw Failure.LastError("file_commit");} + void Disposition(bool delete){byte[] disposition=new byte[]{delete?(byte)1:(byte)0};if(!SetFileInformationByHandle(Handle,4,disposition,1))throw Failure.LastError("file_delete");} + public void Dispose(){Stream.Dispose();} + } + internal static File Create(string path,string expectedOwner){ + // The kernel owns cleanup from the instant CREATE_NEW succeeds, including + // metadata validation, FileStream construction failure and abrupt EOF kill. + // Unsupported extended commit never falls back to a gap-prone legacy path. + using(var identity=WindowsIdentity.GetCurrent()) { + var sid=identity.User;byte[] owner=new byte[sid.BinaryLength];sid.GetBinaryForm(owner,0); + var handle=CreateOwned(expectedOwner,sid.Value,owner, + delegate{return Handle(path,0xC10B0000,0,1,0x04000000);},SetOwner,delegate(SafeFileHandle original){original.Dispose();}); // adds WRITE_OWNER only + return new File(handle,true); + } + } + internal static File Open(string path){return new File(Handle(path,0x81030000,0,3),false);} + internal static bool Same(Dictionary expected,Dictionary actual) { + if(expected.Count!=actual.Count)return false; + foreach(var pair in actual){object value;if(!expected.TryGetValue(pair.Key,out value)||Convert.ToString(value,System.Globalization.CultureInfo.InvariantCulture)!=Convert.ToString(pair.Value,System.Globalization.CultureInfo.InvariantCulture))return false;} + return true; + } + internal static bool Absent(string path){if(GetFileAttributes(path)!=UInt32.MaxValue)return false;int error=Marshal.GetLastWin32Error();return error==2||error==3;} +} +} diff --git a/internal/guest/stream/pool.go b/internal/guest/stream/pool.go new file mode 100644 index 0000000..71b8751 --- /dev/null +++ b/internal/guest/stream/pool.go @@ -0,0 +1,64 @@ +package stream + +import ( + "context" + "github.com/Horcag/agent-machine-control/internal/domain" +) + +// acquire serializes a worker's full request lifecycle. Waiters on an evicted +// worker fail rather than creating a replacement under an old admission. +func (c *Client) acquire(ctx context.Context, key poolKey, create bool) (*worker, func(), error) { + if err := ctx.Err(); err != nil { + return nil, nil, err + } + c.mu.Lock() + if c.closed { + c.mu.Unlock() + return nil, nil, ErrClosed + } + w := c.workers[key] + if w == nil { + if !create || len(c.workers) >= MaxWorkers { + c.mu.Unlock() + return nil, nil, ErrRequest + } + w = &worker{lane: make(chan struct{}, 1)} + c.workers[key] = w + } + c.mu.Unlock() + select { + case w.lane <- struct{}{}: + case <-ctx.Done(): + return nil, nil, ctx.Err() + } + release := func() { <-w.lane } + c.mu.Lock() + valid := !c.closed && c.workers[key] == w + c.mu.Unlock() + if !valid { + release() + return nil, nil, ErrClosed + } + if err := ctx.Err(); err != nil { + release() + return nil, nil, err + } + return w, release, nil +} + +// Retire revokes reuse of one exact worker. Local channel closure is not proof +// of remote process absence; the app retains its native cleanup obligation. +func (c *Client) Retire(target domain.MachineRef, actor, generation string) error { + key := poolKey{target, actor, generation} + c.mu.Lock() + defer c.mu.Unlock() + w := c.workers[key] + if w == nil { + return nil + } + delete(c.workers, key) + if w.channel != nil { + return w.channel.Close() + } + return nil +} diff --git a/internal/guest/stream/prepare.go b/internal/guest/stream/prepare.go new file mode 100644 index 0000000..9b085ce --- /dev/null +++ b/internal/guest/stream/prepare.go @@ -0,0 +1,78 @@ +package stream + +import ( + "context" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" +) + +// Prepare starts a worker and observes its native identity without performing a +// business operation. The app must durably admit this identity before Execute. +func (c *Client) Prepare(ctx context.Context, target domain.MachineRef, actor, generation string) (WorkerIdentity, error) { + if target == "" || actor == "" || generation == "" || c.opener == nil { + return WorkerIdentity{}, ErrRequest + } + ctx, cancel := context.WithTimeout(ctx, 45*time.Second) + defer cancel() + key := poolKey{target, actor, generation} + w, release, err := c.acquire(ctx, key, true) + if err != nil { + return WorkerIdentity{}, err + } + defer release() + + if w.channel != nil { + return w.identity, nil + } + ch, identity, err := c.openWorker(ctx, target) + if err != nil { + c.evict(key, w) + return WorkerIdentity{}, err + } + + c.mu.Lock() + defer c.mu.Unlock() + if c.closed || c.workers[key] != w { + _ = ch.Close() + return WorkerIdentity{}, ErrClosed + } + w.channel = ch + w.identity = identity + return identity, nil +} + +func (c *Client) openWorker(ctx context.Context, target domain.MachineRef) (guestssh.Duplex, WorkerIdentity, error) { + started := time.Now() + ch, err := c.opener.OpenDuplex(ctx, target, StartupCommand) + failure := StartupFailure{Stage: "open", OpenMillis: time.Since(started).Milliseconds()} + if err != nil { + return nil, WorkerIdentity{}, startupFailure(ctx, nil, failure, err) + } + stop := context.AfterFunc(ctx, func() { _ = ch.Close() }) + defer stop() + var identity WorkerIdentity + failure.Stage = "source_write" + started = time.Now() + err = writeSource(ch) + failure.SourceWriteMillis = time.Since(started).Milliseconds() + if err == nil { + failure.Stage = "hello" + started = time.Now() + err = readControl(ch, &identity) + failure.HelloMillis = time.Since(started).Milliseconds() + } + if err == nil && (identity.PID <= 0 || identity.BirthTicks <= 0 || identity.Image == "" || identity.SID == "") { + err = ErrProtocol + } + if err == nil { + err = ctx.Err() + } + if err != nil { + err = startupFailure(ctx, ch, failure, err) + _ = ch.Close() + return nil, WorkerIdentity{}, err + } + return ch, identity, nil +} diff --git a/internal/guest/stream/remote_exit_test.go b/internal/guest/stream/remote_exit_test.go new file mode 100644 index 0000000..79d5a2d --- /dev/null +++ b/internal/guest/stream/remote_exit_test.go @@ -0,0 +1,33 @@ +package stream + +import ( + "errors" + "io" + "testing" +) + +func TestRemoteExitPreservesEOFAndOnlyChannelNumericStatus(t *testing.T) { + for _, code := range []int{0, 1, 215, 255, 0xc0000005} { + channel := &exitChannel{code: code, known: true} + err := observeLifetimeExit(t.Context(), channel, io.EOF) + exit, ok := errors.AsType[*RemoteExit](err) + if !ok || !exit.Valid() || exit.Code != code || !errors.Is(err, io.EOF) { + t.Fatalf("numeric status %d lost or EOF changed: %v", code, err) + } + } + for _, code := range []int{-1, 0x100000000} { + if (&RemoteExit{Code: code}).Valid() { + t.Fatal("invalid remote status accepted") + } + err := observeLifetimeExit(t.Context(), &exitChannel{code: code, known: true}, io.EOF) + if _, ok := errors.AsType[*RemoteExit](err); ok || err != io.EOF { + t.Fatal("invalid remote status recorded", err) + } + } + for _, channel := range []*exitChannel{{code: 1, known: false}, {code: 211, known: true}} { + err := observeLifetimeExit(t.Context(), channel, io.EOF) + if _, ok := errors.AsType[*RemoteExit](err); ok { + t.Fatal("missing status or known lifetime marker replaced", err) + } + } +} diff --git a/internal/guest/stream/security.cs b/internal/guest/stream/security.cs new file mode 100644 index 0000000..01b4c79 --- /dev/null +++ b/internal/guest/stream/security.cs @@ -0,0 +1,63 @@ +namespace AMCStream { +using System; +using System.IO; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; +internal sealed class NativeFailure : IOException { + internal readonly string Stage;internal readonly uint Code; + internal NativeFailure(string stage,uint code){Stage=stage;Code=code;} +} +internal static class Failure { + // Exit diagnostics are bounded metadata, never a successful protocol reply. + internal static uint TimerExit(Exception error,string stage) { + var native=error as NativeFailure;uint code=native==null?0u:native.Code; + if(native!=null)stage=native.Stage; + uint index; + switch(stage) { + case "runspace_open":index=1;break;case "request":index=2;break; + case "observe":index=3;break;case "put":index=4;break;case "get":index=5;break; + case "exec":index=6;break;case "remove_owned":index=7;break; + case "security_token":index=8;break;case "security_lookup":index=9;break; + case "security_enable":index=10;break;case "security_restore":index=11;break; + case "ancestor_open":index=12;break;case "workspace_open":index=13;break; + case "file_open":index=14;break;case "metadata_read":index=15;break; + case "file_id_read":index=16;break;case "security_size":index=17;break; + case "security_read":index=18;break;case "security_parse":index=19;break; + case "file_commit":index=20;break;case "file_delete":index=21;break; + case "control_eof":index=22;break;case "control_read":index=23;break; + case "control_header":index=24;break;case "control_decode":index=25;break; + case "file_owner_set":index=26;break; + default:return Lifetime.FailureTimerExit; + } + return code>65535?Lifetime.FailureTimerExit:0x01000000u|(index<<16)|code; + } + internal static NativeFailure LastError(string stage){return new NativeFailure(stage,unchecked((uint)Marshal.GetLastWin32Error()));} + internal static Dictionary Metadata(Exception error,string stage){ + var native=error as NativeFailure; + return new Dictionary{{"stage",native==null?stage:native.Stage},{"nativeError",native==null?0u:native.Code}}; + } +} +internal sealed class SecurityPrivilege : IDisposable { + [StructLayout(LayoutKind.Sequential)] struct Luid { public uint Low;public int High; } + [StructLayout(LayoutKind.Sequential)] struct Privileges {public uint Count;public Luid ID;public uint Attributes;} + [DllImport("kernel32.dll")] static extern IntPtr GetCurrentProcess(); + [DllImport("advapi32.dll",SetLastError=true)] static extern bool OpenProcessToken(IntPtr process,uint access,out SafeFileHandle token); + [DllImport("advapi32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern bool LookupPrivilegeValue(string system,string name,out Luid id); + [DllImport("advapi32.dll",SetLastError=true)] static extern bool AdjustTokenPrivileges(SafeFileHandle token,bool disable,ref Privileges state,uint length,out Privileges previous,out uint needed); + SafeFileHandle token;Privileges previous; + internal SecurityPrivilege() { + if(!OpenProcessToken(GetCurrentProcess(),0x28,out token))throw Failure.LastError("security_token"); + try { + Luid id;if(!LookupPrivilegeValue(null,"SeSecurityPrivilege",out id))throw Failure.LastError("security_lookup"); + var state=new Privileges{Count=1,ID=id,Attributes=2};uint needed; + if(!AdjustTokenPrivileges(token,false,ref state,(uint)Marshal.SizeOf(typeof(Privileges)),out previous,out needed)||Marshal.GetLastWin32Error()!=0)throw Failure.LastError("security_enable"); + } catch{token.Dispose();throw;} + } + public void Dispose(){ + try {Privileges ignored;uint needed; + if(!AdjustTokenPrivileges(token,false,ref previous,(uint)Marshal.SizeOf(typeof(Privileges)),out ignored,out needed)||Marshal.GetLastWin32Error()!=0)throw Failure.LastError("security_restore"); + } finally {token.Dispose();} + } +} +} diff --git a/internal/guest/stream/startup_failure.go b/internal/guest/stream/startup_failure.go new file mode 100644 index 0000000..4e7688e --- /dev/null +++ b/internal/guest/stream/startup_failure.go @@ -0,0 +1,111 @@ +package stream + +import ( + "context" + "errors" + "fmt" + "io" + + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" +) + +// StartupFailure contains closed local stage/category, optional closed remote +// observation and local elapsed milliseconds. Remote markers and exits never +// establish worker identity or native absence. +// Open includes SSH connection setup and command start, an opaque opener boundary. +type StartupFailure struct { + Stage, Category string + RemoteStage string + OpenMillis, SourceWriteMillis, HelloMillis int64 + ExitObserved bool + ExitCode int +} + +func (f *StartupFailure) Valid() bool { + if f == nil || f.OpenMillis < 0 || f.SourceWriteMillis < 0 || f.HelloMillis < 0 { + return false + } + if f.Stage != "open" && f.Stage != "source_write" && f.Stage != "hello" { + return false + } + if f.Category != "transport" && startupCategoryError(f.Category) == nil { + return false + } + if f.RemoteStage != "" && !guestssh.ValidStartupStage(f.RemoteStage) { + return false + } + return (!f.ExitObserved && f.ExitCode == 0) || (f.ExitObserved && (&RemoteExit{Code: f.ExitCode}).Valid()) +} + +func (f *StartupFailure) Error() string { + if !f.Valid() { + return "guest stream: invalid startup diagnostic" + } + text := fmt.Sprintf("guest-startup-failure:%s:%s:open_ms=%d:source_write_ms=%d:hello_ms=%d", f.Stage, f.Category, f.OpenMillis, f.SourceWriteMillis, f.HelloMillis) + if f.ExitObserved { + text += fmt.Sprintf(":exit=%d", f.ExitCode) + } + if f.RemoteStage != "" { + text += ":remote_stage=" + f.RemoteStage + } + return text +} + +func (f *StartupFailure) Unwrap() []error { + if !f.Valid() { + return nil + } + var result []error + if cause := startupCategoryError(f.Category); cause != nil { + result = append(result, cause) + } + if f.ExitObserved { + result = append(result, &RemoteExit{Code: f.ExitCode}) + } + return result +} + +func startupCategoryError(category string) error { + switch category { + case "deadline": + return context.DeadlineExceeded + case "cancelled": + return context.Canceled + case "eof": + return io.EOF + case "unexpected_eof": + return io.ErrUnexpectedEOF + case "short_write": + return io.ErrShortWrite + case "closed_pipe": + return io.ErrClosedPipe + case "protocol": + return ErrProtocol + default: + return nil + } +} + +func startupFailure(ctx context.Context, channel io.ReadWriteCloser, failure StartupFailure, err error) error { + failure.Category = "transport" + if observer, ok := channel.(interface{ StartupStage() string }); ok { + if stage := observer.StartupStage(); guestssh.ValidStartupStage(stage) { + failure.RemoteStage = stage + } + } + if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.ErrClosedPipe) { + if code, known := observeExitStatus(ctx, channel); known && (&RemoteExit{Code: code}).Valid() { + failure.ExitObserved, failure.ExitCode = true, code + } + } + if ctx.Err() != nil { + err = ctx.Err() + } + for _, category := range []string{"deadline", "cancelled", "eof", "unexpected_eof", "short_write", "closed_pipe", "protocol"} { + if errors.Is(err, startupCategoryError(category)) { + failure.Category = category + break + } + } + return &failure +} diff --git a/internal/guest/stream/startup_failure_test.go b/internal/guest/stream/startup_failure_test.go new file mode 100644 index 0000000..ba688b7 --- /dev/null +++ b/internal/guest/stream/startup_failure_test.go @@ -0,0 +1,172 @@ +package stream + +import ( + "bytes" + "context" + "errors" + "io" + "strings" + "sync" + "testing" + + "github.com/Horcag/agent-machine-control/internal/domain" + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" +) + +type startupChannel struct { + io.Reader + writeErr error + code int + known bool + closed bool + cancel context.CancelFunc +} + +func (c *startupChannel) Write(p []byte) (int, error) { + if c.writeErr != nil { + return 0, c.writeErr + } + return len(p), nil +} +func (c *startupChannel) Close() error { c.closed = true; return nil } +func (c *startupChannel) Wait() error { panic("startup must not wait for an unbounded remote exit") } +func (c *startupChannel) ExitStatus(context.Context) (int, bool) { + if c.cancel != nil { + c.cancel() + } + return c.code, c.known +} + +type startupOpener struct { + channel guestssh.Duplex + err error + calls int +} + +func (o *startupOpener) OpenDuplex(context.Context, domain.MachineRef, string) (guestssh.Duplex, error) { + o.calls++ + return o.channel, o.err +} + +func TestStartupFailuresAreSanitizedAndNeverReplayOrAdmitIdentity(t *testing.T) { + for _, tc := range []struct { + name, stage, category string + openErr, writeErr error + hello []byte + code int + known bool + }{ + {name: "open", stage: "open", category: "transport", openErr: errors.New("private credentials and path")}, + {name: "source", stage: "source_write", category: "closed_pipe", writeErr: io.ErrClosedPipe, code: 1, known: true}, + {name: "hello_exit", stage: "hello", category: "eof", code: 214, known: true}, + {name: "hello_unknown_exit", stage: "hello", category: "eof"}, + {name: "malformed_hello", stage: "hello", category: "protocol", hello: []byte{0, 0, 0, 0}}, + } { + t.Run(tc.name, func(t *testing.T) { + channel := &startupChannel{Reader: bytes.NewReader(tc.hello), writeErr: tc.writeErr, code: tc.code, known: tc.known} + opener := &startupOpener{channel: channel, err: tc.openErr} + client := NewClient(opener) + defer client.Close() + identity, err := client.Prepare(t.Context(), "synthetic", "actor", "generation") + failure, ok := errors.AsType[*StartupFailure](err) + if !ok || !failure.Valid() || failure.Stage != tc.stage || failure.Category != tc.category || identity != (WorkerIdentity{}) || opener.calls != 1 || strings.Contains(err.Error(), "private") { + t.Fatalf("unsafe startup: identity=%+v err=%v calls=%d", identity, err, opener.calls) + } + if failure.ExitObserved != tc.known || failure.ExitCode != tc.code || channel.closed != (tc.openErr == nil) { + t.Fatalf("exit/owned close lost: %+v closed=%v", failure, channel.closed) + } + if _, err := client.Execute(t.Context(), "synthetic", "actor", "generation", request(), nil, nil); !errors.Is(err, ErrRequest) || opener.calls != 1 { + t.Fatalf("failed startup adopted/replayed: %v", err) + } + }) + } +} + +type cancelledStartupChannel struct { + started, closed chan struct{} + once sync.Once +} + +func (c *cancelledStartupChannel) Write(p []byte) (int, error) { return len(p), nil } +func (c *cancelledStartupChannel) Read([]byte) (int, error) { + close(c.started) + <-c.closed + return 0, io.EOF +} +func (c *cancelledStartupChannel) Close() error { c.once.Do(func() { close(c.closed) }); return nil } +func (c *cancelledStartupChannel) Wait() error { panic("unbounded wait") } + +func TestStartupHelloCancellationKeepsCategoryAndClosesOwnedChannel(t *testing.T) { + channel := &cancelledStartupChannel{started: make(chan struct{}), closed: make(chan struct{})} + client := NewClient(&startupOpener{channel: channel}) + defer client.Close() + ctx, cancel := context.WithCancel(t.Context()) + done := make(chan error, 1) + go func() { _, err := client.Prepare(ctx, "synthetic", "actor", "generation"); done <- err }() + <-channel.started + cancel() + err := <-done + failure, ok := errors.AsType[*StartupFailure](err) + if !ok || failure.Stage != "hello" || failure.Category != "cancelled" || failure.ExitObserved || !errors.Is(err, context.Canceled) { + t.Fatal("cancellation diagnostic lost", err) + } +} + +func TestStartupFailureRejectsUnsafeFieldsAndInvalidRemoteStatus(t *testing.T) { + for _, failure := range []*StartupFailure{ + nil, {Stage: "private", Category: "eof"}, {Stage: "hello", Category: "private"}, + {Stage: "hello", Category: "eof", OpenMillis: -1}, {Stage: "hello", Category: "eof", ExitObserved: true, ExitCode: -1}, + } { + if failure.Valid() { + t.Fatal("unsafe startup accepted", failure) + } + } + channel := &startupChannel{code: -1, known: true} + failure := startupFailure(t.Context(), channel, StartupFailure{Stage: "hello"}, io.EOF).(*StartupFailure) + if failure.ExitObserved || !failure.Valid() || !errors.Is(failure, io.EOF) { + t.Fatal(failure) + } +} + +func TestStartupCancellationDuringExitObservationTakesPrecedence(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + channel := &startupChannel{code: 1, known: true, cancel: cancel} + err := startupFailure(ctx, channel, StartupFailure{Stage: "hello"}, io.EOF) + failure := err.(*StartupFailure) + if failure.Category != "cancelled" || failure.ExitObserved || !errors.Is(err, context.Canceled) { + t.Fatal("exit observation hid cancellation", err) + } +} + +type observedStartupChannel struct { + *startupChannel + stage string +} + +func (c *observedStartupChannel) StartupStage() string { return c.stage } + +func TestStartupFailureRemoteStageIsClosedObservationOnly(t *testing.T) { + for _, stage := range []string{"before_compile", "private/path/data"} { + channel := &observedStartupChannel{startupChannel: &startupChannel{Reader: bytes.NewReader(nil)}, stage: stage} + opener := &startupOpener{channel: channel} + client := NewClient(opener) + identity, err := client.Prepare(t.Context(), "synthetic", "actor", "generation") + _ = client.Close() + failure, ok := errors.AsType[*StartupFailure](err) + if !ok || !failure.Valid() || identity != (WorkerIdentity{}) || opener.calls != 1 { + t.Fatal(identity, err) + } + if stage == "before_compile" { + if failure.RemoteStage != stage || !strings.Contains(failure.Error(), ":remote_stage=before_compile") { + t.Fatal(failure) + } + } else if failure.RemoteStage != "" || strings.Contains(failure.Error(), "private") { + t.Fatal("unsafe remote stage retained", failure) + } + } + invalid := &StartupFailure{Stage: "hello", Category: "deadline", RemoteStage: "private/path/data"} + if invalid.Valid() || strings.Contains(invalid.Error(), "private") || invalid.Unwrap() != nil { + t.Fatal("invalid field escaped", invalid) + } +} diff --git a/internal/guest/stream/startup_pipe.ps1 b/internal/guest/stream/startup_pipe.ps1 new file mode 100644 index 0000000..8ed98da --- /dev/null +++ b/internal/guest/stream/startup_pipe.ps1 @@ -0,0 +1,71 @@ +# The inherited pipe may require overlapped I/O before C# can be compiled. +# Define only these fixed native declarations; no helper compiler is started. +$ErrorActionPreference='Stop'; +$assembly=[AppDomain]::CurrentDomain.DefineDynamicAssembly([Reflection.AssemblyName]::new('AMCBootstrapPipe'),[Reflection.Emit.AssemblyBuilderAccess]::Run); +$type=$assembly.DefineDynamicModule('n').DefineType('AMCBootstrapPipe',[Reflection.TypeAttributes]::Public); +$spec=@(@('GetStdHandle','kernel32.dll',[IntPtr],[type[]]@([int])),@('GetFileType','kernel32.dll',[uint32],[type[]]@([IntPtr])),@('NtQueryInformationFile','ntdll.dll',[int],[type[]]@([IntPtr],[IntPtr],[IntPtr],[uint32],[int]))); +foreach($s in $spec){ + $method=$type.DefinePInvokeMethod($s[0],$s[1],([Reflection.MethodAttributes]::Public-bor[Reflection.MethodAttributes]::Static-bor[Reflection.MethodAttributes]::PinvokeImpl),[Reflection.CallingConventions]::Standard,$s[2],$s[3],[Runtime.InteropServices.CallingConvention]::Winapi,[Runtime.InteropServices.CharSet]::Unicode); + $method.SetImplementationFlags($method.GetMethodImplementationFlags()-bor[Reflection.MethodImplAttributes]::PreserveSig); +}; +$Native=$type.CreateType(); + +function Get-AMCAsyncMode([int]$status,[int]$ioStatus,[long]$information,[int]$mode){ + if($status-eq0x103){throw [IO.IOException]::new('214')}; + if($status-ne0-or$ioStatus-ne0){throw [IO.IOException]::new('211')}; + if($information-ne4){throw [IO.IOException]::new('212')}; + $synchronous=$mode-band0x30; + if($synchronous-eq0x30){throw [IO.IOException]::new('211')}; + return ($synchronous-eq0); +}; + +function Open-AMCStandardStream([int]$id,[IO.FileAccess]$access){ + $handle=$Native::GetStdHandle($id); + if($handle-eq[IntPtr]::Zero-or$handle-eq[IntPtr]::new(-1)-or$Native::GetFileType($handle)-ne3){throw [IO.IOException]::new('211')}; + $iosb=[Runtime.InteropServices.Marshal]::AllocHGlobal(2*[IntPtr]::Size); + $modeBuffer=[IntPtr]::Zero;$status=-1; + try{ + $modeBuffer=[Runtime.InteropServices.Marshal]::AllocHGlobal(4); + for($offset=0;$offset-lt2*[IntPtr]::Size;$offset++){[Runtime.InteropServices.Marshal]::WriteByte($iosb,$offset,0)}; + [Runtime.InteropServices.Marshal]::WriteInt32($modeBuffer,0); + $status=$Native::NtQueryInformationFile($handle,$iosb,$modeBuffer,4,16); + if($status-eq0x103){throw [IO.IOException]::new('214')}; + $ioStatus=[Runtime.InteropServices.Marshal]::ReadInt32($iosb); + $information=[Runtime.InteropServices.Marshal]::ReadIntPtr($iosb,[IntPtr]::Size).ToInt64(); + $mode=[Runtime.InteropServices.Marshal]::ReadInt32($modeBuffer); + $async=Get-AMCAsyncMode $status $ioStatus $information $mode; + }finally{ + # Pending native I/O can still own these buffers; process exit reclaims them. + if($status-ne0x103){ + if($modeBuffer-ne[IntPtr]::Zero){[Runtime.InteropServices.Marshal]::FreeHGlobal($modeBuffer)}; + [Runtime.InteropServices.Marshal]::FreeHGlobal($iosb); + }; + }; + $borrowed=[Microsoft.Win32.SafeHandles.SafeFileHandle]::new($handle,$false); + try{return [IO.FileStream]::new($borrowed,$access,65536,$async)}catch{$borrowed.Dispose();throw}; +}; + +function Read-AMCSource([IO.Stream]$i){ + [Console]::Error.WriteLine('AMC_STARTUP_V1:1:before_header_read'); + $h=New-Object byte[] 4;$n=0; + while($n-lt4){$r=$i.Read($h,$n,4-$n);if($r-eq0){exit 1};$n+=$r}; + [Array]::Reverse($h);$l=[BitConverter]::ToUInt32($h,0); + if($l-gt262144){exit 1}; + [Console]::Error.WriteLine('AMC_STARTUP_V1:2:before_source_read'); + $b=New-Object byte[] $l;$n=0; + while($n-lt$l){$r=$i.Read($b,$n,$l-$n);if($r-eq0){exit 1};$n+=$r}; + [Console]::Error.WriteLine('AMC_STARTUP_V1:3:before_compile'); + return ,$b; +}; +# Bind each inherited handle once. The source reader and worker share the same +# stream; reopening an overlapped handle can fail its completion-port binding. +$i=Open-AMCStandardStream -10 ([IO.FileAccess]::Read); +try{ + $b=Read-AMCSource $i; + Add-Type -TypeDefinition ([Text.Encoding]::UTF8.GetString($b)) -ReferencedAssemblies @('System.dll','System.Core.dll','System.Web.Extensions.dll',[Management.Automation.PowerShell].Assembly.Location); + $o=Open-AMCStandardStream -11 ([IO.FileAccess]::Write); + try{ + [Console]::Error.WriteLine('AMC_STARTUP_V1:4:before_worker_run'); + [AMCStream.Worker]::Run($i,$o); + }finally{$o.Dispose()}; +}finally{$i.Dispose()}; diff --git a/internal/guest/stream/types.go b/internal/guest/stream/types.go new file mode 100644 index 0000000..be81cff --- /dev/null +++ b/internal/guest/stream/types.go @@ -0,0 +1,91 @@ +// Package stream transports whole guest operations over an enrolled binary SSH +// worker. Admission and ownership-reference authorization remain with the app. +package stream + +import ( + "context" + "errors" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" + guestssh "github.com/Horcag/agent-machine-control/internal/guest/ssh" +) + +const MaxControl = 256 * 1024 +const BufferSize = 64 * 1024 +const MaxManifest = 128 +const MaxScriptSize = 16 * 1024 * 1024 +const MaxCommandOutput = 16 * 1024 * 1024 +const MaxWorkers = 16 +const MaxTransferSize int64 = 1 << 40 + +var ErrProtocol = errors.New("guest stream: invalid or incomplete protocol") +var ErrIntegrity = errors.New("guest stream: size or digest mismatch") +var ErrRequest = errors.New("guest stream: invalid request") +var ErrClosed = errors.New("guest stream: client closed") + +// DuplexOpener accepts server-owned targets only, never client endpoints. +type DuplexOpener interface { + OpenDuplex(context.Context, domain.MachineRef, string) (guestssh.Duplex, error) +} + +type Request struct { + ID string `json:"id"` + Operation string `json:"operation"` + DeadlineUTC time.Time `json:"deadlineUtc"` + Path string `json:"path,omitempty"` + Size int64 `json:"size"` + SHA256 string `json:"sha256,omitempty"` + MaxOutput int64 `json:"maxOutput"` + Owned []FileIdentity `json:"owned,omitempty"` + ExpectedRoot *RootIdentity `json:"expectedRoot,omitempty"` +} + +type RootIdentity struct { + Path string `json:"path"` + Volume uint64 `json:"volume"` + FileID string `json:"fileId"` + CreationTime int64 `json:"creationTime"` + Owner string `json:"owner"` + SDDLHash string `json:"sddlHash"` +} +type FileIdentity struct { + Path string `json:"path"` + Volume uint64 `json:"volume"` + FileID string `json:"fileId"` + CreationTime int64 `json:"creationTime"` + Owner string `json:"owner"` + SDDLHash string `json:"sddlHash"` + Size int64 `json:"size"` + SHA256 string `json:"sha256"` + RootPath string `json:"rootPath"` + RootVolume uint64 `json:"rootVolume"` + RootFileID string `json:"rootFileId"` + RootCreationTime int64 `json:"rootCreationTime"` + RootOwner string `json:"rootOwner"` + RootSDDLHash string `json:"rootSDDLHash"` +} +type WorkerIdentity struct { + PID int `json:"pid"` + BirthTicks int64 `json:"birthTicks"` + Image string `json:"image"` + SID string `json:"sid"` +} +type Removal struct { + Path string `json:"path"` + Status string `json:"status"` + Absent bool `json:"absent"` +} +type Result struct { + ID string `json:"id"` + Status string `json:"status"` + ExitCode int `json:"exitCode"` + Size int64 `json:"size"` + SHA256 string `json:"sha256"` + Worker WorkerIdentity `json:"worker"` + Root *RootIdentity `json:"root,omitempty"` + Files []FileIdentity `json:"files,omitempty"` + Removed []Removal `json:"removed,omitempty"` + Failure *WorkerFailure `json:"failure,omitempty"` + ControlFailure *ControlFailure `json:"controlFailure,omitempty"` +} diff --git a/internal/guest/stream/validation.go b/internal/guest/stream/validation.go new file mode 100644 index 0000000..888ca89 --- /dev/null +++ b/internal/guest/stream/validation.go @@ -0,0 +1,54 @@ +package stream + +import ( + "time" +) + +func validate(req Request) error { + if req.ID == "" || len(req.ID) > 128 || req.DeadlineUTC.IsZero() || !req.DeadlineUTC.After(time.Now()) || req.Size < 0 || req.Size > MaxTransferSize || req.MaxOutput < 0 || req.MaxOutput > MaxTransferSize { + return ErrRequest + } + return validateOperation(req) +} +func validateOperation(req Request) error { + switch req.Operation { + case "put", "exec": + return validateInput(req) + case "observe", "get", "remove-owned": + return validateReferences(req) + default: + return ErrRequest + } +} +func validateInput(req Request) error { + if !validDigest(req.SHA256) { + return ErrRequest + } + if req.Operation == "put" && req.ExpectedRoot == nil { + return ErrRequest + } + if req.Operation == "exec" && (req.Size > MaxScriptSize || req.MaxOutput == 0 || req.MaxOutput > MaxCommandOutput) { + return ErrRequest + } + return nil +} +func validateReferences(req Request) error { + if req.Size != 0 { + return ErrRequest + } + switch req.Operation { + case "observe": + if req.MaxOutput != 0 || req.Path == "" { + return ErrRequest + } + case "get": + if req.MaxOutput == 0 || req.Path == "" || len(req.Owned) != 1 || req.Owned[0].Path != req.Path { + return ErrRequest + } + case "remove-owned": + if len(req.Owned) == 0 || len(req.Owned) > MaxManifest { + return ErrRequest + } + } + return nil +} diff --git a/internal/guest/stream/worker.cs b/internal/guest/stream/worker.cs new file mode 100644 index 0000000..7dbd35a --- /dev/null +++ b/internal/guest/stream/worker.cs @@ -0,0 +1,202 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Text; +using System.Security.Cryptography; +using System.Security.Principal; +using System.Web.Script.Serialization; +using System.Management.Automation; +using System.Management.Automation.Runspaces; + +namespace AMCStream { +public static class Worker { + static readonly JavaScriptSerializer JSON = new JavaScriptSerializer { MaxJsonLength = 262144 }; + internal const string EmptyDigest="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; + static Stream input, output; + static Dictionary identity; + static Runspace runspace; + static bool responseStarted; + static byte[] Exact(int count) {return Exact(input,count);} + static byte[] Exact(Stream from,int count) { + byte[] data = new byte[count]; int position = 0; + while (position < count) { int n = from.Read(data, position, count-position); if (n == 0) throw new EndOfStreamException(); position += n; } + return data; + } + internal sealed class ControlFailure : IOException { + internal readonly string Stage; + internal ControlFailure(string stage){Stage=stage;} + } + static byte[] ControlBytes(Stream from,int count) { + try{return Exact(from,count);} + catch(EndOfStreamException){throw new ControlFailure("control_eof");} + catch{throw new ControlFailure("control_read");} + } + internal static Dictionary ReadControl(Stream from,Func> decode) { + byte[] h=ControlBytes(from,4);Array.Reverse(h);uint n=BitConverter.ToUInt32(h,0); + if(n==0||n>262144)throw new ControlFailure("control_header"); + byte[] bytes=ControlBytes(from,(int)n); + try{var value=decode(new UTF8Encoding(false,true).GetString(bytes));if(value==null)throw new InvalidDataException();return value;} + catch{throw new ControlFailure("control_decode");} + } + static void Control(object value) { + byte[] body = Encoding.UTF8.GetBytes(JSON.Serialize(value)); if (body.Length > 262144) throw new InvalidDataException(); + responseStarted=true; + byte[] h = BitConverter.GetBytes((uint)body.Length); Array.Reverse(h); output.Write(h,0,4); output.Write(body,0,body.Length); output.Flush(); + } + static string Text(Dictionary value,string key) { object v; return value.TryGetValue(key,out v) ? Convert.ToString(v,System.Globalization.CultureInfo.InvariantCulture) : ""; } + static long Number(Dictionary value,string key) { object v; return value.TryGetValue(key,out v) ? Convert.ToInt64(v,System.Globalization.CultureInfo.InvariantCulture) : 0; } + static DateTime Deadline(Dictionary request) { return DateTime.Parse(Text(request,"deadlineUtc"),null,System.Globalization.DateTimeStyles.RoundtripKind).ToUniversalTime(); } + static void Fresh(DateTime deadline) { if (DateTime.UtcNow >= deadline) throw new TimeoutException(); } + static string Digest(byte[] bytes) { using(var hash=SHA256.Create()) return Hex(hash.ComputeHash(bytes)); } + internal static string Hex(byte[] bytes) { return BitConverter.ToString(bytes).Replace("-","").ToLowerInvariant(); } + static string Transfer(Stream from,Stream to,long size,DateTime deadline) { + byte[] buffer = new byte[65536]; using(var hash=SHA256.Create()) { + while(size>0) { Fresh(deadline); int n=from.Read(buffer,0,(int)Math.Min(buffer.Length,size)); if(n==0) throw new EndOfStreamException(); to.Write(buffer,0,n); hash.TransformBlock(buffer,0,n,null,0);size-=n; } + hash.TransformFinalBlock(new byte[0],0,0);return Hex(hash.Hash); + } + } + static Dictionary Result(string id,string status,long size,string hash,int exit) { + return new Dictionary{{"id",id},{"status",status},{"size",size},{"sha256",hash},{"exitCode",exit},{"worker",identity}}; + } + static void Empty(Dictionary result) { + result["size"]=0L;result["sha256"]=EmptyDigest;Control(result);Control(result); + } + public static void Run(Stream stdin,Stream stdout) { + using(var lifetime=new Lifetime.Guard())Lifetime.RunWorker(delegate{RunOwned(stdin,stdout,lifetime);},lifetime.Termination); + } + static void RunOwned(Stream stdin,Stream stdout,Lifetime.Guard lifetime) { + input=stdin;output=stdout;var process=Process.GetCurrentProcess(); + identity=new Dictionary{{"pid",process.Id},{"birthTicks",process.StartTime.ToUniversalTime().Ticks},{"image",process.MainModule.FileName},{"sid",WindowsIdentity.GetCurrent().User.Value}}; + Control(identity); + try { + // Bootstrap owns these mode-aware streams across source and hello. + // Keep their completion-port bindings for every operation frame. + for(;;) { + Dictionary request; + try{request=ReadControl(input,delegate(string text){return JSON.Deserialize>(text);});} + catch(ControlFailure error){ + lifetime.ArmFailure(DateTime.MaxValue,Failure.TimerExit(error,error.Stage)); + var fatal=Result("","failed",0,EmptyDigest,1); + fatal["controlFailure"]=new Dictionary{{"stage",error.Stage}}; + Empty(fatal);lifetime.HoldTerminal();return; + } + // Malformed or interrupted requests terminate the channel, never replay. + string id=Text(request,"id");if(id.Length==0||id.Length>128)throw new InvalidDataException(); + responseStarted=false;string stage="request";DateTime deadline=DateTime.MaxValue; + try { + deadline=Deadline(request);Fresh(deadline); + string operation=Text(request,"operation");long size=Number(request,"size"),limit=Number(request,"maxOutput"); + if(size<0||size>(1L<<40)||limit<0||limit>(1L<<40))throw new InvalidDataException(); + if(operation!="observe"&&operation!="put"&&operation!="get"&&operation!="exec"&&operation!="remove-owned")throw new InvalidDataException(); + stage="runspace_open"; + if(runspace==null){runspace=RunspaceFactory.CreateRunspace();runspace.Open();} + stage=operation=="remove-owned"?"remove_owned":operation; + if(operation=="observe") Observe(request,id,deadline); + else if(operation=="put") Put(request,id,size,deadline); + else if(operation=="get") Get(request,id,limit,deadline); + else if(operation=="exec") Exec(request,id,size,limit,deadline); + else Remove(request,id,deadline); + } catch(Exception error) { + // A partial response cannot safely become a new control frame. + if(responseStarted)throw; + // Closing our kill-on-close job immediately after Flush can + // race the SSH server's pipe drain. Bound the terminal wait + // before writing, including output backpressure and cleanup. + lifetime.ArmFailure(deadline,Failure.TimerExit(error,stage)); + var result=Result(id,"failed",0,EmptyDigest,1); + result["failure"]=Failure.Metadata(error,stage);Empty(result);Lifetime.DrainTerminal(input);return; + } + } + } finally {if(runspace!=null)runspace.Dispose();} + } + static void Observe(Dictionary request,string id,DateTime deadline) { + string path=Native.FullPath(Text(request,"path")); + using(var roots=Native.LockParents(Path.Combine(path,".amc-observe-placeholder"))) { + Fresh(deadline);var root=roots.Identity();Fresh(deadline); + var result=Result(id,"ok",0,EmptyDigest,0);result["root"]=root;Empty(result); + } + } + static void Put(Dictionary request,string id,long size,DateTime deadline) { + string path=Native.FullPath(Text(request,"path")); + using(var roots=Native.LockParents(path)) { + Fresh(deadline); + var root=roots.Identity();if(!Native.Same((Dictionary)request["expectedRoot"],root))throw new InvalidDataException(); + // CREATE_NEW refuses replacement. Incomplete bytes remain exclusively task-owned + // and are removed by their held handle on every unsuccessful commit. + using(var file=Native.Create(path,(string)root["owner"])) { + string hash=Transfer(input,file.Stream,size,deadline); + if(input.ReadByte()!=1||hash!=Text(request,"sha256")) throw new InvalidDataException(); + Fresh(deadline);file.Stream.Flush(true); + var receipt=file.Identity(path,roots,hash);Fresh(deadline);file.Commit(); + var result=Result(id,"ok",0,EmptyDigest,0);result["files"]=new object[]{receipt};Empty(result); + } + } + } + static void Get(Dictionary request,string id,long limit,DateTime deadline) { + string path=Native.FullPath(Text(request,"path")); + var owned=(System.Collections.IList)request["owned"]; + if(owned.Count!=1)throw new InvalidDataException(); + var expected=(Dictionary)owned[0]; + using(var roots=Native.LockParents(path)) using(var file=Native.Open(path)) { + long size=file.Stream.Length;if(size>limit)throw new InvalidDataException(); + Fresh(deadline); + // Verify the held original file, root, security and contents before + // emitting any bytes. A substituted path must not disclose data. + var original=file.Identity(path,roots); + if(!Native.Same(expected,original))throw new InvalidDataException(); + Fresh(deadline);Control(Result(id,"payload",size,"",0)); + string hash=Transfer(file.Stream,output,size,deadline);Fresh(deadline); + var result=Result(id,"ok",size,hash,0);result["files"]=new object[]{file.Identity(path,roots,hash)};Control(result); + } + } + static void Exec(Dictionary request,string id,long size,long limit,DateTime deadline) { + if(size>16777216||limit==0||limit>16777216)throw new InvalidDataException(); + byte[] script=Exact((int)size);if(input.ReadByte()!=1||Digest(script)!=Text(request,"sha256"))throw new InvalidDataException(); + Fresh(deadline); + using(var shell=PowerShell.Create()) { + shell.Runspace=runspace;runspace.SessionStateProxy.SetVariable("LASTEXITCODE",null);shell.AddScript(new UTF8Encoding(false,true).GetString(script)); + // Streaming the pipeline bounds retained output and observes completion. + var values=new PSDataCollection();var text=new StringBuilder();bool overflow=false,hadError=false;long retained=0;var outputLock=new object(); + Action append=delegate(string line){lock(outputLock){long added=Encoding.UTF8.GetByteCount(line);if(retained+added>limit){if(!overflow){overflow=true;shell.BeginStop(null,null);}}else{text.Append(line);retained+=added;}}}; + values.DataAdded += delegate(object sender,DataAddedEventArgs args) { + foreach(var value in values.ReadAll())append(Convert.ToString(value)+Environment.NewLine); + }; + shell.Streams.Error.DataAdded += delegate(object sender,DataAddedEventArgs args) { + hadError=true;foreach(var error in shell.Streams.Error.ReadAll())append(error.ToString()+Environment.NewLine); + }; + shell.Streams.Warning.DataAdded += delegate(object sender,DataAddedEventArgs args) {shell.Streams.Warning.ReadAll();}; + shell.Streams.Verbose.DataAdded += delegate(object sender,DataAddedEventArgs args) {shell.Streams.Verbose.ReadAll();}; + shell.Streams.Debug.DataAdded += delegate(object sender,DataAddedEventArgs args) {shell.Streams.Debug.ReadAll();}; + shell.Streams.Progress.DataAdded += delegate(object sender,DataAddedEventArgs args) {shell.Streams.Progress.ReadAll();}; + shell.Streams.Information.DataAdded += delegate(object sender,DataAddedEventArgs args) { + foreach(var message in shell.Streams.Information.ReadAll())append(message.ToString()+Environment.NewLine); + }; + IAsyncResult pending=shell.BeginInvoke(null,values); + int wait=(int)Math.Min(Int32.MaxValue,Math.Max(1,(deadline-DateTime.UtcNow).TotalMilliseconds)); + if(!pending.AsyncWaitHandle.WaitOne(wait)) {shell.Stop();throw new TimeoutException();} + try {shell.EndInvoke(pending);} catch(PipelineStoppedException){if(!overflow)throw;} + byte[] payload=Encoding.UTF8.GetBytes(text.ToString());Fresh(deadline); + object nativeExit=runspace.SessionStateProxy.GetVariable("LASTEXITCODE");int exit=hadError||shell.HadErrors?1:(nativeExit==null?0:Convert.ToInt32(nativeExit));var result=Result(id,overflow?"output_limit":"ok",payload.Length,Digest(payload),exit); + Control(result);output.Write(payload,0,payload.Length);Control(result); + } + } + static void Remove(Dictionary request,string id,DateTime deadline) { + var entries=(System.Collections.IList)request["owned"];if(entries.Count==0||entries.Count>128)throw new InvalidDataException(); + var removed=new List(); + foreach(Dictionary expected in entries) { + string path=Text(expected,"path");string status="preserved";bool absent=false; + try { + path=Native.FullPath(path); + using(var roots=Native.LockParents(path)) using(var file=Native.Open(path)) { + Fresh(deadline);var actual=file.Identity(path,roots); + if(Native.Same(expected,actual)) {Fresh(deadline);file.Delete();status="deleted";} + } + absent=Native.Absent(path);if(status=="deleted"&&!absent)status="absence_unconfirmed"; + } catch(TimeoutException){status="expired";} catch {status="preserved";} + removed.Add(new Dictionary{{"path",path},{"status",status},{"absent",absent}}); + } + var result=Result(id,"ok",0,EmptyDigest,0);result["removed"]=removed;Empty(result); + } +} +} diff --git a/internal/mcpadapter/adapter.go b/internal/mcpadapter/adapter.go index 0a1c7db..1738c00 100644 --- a/internal/mcpadapter/adapter.go +++ b/internal/mcpadapter/adapter.go @@ -187,6 +187,7 @@ func (a *Adapter) BuildServer() *mcp.Server { }, }) + a.addGuestTools(server) mcp.AddTool(server, &mcp.Tool{ Name: "doctor", Description: "Return host diagnostics and provider capabilities", @@ -264,7 +265,7 @@ func (a *Adapter) BuildServer() *mcp.Server { mcp.AddTool(server, &mcp.Tool{ Name: "session_write", - Description: "Write input data to a persistent terminal session", + Description: "Type terminal input or launch a short command (up to 64 KiB). Upload whole local files with guest_put; execute local scripts with guest_exec. Prepare their binary worker once with guest_prepare.", }, a.SessionWrite) mcp.AddTool(server, &mcp.Tool{ diff --git a/internal/mcpadapter/adapter_part1_test.go b/internal/mcpadapter/adapter_part1_test.go index 64401e3..5298492 100644 --- a/internal/mcpadapter/adapter_part1_test.go +++ b/internal/mcpadapter/adapter_part1_test.go @@ -97,6 +97,7 @@ func TestToolList(t *testing.T) { toolsResult := getExposedTools(ctx, t) expectedTools := map[string]bool{ + "guest_prepare": true, "guest_put": true, "guest_get": true, "guest_exec": true, "guest_remove_owned": true, "console_screenshot": true, "console_record": true, "console_record_status": true, @@ -141,8 +142,8 @@ func TestToolList(t *testing.T) { } } - if len(toolsResult.Tools) != 27 { - t.Errorf("expected exactly 27 tools, got %d", len(toolsResult.Tools)) + if len(toolsResult.Tools) != 32 { + t.Errorf("expected exactly 32 tools, got %d", len(toolsResult.Tools)) } } diff --git a/internal/mcpadapter/behavior_integration_test.go b/internal/mcpadapter/behavior_integration_test.go index 22ce6ee..da1cdb0 100644 --- a/internal/mcpadapter/behavior_integration_test.go +++ b/internal/mcpadapter/behavior_integration_test.go @@ -80,8 +80,8 @@ func TestBinaryStdioIntegration(t *testing.T) { if err != nil { t.Fatalf("ListTools failed: %v", err) } - if len(toolsRes.Tools) != 27 { - t.Errorf("expected exactly 27 tools, got %d", len(toolsRes.Tools)) + if len(toolsRes.Tools) != 32 { + t.Errorf("expected exactly 32 tools, got %d", len(toolsRes.Tools)) } if err := clientSession.Close(); err != nil { @@ -116,7 +116,7 @@ func resolveTestMCPBinary(t *testing.T) string { binaryPath := filepath.Join(t.TempDir(), binaryName) buildCmd := exec.Command("go", "build", "-o", binaryPath, "../../cmd/amc-mcp") buildCmd.Dir = "." - buildCmd.Env = append(os.Environ(), "GOTOOLCHAIN=go1.26.7") + buildCmd.Env = os.Environ() if out, err := buildCmd.CombinedOutput(); err != nil { t.Fatalf("failed to build amc-mcp binary: %v\nOutput: %s", err, string(out)) } diff --git a/internal/mcpadapter/errors.go b/internal/mcpadapter/errors.go index 9be040b..2f6f2a0 100644 --- a/internal/mcpadapter/errors.go +++ b/internal/mcpadapter/errors.go @@ -3,6 +3,7 @@ package mcpadapter import ( "context" "errors" + "github.com/Horcag/agent-machine-control/internal/app" "net/http" "github.com/Horcag/agent-machine-control/internal/backends/hyperv" @@ -60,45 +61,52 @@ func operationToolError(err error, operationID, receiptID string) *mcp.CallToolR // Category keys are allowlisted protocol metadata, never provider message text. func categoryToolError(category string) string { messages := map[string]string{ - "recording_status_inconclusive": "recording_status_inconclusive: recording status is inconclusive", - "approval_required": "approval_required: operator approval required for this operation", - "target_not_enrolled": "target is not enrolled", - "target_mismatch": "target is not enrolled", - "target_unavailable": "protected target is unavailable", - "host_unavailable": "backend_host_unavailable: backend management host is unavailable", - "module_missing": "backend_module_missing: Hyper-V PowerShell module is unavailable", - "backend_error": "backend_error: backend operation failed", - "backend_unavailable": "backend_unavailable: machine backend is unavailable", - "access_denied": "backend_access_denied: machine backend access denied", - "malformed_response": "backend_malformed_response: machine backend returned an invalid response", - "output_limit": "backend_output_limit: machine backend output exceeded its size limit", - "machine_not_found": "requested resource not found", - "checkpoint_not_found": "requested resource not found", - "not_found": "requested resource not found", - "invalid_argument": "invalid_argument: invalid operation input", - "invalid_state": "operation_conflict: operation conflicts with current state", - "conflict": "operation_conflict: operation conflicts with current state", - "unauthorized": "authentication failed", - "unauthenticated_caller": "authentication failed", - "forbidden": "access_denied: operation denied by policy", - "policy": "access_denied: operation denied by policy", - "policy_denial": "access_denied: operation denied by policy", - "forbidden_operation": "access_denied: operation denied by policy", - "missing_required_scope": "access_denied: operation denied by policy", - "delegation_exceeds_authority": "access_denied: operation denied by policy", - "approval_record_expired": "approval_expired: operator approval has expired", - "approval_record_consumed": "approval_consumed: operator approval has already been consumed", - "approval_record_not_yet_valid": "approval_invalid: operator approval is not yet valid", - "approval_record_mismatch": "approval_invalid: operator approval does not match this operation", - "missing_verified_rollback_point": "rollback_required: operation requires a verified rollback point", - "timeout": "operation_timeout: operation timeout exceeded", - "deadline_exceeded": "operation_timeout: operation timeout exceeded", - "deadline_passed": "operation_timeout: operation timeout exceeded", - "cancelled": "operation_canceled: operation was canceled", - "caller_canceled": "operation_canceled: operation was canceled", - "persistence_failure": "operation_persistence_failure: operation record could not be saved", - "finalization_error": "operation_finalization_error: operation could not be finalized", - "daemon_crash_recovered": "operation_interrupted: operation was interrupted by daemon restart", + "guest_workspace_unprovisioned": "guest_workspace_unprovisioned: an operator must provision authorized workspace identity metadata", + "guest_authority": "guest_authority: guest authority changed or unavailable", + "guest_not_prepared": "guest_not_prepared: prepare the guest worker explicitly", + "guest_ownership": "guest_ownership: guest artifact ownership or identity mismatch", + "guest_uncertain": "guest_uncertain: guest operation outcome is uncertain; do not replay", + "missing_sensitive_evidence_scope": "access_denied: sensitive evidence scope required", + "missing_required_capability": "access_denied: required capability unavailable", + "recording_status_inconclusive": "recording_status_inconclusive: recording status is inconclusive", + "approval_required": "approval_required: operator approval required for this operation", + "target_not_enrolled": "target is not enrolled", + "target_mismatch": "target is not enrolled", + "target_unavailable": "protected target is unavailable", + "host_unavailable": "backend_host_unavailable: backend management host is unavailable", + "module_missing": "backend_module_missing: Hyper-V PowerShell module is unavailable", + "backend_error": "backend_error: backend operation failed", + "backend_unavailable": "backend_unavailable: machine backend is unavailable", + "access_denied": "backend_access_denied: machine backend access denied", + "malformed_response": "backend_malformed_response: machine backend returned an invalid response", + "output_limit": "backend_output_limit: machine backend output exceeded its size limit", + "machine_not_found": "requested resource not found", + "checkpoint_not_found": "requested resource not found", + "not_found": "requested resource not found", + "invalid_argument": "invalid_argument: invalid operation input", + "invalid_state": "operation_conflict: operation conflicts with current state", + "conflict": "operation_conflict: operation conflicts with current state", + "unauthorized": "authentication failed", + "unauthenticated_caller": "authentication failed", + "forbidden": "access_denied: operation denied by policy", + "policy": "access_denied: operation denied by policy", + "policy_denial": "access_denied: operation denied by policy", + "forbidden_operation": "access_denied: operation denied by policy", + "missing_required_scope": "access_denied: operation denied by policy", + "delegation_exceeds_authority": "access_denied: operation denied by policy", + "approval_record_expired": "approval_expired: operator approval has expired", + "approval_record_consumed": "approval_consumed: operator approval has already been consumed", + "approval_record_not_yet_valid": "approval_invalid: operator approval is not yet valid", + "approval_record_mismatch": "approval_invalid: operator approval does not match this operation", + "missing_verified_rollback_point": "rollback_required: operation requires a verified rollback point", + "timeout": "operation_timeout: operation timeout exceeded", + "deadline_exceeded": "operation_timeout: operation timeout exceeded", + "deadline_passed": "operation_timeout: operation timeout exceeded", + "cancelled": "operation_canceled: operation was canceled", + "caller_canceled": "operation_canceled: operation was canceled", + "persistence_failure": "operation_persistence_failure: operation record could not be saved", + "finalization_error": "operation_finalization_error: operation could not be finalized", + "daemon_crash_recovered": "operation_interrupted: operation was interrupted by daemon restart", } if message := messages[category]; message != "" { return message @@ -159,6 +167,9 @@ func classifiedToolError(err error) string { } func protectedToolError(err error) string { + if errors.Is(err, app.ErrGuestUncertain) { + return categoryToolError("guest_uncertain") + } if isApprovalRequired(err) { return "approval_required: operator approval required for this operation" } diff --git a/internal/mcpadapter/guest.go b/internal/mcpadapter/guest.go new file mode 100644 index 0000000..1c6c08f --- /dev/null +++ b/internal/mcpadapter/guest.go @@ -0,0 +1,155 @@ +package mcpadapter + +import ( + "context" + "errors" + "github.com/Horcag/agent-machine-control/internal/domain" + "time" + + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/client" + "github.com/Horcag/agent-machine-control/internal/guest/stream" + "github.com/modelcontextprotocol/go-sdk/mcp" +) + +type GuestInput struct { + Target string `json:"target,omitempty"` + ArtifactIDs []string `json:"artifact_ids,omitempty"` + Source string `json:"source,omitempty" jsonschema:"Explicit local payload path, never sent to the guest"` + Destination string `json:"destination,omitempty" jsonschema:"Explicit new local output path"` + MaxOutput int64 `json:"max_output"` + Reason string `json:"reason"` + IdempotencyKey string `json:"idempotency_key"` + Deadline string `json:"deadline" jsonschema:"Exact canonical RFC3339Nano operation deadline"` + ApprovalID string `json:"approval_id,omitempty"` +} + +func (a *Adapter) guestOperation(ctx context.Context, in GuestInput, operation string) (*mcp.CallToolResult, app.GuestOperationResult, error) { + var zero app.GuestOperationResult + if err := validateGuestInput(in, operation); err != nil { + return mcpToolError(err), zero, nil + } + deadline, err := time.Parse(time.RFC3339Nano, in.Deadline) + if err != nil { + return mcpToolError(NewInputError("invalid guest deadline")), zero, nil + } + ctx, cancel := context.WithDeadline(ctx, deadline) + defer cancel() + cl, err := a.getClient() + if err != nil { + return mcpToolError(err), zero, nil + } + p := app.GuestOperationParams{Target: in.Target, Operation: operation, ArtifactIDs: in.ArtifactIDs, MaxOutput: in.MaxOutput, Reason: in.Reason, IdempotencyKey: in.IdempotencyKey, Deadline: deadline, ApprovalID: in.ApprovalID} + var input stream.ContextReader + source, err := client.OpenGuestPayload(ctx, operation, in.Source) + if err != nil { + return mcpToolError(err), zero, nil + } + if source != nil { + defer source.Close() + p.Size, p.SHA256 = source.Size, source.SHA256 + input = source.Input() + } + var result app.GuestOperationResult + if operation == "get" || operation == "exec" { + if in.Destination == "" { + return mcpToolError(NewInputError("new local destination is required")), zero, nil + } + result, err = cl.GuestOperationToFile(ctx, p, input, in.Destination) + } else { + if in.Destination != "" { + return mcpToolError(NewInputError("unexpected destination")), zero, nil + } + result, err = cl.GuestOperation(ctx, p, input, nil) + } + if source != nil { + err = errors.Join(err, source.Verify()) + } + if err != nil { + return mcpToolError(err), result, nil + } + return nil, result, nil +} +func (a *Adapter) GuestPrepare(ctx context.Context, _ *mcp.CallToolRequest, in GuestInput) (*mcp.CallToolResult, app.GuestOperationResult, error) { + return a.guestOperation(ctx, in, "prepare") +} +func (a *Adapter) GuestPut(ctx context.Context, _ *mcp.CallToolRequest, in GuestInput) (*mcp.CallToolResult, app.GuestOperationResult, error) { + return a.guestOperation(ctx, in, "put") +} +func (a *Adapter) GuestGet(ctx context.Context, _ *mcp.CallToolRequest, in GuestInput) (*mcp.CallToolResult, app.GuestOperationResult, error) { + return a.guestOperation(ctx, in, "get") +} +func (a *Adapter) GuestExec(ctx context.Context, _ *mcp.CallToolRequest, in GuestInput) (*mcp.CallToolResult, app.GuestOperationResult, error) { + return a.guestOperation(ctx, in, "exec") +} +func (a *Adapter) GuestRemoveOwned(ctx context.Context, _ *mcp.CallToolRequest, in GuestInput) (*mcp.CallToolResult, app.GuestOperationResult, error) { + return a.guestOperation(ctx, in, "remove-owned") +} +func (a *Adapter) addGuestTools(server *mcp.Server) { + mcp.AddTool(server, &mcp.Tool{Name: "guest_prepare", InputSchema: guestInputSchema("prepare"), Description: "Prepare the persistent binary worker once before guest_put, guest_get or guest_exec; subsequent operations reuse it with the server-provisioned workspace identity"}, a.GuestPrepare) + mcp.AddTool(server, &mcp.Tool{Name: "guest_put", InputSchema: guestInputSchema("put"), Description: "Upload a whole local file, including gigabyte archives and installers (up to 1 TiB), in one binary stream with SHA-256 verification. Pass its source path after guest_prepare; returns an opaque ownership reference for guest_get and guest_remove_owned."}, a.GuestPut) + mcp.AddTool(server, &mcp.Tool{Name: "guest_get", InputSchema: guestInputSchema("get"), Description: "Download an opaque owned artifact to a new local destination after verification"}, a.GuestGet) + mcp.AddTool(server, &mcp.Tool{Name: "guest_exec", InputSchema: guestInputSchema("exec"), Description: "Send and execute a whole local UTF-8 PowerShell script (up to 16 MiB) through the prepared binary worker in one operation; publish verified output to a new local destination"}, a.GuestExec) + mcp.AddTool(server, &mcp.Tool{Name: "guest_remove_owned", InputSchema: guestInputSchema("remove-owned"), Description: "Remove only artifacts identified by immutable opaque ownership references"}, a.GuestRemoveOwned) +} + +func validateGuestInput(in GuestInput, operation string) error { + if err := domain.ValidateGuestOperationShape(operation, 0, in.MaxOutput, in.ArtifactIDs); err != nil { + return NewInputError("invalid_argument: " + guestInputDescription(operation)) + } + payload := operation == "put" || operation == "exec" + output := operation == "get" || operation == "exec" + if (in.Source != "") != payload || (in.Destination != "") != output { + return NewInputError("invalid_argument: " + guestInputDescription(operation)) + } + return nil +} + +func guestInputDescription(operation string) string { + switch operation { + case "prepare": + return "guest_prepare requires max_output=0 and no source, destination or artifact_ids" + case "put": + return "guest_put requires source, max_output=0 and no destination or artifact_ids" + case "get": + return "guest_get requires one artifact_id, positive max_output, destination and no source" + case "exec": + return "guest_exec requires source, destination, max_output between 1 and 16777216 and no artifact_ids" + default: + return "guest_remove_owned requires 1 to 128 unique artifact_ids, max_output=0 and no source or destination" + } +} + +func guestInputSchema(operation string) map[string]any { + properties := map[string]any{ + "target": map[string]any{"type": "string"}, + "reason": map[string]any{"type": "string"}, + "idempotency_key": map[string]any{"type": "string"}, + "deadline": map[string]any{"type": "string", "description": "Exact canonical RFC3339Nano operation deadline"}, + "approval_id": map[string]any{"type": "string"}, + "max_output": map[string]any{"type": "integer", "minimum": 0, "maximum": 0, "description": "Must be zero; this operation produces no output stream"}, + } + required := []string{"reason", "idempotency_key", "deadline"} + if operation == "put" || operation == "exec" { + properties["source"] = map[string]any{"type": "string", "minLength": 1, "description": "Path to the complete local file; streamed as binary with bounded memory. Put accepts up to 1 TiB; exec accepts UTF-8 scripts up to 16 MiB. The local path stays on the host."} + required = append(required, "source") + } + if operation == "get" || operation == "exec" { + maximum := int64(1 << 40) + if operation == "exec" { + maximum = 16 * 1024 * 1024 + } + properties["max_output"] = map[string]any{"type": "integer", "minimum": 1, "maximum": maximum, "description": "Positive maximum output bytes; output exceeding this limit is rejected"} + properties["destination"] = map[string]any{"type": "string", "minLength": 1, "description": "Explicit new local output path; existing files are never replaced"} + required = append(required, "max_output", "destination") + } + if operation == "get" || operation == "remove-owned" { + maximum := 128 + if operation == "get" { + maximum = 1 + } + properties["artifact_ids"] = map[string]any{"type": "array", "minItems": 1, "maxItems": maximum, "uniqueItems": true, "items": map[string]any{"type": "string", "pattern": "^[0-9a-f]{32}$"}, "description": "Opaque ownership references returned by guest_put"} + required = append(required, "artifact_ids") + } + return map[string]any{"type": "object", "description": guestInputDescription(operation), "properties": properties, "required": required, "additionalProperties": false} +} diff --git a/internal/mcpadapter/guest_test.go b/internal/mcpadapter/guest_test.go new file mode 100644 index 0000000..41be816 --- /dev/null +++ b/internal/mcpadapter/guest_test.go @@ -0,0 +1,191 @@ +package mcpadapter + +import ( + "bytes" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "encoding/json" + "fmt" + "github.com/Horcag/agent-machine-control/internal/app" + "github.com/Horcag/agent-machine-control/internal/client" + "github.com/Horcag/agent-machine-control/internal/daemon" + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/modelcontextprotocol/go-sdk/mcp" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "runtime" + "strings" + "testing" + "time" +) + +func TestGuestInvalidDeadlineNoDispatch(t *testing.T) { + adapter := NewAdapter("missing") + result, _, err := adapter.GuestPrepare(t.Context(), nil, GuestInput{Deadline: "invalid"}) + if err != nil || result == nil || !result.IsError { + t.Fatalf("result=%+v err=%v", result, err) + } +} + +func TestGuestMCPStreamsLocalPayloadAndPublishesVerifiedBinary(t *testing.T) { + payload := []byte{0, 255, 128, 1} + var operations []string + server := httptest.NewServer(guestMCPStreamHandler(t, payload, &operations)) + defer server.Close() + a := &Adapter{client: client.New(server.URL, "synthetic-agent")} + directory := t.TempDir() + source := filepath.Join(directory, "payload.bin") + if err := os.WriteFile(source, []byte("Write-Output 'synthetic'"), 0600); err != nil { + t.Fatal(err) + } + for _, operation := range []string{"prepare", "put", "get", "exec", "remove-owned"} { + in := guestMCPCaseInput(operation, source, directory) + result, receipt, err := callGuestMCP(a, t, in, operation) + if in.Destination != "" && runtime.GOOS != "linux" && runtime.GOOS != "windows" { + assertGuestMCPUnsupportedPublication(t, in.Destination, result, err) + continue + } + if err != nil || result != nil || receipt.Receipt == nil { + t.Fatalf("%s: %+v %v", operation, result, err) + } + if in.Destination != "" { + got, err := os.ReadFile(in.Destination) + if err != nil || !bytes.Equal(got, payload) { + t.Fatalf("verified publication %s: %v", operation, err) + } + } + } + expected := "prepare,put,get,exec,remove-owned" + if runtime.GOOS != "linux" && runtime.GOOS != "windows" { + expected = "prepare,put,remove-owned" + } + if strings.Join(operations, ",") != expected { + t.Fatalf("unexpected dispatch %v", operations) + } +} + +func guestMCPStreamHandler(t *testing.T, payload []byte, operations *[]string) http.HandlerFunc { + t.Helper() + return func(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("Authorization") != "Bearer synthetic-agent" { + t.Error("agent credential lost") + } + p, _, err := daemon.DecodeGuestMetadata(r.Body) + if err != nil { + t.Error(err) + return + } + input, err := io.ReadAll(r.Body) + if err != nil { + t.Error(err) + return + } + if p.Target != "synthetic-target" || p.Reason != "contract" || p.ApprovalID != "synthetic-grant" { + t.Error("exact authority arguments lost") + } + if strings.Contains(fmt.Sprintf("%+v", p), "payload.bin") { + t.Error("local source name escaped into metadata") + } + *operations = append(*operations, p.Operation) + assertGuestMCPUploadBinding(t, p, input) + output := []byte(nil) + if p.Operation == "get" || p.Operation == "exec" { + output = payload + } + writeGuestMCPFinal(t, w, p, output) + } +} +func assertGuestMCPUploadBinding(t *testing.T, p app.GuestOperationParams, input []byte) { + t.Helper() + if p.Operation == "put" || p.Operation == "exec" { + sum := sha256.Sum256(input) + if p.Size != int64(len(input)) || p.SHA256 != hex.EncodeToString(sum[:]) { + t.Error("whole payload binding lost") + } + } +} + +func guestMCPCaseInput(operation, source, directory string) GuestInput { + in := GuestInput{Target: "synthetic-target", Reason: "contract", ApprovalID: "synthetic-grant", IdempotencyKey: "request-" + operation, Deadline: time.Now().Add(time.Minute).Format(time.RFC3339Nano)} + if operation == "put" || operation == "exec" { + in.Source = source + } + if operation == "get" || operation == "exec" { + in.Destination = filepath.Join(directory, operation+".bin") + in.MaxOutput = 64 + } + if operation == "get" || operation == "remove-owned" { + in.ArtifactIDs = []string{strings.Repeat("a", 32)} + } + return in +} +func assertGuestMCPUnsupportedPublication(t *testing.T, destination string, result *mcp.CallToolResult, err error) { + t.Helper() + if err != nil || result == nil || !result.IsError { + t.Fatalf("unsupported publication was admitted: %+v %v", result, err) + } + if _, err = os.Lstat(destination); !os.IsNotExist(err) { + t.Fatalf("unsupported publication created output: %v", err) + } + entries, err := os.ReadDir(filepath.Dir(destination)) + if err != nil || len(entries) != 1 { + t.Fatalf("unsupported publication leaked staging: %v %v", entries, err) + } +} + +func callGuestMCP(a *Adapter, t *testing.T, in GuestInput, operation string) (*mcp.CallToolResult, app.GuestOperationResult, error) { + t.Helper() + switch operation { + case "prepare": + return a.GuestPrepare(t.Context(), nil, in) + case "put": + return a.GuestPut(t.Context(), nil, in) + case "get": + return a.GuestGet(t.Context(), nil, in) + case "exec": + return a.GuestExec(t.Context(), nil, in) + default: + return a.GuestRemoveOwned(t.Context(), nil, in) + } +} +func writeGuestMCPFinal(t *testing.T, w http.ResponseWriter, p app.GuestOperationParams, data []byte) { + t.Helper() + now := time.Now() + sum := sha256.Sum256(data) + receipt := domain.Receipt{ReceiptID: domain.ReceiptID("rcpt-" + strings.Repeat("a", 32)), Fingerprint: domain.Fingerprint("sha256:" + strings.Repeat("a", 64)), Outcome: domain.ExecutionOutcome{Status: domain.OutcomeSuccess}, OperationKind: domain.OperationKind("guest." + p.Operation), IdempotencyKey: p.IdempotencyKey, Target: "local:11111111-1111-4111-8111-111111111111", Actor: "agent:mcp-local", Class: domain.ClassDestructivePrivileged, EffectiveBackend: "ssh", StartedAt: now, CompletedAt: now, ObservationType: domain.ObservationObserved, RedactionStatus: domain.RedactionNotApplicable} + result := app.GuestOperationResult{Receipt: &receipt, Size: int64(len(data)), SHA256: hex.EncodeToString(sum[:])} + if p.Operation == "put" { + result.Size = p.Size + result.SHA256 = p.SHA256 + } + final, _ := json.Marshal(daemon.GuestFinal{Result: result}) + w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Trailer", daemon.GuestResultTrailer) + if len(data) > 0 { + if _, err := w.Write(data); err != nil { + t.Error(err) + } + } + w.Header().Set(daemon.GuestResultTrailer, base64.RawStdEncoding.EncodeToString(final)) +} + +func TestGuestMCPRejectsLocalContractFailuresBeforeDispatch(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { t.Error("invalid local arguments dispatched") })) + defer server.Close() + a := &Adapter{client: client.New(server.URL, "synthetic-agent")} + deadline := time.Now().Add(time.Minute).Format(time.RFC3339Nano) + for _, tc := range []struct{ name, operation, source, destination string }{ + {"get-needs-new-path", "get", "", ""}, {"prepare-no-output", "prepare", "", "unexpected"}, {"remove-no-source", "remove-owned", "unexpected", ""}, {"missing-put", "put", filepath.Join(t.TempDir(), "missing"), ""}, + } { + t.Run(tc.name, func(t *testing.T) { + r, _, err := callGuestMCP(a, t, GuestInput{Deadline: deadline, Source: tc.source, Destination: tc.destination}, tc.operation) + if err != nil || r == nil || !r.IsError { + t.Fatalf("result=%+v error=%v", r, err) + } + }) + } +} diff --git a/internal/mcpadapter/guest_validation_test.go b/internal/mcpadapter/guest_validation_test.go new file mode 100644 index 0000000..444b942 --- /dev/null +++ b/internal/mcpadapter/guest_validation_test.go @@ -0,0 +1,109 @@ +package mcpadapter + +import ( + "encoding/base64" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/Horcag/agent-machine-control/internal/client" + "github.com/Horcag/agent-machine-control/internal/daemon" + "github.com/modelcontextprotocol/go-sdk/mcp" +) + +func TestGuestMCPMalformedShapesNeverDispatch(t *testing.T) { + calls := 0 + server := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { calls++ })) + defer server.Close() + a := &Adapter{client: client.New(server.URL, "synthetic")} + for _, tc := range []struct { + operation string + mutate func(*GuestInput) + }{ + {"prepare", func(in *GuestInput) { in.MaxOutput = 64 }}, + {"prepare", func(in *GuestInput) { in.ArtifactIDs = []string{strings.Repeat("a", 32)} }}, + {"put", func(in *GuestInput) { in.MaxOutput = 1 }}, + {"put", func(in *GuestInput) { in.ArtifactIDs = []string{strings.Repeat("a", 32)} }}, + {"get", func(in *GuestInput) { in.ArtifactIDs = nil }}, + {"get", func(in *GuestInput) { in.Source = "private-secret-path" }}, + {"exec", func(in *GuestInput) { in.MaxOutput = 16*1024*1024 + 1 }}, + {"exec", func(in *GuestInput) { in.ArtifactIDs = []string{strings.Repeat("a", 32)} }}, + {"remove-owned", func(in *GuestInput) { in.ArtifactIDs = nil }}, + {"remove-owned", func(in *GuestInput) { in.MaxOutput = 1 }}, + } { + in := guestMCPCaseInput(tc.operation, "private-secret-path", t.TempDir()) + tc.mutate(&in) + result, _, err := callGuestMCP(a, t, in, tc.operation) + if err != nil || result == nil || !result.IsError { + t.Fatalf("%s: %+v %v", tc.operation, result, err) + } + text := result.Content[0].(*mcp.TextContent).Text + if !strings.Contains(text, "invalid_argument") || !strings.Contains(text, "guest_") || strings.Contains(text, "private-secret") { + t.Fatalf("unsafe or unhelpful error: %q", text) + } + } + if calls != 0 { + t.Fatalf("invalid requests dispatched %d times", calls) + } +} + +func TestGuestMCPTypedDenialAcrossTransports(t *testing.T) { + for _, category := range []string{"approval_required", "guest_workspace_unprovisioned", "invalid_argument", "guest_uncertain"} { + t.Run(category, func(t *testing.T) { + calls := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls++ + if _, _, err := daemon.DecodeGuestMetadata(r.Body); err != nil { + t.Error(err) + } + w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Trailer", daemon.GuestResultTrailer) + data, _ := json.Marshal(daemon.GuestFinal{Category: category, Error: "private-secret-path-token"}) + w.Header().Set(daemon.GuestResultTrailer, base64.RawStdEncoding.EncodeToString(data)) + })) + defer server.Close() + a := &Adapter{client: client.New(server.URL, "synthetic")} + cs := recordingMCPClient(t, a) + result, err := cs.CallTool(t.Context(), &mcp.CallToolParams{Name: "guest_prepare", Arguments: guestMCPCaseInput("prepare", "", t.TempDir())}) + if err != nil || result == nil || !result.IsError || calls != 1 { + t.Fatalf("result=%+v calls=%d err=%v", result, calls, err) + } + text := result.Content[0].(*mcp.TextContent).Text + if !strings.Contains(text, category) || strings.Contains(text, "private") { + t.Fatalf("category lost or secret exposed: %q", text) + } + }) + } +} + +func TestGuestMCPSchemasRejectIrrelevantFieldsBeforeHTTP(t *testing.T) { + calls := 0 + server := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { calls++ })) + defer server.Close() + cs := recordingMCPClient(t, &Adapter{client: client.New(server.URL, "synthetic")}) + for _, tc := range []struct { + operation, field string + value any + }{ + {"prepare", "source", "private-secret-path"}, {"prepare", "max_output", 64}, + {"put", "destination", "private-secret-path"}, {"get", "source", "private-secret-path"}, + {"exec", "artifact_ids", []string{strings.Repeat("a", 32)}}, {"remove-owned", "destination", "private-secret-path"}, + } { + in := guestMCPCaseInput(tc.operation, "private-secret-path", t.TempDir()) + encoded, _ := json.Marshal(in) + var arguments map[string]any + if err := json.Unmarshal(encoded, &arguments); err != nil { + t.Fatal(err) + } + arguments[tc.field] = tc.value + result, err := cs.CallTool(t.Context(), &mcp.CallToolParams{Name: "guest_" + strings.ReplaceAll(tc.operation, "-", "_"), Arguments: arguments}) + if err == nil && (result == nil || !result.IsError) { + t.Fatalf("schema admitted %s %s: %+v", tc.operation, tc.field, result) + } + } + if calls != 0 { + t.Fatalf("malformed schema input reached HTTP %d times", calls) + } +} diff --git a/internal/mcpadapter/testdata/schemas/tools.json b/internal/mcpadapter/testdata/schemas/tools.json index 1996796..fc47c0b 100644 --- a/internal/mcpadapter/testdata/schemas/tools.json +++ b/internal/mcpadapter/testdata/schemas/tools.json @@ -1703,6 +1703,7 @@ }, "response": { "additionalProperties": false, + "description": "Guest helper response for semantic actions only; absent for native input", "properties": { "clipboard": { "additionalProperties": false, @@ -1931,8 +1932,7 @@ "type": [ "null", "object" - ], - "description": "Guest helper response for semantic actions only; absent for native input" + ] } }, "required": [ @@ -2352,6 +2352,1316 @@ "type": "object" } }, + { + "description": "Send and execute a whole local UTF-8 PowerShell script (up to 16 MiB) through the prepared binary worker in one operation; publish verified output to a new local destination", + "inputSchema": { + "additionalProperties": false, + "description": "guest_exec requires source, destination, max_output between 1 and 16777216 and no artifact_ids", + "properties": { + "approval_id": { + "type": "string" + }, + "deadline": { + "description": "Exact canonical RFC3339Nano operation deadline", + "type": "string" + }, + "destination": { + "description": "Explicit new local output path; existing files are never replaced", + "minLength": 1, + "type": "string" + }, + "idempotency_key": { + "type": "string" + }, + "max_output": { + "description": "Positive maximum output bytes; output exceeding this limit is rejected", + "maximum": 16777216, + "minimum": 1, + "type": "integer" + }, + "reason": { + "type": "string" + }, + "source": { + "description": "Path to the complete local file; streamed as binary with bounded memory. Put accepts up to 1 TiB; exec accepts UTF-8 scripts up to 16 MiB. The local path stays on the host.", + "minLength": 1, + "type": "string" + }, + "target": { + "type": "string" + } + }, + "required": [ + "reason", + "idempotency_key", + "deadline", + "source", + "max_output", + "destination" + ], + "type": "object" + }, + "name": "guest_exec", + "outputSchema": { + "additionalProperties": false, + "properties": { + "artifacts": { + "items": { + "additionalProperties": false, + "properties": { + "id": { + "type": "string" + }, + "sha256": { + "type": "string" + }, + "size": { + "type": "integer" + } + }, + "required": [ + "id", + "size", + "sha256" + ], + "type": "object" + }, + "type": [ + "null", + "array" + ] + }, + "cached_receipt": { + "type": "boolean" + }, + "exit_code": { + "type": "integer" + }, + "receipt": { + "additionalProperties": false, + "properties": { + "Actor": { + "type": "string" + }, + "Class": { + "type": "string" + }, + "CompletedAt": { + "type": "string" + }, + "EffectiveBackend": { + "type": "string" + }, + "EvidenceRefs": { + "items": { + "type": "string" + }, + "type": [ + "null", + "array" + ] + }, + "Fingerprint": { + "type": "string" + }, + "IdempotencyFingerprint": { + "type": "string" + }, + "IdempotencyKey": { + "type": "string" + }, + "ObservationType": { + "type": "string" + }, + "OperationKind": { + "type": "string" + }, + "Outcome": { + "additionalProperties": false, + "properties": { + "ErrorCategory": { + "type": "string" + }, + "ErrorMessage": { + "type": "string" + }, + "ExitCode": { + "type": "integer" + }, + "Status": { + "type": "string" + } + }, + "required": [ + "Status", + "ExitCode", + "ErrorCategory", + "ErrorMessage" + ], + "type": "object" + }, + "ReceiptID": { + "type": "string" + }, + "RedactionStatus": { + "type": "string" + }, + "RollbackRef": { + "type": "string" + }, + "StartedAt": { + "type": "string" + }, + "Target": { + "type": "string" + } + }, + "required": [ + "ReceiptID", + "OperationKind", + "Fingerprint", + "IdempotencyFingerprint", + "IdempotencyKey", + "Actor", + "Target", + "Class", + "EffectiveBackend", + "StartedAt", + "CompletedAt", + "Outcome", + "ObservationType", + "EvidenceRefs", + "RollbackRef", + "RedactionStatus" + ], + "type": [ + "null", + "object" + ] + }, + "removed": { + "items": { + "additionalProperties": false, + "properties": { + "absent": { + "type": "boolean" + }, + "accepted": { + "type": "boolean" + }, + "artifact_id": { + "type": "string" + }, + "attempted": { + "type": "boolean" + }, + "status": { + "type": "string" + } + }, + "required": [ + "artifact_id", + "attempted", + "accepted", + "absent", + "status" + ], + "type": "object" + }, + "type": [ + "null", + "array" + ] + }, + "sha256": { + "type": "string" + }, + "size": { + "type": "integer" + }, + "worker": { + "additionalProperties": false, + "properties": { + "birthTicks": { + "type": "integer" + }, + "image": { + "type": "string" + }, + "pid": { + "type": "integer" + }, + "sid": { + "type": "string" + } + }, + "required": [ + "pid", + "birthTicks", + "image", + "sid" + ], + "type": "object" + }, + "worker_record_id": { + "type": "string" + } + }, + "required": [ + "cached_receipt", + "worker", + "size", + "sha256", + "exit_code" + ], + "type": "object" + } + }, + { + "description": "Download an opaque owned artifact to a new local destination after verification", + "inputSchema": { + "additionalProperties": false, + "description": "guest_get requires one artifact_id, positive max_output, destination and no source", + "properties": { + "approval_id": { + "type": "string" + }, + "artifact_ids": { + "description": "Opaque ownership references returned by guest_put", + "items": { + "pattern": "^[0-9a-f]{32}$", + "type": "string" + }, + "maxItems": 1, + "minItems": 1, + "type": "array", + "uniqueItems": true + }, + "deadline": { + "description": "Exact canonical RFC3339Nano operation deadline", + "type": "string" + }, + "destination": { + "description": "Explicit new local output path; existing files are never replaced", + "minLength": 1, + "type": "string" + }, + "idempotency_key": { + "type": "string" + }, + "max_output": { + "description": "Positive maximum output bytes; output exceeding this limit is rejected", + "maximum": 1099511627776, + "minimum": 1, + "type": "integer" + }, + "reason": { + "type": "string" + }, + "target": { + "type": "string" + } + }, + "required": [ + "reason", + "idempotency_key", + "deadline", + "max_output", + "destination", + "artifact_ids" + ], + "type": "object" + }, + "name": "guest_get", + "outputSchema": { + "additionalProperties": false, + "properties": { + "artifacts": { + "items": { + "additionalProperties": false, + "properties": { + "id": { + "type": "string" + }, + "sha256": { + "type": "string" + }, + "size": { + "type": "integer" + } + }, + "required": [ + "id", + "size", + "sha256" + ], + "type": "object" + }, + "type": [ + "null", + "array" + ] + }, + "cached_receipt": { + "type": "boolean" + }, + "exit_code": { + "type": "integer" + }, + "receipt": { + "additionalProperties": false, + "properties": { + "Actor": { + "type": "string" + }, + "Class": { + "type": "string" + }, + "CompletedAt": { + "type": "string" + }, + "EffectiveBackend": { + "type": "string" + }, + "EvidenceRefs": { + "items": { + "type": "string" + }, + "type": [ + "null", + "array" + ] + }, + "Fingerprint": { + "type": "string" + }, + "IdempotencyFingerprint": { + "type": "string" + }, + "IdempotencyKey": { + "type": "string" + }, + "ObservationType": { + "type": "string" + }, + "OperationKind": { + "type": "string" + }, + "Outcome": { + "additionalProperties": false, + "properties": { + "ErrorCategory": { + "type": "string" + }, + "ErrorMessage": { + "type": "string" + }, + "ExitCode": { + "type": "integer" + }, + "Status": { + "type": "string" + } + }, + "required": [ + "Status", + "ExitCode", + "ErrorCategory", + "ErrorMessage" + ], + "type": "object" + }, + "ReceiptID": { + "type": "string" + }, + "RedactionStatus": { + "type": "string" + }, + "RollbackRef": { + "type": "string" + }, + "StartedAt": { + "type": "string" + }, + "Target": { + "type": "string" + } + }, + "required": [ + "ReceiptID", + "OperationKind", + "Fingerprint", + "IdempotencyFingerprint", + "IdempotencyKey", + "Actor", + "Target", + "Class", + "EffectiveBackend", + "StartedAt", + "CompletedAt", + "Outcome", + "ObservationType", + "EvidenceRefs", + "RollbackRef", + "RedactionStatus" + ], + "type": [ + "null", + "object" + ] + }, + "removed": { + "items": { + "additionalProperties": false, + "properties": { + "absent": { + "type": "boolean" + }, + "accepted": { + "type": "boolean" + }, + "artifact_id": { + "type": "string" + }, + "attempted": { + "type": "boolean" + }, + "status": { + "type": "string" + } + }, + "required": [ + "artifact_id", + "attempted", + "accepted", + "absent", + "status" + ], + "type": "object" + }, + "type": [ + "null", + "array" + ] + }, + "sha256": { + "type": "string" + }, + "size": { + "type": "integer" + }, + "worker": { + "additionalProperties": false, + "properties": { + "birthTicks": { + "type": "integer" + }, + "image": { + "type": "string" + }, + "pid": { + "type": "integer" + }, + "sid": { + "type": "string" + } + }, + "required": [ + "pid", + "birthTicks", + "image", + "sid" + ], + "type": "object" + }, + "worker_record_id": { + "type": "string" + } + }, + "required": [ + "cached_receipt", + "worker", + "size", + "sha256", + "exit_code" + ], + "type": "object" + } + }, + { + "description": "Prepare the persistent binary worker once before guest_put, guest_get or guest_exec; subsequent operations reuse it with the server-provisioned workspace identity", + "inputSchema": { + "additionalProperties": false, + "description": "guest_prepare requires max_output=0 and no source, destination or artifact_ids", + "properties": { + "approval_id": { + "type": "string" + }, + "deadline": { + "description": "Exact canonical RFC3339Nano operation deadline", + "type": "string" + }, + "idempotency_key": { + "type": "string" + }, + "max_output": { + "description": "Must be zero; this operation produces no output stream", + "maximum": 0, + "minimum": 0, + "type": "integer" + }, + "reason": { + "type": "string" + }, + "target": { + "type": "string" + } + }, + "required": [ + "reason", + "idempotency_key", + "deadline" + ], + "type": "object" + }, + "name": "guest_prepare", + "outputSchema": { + "additionalProperties": false, + "properties": { + "artifacts": { + "items": { + "additionalProperties": false, + "properties": { + "id": { + "type": "string" + }, + "sha256": { + "type": "string" + }, + "size": { + "type": "integer" + } + }, + "required": [ + "id", + "size", + "sha256" + ], + "type": "object" + }, + "type": [ + "null", + "array" + ] + }, + "cached_receipt": { + "type": "boolean" + }, + "exit_code": { + "type": "integer" + }, + "receipt": { + "additionalProperties": false, + "properties": { + "Actor": { + "type": "string" + }, + "Class": { + "type": "string" + }, + "CompletedAt": { + "type": "string" + }, + "EffectiveBackend": { + "type": "string" + }, + "EvidenceRefs": { + "items": { + "type": "string" + }, + "type": [ + "null", + "array" + ] + }, + "Fingerprint": { + "type": "string" + }, + "IdempotencyFingerprint": { + "type": "string" + }, + "IdempotencyKey": { + "type": "string" + }, + "ObservationType": { + "type": "string" + }, + "OperationKind": { + "type": "string" + }, + "Outcome": { + "additionalProperties": false, + "properties": { + "ErrorCategory": { + "type": "string" + }, + "ErrorMessage": { + "type": "string" + }, + "ExitCode": { + "type": "integer" + }, + "Status": { + "type": "string" + } + }, + "required": [ + "Status", + "ExitCode", + "ErrorCategory", + "ErrorMessage" + ], + "type": "object" + }, + "ReceiptID": { + "type": "string" + }, + "RedactionStatus": { + "type": "string" + }, + "RollbackRef": { + "type": "string" + }, + "StartedAt": { + "type": "string" + }, + "Target": { + "type": "string" + } + }, + "required": [ + "ReceiptID", + "OperationKind", + "Fingerprint", + "IdempotencyFingerprint", + "IdempotencyKey", + "Actor", + "Target", + "Class", + "EffectiveBackend", + "StartedAt", + "CompletedAt", + "Outcome", + "ObservationType", + "EvidenceRefs", + "RollbackRef", + "RedactionStatus" + ], + "type": [ + "null", + "object" + ] + }, + "removed": { + "items": { + "additionalProperties": false, + "properties": { + "absent": { + "type": "boolean" + }, + "accepted": { + "type": "boolean" + }, + "artifact_id": { + "type": "string" + }, + "attempted": { + "type": "boolean" + }, + "status": { + "type": "string" + } + }, + "required": [ + "artifact_id", + "attempted", + "accepted", + "absent", + "status" + ], + "type": "object" + }, + "type": [ + "null", + "array" + ] + }, + "sha256": { + "type": "string" + }, + "size": { + "type": "integer" + }, + "worker": { + "additionalProperties": false, + "properties": { + "birthTicks": { + "type": "integer" + }, + "image": { + "type": "string" + }, + "pid": { + "type": "integer" + }, + "sid": { + "type": "string" + } + }, + "required": [ + "pid", + "birthTicks", + "image", + "sid" + ], + "type": "object" + }, + "worker_record_id": { + "type": "string" + } + }, + "required": [ + "cached_receipt", + "worker", + "size", + "sha256", + "exit_code" + ], + "type": "object" + } + }, + { + "description": "Upload a whole local file, including gigabyte archives and installers (up to 1 TiB), in one binary stream with SHA-256 verification. Pass its source path after guest_prepare; returns an opaque ownership reference for guest_get and guest_remove_owned.", + "inputSchema": { + "additionalProperties": false, + "description": "guest_put requires source, max_output=0 and no destination or artifact_ids", + "properties": { + "approval_id": { + "type": "string" + }, + "deadline": { + "description": "Exact canonical RFC3339Nano operation deadline", + "type": "string" + }, + "idempotency_key": { + "type": "string" + }, + "max_output": { + "description": "Must be zero; this operation produces no output stream", + "maximum": 0, + "minimum": 0, + "type": "integer" + }, + "reason": { + "type": "string" + }, + "source": { + "description": "Path to the complete local file; streamed as binary with bounded memory. Put accepts up to 1 TiB; exec accepts UTF-8 scripts up to 16 MiB. The local path stays on the host.", + "minLength": 1, + "type": "string" + }, + "target": { + "type": "string" + } + }, + "required": [ + "reason", + "idempotency_key", + "deadline", + "source" + ], + "type": "object" + }, + "name": "guest_put", + "outputSchema": { + "additionalProperties": false, + "properties": { + "artifacts": { + "items": { + "additionalProperties": false, + "properties": { + "id": { + "type": "string" + }, + "sha256": { + "type": "string" + }, + "size": { + "type": "integer" + } + }, + "required": [ + "id", + "size", + "sha256" + ], + "type": "object" + }, + "type": [ + "null", + "array" + ] + }, + "cached_receipt": { + "type": "boolean" + }, + "exit_code": { + "type": "integer" + }, + "receipt": { + "additionalProperties": false, + "properties": { + "Actor": { + "type": "string" + }, + "Class": { + "type": "string" + }, + "CompletedAt": { + "type": "string" + }, + "EffectiveBackend": { + "type": "string" + }, + "EvidenceRefs": { + "items": { + "type": "string" + }, + "type": [ + "null", + "array" + ] + }, + "Fingerprint": { + "type": "string" + }, + "IdempotencyFingerprint": { + "type": "string" + }, + "IdempotencyKey": { + "type": "string" + }, + "ObservationType": { + "type": "string" + }, + "OperationKind": { + "type": "string" + }, + "Outcome": { + "additionalProperties": false, + "properties": { + "ErrorCategory": { + "type": "string" + }, + "ErrorMessage": { + "type": "string" + }, + "ExitCode": { + "type": "integer" + }, + "Status": { + "type": "string" + } + }, + "required": [ + "Status", + "ExitCode", + "ErrorCategory", + "ErrorMessage" + ], + "type": "object" + }, + "ReceiptID": { + "type": "string" + }, + "RedactionStatus": { + "type": "string" + }, + "RollbackRef": { + "type": "string" + }, + "StartedAt": { + "type": "string" + }, + "Target": { + "type": "string" + } + }, + "required": [ + "ReceiptID", + "OperationKind", + "Fingerprint", + "IdempotencyFingerprint", + "IdempotencyKey", + "Actor", + "Target", + "Class", + "EffectiveBackend", + "StartedAt", + "CompletedAt", + "Outcome", + "ObservationType", + "EvidenceRefs", + "RollbackRef", + "RedactionStatus" + ], + "type": [ + "null", + "object" + ] + }, + "removed": { + "items": { + "additionalProperties": false, + "properties": { + "absent": { + "type": "boolean" + }, + "accepted": { + "type": "boolean" + }, + "artifact_id": { + "type": "string" + }, + "attempted": { + "type": "boolean" + }, + "status": { + "type": "string" + } + }, + "required": [ + "artifact_id", + "attempted", + "accepted", + "absent", + "status" + ], + "type": "object" + }, + "type": [ + "null", + "array" + ] + }, + "sha256": { + "type": "string" + }, + "size": { + "type": "integer" + }, + "worker": { + "additionalProperties": false, + "properties": { + "birthTicks": { + "type": "integer" + }, + "image": { + "type": "string" + }, + "pid": { + "type": "integer" + }, + "sid": { + "type": "string" + } + }, + "required": [ + "pid", + "birthTicks", + "image", + "sid" + ], + "type": "object" + }, + "worker_record_id": { + "type": "string" + } + }, + "required": [ + "cached_receipt", + "worker", + "size", + "sha256", + "exit_code" + ], + "type": "object" + } + }, + { + "description": "Remove only artifacts identified by immutable opaque ownership references", + "inputSchema": { + "additionalProperties": false, + "description": "guest_remove_owned requires 1 to 128 unique artifact_ids, max_output=0 and no source or destination", + "properties": { + "approval_id": { + "type": "string" + }, + "artifact_ids": { + "description": "Opaque ownership references returned by guest_put", + "items": { + "pattern": "^[0-9a-f]{32}$", + "type": "string" + }, + "maxItems": 128, + "minItems": 1, + "type": "array", + "uniqueItems": true + }, + "deadline": { + "description": "Exact canonical RFC3339Nano operation deadline", + "type": "string" + }, + "idempotency_key": { + "type": "string" + }, + "max_output": { + "description": "Must be zero; this operation produces no output stream", + "maximum": 0, + "minimum": 0, + "type": "integer" + }, + "reason": { + "type": "string" + }, + "target": { + "type": "string" + } + }, + "required": [ + "reason", + "idempotency_key", + "deadline", + "artifact_ids" + ], + "type": "object" + }, + "name": "guest_remove_owned", + "outputSchema": { + "additionalProperties": false, + "properties": { + "artifacts": { + "items": { + "additionalProperties": false, + "properties": { + "id": { + "type": "string" + }, + "sha256": { + "type": "string" + }, + "size": { + "type": "integer" + } + }, + "required": [ + "id", + "size", + "sha256" + ], + "type": "object" + }, + "type": [ + "null", + "array" + ] + }, + "cached_receipt": { + "type": "boolean" + }, + "exit_code": { + "type": "integer" + }, + "receipt": { + "additionalProperties": false, + "properties": { + "Actor": { + "type": "string" + }, + "Class": { + "type": "string" + }, + "CompletedAt": { + "type": "string" + }, + "EffectiveBackend": { + "type": "string" + }, + "EvidenceRefs": { + "items": { + "type": "string" + }, + "type": [ + "null", + "array" + ] + }, + "Fingerprint": { + "type": "string" + }, + "IdempotencyFingerprint": { + "type": "string" + }, + "IdempotencyKey": { + "type": "string" + }, + "ObservationType": { + "type": "string" + }, + "OperationKind": { + "type": "string" + }, + "Outcome": { + "additionalProperties": false, + "properties": { + "ErrorCategory": { + "type": "string" + }, + "ErrorMessage": { + "type": "string" + }, + "ExitCode": { + "type": "integer" + }, + "Status": { + "type": "string" + } + }, + "required": [ + "Status", + "ExitCode", + "ErrorCategory", + "ErrorMessage" + ], + "type": "object" + }, + "ReceiptID": { + "type": "string" + }, + "RedactionStatus": { + "type": "string" + }, + "RollbackRef": { + "type": "string" + }, + "StartedAt": { + "type": "string" + }, + "Target": { + "type": "string" + } + }, + "required": [ + "ReceiptID", + "OperationKind", + "Fingerprint", + "IdempotencyFingerprint", + "IdempotencyKey", + "Actor", + "Target", + "Class", + "EffectiveBackend", + "StartedAt", + "CompletedAt", + "Outcome", + "ObservationType", + "EvidenceRefs", + "RollbackRef", + "RedactionStatus" + ], + "type": [ + "null", + "object" + ] + }, + "removed": { + "items": { + "additionalProperties": false, + "properties": { + "absent": { + "type": "boolean" + }, + "accepted": { + "type": "boolean" + }, + "artifact_id": { + "type": "string" + }, + "attempted": { + "type": "boolean" + }, + "status": { + "type": "string" + } + }, + "required": [ + "artifact_id", + "attempted", + "accepted", + "absent", + "status" + ], + "type": "object" + }, + "type": [ + "null", + "array" + ] + }, + "sha256": { + "type": "string" + }, + "size": { + "type": "integer" + }, + "worker": { + "additionalProperties": false, + "properties": { + "birthTicks": { + "type": "integer" + }, + "image": { + "type": "string" + }, + "pid": { + "type": "integer" + }, + "sid": { + "type": "string" + } + }, + "required": [ + "pid", + "birthTicks", + "image", + "sid" + ], + "type": "object" + }, + "worker_record_id": { + "type": "string" + } + }, + "required": [ + "cached_receipt", + "worker", + "size", + "sha256", + "exit_code" + ], + "type": "object" + } + }, { "description": "Inspect detailed configuration/state of a VM", "inputSchema": { @@ -4788,7 +6098,7 @@ } }, { - "description": "Write input data to a persistent terminal session", + "description": "Type terminal input or launch a short command (up to 64 KiB). Upload whole local files with guest_put; execute local scripts with guest_exec. Prepare their binary worker once with guest_prepare.", "inputSchema": { "additionalProperties": false, "properties": { @@ -4797,7 +6107,7 @@ "type": "string" }, "data": { - "description": "Character data or command to write", + "description": "UTF-8 terminal input or short command, at most 65536 bytes; use guest_put for files and guest_exec for local scripts", "type": "string" }, "deadline": { diff --git a/internal/mcpadapter/types_sessions.go b/internal/mcpadapter/types_sessions.go index 6d8b08b..5b63134 100644 --- a/internal/mcpadapter/types_sessions.go +++ b/internal/mcpadapter/types_sessions.go @@ -45,7 +45,7 @@ type SessionReadResult struct { type SessionWriteInput struct { SessionID string `json:"session_id" jsonschema:"Session ID"` - Data string `json:"data" jsonschema:"Character data or command to write"` + Data string `json:"data" jsonschema:"UTF-8 terminal input or short command, at most 65536 bytes; use guest_put for files and guest_exec for local scripts"` Reason string `json:"reason" jsonschema:"Reason for writing data"` IdempotencyKey string `json:"idempotency_key" jsonschema:"Idempotency key"` Timeout string `json:"timeout" jsonschema:"Operation timeout (e.g. 30s, 5m)"` diff --git a/internal/operations/manager_repair_test.go b/internal/operations/manager_repair_test.go index d82f24a..d214596 100644 --- a/internal/operations/manager_repair_test.go +++ b/internal/operations/manager_repair_test.go @@ -108,7 +108,7 @@ func (b *repairBlockingBackend) RestoreCheckpoint(_ context.Context, _ string, _ func TestManager_PendingAdmittedRunningObservationAndExactRetry(t *testing.T) { blockCh := make(chan struct{}) - backend := &repairBlockingBackend{blockChannel: blockCh} + backend := &repairBlockingBackend{blockChannel: blockCh, entered: make(chan struct{}, 1)} state := testManagerStateDir(t) dir := state.OperationsDir() @@ -153,19 +153,18 @@ func TestManager_PendingAdmittedRunningObservationAndExactRetry(t *testing.T) { t.Fatalf("expected initial state pending, got %s", rec.State) } - // Wait until operation transitions to running - var runningRec *domain.OperationRecord - deadline := time.Now().Add(3 * time.Second) - for time.Now().Before(deadline) { - r, err := mgr.Get(rec.ID, actor) - if err == nil && r.State == domain.OpStateRunning { - runningRec = r - break - } - time.Sleep(10 * time.Millisecond) + // Backend entry proves admission completed. Verify the public state once; + // polling durable files adds I/O and couples this invariant to host load. + waitCtx, cancel := context.WithDeadline(t.Context(), op.Deadline) + defer cancel() + select { + case <-backend.entered: + case <-waitCtx.Done(): + t.Fatalf("operation did not reach the backend: %v", waitCtx.Err()) } - if runningRec == nil { - t.Fatalf("timed out waiting for operation to reach running state") + runningRec, err := mgr.Get(rec.ID, actor) + if err != nil || runningRec.State != domain.OpStateRunning { + t.Fatalf("backend entered before a running observation: %+v, %v", runningRec, err) } // Exact retry while running must return fresh running record diff --git a/internal/sessions/clock_regression_test.go b/internal/sessions/clock_regression_test.go new file mode 100644 index 0000000..5ca2612 --- /dev/null +++ b/internal/sessions/clock_regression_test.go @@ -0,0 +1,70 @@ +package sessions_test + +import ( + "context" + "sync/atomic" + "testing" + "time" + + "github.com/Horcag/agent-machine-control/internal/domain" + "github.com/Horcag/agent-machine-control/internal/sessions" +) + +func TestManagerLifecycleSurvivesBackwardClockStep(t *testing.T) { + createdAt := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC) + var wall atomic.Int64 + wall.Store(createdAt.UnixNano()) + directory := testSessionsDir(t) + transport := &deadlineGuardTransport{channel: newDeadlineGuardChannel()} + manager := sessions.NewManager(directory, transport, func() time.Time { return time.Unix(0, wall.Load()) }) + t.Cleanup(func() { _ = manager.Shutdown(context.Background()) }) + actor := deadlineGuardActor(t) + opened, err := manager.Open(t.Context(), deadlineGuardOperation(actor, "backward-clock"), 80, 24, "xterm") + if err != nil { + t.Fatal(err) + } + wall.Add(time.Minute.Nanoseconds()) + _, _, _, _, advanced, err := manager.Read(t.Context(), opened.ID, actor, 0, 100) + if err != nil || !advanced.LastActivityAt.Equal(createdAt.Add(time.Minute)) { + t.Fatalf("forward activity did not advance: %+v err=%v", advanced, err) + } + wall.Add(-time.Hour.Nanoseconds()) + if _, err := manager.Write(t.Context(), opened.ID, actor, "synthetic", "clock test", "clock-write"); err != nil { + t.Fatal(err) + } + if _, err := manager.Control(t.Context(), opened.ID, actor, domain.ControlKeyCtrlC, "clock test", "clock-control"); err != nil { + t.Fatal(err) + } + _, _, _, _, observed, err := manager.Read(t.Context(), opened.ID, actor, 0, 100) + if err != nil || observed.LastActivityAt.Before(advanced.LastActivityAt) { + t.Fatalf("activity moved before creation: %+v err=%v", observed, err) + } + wall.Store(createdAt.Add(2 * time.Minute).UnixNano()) + _, _, _, _, forward, err := manager.Read(t.Context(), opened.ID, actor, 0, 100) + if err != nil || !forward.LastActivityAt.Equal(createdAt.Add(2*time.Minute)) { + t.Fatalf("clock recovery did not advance: %+v err=%v", forward, err) + } + wall.Store(createdAt.UnixNano()) + closed, err := manager.Close(t.Context(), opened.ID, actor, "clock test cleanup") + if err != nil || closed.Validate() != nil || closed.ClosedAt.Before(forward.LastActivityAt) { + t.Fatalf("clock step corrupted closure: %+v err=%v", closed, err) + } + loaded, err := sessions.NewManager(directory, nil, nil).Get(t.Context(), opened.ID, actor) + if err != nil || loaded.State != domain.SessionStateClosed { + t.Fatalf("clock step corrupted durable state: %+v err=%v", loaded, err) + } +} + +func TestReconcileCrashedSessionSurvivesBackwardClockStep(t *testing.T) { + directory := testSessionsDir(t) + obs := testSessionObservation("sess-a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4", domain.SessionStateActive) + obs.LastActivityAt = obs.CreatedAt.Add(time.Minute) + writeSessionObservation(t, directory, obs) + if _, err := sessions.ReconcileCrashedSessions(t.Context(), directory, obs.CreatedAt.Add(-time.Hour)); err != nil { + t.Fatal(err) + } + loaded, err := sessions.NewManager(directory, nil, nil).Get(t.Context(), obs.ID, testSessionActor(t, obs.OwnerActor)) + if err != nil || loaded.Validate() != nil || loaded.ClosedAt.Before(obs.LastActivityAt) { + t.Fatalf("restart clock step corrupted durable state: %+v err=%v", loaded, err) + } +} diff --git a/internal/sessions/manager.go b/internal/sessions/manager.go index 90dbd66..a0c54c9 100644 --- a/internal/sessions/manager.go +++ b/internal/sessions/manager.go @@ -36,6 +36,8 @@ type Manager struct { sessionsDir string transport guestssh.Transport clock func() time.Time + clockMu sync.Mutex + lastNow time.Time generateSessionID func() (domain.SessionID, error) publishOpenHook func() error syncSessionDir func(string) error @@ -100,7 +102,16 @@ func NewManager(sessionsDir string, transport guestssh.Transport, clock func() t } func (m *Manager) now() time.Time { - return m.clock().UTC() + now := m.clock().UTC() + m.clockMu.Lock() + defer m.clockMu.Unlock() + // Lifecycle observations must remain causal when the wall clock steps back. + // Authorization and execution deadlines use their own clocks and contexts. + if now.Before(m.lastNow) { + return m.lastNow + } + m.lastNow = now + return now } // MutationJournal returns the durable journal owned by this session manager. diff --git a/internal/sessions/reconcile.go b/internal/sessions/reconcile.go index 91542a8..49b58f9 100644 --- a/internal/sessions/reconcile.go +++ b/internal/sessions/reconcile.go @@ -34,6 +34,9 @@ func reconcileSessionFileWithSync( } obs.State = domain.SessionStateCrashed + if now.Before(obs.LastActivityAt) { + now = obs.LastActivityAt + } obs.ClosedAt = &now obs.ErrorMessage = "daemon_crash_recovered"