|
| 1 | +use crate::domain::scanresult::accepted_risk_reason::AcceptedRiskReason; |
| 2 | +use crate::domain::scanresult::package::Package; |
| 3 | +use crate::domain::scanresult::vulnerability::Vulnerability; |
| 4 | +use crate::domain::scanresult::weak_hash::WeakHash; |
| 5 | +use chrono::{DateTime, Utc}; |
| 6 | +use std::collections::HashSet; |
| 7 | +use std::fmt::Debug; |
| 8 | +use std::hash::{Hash, Hasher}; |
| 9 | +use std::sync::{Arc, RwLock}; |
| 10 | + |
| 11 | +pub struct AcceptedRisk { |
| 12 | + id: String, |
| 13 | + reason: AcceptedRiskReason, |
| 14 | + description: String, |
| 15 | + expiration_date: Option<DateTime<Utc>>, |
| 16 | + is_active: bool, |
| 17 | + created_at: DateTime<Utc>, |
| 18 | + updated_at: DateTime<Utc>, |
| 19 | + assigned_to_vulnerabilities: RwLock<HashSet<WeakHash<Vulnerability>>>, |
| 20 | + assigned_to_packages: RwLock<HashSet<WeakHash<Package>>>, |
| 21 | +} |
| 22 | + |
| 23 | +impl Debug for AcceptedRisk { |
| 24 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 25 | + f.debug_struct("AcceptedRisk") |
| 26 | + .field("id", &self.id) |
| 27 | + .field("reason", &self.reason) |
| 28 | + .field("description", &self.description) |
| 29 | + .field("expiration_date", &self.expiration_date) |
| 30 | + .field("is_active", &self.is_active) |
| 31 | + .field("created_at", &self.created_at) |
| 32 | + .field("updated_at", &self.updated_at) |
| 33 | + .finish() |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl AcceptedRisk { |
| 38 | + #[allow(clippy::too_many_arguments)] |
| 39 | + pub(in crate::domain::scanresult) fn new( |
| 40 | + id: String, |
| 41 | + reason: AcceptedRiskReason, |
| 42 | + description: String, |
| 43 | + expiration_date: Option<DateTime<Utc>>, |
| 44 | + is_active: bool, |
| 45 | + created_at: DateTime<Utc>, |
| 46 | + updated_at: DateTime<Utc>, |
| 47 | + ) -> Self { |
| 48 | + Self { |
| 49 | + id, |
| 50 | + reason, |
| 51 | + description, |
| 52 | + expiration_date, |
| 53 | + is_active, |
| 54 | + created_at, |
| 55 | + updated_at, |
| 56 | + assigned_to_vulnerabilities: RwLock::new(HashSet::new()), |
| 57 | + assigned_to_packages: RwLock::new(HashSet::new()), |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + pub fn id(&self) -> &str { |
| 62 | + &self.id |
| 63 | + } |
| 64 | + |
| 65 | + pub fn reason(&self) -> &AcceptedRiskReason { |
| 66 | + &self.reason |
| 67 | + } |
| 68 | + |
| 69 | + pub fn description(&self) -> &str { |
| 70 | + &self.description |
| 71 | + } |
| 72 | + |
| 73 | + pub fn expiration_date(&self) -> Option<DateTime<Utc>> { |
| 74 | + self.expiration_date |
| 75 | + } |
| 76 | + |
| 77 | + pub fn is_active(&self) -> bool { |
| 78 | + self.is_active |
| 79 | + } |
| 80 | + |
| 81 | + pub fn created_at(&self) -> DateTime<Utc> { |
| 82 | + self.created_at |
| 83 | + } |
| 84 | + |
| 85 | + pub fn updated_at(&self) -> DateTime<Utc> { |
| 86 | + self.updated_at |
| 87 | + } |
| 88 | + |
| 89 | + pub fn add_for_vulnerability(self: &Arc<Self>, vulnerability: Arc<Vulnerability>) { |
| 90 | + if self |
| 91 | + .assigned_to_vulnerabilities |
| 92 | + .write() |
| 93 | + .unwrap() |
| 94 | + .insert(WeakHash(Arc::downgrade(&vulnerability))) |
| 95 | + { |
| 96 | + vulnerability.add_accepted_risk(self.clone()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + pub fn assigned_to_vulnerabilities(self: &Arc<Self>) -> Vec<Arc<Vulnerability>> { |
| 101 | + self.assigned_to_vulnerabilities |
| 102 | + .read() |
| 103 | + .unwrap() |
| 104 | + .iter() |
| 105 | + .filter_map(|v| v.0.upgrade()) |
| 106 | + .collect() |
| 107 | + } |
| 108 | + |
| 109 | + pub fn add_for_package(self: &Arc<Self>, a_package: Arc<Package>) { |
| 110 | + if self |
| 111 | + .assigned_to_packages |
| 112 | + .write() |
| 113 | + .unwrap() |
| 114 | + .insert(WeakHash(Arc::downgrade(&a_package))) |
| 115 | + { |
| 116 | + a_package.add_accepted_risk(self.clone()); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + pub fn assigned_to_packages(self: &Arc<Self>) -> Vec<Arc<Package>> { |
| 121 | + self.assigned_to_packages |
| 122 | + .read() |
| 123 | + .unwrap() |
| 124 | + .iter() |
| 125 | + .filter_map(|p| p.0.upgrade()) |
| 126 | + .collect() |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl PartialEq for AcceptedRisk { |
| 131 | + fn eq(&self, other: &Self) -> bool { |
| 132 | + self.id == other.id |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +impl Eq for AcceptedRisk {} |
| 137 | + |
| 138 | +impl Hash for AcceptedRisk { |
| 139 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 140 | + self.id.hash(state); |
| 141 | + } |
| 142 | +} |
0 commit comments