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() -> String {
    format!("Bearer {}", *API_KEY.value())
}

§Scope

How a secret is declared says who it belongs to:

  • Secret::new("ApiKey") 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.
  • Secret::new("ApiKey").shared() has one value for the whole app. Any service may declare it, and each that reads it is granted access to it.
  • Secret::new("ApiKey").global() has one value for the whole app and is available to every service, without being granted.

The scope is written out in the declaration, as the name is: Encore reads both from the source.

§Values

Secret::value returns a Value: a handle to the secret’s value, cheap to make and to clone, which reads as the value itself. A secret’s value is read once per process and does not change while it runs: a new value reaches a service when it restarts.

A secret with no value panics when it is read, as does one whose value is not valid UTF-8: only the request or task that reads it fails. The message names the secret, never its value.

A secret is a String, the only type a secret can be read as for now.

§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§

Secret
A secret, declared as a module-level static: its name, and its value once read. See the module documentation.
Value
A handle to a secret’s value. Reads as the value: &*value, or any of its methods directly.

Traits§

SecretValue
A type a secret’s value can be read as: only String, for now. Sealed.