Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/guest-desktop.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ when the same VM is enrolled again. Other VMs, host administration, enrollment c
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
Expand Down
24 changes: 19 additions & 5 deletions internal/app/console_lab.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"os"
"time"

Expand Down Expand Up @@ -94,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
Expand Down Expand Up @@ -123,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) {
Expand All @@ -136,7 +144,10 @@ func (s *ConsoleService) resolveLabBindingWithResolution(ctx context.Context, gr
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
Expand All @@ -149,11 +160,14 @@ func (s *ConsoleService) resolveLabBindingWithResolution(ctx context.Context, gr
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.requireGrantSafety(ctx, resolution, grant.Mode, grant.AcknowledgeExternalEffects)
Expand Down
25 changes: 19 additions & 6 deletions internal/app/console_lab_active.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"context"
"errors"
"fmt"
"io"
"os"
"strings"
Expand Down Expand Up @@ -68,12 +69,16 @@ func (s *ConsoleService) selectActiveLabGrant(ctx context.Context, actor domain.
continue
}
grant, err := s.loadLabGrant(ctx, id)
if err != nil || (persistentOnly && grant.Mode != ConsoleLabModePersistentVM) || 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
}
Expand All @@ -89,13 +94,21 @@ func (s *ConsoleService) scanActiveLabGrants(ctx context.Context, file *os.File,
if err != nil {
return ConsoleLabGrant{}, err
}
if candidate.GrantID != "" && (selected.GrantID == "" || candidate.IssuedAt.After(selected.IssuedAt) || (candidate.IssuedAt.Equal(selected.IssuedAt) && candidate.GrantID > selected.GrantID)) {
selected = candidate
}
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
}
75 changes: 69 additions & 6 deletions internal/app/console_lab_guest_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package app

import (
"bytes"
"context"
"github.com/Horcag/agent-machine-control/internal/domain"
"github.com/Horcag/agent-machine-control/internal/statedir"
"github.com/Horcag/agent-machine-control/internal/target"
"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) {
Expand All @@ -23,17 +26,77 @@ func TestPersistentVMGrantCoordinatesUncontainedGuestAndSurvivesRestart(t *testi
}
// 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 result, err := h.service.Execute(t.Context(), h.params("prepare", "persistent-prepare"), nil, nil); err != nil || result.Receipt == nil || h.transport.executed != 1 {
t.Fatalf("persistent guest: %+v %v effects=%d", result, err, h.transport.executed)
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 != 1 {
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
Expand Down
105 changes: 105 additions & 0 deletions internal/app/console_lab_lookup_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading
Loading