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