Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/planner/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
69 changes: 66 additions & 3 deletions internal/planner/secret_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Bind workflow grant refs with the job refs

When a workflow step’s connections grant and the job’s secretEnv declare the same contextual secret, this binds only the job ref; renderConnections leaves the grant as secret://KEY. The runner matches grants to job.SecretRefs by exact ref, so buildConnectionPayloads then fails with a grant-not-among-job-refs error. Bind the workflow grant through the same context too, or normalize it during lookup.

Useful? React with 👍 / 👎.

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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Comment on lines +225 to +226

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Bind from the effective CLI workspace context

When execution.state.workspace is omitted but the workspace comes from ORUN_WORKSPACE, a repo link, or orun workspace use, the CLI passes an empty workspace here: generatePlan initializes JobPlanner.Workspace from intentScope(intent) only. bindContext therefore leaves secret://KEY unbound, and the run still sends a ref missing the workspace that the backend requires. Pass the effective workspace and project scope into the planner before binding contextual refs.

Useful? React with 👍 / 👎.

}
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
Expand Down
78 changes: 78 additions & 0 deletions internal/planner/secret_bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
10 changes: 10 additions & 0 deletions specs/orun-scope-engine/implementation-plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions specs/orun-secrets/data-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading