use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SensitivityKind {
KnownToken,
PrivateKey,
HighEntropy,
SensitiveFilename,
GeneratedPath,
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SensitivitySeverity {
Medium,
High,
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SensitivityPolicyOutcome {
Allow,
Warn,
Block,
}
impl SensitivityKind {
pub const ALL: [Self; 5] = [
Self::KnownToken,
Self::PrivateKey,
Self::HighEntropy,
Self::SensitiveFilename,
Self::GeneratedPath,
];
pub fn as_str(&self) -> &'static str {
match self {
Self::KnownToken => "known_token",
Self::PrivateKey => "private_key",
Self::HighEntropy => "high_entropy",
Self::SensitiveFilename => "sensitive_filename",
Self::GeneratedPath => "generated_path",
}
}
pub fn parse(value: &str) -> Option<Self> {
Self::ALL.into_iter().find(|kind| kind.as_str() == value)
}
pub fn severity(&self) -> SensitivitySeverity {
match self {
Self::KnownToken | Self::PrivateKey => SensitivitySeverity::High,
Self::HighEntropy | Self::SensitiveFilename | Self::GeneratedPath => {
SensitivitySeverity::Medium
}
}
}
pub fn policy_outcome(&self) -> SensitivityPolicyOutcome {
match self {
Self::KnownToken | Self::PrivateKey => SensitivityPolicyOutcome::Block,
Self::HighEntropy | Self::SensitiveFilename | Self::GeneratedPath => {
SensitivityPolicyOutcome::Warn
}
}
}
}
impl std::fmt::Display for SensitivityKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl SensitivitySeverity {
pub const ALL: [Self; 2] = [Self::Medium, Self::High];
pub fn as_str(&self) -> &'static str {
match self {
Self::Medium => "medium",
Self::High => "high",
}
}
pub fn parse(value: &str) -> Option<Self> {
Self::ALL
.into_iter()
.find(|severity| severity.as_str() == value)
}
}
impl std::fmt::Display for SensitivitySeverity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl SensitivityPolicyOutcome {
pub const ALL: [Self; 3] = [Self::Allow, Self::Warn, Self::Block];
pub fn as_str(&self) -> &'static str {
match self {
Self::Allow => "allow",
Self::Warn => "warn",
Self::Block => "block",
}
}
pub fn parse(value: &str) -> Option<Self> {
Self::ALL
.into_iter()
.find(|outcome| outcome.as_str() == value)
}
pub fn combine(outcomes: impl IntoIterator<Item = Self>) -> Self {
outcomes.into_iter().fold(Self::Allow, Ord::max)
}
}
impl std::fmt::Display for SensitivityPolicyOutcome {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}