Config / Secrets and required settings
Secrets and required settings
A secret prints as *** everywhere a value can leak: a log line, an error,
JSON.stringify of the whole config. A required setting has no default: a
layer must give it, or the program does not start, with an error that says
which variable or file would.
const config = await store.config('app', [
{
db: { url: secret(String), pool: 10 }, // a secret a layer must give
sentry: { dsn: secret('') }, // a secret, empty unless a layer gives it
region: required(String), // also Number and Boolean
},
env('APP', { vars: { db: { url: 'DATABASE_URL' } } }),
])
const pool = postgres(config.value.db.url.reveal()) // the text, where it is needed
console.log(config.value) // { db: { url: '***', pool: 10 }, sentry: { dsn: '***' }, region: 'eu' }@dataclass
class Db:
url: Secret = secret() # a layer must give it
pool: int = 10
@dataclass
class Sentry:
dsn: Secret = secret("") # empty unless a layer gives it
@dataclass(kw_only=True)
class Settings:
db: Db = field(default_factory=Db)
sentry: Sentry = field(default_factory=Sentry)
region: str # no default: required
config = await store.config("app", [Settings, env("APP", vars={"db.url": "DATABASE_URL"})])
pool = await asyncpg.create_pool(config.value.db.url.reveal())type DB struct {
URL tinystore.Secret `json:"url" config:"required"`
Pool int `json:"pool"`
}
type Settings struct {
DB DB `json:"db"`
Sentry struct {
DSN tinystore.Secret `json:"dsn"` // empty unless a layer gives it
} `json:"sentry"`
Region string `json:"region" config:"required"`
}
config, err := tinystore.OpenConfig(ctx, store, "app", Settings{DB: DB{Pool: 10}},
tinystore.Env("APP", tinystore.Var("db.url", "DATABASE_URL")))
pool, err := pgxpool.New(ctx, config.Get().DB.URL.Reveal())#[derive(Clone, Serialize, Deserialize, Default)]
#[serde(default)]
struct Db {
url: Secret<String>,
pool: u32,
}
#[derive(Clone, Serialize, Deserialize, Default)]
#[serde(default)]
struct Settings {
db: Db,
region: Required<String>,
}
let config = store.config("app", Settings::default()).env(Env::prefix("APP").var("db.url", "DATABASE_URL")).open()?;
let pool = connect(config.get().db.url.reveal())?;Secrets
- A secret prints as
***inconsole.log,JSON.stringify, a template,util.inspect, an error, Python'srepr, Go'sfmtandslog, Rust'sDebugand serde, the store's logs andexplain.reveal()gives its text, where the program hands it to what needs it. - A secret is never kept in the store, so a live change cannot
set it: only a file, a variable or its
_FILEgives it. - A file that is committed beside the code can be marked to refuse secrets.
- An error about a secret names its variable and never quotes its text.
Neither does an error about a map, a list or any value, which may hold a
secret: it names the variable or the file and the setting, and says what
was wrong without the value, as in
APP_TOKENS: its text is not a map: trailing comma at line 1 column 27orapp.yaml: tokens: invalid type, expected a string. - A secret is text. It cannot be
live: a value an admin page could set and read back is not a secret. - A secret in each entry of a map is declared as a field of
entries, and is given and printed like any other
secret. In Rust a
Secretinside a plainBTreeMaporVecprints as***inexplaintoo, the rest of the value as it was given, and a live map or list that holds one refuses a change, which would keep the secret. In Go a plain map whose values hold atinystore.Secretis refused when the config opens.
Hiding a secret from print keeps it out of logs and screenshots. It is not access control: who may read the config at all is the server's tokens' business.
Secrets from files
Docker and Kubernetes mount secrets as files. A variable named with _FILE
reads the setting from that file, its last newline removed:
APP_DB_URL_FILE=/run/secrets/db-url
DATABASE_URL_FILE=/run/secrets/db-urlBoth APP_DB_URL and APP_DB_URL_FILE set at once is invalid: one of them
would be ignored, and which one is a guess. explain names the file the
value came from:
db.url *** DATABASE_URL_FILE, /run/secrets/db-urlRequired settings
config app: region is required: region in app.toml, or set APP_REGION: invalid- A required setting has no default: a layer gives it, or the config does not
open, and the error names every layer that could have. For a secret, it
names the variable with its
_FILE, and no file that refuses secrets. - An empty text counts as missing.
- The value a declaration gives is never counted as given: Go's zero value
and Rust's
Defaultdo not satisfyrequired.
| Language | Required | A secret |
|---|---|---|
| TypeScript | required(String), required(int({ min: 1 })) | secret(String) is required; secret('') is not |
| Python | a field without a default | secret() required; secret("") not |
| Go | config:"required" | tinystore.Secret |
| Rust | Required<T> | Secret<T> |
Settings with no value
Some settings have no sensible default and may stay unset: the password of a
database that uses trust or peer authentication takes none. Declare such a
setting with no value. It is null until a layer gives it, and a secret
declared this way is still a secret:
const config = await store.config('pg', [
{
password: optional(secret(String)), // null until a layer gives it
port: optional(int({ min: 1 })),
},
file('pg.toml'),
env('PG'),
])
const password = config.value.password?.reveal() // undefined if no layer gave it@dataclass
class Pg:
password: Secret | None = None # None until a layer gives it
port: Annotated[int, Range(min=1)] | None = None
config = await store.config("pg", [Pg, file("pg.toml"), env("PG")])
password = config.value.password.reveal() if config.value.password else Nonetype Pg struct {
Password *tinystore.Secret `json:"password"` // nil until a layer gives it
Port *int `json:"port" config:"min=1"`
}
config, err := tinystore.OpenConfig(ctx, store, "pg", Pg{}, tinystore.File("pg.toml"), tinystore.Env("PG"))
if password := config.Get().Password; password != nil {
connect(password.Reveal())
}#[derive(Clone, Default, Serialize, Deserialize)]
#[serde(default)]
struct Pg {
password: Option<Secret<String>>, // None until a layer gives it
port: Option<u16>,
}
let config = store.config("pg", Pg::default()).file("pg.toml").env("PG").open()?;
let password = config.get().password.as_ref().map(|password| password.reveal().clone());- A value that a layer gives is checked like any other. In Bun, Python and
Go above,
PG_PORT=0fails withPG_PORT: 0 is below 1. In Rust the type is the rule, soPG_PORT=70000fails because it doesn't fit au16. explainshows the setting asunset, from the declaration, until a layer gives it a value. A secret that a layer gives shows as***.- A group of settings always has a value. Each of its settings may have none.
- In a declaration as data,
"default": nulldeclares a setting with no value, of any kind:{ "kind": "text", "secret": true, "default": null }.
| Language | No value |
|---|---|
| TypeScript | optional(String), optional(secret(String)), optional(int({ min: 1 })) |
| Python | str | None = None, Secret | None = None |
| Go | a pointer: *string, *tinystore.Secret, *int |
| Rust | Option<T>, and Option<Secret<String>> for a secret |
| As data | "default": null |
In Rust, Secret<Option<String>> is refused when the config opens: write
Option<Secret<String>>.
Marks that nest
live goes outermost, around a value, a rule, required(…) or optional(…):
| Written | Means |
|---|---|
live(100) | 100 by default, changes while running |
live(int(100, { min: 1 })) | the same, at least 1 |
live(required(String)) | a layer must give it at start; it may change later |
live(optional(String)) | null until a layer or a change gives it |
optional(secret(String)) | a secret with no value until a layer gives it |
live(secret(…)) | refused: a secret is never kept |
required(secret(…)) | refused: secret(String) is required already |
Read, required and live leave the type: required(String) reads as a
string, live(int(100)) as a number, and only a secret reads as a
Secret, whose reveal() gives its text. optional adds null:
optional(String) reads as string | null.
See also
- Config: the engine's overview.
- Layers and files: where a value comes from.
- Explain and validate: seeing which layer gave each setting.