Skip to content
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

[tool] Add code quality tools to ci #36

Open
wants to merge 100 commits into
base: release/1.11.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
63de9f4
[tool] Update pipeline to fail whenever there is a warning during com…
savacano28 Dec 18, 2024
6ade698
[tool] Update pipeline to fail whenever there is a warning during com…
savacano28 Dec 19, 2024
ff568a9
[tool] Update pipeline to fail whenever there is a warning during com…
savacano28 Dec 19, 2024
29924c2
[tool] Update pipeline to fail whenever there is a warning during com…
savacano28 Dec 19, 2024
1741d4d
[tool] Update pipeline with code quality tools
savacano28 Dec 23, 2024
265b223
[tool] Update circle ci config with code quality tools
savacano28 Dec 23, 2024
d6ec86f
[agent] remove deprecated method
savacano28 Dec 23, 2024
fba0b32
[tool] Update circle ci config with code quality tools
savacano28 Dec 23, 2024
03f7d6c
[agent] remove deprecated method
savacano28 Dec 23, 2024
428b02e
[tool] Update circle ci config with code quality tools
savacano28 Dec 23, 2024
d2eb25f
[tool] Update circle ci config with code quality tools
savacano28 Dec 23, 2024
b95a551
[tool] Update circle ci config with code quality tools
savacano28 Dec 23, 2024
d42497b
[tool] Update circle ci config with code quality tools
savacano28 Dec 23, 2024
8148f0a
[tool] Update circle ci config with code quality tools
savacano28 Dec 23, 2024
1c283f2
[agent] remove deprecated method
savacano28 Dec 23, 2024
f7c5306
[agent] clean
savacano28 Dec 23, 2024
9a6a2ea
[agent] clean
savacano28 Dec 23, 2024
3531c8e
[agent] clean
savacano28 Dec 23, 2024
4f595cf
[agent] clean
savacano28 Dec 23, 2024
43dd3ca
[agent] clean
savacano28 Dec 23, 2024
ed2ff6c
[agent] clean
savacano28 Dec 23, 2024
7642d38
[agent] clean
savacano28 Dec 23, 2024
93a60cf
[agent] clean
savacano28 Dec 23, 2024
02fb4be
[agent] clean
savacano28 Dec 23, 2024
699ab09
[agent] clean
savacano28 Dec 23, 2024
f5d6dd0
[agent] clean
savacano28 Dec 23, 2024
c1a040f
[agent] clean
savacano28 Dec 23, 2024
4c19737
[agent] clean
savacano28 Dec 24, 2024
c67513e
[agent] clean
savacano28 Dec 24, 2024
b4cd1a2
[agent] clean
savacano28 Dec 24, 2024
ee9ed25
[agent] clean
savacano28 Dec 24, 2024
58ef993
[agent] clean
savacano28 Dec 24, 2024
a1cd97a
[agent] clean
savacano28 Dec 24, 2024
947276a
[agent] clean
savacano28 Dec 24, 2024
3ac0efb
[agent] clean
savacano28 Dec 24, 2024
db0bce1
[agent] clean
savacano28 Dec 24, 2024
6767bbc
[agent] clean
savacano28 Dec 24, 2024
37d68f8
[agent] clean
savacano28 Dec 24, 2024
d4af305
[agent] clean
savacano28 Dec 24, 2024
9108f0c
[agent] clean
savacano28 Dec 24, 2024
dd2b7d1
[agent] clean
savacano28 Dec 24, 2024
61d672b
[agent] clean
savacano28 Dec 24, 2024
8e6cc91
[agent] clean
savacano28 Dec 24, 2024
8d5aa88
[agent] clean
savacano28 Dec 24, 2024
cb489d6
[agent] clean
savacano28 Dec 24, 2024
4595d21
[agent] clean
savacano28 Dec 24, 2024
bff58ea
[agent] clean
savacano28 Dec 24, 2024
36c2e5e
[agent] clean
savacano28 Dec 24, 2024
b1ebb2a
[agent] clean
savacano28 Dec 24, 2024
f25f340
[agent] clean
savacano28 Dec 24, 2024
3f0df14
[agent] clean
savacano28 Dec 24, 2024
352967f
[agent] clean
savacano28 Dec 24, 2024
e72f0bf
[agent] clean
savacano28 Dec 24, 2024
140b439
[agent] clean
savacano28 Dec 24, 2024
385ec1c
[agent] clean
savacano28 Dec 24, 2024
158c067
[agent] clean
savacano28 Dec 24, 2024
457548a
[agent] clean
savacano28 Dec 24, 2024
c8264aa
[agent] clean
savacano28 Dec 24, 2024
cb75a99
[agent] clean
savacano28 Jan 2, 2025
cdeec48
[agent] clean
savacano28 Jan 2, 2025
f087ead
[agent] clean
savacano28 Jan 2, 2025
82565ed
[agent] clean
savacano28 Jan 2, 2025
df784d7
[agent] clean
savacano28 Jan 2, 2025
f34f3da
[agent] clean
savacano28 Jan 2, 2025
6768e1c
[agent] clean
savacano28 Jan 2, 2025
e166dbe
[agent] clean
savacano28 Jan 2, 2025
bce53c3
[agent] clean
savacano28 Jan 2, 2025
bbaf047
[agent] clean
savacano28 Jan 3, 2025
e458252
[agent] clean
savacano28 Jan 3, 2025
d87bd79
[agent] clean
savacano28 Jan 3, 2025
ce97d1e
[agent] clean
savacano28 Jan 3, 2025
c48ff87
[agent] clean
savacano28 Jan 3, 2025
9b7b805
[agent] clean
savacano28 Jan 3, 2025
8de9cce
[agent] clean
savacano28 Jan 3, 2025
304bd62
[agent] clean
savacano28 Jan 3, 2025
a25bed5
[agent] clean
savacano28 Jan 3, 2025
9b1f91f
[agent] clean
savacano28 Jan 3, 2025
aa1fe7b
[agent] clean
savacano28 Jan 3, 2025
92eab54
[agent] Clean
savacano28 Jan 7, 2025
3610fd8
[agent] Clean
savacano28 Jan 7, 2025
7f8154e
[agent] Clean
savacano28 Jan 8, 2025
27ee1f7
[agent] Clean
savacano28 Jan 8, 2025
e274d1b
[agent] Clean
savacano28 Jan 8, 2025
1249ac4
[agent] Clean
savacano28 Jan 8, 2025
ecc93bd
[agent] Clean
savacano28 Jan 8, 2025
0d4d860
[agent] Clean
savacano28 Jan 8, 2025
2108205
[agent] Clean
savacano28 Jan 8, 2025
0d45df8
[agent] Clean
savacano28 Jan 8, 2025
766acc6
[agent] Clean
savacano28 Jan 8, 2025
57a4a16
[agent] Clean
savacano28 Jan 8, 2025
fad063c
[agent] Clean
savacano28 Jan 8, 2025
89aef73
[agent] Clean
savacano28 Jan 8, 2025
ac5c49f
[agent] Clean
savacano28 Jan 8, 2025
49530c1
[agent] Clean
savacano28 Jan 9, 2025
ecb6317
[agent] Clean
savacano28 Jan 9, 2025
29688c1
[agent] Clean
savacano28 Jan 9, 2025
1d527bf
[agent] Clean
savacano28 Jan 9, 2025
eb1d24e
[agent] Clean
savacano28 Jan 9, 2025
a8b3554
[agent] Clean
savacano28 Jan 9, 2025
1a3f12f
[agent] Clean
savacano28 Jan 9, 2025
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
74 changes: 72 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,23 @@ jobs:
keys:
- cargo-{{ arch }}-{{ checksum "Cargo.toml" }}
- cargo-{{ arch }}
- run: choco install -y mingw nsis
- run: curl.exe --output rustup-init.exe --url https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-gnu/rustup-init.exe
- run: curl.exe --output rustup-init.exe --url https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe
- run: ./rustup-init.exe -vy
- run: rustup toolchain install stable-x86_64-pc-windows-msvc
- run: rustup default stable-x86_64-pc-windows-msvc
- run: choco uninstall rust
- run: choco install -y mingw nsis
# Install quality tools
- run: |
rustup component add clippy
rustup component add rustfmt
cargo install cargo-audit
# Run checks
- run: cargo check
- run: cargo clippy -- -D warnings
- run: cargo fmt -- --check
- run: cargo audit
- run: cargo test --release
- run: cargo build --release
- save_cache:
key: cargo-{{ arch }}-{{ checksum "Cargo.toml" }}
Expand Down Expand Up @@ -66,6 +78,20 @@ jobs:
- run: ./rustup-init.exe -vy
- run: Invoke-Expression '& "$env:USERPROFILE\.cargo\bin\rustup" toolchain install stable-aarch64-pc-windows-msvc'
- run: Invoke-Expression '& "$env:USERPROFILE\.cargo\bin\rustup" default stable-aarch64-pc-windows-msvc'
# Install quality tools
- run: |
$env:PATH = "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\Llvm\ARM64\bin;" + $env:PATH;
rustup component add clippy
rustup component add rustfmt
cargo install cargo-audit --force
# Run checks
- run: $env:PATH = "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\Llvm\ARM64\bin;" + $env:PATH; Invoke-Expression '& "$env:USERPROFILE\.cargo\bin\cargo" check'
- run: $env:PATH = "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\Llvm\ARM64\bin;" + $env:PATH; Invoke-Expression '& "$env:USERPROFILE\.cargo\bin\cargo" clippy -- -D warnings'
- run: $env:PATH = "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\Llvm\ARM64\bin;" + $env:PATH; Invoke-Expression '& "$env:USERPROFILE\.cargo\bin\cargo" fmt -- --check'
- run: git config --global --unset url.ssh://[email protected]
- run: cargo audit
- run: git config --global url.ssh://[email protected] https://github.com/
- run: $env:PATH = "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\Llvm\ARM64\bin;" + $env:PATH; Invoke-Expression '& "$env:USERPROFILE\.cargo\bin\cargo" cargo test --release'
- run: $env:PATH = "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\Llvm\ARM64\bin;" + $env:PATH; Invoke-Expression '& "$env:USERPROFILE\.cargo\bin\cargo" build --release'
- save_cache:
key: cargo-{{ arch }}-{{ checksum "Cargo.toml" }}
Expand Down Expand Up @@ -115,6 +141,17 @@ jobs:
- run: sudo apt-get -y install curl musl-tools
- run: curl https://sh.rustup.rs -sSf | sh -s -- -y
- run: . "$HOME/.cargo/env"; rustup target add x86_64-unknown-linux-musl
# Install quality tools
- run: |
rustup component add clippy
rustup component add rustfmt
cargo install cargo-audit
# Run checks
- run: cargo check
- run: cargo clippy -- -D warnings
- run: cargo fmt -- --check
- run: cargo audit
- run: cargo test --release
- run: . "$HOME/.cargo/env"; cargo build --target=x86_64-unknown-linux-musl --release
- run: strip ./target/x86_64-unknown-linux-musl/release/openbas-agent
- save_cache:
Expand Down Expand Up @@ -161,6 +198,17 @@ jobs:
- run: sudo apt-get -y install curl musl-tools
- run: curl https://sh.rustup.rs -sSf | sh -s -- -y
- run: . "$HOME/.cargo/env"; rustup target add aarch64-unknown-linux-musl
# Install quality tools
- run: |
rustup component add clippy
rustup component add rustfmt
cargo install cargo-audit
# Run checks
- run: cargo check
- run: cargo clippy -- -D warnings
- run: cargo fmt -- --check
- run: cargo audit
- run: cargo test --release
- run: . "$HOME/.cargo/env"; cargo build --target=aarch64-unknown-linux-musl --release
- run: strip ./target/aarch64-unknown-linux-musl/release/openbas-agent
- save_cache:
Expand Down Expand Up @@ -205,6 +253,17 @@ jobs:
- cargo-{{ arch }}-{{ checksum "Cargo.toml" }}
- cargo-{{ arch }}
- run: curl https://sh.rustup.rs -sSf | sh -s -- -y
# Install quality tools
- run: |
rustup component add clippy
rustup component add rustfmt
cargo install cargo-audit
# Run checks
- run: cargo check
- run: cargo clippy -- -D warnings
- run: cargo fmt -- --check
- run: cargo audit
- run: cargo test --release
- run: . "$HOME/.cargo/env"; cargo build --release
- run: strip ./target/release/openbas-agent
- save_cache:
Expand Down Expand Up @@ -245,6 +304,17 @@ jobs:
- cargo-{{ arch }}-{{ checksum "Cargo.toml" }}
- cargo-{{ arch }}
- run: curl https://sh.rustup.rs -sSf | sh -s -- -y
# Install quality tools
- run: |
rustup component add clippy
rustup component add rustfmt
cargo install cargo-audit
# Run checks
- run: cargo check
- run: cargo clippy -- -D warnings
- run: cargo fmt -- --check
- run: cargo audit
- run: cargo test --release
- run: . "$HOME/.cargo/env"; cargo build --release
- run: strip ./target/release/openbas-agent
- save_cache:
Expand Down
8 changes: 8 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ assignees: ''

