use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] #[serde(rename_all = "lowercase")] pub enum TrackerType { GitHub, GitLab, Gitea, Jira, } impl std::fmt::Display for TrackerType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::GitHub => write!(f, "github"), Self::GitLab => write!(f, "gitlab"), Self::Gitea => write!(f, "gitea"), Self::Jira => write!(f, "jira"), } } } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] #[serde(rename_all = "snake_case")] pub enum IssueStatus { Open, InProgress, Closed, Resolved, } impl std::fmt::Display for IssueStatus { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Open => write!(f, "open"), Self::InProgress => write!(f, "in_progress"), Self::Closed => write!(f, "closed"), Self::Resolved => write!(f, "resolved"), } } } impl IssueStatus { /// Every status, open ones first. pub const ALL: [Self; 4] = [Self::Open, Self::InProgress, Self::Closed, Self::Resolved]; /// Whether the ticket is done (closed or resolved); the other two are /// open. pub fn is_closed(&self) -> bool { match self { Self::Closed | Self::Resolved => true, Self::Open | Self::InProgress => false, } } } /// What a ticket is about, as its `finding_id` says. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum IssueSubject<'a> { /// One finding, by its id (the hex of its `ObjectId`). Finding(&'a str), /// A known vulnerability in one target's components, by the target id. Cve { /// The target (repository) the CVE was found in. target_id: &'a str, }, } /// What the `finding_id` of a CVE ticket starts with. const CVE_KEY: &str = "cve:"; /// The start of the `finding_id` of every CVE ticket of one target. The whole /// key is `cve:{target_id}:{package}:{cve}`: one ticket per target, package /// and CVE. /// /// # Examples /// /// ``` /// use compliance_core::models::issue::cve_issue_prefix; /// assert_eq!(cve_issue_prefix("abc"), "cve:abc:"); /// ``` pub fn cve_issue_prefix(target_id: &str) -> String { format!("{CVE_KEY}{target_id}:") } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct TrackerIssue { #[serde(rename = "_id", skip_serializing_if = "Option::is_none")] pub id: Option, pub finding_id: String, pub tracker_type: TrackerType, pub external_id: String, pub external_url: String, pub title: String, pub status: IssueStatus, /// The tracker user who closed the issue there (#953), while it stays /// closed: a person's decision the scan does not undo on its own. #[serde(default, skip_serializing_if = "Option::is_none")] pub closed_in_tracker: Option, /// When a complete scan first no longer found the issue's CVE (#994), /// while it stays gone: the issue was told so once and stays open, since /// a CVE that is gone is no proof of a fix. #[serde( default, skip_serializing_if = "Option::is_none", with = "super::serde_helpers::opt_bson_datetime" )] pub gone_noted_at: Option>, #[serde(with = "super::serde_helpers::bson_datetime")] pub created_at: DateTime, #[serde(with = "super::serde_helpers::bson_datetime")] pub updated_at: DateTime, } impl TrackerIssue { pub fn new( finding_id: String, tracker_type: TrackerType, external_id: String, external_url: String, title: String, ) -> Self { let now = Utc::now(); Self { id: None, finding_id, tracker_type, external_id, external_url, title, status: IssueStatus::Open, closed_in_tracker: None, gone_noted_at: None, created_at: now, updated_at: now, } } /// What the ticket is about: its finding, or for a CVE ticket the target /// the CVE is in. `None` when `finding_id` is neither (empty, for an /// issue whose finding had no id). pub fn subject(&self) -> Option> { if let Some(rest) = self.finding_id.strip_prefix(CVE_KEY) { // `rest` is `{target_id}:{package}:{cve}`; `split` always yields // a first piece, so only an empty one is refused. return rest .split(':') .next() .filter(|id| !id.is_empty()) .map(|target_id| IssueSubject::Cve { target_id }); } let id = self.finding_id.as_str(); (id.len() == 24 && id.bytes().all(|b| b.is_ascii_hexdigit())) .then_some(IssueSubject::Finding(id)) } } #[cfg(test)] mod tests { use super::*; fn issue(finding_id: &str) -> TrackerIssue { TrackerIssue::new( finding_id.to_string(), TrackerType::Gitea, "913".into(), "https://gitea.invalid/o/r/issues/913".into(), "[high] semgrep: hardcoded key".into(), ) } #[test] fn a_finding_ticket_is_about_its_finding() { let id = "65a1b2c3d4e5f60718293a4b"; assert_eq!(issue(id).subject(), Some(IssueSubject::Finding(id))); } #[test] fn a_cve_ticket_is_about_its_target() { let key = format!("{}openssl:CVE-2024-0001", cve_issue_prefix("tgt1")); assert_eq!( issue(&key).subject(), Some(IssueSubject::Cve { target_id: "tgt1" }) ); } #[test] fn a_ticket_without_a_usable_id_is_about_nothing() { assert_eq!(issue("").subject(), None); assert_eq!(issue("cve::pkg:CVE-1").subject(), None); assert_eq!(issue("not-an-object-id").subject(), None); // The right length, but not hex. assert_eq!(issue("zzzzzzzzzzzzzzzzzzzzzzzz").subject(), None); } #[test] fn closed_and_resolved_are_closed() { let closed: Vec = IssueStatus::ALL .into_iter() .filter(IssueStatus::is_closed) .collect(); assert_eq!(closed, [IssueStatus::Closed, IssueStatus::Resolved]); } }