phoenix-config 0.1.0

Layered, validated application configuration for Phoenix
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
use std::{
    collections::{BTreeMap, BTreeSet},
    env,
    path::PathBuf,
    sync::Arc,
    time::Duration,
};

use zeroize::Zeroizing;

use crate::{
    AppConfig, ConfigError, DEFAULT_ADDRESS, DEFAULT_DATABASE_URL, DEFAULT_RATE_LIMIT_REQUESTS,
    DEFAULT_RATE_LIMIT_WINDOW_SECONDS, DEFAULT_VITE_DEV_URL, Environment, KNOWN_KEYS, SecretValue,
    files::{self, TomlDirectory},
    validation::{
        default_allowed_hosts, parse_allowed_hosts, parse_positive_u64, parse_trusted_proxies,
        validate_database_url, validate_origin,
    },
};

#[derive(Clone, Debug)]
enum ConfigFile {
    Discover,
    Exact(PathBuf),
    Disabled,
}

#[derive(Clone, Debug)]
struct SecretRequirement {
    key: String,
    minimum_bytes: usize,
}

/// Controls configuration-file selection, application secrets, and test overrides.
#[derive(Clone, Debug)]
pub struct AppConfigBuilder {
    config_file: ConfigFile,
    toml_directory: TomlDirectory,
    required_secrets: Vec<SecretRequirement>,
    overrides: BTreeMap<String, String>,
}

impl Default for AppConfigBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl AppConfigBuilder {
    #[must_use]
    pub fn new() -> Self {
        Self {
            config_file: ConfigFile::Discover,
            toml_directory: TomlDirectory::Discover,
            required_secrets: Vec::new(),
            overrides: BTreeMap::new(),
        }
    }

    /// Load exactly this dotenv file. Unlike conventional discovery, a missing
    /// explicit file is an error.
    #[must_use]
    pub fn config_file(mut self, path: impl Into<PathBuf>) -> Self {
        self.config_file = ConfigFile::Exact(path.into());
        self
    }

    /// Load Laravel-style TOML files from this `config/` directory.
    #[must_use]
    pub fn config_directory(mut self, path: impl Into<PathBuf>) -> Self {
        self.toml_directory = TomlDirectory::Exact(path.into());
        self
    }

    /// Disable dotenv and `config/*.toml` loading while retaining process
    /// variables and overrides.
    #[must_use]
    pub fn without_config_file(mut self) -> Self {
        self.config_file = ConfigFile::Disabled;
        self.toml_directory = TomlDirectory::Disabled;
        self
    }

    /// Require a named secret in production and enforce its minimum byte length
    /// whenever supplied. The framework does not infer or reuse keys between
    /// JWT, encryption, blind-index, or other application purposes.
    #[must_use]
    pub fn required_secret(mut self, key: impl Into<String>, minimum_bytes: usize) -> Self {
        self.required_secrets.push(SecretRequirement {
            key: key.into(),
            minimum_bytes,
        });
        self
    }

    /// Add the highest-precedence value, intended for tests and explicit process
    /// bootstrap code.
    #[must_use]
    pub fn override_value(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.overrides.insert(key.into(), value.into());
        self
    }

