git.schokokeks.org
Repositories
Help
Report an Issue
derivepassphrase.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
d525f43
Branches
Tags
documentation-tree
master
0.1.0
0.1.1
0.1.2
0.1.3
0.2.0
0.3.0
0.3.1
0.3.2
derivepassphrase.git
src
derivepassphrase
exporter
__init__.py
Move vault key and path detection logic into the exporter module
Marco Ricci
commited
d525f43
at 2024-08-18 09:41:16
__init__.py
Blame
History
Raw
import os def get_vault_key() -> bytes: """Automatically determine the vault master key/password. Query the `VAULT_KEY`, `LOGNAME`, `USER` and `USERNAME` environment variables, in that order. This is the same algorithm as vault uses. Returns: The master key/password. This is generally used as input to a key-derivation function to determine the *actual* encryption and signing keys for the vault configuration. Raises: KeyError: We cannot find any of the named environment variables. Please set `VAULT_KEY` manually to the desired value. """ username = ( os.environb.get(b'VAULT_KEY') or os.environb.get(b'LOGNAME') or os.environb.get(b'USER') or os.environb.get(b'USERNAME') ) if not username: env_var = 'VAULT_KEY' raise KeyError(env_var) return username def get_vault_path() -> str | bytes | os.PathLike: """Automatically determine the vault configuration path. Query the `VAULT_PATH` environment variable, or default to `~/.vault`. This is the same algorithm as vault uses. If not absolute, then `VAULT_PATH` is relative to the home directory. Returns: The vault configuration path. Depending on the vault version, this may be a file or a directory. Raises: RuntimeError: We cannot determine the home directory. Please set `HOME` manually to the correct value. """ result = os.path.join( os.path.expanduser('~'), os.environ.get('VAULT_PATH', '.vault') ) if result.startswith('~'): msg = 'Cannot determine home directory' raise RuntimeError(msg) return result