Skip to content

Commit df030d0

Browse files
PiquelChipsclaude
andcommitted
feat: add capabilities and scoped tokens
policy.toml now says who may do what: [[paths]] rules give each user, node or group none, read or write under a path, and [grants] the annotate, land and admin capabilities. Only admin lands on `policy`, so no one grants themselves rights. Servers authenticate bearer tokens: static ones from their configuration, or short-lived scoped ones they sign with their node key and verify without storing, which only narrow their principal's rights (`origin token issue`). A token lasts at most 7 days, and one issued with a token expires with it. OriginService filters what each caller reads: a caller who reads no path sees nothing of the repository, snapshot walks withhold the content of unreadable paths, and revisions that only change such paths are hidden with their statements and events. Writes are checked: records put must be the caller's, statements must be about what they see and of a kind they annotate, promotions included, revisions may only change paths they write and only hold content they supplied or that a snapshot they refer to holds at the same path, and landings may only change what the submitter writes, whatever they land. Where a writer may not write, a successor must have exactly each predecessor's effect: the same files changed from the same entries to the same ones, on the same change below, with exactly the same conflicts unresolved; a root lists only those of the revision it's on. So anyone may take a change over and hand it on, but no base, chain of revisions or other change makes it revert, replace or resolve what they can't write, or add a conflict there. Only a new change gets a root, and revisions evolve only from their own change's, so no one becomes a change's creator, who abandons it, by adding one. Reserved kinds come only from those core names, whoever puts them. A batch is read before the store, so it may hold a stack. Promotions only copy results from records the caller sees. Descriptions and payloads become attachments, an object kind of their own, so no record can carry a file's content. Snapshots are fetched by the record they belong to, never by a tree within one. Clones record each user's rights, keep withheld files out of the working copy and in their snapshots, and refuse pushes the server would reject. A checkout asks the remote only for the files the clone lacks, so one that needs nothing from it works offline. Each user of a clone, and each scope of their tokens, keeps their own rights and event cursor, so the workspaces of one clone can act for different users and agents. Once their rights change, the next fetch compares every change again: the events since the last fetch don't show what they couldn't see then. The API also lists directories and reads files of any snapshot, without its history. New commands: `whoami`, `permissions`, `token issue`. An admin may also abandon anyone's change (`Policy::authorizes`). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 55a24f0 commit df030d0

51 files changed

Lines changed: 7229 additions & 788 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎AGENTS.md‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,30 @@ review bots read and write at the same time, written in Rust.
2121
the HTTP routes (`http.rs`) and the command line's commands that the API
2222
offers only translate into its calls, so neither duplicates the other.
2323
`sync.rs` shares records with a remote (`remote.rs`), which answers the
24-
same calls over the SDK or, for a local path, in-process.
24+
same calls over the SDK or, for a local path, in-process. Every call
25+
takes a `Caller`, authenticated by `auth.rs`; `permissions.rs` filters
26+
what it reads and checks what it writes, by the rights `policy.rs`
27+
resolves from `policy.toml`.
2528
- `tools/xtask`: development tasks, run with `cargo xtask`, such as timing
2629
origin against git (`just bench-git`).
2730

31+
## API changes
32+
33+
Every server feature lives in `OriginService`; transports like HTTP only
34+
translate requests. A new HTTP endpoint also takes a line in the SDK's
35+
`endpoints!` macro, and `just openapi` to update `docs/openapi.json`.
36+
2837
## Testing & Benchmarks
2938

3039
Performance is a primary goal, so benchmarks matter as much as tests. A change
3140
to a hot path (hashing, storage, snapshots, transfer) should come with a
3241
benchmark, and its numbers in the pull request.
3342

