140 lines
5.1 KiB
Rust
140 lines
5.1 KiB
Rust
//! #516: the secret scanner's rules change between versions (gitleaks to
|
|
//! Betterleaks: a lone AWS key id is now `generic-api-key`), and the rule is
|
|
//! part of the fingerprint. A secret reported under another rule keeps its
|
|
//! finding: same row (so the same tracker issue), status and triage, with the
|
|
//! new rule. A row whose rule still reports it is left alone. Runs against a
|
|
//! live Mongo (`TEST_MONGODB_URI`) and skips cleanly without one.
|
|
|
|
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
|
|
|
use compliance_agent::database::{Database, DatabasePool};
|
|
use compliance_agent::pipeline::finding_store::RunFindings;
|
|
use compliance_core::models::{Finding, FindingStatus, ScanType, Severity};
|
|
use mongodb::bson::doc;
|
|
|
|
const TARGET: &str = "target-1";
|
|
|
|
/// A secret scanner finding of `rule` for `secret` at `file:line`, as the
|
|
/// scanner reports it.
|
|
fn secret(rule: &str, file: &str, line: u32, secret: &str) -> Finding {
|
|
let mut f = Finding::new(
|
|
TARGET.into(),
|
|
format!("{rule}-{file}-{line}"),
|
|
"gitleaks".into(),
|
|
ScanType::SecretDetection,
|
|
format!("Secret detected: {rule}"),
|
|
format!("Potential secret ({rule}) found in {file}:{line}. Match: {secret}"),
|
|
Severity::High,
|
|
);
|
|
f.rule_id = Some(rule.into());
|
|
f.file_path = Some(file.into());
|
|
f.line_number = Some(line);
|
|
f.fingerprint_content = Some(secret.into());
|
|
f.code_snippet = Some(secret.into());
|
|
f
|
|
}
|
|
|
|
/// One scan of `tree` reporting `findings`, as the pipeline saves it.
|
|
async fn scan(db: &Database, tree: &std::path::Path, mut findings: Vec<Finding>) -> RunFindings {
|
|
let mut run = RunFindings::new("run").with_tree(tree.to_path_buf());
|
|
run.completed("gitleaks");
|
|
run.label(&mut findings);
|
|
run.save_new(db, &mut findings).await.unwrap();
|
|
run.write_back(db, &mut findings).await.unwrap();
|
|
run.resolve_missing(db, TARGET, &[]).await.unwrap();
|
|
run
|
|
}
|
|
|
|
async fn rows(db: &Database, file: &str) -> Vec<Finding> {
|
|
use futures_util::TryStreamExt;
|
|
let mut rows: Vec<Finding> = db
|
|
.findings()
|
|
.find(doc! { "repo_id": TARGET, "file_path": file })
|
|
.await
|
|
.unwrap()
|
|
.try_collect()
|
|
.await
|
|
.unwrap();
|
|
rows.sort_by(|a, b| a.rule_id.cmp(&b.rule_id));
|
|
rows
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_secret_keeps_its_finding_when_its_rule_changes() {
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let prefix = format!(
|
|
"secretrules_{}",
|
|
&uuid::Uuid::new_v4().simple().to_string()[..10]
|
|
);
|
|
let Ok(pool) = DatabasePool::connect(&uri, &prefix).await else {
|
|
eprintln!("SKIP secret_rules: no MongoDB reachable at {uri}");
|
|
return;
|
|
};
|
|
let tenant = "secret-rules";
|
|
let Ok(db) = pool.for_tenant_id(tenant).await else {
|
|
eprintln!("SKIP secret_rules: MongoDB at {uri} not usable");
|
|
return;
|
|
};
|
|
let tree = std::env::temp_dir().join(format!("cs-secretrules-{}", uuid::Uuid::new_v4()));
|
|
std::fs::create_dir_all(&tree).unwrap();
|
|
let key = "aws_key = \"AKIAZ3MSJH5K4QV7XWRT\"";
|
|
let token = "token: ghp_0123456789abcdefghijABCDEFGHIJ012345";
|
|
std::fs::write(tree.join("cfg.py"), format!("{key}\n")).unwrap();
|
|
std::fs::write(tree.join("ci.yml"), format!("{token}\n")).unwrap();
|
|
|
|
// Arrange: gitleaks found the key and a token; a person accepted the
|
|
// risk of the key.
|
|
scan(
|
|
&db,
|
|
&tree,
|
|
vec![
|
|
secret("aws-access-token", "cfg.py", 1, key),
|
|
secret("github-pat", "ci.yml", 1, token),
|
|
],
|
|
)
|
|
.await;
|
|
let before = rows(&db, "cfg.py").await;
|
|
assert_eq!(before.len(), 1);
|
|
db.findings()
|
|
.update_one(
|
|
doc! { "_id": before[0].id },
|
|
doc! { "$set": { "status": "accepted_risk" } },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
// Act: Betterleaks reports the key under another rule; the token under
|
|
// its rule and, newly, a second one.
|
|
let run = scan(
|
|
&db,
|
|
&tree,
|
|
vec![
|
|
secret("generic-api-key", "cfg.py", 1, key),
|
|
secret("github-pat", "ci.yml", 1, token),
|
|
secret("generic-api-key", "ci.yml", 1, token),
|
|
],
|
|
)
|
|
.await;
|
|
|
|
// Assert: the key's row is the same, with its decision, under the new
|
|
// rule; nothing was closed or reopened.
|
|
let after = rows(&db, "cfg.py").await;
|
|
assert_eq!(after.len(), 1, "no new row for the key");
|
|
assert_eq!(after[0].id, before[0].id);
|
|
assert_eq!(after[0].status, FindingStatus::AcceptedRisk);
|
|
assert_eq!(after[0].rule_id.as_deref(), Some("generic-api-key"));
|
|
assert!(after[0]
|
|
.legacy_fingerprints
|
|
.contains(&before[0].fingerprint));
|
|
assert!(run.status_changed().is_empty(), "no close/reopen churn");
|
|
// The token is still reported under its rule: that row stays, and the
|
|
// new rule's finding is a finding of its own.
|
|
let token_rows = rows(&db, "ci.yml").await;
|
|
assert_eq!(token_rows.len(), 2);
|
|
assert_eq!(run.new_count(), 1);
|
|
|
|
let _ = std::fs::remove_dir_all(&tree);
|
|
pool.drop_tenant(tenant).await.ok();
|
|
}
|