use std::fmt;
use ostrya_core::RefPattern;
use super::Namespace;
use super::rights::{AccessRules, AllowItem, parse_allow_item, parse_deny_item};
pub const MAX_CLAIMS: usize = 256;
const SCHEME: &str = "ostrya:";
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ClaimRule {
Allow(AllowItem),
Deny(RefPattern),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Claim {
pub namespace: Namespace,
pub rule: ClaimRule,
}
impl Claim {
pub fn parse(uri: &str) -> Option<Result<Claim, ClaimError>> {
if !is_claim(uri) {
return None;
}
Some(parse_claim(uri).map_err(|reason| ClaimError::Malformed {
claim: uri.to_owned(),
reason,
}))
}
pub fn parse_ref(text: &str) -> Result<Claim, ClaimError> {
parse_body(text).map_err(|reason| ClaimError::Malformed {
claim: text.to_owned(),
reason,
})
}
}
fn is_claim(uri: &str) -> bool {
uri.get(..SCHEME.len())
.is_some_and(|scheme| scheme.eq_ignore_ascii_case(SCHEME))
}
fn parse_claim(uri: &str) -> Result<Claim, String> {
const FORMS: &str = "a claim starts with ostrya:ref:";
let body = uri
.strip_prefix(SCHEME)
.and_then(|rest| rest.strip_prefix("ref:"))
.ok_or(FORMS)?;
parse_body(body)
}
fn parse_body(body: &str) -> Result<Claim, String> {
let (id, rule) = match body.strip_prefix('!') {
Some(item) => {
let (id, pattern) = parse_deny_item(item)?;
(id, ClaimRule::Deny(pattern))
}
None => {
let (id, item) = parse_allow_item(body)?;
(id, ClaimRule::Allow(item))
}
};
let namespace = id.map_or(Namespace::Local, Namespace::Collection);
Ok(Claim { namespace, rule })
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ClaimError {
Malformed {
claim: String,
reason: String,
},
TooMany,
}
impl fmt::Display for ClaimError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ClaimError::Malformed { claim, reason } => {
write!(f, "malformed claim '{claim}': {reason}")
}
ClaimError::TooMany => write!(f, "the certificate holds more than {MAX_CLAIMS} claims"),
}
}
}
impl std::error::Error for ClaimError {}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Claims {
inner: Vec<Claim>,
}
impl Claims {
pub fn parse<'a>(uris: impl IntoIterator<Item = &'a str>) -> Result<Claims, ClaimError> {
let mut inner = Vec::new();
let mut count = 0;
for uri in uris {
let Some(claim) = Claim::parse(uri) else {
continue;
};
count += 1;
if count > MAX_CLAIMS {
return Err(ClaimError::TooMany);
}
inner.push(claim?);
}
Ok(Claims { inner })
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn as_slice(&self) -> &[Claim] {
&self.inner
}
pub fn rules(&self, namespace: &Namespace, own: Option<&str>) -> Option<AccessRules> {
let namespace = match namespace {
Namespace::Collection(id) if Some(id.as_str()) == own => &Namespace::Local,
other => other,
};
let mut rules = AccessRules::default();
let mut found = false;
for claim in &self.inner {
let names = match (&claim.namespace, namespace) {
(Namespace::Local, Namespace::Local) => true,
(Namespace::Collection(id), Namespace::Local) => Some(id.as_str()) == own,
(Namespace::Collection(id), Namespace::Collection(wanted)) => id == wanted,
(Namespace::Local, Namespace::Collection(_)) => false,
};
if !names {
continue;
}
found = true;
match &claim.rule {
ClaimRule::Allow(item) => rules.allow.push(item.clone()),
ClaimRule::Deny(pattern) => rules.deny.push(pattern.clone()),
}
}
found.then_some(rules)
}
}
impl FromIterator<Claim> for Claims {
fn from_iter<I: IntoIterator<Item = Claim>>(iter: I) -> Claims {
Claims {
inner: iter.into_iter().collect(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn claim(uri: &str) -> Claim {
Claim::parse(uri).unwrap().unwrap()
}
fn allow(uri: &str) -> (Namespace, String) {
let claim = claim(uri);
let ClaimRule::Allow(item) = claim.rule else {
panic!("{uri} is no allow item");
};
(claim.namespace, item.to_string())
}
fn deny(uri: &str) -> (Namespace, String) {
let claim = claim(uri);
let ClaimRule::Deny(pattern) = claim.rule else {
panic!("{uri} is no deny item");
};
(claim.namespace, pattern.as_str().to_owned())
}
fn collection(id: &str) -> Namespace {
Namespace::Collection(id.to_owned())
}
#[test]
fn each_form_parses() {
assert_eq!(
allow("ostrya:ref:wd:devel/**"),
(Namespace::Local, "wd:devel/**".to_owned())
);
assert_eq!(
allow("ostrya:ref:main"),
(Namespace::Local, "r:main".to_owned())
);
assert_eq!(
allow("ostrya:ref:dtcwr:*/stable"),
(Namespace::Local, "rwctd:*/stable".to_owned())
);
assert_eq!(
deny("ostrya:ref:!secret/**"),
(Namespace::Local, "secret/**".to_owned())
);
assert_eq!(
allow("ostrya:ref:t:@com.example.os:os/release/**"),
(collection("com.example.os"), "t:os/release/**".to_owned())
);
assert_eq!(
allow("ostrya:ref:wd:@X.y:os/**"),
(collection("X.y"), "wd:os/**".to_owned())
);
assert_eq!(
allow("ostrya:ref:@_a.b_1:os"),
(collection("_a.b_1"), "r:os".to_owned())
);
assert_eq!(
deny("ostrya:ref:!@a.b:os/secret"),
(collection("a.b"), "os/secret".to_owned())
);
assert_eq!(
allow("ostrya:ref:@own.id:a"),
(collection("own.id"), "r:a".to_owned())
);
let longest = format!("a.{}", "b".repeat(ostrya_core::MAX_COLLECTION_ID_LEN - 2));
assert_eq!(
allow(&format!("ostrya:ref:@{longest}:a")),
(collection(&longest), "r:a".to_owned())
);
}
#[test]
fn another_uri_is_no_claim() {
for uri in [
"",
"https://example.com/",
"urn:a:b",
"spiffe://example.org/ci",
"ostryax:ref:a",
"ostrya",
"ostry:ref:a",
] {
assert!(Claim::parse(uri).is_none(), "{uri:?}");
}
let claims = Claims::parse(["https://example.com/", "urn:a"]).unwrap();
assert!(claims.is_empty());
}
const MALFORMED: &[&str] = &[
"ostrya:",
"ostrya:ref",
"ostrya:ref:",
"ostrya:ref::a",
"ostrya:ref:x:a",
"ostrya:ref:R:a",
"ostrya:ref:rr:a",
"ostrya:ref:r:a:b",
"ostrya:ref:r:!a",
"ostrya:ref:!",
"ostrya:ref:!!a",
"ostrya:ref:!r:a",
"ostrya:ref:a*",
"ostrya:ref:a//b",
"ostrya:ref: a",
"ostrya:ref:a ",
"ostrya:ref:%41",
"ostrya:ref:a\n",
"ostrya:tag:a",
"ostrya:Ref:a",
"ostrya:REF:a",
"OSTRYA:ref:a",
"Ostrya:ref:a",
"ostrya:Collection:a.b:a",
"ostrya:collection:",
"ostrya:collection:a.b:a",
"ostrya:collection:a.b:r:a",
"ostrya:collection:a.b:!a",
"ostrya:ref:@a.b:r:a",
"ostrya:ref:@a.b:",
"ostrya:ref:@a.b",
"ostrya:ref:@:a",
"ostrya:ref:r:@:a",
"ostrya:ref:@fresh:a",
"ostrya:ref:@1a.b:a",
"ostrya:ref:r:@a..b:a",
"ostrya:ref:r:@a.b",
"ostrya:ref:@a.b:@c.d:a",
"ostrya:ref:x:@a.b:a",
"ostrya:ref:@a.b:!a",
"ostrya:ref:!@a.b:!a",
"ostrya:ref:!r:@a.b:a",
"ostrya:ref:!@a.b",
];
#[test]
fn a_malformed_claim_is_refused() {
for &uri in MALFORMED {
let Some(Err(err)) = Claim::parse(uri) else {
panic!("{uri:?} was accepted");
};
let ClaimError::Malformed {
claim: held,
reason,
} = &err
else {
panic!("{uri:?}: {err:?}");
};
assert_eq!(held, uri);
assert!(!reason.is_empty(), "{uri:?}");
assert_eq!(
err.to_string(),
format!("malformed claim '{uri}': {reason}")
);
}
}
#[test]
fn the_reason_names_the_rule() {
let reason = |uri: &str| match Claim::parse(uri) {
Some(Err(ClaimError::Malformed { reason, .. })) => reason,
other => panic!("{uri:?}: {other:?}"),
};
assert_eq!(reason("ostrya:tag:a"), "a claim starts with ostrya:ref:");
assert_eq!(reason("OSTRYA:ref:a"), "a claim starts with ostrya:ref:");
assert_eq!(
reason("ostrya:collection:a.b:r:a"),
"a claim starts with ostrya:ref:"
);
assert_eq!(
reason("ostrya:ref:@fresh:a"),
"the collection id fails the collection-id rule"
);
assert_eq!(
reason("ostrya:ref:!@a..b:a"),
"the collection id fails the collection-id rule"
);
let long = format!("a.{}", "b".repeat(ostrya_core::MAX_COLLECTION_ID_LEN - 1));
assert_eq!(
reason(&format!("ostrya:ref:@{long}:a")),
"the collection id is longer than 255 bytes"
);
assert_eq!(
reason("ostrya:ref:x:a"),
"the modes are one or more of r, w, c, t, and d"
);
assert_eq!(
reason("ostrya:ref:rr:a"),
"each mode occurs at most one time"
);
}
#[test]
fn parse_keeps_the_order_and_stops_at_a_malformed_claim() {
let claims = Claims::parse([
"ostrya:ref:a",
"dns-like",
"ostrya:ref:w:@a.b:b",
"ostrya:ref:a",
])
.unwrap();
let namespaces: Vec<&Namespace> = claims.as_slice().iter().map(|c| &c.namespace).collect();
assert_eq!(
namespaces,
[&Namespace::Local, &collection("a.b"), &Namespace::Local]
);
let err = Claims::parse(["ostrya:ref:a", "ostrya:ref:x:a", "ostrya:ref:y:a"]).unwrap_err();
assert_eq!(
err,
ClaimError::Malformed {
claim: "ostrya:ref:x:a".to_owned(),
reason: "the modes are one or more of r, w, c, t, and d".to_owned(),
}
);
}
#[test]
fn more_than_the_most_claims_is_refused() {
let uris: Vec<String> = (0..MAX_CLAIMS)
.map(|i| format!("ostrya:ref:r{i}"))
.collect();
let mut with_others: Vec<&str> = uris.iter().map(String::as_str).collect();
with_others.push("https://example.com/");
assert_eq!(
Claims::parse(with_others.clone()).unwrap().as_slice().len(),
MAX_CLAIMS
);
with_others.push("OSTRYA:x");
assert_eq!(Claims::parse(with_others).unwrap_err(), ClaimError::TooMany);
assert_eq!(
ClaimError::TooMany.to_string(),
"the certificate holds more than 256 claims"
);
}
#[test]
fn parse_ref_gives_the_claim_of_the_uri() {
let longest = format!("a.{}", "b".repeat(ostrya_core::MAX_COLLECTION_ID_LEN - 2));
for text in [
"wd:devel/**",
"main",
"dtcwr:*/stable",
"!secret/**",
"t:@com.example.os:os/release/**",
"wd:@X.y:os/**",
"@_a.b_1:os",
"!@a.b:os/secret",
"@own.id:a",
&format!("@{longest}:a"),
] {
assert_eq!(
Claim::parse_ref(text).unwrap(),
claim(&format!("ostrya:ref:{text}")),
"{text:?}"
);
}
}
#[test]
fn parse_ref_refuses_a_malformed_text() {
for &uri in MALFORMED {
let Some(text) = uri.strip_prefix("ostrya:ref:") else {
continue;
};
let Some(Err(ClaimError::Malformed { reason, .. })) = Claim::parse(uri) else {
panic!("{uri:?} was accepted");
};
assert_eq!(
Claim::parse_ref(text),
Err(ClaimError::Malformed {
claim: text.to_owned(),
reason,
}),
"{text:?}"
);
}
}
#[test]
fn collect_keeps_the_order_and_has_no_limit() {
let claims: Claims = ["a", "w:@a.b:b", "a"]
.into_iter()
.map(|text| Claim::parse_ref(text).unwrap())
.collect();
let namespaces: Vec<&Namespace> = claims.as_slice().iter().map(|c| &c.namespace).collect();
assert_eq!(
namespaces,
[&Namespace::Local, &collection("a.b"), &Namespace::Local]
);
let texts: Vec<String> = (0..=MAX_CLAIMS).map(|i| format!("r{i}")).collect();
let claims: Claims = texts
.iter()
.map(|text| Claim::parse_ref(text).unwrap())
.collect();
assert_eq!(claims.as_slice().len(), MAX_CLAIMS + 1);
assert_eq!(
claims.as_slice()[MAX_CLAIMS],
Claim::parse_ref(&format!("r{MAX_CLAIMS}")).unwrap()
);
assert!(std::iter::empty::<Claim>().collect::<Claims>().is_empty());
}
}