console: mint platform credentials on granted accounts only
The services door's platform role now needs an active grant, read in the mint transaction; the row records the platform account and the grant generation it was minted in. A platform credential is never bound to an app (ErrAppScopedPlatform). CLIPrincipal resolves a platform-role row only with no app and a live generation (PlatformGenerationLive), fills Principal's PlatformAccountID, Platform and PlatformGeneration, and refuses any other row that carries platform columns. The browser form no longer offers the role. Copy rows svc.platform_app and svc.not_platform. The role matrices and the link-door walk mint their platform credential on a granted account, app-less.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9 files changed,
+422
−10
internal/console/apibackups_test.go+6 −1internal/console/apiruns_key_test.go+7 −1internal/console/apiruns_service_test.go+7 −1internal/console/apiservices.go+8 −0internal/console/links_auth_test.go+10 −1internal/console/platform_copy.go+3 −0internal/console/platform_credentials_test.go+308 −0internal/console/svcpage.go+5 −0internal/console/svcprincipal.go+68 −6
diff --git a/internal/console/apibackups_test.go b/internal/console/apibackups_test.go| index a7af1e5d7..4374307de 100644 |
| --- a/internal/console/apibackups_test.go |
| +++ b/internal/console/apibackups_test.go |
| @@ -202,7 +202,12 @@ func TestAPIInstanceBackupsRoles(t *testing.T) { |
| if role == svcrole.Deploy || role == svcrole.Operate || role == svcrole.Admin { |
| continue |
| } |
| - tok := backupsToken(t, fx.st, OpsAccountID, "no-"+string(role), "titogo", role) |
| + var tok string |
| + if role == svcrole.Platform { |
| + tok = platformRoleToken(t, fx.st, "no-platform") // never app-bound, never ops's |
| + } else { |
| + tok = backupsToken(t, fx.st, OpsAccountID, "no-"+string(role), "titogo", role) |
| + } |
| status, h, body := rawReq(t, http.MethodGet, u, tok, "") |
| want := `{"error":"this credential holds role \"` + string(role) + `\"; this door needs one of [admin deploy operate]"}` + "\n" |
| if status != http.StatusForbidden || h.Get("Content-Type") != "application/json" || body != want { |
diff --git a/internal/console/apiruns_key_test.go b/internal/console/apiruns_key_test.go| index 7fc52243b..c5c666309 100644 |
| --- a/internal/console/apiruns_key_test.go |
| +++ b/internal/console/apiruns_key_test.go |
| @@ -511,7 +511,13 @@ func TestRunKeyLookupRoleMatrix(t *testing.T) { |
| if r == svcrole.Team { |
| scope = "alta" |
| } |
| - tok, err := fx.st.NewServicePrincipalWithScopeContext(context.Background(), OpsAccountID, "role-"+string(r), app, r, rungs, nil, scope, "paul@keymail.dev", time.Now()) |
| + var tok string |
| + var err error |
| + if r == svcrole.Platform { |
| + tok = platformRoleToken(t, fx.st, "role-platform") // never app-bound, never ops's |
| + } else { |
| + tok, err = fx.st.NewServicePrincipalWithScopeContext(context.Background(), OpsAccountID, "role-"+string(r), app, r, rungs, nil, scope, "paul@keymail.dev", time.Now()) |
| + } |
| if err != nil { |
| t.Fatalf("mint %s: %v", r, err) |
| } |
diff --git a/internal/console/apiruns_service_test.go b/internal/console/apiruns_service_test.go| index e6c128a23..8bd5073e1 100644 |
| --- a/internal/console/apiruns_service_test.go |
| +++ b/internal/console/apiruns_service_test.go |
| @@ -144,7 +144,13 @@ func TestRunRoleMatrix(t *testing.T) { |
| if r == svcrole.Team { |
| scope = "alta" |
| } |
| - tok, err := fx.st.NewServicePrincipalWithScopeContext(context.Background(), OpsAccountID, "role-"+string(r), app, r, rungs, nil, scope, "paul@keymail.dev", time.Now()) |
| + var tok string |
| + var err error |
| + if r == svcrole.Platform { |
| + tok = platformRoleToken(t, fx.st, "role-platform") // never app-bound, never ops's |
| + } else { |
| + tok, err = fx.st.NewServicePrincipalWithScopeContext(context.Background(), OpsAccountID, "role-"+string(r), app, r, rungs, nil, scope, "paul@keymail.dev", time.Now()) |
| + } |
| if err != nil { |
| t.Fatalf("mint %s: %v", r, err) |
| } |
diff --git a/internal/console/apiservices.go b/internal/console/apiservices.go| index 5500eddfb..e0c017830 100644 |
| --- a/internal/console/apiservices.go |
| +++ b/internal/console/apiservices.go |
| @@ -269,6 +269,14 @@ func (s *Server) handleAPIServiceCreate(w http.ResponseWriter, r *http.Request) |
| } |
| var ue *UnknownRungError |
| switch { |
| + case errors.Is(err, ErrAppScopedPlatform): |
| + jsonError(w, http.StatusBadRequest, platformCopy["svc.platform_app"]) |
| + return |
| + case errors.Is(err, ErrNotPlatform): |
| + // The grant is the operator's, so the account's state, not the |
| + // request, is what stands in the way. |
| + jsonError(w, http.StatusConflict, platformCopy["svc.not_platform"]) |
| + return |
| case errors.Is(err, ErrAccountScopedDeploy): |
| jsonError(w, http.StatusBadRequest, "a deploy credential must name one app (--app)") |
| return |
diff --git a/internal/console/links_auth_test.go b/internal/console/links_auth_test.go| index 9220aa441..a0cbaf0f8 100644 |
| --- a/internal/console/links_auth_test.go |
| +++ b/internal/console/links_auth_test.go |
| @@ -779,7 +779,12 @@ func TestLinkDoorsRefuseEveryOtherBearer(t *testing.T) { |
| } |
| // A service credential of every role, app-bound and (where the role |
| // may be) account-wide; a role whose credential needs more than a role |
| - // (a deploy's rungs, a team's scope) is minted with it. |
| + // (a deploy's rungs, a team's scope) is minted with it. A platform |
| + // credential needs a grant and is never app-bound. |
| + if err := fx.st.PutPlatformGrant(context.Background(), PlatformGrant{AccountID: acct.ID, MaxCustomers: 1, |
| + Reason: "walk", GrantedBy: "ops@keymail.dev"}, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| for _, role := range svcrole.All() { |
| for _, app := range []string{"kass", ""} { |
| var rungs []string |
| @@ -795,6 +800,10 @@ func TestLinkDoorsRefuseEveryOtherBearer(t *testing.T) { |
| continue |
| } |
| scope = "alta" |
| + case svcrole.Platform: |
| + if app != "" { |
| + continue |
| + } |
| } |
| raw, err := fx.st.NewServicePrincipalWithScopeContext(context.Background(), acct.ID, "walk-"+string(role)+"-"+app, app, role, rungs, commands, scope, "paul@keymail.dev", time.Now()) |
| if err != nil { |
diff --git a/internal/console/platform_copy.go b/internal/console/platform_copy.go| index 5948111a9..d7de0c603 100644 |
| --- a/internal/console/platform_copy.go |
| +++ b/internal/console/platform_copy.go |
| @@ -21,6 +21,9 @@ var platformCopy = map[string]string{ |
| "door.rate": "too many requests. wait a minute and try again", |
| + "svc.platform_app": "a platform credential works for the whole platform account, so it takes no --app", |
| + "svc.not_platform": "only a platform account can hold a platform credential. an operator grants that with carlos accounts platform", |
| + |
| "audit.grant": "made this a platform account: up to %d customer accounts, plans %s, new ones on %s. reason: %s", |
| "audit.grant_no_plans": "made this a platform account: up to %d customer accounts. reason: %s", |
| "audit.revoke": "turned off platform access, %d credentials stopped working. reason: %s", |
diff --git a/internal/console/platform_credentials_test.go b/internal/console/platform_credentials_test.go| new file mode 100644 |
| index 000000000..6e26eaa9f |
| --- /dev/null |
| +++ b/internal/console/platform_credentials_test.go |
| @@ -0,0 +1,308 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "net/http" |
| + "slices" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/svcrole" |
| +) |
| + |
| +func servicesURL(fx *consoleFixture, sqid string) string { |
| + return fx.ts.URL + "/api/cli/services/" + sqid |
| +} |
| + |
| +// grantPlatformGen makes the account a platform account and answers its |
| +// generation. |
| +func grantPlatformGen(t *testing.T, st *Store, accountID int64) int64 { |
| + t.Helper() |
| + ctx := context.Background() |
| + if err := st.PutPlatformGrant(ctx, PlatformGrant{AccountID: accountID, MaxCustomers: 5, Reason: "r", |
| + GrantedBy: "ops@keymail.dev"}, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + g, err := st.PlatformGrant(ctx, accountID) |
| + if err != nil || g == nil { |
| + t.Fatalf("grant %+v %v", g, err) |
| + } |
| + return g.Generation |
| +} |
| + |
| +// regrantPlatform revokes the account's grant and grants it again, so its |
| +// credentials are swept and the grant is active under the next generation. |
| +func regrantPlatform(t *testing.T, st *Store, accountID int64) int64 { |
| + t.Helper() |
| + if _, err := st.RevokePlatformGrant(context.Background(), accountID, "ops@keymail.dev", "r", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + return grantPlatformGen(t, st, accountID) |
| +} |
| + |
| +// platformRoleToken mints a platform credential where one can exist: on |
| +// a granted account of its own (never the ops account), bound to no app. |
| +// The role matrices use it to walk the platform role through their doors. |
| +func platformRoleToken(t *testing.T, st *Store, name string) string { |
| + t.Helper() |
| + id := testAccountID() |
| + if err := st.CreateAccount(id, domains.Sqid(id), "platform", "owner@example.com", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + grantPlatformGen(t, st, id) |
| + raw, err := st.NewServicePrincipal(id, name, "", svcrole.Platform, "owner@example.com", time.Now()) |
| + if err != nil { |
| + t.Fatalf("platform credential %q: %v", name, err) |
| + } |
| + return raw |
| +} |
| + |
| +// probeStatus is the status a bearer gets at the services list door, which |
| +// is closed to every service credential: a credential that resolves gets |
| +// the closed door's 403, one that does not gets the unknown token's 401. |
| +func probeStatus(t *testing.T, fx *consoleFixture, sqid, bearer string) int { |
| + t.Helper() |
| + status, _, _ := rawReq(t, http.MethodGet, servicesURL(fx, sqid), bearer, "") |
| + return status |
| +} |
| + |
| +// wantPlatformPrincipal fails unless raw resolves to acct's platform |
| +// credential under generation gen. |
| +func wantPlatformPrincipal(t *testing.T, st *Store, raw string, acct *Account, gen int64) Principal { |
| + t.Helper() |
| + p, ok := st.CLIPrincipal(raw, time.Now()) |
| + if !ok || p.Kind != PrincipalService || p.Role != svcrole.Platform || p.App != "" || p.AccountID != acct.ID || |
| + p.PlatformAccountID != acct.ID || p.Platform != acct.Sqid || p.PlatformGeneration != gen { |
| + t.Fatalf("CLIPrincipal = %+v %v, want the platform credential of %s at generation %d", p, ok, acct.Sqid, gen) |
| + } |
| + return p |
| +} |
| + |
| +// insertTokenRow writes a cli_tokens row as raw SQL, so a test can hold a |
| +// row no mint door would write. |
| +func insertTokenRow(t *testing.T, st *Store, raw, kind string, accountID int64, app, role string, expires, platformAccountID, platformGeneration int64) { |
| + t.Helper() |
| + now := time.Now().Unix() |
| + if _, err := st.db.ExecContext(context.Background(), `INSERT INTO cli_tokens |
| +(token_hash, address, label, remote_addr, created_at, rotated_at, expires_at, kind, account_id, app, role, name, rungs, |
| +credential_id, commands, scope, platform_account_id, platform_generation) |
| +VALUES (?, 'paul@keymail.dev', '', '', ?, ?, ?, ?, ?, ?, ?, ?, '', ?, '', '', ?, ?)`, |
| + hashToken(raw), now, now, expires, kind, accountID, app, role, "row-"+raw, "cred-"+raw, |
| + platformAccountID, platformGeneration); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +func TestServicesDoorMintsAPlatformCredentialOnlyOnAGrantedAccount(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + acct := planAccount(t, fx, 501) |
| + owner := cliToken(t, fx, "owner"+acct.Sqid+"@example.com", "code-platform-cred-owner") |
| + mint := map[string]any{"name": "cms", "role": "platform"} |
| + |
| + status, out := postJSON(t, servicesURL(fx, acct.Sqid), mint, owner) |
| + wantPlatformError(t, "mint with no grant", status, out, http.StatusConflict, "svc.not_platform") |
| + gen := grantPlatformGen(t, fx.st, acct.ID) |
| + if _, err := fx.st.RevokePlatformGrant(context.Background(), acct.ID, "ops@keymail.dev", "r", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + status, out = postJSON(t, servicesURL(fx, acct.Sqid), mint, owner) |
| + wantPlatformError(t, "mint under a revoked grant", status, out, http.StatusConflict, "svc.not_platform") |
| + if rows, err := fx.st.ServicePrincipals(acct.ID); err != nil || len(rows) != 0 { |
| + t.Fatalf("refused mints left %+v %v", rows, err) |
| + } |
| + |
| + // The re-grant runs under the next generation, and the mint takes it. |
| + if regen := grantPlatformGen(t, fx.st, acct.ID); regen != gen+1 { |
| + t.Fatalf("re-grant generation %d, want %d", regen, gen+1) |
| + } |
| + status, out = postJSON(t, servicesURL(fx, acct.Sqid), mint, owner) |
| + if status != http.StatusCreated || out["role"] != "platform" || out["app"] != "" || out["token"] == "" { |
| + t.Fatalf("mint on a granted account = %d %v, want 201 role platform, no app", status, out) |
| + } |
| + raw, _ := out["token"].(string) |
| + wantPlatformPrincipal(t, fx.st, raw, acct, gen+1) |
| + var platformAccountID, platformGeneration int64 |
| + if err := fx.st.db.QueryRowContext(context.Background(), `SELECT platform_account_id, platform_generation |
| + FROM cli_tokens WHERE token_hash = ?`, hashToken(raw)).Scan(&platformAccountID, &platformGeneration); err != nil { |
| + t.Fatal(err) |
| + } |
| + if platformAccountID != acct.ID || platformGeneration != gen+1 { |
| + t.Fatalf("row platform_account_id %d, platform_generation %d, want %d, %d", platformAccountID, platformGeneration, acct.ID, gen+1) |
| + } |
| +} |
| + |
| +func TestPlatformCredentialTakesNoApp(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + acct := planAccount(t, fx, 501) |
| + owner := "owner" + acct.Sqid + "@example.com" |
| + token := cliToken(t, fx, owner, "code-platform-cred-app") |
| + grantPlatformGen(t, fx.st, acct.ID) |
| + if _, err := fx.st.CreateAppIn(acct.ID, "site", owner, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + status, out := postJSON(t, servicesURL(fx, acct.Sqid), map[string]any{"name": "cms", "role": "platform", "app": "site"}, token) |
| + wantPlatformError(t, "mint bound to an app", status, out, http.StatusBadRequest, "svc.platform_app") |
| + // The store holds the rule, so the browser form cannot mint one either. |
| + if _, err := fx.st.NewServicePrincipal(acct.ID, "cms", "site", svcrole.Platform, owner, time.Now()); !errors.Is(err, ErrAppScopedPlatform) { |
| + t.Fatalf("store mint bound to an app = %v, want ErrAppScopedPlatform", err) |
| + } |
| + if rows, err := fx.st.ServicePrincipals(acct.ID); err != nil || len(rows) != 0 { |
| + t.Fatalf("refused mints left %+v %v", rows, err) |
| + } |
| +} |
| + |
| +func TestPlatformRoleRowWithAnAppDoesNotResolve(t *testing.T) { |
| + s := openTestStore(t) |
| + acct := mustAccount(t, s, "bcd") |
| + gen := grantPlatformGen(t, s, acct.ID) |
| + // The same row without the app resolves, so the app alone refuses it. |
| + insertTokenRow(t, s, "platform-whole", cliKindService, acct.ID, "", string(svcrole.Platform), 0, acct.ID, gen) |
| + if p, ok := s.CLIPrincipal("platform-whole", time.Now()); !ok || p.PlatformGeneration != gen { |
| + t.Fatalf("app-less platform row = %+v %v, want it to resolve", p, ok) |
| + } |
| + insertTokenRow(t, s, "platform-app", cliKindService, acct.ID, "x", string(svcrole.Platform), 0, acct.ID, gen) |
| + if p, ok := s.CLIPrincipal("platform-app", time.Now()); ok { |
| + t.Fatalf("platform row bound to app x resolved to %+v", p) |
| + } |
| +} |
| + |
| +func TestRowWithAPlatformAccountIDOnlyResolvesAsPlatformKind(t *testing.T) { |
| + s := openTestStore(t) |
| + acct := mustAccount(t, s, "bcd") |
| + later := time.Now().Add(time.Hour).Unix() |
| + for _, row := range []struct { |
| + kind, role string |
| + expires int64 |
| + }{ |
| + {cliKindService, string(svcrole.Publish), 0}, |
| + {cliKindApp, "", 0}, |
| + {cliKindUser, "", later}, |
| + } { |
| + plain := "plain-" + row.kind + row.role |
| + insertTokenRow(t, s, plain, row.kind, acct.ID, "", row.role, row.expires, 0, 0) |
| + if _, ok := s.CLIPrincipal(plain, time.Now()); !ok { |
| + t.Fatalf("kind %q role %q with no platform columns does not resolve", row.kind, row.role) |
| + } |
| + for _, cols := range [][2]int64{{5, 0}, {0, 1}, {5, 1}} { |
| + raw := plain + "-" + string(rune('a'+cols[0])) + string(rune('a'+cols[1])) |
| + insertTokenRow(t, s, raw, row.kind, acct.ID, "", row.role, row.expires, cols[0], cols[1]) |
| + if p, ok := s.CLIPrincipal(raw, time.Now()); ok { |
| + t.Errorf("kind %q role %q with platform_account_id %d, platform_generation %d resolved to %+v", |
| + row.kind, row.role, cols[0], cols[1], p) |
| + } |
| + } |
| + } |
| +} |
| + |
| +func TestPlatformCredentialFromAnEndedGenerationIsRefused(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + acct := planAccount(t, fx, 501) |
| + owner := "owner" + acct.Sqid + "@example.com" |
| + gen := grantPlatformGen(t, fx.st, acct.ID) |
| + old, err := fx.st.NewServicePrincipal(acct.ID, "cms", "", svcrole.Platform, owner, time.Now()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + p := wantPlatformPrincipal(t, fx.st, old, acct, gen) |
| + if status := probeStatus(t, fx, acct.Sqid, old); status != http.StatusForbidden { |
| + t.Fatalf("live platform credential = %d, want the closed door's 403", status) |
| + } |
| + |
| + if regen := regrantPlatform(t, fx.st, acct.ID); regen != gen+1 { |
| + t.Fatalf("re-grant generation %d, want %d", regen, gen+1) |
| + } |
| + if status := probeStatus(t, fx, acct.Sqid, old); status != http.StatusUnauthorized { |
| + t.Fatalf("generation %d credential after the re-grant = %d, want 401", gen, status) |
| + } |
| + // The revoke swept the row. Un-revoke it, so only the generation check |
| + // can refuse it (pre-flight C4). |
| + if _, err := fx.st.db.ExecContext(context.Background(), `UPDATE cli_tokens SET revoked_at = 0 WHERE credential_id = ?`, |
| + p.CredentialID); err != nil { |
| + t.Fatal(err) |
| + } |
| + if status := probeStatus(t, fx, acct.Sqid, old); status != http.StatusUnauthorized { |
| + t.Fatalf("un-revoked generation %d credential = %d, want 401", gen, status) |
| + } |
| + if q, ok := fx.st.CLIPrincipal(old, time.Now()); ok { |
| + t.Fatalf("un-revoked generation %d credential resolved to %+v", gen, q) |
| + } |
| + |
| + fresh, err := fx.st.NewServicePrincipal(acct.ID, "cms-2", "", svcrole.Platform, owner, time.Now()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + wantPlatformPrincipal(t, fx.st, fresh, acct, gen+1) |
| + if status := probeStatus(t, fx, acct.Sqid, fresh); status != http.StatusForbidden { |
| + t.Fatalf("fresh platform credential = %d, want the closed door's 403", status) |
| + } |
| +} |
| + |
| +func TestRevokedPlatformCredentialIsGoneFromManagement(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + acct := planAccount(t, fx, 501) |
| + owner := cliToken(t, fx, "owner"+acct.Sqid+"@example.com", "code-platform-cred-manage") |
| + gen := grantPlatformGen(t, fx.st, acct.ID) |
| + mint := map[string]any{"name": "cms", "role": "platform"} |
| + status, out := postJSON(t, servicesURL(fx, acct.Sqid), mint, owner) |
| + if status != http.StatusCreated { |
| + t.Fatalf("mint = %d %v", status, out) |
| + } |
| + old, _ := out["token"].(string) |
| + regrantPlatform(t, fx.st, acct.ID) |
| + |
| + status, out = getJSON(t, servicesURL(fx, acct.Sqid), owner) |
| + if status != http.StatusOK { |
| + t.Fatalf("list = %d %v", status, out) |
| + } |
| + if services, _ := out["services"].([]any); len(services) != 0 { |
| + t.Fatalf("list after the revoke = %v, want no cms", services) |
| + } |
| + |
| + var before int |
| + if err := fx.st.db.QueryRowContext(context.Background(), `SELECT COUNT(*) FROM cli_tokens WHERE account_id = ?`, |
| + acct.ID).Scan(&before); err != nil { |
| + t.Fatal(err) |
| + } |
| + // ErrNoServicePrincipal: the stdlib 404, which carries no token. |
| + if status, body := postRaw(t, servicesURL(fx, acct.Sqid)+"/cms/rotate", map[string]any{}, owner); status != http.StatusNotFound || |
| + body != "404 page not found\n" { |
| + t.Fatalf("rotate after the revoke = %d %q, want the stdlib 404", status, body) |
| + } |
| + var after int |
| + if err := fx.st.db.QueryRowContext(context.Background(), `SELECT COUNT(*) FROM cli_tokens WHERE account_id = ? |
| + AND revoked_at = 0`, acct.ID).Scan(&after); err != nil { |
| + t.Fatal(err) |
| + } |
| + if after != 0 { |
| + t.Fatalf("rotate after the revoke left %d live rows (of %d), want none", after, before) |
| + } |
| + |
| + status, out = postJSON(t, servicesURL(fx, acct.Sqid), mint, owner) |
| + if status != http.StatusCreated { |
| + t.Fatalf("mint again = %d %v, want 201", status, out) |
| + } |
| + fresh, _ := out["token"].(string) |
| + // Until Task 8's customer list door exists, the new token's resolution |
| + // stands in for its GET /api/cli/platform/<p>/customers 200. Task 8 |
| + // Step 5 puts the GET back (pre-flight C3). |
| + wantPlatformPrincipal(t, fx.st, fresh, acct, gen+1) |
| + if status := probeStatus(t, fx, acct.Sqid, old); status != http.StatusUnauthorized { |
| + t.Fatalf("old token after the re-mint = %d, want 401", status) |
| + } |
| +} |
| + |
| +func TestBrowserMintFormDoesNotOfferPlatform(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + acct := planAccount(t, fx, 501) |
| + grantPlatformGen(t, fx.st, acct.ID) |
| + p := &page{} |
| + fx.srv.servicePane(p, acct) |
| + if len(p.ServiceRoles) == 0 { |
| + t.Fatal("the form offers no roles") |
| + } |
| + if slices.Contains(p.ServiceRoles, string(svcrole.Platform)) { |
| + t.Fatalf("the form offers %v, want no %q", p.ServiceRoles, svcrole.Platform) |
| + } |
| +} |
diff --git a/internal/console/svcpage.go b/internal/console/svcpage.go| index cd8e8ebf7..34f30f91e 100644 |
| --- a/internal/console/svcpage.go |
| +++ b/internal/console/svcpage.go |
| @@ -183,7 +183,12 @@ func (s *Server) servicePane(p *page, acct *Account) { |
| } else { |
| p.Services = rows |
| } |
| + // Not the platform role: platform access is API and CLI only (spec |
| + // §2), and a platform credential is minted with carlos services. |
| for _, role := range svcrole.All() { |
| + if role == svcrole.Platform { |
| + continue |
| + } |
| p.ServiceRoles = append(p.ServiceRoles, string(role)) |
| } |
| p.ServiceRungs = deployRungs |
diff --git a/internal/console/svcprincipal.go b/internal/console/svcprincipal.go| index ba73000c8..407135680 100644 |
| --- a/internal/console/svcprincipal.go |
| +++ b/internal/console/svcprincipal.go |
| @@ -21,6 +21,7 @@ package console |
| import ( |
| "context" |
| "crypto/subtle" |
| + "database/sql" |
| "encoding/json" |
| "errors" |
| "fmt" |
| @@ -84,6 +85,13 @@ type Principal struct { |
| // LinkID is a link principal's link (AccountID is its account). "" |
| // for every other kind. |
| LinkID string |
| + // PlatformAccountID, Platform and PlatformGeneration are a platform |
| + // credential's platform: its account id and sqid, and the grant |
| + // generation it was minted in, which CLIPrincipal found live (Astra |
| + // r1-2). Zero for every other credential. |
| + PlatformAccountID int64 |
| + Platform string |
| + PlatformGeneration int64 |
| } |
| // ErrServiceNameTaken is returned by NewServicePrincipal when the account |
| @@ -115,11 +123,12 @@ func (s *Store) CLIPrincipalContext(ctx context.Context, rawToken string, now ti |
| } |
| hash := hashToken(rawToken) |
| var storedHash, addr, kind, app, role, name, rungs, credID, commands, teamScope string |
| - var accountID, expires, revoked int64 |
| + var accountID, expires, revoked, platformAccountID, platformGeneration int64 |
| err := s.db.QueryRowContext(ctx, `SELECT token_hash, address, kind, account_id, app, role, name, expires_at, revoked_at, rungs, |
| -credential_id, commands, scope |
| +credential_id, commands, scope, platform_account_id, platform_generation |
| FROM cli_tokens WHERE token_hash = ?`, hash). |
| - Scan(&storedHash, &addr, &kind, &accountID, &app, &role, &name, &expires, &revoked, &rungs, &credID, &commands, &teamScope) |
| + Scan(&storedHash, &addr, &kind, &accountID, &app, &role, &name, &expires, &revoked, &rungs, &credID, &commands, &teamScope, |
| + &platformAccountID, &platformGeneration) |
| if err != nil { |
| return Principal{}, false |
| } |
| @@ -152,6 +161,14 @@ FROM cli_tokens WHERE token_hash = ?`, hash). |
| } else if !(expires == 0 || now.Unix() <= expires) { |
| return Principal{}, false |
| } |
| + // Only a platform credential carries a platform: on any other row the |
| + // columns are a row this console did not write, and reading past them |
| + // would let a row claim a platform's authority by a column nothing |
| + // checks. |
| + isPlatformRow := kind == cliKindService && role == string(svcrole.Platform) |
| + if !isPlatformRow && (platformAccountID != 0 || platformGeneration != 0) { |
| + return Principal{}, false |
| + } |
| var p Principal |
| switch kind { |
| case cliKindUser: |
| @@ -210,6 +227,21 @@ FROM cli_tokens WHERE token_hash = ?`, hash). |
| } |
| p = Principal{Kind: PrincipalService, Address: addr, AccountID: accountID, App: app, Role: r, Name: name, Rungs: scope, |
| CredentialID: credID, Commands: granted, Scope: teamScope} |
| + // A platform credential is its own account's, bound to no app, |
| + // and works only while the grant generation it was minted in is |
| + // the live one (Astra r1-2). The generation, not the revoke |
| + // sweep, is the authority, and not the grant's state alone: a |
| + // deleted platform's grant row can still read active. |
| + if r == svcrole.Platform { |
| + if app != "" || platformAccountID != accountID || platformGeneration == 0 { |
| + return Principal{}, false |
| + } |
| + sqid, live := s.PlatformGenerationLive(ctx, s.db, platformAccountID, platformGeneration) |
| + if !live { |
| + return Principal{}, false |
| + } |
| + p.PlatformAccountID, p.Platform, p.PlatformGeneration = platformAccountID, sqid, platformGeneration |
| + } |
| case cliKindLink: |
| // A link credential is its link's current credential or nothing: |
| // the links row must be live, in the same account, and hold this |
| @@ -235,6 +267,10 @@ FROM cli_tokens WHERE token_hash = ?`, hash). |
| // and a scope refusal is the caller's to fix while a busy database is not. |
| var ErrAccountScopedService = errors.New("console: a service-role credential must be bound to one app") |
| +// ErrAppScopedPlatform is a platform credential minted with an app. It |
| +// speaks for the whole platform account and is never bound to one app. |
| +var ErrAppScopedPlatform = errors.New("console: a platform credential is never bound to an app") |
| + |
| // ErrAccountScopedDeploy is a deploy credential minted without an app. |
| var ErrAccountScopedDeploy = errors.New("console: a deploy credential must be bound to one app") |
| @@ -373,6 +409,13 @@ func (s *Store) claimRawNewServicePrincipalScoped(accountID int64, name, app str |
| if role == svcrole.Service && app == "" { |
| return "", ErrAccountScopedService |
| } |
| + // A platform credential is the platform account's, never one app's. |
| + // An app-bound one would also be a row the revoke sweep cannot clear |
| + // under the armed claim gate: the gate's permit-free revoke covers rows |
| + // with a platform_account_id, and an app-bound row needs a permit. |
| + if role == svcrole.Platform && app != "" { |
| + return "", ErrAppScopedPlatform |
| + } |
| canon, err := canonicalRungs(rungs) |
| if err != nil { |
| return "", err |
| @@ -426,16 +469,35 @@ func (s *Store) claimRawNewServicePrincipalScoped(accountID int64, name, app str |
| if count > 0 { |
| return "", ErrServiceNameTaken |
| } |
| + // A platform credential holds the platform account's grant, so the |
| + // grant is read in this transaction and the row records the generation |
| + // it was minted in: a revoke after this commit ends that generation, |
| + // and with it the credential (Astra r1-2). The account must be live: |
| + // a deleted platform's grant row can still read active. This function |
| + // takes no ctx (pre-flight C11). |
| + var platformAccountID, platformGeneration int64 |
| + if role == svcrole.Platform { |
| + err := tx.QueryRowContext(context.Background(), `SELECT g.generation FROM platform_grants g |
| + JOIN accounts a ON a.id = g.account_id WHERE g.account_id = ? AND g.state = ? AND a.deleted_at = 0`, |
| + accountID, PlatformGrantActive).Scan(&platformGeneration) |
| + if errors.Is(err, sql.ErrNoRows) { |
| + return "", ErrNotPlatform |
| + } |
| + if err != nil { |
| + return "", err |
| + } |
| + platformAccountID = accountID |
| + } |
| raw, err := randomToken() |
| if err != nil { |
| return "", err |
| } |
| if _, err := tx.Exec(`INSERT INTO cli_tokens |
| (token_hash, address, label, remote_addr, created_at, rotated_at, expires_at, kind, account_id, app, role, name, rungs, |
| -credential_id, commands, scope) |
| -VALUES (?, ?, '', '', ?, ?, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, |
| +credential_id, commands, scope, platform_account_id, platform_generation) |
| +VALUES (?, ?, '', '', ?, ?, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, |
| hashToken(raw), mintedBy, now.Unix(), now.Unix(), cliKindService, accountID, app, string(role), name, canon, |
| - credID, cmds, scope); err != nil { |
| + credID, cmds, scope, platformAccountID, platformGeneration); err != nil { |
| return "", err |
| } |
| if err := tx.Commit(); err != nil { |