Skip to main content

Module secrets

Module secrets 

Source
Expand description

Application secrets. Declared at module scope and resolved from the runtime.

use encore::secrets::Secret;

static API_KEY: Secret = Secret::new("ApiKey");

fn authorization() -> Result<String, encore::Error> {
    Ok(format!("Bearer {}", *API_KEY.value()?))
}

§Scope

Which of the three types a secret is declared with says who it belongs to:

  • A Secret belongs to the service that declares it. Two services may each declare one of the same name and hold different values in them, so adding a secret to one service can never collide with — or silently reuse — a name another already took. It must be declared inside a service.
  • A SharedSecret has one value for the whole app. Any service may declare it, and each that reads it is granted access to it.
  • A GlobalSecret has one value for the whole app and is available to every service, without being granted.

§Values

Secret::value returns a Value: a handle to the secret’s current value, cheap to make and to clone, which reads as the value itself. A secret’s value can change while the app runs; a handle keeps the value it was made with, and the next call to value gives the new one. So take a handle where the value is used, rather than keeping one.

A secret is a String unless its type says otherwise: Secret<Vec<u8>> for bytes, Secret<Json<T>> for a value stored as JSON.

§In a config

A field of a config type may be a secret, naming the secret it reads: #[config(secret = "StripeKey")] api_key: Secret. The secret has the config’s own scope, and is declared in code as any other. The config’s value never holds it; reading the field reads the secret, with value as for any other.

§Tests

Under encore test a test can give a secret a value of its own, which no other test sees: testing::set_secret.

Structs§

GlobalSecret
A secret with one value for the whole app, available to every service without being granted.
Json
A secret value stored as JSON: Secret<Json<Credentials>>. Reads as the T inside.
Secret
A secret belonging to the service that declares it, which holds its own value for it. Must be declared inside a service.
SharedSecret
A secret with one value for the whole app. A service that reads it is granted access to it.
Value
A handle to a secret’s value, as it was when the handle was made. Reads as the value: &*value, or any of its methods directly.

Traits§

AnySecret
Any of the secret types, whatever its value’s type. Sealed: Secret, SharedSecret and GlobalSecret are the only ones.
SecretValue
A type a secret’s value can be read as.