fix(ops): harden runtime placeholder guards
This commit is contained in:
@@ -65,6 +65,103 @@ pub fn parse_utc_rfc3339(value: &str) -> Result<DateTime<Utc>> {
|
||||
.map(|ts| ts.with_timezone(&Utc))
|
||||
}
|
||||
|
||||
pub mod runtime_guard {
|
||||
use anyhow::{Result, bail};
|
||||
|
||||
const TEST_NET_MARKERS: [&str; 3] = ["192.0.2.", "198.51.100.", "203.0.113."];
|
||||
const CONTAINS_PLACEHOLDERS: [&str; 2] = ["HOST-EXAMPLE", "WINDOWS_USER_EXAMPLE"];
|
||||
const EXACT_PLACEHOLDERS: [&str; 9] = [
|
||||
"CHANGE_ME",
|
||||
"CHANGEME",
|
||||
"REPLACE_ME",
|
||||
"REPLACE-ME",
|
||||
"YOUR_TOKEN",
|
||||
"YOUR_API_KEY",
|
||||
"TOKEN",
|
||||
"SECRET",
|
||||
"PASSWORD",
|
||||
];
|
||||
|
||||
pub fn is_runtime_placeholder(value: &str) -> bool {
|
||||
let trimmed = value.trim();
|
||||
if trimmed.is_empty() {
|
||||
return true;
|
||||
}
|
||||
let normalized = trimmed.to_ascii_uppercase();
|
||||
TEST_NET_MARKERS
|
||||
.iter()
|
||||
.any(|marker| normalized.contains(marker))
|
||||
|| CONTAINS_PLACEHOLDERS
|
||||
.iter()
|
||||
.any(|placeholder| normalized.contains(placeholder))
|
||||
|| EXACT_PLACEHOLDERS
|
||||
.iter()
|
||||
.any(|placeholder| normalized == *placeholder)
|
||||
|| normalized == "EXAMPLE"
|
||||
|| normalized.contains("-EXAMPLE")
|
||||
|| normalized.contains("_EXAMPLE")
|
||||
|| normalized.contains("EXAMPLE.")
|
||||
|| normalized.contains(".EXAMPLE")
|
||||
|| normalized.starts_with("YOUR_")
|
||||
|| (normalized.starts_with('<') && normalized.ends_with('>'))
|
||||
}
|
||||
|
||||
pub fn is_secret_placeholder(value: &str) -> bool {
|
||||
is_runtime_placeholder(value)
|
||||
|| matches!(
|
||||
value.trim().to_ascii_uppercase().as_str(),
|
||||
"API_KEY" | "INFLUX_TOKEN" | "WRITE_TOKEN" | "BEARER_TOKEN"
|
||||
)
|
||||
}
|
||||
|
||||
pub fn ensure_runtime_value(name: &str, value: &str, context: &str) -> Result<()> {
|
||||
if is_runtime_placeholder(value) {
|
||||
bail!("{name} contains an empty/example/TEST-NET value while {context}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn ensure_secret_value(name: &str, value: &str, context: &str) -> Result<()> {
|
||||
if is_secret_placeholder(value) {
|
||||
bail!("{name} contains an empty/example secret value while {context}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn ensure_runtime_values<'a>(
|
||||
name: &str,
|
||||
values: impl IntoIterator<Item = &'a String>,
|
||||
context: &str,
|
||||
) -> Result<()> {
|
||||
let mut empty = true;
|
||||
for value in values {
|
||||
empty = false;
|
||||
ensure_runtime_value(name, value, context)?;
|
||||
}
|
||||
if empty {
|
||||
bail!("{name} contains an empty/example value while {context}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn ensure_influx_runtime_config(
|
||||
prefix: &str,
|
||||
url: &str,
|
||||
org: &str,
|
||||
bucket: &str,
|
||||
token: &str,
|
||||
hosts: &[String],
|
||||
) -> Result<()> {
|
||||
let context = format!("{prefix}_ENABLED=true");
|
||||
ensure_runtime_value(&format!("{prefix}_URL"), url, &context)?;
|
||||
ensure_runtime_value(&format!("{prefix}_ORG"), org, &context)?;
|
||||
ensure_runtime_value(&format!("{prefix}_BUCKET"), bucket, &context)?;
|
||||
ensure_secret_value(&format!("{prefix}_TOKEN"), token, &context)?;
|
||||
ensure_runtime_values(&format!("{prefix}_HOSTS"), hosts, &context)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -85,4 +182,45 @@ mod tests {
|
||||
"2026-05-31T10:20:30Z"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runtime_guard_detects_public_placeholders() {
|
||||
assert!(runtime_guard::is_runtime_placeholder("HOST-EXAMPLE"));
|
||||
assert!(runtime_guard::is_runtime_placeholder(
|
||||
"http://192.0.2.10:8086"
|
||||
));
|
||||
assert!(runtime_guard::is_runtime_placeholder("<TOKEN>"));
|
||||
assert!(runtime_guard::is_secret_placeholder("CHANGE_ME"));
|
||||
assert!(!runtime_guard::is_runtime_placeholder("aw_metrics"));
|
||||
assert!(!runtime_guard::is_runtime_placeholder("proxmox"));
|
||||
assert!(!runtime_guard::is_secret_placeholder(
|
||||
"prod-write-token-value"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runtime_guard_validates_full_influx_config() {
|
||||
let hosts = vec!["WINDOWS-HOST".to_string()];
|
||||
runtime_guard::ensure_influx_runtime_config(
|
||||
"AW_WORKTIME_INFLUX",
|
||||
"http://influxdb.internal:8086",
|
||||
"proxmox",
|
||||
"aw_metrics",
|
||||
"prod-write-token-value",
|
||||
&hosts,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let err = runtime_guard::ensure_influx_runtime_config(
|
||||
"AW_WORKTIME_INFLUX",
|
||||
"http://influxdb.internal:8086",
|
||||
"proxmox",
|
||||
"aw_metrics",
|
||||
"CHANGE_ME",
|
||||
&hosts,
|
||||
)
|
||||
.unwrap_err()
|
||||
.to_string();
|
||||
assert!(err.contains("AW_WORKTIME_INFLUX_TOKEN"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ publish = false
|
||||
anyhow.workspace = true
|
||||
chrono.workspace = true
|
||||
clap.workspace = true
|
||||
detmir-core.workspace = true
|
||||
reqwest.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
|
||||
@@ -3,6 +3,7 @@ use std::{thread, time::Duration};
|
||||
use anyhow::{Context, Result, anyhow, bail};
|
||||
use chrono::{DateTime, TimeDelta, Utc};
|
||||
use clap::Parser;
|
||||
use detmir_core::runtime_guard::ensure_influx_runtime_config;
|
||||
use reqwest::blocking::Client;
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
@@ -120,31 +121,18 @@ fn load_config() -> Config {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_runtime_placeholder(value: &str) -> bool {
|
||||
let normalized = value.trim().to_ascii_uppercase();
|
||||
normalized.is_empty()
|
||||
|| normalized.contains("HOST-EXAMPLE")
|
||||
|| normalized.contains("WINDOWS_USER_EXAMPLE")
|
||||
|| normalized.contains("192.0.2.")
|
||||
|| normalized.contains("198.51.100.")
|
||||
|| normalized.contains("203.0.113.")
|
||||
}
|
||||
|
||||
fn validate_runtime_config(config: &Config) -> Result<()> {
|
||||
if !config.influx_enabled {
|
||||
return Ok(());
|
||||
}
|
||||
if is_runtime_placeholder(&config.influx_url) {
|
||||
bail!(
|
||||
"AW_DLP_INFLUX_URL contains an empty/example/TEST-NET value while AW_DLP_INFLUX_ENABLED=true"
|
||||
);
|
||||
}
|
||||
if config.hosts.is_empty() || config.hosts.iter().any(|host| is_runtime_placeholder(host)) {
|
||||
bail!(
|
||||
"AW_DLP_INFLUX_HOSTS contains an empty/example value while AW_DLP_INFLUX_ENABLED=true"
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
ensure_influx_runtime_config(
|
||||
"AW_DLP_INFLUX",
|
||||
&config.influx_url,
|
||||
&config.influx_org,
|
||||
&config.influx_bucket,
|
||||
&config.influx_token,
|
||||
&config.hosts,
|
||||
)
|
||||
}
|
||||
|
||||
fn utc_now() -> DateTime<Utc> {
|
||||
@@ -1056,9 +1044,20 @@ mod tests {
|
||||
let err = validate_runtime_config(&config).unwrap_err().to_string();
|
||||
assert!(err.contains("AW_DLP_INFLUX_URL"));
|
||||
|
||||
config.influx_url = "http://influxdb.example.internal:8086".to_string();
|
||||
config.influx_url = "http://influxdb.internal:8086".to_string();
|
||||
config.influx_token = "prod-write-token-value".to_string();
|
||||
let err = validate_runtime_config(&config).unwrap_err().to_string();
|
||||
assert!(err.contains("AW_DLP_INFLUX_HOSTS"));
|
||||
|
||||
config.hosts = vec!["WINDOWS-HOST".to_string()];
|
||||
config.influx_bucket = "BUCKET-EXAMPLE".to_string();
|
||||
let err = validate_runtime_config(&config).unwrap_err().to_string();
|
||||
assert!(err.contains("AW_DLP_INFLUX_BUCKET"));
|
||||
|
||||
config.influx_bucket = DEFAULT_INFLUX_BUCKET.to_string();
|
||||
config.influx_token = "CHANGE_ME".to_string();
|
||||
let err = validate_runtime_config(&config).unwrap_err().to_string();
|
||||
assert!(err.contains("AW_DLP_INFLUX_TOKEN"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -10,6 +10,7 @@ publish = false
|
||||
anyhow.workspace = true
|
||||
chrono.workspace = true
|
||||
clap.workspace = true
|
||||
detmir-core.workspace = true
|
||||
reqwest.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
|
||||
@@ -3,6 +3,7 @@ use std::{collections::HashMap, thread, time::Duration};
|
||||
use anyhow::{Context, Result, anyhow, bail};
|
||||
use chrono::{DateTime, Datelike, FixedOffset, NaiveDate, TimeDelta, TimeZone, Timelike, Utc};
|
||||
use clap::Parser;
|
||||
use detmir_core::runtime_guard::ensure_influx_runtime_config;
|
||||
use reqwest::blocking::Client;
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
@@ -204,31 +205,18 @@ fn load_config() -> Config {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_runtime_placeholder(value: &str) -> bool {
|
||||
let normalized = value.trim().to_ascii_uppercase();
|
||||
normalized.is_empty()
|
||||
|| normalized.contains("HOST-EXAMPLE")
|
||||
|| normalized.contains("WINDOWS_USER_EXAMPLE")
|
||||
|| normalized.contains("192.0.2.")
|
||||
|| normalized.contains("198.51.100.")
|
||||
|| normalized.contains("203.0.113.")
|
||||
}
|
||||
|
||||
fn validate_runtime_config(config: &Config) -> Result<()> {
|
||||
if !config.influx_enabled {
|
||||
return Ok(());
|
||||
}
|
||||
if is_runtime_placeholder(&config.influx_url) {
|
||||
bail!(
|
||||
"AW_WORKTIME_INFLUX_URL contains an empty/example/TEST-NET value while AW_WORKTIME_INFLUX_ENABLED=true"
|
||||
);
|
||||
}
|
||||
if config.hosts.is_empty() || config.hosts.iter().any(|host| is_runtime_placeholder(host)) {
|
||||
bail!(
|
||||
"AW_WORKTIME_INFLUX_HOSTS contains an empty/example value while AW_WORKTIME_INFLUX_ENABLED=true"
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
ensure_influx_runtime_config(
|
||||
"AW_WORKTIME_INFLUX",
|
||||
&config.influx_url,
|
||||
&config.influx_org,
|
||||
&config.influx_bucket,
|
||||
&config.influx_token,
|
||||
&config.hosts,
|
||||
)
|
||||
}
|
||||
|
||||
fn utc_now() -> DateTime<Utc> {
|
||||
@@ -1042,9 +1030,20 @@ mod tests {
|
||||
let err = validate_runtime_config(&config).unwrap_err().to_string();
|
||||
assert!(err.contains("AW_WORKTIME_INFLUX_URL"));
|
||||
|
||||
config.influx_url = "http://influxdb.example.internal:8086".to_string();
|
||||
config.influx_url = "http://influxdb.internal:8086".to_string();
|
||||
config.influx_token = "prod-write-token-value".to_string();
|
||||
let err = validate_runtime_config(&config).unwrap_err().to_string();
|
||||
assert!(err.contains("AW_WORKTIME_INFLUX_HOSTS"));
|
||||
|
||||
config.hosts = vec!["WINDOWS-HOST".to_string()];
|
||||
config.influx_bucket = "BUCKET-EXAMPLE".to_string();
|
||||
let err = validate_runtime_config(&config).unwrap_err().to_string();
|
||||
assert!(err.contains("AW_WORKTIME_INFLUX_BUCKET"));
|
||||
|
||||
config.influx_bucket = DEFAULT_INFLUX_BUCKET.to_string();
|
||||
config.influx_token = "CHANGE_ME".to_string();
|
||||
let err = validate_runtime_config(&config).unwrap_err().to_string();
|
||||
assert!(err.contains("AW_WORKTIME_INFLUX_TOKEN"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user