-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[implant] Add code quality tools #34
base: release/1.11.0
Are you sure you want to change the base?
Changes from all commits
b92109c
9e4fce9
b8b9461
bc12db1
45ddecd
db7ee9c
2a1eeac
5f98e25
88bc64a
5283cf0
feaf7e1
6c57fae
4eb0510
3a53a71
5645e3c
c617404
c704e43
6b9b2e7
be10a89
70d4534
6fb35df
5662fa8
585e904
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Code Quality Guidelines | ||
|
||
This document outlines the tools and standards used to maintain code quality in this project. Please follow these guidelines to ensure the codebase remains clean, efficient, and secure. | ||
|
||
## Quality Tools | ||
|
||
### Clippy | ||
|
||
Clippy is a Rust linter that provides a collection of lints to catch common mistakes and improve code quality. | ||
|
||
- **How to Run Clippy Locally:** | ||
To run Clippy, execute the following command: | ||
```bash | ||
cargo clippy -- -D warnings | ||
|
||
This will cause the build to fail if there are any warnings or errors. | ||
|
||
Clippy on CI: Clippy is run automatically on CI as part of the build process. Ensure that no warnings or errors are present before pushing your changes. | ||
|
||
### Rustfmt | ||
|
||
Rustfmt automatically formats Rust code to conform to style guidelines. | ||
|
||
- **How to Run Rustfmt Locally:** | ||
To check if your code is formatted properly, run: | ||
```bash | ||
cargo fmt -- --check | ||
|
||
This will not modify any files but will indicate if formatting is required. | ||
|
||
Rustfmt on CI: Rustfmt is run as part of the CI pipeline, and the build will fail if there are formatting issues. | ||
|
||
### Cargo Audit | ||
|
||
Cargo Audit checks for known vulnerabilities in the dependencies of your project. | ||
|
||
- **How to Run Cargo Audit Locally:** | ||
To check for security vulnerabilities, run: | ||
```bash | ||
cargo audit | ||
|
||
If any vulnerabilities are found, please resolve them before submitting your code. | ||
|
||
### Running Tests | ||
|
||
Unit tests and integration tests are run automatically on CI using the following command: | ||
|
||
- **How to Test Locally:** | ||
```bash | ||
cargo test | ||
|
||
Make sure your code passes all tests before pushing. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
use rustls::ClientConfig; | ||
use rustls_platform_verifier::BuilderVerifierExt; | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
use ureq::{Agent, Request}; | ||
|
@@ -15,16 +17,24 @@ pub struct Client { | |
} | ||
|
||
impl Client { | ||
pub fn new(server_url: String, token: String, unsecured_certificate: bool, with_proxy: bool) -> Client { | ||
pub fn new( | ||
server_url: String, | ||
token: String, | ||
unsecured_certificate: bool, | ||
with_proxy: bool, | ||
) -> Client { | ||
let mut http_client = ureq::AgentBuilder::new() | ||
.timeout_connect(Duration::from_secs(2)) | ||
.timeout(Duration::from_secs(5)) | ||
.user_agent(format!("openbas-implant/{}", VERSION).as_str()) | ||
.try_proxy_from_env(with_proxy); | ||
if unsecured_certificate { | ||
let arc_crypto_provider = Arc::new(rustls::crypto::ring::default_provider()); | ||
let config = rustls_platform_verifier::tls_config_with_provider(arc_crypto_provider) | ||
.expect("Failed to create TLS config with crypto provider"); | ||
let config = ClientConfig::builder_with_provider(arc_crypto_provider) | ||
.with_safe_default_protocol_versions() | ||
.unwrap() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Can we keep the expect here ? .expect("Failed to create TLS config with crypto provider") |
||
.with_platform_verifier() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Why adding this line ? |
||
.with_no_client_auth(); | ||
http_client = http_client.tls_config(Arc::new(config)); | ||
} | ||
// Remove trailing slash | ||
|
@@ -42,15 +52,17 @@ impl Client { | |
|
||
pub fn post(&self, route: &str) -> Request { | ||
let api_route = format!("{}{}", self.server_url, route); | ||
let request = self.http_client.post(&api_route) | ||
.set("Authorization", &format!("Bearer {}", self.token)); | ||
return request; | ||
|
||
self.http_client | ||
.post(&api_route) | ||
.set("Authorization", &format!("Bearer {}", self.token)) | ||
} | ||
|
||
pub fn get(&self, route: &str) -> Request { | ||
let api_route = format!("{}{}", self.server_url, route); | ||
let request = self.http_client.get(&api_route) | ||
.set("Authorization", &format!("Bearer {}", self.token)); | ||
return request; | ||
|
||
self.http_client | ||
.get(&api_route) | ||
.set("Authorization", &format!("Bearer {}", self.token)) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,35 +4,35 @@ use std::time::Instant; | |
|
||
use log::info; | ||
|
||
use crate::api::manage_inject::InjectorContractPayload; | ||
use crate::api::Client; | ||
use crate::api::manage_inject::{InjectorContractPayload}; | ||
use crate::handle::handle_execution::handle_execution_result; | ||
use crate::process::command_exec::command_execution; | ||
|
||
fn compute_working_dir() -> PathBuf { | ||
let current_exe_patch = env::current_exe().unwrap(); | ||
return current_exe_patch.parent().unwrap().to_path_buf(); | ||
current_exe_patch.parent().unwrap().to_path_buf() | ||
} | ||
|
||
pub fn compute_command(command: &String) -> String { | ||
let executable_command = command.clone(); | ||
pub fn compute_command(command: &str) -> String { | ||
let executable_command = command; | ||
let working_dir = compute_working_dir(); | ||
return executable_command.replace("#{location}", working_dir.to_str().unwrap()); | ||
executable_command.replace("#{location}", working_dir.to_str().unwrap()) | ||
} | ||
|
||
pub fn handle_execution_command( | ||
semantic: &str, | ||
api: &Client, | ||
inject_id: String, | ||
command: &String, | ||
executor: &String, | ||
executor: &str, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Why changing executor type and not command as well ? |
||
pre_check: bool, | ||
) -> i32 { | ||
let now = Instant::now(); | ||
info!("{} execution: {:?}", semantic, command); | ||
let command_result = command_execution(command.as_str(), executor.as_str(), pre_check); | ||
let command_result = command_execution(command.as_str(), executor, pre_check); | ||
let elapsed = now.elapsed().as_millis(); | ||
return handle_execution_result(semantic, api, inject_id, command_result, elapsed); | ||
handle_execution_result(semantic, api, inject_id, command_result, elapsed) | ||
} | ||
|
||
pub fn handle_command(inject_id: String, api: &Client, contract_payload: &InjectorContractPayload) { | ||
|
@@ -41,7 +41,7 @@ pub fn handle_command(inject_id: String, api: &Client, contract_payload: &Inject | |
let executable_command = compute_command(&command); | ||
let _ = handle_execution_command( | ||
"implant execution", | ||
&api, | ||
api, | ||
inject_id.clone(), | ||
&executable_command, | ||
&executor, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: Can we add the fact of putting the build in error if we have warnings during compilation?