feat: harden community production foundation through story 1.5
This commit is contained in:
1 parent
c30461cc92
commit
f6fc2e5c9b
161 files changed
+16758
-2515
No files matched your search
@@ -0,0 +1,73 @@
|
||||
use std::{env, fs, path::Path};
|
||||
|
||||
use crank_config::render::{
|
||||
BEGIN_MARKER, DOC_BEGIN_MARKER, DOC_END_MARKER, END_MARKER, env_section, reference_section,
|
||||
replace_marked, schema_json,
|
||||
};
|
||||
|
||||
fn main() {
|
||||
if let Err(error) = run() {
|
||||
eprintln!("config contract check failed: {error}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn run() -> Result<(), String> {
|
||||
let mode = env::args().nth(1).unwrap_or_else(|| "--check".to_owned());
|
||||
if !matches!(mode.as_str(), "--check" | "--write") {
|
||||
return Err("expected --check or --write".to_owned());
|
||||
}
|
||||
let write = mode == "--write";
|
||||
sync_file(
|
||||
Path::new("docs/schemas/runtime-config.schema.json"),
|
||||
schema_json(),
|
||||
write,
|
||||
)?;
|
||||
for (path, production) in [
|
||||
(".env.example", false),
|
||||
("deploy/community/.env.example", true),
|
||||
("deploy/community/.env.images.example", true),
|
||||
] {
|
||||
sync_marked(
|
||||
Path::new(path),
|
||||
BEGIN_MARKER,
|
||||
END_MARKER,
|
||||
&env_section(production),
|
||||
write,
|
||||
)?;
|
||||
}
|
||||
sync_marked(
|
||||
Path::new("docs/runtime-config.md"),
|
||||
DOC_BEGIN_MARKER,
|
||||
DOC_END_MARKER,
|
||||
&reference_section(),
|
||||
write,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn sync_marked(
|
||||
path: &Path,
|
||||
begin: &str,
|
||||
end: &str,
|
||||
replacement: &str,
|
||||
write: bool,
|
||||
) -> Result<(), String> {
|
||||
let current =
|
||||
fs::read_to_string(path).map_err(|_| format!("cannot read {}", path.display()))?;
|
||||
let expected = replace_marked(¤t, begin, end, replacement)
|
||||
.ok_or_else(|| format!("missing generated markers in {}", path.display()))?;
|
||||
sync_file(path, expected, write)
|
||||
}
|
||||
|
||||
fn sync_file(path: &Path, expected: String, write: bool) -> Result<(), String> {
|
||||
let current = fs::read_to_string(path).unwrap_or_default();
|
||||
if current == expected {
|
||||
return Ok(());
|
||||
}
|
||||
if write {
|
||||
fs::write(path, expected).map_err(|_| format!("cannot write {}", path.display()))
|
||||
} else {
|
||||
Err(format!("{} is out of date", path.display()))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
use std::fmt;
|
||||
|
||||
use crate::{
|
||||
AdminProcessConfig, CacheSettings, DatabaseSettings, McpProcessConfig, MetricsSettings,
|
||||
MigratorConfig, ObservabilitySettings, OtlpSettings, OutboundSettings, RuntimeSettings,
|
||||
};
|
||||
|
||||
impl fmt::Debug for DatabaseSettings {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("DatabaseSettings")
|
||||
.field("url", &self.url.as_ref().map(|_| "configured"))
|
||||
.field("password", &self.password)
|
||||
.field("pool", &self.pool)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
impl fmt::Debug for CacheSettings {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("CacheSettings")
|
||||
.field("backend", &self.backend)
|
||||
.field("url", &self.url.as_ref().map(|_| "configured"))
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
impl fmt::Debug for OutboundSettings {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("OutboundSettings")
|
||||
.field("allowed_host_count", &self.allowed_hosts.len())
|
||||
.field("denied_host_count", &self.denied_hosts.len())
|
||||
.field("max_response_bytes", &self.max_response_bytes)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
impl fmt::Debug for RuntimeSettings {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("RuntimeSettings")
|
||||
.field("master_key", &self.master_key)
|
||||
.field("base_url", &self.base_url.as_ref().map(|_| "configured"))
|
||||
.field("max_concurrent_unary", &self.max_concurrent_unary)
|
||||
.field("max_concurrent_sessions", &self.max_concurrent_sessions)
|
||||
.field("cache", &self.cache)
|
||||
.field("outbound", &self.outbound)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
impl fmt::Debug for MetricsSettings {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("MetricsSettings")
|
||||
.field("enabled", &self.enabled)
|
||||
.field("loopback", &self.bind_addr.ip().is_loopback())
|
||||
.field(
|
||||
"bearer_token",
|
||||
&self.bearer_token.as_ref().map(|_| "configured"),
|
||||
)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
impl fmt::Debug for OtlpSettings {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("OtlpSettings")
|
||||
.field("endpoint", &self.endpoint.as_ref().map(|_| "configured"))
|
||||
.field(
|
||||
"traces_endpoint",
|
||||
&self.traces_endpoint.as_ref().map(|_| "configured"),
|
||||
)
|
||||
.field("headers", &self.headers.as_ref().map(|_| "configured"))
|
||||
.field(
|
||||
"traces_headers",
|
||||
&self.traces_headers.as_ref().map(|_| "configured"),
|
||||
)
|
||||
.field("max_queue_size", &self.max_queue_size)
|
||||
.field("max_export_batch_size", &self.max_export_batch_size)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
impl fmt::Debug for ObservabilitySettings {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("ObservabilitySettings")
|
||||
.field("environment", &"configured")
|
||||
.field("log_filter", &"configured")
|
||||
.field(
|
||||
"sentry_dsn",
|
||||
&self.sentry_dsn.as_ref().map(|_| "configured"),
|
||||
)
|
||||
.field("metrics", &self.metrics)
|
||||
.field("otlp", &self.otlp)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
impl fmt::Debug for AdminProcessConfig {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("AdminProcessConfig")
|
||||
.field("database", &self.database)
|
||||
.field("runtime", &self.runtime)
|
||||
.field("observability", &self.observability)
|
||||
.field("storage_root", &"configured")
|
||||
.field("session_secret", &self.session_secret)
|
||||
.field("password_pepper", &self.password_pepper)
|
||||
.field("bootstrap_password", &self.bootstrap_password)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
impl fmt::Debug for McpProcessConfig {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("McpProcessConfig")
|
||||
.field("database", &self.database)
|
||||
.field("runtime", &self.runtime)
|
||||
.field("observability", &self.observability)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for MigratorConfig {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("MigratorConfig")
|
||||
.field("database", &self.database)
|
||||
.field("fingerprint", &self.fingerprint())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
use std::fmt;
|
||||
|
||||
use serde::{Serialize, Serializer};
|
||||
|
||||
const MAX_DIAGNOSTICS: usize = 100;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
|
||||
pub enum DiagnosticCode {
|
||||
MissingRequired,
|
||||
InvalidEncoding,
|
||||
InvalidType,
|
||||
OutOfRange,
|
||||
UnknownField,
|
||||
Conflict,
|
||||
UnsafeCombination,
|
||||
DeprecatedNoEffect,
|
||||
}
|
||||
|
||||
impl Serialize for DiagnosticCode {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl DiagnosticCode {
|
||||
pub const fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::MissingRequired => "config.missing_required",
|
||||
Self::InvalidEncoding => "config.invalid_encoding",
|
||||
Self::InvalidType => "config.invalid_type",
|
||||
Self::OutOfRange => "config.out_of_range",
|
||||
Self::UnknownField => "config.unknown_field",
|
||||
Self::Conflict => "config.conflict",
|
||||
Self::UnsafeCombination => "config.unsafe_combination",
|
||||
Self::DeprecatedNoEffect => "config.deprecated_no_effect",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize)]
|
||||
pub struct Diagnostic {
|
||||
pub code: DiagnosticCode,
|
||||
pub field: String,
|
||||
pub message_ru: &'static str,
|
||||
pub message_en: &'static str,
|
||||
}
|
||||
|
||||
impl Diagnostic {
|
||||
pub(crate) fn new(code: DiagnosticCode, field: impl Into<String>) -> Self {
|
||||
let (message_ru, message_en) = match code {
|
||||
DiagnosticCode::MissingRequired => (
|
||||
"Обязательный параметр не настроен.",
|
||||
"A required configuration field is not configured.",
|
||||
),
|
||||
DiagnosticCode::InvalidEncoding => (
|
||||
"Параметр должен быть корректной строкой UTF-8.",
|
||||
"The configuration field must be valid UTF-8.",
|
||||
),
|
||||
DiagnosticCode::InvalidType => (
|
||||
"Параметр имеет недопустимый тип или формат.",
|
||||
"The configuration field has an invalid type or format.",
|
||||
),
|
||||
DiagnosticCode::OutOfRange => (
|
||||
"Параметр находится вне допустимых границ.",
|
||||
"The configuration field is outside its allowed bounds.",
|
||||
),
|
||||
DiagnosticCode::UnknownField => (
|
||||
"Неизвестный параметр в управляемом пространстве имён.",
|
||||
"Unknown field in an owned configuration namespace.",
|
||||
),
|
||||
DiagnosticCode::Conflict => (
|
||||
"Одновременно заданы конфликтующие источники конфигурации.",
|
||||
"Conflicting configuration sources are set at the same time.",
|
||||
),
|
||||
DiagnosticCode::UnsafeCombination => (
|
||||
"Комбинация параметров небезопасна или противоречива.",
|
||||
"The configuration combination is unsafe or inconsistent.",
|
||||
),
|
||||
DiagnosticCode::DeprecatedNoEffect => (
|
||||
"Устаревший параметр не имеет поддерживаемого эффекта.",
|
||||
"The deprecated field has no supported effect.",
|
||||
),
|
||||
};
|
||||
let mut field = field.into();
|
||||
if field.len() > 256 {
|
||||
let mut boundary = 256;
|
||||
while !field.is_char_boundary(boundary) {
|
||||
boundary -= 1;
|
||||
}
|
||||
field.truncate(boundary);
|
||||
}
|
||||
Self {
|
||||
code,
|
||||
field,
|
||||
message_ru,
|
||||
message_en,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct ConfigError {
|
||||
diagnostics: Vec<Diagnostic>,
|
||||
omitted: usize,
|
||||
}
|
||||
|
||||
impl ConfigError {
|
||||
pub fn single(code: DiagnosticCode, field: impl Into<String>) -> Self {
|
||||
Self::from_diagnostics(vec![Diagnostic::new(code, field)])
|
||||
}
|
||||
|
||||
pub(crate) fn from_diagnostics(mut diagnostics: Vec<Diagnostic>) -> Self {
|
||||
diagnostics.sort();
|
||||
diagnostics.dedup();
|
||||
let mut omitted = diagnostics.len().saturating_sub(MAX_DIAGNOSTICS);
|
||||
diagnostics.truncate(MAX_DIAGNOSTICS);
|
||||
while serialized_len(&diagnostics, omitted) > 65_536 && !diagnostics.is_empty() {
|
||||
diagnostics.pop();
|
||||
omitted += 1;
|
||||
}
|
||||
Self {
|
||||
diagnostics,
|
||||
omitted,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn diagnostics(&self) -> &[Diagnostic] {
|
||||
&self.diagnostics
|
||||
}
|
||||
|
||||
pub fn omitted(&self) -> usize {
|
||||
self.omitted
|
||||
}
|
||||
|
||||
pub fn to_json(&self) -> String {
|
||||
#[derive(Serialize)]
|
||||
struct Report<'a> {
|
||||
diagnostics: &'a [Diagnostic],
|
||||
omitted: usize,
|
||||
}
|
||||
serde_json::to_string(&Report {
|
||||
diagnostics: &self.diagnostics,
|
||||
omitted: self.omitted,
|
||||
})
|
||||
.unwrap_or_else(|_| "{\"diagnostics\":[],\"omitted\":0}".to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
fn serialized_len(diagnostics: &[Diagnostic], omitted: usize) -> usize {
|
||||
#[derive(Serialize)]
|
||||
struct Report<'a> {
|
||||
diagnostics: &'a [Diagnostic],
|
||||
omitted: usize,
|
||||
}
|
||||
serde_json::to_vec(&Report {
|
||||
diagnostics,
|
||||
omitted,
|
||||
})
|
||||
.map_or(usize::MAX, |value| value.len())
|
||||
}
|
||||
|
||||
impl fmt::Display for ConfigError {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
for (index, diagnostic) in self.diagnostics.iter().enumerate() {
|
||||
if index > 0 {
|
||||
formatter.write_str("; ")?;
|
||||
}
|
||||
write!(
|
||||
formatter,
|
||||
"{} field={} ru={} en={}",
|
||||
diagnostic.code.as_str(),
|
||||
diagnostic.field,
|
||||
diagnostic.message_ru,
|
||||
diagnostic.message_en
|
||||
)?;
|
||||
}
|
||||
if self.omitted > 0 {
|
||||
write!(formatter, "; diagnostics_omitted={}", self.omitted)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for ConfigError {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn unicode_fields_and_worst_case_json_remain_bounded() {
|
||||
let field = format!("{}{}", "\\\"".repeat(120), "💣".repeat(100));
|
||||
let diagnostics = (0..200)
|
||||
.map(|index| Diagnostic::new(DiagnosticCode::InvalidType, format!("{index}:{field}")))
|
||||
.collect();
|
||||
let error = ConfigError::from_diagnostics(diagnostics);
|
||||
let json = error.to_json();
|
||||
assert!(json.len() <= 65_536);
|
||||
assert!(error.omitted() > 0);
|
||||
assert!(serde_json::from_str::<serde_json::Value>(&json).is_ok());
|
||||
assert!(json.contains("config.invalid_type"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
pub(crate) fn sha256_hex(parts: impl IntoIterator<Item = String>) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(b"crank-config-fingerprint-v1\0");
|
||||
for part in parts {
|
||||
hasher.update(part.len().to_le_bytes());
|
||||
hasher.update(part.as_bytes());
|
||||
}
|
||||
format!("{:x}", hasher.finalize())
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
//! Typed bootstrap configuration contract for Crank processes.
|
||||
|
||||
mod debug;
|
||||
mod diagnostic;
|
||||
mod fingerprint;
|
||||
mod migrator;
|
||||
mod process;
|
||||
pub mod render;
|
||||
mod schema;
|
||||
mod source;
|
||||
mod validation;
|
||||
mod value;
|
||||
|
||||
pub use diagnostic::{ConfigError, Diagnostic, DiagnosticCode};
|
||||
pub use migrator::{MigratorConfig, parse_migrator};
|
||||
pub use process::{
|
||||
AdminProcessConfig, CacheBackend, CacheSettings, DatabaseSettings, DeprecationRecord,
|
||||
EffectiveConfig, McpProcessConfig, MetricsSettings, ObservabilitySettings, OtlpSettings,
|
||||
OutboundSettings, PoolSettings, ProcessKind, RateLimitSettings, RuntimeSettings, parse_process,
|
||||
};
|
||||
pub use schema::{
|
||||
FieldMode, FieldSpec, ProcessScope, Sensitivity, deployment_field_registry, field_registry,
|
||||
};
|
||||
pub use source::ConfigSource;
|
||||
pub use value::SecretString;
|
||||
@@ -0,0 +1,47 @@
|
||||
use crate::{
|
||||
ConfigError, ConfigSource, DatabaseSettings, DeprecationRecord, fingerprint::sha256_hex,
|
||||
process::parse_database_source,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MigratorConfig {
|
||||
pub database: DatabaseSettings,
|
||||
fingerprint: String,
|
||||
deprecations: Vec<DeprecationRecord>,
|
||||
}
|
||||
|
||||
impl MigratorConfig {
|
||||
pub fn fingerprint(&self) -> &str {
|
||||
&self.fingerprint
|
||||
}
|
||||
|
||||
pub fn deprecations(&self) -> &[DeprecationRecord] {
|
||||
&self.deprecations
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_migrator(source: ConfigSource) -> Result<MigratorConfig, ConfigError> {
|
||||
let (database, deprecations) = parse_database_source(source.retain_for_migrator())?;
|
||||
let fingerprint = sha256_hex([
|
||||
"schema=crank-config-migrator-v1".to_owned(),
|
||||
format!("url={}", database.url.is_some()),
|
||||
format!("host={}", database.host.to_ascii_lowercase()),
|
||||
format!("port={}", database.port),
|
||||
format!("database={}", database.database),
|
||||
format!("username={}", database.username),
|
||||
format!("password={}", database.password.is_configured()),
|
||||
format!(
|
||||
"pool={}:{}:{}:{}:{}",
|
||||
database.pool.max_connections,
|
||||
database.pool.min_connections,
|
||||
database.pool.acquire_timeout_ms,
|
||||
database.pool.idle_timeout_ms,
|
||||
database.pool.max_lifetime_ms
|
||||
),
|
||||
]);
|
||||
Ok(MigratorConfig {
|
||||
database,
|
||||
fingerprint,
|
||||
deprecations,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,1000 @@
|
||||
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,
|
||||
fingerprint::sha256_hex,
|
||||
schema::semantic_path_for,
|
||||
validation::{valid_database_host, valid_database_identifier, valid_percent_encoding},
|
||||
};
|
||||
|
||||
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,
|
||||
pub source_class: &'static str,
|
||||
pub replacement: &'static str,
|
||||
pub removal_window: &'static str,
|
||||
}
|
||||
|
||||
impl ProcessKind {
|
||||
pub const fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::AdminApi => "admin-api",
|
||||
Self::McpServer => "mcp-server",
|
||||
}
|
||||
}
|
||||
|
||||
const fn scope(self) -> ProcessScope {
|
||||
match self {
|
||||
Self::AdminApi => ProcessScope::AdminApi,
|
||||
Self::McpServer => ProcessScope::McpServer,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PoolSettings {
|
||||
pub max_connections: u32,
|
||||
pub min_connections: u32,
|
||||
pub acquire_timeout_ms: u64,
|
||||
pub idle_timeout_ms: u64,
|
||||
pub max_lifetime_ms: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DatabaseSettings {
|
||||
pub url: Option<SecretString>,
|
||||
pub host: String,
|
||||
pub port: u16,
|
||||
pub database: String,
|
||||
pub username: String,
|
||||
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_response_bytes: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RuntimeSettings {
|
||||
pub master_key: SecretString,
|
||||
pub base_url: Option<String>,
|
||||
pub max_concurrent_unary: usize,
|
||||
pub max_concurrent_sessions: usize,
|
||||
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>,
|
||||
pub traces_endpoint: Option<String>,
|
||||
pub protocol: Option<String>,
|
||||
pub traces_protocol: Option<String>,
|
||||
pub timeout: Option<String>,
|
||||
pub traces_timeout: Option<String>,
|
||||
pub headers: Option<SecretString>,
|
||||
pub traces_headers: Option<SecretString>,
|
||||
pub max_queue_size: usize,
|
||||
pub max_export_batch_size: usize,
|
||||
pub schedule_delay: String,
|
||||
pub export_timeout: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ObservabilitySettings {
|
||||
pub environment: String,
|
||||
pub log_filter: String,
|
||||
pub sentry_dsn: Option<SecretString>,
|
||||
pub metrics: MetricsSettings,
|
||||
pub otlp: OtlpSettings,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AdminProcessConfig {
|
||||
pub database: DatabaseSettings,
|
||||
pub runtime: RuntimeSettings,
|
||||
pub observability: ObservabilitySettings,
|
||||
pub bind_addr: SocketAddr,
|
||||
pub storage_root: PathBuf,
|
||||
pub rate_limit: RateLimitSettings,
|
||||
pub invocation_log_retention_days: i64,
|
||||
pub session_secret: SecretString,
|
||||
pub password_pepper: SecretString,
|
||||
pub session_ttl_hours: i64,
|
||||
pub trust_forwarded_headers: bool,
|
||||
pub bootstrap_email: String,
|
||||
pub bootstrap_password: SecretString,
|
||||
pub bootstrap_display_name: String,
|
||||
pub demo_seed: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct McpProcessConfig {
|
||||
pub database: DatabaseSettings,
|
||||
pub runtime: RuntimeSettings,
|
||||
pub observability: ObservabilitySettings,
|
||||
pub bind_addr: SocketAddr,
|
||||
pub refresh_ms: u64,
|
||||
pub rate_limit: RateLimitSettings,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum Projection {
|
||||
Admin(AdminProcessConfig),
|
||||
Mcp(McpProcessConfig),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct EffectiveConfig {
|
||||
kind: ProcessKind,
|
||||
fingerprint: String,
|
||||
projection: Projection,
|
||||
deprecations: Vec<DeprecationRecord>,
|
||||
}
|
||||
|
||||
impl EffectiveConfig {
|
||||
pub fn admin(&self) -> Option<&AdminProcessConfig> {
|
||||
match &self.projection {
|
||||
Projection::Admin(config) => Some(config),
|
||||
Projection::Mcp(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mcp(&self) -> Option<&McpProcessConfig> {
|
||||
match &self.projection {
|
||||
Projection::Mcp(config) => Some(config),
|
||||
Projection::Admin(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fingerprint(&self) -> &str {
|
||||
&self.fingerprint
|
||||
}
|
||||
|
||||
pub fn deprecations(&self) -> &[DeprecationRecord] {
|
||||
&self.deprecations
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for EffectiveConfig {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter
|
||||
.debug_struct("EffectiveConfig")
|
||||
.field("process", &self.kind.as_str())
|
||||
.field("fingerprint", &self.fingerprint)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
struct Parser<'a> {
|
||||
kind: ProcessKind,
|
||||
values: &'a BTreeMap<String, String>,
|
||||
diagnostics: Vec<Diagnostic>,
|
||||
deprecations: Vec<DeprecationRecord>,
|
||||
}
|
||||
|
||||
impl<'a> Parser<'a> {
|
||||
fn new(kind: ProcessKind, values: &'a BTreeMap<String, String>) -> Self {
|
||||
Self {
|
||||
kind,
|
||||
values,
|
||||
diagnostics: Vec::new(),
|
||||
deprecations: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn check_source(&mut self) {
|
||||
let known = field_registry()
|
||||
.iter()
|
||||
.map(|field| field.env_name)
|
||||
.collect::<std::collections::BTreeSet<_>>();
|
||||
let deployment_only = deployment_field_registry()
|
||||
.iter()
|
||||
.copied()
|
||||
.collect::<std::collections::BTreeSet<_>>();
|
||||
for (name, value) in self.values {
|
||||
let owned = name.starts_with("CRANK_")
|
||||
|| name.starts_with("POSTGRES_")
|
||||
|| name.starts_with("OTEL_");
|
||||
if !owned && !known.contains(name.as_str()) {
|
||||
continue;
|
||||
}
|
||||
if deployment_only.contains(name.as_str()) {
|
||||
continue;
|
||||
}
|
||||
let spec = field_registry().iter().find(|field| field.env_name == name);
|
||||
if owned && spec.is_none() {
|
||||
self.push(DiagnosticCode::UnknownField, "environment.unknown");
|
||||
continue;
|
||||
}
|
||||
if let Some(spec) = spec {
|
||||
let applicable = match spec.process {
|
||||
ProcessScope::Shared => true,
|
||||
ProcessScope::AdminApi => self.kind == ProcessKind::AdminApi,
|
||||
ProcessScope::McpServer => self.kind == ProcessKind::McpServer,
|
||||
};
|
||||
if !applicable {
|
||||
self.push(DiagnosticCode::UnknownField, spec.semantic_path);
|
||||
continue;
|
||||
}
|
||||
if value.len() > MAX_ENV_VALUE_BYTES || value.chars().any(char::is_control) {
|
||||
self.push(DiagnosticCode::OutOfRange, spec.semantic_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
if self
|
||||
.values
|
||||
.get("CRANK_CACHE_DEFAULT_TTL_MS")
|
||||
.is_some_and(|value| !value.is_empty())
|
||||
{
|
||||
self.push(
|
||||
DiagnosticCode::DeprecatedNoEffect,
|
||||
"CRANK_CACHE_DEFAULT_TTL_MS",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn push(&mut self, code: DiagnosticCode, field: impl Into<String>) {
|
||||
let field = field.into();
|
||||
let semantic = field_registry()
|
||||
.iter()
|
||||
.find(|spec| spec.env_name == field)
|
||||
.map_or(field.as_str(), |spec| spec.semantic_path);
|
||||
self.diagnostics.push(Diagnostic::new(code, semantic));
|
||||
}
|
||||
|
||||
fn optional(&mut self, name: &'static str) -> Option<String> {
|
||||
self.values.get(name).and_then(|value| {
|
||||
if value.is_empty() {
|
||||
None
|
||||
} else if value.len() > MAX_ENV_VALUE_BYTES || value.chars().any(char::is_control) {
|
||||
self.push(DiagnosticCode::OutOfRange, name);
|
||||
None
|
||||
} else if value.trim().is_empty() {
|
||||
self.push(DiagnosticCode::InvalidType, name);
|
||||
None
|
||||
} else {
|
||||
Some(value.clone())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn string(&mut self, name: &'static str, default: Option<&str>) -> String {
|
||||
match self
|
||||
.optional(name)
|
||||
.or_else(|| default.map(ToOwned::to_owned))
|
||||
{
|
||||
Some(value) => value,
|
||||
None => {
|
||||
self.push(DiagnosticCode::MissingRequired, name);
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn secret(&mut self, name: &'static str, default: Option<&str>) -> SecretString {
|
||||
SecretString::new(self.string(name, default))
|
||||
}
|
||||
|
||||
fn optional_secret(&mut self, name: &'static str) -> Option<SecretString> {
|
||||
self.optional(name).map(SecretString::new)
|
||||
}
|
||||
|
||||
fn number(&mut self, name: &'static str) -> u64 {
|
||||
let spec = field_registry()
|
||||
.iter()
|
||||
.find(|field| field.env_name == name)
|
||||
.expect("registered numeric field");
|
||||
let default = spec
|
||||
.default
|
||||
.and_then(|value| value.parse().ok())
|
||||
.unwrap_or_default();
|
||||
let min = spec.minimum.expect("numeric minimum");
|
||||
let max = spec.maximum.expect("numeric maximum");
|
||||
let Some(raw) = self.values.get(name) else {
|
||||
return default;
|
||||
};
|
||||
if raw.is_empty() || raw.trim() != raw {
|
||||
self.push(DiagnosticCode::InvalidType, name);
|
||||
return default;
|
||||
}
|
||||
match raw.parse::<u64>() {
|
||||
Ok(value) if (min..=max).contains(&value) => value,
|
||||
Ok(_) => {
|
||||
self.push(DiagnosticCode::OutOfRange, name);
|
||||
default
|
||||
}
|
||||
Err(_) => {
|
||||
self.push(DiagnosticCode::InvalidType, name);
|
||||
default
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn boolean(&mut self, name: &'static str) -> bool {
|
||||
let default = field_registry()
|
||||
.iter()
|
||||
.find(|field| field.env_name == name)
|
||||
.and_then(|field| field.default)
|
||||
.and_then(|value| value.parse().ok())
|
||||
.unwrap_or(false);
|
||||
let Some(raw) = self.values.get(name) else {
|
||||
return default;
|
||||
};
|
||||
match raw.to_ascii_lowercase().as_str() {
|
||||
"true" | "1" => true,
|
||||
"false" | "0" => false,
|
||||
"yes" | "on" => {
|
||||
self.deprecations.push(DeprecationRecord {
|
||||
field: semantic_path_for(name),
|
||||
source_class: "canonical_env_compatibility_spelling",
|
||||
replacement: "true",
|
||||
removal_window: "after-0.3",
|
||||
});
|
||||
true
|
||||
}
|
||||
"no" | "off" => {
|
||||
self.deprecations.push(DeprecationRecord {
|
||||
field: semantic_path_for(name),
|
||||
source_class: "canonical_env_compatibility_spelling",
|
||||
replacement: "false",
|
||||
removal_window: "after-0.3",
|
||||
});
|
||||
false
|
||||
}
|
||||
_ => {
|
||||
self.push(DiagnosticCode::InvalidType, name);
|
||||
default
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn socket(&mut self, name: &'static str) -> SocketAddr {
|
||||
let default = field_registry()
|
||||
.iter()
|
||||
.find(|field| field.env_name == name)
|
||||
.and_then(|field| field.default)
|
||||
.expect("registered socket default");
|
||||
let raw = self.string(name, Some(default));
|
||||
match raw.parse::<SocketAddr>() {
|
||||
Ok(value) if value.port() != 0 => value,
|
||||
Ok(_) => {
|
||||
self.push(DiagnosticCode::OutOfRange, name);
|
||||
default.parse().expect("static socket default")
|
||||
}
|
||||
Err(_) => {
|
||||
self.push(DiagnosticCode::InvalidType, name);
|
||||
default.parse().expect("static socket default")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn absolute_path(&mut self, name: &'static str, default: &'static str) -> PathBuf {
|
||||
let raw = self.string(name, Some(default));
|
||||
let path = PathBuf::from(&raw);
|
||||
if raw.len() > 4096 || !path.is_absolute() {
|
||||
self.push(DiagnosticCode::InvalidType, name);
|
||||
return PathBuf::from(default);
|
||||
}
|
||||
path
|
||||
}
|
||||
|
||||
fn url(&mut self, name: &'static str) -> Option<String> {
|
||||
let raw = self.optional(name)?;
|
||||
match Url::parse(&raw) {
|
||||
Ok(url)
|
||||
if matches!(url.scheme(), "http" | "https")
|
||||
&& url.host_str().is_some()
|
||||
&& url.username().is_empty()
|
||||
&& url.password().is_none()
|
||||
&& url.query().is_none()
|
||||
&& url.fragment().is_none() =>
|
||||
{
|
||||
Some(raw)
|
||||
}
|
||||
_ => {
|
||||
self.push(DiagnosticCode::InvalidType, name);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn duration(&mut self, name: &'static str, default: Option<u64>) -> Option<String> {
|
||||
let raw = match self.values.get(name) {
|
||||
Some(raw) if !raw.is_empty() => raw,
|
||||
_ => return default.map(|value| value.to_string()),
|
||||
};
|
||||
if raw.trim() != raw {
|
||||
self.push(DiagnosticCode::InvalidType, name);
|
||||
return default.map(|value| value.to_string());
|
||||
}
|
||||
match raw.parse::<u64>() {
|
||||
Ok(value) if (1..=300_000).contains(&value) => Some(value.to_string()),
|
||||
Ok(_) => {
|
||||
self.push(DiagnosticCode::OutOfRange, name);
|
||||
default.map(|value| value.to_string())
|
||||
}
|
||||
Err(_) => {
|
||||
self.push(DiagnosticCode::InvalidType, name);
|
||||
default.map(|value| value.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn headers(&mut self, name: &'static str) -> Option<SecretString> {
|
||||
let raw = self.optional(name)?;
|
||||
let valid = raw
|
||||
.split(',')
|
||||
.filter(|item| !item.trim().is_empty())
|
||||
.all(|item| {
|
||||
item.split_once('=').is_some_and(|(header, value)| {
|
||||
let header = header.trim();
|
||||
!header.is_empty()
|
||||
&& header.bytes().all(|byte| {
|
||||
byte.is_ascii_alphanumeric()
|
||||
|| matches!(
|
||||
byte,
|
||||
b'!' | b'#'
|
||||
| b'$'
|
||||
| b'%'
|
||||
| b'&'
|
||||
| b'\''
|
||||
| b'*'
|
||||
| b'+'
|
||||
| b'-'
|
||||
| b'.'
|
||||
| b'^'
|
||||
| b'_'
|
||||
| b'`'
|
||||
| b'|'
|
||||
| b'~'
|
||||
)
|
||||
})
|
||||
&& !value.trim().is_empty()
|
||||
&& valid_percent_encoding(value)
|
||||
})
|
||||
});
|
||||
if !valid {
|
||||
self.push(DiagnosticCode::InvalidType, name);
|
||||
None
|
||||
} else {
|
||||
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 {
|
||||
let database_url = parser.optional_secret("CRANK_DATABASE_URL");
|
||||
if database_url.is_some() {
|
||||
parser.deprecations.push(DeprecationRecord {
|
||||
field: semantic_path_for("CRANK_DATABASE_URL"),
|
||||
source_class: "legacy_alias",
|
||||
replacement: "POSTGRES_HOST/PORT/DB/USER/PASSWORD",
|
||||
removal_window: "after-0.3",
|
||||
});
|
||||
}
|
||||
let decomposed_defaults = [
|
||||
("POSTGRES_HOST", "postgres"),
|
||||
("POSTGRES_PORT", "5432"),
|
||||
("POSTGRES_DB", "crank"),
|
||||
("POSTGRES_USER", "crank"),
|
||||
("POSTGRES_PASSWORD", "crank"),
|
||||
];
|
||||
if database_url.is_some()
|
||||
&& decomposed_defaults.iter().any(|(name, default)| {
|
||||
parser
|
||||
.values
|
||||
.get(*name)
|
||||
.is_some_and(|value| !value.is_empty() && value != default)
|
||||
})
|
||||
{
|
||||
parser.push(DiagnosticCode::Conflict, "database.source");
|
||||
}
|
||||
if let Some(url) = database_url.as_ref() {
|
||||
let valid = Url::parse(url.expose_secret()).ok().is_some_and(|url| {
|
||||
matches!(url.scheme(), "postgres" | "postgresql")
|
||||
&& url.host_str().is_some()
|
||||
&& !url.path().trim_matches('/').is_empty()
|
||||
&& url.query_pairs().all(|(name, value)| {
|
||||
name != "sslmode"
|
||||
|| matches!(
|
||||
value.as_ref(),
|
||||
"disable"
|
||||
| "allow"
|
||||
| "prefer"
|
||||
| "require"
|
||||
| "verify-ca"
|
||||
| "verify-full"
|
||||
)
|
||||
})
|
||||
});
|
||||
if !valid {
|
||||
parser.push(DiagnosticCode::InvalidType, "CRANK_DATABASE_URL");
|
||||
}
|
||||
}
|
||||
let database = DatabaseSettings {
|
||||
url: database_url,
|
||||
host: parser.string("POSTGRES_HOST", Some("postgres")),
|
||||
port: parser.number("POSTGRES_PORT") as u16,
|
||||
database: parser.string("POSTGRES_DB", Some("crank")),
|
||||
username: parser.string("POSTGRES_USER", Some("crank")),
|
||||
password: parser.secret("POSTGRES_PASSWORD", Some("crank")),
|
||||
pool: PoolSettings {
|
||||
max_connections: parser.number("POSTGRES_MAX_CONNECTIONS") as u32,
|
||||
min_connections: parser.number("POSTGRES_MIN_CONNECTIONS") as u32,
|
||||
acquire_timeout_ms: parser.number("POSTGRES_ACQUIRE_TIMEOUT_MS"),
|
||||
idle_timeout_ms: parser.number("POSTGRES_IDLE_TIMEOUT_MS"),
|
||||
max_lifetime_ms: parser.number("POSTGRES_MAX_LIFETIME_MS"),
|
||||
},
|
||||
};
|
||||
if !valid_database_host(&database.host) {
|
||||
parser.push(DiagnosticCode::InvalidType, "POSTGRES_HOST");
|
||||
}
|
||||
for (name, value) in [
|
||||
("POSTGRES_DB", database.database.as_str()),
|
||||
("POSTGRES_USER", database.username.as_str()),
|
||||
] {
|
||||
if !valid_database_identifier(value) {
|
||||
parser.push(DiagnosticCode::InvalidType, name);
|
||||
}
|
||||
}
|
||||
if database.pool.min_connections > database.pool.max_connections {
|
||||
parser.push(
|
||||
DiagnosticCode::UnsafeCombination,
|
||||
"database.pool.min_connections",
|
||||
);
|
||||
}
|
||||
database
|
||||
}
|
||||
|
||||
pub(crate) fn parse_database_source(
|
||||
source: ConfigSource,
|
||||
) -> Result<(DatabaseSettings, Vec<DeprecationRecord>), ConfigError> {
|
||||
let values = source.values();
|
||||
let mut parser = Parser::new(ProcessKind::AdminApi, values);
|
||||
parser.check_source();
|
||||
let database = parse_database(&mut parser);
|
||||
if !parser.diagnostics.is_empty() {
|
||||
return Err(ConfigError::from_diagnostics(parser.diagnostics));
|
||||
}
|
||||
Ok((database, parser.deprecations))
|
||||
}
|
||||
|
||||
pub fn parse_process(
|
||||
kind: ProcessKind,
|
||||
source: ConfigSource,
|
||||
) -> Result<EffectiveConfig, ConfigError> {
|
||||
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
|
||||
.values
|
||||
.get("CRANK_CACHE_BACKEND")
|
||||
.map(String::as_str)
|
||||
.unwrap_or("memory")
|
||||
.to_ascii_lowercase()
|
||||
.as_str()
|
||||
{
|
||||
"memory" => CacheBackend::Memory,
|
||||
"valkey" => CacheBackend::Valkey,
|
||||
"redis" => CacheBackend::Redis,
|
||||
_ => {
|
||||
parser.push(DiagnosticCode::InvalidType, "CRANK_CACHE_BACKEND");
|
||||
CacheBackend::Memory
|
||||
}
|
||||
};
|
||||
if cache_backend == CacheBackend::Redis {
|
||||
parser.deprecations.push(DeprecationRecord {
|
||||
field: semantic_path_for("CRANK_CACHE_BACKEND"),
|
||||
source_class: "canonical_env_compatibility_value",
|
||||
replacement: "valkey",
|
||||
removal_window: "after-0.3",
|
||||
});
|
||||
}
|
||||
let cache_url = parser.optional_secret("CRANK_CACHE_URL");
|
||||
if let Some(url) = cache_url.as_ref()
|
||||
&& Url::parse(url.expose_secret()).ok().is_none_or(|url| {
|
||||
!matches!(url.scheme(), "redis" | "rediss")
|
||||
|| url.host_str().is_none()
|
||||
|| url.query().is_some()
|
||||
|| url.fragment().is_some()
|
||||
})
|
||||
{
|
||||
parser.push(DiagnosticCode::InvalidType, "CRANK_CACHE_URL");
|
||||
}
|
||||
if matches!(cache_backend, CacheBackend::Valkey | CacheBackend::Redis) && cache_url.is_none() {
|
||||
parser.push(DiagnosticCode::MissingRequired, "CRANK_CACHE_URL");
|
||||
}
|
||||
if cache_backend == CacheBackend::Memory && cache_url.is_some() {
|
||||
parser.push(DiagnosticCode::Conflict, "cache.url");
|
||||
}
|
||||
|
||||
let runtime = RuntimeSettings {
|
||||
master_key: parser.secret("CRANK_MASTER_KEY", None),
|
||||
base_url: parser.url("CRANK_BASE_URL").or_else(|| {
|
||||
field_registry()
|
||||
.iter()
|
||||
.find(|field| field.env_name == "CRANK_BASE_URL")
|
||||
.and_then(|field| field.default_for(kind.scope()))
|
||||
.map(ToOwned::to_owned)
|
||||
}),
|
||||
max_concurrent_unary: parser.number("CRANK_RUNTIME_MAX_CONCURRENT_UNARY") as usize,
|
||||
max_concurrent_sessions: if kind == ProcessKind::McpServer {
|
||||
parser.number("CRANK_RUNTIME_MAX_CONCURRENT_SESSIONS") as usize
|
||||
} else {
|
||||
16
|
||||
},
|
||||
cache: CacheSettings {
|
||||
backend: cache_backend,
|
||||
url: cache_url,
|
||||
},
|
||||
outbound: OutboundSettings {
|
||||
allowed_hosts: parser.host_list("CRANK_OUTBOUND_ALLOWED_HOSTS"),
|
||||
denied_hosts: parser.host_list("CRANK_OUTBOUND_DENIED_HOSTS"),
|
||||
max_response_bytes: parser.number("CRANK_OUTBOUND_MAX_RESPONSE_BYTES") as usize,
|
||||
},
|
||||
};
|
||||
|
||||
let metrics_bind_name = match kind {
|
||||
ProcessKind::AdminApi => "CRANK_ADMIN_METRICS_BIND",
|
||||
ProcessKind::McpServer => "CRANK_MCP_METRICS_BIND",
|
||||
};
|
||||
let metrics = MetricsSettings {
|
||||
enabled: parser.boolean("CRANK_METRICS_ENABLED"),
|
||||
bind_addr: parser.socket(metrics_bind_name),
|
||||
bearer_token: parser.optional_secret("CRANK_METRICS_BEARER_TOKEN"),
|
||||
};
|
||||
if metrics.enabled && !metrics.bind_addr.ip().is_loopback() && metrics.bearer_token.is_none() {
|
||||
parser.push(
|
||||
DiagnosticCode::UnsafeCombination,
|
||||
"observability.metrics.bearer_token",
|
||||
);
|
||||
}
|
||||
|
||||
let otlp = OtlpSettings {
|
||||
endpoint: parser.url("OTEL_EXPORTER_OTLP_ENDPOINT"),
|
||||
traces_endpoint: parser.url("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"),
|
||||
protocol: parser.optional("OTEL_EXPORTER_OTLP_PROTOCOL").or_else(|| {
|
||||
field_registry()
|
||||
.iter()
|
||||
.find(|field| field.env_name == "OTEL_EXPORTER_OTLP_PROTOCOL")
|
||||
.and_then(|field| field.default)
|
||||
.map(ToOwned::to_owned)
|
||||
}),
|
||||
traces_protocol: parser.optional("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"),
|
||||
timeout: parser.duration(
|
||||
"OTEL_EXPORTER_OTLP_TIMEOUT",
|
||||
field_registry()
|
||||
.iter()
|
||||
.find(|field| field.env_name == "OTEL_EXPORTER_OTLP_TIMEOUT")
|
||||
.and_then(|field| field.default)
|
||||
.and_then(|value| value.parse().ok()),
|
||||
),
|
||||
traces_timeout: parser.duration("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT", None),
|
||||
headers: parser.headers("OTEL_EXPORTER_OTLP_HEADERS"),
|
||||
traces_headers: parser.headers("OTEL_EXPORTER_OTLP_TRACES_HEADERS"),
|
||||
max_queue_size: parser.number("OTEL_BSP_MAX_QUEUE_SIZE") as usize,
|
||||
max_export_batch_size: parser.number("OTEL_BSP_MAX_EXPORT_BATCH_SIZE") as usize,
|
||||
schedule_delay: parser
|
||||
.duration("OTEL_BSP_SCHEDULE_DELAY", Some(5_000))
|
||||
.expect("duration default"),
|
||||
export_timeout: parser
|
||||
.duration("OTEL_BSP_EXPORT_TIMEOUT", Some(30_000))
|
||||
.expect("duration default"),
|
||||
};
|
||||
if otlp.max_export_batch_size > otlp.max_queue_size {
|
||||
parser.push(
|
||||
DiagnosticCode::UnsafeCombination,
|
||||
"observability.otlp.max_export_batch_size",
|
||||
);
|
||||
}
|
||||
for (name, protocol) in [
|
||||
("OTEL_EXPORTER_OTLP_PROTOCOL", otlp.protocol.as_deref()),
|
||||
(
|
||||
"OTEL_EXPORTER_OTLP_TRACES_PROTOCOL",
|
||||
otlp.traces_protocol.as_deref(),
|
||||
),
|
||||
] {
|
||||
if protocol.is_some_and(|value| value != "http/protobuf") {
|
||||
parser.push(DiagnosticCode::InvalidType, name);
|
||||
}
|
||||
}
|
||||
|
||||
let observability = ObservabilitySettings {
|
||||
environment: parser.string("CRANK_ENVIRONMENT", Some("development")),
|
||||
log_filter: parser.string(
|
||||
"CRANK_LOG_LEVEL",
|
||||
field_registry()
|
||||
.iter()
|
||||
.find(|field| field.env_name == "CRANK_LOG_LEVEL")
|
||||
.and_then(|field| field.default_for(kind.scope())),
|
||||
),
|
||||
sentry_dsn: parser.optional_secret("CRANK_SENTRY_DSN"),
|
||||
metrics,
|
||||
otlp,
|
||||
};
|
||||
if observability.environment.len() > 64
|
||||
|| !observability
|
||||
.environment
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b'+'))
|
||||
{
|
||||
parser.push(DiagnosticCode::InvalidType, "CRANK_ENVIRONMENT");
|
||||
}
|
||||
if let Some(dsn) = observability.sentry_dsn.as_ref()
|
||||
&& Url::parse(dsn.expose_secret())
|
||||
.ok()
|
||||
.is_none_or(|url| !matches!(url.scheme(), "http" | "https") || url.host_str().is_none())
|
||||
{
|
||||
parser.push(DiagnosticCode::InvalidType, "CRANK_SENTRY_DSN");
|
||||
}
|
||||
|
||||
let projection = match kind {
|
||||
ProcessKind::AdminApi => {
|
||||
let rps = parser.number("CRANK_ADMIN_RATE_LIMIT_RPS") as u32;
|
||||
let burst = parser.number("CRANK_ADMIN_RATE_LIMIT_BURST") as u32;
|
||||
if burst < rps {
|
||||
parser.push(DiagnosticCode::UnsafeCombination, "admin.rate_limit.burst");
|
||||
}
|
||||
Projection::Admin(AdminProcessConfig {
|
||||
database,
|
||||
runtime,
|
||||
observability,
|
||||
bind_addr: parser.socket("CRANK_ADMIN_BIND"),
|
||||
storage_root: parser.absolute_path("CRANK_STORAGE_ROOT", "/var/lib/crank/storage"),
|
||||
rate_limit: RateLimitSettings {
|
||||
requests_per_second: rps,
|
||||
burst,
|
||||
},
|
||||
invocation_log_retention_days: parser.number("CRANK_INVOCATION_LOG_RETENTION_DAYS")
|
||||
as i64,
|
||||
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"),
|
||||
bootstrap_email: parser.string("CRANK_BOOTSTRAP_ADMIN_EMAIL", None),
|
||||
bootstrap_password: parser.secret("CRANK_BOOTSTRAP_ADMIN_PASSWORD", None),
|
||||
bootstrap_display_name: parser
|
||||
.string("CRANK_BOOTSTRAP_ADMIN_DISPLAY_NAME", Some("Crank Owner")),
|
||||
demo_seed: parser.boolean("CRANK_DEMO_SEED"),
|
||||
})
|
||||
}
|
||||
ProcessKind::McpServer => {
|
||||
let rps = parser.number("CRANK_MCP_RATE_LIMIT_RPS") as u32;
|
||||
let burst = parser.number("CRANK_MCP_RATE_LIMIT_BURST") as u32;
|
||||
if burst < rps {
|
||||
parser.push(DiagnosticCode::UnsafeCombination, "mcp.rate_limit.burst");
|
||||
}
|
||||
Projection::Mcp(McpProcessConfig {
|
||||
database,
|
||||
runtime,
|
||||
observability,
|
||||
bind_addr: parser.socket("CRANK_MCP_BIND"),
|
||||
refresh_ms: parser.number("CRANK_MCP_REFRESH_MS"),
|
||||
rate_limit: RateLimitSettings {
|
||||
requests_per_second: rps,
|
||||
burst,
|
||||
},
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
if !parser.diagnostics.is_empty() {
|
||||
return Err(ConfigError::from_diagnostics(parser.diagnostics));
|
||||
}
|
||||
|
||||
let fingerprint_parts = fingerprint_parts(kind, &projection);
|
||||
Ok(EffectiveConfig {
|
||||
kind,
|
||||
fingerprint: sha256_hex(fingerprint_parts),
|
||||
projection,
|
||||
deprecations: parser.deprecations,
|
||||
})
|
||||
}
|
||||
|
||||
fn fingerprint_parts(kind: ProcessKind, projection: &Projection) -> Vec<String> {
|
||||
let (database, runtime, observability, process_parts): (
|
||||
&DatabaseSettings,
|
||||
&RuntimeSettings,
|
||||
&ObservabilitySettings,
|
||||
Vec<String>,
|
||||
) = match projection {
|
||||
Projection::Admin(config) => (
|
||||
&config.database,
|
||||
&config.runtime,
|
||||
&config.observability,
|
||||
vec![
|
||||
format!("bind={}", config.bind_addr),
|
||||
format!(
|
||||
"rate={}:{}",
|
||||
config.rate_limit.requests_per_second, config.rate_limit.burst
|
||||
),
|
||||
format!("retention={}", config.invocation_log_retention_days),
|
||||
format!("session_ttl={}", config.session_ttl_hours),
|
||||
format!("trusted={}", config.trust_forwarded_headers),
|
||||
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()
|
||||
),
|
||||
],
|
||||
),
|
||||
Projection::Mcp(config) => (
|
||||
&config.database,
|
||||
&config.runtime,
|
||||
&config.observability,
|
||||
vec![
|
||||
format!("bind={}", config.bind_addr),
|
||||
format!("refresh={}", config.refresh_ms),
|
||||
format!(
|
||||
"rate={}:{}",
|
||||
config.rate_limit.requests_per_second, config.rate_limit.burst
|
||||
),
|
||||
],
|
||||
),
|
||||
};
|
||||
let (db_host, db_port, db_name, db_user, db_sslmode) = database
|
||||
.url
|
||||
.as_ref()
|
||||
.and_then(|secret| {
|
||||
Url::parse(secret.expose_secret()).ok().map(|url| {
|
||||
(
|
||||
url.host_str().unwrap_or_default().to_ascii_lowercase(),
|
||||
url.port().unwrap_or(5432),
|
||||
url.path().trim_start_matches('/').to_owned(),
|
||||
url.username().to_owned(),
|
||||
url.query_pairs()
|
||||
.find(|(name, _)| name == "sslmode")
|
||||
.map_or_else(|| "default".to_owned(), |(_, value)| value.into_owned()),
|
||||
)
|
||||
})
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
(
|
||||
database.host.to_ascii_lowercase(),
|
||||
database.port,
|
||||
database.database.clone(),
|
||||
database.username.clone(),
|
||||
"default".to_owned(),
|
||||
)
|
||||
});
|
||||
let mut allowed = runtime.outbound.allowed_hosts.clone();
|
||||
allowed.sort();
|
||||
let mut denied = runtime.outbound.denied_hosts.clone();
|
||||
denied.sort();
|
||||
let mut parts = vec![
|
||||
"schema=crank-config-v1".to_owned(),
|
||||
format!("process={}", kind.as_str()),
|
||||
format!("database={db_host}:{db_port}/{db_name}:{db_user}:{db_sslmode}"),
|
||||
format!("database_password={}", database.password.is_configured()),
|
||||
format!(
|
||||
"pool={}:{}:{}:{}:{}",
|
||||
database.pool.max_connections,
|
||||
database.pool.min_connections,
|
||||
database.pool.acquire_timeout_ms,
|
||||
database.pool.idle_timeout_ms,
|
||||
database.pool.max_lifetime_ms
|
||||
),
|
||||
format!("master_key={}", runtime.master_key.is_configured()),
|
||||
format!(
|
||||
"base_url={}",
|
||||
runtime.base_url.as_deref().unwrap_or("unconfigured")
|
||||
),
|
||||
format!("unary={}", runtime.max_concurrent_unary),
|
||||
format!("sessions={}", runtime.max_concurrent_sessions),
|
||||
format!(
|
||||
"cache={:?}:{}",
|
||||
runtime.cache.backend,
|
||||
runtime.cache.url.is_some()
|
||||
),
|
||||
format!("allowed={}", allowed.join(",")),
|
||||
format!("denied={}", denied.join(",")),
|
||||
format!("max_response={}", runtime.outbound.max_response_bytes),
|
||||
format!("environment={}", observability.environment),
|
||||
format!("log_filter={}", observability.log_filter),
|
||||
format!("sentry={}", observability.sentry_dsn.is_some()),
|
||||
format!(
|
||||
"metrics={}:{}:{}",
|
||||
observability.metrics.enabled,
|
||||
observability.metrics.bind_addr,
|
||||
observability.metrics.bearer_token.is_some()
|
||||
),
|
||||
format!(
|
||||
"otlp={}:{}:{}:{}",
|
||||
observability.otlp.endpoint.is_some(),
|
||||
observability.otlp.traces_endpoint.is_some(),
|
||||
observability.otlp.headers.is_some(),
|
||||
observability.otlp.traces_headers.is_some()
|
||||
),
|
||||
format!(
|
||||
"otlp_limits={}:{}:{}:{}",
|
||||
observability.otlp.max_queue_size,
|
||||
observability.otlp.max_export_batch_size,
|
||||
observability.otlp.schedule_delay,
|
||||
observability.otlp.export_timeout
|
||||
),
|
||||
];
|
||||
parts.extend(process_parts);
|
||||
parts
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{FieldMode, FieldSpec, Sensitivity, deployment_field_registry, field_registry};
|
||||
|
||||
pub const BEGIN_MARKER: &str = "# BEGIN GENERATED CRANK RUNTIME CONFIG";
|
||||
pub const END_MARKER: &str = "# END GENERATED CRANK RUNTIME CONFIG";
|
||||
pub const DOC_BEGIN_MARKER: &str = "<!-- BEGIN GENERATED CRANK RUNTIME CONFIG -->";
|
||||
pub const DOC_END_MARKER: &str = "<!-- END GENERATED CRANK RUNTIME CONFIG -->";
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct RuntimeContract<'a> {
|
||||
schema_version: u32,
|
||||
generated_by: &'static str,
|
||||
fields: &'a [FieldSpec],
|
||||
deployment_only_fields: &'static [&'static str],
|
||||
}
|
||||
|
||||
pub fn schema_json() -> String {
|
||||
let contract = RuntimeContract {
|
||||
schema_version: 1,
|
||||
generated_by: "crank-config",
|
||||
fields: field_registry(),
|
||||
deployment_only_fields: deployment_field_registry(),
|
||||
};
|
||||
let mut rendered = serde_json::to_string_pretty(&contract).expect("static contract serializes");
|
||||
rendered.push('\n');
|
||||
rendered
|
||||
}
|
||||
|
||||
pub fn env_section(production: bool) -> String {
|
||||
let mut output = String::new();
|
||||
output.push_str(BEGIN_MARKER);
|
||||
output.push('\n');
|
||||
for field in field_registry()
|
||||
.iter()
|
||||
.filter(|field| field.mode == FieldMode::Effective)
|
||||
{
|
||||
let value = example_value(field, production);
|
||||
output.push_str(field.env_name);
|
||||
output.push('=');
|
||||
output.push_str(&value);
|
||||
output.push('\n');
|
||||
}
|
||||
output.push_str(END_MARKER);
|
||||
output.push('\n');
|
||||
output
|
||||
}
|
||||
|
||||
pub fn reference_section() -> String {
|
||||
let mut output = String::new();
|
||||
output.push_str(DOC_BEGIN_MARKER);
|
||||
output.push_str("\n\n| Environment | Semantic path | Process | Type/unit | Default | Bounds | Sensitivity | Mode |\n");
|
||||
output.push_str("|---|---|---|---|---|---|---|---|\n");
|
||||
for field in field_registry() {
|
||||
let unit = field.unit.unwrap_or("-");
|
||||
let default = match (field.sensitivity, field.default, field.required) {
|
||||
(Sensitivity::Secret, Some(_), _) => "configured",
|
||||
(Sensitivity::Secret, None, true) => "required/blank",
|
||||
(Sensitivity::Secret, None, false) => "blank",
|
||||
(_, Some(default), _) => default,
|
||||
(_, None, true) => "required/blank",
|
||||
(_, None, false) => "blank",
|
||||
};
|
||||
let bounds = match (field.minimum, field.maximum) {
|
||||
(Some(minimum), Some(maximum)) => format!("{minimum}..={maximum}"),
|
||||
_ => "-".to_owned(),
|
||||
};
|
||||
output.push_str(&format!(
|
||||
"| `{}` | `{}` | `{:?}` | `{}/{}` | `{}` | `{}` | `{:?}` | `{:?}` |\n",
|
||||
field.env_name,
|
||||
field.semantic_path,
|
||||
field.process,
|
||||
field.value_type,
|
||||
unit,
|
||||
default,
|
||||
bounds,
|
||||
field.sensitivity,
|
||||
field.mode,
|
||||
));
|
||||
}
|
||||
output.push('\n');
|
||||
output.push_str(DOC_END_MARKER);
|
||||
output.push('\n');
|
||||
output
|
||||
}
|
||||
|
||||
pub fn replace_marked(content: &str, begin: &str, end: &str, replacement: &str) -> Option<String> {
|
||||
let start = content.find(begin)?;
|
||||
let tail = &content[start..];
|
||||
let end_offset = tail.find(end)? + end.len();
|
||||
let suffix_start = start + end_offset;
|
||||
let mut rendered = String::with_capacity(content.len() + replacement.len());
|
||||
rendered.push_str(&content[..start]);
|
||||
rendered.push_str(replacement.trim_end());
|
||||
rendered.push_str(&content[suffix_start..]);
|
||||
Some(rendered)
|
||||
}
|
||||
|
||||
fn example_value(field: &FieldSpec, production: bool) -> String {
|
||||
if field.sensitivity == Sensitivity::Secret {
|
||||
return String::new();
|
||||
}
|
||||
match (field.env_name, production) {
|
||||
("CRANK_ENVIRONMENT", true) => "production".to_owned(),
|
||||
("CRANK_BASE_URL", _) => "http://localhost:3000".to_owned(),
|
||||
("POSTGRES_HOST", true) => "postgres".to_owned(),
|
||||
("CRANK_TRUST_FORWARDED_HEADERS", true) => "true".to_owned(),
|
||||
_ => field.default.unwrap_or("").to_owned(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,797 @@
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ProcessScope {
|
||||
Shared,
|
||||
AdminApi,
|
||||
McpServer,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Sensitivity {
|
||||
Public,
|
||||
Internal,
|
||||
Secret,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum FieldMode {
|
||||
Effective,
|
||||
DeprecatedNoEffect,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
|
||||
pub struct FieldSpec {
|
||||
pub semantic_path: &'static str,
|
||||
pub env_name: &'static str,
|
||||
pub process: ProcessScope,
|
||||
pub value_type: &'static str,
|
||||
pub unit: Option<&'static str>,
|
||||
pub default: Option<&'static str>,
|
||||
pub required: bool,
|
||||
pub minimum: Option<u64>,
|
||||
pub maximum: Option<u64>,
|
||||
pub sensitivity: Sensitivity,
|
||||
pub mode: FieldMode,
|
||||
pub compatibility: Option<&'static str>,
|
||||
pub rules: &'static [&'static str],
|
||||
}
|
||||
|
||||
impl FieldSpec {
|
||||
pub fn default_for(self, process: ProcessScope) -> Option<&'static str> {
|
||||
match (self.env_name, process) {
|
||||
("CRANK_BASE_URL", ProcessScope::AdminApi) => Some("http://localhost:3000"),
|
||||
("CRANK_LOG_LEVEL", ProcessScope::AdminApi) => Some("admin_api=info,tower_http=info"),
|
||||
("CRANK_LOG_LEVEL", ProcessScope::McpServer) => Some("mcp_server=info,tower_http=info"),
|
||||
_ => self.default,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn semantic_path_for(env_name: &str) -> &str {
|
||||
field_registry()
|
||||
.iter()
|
||||
.find(|field| field.env_name == env_name)
|
||||
.map_or(env_name, |field| field.semantic_path)
|
||||
}
|
||||
|
||||
macro_rules! f {
|
||||
($path:literal,$name:literal,$proc:ident,$type:literal,$unit:expr,$default:expr,$min:expr,$max:expr,$sensitivity:ident) => {
|
||||
FieldSpec {
|
||||
semantic_path: $path,
|
||||
env_name: $name,
|
||||
process: ProcessScope::$proc,
|
||||
value_type: $type,
|
||||
unit: $unit,
|
||||
default: $default,
|
||||
required: false,
|
||||
minimum: $min,
|
||||
maximum: $max,
|
||||
sensitivity: Sensitivity::$sensitivity,
|
||||
mode: FieldMode::Effective,
|
||||
compatibility: None,
|
||||
rules: &[],
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static FIELDS: [FieldSpec; 57] = [
|
||||
FieldSpec {
|
||||
compatibility: Some("legacy URL form"),
|
||||
rules: &[
|
||||
"takes precedence over generated default-valued POSTGRES_HOST/PORT/DB/USER/PASSWORD",
|
||||
"conflicts with any non-default decomposed database value",
|
||||
],
|
||||
..f!(
|
||||
"database.url",
|
||||
"CRANK_DATABASE_URL",
|
||||
Shared,
|
||||
"url",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Secret
|
||||
)
|
||||
},
|
||||
f!(
|
||||
"database.host",
|
||||
"POSTGRES_HOST",
|
||||
Shared,
|
||||
"string",
|
||||
None,
|
||||
Some("postgres"),
|
||||
None,
|
||||
None,
|
||||
Internal
|
||||
),
|
||||
f!(
|
||||
"database.port",
|
||||
"POSTGRES_PORT",
|
||||
Shared,
|
||||
"u16",
|
||||
Some("port"),
|
||||
Some("5432"),
|
||||
Some(1),
|
||||
Some(65535),
|
||||
Public
|
||||
),
|
||||
f!(
|
||||
"database.name",
|
||||
"POSTGRES_DB",
|
||||
Shared,
|
||||
"string",
|
||||
None,
|
||||
Some("crank"),
|
||||
None,
|
||||
None,
|
||||
Internal
|
||||
),
|
||||
f!(
|
||||
"database.user",
|
||||
"POSTGRES_USER",
|
||||
Shared,
|
||||
"string",
|
||||
None,
|
||||
Some("crank"),
|
||||
None,
|
||||
None,
|
||||
Internal
|
||||
),
|
||||
f!(
|
||||
"database.password",
|
||||
"POSTGRES_PASSWORD",
|
||||
Shared,
|
||||
"secret",
|
||||
None,
|
||||
Some("configured"),
|
||||
None,
|
||||
None,
|
||||
Secret
|
||||
),
|
||||
FieldSpec {
|
||||
rules: &["must be >= min_connections"],
|
||||
..f!(
|
||||
"database.pool.max_connections",
|
||||
"POSTGRES_MAX_CONNECTIONS",
|
||||
Shared,
|
||||
"u32",
|
||||
Some("connections"),
|
||||
Some("20"),
|
||||
Some(1),
|
||||
Some(1024),
|
||||
Public
|
||||
)
|
||||
},
|
||||
FieldSpec {
|
||||
rules: &["must be <= max_connections"],
|
||||
..f!(
|
||||
"database.pool.min_connections",
|
||||
"POSTGRES_MIN_CONNECTIONS",
|
||||
Shared,
|
||||
"u32",
|
||||
Some("connections"),
|
||||
Some("2"),
|
||||
Some(0),
|
||||
Some(1024),
|
||||
Public
|
||||
)
|
||||
},
|
||||
f!(
|
||||
"database.pool.acquire_timeout_ms",
|
||||
"POSTGRES_ACQUIRE_TIMEOUT_MS",
|
||||
Shared,
|
||||
"u64",
|
||||
Some("milliseconds"),
|
||||
Some("5000"),
|
||||
Some(1),
|
||||
Some(300000),
|
||||
Public
|
||||
),
|
||||
f!(
|
||||
"database.pool.idle_timeout_ms",
|
||||
"POSTGRES_IDLE_TIMEOUT_MS",
|
||||
Shared,
|
||||
"u64",
|
||||
Some("milliseconds"),
|
||||
Some("600000"),
|
||||
Some(1000),
|
||||
Some(86400000),
|
||||
Public
|
||||
),
|
||||
f!(
|
||||
"database.pool.max_lifetime_ms",
|
||||
"POSTGRES_MAX_LIFETIME_MS",
|
||||
Shared,
|
||||
"u64",
|
||||
Some("milliseconds"),
|
||||
Some("1800000"),
|
||||
Some(1000),
|
||||
Some(86400000),
|
||||
Public
|
||||
),
|
||||
FieldSpec {
|
||||
required: true,
|
||||
..f!(
|
||||
"runtime.master_key",
|
||||
"CRANK_MASTER_KEY",
|
||||
Shared,
|
||||
"secret",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Secret
|
||||
)
|
||||
},
|
||||
f!(
|
||||
"runtime.base_url",
|
||||
"CRANK_BASE_URL",
|
||||
Shared,
|
||||
"url",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Internal
|
||||
),
|
||||
f!(
|
||||
"runtime.max_concurrent_unary",
|
||||
"CRANK_RUNTIME_MAX_CONCURRENT_UNARY",
|
||||
Shared,
|
||||
"u32",
|
||||
Some("requests"),
|
||||
Some("64"),
|
||||
Some(1),
|
||||
Some(65535),
|
||||
Public
|
||||
),
|
||||
FieldSpec {
|
||||
compatibility: Some("redis value is deprecated in favor of valkey"),
|
||||
rules: &["external backend requires cache.url"],
|
||||
..f!(
|
||||
"cache.backend",
|
||||
"CRANK_CACHE_BACKEND",
|
||||
Shared,
|
||||
"enum",
|
||||
None,
|
||||
Some("memory"),
|
||||
None,
|
||||
None,
|
||||
Public
|
||||
)
|
||||
},
|
||||
FieldSpec {
|
||||
rules: &["forbidden with memory backend"],
|
||||
..f!(
|
||||
"cache.url",
|
||||
"CRANK_CACHE_URL",
|
||||
Shared,
|
||||
"url",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Secret
|
||||
)
|
||||
},
|
||||
FieldSpec {
|
||||
mode: FieldMode::DeprecatedNoEffect,
|
||||
..f!(
|
||||
"cache.default_ttl_ms",
|
||||
"CRANK_CACHE_DEFAULT_TTL_MS",
|
||||
Shared,
|
||||
"u64",
|
||||
Some("milliseconds"),
|
||||
None,
|
||||
Some(1),
|
||||
Some(86400000),
|
||||
Public
|
||||
)
|
||||
},
|
||||
f!(
|
||||
"outbound.allowed_hosts",
|
||||
"CRANK_OUTBOUND_ALLOWED_HOSTS",
|
||||
Shared,
|
||||
"host_list",
|
||||
None,
|
||||
Some(""),
|
||||
None,
|
||||
None,
|
||||
Internal
|
||||
),
|
||||
FieldSpec {
|
||||
rules: &["deny entries override allow entries"],
|
||||
..f!(
|
||||
"outbound.denied_hosts",
|
||||
"CRANK_OUTBOUND_DENIED_HOSTS",
|
||||
Shared,
|
||||
"host_list",
|
||||
None,
|
||||
Some(""),
|
||||
None,
|
||||
None,
|
||||
Internal
|
||||
)
|
||||
},
|
||||
f!(
|
||||
"outbound.max_response_bytes",
|
||||
"CRANK_OUTBOUND_MAX_RESPONSE_BYTES",
|
||||
Shared,
|
||||
"u64",
|
||||
Some("bytes"),
|
||||
Some("4194304"),
|
||||
Some(1),
|
||||
Some(67108864),
|
||||
Public
|
||||
),
|
||||
f!(
|
||||
"observability.environment",
|
||||
"CRANK_ENVIRONMENT",
|
||||
Shared,
|
||||
"label",
|
||||
None,
|
||||
Some("development"),
|
||||
None,
|
||||
None,
|
||||
Public
|
||||
),
|
||||
f!(
|
||||
"observability.log_filter",
|
||||
"CRANK_LOG_LEVEL",
|
||||
Shared,
|
||||
"string",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Public
|
||||
),
|
||||
f!(
|
||||
"observability.sentry_dsn",
|
||||
"CRANK_SENTRY_DSN",
|
||||
Shared,
|
||||
"url",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Secret
|
||||
),
|
||||
FieldSpec {
|
||||
compatibility: Some("yes/no/on/off spellings are deprecated"),
|
||||
..f!(
|
||||
"observability.metrics.enabled",
|
||||
"CRANK_METRICS_ENABLED",
|
||||
Shared,
|
||||
"bool",
|
||||
None,
|
||||
Some("true"),
|
||||
None,
|
||||
None,
|
||||
Public
|
||||
)
|
||||
},
|
||||
FieldSpec {
|
||||
rules: &["required when an enabled metrics bind is non-loopback"],
|
||||
..f!(
|
||||
"observability.metrics.bearer_token",
|
||||
"CRANK_METRICS_BEARER_TOKEN",
|
||||
Shared,
|
||||
"secret",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Secret
|
||||
)
|
||||
},
|
||||
f!(
|
||||
"observability.otlp.endpoint",
|
||||
"OTEL_EXPORTER_OTLP_ENDPOINT",
|
||||
Shared,
|
||||
"url",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Internal
|
||||
),
|
||||
FieldSpec {
|
||||
rules: &["overrides generic OTLP endpoint"],
|
||||
..f!(
|
||||
"observability.otlp.traces_endpoint",
|
||||
"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT",
|
||||
Shared,
|
||||
"url",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Internal
|
||||
)
|
||||
},
|
||||
f!(
|
||||
"observability.otlp.protocol",
|
||||
"OTEL_EXPORTER_OTLP_PROTOCOL",
|
||||
Shared,
|
||||
"enum",
|
||||
None,
|
||||
Some("http/protobuf"),
|
||||
None,
|
||||
None,
|
||||
Public
|
||||
),
|
||||
f!(
|
||||
"observability.otlp.traces_protocol",
|
||||
"OTEL_EXPORTER_OTLP_TRACES_PROTOCOL",
|
||||
Shared,
|
||||
"enum",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Public
|
||||
),
|
||||
f!(
|
||||
"observability.otlp.timeout",
|
||||
"OTEL_EXPORTER_OTLP_TIMEOUT",
|
||||
Shared,
|
||||
"duration",
|
||||
Some("milliseconds"),
|
||||
Some("10000"),
|
||||
Some(1),
|
||||
Some(300000),
|
||||
Public
|
||||
),
|
||||
f!(
|
||||
"observability.otlp.traces_timeout",
|
||||
"OTEL_EXPORTER_OTLP_TRACES_TIMEOUT",
|
||||
Shared,
|
||||
"duration",
|
||||
Some("milliseconds"),
|
||||
None,
|
||||
Some(1),
|
||||
Some(300000),
|
||||
Public
|
||||
),
|
||||
f!(
|
||||
"observability.otlp.headers",
|
||||
"OTEL_EXPORTER_OTLP_HEADERS",
|
||||
Shared,
|
||||
"headers",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Secret
|
||||
),
|
||||
f!(
|
||||
"observability.otlp.traces_headers",
|
||||
"OTEL_EXPORTER_OTLP_TRACES_HEADERS",
|
||||
Shared,
|
||||
"headers",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Secret
|
||||
),
|
||||
f!(
|
||||
"observability.otlp.max_queue_size",
|
||||
"OTEL_BSP_MAX_QUEUE_SIZE",
|
||||
Shared,
|
||||
"u32",
|
||||
Some("spans"),
|
||||
Some("2048"),
|
||||
Some(1),
|
||||
Some(65536),
|
||||
Public
|
||||
),
|
||||
FieldSpec {
|
||||
rules: &["must be <= max_queue_size"],
|
||||
..f!(
|
||||
"observability.otlp.max_export_batch_size",
|
||||
"OTEL_BSP_MAX_EXPORT_BATCH_SIZE",
|
||||
Shared,
|
||||
"u32",
|
||||
Some("spans"),
|
||||
Some("512"),
|
||||
Some(1),
|
||||
Some(65536),
|
||||
Public
|
||||
)
|
||||
},
|
||||
f!(
|
||||
"observability.otlp.schedule_delay",
|
||||
"OTEL_BSP_SCHEDULE_DELAY",
|
||||
Shared,
|
||||
"duration",
|
||||
Some("milliseconds"),
|
||||
Some("5000"),
|
||||
Some(1),
|
||||
Some(300000),
|
||||
Public
|
||||
),
|
||||
f!(
|
||||
"observability.otlp.export_timeout",
|
||||
"OTEL_BSP_EXPORT_TIMEOUT",
|
||||
Shared,
|
||||
"duration",
|
||||
Some("milliseconds"),
|
||||
Some("30000"),
|
||||
Some(1),
|
||||
Some(300000),
|
||||
Public
|
||||
),
|
||||
f!(
|
||||
"admin.bind",
|
||||
"CRANK_ADMIN_BIND",
|
||||
AdminApi,
|
||||
"socket",
|
||||
None,
|
||||
Some("0.0.0.0:3001"),
|
||||
None,
|
||||
None,
|
||||
Internal
|
||||
),
|
||||
f!(
|
||||
"admin.metrics_bind",
|
||||
"CRANK_ADMIN_METRICS_BIND",
|
||||
AdminApi,
|
||||
"socket",
|
||||
None,
|
||||
Some("127.0.0.1:9464"),
|
||||
None,
|
||||
None,
|
||||
Internal
|
||||
),
|
||||
f!(
|
||||
"admin.storage_root",
|
||||
"CRANK_STORAGE_ROOT",
|
||||
AdminApi,
|
||||
"absolute_path",
|
||||
None,
|
||||
Some("/var/lib/crank/storage"),
|
||||
None,
|
||||
None,
|
||||
Internal
|
||||
),
|
||||
f!(
|
||||
"admin.rate_limit.rps",
|
||||
"CRANK_ADMIN_RATE_LIMIT_RPS",
|
||||
AdminApi,
|
||||
"u32",
|
||||
Some("requests_per_second"),
|
||||
Some("30"),
|
||||
Some(1),
|
||||
Some(100000),
|
||||
Public
|
||||
),
|
||||
FieldSpec {
|
||||
rules: &["must be >= admin rate RPS"],
|
||||
..f!(
|
||||
"admin.rate_limit.burst",
|
||||
"CRANK_ADMIN_RATE_LIMIT_BURST",
|
||||
AdminApi,
|
||||
"u32",
|
||||
Some("requests"),
|
||||
Some("60"),
|
||||
Some(1),
|
||||
Some(1000000),
|
||||
Public
|
||||
)
|
||||
},
|
||||
f!(
|
||||
"admin.invocation_log_retention_days",
|
||||
"CRANK_INVOCATION_LOG_RETENTION_DAYS",
|
||||
AdminApi,
|
||||
"u32",
|
||||
Some("days"),
|
||||
Some("30"),
|
||||
Some(1),
|
||||
Some(36500),
|
||||
Public
|
||||
),
|
||||
FieldSpec {
|
||||
required: true,
|
||||
..f!(
|
||||
"admin.session.secret",
|
||||
"CRANK_SESSION_SECRET",
|
||||
AdminApi,
|
||||
"secret",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Secret
|
||||
)
|
||||
},
|
||||
FieldSpec {
|
||||
required: true,
|
||||
..f!(
|
||||
"admin.password_pepper",
|
||||
"CRANK_PASSWORD_PEPPER",
|
||||
AdminApi,
|
||||
"secret",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Secret
|
||||
)
|
||||
},
|
||||
f!(
|
||||
"admin.session.ttl_hours",
|
||||
"CRANK_SESSION_TTL_HOURS",
|
||||
AdminApi,
|
||||
"u32",
|
||||
Some("hours"),
|
||||
Some("24"),
|
||||
Some(1),
|
||||
Some(8760),
|
||||
Public
|
||||
),
|
||||
FieldSpec {
|
||||
compatibility: Some("yes/no/on/off spellings are deprecated"),
|
||||
..f!(
|
||||
"admin.trust_forwarded_headers",
|
||||
"CRANK_TRUST_FORWARDED_HEADERS",
|
||||
AdminApi,
|
||||
"bool",
|
||||
None,
|
||||
Some("false"),
|
||||
None,
|
||||
None,
|
||||
Public
|
||||
)
|
||||
},
|
||||
FieldSpec {
|
||||
required: true,
|
||||
..f!(
|
||||
"admin.bootstrap.email",
|
||||
"CRANK_BOOTSTRAP_ADMIN_EMAIL",
|
||||
AdminApi,
|
||||
"string",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Internal
|
||||
)
|
||||
},
|
||||
FieldSpec {
|
||||
required: true,
|
||||
..f!(
|
||||
"admin.bootstrap.password",
|
||||
"CRANK_BOOTSTRAP_ADMIN_PASSWORD",
|
||||
AdminApi,
|
||||
"secret",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Secret
|
||||
)
|
||||
},
|
||||
f!(
|
||||
"admin.bootstrap.display_name",
|
||||
"CRANK_BOOTSTRAP_ADMIN_DISPLAY_NAME",
|
||||
AdminApi,
|
||||
"string",
|
||||
None,
|
||||
Some("Crank Owner"),
|
||||
None,
|
||||
None,
|
||||
Internal
|
||||
),
|
||||
FieldSpec {
|
||||
compatibility: Some("yes/no/on/off spellings are deprecated"),
|
||||
..f!(
|
||||
"admin.demo_seed",
|
||||
"CRANK_DEMO_SEED",
|
||||
AdminApi,
|
||||
"bool",
|
||||
None,
|
||||
Some("false"),
|
||||
None,
|
||||
None,
|
||||
Public
|
||||
)
|
||||
},
|
||||
f!(
|
||||
"mcp.bind",
|
||||
"CRANK_MCP_BIND",
|
||||
McpServer,
|
||||
"socket",
|
||||
None,
|
||||
Some("0.0.0.0:3002"),
|
||||
None,
|
||||
None,
|
||||
Internal
|
||||
),
|
||||
f!(
|
||||
"mcp.metrics_bind",
|
||||
"CRANK_MCP_METRICS_BIND",
|
||||
McpServer,
|
||||
"socket",
|
||||
None,
|
||||
Some("127.0.0.1:9465"),
|
||||
None,
|
||||
None,
|
||||
Internal
|
||||
),
|
||||
f!(
|
||||
"mcp.refresh_ms",
|
||||
"CRANK_MCP_REFRESH_MS",
|
||||
McpServer,
|
||||
"u64",
|
||||
Some("milliseconds"),
|
||||
Some("5000"),
|
||||
Some(100),
|
||||
Some(3600000),
|
||||
Public
|
||||
),
|
||||
f!(
|
||||
"mcp.rate_limit.rps",
|
||||
"CRANK_MCP_RATE_LIMIT_RPS",
|
||||
McpServer,
|
||||
"u32",
|
||||
Some("requests_per_second"),
|
||||
Some("60"),
|
||||
Some(1),
|
||||
Some(100000),
|
||||
Public
|
||||
),
|
||||
FieldSpec {
|
||||
rules: &["must be >= MCP rate RPS"],
|
||||
..f!(
|
||||
"mcp.rate_limit.burst",
|
||||
"CRANK_MCP_RATE_LIMIT_BURST",
|
||||
McpServer,
|
||||
"u32",
|
||||
Some("requests"),
|
||||
Some("120"),
|
||||
Some(1),
|
||||
Some(1000000),
|
||||
Public
|
||||
)
|
||||
},
|
||||
f!(
|
||||
"runtime.max_concurrent_sessions",
|
||||
"CRANK_RUNTIME_MAX_CONCURRENT_SESSIONS",
|
||||
McpServer,
|
||||
"u32",
|
||||
Some("sessions"),
|
||||
Some("16"),
|
||||
Some(1),
|
||||
Some(65535),
|
||||
Public
|
||||
),
|
||||
];
|
||||
|
||||
pub fn field_registry() -> &'static [FieldSpec] {
|
||||
&FIELDS
|
||||
}
|
||||
|
||||
static DEPLOYMENT_FIELDS: [&str; 12] = [
|
||||
"COMPOSE_PROJECT_NAME",
|
||||
"POSTGRES_PUBLISH_BIND",
|
||||
"POSTGRES_PUBLISH_PORT",
|
||||
"CRANK_ADMIN_API_IMAGE",
|
||||
"CRANK_MCP_SERVER_IMAGE",
|
||||
"CRANK_UI_IMAGE",
|
||||
"CRANK_PUBLISH_BIND",
|
||||
"CRANK_ADMIN_PUBLISH_PORT",
|
||||
"CRANK_MCP_PUBLISH_PORT",
|
||||
"CRANK_UI_PUBLISH_PORT",
|
||||
"VALKEY_PUBLISH_BIND",
|
||||
"VALKEY_PUBLISH_PORT",
|
||||
];
|
||||
|
||||
pub fn deployment_field_registry() -> &'static [&'static str] {
|
||||
&DEPLOYMENT_FIELDS
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
use std::{collections::BTreeMap, ffi::OsString};
|
||||
|
||||
use crate::{ConfigError, Diagnostic, DiagnosticCode, deployment_field_registry, field_registry};
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct ConfigSource {
|
||||
values: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
impl ConfigSource {
|
||||
pub fn from_utf8(values: BTreeMap<String, String>) -> Self {
|
||||
Self { values }
|
||||
}
|
||||
|
||||
pub fn from_os() -> Result<Self, ConfigError> {
|
||||
Self::from_os_iter(std::env::vars_os())
|
||||
}
|
||||
|
||||
pub fn from_os_for_migrator() -> Result<Self, ConfigError> {
|
||||
Self::from_os_iter_filtered(std::env::vars_os(), |name| {
|
||||
name.starts_with("POSTGRES_") || name.starts_with("CRANK_DATABASE_")
|
||||
})
|
||||
}
|
||||
|
||||
pub fn from_os_iter<I>(values: I) -> Result<Self, ConfigError>
|
||||
where
|
||||
I: IntoIterator<Item = (OsString, OsString)>,
|
||||
{
|
||||
Self::from_os_iter_filtered(values, |_| true)
|
||||
}
|
||||
|
||||
fn from_os_iter_filtered<I, F>(values: I, include: F) -> Result<Self, ConfigError>
|
||||
where
|
||||
I: IntoIterator<Item = (OsString, OsString)>,
|
||||
F: Fn(&str) -> bool,
|
||||
{
|
||||
let mut parsed = BTreeMap::new();
|
||||
let mut diagnostics = Vec::new();
|
||||
for (name, value) in values {
|
||||
let Ok(name) = name.into_string() else {
|
||||
// Owned names are ASCII. A non-UTF-8 name therefore cannot belong
|
||||
// to Crank and must not make startup depend on unrelated OS state.
|
||||
continue;
|
||||
};
|
||||
if !include(&name) {
|
||||
continue;
|
||||
}
|
||||
let owned = name.starts_with("CRANK_")
|
||||
|| name.starts_with("POSTGRES_")
|
||||
|| name.starts_with("OTEL_");
|
||||
let known = field_registry().iter().any(|field| field.env_name == name)
|
||||
|| deployment_field_registry().contains(&name.as_str());
|
||||
if !owned && !known {
|
||||
continue;
|
||||
}
|
||||
let value = match value.into_string() {
|
||||
Ok(value) => value,
|
||||
Err(_) => {
|
||||
let field = field_registry()
|
||||
.iter()
|
||||
.find(|field| field.env_name == name)
|
||||
.map_or("environment.unknown", |field| field.semantic_path);
|
||||
diagnostics.push(Diagnostic::new(DiagnosticCode::InvalidEncoding, field));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
parsed.insert(name, value);
|
||||
}
|
||||
if diagnostics.is_empty() {
|
||||
Ok(Self { values: parsed })
|
||||
} else {
|
||||
Err(ConfigError::from_diagnostics(diagnostics))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn values(&self) -> &BTreeMap<String, String> {
|
||||
&self.values
|
||||
}
|
||||
|
||||
pub(crate) fn retain_for_migrator(mut self) -> Self {
|
||||
self.values
|
||||
.retain(|name, _| name.starts_with("POSTGRES_") || name.starts_with("CRANK_DATABASE_"));
|
||||
self
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
pub(crate) fn valid_percent_encoding(value: &str) -> bool {
|
||||
let bytes = value.as_bytes();
|
||||
let mut index = 0;
|
||||
while index < bytes.len() {
|
||||
if bytes[index] == b'%' {
|
||||
if index + 2 >= bytes.len()
|
||||
|| !bytes[index + 1].is_ascii_hexdigit()
|
||||
|| !bytes[index + 2].is_ascii_hexdigit()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
index += 3;
|
||||
} else {
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub(crate) fn valid_database_host(value: &str) -> bool {
|
||||
!value.is_empty()
|
||||
&& value.len() <= 253
|
||||
&& !value.chars().any(char::is_whitespace)
|
||||
&& (value.parse::<std::net::IpAddr>().is_ok()
|
||||
|| value.split('.').all(|label| {
|
||||
!label.is_empty()
|
||||
&& label.len() <= 63
|
||||
&& label
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
|
||||
&& !label.starts_with('-')
|
||||
&& !label.ends_with('-')
|
||||
}))
|
||||
}
|
||||
|
||||
pub(crate) fn valid_database_identifier(value: &str) -> bool {
|
||||
!value.is_empty()
|
||||
&& value.len() <= 128
|
||||
&& value
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-' | b'.'))
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
pub struct SecretString(String);
|
||||
|
||||
impl SecretString {
|
||||
pub(crate) fn new(value: String) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
|
||||
/// Deliberate composition boundary. Never use in diagnostics or fingerprints.
|
||||
pub fn expose_secret(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
|
||||
pub fn is_configured(&self) -> bool {
|
||||
!self.0.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for SecretString {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter
|
||||
.debug_tuple("SecretString")
|
||||
.field(&if self.is_configured() {
|
||||
"configured"
|
||||
} else {
|
||||
"unconfigured"
|
||||
})
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for SecretString {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter.write_str(if self.is_configured() {
|
||||
"configured"
|
||||
} else {
|
||||
"unconfigured"
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user