egrix 0.1.0

Find leaked credentials and suspicious network destinations in AI agent transcripts. Local, offline, redacted by default.
//! The command line, and nothing else: no behavior lives here.

use crate::model::Agent;
use clap::{Args, Parser, Subcommand, ValueEnum};
use std::path::PathBuf;

#[derive(Parser)]
#[command(
    name = "egrix",
    version,
    about = "Find leaked credentials and suspicious network destinations in your AI agents' history.",
    long_about = "Find leaked credentials and suspicious network destinations in your AI agents' history.\n\nRuns locally, makes no network calls, and redacts every value it prints.\nWith no command, opens the dashboard in a terminal and prints a summary otherwise.",
    after_help = "Exit codes: 0 no new findings, 1 new findings, 2 error."
)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Option<Command>,
}

#[derive(Subcommand)]
pub enum Command {
    /// Scan for secrets and network destinations and print a summary
    Scan {
        #[command(flatten)]
        filter: Filter,
        #[command(flatten)]
        output: Output,
    },
    /// List leaked credentials
    Secrets {
        #[command(flatten)]
        filter: Filter,
        #[command(flatten)]
        output: Output,
        /// How to group the list
        #[arg(long, value_enum, default_value = "repo")]
        group: Group,
        /// Show the transcript around one secret, by id
        #[arg(long, value_name = "ID")]
        show: Option<String>,
    },
    /// List external hosts the agents touched
    Egress {
        #[command(flatten)]
        filter: Filter,
        #[command(flatten)]
        output: Output,
        /// Show the transcript around each sighting of one host
        #[arg(long, value_name = "HOST")]
        show: Option<String>,
        /// Include allowed hosts in the list
        #[arg(long)]
        all: bool,
    },
    /// Mask found secrets inside the transcript files (keeps a backup)
    Fix {
        #[command(flatten)]
        filter: Filter,
        /// Only this secret, by id
        #[arg(long, value_name = "ID")]
        id: Option<String>,
        /// Put masked secrets back instead
        #[arg(long)]
        undo: bool,
        /// Do not ask for confirmation
        #[arg(long, short)]
        yes: bool,
    },
    /// Record what you did about a secret
    Mark {
        /// Secret id, or a unique prefix of it
        id: String,
        /// The credential was rotated at the provider
        #[arg(long, group = "what")]
        rotated: bool,
        /// False positive: stop reporting it
        #[arg(long, group = "what")]
        ignored: bool,
        /// Clear both marks
        #[arg(long, group = "what")]
        clear: bool,
    },
    /// Manage your allow and deny lists
    Hosts {
        #[command(subcommand)]
        action: HostsAction,
    },
    /// Inspect the secret rules
    Rules {
        #[command(subcommand)]
        action: RulesAction,
    },
    /// Accept the current findings, so only new ones fail a scan
    Baseline {
        #[command(flatten)]
        filter: Filter,
    },
}

#[derive(Subcommand)]
pub enum HostsAction {
    /// Trust a host and its subdomains
    Allow { host: String },
    /// Flag a host and its subdomains
    Deny { host: String },
    /// Take a host off both lists
    Remove { host: String },
    /// Print both lists
    List,
}

#[derive(Subcommand)]
pub enum RulesAction {
    /// Print every active rule
    List,
    /// Run the rules over a file and print what they find
    Test { file: PathBuf },
}

#[derive(Args, Clone, Default)]
pub struct Filter {
    /// Only this agent's history
    #[arg(long, value_enum)]
    pub agent: Option<Agent>,
    /// Only the last period: 7d, 2w, 3m
    #[arg(long, value_name = "SPAN")]
    pub since: Option<String>,
    /// Only repositories whose name or remote contains this
    #[arg(long, value_name = "NAME")]
    pub repo: Option<String>,
}

#[derive(Args, Clone, Default)]
pub struct Output {
    /// Machine-readable JSON
    #[arg(long, conflicts_with = "sarif")]
    pub json: bool,
    /// SARIF 2.1.0, for code-scanning tools
    #[arg(long)]
    pub sarif: bool,
    /// Print raw secret values instead of redacted previews
    #[arg(long)]
    pub reveal: bool,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, ValueEnum)]
pub enum Group {
    #[default]
    Repo,
    Remote,
    Rule,
    Agent,
}

impl Group {
    pub fn next(self) -> Self {
        match self {
            Group::Repo => Group::Remote,
            Group::Remote => Group::Rule,
            Group::Rule => Group::Agent,
            Group::Agent => Group::Repo,
        }
    }
    pub fn name(self) -> &'static str {
        match self {
            Group::Repo => "repository",
            Group::Remote => "remote",
            Group::Rule => "rule",
            Group::Agent => "agent",
        }
    }
    /// The heading a secret falls under.
    pub fn key(self, secret: &crate::model::Secret) -> String {
        let loc = &secret.location;
        match self {
            Group::Repo => loc.repo_name().to_string(),
            Group::Remote => crate::report::repo_label(loc.repo.as_ref(), true),
            Group::Rule => secret.rule.clone(),
            Group::Agent => loc.agent.name().to_string(),
        }
    }
}