| 1 | // The identity registry of a valley instance: who the instance grants |
| 2 | // something to, with which keys, and for how long. Identity is a governed |
| 3 | // registry (dcr-b87f6e8) — one declared CUE document in the instance |
| 4 | // repository, changed only through the integration path — and this file is |
| 5 | // its schema. The schema is the substrate's floor over every instance |
| 6 | // registry, and a floor mandates properties, never names: mandatory expiry |
| 7 | // where a raw key is held, no external principal governing the registry, |
| 8 | // and the genesis entry governing it. Which principals exist, which |
| 9 | // boundaries the instance runs, and what a grant is called are instance |
| 10 | // content. |
| 11 | // |
| 12 | // Two tools read this file. The compiler (identity/) renders a registry |
| 13 | // into the artifacts the enforcement boundaries check — the known-signers |
| 14 | // file the integrator and `attest verify` read, the git user's |
| 15 | // authorized_keys, the grants the pre-receive hook checks, and the two |
| 16 | // approval signers files. The read-access reader (readaccess/) takes the |
| 17 | // read grants out of it for the web publisher, which checks them at the |
| 18 | // edge read boundary. |
| 19 | // Expiry is enforced by both rather than here, because a document has no |
| 20 | // clock: each omits an expired entry, so access ends at the first |
| 21 | // compilation or publication after expiry. |
| 22 | // |
| 23 | // Like the host, event and policy schemas, this file is deliberately not |
| 24 | // Nix. A registry names principals, keys and boundaries; it never names |
| 25 | // derivations, systems or store paths, so it travels between machines |
| 26 | // unchanged. |
| 27 | // |
| 28 | // Validate a registry against this schema: |
| 29 | // cue vet -c schema/identity.cue examples/identity/registry/*.cue |
| 30 | package identity |
| 31 | |
| 32 | import "strings" |
| 33 | |
| 34 | // A principal is a named actor the instance grants something to. |
| 35 | #Principal: { |
| 36 | // human: a person. machine: a computer acting for itself. service: a |
| 37 | // program the instance runs. An agent run is none of these and holds no |
| 38 | // entry — a run acts under authority delegated from a principal |
| 39 | // (ida-a8243d2) and is named by its provenance (ida-45178f6), so the |
| 40 | // registry holds the top of that chain and never its links. |
| 41 | kind: "human" | "machine" | "service" |
| 42 | |
| 43 | // The keys that prove possession. A bearer secret — a credential whose |
| 44 | // possession is the entire proof — is not a key and has no place here |
| 45 | // (dcr-b87f6e8). A principal may hold no key only when every grant it |
| 46 | // holds is at an edge-read boundary (readScopes): such a reader signs, |
| 47 | // pushes, approves and governs nothing, and reads through the read |
| 48 | // links the host issues, whose verifiers live in protected host state |
| 49 | // outside every repository. docs/security.md, "Bearer read |
| 50 | // credentials", records that decision, which supersedes dcr-b87f6e8's |
| 51 | // ban on bearer secrets for these read credentials alone. |
| 52 | keys: [...#Key] |
| 53 | |
| 54 | // What this principal may do, keyed by grant name. The name is instance |
| 55 | // content; what makes a grant real is the boundary it names. |
| 56 | grants: [#Name]: #Grant |
| 57 | |
| 58 | // The identity an external collaborator already controls — a domain |
| 59 | // handle or a decentralized identifier. It is cited, never minted: the |
| 60 | // registry decides what that identity may do here, and never becomes the |
| 61 | // person's identity. null for a principal of this instance. |
| 62 | external: #External | *null |
| 63 | |
| 64 | // The first day this entry no longer counts. The compiler compares it |
| 65 | // against the day it runs and omits the entry from every artifact from |
| 66 | // that day on, so an expiry is a revocation already scheduled. |
| 67 | expires?: #Date |
| 68 | |
| 69 | // Mandatory expiry, the floor's first property. A machine or service |
| 70 | // holds its raw key until the certificate issuance service arrives |
| 71 | // (dcr-b87f6e8), so the key it holds is one with an end; an external |
| 72 | // principal is not this instance's to vouch for indefinitely. |
| 73 | if kind != "human" {expires: #Date} |
| 74 | if external != null {expires: #Date} |
| 75 | } |
| 76 | |
| 77 | // One key of a principal. |
| 78 | #Key: { |
| 79 | // ed25519 is the only algorithm the note format signs or verifies with |
| 80 | // (attest/sign.go) and the only one sshd is asked to accept here. A |
| 81 | // passkey is the same class of credential (dcr-b87f6e8) and becomes a |
| 82 | // second class the day a surface authenticates against one. |
| 83 | class: "ssh-ed25519" |
| 84 | |
| 85 | // What the private half is bound to: hardware a person holds, or one |
| 86 | // host. Nothing here is bound to a file that travels. |
| 87 | bound: "hardware" | "host" |
| 88 | |
| 89 | // The public half, exactly as `authorized_keys` writes it — the type, |
| 90 | // base64 of the SSH wire format, and an optional comment. |
| 91 | public: =~"^ssh-ed25519 [A-Za-z0-9+/]+={0,2}( [^\\n]*)?$" |
| 92 | |
| 93 | // The name this key signs notes under, when it signs at all. Per key and |
| 94 | // not per entry: the note format's key hash binds the name to the key |
| 95 | // (attest/sign.go), so one person signing from two machines under two |
| 96 | // names holds two verifier keys, and recording the wrong name compiles a |
| 97 | // line no verifier will ever match. A key without it pushes and never |
| 98 | // attests. |
| 99 | signs?: #SignerName |
| 100 | |
| 101 | // Evidence that the private half was made on a YubiKey and cannot |
| 102 | // leave it. The compiler verifies it against the Yubico roots it |
| 103 | // carries, and a key whose attestation verifies and says every |
| 104 | // signature needs a touch is a hardware approval key. Without one, a |
| 105 | // key is never a hardware approval key, whatever bound says. A key |
| 106 | // with one is held on a device, so it is bound to hardware. |
| 107 | attestation?: #Attestation |
| 108 | if attestation != _|_ {bound: "hardware"} |
| 109 | } |
| 110 | |
| 111 | // A YubiKey PIV attestation of one key: two certificates, each PEM as the |
| 112 | // YubiKey writes it. The schema holds their shape; the compiler checks |
| 113 | // what they say. |
| 114 | #Attestation: { |
| 115 | // The certificate the YubiKey wrote for the slot holding the key: |
| 116 | // `yubico-piv-tool -a attest -s 9c` for slot 9c. |
| 117 | slot: #Certificate |
| 118 | |
| 119 | // The YubiKey's own attestation certificate, which signed the slot's: |
| 120 | // `yubico-piv-tool -a read-certificate -s f9`. |
| 121 | f9: #Certificate |
| 122 | } |
| 123 | |
| 124 | // One PEM certificate and nothing else. |
| 125 | #Certificate: =~"^-----BEGIN CERTIFICATE-----\n([A-Za-z0-9+/=]+\n)+-----END CERTIFICATE-----\n?$" |
| 126 | |
| 127 | // A grant is a named permission. |
| 128 | #Grant: { |
| 129 | // The enforcement boundary that checks it: a key of `boundaries`. A |
| 130 | // grant may exist only where a boundary refuses the action when the |
| 131 | // grant is absent (dcr-b87f6e8), and naming the boundary is what makes |
| 132 | // that checkable — a grant naming a boundary the instance does not run |
| 133 | // fails vet. |
| 134 | boundary: #Name |
| 135 | |
| 136 | // The projects a read grant covers. A grant at a boundary of kind |
| 137 | // "edge-read" names them, and a grant at any other boundary does not: |
| 138 | // the edge read boundary is the one boundary that checks a project |
| 139 | // scope. Every other boundary is compiled host-wide, and push cannot be |
| 140 | // anything else, because per-project access is not honestly enforceable |
| 141 | // over a shared git user (dcr-0f5d9b1). A scope there would be a scope |
| 142 | // nothing checks. readScopes below holds each grant to this. |
| 143 | projects?: #Scope |
| 144 | } |
| 145 | |
| 146 | // The projects a read grant covers: "all" for every project, or each |
| 147 | // project's name with the publication identity its own policy/read declares |
| 148 | // (schema/read.cue). A project whose policy declares another identity is not |
| 149 | // covered, so a new project that reuses a name does not inherit the old |
| 150 | // one's grants. An empty set covers no project. Neither form lets anyone |
| 151 | // read a project whose own policy/read does not name them. |
| 152 | #Scope: "all" | close({[#ProjectName]: #Identity}) |
| 153 | |
| 154 | // A project's name and publication identity, as schema/read.cue holds them. |
| 155 | #ProjectName: =~"^[a-zA-Z0-9][a-zA-Z0-9._-]*$" & strings.MaxRunes(64) |
| 156 | #Identity: =~"^[0-9a-f]{32}$" |
| 157 | |
| 158 | // The reserved principal for a reader who presents no credential. It is a |
| 159 | // variant of its own, never an entry under principals: it holds no key, no |
| 160 | // credential and no external identity, and it does not expire. It holds |
| 161 | // read grants alone, so it never pushes, approves, restacks or governs the |
| 162 | // registry, and it is never the genesis entry. A key sshd admits without a |
| 163 | // principal tag pushes as no principal, and is not anyone. |
| 164 | #Anyone: { |
| 165 | grants: [#Name]: #Grant |
| 166 | } |
| 167 | |
| 168 | // An enforcement boundary: the point in the running system that refuses an |
| 169 | // action when the grant is absent. |
| 170 | #Boundary: { |
| 171 | // What the enforcement point is. |
| 172 | // |
| 173 | // "git-push" — sshd on a host serving the instance's repositories. The |
| 174 | // compiler renders the git user's authorized_keys from the grants at |
| 175 | // this boundary, tagging each key with its principal's name, and the |
| 176 | // pre-receive hook reads that name off the key that pushed |
| 177 | // (nix/valley-host.nix). |
| 178 | // |
| 179 | // "registry" — the integration path that admits a change to the registry |
| 180 | // document itself. Governance of the registry's own stream is a grant |
| 181 | // here, and the required checks of that path class include human |
| 182 | // approval (ida-b7025b5). |
| 183 | // |
| 184 | // "approve" — the pre-receive hook (valleyhook/) on a host serving the |
| 185 | // instance's repositories, checking writes to the integration-request |
| 186 | // namespace (refs/the-valley/integration-requests/*). This is the |
| 187 | // approve grant, the request verb of dcr-e544f20. "request" is the same |
| 188 | // kind under the name it had first, and is also accepted: a grant at a |
| 189 | // boundary of either kind is the approve grant. The compiler writes |
| 190 | // each holder into the grants file the hook reads, and the hook refuses |
| 191 | // a principal it does not name there: it cannot file, replace or |
| 192 | // withdraw a request. The grant rides on push — only a key sshd admits |
| 193 | // reaches the hook — so a principal that files requests holds a |
| 194 | // "git-push" grant too. |
| 195 | // |
| 196 | // "restack" — the same pre-receive hook, checking the two writes the |
| 197 | // host restacker needs beyond an ordinary push: adding one version to |
| 198 | // another principal's ready change record, on the branch and under the |
| 199 | // title of its active version, and writing restack reports |
| 200 | // (refs/the-valley/restack/*). The compiler writes each holder into the |
| 201 | // grants file as "restack <principal>". The grant opens nothing else: |
| 202 | // no other record transition, and no integration request. It rides on |
| 203 | // push too. |
| 204 | // |
| 205 | // "edge-read" — the edge read boundary: the web publisher on a host |
| 206 | // serving the instance's repositories (web/cmd/valley-web-publish), |
| 207 | // and the web server at one destination, which serves what the |
| 208 | // publisher sends it. A grant here is a read grant. It lets its holder |
| 209 | // read the projects its scope covers, wherever a project's own |
| 210 | // policy/read names the holder too (schema/read.cue). The publisher |
| 211 | // sends the destination a project only when both consents let anyone |
| 212 | // read it, and the server there authenticates no reader, so a read |
| 213 | // grant to a named principal opens nothing at the edge. |
| 214 | kind: "git-push" | "registry" | "approve" | "request" | "restack" | "edge-read" |
| 215 | |
| 216 | // The destination an edge read boundary serves: the name the publisher |
| 217 | // is given as --destination and signs every manifest for. An edge read |
| 218 | // boundary names one, and no other boundary does; readScopes below holds |
| 219 | // each boundary to this. |
| 220 | destination?: #Name |
| 221 | } |
| 222 | |
| 223 | // An identity the registry cites rather than mints. |
| 224 | #External: { |
| 225 | // What kind of identity is being cited. |
| 226 | kind: "domain" | "did" |
| 227 | |
| 228 | // The handle itself: a domain name, or a decentralized identifier. It is |
| 229 | // verified at onboarding and re-verifiable by anyone. |
| 230 | handle: =~"^[^\\s]+$" |
| 231 | } |
| 232 | |
| 233 | // A calendar day, UTC. |
| 234 | #Date: =~"^[0-9]{4}-[0-9]{2}-[0-9]{2}$" |
| 235 | |
| 236 | // Principal, grant and boundary names. Held to the shape a principal name |
| 237 | // must have to ride into the pre-receive hook's environment |
| 238 | // (nix/valley-host.nix), because that is where a compiled name ends up. |
| 239 | #Name: =~"^[a-zA-Z0-9][a-zA-Z0-9._-]*$" |
| 240 | |
| 241 | // A name a key signs under, held to what a signature line can carry |
| 242 | // (attest/sign.go): no space, because a space separates the name from the |
| 243 | // signature, and no plus, because a plus separates the fields of a verifier |
| 244 | // key. |
| 245 | #SignerName: =~"^[^\\s+]+$" |
| 246 | |
| 247 | // ---------------------------------------------------------------------- |
| 248 | // The registry. |
| 249 | |
| 250 | // The enforcement boundaries this instance runs. Every grant names one of |
| 251 | // these, and the cross-check below adds any name a grant used, so a grant |
| 252 | // at a boundary the instance does not run fails vet as an incomplete |
| 253 | // boundary rather than compiling into nothing. |
| 254 | boundaries: [#Name]: #Boundary |
| 255 | boundaries: { |
| 256 | for _, p in principals for _, g in p.grants {(g.boundary): _} |
| 257 | if anyone != _|_ for _, g in anyone.grants {(g.boundary): _} |
| 258 | } |
| 259 | |
| 260 | // The entries. anyone is reserved for the variant below, so an entry under |
| 261 | // that name fails vet naming principals.anyone. |
| 262 | principals: [#Name]: #Principal |
| 263 | principals: anyone?: "INVALID: anyone is reserved for the reader with no credential; its grants go under the top-level anyone" |
| 264 | |
| 265 | // The read grants of a reader who presents no credential (#Anyone). |
| 266 | anyone?: #Anyone |
| 267 | |
| 268 | // The genesis entry: created at instance birth, the anchor later boundaries |
| 269 | // verify against, and the one structural requirement of a registry. |
| 270 | genesis: #Name & !="anyone" |
| 271 | |
| 272 | // Derived: the genesis entry's grants that govern the registry's own |
| 273 | // stream. A registry whose genesis governs nothing fails vet with an error |
| 274 | // naming this field — there would be no anchor, and no hand the registry |
| 275 | // itself is in. |
| 276 | genesisGovernance: [ |
| 277 | for gn, g in principals[genesis].grants |
| 278 | if boundaries[g.boundary].kind == "registry" {gn}, |
| 279 | ] |
| 280 | genesisGovernance: [_, ...] |
| 281 | |
| 282 | // Derived, and empty in every valid registry: the grants at a registry |
| 283 | // boundary held by a cited identity. The floor forbids them — an external |
| 284 | // principal governing the registry would put the instance's own membership |
| 285 | // in a hand the instance does not control — so each one lands here as a |
| 286 | // sentinel string against a `bool` and fails vet naming the principal and |
| 287 | // the grant. |
| 288 | externalGovernance: [string]: bool |
| 289 | externalGovernance: { |
| 290 | for pn, p in principals if p.external != null |
| 291 | for gn, g in p.grants if boundaries[g.boundary].kind == "registry" { |
| 292 | "\(pn).\(gn)": "INVALID: an external principal may not hold registry governance" |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // Derived, and empty in every valid registry: the boundaries and grants |
| 297 | // whose read scope does not fit. An edge read boundary names its |
| 298 | // destination, and no other boundary names one. A grant at an edge read |
| 299 | // boundary names the projects it covers, and no other grant names any. |
| 300 | // anyone holds grants at edge read boundaries alone, and so does a |
| 301 | // principal with no keys. Each boundary or grant |
| 302 | // that breaks a rule lands here as a sentinel string against a `bool` and |
| 303 | // fails vet naming it. |
| 304 | readScopes: [string]: bool |
| 305 | readScopes: { |
| 306 | for bn, b in boundaries { |
| 307 | if b.kind == "edge-read" && b.destination == _|_ { |
| 308 | "\(bn)": "INVALID: an edge-read boundary names its destination" |
| 309 | } |
| 310 | if b.kind != "edge-read" && b.destination != _|_ { |
| 311 | "\(bn)": "INVALID: only an edge-read boundary names a destination" |
| 312 | } |
| 313 | } |
| 314 | for pn, p in principals for gn, g in p.grants { |
| 315 | if boundaries[g.boundary].kind == "edge-read" && g.projects == _|_ { |
| 316 | "\(pn).\(gn)": "INVALID: a grant at an edge-read boundary names the projects it covers" |
| 317 | } |
| 318 | if boundaries[g.boundary].kind != "edge-read" && g.projects != _|_ { |
| 319 | "\(pn).\(gn)": "INVALID: only a grant at an edge-read boundary names projects" |
| 320 | } |
| 321 | } |
| 322 | for pn, p in principals if len(p.keys) == 0 for gn, g in p.grants { |
| 323 | if boundaries[g.boundary].kind != "edge-read" { |
| 324 | "\(pn).\(gn)": "INVALID: a principal with no keys holds read grants alone, at edge-read boundaries" |
| 325 | } |
| 326 | } |
| 327 | if anyone != _|_ for gn, g in anyone.grants { |
| 328 | if boundaries[g.boundary].kind != "edge-read" { |
| 329 | "anyone.\(gn)": "INVALID: anyone holds read grants alone, at edge-read boundaries" |
| 330 | } |
| 331 | if boundaries[g.boundary].kind == "edge-read" && g.projects == _|_ { |
| 332 | "anyone.\(gn)": "INVALID: a grant at an edge-read boundary names the projects it covers" |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // A file's top level cannot be closed (embedding a definition would open it |
| 338 | // instead), so reject stray top-level fields explicitly: anything but the |
| 339 | // registry's own fields conflicts with this sentinel and fails vet with an |
| 340 | // error naming the field. |
| 341 | [ |
| 342 | !="boundaries" & |
| 343 | !="principals" & |
| 344 | !="anyone" & |
| 345 | !="genesis" & |
| 346 | !="genesisGovernance" & |
| 347 | !="externalGovernance" & |
| 348 | !="readScopes", |
| 349 | ]: "INVALID: unknown top-level field; only \"boundaries\", \"principals\", \"anyone\" and \"genesis\" are allowed" |