|
| 1 | +use core::str; |
| 2 | +use error_stack::{Report, ResultExt}; |
| 3 | + |
| 4 | +use crate::error::TrustedServerError; |
| 5 | +use crate::settings::Settings; |
| 6 | + |
| 7 | +const SETTINGS_DATA: &[u8] = include_bytes!("../../../target/trusted-server-out.toml"); |
| 8 | + |
| 9 | +/// Creates a new [`Settings`] instance from the embedded configuration file. |
| 10 | +// / |
| 11 | +// / Loads the configuration from the embedded `trusted-server.toml` file |
| 12 | +// / and applies any environment variable overrides. |
| 13 | +// / |
| 14 | +// / # Errors |
| 15 | +// / |
| 16 | +// / - [`TrustedServerError::InvalidUtf8`] if the embedded TOML file contains invalid UTF-8 |
| 17 | +// / - [`TrustedServerError::Configuration`] if the configuration is invalid or missing required fields |
| 18 | +// / - [`TrustedServerError::InsecureSecretKey`] if the secret key is set to the default value |
| 19 | +pub fn get_settings() -> Result<Settings, Report<TrustedServerError>> { |
| 20 | + let toml_bytes = SETTINGS_DATA; |
| 21 | + let toml_str = str::from_utf8(toml_bytes).change_context(TrustedServerError::InvalidUtf8 { |
| 22 | + message: "embedded trusted-server.toml file".to_string(), |
| 23 | + })?; |
| 24 | + |
| 25 | + let settings = Settings::from_toml(toml_str)?; |
| 26 | + |
| 27 | + // Validate that the secret key is not the default |
| 28 | + if settings.synthetic.secret_key == "secret-key" { |
| 29 | + return Err(Report::new(TrustedServerError::InsecureSecretKey)); |
| 30 | + } |
| 31 | + |
| 32 | + Ok(settings) |
| 33 | +} |
| 34 | + |
| 35 | +#[cfg(test)] |
| 36 | +mod tests { |
| 37 | + use super::*; |
| 38 | + |
| 39 | + #[test] |
| 40 | + fn test_get_settings() { |
| 41 | + // Test that Settings::new() loads successfully |
| 42 | + let settings = get_settings(); |
| 43 | + assert!(settings.is_ok(), "Settings should load from embedded TOML"); |
| 44 | + |
| 45 | + let settings = settings.unwrap(); |
| 46 | + // Verify basic structure is loaded |
| 47 | + assert!(!settings.ad_server.ad_partner_backend.is_empty()); |
| 48 | + assert!(!settings.ad_server.sync_url.is_empty()); |
| 49 | + assert!(!settings.publisher.domain.is_empty()); |
| 50 | + assert!(!settings.publisher.cookie_domain.is_empty()); |
| 51 | + assert!(!settings.publisher.origin_url.is_empty()); |
| 52 | + assert!(!settings.prebid.server_url.is_empty()); |
| 53 | + assert!(!settings.synthetic.counter_store.is_empty()); |
| 54 | + assert!(!settings.synthetic.opid_store.is_empty()); |
| 55 | + assert!(!settings.synthetic.secret_key.is_empty()); |
| 56 | + assert!(!settings.synthetic.template.is_empty()); |
| 57 | + } |
| 58 | +} |
0 commit comments