Skip to content
Open
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
223 changes: 182 additions & 41 deletions apps/origin-src/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,35 @@
//! [retention]
//! "ci.*" = "90d"
//!
//! [[paths]]
//! [grants] # capabilities, but path access
//! "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" }
//! ```
//!
//! A target without a table of its own has the authority of `policy`, and
//! requires nothing. Every name a policy refers to must be defined in it,
//! and unknown keys are errors, so a typo never drops a requirement. Path
//! rules are parsed and checked, but not enforced yet.
//! and unknown keys are errors, so a typo never drops a requirement.
//!
//! What each caller may do, their [`Rights`], comes from the last two
//! tables. Under a path, the most specific `[[paths]]` rule that mentions
//! someone decides, and paths no rule mentions them for are writable. A
//! policy without `[grants]` lets everyone land on every target but
//! `policy` and issue every kind of statement. With `[grants]`, each has
//! only what it grants them. Only `admin` lands on `policy`, so no one
//! grants themselves rights they don't have, and no one is `admin` until
//! the policy says so: the node's own user, on its command line, is.

use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;
use std::time::Duration;

use origin_platform::Platform;
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,
Expand All @@ -56,6 +69,8 @@ pub struct Policy {
targets: BTreeMap<TargetName, TargetRules>,
/// How long payloads are kept.
pub retention: Retention,
/// `[grants]`, if the policy has the table.
grants: Option<Vec<(Grantee, Vec<Capability>)>>,
paths: Vec<PathRule>,
}

Expand All @@ -72,7 +87,7 @@ pub struct PathRule {
/// The file or directory the rule covers, with everything under it.
pub path: RepoPath,
/// What each grantee may do there.
pub grants: BTreeMap<Grantee, Access>,
pub grants: BTreeMap<Grantee, Level>,
}

/// Who a path rule grants access to.
Expand All @@ -84,17 +99,6 @@ pub enum Grantee {
Principals(Principals),
}

/// What a path rule grants.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Access {
/// Nothing, not even the content.
None,
/// Reading the content.
Read,
/// Proposing revisions that change it.
Write,
}

