Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ review bots read and write at the same time, written in Rust.
- `tools/xtask`: development tasks, run with `cargo xtask`, such as timing
origin against git (`just bench-git`).

## API changes

Every server feature lives in `OriginService`; transports like HTTP only
translate requests. A new HTTP endpoint also takes a line in the SDK's
`endpoints!` macro, and `just openapi` to update `docs/openapi.json`.

## Testing & Benchmarks

Performance is a primary goal, so benchmarks matter as much as tests. A change
Expand Down
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,59 @@ send each server under `[tokens]`, by its URL
(`"http://127.0.0.1:7878" = "<token>"`), or in `$ORIGIN_TOKEN`. See
[the design](docs/design.md) for the model.

## Permissions

What each user may do lives in `policy.toml`, so changing it is a landing
on `policy`, which only `admin` may make:

```toml
[grants] # capabilities: annotate, land, admin
"group:maintainers" = ["admin", "land *", "annotate review"]
"group:runners" = ["annotate ci.*"]

[[paths]] # path access: none, read or write
path = "secrets"
grants = { all = "none", "group:maintainers" = "write" }

[[paths]] # CI's checks: only maintainers redefine them
path = "checks.toml"
grants = { all = "read", "group:maintainers" = "write" }
```

Under a path, the most specific rule that mentions you decides, and paths
no rule mentions you for are writable. Without `[grants]`, everyone lands
on every target but `policy` and issues every kind of statement. Content
you may not read never reaches your clone: those files are missing from
the working copy and stay as they were in your revisions, and fetching
restores them once you may read them. Revisions that only change what you
can't read, and the statements about them, are hidden from you, and you
may not issue statements about them, nor promote their results. Reading
nothing hides the repository whole. You may only push revisions that
change paths you write, and land what changes only paths you write. You
may take over someone else's change, and hand it on: where you can't
write, your revision must do exactly what theirs did, the same files
changed from the same contents to the same ones, with exactly the same
conflicts unresolved, on the same change below, and a new change lists
there only those of the revision it's on. Only a new change gets a root
revision. Anyone who reads a change, even with a token that only reads,
may still copy its head as it is: the change then diverges until someone
converges it again. The statements you push need their kind's `annotate`, promotions
included, but for the reserved `core.*` kinds, which come only from whom
core names: `core.abandon` from the change's creator, who made its root
revision, or an `admin`, so you can't abandon a change you took over;
`core.conflict` from a revision's author, or a node. Each user of a clone,
and each scope of their tokens, keeps their own rights on the remote, as
of their last fetch, so workspaces of one clone can act for different
users, or agents.

`origin token issue --scope <capability>... --ttl <period>` asks the remote
for a token acting for you with a narrower scope, such as `write src`,
`read /`, `annotate ci.*`, `land main` or `admin`; the server verifies it
without storing it. A server's operator signs one for any principal with
`origin token issue --config origin-server.toml --as user:agent-1 --scope
...`. A token never grants more than its principal's rights, lasts at most
7 days, and a token issued with a token expires with it.

`converge` merges the files of a change's heads. A text file two heads
changed holds both versions between conflict markers (`<<<<<<<`, `=======`,
`>>>>>>>`), and a binary one keeps one side. A `core.conflict` statement
Expand Down
2 changes: 1 addition & 1 deletion apps/origin-src/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ origin-client.workspace = true
origin-platform.workspace = true
origin-shared.workspace = true
serde.workspace = true
serde_json.workspace = true
similar.workspace = true
thiserror.workspace = true
tokio.workspace = true
Expand All @@ -39,7 +40,6 @@ zstd.workspace = true
divan.workspace = true
# The benchmarks build their working trees with the fixtures.
origin-src = { workspace = true, features = ["fixture"] }
serde_json.workspace = true
tempfile.workspace = true

