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
49 changes: 38 additions & 11 deletions apps/origin-src/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ impl Wakeup {
}
}

/// Which events a reader takes: those a [`Filter`] passes, or a closure
/// accepts, such as one that also checks what a caller sees.
pub trait Accept {
/// Whether the reader takes `event`.
///
/// # Errors
///
/// Fails if what decides cannot be read.
fn accept(&mut self, event: &EventKind) -> Result<bool, StoreError>;
}

impl Accept for &Filter {
fn accept(&mut self, event: &EventKind) -> Result<bool, StoreError> {
Ok(self.matches(event))
}
}

impl<F: FnMut(&EventKind) -> Result<bool, StoreError>> Accept for F {
fn accept(&mut self, event: &EventKind) -> Result<bool, StoreError> {
self(event)
}
}

impl<'a, P: Platform> Events<'a, P> {
/// How often [`Events::wait`] reads the op log.
pub const POLL: Duration = Duration::from_millis(50);
Expand Down Expand Up @@ -110,14 +133,19 @@ impl<'a, P: Platform> Events<'a, P> {
self.store.oplog().settled()
}

/// The events after `cursor` that pass `filter`, oldest first: those
/// The events after `cursor` that `accept` takes, oldest first: those
/// of whole entries, until there are `limit` or more.
///
/// # Errors
///
/// Fails with [`StoreError::Expired`] if the op log no longer keeps the
/// entries after `cursor`, or if it or a record cannot be read.
pub fn after(&self, cursor: u64, filter: &Filter, limit: usize) -> Result<Page, StoreError> {
pub fn after(
&self,
cursor: u64,
mut accept: impl Accept,
limit: usize,
) -> Result<Page, StoreError> {
let mut page = Page {
events: Vec::new(),
next: cursor,
Expand All @@ -131,15 +159,14 @@ impl<'a, P: Platform> Events<'a, P> {
let settled = oplog.settled()?;
let mut waiting = None;
for entry in read.entries.iter().take_while(|entry| entry.seq <= settled) {
page.events.extend(
self.derive(entry, &mut waiting)?
.into_iter()
.filter(|kind| filter.matches(kind))
.map(|kind| Event {
for kind in self.derive(entry, &mut waiting)? {
if accept.accept(&kind)? {
page.events.push(Event {
cursor: entry.seq,
kind,
}),
);
});
}
}
page.next = entry.seq;
if page.events.len() >= limit {
break;
Expand All @@ -158,7 +185,7 @@ impl<'a, P: Platform> Events<'a, P> {
pub fn wait(
&self,
cursor: u64,
filter: &Filter,
mut accept: impl Accept,
limit: usize,
timeout: Duration,
) -> Result<Page, StoreError> {
Expand All @@ -167,7 +194,7 @@ impl<'a, P: Platform> Events<'a, P> {
loop {
// Read before the op log, so no write after it is missed.
let seen = self.wakeup.map(Wakeup::writes);
let page = self.after(cursor, filter, limit)?;
let page = self.after(cursor, |event: &EventKind| accept.accept(event), limit)?;
let left = timeout.saturating_sub(start.elapsed());
if !page.events.is_empty() || left.is_zero() {
return Ok(page);
Expand Down
13 changes: 10 additions & 3 deletions apps/origin-src/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl utoipa::PartialSchema for Binary {

impl utoipa::ToSchema for Binary {}

/// Lists the repositories on the server.
/// Lists the repositories on the server that the caller sees.
#[utoipa::path(
get,
path = "/api/v1/repos",
Expand All @@ -186,8 +186,15 @@ impl utoipa::ToSchema for Binary {}
(status = 401, description = "The token is invalid or expired", body = ApiError)
)
)]
async fn list_repos(State(repositories): Repos, caller: Caller) -> Json<Vec<RepoSummary>> {
Json(repositories.list(&caller))
async fn list_repos(
State(repositories): Repos,
caller: Caller,
) -> Result<Json<Vec<RepoSummary>>, HttpError> {
tokio::task::spawn_blocking(move || repositories.list(&caller))
.await
.map_err(|error| HttpError::Panicked(error.to_string()))?
.map(Json)
.map_err(HttpError::Service)
}

/// A part of a fetch, as a response: its pack, with the [`MORE`] header if
Expand Down
237 changes: 232 additions & 5 deletions apps/origin-src/src/permissions.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
//! Who calls the service: the node's own user, an anonymous client, or a
//! principal the server [authenticated](crate::auth), whose token may
//! narrow their rights to its scope.
//! Enforcing what callers may do, as their
//! [`Rights`](origin_shared::access::Rights) say: what they read
//! of a repository's records.
//!
//! **Reading.** A caller who reads no path doesn't see the repository at
//! all. Otherwise they see its targets and acceptances, every revision that
//! changes a path they read, or changes nothing, and the statements about
//! what they see. Snapshots withhold the content of the paths they don't
//! read (see [`Store::added`]).

use origin_shared::Principal;
use origin_shared::access::Capability;
use std::collections::HashMap;

use origin_platform::Platform;
use origin_shared::access::{Capability, PathAccess};
use origin_shared::event::EventKind;
use origin_shared::{Principal, RecordId, RepoPath, Revision, RevisionId};

use crate::store::{Store, StoreError};

/// Who calls the service.
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -43,6 +55,89 @@ impl Caller {
}
}

/// What a caller sees of a repository's records, remembering what it
/// decided.
pub struct Reader<'a, P> {
store: &'a Store<P>,
/// What the caller may do with each path.
paths: &'a PathAccess,
seen: HashMap<RecordId, bool>,
}

impl<'a, P: Platform> Reader<'a, P> {
/// What a caller who may do `paths` sees of `store`.
pub fn new(store: &'a Store<P>, paths: &'a PathAccess) -> Self {
Self {
store,
paths,
seen: HashMap::new(),
}
}

/// Whether the caller sees the repository: they read some path.
#[must_use]
pub fn sees_repository(&self) -> bool {
self.paths.reads_under(&RepoPath::root())
}

/// Whether the caller sees every record.
fn sees_all(&self) -> bool {
self.paths.reads_all()
}

/// Whether the caller sees `record`, as the module explains. A record
/// this node lacks, or whose base it lacks, is not seen.
///
/// # Errors
///
/// Fails if a record or tree cannot be read.
pub fn sees(&mut self, record: RecordId) -> Result<bool, StoreError> {
if self.sees_all() {
return Ok(true);
}
if !self.sees_repository() {
return Ok(false);
}
if let Some(&seen) = self.seen.get(&record) {
return Ok(seen);
}
let seen = match record {
RecordId::Revision(id) => self.sees_revision(id),
RecordId::Statement(id) => match self.store.get(id) {
Ok(statement) => self.sees(statement.subject),
Err(error) => Err(error),
},
RecordId::Acceptance(_) | RecordId::Snapshot(_) => Ok(true),
};
let seen = match seen {
Err(StoreError::NotFound(_)) => false,
seen => seen?,
};
self.seen.insert(record, seen);
Ok(seen)
}

fn sees_revision(&self, id: RevisionId) -> Result<bool, StoreError> {
let revision: Revision = self.store.get(id)?;
let changes = self
.store
.diff(self.store.base_snapshot(revision.base)?, revision.snapshot)?;
Ok(changes.is_empty() || changes.iter().any(|change| self.paths.reads(&change.path)))
}

/// Whether the caller sees what `event` is about.
///
/// # Errors
///
/// Fails like [`Reader::sees`].
pub fn sees_event(&mut self, event: &EventKind) -> Result<bool, StoreError> {
match event {
EventKind::CheckRequested { subject, .. } => self.sees(*subject),
event => self.sees(event.record()),
}
}
}

/// Something a caller may not do.
#[derive(Debug, PartialEq, Eq, thiserror::Error)]
pub enum Forbidden {
Expand All @@ -53,3 +148,135 @@ pub enum Forbidden {
#[error("a token may only narrow your rights, which don't include `{0}`")]
Scope(Capability),
}

#[cfg(test)]
mod tests {
use std::sync::Arc;

use origin_shared::{NodeKey, TargetName};

use super::*;
use crate::queue::tests::Repo;
use crate::service::{Node, OriginService, ServiceError};

/// Ada writes everything and changes the rules. Eve reads all but
/// `secrets`, writes only `src`, and issues reviews and checks. Both
/// land on `main`.
const RULES: &str = r#"
[targets.main]
authority = "node:queue"

[grants]
"user:ada" = ["admin", "land *"]
"user:eve" = ["land *", "annotate review", "annotate ci.*"]

[[paths]]
path = "/"
grants = { all = "read", "user:ada" = "write" }

[[paths]]
path = "src"
grants = { "user:eve" = "write" }

[[paths]]
path = "secrets"
grants = { all = "none", "user:ada" = "write" }
"#;

/// The repository, with `main` holding a README, a library, a secret
/// and a protected file, and its service.
struct Fixture {
repo: Repo,
service: OriginService,
}

impl Fixture {
fn new() -> Self {
let repo = Repo::new(RULES);
repo.change_on_main(
1,
&[
("README.md", "hello"),
("src/lib.rs", "fn a() {}"),
("secrets/key", "hunter2"),
("protected/file", "keep"),
],
);
repo.land(1);
let node = Node {
name: "queue".parse().unwrap(),
key: Arc::new(NodeKey::from_bytes(&[7; 32])),
};
let service = OriginService::new(repo.store.clone(), node);
Self { repo, service }
}
}

fn caller(principal: &str) -> Caller {
Caller::Principal {
principal: principal.parse().unwrap(),
scope: None,
}
}

/// `caller`, with a token narrowing them to `scope`, expiring at
/// `expires`.
fn scoped(principal: &str, scope: &[&str], expires: u64) -> Caller {
Caller::Principal {
principal: principal.parse().unwrap(),
scope: Some(Scope {
capabilities: scope.iter().map(|c| c.parse().unwrap()).collect(),
expires,
}),
}
}

/// A week from now, in seconds since the Unix epoch.
fn in_a_week() -> u64 {
let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH);
(now.unwrap() + OriginService::MAX_TOKEN_TTL).as_secs()
}

#[test]
fn callers_who_read_nothing_see_nothing() {
let fixture = Fixture::new();
let revision = fixture.repo.change_on_main(2, &[]);
let blind = scoped("user:eve", &["annotate review"], in_a_week());
let service = &fixture.service;
assert!(!service.sees_repository(&blind).unwrap());
let records = service.records(&blind, &[revision.into()]).unwrap();
assert!(
records.pack.objects.is_empty(),
"not even a revision changing nothing"
);
assert_eq!(service.targets(&blind).unwrap(), []);
let log = origin_shared::api::LogQuery {
target: TargetName::MAIN,
since: 0,
limit: None,
};
assert!(matches!(
service.log(&blind, &log),
Err(ServiceError::UnknownTarget(_))
));
assert_eq!(service.log(&caller("user:eve"), &log).unwrap().len(), 2);
}

#[test]
fn tokens_issued_from_a_token_expire_with_it() {
let fixture = Fixture::new();
let expires = in_a_week() - 6 * 24 * 3600;
let parent = scoped("user:eve", &["annotate review", "land *"], expires);
let request = origin_shared::api::TokenRequest {
scope: vec!["annotate review".parse().unwrap()],
ttl: origin_shared::Period(OriginService::MAX_TOKEN_TTL),
};
let issued = fixture.service.issue_token(&parent, &request).unwrap();
assert_eq!(issued.expires, expires);
let issued = fixture
.service
.issue_token(&caller("user:eve"), &request)
.unwrap();
assert!(issued.expires > expires, "a static token's last a week");
}
}
Loading