    /// Merge and validate all configured sources.
    ///
    /// Precedence (low → high): `config/*.toml` → `.env` → process environment →
    /// builder overrides. `DATABASE_URL` from `.env`/process wins over TOML; otherwise
    /// the URL is built from `config/database.toml` + `DB_CONNECTION` / `DB_PASSWORD`.
    ///
    /// # Errors
    ///
    /// Returns a source, validation, or production-requirement error.
    pub fn load(self) -> Result<AppConfig, ConfigError> {
        validate_secret_requirements(&self.required_secrets)?;
        let mut values = files::load_toml_defaults(&self.toml_directory)?;
        let mut explicit_keys = BTreeSet::new();

        match &self.config_file {
            ConfigFile::Discover => match dotenvy::dotenv_iter() {
                Ok(iter) => extend_dotenv(&mut values, &mut explicit_keys, iter)?,
                Err(error) if error.not_found() => {}
                Err(error) => return Err(ConfigError::Dotenv(error)),
            },
            ConfigFile::Exact(path) => {
                let iter = dotenvy::from_path_iter(path).map_err(ConfigError::Dotenv)?;
                extend_dotenv(&mut values, &mut explicit_keys, iter)?;
            }
            ConfigFile::Disabled => {}
        }

        let mut environment_keys = KNOWN_KEYS
            .iter()
            .map(|key| (*key).to_owned())
            .collect::<BTreeSet<_>>();
        environment_keys.extend(
            self.required_secrets
                .iter()
                .map(|requirement| requirement.key.clone()),
        );
        for key in environment_keys {
            match env::var(&key) {
                Ok(value) => {
                    values.insert(key.clone(), value);
                    explicit_keys.insert(key);
                }
                Err(env::VarError::NotPresent) => {}
                Err(env::VarError::NotUnicode(_)) => {
                    return Err(ConfigError::NonUnicode(key));
                }
            }
        }
        for key in self.overrides.keys() {
            explicit_keys.insert(key.clone());
        }
        values.extend(self.overrides);
        files::apply_database_overrides(&mut values, &self.toml_directory, &explicit_keys)?;
        AppConfig::from_values(&values, &self.required_secrets)
    }
}

impl AppConfig {
    /// Load the conventional optional `.env` and process environment.
    ///
    /// # Errors
    ///
    /// Returns an error for malformed files, invalid values, or missing
    /// production requirements.
    pub fn load() -> Result<Self, ConfigError> {
        AppConfigBuilder::new().load()
    }

    #[must_use]
    pub fn builder() -> AppConfigBuilder {
        AppConfigBuilder::new()
    }

    fn from_values(
        values: &BTreeMap<String, String>,
        required_secrets: &[SecretRequirement],
    ) -> Result<Self, ConfigError> {
        let environment = setting(values, "APP_ENV")
            .as_deref()
            .unwrap_or("development")
            .parse()?;
        let address = setting(values, "APP_ADDR").unwrap_or_else(|| DEFAULT_ADDRESS.to_owned());
        if address.is_empty() || address.bytes().any(|byte| byte.is_ascii_whitespace()) {
            return Err(ConfigError::invalid(
                "APP_ADDR",
                "expected a non-empty host:port value",
            ));
        }

        let public_url =
            required_in_production(environment, "APP_URL", setting(values, "APP_URL"), || {
                format!("http://{address}")
            })?;
        let public_host = validate_origin("APP_URL", &public_url)?;

        let database_url = required_in_production(
            environment,
            "DATABASE_URL",
            setting(values, "DATABASE_URL"),
            || DEFAULT_DATABASE_URL.to_owned(),
        )?;
        validate_database_url(&database_url)?;

        let vite_dev_url = if environment.is_production() {
            None
        } else {
            let value =
                setting(values, "VITE_DEV_URL").unwrap_or_else(|| DEFAULT_VITE_DEV_URL.to_owned());
            validate_origin("VITE_DEV_URL", &value)?;
            Some(value)
        };

        let proxies = if environment.is_production() {
            setting(values, "TRUSTED_PROXIES")
                .ok_or(ConfigError::MissingProduction("TRUSTED_PROXIES"))?
        } else {
            setting(values, "TRUSTED_PROXIES").unwrap_or_else(|| "none".to_owned())
        };
        let trusted_proxies = parse_trusted_proxies(&proxies)?;

        let allowed_hosts = match setting(values, "ALLOWED_HOSTS") {
            Some(value) => parse_allowed_hosts(&value)?,
            None if environment.is_production() => {
                return Err(ConfigError::MissingProduction("ALLOWED_HOSTS"));
            }
            None => default_allowed_hosts(&public_host),
        };
        if !allowed_hosts.contains(&public_host) {
            return Err(ConfigError::invalid(
                "ALLOWED_HOSTS",
                "must include the host from APP_URL",
            ));
        }

        let rate_limit_requests = parse_positive_u64(
            "RATE_LIMIT_REQUESTS",
            setting(values, "RATE_LIMIT_REQUESTS"),
            DEFAULT_RATE_LIMIT_REQUESTS,
        )?;
        let rate_limit_window = Duration::from_secs(parse_positive_u64(
            "RATE_LIMIT_WINDOW_SECONDS",
            setting(values, "RATE_LIMIT_WINDOW_SECONDS"),
            DEFAULT_RATE_LIMIT_WINDOW_SECONDS,
        )?);
        let secrets = load_secrets(values, environment, required_secrets)?;

        Ok(Self {
            environment,
            address,
            public_url,
            database_url,
            vite_dev_url,
            trusted_proxies: trusted_proxies.into(),
            allowed_hosts: allowed_hosts.into(),
            rate_limit_requests,
            rate_limit_window,
            secrets: Arc::new(secrets),
        })
    }
}

