Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions src/app/document_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ impl InMemoryDocumentDatabase {
self.read_document(uri).await.map(|e| e.text)
}

pub async fn remove_document(&self, uri: &str) {
self.documents.write().await.remove(uri);
}

pub async fn append_document_diagnostics(
&self,
uri: impl Into<String>,
Expand Down Expand Up @@ -164,17 +160,6 @@ mod tests {
assert_eq!(document.text, "updated");
}

#[tokio::test]
async fn test_remove_document() {
let db = InMemoryDocumentDatabase::default();

db.write_document_text("file://main.rs", "contents").await;
assert!(db.read_document("file://main.rs").await.is_some());

db.remove_document("file://main.rs").await;
assert!(db.read_document("file://main.rs").await.is_none());
}

#[tokio::test]
async fn test_add_diagnostics() {
let db = InMemoryDocumentDatabase::default();
Expand All @@ -200,13 +185,13 @@ mod tests {

db.append_document_diagnostics(
"file://mod1.rs",
&vec![create_diagnostic((0, 0), (0, 6), "Incorrect module name")],
&[create_diagnostic((0, 0), (0, 6), "Incorrect module name")],
)
.await;

db.append_document_diagnostics(
"file://mod2.rs",
&vec![
&[
create_diagnostic((0, 0), (0, 6), "Incorrect module name"),
create_diagnostic((0, 7), (0, 8), "Unexpected token"),
],
Expand Down
2 changes: 2 additions & 0 deletions src/app/image_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub trait ImageBuilder {
}

pub struct ImageBuildResult {
// FIXME(fede): Eventually we will need to check if this dead code is actually needed for our use case
#[allow(dead_code)]
pub image_id: String,
pub image_name: String,
}
Expand Down
32 changes: 0 additions & 32 deletions src/app/image_scanner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::error::Error;

use thiserror::Error;
use tracing::info;

use crate::domain::scanresult::scan_result::ScanResult;

Expand All @@ -10,37 +9,6 @@ pub trait ImageScanner {
async fn scan_image(&self, image_pull_string: &str) -> Result<ScanResult, ImageScanError>;
}

#[derive(Clone, Copy, Debug, Default)]
pub struct Vulnerabilities {
pub critical: usize,
pub high: usize,
pub medium: usize,
pub low: usize,
pub negligible: usize,
}

impl From<ScanResult> for Vulnerabilities {
fn from(value: ScanResult) -> Self {
value
.vulnerabilities()
.into_iter()
.fold(Self::default(), |mut acc, v| {
use crate::domain::scanresult::severity::Severity;
match v.severity() {
Severity::Critical => acc.critical += 1,
Severity::High => acc.high += 1,
Severity::Medium => acc.medium += 1,
Severity::Low => acc.low += 1,
Severity::Negligible => acc.negligible += 1,
Severity::Unknown => {
info!("unknown severity {:?}", v)
}
}
acc
})
}
}

#[derive(Error, Debug)]
pub enum ImageScanError {
#[error("error in the internal scanner execution: {0}")]
Expand Down
30 changes: 17 additions & 13 deletions src/app/lsp_server/command_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl From<CommandInfo> for CodeLens {
}
}

pub fn generate_commands_for_uri(uri: &Url, content: &str) -> Vec<CommandInfo> {
pub fn generate_commands_for_uri(uri: &Url, content: &str) -> Result<Vec<CommandInfo>, String> {
let file_uri = uri.as_str();

if file_uri.contains("docker-compose.yml")
Expand All @@ -65,24 +65,28 @@ pub fn generate_commands_for_uri(uri: &Url, content: &str) -> Vec<CommandInfo> {
{
generate_compose_commands(uri, content)
} else {
generate_dockerfile_commands(uri, content)
Ok(generate_dockerfile_commands(uri, content))
}
}

fn generate_compose_commands(url: &Url, content: &str) -> Vec<CommandInfo> {
fn generate_compose_commands(url: &Url, content: &str) -> Result<Vec<CommandInfo>, String> {
let mut commands = vec![];
if let Ok(instructions) = parse_compose_file(content) {
for instruction in instructions {
commands.push(
SupportedCommands::ExecuteBaseImageScan {
location: Location::new(url.clone(), instruction.range),
image: instruction.image_name,
}
.into(),
);
match parse_compose_file(content) {
Ok(instructions) => {
for instruction in instructions {
commands.push(
SupportedCommands::ExecuteBaseImageScan {
location: Location::new(url.clone(), instruction.range),
image: instruction.image_name,
}
.into(),
);
}
}
Err(err) => return Err(format!("{}", err)),
}
commands

Ok(commands)
}

fn generate_dockerfile_commands(uri: &Url, content: &str) -> Vec<CommandInfo> {
Expand Down
4 changes: 2 additions & 2 deletions src/app/lsp_server/lsp_server_inner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde_json::Value;
use tower_lsp::jsonrpc::{Error, ErrorCode, Result};
use tower_lsp::jsonrpc::{self, Error, ErrorCode, Result};
use tower_lsp::lsp_types::HoverContents::Markup;
use tower_lsp::lsp_types::MarkupKind::Markdown;
use tower_lsp::lsp_types::{
Expand Down Expand Up @@ -78,7 +78,7 @@ where
};

let commands = command_generator::generate_commands_for_uri(uri, &content);
Ok(commands)
commands.map_err(|e| jsonrpc::Error::internal_error().with_message(e))
}

pub async fn initialize(
Expand Down
9 changes: 1 addition & 8 deletions src/app/lsp_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tower_lsp::jsonrpc::{Error, Result};
use tower_lsp::lsp_types::{
CodeActionParams, CodeActionResponse, CodeLens, CodeLensParams, DidChangeConfigurationParams,
DidChangeTextDocumentParams, DidOpenTextDocumentParams, ExecuteCommandParams, Hover,
HoverParams, InitializeParams, InitializeResult, InitializedParams, Range,
HoverParams, InitializeParams, InitializeResult, InitializedParams,
};

use super::{InMemoryDocumentDatabase, LSPClient};
Expand Down Expand Up @@ -41,13 +41,6 @@ impl<C, F: ComponentFactory> LSPServer<C, F> {
}
}

struct CommandInfo {
title: String,
command: String,
arguments: Option<Vec<Value>>,
range: Range,
}

#[async_trait::async_trait]
impl<C, F> LanguageServer for LSPServer<C, F>
where
Expand Down
2 changes: 1 addition & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod queries;

pub use document_database::*;
pub use image_builder::{ImageBuildError, ImageBuildResult, ImageBuilder};
pub use image_scanner::{ImageScanError, ImageScanner, Vulnerabilities};
pub use image_scanner::{ImageScanError, ImageScanner};
pub use lsp_client::LSPClient;
pub use lsp_interactor::LspInteractor;
pub use lsp_server::LSPServer;
1 change: 1 addition & 0 deletions src/domain/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#![allow(dead_code)]
pub mod scanresult;
4 changes: 3 additions & 1 deletion src/infra/compose_ast_parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use thiserror::Error;
use tower_lsp::lsp_types::{Position, Range};

#[derive(Debug, PartialEq)]
Expand All @@ -6,8 +7,9 @@ pub struct ImageInstruction {
pub range: Range,
}

#[derive(Debug)]
#[derive(Debug, Error)]
pub enum ParseError {
#[error("Invalid yaml: {0}")]
InvalidYaml(marked_yaml::LoadError),
}

Expand Down
4 changes: 2 additions & 2 deletions src/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ mod sysdig_image_scanner_json_scan_result_v1;
pub use sysdig_image_scanner::{SysdigAPIToken, SysdigImageScanner};
pub mod lsp_logger;
pub use component_factory_impl::ConcreteComponentFactory;
pub use compose_ast_parser::{ImageInstruction, parse_compose_file};
pub use compose_ast_parser::parse_compose_file;
pub use docker_image_builder::DockerImageBuilder;
pub use dockerfile_ast_parser::{Instruction, parse_dockerfile};
pub use dockerfile_ast_parser::parse_dockerfile;
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(dead_code)]
#![cfg_attr(not(test), deny(clippy::unwrap_used))]
#![cfg_attr(not(test), deny(clippy::expect_used))]

Expand Down
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use clap::Parser;
use sysdig_lsp::{
use crate::{
app::LSPServer,
infra::{ConcreteComponentFactory, lsp_logger::LSPLogger},
};
use clap::Parser;
use tower_lsp::{LspService, Server};
use tracing_subscriber::layer::SubscriberExt;

mod app;
mod domain;
mod infra;

#[derive(Parser, Debug)]
#[command(version, author, about, long_about)]
struct Args {}
Expand Down