|
| 1 | +/* |
| 2 | +Copyright 2026 The Faros Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +*/ |
| 10 | + |
| 11 | +package api |
| 12 | + |
| 13 | +import ( |
| 14 | + "context" |
| 15 | + "fmt" |
| 16 | + "io" |
| 17 | + "net/http" |
| 18 | + "strings" |
| 19 | + "time" |
| 20 | +) |
| 21 | + |
| 22 | +// App Studio no longer holds a kubeconfig to the runtime cluster. The live |
| 23 | +// development data plane (logs, file sync, restart, preview readiness) is now |
| 24 | +// served by the infrastructure provider as subresources on the SandboxRunner |
| 25 | +// instance, reached through the hub backend proxy: |
| 26 | +// |
| 27 | +// {hub}/services/providers/infrastructure/dataplane/clusters/{cluster}/sandboxrunners/{name}/{verb} |
| 28 | +// |
| 29 | +// The caller's bearer token is forwarded as-is; the infra provider authorizes |
| 30 | +// the request as that caller (a tenant-scoped GET on the instance) and owns the |
| 31 | +// runtime-cluster credential. See docs/app-studio-runtime-decoupling.md. |
| 32 | +const ( |
| 33 | + infraDataPlaneProvider = "infrastructure" |
| 34 | + sandboxRunnersResource = "sandboxrunners" |
| 35 | + |
| 36 | + dataPlaneVerbLog = "log" |
| 37 | + dataPlaneVerbSync = "sync" |
| 38 | + dataPlaneVerbRestart = "restart" |
| 39 | + dataPlaneVerbProxy = "proxy" |
| 40 | + |
| 41 | + dataPlaneCallTimeout = 30 * time.Second |
| 42 | +) |
| 43 | + |
| 44 | +// sandboxDataPlaneURL composes the hub URL for a SandboxRunner data-plane verb. |
| 45 | +// tail (with leading slash) is appended after the verb — used only by the open |
| 46 | +// "proxy" verb; the control verbs leave it empty. |
| 47 | +func (s *Server) sandboxDataPlaneURL(clusterID, runnerName, verb, tail string) string { |
| 48 | + u := strings.TrimRight(s.hubBase, "/") + |
| 49 | + fmt.Sprintf("/services/providers/%s/dataplane/clusters/%s/%s/%s/%s", |
| 50 | + infraDataPlaneProvider, clusterID, sandboxRunnersResource, runnerName, verb) |
| 51 | + if tail != "" { |
| 52 | + u += tail |
| 53 | + } |
| 54 | + return u |
| 55 | +} |
| 56 | + |
| 57 | +// newSandboxDataPlaneRequest builds a data-plane request authenticated as the |
| 58 | +// caller (the same bearer token the caller authenticated to App Studio with). |
| 59 | +func (s *Server) newSandboxDataPlaneRequest(ctx context.Context, method string, id identity, runnerName, verb, tail string, body io.Reader) (*http.Request, error) { |
| 60 | + if strings.TrimSpace(s.hubBase) == "" { |
| 61 | + return nil, fmt.Errorf("hub URL is not configured; cannot reach the infrastructure data plane") |
| 62 | + } |
| 63 | + if strings.TrimSpace(id.clusterID) == "" { |
| 64 | + return nil, fmt.Errorf("no workspace cluster on request; cannot address the sandbox runner") |
| 65 | + } |
| 66 | + req, err := http.NewRequestWithContext(ctx, method, s.sandboxDataPlaneURL(id.clusterID, runnerName, verb, tail), body) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + if token := strings.TrimSpace(id.token); token != "" { |
| 71 | + req.Header.Set("Authorization", "Bearer "+token) |
| 72 | + } |
| 73 | + return req, nil |
| 74 | +} |
| 75 | + |
| 76 | +// sandboxDataPlaneClient returns an HTTP client for data-plane calls, honoring |
| 77 | +// the same TLS-skip knob the MCP client uses for hub-internal addressing. |
| 78 | +func (s *Server) sandboxDataPlaneClient(timeout time.Duration) *http.Client { |
| 79 | + return &http.Client{Timeout: timeout, Transport: projectMCPTransport(s.mcpInsecureSkipTLSVerify)} |
| 80 | +} |
| 81 | + |
| 82 | +// sandboxDataPlanePost sends a POST verb (sync, restart) and returns the body + |
| 83 | +// status code. The caller maps non-2xx to an error so the runner's own response |
| 84 | +// surfaces to the UI. |
| 85 | +func (s *Server) sandboxDataPlanePost(ctx context.Context, id identity, runnerName, verb string, payload []byte) ([]byte, int, error) { |
| 86 | + callCtx, cancel := context.WithTimeout(ctx, dataPlaneCallTimeout) |
| 87 | + defer cancel() |
| 88 | + req, err := s.newSandboxDataPlaneRequest(callCtx, http.MethodPost, id, runnerName, verb, "", strings.NewReader(string(payload))) |
| 89 | + if err != nil { |
| 90 | + return nil, 0, err |
| 91 | + } |
| 92 | + req.Header.Set("Content-Type", "application/json") |
| 93 | + resp, err := s.sandboxDataPlaneClient(dataPlaneCallTimeout).Do(req) |
| 94 | + if err != nil { |
| 95 | + return nil, 0, fmt.Errorf("sandbox data plane %s: %w", verb, err) |
| 96 | + } |
| 97 | + defer func() { _ = resp.Body.Close() }() |
| 98 | + body, err := io.ReadAll(io.LimitReader(resp.Body, 16<<20)) |
| 99 | + if err != nil { |
| 100 | + return nil, resp.StatusCode, err |
| 101 | + } |
| 102 | + return body, resp.StatusCode, nil |
| 103 | +} |
| 104 | + |
| 105 | +// sandboxDataPlaneStream proxies a streaming GET verb (logs) straight to w, |
| 106 | +// copying the upstream status and content type. |
| 107 | +func (s *Server) sandboxDataPlaneStream(ctx context.Context, id identity, runnerName, verb string, w http.ResponseWriter) error { |
| 108 | + req, err := s.newSandboxDataPlaneRequest(ctx, http.MethodGet, id, runnerName, verb, "", nil) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + // No client timeout: log streams are long-lived; ctx cancellation (request |
| 113 | + // close) ends them. |
| 114 | + resp, err := s.sandboxDataPlaneClient(0).Do(req) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("sandbox data plane %s: %w", verb, err) |
| 117 | + } |
| 118 | + defer func() { _ = resp.Body.Close() }() |
| 119 | + if ct := resp.Header.Get("Content-Type"); ct != "" { |
| 120 | + w.Header().Set("Content-Type", ct) |
| 121 | + } |
| 122 | + w.WriteHeader(resp.StatusCode) |
| 123 | + flusher, _ := w.(http.Flusher) |
| 124 | + buf := make([]byte, 32<<10) |
| 125 | + for { |
| 126 | + n, readErr := resp.Body.Read(buf) |
| 127 | + if n > 0 { |
| 128 | + if _, writeErr := w.Write(buf[:n]); writeErr != nil { |
| 129 | + return writeErr |
| 130 | + } |
| 131 | + if flusher != nil { |
| 132 | + flusher.Flush() |
| 133 | + } |
| 134 | + } |
| 135 | + if readErr == io.EOF { |
| 136 | + return nil |
| 137 | + } |
| 138 | + if readErr != nil { |
| 139 | + return readErr |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// sandboxDataPlaneProbe performs a GET against the open proxy verb (path tail) |
| 145 | +// and returns the upstream status + a bounded body, for preview readiness. |
| 146 | +func (s *Server) sandboxDataPlaneProbe(ctx context.Context, id identity, runnerName, tail string) (int, []byte, error) { |
| 147 | + callCtx, cancel := context.WithTimeout(ctx, previewReadinessProbeTimeout) |
| 148 | + defer cancel() |
| 149 | + req, err := s.newSandboxDataPlaneRequest(callCtx, http.MethodGet, id, runnerName, dataPlaneVerbProxy, tail, nil) |
| 150 | + if err != nil { |
| 151 | + return 0, nil, err |
| 152 | + } |
| 153 | + resp, err := s.sandboxDataPlaneClient(previewReadinessProbeTimeout).Do(req) |
| 154 | + if err != nil { |
| 155 | + return 0, nil, err |
| 156 | + } |
| 157 | + defer func() { _ = resp.Body.Close() }() |
| 158 | + body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) |
| 159 | + if err != nil { |
| 160 | + return resp.StatusCode, nil, err |
| 161 | + } |
| 162 | + return resp.StatusCode, body, nil |
| 163 | +} |
0 commit comments