fn extend_dotenv(
    values: &mut BTreeMap<String, String>,
    explicit_keys: &mut BTreeSet<String>,
    entries: impl IntoIterator<Item = dotenvy::Result<(String, String)>>,
) -> Result<(), ConfigError> {
    for entry in entries {
        let (key, value) = entry.map_err(ConfigError::Dotenv)?;
        values.insert(key.clone(), value);
        explicit_keys.insert(key);
    }
    Ok(())
}

fn setting(values: &BTreeMap<String, String>, key: &str) -> Option<String> {
    values
        .get(key)
        .filter(|value| !value.trim().is_empty())
        .cloned()
}

fn required_in_production(
    environment: Environment,
    key: &'static str,
    value: Option<String>,
    default: impl FnOnce() -> String,
) -> Result<String, ConfigError> {
    match value {
        Some(value) => Ok(value),
        None if environment.is_production() => Err(ConfigError::MissingProduction(key)),
        None => Ok(default()),
    }
}

fn validate_secret_requirements(requirements: &[SecretRequirement]) -> Result<(), ConfigError> {
    let mut keys = BTreeSet::new();
    for requirement in requirements {
        let valid_key = !requirement.key.is_empty()
            && requirement
                .key
                .bytes()
                .all(|byte| byte.is_ascii_uppercase() || byte.is_ascii_digit() || byte == b'_');
        if !valid_key || requirement.minimum_bytes == 0 || !keys.insert(&requirement.key) {
            return Err(ConfigError::InvalidSecretRequirement(
                requirement.key.clone(),
            ));
        }
    }
    Ok(())
}

fn load_secrets(
    values: &BTreeMap<String, String>,
    environment: Environment,
    requirements: &[SecretRequirement],
) -> Result<BTreeMap<String, SecretValue>, ConfigError> {
    let mut secrets = BTreeMap::new();
    for requirement in requirements {
        match setting(values, &requirement.key) {
            Some(value) if value.len() >= requirement.minimum_bytes => {
                secrets.insert(
                    requirement.key.clone(),
                    SecretValue(Arc::new(Zeroizing::new(value))),
                );
            }
            Some(_) => {
                return Err(ConfigError::SecretTooShort {
                    key: requirement.key.clone(),
                    minimum_bytes: requirement.minimum_bytes,
                });
            }
            None if environment.is_production() => {
                return Err(ConfigError::MissingRequiredSecret(requirement.key.clone()));
            }
            None => {}
        }
    }
    Ok(secrets)
}

#[cfg(test)]
mod tests {
    use std::{fs, time::SystemTime};

    use super::*;

    fn config(values: &[(&str, &str)]) -> Result<AppConfig, ConfigError> {
        values
            .iter()
            .fold(
                AppConfig::builder().without_config_file(),
                |builder, (key, value)| builder.override_value(*key, *value),
            )
            .load()
    }

