CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Canceled after 6m28s
Git tokens, tracker tokens, webhook secrets and pentest passwords were stored
in plain text and returned by the API. The old crypto helper fell back to
plain text when no key was set.
- `secrets::SecretBox`: AES-256-GCM under `SECRETS_ENCRYPTION_KEY`, with sealed
values stored as `enc:v1:…`. With no key it refuses to seal instead of
storing plain text. The key is required when APP_ENV is hardened, and a
malformed key stops startup in every environment. It replaces
`pentest/crypto.rs`.
- `compliance_core::models::Credentials` lists each record's credential fields
once. The agent seals them before a write and redacts them from every target,
DAST target and pentest-session response, including the MCP pentest tools.
- `PATCH /targets/{id}`: a credential left out keeps its stored value, an
empty string clears it, and a new one is sealed. A client echoing a redacted
record back therefore cannot erase a token.
- Pentest sessions are sealed before the first insert. Previously they were
inserted in plain text and encrypted afterwards.
- On startup the agent seals credentials that were stored in plain text.
- Webhook receivers fail closed: a target without a usable secret gets 401
instead of unsigned acceptance. GitLab tokens are compared in constant time.
New targets no longer get a webhook secret at creation; issuing one comes
back with the webhook work.
Part 1 of #260 (agent). Dashboard session hardening, the open redirect and
the webhook proxy follow in a separate PR.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
178 lines
6.3 KiB
Rust
178 lines
6.3 KiB
Rust
// Shared test harness for E2E / integration tests.
|
|
//
|
|
// Spins up the agent API server on a random port with an isolated test
|
|
// database. Each test gets a fresh database that is dropped on cleanup.
|
|
//
|
|
// Included via `mod common;` in several test binaries; not every binary uses
|
|
// every helper, so allow dead code here.
|
|
#![allow(dead_code)]
|
|
|
|
use std::sync::Arc;
|
|
|
|
use compliance_agent::agent::ComplianceAgent;
|
|
use compliance_agent::api;
|
|
use compliance_agent::database::DatabasePool;
|
|
use compliance_core::AgentConfig;
|
|
use secrecy::SecretString;
|
|
|
|
/// The runner bearer token wired into the test config.
|
|
pub const TEST_RUNNER_TOKEN: &str = "test-runner-token";
|
|
|
|
/// The credential-sealing key wired into the test config (test-only value).
|
|
pub const TEST_SECRETS_KEY: &str =
|
|
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
|
|
|
/// A minimal dev [`AgentConfig`] for tests: unauthenticated (no Keycloak), the
|
|
/// Werkbank runner API enabled with [`TEST_RUNNER_TOKEN`].
|
|
pub fn dev_config(mongodb_uri: String, db_name: String) -> AgentConfig {
|
|
AgentConfig {
|
|
mongodb_uri,
|
|
mongodb_database: db_name,
|
|
litellm_url: std::env::var("TEST_LITELLM_URL")
|
|
.unwrap_or_else(|_| "http://localhost:4000".into()),
|
|
agent_port: 0, // not used — we bind ourselves
|
|
scan_schedule: String::new(),
|
|
cve_monitor_schedule: String::new(),
|
|
git_clone_base_path: "/tmp/compliance-scanner-tests/repos".into(),
|
|
artifact_store_base_path: "/tmp/compliance-scanner-tests/artifacts".into(),
|
|
pentest_imap_tls: false,
|
|
werkbank_runner_token: Some(SecretString::from(TEST_RUNNER_TOKEN.to_string())),
|
|
secrets_encryption_key: Some(SecretString::from(TEST_SECRETS_KEY.to_string())),
|
|
// Unauthenticated dev: no Keycloak, no audience, no registry.
|
|
keycloak_audience: None,
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
/// A running test server with a unique database.
|
|
pub struct TestServer {
|
|
pub base_url: String,
|
|
pub client: reqwest::Client,
|
|
db_name: String,
|
|
mongodb_uri: String,
|
|
}
|
|
|
|
impl TestServer {
|
|
/// Start an agent API server on a random port with an isolated database.
|
|
pub async fn start() -> Self {
|
|
let mongodb_uri = std::env::var("TEST_MONGODB_URI")
|
|
.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
|
|
// (`<prefix>_<32 hex>` <= 63), so use a 16-hex-char suffix.
|
|
let db_name = format!("t_{}", &uuid::Uuid::new_v4().simple().to_string()[..16]);
|
|
|
|
let db_pool = DatabasePool::connect(&mongodb_uri, &db_name)
|
|
.await
|
|
.expect("Failed to build DatabasePool");
|
|
|
|
let config = dev_config(mongodb_uri.clone(), db_name.clone());
|
|
|
|
let agent = ComplianceAgent::new(config, db_pool);
|
|
|
|
// Build the router with the agent extension. After M7.2-B every
|
|
// handler takes a TenantCtx extractor; without KC in the test
|
|
// harness, the dev-tenant injector mounts a synthetic context so
|
|
// tests run end-to-end against `<db_name>_dev`.
|
|
let app = api::routes::build_router()
|
|
.layer(axum::extract::Extension(Arc::new(agent)))
|
|
.layer(axum::middleware::from_fn(api::server::inject_dev_tenant))
|
|
.layer(tower_http::cors::CorsLayer::permissive());
|
|
|
|
// Bind to port 0 to get a random available port
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
|
|
.await
|
|
.expect("Failed to bind test server");
|
|
let port = listener.local_addr().expect("no local addr").port();
|
|
|
|
tokio::spawn(async move {
|
|
axum::serve(listener, app).await.ok();
|
|
});
|
|
|
|
let base_url = format!("http://127.0.0.1:{port}");
|
|
let client = reqwest::Client::builder()
|
|
.timeout(std::time::Duration::from_secs(30))
|
|
.build()
|
|
.expect("Failed to build HTTP client");
|
|
|
|
// Wait for server to be ready
|
|
for _ in 0..50 {
|
|
if client
|
|
.get(format!("{base_url}/api/v1/health"))
|
|
.send()
|
|
.await
|
|
.is_ok()
|
|
{
|
|
break;
|
|
}
|
|
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
|
|
}
|
|
|
|
Self {
|
|
base_url,
|
|
client,
|
|
db_name,
|
|
mongodb_uri,
|
|
}
|
|
}
|
|
|
|
/// GET helper
|
|
pub async fn get(&self, path: &str) -> reqwest::Response {
|
|
self.client
|
|
.get(format!("{}{path}", self.base_url))
|
|
.send()
|
|
.await
|
|
.expect("GET request failed")
|
|
}
|
|
|
|
/// POST helper with JSON body
|
|
pub async fn post(&self, path: &str, body: &serde_json::Value) -> reqwest::Response {
|
|
self.client
|
|
.post(format!("{}{path}", self.base_url))
|
|
.json(body)
|
|
.send()
|
|
.await
|
|
.expect("POST request failed")
|
|
}
|
|
|
|
/// PATCH helper with JSON body
|
|
pub async fn patch(&self, path: &str, body: &serde_json::Value) -> reqwest::Response {
|
|
self.client
|
|
.patch(format!("{}{path}", self.base_url))
|
|
.json(body)
|
|
.send()
|
|
.await
|
|
.expect("PATCH request failed")
|
|
}
|
|
|
|
/// DELETE helper
|
|
pub async fn delete(&self, path: &str) -> reqwest::Response {
|
|
self.client
|
|
.delete(format!("{}{path}", self.base_url))
|
|
.send()
|
|
.await
|
|
.expect("DELETE request failed")
|
|
}
|
|
|
|
/// Get the unique database name for direct MongoDB access in tests.
|
|
pub fn db_name(&self) -> &str {
|
|
&self.db_name
|
|
}
|
|
|
|
/// Drop every per-tenant database belonging to this test run.
|
|
/// Post-M7.2-D the agent never opens a `db_name` directly —
|
|
/// data lives only in `<db_name>_<tenant>` per-tenant databases.
|
|
pub async fn cleanup(&self) {
|
|
if let Ok(client) = mongodb::Client::with_uri_str(&self.mongodb_uri).await {
|
|
if let Ok(names) = client.list_database_names().await {
|
|
let prefix = format!("{}_", self.db_name);
|
|
for name in names {
|
|
if name.starts_with(&prefix) {
|
|
client.database(&name).drop().await.ok();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|