pub mod download;
mod process;
pub mod skillspector;
pub mod snyk_leftover;
mod text;
use serde::{Deserialize, Serialize};
pub use process::RUNS_DIR;
use super::{Finding, Severity, Verdict};
use crate::error::{CoreError, ErrorCode};
pub const TOOLS_DIR: &str = "tools";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Scanner {
#[serde(rename = "skillspector")]
SkillSpector,
}
impl Scanner {
pub const fn as_str(self) -> &'static str {
match self {
Scanner::SkillSpector => "skillspector",
}
}
pub const fn name(self) -> &'static str {
match self {
Scanner::SkillSpector => "SkillSpector",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ExternalStatus {
Scanned,
NotScanned,
NotUnderstood,
}
impl ExternalStatus {
pub const fn as_str(self) -> &'static str {
match self {
ExternalStatus::Scanned => "scanned",
ExternalStatus::NotScanned => "not_scanned",
ExternalStatus::NotUnderstood => "not_understood",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NotScanned {
NotAvailable,
NotPrepared,
Timeout,
Canceled,
Failed,
}
impl NotScanned {
pub const fn as_str(self) -> &'static str {
match self {
NotScanned::NotAvailable => "not_available",
NotScanned::NotPrepared => "not_prepared",
NotScanned::Timeout => "timeout",
NotScanned::Canceled => "canceled",
NotScanned::Failed => "failed",
}
}
pub const fn text(self) -> &'static str {
match self {
NotScanned::NotAvailable => "SkillSpector was not found; see `surject scanner status`",
NotScanned::NotPrepared => {
"SkillSpector is not downloaded yet; run `surject scanner prepare`"
}
NotScanned::Timeout => "timed out",
NotScanned::Canceled => "canceled",
NotScanned::Failed => "the scanner failed",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExternalReport {
pub scanner: Scanner,
pub version: String,
pub status: ExternalStatus,
pub reason: Option<NotScanned>,
pub verdict: Verdict,
pub findings: Vec<Finding>,
#[serde(default)]
pub incomplete: bool,
pub content_hash: Option<String>,
pub detail: Option<String>,
}
impl ExternalReport {
pub fn not_scanned(
scanner: Scanner,
version: &str,
reason: NotScanned,
detail: Option<String>,
) -> Self {
Self {
scanner,
version: version.to_owned(),
status: ExternalStatus::NotScanned,
reason: Some(reason),
verdict: Verdict::Clean,
findings: Vec::new(),
incomplete: false,
content_hash: None,
detail,
}
}
pub fn not_understood(scanner: Scanner, version: &str, why: &str) -> Self {
Self {
scanner,
version: version.to_owned(),
status: ExternalStatus::NotUnderstood,
reason: None,
verdict: Verdict::Clean,
findings: Vec::new(),
incomplete: false,
content_hash: None,
detail: Some(format!(
"scanner output not understood (version {version}): {why}"
)),
}
}
pub fn summary(&self) -> String {
match (self.status, self.reason) {
(ExternalStatus::NotScanned, Some(reason)) => {
format!("not scanned ({})", reason.text())
}
(ExternalStatus::NotUnderstood, _) => {
format!("scanner output not understood (version {})", self.version)
}
_ => {
let n = self.findings.len();
let plural = if n == 1 { "" } else { "s" };
match (self.incomplete, self.verdict) {
(true, Verdict::Clean) => format!("not fully scanned ({n} finding{plural})"),
(true, verdict) => {
format!(
"{} ({n} finding{plural}), not fully scanned",
verdict.as_str()
)
}
(false, verdict) => format!("{} ({n} finding{plural})", verdict.as_str()),
}
}
}
}
pub fn dangerous(&self) -> impl Iterator<Item = &Finding> {
self.findings
.iter()
.filter(|finding| finding.severity == Severity::Dangerous)
}
}
pub fn gate(report: &ExternalReport, allow_dangerous: bool) -> Result<(), CoreError> {
if allow_dangerous || report.verdict != Verdict::Dangerous {
return Ok(());
}
let labels: Vec<String> = report.dangerous().map(Finding::label).collect();
let mut shown = labels
.iter()
.take(3)
.cloned()
.collect::<Vec<_>>()
.join(", ");
if labels.len() > 3 {
shown.push_str(&format!(" and {} more", labels.len() - 3));
}
let plural = if labels.len() == 1 { "" } else { "s" };
Err(CoreError::new(
ErrorCode::ScanBlocked,
format!(
"{} found {} dangerous finding{plural}: {shown}; review them, and install anyway only if you trust this content",
report.scanner.name(),
labels.len()
),
))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Choice {
#[default]
Default,
On,
Off,
}
impl Choice {
pub const fn wants(self, enabled: bool) -> bool {
match self {
Choice::On => true,
Choice::Off => false,
Choice::Default => enabled,
}
}
}