From fa28789a115b5bd99d031256c60b4e6782052d1f Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 16:03:51 +0100 Subject: [PATCH] feat(policy): let admins abandon changes Only a change's creator could abandon it, so a change nobody else may give up could only stay in the way, or wait for its creator. `core.abandon` now also counts from an `admin` of the policy in force: `Policy::authorizes` adds them to what core allows, and the landing queue, `origin abandon` and `origin show` all count abandonments by it. Who may issue a reserved kind is decided from a `RevisionView`, where the revisions it needs are read, so more than a store can answer it. --- apps/origin-src/src/annotations/mod.rs | 42 +++++++++++++++++----- apps/origin-src/src/policy.rs | 38 ++++++++++++++++++-- apps/origin-src/src/queue/session.rs | 4 ++- apps/origin-src/src/queue/tests/changes.rs | 25 +++++++++++-- apps/origin-src/src/workspace/mod.rs | 23 ++++++++---- apps/origin-src/tests/cli.rs | 15 +++++++- 6 files changed, 125 insertions(+), 22 deletions(-) diff --git a/apps/origin-src/src/annotations/mod.rs b/apps/origin-src/src/annotations/mod.rs index 4031c1ca..b3ede0d2 100644 --- a/apps/origin-src/src/annotations/mod.rs +++ b/apps/origin-src/src/annotations/mod.rs @@ -3,7 +3,7 @@ //! //! Core interprets only the reserved kinds: [`Kind::ABANDON`], //! [`Kind::CONFLICT`] and [`Kind::DIVERGENT`], and only from the issuers -//! [`Store::authorizes`] names. Every other kind belongs to whoever issues +//! [`RevisionView::authorizes`] names. Every other kind belongs to whoever issues //! it. Supersession counts only within one issuer: a statement claiming to //! supersede another issuer's is issued like any other, and the one it //! names still stands. [`Annotations::supersede`] supersedes an issuer's @@ -18,7 +18,8 @@ use std::collections::HashSet; use origin_platform::{Native, Platform}; use origin_shared::api::Annotation; use origin_shared::{ - ChangeId, EncodedObject, Kind, Principal, Record, RecordId, RevisionId, Statement, StatementId, + ChangeId, EncodedObject, Kind, Principal, Record, RecordId, Revision, RevisionId, Statement, + StatementId, }; use crate::index::Index; @@ -282,13 +283,23 @@ impl<'a, P: Platform> Annotations<'a, P> { } } -impl Store

