feat: complete Epic 1 production foundation
This commit is contained in:
@@ -1,7 +1,3 @@
|
||||
use std::{collections::BTreeMap, fmt, net::SocketAddr, path::PathBuf};
|
||||
|
||||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
ConfigError, ConfigSource, Diagnostic, DiagnosticCode, ProcessScope, SecretString,
|
||||
deployment_field_registry, field_registry,
|
||||
@@ -9,14 +5,20 @@ use crate::{
|
||||
schema::semantic_path_for,
|
||||
validation::{valid_database_host, valid_database_identifier, valid_percent_encoding},
|
||||
};
|
||||
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
fmt,
|
||||
net::{IpAddr, SocketAddr},
|
||||
path::PathBuf,
|
||||
};
|
||||
use url::Url;
|
||||
mod list_parsers;
|
||||
const MAX_ENV_VALUE_BYTES: usize = 8_192;
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum ProcessKind {
|
||||
AdminApi,
|
||||
McpServer,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct DeprecationRecord {
|
||||
pub field: &'static str,
|
||||
@@ -24,7 +26,6 @@ pub struct DeprecationRecord {
|
||||
pub replacement: &'static str,
|
||||
pub removal_window: &'static str,
|
||||
}
|
||||
|
||||
impl ProcessKind {
|
||||
pub const fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
@@ -49,7 +50,6 @@ pub struct PoolSettings {
|
||||
pub idle_timeout_ms: u64,
|
||||
pub max_lifetime_ms: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DatabaseSettings {
|
||||
pub url: Option<SecretString>,
|
||||
@@ -60,27 +60,24 @@ pub struct DatabaseSettings {
|
||||
pub password: SecretString,
|
||||
pub pool: PoolSettings,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum CacheBackend {
|
||||
Memory,
|
||||
Valkey,
|
||||
Redis,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CacheSettings {
|
||||
pub backend: CacheBackend,
|
||||
pub url: Option<SecretString>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct OutboundSettings {
|
||||
pub allowed_hosts: Vec<String>,
|
||||
pub denied_hosts: Vec<String>,
|
||||
pub max_request_bytes: usize,
|
||||
pub max_response_bytes: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RuntimeSettings {
|
||||
pub master_key: SecretString,
|
||||
@@ -90,20 +87,17 @@ pub struct RuntimeSettings {
|
||||
pub cache: CacheSettings,
|
||||
pub outbound: OutboundSettings,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RateLimitSettings {
|
||||
pub requests_per_second: u32,
|
||||
pub burst: u32,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MetricsSettings {
|
||||
pub enabled: bool,
|
||||
pub bind_addr: SocketAddr,
|
||||
pub bearer_token: Option<SecretString>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct OtlpSettings {
|
||||
pub endpoint: Option<String>,
|
||||
@@ -141,9 +135,9 @@ pub struct AdminProcessConfig {
|
||||
pub session_secret: SecretString,
|
||||
pub password_pepper: SecretString,
|
||||
pub session_ttl_hours: i64,
|
||||
pub trust_forwarded_headers: bool,
|
||||
pub trusted_proxy_ips: Vec<IpAddr>,
|
||||
pub bootstrap_email: String,
|
||||
pub bootstrap_password: SecretString,
|
||||
pub bootstrap_password: Option<SecretString>,
|
||||
pub bootstrap_display_name: String,
|
||||
pub demo_seed: bool,
|
||||
}
|
||||
@@ -313,7 +307,15 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
fn secret(&mut self, name: &'static str, default: Option<&str>) -> SecretString {
|
||||
SecretString::new(self.string(name, default))
|
||||
let value = self.string(name, default);
|
||||
if let Some(spec) = field_registry().iter().find(|field| field.env_name == name)
|
||||
&& let Some(minimum) = spec.minimum
|
||||
&& !value.is_empty()
|
||||
&& value.len() < minimum as usize
|
||||
{
|
||||
self.push(DiagnosticCode::OutOfRange, spec.semantic_path);
|
||||
}
|
||||
SecretString::new(value)
|
||||
}
|
||||
|
||||
fn optional_secret(&mut self, name: &'static str) -> Option<SecretString> {
|
||||
@@ -501,41 +503,6 @@ impl<'a> Parser<'a> {
|
||||
Some(SecretString::new(raw))
|
||||
}
|
||||
}
|
||||
|
||||
fn host_list(&mut self, name: &'static str) -> Vec<String> {
|
||||
let Some(raw) = self.optional(name) else {
|
||||
return Vec::new();
|
||||
};
|
||||
if raw.len() > 16_384 {
|
||||
self.push(DiagnosticCode::OutOfRange, name);
|
||||
return Vec::new();
|
||||
}
|
||||
let mut items = Vec::new();
|
||||
for item in raw.split(',') {
|
||||
let item = item.trim().to_ascii_lowercase();
|
||||
let base = item.strip_prefix("*.").unwrap_or(&item);
|
||||
let valid_ip = !item.starts_with("*.") && base.parse::<std::net::IpAddr>().is_ok();
|
||||
if item.is_empty()
|
||||
|| item.len() > 253
|
||||
|| item.contains('/')
|
||||
|| item.contains(char::is_whitespace)
|
||||
|| base.is_empty()
|
||||
|| (!valid_ip && base.contains(':'))
|
||||
|| (item.starts_with("*.") && base.parse::<std::net::IpAddr>().is_ok())
|
||||
{
|
||||
self.push(DiagnosticCode::InvalidType, name);
|
||||
continue;
|
||||
}
|
||||
if !items.contains(&item) {
|
||||
items.push(item);
|
||||
}
|
||||
}
|
||||
if items.len() > 256 {
|
||||
self.push(DiagnosticCode::OutOfRange, name);
|
||||
items.truncate(256);
|
||||
}
|
||||
items
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_database(parser: &mut Parser<'_>) -> DatabaseSettings {
|
||||
@@ -634,7 +601,6 @@ pub(crate) fn parse_database_source(
|
||||
}
|
||||
Ok((database, parser.deprecations))
|
||||
}
|
||||
|
||||
pub fn parse_process(
|
||||
kind: ProcessKind,
|
||||
source: ConfigSource,
|
||||
@@ -642,7 +608,6 @@ pub fn parse_process(
|
||||
let values = source.values();
|
||||
let mut parser = Parser::new(kind, values);
|
||||
parser.check_source();
|
||||
|
||||
let database = parse_database(&mut parser);
|
||||
|
||||
let cache_backend = match parser
|
||||
@@ -709,6 +674,7 @@ pub fn parse_process(
|
||||
outbound: OutboundSettings {
|
||||
allowed_hosts: parser.host_list("CRANK_OUTBOUND_ALLOWED_HOSTS"),
|
||||
denied_hosts: parser.host_list("CRANK_OUTBOUND_DENIED_HOSTS"),
|
||||
max_request_bytes: parser.number("CRANK_OUTBOUND_MAX_REQUEST_BYTES") as usize,
|
||||
max_response_bytes: parser.number("CRANK_OUTBOUND_MAX_RESPONSE_BYTES") as usize,
|
||||
},
|
||||
};
|
||||
@@ -811,14 +777,24 @@ pub fn parse_process(
|
||||
ProcessKind::AdminApi => {
|
||||
let rps = parser.number("CRANK_ADMIN_RATE_LIMIT_RPS") as u32;
|
||||
let burst = parser.number("CRANK_ADMIN_RATE_LIMIT_BURST") as u32;
|
||||
let bind_addr = parser.socket("CRANK_ADMIN_BIND");
|
||||
if burst < rps {
|
||||
parser.push(DiagnosticCode::UnsafeCombination, "admin.rate_limit.burst");
|
||||
}
|
||||
if observability.environment == "production"
|
||||
&& !bind_addr.ip().is_loopback()
|
||||
&& !runtime
|
||||
.base_url
|
||||
.as_deref()
|
||||
.is_some_and(|url| url.starts_with("https://"))
|
||||
{
|
||||
parser.push(DiagnosticCode::UnsafeCombination, "admin.exposure.tls");
|
||||
}
|
||||
Projection::Admin(AdminProcessConfig {
|
||||
database,
|
||||
runtime,
|
||||
observability,
|
||||
bind_addr: parser.socket("CRANK_ADMIN_BIND"),
|
||||
bind_addr,
|
||||
storage_root: parser.absolute_path("CRANK_STORAGE_ROOT", "/var/lib/crank/storage"),
|
||||
rate_limit: RateLimitSettings {
|
||||
requests_per_second: rps,
|
||||
@@ -829,9 +805,9 @@ pub fn parse_process(
|
||||
session_secret: parser.secret("CRANK_SESSION_SECRET", None),
|
||||
password_pepper: parser.secret("CRANK_PASSWORD_PEPPER", None),
|
||||
session_ttl_hours: parser.number("CRANK_SESSION_TTL_HOURS") as i64,
|
||||
trust_forwarded_headers: parser.boolean("CRANK_TRUST_FORWARDED_HEADERS"),
|
||||
trusted_proxy_ips: parser.ip_list("CRANK_TRUSTED_PROXY_IPS"),
|
||||
bootstrap_email: parser.string("CRANK_BOOTSTRAP_ADMIN_EMAIL", None),
|
||||
bootstrap_password: parser.secret("CRANK_BOOTSTRAP_ADMIN_PASSWORD", None),
|
||||
bootstrap_password: parser.optional_secret("CRANK_BOOTSTRAP_ADMIN_PASSWORD"),
|
||||
bootstrap_display_name: parser
|
||||
.string("CRANK_BOOTSTRAP_ADMIN_DISPLAY_NAME", Some("Crank Owner")),
|
||||
demo_seed: parser.boolean("CRANK_DEMO_SEED"),
|
||||
@@ -889,14 +865,17 @@ fn fingerprint_parts(kind: ProcessKind, projection: &Projection) -> Vec<String>
|
||||
),
|
||||
format!("retention={}", config.invocation_log_retention_days),
|
||||
format!("session_ttl={}", config.session_ttl_hours),
|
||||
format!("trusted={}", config.trust_forwarded_headers),
|
||||
format!("trusted_proxies={:?}", config.trusted_proxy_ips),
|
||||
format!("demo={}", config.demo_seed),
|
||||
"storage=path-configured".to_owned(),
|
||||
format!("session_secret={}", config.session_secret.is_configured()),
|
||||
format!("pepper={}", config.password_pepper.is_configured()),
|
||||
format!(
|
||||
"bootstrap_password={}",
|
||||
config.bootstrap_password.is_configured()
|
||||
config
|
||||
.bootstrap_password
|
||||
.as_ref()
|
||||
.is_some_and(SecretString::is_configured)
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -970,6 +949,7 @@ fn fingerprint_parts(kind: ProcessKind, projection: &Projection) -> Vec<String>
|
||||
),
|
||||
format!("allowed={}", allowed.join(",")),
|
||||
format!("denied={}", denied.join(",")),
|
||||
format!("max_request={}", runtime.outbound.max_request_bytes),
|
||||
format!("max_response={}", runtime.outbound.max_response_bytes),
|
||||
format!("environment={}", observability.environment),
|
||||
format!("log_filter={}", observability.log_filter),
|
||||
|
||||
Reference in New Issue
Block a user