Skip to main content

systemprompt_config/bootstrap/secrets/
mod.rs

1//! Process-wide secrets bootstrap.
2//!
3//! Loads the secrets document referenced by the active profile (or
4//! the equivalent environment variables in subprocess/Fly.io modes),
5//! validates required fields, and exposes typed accessors for the
6//! manifest signing seed and database URLs.
7
8mod io;
9mod loader;
10mod logging;
11
12use std::path::{Path, PathBuf};
13use std::sync::OnceLock;
14
15use base64::Engine;
16use systemprompt_models::profile::resolve_with_home;
17use systemprompt_models::secrets::Secrets;
18
19use super::manifest::{
20    MANIFEST_SIGNING_SEED_BYTES, decode_seed, dir_is_writable, generate_seed, persist_seed,
21};
22use super::profile::ProfileBootstrap;
23use crate::error::{ConfigError, ConfigResult};
24
25pub use io::{handle_load_error, load_secrets_from_path};
26pub use logging::{
27    build_loaded_secrets_message, log_secrets_issue, log_secrets_skip, log_secrets_warn,
28};
29
30static SECRETS: OnceLock<Secrets> = OnceLock::new();
31
32#[derive(Debug, Clone, Copy)]
33pub struct SecretsBootstrap;
34
35#[derive(Debug, thiserror::Error)]
36#[non_exhaustive]
37pub enum SecretsBootstrapError {
38    #[error(
39        "Secrets not initialized. Call SecretsBootstrap::init() after ProfileBootstrap::init()"
40    )]
41    NotInitialized,
42
43    #[error("Secrets already initialized")]
44    AlreadyInitialized,
45
46    #[error("Profile not initialized. Call ProfileBootstrap::init() first")]
47    ProfileNotInitialized,
48
49    #[error("Secrets file not found: {path}")]
50    FileNotFound { path: String },
51
52    #[error("Invalid secrets file: {message}")]
53    InvalidSecretsFile { message: String },
54
55    #[error("No secrets configured. Create a secrets.json file.")]
56    NoSecretsConfigured,
57
58    #[error(
59        "OAuth at-rest pepper is required. Add 'oauth_at_rest_pepper' (>= 32 chars) to your \
60         secrets file or set OAUTH_AT_REST_PEPPER environment variable."
61    )]
62    OauthAtRestPepperRequired,
63
64    #[error(
65        "Database URL is required. Add 'database_url' to your secrets.json or set DATABASE_URL \
66         environment variable."
67    )]
68    DatabaseUrlRequired,
69
70    #[error(
71        "manifest_signing_secret_seed is missing from the secrets file and the bootstrap path is \
72         not writable. Run `systemprompt admin bridge rotate-signing-key` against a writable \
73         secrets file, or add a base64-encoded 32-byte value under `manifest_signing_secret_seed`."
74    )]
75    ManifestSeedUnavailable,
76
77    #[error("manifest_signing_secret_seed is invalid: {message}")]
78    ManifestSeedInvalid { message: String },
79
80    #[error(
81        "manifest_signing_secret_seed missing in subprocess env — parent must propagate \
82         MANIFEST_SIGNING_SECRET_SEED so subprocesses don't regenerate and clobber the secrets \
83         file"
84    )]
85    SubprocessSeedMissing,
86}
87
88impl SecretsBootstrap {
89    pub fn init() -> ConfigResult<&'static Secrets> {
90        if SECRETS.get().is_some() {
91            return Err(SecretsBootstrapError::AlreadyInitialized.into());
92        }
93
94        let mut secrets = loader::load_from_profile_config()?;
95        Self::ensure_manifest_signing_seed(&mut secrets)?;
96
97        Self::log_loaded_secrets(&secrets);
98
99        SECRETS
100            .set(secrets)
101            .map_err(|_| SecretsBootstrapError::AlreadyInitialized)?;
102
103        SECRETS
104            .get()
105            .ok_or_else(|| SecretsBootstrapError::NotInitialized.into())
106    }
107
108    pub fn oauth_at_rest_pepper() -> Result<&'static str, SecretsBootstrapError> {
109        Ok(&Self::get()?.oauth_at_rest_pepper)
110    }
111
112    pub fn manifest_signing_secret_seed()
113    -> Result<[u8; MANIFEST_SIGNING_SEED_BYTES], SecretsBootstrapError> {
114        let encoded = Self::get()?
115            .manifest_signing_secret_seed
116            .as_deref()
117            .ok_or(SecretsBootstrapError::ManifestSeedUnavailable)?;
118        decode_seed(encoded)
119    }
120
121    pub fn rotate_manifest_signing_seed() -> ConfigResult<[u8; MANIFEST_SIGNING_SEED_BYTES]> {
122        let path = Self::resolved_secrets_file_path()?;
123        let seed = generate_seed();
124        persist_seed(&path, &seed)?;
125        Ok(seed)
126    }
127
128    fn ensure_manifest_signing_seed(secrets: &mut Secrets) -> ConfigResult<()> {
129        if let Some(encoded) = secrets.manifest_signing_secret_seed.as_deref() {
130            decode_seed(encoded)?;
131            return Ok(());
132        }
133        if std::env::var("SYSTEMPROMPT_SUBPROCESS").is_ok() {
134            return Err(SecretsBootstrapError::SubprocessSeedMissing.into());
135        }
136        let Ok(path) = Self::resolved_secrets_file_path() else {
137            tracing::warn!(
138                "manifest_signing_secret_seed missing and no writable secrets file is configured"
139            );
140            return Ok(());
141        };
142        if !path.exists() {
143            tracing::warn!(
144                path = %path.display(),
145                "manifest_signing_secret_seed missing and secrets file does not exist on disk"
146            );
147            return Ok(());
148        }
149        let seed = generate_seed();
150        secrets.manifest_signing_secret_seed =
151            Some(base64::engine::general_purpose::STANDARD.encode(seed));
152
153        // The profile directory may be mounted read-only (e.g. an air-gapped
154        // deployment with a `:ro` profile mount). The seed is only needed for
155        // manifest signing within this process, so a failed persist is a
156        // warning, not a fatal error: the in-memory seed above keeps signing
157        // working for this boot. Operators wanting a stable seed across boots
158        // should set `MANIFEST_SIGNING_SECRET_SEED` or use a writable dir.
159        let profile_dir = path.parent().unwrap_or_else(|| Path::new("."));
160        if !dir_is_writable(profile_dir) {
161            tracing::warn!(
162                path = %path.display(),
163                "profile dir is read-only — using an ephemeral manifest signing seed for this \
164                 boot; set MANIFEST_SIGNING_SECRET_SEED or use a writable dir to persist it"
165            );
166            return Ok(());
167        }
168        if let Err(err) = persist_seed(&path, &seed) {
169            tracing::warn!(
170                path = %path.display(),
171                error = %err,
172                "could not persist manifest_signing_secret_seed — using an ephemeral seed for \
173                 this boot; set MANIFEST_SIGNING_SECRET_SEED to make it stable"
174            );
175            return Ok(());
176        }
177        tracing::info!(
178            path = %path.display(),
179            "Generated and persisted fresh manifest_signing_secret_seed"
180        );
181        Ok(())
182    }
183
184    fn resolved_secrets_file_path() -> ConfigResult<PathBuf> {
185        let profile =
186            ProfileBootstrap::get().map_err(|_| SecretsBootstrapError::ProfileNotInitialized)?;
187        let secrets_config = profile
188            .secrets
189            .as_ref()
190            .ok_or(SecretsBootstrapError::NoSecretsConfigured)?;
191        let profile_path = ProfileBootstrap::get_path()
192            .map_err(|_| SecretsBootstrapError::ProfileNotInitialized)?;
193        let profile_dir = Path::new(profile_path)
194            .parent()
195            .ok_or_else(|| ConfigError::other("Invalid profile path - no parent directory"))?;
196        Ok(resolve_with_home(profile_dir, &secrets_config.secrets_path))
197    }
198
199    pub fn database_url() -> Result<&'static str, SecretsBootstrapError> {
200        Ok(&Self::get()?.database_url)
201    }
202
203    pub fn database_write_url() -> Result<Option<&'static str>, SecretsBootstrapError> {
204        Ok(Self::get()?.database_write_url.as_deref())
205    }
206
207    pub fn get() -> Result<&'static Secrets, SecretsBootstrapError> {
208        SECRETS.get().ok_or(SecretsBootstrapError::NotInitialized)
209    }
210
211    pub fn require() -> Result<&'static Secrets, SecretsBootstrapError> {
212        Self::get()
213    }
214
215    #[must_use]
216    pub fn is_initialized() -> bool {
217        SECRETS.get().is_some()
218    }
219
220    pub fn try_init() -> ConfigResult<&'static Secrets> {
221        if SECRETS.get().is_some() {
222            return Self::get().map_err(Into::into);
223        }
224        Self::init()
225    }
226
227    fn log_loaded_secrets(secrets: &Secrets) {
228        let message = build_loaded_secrets_message(secrets);
229        tracing::debug!("{}", message);
230    }
231}