{ +/// Where the revisions are read that decide who may issue a reserved kind: +/// a store. +pub trait RevisionView { + /// The revision `id`. + /// + /// # Errors + /// + /// Fails if it cannot be found or read. + fn revision(&self, id: RevisionId) -> Result; + /// Whether `statement`'s issuer may issue a statement of its kind, if /// that kind is reserved: /// /// - `core.abandon`, about a revision, only the change's creator: the /// author of its first revision, so no one who took it over, or - /// rebased it, gives it up for them; + /// rebased it, gives it up for them; or an `admin`, which + /// [`Policy::authorizes`](crate::policy::Policy::authorizes) adds; /// - `core.conflict`, a node, as a landing queue that could not rebase /// the record, or the author of the revision it's about, listing /// the conflicts its files still have; @@ -299,8 +310,8 @@ impl Store

{ /// /// # Errors /// - /// Fails if a revision cannot be read. - pub fn authorizes(&self, statement: &Statement) -> Result { + /// Fails if the revision it's about cannot be read. + fn authorizes(&self, statement: &Statement) -> Result { let issuer = &statement.issuer; if !statement.kind.is_reserved() { return Ok(true); @@ -316,7 +327,7 @@ impl Store

{ Ok(if *kind == Kind::ABANDON { self.created(subject, issuer)? } else if *kind == Kind::CONFLICT { - is_node || self.get(subject)?.author == *issuer + is_node || self.revision(subject)?.author == *issuer } else { *kind == Kind::DIVERGENT && is_node }) @@ -325,6 +336,11 @@ impl Store

{ /// Whether `author` created the change of `revision`: made each /// revision it evolves from that evolves from none. Records being /// immutable, there is always one, and no one adds another later. + /// History this view lacks leaves it unknown: no. + /// + /// # Errors + /// + /// Fails if a revision cannot be read. fn created(&self, revision: RevisionId, author: &Principal) -> Result { let mut seen = HashSet::new(); let mut next = vec![revision]; @@ -332,7 +348,11 @@ impl Store

{ if !seen.insert(id) { continue; } - let revision = self.get(id)?; + let revision = match self.revision(id) { + Ok(revision) => revision, + Err(StoreError::NotFound(_)) => return Ok(false), + Err(error) => return Err(error), + }; if revision.predecessors.is_empty() && revision.author != *author { return Ok(false); } @@ -342,6 +362,12 @@ impl Store

{ } } +impl RevisionView for Store

{ + fn revision(&self, id: RevisionId) -> Result { + self.get(id) + } +} + /// A failure to issue a statement. #[derive(Debug, thiserror::Error)] pub enum AnnotateError { diff --git a/apps/origin-src/src/policy.rs b/apps/origin-src/src/policy.rs index da61253a..9f380688 100644 --- a/apps/origin-src/src/policy.rs +++ b/apps/origin-src/src/policy.rs @@ -53,11 +53,12 @@ use origin_shared::access::{Capability, Level, PathAccess, Rights, Targets}; use origin_shared::policy::{KindPattern, Principals, Requirement}; use origin_shared::{ AcceptanceBody, AcceptanceId, EncodedObject, EntryKind, EntryName, InvalidName, InvalidPeriod, - Kind, NodePublicKey, Period, Principal, PrincipalName, Record, RepoPath, TargetName, Tree, - TreeEntry, TreeId, + Kind, NodePublicKey, Period, Principal, PrincipalName, Record, RepoPath, Statement, TargetName, + Tree, TreeEntry, TreeId, }; use serde::Deserialize; +use crate::annotations::RevisionView; use crate::store::{Store, StoreError}; /// The policy of a repository. @@ -248,6 +249,39 @@ impl Policy { } } + /// Whether `statement`'s issuer may issue a statement of its kind: as + /// [`Store::authorizes`] says, or for `core.abandon`, if the policy + /// makes them `admin`. + /// + /// # Errors + /// + /// Fails if a revision cannot be read. + pub fn authorizes( + &self, + revisions: &impl RevisionView, + statement: &Statement, + ) -> Result { + let admin = self.rights(Some(&statement.issuer)).admin; + Self::authorizes_admin(revisions, statement, admin) + } + + /// [`Policy::authorizes`], for an issuer who is `admin` or not: as the + /// token they put the statement with narrows their rights, say. + /// + /// # Errors + /// + /// Fails if a revision cannot be read. + pub fn authorizes_admin( + revisions: &impl RevisionView, + statement: &Statement, + admin: bool, + ) -> Result { + if statement.kind == Kind::ABANDON && statement.promoted_from.is_none() && admin { + return Ok(true); + } + revisions.authorizes(statement) + } + /// What `principal`, or an anonymous caller, may do, as the module /// explains. Anonymous callers never write. #[must_use] diff --git a/apps/origin-src/src/queue/session.rs b/apps/origin-src/src/queue/session.rs index 369e244b..01faff2a 100644 --- a/apps/origin-src/src/queue/session.rs +++ b/apps/origin-src/src/queue/session.rs @@ -93,7 +93,9 @@ impl Session<'_, '_, P> { let claims = self.annotations.claims(RecordId::Revision(head))?; for claim in &claims { let statement = claim.statement(); - if statement.kind == Kind::ABANDON && self.store().authorizes(statement)? { + if statement.kind == Kind::ABANDON + && self.policy.authorizes(self.store(), statement)? + { return Err(LandError::Abandoned(change)); } } diff --git a/apps/origin-src/src/queue/tests/changes.rs b/apps/origin-src/src/queue/tests/changes.rs index e7ab48b0..28769a43 100644 --- a/apps/origin-src/src/queue/tests/changes.rs +++ b/apps/origin-src/src/queue/tests/changes.rs @@ -192,9 +192,13 @@ fn pending_landings_round_trip() { #[test] fn only_the_creator_of_a_change_abandons_it() { - let repo = Repo::new("[targets.main]\nauthority = \"node:queue\"\n"); + let repo = Repo::new( + "[targets.main]\nauthority = \"node:queue\"\n\ + [grants]\nall = [\"land *\", \"annotate *\"]\n\"user:root\" = [\"admin\"]\n", + ); let revision = repo.change_on_main(1, &[("file", "work")]); - let abandon = |issuer: &str| Statement { + let given_up = repo.change_on_main(2, &[("other", "work")]); + let abandon = |issuer: &str, revision| Statement { subject: RecordId::Revision(revision), kind: Kind::ABANDON, verdict: None, @@ -205,9 +209,24 @@ fn only_the_creator_of_a_change_abandons_it() { promoted_from: None, }; repo.store - .write_all(&[abandon("user:eve").encode()]) + .write_all(&[ + abandon("user:eve", revision).encode(), + abandon("user:root", given_up).encode(), + ]) .unwrap(); assert!(matches!(repo.land(1)[..], [Landing::Landed { .. }])); + let error = repo + .queue() + .land_change( + &mut repo.annotations(), + ChangeId::from_bytes([2; 16]), + &TargetName::MAIN, + ) + .unwrap_err(); + assert!( + matches!(error, LandError::Abandoned(_)), + "an admin may: {error}" + ); } #[test] diff --git a/apps/origin-src/src/workspace/mod.rs b/apps/origin-src/src/workspace/mod.rs index 69d3b85b..9439fb83 100644 --- a/apps/origin-src/src/workspace/mod.rs +++ b/apps/origin-src/src/workspace/mod.rs @@ -1127,10 +1127,10 @@ impl Workspace { /// /// # Errors /// - /// Fails if the change cannot be found or diverged, the user didn't - /// create it, so readers would ignore the statement, or the records - /// cannot be written. Then no abandonment is written and nothing is - /// checked out. + /// Fails if the change cannot be found or diverged, the user neither + /// created it nor is an `admin`, so readers would ignore the statement, + /// or the records cannot be written. Then no abandonment is written and + /// nothing is checked out. pub fn abandon(&mut self, change: Option<&str>, reason: &str) -> Result<(), WorkspaceError> { let mut working_copy = self.refresh()?; let index = &working_copy.index; @@ -1150,7 +1150,8 @@ impl Workspace { supersedes: None, promoted_from: None, }; - if !self.store.authorizes(&statement)? { + // As readers count it. + if !Policy::load(&self.store)?.authorizes(&self.store, &statement)? { return Err(AnnotateError::Reserved { kind: statement.kind, issuer: statement.issuer, @@ -1476,16 +1477,24 @@ impl Workspace { } /// Who abandoned a revision, and why: only those who may, as - /// [`Store::authorizes`] says. + /// [`Policy::authorizes`] says, as the landing queue counts them. fn abandonments( &self, index: &Index, revision: RevisionId, ) -> Result, WorkspaceError> { let mut abandonments = Vec::new(); + let mut policy = None; for &id in index.statements(RecordId::Revision(revision)) { let statement = self.store.get(id)?; - if statement.kind == Kind::ABANDON && self.store.authorizes(&statement)? { + if statement.kind != Kind::ABANDON { + continue; + } + let policy = match &policy { + Some(policy) => policy, + None => policy.insert(Policy::load(&self.store)?), + }; + if policy.authorizes(&self.store, &statement)? { let reason = match statement.payload { Some(payload) => self.text(payload)?, None => String::new(), diff --git a/apps/origin-src/tests/cli.rs b/apps/origin-src/tests/cli.rs index 2f961ad4..dc4e4e8d 100644 --- a/apps/origin-src/tests/cli.rs +++ b/apps/origin-src/tests/cli.rs @@ -517,7 +517,7 @@ fn init_with_a_malformed_key_leaves_no_repository() { } #[test] -fn only_the_creator_of_a_change_abandons_it() { +fn only_the_creator_of_a_change_or_admins_abandon_it() { let repo = Repo::new(); repo.write("file", "ada's\n"); repo.ok(&["describe", "Ada's change"]); @@ -540,6 +540,19 @@ fn only_the_creator_of_a_change_abandons_it() { assert_eq!(repo.ok(&["statements"]), statements); fs::write(&config, "user = \"ada\"\n").unwrap(); assert!(!repo.ok(&["show", &change]).contains("bandoned")); + + // Once the policy makes eve an admin, she may. + repo.ok(&["new", "--on", "policy"]); + let policy = repo + .read("policy.toml") + .replace("\n# [grants]\n", "\n[grants]\n\"user:eve\" = [\"admin\"]\n"); + repo.write("policy.toml", &policy); + repo.ok(&["land"]); + fs::write(&config, "user = \"eve\"\n").unwrap(); + repo.ok(&["abandon", &change, "--reason", "Not mine"]); + fs::write(&config, "user = \"ada\"\n").unwrap(); + let show = repo.ok(&["show", &change]); + assert!(show.contains("Abandoned by user:eve: Not mine\n"), "{show}"); } #[test]