[[bench]]
Expand Down
2 changes: 1 addition & 1 deletion apps/origin-src/src/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ impl<'a, P: Platform> Writer<'a, P> {

/// What revisions do where their writer may not write, as the module
/// explains: the service checks it of every revision a signed-in caller
/// puts.
/// puts, and a client of those it pushes, before sending anything.
pub struct Effects<'a, P, V> {
store: &'a Store<P>,
/// Where revisions are read: a batch's, then the store's.
Expand Down
70 changes: 60 additions & 10 deletions apps/origin-src/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
//! are all the changes this node knows compared.
//! - Snapshots don't come with records: [`Sync::need`] fetches one when
//! something reads it, such as a checkout, so a clone fetches one
//! snapshot, its target's tip. A snapshot is marked while it's fetched
//! ([`Store::fetching`]), so one whose fetch was interrupted is fetched
//! again rather than taken for whole.
//! snapshot, its target's tip. The remote withholds the content of the
//! paths the user may not read: the trees keep their place, and nothing
//! reads them (see [`Store::with_withheld`]). A snapshot is marked while
//! it's fetched ([`Store::fetching`]), so one whose fetch was interrupted
//! is fetched again rather than taken for whole.
//! - **Pushing** shares revisions, with their snapshots, and the user's
//! statements. It never lands anything. It first compares each change's heads with the
//! remote's, and refuses before uploading anything when the remote has a
//! revision this node lacks: the change is stale. The remote checks the
//! statements. It never lands anything. It first compares each change's
//! heads with the remote's, and refuses before uploading anything when
//! the remote has a revision this node lacks: the change is stale, or a
//! revision changes a path the user may not write. The remote checks the
//! heads again as it publishes the revisions, so a push racing another
//! is refused too. Revisions only go to a remote that has the acceptance
//! they're based on. Each object goes after those it needs: snapshots
Expand All @@ -50,6 +53,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::{iter, mem};

use origin_platform::Native;
use origin_shared::access::{Level, PathAccess};
use origin_shared::api::{
ErrorCode, EventsQuery, Expected, FetchRecords, FetchSnapshot, Inventory, InventoryRequest,
List, LogQuery, PutQuery,
Expand All @@ -59,12 +63,13 @@ use origin_shared::object::{BadSignature, DecodeError};
use origin_shared::pack::{Pack, Packer, Part};
use origin_shared::{
Acceptance, AcceptanceId, Base, ChangeId, EncodedObject, Head, Object, ObjectId, ObjectKind,
Principal, PrincipalName, Record, RecordId, Revision, RevisionId, Statement, StatementId,
TargetName, TreeId,
Principal, PrincipalName, Record, RecordId, RepoPath, Revision, RevisionId, Statement,
StatementId, TargetName, TreeId,
};

use crate::annotations::Annotations;
use crate::index::Index;
use crate::permissions::{Effects, Forbidden};
use crate::policy::{Policy, PolicyError};
use crate::remote::{Remote, RemoteError};
use crate::service::Dependencies;
Expand All @@ -78,6 +83,8 @@ pub struct Sync<'a> {
node: Principal,
/// The user, whose statements pushes share.
user: Principal,
/// What the user may write on the remote, as of the last fetch.
writes: PathAccess,
/// The most bytes a push's request holds: [`Pack::LIMIT`], but in
/// tests.
pack_limit: usize,
Expand Down Expand Up @@ -358,17 +365,29 @@ impl<'a> Sync<'a> {
/// server sends.
const EVENTS: u32 = 1024;

/// Syncs `store` with `remote`, as the node `node`, for `user`.
/// Syncs `store` with `remote`, as the node `node`, for `user`, who
/// writes everything until [`Sync::writing`] says otherwise.
pub fn new(store: &'a Store, remote: &'a Remote, node: PrincipalName, user: Principal) -> Self {
Self {
store,
remote,
node: Principal::Node(node),
user,
writes: PathAccess::uniform(Level::Write),
pack_limit: Pack::LIMIT,
}
}

/// Syncs knowing the user may only write `paths`, so pushes that would
/// be refused fail before sending anything.
#[must_use]
pub fn writing(self, paths: PathAccess) -> Self {
Self {
writes: paths,
..self
}
}

/// Syncs pushing requests of at most `limit` bytes.
#[cfg(test)]
fn with_pack_limit(self, limit: usize) -> Self {
Expand Down Expand Up @@ -948,6 +967,24 @@ impl<'a> Sync<'a> {
Ok(true)
}

/// Fetches what the snapshot of `record` holds under `paths`, whatever
/// the store has: files a checkout lacks, such as those whose content
/// was withheld until the user could read them.
///
/// # Errors
///
/// Fails if the remote doesn't have the record, or the remote or the
/// store fails.
pub fn need_paths(&self, record: RecordId, paths: Vec<RepoPath>) -> Result<(), SyncError> {
let request = FetchSnapshot {
of: record,
have: Vec::new(),
paths,
skip: 0,
};
self.fetch_parts(request, None)
}

/// Fetches what `request` asks for, part after part, in one batch. With
/// `whole`, the snapshot it fetches whole, which is marked until the
/// batch is durable.
Expand Down Expand Up @@ -1013,6 +1050,7 @@ impl<'a> Sync<'a> {
{
self.check_shared(*id, revision, base)?;
}
self.check_writes(revision)?;
}

// Snapshots and attachments first, then revisions, then statements.
Expand Down Expand Up @@ -1265,6 +1303,15 @@ impl<'a> Sync<'a> {
Ok(())
}

/// Checks that `revision` has an effect the user may give it (see
/// [`Effects`]).
fn check_writes(&self, revision: &Revision) -> Result<(), SyncError> {
match Effects::new(self.store, self.store, &self.writes).check(revision)? {
Some(forbidden) => Err(forbidden.into()),
None => Ok(()),
}
}

/// `changes`, and every change below their revisions, following their
/// bases and dependencies.
fn stack(&self, index: &Index, changes: &[ChangeId]) -> Result<Vec<ChangeId>, SyncError> {
Expand Down Expand Up @@ -1366,6 +1413,9 @@ pub enum SyncError {
/// A snapshot was needed of a record that has none.
#[error("{0} has no snapshot")]
NoSnapshot(RecordId),
/// A push would change what the user may not.
#[error("the remote would refuse the push, as of your rights when you last fetched")]
Forbidden(#[from] Forbidden),
/// The change has no revision to push.
#[error("change {} has no revisions", .0.short())]
UnknownChange(ChangeId),
Expand Down Expand Up @@ -1442,7 +1492,7 @@ mod tests {

use origin_platform::Native;
use origin_shared::event::Filter;
use origin_shared::{AcceptanceBody, Kind, NodeKey, RepoPath, Verdict};
use origin_shared::{AcceptanceBody, Kind, NodeKey, Verdict};

use super::*;
use crate::events::Events;
Expand Down
Loading