Skip to content

Commit 9a6ba74

Browse files
authored
Use env variables during build
1 parent faa87a9 commit 9a6ba74

File tree

7 files changed

+126
-49
lines changed

7 files changed

+126
-49
lines changed

Cargo.lock

Lines changed: 30 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/common/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ brotli = "3.3"
1313
chrono = "0.4"
1414
config = "0.15.11"
1515
cookie = "0.18.1"
16-
derive_more = { version = "1.0", features = ["display", "error"] }
16+
derive_more = { version = "2.0", features = ["display", "error"] }
1717
error-stack = "0.5"
1818
fastly = "0.11.5"
1919
futures = "0.3"
@@ -35,9 +35,10 @@ urlencoding = "2.1"
3535
serde = { version = "1.0", features = ["derive"] }
3636
serde_json = "1.0.91"
3737
config = "0.15.11"
38-
derive_more = { version = "1.0", features = ["display", "error"] }
38+
derive_more = { version = "2.0", features = ["display", "error"] }
3939
error-stack = "0.5"
4040
http = "1.3.1"
41+
toml = "0.9.0"
4142

4243
[dev-dependencies]
4344
regex = "1.1.1"

crates/common/build.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@ mod settings;
66

77
use serde_json::Value;
88
use std::collections::HashSet;
9+
use std::fs;
10+
use std::path::Path;
11+
12+
const TRUSTED_SERVER_INIT_CONFIG_PATH: &str = "../../trusted-server.toml";
13+
const TRUSTED_SERVER_OUTPUT_CONFIG_PATH: &str = "../../target/trusted-server-out.toml";
914

1015
fn main() {
11-
// Watch the settings.rs file for changes
12-
println!("cargo:rerun-if-changed=../../trusted-server.toml");
16+
merge_toml();
17+
rerun_if_changed();
18+
}
19+
20+
fn rerun_if_changed() {
21+
// Watch the root trusted-server.toml file for changes
22+
println!("cargo:rerun-if-changed={}", TRUSTED_SERVER_INIT_CONFIG_PATH);
1323

1424
// Create a default Settings instance and convert to JSON to discover all fields
1525
let default_settings = settings::Settings::default();
@@ -27,6 +37,26 @@ fn main() {
2737
}
2838
}
2939

40+
fn merge_toml() {
41+
// Get the OUT_DIR where we'll copy the config file
42+
let dest_path = Path::new(TRUSTED_SERVER_OUTPUT_CONFIG_PATH);
43+
44+
// Read init config
45+
let init_config_path = Path::new(TRUSTED_SERVER_INIT_CONFIG_PATH);
46+
let toml_content = fs::read_to_string(init_config_path)
47+
.unwrap_or_else(|_| panic!("Failed to read {:?}", init_config_path));
48+
49+
// For build time: use from_toml to parse with environment variables
50+
let settings = settings::Settings::from_toml(&toml_content)
51+
.expect("Failed to parse settings at build time");
52+
53+
// Write the merged settings to the output directory as TOML
54+
let merged_toml =
55+
toml::to_string_pretty(&settings).expect("Failed to serialize settings to TOML");
56+
57+
fs::write(dest_path, merged_toml).unwrap_or_else(|_| panic!("Failed to write {:?}", dest_path));
58+
}
59+
3060
fn collect_env_vars(value: &Value, env_vars: &mut HashSet<String>, path: Vec<String>) {
3161
if let Value::Object(map) = value {
3262
for (key, val) in map {

crates/common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub mod prebid;
3232
pub mod privacy;
3333
pub mod publisher;
3434
pub mod settings;
35+
pub mod settings_data;
3536
pub mod synthetic;
3637
pub mod templates;
3738
pub mod test_support;

crates/common/src/settings.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -126,26 +126,6 @@ mod tests {
126126

127127
use crate::test_support::tests::crate_test_settings_str;
128128

129-
#[test]
130-
fn test_settings_new() {
131-
// Test that Settings::new() loads successfully
132-
let settings = Settings::new();
133-
assert!(settings.is_ok(), "Settings should load from embedded TOML");
134-
135-
let settings = settings.unwrap();
136-
// Verify basic structure is loaded
137-
assert!(!settings.ad_server.ad_partner_backend.is_empty());
138-
assert!(!settings.ad_server.sync_url.is_empty());
139-
assert!(!settings.publisher.domain.is_empty());
140-
assert!(!settings.publisher.cookie_domain.is_empty());
141-
assert!(!settings.publisher.origin_url.is_empty());
142-
assert!(!settings.prebid.server_url.is_empty());
143-
assert!(!settings.synthetic.counter_store.is_empty());
144-
assert!(!settings.synthetic.opid_store.is_empty());
145-
assert!(!settings.synthetic.secret_key.is_empty());
146-
assert!(!settings.synthetic.template.is_empty());
147-
}
148-
149129
#[test]
150130
fn test_settings_from_valid_toml() {
151131
let toml_str = crate_test_settings_str();

crates/common/src/settings_data.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

crates/fastly/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ use trusted_server_common::prebid::handle_prebid_test;
1515
use trusted_server_common::privacy::handle_privacy_policy;
1616
use trusted_server_common::publisher::handle_main_page;
1717
use trusted_server_common::settings::Settings;
18+
use trusted_server_common::settings_data::get_settings;
1819
use trusted_server_common::templates::GAM_TEST_TEMPLATE;
1920
use trusted_server_common::why::handle_why_trusted_server;
2021

2122
#[fastly::main]
2223
fn main(req: Request) -> Result<Response, Error> {
2324
init_logger();
2425

25-
let settings = match Settings::new() {
26+
let settings = match get_settings() {
2627
Ok(s) => s,
2728
Err(e) => {
2829
log::error!("Failed to load settings: {:?}", e);

0 commit comments

Comments
 (0)