Skip to content

feat: caller-named claims — per-subject scoping and attach-existing-claim Contexts #59

Open

Description

@pdettori

Companion change for rossoctl/moca#358, specified in moca's per-subject claim names design §2.2 (PR rossoctl/moca#543). Moca lifts its claimName stop-gap and forwards a caller-supplied slug; Context Service derives, scopes and enforces the claim. Verified against this repo at main 7951826: a Context today is its managed PVC, so this is most of the work on the Moca side of the feature.

What is needed

  1. Metadata record for attached-claim Contexts. A namespaced ConfigMap per attached-claim Context (the existing queryIndexName pattern) carries name, type, revision history, frozen revision, owner, grants, consumers, audit and the attachment. Writing annotations on the caller's PVC is rejected: anyone with edit rights on the claim could rewrite its grants. Managed-PVC Contexts keep today's PVC-carried metadata untouched (additive). The record is deterministically named for the Context name and created with create-if-absent semantics, and the managed path creates the same reservation before its PVC — today its uniqueness comes for free from PersistentVolumeClaims.Create on context-<name> (manager.go:72) — so a managed and an attached create of one Context name cannot both succeed.
  2. contextId = the record's UID for attached-claim Contexts, never the claim's UID (shared across Contexts, stable across delete-recreate). Upload, freeze and export verify their expected UID against the record where they verify pvc.UID today (context_upload.go).
  3. One claim backs at most one live Context: 409 claim_in_use. The check is a per-claim marker object, deterministically named from the claim and created if-absent — a list-then-create check cannot refuse both of two concurrent creates that name the same claim. Delete removes the record and the marker before the claim counts as released; the re-created Context's fresh record has a fresh UID.
  4. storage.claimName accepted on create (internal and public). When set, no PVC is created: the claim named moca-<scope>-<slug> must already exist for the request's subject (409 claim_not_found, never provisioned). <scope> = HMAC-SHA256(secret, subjectKey)[:32] where subjectKey = kind + ":" + name (access.go:315's encoding — so user:x and serviceaccount:x never share a scope) and the secret is a new CONTEXT_CLAIM_SECRET setting, stable per deployment (random per process if unset — fine single-replica, wrong for more). size/storageClass are ignored on that path.
  5. The claim is a revision store, exactly as a managed PVC is today. An upload materializes into .context-service/materialized/<digest>/ on it — content-addressed (materializeUploadWithLimit, cmd/context-service/main.go:210; UploadContext refuses any other WorkspacePath) — the revision id is the digest of exactly what export streams (exportMaterializedRevision), and export streams only that directory, so the freeze pin's property is unchanged. Pre-existing content at the claim's root is invisible: never read, never part of a revision, never exported, never overwritten — Context Service touches only .context-service/ on the claim. Bring-your-own-PVC buys the caller the volume's capacity, storage class and placement, not its data. Two footnotes on an adopted claim: ensureMaterializationDirectory chmods .context-service/ and materialized/ to 0755, and the expanded-size limit (contextUploadExpandedLimit) comes from the claim's requested size, not its free space. DELETE removes the Context (record included) but never the claim.
  6. The trusted create response gains attachment.claimName — the only attachment field on the trusted contract — amending the "omits the PVC attachment because Moca receives content as bundles, not mounts" rationale at uploads.go:106: for an attached-claim Context, Moca stores the derived name and returns it to the owner.

Deliberately not built: importing the claim's existing tree as a revision (digesting and materializing the root content as its own revision, with its own size and type checks) — a further change with no consumer yet; see the moca spec §3's limits.

Tests (with the change)

  • The same slug from two subjects derives to two different claim names; a subject cannot attach a claim outside its scope.
  • claim_not_found and claim_in_use; two concurrent creates naming the same claim let exactly one win, and a managed and an attached create of one Context name let exactly one win (the shared reservation).
  • A freeze with a stale contextId fails against the record's UID; deleting the Context removes the record and leaves the claim.
  • Context Service writes only under .context-service/ on an adopted claim, and pre-existing content there never reaches a revision or an export.

🤖 Generated with Claude Code

Activity

  1. moonlight16 commented on Oct 20, 2026

    @moonlight16
    Member

    Thanks for writing this up, Paolo. Some history and where phases 1 and 2 have landed, before we build it.

    History. Context Service could already attach an existing PVC: a182685 ("Support externally managed workspace claims", contextctl create --claim <pvc> --read-only|--read-write). It mounted the claim into a sandbox pool and never deleted it. #51 (Context Service stores context only) removed it along with the sandbox pools. So #59 brings the idea back, but attached to a Context instead of a sandbox pool, and that adds two things the old version never had to handle: per-user scoping, and frozen revisions on a volume the user also controls.

    Where this meets Moca's phase 2. The phase 2 design (rossoctl/moca#553 and its follow-up rossoctl/moca#561) gives each workload its own sandboxes and a PVC. Its §11 lists simplifications to explore, including "MOCA owns the workload's PVC; Context Service only fills it with the revision." If we go that way, bring-your-own-claim belongs in Moca's workload API, close to the old --claim behavior, and Context Service needs no attach-claim path at all. I'd like to settle that before we build #59.

    If we do build it here, four things to fix first:

    1. The first create can't succeed. The user must pre-create moca-<scope>-<slug>, but nothing tells them that name before a create succeeds, and 409 claim_not_found doesn't include it. Return the expected name in the 409, or add a call that derives it.
    2. The per-user scoping doesn't hold on the public route. requestSubject trusts X-Context-Subject without authentication (internal/api/handler.go:387), so claimName must be accepted only on /internal/v1/contexts.
    3. Helper pods would re-own the user's whole volume. The upload and export pods set fsGroup (internal/kube/context_upload.go:359-372), so kubelet would change group ownership on everything already on the claim. Attached claims should get no fsGroup.
    4. Freeze doesn't protect a user-owned volume. exportMaterializedRevision (cmd/context-service/main.go:114) streams the revision folder without re-checking it, so anyone with write access to the claim can change a frozen revision. Verify the content against its digest on export.

    Smaller items:

    • Bind the Context to the claim's UID, so a deleted and re-created claim with the same name is caught.
    • Snapshots and clones also need to take the name reservation.
    • Turn off snapshot and clone for attached claims for now.
    • Make an unset CONTEXT_CLAIM_SECRET a startup error.
    • Pin down the HMAC input encoding.

    Proposal: hold #59 until we decide who owns workload storage in phase 2. If it stays in Context Service, we fix the four above, and the old --claim code is a good starting point for the claim lookup, access-mode checks and the never-delete rule.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions