Files
iam/internal/oidc/workload_test.go
zeekay 4b2ea484f5 oidc: a GitHub run's subject may carry its ids
GitHub signs some runs' subject with the account's and repository's
ids beside their names: a hanzo-inc/app run carries
repo:hanzo-inc@308927965/app@1345862086:ref:refs/heads/main. The
workload grant read only repo:<owner>/<name>:, so every such run was
refused with "the assertion does not name a repository" and fell back
to a stored KMS pair, or failed where none was set (zoo-apps/ngo).

The ids spelling names the repository only when both ids are the ones
the token states (repository_owner_id, repository_id); another id, or
a token that states none, is still refused.

Claude-Session: https://claude.ai/code/session_01BLLMyBFSvAfCt7ZGqYuiEz
2026-10-22 11:45:43 -07:00

954 lines
37 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package oidc
import (
"context"
"crypto/x509"
"encoding/json"
"encoding/pem"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/hanzoai/orm"
"github.com/zap-proto/zip"
"github.com/hanzoai/iam/pkg/schema"
)
// RFC 7523 — a workload authenticates with its pod's ServiceAccount token and
// receives the application token client_credentials would have given it for a
// stored secret. These tests stand up a real cluster issuer (a JWKS served over
// TLS behind its own CA) and pin both halves: the happy path mints exactly what
// the secret path mints, and every mismatch — audience, issuer, key, namespace,
// registration, expiry — fails closed.
// cluster fixture ------------------------------------------------------------
// fakeCluster is an in-test Kubernetes issuer: a signing key, a JWKS served over
// TLS, and the CA file a client needs to read it. The published key set is
// mutable so a test can rotate the cluster's signing key underneath a running
// server, which is the case no static fixture can produce.
type fakeCluster struct {
iss string
jwks string
ca string
// requires is the bearer the apiserver demands before it will serve the key
// set, and bearerFile is where IAM reads its copy — the k3s shape, where an
// anonymous read answers 401.
requires string
bearerFile string
mu sync.Mutex
keys []map[string]any
reads int
}
// newCluster starts an issuer publishing one key under kid.
func newCluster(t *testing.T, kid string) *fakeCluster {
t.Helper()
c := &fakeCluster{iss: "https://kubernetes.default.svc.cluster.local"}
c.publish(t, kid)
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.mu.Lock()
defer c.mu.Unlock()
if c.requires != "" && r.Header.Get("Authorization") != "Bearer "+c.requires {
w.WriteHeader(http.StatusUnauthorized)
return
}
c.reads++
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{"keys": c.keys})
}))
t.Cleanup(srv.Close)
c.jwks = srv.URL + "/openid/v1/jwks"
c.ca = writeCA(t, srv.Certificate())
return c
}
// demand makes the issuer refuse an anonymous read and returns the path IAM
// reads its own discovery token from. Writing the token to a FILE is the point:
// the kubelet rotates it in place, so IAM must read it per fetch.
func (c *fakeCluster) demand(t *testing.T, token string) {
t.Helper()
path := filepath.Join(t.TempDir(), "token")
if err := os.WriteFile(path, []byte(token+"\n"), 0o600); err != nil {
t.Fatalf("write discovery token: %v", err)
}
c.mu.Lock()
defer c.mu.Unlock()
c.requires, c.bearerFile = token, path
}
// rotate rewrites the discovery token on disk and at the apiserver, the way a
// kubelet refreshes a projected volume under a running process.
func (c *fakeCluster) rotate(t *testing.T, token string) {
t.Helper()
c.mu.Lock()
path := c.bearerFile
c.requires = token
c.mu.Unlock()
if err := os.WriteFile(path, []byte(token+"\n"), 0o600); err != nil {
t.Fatalf("rotate discovery token: %v", err)
}
}
// publish adds a key id to what the JWKS serves. The key MATERIAL is the shared
// test key throughout: what is under test is which kid the server can resolve,
// not which modulus it carries.
func (c *fakeCluster) publish(t *testing.T, kid string) {
t.Helper()
k := rsaJWK(&sharedKey(t).PublicKey)
k["kid"], k["alg"], k["use"] = kid, "RS256", "sig"
c.mu.Lock()
defer c.mu.Unlock()
c.keys = append(c.keys, k)
}
// fetches is how many times the JWKS has been read.
func (c *fakeCluster) fetches() int {
c.mu.Lock()
defer c.mu.Unlock()
return c.reads
}
// trust points IAM at this cluster and states which ServiceAccounts speak, each
// as "<namespace>:<name>", and the org each one speaks in.
func (c *fakeCluster) trust(t *testing.T, workloads map[string]string) {
t.Helper()
issuers, err := json.Marshal(map[string]any{
c.iss: map[string]string{"jwks_uri": c.jwks, "ca_file": c.ca, "bearer_file": c.bearerFile},
})
if err != nil {
t.Fatalf("marshal cluster config: %v", err)
}
orgs, err := json.Marshal(workloads)
if err != nil {
t.Fatalf("marshal workload config: %v", err)
}
t.Setenv(envClusters, string(issuers))
t.Setenv(envWorkloads, string(orgs))
}
// assertion signs a ServiceAccount token the way a kubelet projects one.
func (c *fakeCluster) assertion(t *testing.T, kid, sub, audience string, life time.Duration) string {
t.Helper()
return signAs(t, c.iss, kid, sub, audience, life)
}
// signAs signs an assertion under an arbitrary issuer, so a test can present one
// that is correctly signed and still names a cluster nobody configured.
func signAs(t *testing.T, iss, kid, sub, audience string, life time.Duration) string {
t.Helper()
now := nowFunc()
tok := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.RegisteredClaims{
Issuer: iss,
Subject: sub,
Audience: jwt.ClaimStrings{audience},
IssuedAt: jwt.NewNumericDate(now.Add(-time.Minute)),
NotBefore: jwt.NewNumericDate(now.Add(-time.Minute)),
ExpiresAt: jwt.NewNumericDate(now.Add(life)),
})
tok.Header["kid"] = kid
s, err := tok.SignedString(sharedKey(t))
if err != nil {
t.Fatalf("sign assertion: %v", err)
}
return s
}
// writeCA writes a certificate where a ca_file entry can name it.
func writeCA(t *testing.T, cert *x509.Certificate) string {
t.Helper()
path := filepath.Join(t.TempDir(), "cluster-ca.crt")
block := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw})
if err := os.WriteFile(path, block, 0o600); err != nil {
t.Fatalf("write ca file: %v", err)
}
return path
}
// audience is the issuer an assertion must name — the same value tokenIssuer
// resolves for the test host, so the fixture and the server cannot drift.
func audience() string { iss, _ := resolveIssuer("hanzo.id"); return iss }
// present posts an assertion at the token endpoint. No client_id and no
// client_secret travel with it: the assertion IS the authentication, which is
// the whole point of the grant.
func present(t *testing.T, app *zip.App, assertion string, extra url.Values) (int, map[string]any) {
t.Helper()
form := url.Values{
"grant_type": {grantTypeAssertion},
"assertion": {assertion},
}
for k, v := range extra {
form[k] = v
}
resp, body := do(t, app, formReq("POST", PathToken, form))
return resp.StatusCode, decode(t, body)
}
// serviceApp seeds a declared workload application, shaped the way
// provision.yaml derives one from `type: service` plus
// `grants: [urn:ietf:params:oauth:grant-type:jwt-bearer]`: clientId
// <org>-<app>, client_credentials from the type, the workload grant declared.
func serviceApp(t *testing.T, db orm.DB, clientID string) {
t.Helper()
seedApp(t, db, appOpts{
clientID: clientID,
secret: "a-secret-the-pod-never-holds",
grants: []string{"client_credentials", grantTypeAssertion},
})
}
// tests ----------------------------------------------------------------------
func TestWorkload_mintsForServiceAccount(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
app, db := newServer(t)
serviceApp(t, db, "hanzo-pkg")
status, tok := present(t, app,
c.assertion(t, "cluster-key-1", "system:serviceaccount:hanzo:pkg", audience(), time.Hour), nil)
if status != 200 {
t.Fatalf("status = %d; body=%v", status, tok)
}
access, _ := tok["access_token"].(string)
if access == "" {
t.Fatalf("no access_token; body=%v", tok)
}
if tok["token_type"] != "Bearer" {
t.Errorf("token_type = %v, want Bearer", tok["token_type"])
}
if _, ok := tok["refresh_token"]; ok {
t.Error("a machine token must carry no refresh token")
}
// The token IAM's own JWKS verifies, carrying exactly what client_credentials
// mints for this application: the app is the principal, the org is the tenant,
// and the class says a program obtained it.
claims, err := verifyToken(context.Background(), db, access)
if err != nil {
t.Fatalf("minted token does not verify: %v", err)
}
if claims.Subject != "admin/hanzo-pkg" {
t.Errorf("sub = %q, want admin/hanzo-pkg", claims.Subject)
}
if claims.Owner != "hanzo" || claims.Name != "hanzo-pkg" {
t.Errorf("owner/name = %q/%q, want hanzo / hanzo-pkg", claims.Owner, claims.Name)
}
if claims.Azp != "hanzo-pkg" {
t.Errorf("azp = %q, want hanzo-pkg", claims.Azp)
}
if claims.Type != schema.Program {
t.Errorf("type = %q, want %q", claims.Type, schema.Program)
}
if len(claims.Orgs) != 0 {
t.Errorf("a machine token carries no membership set, got %v", claims.Orgs)
}
// The mint is on the record: who presented, for which application, in which org.
rows := auditRows(t, db, schema.ActionWorkloadToken)
if len(rows) != 1 {
t.Fatalf("audit rows = %d, want 1", len(rows))
}
if rows[0].Object != "system:serviceaccount:hanzo:pkg" {
t.Errorf("audit object = %q, want the service account", rows[0].Object)
}
if rows[0].User != "hanzo/hanzo-pkg" || rows[0].Owner != "hanzo" {
t.Errorf("audit user/owner = %q/%q, want hanzo/hanzo-pkg and hanzo", rows[0].User, rows[0].Owner)
}
}
func TestWorkload_mintsForTheResourceItNames(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
app, db := newServer(t)
serviceApp(t, db, "hanzo-pkg")
status, tok := present(t, app,
c.assertion(t, "cluster-key-1", "system:serviceaccount:hanzo:pkg", audience(), time.Hour),
url.Values{"resource": {"hanzo-cloud"}, "scope": {"read"}})
if status != 200 {
t.Fatalf("status = %d; body=%v", status, tok)
}
claims, err := verifyToken(context.Background(), db, tok["access_token"].(string))
if err != nil {
t.Fatalf("minted token does not verify: %v", err)
}
if len(claims.Audience) != 1 || claims.Audience[0] != "hanzo-cloud" {
t.Errorf("aud = %v, want [hanzo-cloud]", claims.Audience)
}
if claims.Scope != "read" {
t.Errorf("scope = %q, want read", claims.Scope)
}
}
func TestWorkload_refusesWrongAudience(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
app, db := newServer(t)
serviceApp(t, db, "hanzo-pkg")
// A token the kubelet projected for some other service. It is correctly signed
// by the cluster and names a real service account; only `aud` is not ours.
status, tok := present(t, app,
c.assertion(t, "cluster-key-1", "system:serviceaccount:hanzo:pkg", "https://vault.internal", time.Hour), nil)
if status != 400 || tok["error"] != "invalid_grant" {
t.Fatalf("status/error = %d/%v, want 400 invalid_grant", status, tok["error"])
}
}
func TestWorkload_refusesUnknownIssuer(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
app, db := newServer(t)
serviceApp(t, db, "hanzo-pkg")
// Signed with the same key, naming a cluster nobody configured. The issuer is
// what selects the key source, so there is no verification path at all.
status, tok := present(t, app,
signAs(t, "https://oidc.some-other-cluster", "cluster-key-1",
"system:serviceaccount:hanzo:pkg", audience(), time.Hour), nil)
if status != 400 || tok["error"] != "invalid_grant" {
t.Fatalf("status/error = %d/%v, want 400 invalid_grant", status, tok["error"])
}
}
func TestWorkload_rereadsOnUnknownKid(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
app, db := newServer(t)
serviceApp(t, db, "hanzo-pkg")
sub := "system:serviceaccount:hanzo:pkg"
if status, tok := present(t, app, c.assertion(t, "cluster-key-1", sub, audience(), time.Hour), nil); status != 200 {
t.Fatalf("first mint: status = %d; body=%v", status, tok)
}
if got := c.fetches(); got != 1 {
t.Fatalf("JWKS reads = %d after the first mint, want 1", got)
}
// The cluster rotates. Nothing tells IAM; the first token under the new kid is
// what asks, and one re-read resolves it.
c.publish(t, "cluster-key-2")
if status, tok := present(t, app, c.assertion(t, "cluster-key-2", sub, audience(), time.Hour), nil); status != 200 {
t.Fatalf("after rotation: status = %d; body=%v", status, tok)
}
if got := c.fetches(); got != 2 {
t.Fatalf("JWKS reads = %d after the rotation, want 2", got)
}
// An invented kid costs ONE read per held set, not one per request — otherwise
// anyone who can reach the token endpoint has an amplifier pointed at the
// apiserver.
for i := 0; i < 3; i++ {
if status, _ := present(t, app, c.assertion(t, "no-such-kid", sub, audience(), time.Hour), nil); status != 400 {
t.Fatalf("invented kid: status = %d, want 400", status)
}
}
if got := c.fetches(); got != 3 {
t.Fatalf("JWKS reads = %d after three invented kids, want 3", got)
}
}
func TestWorkload_refusesAnUndeclaredServiceAccount(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
app, db := newServer(t)
serviceApp(t, db, "hanzo-pkg")
// kube-system is a real namespace with real service accounts and no org.
status, tok := present(t, app,
c.assertion(t, "cluster-key-1", "system:serviceaccount:kube-system:pkg", audience(), time.Hour), nil)
if status != 403 || tok["error"] != "unauthorized_client" {
t.Fatalf("status/error = %d/%v, want 403 unauthorized_client", status, tok["error"])
}
}
// A ServiceAccount speaks for an app only where the configuration names it. The
// same name in another namespace of the same org is a different account, created
// by whoever may write that namespace, and it mints nothing — not the declared
// workload app its name spells, and not a client_credentials app it spells either.
func TestWorkload_mintsOnlyForTheServiceAccountNamed(t *testing.T) {
c := newCluster(t, "cluster-key-1")
// collab runs one of hanzo's own workloads, so it is a namespace the org
// writes; what it may not do is speak for an app declared elsewhere.
c.trust(t, map[string]string{"hanzo:pkg": "hanzo", "collab:board": "hanzo"})
app, db := newServer(t)
serviceApp(t, db, "hanzo-pkg")
serviceApp(t, db, "hanzo-board")
seedApp(t, db, appOpts{clientID: "hanzo-admin-agent", secret: "s", grants: []string{"client_credentials"}})
for _, sub := range []string{
"system:serviceaccount:collab:pkg", // a declared workload app, from the wrong namespace
"system:serviceaccount:collab:admin-agent", // a secret-holding client, from a namespace that runs workloads
"system:serviceaccount:hanzo:admin-agent", // the same, from the namespace the declared workload runs in
"system:serviceaccount:hanzo:board", // a declared workload app, from the wrong namespace
} {
status, tok := present(t, app, c.assertion(t, "cluster-key-1", sub, audience(), time.Hour), nil)
if status != 403 || tok["error"] != "unauthorized_client" {
t.Errorf("%s: status/error = %d/%v, want 403 unauthorized_client", sub, status, tok["error"])
}
}
if rows := auditRows(t, db, schema.ActionWorkloadToken); len(rows) != 0 {
t.Fatalf("audit rows = %d for refused mints, want 0", len(rows))
}
// The accounts the configuration names still speak, each for its own app.
for sub, azp := range map[string]string{
"system:serviceaccount:hanzo:pkg": "hanzo-pkg",
"system:serviceaccount:collab:board": "hanzo-board",
} {
status, tok := present(t, app, c.assertion(t, "cluster-key-1", sub, audience(), time.Hour), nil)
if status != 200 {
t.Fatalf("%s: status = %d; body=%v", sub, status, tok)
}
claims, err := verifyToken(context.Background(), db, tok["access_token"].(string))
if err != nil {
t.Fatalf("%s: minted token does not verify: %v", sub, err)
}
if claims.Azp != azp {
t.Errorf("%s: azp = %q, want %q", sub, claims.Azp, azp)
}
}
}
// The configuration names ServiceAccounts, never namespaces. A key that is not
// "<namespace>:<name>" — a bare namespace above all, which reads as "every
// account in it" — or an entry with no org is refused as a whole, loudly, rather
// than matching nothing and looking like a deployment where no workload speaks.
func TestWorkload_refusesAConfigThatNamesNoServiceAccount(t *testing.T) {
c := newCluster(t, "cluster-key-1")
app, db := newServer(t)
serviceApp(t, db, "hanzo-pkg")
assertion := c.assertion(t, "cluster-key-1", "system:serviceaccount:hanzo:pkg", audience(), time.Hour)
for _, cfg := range []map[string]string{
{"hanzo": "hanzo"},
{"hanzo:pkg": "hanzo", "operator-system": "hanzo"},
{"hanzo:": "hanzo"},
{":pkg": "hanzo"},
{"hanzo:pkg:extra": "hanzo"},
{"hanzo:pkg": ""},
} {
c.trust(t, cfg)
status, tok := present(t, app, assertion, nil)
if status != 500 || tok["error"] != "server_error" {
t.Errorf("%v: status/error = %d/%v, want 500 server_error", cfg, status, tok["error"])
}
}
if rows := auditRows(t, db, schema.ActionWorkloadToken); len(rows) != 0 {
t.Fatalf("audit rows = %d for refused mints, want 0", len(rows))
}
}
func TestWorkload_refusesUnprovisionedApplication(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
app, _ := newServer(t) // nothing seeded: hanzo-pkg was never declared
status, tok := present(t, app,
c.assertion(t, "cluster-key-1", "system:serviceaccount:hanzo:pkg", audience(), time.Hour), nil)
if status != 403 || tok["error"] != "unauthorized_client" {
t.Fatalf("status/error = %d/%v, want 403 unauthorized_client", status, tok["error"])
}
}
func TestWorkload_refusesApplicationThatDeclaresNoMachineGrant(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
app, db := newServer(t)
// A browser client that happens to be named <org>-<account>. It never declared
// client_credentials, so it may not be handed that grant's token by another door.
seedApp(t, db, appOpts{clientID: "hanzo-pkg", secret: "s", grants: []string{"authorization_code"}})
status, tok := present(t, app,
c.assertion(t, "cluster-key-1", "system:serviceaccount:hanzo:pkg", audience(), time.Hour), nil)
if status != 403 || tok["error"] != "unauthorized_client" {
t.Fatalf("status/error = %d/%v, want 403 unauthorized_client", status, tok["error"])
}
}
// A machine client that never declared the workload grant — client_credentials
// only, `type: service` with nothing appended — is refused even though a
// ServiceAccount in its own org's namespace spells its clientId exactly. Naming
// an account is all it takes to spell one; the declaration is what the
// registration says, and only a reviewed document writes it.
func TestWorkload_refusesAClientCredentialsOnlyApplication(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"hanzo:cloud": "hanzo"})
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-cloud", secret: "s", grants: []string{"client_credentials"}})
status, tok := present(t, app,
c.assertion(t, "cluster-key-1", "system:serviceaccount:hanzo:cloud", audience(), time.Hour), nil)
if status != 403 || tok["error"] != "unauthorized_client" {
t.Fatalf("status/error = %d/%v, want 403 unauthorized_client", status, tok["error"])
}
if rows := auditRows(t, db, schema.ActionWorkloadToken); len(rows) != 0 {
t.Fatalf("audit rows = %d for a refused mint, want 0", len(rows))
}
}
// The workload grant alone is not enough either: the token handed back is the
// one client_credentials mints, so the registration must also declare that.
func TestWorkload_refusesAWorkloadGrantWithoutClientCredentials(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-pkg", secret: "s", grants: []string{grantTypeAssertion}})
status, tok := present(t, app,
c.assertion(t, "cluster-key-1", "system:serviceaccount:hanzo:pkg", audience(), time.Hour), nil)
if status != 403 || tok["error"] != "unauthorized_client" {
t.Fatalf("status/error = %d/%v, want 403 unauthorized_client", status, tok["error"])
}
}
// A browser client that declares the workload grant is still refused: the
// declaration opens the door only for an app that is a machine as well.
func TestWorkload_refusesAWorkloadGrantOnABrowserClient(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-pkg", grants: []string{"authorization_code", grantTypeAssertion}})
status, tok := present(t, app,
c.assertion(t, "cluster-key-1", "system:serviceaccount:hanzo:pkg", audience(), time.Hour), nil)
if status != 403 || tok["error"] != "unauthorized_client" {
t.Fatalf("status/error = %d/%v, want 403 unauthorized_client", status, tok["error"])
}
}
func TestWorkload_refusesApplicationOfAnotherOrg(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"zoo-ns:pkg": "zoo"})
app, db := newServer(t)
// The clientId a zoo namespace derives, registered under the hanzo org. A
// clientId is a globally unique STRING; without the org check the zoo namespace
// would mint hanzo's token.
serviceApp(t, db, "zoo-pkg")
status, tok := present(t, app,
c.assertion(t, "cluster-key-1", "system:serviceaccount:zoo-ns:pkg", audience(), time.Hour), nil)
if status != 403 || tok["error"] != "unauthorized_client" {
t.Fatalf("status/error = %d/%v, want 403 unauthorized_client", status, tok["error"])
}
}
func TestWorkload_refusesExpiredAssertion(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
app, db := newServer(t)
serviceApp(t, db, "hanzo-pkg")
status, tok := present(t, app,
c.assertion(t, "cluster-key-1", "system:serviceaccount:hanzo:pkg", audience(), -time.Hour), nil)
if status != 400 || tok["error"] != "invalid_grant" {
t.Fatalf("status/error = %d/%v, want 400 invalid_grant", status, tok["error"])
}
}
func TestWorkload_refusesAHumanSubject(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
app, db := newServer(t)
serviceApp(t, db, "hanzo-pkg")
status, tok := present(t, app,
c.assertion(t, "cluster-key-1", "[email protected]", audience(), time.Hour), nil)
if status != 400 || tok["error"] != "invalid_grant" {
t.Fatalf("status/error = %d/%v, want 400 invalid_grant", status, tok["error"])
}
}
func TestWorkload_unconfiguredIsNotAGrant(t *testing.T) {
t.Setenv(envClusters, "")
t.Setenv(envWorkloads, "")
app, _ := newServer(t)
status, tok := present(t, app, "irrelevant", nil)
if status != 400 || tok["error"] != "unsupported_grant_type" {
t.Fatalf("status/error = %d/%v, want 400 unsupported_grant_type", status, tok["error"])
}
// And discovery does not advertise a grant this deployment will refuse.
_, body := do(t, app, formReqNoBody("GET", PathDiscovery))
if containsStr(decode(t, body)["grant_types_supported"], grantTypeAssertion) {
t.Error("discovery advertises the workload grant with no cluster configured")
}
}
func TestWorkload_configuredIsAdvertised(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
app, _ := newServer(t)
_, body := do(t, app, formReqNoBody("GET", PathDiscovery))
if !containsStr(decode(t, body)["grant_types_supported"], grantTypeAssertion) {
t.Error("discovery omits the workload grant with a cluster configured")
}
}
// An apiserver that refuses an anonymous read of its key set — k3s answers 401 —
// is read with IAM's own ServiceAccount token, and keeps being read after the
// kubelet rotates it.
func TestWorkload_readsAKeySetThatDemandsABearer(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.demand(t, "discovery-token-1")
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
app, db := newServer(t)
serviceApp(t, db, "hanzo-pkg")
sub := "system:serviceaccount:hanzo:pkg"
status, tok := present(t, app, c.assertion(t, "cluster-key-1", sub, audience(), time.Hour), nil)
if status != 200 {
t.Fatalf("status = %d; body=%v", status, tok)
}
// The kubelet rewrites the projected token. A copy taken at the first fetch
// would be refused from here on; the file is what IAM reads.
c.rotate(t, "discovery-token-2")
c.publish(t, "cluster-key-2") // forces a fresh read rather than a cache hit
status, tok = present(t, app, c.assertion(t, "cluster-key-2", sub, audience(), time.Hour), nil)
if status != 200 {
t.Fatalf("after the token rotated: status = %d; body=%v", status, tok)
}
if got := c.fetches(); got != 2 {
t.Fatalf("JWKS reads = %d, want 2", got)
}
}
// Without the bearer the apiserver answers 401, there is no key, and the grant
// fails closed rather than minting on an unverified assertion.
func TestWorkload_refusesWhenTheKeySetIsUnreadable(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.demand(t, "discovery-token-1")
c.bearerFile = "" // configured without the token the apiserver requires
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
app, db := newServer(t)
serviceApp(t, db, "hanzo-pkg")
status, tok := present(t, app,
c.assertion(t, "cluster-key-1", "system:serviceaccount:hanzo:pkg", audience(), time.Hour), nil)
if status != 400 || tok["error"] != "invalid_grant" {
t.Fatalf("status/error = %d/%v, want 400 invalid_grant", status, tok["error"])
}
}
func TestWorkload_refusesMalformedConfig(t *testing.T) {
t.Setenv(envClusters, "{not json")
t.Setenv(envWorkloads, `{"hanzo:pkg":"hanzo"}`)
app, _ := newServer(t)
// Unreadable is not the same answer as unconfigured: one bad character must not
// look like a deployment that never offered the grant.
status, tok := present(t, app, "irrelevant", nil)
if status != 500 || tok["error"] != "server_error" {
t.Fatalf("status/error = %d/%v, want 500 server_error", status, tok["error"])
}
}
// serviceAccount reads the two names a Kubernetes subject carries, and refuses
// every shape that is not one.
func TestServiceAccount(t *testing.T) {
for _, tc := range []struct{ sub, namespace, name string }{
{"system:serviceaccount:hanzo:pkg", "hanzo", "pkg"},
{"system:serviceaccount:operator-system:operator", "operator-system", "operator"},
{"system:serviceaccount:hanzo:", "", ""},
{"system:serviceaccount::pkg", "", ""},
{"system:serviceaccount:hanzo:pkg:extra", "", ""},
{"system:serviceaccount:hanzo", "", ""},
{"system:node:worker-1", "", ""},
{"hanzo/z", "", ""},
{"", "", ""},
} {
ns, name := serviceAccount(tc.sub)
if ns != tc.namespace || name != tc.name {
t.Errorf("serviceAccount(%q) = %q, %q; want %q, %q", tc.sub, ns, name, tc.namespace, tc.name)
}
}
}
// lifeOf honours the lifetime an issuer asks for and falls back when it asks for
// nothing usable — never to zero, which would be a read per request.
func TestLifeOf(t *testing.T) {
for _, tc := range []struct {
header string
want time.Duration
}{
{"public, max-age=300", 5 * time.Minute},
{"max-age=60", time.Minute},
{"no-cache, private", keyLife},
{"max-age=0", keyLife},
{"max-age=nonsense", keyLife},
{"", keyLife},
} {
if got := lifeOf(tc.header); got != tc.want {
t.Errorf("lifeOf(%q) = %v, want %v", tc.header, got, tc.want)
}
}
}
// GitHub Actions ---------------------------------------------------------------
// newGitHub is a GitHub Actions issuer: the cluster fixture, publishing under
// GitHub's own issuer name.
func newGitHub(t *testing.T, kid string) *fakeCluster {
t.Helper()
c := newCluster(t, kid)
c.iss = githubIssuer
return c
}
// trustRepos states which repositories speak, beside the issuer set trust wrote.
func trustRepos(t *testing.T, repos map[string]repository) {
t.Helper()
raw, err := json.Marshal(repos)
if err != nil {
t.Fatalf("marshal repositories: %v", err)
}
t.Setenv(envRepositories, string(raw))
}
// run signs an Actions ID token for repo at ref, owned by the account ownerID.
func (c *fakeCluster) run(t *testing.T, kid, repo, ref, ownerID, audience string) string {
t.Helper()
now := nowFunc()
owner, _, _ := strings.Cut(repo, "/")
tok := jwt.NewWithClaims(jwt.SigningMethodRS256, assertionClaims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: c.iss,
Subject: "repo:" + repo + ":ref:" + ref,
Audience: jwt.ClaimStrings{audience},
IssuedAt: jwt.NewNumericDate(now.Add(-time.Minute)),
NotBefore: jwt.NewNumericDate(now.Add(-time.Minute)),
ExpiresAt: jwt.NewNumericDate(now.Add(5 * time.Minute)),
},
RepositoryOwner: owner,
RepositoryOwnerID: ownerID,
Repository: repo,
Ref: ref,
})
tok.Header["kid"] = kid
s, err := tok.SignedString(sharedKey(t))
if err != nil {
t.Fatalf("sign run token: %v", err)
}
return s
}
func TestWorkload_mintsForAGitHubRun(t *testing.T) {
gh := newGitHub(t, "gh-key-1")
gh.trust(t, nil)
trustRepos(t, map[string]repository{"hanzoai": {Org: "hanzo", App: "ci", OwnerID: "100"}})
app, db := newServer(t)
serviceApp(t, db, "hanzo-ci")
status, tok := present(t, app, gh.run(t, "gh-key-1", "hanzoai/iam", "refs/heads/main", "100", audience()), nil)
if status != 200 {
t.Fatalf("status = %d; body=%v", status, tok)
}
claims, err := verifyToken(context.Background(), db, tok["access_token"].(string))
if err != nil {
t.Fatalf("minted token does not verify: %v", err)
}
if claims.Owner != "hanzo" || claims.Azp != "hanzo-ci" || claims.Type != schema.Program {
t.Errorf("owner/azp/type = %q/%q/%q, want hanzo/hanzo-ci/%q", claims.Owner, claims.Azp, claims.Type, schema.Program)
}
rows := auditRows(t, db, schema.ActionWorkloadToken)
if len(rows) != 1 || rows[0].Object != "repo:hanzoai/iam:ref:refs/heads/main" {
t.Fatalf("audit = %+v, want one row naming the run's subject", rows)
}
}
// The most specific entry wins: a ref over its repository, a repository over its
// account. Each still reaches only the application its entry names.
func TestWorkload_aGitHubRunSpeaksForItsMostSpecificEntry(t *testing.T) {
gh := newGitHub(t, "gh-key-1")
gh.trust(t, nil)
trustRepos(t, map[string]repository{
"hanzo-apps": {Org: "hanzo", App: "ci", OwnerID: "200"},
"hanzo-apps/network": {Org: "hanzo", App: "sites-ci", OwnerID: "200"},
"hanzo-apps/network@refs/tags/v1.0.0": {Org: "hanzo", App: "release-ci", OwnerID: "200"},
})
app, db := newServer(t)
for _, id := range []string{"hanzo-ci", "hanzo-sites-ci", "hanzo-release-ci"} {
serviceApp(t, db, id)
}
for _, tc := range []struct{ repo, ref, azp string }{
{"hanzo-apps/ai", "refs/heads/main", "hanzo-ci"},
{"hanzo-apps/network", "refs/heads/main", "hanzo-sites-ci"},
{"hanzo-apps/network", "refs/tags/v1.0.0", "hanzo-release-ci"},
} {
status, tok := present(t, app, gh.run(t, "gh-key-1", tc.repo, tc.ref, "200", audience()), nil)
if status != 200 {
t.Fatalf("%s@%s: status = %d; body=%v", tc.repo, tc.ref, status, tok)
}
claims, err := verifyToken(context.Background(), db, tok["access_token"].(string))
if err != nil {
t.Fatalf("%s@%s: minted token does not verify: %v", tc.repo, tc.ref, err)
}
if claims.Azp != tc.azp {
t.Errorf("%s@%s: azp = %q, want %q", tc.repo, tc.ref, claims.Azp, tc.azp)
}
}
}
// An account name can be given up and registered again by someone else; the
// account id cannot. A run from an account carrying a configured name but
// another id mints nothing, and neither does an account nobody configured.
func TestWorkload_refusesAGitHubRunFromAnotherAccount(t *testing.T) {
gh := newGitHub(t, "gh-key-1")
gh.trust(t, nil)
trustRepos(t, map[string]repository{"hanzoai": {Org: "hanzo", App: "ci", OwnerID: "100"}})
app, db := newServer(t)
serviceApp(t, db, "hanzo-ci")
for _, run := range []string{
gh.run(t, "gh-key-1", "hanzoai/iam", "refs/heads/main", "999", audience()), // the name, another account
gh.run(t, "gh-key-1", "someone/iam", "refs/heads/main", "100", audience()), // an account nobody configured
} {
status, tok := present(t, app, run, nil)
if status != 403 || tok["error"] != "unauthorized_client" {
t.Errorf("status/error = %d/%v, want 403 unauthorized_client", status, tok["error"])
}
}
if rows := auditRows(t, db, schema.ActionWorkloadToken); len(rows) != 0 {
t.Fatalf("audit rows = %d for refused mints, want 0", len(rows))
}
}
// immutableRun signs a run token whose subject is GitHub's immutable spelling,
// `repo:<owner>@<owner id>/<name>@<repository id>:ref:<ref>`, with sub naming
// subOwnerID and subRepoID while the claims carry ownerID and repoID.
func (c *fakeCluster) immutableRun(t *testing.T, kid, repo, ref, ownerID, repoID, subOwnerID, subRepoID string) string {
t.Helper()
now := nowFunc()
owner, name, _ := strings.Cut(repo, "/")
tok := jwt.NewWithClaims(jwt.SigningMethodRS256, assertionClaims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: c.iss,
Subject: "repo:" + owner + "@" + subOwnerID + "/" + name + "@" + subRepoID + ":ref:" + ref,
Audience: jwt.ClaimStrings{audience()},
IssuedAt: jwt.NewNumericDate(now.Add(-time.Minute)),
NotBefore: jwt.NewNumericDate(now.Add(-time.Minute)),
ExpiresAt: jwt.NewNumericDate(now.Add(5 * time.Minute)),
},
RepositoryOwner: owner,
RepositoryOwnerID: ownerID,
Repository: repo,
RepositoryID: repoID,
Ref: ref,
})
tok.Header["kid"] = kid
s, err := tok.SignedString(sharedKey(t))
if err != nil {
t.Fatalf("sign run token: %v", err)
}
return s
}
// GitHub signs a run's subject with the account's and the repository's ids
// beside their names — a hanzo-inc/app run carries
// `repo:hanzo-inc@308927965/app@1345862086:ref:refs/heads/main` — and a run so
// spelled speaks for its repository like any other. Ids in the subject that are
// not the ones the token states name no repository.
func TestWorkload_aGitHubRunSpelledWithIdsSpeaks(t *testing.T) {
gh := newGitHub(t, "gh-key-1")
gh.trust(t, nil)
trustRepos(t, map[string]repository{"hanzo-inc": {Org: "hanzo", App: "ci", OwnerID: "308927965"}})
app, db := newServer(t)
serviceApp(t, db, "hanzo-ci")
const owner, repo = "308927965", "1345862086"
status, tok := present(t, app, gh.immutableRun(t, "gh-key-1", "hanzo-inc/app", "refs/heads/main", owner, repo, owner, repo), nil)
if status != 200 {
t.Fatalf("status = %d; body=%v", status, tok)
}
claims, err := verifyToken(context.Background(), db, tok["access_token"].(string))
if err != nil {
t.Fatalf("minted token does not verify: %v", err)
}
if claims.Owner != "hanzo" || claims.Azp != "hanzo-ci" {
t.Errorf("owner/azp = %q/%q, want hanzo/hanzo-ci", claims.Owner, claims.Azp)
}
for _, run := range []string{
gh.immutableRun(t, "gh-key-1", "hanzo-inc/app", "refs/heads/main", owner, repo, owner, "7"), // another repository's id
gh.immutableRun(t, "gh-key-1", "hanzo-inc/app", "refs/heads/main", owner, repo, "999", repo), // another account's id
gh.immutableRun(t, "gh-key-1", "hanzo-inc/app", "refs/heads/main", owner, "", owner, ""), // no repository id stated
} {
status, tok := present(t, app, run, nil)
if status != 403 || tok["error"] != "unauthorized_client" {
t.Errorf("status/error = %d/%v, want 403 unauthorized_client", status, tok["error"])
}
}
}
// A run's token names the audience it was asked for. One asked for another
// relying party cannot be spent here.
func TestWorkload_refusesAGitHubRunForAnotherAudience(t *testing.T) {
gh := newGitHub(t, "gh-key-1")
gh.trust(t, nil)
trustRepos(t, map[string]repository{"hanzoai": {Org: "hanzo", App: "ci", OwnerID: "100"}})
app, db := newServer(t)
serviceApp(t, db, "hanzo-ci")
status, tok := present(t, app, gh.run(t, "gh-key-1", "hanzoai/iam", "refs/heads/main", "100", "https://github.com/hanzoai"), nil)
if status != 400 || tok["error"] != "invalid_grant" {
t.Fatalf("status/error = %d/%v, want 400 invalid_grant", status, tok["error"])
}
}
// A repository subject from a cluster issuer is read as what a cluster issues —
// a service account — and names none.
func TestWorkload_readsARepositoryOnlyFromGitHub(t *testing.T) {
c := newCluster(t, "cluster-key-1")
c.trust(t, map[string]string{"hanzo:pkg": "hanzo"})
trustRepos(t, map[string]repository{"hanzoai": {Org: "hanzo", App: "ci", OwnerID: "100"}})
app, db := newServer(t)
serviceApp(t, db, "hanzo-ci")
status, tok := present(t, app, c.run(t, "cluster-key-1", "hanzoai/iam", "refs/heads/main", "100", audience()), nil)
if status != 400 || tok["error"] != "invalid_grant" {
t.Fatalf("status/error = %d/%v, want 400 invalid_grant", status, tok["error"])
}
}
func TestWorkload_refusesARepositoryConfigItCannotRead(t *testing.T) {
gh := newGitHub(t, "gh-key-1")
gh.trust(t, nil)
app, db := newServer(t)
serviceApp(t, db, "hanzo-ci")
run := gh.run(t, "gh-key-1", "hanzoai/iam", "refs/heads/main", "100", audience())
for _, cfg := range []map[string]repository{
{"hanzoai": {Org: "hanzo", App: "ci"}}, // no account id
{"hanzoai": {Org: "hanzo", App: "ci", OwnerID: "hanzoai"}}, // an id that is a name
{"hanzoai": {App: "ci", OwnerID: "100"}}, // no org
{"hanzoai/*": {Org: "hanzo", App: "ci", OwnerID: "100"}}, // a glob
{"hanzoai@refs/heads/main": {Org: "hanzo", App: "ci", OwnerID: "100"}}, // a ref with no repository
{"hanzoai/iam@main": {Org: "hanzo", App: "ci", OwnerID: "100"}}, // a ref that is not one
} {
trustRepos(t, cfg)
status, tok := present(t, app, run, nil)
if status != 500 || tok["error"] != "server_error" {
t.Errorf("%v: status/error = %d/%v, want 500 server_error", cfg, status, tok["error"])
}
}
}