use crate::error::{Result, VaultError};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;
pub const MAX_SEGMENT: usize = 128;
pub const MAX_DEPTH: usize = 16;
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct Scope(String);
fn check_segment(s: &str) -> Result<()> {
let ok = !s.is_empty()
&& s.len() <= MAX_SEGMENT
&& s.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b':' | b'@' | b'+'));
if ok { Ok(()) } else { Err(VaultError::invalid("scope segment", format!("{s:?}"))) }
}
impl Scope {
pub fn root() -> Self {
Scope(String::new())
}
pub fn new<I, S>(segments: I) -> Result<Self>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut out = Scope::root();
for s in segments {
out = out.child(s.as_ref())?;
}
Ok(out)
}
pub fn parse(s: &str) -> Result<Self> {
if s.is_empty() { Ok(Scope::root()) } else { Scope::new(s.split('/')) }
}
pub fn child(&self, segment: &str) -> Result<Self> {
check_segment(segment)?;
if self.depth() >= MAX_DEPTH {
return Err(VaultError::invalid("scope", format!("deeper than {MAX_DEPTH} segments")));
}
Ok(if self.0.is_empty() { Scope(segment.to_string()) } else { Scope(format!("{}/{segment}", self.0)) })
}
pub fn parent(&self) -> Option<Scope> {
if self.0.is_empty() {
return None;
}
Some(match self.0.rfind('/') {
Some(i) => Scope(self.0[..i].to_string()),
None => Scope::root(),
})
}
pub fn segments(&self) -> impl Iterator<Item = &str> {
self.0.split('/').filter(|s| !s.is_empty())
}
pub fn depth(&self) -> usize {
self.segments().count()
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn is_root(&self) -> bool {
self.0.is_empty()
}
pub fn contains(&self, other: &Scope) -> bool {
self.0.is_empty()
|| other.0 == self.0
|| (other.0.len() > self.0.len() && other.0.starts_with(&self.0) && other.0.as_bytes()[self.0.len()] == b'/')
}
}
impl fmt::Display for Scope {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl FromStr for Scope {
type Err = VaultError;
fn from_str(s: &str) -> Result<Self> {
Scope::parse(s)
}
}
impl TryFrom<String> for Scope {
type Error = VaultError;
fn try_from(s: String) -> Result<Self> {
Scope::parse(&s)
}
}
impl From<Scope> for String {
fn from(s: Scope) -> String {
s.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Environment {
Test,
Live,
}
impl Environment {
pub fn as_str(self) -> &'static str {
match self {
Environment::Test => "test",
Environment::Live => "live",
}
}
pub fn parse(s: &str) -> Result<Self> {
match s.trim().to_ascii_lowercase().as_str() {
"test" | "dry-run" | "dryrun" | "paper" | "sandbox" => Ok(Environment::Test),
"live" => Ok(Environment::Live),
other => Err(VaultError::invalid("environment", format!("{other:?}: test or live"))),
}
}
pub fn may_touch(self, secret: Environment) -> bool {
self == Environment::Live || secret == Environment::Test
}
}
impl fmt::Display for Environment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for Environment {
type Err = VaultError;
fn from_str(s: &str) -> Result<Self> {
Environment::parse(s)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scopes_nest_and_validate() {
let root = Scope::root();
let a = Scope::new(["a"]).unwrap();
let ab = a.child("b").unwrap();
let abc = Scope::parse("ab/c").unwrap();
assert!(root.contains(&ab) && a.contains(&ab) && ab.contains(&ab));
assert!(!a.contains(&abc), "prefix match must stop at a segment boundary");
assert!(!ab.contains(&a));
assert_eq!(ab.parent(), Some(a.clone()));
assert_eq!(a.parent(), Some(Scope::root()));
assert_eq!(root.parent(), None);
assert_eq!(ab.segments().collect::<Vec<_>>(), ["a", "b"]);
for bad in ["", "a b", "a//b", "/a", "a/", "é"] {
assert!(Scope::parse(bad).is_err() || bad.is_empty(), "{bad:?}");
}
assert!(Scope::new(["x".repeat(129)]).is_err());
assert!(Scope::new(std::iter::repeat_n("x", 17)).is_err());
let j = serde_json::to_string(&ab).unwrap();
assert_eq!(j, "\"a/b\"");
assert_eq!(serde_json::from_str::<Scope>(&j).unwrap(), ab);
assert!(serde_json::from_str::<Scope>("\"a b\"").is_err());
}
#[test]
fn environments_parse_and_gate() {
assert_eq!(Environment::parse("Dry-Run").unwrap(), Environment::Test);
assert_eq!(Environment::parse("live").unwrap(), Environment::Live);
assert!(Environment::parse("prod").is_err());
assert!(Environment::Live.may_touch(Environment::Test));
assert!(Environment::Live.may_touch(Environment::Live));
assert!(Environment::Test.may_touch(Environment::Test));
assert!(!Environment::Test.may_touch(Environment::Live));
}
}