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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ change), and appends an acceptance signed by your node, which must be the
target's authority. A landing that conflicts or lacks evidence waits: issue
the statements, or a new revision, and run `origin land` again. Policy is a
target too: edit it with `origin new --on policy`, then `origin land`.
Issuing a statement a waiting landing lacks, to the repository that orders
its target, lets it continue: the queue retries it for whoever submitted it.

Workspaces work like git worktrees: each has its own current change and
shares the repository's records, so agents can work side by side. A new
Expand Down Expand Up @@ -178,7 +180,10 @@ for a token acting for you with a narrower scope, such as `write src`,
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.
7 days, and a token issued with a token expires with it. A landing that
waits is retried for its submitter when its evidence arrives, until their
token expires: then `origin land --status` says so, and it waits for
someone to land it again.

`converge` merges the files of a change's heads. A text file two heads
changed holds both versions between conflict markers (`<<<<<<<`, `=======`,
Expand Down
12 changes: 12 additions & 0 deletions apps/origin-src/src/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ pub struct Scope {
}

impl Caller {
/// When the caller's token expires, in seconds since the Unix epoch, if
/// it's a scoped one.
#[must_use]
pub fn expires(&self) -> Option<u64> {
match self {
Self::Principal {
scope: Some(scope), ..
} => Some(scope.expires),
Self::Principal { scope: None, .. } | Self::Owner | Self::Anonymous => None,
}
}

/// Who the caller is, unless the owner or anonymous.
#[must_use]
pub fn principal(&self) -> Option<&Principal> {
Expand Down
57 changes: 42 additions & 15 deletions apps/origin-src/src/queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
//! with the same input digest are promoted to it first, and a valid
//! promotion claims what its original does, as its original's issuer.
//! 4. **Missing evidence** leaves the landing waiting, until the statements
//! are issued and the landing is retried.
//! are issued and the landing is retried, for its submitter, under the
//! policy in force then: the [service](crate::service) retries it
//! whenever statements about its subject arrive.
//! 5. **Otherwise** this node signs an acceptance naming what landed, the
//! evidence and the policy, and appends it to the target's chain.
//!
Expand All @@ -43,8 +45,8 @@
//!
//! The queue of each target lives in `queue/<target id>/`: a `lock` held
//! while landing, so landings on one target never race, and a file for each
//! landing that waits, `change-<id>` or `target-<target id>`, so waiting
//! landings can be listed and retried.
//! landing that waits, `change-<id>` or `target-<target id>`, naming its
//! submitter, so waiting landings can be listed and retried.

mod pending;
mod session;
Expand All @@ -64,7 +66,7 @@ use origin_shared::{

use crate::annotations::{AnnotateError, Annotations};
use crate::conflict::Conflicts;
use crate::permissions::Forbidden;
use crate::permissions::{Caller, Forbidden};
use crate::policy::{Policy, PolicyError};
use crate::store::{Store, StoreError};

Expand All @@ -77,8 +79,19 @@ pub struct Queue<'a, P = Native> {
store: &'a Store<P>,
key: &'a NodeKey,
node: PrincipalName,
/// Who submits the landings, which a waiting one is retried for.
submitter: Caller,
/// What the submitter may do with each path.
submitter: PathAccess,
paths: PathAccess,
}

/// A landing that waits, and who submitted it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Waiting {
/// The landing.
pub pending: Pending,
/// Its submitter, whose rights it lands with when it's retried.
pub submitter: Caller,
}

impl<'a, P: Platform> Queue<'a, P> {
Expand All @@ -89,16 +102,18 @@ impl<'a, P: Platform> Queue<'a, P> {
store,
key,
node,
submitter: PathAccess::uniform(Level::Write),
submitter: Caller::Owner,
paths: PathAccess::uniform(Level::Write),
}
}

/// The queues, landing for a submitter who may do `paths`: everything,
/// by default.
/// The queues, landing for `submitter`, who may do `paths`: the owner,
/// who may do everything, by default.
#[must_use]
pub fn submitted_by(self, paths: PathAccess) -> Self {
pub fn submitted_by(self, submitter: Caller, paths: PathAccess) -> Self {
Self {
submitter: paths,
submitter,
paths,
..self
}
}
Expand Down Expand Up @@ -297,9 +312,21 @@ impl<'a, P: Platform> Queue<'a, P> {
///
/// # Errors
///
/// Fails like [`Queue::waiting`].
pub fn pending(store: &Store<P>) -> Result<Vec<Pending>, StoreError> {
Ok(Self::waiting(store)?
.into_iter()
.map(|waiting| waiting.pending)
.collect())
}

/// Every landing that waits, with its submitter, sorted by target.
///
/// # Errors
///
/// Fails if the queues cannot be listed, or a landing cannot be read or
/// is malformed.
pub fn pending(store: &Store<P>) -> Result<Vec<Pending>, StoreError> {
pub fn waiting(store: &Store<P>) -> Result<Vec<Waiting>, StoreError> {
let root = store.queue_dir();
let list = |dir: &PathBuf| match fs::read_dir(dir) {
Ok(entries) => entries
Expand All @@ -322,15 +349,15 @@ impl<'a, P: Platform> Queue<'a, P> {
Err(error) if error.kind() == io::ErrorKind::NotFound => continue,
Err(source) => return Err(StoreError::io(&path, source)),
};
let mut landing = PendingFile::decode(&text)
let mut waiting = PendingFile::decode(&text)
.ok_or_else(|| StoreError::Malformed(path.clone()))?;
if let Blocked::Conflict { statement, paths } = &mut landing.blocked {
if let Blocked::Conflict { statement, paths } = &mut waiting.pending.blocked {
*paths = Conflicts::listed(store, &store.get(*statement)?)?.paths();
}
pending.push(landing);
pending.push(waiting);
}
}
pending.sort_by(|a, b| a.onto.cmp(&b.onto));
pending.sort_by(|a, b| a.pending.onto.cmp(&b.pending.onto));
Ok(pending)
}

Expand Down
62 changes: 52 additions & 10 deletions apps/origin-src/src/queue/pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ use origin_shared::RecordId;
use origin_shared::landing::{Blocked, Pending, Proposal};
use origin_shared::policy::Requirement;

use crate::permissions::{Caller, Scope};

use super::Waiting;

/// The format of the files keeping waiting landings.
pub(super) struct PendingFile;

Expand All @@ -13,10 +17,13 @@ impl PendingFile {
const HEADER: &str = "origin pending 1";

/// Encodes the landing as lines: the header, `onto <target>`, `change
/// <id>` or `target <name>`, `subject <kind> <id>`, then a `missing
/// <kind> <verdict> <issuer>` line for each requirement it lacks, or
/// `conflict <statement>`, whose payload lists the paths.
pub(super) fn encode(pending: &Pending) -> String {
/// <id>` or `target <name>`, `subject <kind> <id>`, `by owner` or `by
/// <principal>` for its submitter, then if their token is scoped, an
/// `expires <seconds>` line and a `scope <capability>` line for each of
/// its capabilities, then a
/// `missing <kind> <verdict> <issuer>` line for each requirement it
/// lacks, or `conflict <statement>`, whose payload lists the paths.
pub(super) fn encode(pending: &Pending, submitter: &Caller) -> String {
let mut lines = vec![Self::HEADER.to_owned(), format!("onto {}", pending.onto)];
lines.push(match &pending.proposal {
Proposal::Change(change) => format!("change {change}"),
Expand All @@ -29,6 +36,18 @@ impl PendingFile {
RecordId::Acceptance(id) => ("acceptance", id.to_hex()),
};
lines.push(format!("subject {kind} {id}"));
match submitter {
Caller::Principal { principal, scope } => {
lines.push(format!("by {principal}"));
if let Some(scope) = scope {
lines.push(format!("expires {}", scope.expires));
let capabilities = scope.capabilities.iter();
lines.extend(capabilities.map(|capability| format!("scope {capability}")));
}
}
// Anonymous callers never land.
Caller::Owner | Caller::Anonymous => lines.push("by owner".to_owned()),
}
match &pending.blocked {
Blocked::Evidence(missing) => lines.extend(missing.iter().map(|requirement| {
format!(
Expand All @@ -43,7 +62,7 @@ impl PendingFile {

/// Decodes what [`PendingFile::encode`] wrote, without the conflicting
/// paths, or `None` if it's malformed.
pub(super) fn decode(text: &str) -> Option<Pending> {
pub(super) fn decode(text: &str) -> Option<Waiting> {
let mut lines = text.lines();
if lines.next()? != Self::HEADER {
return None;
Expand All @@ -61,10 +80,18 @@ impl PendingFile {
("acceptance", id) => RecordId::Acceptance(id.parse().ok()?),
_ => return None,
};
let principal = match lines.next()?.strip_prefix("by ")? {
"owner" => None,
principal => Some(principal.parse().ok()?),
};
let mut expires = None;
let mut capabilities = Vec::new();
let mut missing = Vec::new();
let mut conflict = None;
for line in lines {
match line.split_once(' ')? {
("expires", at) => expires = Some(at.parse().ok()?),
("scope", capability) => capabilities.push(capability.parse().ok()?),
("missing", requirement) => {
let mut fields = requirement.split(' ');
missing.push(Requirement {
Expand All @@ -84,11 +111,26 @@ impl PendingFile {
},
None => Blocked::Evidence(missing),
};
Some(Pending {
onto,
proposal,
subject,
blocked,
if expires.is_none() && !capabilities.is_empty() {
return None;
}
let scope = expires.map(|expires| Scope {
capabilities,
expires,
});
let submitter = match principal {
Some(principal) => Caller::Principal { principal, scope },
None => Caller::Owner,
};
Some(Waiting {
pending: Pending {
onto,
proposal,
subject,
blocked,
expires,
},
submitter,
})
}
}
10 changes: 6 additions & 4 deletions apps/origin-src/src/queue/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ impl<P: Platform> Session<'_, '_, P> {
if !candidate.trees.is_empty() {
store.write_all(&candidate.trees)?;
}
if !self.queue.submitter.writes_all() {
if !self.queue.paths.writes_all() {
for change in store.diff(self.state, candidate.state)? {
if !self.queue.submitter.writes(&change.path) {
if !self.queue.paths.writes(&change.path) {
return Err(Forbidden::Write(change.path).into());
}
}
Expand Down Expand Up @@ -229,7 +229,7 @@ impl<P: Platform> Session<'_, '_, P> {
store.write_all(&records)?;
records.clear();
// Only from what the submitter sees.
let mut reader = Reader::new(store, &self.queue.submitter);
let mut reader = Reader::new(store, &self.queue.paths);
let promotions = self.annotations.promotions(
candidate.subject,
&self.queue.principal(),
Expand Down Expand Up @@ -257,6 +257,7 @@ impl<P: Platform> Session<'_, '_, P> {
proposal: candidate.kept_as,
subject: candidate.subject,
blocked: Blocked::Evidence(missing),
expires: self.queue.submitter.expires(),
};
self.keep(&pending)?;
return Ok(Landing::Pending(pending));
Expand Down Expand Up @@ -340,6 +341,7 @@ impl<P: Platform> Session<'_, '_, P> {
statement,
paths: conflicts.paths(),
},
expires: self.queue.submitter.expires(),
};
self.keep(&pending)?;
Ok(Landing::Pending(pending))
Expand All @@ -360,7 +362,7 @@ impl<P: Platform> Session<'_, '_, P> {
let write = self.store().durable_write();
write.stage(
self.pending_path(&pending.proposal)?,
PendingFile::encode(pending).as_bytes(),
PendingFile::encode(pending, &self.queue.submitter).as_bytes(),
)?;
let waiting = Effect::Waiting {
onto: pending.onto.clone(),
Expand Down
33 changes: 27 additions & 6 deletions apps/origin-src/src/queue/tests/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,15 @@ fn pending_landings_round_trip() {
verdict: Verdict::Pass,
issuer: "group:runners".parse().unwrap(),
}]),
expires: None,
};
let owner = Waiting {
pending: pending.clone(),
submitter: Caller::Owner,
};
assert_eq!(
PendingFile::decode(&PendingFile::encode(&pending)),
Some(pending.clone())
PendingFile::decode(&PendingFile::encode(&pending, &Caller::Owner)),
Some(owner)
);
let conflict = Pending {
proposal: change(9),
Expand All @@ -183,10 +188,26 @@ fn pending_landings_round_trip() {
},
..pending
};
assert_eq!(
PendingFile::decode(&PendingFile::encode(&conflict)),
Some(conflict)
);
let scoped = |capabilities: &[&str]| Waiting {
pending: Pending {
expires: Some(1_900_000_000),
..conflict.clone()
},
submitter: Caller::Principal {
principal: "user:agent".parse().unwrap(),
scope: Some(Scope {
capabilities: capabilities.iter().map(|c| c.parse().unwrap()).collect(),
expires: 1_900_000_000,
}),
},
};
// A token scoped to nothing stays scoped.
for scoped in [scoped(&["write src", "land *"]), scoped(&[])] {
assert_eq!(
PendingFile::decode(&PendingFile::encode(&scoped.pending, &scoped.submitter)),
Some(scoped)
);
}
assert_eq!(PendingFile::decode("origin pending 2\n"), None);
}

Expand Down
8 changes: 7 additions & 1 deletion apps/origin-src/src/queue/tests/evidence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,13 @@ fn promotes_only_what_the_submitter_sees() {
let blind = PathAccess::new(Level::Write, [(secrets, Level::None)]);
let waiting = repo
.queue()
.submitted_by(blind)
.submitted_by(
Caller::Principal {
principal: "user:eve".parse().unwrap(),
scope: None,
},
blind,
)
.land_change(
&mut repo.annotations(),
ChangeId::from_bytes([2; 16]),
Expand Down
1 change: 1 addition & 0 deletions apps/origin-src/src/queue/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use origin_shared::{
use super::*;
use crate::annotations::Annotations;
use crate::conflict::Conflicts;
use crate::permissions::{Caller, Scope};
use crate::policy::{Policy, PolicyError};
use crate::store::Store;
use crate::tree::tests::build;
Expand Down
Loading