Files
pruefwerk/compliance-agent/src/config.rs
T
sharangandClaude Opus 5.5 918c871a25
CI / Check (push) Skipped
CI / Detect Changes (push) Successful in 7s
CI / Deploy Docs (push) Skipped
CI / Deploy Dashboard (push) Successful in 6m31s
CI / Deploy MCP (push) Successful in 3m45s
CI / Deploy Agent (push) Successful in 8m33s
feat(findings): one finding for a rule that fires all over a repository (#464) (#477)
Closes #464.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
2026-10-05 23:52:07 +00:00

183 lines
8.5 KiB
Rust

use compliance_core::config::{AppEnv, BreakpilotConfig, Features, PlcRuntimeConfig};
use compliance_core::AgentConfig;
use secrecy::SecretString;
use crate::error::AgentError;
fn env_var(key: &str) -> Result<String, AgentError> {
std::env::var(key).map_err(|_| AgentError::Config(format!("Missing env var: {key}")))
}
fn env_var_opt(key: &str) -> Option<String> {
std::env::var(key).ok().filter(|v| !v.is_empty())
}
fn env_secret_opt(key: &str) -> Option<SecretString> {
env_var_opt(key).map(SecretString::from)
}
pub fn load_config() -> Result<AgentConfig, AgentError> {
let app_env =
AppEnv::parse(&env_var_opt("APP_ENV").unwrap_or_default()).map_err(AgentError::Config)?;
let config = build_config(app_env)?;
// A malformed key would leave every credential unstorable, so refuse to
// start in every environment, not only hardened ones.
crate::secrets::SecretBox::check_config(&config)
.map_err(|e| AgentError::Config(e.to_string()))?;
// Same for the managed git source settings: a bad host pattern must not
// leave every host (or none) allowed.
crate::git_source::Settings::from_env().map_err(AgentError::Config)?;
// And for how findings are grouped (#464).
crate::pipeline::grouping::GroupingPolicy::from_env().map_err(AgentError::Config)?;
if let Err(problems) = config.validate_deployment() {
return Err(AgentError::Config(format!(
"refusing to start in {}:\n - {}",
app_env.as_str(),
problems.join("\n - ")
)));
}
Ok(config)
}
fn build_config(app_env: AppEnv) -> Result<AgentConfig, AgentError> {
Ok(AgentConfig {
mongodb_uri: env_var("MONGODB_URI")?,
mongodb_database: env_var_opt("MONGODB_DATABASE")
.unwrap_or_else(|| "compliance_scanner".to_string()),
litellm_url: env_var_opt("LITELLM_URL")
.unwrap_or_else(|| "http://localhost:4000".to_string()),
litellm_api_key: SecretString::from(env_var_opt("LITELLM_API_KEY").unwrap_or_default()),
litellm_model: env_var_opt("LITELLM_MODEL").unwrap_or_else(|| "gpt-4o".to_string()),
litellm_embed_model: env_var_opt("LITELLM_EMBED_MODEL")
.unwrap_or_else(|| "text-embedding-3-small".to_string()),
github_token: env_secret_opt("GITHUB_TOKEN"),
github_webhook_secret: env_secret_opt("GITHUB_WEBHOOK_SECRET"),
gitlab_url: env_var_opt("GITLAB_URL"),
gitlab_token: env_secret_opt("GITLAB_TOKEN"),
gitlab_webhook_secret: env_secret_opt("GITLAB_WEBHOOK_SECRET"),
jira_url: env_var_opt("JIRA_URL"),
jira_email: env_var_opt("JIRA_EMAIL"),
jira_api_token: env_secret_opt("JIRA_API_TOKEN"),
jira_project_key: env_var_opt("JIRA_PROJECT_KEY"),
searxng_url: env_var_opt("SEARXNG_URL"),
nvd_api_key: env_secret_opt("NVD_API_KEY"),
agent_port: env_var_opt("AGENT_PORT")
.and_then(|p| p.parse().ok())
.unwrap_or(3001),
webhook_port: env_var_opt("WEBHOOK_PORT")
.and_then(|p| p.parse().ok())
.unwrap_or(3002),
scan_concurrency: env_var_opt("SCAN_CONCURRENCY")
.and_then(|v| v.parse::<usize>().ok())
.filter(|n| *n > 0)
.unwrap_or(2),
scan_schedule: env_var_opt("SCAN_SCHEDULE").unwrap_or_else(|| "0 0 */6 * * *".to_string()),
cve_monitor_schedule: env_var_opt("CVE_MONITOR_SCHEDULE")
.unwrap_or_else(|| "0 0 * * * *".to_string()),
git_clone_base_path: env_var_opt("GIT_CLONE_BASE_PATH")
.unwrap_or_else(|| "/tmp/compliance-scanner/repos".to_string()),
artifact_store_base_path: env_var_opt("ARTIFACT_STORE_BASE_PATH")
.unwrap_or_else(|| "/data/compliance-scanner/artifacts".to_string()),
keycloak_url: env_var_opt("KEYCLOAK_URL"),
keycloak_realm: env_var_opt("KEYCLOAK_REALM"),
keycloak_admin_username: env_var_opt("KEYCLOAK_ADMIN_USERNAME"),
keycloak_admin_password: env_secret_opt("KEYCLOAK_ADMIN_PASSWORD"),
pentest_verification_email: env_var_opt("PENTEST_VERIFICATION_EMAIL"),
pentest_imap_host: env_var_opt("PENTEST_IMAP_HOST"),
pentest_imap_port: env_var_opt("PENTEST_IMAP_PORT").and_then(|p| p.parse().ok()),
pentest_imap_tls: env_var_opt("PENTEST_IMAP_TLS")
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
.unwrap_or(true),
pentest_imap_username: env_var_opt("PENTEST_IMAP_USERNAME"),
pentest_imap_password: env_secret_opt("PENTEST_IMAP_PASSWORD"),
admin_api_token: env_secret_opt("ADMIN_API_TOKEN"),
secrets_encryption_key: env_secret_opt("SECRETS_ENCRYPTION_KEY"),
tenant_registry_url: env_var_opt("TENANT_REGISTRY_URL"),
plc_runtime: load_plc_runtime_config(),
werkbank_runner_token: env_secret_opt("WERKBANK_RUNNER_TOKEN"),
breakpilot: load_breakpilot_config(),
product_key: env_var_opt("PRODUCT_KEY").unwrap_or_else(|| "pruefwerk".to_string()),
// Audience defaults to the product key; set KEYCLOAK_AUDIENCE="" to disable in dev.
keycloak_audience: match std::env::var("KEYCLOAK_AUDIENCE") {
Ok(v) if v.is_empty() => None,
Ok(v) => Some(v),
Err(_) => Some("pruefwerk".to_string()),
},
tenant_registry_client_id: env_var_opt("TENANT_REGISTRY_CLIENT_ID"),
tenant_registry_client_secret: env_secret_opt("TENANT_REGISTRY_CLIENT_SECRET"),
cors_allowed_origins: env_csv("CORS_ALLOWED_ORIGINS"),
platform_allowed_clients: {
// Built-in defaults are a dev convenience only: a hardened
// deployment names the clients it trusts, so a prod agent never
// accepts a staging service token (validate_deployment enforces it).
let v = env_csv("PLATFORM_ALLOWED_CLIENTS");
if v.is_empty() && !app_env.is_hardened() {
[
"tenant-registry-svc-stage",
"tenant-registry-svc-prod",
"portal-svc-stage",
"portal-svc-prod",
]
.iter()
.map(|s| s.to_string())
.collect()
} else {
v
}
},
features: Features::parse(&env_var_opt("PW_FEATURES").unwrap_or_default()),
app_env,
})
}
fn env_csv(key: &str) -> Vec<String> {
env_var_opt(key)
.map(|s| {
s.split(',')
.map(str::trim)
.filter(|s| !s.is_empty())
.map(String::from)
.collect()
})
.unwrap_or_default()
}
/// Build the ephemeral soft-PLC provisioning config from the environment,
/// falling back to [`PlcRuntimeConfig::default`] for any unset knob. Disabled
/// unless `PLC_RUNTIME_ENABLED` is truthy — it requires Docker access.
fn load_plc_runtime_config() -> PlcRuntimeConfig {
let d = PlcRuntimeConfig::default();
PlcRuntimeConfig {
enabled: env_var_opt("PLC_RUNTIME_ENABLED")
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
.unwrap_or(d.enabled),
image: env_var_opt("PLC_RUNTIME_IMAGE").unwrap_or(d.image),
network: env_var_opt("PLC_RUNTIME_NETWORK").unwrap_or(d.network),
memory: env_var_opt("PLC_RUNTIME_MEMORY").unwrap_or(d.memory),
cpus: env_var_opt("PLC_RUNTIME_CPUS").unwrap_or(d.cpus),
max_lifetime_secs: env_var_opt("PLC_RUNTIME_MAX_LIFETIME_SECS")
.and_then(|v| v.parse().ok())
.unwrap_or(d.max_lifetime_secs),
openplc_user: env_var_opt("PLC_RUNTIME_OPENPLC_USER").unwrap_or(d.openplc_user),
openplc_password: env_secret_opt("PLC_RUNTIME_OPENPLC_PASSWORD")
.unwrap_or(d.openplc_password),
}
}
/// Assemble the breakpilot OSCAL-catalog source from env, defaulting the snapshot
/// directory. A missing `BREAKPILOT_BASE_URL` leaves the controls provider off.
fn load_breakpilot_config() -> BreakpilotConfig {
let d = BreakpilotConfig::default();
BreakpilotConfig {
base_url: env_var_opt("BREAKPILOT_BASE_URL"),
token: env_secret_opt("BREAKPILOT_TOKEN"),
snapshot_dir: env_var_opt("BREAKPILOT_SNAPSHOT_DIR").unwrap_or(d.snapshot_dir),
semantic_mapping: env_var_opt("BREAKPILOT_SEMANTIC_MAPPING")
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
.unwrap_or(d.semantic_mapping),
grounded_control_checks: env_var_opt("BREAKPILOT_GROUNDED_CHECKS")
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
.unwrap_or(d.grounded_control_checks),
}
}