Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9e25622f18 | ||
|
|
4eb4b1f74b | ||
|
|
def7371d6a |
+5
-48
@@ -9,25 +9,10 @@ on:
|
|||||||
env:
|
env:
|
||||||
CARGO_TERM_COLOR: always
|
CARGO_TERM_COLOR: always
|
||||||
RUSTFLAGS: "-D warnings"
|
RUSTFLAGS: "-D warnings"
|
||||||
# Compile cache: sccache -> Hetzner S3 (breakpilot-sccache), runner-independent
|
# sccache caches compilation artifacts within a job so that compiling
|
||||||
# and persistent across CI runs (own key prefix). Reuses the shared cluster S3
|
# both --features server and --features web shares common crate work.
|
||||||
# creds (same bucket as werkpilot). Requires repo secrets HETZNER_S3_ACCESS_KEY
|
|
||||||
# and HETZNER_S3_SECRET_KEY.
|
|
||||||
RUSTC_WRAPPER: /usr/local/bin/sccache
|
RUSTC_WRAPPER: /usr/local/bin/sccache
|
||||||
SCCACHE_BUCKET: breakpilot-sccache
|
SCCACHE_DIR: /tmp/sccache
|
||||||
SCCACHE_ENDPOINT: https://nbg1.your-objectstorage.com
|
|
||||||
SCCACHE_REGION: auto
|
|
||||||
SCCACHE_S3_USE_SSL: "true"
|
|
||||||
SCCACHE_S3_KEY_PREFIX: compliance-scanner
|
|
||||||
AWS_ACCESS_KEY_ID: ${{ secrets.HETZNER_S3_ACCESS_KEY }}
|
|
||||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.HETZNER_S3_SECRET_KEY }}
|
|
||||||
# compliance-agent depends on tramiton-core via git; use the system git so the
|
|
||||||
# credential rewrite below (see "Configure git auth ...") is honored on fetch.
|
|
||||||
CARGO_NET_GIT_FETCH_WITH_CLI: "true"
|
|
||||||
# Throttle cargo so a ~670-crate concurrent download burst doesn't 429 the
|
|
||||||
# Kellnr mirror: fewer concurrent connections (HTTP/1.1) + more retries.
|
|
||||||
CARGO_NET_RETRY: "10"
|
|
||||||
CARGO_HTTP_MULTIPLEXING: "false"
|
|
||||||
|
|
||||||
# Cancel in-progress runs for the same branch/PR
|
# Cancel in-progress runs for the same branch/PR
|
||||||
concurrency:
|
concurrency:
|
||||||
@@ -51,44 +36,16 @@ jobs:
|
|||||||
git remote add origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
|
git remote add origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
|
||||||
git fetch --depth=1 origin "${GITHUB_SHA}"
|
git fetch --depth=1 origin "${GITHUB_SHA}"
|
||||||
git checkout FETCH_HEAD
|
git checkout FETCH_HEAD
|
||||||
# Resolve crates.io deps through the self-hosted Kellnr mirror (cached,
|
|
||||||
# crates.io-independent). Git deps (tramiton-core) are unaffected — source
|
|
||||||
# replacement only applies to crates.io-sourced crates.
|
|
||||||
- name: Use Kellnr crates.io mirror
|
|
||||||
run: |
|
|
||||||
: "${CARGO_HOME:=/usr/local/cargo}"
|
|
||||||
mkdir -p "$CARGO_HOME"
|
|
||||||
{
|
|
||||||
echo '[source.crates-io]'
|
|
||||||
echo 'replace-with = "kellnr"'
|
|
||||||
echo '[registries.kellnr]'
|
|
||||||
echo 'index = "sparse+https://crates.meghsakha.com/api/v1/cratesio/"'
|
|
||||||
} >> "$CARGO_HOME/config.toml"
|
|
||||||
env:
|
|
||||||
RUSTC_WRAPPER: ""
|
|
||||||
- name: Install tools
|
- name: Install tools
|
||||||
run: |
|
run: |
|
||||||
rustup component add rustfmt clippy
|
rustup component add rustfmt clippy
|
||||||
curl -fsSL https://github.com/mozilla/sccache/releases/download/v0.10.0/sccache-v0.10.0-x86_64-unknown-linux-musl.tar.gz \
|
curl -fsSL https://github.com/mozilla/sccache/releases/download/v0.9.1/sccache-v0.9.1-x86_64-unknown-linux-musl.tar.gz \
|
||||||
| tar xz --strip-components=1 -C /usr/local/bin/ sccache-v0.10.0-x86_64-unknown-linux-musl/sccache
|
| tar xz --strip-components=1 -C /usr/local/bin/ sccache-v0.9.1-x86_64-unknown-linux-musl/sccache
|
||||||
chmod +x /usr/local/bin/sccache
|
chmod +x /usr/local/bin/sccache
|
||||||
cargo install cargo-audit --locked
|
cargo install cargo-audit --locked
|
||||||
env:
|
env:
|
||||||
RUSTC_WRAPPER: ""
|
RUSTC_WRAPPER: ""
|
||||||
|
|
||||||
# compliance-agent has a git dependency on tramiton-core (a private repo on
|
|
||||||
# this Gitea instance). Rewrite its SSH URL to HTTPS + a PAT so the runner
|
|
||||||
# can fetch it. Requires the repo secret TRAMITON_FETCH_TOKEN (a Gitea PAT
|
|
||||||
# with read:repository, owned by a user with access to sharang/tramiton).
|
|
||||||
# (Honored on fetch because CARGO_NET_GIT_FETCH_WITH_CLI=true uses system git.)
|
|
||||||
- name: Configure git auth for private tramiton dependency
|
|
||||||
run: |
|
|
||||||
git config --global \
|
|
||||||
url."https://sharang:${{ secrets.TRAMITON_FETCH_TOKEN }}@gitea.meghsakha.com/".insteadOf \
|
|
||||||
"ssh://git@gitea.meghsakha.com:22222/"
|
|
||||||
env:
|
|
||||||
RUSTC_WRAPPER: ""
|
|
||||||
|
|
||||||
# Format (no compilation needed)
|
# Format (no compilation needed)
|
||||||
- name: Format
|
- name: Format
|
||||||
run: cargo fmt --all --check
|
run: cargo fmt --all --check
|
||||||
|
|||||||
Generated
+2
-65
@@ -692,7 +692,6 @@ dependencies = [
|
|||||||
"tower-http",
|
"tower-http",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
"tramiton-core",
|
|
||||||
"urlencoding",
|
"urlencoding",
|
||||||
"uuid",
|
"uuid",
|
||||||
"walkdir",
|
"walkdir",
|
||||||
@@ -4198,7 +4197,7 @@ version = "3.4.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "219cb19e96be00ab2e37d6e299658a0cfa83e52429179969b0f0121b4ac46983"
|
checksum = "219cb19e96be00ab2e37d6e299658a0cfa83e52429179969b0f0121b4ac46983"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"toml_edit 0.23.10+spec-1.0.0",
|
"toml_edit",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -4997,15 +4996,6 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "serde_spanned"
|
|
||||||
version = "0.6.9"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3"
|
|
||||||
dependencies = [
|
|
||||||
"serde",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_urlencoded"
|
name = "serde_urlencoded"
|
||||||
version = "0.7.1"
|
version = "0.7.1"
|
||||||
@@ -5820,27 +5810,6 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "toml"
|
|
||||||
version = "0.8.23"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362"
|
|
||||||
dependencies = [
|
|
||||||
"serde",
|
|
||||||
"serde_spanned",
|
|
||||||
"toml_datetime 0.6.11",
|
|
||||||
"toml_edit 0.22.27",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "toml_datetime"
|
|
||||||
version = "0.6.11"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c"
|
|
||||||
dependencies = [
|
|
||||||
"serde",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toml_datetime"
|
name = "toml_datetime"
|
||||||
version = "0.7.5+spec-1.1.0"
|
version = "0.7.5+spec-1.1.0"
|
||||||
@@ -5850,20 +5819,6 @@ dependencies = [
|
|||||||
"serde_core",
|
"serde_core",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "toml_edit"
|
|
||||||
version = "0.22.27"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a"
|
|
||||||
dependencies = [
|
|
||||||
"indexmap 2.13.0",
|
|
||||||
"serde",
|
|
||||||
"serde_spanned",
|
|
||||||
"toml_datetime 0.6.11",
|
|
||||||
"toml_write",
|
|
||||||
"winnow",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toml_edit"
|
name = "toml_edit"
|
||||||
version = "0.23.10+spec-1.0.0"
|
version = "0.23.10+spec-1.0.0"
|
||||||
@@ -5871,7 +5826,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "84c8b9f757e028cee9fa244aea147aab2a9ec09d5325a9b01e0a49730c2b5269"
|
checksum = "84c8b9f757e028cee9fa244aea147aab2a9ec09d5325a9b01e0a49730c2b5269"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"indexmap 2.13.0",
|
"indexmap 2.13.0",
|
||||||
"toml_datetime 0.7.5+spec-1.1.0",
|
"toml_datetime",
|
||||||
"toml_parser",
|
"toml_parser",
|
||||||
"winnow",
|
"winnow",
|
||||||
]
|
]
|
||||||
@@ -5885,12 +5840,6 @@ dependencies = [
|
|||||||
"winnow",
|
"winnow",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "toml_write"
|
|
||||||
version = "0.1.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tonic"
|
name = "tonic"
|
||||||
version = "0.12.3"
|
version = "0.12.3"
|
||||||
@@ -6137,18 +6086,6 @@ dependencies = [
|
|||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "tramiton-core"
|
|
||||||
version = "0.4.0"
|
|
||||||
source = "git+ssh://git@gitea.meghsakha.com:22222/sharang/tramiton.git?tag=v0.4.0#e3dc1bf7027a2f6d7b1fe43043d6dfa887ce4af3"
|
|
||||||
dependencies = [
|
|
||||||
"serde",
|
|
||||||
"tempfile",
|
|
||||||
"thiserror 1.0.69",
|
|
||||||
"toml",
|
|
||||||
"walkdir",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tree-sitter"
|
name = "tree-sitter"
|
||||||
version = "0.24.7"
|
version = "0.24.7"
|
||||||
|
|||||||
@@ -10,11 +10,6 @@ workspace = true
|
|||||||
compliance-core = { workspace = true, features = ["mongodb", "telemetry", "axum"] }
|
compliance-core = { workspace = true, features = ["mongodb", "telemetry", "axum"] }
|
||||||
compliance-graph = { path = "../compliance-graph" }
|
compliance-graph = { path = "../compliance-graph" }
|
||||||
compliance-dast = { path = "../compliance-dast" }
|
compliance-dast = { path = "../compliance-dast" }
|
||||||
# Native firmware build/target detection for bare-metal & RTOS artifacts.
|
|
||||||
# Same-company IP, used directly (not via CLI) so the whole tramiton suite is
|
|
||||||
# available to the onboarding classifier. NOTE: CI must be able to fetch this
|
|
||||||
# private repo (see the git-auth step in .gitea/workflows/ci.yml).
|
|
||||||
tramiton-core = { git = "ssh://git@gitea.meghsakha.com:22222/sharang/tramiton.git", tag = "v0.4.0" }
|
|
||||||
serde = { workspace = true }
|
serde = { workspace = true }
|
||||||
serde_json = { workspace = true }
|
serde_json = { workspace = true }
|
||||||
tokio = { workspace = true }
|
tokio = { workspace = true }
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ pub mod help_chat;
|
|||||||
pub mod issues;
|
pub mod issues;
|
||||||
pub mod mcp_tokens;
|
pub mod mcp_tokens;
|
||||||
pub mod notifications;
|
pub mod notifications;
|
||||||
pub mod onboarding;
|
|
||||||
pub mod pentest_handlers;
|
pub mod pentest_handlers;
|
||||||
pub use pentest_handlers as pentest;
|
pub use pentest_handlers as pentest;
|
||||||
pub mod repos;
|
pub mod repos;
|
||||||
|
|||||||
@@ -1,350 +0,0 @@
|
|||||||
//! Onboarding API — CRUD for unified targets, artifact add, classification, and
|
|
||||||
//! the scan-applicability matrix. The wizard (and future integrations) drive
|
|
||||||
//! onboarding through these endpoints.
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use axum::extract::{Extension, Path, Query};
|
|
||||||
use axum::http::StatusCode;
|
|
||||||
use axum::Json;
|
|
||||||
use mongodb::bson::{doc, oid::ObjectId, to_bson};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
use compliance_core::models::{
|
|
||||||
Artifact, ArtifactKind, ComplianceProfile, OnboardedTarget, PlcFormat, TargetScanConfig,
|
|
||||||
TargetType,
|
|
||||||
};
|
|
||||||
use compliance_core::scan_matrix::{applicable_scans, supports_pentest};
|
|
||||||
use compliance_core::tenant_ctx::TenantCtx;
|
|
||||||
|
|
||||||
use crate::agent::ComplianceAgent;
|
|
||||||
use crate::classify::{classify_target, MockFirmwareDetector};
|
|
||||||
|
|
||||||
use super::dto::tenant_db;
|
|
||||||
use super::{collect_cursor_async, ApiResponse, PaginationParams};
|
|
||||||
|
|
||||||
type AgentExt = Extension<Arc<ComplianceAgent>>;
|
|
||||||
|
|
||||||
/// A client-supplied artifact spec. The server builds the [`Artifact`] (and its
|
|
||||||
/// id) from it, so clients never set internal fields.
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct ArtifactInput {
|
|
||||||
pub kind: ArtifactKind,
|
|
||||||
pub source_ref: String,
|
|
||||||
#[serde(default)]
|
|
||||||
pub branch: Option<String>,
|
|
||||||
#[serde(default)]
|
|
||||||
pub plc_format: Option<PlcFormat>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ArtifactInput {
|
|
||||||
fn build(&self) -> Artifact {
|
|
||||||
let s = self.source_ref.clone();
|
|
||||||
match self.kind {
|
|
||||||
ArtifactKind::GitRepo => {
|
|
||||||
Artifact::git_repo(s, self.branch.clone().unwrap_or_else(|| "main".to_string()))
|
|
||||||
}
|
|
||||||
ArtifactKind::LiveUrl => Artifact::live_url(s),
|
|
||||||
ArtifactKind::FirmwareImage => Artifact::firmware_image(s),
|
|
||||||
ArtifactKind::SourceArchive => Artifact::source_archive(s),
|
|
||||||
ArtifactKind::MobilePackage => Artifact::mobile_package(s),
|
|
||||||
ArtifactKind::ContainerImage => Artifact::container_image(s),
|
|
||||||
ArtifactKind::PlcProject => {
|
|
||||||
Artifact::plc_project(s, self.plc_format.unwrap_or(PlcFormat::PlcopenXml))
|
|
||||||
}
|
|
||||||
ArtifactKind::PlaintextDescription => Artifact::plaintext(s),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct CreateTargetRequest {
|
|
||||||
pub name: String,
|
|
||||||
pub target_type: TargetType,
|
|
||||||
#[serde(default)]
|
|
||||||
pub description: Option<String>,
|
|
||||||
#[serde(default)]
|
|
||||||
pub artifacts: Vec<ArtifactInput>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct UpdateTargetRequest {
|
|
||||||
pub name: Option<String>,
|
|
||||||
pub target_type: Option<TargetType>,
|
|
||||||
pub scan_config: Option<TargetScanConfig>,
|
|
||||||
pub compliance_profile: Option<ComplianceProfile>,
|
|
||||||
pub scan_schedule: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// One applicable-scan option, serialized for the wizard.
|
|
||||||
#[derive(Serialize)]
|
|
||||||
pub struct ScanOptionDto {
|
|
||||||
pub scan: String,
|
|
||||||
pub default_on: bool,
|
|
||||||
pub rationale: String,
|
|
||||||
pub required_artifact: Option<String>,
|
|
||||||
pub blocked_reason: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
pub struct ApplicableScansResponse {
|
|
||||||
pub scans: Vec<ScanOptionDto>,
|
|
||||||
pub pentest_supported: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_oid(id: &str) -> Result<ObjectId, StatusCode> {
|
|
||||||
ObjectId::parse_str(id).map_err(|_| StatusCode::BAD_REQUEST)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// GET /api/v1/targets — list onboarded targets (paginated).
|
|
||||||
#[tracing::instrument(skip_all)]
|
|
||||||
pub async fn list_targets(
|
|
||||||
Extension(agent): AgentExt,
|
|
||||||
tenant: TenantCtx,
|
|
||||||
Query(params): Query<PaginationParams>,
|
|
||||||
) -> Result<Json<ApiResponse<Vec<OnboardedTarget>>>, StatusCode> {
|
|
||||||
let db = tenant_db(&agent, &tenant).await?;
|
|
||||||
let skip = (params.page.saturating_sub(1)) * params.limit as u64;
|
|
||||||
let total = db
|
|
||||||
.onboarded_targets()
|
|
||||||
.count_documents(doc! {})
|
|
||||||
.await
|
|
||||||
.unwrap_or(0);
|
|
||||||
let targets = match db
|
|
||||||
.onboarded_targets()
|
|
||||||
.find(doc! {})
|
|
||||||
.skip(skip)
|
|
||||||
.limit(params.limit)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(cursor) => collect_cursor_async(cursor).await,
|
|
||||||
Err(e) => {
|
|
||||||
tracing::warn!("Failed to fetch onboarded targets: {e}");
|
|
||||||
Vec::new()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(Json(ApiResponse {
|
|
||||||
data: targets,
|
|
||||||
total: Some(total),
|
|
||||||
page: Some(params.page),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// POST /api/v1/targets — create an onboarded target.
|
|
||||||
#[tracing::instrument(skip_all)]
|
|
||||||
pub async fn create_target(
|
|
||||||
Extension(agent): AgentExt,
|
|
||||||
tenant: TenantCtx,
|
|
||||||
Json(req): Json<CreateTargetRequest>,
|
|
||||||
) -> Result<Json<ApiResponse<OnboardedTarget>>, StatusCode> {
|
|
||||||
let mut target = OnboardedTarget::new(req.name, req.target_type);
|
|
||||||
target.description = req.description;
|
|
||||||
target.artifacts = req.artifacts.iter().map(ArtifactInput::build).collect();
|
|
||||||
|
|
||||||
let db = tenant_db(&agent, &tenant).await?;
|
|
||||||
let res = db
|
|
||||||
.onboarded_targets()
|
|
||||||
.insert_one(&target)
|
|
||||||
.await
|
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
target.id = res.inserted_id.as_object_id();
|
|
||||||
Ok(Json(ApiResponse {
|
|
||||||
data: target,
|
|
||||||
total: None,
|
|
||||||
page: None,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// GET /api/v1/targets/{id} — fetch one target.
|
|
||||||
#[tracing::instrument(skip_all, fields(target_id = %id))]
|
|
||||||
pub async fn get_target(
|
|
||||||
Extension(agent): AgentExt,
|
|
||||||
tenant: TenantCtx,
|
|
||||||
Path(id): Path<String>,
|
|
||||||
) -> Result<Json<ApiResponse<OnboardedTarget>>, StatusCode> {
|
|
||||||
let oid = parse_oid(&id)?;
|
|
||||||
let db = tenant_db(&agent, &tenant).await?;
|
|
||||||
let target = db
|
|
||||||
.onboarded_targets()
|
|
||||||
.find_one(doc! { "_id": oid })
|
|
||||||
.await
|
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
|
||||||
.ok_or(StatusCode::NOT_FOUND)?;
|
|
||||||
Ok(Json(ApiResponse {
|
|
||||||
data: target,
|
|
||||||
total: None,
|
|
||||||
page: None,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// PATCH /api/v1/targets/{id} — update mutable fields.
|
|
||||||
#[tracing::instrument(skip_all, fields(target_id = %id))]
|
|
||||||
pub async fn update_target(
|
|
||||||
Extension(agent): AgentExt,
|
|
||||||
tenant: TenantCtx,
|
|
||||||
Path(id): Path<String>,
|
|
||||||
Json(req): Json<UpdateTargetRequest>,
|
|
||||||
) -> Result<Json<ApiResponse<OnboardedTarget>>, StatusCode> {
|
|
||||||
let oid = parse_oid(&id)?;
|
|
||||||
let db = tenant_db(&agent, &tenant).await?;
|
|
||||||
|
|
||||||
let mut set = doc! { "updated_at": mongodb::bson::DateTime::now() };
|
|
||||||
if let Some(name) = req.name {
|
|
||||||
set.insert("name", name);
|
|
||||||
}
|
|
||||||
if let Some(tt) = req.target_type {
|
|
||||||
set.insert(
|
|
||||||
"target_type",
|
|
||||||
to_bson(&tt).map_err(|_| StatusCode::BAD_REQUEST)?,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if let Some(sc) = req.scan_config {
|
|
||||||
set.insert(
|
|
||||||
"scan_config",
|
|
||||||
to_bson(&sc).map_err(|_| StatusCode::BAD_REQUEST)?,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if let Some(cp) = req.compliance_profile {
|
|
||||||
set.insert(
|
|
||||||
"compliance_profile",
|
|
||||||
to_bson(&cp).map_err(|_| StatusCode::BAD_REQUEST)?,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if let Some(ss) = req.scan_schedule {
|
|
||||||
set.insert("scan_schedule", ss);
|
|
||||||
}
|
|
||||||
|
|
||||||
db.onboarded_targets()
|
|
||||||
.update_one(doc! { "_id": oid }, doc! { "$set": set })
|
|
||||||
.await
|
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
|
|
||||||
get_target(Extension(agent), tenant, Path(id)).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// DELETE /api/v1/targets/{id} — remove the target and its findings/scans.
|
|
||||||
#[tracing::instrument(skip_all, fields(target_id = %id))]
|
|
||||||
pub async fn delete_target(
|
|
||||||
Extension(agent): AgentExt,
|
|
||||||
tenant: TenantCtx,
|
|
||||||
Path(id): Path<String>,
|
|
||||||
) -> Result<Json<serde_json::Value>, StatusCode> {
|
|
||||||
let oid = parse_oid(&id)?;
|
|
||||||
let db = tenant_db(&agent, &tenant).await?;
|
|
||||||
db.onboarded_targets()
|
|
||||||
.delete_one(doc! { "_id": oid })
|
|
||||||
.await
|
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
// Cascade the collections keyed by repo_id == target id (best-effort).
|
|
||||||
let by_repo = doc! { "repo_id": &id };
|
|
||||||
let _ = db.findings().delete_many(by_repo.clone()).await;
|
|
||||||
let _ = db.scan_runs().delete_many(by_repo.clone()).await;
|
|
||||||
let _ = db.sbom_entries().delete_many(by_repo.clone()).await;
|
|
||||||
let _ = db.cve_alerts().delete_many(by_repo).await;
|
|
||||||
Ok(Json(serde_json::json!({ "status": "deleted" })))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// POST /api/v1/targets/{id}/artifacts — attach an artifact (by reference).
|
|
||||||
#[tracing::instrument(skip_all, fields(target_id = %id))]
|
|
||||||
pub async fn add_artifact(
|
|
||||||
Extension(agent): AgentExt,
|
|
||||||
tenant: TenantCtx,
|
|
||||||
Path(id): Path<String>,
|
|
||||||
Json(input): Json<ArtifactInput>,
|
|
||||||
) -> Result<Json<ApiResponse<OnboardedTarget>>, StatusCode> {
|
|
||||||
let oid = parse_oid(&id)?;
|
|
||||||
let db = tenant_db(&agent, &tenant).await?;
|
|
||||||
let artifact = to_bson(&input.build()).map_err(|_| StatusCode::BAD_REQUEST)?;
|
|
||||||
db.onboarded_targets()
|
|
||||||
.update_one(
|
|
||||||
doc! { "_id": oid },
|
|
||||||
doc! { "$push": { "artifacts": artifact }, "$set": { "updated_at": mongodb::bson::DateTime::now() } },
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
get_target(Extension(agent), tenant, Path(id)).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// GET /api/v1/targets/{id}/applicable-scans — the scan-applicability matrix.
|
|
||||||
#[tracing::instrument(skip_all, fields(target_id = %id))]
|
|
||||||
pub async fn applicable_scans_for_target(
|
|
||||||
Extension(agent): AgentExt,
|
|
||||||
tenant: TenantCtx,
|
|
||||||
Path(id): Path<String>,
|
|
||||||
) -> Result<Json<ApiResponse<ApplicableScansResponse>>, StatusCode> {
|
|
||||||
let oid = parse_oid(&id)?;
|
|
||||||
let db = tenant_db(&agent, &tenant).await?;
|
|
||||||
let target = db
|
|
||||||
.onboarded_targets()
|
|
||||||
.find_one(doc! { "_id": oid })
|
|
||||||
.await
|
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
|
||||||
.ok_or(StatusCode::NOT_FOUND)?;
|
|
||||||
|
|
||||||
let scans = applicable_scans(&target)
|
|
||||||
.into_iter()
|
|
||||||
.map(|o| ScanOptionDto {
|
|
||||||
scan: o.scan.to_string(),
|
|
||||||
default_on: o.default_on,
|
|
||||||
rationale: o.rationale,
|
|
||||||
required_artifact: o.required_artifact.map(|k| k.to_string()),
|
|
||||||
blocked_reason: o.blocked_reason,
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
Ok(Json(ApiResponse {
|
|
||||||
data: ApplicableScansResponse {
|
|
||||||
scans,
|
|
||||||
pentest_supported: supports_pentest(target.target_type),
|
|
||||||
},
|
|
||||||
total: None,
|
|
||||||
page: None,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// POST /api/v1/targets/{id}/detect — classify the target from its artifacts.
|
|
||||||
///
|
|
||||||
/// This is the lightweight pass: it classifies from artifact kinds without
|
|
||||||
/// ingesting (cloning) sources, so it returns immediately. Deep detection (after
|
|
||||||
/// ingest, with tramiton firmware analysis) is a follow-up background step.
|
|
||||||
#[tracing::instrument(skip_all, fields(target_id = %id))]
|
|
||||||
pub async fn detect_target(
|
|
||||||
Extension(agent): AgentExt,
|
|
||||||
tenant: TenantCtx,
|
|
||||||
Path(id): Path<String>,
|
|
||||||
) -> Result<Json<ApiResponse<OnboardedTarget>>, StatusCode> {
|
|
||||||
let oid = parse_oid(&id)?;
|
|
||||||
let db = tenant_db(&agent, &tenant).await?;
|
|
||||||
let mut target = db
|
|
||||||
.onboarded_targets()
|
|
||||||
.find_one(doc! { "_id": oid })
|
|
||||||
.await
|
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
|
||||||
.ok_or(StatusCode::NOT_FOUND)?;
|
|
||||||
|
|
||||||
// No ingested working paths here → kind-based classification only; the mock
|
|
||||||
// firmware detector is never invoked (no firmware working path present).
|
|
||||||
let empty = HashMap::new();
|
|
||||||
let detector = MockFirmwareDetector { detection: None };
|
|
||||||
let classification = classify_target(&target, &empty, &detector)
|
|
||||||
.await
|
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
|
|
||||||
let classification_bson =
|
|
||||||
to_bson(&classification).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
db.onboarded_targets()
|
|
||||||
.update_one(
|
|
||||||
doc! { "_id": oid },
|
|
||||||
doc! { "$set": { "classification": classification_bson, "updated_at": mongodb::bson::DateTime::now() } },
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
target.classification = Some(classification);
|
|
||||||
|
|
||||||
Ok(Json(ApiResponse {
|
|
||||||
data: target,
|
|
||||||
total: None,
|
|
||||||
page: None,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
@@ -25,29 +25,6 @@ pub fn build_router() -> Router {
|
|||||||
"/api/v1/repositories/{id}/webhook-config",
|
"/api/v1/repositories/{id}/webhook-config",
|
||||||
get(handlers::get_webhook_config),
|
get(handlers::get_webhook_config),
|
||||||
)
|
)
|
||||||
// Unified onboarding targets (#131).
|
|
||||||
.route(
|
|
||||||
"/api/v1/targets",
|
|
||||||
get(handlers::onboarding::list_targets).post(handlers::onboarding::create_target),
|
|
||||||
)
|
|
||||||
.route(
|
|
||||||
"/api/v1/targets/{id}",
|
|
||||||
get(handlers::onboarding::get_target)
|
|
||||||
.patch(handlers::onboarding::update_target)
|
|
||||||
.delete(handlers::onboarding::delete_target),
|
|
||||||
)
|
|
||||||
.route(
|
|
||||||
"/api/v1/targets/{id}/artifacts",
|
|
||||||
post(handlers::onboarding::add_artifact),
|
|
||||||
)
|
|
||||||
.route(
|
|
||||||
"/api/v1/targets/{id}/applicable-scans",
|
|
||||||
get(handlers::onboarding::applicable_scans_for_target),
|
|
||||||
)
|
|
||||||
.route(
|
|
||||||
"/api/v1/targets/{id}/detect",
|
|
||||||
post(handlers::onboarding::detect_target),
|
|
||||||
)
|
|
||||||
.route("/api/v1/findings", get(handlers::list_findings))
|
.route("/api/v1/findings", get(handlers::list_findings))
|
||||||
.route("/api/v1/findings/{id}", get(handlers::get_finding))
|
.route("/api/v1/findings/{id}", get(handlers::get_finding))
|
||||||
.route(
|
.route(
|
||||||
|
|||||||
@@ -1,40 +1,55 @@
|
|||||||
//! Firmware classification via tramiton.
|
//! Firmware classification via tramiton.
|
||||||
//!
|
//!
|
||||||
//! tramiton is the company's firmware build/repro engine; we do not re-implement
|
//! tramiton is the company's firmware build/repro engine; we do not re-implement
|
||||||
//! its detection. We depend on `tramiton-core` directly (same-company IP) and run
|
//! its detection. This module shells out to `tramiton detect --json` behind a
|
||||||
//! its provider analysis in-process behind a [`FirmwareDetector`] port, mapping
|
//! [`FirmwareDetector`] port (so a future in-process or cloud impl can slot in)
|
||||||
//! tramiton's `BuildPlan` onto a [`TargetType`]. A deterministic
|
//! and maps the resulting build plan onto a [`TargetType`]. A deterministic
|
||||||
//! [`MockFirmwareDetector`] backs the tests so CI unit tests need neither the
|
//! [`MockFirmwareDetector`] backs the tests so CI never needs the binary.
|
||||||
//! tramiton sources nor a real firmware tree.
|
//!
|
||||||
|
//! The parsed structs mirror a *subset* of tramiton's `BuildPlan` JSON — we
|
||||||
|
//! deliberately do not depend on the proprietary `tramiton-core` crate.
|
||||||
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
use compliance_core::error::CoreError;
|
use compliance_core::error::CoreError;
|
||||||
use compliance_core::models::{DetectedFact, TargetType};
|
use compliance_core::models::{DetectedFact, TargetType};
|
||||||
use compliance_core::traits::ClassifierVerdict;
|
use compliance_core::traits::ClassifierVerdict;
|
||||||
|
|
||||||
/// A minimal firmware-detection summary, mapped from tramiton's `BuildPlan`.
|
/// The top-level `tramiton detect --json` document (fields we use).
|
||||||
/// Kept small and tramiton-independent so the classifier and the test mock don't
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
/// need to construct a full tramiton plan.
|
pub struct TramitonDetect {
|
||||||
#[derive(Debug, Clone, Default)]
|
/// The selected build plan, if tramiton could form one.
|
||||||
pub struct FirmwareDetection {
|
#[serde(default)]
|
||||||
|
pub plan: Option<TramitonBuildPlan>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The subset of tramiton's `BuildPlan` we consume for classification.
|
||||||
|
#[derive(Debug, Clone, Default, Deserialize)]
|
||||||
|
pub struct TramitonBuildPlan {
|
||||||
/// The detecting provider (e.g. `zephyr`, `cmake`, `source-archaeology`).
|
/// The detecting provider (e.g. `zephyr`, `cmake`, `source-archaeology`).
|
||||||
|
#[serde(default)]
|
||||||
pub provider: String,
|
pub provider: String,
|
||||||
/// Detection confidence: `low` | `medium` | `high`.
|
/// Detection confidence: `low` | `medium` | `high`.
|
||||||
|
#[serde(default)]
|
||||||
pub confidence: String,
|
pub confidence: String,
|
||||||
/// Build-system label (e.g. `Zephyr`, `ESP-IDF`, `CMake`).
|
/// Build system (kebab-case: `zephyr`, `esp-idf`, `cmake`, `make`, ...).
|
||||||
|
#[serde(default)]
|
||||||
pub build_system: String,
|
pub build_system: String,
|
||||||
/// Framework, when known (`zephyr`, `esp-idf`, `bare-metal`, ...).
|
/// Framework, when known (`zephyr`, `esp-idf`, `mbed`, `bare-metal`, ...).
|
||||||
pub framework: Option<String>,
|
pub framework: Option<String>,
|
||||||
/// Target board / MCU / arch.
|
/// Target board / MCU / arch.
|
||||||
pub target: FirmwareTarget,
|
#[serde(default)]
|
||||||
|
pub target: TramitonTarget,
|
||||||
/// Unresolved gaps in the plan.
|
/// Unresolved gaps in the plan.
|
||||||
|
#[serde(default)]
|
||||||
pub gaps: Vec<String>,
|
pub gaps: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The detected firmware target (board / MCU / arch).
|
/// tramiton's target descriptor.
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default, Deserialize)]
|
||||||
pub struct FirmwareTarget {
|
pub struct TramitonTarget {
|
||||||
/// Board name.
|
/// Board name.
|
||||||
pub board: Option<String>,
|
pub board: Option<String>,
|
||||||
/// MCU part.
|
/// MCU part.
|
||||||
@@ -46,52 +61,57 @@ pub struct FirmwareTarget {
|
|||||||
/// A source of tramiton firmware detection.
|
/// A source of tramiton firmware detection.
|
||||||
#[allow(async_fn_in_trait)]
|
#[allow(async_fn_in_trait)]
|
||||||
pub trait FirmwareDetector: Send + Sync {
|
pub trait FirmwareDetector: Send + Sync {
|
||||||
/// Run detection over a path, returning a firmware detection if tramiton
|
/// Run detection over a path, returning tramiton's build plan if any.
|
||||||
/// could form a build plan.
|
async fn detect(&self, path: &Path) -> Result<Option<TramitonBuildPlan>, CoreError>;
|
||||||
async fn detect(&self, path: &Path) -> Result<Option<FirmwareDetection>, CoreError>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Uses `tramiton-core` in-process. The analysis is blocking (filesystem walk),
|
/// Shells out to the `tramiton` CLI. A missing binary or a non-zero exit is
|
||||||
/// so it runs on a blocking thread to avoid stalling the async runtime. A path
|
/// treated as "no detection" rather than an error, so firmware classification
|
||||||
/// with no recognizable build system yields `Ok(None)`.
|
/// degrades gracefully when tramiton is not installed.
|
||||||
pub struct TramitonNative;
|
pub struct TramitonCli {
|
||||||
|
/// The `tramiton` binary to invoke.
|
||||||
|
pub bin: String,
|
||||||
|
}
|
||||||
|
|
||||||
impl FirmwareDetector for TramitonNative {
|
impl TramitonCli {
|
||||||
async fn detect(&self, path: &Path) -> Result<Option<FirmwareDetection>, CoreError> {
|
/// Construct from `TRAMITON_BIN` (default `tramiton`).
|
||||||
let path = path.to_path_buf();
|
pub fn from_env() -> Self {
|
||||||
let plan = tokio::task::spawn_blocking(move || {
|
Self {
|
||||||
let repo = tramiton_core::Repo::new(&path);
|
bin: std::env::var("TRAMITON_BIN").unwrap_or_else(|_| "tramiton".to_string()),
|
||||||
tramiton_core::provider::analyze(&repo)
|
}
|
||||||
})
|
|
||||||
.await
|
|
||||||
.map_err(|e| CoreError::Other(format!("tramiton detect task join error: {e}")))?
|
|
||||||
.map_err(|e| CoreError::Other(format!("tramiton analyze error: {e}")))?;
|
|
||||||
Ok(plan.map(|bp| detection_from_build_plan(&bp)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Map tramiton's `BuildPlan` onto our minimal detection summary.
|
impl FirmwareDetector for TramitonCli {
|
||||||
fn detection_from_build_plan(bp: &tramiton_core::BuildPlan) -> FirmwareDetection {
|
async fn detect(&self, path: &Path) -> Result<Option<TramitonBuildPlan>, CoreError> {
|
||||||
FirmwareDetection {
|
let output = tokio::process::Command::new(&self.bin)
|
||||||
provider: bp.provider.clone(),
|
.arg("detect")
|
||||||
confidence: bp.confidence.to_string(),
|
.arg("--json")
|
||||||
build_system: bp.build_system.label().to_string(),
|
.arg(path)
|
||||||
framework: bp.framework.clone(),
|
.output()
|
||||||
target: FirmwareTarget {
|
.await;
|
||||||
board: bp.target.board.clone(),
|
match output {
|
||||||
mcu: bp.target.mcu.clone(),
|
Ok(o) if o.status.success() => {
|
||||||
arch: bp.target.arch.clone(),
|
let parsed: TramitonDetect = serde_json::from_slice(&o.stdout)?;
|
||||||
},
|
Ok(parsed.plan)
|
||||||
gaps: bp.gaps.clone(),
|
}
|
||||||
|
// Non-zero exit: tramiton ran but formed no plan.
|
||||||
|
Ok(_) => Ok(None),
|
||||||
|
// Binary not found / not executable: degrade gracefully.
|
||||||
|
Err(_) => Ok(None),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Map a firmware detection to a target type. Framework/build-system signals
|
/// Map a tramiton build plan to a target type. Framework/build-system signals
|
||||||
/// distinguish RTOS from bare-metal from Yocto.
|
/// distinguish RTOS from bare-metal from Yocto.
|
||||||
pub fn detection_to_target_type(det: &FirmwareDetection) -> TargetType {
|
pub fn plan_to_target_type(plan: &TramitonBuildPlan) -> TargetType {
|
||||||
let framework = det.framework.as_deref().unwrap_or("").to_lowercase();
|
let framework = plan.framework.as_deref().unwrap_or("").to_lowercase();
|
||||||
let build_system = det.build_system.to_lowercase();
|
let build_system = plan.build_system.to_lowercase();
|
||||||
let signal = format!("{framework} {build_system} {}", det.provider.to_lowercase());
|
let signal = format!(
|
||||||
|
"{framework} {build_system} {}",
|
||||||
|
plan.provider.to_lowercase()
|
||||||
|
);
|
||||||
|
|
||||||
const RTOS: [&str; 6] = ["zephyr", "esp-idf", "freertos", "nuttx", "riot", "chibios"];
|
const RTOS: [&str; 6] = ["zephyr", "esp-idf", "freertos", "nuttx", "riot", "chibios"];
|
||||||
if signal.contains("bitbake") || signal.contains("yocto") || signal.contains("openembedded") {
|
if signal.contains("bitbake") || signal.contains("yocto") || signal.contains("openembedded") {
|
||||||
@@ -113,35 +133,35 @@ fn confidence_score(label: &str) -> f32 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Turn a firmware detection into a classifier verdict, carrying the MCU / board
|
/// Turn a tramiton build plan into a classifier verdict, carrying the MCU / board
|
||||||
/// / build-system as facts.
|
/// / build-system as facts.
|
||||||
pub fn detection_to_verdict(det: &FirmwareDetection) -> ClassifierVerdict {
|
pub fn plan_to_verdict(plan: &TramitonBuildPlan) -> ClassifierVerdict {
|
||||||
let target_type = detection_to_target_type(det);
|
let target_type = plan_to_target_type(plan);
|
||||||
let mut facts = vec![DetectedFact::new(
|
let mut facts = vec![DetectedFact::new(
|
||||||
"build_system",
|
"build_system",
|
||||||
det.build_system.clone(),
|
plan.build_system.clone(),
|
||||||
"tramiton",
|
"tramiton",
|
||||||
)];
|
)];
|
||||||
if let Some(fw) = &det.framework {
|
if let Some(fw) = &plan.framework {
|
||||||
facts.push(DetectedFact::new("framework", fw.clone(), "tramiton"));
|
facts.push(DetectedFact::new("framework", fw.clone(), "tramiton"));
|
||||||
}
|
}
|
||||||
if let Some(mcu) = &det.target.mcu {
|
if let Some(mcu) = &plan.target.mcu {
|
||||||
facts.push(DetectedFact::new("mcu", mcu.clone(), "tramiton"));
|
facts.push(DetectedFact::new("mcu", mcu.clone(), "tramiton"));
|
||||||
}
|
}
|
||||||
if let Some(board) = &det.target.board {
|
if let Some(board) = &plan.target.board {
|
||||||
facts.push(DetectedFact::new("board", board.clone(), "tramiton"));
|
facts.push(DetectedFact::new("board", board.clone(), "tramiton"));
|
||||||
}
|
}
|
||||||
if let Some(arch) = &det.target.arch {
|
if let Some(arch) = &plan.target.arch {
|
||||||
facts.push(DetectedFact::new("arch", arch.clone(), "tramiton"));
|
facts.push(DetectedFact::new("arch", arch.clone(), "tramiton"));
|
||||||
}
|
}
|
||||||
ClassifierVerdict {
|
ClassifierVerdict {
|
||||||
target_type,
|
target_type,
|
||||||
confidence: confidence_score(&det.confidence),
|
confidence: confidence_score(&plan.confidence),
|
||||||
facts,
|
facts,
|
||||||
rationale: format!(
|
rationale: format!(
|
||||||
"tramiton detected build system '{}'{}",
|
"tramiton detected build system '{}'{}",
|
||||||
det.build_system,
|
plan.build_system,
|
||||||
det.framework
|
plan.framework
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|f| format!(" (framework {f})"))
|
.map(|f| format!(" (framework {f})"))
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
@@ -149,15 +169,15 @@ pub fn detection_to_verdict(det: &FirmwareDetection) -> ClassifierVerdict {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A deterministic [`FirmwareDetector`] for tests — returns a preset detection.
|
/// A deterministic [`FirmwareDetector`] for tests — returns a preset plan.
|
||||||
pub struct MockFirmwareDetector {
|
pub struct MockFirmwareDetector {
|
||||||
/// The detection to return (or `None` for "no detection").
|
/// The plan to return (or `None` for "no detection").
|
||||||
pub detection: Option<FirmwareDetection>,
|
pub plan: Option<TramitonBuildPlan>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FirmwareDetector for MockFirmwareDetector {
|
impl FirmwareDetector for MockFirmwareDetector {
|
||||||
async fn detect(&self, _path: &Path) -> Result<Option<FirmwareDetection>, CoreError> {
|
async fn detect(&self, _path: &Path) -> Result<Option<TramitonBuildPlan>, CoreError> {
|
||||||
Ok(self.detection.clone())
|
Ok(self.plan.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,13 +186,13 @@ impl FirmwareDetector for MockFirmwareDetector {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
fn detection(build_system: &str, framework: Option<&str>) -> FirmwareDetection {
|
fn plan(build_system: &str, framework: Option<&str>) -> TramitonBuildPlan {
|
||||||
FirmwareDetection {
|
TramitonBuildPlan {
|
||||||
provider: build_system.to_string(),
|
provider: build_system.to_string(),
|
||||||
confidence: "high".to_string(),
|
confidence: "high".to_string(),
|
||||||
build_system: build_system.to_string(),
|
build_system: build_system.to_string(),
|
||||||
framework: framework.map(|s| s.to_string()),
|
framework: framework.map(|s| s.to_string()),
|
||||||
target: FirmwareTarget {
|
target: TramitonTarget {
|
||||||
mcu: Some("stm32f429".to_string()),
|
mcu: Some("stm32f429".to_string()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
@@ -183,7 +203,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn zephyr_maps_to_rtos() {
|
fn zephyr_maps_to_rtos() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
detection_to_target_type(&detection("zephyr", Some("zephyr"))),
|
plan_to_target_type(&plan("zephyr", Some("zephyr"))),
|
||||||
TargetType::FirmwareRtos
|
TargetType::FirmwareRtos
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -191,7 +211,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn bare_cmake_maps_to_bare_metal() {
|
fn bare_cmake_maps_to_bare_metal() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
detection_to_target_type(&detection("cmake", Some("bare-metal"))),
|
plan_to_target_type(&plan("cmake", Some("bare-metal"))),
|
||||||
TargetType::FirmwareBareMetal
|
TargetType::FirmwareBareMetal
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -199,14 +219,14 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn bitbake_maps_to_yocto() {
|
fn bitbake_maps_to_yocto() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
detection_to_target_type(&detection("bitbake", None)),
|
plan_to_target_type(&plan("bitbake", None)),
|
||||||
TargetType::EmbeddedLinuxYocto
|
TargetType::EmbeddedLinuxYocto
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn verdict_carries_mcu_fact_and_confidence() {
|
fn verdict_carries_mcu_fact_and_confidence() {
|
||||||
let v = detection_to_verdict(&detection("esp-idf", Some("esp-idf")));
|
let v = plan_to_verdict(&plan("esp-idf", Some("esp-idf")));
|
||||||
assert_eq!(v.target_type, TargetType::FirmwareRtos);
|
assert_eq!(v.target_type, TargetType::FirmwareRtos);
|
||||||
assert!((v.confidence - 0.9).abs() < f32::EPSILON);
|
assert!((v.confidence - 0.9).abs() < f32::EPSILON);
|
||||||
assert!(v
|
assert!(v
|
||||||
@@ -214,4 +234,13 @@ mod tests {
|
|||||||
.iter()
|
.iter()
|
||||||
.any(|f| f.key == "mcu" && f.value == "stm32f429"));
|
.any(|f| f.key == "mcu" && f.value == "stm32f429"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn detect_json_parses() {
|
||||||
|
let json = r#"{"repo":"/x","detections":[],"plan":{"provider":"zephyr","confidence":"high","build_system":"zephyr","framework":"zephyr","target":{"mcu":"nrf52840","board":"nrf52840dk","arch":"arm"},"gaps":[]}}"#;
|
||||||
|
let parsed: TramitonDetect = serde_json::from_str(json).expect("parse");
|
||||||
|
let plan = parsed.plan.expect("plan present");
|
||||||
|
assert_eq!(plan.target.mcu.as_deref(), Some("nrf52840"));
|
||||||
|
assert_eq!(plan_to_target_type(&plan), TargetType::FirmwareRtos);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ mod firmware;
|
|||||||
mod language;
|
mod language;
|
||||||
|
|
||||||
pub use firmware::{
|
pub use firmware::{
|
||||||
FirmwareDetection, FirmwareDetector, FirmwareTarget, MockFirmwareDetector, TramitonNative,
|
FirmwareDetector, MockFirmwareDetector, TramitonBuildPlan, TramitonCli, TramitonDetect,
|
||||||
|
TramitonTarget,
|
||||||
};
|
};
|
||||||
pub use language::HeuristicClassifier;
|
pub use language::HeuristicClassifier;
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ use compliance_core::models::{
|
|||||||
};
|
};
|
||||||
use compliance_core::traits::{ClassificationInput, ClassifierVerdict, TargetClassifier};
|
use compliance_core::traits::{ClassificationInput, ClassifierVerdict, TargetClassifier};
|
||||||
|
|
||||||
use firmware::detection_to_verdict;
|
use firmware::plan_to_verdict;
|
||||||
|
|
||||||
/// Classify a target from its artifacts and their ingested working paths, using
|
/// Classify a target from its artifacts and their ingested working paths, using
|
||||||
/// the heuristic classifier plus the tramiton firmware detector. Verdicts are
|
/// the heuristic classifier plus the tramiton firmware detector. Verdicts are
|
||||||
@@ -59,8 +60,8 @@ pub async fn classify_target<D: FirmwareDetector>(
|
|||||||
let Some(path) = working_paths.get(&artifact.id) else {
|
let Some(path) = working_paths.get(&artifact.id) else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
if let Some(detection) = firmware_detector.detect(path).await? {
|
if let Some(plan) = firmware_detector.detect(path).await? {
|
||||||
verdicts.push(detection_to_verdict(&detection));
|
verdicts.push(plan_to_verdict(&plan));
|
||||||
tramiton_used = true;
|
tramiton_used = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -154,7 +155,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn no_firmware() -> MockFirmwareDetector {
|
fn no_firmware() -> MockFirmwareDetector {
|
||||||
MockFirmwareDetector { detection: None }
|
MockFirmwareDetector { plan: None }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -189,12 +190,12 @@ mod tests {
|
|||||||
target.artifacts.push(artifact);
|
target.artifacts.push(artifact);
|
||||||
|
|
||||||
let detector = MockFirmwareDetector {
|
let detector = MockFirmwareDetector {
|
||||||
detection: Some(FirmwareDetection {
|
plan: Some(TramitonBuildPlan {
|
||||||
provider: "zephyr".to_string(),
|
provider: "zephyr".to_string(),
|
||||||
confidence: "high".to_string(),
|
confidence: "high".to_string(),
|
||||||
build_system: "zephyr".to_string(),
|
build_system: "zephyr".to_string(),
|
||||||
framework: Some("zephyr".to_string()),
|
framework: Some("zephyr".to_string()),
|
||||||
target: FirmwareTarget {
|
target: TramitonTarget {
|
||||||
mcu: Some("nrf52840".to_string()),
|
mcu: Some("nrf52840".to_string()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -25,9 +25,8 @@ impl TestServer {
|
|||||||
let mongodb_uri = std::env::var("TEST_MONGODB_URI")
|
let mongodb_uri = std::env::var("TEST_MONGODB_URI")
|
||||||
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
||||||
|
|
||||||
// Unique db-name prefix per run. Must fit the pool's 30-char cap
|
// Unique database name per test run to avoid collisions
|
||||||
// (`<prefix>_<32 hex>` <= 63), so use a 16-hex-char suffix.
|
let db_name = format!("test_{}", uuid::Uuid::new_v4().simple());
|
||||||
let db_name = format!("t_{}", &uuid::Uuid::new_v4().simple().to_string()[..16]);
|
|
||||||
|
|
||||||
let db_pool = DatabasePool::connect(&mongodb_uri, &db_name)
|
let db_pool = DatabasePool::connect(&mongodb_uri, &db_name)
|
||||||
.await
|
.await
|
||||||
|
|||||||
@@ -2,6 +2,5 @@ mod cascade_delete;
|
|||||||
mod dast;
|
mod dast;
|
||||||
mod findings;
|
mod findings;
|
||||||
mod health;
|
mod health;
|
||||||
mod onboarding;
|
|
||||||
mod repositories;
|
mod repositories;
|
||||||
mod stats;
|
mod stats;
|
||||||
|
|||||||
@@ -1,115 +0,0 @@
|
|||||||
use crate::common::TestServer;
|
|
||||||
use serde_json::json;
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn create_list_and_applicable_scans() {
|
|
||||||
let server = TestServer::start().await;
|
|
||||||
|
|
||||||
// Initially empty.
|
|
||||||
let resp = server.get("/api/v1/targets").await;
|
|
||||||
assert_eq!(resp.status(), 200);
|
|
||||||
let body: serde_json::Value = resp.json().await.unwrap();
|
|
||||||
assert_eq!(body["data"].as_array().unwrap().len(), 0);
|
|
||||||
|
|
||||||
// Create a web-app target with a git repo + a live URL.
|
|
||||||
let resp = server
|
|
||||||
.post(
|
|
||||||
"/api/v1/targets",
|
|
||||||
&json!({
|
|
||||||
"name": "acme-web",
|
|
||||||
"target_type": "web_app",
|
|
||||||
"artifacts": [
|
|
||||||
{ "kind": "git_repo", "source_ref": "https://git/acme.git", "branch": "main" },
|
|
||||||
{ "kind": "live_url", "source_ref": "https://acme.example.com" }
|
|
||||||
]
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
assert_eq!(resp.status(), 200);
|
|
||||||
let body: serde_json::Value = resp.json().await.unwrap();
|
|
||||||
let id = body["data"]["_id"]["$oid"].as_str().unwrap().to_string();
|
|
||||||
assert!(!id.is_empty());
|
|
||||||
assert_eq!(body["data"]["artifacts"].as_array().unwrap().len(), 2);
|
|
||||||
|
|
||||||
// List returns it.
|
|
||||||
let resp = server.get("/api/v1/targets").await;
|
|
||||||
let body: serde_json::Value = resp.json().await.unwrap();
|
|
||||||
assert_eq!(body["data"].as_array().unwrap().len(), 1);
|
|
||||||
|
|
||||||
// Applicable scans: SAST present + DAST offered (live URL present), pentest supported.
|
|
||||||
let resp = server
|
|
||||||
.get(&format!("/api/v1/targets/{id}/applicable-scans"))
|
|
||||||
.await;
|
|
||||||
assert_eq!(resp.status(), 200);
|
|
||||||
let body: serde_json::Value = resp.json().await.unwrap();
|
|
||||||
let scans = body["data"]["scans"].as_array().unwrap();
|
|
||||||
let names: Vec<&str> = scans.iter().filter_map(|s| s["scan"].as_str()).collect();
|
|
||||||
assert!(names.contains(&"sast"));
|
|
||||||
assert!(names.contains(&"dast"));
|
|
||||||
assert_eq!(body["data"]["pentest_supported"], true);
|
|
||||||
|
|
||||||
server.cleanup().await;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn detect_classifies_a_plc_target() {
|
|
||||||
let server = TestServer::start().await;
|
|
||||||
|
|
||||||
// A PLC project artifact is a strong kind-based signal.
|
|
||||||
let resp = server
|
|
||||||
.post(
|
|
||||||
"/api/v1/targets",
|
|
||||||
&json!({
|
|
||||||
"name": "line-controller",
|
|
||||||
"target_type": "backend_service", // deliberately wrong; detect should suggest PLC
|
|
||||||
"artifacts": [
|
|
||||||
{ "kind": "plc_project", "source_ref": "line.xml", "plc_format": "plcopen_xml" }
|
|
||||||
]
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
let body: serde_json::Value = resp.json().await.unwrap();
|
|
||||||
let id = body["data"]["_id"]["$oid"].as_str().unwrap().to_string();
|
|
||||||
|
|
||||||
let resp = server
|
|
||||||
.post(&format!("/api/v1/targets/{id}/detect"), &json!({}))
|
|
||||||
.await;
|
|
||||||
assert_eq!(resp.status(), 200);
|
|
||||||
let body: serde_json::Value = resp.json().await.unwrap();
|
|
||||||
assert_eq!(body["data"]["classification"]["suggested"], "plc_sps");
|
|
||||||
|
|
||||||
server.cleanup().await;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn add_artifact_and_delete_target() {
|
|
||||||
let server = TestServer::start().await;
|
|
||||||
|
|
||||||
let resp = server
|
|
||||||
.post(
|
|
||||||
"/api/v1/targets",
|
|
||||||
&json!({ "name": "svc", "target_type": "backend_service" }),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
let body: serde_json::Value = resp.json().await.unwrap();
|
|
||||||
let id = body["data"]["_id"]["$oid"].as_str().unwrap().to_string();
|
|
||||||
|
|
||||||
// Attach a git repo.
|
|
||||||
let resp = server
|
|
||||||
.post(
|
|
||||||
&format!("/api/v1/targets/{id}/artifacts"),
|
|
||||||
&json!({ "kind": "git_repo", "source_ref": "https://git/svc.git" }),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
assert_eq!(resp.status(), 200);
|
|
||||||
let body: serde_json::Value = resp.json().await.unwrap();
|
|
||||||
assert_eq!(body["data"]["artifacts"].as_array().unwrap().len(), 1);
|
|
||||||
|
|
||||||
// Delete it.
|
|
||||||
let resp = server.delete(&format!("/api/v1/targets/{id}")).await;
|
|
||||||
assert_eq!(resp.status(), 200);
|
|
||||||
let resp = server.get(&format!("/api/v1/targets/{id}")).await;
|
|
||||||
assert_eq!(resp.status(), 404);
|
|
||||||
|
|
||||||
server.cleanup().await;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user