---

## Context

<!--
- Why is this solution needed?
- What value or benefits will the end-users gain from this change?
- Is this change a technical improvement? just for internal use, or does it impact users as well?
-->

## Use case

<!-- Please describe the use case for which you need a solution -->
Expand Down
52 changes: 52 additions & 0 deletions CODE_QUALITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Code Quality Guidelines
savacano28 marked this conversation as resolved.
Show resolved Hide resolved

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](https://doc.rust-lang.org/clippy/usage.html) 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](https://doc.rust-lang.org/clippy/development/adding_lints.html#running-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](https://docs.rs/cargo-audit/latest/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.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
ureq = { version = "2.1", features = ["tls", "json"] }
config = "0.14.0"
serde = { version = "1.0.203", features = ["derive"] }
log = { version = "0.4.21", features = ["kv"] }
log = { version = "0.4.22", features = ["kv"] }
hostname = "0.4.0"
network-interface = "2.0.0"
signal-hook = "0.3.17"
Expand All @@ -18,7 +18,7 @@ tracing-subscriber = { version = "0.3.18", features = ["json"] }
tracing-appender = "0.2.3"
rolling-file = "0.2.0"
rustls = { version ="0.23.13", features = ["ring"], default-features = false }
rustls-platform-verifier = "0.4"
rustls-platform-verifier = "0.5.0"

[target.'cfg(windows)'.dependencies]
windows-service = "0.7.0"
Expand Down
34 changes: 17 additions & 17 deletions src/api/manage_jobs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::Deserialize;
use crate::common::error_model::Error;
use serde::Deserialize;

use super::Client;

Expand All @@ -13,7 +13,12 @@ pub struct JobResponse {
}

impl Client {
pub fn list_jobs(&self, is_service: bool, is_elevated: bool, executed_by_user: String) -> Result<Vec<JobResponse>, Error> {
pub fn list_jobs(
&self,
is_service: bool,
is_elevated: bool,
executed_by_user: String,
) -> Result<Vec<JobResponse>, Error> {
// Post the input to the OpenBAS API
let post_data = ureq::json!({
"asset_external_reference": mid::get("openbas").unwrap(),
Expand All @@ -22,29 +27,24 @@ impl Client {
"agent_executed_by_user": executed_by_user
});
match self.post("/api/endpoints/jobs").send_json(post_data) {
Ok(response) => {
Ok(response.into_json()?)
}
Ok(response) => Ok(response.into_json()?),
Err(ureq::Error::Status(_, response)) => {
Err(Error::Api(response.into_string().unwrap()))
},
Err(err) => {
Err(Error::Internal(err.to_string()))
}
Err(err) => Err(Error::Internal(err.to_string())),
}
}
pub fn clean_job(&self, job_id: &str) -> Result<(), Error> {
// Post the input to the OpenBAS API
return match self.delete(&format!("/api/endpoints/jobs/{}", job_id)).call() {
Ok(_) => {
Ok(())
}
match self
.delete(&format!("/api/endpoints/jobs/{}", job_id))
.call()
{
Ok(_) => Ok(()),
Err(ureq::Error::Status(_, response)) => {
Err(Error::Api(response.into_string().unwrap()))
},
Err(err) => {
Err(Error::Internal(err.to_string()))
}
};
Err(err) => Err(Error::Internal(err.to_string())),
}
}
}
}
34 changes: 23 additions & 11 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use rustls::ClientConfig;
use rustls_platform_verifier::BuilderVerifierExt;
use std::sync::Arc;
use std::time::Duration;
use ureq::{Agent, Request};

mod register_agent;
mod manage_jobs;
mod register_agent;

const VERSION: &str = env!("CARGO_PKG_VERSION");

Expand All @@ -15,18 +17,28 @@ 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-agent/{}", 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)
savacano28 marked this conversation as resolved.
Show resolved Hide resolved
.with_safe_default_protocol_versions()
.expect("Failed to create TLS config with crypto provider")
.with_platform_verifier()
.with_no_client_auth();

http_client = http_client.tls_config(Arc::new(config));
}

// Remove trailing slash
let mut url = server_url;
if url.ends_with('/') {
Expand All @@ -42,15 +54,15 @@ 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));
request
self.http_client
.post(&api_route)
.set("Authorization", &format!("Bearer {}", self.token))
}

