Per-client secrets
Some credentials belong to a client, not to the application: the SFTP password of a client’s server, an API key for a client’s own system. Store a reference to it with the client’s settings and resolve it when you act for that client.
Secret password = resolver.require(ctx.client(), SecretRef.parse("files:sftp-password"));password, err := resolver.Require(ctx, cx.Client(), secrets.MustParseRef("files:sftp-password"))The client always comes from the caller (normally the Context), never from the reference, so a
reference cannot reach another client’s secret even if someone can edit it. Application-wide secrets
(a database password, the platform client secret) are not this: they are configuration settings of
type secret (Configuration). The threat model is in the secrets
specification.
References
Section titled “References”A reference is name (the default store) or store:name (a named one). The name matches
[a-z0-9]+(-[a-z0-9]+)* and is at most 100 characters: no dots, slashes, separators or underscores, so
there is no traversal. A malformed reference is an error when parsed (IllegalArgumentException in
Java; ParseRef and NewRef return an error in Go).
Setting it up
Section titled “Setting it up”Secret stores are named in code, each with the driver it uses. There is no secrets configuration
section (an application can have many stores, so they are code; see Configuration or
code); a leftover one fails start-up naming what to
write instead.
List SecretsModule (in the core) with one or more stores:
SecretsModule.Builder two = SecretsModule.builder().store("files", SecretStores.file(secrets)).store("env", SecretStores.env("CLIENT_SECRET_"));App app = start(two.defaultStore("files").build(), Map.of());app.get(SecretResolver.class) is then available. Store names match [a-z][a-z0-9-]*; with one store
it is the default, with several defaultStore(name) must name one (build() throws otherwise).
A SecretStoreFactory receives the lookup, so a directory or prefix that differs per deployment is a
setting of your own, read there:
SecretStoreFactory fromSetting = (name, lookup) -> SecretStores.file(Path.of(lookup.get(io.flowcatalyst.framework.config.Config.class).get(DIRECTORY))) .create(name, lookup);d := secrets.Declare(app) // in main: claims the secrets componentresolver, err := secrets.Build(ctx, b, d, // in the build function secrets.WithStore("files", secrets.File("/run/secrets/clients").WithMaxBytes(4096)), secrets.WithStore("env", secrets.Env("CLIENT_SECRET_").WithCache(secrets.NoCache())), secrets.DefaultStore("files"))Names match [a-z][a-z0-9-]*; one store is the default, several need DefaultStore. Build reports
every problem at once as a *fw.StartupError. A deployment-specific directory or prefix is the
application’s own setting, read where Build is called.
The file driver
Section titled “The file driver”A secret is the file <directory>/<client>/<name>:
/run/secrets/clients/acme/sftp-password/run/secrets/clients/globex/sftp-password- The directory must be absolute (checked when built) and exist at start-up.
withMaxBytes(Java) orWithMaxBytes(Go) bounds a file: default 65 536, 1 B to 1 MiB; a larger file is an error. - Content is read as UTF-8 (strictly); one trailing newline (
\nor\r\n) is removed, soecho secret > fileworks. An empty file is “not found”. - Symbolic links are followed (Kubernetes secret volumes use them), but the real file must be inside
the directory and end in exactly
<client>/<name>; anything else is “not found” with a warning. So a link out of the directory, a link to another client’s file, or a client id differing only by case on a case-insensitive file system never reads someone else’s file. (Go compares against the directory entries, becauseEvalSymlinksdoes not canonicalise case.) - A client id starting with
.is refused. A file readable by every local user is logged at WARN once, not refused.
The env driver
Section titled “The env driver”A secret is the variable prefix + CLIENT + "__" + NAME, upper-cased with - turned into _: client
acme-corp, name sftp-password, prefix CLIENT_SECRET_ gives
CLIENT_SECRET_ACME_CORP__SFTP_PASSWORD.
- The prefix (checked when built) matches
[A-Z][A-Z0-9_]*_, has at least 4 characters, and may not start withAPP_,HTTP_,DATABASE_,REDIS_,LOCKS_,FLOWCATALYST_orSECRETS_, so no reference can reach a framework setting. - Only client ids of lowercase letters, digits and single hyphens can be encoded; any other client is refused, never approximated, because an approximation could map two clients to one variable. Use the file driver for such clients.
- Variables are read by exact name from the environment the application was configured with (in
Java the map given to
AppBuilder.environmentin tests; in Gofw.Boot.LookupEnv); they are never listed. An absent or empty variable is “not found”.
In Java the factories are SecretStores.file(directory) and SecretStores.env(prefix); in Go
secrets.File(dir) and secrets.Env(prefix).
The platform driver
Section titled “The platform driver”A client’s secret can live in the platform’s configuration, read with the reveal call as the
application’s service account. The driver is part of the FlowCatalyst integration module:
PlatformSecrets.store() in Java, flowcatalyst.PlatformSecrets(client) in Go (optionally
.Section("ftp")). It is described on Platform config and
secrets. The SecretStores.platform() (Java) and
secrets.Platform() (Go) in the core module are placeholders: they fail start-up with a message naming
the real driver.
In short, for a name n and client c the driver reads the platform configuration property
(application, section, n) at client scope c (section secrets unless set), which needs the
platform:application-service:config:reveal permission. The client may be the platform’s client id
(clt_...) or the client’s identifier; any other client is refused without a call. There is no
fall-back to the global value. A 200 is found; 404 Config_NOT_FOUND or Client_NOT_FOUND is not
found; 400 NOT_A_SECRET is refused; any other 404, a 403, a 5xx, a network error or an answer
without a value is a store error carrying no value, body or cause.
The cache
Section titled “The cache”Each store has a cache policy.
| Policy | Meaning |
|---|---|
| default | found values reused for 5 minutes, at most 10 000 entries (the least recently used goes first) |
| custom (ttl, max entries) | ttl 0 to 24 h, entries 1 to 1 000 000 |
| none | no cache: every lookup reads the store |
| stale-if-error window | how long past its ttl an expired value may still be served while the store is failing: 0 to 24 h, default 5 minutes, 0 turns it off |
CachePolicy.defaults(), CachePolicy.of(ttl, maxEntries), CachePolicy.none() and
.withStaleIfError(window); attach one to a store with withCache.
secrets.DefaultCache(), secrets.NewCachePolicy(ttl, max), secrets.NoCache() and
.WithStaleIfError(d); attach one with .WithCache(policy) on a factory. A driver written outside the
package gets the same cache from secrets.Cached(store, policy, b).
The cache does three things beyond remembering a value:
- Stale on error. If the store fails (a platform that is down, a
5xx, a network error), the last value found is served for the stale window past its expiry, and a warning names the store, client and secret (never the value). It never applies to “not found” or a refusal: those are answers, they drop the entry, and the secret is gone. While the store keeps failing, every lookup after the ttl still tries the store once before falling back. - Single flight. Concurrent lookups of one key that all miss make one call to the store; the others wait for its result, or share its failure. Different keys do not wait for each other.
- Aliases. A store that knows two names for one client (the platform driver:
clt_...and the identifier) reports one canonical name, so both names share one entry and eviction under either name drops it. Stores that do not (env,file) are unchanged.
Only found values are cached.
Using it
Section titled “Using it”resolver.resolve(client, ref)returnsSecretResult.Found(secret),NotFoundorRefused(reason).resolver.require(client, ref)returns theSecretor throwsSecretNotFoundException(for both not found and refused).- A store that cannot be read (permissions, I/O, a file that is too large or not UTF-8) throws
SecretStoreException. - After rotating a secret,
resolver.evict(client, ref)orresolver.evictAll()makes the next call read the store; otherwise the new value is seen when the cache entry expires.resolver.evictEverywhere(client, name)drops one entry from every store (the platform-config admin page uses it after a write).
Resolver.ResolvereturnsFound{Secret},NotFound{}orRefused{Reason}.Requirereturns theconfig.Secretor an error wrappingErrNotFound(for not found and refused; the message names the store, client and name, never a value).- A store that cannot be read (I/O error, invalid UTF-8, file too large) is a
*StoreErrorwhose message has neither a value nor content.ErrNoStoreandErrUnknownStoreare for a resolver without a usable store. - After rotating a secret,
Resolver.Evict(client, ref)orEvictAll()makes the next call read the store. - A custom store implements
Storeand aStoreFactory, and wraps itself withsecrets.Cached.
Values only ever appear inside a secret type: it prints as ****, and no exception message, result or
log line contains a value or file content. Reveal one (reveal() in Java, Reveal() in Go) only where
you hand the value to the library that needs it, and never log it.
To check a credential a caller presents against one you hold, use secret.matches(candidate)
(Matches in Go): it compares SHA-256 digests in constant time, so timing reveals neither the content
nor the length.
Testing
Section titled “Testing”Point a file store at a temporary directory, or give the application builder the environment for an env
store. In Java, SecretResolver.of(Map.of("files", store), "files") builds a resolver over stores you
make yourself.