# How do I make my program refuse to start when a secret is missing or weak?
Source: https://ruststudio.org/community/t/72/how-do-i-make-my-program-refuse-to-start-when-a-secret-is-missing-or-weak
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, Windows 11
Category: Help and questions | Kind: FAQ | Tags: security, configuration, secrets | Views: 0

## Short answer
Validate configuration once at start-up and exit with an error if a secret is missing, too short or a known placeholder. Collect every problem into one message so the operator fixes them together. A service that starts with a weak key looks healthy while being unsafe.

## Answer
## Answer
Failing at start-up is the cheapest place to find a configuration mistake: the operator sees it at once, and no user is ever served by a half-configured service. The opposite, a silent default, turns a typing mistake into a security hole that nobody notices.

Write one `validate` function that turns the raw values into a `Settings` struct, or a list of everything that is wrong. Only the validated struct is passed on to the rest of the program.

```rust title=src/main.rs
const PLACEHOLDERS: [&str; 3] = ["changeme", "secret", "password"];

struct Settings {
    signing_key: String,
}

/// Collect every problem, so one start-up attempt reports all of them.
fn validate(signing_key: Option<&str>) -> Result<Settings, Vec<String>> {
    let mut problems = Vec::new();
    match signing_key {
        None => problems.push("SIGNING_KEY is not set".to_string()),
        Some(k) => {
            if PLACEHOLDERS.contains(&k.to_lowercase().as_str()) {
                problems.push("SIGNING_KEY is a placeholder value".to_string());
            }
            if k.len() < 32 {
                problems.push("SIGNING_KEY must be at least 32 bytes".to_string());
            }
        }
    }
    match signing_key {
        Some(k) if problems.is_empty() => Ok(Settings { signing_key: k.to_string() }),
        _ => Err(problems),
    }
}

fn main() {
    for input in [None, Some("changeme"), Some("0123456789abcdef0123456789abcdef")] {
        match validate(input) {
            Ok(s) => println!("ok, key is {} bytes", s.signing_key.len()),
            Err(list) => println!("refusing to start: {}", list.join("; ")),
        }
    }
}
```

```text
refusing to start: SIGNING_KEY is not set
refusing to start: SIGNING_KEY is a placeholder value; SIGNING_KEY must be at least 32 bytes
ok, key is 32 bytes
```

The messages name the setting and the rule. They never print the value that was given.

## How to do it
1. Decide minimum rules for each secret: present, minimum length, not on a short list of placeholders.
2. Run the validation before opening any port or database connection.
3. Print the problems to standard error and exit with a non-zero status (`std::process::exit(1)`).
4. Generate real keys with a random generator, not by typing them; see the thread on random tokens and keys.

## Common mistakes
- Length checks only. A 40-character `aaaa...` passes. A length rule catches typos, not weak choices; generate secrets, do not invent them.
- Validating only some settings and reading others later with `unwrap`. A missing value then panics in the middle of a request.
- Logging the rejected value "to help". Name the setting; keep the value out of the log.

## Related questions
### Should development and production use different rules?
They can differ in where the value comes from, not in whether it is checked. A program that skips checks in development tends to ship that mode by accident.

### Can a missing optional setting have a default?
Yes, for settings that are not security relevant (a port, a page size). Secrets, signing keys and database passwords should never have a default.

## Sources
- OWASP Secrets Management Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html
- The Twelve-Factor App, "Config": https://12factor.net/config