impl Policy {
/// The file holding the policy, at the root of the `policy` target.
pub const FILE: &str = "policy.toml";
Expand Down Expand Up @@ -231,6 +235,74 @@ impl Policy {
pub fn paths(&self) -> &[PathRule] {
&self.paths
}

/// Whether `grantee` covers `principal`, if there's one: `all` covers
/// anonymous callers too.
fn covers(&self, grantee: &Grantee, principal: Option<&Principal>) -> bool {
match (grantee, principal) {
(Grantee::All, _) => true,
(Grantee::Principals(principals), Some(principal)) => {
self.admits(principals, principal)
}
(Grantee::Principals(_), None) => false,
}
}

/// What `principal`, or an anonymous caller, may do, as the module
/// explains. Anonymous callers never write.
#[must_use]
pub fn rights(&self, principal: Option<&Principal>) -> Rights {
let mut rules: BTreeMap<RepoPath, Level> = BTreeMap::new();
for rule in &self.paths {
let granted = rule
.grants
.iter()
.filter(|(grantee, _)| self.covers(grantee, principal))
.map(|(_, &level)| level)
.max();
if let Some(level) = granted {
let at = rules.entry(rule.path.clone()).or_insert(level);
*at = (*at).max(level);
}
}
let mut paths = PathAccess::new(Level::Write, rules);
if principal.is_none() {
paths = paths.min(&PathAccess::uniform(Level::Read));
}
let mut rights = Rights {
principal: principal.cloned(),
groups: principal.map_or_else(Vec::new, |principal| {
self.groups
.iter()
.filter(|(_, members)| members.contains(principal))
.map(|(group, _)| group.clone())
.collect()
}),
scope: None,
paths,
annotate: vec![KindPattern::Any],
land: vec![Targets::Any],
admin: false,
};
if let Some(grants) = &self.grants {
rights.annotate.clear();
rights.land.clear();
let granted = grants
.iter()
.filter(|(grantee, _)| self.covers(grantee, principal))
.flat_map(|(_, capabilities)| capabilities);
for capability in granted {
match capability {
Capability::Annotate(kinds) => rights.annotate.push(kinds.clone()),
Capability::Land(targets) => rights.land.push(targets.clone()),
Capability::Admin => rights.admin = true,
// Refused when parsing: paths are in `[[paths]]`.
Capability::Read(_) | Capability::Write(_) => {}
}
}
}
rights
}
}

/// `policy.toml` as written, which [`Policy::try_from`] parses and checks.
Expand All @@ -245,6 +317,7 @@ struct PolicyFile {
targets: BTreeMap<String, TargetFile>,
#[serde(default)]
retention: Retention,
grants: Option<BTreeMap<String, Vec<String>>>,
#[serde(default)]
paths: Vec<PathRuleFile>,
}
Expand Down Expand Up @@ -317,6 +390,7 @@ impl TryFrom<PolicyFile> for Policy {
groups,
targets: BTreeMap::new(),
retention: file.retention,
grants: None,
paths: Vec::new(),
};
for (name, target) in file.targets {
Expand Down Expand Up @@ -346,18 +420,34 @@ impl TryFrom<PolicyFile> for Policy {
.targets
.insert(name, TargetRules { authority, require });
}
if let Some(grants) = file.grants {
let grants = grants
.into_iter()
.map(|(grantee, capabilities)| {
let grantee = policy.grantee(PolicyFile::parse("grantee", grantee)?)?;
let capabilities = capabilities
.into_iter()
.map(
|capability| match PolicyFile::parse("capability", capability)? {
Capability::Read(path) | Capability::Write(path) => {
Err(RuleError::PathGrant(path))
}
capability => Ok(capability),
},
)
.collect::<Result<_, _>>()?;
Ok((grantee, capabilities))
})
.collect::<Result<_, RuleError>>()?;
policy.grants = Some(grants);
}
for rule in file.paths {
let grants = rule
.grants
.into_iter()
.map(|(grantee, access)| {
let grantee = match PolicyFile::parse("grantee", grantee)? {
Grantee::Principals(principals) => {
Grantee::Principals(policy.check(principals)?)
}
Grantee::All => Grantee::All,
};
Ok((grantee, PolicyFile::parse("access", access)?))
.map(|(grantee, level)| {
let grantee = policy.grantee(PolicyFile::parse("grantee", grantee)?)?;
Ok((grantee, PolicyFile::parse("access", level)?))
})
.collect::<Result<_, RuleError>>()?;
policy.paths.push(PathRule {
Expand Down Expand Up @@ -392,10 +482,29 @@ impl Policy {
\n\
[targets.main]\n\
authority = \"node:{node}\"\n\
# require = [{{ kind = \"ci.test\", verdict = \"pass\", issuer = \"group:runners\" }}]\n"
# require = [{{ kind = \"ci.test\", verdict = \"pass\", issuer = \"group:runners\" }}]\n\
\n\
# Who may land, annotate, and change this policy (`admin`). Without\n\
# [grants], everyone lands and annotates, and no one is admin\n\
# remotely.\n\
# [grants]\n\
# \"user:alice\" = [\"admin\", \"land *\"]\n\
\n\
# Who may read and write paths: everyone writes everything by default.\n\
# [[paths]]\n\
# path = \"secrets\"\n\
# grants = {{ all = \"none\", \"user:alice\" = \"write\" }}\n"
)
}

/// Checks that the group `grantee` names, if any, is defined.
fn grantee(&self, grantee: Grantee) -> Result<Grantee, RuleError> {
match grantee {
Grantee::Principals(principals) => Ok(Grantee::Principals(self.check(principals)?)),
Grantee::All => Ok(Grantee::All),
}
}

/// Checks that the group `principals` names, if any, is defined.
fn check(&self, principals: Principals) -> Result<Principals, RuleError> {
match &principals {
Expand All @@ -421,19 +530,6 @@ impl FromStr for Grantee {
}
}

impl FromStr for Access {
type Err = String;

fn from_str(input: &str) -> Result<Self, Self::Err> {
match input {
"none" => Ok(Self::None),
"read" => Ok(Self::Read),
"write" => Ok(Self::Write),
_ => Err(format!("`{input}` is not `none`, `read` or `write`")),
}
}
}

/// An invalid rule in `policy.toml`.
#[derive(Debug, PartialEq, Eq, thiserror::Error)]
pub enum RuleError {
Expand Down Expand Up @@ -466,6 +562,9 @@ pub enum RuleError {
/// A rule names a group `[groups]` doesn't define.
#[error("group:{0} is not defined in [groups]")]
UnknownGroup(PrincipalName),
/// `[grants]` grants path access, which `[[paths]]` rules set.
#[error("[grants] cannot grant access to `{0}`: add a [[paths]] rule")]
PathGrant(RepoPath),
}

/// How long the payloads of each kind of statement are kept, measured from
Expand Down Expand Up @@ -621,11 +720,53 @@ mod tests {
[targets.main]\nauthority = \"node:hosted\"\nrequire = [\n\
{{ kind = \"ci.test\", verdict = \"pass\", issuer = \"group:runners\" }},\n\
{{ kind = \"review\", verdict = \"pass\", issuer = \"user:bob\" }},\n]\n\
[grants]\n\"group:maintainers\" = [\"admin\", \"land *\"]\n\
\"group:runners\" = [\"annotate ci.*\"]\nall = [\"land main\"]\n\
[[paths]]\npath = \"secrets\"\n\
grants = {{ all = \"none\", \"group:maintainers\" = \"write\" }}\n"
grants = {{ all = \"none\", \"group:maintainers\" = \"write\" }}\n\
[[paths]]\npath = \"/\"\n\
grants = {{ all = \"read\", \"node:ci-1\" = \"write\" }}\n"
)
}

#[test]
fn rights_come_from_grants_and_paths() {
let policy: Policy = toml::from_str(&full_policy()).unwrap();
let path = |path: &str| RepoPath::new(path).unwrap();
let alice = policy.rights(Some(&principal("user:alice")));
assert_eq!(alice.groups, [PrincipalName::new("maintainers").unwrap()]);
assert!(alice.admin && alice.may_land(&TargetName::POLICY));
assert!(alice.may_land(&TargetName::new("release").unwrap()));
assert!(!alice.may_annotate(&Kind::new("ci.test").unwrap()));
assert_eq!(alice.paths.level(&path("secrets/key")), Level::Write);
assert_eq!(alice.paths.level(&path("src")), Level::Read, "`all` at `/`");

let ci = policy.rights(Some(&principal("node:ci-1")));
assert!(ci.may_annotate(&Kind::new("ci.test").unwrap()));
assert!(ci.may_land(&TargetName::MAIN), "`all` lands on `main`");
assert!(!ci.may_land(&TargetName::POLICY) && !ci.admin);
assert_eq!(ci.paths.level(&path("src")), Level::Write);
assert_eq!(ci.paths.level(&path("secrets/key")), Level::None);

let anonymous = policy.rights(None);
assert_eq!(anonymous.paths.level(&path("src")), Level::Read);
assert!(!anonymous.paths.reads(&path("secrets")));

// Without rules, everyone writes everything and lands but on
// `policy`, and no one is admin.
let open = Policy::default().rights(Some(&principal("user:bob")));
assert!(open.paths.writes_all());
assert!(open.may_land(&TargetName::MAIN) && !open.admin);
assert!(open.may_annotate(&Kind::new("review").unwrap()));
assert!(!Policy::default().rights(None).paths.writes(&path("x")));

let error = toml::from_str::<Policy>("[grants]\nall = [\"write src\"]\n").unwrap_err();
assert!(
error.to_string().contains("add a [[paths]] rule"),
"{error}"
);
}

fn principal(principal: &str) -> Principal {
principal.parse().unwrap()
}
Expand Down Expand Up @@ -664,11 +805,11 @@ mod tests {
assert_eq!(policy.authority(&other), Some(&hosted));
assert_eq!(policy.requirements(&other), []);

let [rule] = policy.paths() else {
panic!("one path rule")
let [rule, _] = policy.paths() else {
panic!("two path rules")
};
assert_eq!(rule.path, RepoPath::new("secrets").unwrap());
assert_eq!(rule.grants.get(&Grantee::All), Some(&Access::None));
assert_eq!(rule.grants.get(&Grantee::All), Some(&Level::None));
}

#[test]
Expand Down
Loading