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

[implant] Add code quality tools #34

Merged
merged 38 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
566522e
[agent] Add code quality tools
savacano28 Dec 23, 2024
e23561e
[implant] Add code quality tools
savacano28 Dec 23, 2024
f50916e
[implant] Add code quality tools
savacano28 Dec 23, 2024
661d58b
[implant] Add code quality tools
savacano28 Dec 23, 2024
d73feaf
[implant] Add code quality tools
savacano28 Dec 23, 2024
152d100
[implant] Add code quality tools
savacano28 Dec 23, 2024
3526f62
[implant] Add code quality tools
savacano28 Dec 23, 2024
f51aedb
[implant] Add code quality tools
savacano28 Dec 23, 2024
4a73597
[implant] clean
savacano28 Dec 23, 2024
260c8de
[implant] clean
savacano28 Dec 23, 2024
231ee38
[implant] clean
savacano28 Dec 23, 2024
5d800bc
[implant] clean
savacano28 Dec 23, 2024
34f5868
[implant] clean
savacano28 Dec 24, 2024
71b8b60
[implant] clean
savacano28 Dec 24, 2024
3173c5d
[implant] clean
savacano28 Dec 24, 2024
7047f4e
[implant] clean
savacano28 Dec 24, 2024
515546d
[implant] clean
savacano28 Dec 24, 2024
9766a29
[implant] clean
savacano28 Dec 24, 2024
8a82a7b
[implant] clean
savacano28 Dec 24, 2024
437ecee
[implant] clean
savacano28 Dec 24, 2024
49387f0
[implant] clean
savacano28 Dec 24, 2024
bce5520
[implant] clean
savacano28 Dec 24, 2024
2073640
[implant] clean
savacano28 Dec 24, 2024
7d0953f
[implant] clean
savacano28 Jan 7, 2025
154cd2d
[implant] clean
savacano28 Jan 7, 2025
b7ccbd0
[implant] Clean
savacano28 Jan 8, 2025
48105e7
[implant] Clean
savacano28 Jan 8, 2025
4a0aa21
[implant] Clean
savacano28 Jan 8, 2025
6405dd0
[implant] Clean
savacano28 Jan 8, 2025
f93f390
[implant] Clean
savacano28 Jan 8, 2025
47e1cc3
[agent] Clean
savacano28 Jan 8, 2025
02b726c
[implant] Clean
savacano28 Jan 9, 2025
85ccd1b
[implant] Clean
savacano28 Jan 9, 2025
097bb2b
[implant] Clean
savacano28 Jan 9, 2025
c3d3147
[implant] Clean
savacano28 Jan 9, 2025
1d3af85
[implant] Improve doc
savacano28 Jan 22, 2025
332fee1
[implant] Clean
savacano28 Jan 23, 2025
faef858
[implant] Add cargo test for build jobs
savacano28 Jan 24, 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
80 changes: 78 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@ 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 build --release
- run: cargo test --release
- save_cache:
key: cargo-{{ arch }}-{{ checksum "Cargo.toml" }}
paths:
Expand All @@ -37,6 +49,7 @@ jobs:
- run: rustup toolchain install stable-x86_64-pc-windows-msvc
- run: rustup default stable-x86_64-pc-windows-msvc
- run: cargo build --release
- run: cargo test --release
- run:
name: push to jfrog
command: |
Expand All @@ -62,7 +75,21 @@ 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;
savacano28 marked this conversation as resolved.
Show resolved Hide resolved
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" build --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" test --release'
- save_cache:
key: cargo-{{ arch }}-{{ checksum "Cargo.toml" }}
paths:
Expand All @@ -84,6 +111,7 @@ jobs:
- 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'
- 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'
- 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" test --release'
- run:
name: push to jfrog
command: |
Expand All @@ -106,7 +134,18 @@ 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
RomuDeuxfois marked this conversation as resolved.
Show resolved Hide resolved
- 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: . "$HOME/.cargo/env"; cargo build --target=x86_64-unknown-linux-musl --release
- run: . "$HOME/.cargo/env"; cargo test --release
- run: strip ./target/x86_64-unknown-linux-musl/release/openbas-implant
- save_cache:
key: cargo-{{ arch }}-{{ checksum "Cargo.toml" }}
Expand All @@ -126,6 +165,7 @@ jobs:
- run: curl https://sh.rustup.rs -sSf | sh -s -- -y
- run: . "$HOME/.cargo/env"; rustup target add x86_64-unknown-linux-musl
- run: . "$HOME/.cargo/env"; cargo build --target=x86_64-unknown-linux-musl --release
- run: . "$HOME/.cargo/env"; cargo test --release
- run: strip ./target/x86_64-unknown-linux-musl/release/openbas-implant
- run:
name: push to jfrog
Expand All @@ -150,7 +190,18 @@ 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: . "$HOME/.cargo/env"; cargo build --target=aarch64-unknown-linux-musl --release
- run: . "$HOME/.cargo/env"; cargo test --release
- run: strip ./target/aarch64-unknown-linux-musl/release/openbas-implant
- save_cache:
key: cargo-{{ arch }}-{{ checksum "Cargo.toml" }}
Expand All @@ -171,6 +222,7 @@ jobs:
- run: curl https://sh.rustup.rs -sSf | sh -s -- -y
- run: . "$HOME/.cargo/env"; rustup target add aarch64-unknown-linux-musl
- run: . "$HOME/.cargo/env"; cargo build --target=aarch64-unknown-linux-musl --release
- run: . "$HOME/.cargo/env"; cargo test --release
- run: strip ./target/aarch64-unknown-linux-musl/release/openbas-implant
- run:
name: push to jfrog
Expand All @@ -192,7 +244,18 @@ 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: . "$HOME/.cargo/env"; cargo build --release
- run: . "$HOME/.cargo/env"; cargo test --release
- run: strip ./target/release/openbas-implant
- save_cache:
key: cargo-{{ arch }}-{{ checksum "Cargo.toml" }}
Expand All @@ -210,6 +273,7 @@ jobs:
- cargo-{{ arch }}
- run: curl https://sh.rustup.rs -sSf | sh -s -- -y
- run: . "$HOME/.cargo/env"; cargo build --release
- run: . "$HOME/.cargo/env"; cargo test --release
- run: strip ./target/release/openbas-implant
- run:
name: push to jfrog
Expand All @@ -230,7 +294,18 @@ 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: . "$HOME/.cargo/env"; cargo build --release
- run: . "$HOME/.cargo/env"; cargo test --release
- run: strip ./target/release/openbas-implant
- save_cache:
key: cargo-{{ arch }}-{{ checksum "Cargo.toml" }}
Expand All @@ -247,6 +322,7 @@ jobs:
- cargo-{{ arch }}
- run: curl https://sh.rustup.rs -sSf | sh -s -- -y
- run: . "$HOME/.cargo/env"; cargo build --release
- run: . "$HOME/.cargo/env"; cargo test --release
- run: strip ./target/release/openbas-implant
- run:
name: push to jfrog
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
73 changes: 73 additions & 0 deletions CODE_QUALITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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](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:**
```bash
cargo clippy -- -D warnings
```
The -D warnings flag tells Clippy to deny all warnings. This will cause the build to fail if there are any warnings, ensuring that your codebase is free of common issues and follows best practices.

**How to Fix Clippy Errors:**
1. Review the message Clippy provides. It typically points to the specific line of code and describes the issue.
2. Fix the reported issue manually. Clippy often provides suggestions or hints about how to resolve the issue (e.g., removing unused variables, changing inefficient code patterns).

**Automatically Fix Simple Clippy Issues:**
Some common Clippy warnings can be automatically fixed using the cargo fix command:
```bash
cargo fix --clippy
```
This command will automatically apply fixes for the majority of Clippy lints that can be safely corrected. After running this command, you can re-run cargo clippy to verify that the issues have been resolved.

Clippy on CI: Clippy is run automatically as part of the CI process. The build will fail if any warnings or errors are found.

### 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:**
```bash
cargo fmt -- --check
```

This command will not modify any files but will indicate if any files are not correctly formatted. If Rustfmt reports any differences, you can run to automatically format your code:
```bash
cargo fmt
```

Rustfmt on CI: Rustfmt is run as part of the CI pipeline.

### 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:**
```bash
cargo audit
```
This will audit your project’s dependencies and check for any known vulnerabilities. If vulnerabilities are found, you'll see details about the affected packages and versions.

**How to Fix Vulnerabilities:**

```bash
cargo update
```
In case an update doesn't resolve the issue, look for advice on upgrading to a non-vulnerable version or consider using alternative packages.

Cargo Audit on CI: Cargo Audit should be included in the CI pipeline to ensure that no vulnerabilities are introduced into the codebase.

### Running Tests

Unit tests and integration tests are run automatically on CI using the following command:

**How to Test Locally:**
```bash
cargo test
```
Tests on CI: All tests should be executed as part of the CI pipeline. If any tests fail, the build should fail.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ regex = "1.10.5"
serde_json = "1.0.117"
mailparse = "0.15.0"
rustls = { version ="0.23.13", features = ["ring"], default-features = false }
rustls-platform-verifier = "0.5"
rustls-platform-verifier = "0.5.0"
34 changes: 21 additions & 13 deletions src/api/manage_inject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{env, fs};
use std::fs::File;
use std::io::{BufWriter, Write};
use std::{env, fs};

use mailparse::{parse_content_disposition, parse_header};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -68,15 +68,20 @@ pub struct UpdateInput {
}

impl Client {

pub fn get_executable_payload(&self, inject_id: String) -> Result<InjectorContractPayload, Error> {
return match self.get(&format!("/api/injects/{}/executable-payload", inject_id)).call() {
pub fn get_executable_payload(
&self,
inject_id: String,
) -> Result<InjectorContractPayload, Error> {
match self
.get(&format!("/api/injects/{}/executable-payload", inject_id))
.call()
{
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())),
};
}
}

pub fn update_status(
Expand All @@ -87,7 +92,10 @@ impl Client {
) -> Result<UpdateInjectResponse, Error> {
let post_data = ureq::json!(input);
match self
.post(&format!("/api/injects/execution/{}/callback/{}", agent_id, inject_id))
.post(&format!(
"/api/injects/execution/{}/callback/{}",
agent_id, inject_id
))
.send_json(post_data)
{
Ok(response) => Ok(response.into_json()?),
Expand All @@ -99,7 +107,7 @@ impl Client {
}

pub fn download_file(&self, document_id: &String, in_memory: bool) -> Result<String, Error> {
return match self
match self
.get(&format!("/api/documents/{}/file", document_id))
.call()
{
Expand All @@ -112,26 +120,26 @@ impl Client {
let executable_path = current_exe_patch.parent().unwrap();
let name = dis.params.get("filename").unwrap();
let file_directory = executable_path.join(name);
return if in_memory {
if in_memory {
let buf = BufWriter::new(Vec::new());
let _ = write_response(buf, response);
Ok(String::from(name))
} else {
let output_file = File::create(file_directory.clone()).unwrap();
let file_write = write_response(output_file, response);
return match file_write {
match file_write {
Ok(_) => Ok(String::from(name)),
Err(err) => {
let _ = fs::remove_file(file_directory.clone());
return Err(Error::Io(err));
Err(Error::Io(err))
}
};
};
}
}
}
Err(ureq::Error::Status(_, response)) => {
Err(Error::Api(response.into_string().unwrap()))
}
Err(err) => Err(Error::Internal(err.to_string())),
};
}
}
}
6 changes: 3 additions & 3 deletions src/api/manage_reporting.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::api::Client;
use crate::api::manage_inject::UpdateInput;
use crate::api::Client;
use crate::handle::ExecutionOutput;

pub fn report_success(
Expand All @@ -13,7 +13,7 @@ pub fn report_success(
) {
let message = ExecutionOutput {
action: String::from(semantic),
stderr: stderr.unwrap_or(String::new()),
stderr: stderr.unwrap_or_default(),
stdout,
exit_code: -1,
};
Expand All @@ -40,7 +40,7 @@ pub fn report_error(
) {
let message = ExecutionOutput {
action: String::from(semantic),
stdout: stdout.unwrap_or(String::new()),
stdout: stdout.unwrap_or_default(),
stderr,
exit_code: -1,
};
Expand Down
Loading