pub fn delete(&self, route: &str) -> Request {
let api_route = format!("{}{}", self.server_url, route);
let request = self.http_client.delete(&api_route)
.set("Authorization", &format!("Bearer {}", self.token));
request
self.http_client
.delete(&api_route)
.set("Authorization", &format!("Bearer {}", self.token))
}
}
}
22 changes: 12 additions & 10 deletions src/api/register_agent.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::Client;
use crate::common::error_model::Error;
use network_interface::NetworkInterface;
use network_interface::NetworkInterfaceConfig;
use serde::Deserialize;
use std::env;
use super::Client;

const VERSION: &str = env!("CARGO_PKG_VERSION");

Expand All @@ -26,31 +26,33 @@ pub fn get_arch() -> String {
"aarch64" => "arm64", // Mac still use the old nomenclature
other => other,
};
return String::from(arch);
String::from(arch)
}

pub fn get_operating_system() -> String {
let os = match env::consts::OS {
match env::consts::OS {
"macos" => String::from("MacOS"),
other => capitalize(other),
};
return os;
}
}

impl Client {
pub fn register_agent(&self, is_service: bool, is_elevated: bool, executed_by_user: String) -> Result<RegisterAgentResponse, Error> {
pub fn register_agent(
&self,
is_service: bool,
is_elevated: bool,
executed_by_user: String,
) -> Result<RegisterAgentResponse, Error> {
// region Build the content to register
let networks = NetworkInterface::show().unwrap();
let mac_addresses: Vec<String> = networks
.iter()
.map(|interface| &interface.mac_addr)
.filter(|mac_opts| mac_opts.is_some())
.map(|mac| mac.clone().unwrap())
.filter_map(|mac| mac.clone())
savacano28 marked this conversation as resolved.
Show resolved Hide resolved
.collect();
let ip_addresses: Vec<String> = networks
.iter()
.map(|interface| &interface.addr)
.flatten()
.flat_map(|interface| &interface.addr)
.map(|addr| addr.ip().to_string())
.collect();
let post_data = ureq::json!({
Expand Down
Loading