Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@ jobs:
- uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5
- run: sudo apt-get update -qq && sudo apt-get install -y -qq shellcheck
- run: shellcheck deploy/*.sh scripts/*.sh
- run: bash deploy/test-release-preflight.sh
- run: scripts/check-pins.sh
163 changes: 163 additions & 0 deletions crates/outer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,28 @@ impl Config {
self.tls_cert.is_some() && self.tls_key.is_some() && self.origin_pull_ca.is_some()
}

/// Boot-time file checks shared by `serve` and `check-config`: every file
/// the config points at that `serve` must read to listen (the TLS triple)
/// is read and parsed here first. Pure reads — no DB, no network, no
/// sockets — and every failure names the variable, never its value.
/// `validate` (above) already covers presence and consistency plus the
/// `OUTER_REVIEW_LOGINS_FILE` read; this covers readability, which used
/// to fail only at boot, after the old container was already stopping.
pub fn boot_file_problems(&self) -> Vec<String> {
let mut problems = Vec::new();
if self.tls_enabled() {
let files = crate::http::tls::TlsFiles {
cert: self.tls_cert.clone().expect("tls triple is set"),
key: self.tls_key.clone().expect("tls triple is set"),
ca: self.origin_pull_ca.clone().expect("tls triple is set"),
};
if let Err(err) = crate::http::tls::load_server_config(&files) {
problems.push(format!("{err:#}"));
}
}
problems
}

/// True when the platform oxfmt binary exists and is executable.
/// Formatting is skipped (commits still work) when this is false.
pub fn oxfmt_available(&self) -> bool {
Expand Down Expand Up @@ -1845,4 +1867,145 @@ mod tests {
}
}
}
/// Minimal production vars: everything `validate` requires off localhost.
/// Every secret carries `NEVER-LEAK` so tests can assert errors name the
/// variable without echoing its value.
fn prod_vars(extra: &[(&str, &str)]) -> BTreeMap<String, String> {
let mut base = vec![
("OUTER_DOMAIN", "prod.example"),
("OUTER_STORE", "r2"),
(
"OUTER_R2_ENDPOINT",
"https://acct.eu.r2.cloudflarestorage.com",
),
("OUTER_R2_BUCKET", "outer"),
("R2_ACCESS_KEY_ID", "r2-key-NEVER-LEAK-1"),
("R2_SECRET_ACCESS_KEY", "r2-secret-NEVER-LEAK-2"),
("CLOUDFLARE_ACCOUNT_ID", "cf-acct"),
("CLOUDFLARE_API_TOKEN", "cf-token-NEVER-LEAK-3"),
("STRIPE_SECRET_KEY", "sk-live-NEVER-LEAK-4"),
("STRIPE_WEBHOOK_SECRET", "whsec-NEVER-LEAK-5"),
("STRIPE_PRICE_PRO", "price-NEVER-LEAK-6"),
("OUTER_TERMS_VERSION", "2026-11-14"),
("OUTER_SECRET", "outer-secret-NEVER-LEAK-7"),
];
base.extend_from_slice(extra);
vars(&base)
}

fn assert_no_secrets(problems: &[String]) {
let shown = problems.join("\n");
assert!(
!shown.contains("NEVER-LEAK"),
"a secret value leaked: {shown}"
);
assert!(!shown.contains("$argon2"), "a hash leaked: {shown}");
}

#[test]
fn boot_files_accept_localhost_without_tls() {
let cfg = Config::from_vars(&vars(&[("OUTER_STORE", "memory")])).unwrap();
assert!(cfg.boot_file_problems().is_empty());
}

#[test]
fn boot_files_refuse_missing_tls_files_without_echoing_values() {
let dir = tempfile::tempdir().unwrap();
let missing = |name: &str| dir.path().join(name).to_string_lossy().into_owned();
let cfg = Config::from_vars(&prod_vars(&[
("OUTER_TLS_CERT", missing("origin.pem").as_str()),
("OUTER_TLS_KEY", missing("origin.key").as_str()),
(
"OUTER_ORIGIN_PULL_CA",
missing("origin-pull-ca.pem").as_str(),
),
]))
.unwrap();
let problems = cfg.boot_file_problems();
assert_eq!(problems.len(), 1, "{problems:?}");
assert!(problems[0].contains("OUTER_TLS_CERT"), "{problems:?}");
assert_no_secrets(&problems);
}

#[test]
fn boot_files_refuse_garbage_tls_pem_without_echoing_values() {
let dir = tempfile::tempdir().unwrap();
for name in ["origin.pem", "origin.key", "origin-pull-ca.pem"] {
std::fs::write(dir.path().join(name), "not pem at all\n").unwrap();
}
let path = |name: &str| dir.path().join(name).to_string_lossy().into_owned();
let cfg = Config::from_vars(&prod_vars(&[
("OUTER_TLS_CERT", path("origin.pem").as_str()),
("OUTER_TLS_KEY", path("origin.key").as_str()),
("OUTER_ORIGIN_PULL_CA", path("origin-pull-ca.pem").as_str()),
]))
.unwrap();
let problems = cfg.boot_file_problems();
assert_eq!(problems.len(), 1, "{problems:?}");
assert!(problems[0].contains("OUTER_TLS_CERT"), "{problems:?}");
assert_no_secrets(&problems);
}

#[test]
fn boot_files_accept_a_valid_tls_triple() {
let dir = tempfile::tempdir().unwrap();
let mut ca_params = rcgen::CertificateParams::new(vec!["test CA".to_string()]).unwrap();
ca_params.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained);
let ca_key = rcgen::KeyPair::generate().unwrap();
let ca_cert = ca_params.self_signed(&ca_key).unwrap();
let issuer = rcgen::Issuer::new(ca_params, ca_key);
let params = rcgen::CertificateParams::new(vec!["outer.test".to_string()]).unwrap();
let key = rcgen::KeyPair::generate().unwrap();
let cert = params.signed_by(&key, &issuer).unwrap();
let blobs = [
("OUTER_TLS_CERT", "origin.pem", cert.pem().into_bytes()),
(
"OUTER_TLS_KEY",
"origin.key",
key.serialize_pem().into_bytes(),
),
(
"OUTER_ORIGIN_PULL_CA",
"origin-pull-ca.pem",
ca_cert.pem().into_bytes(),
),
];
let mut map = prod_vars(&[]);
for (var, name, bytes) in &blobs {
let file = dir.path().join(name);
std::fs::write(&file, bytes).unwrap();
map.insert(var.to_string(), file.to_string_lossy().into_owned());
}
let cfg = Config::from_vars(&map).unwrap();
assert!(cfg.boot_file_problems().is_empty(), "{cfg:?}");
}

#[test]
fn config_errors_never_echo_secret_values() {
// Missing Stripe off localhost plus a malformed review-logins entry:
// every problem must name its variable while echoing no value.
let dir = tempfile::tempdir().unwrap();
let logins = dir.path().join("review-logins");
std::fs::write(&logins, "review@example.com:not-a-phc-NEVER-LEAK-8\n").unwrap();
let mut map = prod_vars(&[
("OUTER_TLS_CERT", "/tls/origin.pem"),
("OUTER_TLS_KEY", "/tls/origin.key"),
("OUTER_ORIGIN_PULL_CA", "/tls/origin-pull-ca.pem"),
(
"OUTER_REVIEW_LOGINS_FILE",
logins.to_string_lossy().as_ref(),
),
]);
map.remove("STRIPE_SECRET_KEY");
let err = Config::from_vars(&map).unwrap_err();
assert!(
err.iter().any(|p| p.contains("STRIPE_SECRET_KEY")),
"{err:?}"
);
assert!(
err.iter().any(|p| p.contains("OUTER_REVIEW_LOGINS_FILE")),
"{err:?}"
);
assert_no_secrets(&err);
}
}
70 changes: 67 additions & 3 deletions crates/outer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,26 @@ async fn main() -> ExitCode {
let Some(config) = load_config() else {
return ExitCode::from(2);
};
// The same file validation `check-config` runs, before
// `prepare_host` creates dirs and chmods: refuse an unreadable
// TLS triple before touching the host.
if !report_boot_files(&config) {
return ExitCode::from(2);
}
init_tracing(&config.log_filter);
if let Err(err) = prepare_host(&config) {
return report(Err(err));
}
report(serve(config).await)
}
["check-config"] => match load_config() {
Some(_) => {
println!("configuration ok");
ExitCode::SUCCESS
Some(config) => {
if report_boot_files(&config) {
println!("configuration ok");
ExitCode::SUCCESS
} else {
ExitCode::from(2)
}
}
None => ExitCode::from(2),
},
Expand Down Expand Up @@ -173,6 +183,21 @@ fn load_config() -> Option<Config> {
}
}

/// The one shared boot file check: print `Config::boot_file_problems` and
/// report whether the config is bootable. `serve` runs this before
/// `prepare_host` touches the host; `check-config` is this and nothing else.
fn report_boot_files(config: &Config) -> bool {
let problems = config.boot_file_problems();
if problems.is_empty() {
return true;
}
eprintln!("outer: invalid configuration:");
for problem in problems {
eprintln!(" - {problem}");
}
false
}

fn build_store(config: &Config) -> anyhow::Result<Store> {
match &config.store {
StoreKind::Memory => {
Expand Down Expand Up @@ -1212,3 +1237,42 @@ async fn drain_and_wait(pool: sqlx::SqlitePool, drain: Option<Arc<std::sync::ato
shutdown_signal().await;
outer::handoff::drain_and_wait(&pool, drain, shutdown_signal()).await;
}

#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeMap;

fn vars(pairs: &[(&str, &str)]) -> BTreeMap<String, String> {
pairs
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect()
}

#[test]
fn boot_gate_passes_localhost_and_refuses_garbage_tls() {
// The gate both `serve` (before `prepare_host`) and `check-config`
// share: localhost without TLS passes, an unreadable triple fails.
// Presence alone is not enough — `validate` accepts a set triple
// and only this gate reads it.
let ok = Config::from_vars(&vars(&[("OUTER_STORE", "memory")])).unwrap();
assert!(report_boot_files(&ok));
let dir = tempfile::tempdir().unwrap();
let cert = dir.path().join("origin.pem");
std::fs::write(&cert, "not pem at all\n").unwrap();
let missing = dir
.path()
.join("missing.pem")
.to_string_lossy()
.into_owned();
let bad = Config::from_vars(&vars(&[
("OUTER_STORE", "memory"),
("OUTER_TLS_CERT", cert.to_string_lossy().as_ref()),
("OUTER_TLS_KEY", missing.as_str()),
("OUTER_ORIGIN_PULL_CA", missing.as_str()),
]))
.unwrap();
assert!(!report_boot_files(&bad));
}
}
4 changes: 4 additions & 0 deletions deploy/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ services:
outer:
image: ${OUTER_IMAGE:?set OUTER_IMAGE in /srv/outer/release.env (deploy/release.sh writes it)}
restart: unless-stopped
# The image is local-only (deploy/release.sh streams it in with
# save|load): never pull, so a missing image fails closed instead of
# running a same-named registry image with the production env.
pull_policy: never
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
network_mode: host
# Above outer's stop budget: on SIGTERM outer refuses new deploys and
# waits out the in-flight ones (a release handoff drains this way), and
Expand Down
2 changes: 1 addition & 1 deletion deploy/handoff.sh
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ handoff_to() {
docker rm -f outer-next >/dev/null 2>&1 || true
# Same volumes, env, caps and host network as the compose service
# (deploy/compose.yaml): the newcomer is the service, started by hand.
docker run -d --name outer-next --network host \
docker run -d --pull=never --name outer-next --network host \
--cap-add NET_ADMIN --cap-add SETUID --cap-add SETGID --cap-add CHOWN --cap-add SYS_ADMIN \
--security-opt apparmor:unconfined \
--env-file /srv/outer/outer.env \
Expand Down
Loading
Loading