diff --git a/internal/planner/planner.go b/internal/planner/planner.go index b952bdb2..36a8e7bd 100644 --- a/internal/planner/planner.go +++ b/internal/planner/planner.go @@ -94,7 +94,7 @@ func (jp *JobPlanner) PlanJobs(instances map[string][]*model.ComponentInstance) if mergeErr != nil { return nil, fmt.Errorf("component %s (env %s): %w", compInst.ComponentName, envName, mergeErr) } - optionalSecretRefs, optErr := mergeOptionalRefs(compInst.OptionalSecretEnv, secretRefs) + optionalSecretRefs, optErr := mergeOptionalRefs(compInst.OptionalSecretEnv, secretRefs, jp.Workspace, jp.Project, envName) if optErr != nil { return nil, fmt.Errorf("component %s (env %s): %w", compInst.ComponentName, envName, optErr) } diff --git a/internal/planner/secret_bindings.go b/internal/planner/secret_bindings.go index 4ffef8ef..1e7fd8ec 100644 --- a/internal/planner/secret_bindings.go +++ b/internal/planner/secret_bindings.go @@ -115,7 +115,16 @@ func mergeBindingRefs(secretEnv map[string]string, bindings []model.ResolvedSecr merged := make(map[string]string, len(secretEnv)+len(bindings)) for k, v := range secretEnv { - merged[k] = v + bound, ok, err := bindContext(v, workspace, project, env) + if err != nil { + return nil, fmt.Errorf("secretEnv %s: %w", k, err) + } + if !ok { + // No scope to bind from (an unlinked repo planned offline): keep the + // reference exactly as authored, as every CLI before SG3 did. + bound = v + } + merged[k] = bound } for _, b := range bindings { @@ -164,7 +173,7 @@ func mergeBindingRefs(secretEnv map[string]string, bindings []model.ResolvedSecr // visible here is an env var name the HARD map (secretEnv ∪ profile // bindings) also claims — one name cannot be fail-closed and best-effort at // once, so that is a compile error naming the key. -func mergeOptionalRefs(optionalSecretEnv, hardRefs map[string]string) (map[string]string, error) { +func mergeOptionalRefs(optionalSecretEnv, hardRefs map[string]string, workspace, project, env string) (map[string]string, error) { if len(optionalSecretEnv) == 0 { return nil, nil } @@ -173,11 +182,65 @@ func mergeOptionalRefs(optionalSecretEnv, hardRefs map[string]string) (map[strin if _, taken := hardRefs[k]; taken { return nil, fmt.Errorf("key %s is bound as both a required reference (secretEnv or a profile secretBinding) and an optionalSecretEnv reference — one env var cannot be fail-closed and best-effort at once", k) } - merged[k] = v + bound, ok, err := bindContext(v, workspace, project, env) + if err != nil { + return nil, fmt.Errorf("optionalSecretEnv %s: %w", k, err) + } + if !ok { + bound = v + } + merged[k] = bound } return merged, nil } +// bindContext fills a secret reference's omitted workspace, project and env +// from the job's own scope (specs/orun-scope-engine SG3, design.md § 2), so one +// manifest line — `secret://DATABASE_URL`, or `secret://env:prod/WIRING_X` for +// a job that reads another environment's value — works in every workspace, +// repository and environment it is planned into. +// +// It never overrides an authored dimension and never widens: the server still +// receives a fully pinned reference and still verifies its workspace and +// project against the run. A reference that is already pinned is returned in +// its AUTHORED spelling, so a manifest that did not change keeps its plan +// digest. The component dimension is left as authored: the server derives it +// from the lease, and an emitted segment is only an assertion. +// +// ok is false when a dimension is omitted and the plan has none to give; the +// caller then keeps the reference as authored, exactly as before SG3, so a plan +// that compiles today never stops compiling. A value that is not a secret:// reference, or does not parse, is +// returned unchanged for the expander's guards to report. +func bindContext(raw, workspace, project, env string) (string, bool, error) { + if !scoperef.IsSchemeRef(raw, scoperef.SchemeSecret) { + return raw, true, nil + } + ref, err := scoperef.Parse(raw) + if err != nil { + return raw, true, nil + } + if ref.Workspace != "" && ref.Project != "" && ref.Env != "" { + return raw, true, nil + } + if ref.Workspace == "" { + ref.Workspace = workspace + } + if ref.Project == "" { + ref.Project = project + } + if ref.Env == "" { + ref.Env = env + } + if ref.Workspace == "" || ref.Project == "" || ref.Env == "" { + return "", false, nil + } + bound := ref.String() + if _, err := scoperef.Parse(bound); err != nil { + return "", false, fmt.Errorf("binding the reference to the run's scope produced an invalid reference: %w", err) + } + return bound, true, nil +} + // sameRef reports whether two reference spellings denote the same reference. // An unparseable side falls back to exact string equality — the caller has // already validated the binding's own reference, and a malformed secretEnv diff --git a/internal/planner/secret_bindings_test.go b/internal/planner/secret_bindings_test.go index e34ea1a0..58950e0a 100644 --- a/internal/planner/secret_bindings_test.go +++ b/internal/planner/secret_bindings_test.go @@ -165,3 +165,81 @@ func TestPlanJobs_RequiredBindingUnmappableIsCompileError(t *testing.T) { t.Fatal("expected compile error for required-but-unmappable binding") } } + +// SG3: omitted dimensions bind from the job's scope. +func TestBindContext(t *testing.T) { + cases := []struct { + name, in, ws, prj, env, want string + ok bool + }{ + {"fully contextual", "secret://CLOUDFLARE_API_TOKEN", "ws_ABC", "web", "stage", "secret://ws_ABC/project:web/env:stage/CLOUDFLARE_API_TOKEN", true}, + {"env pinned keeps its env", "secret://env:prod/WIRING_X", "ws_ABC", "web", "stage", "secret://ws_ABC/project:web/env:prod/WIRING_X", true}, + {"workspace-wide key", "secret://ws_ABC/NPM_TOKEN", "ws_ABC", "web", "dev", "secret://ws_ABC/project:web/env:dev/NPM_TOKEN", true}, + {"version kept", "secret://DB@7", "ws_ABC", "web", "prod", "secret://ws_ABC/project:web/env:prod/DB@7", true}, + {"authored workspace is never overridden", "secret://other/KEY", "ws_ABC", "web", "prod", "secret://other/project:web/env:prod/KEY", true}, + {"fully pinned positional keeps its spelling", "secret://acme/api/prod/DB", "ws_ABC", "web", "stage", "secret://acme/api/prod/DB", true}, + {"no workspace to bind", "secret://KEY", "", "web", "prod", "", false}, + {"not a reference", "plain", "ws_ABC", "web", "prod", "plain", true}, + } + for _, c := range cases { + got, ok, err := bindContext(c.in, c.ws, c.prj, c.env) + if err != nil { + t.Fatalf("%s: unexpected error %v", c.name, err) + } + if ok != c.ok || (ok && got != c.want) { + t.Errorf("%s: bindContext(%q) = %q, %v; want %q, %v", c.name, c.in, got, ok, c.want, c.ok) + } + } +} + +func TestMergeBindingRefs_BindsContextualSecretEnv(t *testing.T) { + got, err := mergeBindingRefs(map[string]string{ + "TOKEN": "secret://CLOUDFLARE_API_TOKEN", + "PINNED": "secret://acme/api/stage/KEY", + "WIRING_PROD": "secret://env:prod/WIRING_CLOUDFLARE_KV", + }, nil, "ws_ABC", "agent-visibility", "stage") + if err != nil { + t.Fatal(err) + } + want := map[string]string{ + "TOKEN": "secret://ws_ABC/project:agent-visibility/env:stage/CLOUDFLARE_API_TOKEN", + "PINNED": "secret://acme/api/stage/KEY", + "WIRING_PROD": "secret://ws_ABC/project:agent-visibility/env:prod/WIRING_CLOUDFLARE_KV", + } + for k, v := range want { + if got[k] != v { + t.Errorf("%s = %q, want %q", k, got[k], v) + } + } +} + +func TestMergeBindingRefs_UnboundContextualIsKeptWithoutScope(t *testing.T) { + got, err := mergeBindingRefs(map[string]string{"TOKEN": "secret://KEY"}, nil, "", "", "stage") + if err != nil { + t.Fatal(err) + } + if got["TOKEN"] != "secret://KEY" { + t.Errorf("TOKEN = %q, want it kept as authored", got["TOKEN"]) + } +} + +func TestMergeBindingRefs_ContextualAgreesWithBinding(t *testing.T) { + // A contextual secretEnv entry and a binding for the same key and asEnv bind + // to the same reference, so they must not be reported as a conflict. + _, err := mergeBindingRefs(map[string]string{"CLOUDFLARE_API_TOKEN": "secret://CLOUDFLARE_API_TOKEN"}, + []model.ResolvedSecretBinding{{Key: "CLOUDFLARE_API_TOKEN", AsEnv: "CLOUDFLARE_API_TOKEN", Required: true}}, + "ws_ABC", "web", "prod") + if err != nil { + t.Fatalf("unexpected conflict: %v", err) + } +} + +func TestMergeOptionalRefs_BindsContextual(t *testing.T) { + got, err := mergeOptionalRefs(map[string]string{"ADMIN_TOKEN": "secret://ADMIN_TOKEN"}, nil, "ws_ABC", "web", "prod") + if err != nil { + t.Fatal(err) + } + if want := "secret://ws_ABC/project:web/env:prod/ADMIN_TOKEN"; got["ADMIN_TOKEN"] != want { + t.Errorf("ADMIN_TOKEN = %q, want %q", got["ADMIN_TOKEN"], want) + } +} diff --git a/specs/orun-scope-engine/implementation-plan.md b/specs/orun-scope-engine/implementation-plan.md index 2ed115e0..b5e46779 100644 --- a/specs/orun-scope-engine/implementation-plan.md +++ b/specs/orun-scope-engine/implementation-plan.md @@ -61,6 +61,16 @@ ## SG3 — Contextual omission +> **Status — binding shipped.** `bindContext` binds omitted workspace, project +> and env in the planner (`internal/planner/secret_bindings.go`) for `secretEnv` +> and `optionalSecretEnv`, from the same scope the composition `secretBindings` +> already use. Fully pinned references keep their authored spelling, so their +> plan digest does not change. Without a workspace or project to bind from (an +> unlinked repo planned offline) a reference is kept as authored rather than +> failing the plan, so nothing that compiled before stops compiling. The +> component dimension is left to the lease. **Still to do:** the `{{…}}` +> reference-slot deprecation warning. + - **Build.** - `bindContext` (`design.md` § 2): omitted dimensions bind from the component instance at expand. Never overrides an authored value; a required slot whose diff --git a/specs/orun-secrets/data-model.md b/specs/orun-secrets/data-model.md index 5e76b5f3..1876658b 100644 --- a/specs/orun-secrets/data-model.md +++ b/specs/orun-secrets/data-model.md @@ -72,12 +72,15 @@ spec: env: # existing plaintext env (unchanged) LOG_LEVEL: info secretEnv: # NEW — references only, never values - DATABASE_URL: "secret://acme/api/{{env}}/DATABASE_URL" - STRIPE_KEY: "secret://acme/api/{{env}}/STRIPE_KEY" + DATABASE_URL: "secret://DATABASE_URL" + STRIPE_KEY: "secret://STRIPE_KEY" ``` -`{{env}}` interpolates the resolving environment (existing interpolation, -`internal/expand/expander.go:321-348`). The expander gains `mergeSecretEnv` +The omitted workspace, project and environment bind from the job's own scope +when the plan is built (`bindContext`, `internal/planner/secret_bindings.go`; +`specs/orun-scope-engine` SG3), so the same line resolves in every environment, +repository and workspace. The earlier `{{env}}` spelling collapsed to `//` and +never ran. The expander gains `mergeSecretEnv` mirroring `mergeEnv`, writing `ComponentInstance.SecretEnv` (`internal/model/intent.go:329-358`). **Leak guard:** a literal (non-reference) value in `secretEnv` is a compile error.