Skip to content
Open
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
25 changes: 21 additions & 4 deletions app/src/billing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,30 @@ impl Stripe {
}
}

/// At boot: production never runs on a test key (`sk_test_`, `rk_test_`), which would take no real payments while
/// granting real plans and credits.
/// At boot: production needs Stripe's key and webhook secret, and never runs on a test key (`sk_test_`, `rk_test_`),
/// which would take no real payments while granting real plans and credits.
///
/// # Errors
/// In production, when the Stripe key isn't a live one.
/// In production, when either is missing or the key isn't a live one.
pub fn check(stripe: &Stripe, production: bool) -> laravel::Result<()> {
if production && stripe.live() == Some(false) {
if !production {
return Ok(());
}
let set = |v: &Option<String>| v.as_deref().is_some_and(|s| !s.trim().is_empty());
let missing: Vec<&str> =
[("STRIPE_SECRET_KEY", &stripe.secret_key), ("STRIPE_WEBHOOK_SECRET", &stripe.webhook_secret)]
.into_iter()
.filter(|(_, v)| !set(v))
.map(|(name, _)| name)
.collect();
if !missing.is_empty() {
return Err(laravel::Error::msg(format!(
"Stripe isn't configured: {} not set. Production needs both: without them nobody can pay, and Stripe's \
events (payments, subscriptions) are refused.",
missing.join(", ")
)));
}
if stripe.live() == Some(false) {
return Err(laravel::Error::msg(
"STRIPE_SECRET_KEY is a test key; production needs a live one (sk_live_ or rk_live_)",
));
Expand Down
34 changes: 34 additions & 0 deletions app/src/forge/github_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,40 @@ pub struct GithubApp {
pub webhook_secret: Option<String>,
}

/// At boot: production runs only with GitHub fully configured. Sign-in needs the OAuth application
/// (`GITHUB_CLIENT_ID`, `GITHUB_CLIENT_SECRET`), and checks, pull request comments and the webhook need the App
/// (`GITHUB_APP_ID`, `GITHUB_APP_PRIVATE_KEY_BASE64`, `GITHUB_WEBHOOK_SECRET`). Without them the app would start
/// with no way to sign in and nothing reported to GitHub, and say so nowhere (3 Dec 2026: the cutover's manifests
/// read the wrong Secret keys, and the sign-in page offered no forge).
///
/// # Errors
/// In production, naming each setting that's missing.
pub fn check(app: &GithubApp, apps: &crate::auth::sign_in::ForgeApps, production: bool) -> laravel::Result<()> {
if !production {
return Ok(());
}
let set = |v: &Option<String>| v.as_deref().is_some_and(|s| !s.trim().is_empty());
let missing: Vec<&str> = [
("GITHUB_CLIENT_ID", &apps.github),
("GITHUB_CLIENT_SECRET", &apps.github_secret),
("GITHUB_APP_ID", &app.app_id),
("GITHUB_APP_PRIVATE_KEY_BASE64", &app.private_key_base64),
("GITHUB_WEBHOOK_SECRET", &app.webhook_secret),
]
.into_iter()
.filter(|(_, v)| !set(v))
.map(|(name, _)| name)
.collect();
if missing.is_empty() {
return Ok(());
}
Err(laravel::Error::msg(format!(
"GitHub isn't configured: {} not set. Production needs all five: without them nobody can sign in, and no \
check or pull request comment reaches GitHub.",
missing.join(", ")
)))
}

fn err(e: impl std::fmt::Display) -> laravel::Error {
laravel::Error::msg(format!("GitHub: {e}"))
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ pub fn configure() -> ApplicationBuilder {
billing::check(config.section::<billing::Stripe>(), config.app.env.is_production())?;
http::security::check(config, config.app.env.is_production())?;
config.section::<auth::sign_in::DevLogin>().check(&config.app.env)?;
notify::postmark::check(config.section::<notify::postmark::Postmark>(), config.app.env.is_production())?;
forge::github_app::check(
config.section::<forge::github_app::GithubApp>(),
config.section::<auth::sign_in::ForgeApps>(),
config.app.env.is_production(),
)?;
Ok(())
})
.with_middleware(|middleware, _| {
Expand Down
14 changes: 14 additions & 0 deletions app/src/notify/postmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ pub struct Postmark {
pub reply_to: String,
}

/// At boot: production sends email, so it needs `POSTMARK_SERVER_TOKEN`. Without it, every email (invitations,
/// receipts, sign-in notices) would wait in the outbox for good, and say so nowhere.
///
/// # Errors
/// In production, when the token isn't set.
pub fn check(postmark: &Postmark, production: bool) -> laravel::Result<()> {
if production && postmark.token.as_deref().is_none_or(|t| t.trim().is_empty()) {
return Err(laravel::Error::msg(
"POSTMARK_SERVER_TOKEN isn't set: production needs it, or no email is ever sent",
));
}
Ok(())
}

/// Send one email with its text and HTML bodies.
///
/// # Errors
Expand Down
6 changes: 5 additions & 1 deletion app/tests/billing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,15 @@ async fn only_admins_check_out_and_get_a_stripe_url(app: Application) -> laravel

#[test]
fn production_never_runs_on_a_test_key() {
let stripe = |key: &str| app::billing::Stripe { secret_key: Some(key.into()), webhook_secret: None };
let stripe =
|key: &str| app::billing::Stripe { secret_key: Some(key.into()), webhook_secret: Some("whsec_x".into()) };
assert!(app::billing::check(&stripe("sk_test_x"), true).is_err());
assert!(app::billing::check(&stripe("rk_test_x"), true).is_err());
assert!(app::billing::check(&stripe("rk_live_x"), true).is_ok());
assert!(app::billing::check(&stripe("sk_test_x"), false).is_ok());
let unset = app::billing::Stripe { secret_key: None, webhook_secret: None };
assert!(app::billing::check(&unset, true).is_err(), "production needs Stripe");
assert!(app::billing::check(&unset, false).is_ok());
}

fn stripe_test_key() -> Application {
Expand Down
42 changes: 41 additions & 1 deletion app/tests/hosts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ fn in_production() -> laravel::foundation::ApplicationBuilder {
("S3_SECRET_ACCESS_KEY", "visualci-dev-secret"),
("RENDERER_TOKEN", "renderer-test-token"),
("RENDERER_VERSION", "1"),
("GITHUB_CLIENT_ID", "Iv1.test"),
("GITHUB_CLIENT_SECRET", "github-test-secret"),
("GITHUB_APP_ID", "1"),
("GITHUB_APP_PRIVATE_KEY_BASE64", "dGVzdA=="),
("GITHUB_WEBHOOK_SECRET", "github-webhook-test-secret"),
("STRIPE_SECRET_KEY", "rk_live_test"),
("STRIPE_WEBHOOK_SECRET", "whsec_test"),
("POSTMARK_SERVER_TOKEN", "postmark-test-token"),
])
}

Expand Down Expand Up @@ -159,7 +167,7 @@ async fn every_response_carries_the_security_headers(app: Application) {
}

/// The development sign-in can't exist in production: production won't boot with DEV_LOGIN on, and without it the
/// route isn't there (a 404, not a refusal) and the providers don't offer it.
/// route isn't there (a 405: only GET /auth/{forge} matches the path) and the providers don't offer it.
#[tokio::test(flavor = "multi_thread")]
async fn production_has_no_development_sign_in() {
let hosts = [
Expand Down Expand Up @@ -190,3 +198,35 @@ async fn production_has_no_development_sign_in() {
// No such route: only the sign-in start, GET /auth/{forge}, matches the path
.assert_status(405);
}

#[tokio::test(flavor = "multi_thread")]
async fn production_needs_github_stripe_and_email_configured() {
let hosts = [
("VISUALCI_SITE_URL", "https://visualci.test"),
("VISUALCI_APP_URL", "https://app.visualci.test"),
("VISUALCI_API_URL", "https://api.visualci.test"),
("VISUALCI_MCP_URL", "https://mcp.visualci.test"),
("OAUTH_ISSUER", "https://mcp.visualci.test"),
("OAUTH_AUTHORIZATION_ENDPOINT", "https://app.visualci.test/oauth/authorize"),
];
// The cutover's mistake: the GitHub settings empty, as an optional secret key that doesn't exist leaves them
let unset = in_production().with_envs(hosts).with_envs([("GITHUB_CLIENT_ID", ""), ("GITHUB_WEBHOOK_SECRET", "")]);
let refused = unset.create().boot().await.expect_err("production doesn't boot without GitHub");
let message = format!("{refused:?}");
assert!(message.contains("GITHUB_CLIENT_ID, GITHUB_WEBHOOK_SECRET not set"), "{message}");

// Stripe and Postmark the same way
for name in ["STRIPE_SECRET_KEY", "STRIPE_WEBHOOK_SECRET", "POSTMARK_SERVER_TOKEN"] {
let refused = in_production().with_envs(hosts).with_env(name, "").create().boot().await;
let message = format!("{:?}", refused.expect_err("production doesn't boot without it"));
assert!(message.contains(name), "{name}: {message}");
}

let app = in_production().with_envs(hosts).create();
app.boot().await.expect("it boots with all five");
app.get("/auth/providers")
.header("host", "app.visualci.test")
.await
.assert_ok()
.assert_json_path("forges.0", "github");
}
Loading