fix(ops): harden runtime placeholder guards
This commit is contained in:
@@ -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