use crate::detectors::built_in_ids;
use crate::error::SecretScanErrorCode;
use crate::json::{self, Value};
use crate::types::is_identifier;
const DETECTOR_IDS_MAX: usize = 256;
pub(crate) const PII_ADAPTER_ID: &str = "pii-domain";
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum Mode {
#[default]
All,
Include,
Exclude,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DetectionSelection {
pub(crate) mode: Mode,
ids: Vec<String>,
}
impl DetectionSelection {
#[must_use]
pub const fn all() -> Self {
Self {
mode: Mode::All,
ids: Vec::new(),
}
}
#[must_use]
pub fn include<I, S>(ids: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self {
mode: Mode::Include,
ids: ids.into_iter().map(Into::into).collect(),
}
}
#[must_use]
pub fn exclude<I, S>(ids: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self {
mode: Mode::Exclude,
ids: ids.into_iter().map(Into::into).collect(),
}
}
pub fn from_json(json: &str) -> Result<Self, DetectionConfigError> {
let value = json::parse(json).ok_or(DetectionConfigError::class(CLASS_WRONG_TYPE, None))?;
selection_from_value(&value).map_err(Fault::error)
}
#[must_use]
pub fn is_all(&self) -> bool {
self.mode == Mode::All || (self.mode == Mode::Exclude && self.ids.is_empty())
}
#[must_use]
pub const fn mode(&self) -> &'static str {
match self.mode {
Mode::All => "all-included",
Mode::Include => "include",
Mode::Exclude => "exclude",
}
}
pub fn ids(&self) -> impl Iterator<Item = &str> {
self.ids.iter().map(String::as_str)
}
}
pub(crate) const CLASS_UNKNOWN_FIELD: &str = "UNKNOWN_FIELD";
pub(crate) const CLASS_WRONG_TYPE: &str = "WRONG_TYPE";
pub(crate) const CLASS_TOO_MANY: &str = "TOO_MANY_DETECTOR_IDS";
pub(crate) const CLASS_IDENTIFIER: &str = "INVALID_IDENTIFIER";
pub(crate) const CLASS_UNSUPPORTED: &str = "DETECTION_SELECTION_UNSUPPORTED";
pub(crate) const CLASS_DUPLICATE: &str = "DUPLICATE_DETECTOR_ID";
pub(crate) const CLASS_NOT_SELECTABLE: &str = "DETECTOR_NOT_SELECTABLE";
pub(crate) const CLASS_NOT_INCLUDED: &str = "DETECTOR_NOT_INCLUDED";
pub(crate) const CLASS_UNKNOWN_ID: &str = "UNKNOWN_DETECTOR_ID";
pub(crate) const CLASS_CONFLICT: &str = "DETECTION_SELECTOR_CONFLICT";
pub(crate) const CLASS_EMPTY: &str = "EMPTY_DETECTION_SET";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DetectionConfigError {
code: SecretScanErrorCode,
class: &'static str,
index: Option<usize>,
}
impl DetectionConfigError {
const fn class(class: &'static str, index: Option<usize>) -> Self {
let code = if matches!(class.as_bytes(), b"EMPTY_DETECTION_SET") {
SecretScanErrorCode::EmptyDetectionSet
} else {
SecretScanErrorCode::InvalidDetectionConfig
};
Self { code, class, index }
}
#[must_use]
pub const fn code(self) -> SecretScanErrorCode {
self.code
}
#[must_use]
pub const fn class_name(self) -> &'static str {
self.class
}
#[must_use]
pub const fn index(self) -> Option<usize> {
self.index
}
}
impl std::fmt::Display for DetectionConfigError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(self.code.message())
}
}
impl std::error::Error for DetectionConfigError {}
impl From<DetectionConfigError> for crate::SecretScanError {
fn from(error: DetectionConfigError) -> Self {
error.code.into()
}
}
#[derive(Clone, Copy)]
pub(crate) struct Fault {
pub(crate) class: &'static str,
array: Option<&'static str>,
index: Option<usize>,
}
impl Fault {
const fn new(class: &'static str) -> Self {
Self {
class,
array: None,
index: None,
}
}
const fn at(class: &'static str, array: &'static str, index: usize) -> Self {
Self {
class,
array: Some(array),
index: Some(index),
}
}
const fn error(self) -> DetectionConfigError {
DetectionConfigError::class(self.class, self.index)
}
pub(crate) fn path(self) -> String {
match (self.array, self.index) {
(None, Some(index)) if self.class == CLASS_UNKNOWN_FIELD => {
format!("detection.@{index}")
}
(Some(array), Some(index)) => format!("detection.{array}[{index}]"),
(Some(array), None) => format!("detection.{array}"),
_ => "detection".to_owned(),
}
}
}
pub(crate) fn selection_from_value(value: &Value) -> Result<DetectionSelection, Fault> {
let Value::Object(members) = value else {
return Err(Fault::new(CLASS_WRONG_TYPE));
};
let mut include: Option<&Value> = None;
let mut exclude: Option<&Value> = None;
for (position, (name, member)) in members.iter().enumerate() {
match name.as_str() {
"include" => include = Some(member),
"exclude" => exclude = Some(member),
_ => {
return Err(Fault {
class: CLASS_UNKNOWN_FIELD,
array: None,
index: Some(position),
});
}
}
}
let list = |member: &Value, array: &'static str| -> Result<Vec<String>, Fault> {
let Value::Array(items) = member else {
return Err(Fault {
class: CLASS_WRONG_TYPE,
array: Some(array),
index: None,
});
};
let mut ids = Vec::with_capacity(items.len().min(DETECTOR_IDS_MAX));
for (index, item) in items.iter().enumerate() {
let Value::Str(id) = item else {
return Err(Fault::at(CLASS_WRONG_TYPE, array, index));
};
ids.push(id.clone());
}
if ids.len() > DETECTOR_IDS_MAX {
return Err(Fault {
class: CLASS_TOO_MANY,
array: Some(array),
index: None,
});
}
if let Some(index) = ids.iter().position(|id| !is_identifier(id)) {
return Err(Fault::at(CLASS_IDENTIFIER, array, index));
}
Ok(ids)
};
match (include, exclude) {
(Some(_), Some(_)) => Err(Fault::new(CLASS_CONFLICT)),
(Some(member), None) => Ok(DetectionSelection::include(list(member, "include")?)),
(None, Some(member)) => Ok(DetectionSelection::exclude(list(member, "exclude")?)),
(None, None) => Ok(DetectionSelection::all()),
}
}
pub(crate) struct PlanEntry<'a> {
pub(crate) id: &'a str,
pub(crate) built_in: bool,
}
pub(crate) fn plan_selection(
selection: &DetectionSelection,
entries: &[PlanEntry<'_>],
) -> Result<Vec<bool>, DetectionConfigError> {
let included: Vec<&str> = entries
.iter()
.filter(|entry| entry.built_in)
.map(|entry| entry.id)
.collect();
let named = judge(selection, &included, |id| {
id == PII_ADAPTER_ID
|| entries
.iter()
.any(|entry| !entry.built_in && entry.id == id)
})
.map_err(Fault::error)?;
let keep: Vec<bool> = entries
.iter()
.map(|entry| {
!entry.built_in
|| match selection.mode {
Mode::All => true,
Mode::Include => named.contains(&entry.id),
Mode::Exclude => !named.contains(&entry.id),
}
})
.collect();
if selection.mode != Mode::All && !keep.iter().any(|kept| *kept) {
return Err(DetectionConfigError::class(CLASS_EMPTY, None));
}
Ok(keep)
}
pub(crate) fn judge<'a>(
selection: &'a DetectionSelection,
included: &[&str],
not_selectable: impl Fn(&str) -> bool,
) -> Result<Vec<&'a str>, Fault> {
let array = match selection.mode {
Mode::Exclude => "exclude",
_ => "include",
};
let mut named: Vec<&str> = Vec::new();
for (index, id) in selection.ids.iter().enumerate() {
let id = id.as_str();
let class = if named.contains(&id) {
CLASS_DUPLICATE
} else if included.contains(&id) {
named.push(id);
continue;
} else if not_selectable(id) {
CLASS_NOT_SELECTABLE
} else if built_in_ids().any(|known| known == id) {
CLASS_NOT_INCLUDED
} else {
CLASS_UNKNOWN_ID
};
return Err(Fault::at(class, array, index));
}
Ok(named)
}
impl DetectionSelection {
pub(crate) fn enabled_of<'a>(&self, included: &[&'a str]) -> Result<Vec<&'a str>, Fault> {
let named = judge(self, included, |id| id == PII_ADAPTER_ID)?;
Ok(included
.iter()
.copied()
.filter(|id| match self.mode {
Mode::All => true,
Mode::Include => named.contains(id),
Mode::Exclude => !named.contains(id),
})
.collect())
}
}