3443
- `just bench`: the divan micro-benchmarks (encoding, store writes,
35-
snapshots, input digests and promotion, landing, reading events, the
36-
server's request latency and long-poll wakeups, syncing a 20,000-file
37-
tree over HTTP, and a fetch that brings nothing from a long history).
44+
snapshots, input digests and promotion, landing, permission checks,
45+
reading events, the server's request latency and long-poll wakeups,
46+
syncing a 20,000-file tree over HTTP, and a fetch that brings nothing
47+
from a long history).
3848
- `just bench-git`: times origin against git on a 20,000-file tree with
3949
hyperfine, and rewrites `docs/benchmarks.md`.
4050

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎README.md‎

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ origin remote [<url>] # show or set the remote: http://host:port/<
4949
origin push [<change>...] # share changes and the ones below them; lands nothing
5050
origin fetch [--target <t>] [--change <id>] # new acceptances, and the records of known changes
5151
origin pull # fetch, then move the working copy onto its target's tip
52+
origin whoami | permissions # who the remote thinks you are, and what you may do there
53+
origin token issue --scope "write src" [--ttl 1h] # a short-lived token for an agent
5254
```
5355

5456
Landing rebases a change onto the target's tip, file by file, checks the
@@ -105,7 +107,62 @@ Changes are named by a unique prefix of their id, or `@` for the working
105107
copy's. A revision is `@`, a change (for its head), or a unique prefix of
106108
the revision's id. Your name and node key live in `~/.config/origin` (or
107109
`$ORIGIN_CONFIG_DIR`): set `user = "<name>"` in its `config.toml` if `$USER`
108-
isn't the name you want. See [the design](docs/design.md) for the model.
110+
isn't the name you want, and the token to send each server under
111+
`[tokens]`, by its URL (`"http://127.0.0.1:7878" = "<token>"`), or in
112+
`$ORIGIN_TOKEN`. See [the design](docs/design.md) for the model.
113+
114+
## Permissions
115+
116+
What each user may do lives in `policy.toml`, so changing it is a landing
117+
on `policy`, which only `admin` may make:
118+
119+
```toml
120+
[grants] # capabilities: annotate, land, admin
121+
"group:maintainers" = ["admin", "land *", "annotate review"]
122+
"group:runners" = ["annotate ci.*"]
123+
124+
[[paths]] # path access: none, read or write
125+
path = "secrets"
126+
grants = { all = "none", "group:maintainers" = "write" }
127+
128+
[[paths]] # CI's checks: only maintainers redefine them
129+
path = "checks.toml"
130+
grants = { all = "read", "group:maintainers" = "write" }
131+
```
132+
133+
Under a path, the most specific rule that mentions you decides, and paths
134+
no rule mentions you for are writable. Without `[grants]`, everyone lands
135+
on every target but `policy` and issues every kind of statement. Content
136+
you may not read never reaches your clone: those files are missing from
137+
the working copy and stay as they were in your revisions, and fetching
138+
restores them once you may read them. Revisions that only change what you
139+
can't read, and the statements about them, are hidden from you, and you
140+
may not issue statements about them, nor promote their results. Reading
141+
nothing hides the repository whole. You may only push revisions that
142+
change paths you write, and land what changes only paths you write. You
143+
may take over someone else's change, and hand it on: where you can't
144+
write, your revision must do exactly what theirs did, the same files
145+
changed from the same contents to the same ones, with exactly the same
146+
conflicts unresolved, on the same change below, and a new change lists
147+
there only those of the revision it's on. Only a new change gets a root
148+
revision. Anyone who reads a change, even with a token that only reads,
149+
may still copy its head as it is: the change then diverges until someone
150+
converges it again. The statements you push need their kind's `annotate`, promotions
151+
included, but for the reserved `core.*` kinds, which come only from whom
152+
core names: `core.abandon` from the change's creator, who made its root
153+
revision, or an `admin`, so you can't abandon a change you took over;
154+
`core.conflict` from a revision's author, or a node. Each user of a clone,
155+
and each scope of their tokens, keeps their own rights on the remote, as
156+
of their last fetch, so workspaces of one clone can act for different
157+
users, or agents.
158+
159+
`origin token issue --scope <capability>... --ttl <period>` asks the remote
160+
for a token acting for you with a narrower scope, such as `write src`,
161+
`read /`, `annotate ci.*`, `land main` or `admin`; the server verifies it
162+
without storing it. A server's operator signs one for any principal with
163+
`origin token issue --config origin-server.toml --as user:agent-1 --scope
164+
...`. A token never grants more than its principal's rights, lasts at most
165+
7 days, and a token issued with a token expires with it.
109166

110167
`converge` merges the files of a change's heads. A text file two heads
111168
changed holds both versions between conflict markers (`<<<<<<<`, `=======`,
@@ -136,23 +193,29 @@ key = "node.key" # its secret key, created if missing
136193

137194
[repos]
138195
demo = "repos/demo"
196+
197+
[tokens] # static tokens, by principal
198+
"user:alice" = "a long random secret"
139199
```
140200

