-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscan_base_image.rs
More file actions
119 lines (107 loc) · 3.57 KB
/
scan_base_image.rs
File metadata and controls
119 lines (107 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use itertools::Itertools;
use tower_lsp::lsp_types::{Diagnostic, DiagnosticSeverity, Location, MessageType};
use crate::{
app::{
ImageScanner, LSPClient, LspInteractor, lsp_server::WithContext, markdown::MarkdownData,
},
domain::scanresult::severity::Severity,
};
use super::LspCommand;
pub struct ScanBaseImageCommand<'a, C, S: ?Sized>
where
S: ImageScanner,
{
image_scanner: &'a S,
interactor: &'a LspInteractor<C>,
location: Location,
image: String,
}
impl<'a, C, S: ?Sized> ScanBaseImageCommand<'a, C, S>
where
S: ImageScanner,
{
pub fn new(
image_scanner: &'a S,
interactor: &'a LspInteractor<C>,
location: Location,
image: String,
) -> Self {
Self {
image_scanner,
interactor,
location,
image,
}
}
}
#[async_trait::async_trait]
impl<'a, C, S: ?Sized> LspCommand for ScanBaseImageCommand<'a, C, S>
where
C: LSPClient + Sync,
S: ImageScanner + Sync,
{
async fn execute(&mut self) -> tower_lsp::jsonrpc::Result<()> {
let image_name = &self.image;
self.interactor
.show_message(
MessageType::INFO,
format!("Starting scan of {image_name}...").as_str(),
)
.await;
let scan_result = self
.image_scanner
.scan_image(image_name)
.await
.map_err(|e| tower_lsp::jsonrpc::Error::internal_error().with_message(e.to_string()))?;
self.interactor
.show_message(
MessageType::INFO,
format!("Finished scan of {image_name}.").as_str(),
)
.await;
let diagnostic = {
let mut diagnostic = Diagnostic {
range: self.location.range,
severity: Some(DiagnosticSeverity::HINT),
message: "No vulnerabilities found.".to_owned(),
..Default::default()
};
if !scan_result.vulnerabilities().is_empty() {
let vulns = scan_result
.vulnerabilities()
.iter()
.counts_by(|v| v.severity());
diagnostic.message = format!(
"Vulnerabilities found for {}: {} Critical, {} High, {} Medium, {} Low, {} Negligible",
image_name,
vulns.get(&Severity::Critical).unwrap_or(&0_usize),
vulns.get(&Severity::High).unwrap_or(&0_usize),
vulns.get(&Severity::Medium).unwrap_or(&0_usize),
vulns.get(&Severity::Low).unwrap_or(&0_usize),
vulns.get(&Severity::Negligible).unwrap_or(&0_usize),
);
diagnostic.severity = Some(if scan_result.evaluation_result().is_passed() {
DiagnosticSeverity::INFORMATION
} else {
DiagnosticSeverity::ERROR
});
}
diagnostic
};
let uri = self.location.uri.as_str();
self.interactor.remove_diagnostics(uri).await;
self.interactor.remove_documentations(uri).await;
self.interactor
.append_document_diagnostics(uri, &[diagnostic])
.await;
self.interactor.publish_all_diagnostics().await?;
self.interactor
.append_documentation(
self.location.uri.as_str(),
self.location.range,
MarkdownData::from(scan_result).to_string(),
)
.await;
Ok(())
}
}