Skip to content
In this guideRust SDK

Rust SDK

Reads the vault when your program starts, with async (tokio) and blocking APIs. Values arrive as SecretString.

  • coffrebox 0.1.0
  • Rust 1.89+
  • async and blocking

Install

Terminal
cargo add coffrebox

Or directly in Cargo.toml:

Cargo.toml
[dependencies]
coffrebox = "0.1"

Load at startup

Blocking

The recommended way to fill the environment: call blocking::load() as the first thing in main, before any other thread exists. It reads COFFREBOX_URL and COFFREBOX_TOKEN.

main.rs
fn main() -> Result<(), coffrebox::Error> {
    let _vault = coffrebox::blocking::load()?;

    let db = std::env::var("DATABASE_URL").expect("came from the vault");
    // Build the tokio runtime, start the server...
    Ok(())
}

Async

This example uses the tokio runtime, which you add to your project with cargo add tokio --features macros,rt.

main.rs
use coffrebox::ExposeSecret;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), coffrebox::Error> {
    let vault = coffrebox::load().await?;
    let db = vault.get("DATABASE_URL").await?;
    connect_db(db.expose_secret()).await;
    Ok(())
}

load() fails closed. If it can’t load the secrets (server down with no valid cache, access revoked, tampered response), it returns an error and your program doesn’t start half-configured.

Environment and threads. Changing environment variables while other threads are running is undefined behavior on several platforms (std::env::set_var is unsafe in Rust 2024). Call blocking::load() at the start of main, or load() in a single-threaded runtime before you create other threads. If you don’t need the environment, use Client::connect and get().

Options

main.rs
use std::time::Duration;
use coffrebox::{ClientConfig, OfflineMode, Watch};

let config = ClientConfig::from_env()?             // COFFREBOX_URL, COFFREBOX_TOKEN and COFFREBOX_CACHE_DIR
    .with_override_env(false)                      // keeps variables that already exist (default)
    .with_watch(Watch::default_interval())         // checks every 5 minutes (default: Watch::Off)
    .with_ttl(Duration::from_secs(300))            // how long get() trusts the values in memory
    .with_max_stale(Duration::from_secs(72 * 3600)) // with the server down, a cache older than this is rejected
    .with_offline(OfflineMode::UseCache);          // or OfflineMode::Fail
let vault = coffrebox::load_with(config).await?;

ClientConfig::new(url, token) builds the configuration without reading COFFREBOX_URL and COFFREBOX_TOKEN.

Live updates

With Watch::Off, the default, the vault is read once and there are no background requests; get() checks the server again once the ttl has passed.

With Watch::Every(interval) (minimum 30 s) or Watch::default_interval() (5 minutes), the SDK asks the server whether the vault changed. With no change, the response is a 304 with no body. With a change, the whole encrypted vault is verified and compared in your process, and you get the exact names:

main.rs
vault.on_change(|names| println!("changed: {names:?}"));

let mut changes = vault.subscribe(); // tokio broadcast receiver
while let Ok(names) = changes.recv().await {
    // reconnect the pool, for example
}

get() returns the new value right away. Unlike the Node.js SDK, the Rust SDK doesn’t rewrite the environment after startup (see the warning about threads): react to on_change and read with get().

Cost: with Watch on, about 20 small requests per hour per process.

Errors

coffrebox::Error never includes values or the token. Error::code() returns the same stable codes as the Node.js SDK:

Variantcode()When
NotFound { key }COFFREBOX_NOT_FOUNDget() for a secret that doesn’t exist.
RevokedCOFFREBOX_REVOKEDThe access was revoked. The on-disk cache is deleted.
Offline { detail }COFFREBOX_OFFLINEThe server is down and there’s no usable cache (or OfflineMode::Fail).
Tampered { reason }COFFREBOX_TAMPEREDA signature, a hash, or the revision didn’t match.
StaleCacheCOFFREBOX_STALE_CACHEThe server is down and the cache is older than max_stale.
InvalidConfig(_)COFFREBOX_INVALID_CONFIGA URL, token, or option is missing or invalid, there’s a problem with the on-disk cache, or the client was already closed.

Two vaults in the same process

Each token opens a single vault. Create one client per token:

main.rs
use coffrebox::{Client, ClientConfig};

let own = Client::connect(ClientConfig::new(&url, own_token)).await?;
let shared = Client::connect(ClientConfig::new(&url, shared_token)).await?;

Security

The Rust SDK follows the same rules as the Node.js SDK and shares its cache format: the token is the key, the on-disk cache is only ciphertext (0600), a revision lower than the last one seen is rejected, and revocation deletes the cache. More details in Security.