141201
The HTTP API, under `/api/v1`, puts records in batches, if the changes
142202
they revise are where the client saw them, and fetches them by id, walks
143203
snapshots for the trees and blobs a client lacks (the only way files leave
144204
a server), in parts of at most 64 MiB each way, compares the records of
145-
changes, lists statements, revisions, targets and their logs, lands
146-
changes and targets, and streams
205+
changes, lists statements,
206+
revisions, targets and their logs, lists the directories and reads the
207+
files of a target's tip or any revision, without its history, issues
208+
scoped tokens, lands changes and targets, and streams
147209
events by long-poll: `GET
148210
/api/v1/repos/<repo>/events?cursor=<n>&wait=30s` answers as soon as there
149211
are events after the cursor, with a JSON batch, the next cursor, and the
150212
generation of the op log it's of: pass it back as `generation`, and a
151213
cursor of another log, such as from before the repository was restored,
152214
is refused as expired.
153215
[`docs/openapi.json`](docs/openapi.json) describes it, and
154-
[`origin-client`](crates/origin-client) is its Rust SDK. Nothing
155-
authenticates callers yet.
216+
[`origin-client`](crates/origin-client) is its Rust SDK. Callers send
217+
`Authorization: Bearer <token>`; without one, they are anonymous, and only
218+
read what everyone may.
156219

157220
## Development
158221

‎apps/origin-src/Cargo.toml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ fixture = []
1818
[dependencies]
1919
anyhow.workspace = true
2020
axum.workspace = true
21+
blake3.workspace = true
2122
clap.workspace = true
2223
getrandom.workspace = true
2324
origin-client.workspace = true
2425
origin-platform.workspace = true
2526
origin-shared.workspace = true
2627
serde.workspace = true
28+
serde_json.workspace = true
2729
similar.workspace = true
2830
thiserror.workspace = true
2931
tokio.workspace = true
@@ -38,7 +40,6 @@ zstd.workspace = true
3840
divan.workspace = true
3941
# The benchmarks build their working trees with the fixtures.
4042
origin-src = { workspace = true, features = ["fixture"] }
41-
serde_json.workspace = true
4243
tempfile.workspace = true
4344

4445
[[bench]]
@@ -49,6 +50,10 @@ harness = false
4950
name = "land"
5051
harness = false
5152

53+
[[bench]]
54+
name = "permissions"
55+
harness = false
56+
5257
[[bench]]
5358
name = "server"
5459
harness = false

‎apps/origin-src/benches/annotations.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ fn promotions_cached(bencher: Bencher<'_, '_>) {
182182
let mut annotations = Annotations::open(&store).unwrap();
183183
assert_eq!(
184184
annotations
185-
.promotions(RecordId::Revision(target), &issuer)
185+
.promotions(RecordId::Revision(target), &issuer, |_| Ok(true))
186186
.unwrap()
187187
.len(),
188188
9
@@ -193,7 +193,7 @@ fn promotions_cached(bencher: Bencher<'_, '_>) {
193193
.with_inputs(|| Annotations::new(&store, index.clone()))
194194
.bench_local_refs(|annotations| {
195195
annotations
196-
.promotions(RecordId::Revision(target), &issuer)
196+
.promotions(RecordId::Revision(target), &issuer, |_| Ok(true))
197197
.unwrap()
198198
});
199199
}
@@ -214,7 +214,7 @@ fn promotions_uncached(bencher: Bencher<'_, '_>) {
214214
})
215215
.bench_local_refs(|annotations| {
216216
annotations
217-
.promotions(RecordId::Revision(target), &issuer)
217+
.promotions(RecordId::Revision(target), &issuer, |_| Ok(true))
218218
.unwrap()
219219
});
220220
}

‎apps/origin-src/benches/land.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl Repo {
9393
author: "user:bench".parse().unwrap(),
9494
}
9595
.encode();
96-
let description = EncodedObject::blob(b"");
96+
let description = EncodedObject::attachment(b"");
9797
self.store.write_all(&[content, tree, description]).unwrap();
9898
self.store.write_all(&[revision]).unwrap();
9999
change

0 commit comments

Comments
 (0)