Skip to content

An open-source, faster Devin alternative

TypeScript

make sure we're populating secret store id

Brian Reardon01968746090c
diff --git a/internal/api/image_builder.go b/internal/api/image_builder.go
index 9d1d4d80..dd025844 100644
--- a/internal/api/image_builder.go
+++ b/internal/api/image_builder.go
@@ -442,7 +442,9 @@ func (s *Server) buildImage(ctx context.Context, orgID uuid.UUID, manifest *Imag
if workerID == "" {
workerID = "w-local-1"
}
- _, _ = s.store.CreateSandboxSession(ctx, buildSandboxID, orgID, nil, cfg.Template, region, workerID, cfgJSON, []byte("{}"))
+ // Image-builder sandboxes never bind to a customer secret store —
+ // secret_store_id stays NULL. They run a Dockerfile build, not user code.
+ _, _ = s.store.CreateSandboxSession(ctx, buildSandboxID, orgID, nil, cfg.Template, region, workerID, cfgJSON, []byte("{}"), nil)
if s.workerRegistry != nil {
if w := s.workerRegistry.GetWorker(workerID); w != nil && w.GoldenVersion != "" {
_ = s.store.SetSandboxGoldenVersion(ctx, buildSandboxID, w.GoldenVersion)
diff --git a/internal/api/sandbox.go b/internal/api/sandbox.go
index 4355c9c6..275ea027 100644
--- a/internal/api/sandbox.go
+++ b/internal/api/sandbox.go
@@ -149,8 +149,11 @@ func (s *Server) createSandbox(c echo.Context) error {
// Fresh-create path: resolve the secret store (if any) into cfg.SecretEnvs.
// This is intentionally below the snapshot/image branch so the snapshot
// path never resolves a user-supplied store — forks inherit only.
+ var secretStoreID *uuid.UUID
if cfg.SecretStore != "" && hasOrg {
- if err := s.resolveSecretStoreInto(ctx, orgID, &cfg); err != nil {
+ var err error
+ secretStoreID, err = s.resolveSecretStoreInto(ctx, orgID, &cfg)
+ if err != nil {
log.Printf("api: %v", err)
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}
@@ -158,7 +161,7 @@ func (s *Server) createSandbox(c echo.Context) error {
// Server mode with worker registry: dispatch to remote worker via gRPC
if s.workerRegistry != nil {
- return s.createSandboxRemote(c, ctx, cfg, orgID, hasOrg)
+ return s.createSandboxRemote(c, ctx, cfg, orgID, hasOrg, secretStoreID)
}
// Combined/worker mode: create locally
@@ -219,7 +222,7 @@ func (s *Server) createSandbox(c echo.Context) error {
if template == "" {
template = "default"
}
- _, _ = s.store.CreateSandboxSession(ctx, sb.ID, orgID, auth.GetUserID(c), template, region, workerID, cfgJSON, metadataJSON)
+ _, _ = s.store.CreateSandboxSession(ctx, sb.ID, orgID, auth.GetUserID(c), template, region, workerID, cfgJSON, metadataJSON, secretStoreID)
}
return c.JSON(http.StatusCreated, sb)
@@ -315,23 +318,28 @@ func (s *Server) createSandboxWithSSE(c echo.Context, ctx context.Context, orgID
// happens, so the same logic runs on both fresh creates and on
// fork-from-checkpoint inheritance. If the user updates a secret between
// snapshot and fork, the fork sees the new value.
-func (s *Server) resolveSecretStoreInto(ctx context.Context, orgID [16]byte, cfg *types.SandboxConfig) error {
+// resolveSecretStoreInto looks up the named secret store, decrypts entries
+// into cfg, and returns the store's UUID (or nil if no store was bound).
+// The UUID is plumbed back to CreateSandboxSession so sandbox_sessions.
+// secret_store_id gets populated — required for the secret-refresh fanout
+// (ListRunningSandboxesByStore filters on this column).
+func (s *Server) resolveSecretStoreInto(ctx context.Context, orgID [16]byte, cfg *types.SandboxConfig) (*uuid.UUID, error) {
if cfg.SecretStore == "" || s.store == nil {
- return nil
+ return nil, nil
}
store, err := s.store.GetSecretStoreByName(ctx, orgID, cfg.SecretStore)
if err != nil {
- return fmt.Errorf("secret store not found: %s", cfg.SecretStore)
+ return nil, fmt.Errorf("secret store not found: %s", cfg.SecretStore)
}
cfg.EgressAllowlist = store.EgressAllowlist
secrets, err := s.store.DecryptSecretEntries(ctx, store.ID)
if err != nil {
- return fmt.Errorf("failed to decrypt secrets for store %s: %w", cfg.SecretStore, err)
+ return nil, fmt.Errorf("failed to decrypt secrets for store %s: %w", cfg.SecretStore, err)
}
if len(secrets) == 0 {
- return nil
+ return &store.ID, nil
}
if cfg.SecretEnvs == nil {
cfg.SecretEnvs = make(map[string]string, len(secrets))
@@ -345,7 +353,7 @@ func (s *Server) resolveSecretStoreInto(ctx context.Context, orgID [16]byte, cfg
cfg.SecretAllowedHosts[secret.Name] = secret.AllowedHosts
}
}
- return nil
+ return &store.ID, nil
}
// cfgForPersistence returns a copy of cfg suitable for marshaling into PG
@@ -396,7 +404,10 @@ func flattenSecretAllowedHosts(m map[string][]string) map[string]string {
}
// createSandboxRemote dispatches sandbox creation to a remote worker via gRPC.
-func (s *Server) createSandboxRemote(c echo.Context, ctx context.Context, cfg types.SandboxConfig, orgID [16]byte, hasOrg bool) error {
+// secretStoreID is the resolved store UUID (or nil) from resolveSecretStoreInto;
+// passed straight to CreateSandboxSessionWithStatus so the row's secret_store_id
+// column is populated and the secret-refresh fanout can find this sandbox.
+func (s *Server) createSandboxRemote(c echo.Context, ctx context.Context, cfg types.SandboxConfig, orgID [16]byte, hasOrg bool, secretStoreID *uuid.UUID) error {
// Select region (explicit header, or default to server's region)
region := c.Request().Header.Get("Fly-Region")
if region == "" {
@@ -474,7 +485,7 @@ func (s *Server) createSandboxRemote(c echo.Context, ctx context.Context, cfg ty
}
cfgJSON, _ := json.Marshal(cfgForPersistence(cfg))
metadataJSON, _ := json.Marshal(cfg.Metadata)
- _, _ = s.store.CreateSandboxSessionWithStatus(ctx, sandboxID, orgID, auth.GetUserID(c), template, region, worker.ID, cfgJSON, metadataJSON, "pending")
+ _, _ = s.store.CreateSandboxSessionWithStatus(ctx, sandboxID, orgID, auth.GetUserID(c), template, region, worker.ID, cfgJSON, metadataJSON, "pending", secretStoreID)
if worker.GoldenVersion != "" {
_ = s.store.SetSandboxGoldenVersion(ctx, sandboxID, worker.GoldenVersion)
}
@@ -2289,13 +2300,23 @@ func (s *Server) createFromCheckpointCore(c echo.Context, userEnvs map[string]st
uniqueStores = append(uniqueStores, name)
}
}
+ // secretStoreID tracks the resolved store of the LAST (winning) layer in
+ // uniqueStores — that's the one we want recorded on the sandbox row, since
+ // later layers shadow earlier ones for env collisions. Plumbed back to
+ // CreateSandboxSessionWithStatus below so secret_store_id is populated for
+ // the refresh fanout.
+ var secretStoreID *uuid.UUID
if len(uniqueStores) > 0 {
var allEgress []string
for _, storeName := range uniqueStores {
originalCfg.SecretStore = storeName
- if err := s.resolveSecretStoreInto(ctx, orgID, &originalCfg); err != nil {
+ storeID, err := s.resolveSecretStoreInto(ctx, orgID, &originalCfg)
+ if err != nil {
return nil, http.StatusBadRequest, err
}
+ if storeID != nil {
+ secretStoreID = storeID
+ }
allEgress = append(allEgress, originalCfg.EgressAllowlist...)
}
// Deduplicate egress
@@ -2409,7 +2430,7 @@ func (s *Server) createFromCheckpointCore(c echo.Context, userEnvs map[string]st
if s.store != nil {
cfgJSON, _ := json.Marshal(cfgForPersistence(originalCfg))
metadataJSON, _ := json.Marshal(mergedMeta)
- _, _ = s.store.CreateSandboxSessionWithStatus(ctx, sandboxID, orgID, auth.GetUserID(c), template, region, workerID, cfgJSON, metadataJSON, "pending")
+ _, _ = s.store.CreateSandboxSessionWithStatus(ctx, sandboxID, orgID, auth.GetUserID(c), template, region, workerID, cfgJSON, metadataJSON, "pending", secretStoreID)
}
// Boot VM synchronously so the worker's in-memory sandbox map is populated
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.go
index 142afc3d..5147dce4 100644
--- a/internal/controlplane/server.go
+++ b/internal/controlplane/server.go
@@ -8,6 +8,7 @@ import (
"strings"
"time"
+ "github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"google.golang.org/grpc"
@@ -132,11 +133,13 @@ func (s *Server) createSandbox(c echo.Context) error {
// Resolve secret store: decrypt secrets + inherit egress allowlist
var egressAllowlist []string
var secretAllowedHosts map[string]string // env var name → comma-separated hosts (for proto)
+ var secretStoreID *uuid.UUID // populated below; passed to CreateSandboxSession so the row's secret_store_id column is set for the refresh fanout
if req.SecretStore != "" {
store, err := s.store.GetSecretStoreByName(c.Request().Context(), orgID, req.SecretStore)
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "secret store not found: " + req.SecretStore})
}
+ secretStoreID = &store.ID
egressAllowlist = store.EgressAllowlist
@@ -217,7 +220,7 @@ func (s *Server) createSandbox(c echo.Context) error {
}
cfgJSON, _ := json.Marshal(req)
metadataJSON, _ := json.Marshal(req.Metadata)
- _, _ = s.store.CreateSandboxSession(ctx, grpcResp.SandboxId, orgID, auth.GetUserID(c), template, region, worker.ID, cfgJSON, metadataJSON)
+ _, _ = s.store.CreateSandboxSession(ctx, grpcResp.SandboxId, orgID, auth.GetUserID(c), template, region, worker.ID, cfgJSON, metadataJSON, secretStoreID)
// Persist golden version from worker heartbeat so the scaler can read it
// from PG instead of relying on in-memory state via gRPC.
diff --git a/internal/db/store.go b/internal/db/store.go
index 9f984573..2931a5d7 100644
--- a/internal/db/store.go
+++ b/internal/db/store.go
@@ -516,17 +516,25 @@ type SandboxSession struct {
GoldenVersion *string `json:"goldenVersion,omitempty"`
}
-func (s *Store) CreateSandboxSession(ctx context.Context, sandboxID string, orgID uuid.UUID, userID *uuid.UUID, template, region, workerID string, config, metadata json.RawMessage) (*SandboxSession, error) {
- return s.CreateSandboxSessionWithStatus(ctx, sandboxID, orgID, userID, template, region, workerID, config, metadata, "running")
-}
-
-func (s *Store) CreateSandboxSessionWithStatus(ctx context.Context, sandboxID string, orgID uuid.UUID, userID *uuid.UUID, template, region, workerID string, config, metadata json.RawMessage, status string) (*SandboxSession, error) {
+func (s *Store) CreateSandboxSession(ctx context.Context, sandboxID string, orgID uuid.UUID, userID *uuid.UUID, template, region, workerID string, config, metadata json.RawMessage, secretStoreID *uuid.UUID) (*SandboxSession, error) {
+ return s.CreateSandboxSessionWithStatus(ctx, sandboxID, orgID, userID, template, region, workerID, config, metadata, "running", secretStoreID)
+}
+
+// CreateSandboxSessionWithStatus inserts a new sandbox_sessions row.
+//
+// secretStoreID is the resolved secret_stores.id when the sandbox is bound to
+// a store (nil otherwise). The column is required for the secret-refresh
+// fan-out: ListRunningSandboxesByStore filters on it. Pre-fix the column was
+// silently NULL because the INSERT omitted it, which made the fanout a no-op
+// and meant a customer's PUT to /secret-stores/:id/entries/:name didn't
+// propagate to running sandboxes — they kept the value from create-time.
+func (s *Store) CreateSandboxSessionWithStatus(ctx context.Context, sandboxID string, orgID uuid.UUID, userID *uuid.UUID, template, region, workerID string, config, metadata json.RawMessage, status string, secretStoreID *uuid.UUID) (*SandboxSession, error) {
session := &SandboxSession{}
err := s.pool.QueryRow(ctx,
- `INSERT INTO sandbox_sessions (sandbox_id, org_id, user_id, template, region, worker_id, config, metadata, status)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
+ `INSERT INTO sandbox_sessions (sandbox_id, org_id, user_id, template, region, worker_id, config, metadata, status, secret_store_id)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING id, sandbox_id, org_id, user_id, template, region, worker_id, status, config, metadata, started_at, based_on_checkpoint_id, last_patch_sequence, patch_error, golden_version`,
- sandboxID, orgID, userID, template, region, workerID, config, metadata, status,
+ sandboxID, orgID, userID, template, region, workerID, config, metadata, status, secretStoreID,
).Scan(&session.ID, &session.SandboxID, &session.OrgID, &session.UserID, &session.Template,
&session.Region, &session.WorkerID, &session.Status, &session.Config, &session.Metadata, &session.StartedAt,
&session.BasedOnCheckpointID, &session.LastPatchSequence, &session.PatchError, &session.GoldenVersion)