use crate::error::{Result, VaultError};
use crate::scope::{Environment, Scope};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;
use zeroize::Zeroizing;
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct SecretId(String);
impl SecretId {
pub(crate) fn random() -> Self {
SecretId(uuid::Uuid::new_v4().to_string())
}
pub fn parse(s: &str) -> Result<Self> {
uuid::Uuid::parse_str(s.trim()).map(|u| SecretId(u.hyphenated().to_string())).map_err(|_| VaultError::invalid("secret id", format!("{s:?}")))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for SecretId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl FromStr for SecretId {
type Err = VaultError;
fn from_str(s: &str) -> Result<Self> {
SecretId::parse(s)
}
}
impl TryFrom<String> for SecretId {
type Error = VaultError;
fn try_from(s: String) -> Result<Self> {
SecretId::parse(&s)
}
}
impl From<SecretId> for String {
fn from(s: SecretId) -> String {
s.0
}
}
pub type Labels = BTreeMap<String, String>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum VersionState {
Active,
Pending,
Previous,
Retired,
Discarded,
}
impl VersionState {
pub(crate) fn as_str(self) -> &'static str {
match self {
VersionState::Active => "active",
VersionState::Pending => "pending",
VersionState::Previous => "previous",
VersionState::Retired => "retired",
VersionState::Discarded => "discarded",
}
}
pub(crate) fn parse(s: &str) -> Result<Self> {
Ok(match s {
"active" => VersionState::Active,
"pending" => VersionState::Pending,
"previous" => VersionState::Previous,
"retired" => VersionState::Retired,
"discarded" => VersionState::Discarded,
other => return Err(VaultError::Storage(format!("unknown version state {other:?}"))),
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Which {
#[default]
Active,
Pending,
Previous,
}
impl Which {
pub fn as_str(self) -> &'static str {
match self {
Which::Active => "active",
Which::Pending => "pending",
Which::Previous => "previous",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct VersionMeta {
pub version: u32,
pub state: VersionState,
pub created_at: i64,
pub created_by: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SecretMeta {
pub id: SecretId,
pub scope: Scope,
pub name: String,
pub kind: String,
pub environment: Environment,
pub labels: Labels,
pub created_at: i64,
pub created_by: String,
pub updated_at: i64,
pub active_version: u32,
pub pending_version: Option<u32>,
pub previous_version: Option<u32>,
}
impl SecretMeta {
pub fn version(&self, which: Which) -> Option<u32> {
match which {
Which::Active => Some(self.active_version),
Which::Pending => self.pending_version,
Which::Previous => self.previous_version,
}
}
}
pub const MAX_LABELS: usize = 64;
fn check_text(what: &'static str, s: &str, max: usize, allow_empty: bool) -> Result<()> {
if (!allow_empty && s.trim().is_empty()) || s.len() > max || s.chars().any(char::is_control) {
return Err(VaultError::invalid(
what,
format!("must be {}1 to {max} bytes without control characters", if allow_empty { "0 or " } else { "" }),
));
}
Ok(())
}
pub(crate) fn check_identity(s: &str) -> Result<()> {
check_text("identity", s, 256, false)
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct NewSecret {
pub scope: Scope,
pub name: String,
pub kind: String,
pub environment: Environment,
pub labels: Labels,
}
impl NewSecret {
pub fn new(scope: Scope, name: impl Into<String>, environment: Environment) -> Self {
NewSecret { scope, name: name.into(), kind: "secret".into(), environment, labels: Labels::new() }
}
pub fn kind(mut self, kind: impl Into<String>) -> Self {
self.kind = kind.into();
self
}
pub fn label(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.labels.insert(key.into(), value.into());
self
}
pub(crate) fn validate(&self) -> Result<()> {
check_text("name", &self.name, 128, false)?;
check_text("kind", &self.kind, 64, false)?;
check_labels(&self.labels)
}
}
pub(crate) fn check_labels(labels: &Labels) -> Result<()> {
if labels.len() > MAX_LABELS {
return Err(VaultError::invalid("labels", format!("at most {MAX_LABELS}")));
}
for (k, v) in labels {
check_text("label key", k, 64, false)?;
check_text("label value", v, 512, true)?;
}
Ok(())
}
pub struct Plaintext(Zeroizing<Vec<u8>>);
impl Plaintext {
pub(crate) fn new(bytes: Zeroizing<Vec<u8>>) -> Self {
Plaintext(bytes)
}
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
pub fn as_str(&self) -> Result<&str> {
std::str::from_utf8(&self.0).map_err(|_| VaultError::invalid("value", "not UTF-8"))
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl fmt::Debug for Plaintext {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Plaintext(<{} bytes redacted>)", self.0.len())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ids_labels_and_plaintext() {
let id = SecretId::random();
assert_eq!(SecretId::parse(id.as_str()).unwrap(), id);
assert!(SecretId::parse("nope").is_err());
let p = Plaintext::new(Zeroizing::new(b"hunter2".to_vec()));
assert_eq!(format!("{p:?}"), "Plaintext(<7 bytes redacted>)");
assert_eq!(p.as_str().unwrap(), "hunter2");
let n = NewSecret::new(Scope::root(), "", Environment::Test);
assert!(n.validate().is_err());
let n = NewSecret::new(Scope::root(), "ok", Environment::Test).label("k", "v\n");
assert!(n.validate().is_err());
let mut l = Labels::new();
for i in 0..65 {
l.insert(format!("k{i}"), String::new());
}
assert!(check_labels(&l).is_err());
}
}