Skip to content

In production, panic if email environment variables aren't set #4596

@carols10cents

Description

@carols10cents

We had some reports that email verification emails weren't arriving. Our best suspicion of the cause is that somehow an environment variable wasn't set (there weren't any config changes logged on the server, so we're not sure how that could have happened).

We should always have email configured in production, so this code:

crates.io/src/email.rs

Lines 17 to 36 in dd9893b

/// Create a new instance detecting the backend from the environment. This will either connect
/// to a SMTP server or store the emails on the local filesystem.
pub fn from_environment() -> Self {
let backend = match (
dotenv::var("MAILGUN_SMTP_LOGIN"),
dotenv::var("MAILGUN_SMTP_PASSWORD"),
dotenv::var("MAILGUN_SMTP_SERVER"),
) {
(Ok(login), Ok(password), Ok(server)) => EmailBackend::Smtp {
server,
login,
password,
},
_ => EmailBackend::FileSystem {
path: "/tmp".into(),
},
};
Self { backend }
}

should never fall back to EmailBackend::FileSystem in production, it should panic. The Env enum can be used for this:

crates.io/src/lib.rs

Lines 60 to 71 in dd9893b

/// Used for setting different values depending on whether the app is being run in production,
/// in development, or for testing.
///
/// The app's `config.env` value is set in *src/bin/server.rs* to `Production` if the environment
/// variable `HEROKU` is set and `Development` otherwise. `config.env` is set to `Test`
/// unconditionally in *src/test/all.rs*.
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum Env {
Development,
Test,
Production,
}

cc @rust-lang/crates-io-on-call, this would be super helpful for production support.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-backend ⚙️C-internal 🔧Category: Nonessential work that would make the codebase more consistent or clear

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions