use crate::time::Day;
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
use std::fmt;
use std::path::PathBuf;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, clap::ValueEnum)]
#[serde(rename_all = "lowercase")]
pub enum Agent {
Claude,
Codex,
Gemini,
Copilot,
Antigravity,
}
impl Agent {
pub const ALL: [Agent; 5] = [Agent::Claude, Agent::Codex, Agent::Gemini, Agent::Copilot, Agent::Antigravity];
pub fn name(self) -> &'static str {
match self {
Agent::Claude => "claude",
Agent::Codex => "codex",
Agent::Gemini => "gemini",
Agent::Copilot => "copilot",
Agent::Antigravity => "antigravity",
}
}
}
impl fmt::Display for Agent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Severity {
High,
#[default]
Medium,
Low,
}
impl Severity {
pub fn name(self) -> &'static str {
match self {
Severity::High => "high",
Severity::Medium => "medium",
Severity::Low => "low",
}
}
pub fn glyph(self) -> &'static str {
match self {
Severity::High => "▲",
Severity::Medium => "■",
Severity::Low => "·",
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Repo {
pub name: String,
pub path: String,
pub remote: Option<String>,
}
impl Repo {
pub fn matches(&self, filter: &str) -> bool {
self.name.to_lowercase().contains(filter) || self.remote.as_ref().is_some_and(|r| r.to_lowercase().contains(filter))
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Location {
pub agent: Agent,
pub file: PathBuf,
pub line: usize,
pub repo: Option<Repo>,
pub branch: Option<String>,
pub day: Day,
}
impl Location {
pub fn repo_name(&self) -> &str {
self.repo.as_ref().map_or("(no repository)", |r| r.name.as_str())
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
pub struct Progress {
pub rotated: bool,
pub masked: bool,
pub ignored: bool,
}
#[derive(Clone, Debug, Serialize)]
pub struct Secret {
pub id: String,
pub rule: String,
pub severity: Severity,
pub preview: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub value: Option<String>,
pub location: Location,
pub files: BTreeSet<PathBuf>,
pub occurrences: usize,
pub new: bool,
pub progress: Progress,
}
impl Secret {
pub fn is_open(&self) -> bool {
!self.progress.ignored && !(self.progress.rotated && self.progress.masked)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Verdict {
Flagged,
Unknown,
Allowed,
}
impl Verdict {
pub fn name(self) -> &'static str {
match self {
Verdict::Flagged => "flagged",
Verdict::Unknown => "unknown",
Verdict::Allowed => "allowed",
}
}
pub fn glyph(self) -> &'static str {
match self {
Verdict::Flagged => "✗",
Verdict::Unknown => "?",
Verdict::Allowed => "✓",
}
}
}
#[derive(Clone, Debug, Serialize)]
pub struct Host {
pub host: String,
pub verdict: Verdict,
pub reason: String,
pub occurrences: usize,
pub locations: Vec<Location>,
pub last_seen: Day,
pub new: bool,
}
impl Host {
pub fn repo_name(&self) -> &str {
self.locations.first().map_or("(no repository)", Location::repo_name)
}
}
#[derive(Clone, Debug, Default, Serialize)]
pub struct Report {
pub scanned_files: usize,
pub agents: Vec<Agent>,
pub today: Day,
pub secrets: Vec<Secret>,
pub hosts: Vec<Host>,
}
impl Report {
pub fn open_secrets(&self) -> impl Iterator<Item = &Secret> {
self.secrets.iter().filter(|s| s.is_open())
}
pub fn flagged_hosts(&self) -> impl Iterator<Item = &Host> {
self.hosts.iter().filter(|h| h.verdict == Verdict::Flagged)
}
pub fn secrets_since(&self, days: Option<i64>) -> impl Iterator<Item = &Secret> {
let cutoff = days.map(|n| self.today - n);
self.secrets.iter().filter(move |s| cutoff.is_none_or(|c| s.location.day >= c))
}
pub fn hosts_since(&self, days: Option<i64>) -> impl Iterator<Item = &Host> {
let cutoff = days.map(|n| self.today - n);
self.hosts.iter().filter(move |h| cutoff.is_none_or(|c| h.last_seen >= c))
}
#[cfg_attr(not(feature = "tui"), allow(dead_code))]
pub fn open_secrets_since(&self, days: Option<i64>) -> impl Iterator<Item = &Secret> {
self.secrets_since(days).filter(|s| s.is_open())
}
#[cfg_attr(not(feature = "tui"), allow(dead_code))]
pub fn flagged_hosts_since(&self, days: Option<i64>) -> impl Iterator<Item = &Host> {
self.hosts_since(days).filter(|h| h.verdict == Verdict::Flagged)
}
pub fn has_new_findings(&self) -> bool {
self.open_secrets().any(|s| s.new) || self.flagged_hosts().any(|h| h.new)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn location(day: Day) -> Location {
Location { agent: Agent::Claude, file: PathBuf::from("x.jsonl"), line: 1, repo: None, branch: None, day }
}
fn secret(id: &str, day: Day, progress: Progress) -> Secret {
Secret {
id: id.into(),
rule: "test-rule".into(),
severity: Severity::Low,
preview: "****".into(),
value: None,
location: location(day),
files: BTreeSet::new(),
occurrences: 1,
new: false,
progress,
}
}
fn host(name: &str, day: Day) -> Host {
Host {
host: name.into(),
verdict: Verdict::Flagged,
reason: "test".into(),
occurrences: 1,
locations: vec![location(day)],
last_seen: day,
new: false,
}
}
#[test]
fn period_helpers_window_secrets_and_hosts() {
let mut report = Report { today: 100, ..Report::default() };
report.secrets.push(secret("recent", 100, Progress::default()));
report.secrets.push(secret("old", 60, Progress::default()));
report.secrets.push(secret("resolved", 100, Progress { rotated: true, masked: true, ignored: false }));
report.hosts.push(host("recent.test", 100));
report.hosts.push(host("old.test", 60));
let ids = |days: Option<i64>| report.secrets_since(days).map(|s| s.id.as_str()).collect::<Vec<_>>();
assert_eq!(ids(Some(30)), ["recent", "resolved"]);
assert_eq!(ids(None).len(), 3);
assert_eq!(report.open_secrets_since(Some(30)).count(), 1, "resolved secrets are closed");
assert_eq!(report.flagged_hosts_since(Some(30)).count(), 1);
assert_eq!(report.flagged_hosts_since(None).count(), 2);
}
}