Secrets: OpenBao and HashiCorp Vault
The plugin secrets (secrets(), class yf_secrets) reads secrets from
OpenBao or HashiCorp Vault over their HTTP API (driver vault, the same for both) and keeps
them across requests, so a page neither logs in nor asks the server each time. It is off by default: until a site sets
$PROJECT_CONF['secrets']['ENABLED'] every call returns its default and nothing is contacted. Sites that would rather not
change code can let OpenBao / Vault Agent render a file instead, which the file driver below or the site config reads.
$key = secrets()->get('myproject/prod/llm', 'openai_key'); // the latest version, null when missing
$key = secrets()->get('myproject/prod/llm', 'openai_key', ''); // '' when missing (and while the plugin is off)
$old = secrets()->get('myproject/prod/llm', 'openai_key', '', 3); // version 3
$old = secrets()->get('kv:myproject/prod/llm#openai_key@3'); // the same as one reference
$all = secrets()->entry('myproject/prod/db'); // ['user' => ..., 'password' => ...]
$ver = secrets()->version('myproject/prod/db'); // 4
$names = secrets()->list('myproject/prod/'); // ['db', 'llm', 'mail/']
try {
$pass = secrets()->get('myproject/prod/db', 'password');
} catch (yf_secrets_exception $e) {
// the server is down past STALE_GRACE, or refuses the path: $e->reason unavailable / denied / config / invalid
logger()->error($e->getMessage()); // path and HTTP status only, never a value
}
Values come from a KV v2 engine (KV_MOUNT, default secret): a path names a secret, a key one of its values,
and a version pins an older one. Two more drivers keep the same calls: env (a path is an environment variable) and
file (a JSON file {"path": {"key": "value"}}, for example rendered by the Agent).
Keys of the other plugins
MAP names environment variables the plugin answers. The key lookups of llm, translate and
cloud_storage ask secrets_env() first (it checks the config before loading the plugin), so their profiles keep key_env names and get the value
from the server. Names not in MAP, and every name while the plugin is off, are looked up as before (environment,
$CONF, constant).
$PROJECT_CONF['secrets']['MAP'] = [
'OPENAI_API_KEY' => 'kv:myproject/prod/llm#openai_key',
'DEEPL_API_KEY' => 'kv:myproject/prod/translate#deepl',
'AWS_SECRET_ACCESS_KEY' => 'kv:myproject/prod/s3#secret@2', // pinned to version 2
];
// llm(), translate and cloud_storage find them like an environment variable; secrets()->env('OPENAI_API_KEY') too
From config/db_setup.php, which runs before the framework's classes, load the class and use
yf_secrets::boot(): it reads $PROJECT_CONF['secrets'] as the plugin does.
// config/db_setup.php runs before the framework's classes: load the class itself
require_once YF_PATH . 'plugins/secrets/classes/yf_secrets.class.php';
define('DB_USER', yf_secrets::boot()->get('myproject/prod/db', 'user'));
define('DB_PSWD', yf_secrets::boot()->get('myproject/prod/db', 'password'));
When the server is down a cached value is served for STALE_GRACE seconds past its fresh time (one hour by default),
never past the expiry of a lease; after that the call throws yf_secrets_exception (fail closed). A path the server refuses
(HTTP 403 after a fresh login) is never served from the cache.