secret_store¶
panel_flowdash.secret_store
¶
Secrets in the project file, such as model provider keys.
A secret is written in the project file either as a reference to an
environment variable, ${OPENROUTER_API_KEY}, or, opted into from the
admin view, encrypted with Fernet: {encrypted: <token>, key_id: <id>}.
The resolved value is never written to the file, sent to the browser or
logged, and never put into os.environ: it is handed to the client
that needs it, e.g. a model provider.
The encryption key is, in order, FLOWDASH_SECRET_KEY (several keys,
comma-separated, the first encrypting and all decrypting, for rotation),
the OS keyring (with keyring installed, for a local single-user setup)
or a key file outside the project, ~/.config/flowdash/secret.key
(FLOWDASH_SECRET_KEY_FILE), made with mode 0600 the first time a
secret is stored. Workers of one deployment share FLOWDASH_SECRET_KEY.
Each stored value names the key it was encrypted with by key_id, so a
value whose key is gone says so and asks to be entered again.
When people sign in (OAuth or a users file), the keyring and the key file
belong to the server's account rather than the deployment, so new secrets
are stored only with FLOWDASH_SECRET_KEY set; see storage_blocked.
Values stored before with the keyring or key file still decrypt.
Encryption needs cryptography (pip install 'panel-flowdash[secrets]').
INSTALL_HINT = "Storing keys encrypted needs `pip install 'panel-flowdash[secrets]'`."
module-attribute
¶
KEYRING_NAME = 'secret-key'
module-attribute
¶
KEYRING_SERVICE = 'panel-flowdash'
module-attribute
¶
KEY_ENV = 'FLOWDASH_SECRET_KEY'
module-attribute
¶
KEY_FILE_ENV = 'FLOWDASH_SECRET_KEY_FILE'
module-attribute
¶
SIGN_IN_HINT = f'Set {KEY_ENV} to store keys encrypted when people sign in.'
module-attribute
¶
_KEY_LOCK = threading.Lock()
module-attribute
¶
_REFERENCE = re.compile('^\\$\\{([A-Za-z_][A-Za-z0-9_]*)\\}$')
module-attribute
¶
__all__ = ['KEY_ENV', 'KEY_FILE_ENV', 'SIGN_IN_HINT', 'SecretError', 'SecretInfo', 'describe_secret', 'encrypt_secret', 'encryption_available', 'env_reference', 'is_secret', 'resolve_secret', 'storage_blocked']
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
SecretError
¶
Bases: ValueError
A secret that cannot be resolved; the message never holds its value.
SecretInfo
dataclass
¶
What the admin view may show about a secret: where it comes from, never its value.
source is "env", "stored" or None (not set). env names
the variable of a reference; last4 is the value's last four
characters when it resolves, and error why it does not.
_decrypt(value)
¶
_fernet(keys)
¶
_from_keyring()
¶
_key_file()
¶
_key_id(key)
¶
_keyring()
¶
_keys(create=False)
¶
Return the encryption keys, the one that encrypts first; with create, make one if none.
_read_key_file(path)
¶
_to_keyring(key)
¶
_write_key_file(path, key)
¶
check_secret(value, what)
¶
Return why value may not stand for a secret in the project file, or None.
describe_secret(value, environ=None)
¶
Return where value comes from and whether it resolves, without its value.
encrypt_secret(value, *, signed_in=False)
¶
Return value encrypted, as the project file stores it: {encrypted, key_id}.
signed_in defaults to an app without sign-in, where the keyring and
key file may store; pass the app's sign-in state from any view that
stores what a person typed. Raises SecretError when storage_blocked
says it may not be stored.
encryption_available()
¶
Whether cryptography is installed, so secrets can be stored encrypted.
env_reference(name)
¶
Return the project file's reference to the environment variable name.
expand_env(value, environ=None)
¶
Replace ${NAME} and ${NAME:-default} references with environment values.
Text in value, or in the lists and mappings it holds, is expanded;
other values are returned as they are. Raises ValueError naming a
variable that is not set and has no default. A loader keeps the
reference and expands it where it is used, so dump writes the
reference back and never the secret.
fernet_key()
¶
Return a new key for FLOWDASH_SECRET_KEY.
is_secret(value)
¶
Whether value is written the way a secret may be: a reference or an encrypted value.
resolve_secret(value, environ=None)
¶
Return the secret value stands for, or None when it is not set.
A reference reads its environment variable, an encrypted value is
decrypted. Raises SecretError for a variable that is not set or a value
that cannot be decrypted.
storage_blocked(signed_in=False)
¶
Return why a new secret cannot be stored encrypted here, or None when it can.
With signed_in (people sign in with OAuth or a users file) a secret is
stored only with FLOWDASH_SECRET_KEY: the keyring and the key file
are not offered then. Environment variable references always work.