-
Notifications
You must be signed in to change notification settings - Fork 13
feat(identity): present per-agent workload token on platform callouts (#444 item 1) #445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package runtime | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| coreruntime "github.com/initializ/forge/forge-core/runtime" | ||
| ) | ||
|
|
||
| // Per-site wire pins for X-Workload-Token (agent-identity L1, #444, PR #445 | ||
| // review): each platform callout is an N-of-N surface, so a future refactor | ||
| // dropping StampWorkloadToken at one site must fail a test HERE, not just the | ||
| // shared-helper unit tests. These cover the two forge-cli callouts (admission, | ||
| // PDP); the platform-token, authorize-URL, and remote-session sites are pinned | ||
| // in forge-core. | ||
|
|
||
| // activateWorkloadToken points the reader at a temp token file in k8s_sa mode | ||
| // and returns the token value the server should observe. | ||
| func activateWorkloadToken(t *testing.T) string { | ||
| t.Helper() | ||
| path := filepath.Join(t.TempDir(), "token") | ||
| if err := os.WriteFile(path, []byte("wl-token\n"), 0o600); err != nil { | ||
| t.Fatalf("write token file: %v", err) | ||
| } | ||
| t.Setenv(coreruntime.EnvWorkloadIdentityMode, coreruntime.WorkloadIdentityModeK8sSA) | ||
| t.Setenv(coreruntime.EnvWorkloadTokenPath, path) | ||
| return "wl-token" | ||
| } | ||
|
|
||
| func TestPlatformAdmissionChecker_PresentsWorkloadToken(t *testing.T) { | ||
| want := activateWorkloadToken(t) | ||
| var got string | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| got = r.Header.Get(coreruntime.HeaderWorkloadToken) | ||
| _, _ = w.Write([]byte(`{"decision":"admit"}`)) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| NewPlatformAdmissionChecker(srv.URL, "ag", "org-7", "ws-3", "tok", nil).Admit(context.Background()) | ||
| if got != want { | ||
| t.Errorf("admission callout %s = %q, want %q", coreruntime.HeaderWorkloadToken, got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestPDPResolver_PresentsWorkloadToken(t *testing.T) { | ||
| want := activateWorkloadToken(t) | ||
| var got string | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| got = r.Header.Get(coreruntime.HeaderWorkloadToken) | ||
| writeEnvelope(w, `{"decision":"allow","reason":"ok","policy_version":1}`) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| testResolver(srv.URL).Resolve(context.Background(), hctx("svc__op", `{}`)) | ||
| if got != want { | ||
| t.Errorf("PDP callout %s = %q, want %q", coreruntime.HeaderWorkloadToken, got, want) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package runtime | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| // Per-site wire pin for X-Workload-Token on the remote session store | ||
| // (agent-identity L1, #444, PR #445 review). setHeaders is shared across | ||
| // Load/Save/Delete, so pinning one path guards them all. | ||
| func TestRemoteSessionStore_PresentsWorkloadToken(t *testing.T) { | ||
| tokPath := filepath.Join(t.TempDir(), "token") | ||
| if err := os.WriteFile(tokPath, []byte("wl-sess\n"), 0o600); err != nil { | ||
| t.Fatalf("write token file: %v", err) | ||
| } | ||
| t.Setenv(EnvWorkloadIdentityMode, WorkloadIdentityModeK8sSA) | ||
| t.Setenv(EnvWorkloadTokenPath, tokPath) | ||
|
|
||
| var got string | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| got = r.Header.Get(HeaderWorkloadToken) | ||
| w.WriteHeader(http.StatusNotFound) // Load treats 404 as "no session" (nil, nil) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| // Load stamps setHeaders before the GET; the 404 return is irrelevant. | ||
| _, _ = newTestRemoteStore(srv.URL).Load("task-x") | ||
| if got != "wl-sess" { | ||
| t.Errorf("session-store callout %s = %q, want wl-sess", HeaderWorkloadToken, got) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package runtime | ||
|
|
||
| import ( | ||
| "io" | ||
| "net/http" | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| // Agent workload-identity presentation (agent-identity L1, issue #444, item 1). | ||
| // | ||
| // When an agent is deployed with WORKLOAD_IDENTITY_MODE=k8s_sa, agent-builder | ||
| // provisions a per-agent Kubernetes ServiceAccount and projects an | ||
| // audience-bound, kubelet-rotated SA token into a file. Forge presents that | ||
| // token on platform-authenticated callouts via the X-Workload-Token header so | ||
| // the platform's per-agent entitlement check (§19.13) can bind the call to the | ||
| // agent's workload identity — e.g. an agent bound to svc-runbooks can no longer | ||
| // fetch svc-security's token. | ||
| // | ||
| // The token's audience (initializ:platform-token-endpoint) is baked in by the | ||
| // kubelet projection; forge only reads and forwards the file, it does not mint. | ||
| const ( | ||
| // EnvWorkloadIdentityMode gates presentation. Only "k8s_sa" is handled | ||
| // today; SPIRE (attested:workload) is a later phase (#444 item 6). | ||
| EnvWorkloadIdentityMode = "WORKLOAD_IDENTITY_MODE" | ||
|
|
||
| // EnvWorkloadTokenPath overrides the projected-token file location. | ||
| EnvWorkloadTokenPath = "INITIALIZ_WORKLOAD_TOKEN_PATH" | ||
|
|
||
| // DefaultWorkloadTokenPath is where agent-builder projects the token. | ||
| DefaultWorkloadTokenPath = "/var/run/secrets/initializ.ai/workload/token" | ||
|
|
||
| // HeaderWorkloadToken carries the projected SA token to the platform. | ||
| HeaderWorkloadToken = "X-Workload-Token" | ||
|
|
||
| // WorkloadIdentityModeK8sSA is the k8s ServiceAccount-token mode. | ||
| WorkloadIdentityModeK8sSA = "k8s_sa" | ||
|
|
||
| // maxWorkloadTokenBytes bounds the projected-token read. A projected JWT | ||
| // is ~1 KB; the path is operator-controlled, so cap the read and fail | ||
| // closed to "" on an oversized/misconfigured file rather than loading an | ||
| // arbitrarily large value into an outbound header. | ||
| maxWorkloadTokenBytes = 8 << 10 // 8 KiB | ||
| ) | ||
|
|
||
| // WorkloadToken reads the projected ServiceAccount token FRESH from the | ||
| // configured path and returns it, or "" when workload identity is not active | ||
| // or no token is available. | ||
| // | ||
| // It is read on every call and never cached: the kubelet rotates the file in | ||
| // place, so a cached value goes stale and the platform's TokenReview rejects it | ||
| // (the "no_token" failure class the L1 contract warns about). | ||
| // | ||
| // Returns "" (header omitted downstream) when: | ||
| // - WORKLOAD_IDENTITY_MODE != "k8s_sa" (not a workload-identity deployment), or | ||
| // - the token file is absent, unreadable, or empty. | ||
| // | ||
| // The empty case is the normal path for non-k8s_sa deployments; presentation is | ||
| // additive and never blocks a callout. | ||
| func WorkloadToken() string { | ||
| if os.Getenv(EnvWorkloadIdentityMode) != WorkloadIdentityModeK8sSA { | ||
| return "" | ||
| } | ||
| path := os.Getenv(EnvWorkloadTokenPath) | ||
| if path == "" { | ||
| path = DefaultWorkloadTokenPath | ||
| } | ||
| f, err := os.Open(path) | ||
| if err != nil { | ||
| return "" | ||
| } | ||
| defer func() { _ = f.Close() }() | ||
| // Bounded read: one byte past the cap so an oversized file is detectable | ||
| // and rejected (fail closed to "") rather than streamed wholesale into the | ||
| // header. | ||
| b, err := io.ReadAll(io.LimitReader(f, maxWorkloadTokenBytes+1)) | ||
| if err != nil || len(b) > maxWorkloadTokenBytes { | ||
| return "" | ||
| } | ||
| // Projected token files carry a trailing newline; trim so the header value | ||
| // is the bare JWT. | ||
| return strings.TrimSpace(string(b)) | ||
| } | ||
|
|
||
| // StampWorkloadToken sets the X-Workload-Token header from the freshly-read | ||
| // projected SA token. When no token is available it leaves the header unset — | ||
| // matching the tenancy-header contract (Org-Id/Workspace-Id are omitted rather | ||
| // than sent empty), so the platform distinguishes "unset" from "empty". | ||
| // | ||
| // Call this at every platform-authenticated callout, right after the | ||
| // Authorization + tenancy headers are stamped. | ||
| func StampWorkloadToken(h http.Header) { | ||
| if tok := WorkloadToken(); tok != "" { | ||
| h.Set(HeaderWorkloadToken, tok) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change requested (representative of all five sites): this stamp has no wire-level test asserting the header is present here. Only platform_token.go has one; admission/PDP/remote-session/authorize rely on the shared helper's unit tests. Since this is a five-site N-of-N surface, a refactor could silently drop this call with nothing failing. Add a per-site "header present when active" assertion (or a table test across the five callouts).