Skip to content

An open-source, faster Devin alternative

TypeScript

feat(secrets): run the sealed-token proxy inside the guest and route secret rotation through the backend seam, which previously answered 200 with refreshed=0 while the box kept serving the old value

Brian Reardon32fd773b84e2
diff --git a/internal/api/projects.go b/internal/api/projects.go
index d936e785..1c8c6bf6 100644
--- a/internal/api/projects.go
+++ b/internal/api/projects.go
@@ -270,6 +270,12 @@ func (s *Server) listSecretEntries(c echo.Context) error {
// returns within this bound regardless of how many sandboxes are affected.
const secretRefreshTimeout = 15 * time.Second
+// secretRefreshBackendTimeout bounds one managed-backend refresh. Longer than
+// the 5s allowed for a gRPC hop to a worker on our own network, because this
+// call crosses a region to the box and then waits on the guest's proxy; still
+// well inside secretRefreshTimeout so the overall fanout stays bounded.
+const secretRefreshBackendTimeout = 10 * time.Second
+
// fanoutSecretRefresh tells every worker hosting a sandbox that uses this
// SecretStore to update its proxy session's value for `name` to `newValue`.
// Synchronous: caller blocks until all in-flight RPCs complete or hit the
@@ -285,7 +291,12 @@ const secretRefreshTimeout = 15 * time.Second
// — pushing to a worker that doesn't have a session for this sandbox returns
// updated=false (transient miss, logged but not fatal).
func (s *Server) fanoutSecretRefresh(parentCtx context.Context, storeID uuid.UUID, name, newValue string) (refreshed int, failures []string) {
- if s.store == nil || s.workerRegistry == nil {
+ // Deliberately NOT gated on s.workerRegistry: a cell whose sandboxes all
+ // live on a managed backend has no worker registry at all, and returning
+ // early here made every rotation on such a cell a silent no-op — HTTP 200,
+ // refreshed=0, and the guest serving its old secret for the rest of its
+ // life. pushOne asks the backend seam per sandbox instead.
+ if s.store == nil {
return 0, nil
}
@@ -316,6 +327,22 @@ func (s *Server) fanoutSecretRefresh(parentCtx context.Context, storeID uuid.UUI
// (source + destination). pushOne handles a single (sandboxID, workerID)
// pair so we can reuse for both halves of the dual-push.
pushOne := func(sandboxID, workerID string) result {
+ // The backend seam first. A managed backend holds the sandbox itself
+ // and has no entry in the worker registry, so asking the registry for
+ // it yields "unreachable" at best — and on a registry-less cell the
+ // rotation never happened at all.
+ if mgr, ok := s.execManagerFor(sandboxID); ok {
+ callCtx, callCancel := context.WithTimeout(ctx, secretRefreshBackendTimeout)
+ defer callCancel()
+ updated, err := mgr.UpdateSandboxSecret(callCtx, sandboxID, name, newValue)
+ if err != nil {
+ return result{sandboxID: sandboxID, err: fmt.Errorf("backend refresh: %w", err)}
+ }
+ return result{sandboxID: sandboxID, ok: updated}
+ }
+ if s.workerRegistry == nil {
+ return result{sandboxID: sandboxID, err: fmt.Errorf("no backend and no worker registry holds sandbox %s", sandboxID)}
+ }
client, err := s.workerRegistry.GetWorkerClient(workerID)
if err != nil {
return result{sandboxID: sandboxID, err: fmt.Errorf("worker %s unreachable: %w", workerID, err)}
diff --git a/internal/secretsproxy/endpoint_test.go b/internal/secretsproxy/endpoint_test.go
new file mode 100644
index 00000000..912c1b75
--- /dev/null
+++ b/internal/secretsproxy/endpoint_test.go
@@ -0,0 +1,48 @@
+package secretsproxy
+
+import (
+ "strings"
+ "testing"
+)
+
+// endpoint_test.go — the worker fleet must not notice the override.
+//
+// SetEndpoint exists for a proxy that does not run on a worker (the MicroVM
+// backend runs one inside the guest). Every worker leaves it unset, and if the
+// default ever stopped being the anycast address their sandboxes would be
+// pointed somewhere nothing answers — which surfaces as every outbound HTTPS
+// request timing out, on the whole fleet, with no error anywhere.
+
+func TestUnsetEndpointIsTheAnycastAddress(t *testing.T) {
+ p := newProxyForTest(t)
+ if got := p.proxyEndpoint(); got != AnycastEndpoint() {
+ t.Fatalf("proxyEndpoint() = %q, want %q — the whole QEMU fleet reads this", got, AnycastEndpoint())
+ }
+
+ env := p.CreateSealedEnvs("sbx", "10.0.0.2", "10.0.0.1", nil,
+ map[string]string{"K": "v"}, nil, nil)
+ if env["HTTPS_PROXY"] != AnycastEndpoint() {
+ t.Errorf("HTTPS_PROXY = %q, want the anycast address unchanged", env["HTTPS_PROXY"])
+ }
+ if !strings.Contains(env["HTTPS_PROXY"], AnycastIP) {
+ t.Errorf("HTTPS_PROXY lost the anycast IP: %q", env["HTTPS_PROXY"])
+ }
+}
+
+func TestSetEndpointOverridesOnlyTheProxyURL(t *testing.T) {
+ p := newProxyForTest(t)
+ p.SetEndpoint("http://127.0.0.1:3128")
+
+ env := p.CreateSealedEnvs("sbx", "127.0.0.1", "", nil,
+ map[string]string{"K": "v"}, nil, nil)
+ if env["HTTPS_PROXY"] != "http://127.0.0.1:3128" {
+ t.Errorf("HTTPS_PROXY = %q, want the override", env["HTTPS_PROXY"])
+ }
+ // Everything else about the recipe is unchanged — only the address moves.
+ if env["NO_PROXY"] == "" || env["SSL_CERT_FILE"] == "" {
+ t.Error("the override changed more than the proxy address")
+ }
+ if !strings.HasPrefix(env["K"], "osb_sealed_") {
+ t.Error("the override affected sealing")
+ }
+}
diff --git a/internal/secretsproxy/proxy.go b/internal/secretsproxy/proxy.go
index df8a390d..762f6597 100644
--- a/internal/secretsproxy/proxy.go
+++ b/internal/secretsproxy/proxy.go
@@ -135,8 +135,33 @@ type SecretsProxy struct {
// dialFunc overrides the default dialUpstream for testing.
// If nil, the default dialUpstream is used.
- dialFunc func(target, serverName string) (*tls.Conn, error)
+ dialFunc func(target, serverName string) (*tls.Conn, error)
skipPrivateIPCheck bool // for testing with loopback
+
+ // endpoint overrides what CreateSealedEnvs tells the sandbox to use as its
+ // HTTP(S)_PROXY. Empty means the anycast link-local address, which is what
+ // every worker uses and what the QEMU fleet has always been given.
+ //
+ // It exists for a proxy that does NOT run on a worker: the MicroVM backend
+ // runs one inside the guest itself, where 169.254.169.253 is nobody and the
+ // sandbox has to be pointed at loopback instead. See SetEndpoint.
+ endpoint string
+}
+
+// SetEndpoint overrides the proxy URL handed to sandboxes.
+//
+// Callers that leave this unset — every worker — keep AnycastEndpoint()
+// exactly as before. Only a proxy reachable at some other address needs it, and
+// getting it wrong is not subtle: the sandbox's egress goes to an address
+// nothing answers and every outbound request times out.
+func (p *SecretsProxy) SetEndpoint(url string) { p.endpoint = url }
+
+// proxyEndpoint is the address sandboxes are told to use.
+func (p *SecretsProxy) proxyEndpoint() string {
+ if p.endpoint != "" {
+ return p.endpoint
+ }
+ return AnycastEndpoint()
}
// NewSecretsProxy creates a new secrets proxy. Call Start() to begin accepting.
@@ -357,7 +382,7 @@ func (p *SecretsProxy) CreateSealedEnvs(sandboxID, guestIP, gatewayIP string, pl
// VM reaches the right proxy regardless of which host it's currently on.
// gatewayIP is still a parameter for backwards compat / non-anycast paths.
_ = gatewayIP
- proxyURL := AnycastEndpoint()
+ proxyURL := p.proxyEndpoint()
caCertPath := "/usr/local/share/ca-certificates/opensandbox-proxy.crt"
noProxy := "localhost,127.0.0.1,::1"