From 38f1a3a3a9dcfdd814371c8bfffdd774015dff73 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Tue, 17 Nov 2026 23:37:43 -0500 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20hub:=20v1=20registration=20reso?= =?UTF-8?q?urces,=20per-repo=20proofs=20and=20revision?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rollout step 1, part B of the hub registration protocol (hivecommons/hive#18642), ported from the tuna-os/hive#189 prototype: - Per-repo proof times (SelfServiceHive.RepoProofs) and a Revision counter. Both are additive JSON fields, so older binaries ignore them; a record without a revision loads as 0. Heartbeats do not bump the revision, so a live spoke never invalidates an operator's ETag. - Store update() that restores the key hashes, owner, ID, CreatedAt and the heartbeat stamp after any mutation, plus rotate and revoke guarded by an ETag precondition. - selfServiceRegisterCore and selfServiceProveOwnership split out of the legacy register handler. Tests pin the legacy /api/hub/selfservice/* responses byte for byte. - v1 POST/GET/list/DELETE and POST .../keys: an ETag on every record, optional If-Match on delete and rotate, 413 with limit: max_repos over the repo cap, 201 on rotate, and a 400 for a caller-chosen grace window. Behind HIVE_HUB_SELFSERVICE_REGISTRATION. No GitHub writes; heartbeat_signing.go and the credential fence are untouched. Co-authored-by: James Reilly <5840441+hanthor@users.noreply.github.com> Signed-off-by: James Reilly Signed-off-by: Andrew Anderson --- .../added-18642-hub-v1-registrations.md | 1 + src/docs/api-reference/hub-server.md | 5 + src/docs/env-vars.md | 2 +- src/docs/hub-deployment.md | 2 +- src/pkg/hub/selfservice_api.go | 129 +++-- src/pkg/hub/selfservice_registry.go | 126 +++++ src/pkg/hub/selfservice_v1_api.go | 237 +++++++- src/pkg/hub/selfservice_v1_api_test.go | 514 +++++++++++++++++- src/pkg/hub/selfservice_v1_registry_test.go | 317 +++++++++++ 9 files changed, 1266 insertions(+), 67 deletions(-) create mode 100644 changelog.d/added-18642-hub-v1-registrations.md create mode 100644 src/pkg/hub/selfservice_v1_registry_test.go diff --git a/changelog.d/added-18642-hub-v1-registrations.md b/changelog.d/added-18642-hub-v1-registrations.md new file mode 100644 index 0000000000..b6ca69c681 --- /dev/null +++ b/changelog.d/added-18642-hub-v1-registrations.md @@ -0,0 +1 @@ +- Added the hub registration protocol v1 registration resources (rollout step 1 of [#18642](https://github.com/hivecommons/hive/issues/18642)): `POST/GET /api/hub/v1/registrations`, `GET/DELETE /api/hub/v1/registrations/{id}` and `POST .../{id}/keys` over the existing self-service registry, with a strong `ETag` on every record (from a revision counter that heartbeats do not bump), optional `If-Match` on delete and rotate, per-repo ownership proof times (`repo_proofs`), a `413` with `limit: max_repos` for a repo list over the cap and `201` on rotate. The legacy `/api/hub/selfservice/*` responses are unchanged and everything stays behind `HIVE_HUB_SELFSERVICE_REGISTRATION`. diff --git a/src/docs/api-reference/hub-server.md b/src/docs/api-reference/hub-server.md index c8a838b736..725d67a417 100644 --- a/src/docs/api-reference/hub-server.md +++ b/src/docs/api-reference/hub-server.md @@ -59,6 +59,11 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `GET` | `/api/hub/selfservice/hives` | Hub auth | The caller's own self-service (`byo-`) hives: `{hives[]}` of `{id, owner, org, repos, dashboard_url, created_at, ownership_checked_at, last_accepted_heartbeat_at, online}`; never a key or a hash. `404` while `HIVE_HUB_SELFSERVICE_REGISTRATION` is off | `pkg/hub/server.go:1787` | | `POST` | `/api/hub/selfservice/hives` | Hub auth | Register a self-hosted hive: body `{name, org, repos[], dashboard_url}`; the ID is `byo---` (the double hyphen keeps IDs unambiguous, since logins contain hyphens but never `--`). Proves `permissions.admin` on every `owner/name` repo with the caller's own stored GitHub token (`403` naming the refused repo, nothing stored); `400` invalid or reserved name, a login that contains `--` or starts or ends with `-`, bare-org or `.`/`..` repo, or non-`https`/private `dashboard_url`; `409` ID already registered; `429` over `HIVE_HUB_SELFSERVICE_MAX_PER_USER` (enforced again under the store lock, so concurrent registrations cannot exceed it). `201 {hive, key, hub_url, snippets{docker_compose, podman, kubernetes}}`: the raw key appears here once and only its hash is stored. `404` while the flag is off | `pkg/hub/server.go:1788` | | `GET` | `/api/hub/v1` | Public | Hub registration protocol v1 discovery ([#18642](https://github.com/hivecommons/hive/issues/18642)): `{protocol, version, base_path, operations[], patch_fields[], limits{max_repos, max_per_user, expiry, key_rotation_overlap}, proof_methods[]}`. Readable without signing in so a client learns the limits before it hits a `4xx`. `proof_methods` is `["hub-session-token"]`: ownership is proven with the GitHub token stored at hub sign-in. `max_repos` is `HIVE_HUB_SELFSERVICE_MAX_REPOS`. `404` while `HIVE_HUB_SELFSERVICE_REGISTRATION` is off | `pkg/hub/selfservice_v1_api.go` | +| `POST` | `/api/hub/v1/registrations` | Hub auth | Register a self-hosted hive as a resource ([#18642](https://github.com/hivecommons/hive/issues/18642)): the same body, proof and refusals as `POST /api/hub/selfservice/hives` (including `429` over `HIVE_HUB_SELFSERVICE_MAX_PER_USER`), except that a repo list over `HIVE_HUB_SELFSERVICE_MAX_REPOS` answers `413 {error, limit: "max_repos", max_repos}` instead of `400`. `201 {registration, key, hub_url, snippets}` with `Location` and a strong `ETag` header; `registration` is the legacy view plus `etag` and `repo_proofs[{repo, checked_at}]` (when the owner's admin permission on each repo was last proven). The raw key appears once. `404` while `HIVE_HUB_SELFSERVICE_REGISTRATION` is off | `pkg/hub/selfservice_v1_api.go` | +| `GET` | `/api/hub/v1/registrations` | Hub auth | The caller's own registrations: `{registrations[]}`, each with its `etag` and `repo_proofs`; never a key or a hash | `pkg/hub/selfservice_v1_api.go` | +| `GET` | `/api/hub/v1/registrations/{id}` | Hub auth | One of the caller's registrations: `{registration}` with a strong `ETag` header; `If-None-Match` with the current ETag answers `304`. Another user's hive, a missing hive and a hub admin who does not own it all get `404`. The ETag comes from a revision counter that heartbeats do not bump, and reveals nothing about the key | `pkg/hub/selfservice_v1_api.go` | +| `POST` | `/api/hub/v1/registrations/{id}/keys` | Hub auth | Owner only: rotate the heartbeat key; the previous key keeps authenticating for the 10 minute overlap. `201 {id, key, hub_url, overlap, snippets}` (the legacy rotate answers `200`) with the new `ETag`; the raw key appears once. Optional `If-Match` (`412` when stale). A body asking for a grace window is refused with `400`: caller-chosen grace is a later rollout step | `pkg/hub/selfservice_v1_api.go` | +| `DELETE` | `/api/hub/v1/registrations/{id}` | Hub auth | Owner or hub admin: revoke the key and remove the registry entry. `200 {removed, id}`. Optional `If-Match` (`412` when stale, carrying the current `ETag`) | `pkg/hub/selfservice_v1_api.go` | | `POST` | `/api/hub/selfservice/hives/{id}/rotate` | Hub auth | Owner only: mint a new heartbeat key for `{id}`; the previous key keeps authenticating for `overlap` (10 minutes). `200 {id, key, hub_url, overlap, snippets}`, raw key once. Another user's hive, a missing hive and a hub admin who does not own it all get `404` | `pkg/hub/server.go:1789` | | `DELETE` | `/api/hub/selfservice/hives/{id}` | Hub auth | Owner or hub admin: revoke the key and remove the registry entry. `200 {removed, id}`; another user's hive is `404` | `pkg/hub/server.go:1790` | | `POST` | `/api/contribute/register` | Hub handler-specific | Contribute Proxy | `pkg/hub/server.go:1794` | diff --git a/src/docs/env-vars.md b/src/docs/env-vars.md index 531b7698a5..3ea0410edf 100644 --- a/src/docs/env-vars.md +++ b/src/docs/env-vars.md @@ -98,7 +98,7 @@ behavior. [Maintenance limitations](#keeping-this-reference-current) still apply | `HIVE_HUB_MAIL_CHUNK_BYTES` | No | `786432` | Piece size when the hub relays a large mail-in delivery to the spoke (768 KiB, under the 1 MiB ingress default); clamped to 64 KiB to 8 MiB. | | `HIVE_HUB_MAIL_SPOOL_DIR` | No | system temp directory | Where the hub keeps a delivery on disk while it relays it. | | `HIVE_HUB_SELFSERVICE_MAX_PER_USER` | No | `5` | Maximum self-service hives one user may have registered at once; the next registration answers `429` until one is removed. | -| `HIVE_HUB_SELFSERVICE_MAX_REPOS` | No | `20` | Maximum repos one self-service registration may list ([#18642](https://github.com/hivecommons/hive/issues/18642)). Each listed repo costs one read-only GitHub ownership check, so values above `500` are clamped to `500`; a non-numeric or non-positive value falls back to `20`. Advertised by `GET /api/hub/v1` as `limits.max_repos`. Over the cap, `POST /api/hub/selfservice/hives` answers `400`. Read per request. Only meaningful while `HIVE_HUB_SELFSERVICE_REGISTRATION` is on. | +| `HIVE_HUB_SELFSERVICE_MAX_REPOS` | No | `20` | Maximum repos one self-service registration may list ([#18642](https://github.com/hivecommons/hive/issues/18642)). Each listed repo costs one read-only GitHub ownership check, so values above `500` are clamped to `500`; a non-numeric or non-positive value falls back to `20`. Advertised by `GET /api/hub/v1` as `limits.max_repos`. Over the cap, `POST /api/hub/selfservice/hives` answers `400`; `POST /api/hub/v1/registrations` answers `413` with `limit: max_repos`. Read per request. Only meaningful while `HIVE_HUB_SELFSERVICE_REGISTRATION` is on. | | `HIVE_HUB_SELFSERVICE_EXPIRY` | No | `24h` | How long a self-service hive may go without an accepted heartbeat before the expiry sweep revokes its key and removes its registry entry (counted from the last accepted heartbeat, or from registration while there has been none). Reported by `GET /api/hub/selfservice/status` as `expiry`. Go duration syntax. | | `HIVE_HUB_SELFSERVICE_SWEEP_INTERVAL` | No | `10m` | How often the self-service expiry sweep (`src/pkg/hub/selfservice_expiry.go`) runs. Each removal writes `audit: selfservice_hive_expired` (see [audit-log.md](audit-log.md)). Read at startup; restart after changing. Go duration syntax. | | `HIVE_HUB_AGENT_RESTART_PROBLEM_THRESHOLD` | No | `5` | Hosted hub threshold for surfacing agent restart storms on `/fleet`. When an agent reports at least this many restarts in the last 24 hours (or since the hub-side reset marker), the hive gets a restart problem chip and drift signal. | diff --git a/src/docs/hub-deployment.md b/src/docs/hub-deployment.md index e6ae911380..0be54f6de0 100644 --- a/src/docs/hub-deployment.md +++ b/src/docs/hub-deployment.md @@ -102,7 +102,7 @@ The fence above is always on. The registration flow that fills the `byo-` namesp |---|---|---| | `HIVE_HUB_SELFSERVICE_REGISTRATION` | off | Turns on `/api/hub/selfservice/*` and the **Register your own hive** form on the dashboard. Accepts `1/true/yes/on`. Read per request, so no restart is needed. While off, every route except `GET /api/hub/selfservice/status` answers `404`, and the dashboard button sends users to the self-hosting guide instead of the form. | | `HIVE_HUB_SELFSERVICE_MAX_PER_USER` | `5` | How many `byo-` hives one GitHub login may hold at once. The next registration answers `429` until the user removes one. | -| `HIVE_HUB_SELFSERVICE_MAX_REPOS` | `20` | How many repos one registration may list ([#18642](https://github.com/hivecommons/hive/issues/18642)). Each repo is one read-only GitHub ownership check, so values above `500` are clamped to `500`. The registration route answers `400` over the cap. Advertised to clients by `GET /api/hub/v1` as `limits.max_repos`. Read per request. | +| `HIVE_HUB_SELFSERVICE_MAX_REPOS` | `20` | How many repos one registration may list ([#18642](https://github.com/hivecommons/hive/issues/18642)). Each repo is one read-only GitHub ownership check, so values above `500` are clamped to `500`. `POST /api/hub/selfservice/hives` answers `400` over the cap; `POST /api/hub/v1/registrations` answers `413` with `limit: max_repos`. Advertised to clients by `GET /api/hub/v1` as `limits.max_repos`. Read per request. | | `HIVE_HUB_SELFSERVICE_EXPIRY` | `24h` | How long a registered hive may go without an accepted heartbeat before the sweep removes it. Counted from the last accepted heartbeat, or from registration while there has been none. | | `HIVE_HUB_SELFSERVICE_SWEEP_INTERVAL` | `10m` | How often the expiry sweep runs. Read at startup; restart after changing. | diff --git a/src/pkg/hub/selfservice_api.go b/src/pkg/hub/selfservice_api.go index 460c6d0bd0..e9f42c9429 100644 --- a/src/pkg/hub/selfservice_api.go +++ b/src/pkg/hub/selfservice_api.go @@ -441,76 +441,140 @@ func (s *HubServer) handleSelfServiceRegister(w http.ResponseWriter, r *http.Req if !s.requireSelfServiceEnabled(w) { return } + stored, rawKey, ok := s.selfServiceRegisterCore(w, r, false) + if !ok { + return + } + hubURL := hubPublicURL() + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + _ = json.NewEncoder(w).Encode(map[string]any{ + "hive": selfServiceView(stored, false), + "key": rawKey, + "hub_url": hubURL, + "snippets": selfServiceSnippets(hubURL, stored.ID, rawKey), + }) +} + +// selfServiceRegisterCore validates a registration body, runs the ownership +// proof and stores the record. It writes every refusal itself and returns +// ok=false; on success it writes nothing, so the v6 route and the v1 resource +// route (selfservice_v1_api.go) can each shape their own 201 body. v1 selects +// the v1 over-cap answer (413 naming the limit); the v6 route keeps its 400. +func (s *HubServer) selfServiceRegisterCore(w http.ResponseWriter, r *http.Request, v1 bool) (SelfServiceHive, string, bool) { login := s.getAuthUser(r) user := loadSaaSUser(login) if user == nil { writeJSONError(w, http.StatusUnauthorized, "not authenticated") - return + return SelfServiceHive{}, "", false } var body selfServiceRegisterRequest if err := json.NewDecoder(io.LimitReader(r.Body, selfServiceRequestBodyLimit())).Decode(&body); err != nil { writeJSONError(w, http.StatusBadRequest, "invalid JSON body") - return + return SelfServiceHive{}, "", false } githubLogin := topRepoProfileLogin(user) if githubLogin == "" { s.auditSelfServiceRefused(login, "", selfServiceRefusedNoGitHubLogin) writeJSONError(w, http.StatusBadRequest, "a GitHub identity is required to register a self-hosted hive") - return + return SelfServiceHive{}, "", false } id, reason, err := selfServiceBuildID(githubLogin, body.Name) if err != nil { s.auditSelfServiceRefused(login, "", reason) writeJSONError(w, http.StatusBadRequest, err.Error()) - return + return SelfServiceHive{}, "", false } if s.selfServiceRegistryHas(id) { s.auditSelfServiceRefused(login, id, selfServiceRefusedDuplicateID) writeJSONError(w, http.StatusConflict, "a hive with this ID is already registered") - return + return SelfServiceHive{}, "", false } if _, exists := s.selfService.get(id); exists { s.auditSelfServiceRefused(login, id, selfServiceRefusedDuplicateID) writeJSONError(w, http.StatusConflict, "a hive with this ID is already registered") - return + return SelfServiceHive{}, "", false } org := strings.TrimSpace(body.Org) if org != "" && !isValidName(org) { s.auditSelfServiceRefused(login, id, selfServiceRefusedInvalidName) writeJSONError(w, http.StatusBadRequest, "org may contain only letters, digits, '.', '_' and '-'") - return + return SelfServiceHive{}, "", false } repos, err := selfServiceValidateRepos(body.Repos) if err != nil { s.auditSelfServiceRefused(login, id, selfServiceRefusedInvalidRepos) + if maxRepos := selfServiceMaxRepos(); v1 && len(body.Repos) > maxRepos { + writeHubV1RepoCapError(w, err, maxRepos) + return SelfServiceHive{}, "", false + } writeJSONError(w, http.StatusBadRequest, err.Error()) - return + return SelfServiceHive{}, "", false } dashboardURL, err := selfServiceValidateDashboardURL(r.Context(), body.DashboardURL) if err != nil { s.auditSelfServiceRefused(login, id, selfServiceRefusedInvalidURL) writeJSONError(w, http.StatusBadRequest, err.Error()) - return + return SelfServiceHive{}, "", false } if maxPerUser := selfServiceMaxPerUser(); s.selfServiceCount(login) >= maxPerUser { s.auditSelfServiceRefused(login, id, selfServiceRefusedCap, "max_per_user", maxPerUser) writeJSONError(w, http.StatusTooManyRequests, fmt.Sprintf("you already have %d self-hosted hives registered; remove one first", maxPerUser)) - return + return SelfServiceHive{}, "", false + } + + if !s.selfServiceProveOwnership(w, r, login, id, user, repos) { + return SelfServiceHive{}, "", false + } + + now := time.Now().UTC() + // The cap is enforced again inside the store, under the lock that inserts + // the record: the count above ran before up to selfServiceMaxRepos() GitHub + // calls, so concurrent POSTs from one user could all have passed it. + maxPerUser := selfServiceMaxPerUser() + rawKey, err := s.selfService.registerCapped(SelfServiceHive{ + ID: id, Owner: login, Org: org, Repos: repos, DashboardURL: dashboardURL, OwnershipCheckedAt: now, + RepoProofs: selfServiceProofTimes(nil, repos, time.Time{}, now, repos), + }, maxPerUser) + if err != nil { + if errors.Is(err, errSelfServiceExists) { + s.auditSelfServiceRefused(login, id, selfServiceRefusedDuplicateID) + writeJSONError(w, http.StatusConflict, "a hive with this ID is already registered") + return SelfServiceHive{}, "", false + } + if errors.Is(err, errSelfServiceCapReached) { + s.auditSelfServiceRefused(login, id, selfServiceRefusedCap, "max_per_user", maxPerUser) + writeJSONError(w, http.StatusTooManyRequests, fmt.Sprintf("you already have %d self-hosted hives registered; remove one first", maxPerUser)) + return SelfServiceHive{}, "", false + } + s.logger.Error("self-service registration: store write failed", "user", login, "hive", id, "error", err) + writeJSONError(w, http.StatusInternalServerError, "could not persist the registration") + return SelfServiceHive{}, "", false } + s.logger.Info("audit: selfservice_hive_registered", "user", login, "hive", id, "repos", len(repos), "org", org) + + stored, _ := s.selfService.get(id) + return stored, rawKey, true +} +// selfServiceProveOwnership checks that user administers every repo in repos, +// with the user's OWN stored GitHub token. It is a read (GET /repos/{o}/{r}) +// and nothing else. It writes the refusal and returns false when any repo +// fails; true means every repo passed. +func (s *HubServer) selfServiceProveOwnership(w http.ResponseWriter, r *http.Request, login, id string, user *SaaSUser, repos []string) bool { // Ownership proof with the user's OWN token, never the hub's App token: the // question is what this person administers, not what the hub can see. if strings.TrimSpace(user.EncryptedToken) == "" { s.auditSelfServiceRefused(login, id, selfServiceRefusedNoToken) writeJSONError(w, http.StatusForbidden, "no GitHub token is stored for your account; sign out and back in with GitHub, then retry") - return + return false } token, err := decryptToken(user.EncryptedToken) if err != nil { s.logger.Warn("self-service registration: stored token could not be decrypted", "user", login, "error", err) writeJSONError(w, http.StatusInternalServerError, "stored GitHub token could not be read") - return + return false } checker := s.ownershipChecker() for _, repo := range repos { @@ -520,7 +584,7 @@ func (s *HubServer) handleSelfServiceRegister(w http.ResponseWriter, r *http.Req s.auditSelfServiceRefused(login, id, selfServiceRefusedOwnershipError, "repo", repo) s.logger.Warn("self-service registration: ownership check failed", "user", login, "hive", id, "repo", repo, "error", err) writeJSONError(w, http.StatusBadGateway, fmt.Sprintf("could not verify your permission on %s; try again", repo)) - return + return false } if !admin { s.auditSelfServiceRefused(login, id, selfServiceRefusedNotAdmin, "repo", repo) @@ -530,45 +594,10 @@ func (s *HubServer) handleSelfServiceRegister(w http.ResponseWriter, r *http.Req "error": fmt.Sprintf("your GitHub account is not an admin of %s", repo), "repo": repo, }) - return + return false } } - - now := time.Now().UTC() - // The cap is enforced again inside the store, under the lock that inserts - // the record: the count above ran before up to selfServiceMaxRepos() GitHub - // calls, so concurrent POSTs from one user could all have passed it. - maxPerUser := selfServiceMaxPerUser() - rawKey, err := s.selfService.registerCapped(SelfServiceHive{ - ID: id, Owner: login, Org: org, Repos: repos, DashboardURL: dashboardURL, OwnershipCheckedAt: now, - }, maxPerUser) - if err != nil { - if errors.Is(err, errSelfServiceExists) { - s.auditSelfServiceRefused(login, id, selfServiceRefusedDuplicateID) - writeJSONError(w, http.StatusConflict, "a hive with this ID is already registered") - return - } - if errors.Is(err, errSelfServiceCapReached) { - s.auditSelfServiceRefused(login, id, selfServiceRefusedCap, "max_per_user", maxPerUser) - writeJSONError(w, http.StatusTooManyRequests, fmt.Sprintf("you already have %d self-hosted hives registered; remove one first", maxPerUser)) - return - } - s.logger.Error("self-service registration: store write failed", "user", login, "hive", id, "error", err) - writeJSONError(w, http.StatusInternalServerError, "could not persist the registration") - return - } - s.logger.Info("audit: selfservice_hive_registered", "user", login, "hive", id, "repos", len(repos), "org", org) - - stored, _ := s.selfService.get(id) - hubURL := hubPublicURL() - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) - _ = json.NewEncoder(w).Encode(map[string]any{ - "hive": selfServiceView(stored, false), - "key": rawKey, - "hub_url": hubURL, - "snippets": selfServiceSnippets(hubURL, id, rawKey), - }) + return true } // selfServiceOwnedHive loads the hive named in the path and checks login owns diff --git a/src/pkg/hub/selfservice_registry.go b/src/pkg/hub/selfservice_registry.go index 8cd0bd4b35..ffa718c3e1 100644 --- a/src/pkg/hub/selfservice_registry.go +++ b/src/pkg/hub/selfservice_registry.go @@ -72,6 +72,9 @@ var ( // errSelfServiceCapReached is returned by registerCapped when the owner // already holds the maximum number of hives (#18166). errSelfServiceCapReached = errors.New("self-service per-owner cap reached") + // errSelfServiceStale is returned by update when the caller's ETag no + // longer names the stored record (#18642): someone else changed it first. + errSelfServiceStale = errors.New("self-service hive changed since it was read") ) // SelfServiceHive is one owner-registered hive. Only hashes of its keys are @@ -94,10 +97,71 @@ type SelfServiceHive struct { // OwnershipCheckedAt is when the hub last confirmed the owner's admin // permission on Repos. Zero when never checked. OwnershipCheckedAt time.Time `json:"ownership_checked_at,omitempty"` + // RepoProofs is when the owner's admin permission was last proven, per + // repo, keyed by the lower-cased owner/name (#18642). Create sets every + // repo; a v1 PATCH sets only the repos it adds and drops the ones it + // removes. Records from before the field have no entries; their repos + // were all proven at OwnershipCheckedAt. + RepoProofs map[string]time.Time `json:"repo_proofs,omitempty"` // LastAcceptedHeartbeatAt is the last heartbeat that passed the bearer // check. Zero until the first one; the expiry sweep (selfservice_expiry.go) // counts the window from here, or from CreatedAt while it is zero. LastAcceptedHeartbeatAt time.Time `json:"last_accepted_heartbeat_at,omitempty"` + // Revision counts owner-visible changes (register, PATCH, key rotation) + // and feeds the v1 API's ETag (#18642). A heartbeat stamp does not bump + // it, so a live spoke does not invalidate an operator's ETag. Records + // written before the field existed load as 0, which is still a valid + // revision. + Revision int64 `json:"revision,omitempty"` +} + +// selfServiceProofTimes returns the per-repo proof map for repos: repos in +// proven are stamped at; any other repo keeps its entry in prev, or gets +// fallback (a record's OwnershipCheckedAt) when it has none. Repos not in +// repos are dropped. prev is never modified. +func selfServiceProofTimes(prev map[string]time.Time, repos []string, fallback, at time.Time, proven []string) map[string]time.Time { + stamp := make(map[string]struct{}, len(proven)) + for _, repo := range proven { + stamp[strings.ToLower(repo)] = struct{}{} + } + out := make(map[string]time.Time, len(repos)) + for _, repo := range repos { + key := strings.ToLower(repo) + if _, fresh := stamp[key]; fresh { + out[key] = at.UTC() + } else if t, ok := prev[key]; ok { + out[key] = t + } else if !fallback.IsZero() { + out[key] = fallback.UTC() + } + } + return out +} + +// selfServiceETag is the strong ETag for h. It covers the ID, the creation +// time (so a delete and re-register of the same ID never reuses an ETag) and +// the revision. It reveals nothing about the key. +func selfServiceETag(h SelfServiceHive) string { + sum := sha256.Sum256([]byte(fmt.Sprintf("%s|%d|%d", h.ID, h.CreatedAt.UnixNano(), h.Revision))) + return `"` + hex.EncodeToString(sum[:12]) + `"` +} + +// selfServiceETagMatches applies an If-Match value to h: empty (no +// precondition) and "*" always match; otherwise one listed ETag must equal +// h's current one. If-Match uses the strong comparison (RFC 9110 13.1.1), so +// a W/ tag never matches. +func selfServiceETagMatches(h SelfServiceHive, ifMatch string) bool { + ifMatch = strings.TrimSpace(ifMatch) + if ifMatch == "" || ifMatch == "*" { + return true + } + current := selfServiceETag(h) + for _, tag := range strings.Split(ifMatch, ",") { + if strings.TrimSpace(tag) == current { + return true + } + } + return false } // selfServiceStore holds every SelfServiceHive and persists them as one JSON @@ -261,6 +325,7 @@ func (st *selfServiceStore) registerCapped(h SelfServiceHive, maxPerOwner int) ( h.PrevKeyExpiresAt = time.Time{} h.CreatedAt = st.clock().UTC() h.LastAcceptedHeartbeatAt = time.Time{} + h.Revision = 1 st.hives[h.ID] = &h if err := st.saveLocked(); err != nil { delete(st.hives, h.ID) @@ -272,6 +337,12 @@ func (st *selfServiceStore) registerCapped(h SelfServiceHive, maxPerOwner int) ( // rotate mints a new key for id and returns it ONCE. The previous key keeps // authenticating until selfServiceKeyRotationOverlap has passed. func (st *selfServiceStore) rotate(id string) (string, error) { + return st.rotateIfMatch(id, "") +} + +// rotateIfMatch is rotate guarded by an ETag precondition (see update for +// the ifMatch rules); a mismatch returns errSelfServiceStale. +func (st *selfServiceStore) rotateIfMatch(id, ifMatch string) (string, error) { if st == nil { return "", errSelfServiceNilStore } @@ -285,10 +356,14 @@ func (st *selfServiceStore) rotate(id string) (string, error) { if !ok { return "", errSelfServiceNotFound } + if !selfServiceETagMatches(*h, ifMatch) { + return "", errSelfServiceStale + } before := *h h.PrevKeyHash = h.KeyHash h.PrevKeyExpiresAt = st.clock().UTC().Add(selfServiceKeyRotationOverlap) h.KeyHash = hash + h.Revision++ if err := st.saveLocked(); err != nil { *h = before return "", err @@ -296,8 +371,56 @@ func (st *selfServiceStore) rotate(id string) (string, error) { return raw, nil } +// update applies mutate to the record for id when ifMatch is empty, "*", or +// the record's current ETag, bumps its revision and persists it. The key +// hashes, owner, ID and timestamps other than what mutate sets are restored +// after mutate runs, so an edit can never touch the credential: the hive's +// key stays valid across every update. A mutate error or a failed write +// leaves the record as it was. Returns the stored copy. +func (st *selfServiceStore) update(id, ifMatch string, mutate func(*SelfServiceHive) error) (SelfServiceHive, error) { + if st == nil { + return SelfServiceHive{}, errSelfServiceNilStore + } + st.mu.Lock() + defer st.mu.Unlock() + h, ok := st.hives[id] + if !ok { + return SelfServiceHive{}, errSelfServiceNotFound + } + if !selfServiceETagMatches(*h, ifMatch) { + return SelfServiceHive{}, errSelfServiceStale + } + before := *h + next := *h + next.Repos = append([]string(nil), h.Repos...) + next.RepoProofs = make(map[string]time.Time, len(h.RepoProofs)) + for k, v := range h.RepoProofs { + next.RepoProofs[k] = v + } + if err := mutate(&next); err != nil { + return SelfServiceHive{}, err + } + // Fields an edit may never change. + next.ID, next.Owner, next.CreatedAt = before.ID, before.Owner, before.CreatedAt + next.KeyHash, next.PrevKeyHash, next.PrevKeyExpiresAt = before.KeyHash, before.PrevKeyHash, before.PrevKeyExpiresAt + next.LastAcceptedHeartbeatAt = before.LastAcceptedHeartbeatAt + next.Revision = before.Revision + 1 + *h = next + if err := st.saveLocked(); err != nil { + *h = before + return SelfServiceHive{}, err + } + return *h, nil +} + // revoke removes id. After it, no key authenticates the hive. func (st *selfServiceStore) revoke(id string) (bool, error) { + return st.revokeIfMatch(id, "") +} + +// revokeIfMatch is revoke guarded by an ETag precondition (see update for +// the ifMatch rules); a mismatch returns errSelfServiceStale. +func (st *selfServiceStore) revokeIfMatch(id, ifMatch string) (bool, error) { if st == nil { return false, errSelfServiceNilStore } @@ -307,6 +430,9 @@ func (st *selfServiceStore) revoke(id string) (bool, error) { if !ok { return false, nil } + if !selfServiceETagMatches(*h, ifMatch) { + return false, errSelfServiceStale + } delete(st.hives, id) if err := st.saveLocked(); err != nil { st.hives[id] = h diff --git a/src/pkg/hub/selfservice_v1_api.go b/src/pkg/hub/selfservice_v1_api.go index 1fb981cee8..5cc47627eb 100644 --- a/src/pkg/hub/selfservice_v1_api.go +++ b/src/pkg/hub/selfservice_v1_api.go @@ -2,22 +2,29 @@ package hub // Hub registration protocol v1, rollout step 1 (hivecommons/hive#18642). // -// /api/hub/v1 is a versioned view of the self-service (byo-) registry that -// selfservice_api.go already serves under /api/hub/selfservice. This file -// holds the discovery document; the resource routes land in later steps of the -// same rollout. The legacy routes keep their bodies and status codes. +// /api/hub/v1 is a versioned resource view of the self-service (byo-) +// registry that selfservice_api.go already serves under /api/hub/selfservice. +// It adds discovery (GET /api/hub/v1) and the registry as resources: create, +// list, get, delete and key rotation, with an ETag on every record and an +// optional If-Match on every write. In-place edits (PATCH) are a later PR of +// the same rollout. The legacy routes keep their bodies and status codes. // // Invariants (in addition to selfservice_api.go's): -// - Everything here answers 404 while HIVE_HUB_SELFSERVICE_REGISTRATION is -// off, so a hub that does not offer self-service does not advertise the -// protocol. -// - Nothing here writes to GitHub or to the registry. -// - Discovery lists exactly the operations this hub implements; a client -// never reads an operation that would answer 404. +// - Nothing here writes to GitHub. The only GitHub call is the read-only +// ownership check (GET /repos/{owner}/{repo}) at create, shared with the +// legacy route. +// - Every write carries an ETag precondition check inside the store lock, so +// two operators acting at once get 412, never a silent overwrite. import ( + "bytes" "encoding/json" + "errors" + "fmt" + "io" "net/http" + "strings" + "time" ) const ( @@ -31,7 +38,6 @@ const ( // hub checks permissions.admin on each repo with the GitHub token stored at // hub sign-in. The RFC's github-user-token (a per-request token with // explicit scopes) is rollout step 2 and is not advertised until it exists. - // Methods are added later, never renamed. hubV1ProofHubSessionToken = "hub-session-token" ) @@ -44,10 +50,14 @@ type hubV1Operation struct { IfMatch string `json:"if_match,omitempty"` } -// hubV1Operations is what this hub implements. Discovery lists exactly this, -// and the route registration below is checked against it by a test. +// hubV1Operations is what this hub implements. Discovery lists exactly this. var hubV1Operations = []hubV1Operation{ {Name: "discovery", Method: http.MethodGet, Path: hubV1BasePath}, + {Name: "create", Method: http.MethodPost, Path: hubV1BasePath + "/registrations"}, + {Name: "list", Method: http.MethodGet, Path: hubV1BasePath + "/registrations"}, + {Name: "get", Method: http.MethodGet, Path: hubV1BasePath + "/registrations/{id}"}, + {Name: "rotate_key", Method: http.MethodPost, Path: hubV1BasePath + "/registrations/{id}/keys", IfMatch: "optional"}, + {Name: "delete", Method: http.MethodDelete, Path: hubV1BasePath + "/registrations/{id}", IfMatch: "optional"}, } // hubV1PatchFields are the registration fields an update may change. Empty @@ -55,10 +65,60 @@ var hubV1Operations = []hubV1Operation{ var hubV1PatchFields = []string{} // registerHubV1Routes wires the v1 routes onto the hub mux. Discovery is -// public (a spoke reads it before it has a session) and answers 404 while +// public (a spoke reads it before it has a session); every other route is +// behind the hub session like the v6 routes. All of them answer 404 while // HIVE_HUB_SELFSERVICE_REGISTRATION is off. func (s *HubServer) registerHubV1Routes() { s.mux.HandleFunc("GET "+hubV1BasePath, s.handleHubV1Discovery) + s.mux.HandleFunc("POST "+hubV1BasePath+"/registrations", s.requireAuth(s.handleHubV1Create)) + s.mux.HandleFunc("GET "+hubV1BasePath+"/registrations", s.requireAuth(s.handleHubV1List)) + s.mux.HandleFunc("GET "+hubV1BasePath+"/registrations/{id}", s.requireAuth(s.handleHubV1Get)) + s.mux.HandleFunc("DELETE "+hubV1BasePath+"/registrations/{id}", s.requireAuth(s.handleHubV1Delete)) + s.mux.HandleFunc("POST "+hubV1BasePath+"/registrations/{id}/keys", s.requireAuth(s.handleHubV1RotateKey)) +} + +// hubV1RegistrationView is a v6 view plus the record's ETag, so a list +// response carries a usable precondition for every item, and the per-repo +// proof times. +type hubV1RegistrationView struct { + selfServiceHiveView + ETag string `json:"etag"` + RepoProofs []hubV1RepoProof `json:"repo_proofs"` +} + +// hubV1RepoProof is when the owner's admin permission on one repo was last +// proven. +type hubV1RepoProof struct { + Repo string `json:"repo"` + CheckedAt time.Time `json:"checked_at"` +} + +func (s *HubServer) hubV1View(h SelfServiceHive) hubV1RegistrationView { + proofs := make([]hubV1RepoProof, 0, len(h.Repos)) + for _, repo := range h.Repos { + at, ok := h.RepoProofs[strings.ToLower(repo)] + if !ok { + // A record from before per-repo proofs: every repo was proven + // when the record was. + at = h.OwnershipCheckedAt + } + proofs = append(proofs, hubV1RepoProof{Repo: repo, CheckedAt: at}) + } + return hubV1RegistrationView{ + selfServiceHiveView: selfServiceView(h, s.selfServiceOnline(h.ID)), + ETag: selfServiceETag(h), + RepoProofs: proofs, + } +} + +// writeHubV1RepoCapError is the v1 over-cap answer for create (and, later, PATCH): 413 +// naming the limit, so a client can read it without parsing the message. +func writeHubV1RepoCapError(w http.ResponseWriter, err error, maxRepos int) { + writeHubV1JSON(w, http.StatusRequestEntityTooLarge, "", hubV1RepoCapBody(err, maxRepos)) +} + +func hubV1RepoCapBody(err error, maxRepos int) map[string]any { + return map[string]any{"error": err.Error(), "limit": "max_repos", "max_repos": maxRepos} } func writeHubV1JSON(w http.ResponseWriter, code int, etag string, body any) { @@ -90,3 +150,152 @@ func (s *HubServer) handleHubV1Discovery(w http.ResponseWriter, r *http.Request) "proof_methods": []string{hubV1ProofHubSessionToken}, }) } + +// handleHubV1Create is POST /api/hub/v1/registrations: the v6 register logic +// with a resource-shaped 201 (Location and ETag headers). +func (s *HubServer) handleHubV1Create(w http.ResponseWriter, r *http.Request) { + if !s.requireSelfServiceEnabled(w) { + return + } + stored, rawKey, ok := s.selfServiceRegisterCore(w, r, true) + if !ok { + return + } + hubURL := hubPublicURL() + w.Header().Set("Location", hubV1BasePath+"/registrations/"+stored.ID) + writeHubV1JSON(w, http.StatusCreated, selfServiceETag(stored), map[string]any{ + "registration": s.hubV1View(stored), + "key": rawKey, + "hub_url": hubURL, + "snippets": selfServiceSnippets(hubURL, stored.ID, rawKey), + }) +} + +// handleHubV1List is GET /api/hub/v1/registrations: the caller's own records. +func (s *HubServer) handleHubV1List(w http.ResponseWriter, r *http.Request) { + if !s.requireSelfServiceEnabled(w) { + return + } + login := s.getAuthUser(r) + views := make([]hubV1RegistrationView, 0) + for _, h := range s.selfService.list() { + if selfServiceOwns(h, login) { + views = append(views, s.hubV1View(h)) + } + } + writeHubV1JSON(w, http.StatusOK, "", map[string]any{"registrations": views}) +} + +// handleHubV1Get is GET /api/hub/v1/registrations/{id}. If-None-Match with the +// current ETag answers 304. +func (s *HubServer) handleHubV1Get(w http.ResponseWriter, r *http.Request) { + if !s.requireSelfServiceEnabled(w) { + return + } + h, ok := s.selfServiceOwnedHive(w, r, s.getAuthUser(r), false) + if !ok { + return + } + etag := selfServiceETag(h) + if inm := strings.TrimSpace(r.Header.Get("If-None-Match")); inm != "" && selfServiceETagMatches(h, strings.ReplaceAll(inm, "W/", "")) { + w.Header().Set("ETag", etag) + w.WriteHeader(http.StatusNotModified) + return + } + writeHubV1JSON(w, http.StatusOK, etag, map[string]any{"registration": s.hubV1View(h)}) +} + +func decodeStrict(data []byte, v any) error { + dec := json.NewDecoder(bytes.NewReader(data)) + dec.DisallowUnknownFields() + if err := dec.Decode(v); err != nil { + return err + } + if dec.More() { + return errors.New("trailing data after JSON value") + } + return nil +} + +// handleHubV1RotateKey is POST /api/hub/v1/registrations/{id}/keys: the v6 +// rotation (the previous key keeps working for selfServiceKeyRotationOverlap) +// with an optional If-Match. A caller-chosen grace window is rollout step 3, +// so a body asking for one is refused rather than silently ignored. +func (s *HubServer) handleHubV1RotateKey(w http.ResponseWriter, r *http.Request) { + if !s.requireSelfServiceEnabled(w) { + return + } + login := s.getAuthUser(r) + h, ok := s.selfServiceOwnedHive(w, r, login, false) + if !ok { + return + } + data, err := io.ReadAll(io.LimitReader(r.Body, selfServiceMaxRequestBody)) + if err != nil { + writeJSONError(w, http.StatusBadRequest, "could not read the request body") + return + } + if len(bytes.TrimSpace(data)) > 0 { + var body struct{} + if err := decodeStrict(data, &body); err != nil { + writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("this hub takes no rotation options; the previous key stays valid for %s", selfServiceKeyRotationOverlap)) + return + } + } + rawKey, err := s.selfService.rotateIfMatch(h.ID, r.Header.Get("If-Match")) + if err != nil { + switch { + case errors.Is(err, errSelfServiceNotFound): + writeJSONError(w, http.StatusNotFound, "self-hosted hive not found") + case errors.Is(err, errSelfServiceStale): + current, _ := s.selfService.get(h.ID) + writeHubV1JSON(w, http.StatusPreconditionFailed, selfServiceETag(current), map[string]any{ + "error": "the registration changed since you read it; GET it again and retry", + }) + default: + s.logger.Error("self-service rotation: store write failed", "user", login, "hive", h.ID, "error", err) + writeJSONError(w, http.StatusInternalServerError, "could not persist the new key") + } + return + } + s.logger.Info("audit: selfservice_hive_rotated", "user", login, "hive", h.ID, "overlap", selfServiceKeyRotationOverlap.String()) + stored, _ := s.selfService.get(h.ID) + hubURL := hubPublicURL() + writeHubV1JSON(w, http.StatusCreated, selfServiceETag(stored), map[string]any{ + "id": h.ID, + "key": rawKey, + "hub_url": hubURL, + "overlap": selfServiceKeyRotationOverlap.String(), + "snippets": selfServiceSnippets(hubURL, h.ID, rawKey), + }) +} + +// handleHubV1Delete is DELETE /api/hub/v1/registrations/{id}: the owner or a +// hub admin revokes the key and removes the registry entry, with an optional +// If-Match. +func (s *HubServer) handleHubV1Delete(w http.ResponseWriter, r *http.Request) { + if !s.requireSelfServiceEnabled(w) { + return + } + login := s.getAuthUser(r) + h, ok := s.selfServiceOwnedHive(w, r, login, true) + if !ok { + return + } + removed, err := s.selfService.revokeIfMatch(h.ID, r.Header.Get("If-Match")) + if err != nil { + if errors.Is(err, errSelfServiceStale) { + current, _ := s.selfService.get(h.ID) + writeHubV1JSON(w, http.StatusPreconditionFailed, selfServiceETag(current), map[string]any{ + "error": "the registration changed since you read it; GET it again and retry", + }) + return + } + s.logger.Error("self-service revocation: store write failed", "user", login, "hive", h.ID, "error", err) + writeJSONError(w, http.StatusInternalServerError, "could not persist the revocation") + return + } + s.removeRegistryEntry(h.ID, login) + s.logger.Info("audit: selfservice_hive_revoked", "user", login, "hive", h.ID, "owner", h.Owner, "by_admin", !selfServiceOwns(h, login)) + writeHubV1JSON(w, http.StatusOK, "", map[string]any{"removed": removed, "id": h.ID}) +} diff --git a/src/pkg/hub/selfservice_v1_api_test.go b/src/pkg/hub/selfservice_v1_api_test.go index 98e82ae7bc..18381f8679 100644 --- a/src/pkg/hub/selfservice_v1_api_test.go +++ b/src/pkg/hub/selfservice_v1_api_test.go @@ -7,20 +7,87 @@ import ( "net/http" "net/http/httptest" "reflect" + "sort" "strings" "testing" + "time" ) // Tests for the hub registration protocol v1 discovery and the configurable // repo cap, rollout step 1 (#18642). They reuse the legacy self-service test // env (selfservice_api_test.go): stubbed GitHub, temp stores, the flag on. +const hubV1TestID = "byo-alice--lab" + func newHubV1TestEnv(t *testing.T) *selfServiceTestEnv { t.Helper() t.Setenv(selfServiceMaxReposEnv, "") return newSelfServiceTestEnv(t) } +// v1 sends a request through requireAuth to a v1 handler, filling the {id} +// path value the mux would set in production. +func (env *selfServiceTestEnv) v1(t *testing.T, user, method, path, body string, headers map[string]string, handler http.HandlerFunc) *httptest.ResponseRecorder { + t.Helper() + req := httptest.NewRequest(method, path, strings.NewReader(body)) + req.Header.Set("Origin", selfServiceTestOrigin) + for k, v := range headers { + req.Header.Set(k, v) + } + if user != "" { + req.AddCookie(&http.Cookie{ + Name: "hive_hub_user", + Value: mintHubUserCookieValueV2(deriveDomainKey(grantableTestSecret, infoSessionEd25519Seed), user), + }) + } + if rest, ok := strings.CutPrefix(path, hubV1BasePath+"/registrations/"); ok { + id, _, _ := strings.Cut(rest, "/") + req.SetPathValue("id", id) + } + rec := httptest.NewRecorder() + env.s.requireAuth(handler)(rec, req) + return rec +} + +func (env *selfServiceTestEnv) v1Get(t *testing.T, user, id string) *httptest.ResponseRecorder { + t.Helper() + return env.v1(t, user, http.MethodGet, hubV1BasePath+"/registrations/"+id, "", nil, env.s.handleHubV1Get) +} + +// etagOf reads the current ETag through GET, as a client would. +func (env *selfServiceTestEnv) etagOf(t *testing.T, id string) string { + t.Helper() + rec := env.v1Get(t, selfServiceTestUser, id) + if rec.Code != http.StatusOK { + t.Fatalf("GET %s: %d %s", id, rec.Code, rec.Body.String()) + } + etag := rec.Header().Get("ETag") + if etag == "" { + t.Fatal("GET returned no ETag") + } + return etag +} + +func decodeV1Registration(t *testing.T, rec *httptest.ResponseRecorder) hubV1RegistrationView { + t.Helper() + var resp struct { + Registration hubV1RegistrationView `json:"registration"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { + t.Fatalf("decode registration: %v (%s)", err, rec.Body.String()) + } + return resp.Registration +} + +func sortedLower(in []string) []string { + out := make([]string, len(in)) + for i, s := range in { + out[i] = strings.ToLower(s) + } + sort.Strings(out) + return out +} + func TestHubV1Discovery(t *testing.T) { cases := []struct { name string @@ -77,9 +144,27 @@ func TestHubV1Discovery(t *testing.T) { if !reflect.DeepEqual(d.ProofMethods, []string{"hub-session-token"}) { t.Fatalf("proof_methods = %v", d.ProofMethods) } - if len(d.Operations) != 1 || d.Operations[0].Name != "discovery" || d.Operations[0].Method != http.MethodGet || d.Operations[0].Path != "/api/hub/v1" { + got := map[string]hubV1Operation{} + for _, op := range d.Operations { + got[op.Name] = op + } + want := map[string]string{ + "discovery": "GET /api/hub/v1", + "create": "POST /api/hub/v1/registrations", + "list": "GET /api/hub/v1/registrations", + "get": "GET /api/hub/v1/registrations/{id}", + "rotate_key": "POST /api/hub/v1/registrations/{id}/keys", + "delete": "DELETE /api/hub/v1/registrations/{id}", + } + if len(got) != len(want) { t.Fatalf("operations = %+v", d.Operations) } + for name, route := range want { + op, ok := got[name] + if !ok || op.Method+" "+op.Path != route { + t.Errorf("operation %s = %+v, want %s", name, op, route) + } + } if len(d.PatchFields) != 0 { t.Fatalf("patch_fields = %v, want none until update exists", d.PatchFields) } @@ -211,3 +296,430 @@ func TestSelfServiceRegisterRepoCapIsConfigurable(t *testing.T) { }) } } + +func TestHubV1CreateGetList(t *testing.T) { + env := newHubV1TestEnv(t) + rec := env.v1(t, selfServiceTestUser, http.MethodPost, hubV1BasePath+"/registrations", selfServiceHappyBody, nil, env.s.handleHubV1Create) + if rec.Code != http.StatusCreated { + t.Fatalf("create: %d %s", rec.Code, rec.Body.String()) + } + if loc := rec.Header().Get("Location"); loc != hubV1BasePath+"/registrations/"+hubV1TestID { + t.Fatalf("Location = %q", loc) + } + etag := rec.Header().Get("ETag") + var created struct { + Registration hubV1RegistrationView `json:"registration"` + Key string `json:"key"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &created); err != nil { + t.Fatal(err) + } + if etag == "" || created.Registration.ETag != etag || created.Key == "" { + t.Fatalf("create: etag header %q, body etag %q, key set %v", etag, created.Registration.ETag, created.Key != "") + } + if _, ok := env.s.verifyHeartbeatBearerGeneration(created.Key, hubV1TestID); !ok { + t.Fatal("v1-created key does not authenticate") + } + + if got := env.etagOf(t, hubV1TestID); got != etag { + t.Fatalf("GET ETag %q != create ETag %q", got, etag) + } + notModified := env.v1(t, selfServiceTestUser, http.MethodGet, hubV1BasePath+"/registrations/"+hubV1TestID, "", map[string]string{"If-None-Match": etag}, env.s.handleHubV1Get) + if notModified.Code != http.StatusNotModified { + t.Fatalf("If-None-Match: %d", notModified.Code) + } + + list := env.v1(t, selfServiceTestUser, http.MethodGet, hubV1BasePath+"/registrations", "", nil, env.s.handleHubV1List) + var listed struct { + Registrations []hubV1RegistrationView `json:"registrations"` + } + if err := json.Unmarshal(list.Body.Bytes(), &listed); err != nil { + t.Fatal(err) + } + if len(listed.Registrations) != 1 || listed.Registrations[0].ETag != etag { + t.Fatalf("list = %s", list.Body.String()) + } + if strings.Contains(list.Body.String(), created.Key) || strings.Contains(list.Body.String(), "key_hash") { + t.Fatal("list leaked key material") + } + other := env.v1(t, selfServiceTestOther, http.MethodGet, hubV1BasePath+"/registrations", "", nil, env.s.handleHubV1List) + if strings.Contains(other.Body.String(), hubV1TestID) { + t.Fatal("another user's list shows alice's registration") + } +} + +func TestHubV1RotateAndDelete(t *testing.T) { + cases := []struct { + name string + method string + suffix string + user string + ifMatch string // "current", "stale" or "" + body string + wantStatus int + }{ + {name: "rotate without precondition", method: http.MethodPost, suffix: "/keys", user: selfServiceTestUser, wantStatus: http.StatusCreated}, + {name: "rotate with current etag", method: http.MethodPost, suffix: "/keys", user: selfServiceTestUser, ifMatch: "current", wantStatus: http.StatusCreated}, + {name: "rotate with stale etag", method: http.MethodPost, suffix: "/keys", user: selfServiceTestUser, ifMatch: "stale", wantStatus: http.StatusPreconditionFailed}, + {name: "rotate grace is not offered yet", method: http.MethodPost, suffix: "/keys", user: selfServiceTestUser, body: `{"grace":"1h"}`, wantStatus: http.StatusBadRequest}, + {name: "rotate someone else's", method: http.MethodPost, suffix: "/keys", user: selfServiceTestOther, wantStatus: http.StatusNotFound}, + {name: "delete with stale etag", method: http.MethodDelete, user: selfServiceTestUser, ifMatch: "stale", wantStatus: http.StatusPreconditionFailed}, + {name: "delete with current etag", method: http.MethodDelete, user: selfServiceTestUser, ifMatch: "current", wantStatus: http.StatusOK}, + {name: "delete by hub admin", method: http.MethodDelete, user: selfServiceTestAdmin, wantStatus: http.StatusOK}, + {name: "delete someone else's", method: http.MethodDelete, user: selfServiceTestOther, wantStatus: http.StatusNotFound}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + env := newHubV1TestEnv(t) + id, key := env.registerHappy(t) + ifMatch := "" + switch tc.ifMatch { + case "current": + ifMatch = env.etagOf(t, id) + case "stale": + ifMatch = env.etagOf(t, id) + // Another operator changes the record first. + if _, err := env.s.selfService.update(id, ifMatch, func(*SelfServiceHive) error { return nil }); err != nil { + t.Fatalf("setup update: %v", err) + } + } + headers := map[string]string{} + if ifMatch != "" { + headers["If-Match"] = ifMatch + } + handler := env.s.handleHubV1Delete + if tc.suffix == "/keys" { + handler = env.s.handleHubV1RotateKey + } + rec := env.v1(t, tc.user, tc.method, hubV1BasePath+"/registrations/"+id+tc.suffix, tc.body, headers, handler) + if rec.Code != tc.wantStatus { + t.Fatalf("status = %d, want %d (%s)", rec.Code, tc.wantStatus, rec.Body.String()) + } + _, stillThere := env.s.selfService.get(id) + _, keyOK := env.s.verifyHeartbeatBearerGeneration(key, id) + switch { + case tc.method == http.MethodDelete && tc.wantStatus == http.StatusOK: + if stillThere || keyOK { + t.Fatal("delete left the record or the key alive") + } + case tc.suffix == "/keys" && tc.wantStatus == http.StatusCreated: + var resp struct { + Key string `json:"key"` + } + _ = json.Unmarshal(rec.Body.Bytes(), &resp) + if _, ok := env.s.verifyHeartbeatBearerGeneration(resp.Key, id); !ok || resp.Key == key { + t.Fatal("rotation did not mint a working new key") + } + if !keyOK { + t.Fatal("old key refused inside the rotation overlap") + } + if stored, _ := env.s.selfService.get(id); rec.Header().Get("ETag") != selfServiceETag(stored) { + t.Fatal("rotation response ETag is not the record's") + } + default: + if !stillThere || !keyOK { + t.Fatal("refused request removed the record or broke the key") + } + } + }) + } +} + +// A v1 create over the repo cap answers 413 naming the limit; the legacy +// create keeps its 400 (pinned in TestSelfServiceRegisterRepoCapIsConfigurable). +func TestHubV1CreateRepoCap(t *testing.T) { + repos := func(n int) string { + out := make([]string, n) + for i := range out { + out[i] = fmt.Sprintf("alice/bulk-%02d", i) + } + b, _ := json.Marshal(out) + return string(b) + } + cases := []struct { + name string + env string + n int + wantStatus int + }{ + {name: "at the default cap", env: "", n: 20, wantStatus: http.StatusCreated}, + {name: "over the default cap", env: "", n: 21, wantStatus: http.StatusRequestEntityTooLarge}, + {name: "raised cap accepts 21", env: "25", n: 21, wantStatus: http.StatusCreated}, + {name: "raised cap refuses past it", env: "25", n: 26, wantStatus: http.StatusRequestEntityTooLarge}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + env := newHubV1TestEnv(t) + t.Setenv(selfServiceMaxReposEnv, tc.env) + body := fmt.Sprintf(`{"name":"big","repos":%s}`, repos(tc.n)) + rec := env.v1(t, selfServiceTestUser, http.MethodPost, hubV1BasePath+"/registrations", body, nil, env.s.handleHubV1Create) + if rec.Code != tc.wantStatus { + t.Fatalf("status = %d, want %d (%s)", rec.Code, tc.wantStatus, rec.Body.String()) + } + if tc.wantStatus == http.StatusCreated { + return + } + var resp struct { + Error string `json:"error"` + Limit string `json:"limit"` + MaxRepos int `json:"max_repos"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { + t.Fatal(err) + } + if resp.Limit != "max_repos" || resp.MaxRepos != selfServiceMaxRepos() || resp.Error == "" { + t.Fatalf("413 body = %+v", resp) + } + if len(env.s.selfService.list()) != 0 { + t.Fatal("refused create stored a record") + } + }) + } +} + +// A v1 create refused for any other reason keeps the legacy status codes. +func TestHubV1CreateRefusalsMatchLegacy(t *testing.T) { + cases := []struct { + name string + user string + body string + want int + }{ + {name: "not signed in", user: "", body: selfServiceHappyBody, want: http.StatusUnauthorized}, + {name: "not admin of a repo", user: selfServiceTestUser, body: `{"name":"Lab","repos":["alice/readonly"]}`, want: http.StatusForbidden}, + {name: "no repos", user: selfServiceTestUser, body: `{"name":"Lab","repos":[]}`, want: http.StatusBadRequest}, + {name: "bad dashboard url", user: selfServiceTestUser, body: `{"name":"Lab","repos":["alice/lab"],"dashboard_url":"http://lab.example.org/"}`, want: http.StatusBadRequest}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + env := newHubV1TestEnv(t) + legacy := env.register(t, tc.user, tc.body) + v1 := env.v1(t, tc.user, http.MethodPost, hubV1BasePath+"/registrations", tc.body, nil, env.s.handleHubV1Create) + if legacy.Code != tc.want || v1.Code != tc.want { + t.Fatalf("legacy %d, v1 %d, want %d (%s | %s)", legacy.Code, v1.Code, tc.want, legacy.Body.String(), v1.Body.String()) + } + if len(env.s.selfService.list()) != 0 { + t.Fatal("a refused create stored a record") + } + }) + } +} + +func TestHubV1FlagOffHidesEveryRoute(t *testing.T) { + env := newHubV1TestEnv(t) + id, _ := env.registerHappy(t) + t.Setenv(selfServiceRegistrationEnv, "") + path := hubV1BasePath + "/registrations" + cases := []struct { + name string + method string + path string + handler http.HandlerFunc + }{ + {"create", http.MethodPost, path, env.s.handleHubV1Create}, + {"list", http.MethodGet, path, env.s.handleHubV1List}, + {"get", http.MethodGet, path + "/" + id, env.s.handleHubV1Get}, + {"rotate", http.MethodPost, path + "/" + id + "/keys", env.s.handleHubV1RotateKey}, + {"delete", http.MethodDelete, path + "/" + id, env.s.handleHubV1Delete}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + rec := env.v1(t, selfServiceTestUser, tc.method, tc.path, selfServiceHappyBody, nil, tc.handler) + if rec.Code != http.StatusNotFound { + t.Fatalf("%s with the flag off: %d, want 404 (%s)", tc.name, rec.Code, rec.Body.String()) + } + }) + } + if _, ok := env.s.selfService.get(id); !ok { + t.Fatal("a flag-off request changed the registry") + } +} + +func TestHubV1RoutesRequireSession(t *testing.T) { + env := newHubV1TestEnv(t) + id, _ := env.registerHappy(t) + path := hubV1BasePath + "/registrations" + cases := []struct { + name string + method string + path string + handler http.HandlerFunc + }{ + {"create", http.MethodPost, path, env.s.handleHubV1Create}, + {"list", http.MethodGet, path, env.s.handleHubV1List}, + {"get", http.MethodGet, path + "/" + id, env.s.handleHubV1Get}, + {"rotate", http.MethodPost, path + "/" + id + "/keys", env.s.handleHubV1RotateKey}, + {"delete", http.MethodDelete, path + "/" + id, env.s.handleHubV1Delete}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + rec := env.v1(t, "", tc.method, tc.path, "", nil, tc.handler) + if rec.Code != http.StatusUnauthorized { + t.Fatalf("%s without a session: %d, want 401", tc.name, rec.Code) + } + }) + } +} + +// TestHubV1LegacyRoutesUnchanged pins the legacy /api/hub/selfservice/* +// responses so the v1 work cannot drift them: exact top-level keys, no ETag +// header, none of the v1-only fields, and the view bytes equal to what the +// pre-v1 encoder produced for the same record. +func TestHubV1LegacyRoutesUnchanged(t *testing.T) { + topKeys := func(t *testing.T, rec *httptest.ResponseRecorder) []string { + t.Helper() + var m map[string]json.RawMessage + if err := json.Unmarshal(rec.Body.Bytes(), &m); err != nil { + t.Fatalf("body is not a JSON object: %s", rec.Body.String()) + } + keys := make([]string, 0, len(m)) + for k := range m { + keys = append(keys, k) + } + sort.Strings(keys) + return keys + } + encode := func(v any) string { + var b strings.Builder + if err := json.NewEncoder(&b).Encode(v); err != nil { + t.Fatal(err) + } + return b.String() + } + env := newHubV1TestEnv(t) + + reg := env.register(t, selfServiceTestUser, selfServiceHappyBody) + list := env.list(t, selfServiceTestUser) + status := env.status(t, selfServiceTestUser) + cases := []struct { + name string + rec *httptest.ResponseRecorder + wantCode int + wantKeys []string + }{ + {name: "register", rec: reg, wantCode: http.StatusCreated, wantKeys: []string{"hive", "hub_url", "key", "snippets"}}, + {name: "list", rec: list, wantCode: http.StatusOK, wantKeys: []string{"hives"}}, + {name: "status", rec: status, wantCode: http.StatusOK, wantKeys: []string{"count", "enabled", "expiry", "max_per_user"}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if tc.rec.Code != tc.wantCode { + t.Fatalf("status = %d, want %d (%s)", tc.rec.Code, tc.wantCode, tc.rec.Body.String()) + } + if got := topKeys(t, tc.rec); !reflect.DeepEqual(got, tc.wantKeys) { + t.Fatalf("keys = %v, want %v", got, tc.wantKeys) + } + if tc.rec.Header().Get("ETag") != "" { + t.Fatal("legacy route started sending an ETag") + } + for _, leaked := range []string{`"etag"`, `"revision"`, `"key_hash"`, `"repo_proofs"`} { + if strings.Contains(tc.rec.Body.String(), leaked) { + t.Fatalf("legacy body gained %s: %s", leaked, tc.rec.Body.String()) + } + } + }) + } + + // Byte-for-byte: the legacy view of the stored record is exactly the + // pre-v1 selfServiceHiveView encoding, in the register and list bodies. + stored, ok := env.s.selfService.get(hubV1TestID) + if !ok { + t.Fatal("registered hive missing") + } + wantView := encode(selfServiceView(stored, false)) + var created struct { + Hive json.RawMessage `json:"hive"` + } + if err := json.Unmarshal(reg.Body.Bytes(), &created); err != nil { + t.Fatal(err) + } + if got := string(created.Hive) + "\n"; got != wantView { + t.Fatalf("legacy register view changed:\n got %s want %s", got, wantView) + } + wantList := encode(map[string]any{"hives": []selfServiceHiveView{selfServiceView(stored, false)}}) + if list.Body.String() != wantList { + t.Fatalf("legacy list body changed:\n got %s want %s", list.Body.String(), wantList) + } + // The view struct's wire fields are the pre-v1 set and no others. + viewKeys := map[string]struct{}{} + var m map[string]json.RawMessage + if err := json.Unmarshal(created.Hive, &m); err != nil { + t.Fatal(err) + } + for k := range m { + viewKeys[k] = struct{}{} + } + for _, k := range []string{"id", "owner", "org", "repos", "dashboard_url", "created_at", "ownership_checked_at", "online"} { + if _, ok := viewKeys[k]; !ok { + t.Errorf("legacy view lost %q", k) + continue + } + delete(viewKeys, k) + } + delete(viewKeys, "last_accepted_heartbeat_at") + if len(viewKeys) != 0 { + t.Fatalf("legacy view gained fields: %v", viewKeys) + } + + // The legacy rotate (200) and delete still answer as before, and see a + // record that v1 has also touched. + if rec := env.v1(t, selfServiceTestUser, http.MethodPost, hubV1BasePath+"/registrations/"+hubV1TestID+"/keys", "", nil, env.s.handleHubV1RotateKey); rec.Code != http.StatusCreated { + t.Fatalf("v1 rotate: %d %s", rec.Code, rec.Body.String()) + } + rot := env.rotate(t, selfServiceTestUser, hubV1TestID) + if rot.Code != http.StatusOK || !reflect.DeepEqual(topKeys(t, rot), []string{"hub_url", "id", "key", "overlap", "snippets"}) { + t.Fatalf("legacy rotate: %d %s", rot.Code, rot.Body.String()) + } + del := env.remove(t, selfServiceTestUser, hubV1TestID) + if del.Code != http.StatusOK || del.Body.String() != encode(map[string]any{"id": hubV1TestID, "removed": true}) { + t.Fatalf("legacy delete: %d %s", del.Code, del.Body.String()) + } +} + +// TestHubV1RepoProofs checks the per-repo proof times: create stamps every +// repo with the same instant as OwnershipCheckedAt, and a record from before +// the field reports OwnershipCheckedAt for every repo. +func TestHubV1RepoProofs(t *testing.T) { + proofsOf := func(t *testing.T, env *selfServiceTestEnv) map[string]time.Time { + t.Helper() + rec := env.v1Get(t, selfServiceTestUser, hubV1TestID) + out := map[string]time.Time{} + for _, p := range decodeV1Registration(t, rec).RepoProofs { + out[p.Repo] = p.CheckedAt + } + return out + } + for _, legacy := range []bool{false, true} { + name := "create stamps every repo" + if legacy { + name = "legacy record reads OwnershipCheckedAt" + } + t.Run(name, func(t *testing.T) { + env := newHubV1TestEnv(t) + id, _ := env.registerHappy(t) + created, _ := env.s.selfService.get(id) + if len(created.RepoProofs) != 2 { + t.Fatalf("create stored proofs %v", created.RepoProofs) + } + for repo, at := range created.RepoProofs { + if !at.Equal(created.OwnershipCheckedAt) { + t.Fatalf("create proof for %s = %v, want %v", repo, at, created.OwnershipCheckedAt) + } + } + if legacy { + if _, err := env.s.selfService.update(id, "", func(h *SelfServiceHive) error { h.RepoProofs = nil; return nil }); err != nil { + t.Fatal(err) + } + } + got := proofsOf(t, env) + if len(got) != 2 { + t.Fatalf("repo_proofs = %v", got) + } + for _, repo := range []string{"alice/lab", "alice/tools"} { + if !got[repo].Equal(created.OwnershipCheckedAt) || got[repo].IsZero() { + t.Errorf("%s proof = %v, want %v", repo, got[repo], created.OwnershipCheckedAt) + } + } + }) + } +} diff --git a/src/pkg/hub/selfservice_v1_registry_test.go b/src/pkg/hub/selfservice_v1_registry_test.go new file mode 100644 index 0000000000..3e95e6ceeb --- /dev/null +++ b/src/pkg/hub/selfservice_v1_registry_test.go @@ -0,0 +1,317 @@ +package hub + +import ( + "encoding/json" + "errors" + "os" + "strings" + "testing" + "time" +) + +// Store-level tests for the v1 registration protocol (#18642): per-repo proof +// times, the revision counter behind the ETag, and the in-place update. + +func registerV1TestHive(t *testing.T, st *selfServiceStore) (SelfServiceHive, string) { + t.Helper() + raw, err := st.register(SelfServiceHive{ + ID: "byo-alice--lab", Owner: "alice", Org: "alice", + Repos: []string{"alice/lab", "alice/tools"}, DashboardURL: "https://lab.example.org/", + OwnershipCheckedAt: time.Date(2026, 11, 13, 11, 0, 0, 0, time.UTC), + }) + if err != nil { + t.Fatal(err) + } + h, ok := st.get("byo-alice--lab") + if !ok { + t.Fatal("registered hive missing") + } + return h, raw +} + +func TestSelfServiceRevisionStartsAtOneAndBumpsOnOwnerVisibleChanges(t *testing.T) { + st, _ := newSelfServiceTestStore(t) + h, _ := registerV1TestHive(t, st) + if h.Revision != 1 { + t.Fatalf("revision at create = %d, want 1", h.Revision) + } + if _, err := st.rotate(h.ID); err != nil { + t.Fatal(err) + } + rotated, _ := st.get(h.ID) + if rotated.Revision != 2 { + t.Fatalf("revision after rotate = %d, want 2", rotated.Revision) + } + updated, err := st.update(h.ID, "", func(r *SelfServiceHive) error { r.DashboardURL = ""; return nil }) + if err != nil { + t.Fatal(err) + } + if updated.Revision != 3 { + t.Fatalf("revision after update = %d, want 3", updated.Revision) + } +} + +// A live spoke heartbeats constantly. If that moved the ETag, an operator's +// If-Match could never survive, and the ETag would leak liveness. +func TestSelfServiceHeartbeatDoesNotBumpRevisionOrETag(t *testing.T) { + st, now := newSelfServiceTestStore(t) + h, _ := registerV1TestHive(t, st) + etag := selfServiceETag(h) + + for i := 0; i < 3; i++ { + *now = now.Add(selfServiceHeartbeatStampInterval + time.Second) + if err := st.noteAcceptedHeartbeat(h.ID, *now); err != nil { + t.Fatal(err) + } + } + after, _ := st.get(h.ID) + if after.LastAcceptedHeartbeatAt.IsZero() { + t.Fatal("heartbeat stamp was not recorded") + } + if after.Revision != h.Revision || selfServiceETag(after) != etag { + t.Fatalf("heartbeats moved the revision to %d (ETag %s -> %s)", after.Revision, etag, selfServiceETag(after)) + } + if !selfServiceETagMatches(after, etag) { + t.Fatal("an ETag read before the heartbeats no longer matches") + } +} + +// Records written by a binary from before the field have no revision. They +// must load as 0 with a stable, valid ETag, and the first change moves it. +func TestSelfServiceRevisionFallbackForOldRecords(t *testing.T) { + st, _ := newSelfServiceTestStore(t) + h, _ := registerV1TestHive(t, st) + + // An old binary's file: no revision and no repo_proofs. + data, err := json.Marshal(map[string]any{"hives": []map[string]any{{ + "id": h.ID, "owner": h.Owner, "repos": h.Repos, "key_hash": h.KeyHash, + "created_at": h.CreatedAt, "ownership_checked_at": h.OwnershipCheckedAt, + }}}) + if err != nil { + t.Fatal(err) + } + if strings.Contains(string(data), "revision") { + t.Fatal("fixture carries a revision") + } + if err := os.WriteFile(st.path, data, 0o600); err != nil { + t.Fatal(err) + } + reloaded := newSelfServiceStore(st.path) + if err := reloaded.load(); err != nil { + t.Fatal(err) + } + old, ok := reloaded.get(h.ID) + if !ok { + t.Fatal("old record lost") + } + if old.Revision != 0 || len(old.RepoProofs) != 0 { + t.Fatalf("old record loaded as revision %d proofs %v", old.Revision, old.RepoProofs) + } + etag := selfServiceETag(old) + if etag == "" || !selfServiceETagMatches(old, etag) { + t.Fatalf("revision-0 ETag %q does not match its record", etag) + } + next, err := reloaded.update(h.ID, etag, func(r *SelfServiceHive) error { r.DashboardURL = "https://x.example.org/"; return nil }) + if err != nil { + t.Fatal(err) + } + if next.Revision != 1 || selfServiceETag(next) == etag { + t.Fatalf("first change after revision 0: revision %d, ETag unchanged %v", next.Revision, selfServiceETag(next) == etag) + } +} + +// The new fields are additive: a binary that predates them decodes the file +// and ignores them (it has no DisallowUnknownFields on the registry file). +func TestSelfServiceNewFieldsAreAdditiveJSON(t *testing.T) { + st, _ := newSelfServiceTestStore(t) + h, _ := registerV1TestHive(t, st) + data, err := json.Marshal(h) + if err != nil { + t.Fatal(err) + } + var old struct { + ID string `json:"id"` + Owner string `json:"owner"` + Repos []string `json:"repos"` + } + if err := json.Unmarshal(data, &old); err != nil { + t.Fatalf("a reader of the old shape cannot decode the record: %v", err) + } + if old.ID != h.ID || len(old.Repos) != 2 { + t.Fatalf("old-shape decode = %+v", old) + } + for _, field := range []string{`"revision"`, `"repo_proofs"`} { + if !strings.Contains(string(data), field) { + t.Errorf("record JSON lacks %s", field) + } + } +} + +func TestSelfServiceUpdatePreservesCredentialAndIdentity(t *testing.T) { + st, now := newSelfServiceTestStore(t) + h, raw := registerV1TestHive(t, st) + *now = now.Add(time.Minute) + if _, err := st.rotate(h.ID); err != nil { // gives the record a previous key too + t.Fatal(err) + } + *now = now.Add(time.Minute) + beat := *now + if err := st.noteAcceptedHeartbeat(h.ID, beat); err != nil { + t.Fatal(err) + } + before, _ := st.get(h.ID) + + // A mutate that tries to rewrite everything an edit must not touch. + after, err := st.update(h.ID, selfServiceETag(before), func(r *SelfServiceHive) error { + r.ID, r.Owner = "byo-mallory--x", "mallory" + r.CreatedAt = time.Unix(0, 0).UTC() + r.KeyHash, r.PrevKeyHash = "attacker", "attacker" + r.PrevKeyExpiresAt = time.Time{} + r.LastAcceptedHeartbeatAt = time.Unix(0, 0).UTC() + r.Revision = 999 + r.Repos = []string{"alice/docs"} + return nil + }) + if err != nil { + t.Fatal(err) + } + if after.ID != before.ID || after.Owner != before.Owner || !after.CreatedAt.Equal(before.CreatedAt) { + t.Fatalf("update changed identity: %+v", after) + } + if after.KeyHash != before.KeyHash || after.PrevKeyHash != before.PrevKeyHash || !after.PrevKeyExpiresAt.Equal(before.PrevKeyExpiresAt) { + t.Fatal("update changed the key hashes") + } + if !after.LastAcceptedHeartbeatAt.Equal(beat) { + t.Fatalf("update lost the heartbeat stamp: %v, want %v", after.LastAcceptedHeartbeatAt, beat) + } + if after.Revision != before.Revision+1 { + t.Fatalf("revision = %d, want %d", after.Revision, before.Revision+1) + } + if len(after.Repos) != 1 || after.Repos[0] != "alice/docs" { + t.Fatalf("the permitted change was not applied: %v", after.Repos) + } + if !st.verify(h.ID, raw) { + // raw was the key before the rotate; it is the previous key now and + // must still verify inside the overlap. + t.Fatal("previous key stopped verifying after the update") + } + // Persisted, not only in memory. + reloaded := newSelfServiceStore(st.path) + if err := reloaded.load(); err != nil { + t.Fatal(err) + } + if got, _ := reloaded.get(h.ID); got.Revision != after.Revision || got.KeyHash != before.KeyHash { + t.Fatalf("persisted record = %+v", got) + } +} + +func TestSelfServiceUpdateRefusals(t *testing.T) { + st, _ := newSelfServiceTestStore(t) + h, _ := registerV1TestHive(t, st) + noop := func(*SelfServiceHive) error { return nil } + + if _, err := st.update("byo-nobody--x", "", noop); !errors.Is(err, errSelfServiceNotFound) { + t.Fatalf("unknown id: %v", err) + } + for _, stale := range []string{`"nope"`, `W/` + selfServiceETag(h), `"` + strings.Repeat("0", 24) + `"`} { + if _, err := st.update(h.ID, stale, noop); !errors.Is(err, errSelfServiceStale) { + t.Fatalf("If-Match %q: %v, want stale", stale, err) + } + } + // Each form is built from the record as it is NOW; every success bumps + // the revision, so a tag captured once would be stale for the next form. + accepted := []func(SelfServiceHive) string{ + func(SelfServiceHive) string { return "" }, + func(SelfServiceHive) string { return "*" }, + func(cur SelfServiceHive) string { return selfServiceETag(cur) }, + func(cur SelfServiceHive) string { return `"nope", ` + selfServiceETag(cur) }, + } + for i, build := range accepted { + cur, _ := st.get(h.ID) + if _, err := st.update(h.ID, build(cur), noop); err != nil { + t.Fatalf("accepted If-Match form %d refused: %v", i, err) + } + } + h, _ = st.get(h.ID) + + // A mutate error leaves the record as it was. + before, _ := st.get(h.ID) + boom := errors.New("boom") + if _, err := st.update(h.ID, "", func(r *SelfServiceHive) error { r.DashboardURL = "x"; return boom }); !errors.Is(err, boom) { + t.Fatalf("mutate error = %v", err) + } + after, _ := st.get(h.ID) + if after.DashboardURL != before.DashboardURL || after.Revision != before.Revision { + t.Fatalf("a failed mutate changed the record: %+v", after) + } +} + +func TestSelfServiceRotateAndRevokeIfMatch(t *testing.T) { + st, _ := newSelfServiceTestStore(t) + h, _ := registerV1TestHive(t, st) + if _, err := st.rotateIfMatch(h.ID, `"stale"`); !errors.Is(err, errSelfServiceStale) { + t.Fatalf("rotate with a stale ETag: %v", err) + } + if _, err := st.revokeIfMatch(h.ID, `"stale"`); !errors.Is(err, errSelfServiceStale) { + t.Fatalf("revoke with a stale ETag: %v", err) + } + if _, ok := st.get(h.ID); !ok { + t.Fatal("a refused revoke removed the record") + } + if _, err := st.rotateIfMatch(h.ID, selfServiceETag(h)); err != nil { + t.Fatalf("rotate with the current ETag: %v", err) + } + cur, _ := st.get(h.ID) + if removed, err := st.revokeIfMatch(h.ID, selfServiceETag(cur)); err != nil || !removed { + t.Fatalf("revoke with the current ETag: %v %v", removed, err) + } +} + +func TestSelfServiceETagIsStrongAndKeyBlind(t *testing.T) { + h := SelfServiceHive{ID: "byo-alice--lab", CreatedAt: time.Date(2026, 11, 13, 12, 0, 0, 0, time.UTC), Revision: 4, KeyHash: "aaaa"} + tag := selfServiceETag(h) + if !strings.HasPrefix(tag, `"`) || !strings.HasSuffix(tag, `"`) || strings.HasPrefix(tag, "W/") { + t.Fatalf("ETag %q is not a strong tag", tag) + } + other := h + other.KeyHash = "bbbb" + if selfServiceETag(other) != tag { + t.Fatal("the ETag depends on the key hash") + } + other = h + other.Revision++ + if selfServiceETag(other) == tag { + t.Fatal("a revision change did not change the ETag") + } + other = h + other.CreatedAt = other.CreatedAt.Add(time.Second) + if selfServiceETag(other) == tag { + t.Fatal("delete and re-register of the same ID would reuse an ETag") + } + if selfServiceETagMatches(h, "W/"+tag) { + t.Fatal("a weak tag matched under strong comparison") + } +} + +func TestSelfServiceProofTimes(t *testing.T) { + t0 := time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC) + t1 := t0.Add(time.Hour) + fallback := t0.Add(-time.Hour) + prev := map[string]time.Time{"alice/lab": t0, "alice/gone": t0} + got := selfServiceProofTimes(prev, []string{"Alice/Lab", "alice/new", "alice/old"}, fallback, t1, []string{"alice/new"}) + want := map[string]time.Time{"alice/lab": t0, "alice/new": t1, "alice/old": fallback} + if len(got) != len(want) { + t.Fatalf("proofs = %v, want %v", got, want) + } + for k, v := range want { + if !got[k].Equal(v) { + t.Errorf("proof[%s] = %v, want %v", k, got[k], v) + } + } + if len(prev) != 2 || !prev["alice/lab"].Equal(t0) { + t.Fatal("selfServiceProofTimes modified its input") + } + if none := selfServiceProofTimes(nil, []string{"alice/x"}, time.Time{}, t1, nil); len(none) != 0 { + t.Fatalf("a repo with no proof and no fallback got a time: %v", none) + } +} From 9fc8868bb0e3c4c8abdb424ceca418a4900fdb17 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Tue, 17 Nov 2026 23:49:25 -0500 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20hub:=20fix=20v1=20registry?= =?UTF-8?q?=20test=20fixture=20and=20drop=20an=20unused=20helper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: James Reilly <5840441+hanthor@users.noreply.github.com> Signed-off-by: Andrew Anderson --- src/pkg/hub/selfservice_v1_api_test.go | 9 --------- src/pkg/hub/selfservice_v1_registry_test.go | 4 +++- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/pkg/hub/selfservice_v1_api_test.go b/src/pkg/hub/selfservice_v1_api_test.go index 18381f8679..6844306290 100644 --- a/src/pkg/hub/selfservice_v1_api_test.go +++ b/src/pkg/hub/selfservice_v1_api_test.go @@ -79,15 +79,6 @@ func decodeV1Registration(t *testing.T, rec *httptest.ResponseRecorder) hubV1Reg return resp.Registration } -func sortedLower(in []string) []string { - out := make([]string, len(in)) - for i, s := range in { - out[i] = strings.ToLower(s) - } - sort.Strings(out) - return out -} - func TestHubV1Discovery(t *testing.T) { cases := []struct { name string diff --git a/src/pkg/hub/selfservice_v1_registry_test.go b/src/pkg/hub/selfservice_v1_registry_test.go index 3e95e6ceeb..e710f6d878 100644 --- a/src/pkg/hub/selfservice_v1_registry_test.go +++ b/src/pkg/hub/selfservice_v1_registry_test.go @@ -14,10 +14,12 @@ import ( func registerV1TestHive(t *testing.T, st *selfServiceStore) (SelfServiceHive, string) { t.Helper() + proved := time.Date(2026, 11, 13, 11, 0, 0, 0, time.UTC) raw, err := st.register(SelfServiceHive{ ID: "byo-alice--lab", Owner: "alice", Org: "alice", Repos: []string{"alice/lab", "alice/tools"}, DashboardURL: "https://lab.example.org/", - OwnershipCheckedAt: time.Date(2026, 11, 13, 11, 0, 0, 0, time.UTC), + OwnershipCheckedAt: proved, + RepoProofs: map[string]time.Time{"alice/lab": proved, "alice/tools": proved}, }) if err != nil { t.Fatal(err)