git init
This commit is contained in:
263
agent/src/config.rs
Normal file
263
agent/src/config.rs
Normal file
@@ -0,0 +1,263 @@
|
||||
use anyhow::{bail, Context, Result};
|
||||
use serde::Deserialize;
|
||||
use std::{net::SocketAddr, path::Path};
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct Config {
|
||||
pub server: Server,
|
||||
pub auth: Auth,
|
||||
pub limits: Limits,
|
||||
pub services: Services,
|
||||
pub firewall: Firewall,
|
||||
pub logging: Logging,
|
||||
pub telemetry: Telemetry,
|
||||
pub logs: Logs,
|
||||
pub tls: Tls,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct Server {
|
||||
pub listen: SocketAddr,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct Auth {
|
||||
pub mode: String,
|
||||
pub token_file: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct Limits {
|
||||
pub max_concurrent_requests: usize,
|
||||
pub request_timeout_seconds: u64,
|
||||
pub max_body_bytes: usize,
|
||||
pub read_requests_per_second: f64,
|
||||
pub write_requests_per_second: f64,
|
||||
pub burst: f64,
|
||||
pub command_timeout_seconds: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct Services {
|
||||
pub allowed: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct Firewall {
|
||||
pub management_enabled: bool,
|
||||
pub rollback_timeout_seconds: u64,
|
||||
pub max_snapshots: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct Logging {
|
||||
pub level: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct Telemetry {
|
||||
pub connections_enabled: bool,
|
||||
pub process_info_enabled: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct Logs {
|
||||
pub enabled: bool,
|
||||
pub allowed_units: Vec<String>,
|
||||
pub max_entries_per_request: usize,
|
||||
pub max_message_bytes: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Default)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct Tls {
|
||||
pub enabled: bool,
|
||||
pub certificate_file: String,
|
||||
pub private_key_file: String,
|
||||
}
|
||||
|
||||
impl Default for Server {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
listen: "127.0.0.1:9105"
|
||||
.parse()
|
||||
.expect("valid default listen address"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Auth {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
mode: "token".into(),
|
||||
token_file: "/etc/vps-agent/credentials/control-plane.token".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Limits {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
max_concurrent_requests: 32,
|
||||
request_timeout_seconds: 5,
|
||||
max_body_bytes: 65_536,
|
||||
read_requests_per_second: 10.0,
|
||||
write_requests_per_second: 2.0,
|
||||
burst: 20.0,
|
||||
command_timeout_seconds: 4,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Firewall {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
management_enabled: false,
|
||||
rollback_timeout_seconds: 60,
|
||||
max_snapshots: 5,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Logging {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
level: "info".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Telemetry {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
connections_enabled: true,
|
||||
process_info_enabled: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Logs {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
enabled: false,
|
||||
allowed_units: vec!["vps-agent.service".into()],
|
||||
max_entries_per_request: 200,
|
||||
max_message_bytes: 16_384,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load(path: &Path) -> Result<Self> {
|
||||
let text = std::fs::read_to_string(path)
|
||||
.with_context(|| format!("read configuration {}", path.display()))?;
|
||||
let config: Self = toml::from_str(&text).context("parse TOML configuration")?;
|
||||
config.validate()?;
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
pub fn validate(&self) -> Result<()> {
|
||||
if self.auth.mode != "token" {
|
||||
bail!("auth.mode must be 'token' in v1")
|
||||
}
|
||||
if self.server.listen.ip().is_unspecified() {
|
||||
bail!("refusing unspecified/public listen address; bind a WireGuard IP explicitly")
|
||||
}
|
||||
if self.limits.max_concurrent_requests == 0 || self.limits.max_concurrent_requests > 256 {
|
||||
bail!("max_concurrent_requests must be between 1 and 256")
|
||||
}
|
||||
if !(1..=64).contains(&self.limits.request_timeout_seconds) {
|
||||
bail!("request_timeout_seconds must be between 1 and 64")
|
||||
}
|
||||
if !(1024..=1_048_576).contains(&self.limits.max_body_bytes) {
|
||||
bail!("max_body_bytes must be between 1024 and 1048576")
|
||||
}
|
||||
if self.limits.read_requests_per_second <= 0.0
|
||||
|| self.limits.write_requests_per_second <= 0.0
|
||||
|| self.limits.burst < 1.0
|
||||
{
|
||||
bail!("rate limits must be positive")
|
||||
}
|
||||
if self.services.allowed.len() > 128 {
|
||||
bail!("at most 128 services may be allow-listed")
|
||||
}
|
||||
if self.logs.allowed_units.len() > 128
|
||||
|| !(1..=1000).contains(&self.logs.max_entries_per_request)
|
||||
|| !(1024..=65_536).contains(&self.logs.max_message_bytes)
|
||||
{
|
||||
bail!("logs limits or allowed unit count are invalid")
|
||||
}
|
||||
if self.logs.enabled && self.logs.allowed_units.is_empty() {
|
||||
bail!("logs.allowed_units must not be empty when log collection is enabled")
|
||||
}
|
||||
for name in &self.logs.allowed_units {
|
||||
if !valid_service_name(name) {
|
||||
bail!("invalid allow-listed log unit name")
|
||||
}
|
||||
}
|
||||
for name in &self.services.allowed {
|
||||
if !valid_service_name(name) {
|
||||
bail!("invalid allow-listed service name")
|
||||
}
|
||||
}
|
||||
if self.firewall.management_enabled {
|
||||
bail!("firewall management requires the v2 privileged helper and is unavailable")
|
||||
}
|
||||
if !matches!(
|
||||
self.logging.level.as_str(),
|
||||
"error" | "warn" | "info" | "debug" | "trace"
|
||||
) {
|
||||
bail!("logging.level is invalid")
|
||||
}
|
||||
if self.tls.enabled
|
||||
&& (self.tls.certificate_file.is_empty() || self.tls.private_key_file.is_empty())
|
||||
{
|
||||
bail!("TLS certificate and private key paths are required")
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn valid_service_name(name: &str) -> bool {
|
||||
!name.is_empty()
|
||||
&& name.len() <= 128
|
||||
&& name
|
||||
.bytes()
|
||||
.all(|c| c.is_ascii_alphanumeric() || matches!(c, b'-' | b'_' | b'.' | b'@' | b':'))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn safe_defaults_bind_loopback() {
|
||||
let config = Config::default();
|
||||
assert!(config.server.listen.ip().is_loopback());
|
||||
assert!(!config.firewall.management_enabled);
|
||||
config.validate().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_unspecified_bind() {
|
||||
let mut config = Config::default();
|
||||
config.server.listen = "0.0.0.0:9105".parse().unwrap();
|
||||
assert!(config.validate().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validates_service_names() {
|
||||
assert!(valid_service_name("wg-quick@wg0.service"));
|
||||
assert!(!valid_service_name("nginx;reboot"));
|
||||
assert!(!valid_service_name("../../etc/passwd"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user