    #[test]
    fn development_has_working_local_defaults() {
        let config = config(&[]).unwrap();

        assert_eq!(config.environment(), Environment::Development);
        assert_eq!(config.address(), DEFAULT_ADDRESS);
        assert_eq!(config.database_url(), DEFAULT_DATABASE_URL);
        assert_eq!(config.vite_dev_url(), Some(DEFAULT_VITE_DEV_URL));
        assert!(config.trusted_proxies().is_empty());
        assert!(
            config
                .allowed_hosts()
                .iter()
                .any(|host| host == "localhost")
        );
        assert_eq!(config.rate_limit_requests(), 60);
        assert_eq!(config.rate_limit_window(), Duration::from_mins(1));
    }

    #[test]
    fn production_requires_explicit_network_and_database_boundaries() {
        let missing = config(&[("APP_ENV", "production")]).unwrap_err();
        assert!(matches!(missing, ConfigError::MissingProduction("APP_URL")));

        let config = config(&[
            ("APP_ENV", "production"),
            ("APP_URL", "https://app.example.test"),
            ("DATABASE_URL", "postgresql://database/app"),
            ("TRUSTED_PROXIES", "127.0.0.1,::1"),
            ("ALLOWED_HOSTS", "app.example.test"),
        ])
        .unwrap();
        assert!(config.environment().is_production());
        assert_eq!(config.trusted_proxies().len(), 2);
        assert_eq!(config.allowed_hosts(), ["app.example.test"]);
    }

    #[test]
    fn application_declared_secrets_are_required_and_redacted() {
        let builder = AppConfig::builder()
            .without_config_file()
            .required_secret("JWT_SECRET", 32)
            .override_value("APP_ENV", "production")
            .override_value("APP_URL", "https://app.example.test")
            .override_value("DATABASE_URL", "postgresql://database/app")
            .override_value("TRUSTED_PROXIES", "none")
            .override_value("ALLOWED_HOSTS", "app.example.test");
        assert!(matches!(
            builder.clone().load(),
            Err(ConfigError::MissingRequiredSecret(key)) if key == "JWT_SECRET"
        ));

        let config = builder
            .override_value("JWT_SECRET", "0123456789abcdef0123456789abcdef")
            .load()
            .unwrap();
        assert_eq!(
            config.secret("JWT_SECRET").unwrap().expose(),
            "0123456789abcdef0123456789abcdef"
        );
        assert!(!format!("{config:?}").contains("0123456789abcdef"));
    }

    #[test]
    fn toml_database_connection_selects_pgsql() {
        let id = SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let root = env::temp_dir().join(format!("phoenix-config-dir-{}-{id}", std::process::id()));
        let config_dir = root.join("config");
        fs::create_dir_all(&config_dir).unwrap();
        fs::write(
            config_dir.join("database.toml"),
            r#"
default = "sqlite"

[connections.sqlite]
driver = "sqlite"
database = "storage/app.sqlite"

[connections.pgsql]
driver = "pgsql"
host = "127.0.0.1"
port = 5432
database = "phoenix"
username = "phoenix"
password = ""
"#,
        )
        .unwrap();

        let config = AppConfig::builder()
            .without_config_file()
            .config_directory(&config_dir)
            .override_value("DB_CONNECTION", "pgsql")
            .override_value("DB_PASSWORD", "s3cret")
            .load()
            .unwrap();
        assert_eq!(
            config.database_url(),
            "postgresql://phoenix:s3cret@127.0.0.1:5432/phoenix"
        );
        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn explicit_database_url_wins_over_toml() {
        let id = SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let root = env::temp_dir().join(format!("phoenix-config-url-{}-{id}", std::process::id()));
        let config_dir = root.join("config");
        fs::create_dir_all(&config_dir).unwrap();
        fs::write(
            config_dir.join("database.toml"),
            r#"
default = "pgsql"
[connections.pgsql]
driver = "pgsql"
host = "127.0.0.1"
database = "from-toml"
username = "phoenix"
"#,
        )
        .unwrap();

        let config = AppConfig::builder()
            .without_config_file()
            .config_directory(&config_dir)
            .override_value("DATABASE_URL", "sqlite:storage/override.sqlite")
            .load()
            .unwrap();
        assert_eq!(config.database_url(), "sqlite:storage/override.sqlite");
        fs::remove_dir_all(root).unwrap();
    }
}