Repository navigation
feat: macOS desktop role and managed browser (PR4) #501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
chruffins
wants to merge
3
commits into
feat/macos-guest-service
Choose a base branch
from
feat/macos-desktop-browser
base: feat/macos-guest-service
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "net" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/go-chi/chi/v5" | ||
| "github.com/kernel/hypeman/lib/desktop" | ||
| "github.com/kernel/hypeman/lib/hypervisor" | ||
| "github.com/kernel/hypeman/lib/instances" | ||
| mw "github.com/kernel/hypeman/lib/middleware" | ||
| ) | ||
|
|
||
| // CDPHandler requires instance-write authentication and resolution in the router. | ||
| func (s *ApiService) CDPHandler(w http.ResponseWriter, r *http.Request) { | ||
| s.serveDesktop(w, r, newDesktopTransport) | ||
| } | ||
|
|
||
| func newDesktopTransport(inst *instances.Instance) (*http.Transport, error) { | ||
| dialer, err := hypervisor.NewVsockDialer(hypervisor.Type(inst.HypervisorType), inst.VsockSocket, inst.VsockCID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &http.Transport{DisableKeepAlives: true, ResponseHeaderTimeout: 20 * time.Second, DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { | ||
| return dialer.DialVsock(ctx, int(desktop.AgentPort)) | ||
| }}, nil | ||
| } | ||
|
|
||
| func (s *ApiService) serveDesktop(w http.ResponseWriter, r *http.Request, transportFor func(*instances.Instance) (*http.Transport, error)) { | ||
| inst := mw.GetResolvedInstance[instances.Instance](r.Context()) | ||
| if inst == nil { | ||
| http.Error(w, "instance not resolved", 500) | ||
| return | ||
| } | ||
| if inst.MacOS == nil || hypervisor.Type(inst.HypervisorType) != hypervisor.TypeVZ || inst.MacOS.DesktopAgentUID == 0 || inst.SkipGuestAgent { | ||
| http.Error(w, "instance does not declare an enabled macOS desktop agent", 501) | ||
| return | ||
| } | ||
| if inst.State != instances.StateRunning { | ||
| http.Error(w, "instance must be running", 409) | ||
| return | ||
| } | ||
| if s.Config == nil || s.Config.MacOSDesktopOrigin == "" { | ||
| http.Error(w, "desktop API origin not configured", 503) | ||
| return | ||
| } | ||
| origin, err := desktop.ParseOrigin(s.Config.MacOSDesktopOrigin) | ||
| if err != nil { | ||
| http.Error(w, "invalid desktop API origin", 503) | ||
| return | ||
| } | ||
| if origins := r.Header.Values("Origin"); len(origins) > 1 || len(origins) == 1 && !desktop.SameOrigin(origins[0], origin) { | ||
| http.Error(w, "cross-origin desktop access rejected", 403) | ||
| return | ||
| } | ||
| selector := chi.URLParam(r, "id") | ||
| if selector == "" { | ||
| selector = inst.Id | ||
| } | ||
| prefix := "/instances/" + selector + "/cdp" | ||
| if !strings.HasPrefix(r.URL.Path, prefix+"/") || desktop.ValidateBodylessRequest(r) != nil { | ||
| http.Error(w, "invalid desktop request", 400) | ||
| return | ||
| } | ||
| path := strings.TrimPrefix(r.URL.Path, prefix) | ||
| status := path == "/status" && r.Method == http.MethodGet | ||
| start := path == "/start" && r.Method == http.MethodPost | ||
| request := r.Clone(r.Context()) | ||
| request.URL.Path = path | ||
| if !status && !start { | ||
| if err := desktop.ValidateCDPRequest(request); err != nil { | ||
| http.Error(w, "unsupported CDP request", 400) | ||
| return | ||
| } | ||
| } | ||
| s.desktopSlotsOnce.Do(func() { s.desktopSlots = make(chan struct{}, desktop.MaxSessions) }) | ||
| select { | ||
| case s.desktopSlots <- struct{}{}: | ||
| defer func() { <-s.desktopSlots }() | ||
| default: | ||
| http.Error(w, "desktop session limit reached", http.StatusTooManyRequests) | ||
| return | ||
| } | ||
| transport, err := transportFor(inst) | ||
| if err != nil { | ||
| http.Error(w, "desktop transport unavailable", 503) | ||
| return | ||
| } | ||
| defer transport.CloseIdleConnections() | ||
| timeout := 3 * time.Second | ||
| if start { | ||
| timeout = 20 * time.Second | ||
| } | ||
| ctx, cancel := context.WithTimeout(r.Context(), timeout) | ||
| state, err := desktop.Probe(ctx, transport, inst.MacOS.DesktopAgentUID, start) | ||
| cancel() | ||
| if err != nil { | ||
| http.Error(w, "selected desktop agent unavailable or incompatible", 503) | ||
| return | ||
| } | ||
| if status || start { | ||
| w.Header().Set("Content-Type", "application/json") | ||
| w.Header().Set("Cache-Control", "no-store") | ||
| _ = json.NewEncoder(w).Encode(struct { | ||
| desktop.Status | ||
| DesktopReady bool `json:"desktop_ready"` | ||
| }{state, state.SessionReady()}) | ||
| return | ||
| } | ||
| if !state.BrowserReady { | ||
| http.Error(w, "selected desktop session or managed browser not ready", 409) | ||
| return | ||
| } | ||
| scheme := "ws" | ||
| if origin.Scheme == "https" { | ||
| scheme = "wss" | ||
| } | ||
| base := scheme + "://" + origin.Host + "/instances/" + url.PathEscape(inst.Id) + "/cdp" | ||
| proxy, err := desktop.NewCDPProxy(transport, base) | ||
| if err != nil { | ||
| http.Error(w, "invalid instance CDP configuration", 503) | ||
| return | ||
| } | ||
| proxy.ServeHTTP(w, request) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "net" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/go-chi/chi/v5" | ||
| "github.com/golang-jwt/jwt/v5" | ||
| "github.com/kernel/hypeman/cmd/api/config" | ||
| "github.com/kernel/hypeman/lib/desktop" | ||
| "github.com/kernel/hypeman/lib/images" | ||
| "github.com/kernel/hypeman/lib/instances" | ||
| mw "github.com/kernel/hypeman/lib/middleware" | ||
| "github.com/kernel/hypeman/lib/scopes" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type desktopInstances struct { | ||
| instances.Manager | ||
| inst *instances.Instance | ||
| lookups atomic.Int32 | ||
| } | ||
|
|
||
| func (m *desktopInstances) GetInstance(_ context.Context, name string) (*instances.Instance, error) { | ||
| m.lookups.Add(1) | ||
| if name != "test" && name != "alias" { | ||
| return nil, instances.ErrNotFound | ||
| } | ||
| return m.inst, nil | ||
| } | ||
| func desktopInstance() *instances.Instance { | ||
| return &instances.Instance{StoredMetadata: instances.StoredMetadata{Id: "test", HypervisorType: "vz", MacOS: &images.MacOSImage{DesktopAgentUID: 501}}, State: instances.StateRunning} | ||
| } | ||
| func desktopToken(t *testing.T, permission string) string { | ||
| t.Helper() | ||
| s, err := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{"sub": "test", "exp": time.Now().Add(time.Hour).Unix(), "permissions": []string{permission}}).SignedString([]byte("synthetic-test-secret")) | ||
| require.NoError(t, err) | ||
| return s | ||
| } | ||
| func desktopRouter(s *ApiService, factory func(*instances.Instance) (*http.Transport, error)) http.Handler { | ||
| r := chi.NewRouter() | ||
| sub := r.With(mw.JwtAuth("synthetic-test-secret"), scopes.RequireScope(scopes.InstanceWrite), mw.ResolveResource(s.NewResolvers(), ResolverErrorResponder)) | ||
| h := func(w http.ResponseWriter, r *http.Request) { s.serveDesktop(w, r, factory) } | ||
| sub.Get("/instances/{id}/cdp/*", h) | ||
| sub.Post("/instances/{id}/cdp/start", h) | ||
| return r | ||
| } | ||
|
|
||
| func TestDesktopAdmissionBeforeDial(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| name string | ||
| change func(*instances.Instance, *config.Config, *http.Request) | ||
| code int | ||
| }{ | ||
| {"missing token", func(_ *instances.Instance, _ *config.Config, r *http.Request) { r.Header.Del("Authorization") }, 401}, | ||
| {"read-only", func(_ *instances.Instance, _ *config.Config, r *http.Request) { | ||
| r.Header.Set("Authorization", "Bearer "+desktopToken(t, string(scopes.InstanceRead))) | ||
| }, 403}, | ||
| {"undeclared", func(i *instances.Instance, _ *config.Config, _ *http.Request) { i.MacOS.DesktopAgentUID = 0 }, 501}, | ||
| {"disabled", func(i *instances.Instance, _ *config.Config, _ *http.Request) { i.SkipGuestAgent = true }, 501}, | ||
| {"stopped", func(i *instances.Instance, _ *config.Config, _ *http.Request) { i.State = instances.StateStopped }, 409}, | ||
| {"unknown instance", func(_ *instances.Instance, _ *config.Config, r *http.Request) { | ||
| r.URL.Path = "/instances/other/cdp/json/version" | ||
| }, 404}, | ||
| {"no origin config", func(_ *instances.Instance, c *config.Config, _ *http.Request) { c.MacOSDesktopOrigin = "" }, 503}, | ||
| {"wrong origin", func(_ *instances.Instance, _ *config.Config, r *http.Request) { | ||
| r.Header.Set("Origin", "https://evil.example") | ||
| }, 403}, | ||
| {"multiple origins", func(_ *instances.Instance, _ *config.Config, r *http.Request) { | ||
| r.Header.Add("Origin", "https://api.example") | ||
| r.Header.Add("Origin", "https://api.example") | ||
| }, 403}, | ||
| {"unsafe discovery", func(_ *instances.Instance, _ *config.Config, r *http.Request) { | ||
| r.URL.Path = "/instances/test/cdp/json/new" | ||
| }, 400}, | ||
| {"query", func(_ *instances.Instance, _ *config.Config, r *http.Request) { r.URL.RawQuery = "token=secret" }, 400}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| i := desktopInstance() | ||
| c := &config.Config{MacOSDesktopOrigin: "https://api.example"} | ||
| m := &desktopInstances{inst: i} | ||
| s := &ApiService{Config: c, InstanceManager: m} | ||
| r := httptest.NewRequest("GET", "http://spoof.example/instances/test/cdp/json/version", nil) | ||
| r.Header.Set("Authorization", "Bearer "+desktopToken(t, string(scopes.InstanceWrite))) | ||
| tc.change(i, c, r) | ||
| w := httptest.NewRecorder() | ||
| desktopRouter(s, func(*instances.Instance) (*http.Transport, error) { t.Fatal("dial before admission"); return nil, nil }).ServeHTTP(w, r) | ||
| require.Equal(t, tc.code, w.Code, w.Body.String()) | ||
| if tc.code == 401 || tc.name == "read-only" { | ||
| require.Zero(t, m.lookups.Load()) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestDesktopHandshakeAndDiscovery(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| name string | ||
| uid uint32 | ||
| ready bool | ||
| code int | ||
| }{{"ready", 501, true, 200}, {"wrong desktop user", 502, true, 503}, {"root desktop user", 0, true, 503}, {"browser not ready", 501, false, 409}} { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| var calls atomic.Int32 | ||
| guest := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| calls.Add(1) | ||
| require.Empty(t, r.Header.Get("Authorization")) | ||
| require.Empty(t, r.Header.Get("Cookie")) | ||
| require.Empty(t, r.Header.Get("Forwarded")) | ||
| w.Header().Set("Content-Type", "application/json") | ||
| if r.URL.Path == "/status" { | ||
| _ = json.NewEncoder(w).Encode(desktop.Status{Version: 1, OS: "darwin", Architecture: "arm64", UID: tc.uid, ConsoleUID: tc.uid, GUISession: true, BrowserManaged: tc.ready, BrowserReady: tc.ready}) | ||
| return | ||
| } | ||
| require.Equal(t, "/json/version", r.URL.Path) | ||
| _, _ = w.Write([]byte(`{"Browser":"Chrome/test","webSocketDebuggerUrl":"ws://127.0.0.1:9222/devtools/browser/test-id"}`)) | ||
| })) | ||
| defer guest.Close() | ||
| m := &desktopInstances{inst: desktopInstance()} | ||
| s := &ApiService{Config: &config.Config{MacOSDesktopOrigin: "https://api.example"}, InstanceManager: m} | ||
| factory := func(*instances.Instance) (*http.Transport, error) { | ||
| return &http.Transport{DisableKeepAlives: true, DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { | ||
| return (&net.Dialer{}).DialContext(ctx, "tcp", strings.TrimPrefix(guest.URL, "http://")) | ||
| }}, nil | ||
| } | ||
| r := httptest.NewRequest("GET", "http://spoof.example/instances/alias/cdp/json/version", nil) | ||
| r.Header.Set("Authorization", "Bearer "+desktopToken(t, string(scopes.InstanceWrite))) | ||
| r.Header.Set("Cookie", "secret=value") | ||
| r.Header.Set("Forwarded", "host=evil.example") | ||
| r.Header.Set("Origin", "https://api.example") | ||
| w := httptest.NewRecorder() | ||
| desktopRouter(s, factory).ServeHTTP(w, r) | ||
| require.Equal(t, tc.code, w.Code, w.Body.String()) | ||
| if tc.code == 200 { | ||
| require.Contains(t, w.Body.String(), "wss://api.example/instances/test/cdp/devtools/browser/test-id") | ||
| require.Equal(t, int32(2), calls.Load()) | ||
| } else { | ||
| require.Equal(t, int32(1), calls.Load()) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "net" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/gorilla/websocket" | ||
| "github.com/kernel/hypeman/cmd/api/config" | ||
| "github.com/kernel/hypeman/lib/desktop" | ||
| "github.com/kernel/hypeman/lib/instances" | ||
| "github.com/kernel/hypeman/lib/scopes" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestDesktopWebsocketReconnect(t *testing.T) { | ||
| guest := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.Header.Get("Authorization") != "" || r.Header.Get("Cookie") != "" || r.Header.Get("Origin") != "" { | ||
| t.Error("guest received API authority") | ||
| http.Error(w, "unexpected headers", 400) | ||
| return | ||
| } | ||
| if r.URL.Path == "/status" { | ||
| _ = json.NewEncoder(w).Encode(desktop.Status{Version: 1, OS: "darwin", Architecture: "arm64", UID: 501, ConsoleUID: 501, GUISession: true, BrowserManaged: true, BrowserReady: true}) | ||
| return | ||
| } | ||
| if r.URL.Path != "/devtools/browser/test-id" { | ||
| t.Error("unexpected browser route") | ||
| http.Error(w, "bad route", 400) | ||
| return | ||
| } | ||
| upgrader := websocket.Upgrader{} | ||
| conn, err := upgrader.Upgrade(w, r, nil) | ||
| if err != nil { | ||
| t.Error(err) | ||
| return | ||
| } | ||
| defer conn.Close() | ||
| kind, data, err := conn.ReadMessage() | ||
| if err != nil { | ||
| t.Error(err) | ||
| return | ||
| } | ||
| if err := conn.WriteMessage(kind, data); err != nil { | ||
| t.Error(err) | ||
| } | ||
| })) | ||
| defer guest.Close() | ||
| s := &ApiService{Config: &config.Config{MacOSDesktopOrigin: "https://api.example"}, InstanceManager: &desktopInstances{inst: desktopInstance()}} | ||
| factory := func(*instances.Instance) (*http.Transport, error) { | ||
| return &http.Transport{DisableKeepAlives: true, DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { | ||
| return (&net.Dialer{}).DialContext(ctx, "tcp", strings.TrimPrefix(guest.URL, "http://")) | ||
| }}, nil | ||
| } | ||
| api := httptest.NewServer(desktopRouter(s, factory)) | ||
| defer api.Close() | ||
| for i := 0; i < 2; i++ { | ||
| headers := http.Header{"Authorization": {"Bearer " + desktopToken(t, string(scopes.InstanceWrite))}, "Cookie": {"secret=value"}, "Origin": {"https://api.example"}} | ||
| c, _, err := websocket.DefaultDialer.Dial("ws"+strings.TrimPrefix(api.URL, "http")+"/instances/test/cdp/devtools/browser/test-id", headers) | ||
| require.NoError(t, err) | ||
| require.NoError(t, c.WriteMessage(websocket.TextMessage, []byte(`{"id":1,"method":"Browser.getVersion"}`))) | ||
| _, data, err := c.ReadMessage() | ||
| require.NoError(t, err) | ||
| require.Contains(t, string(data), "Browser.getVersion") | ||
| require.NoError(t, c.Close()) | ||
| require.Eventually(t, func() bool { return len(s.desktopSlots) == 0 }, 2*time.Second, 10*time.Millisecond) | ||
| } | ||
| } | ||
|
|
||
| func TestDesktopActiveSessionLimit(t *testing.T) { | ||
| s := &ApiService{Config: &config.Config{MacOSDesktopOrigin: "https://api.example"}, InstanceManager: &desktopInstances{inst: desktopInstance()}} | ||
| s.desktopSlotsOnce.Do(func() { s.desktopSlots = make(chan struct{}, desktop.MaxSessions) }) | ||
| for i := 0; i < desktop.MaxSessions; i++ { | ||
| s.desktopSlots <- struct{}{} | ||
| } | ||
| r := httptest.NewRequest("GET", "http://api.example/instances/test/cdp/status", nil) | ||
| r.Header.Set("Authorization", "Bearer "+desktopToken(t, string(scopes.InstanceWrite))) | ||
| w := httptest.NewRecorder() | ||
| desktopRouter(s, func(*instances.Instance) (*http.Transport, error) { | ||
| t.Fatal("dial above session limit") | ||
| return nil, nil | ||
| }).ServeHTTP(w, r) | ||
| require.Equal(t, http.StatusTooManyRequests, w.Code) | ||
| require.Len(t, s.desktopSlots, desktop.MaxSessions) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Start maps session-not-ready to 503
Medium Severity
POST /cdp/starttreats every failedProbeas503agent-incompatible. The guest already returns409when the Aqua session is not ready, butProbeonly checks for HTTP 200, so a logged-out or non-GUI console is reported as an unavailable desktop agent.Additional Locations (1)
lib/desktop/service.go#L156-L159Reviewed by Cursor Bugbot for commit f170546. Configure here.