9fa74020070b37e150e4f34b3bd77ea279d0350c
Marco Ricci Add command-line interface...

Marco Ricci authored 3 weeks ago

1) # SPDX-FileCopyrightText: 2024 Marco Ricci <m@the13thletter.info>
2) #
3) # SPDX-License-Identifier: MIT
4) 
5) """Command-line interface for derivepassphrase_export."""
6) 
7) from __future__ import annotations
8) 
9) import base64
10) import importlib
11) import json
12) import logging
Marco Ricci Move vault key and path det...

Marco Ricci authored 1 month ago

13) import os
Marco Ricci Add command-line interface...

Marco Ricci authored 3 weeks ago

14) from typing import TYPE_CHECKING, Any, Literal
15) 
16) import click
17) from typing_extensions import assert_never
18) 
19) import derivepassphrase as dpp
20) from derivepassphrase import _types
21) 
22) if TYPE_CHECKING:
23)     import types
24)     from collections.abc import Sequence
25) 
26) __author__ = dpp.__author__
27) __version__ = dpp.__version__
28) 
29) __all__ = ('derivepassphrase_export',)
30) 
31) PROG_NAME = 'derivepassphrase_export'
Marco Ricci Move vault key and path det...

Marco Ricci authored 1 month ago

32) 
33) 
34) def get_vault_key() -> bytes:
35)     """Automatically determine the vault master key/password.
36) 
37)     Query the `VAULT_KEY`, `LOGNAME`, `USER` and `USERNAME` environment
38)     variables, in that order.  This is the same algorithm as vault uses.
39) 
40)     Returns:
41)         The master key/password.  This is generally used as input to
42)         a key-derivation function to determine the *actual* encryption
43)         and signing keys for the vault configuration.
44) 
45)     Raises:
46)         KeyError:
47)             We cannot find any of the named environment variables.
48)             Please set `VAULT_KEY` manually to the desired value.
49) 
50)     """
51) 
52)     username = (
53)         os.environb.get(b'VAULT_KEY')
54)         or os.environb.get(b'LOGNAME')
55)         or os.environb.get(b'USER')
56)         or os.environb.get(b'USERNAME')
57)     )
58)     if not username:
59)         env_var = 'VAULT_KEY'
60)         raise KeyError(env_var)
61)     return username
62) 
63) 
64) def get_vault_path() -> str | bytes | os.PathLike:
65)     """Automatically determine the vault configuration path.
66) 
67)     Query the `VAULT_PATH` environment variable, or default to
68)     `~/.vault`.  This is the same algorithm as vault uses.  If not
69)     absolute, then `VAULT_PATH` is relative to the home directory.
70) 
71)     Returns:
72)         The vault configuration path.  Depending on the vault version,
73)         this may be a file or a directory.
74) 
75)     Raises:
76)         RuntimeError:
77)             We cannot determine the home directory.  Please set `HOME`
78)             manually to the correct value.
79) 
80)     """
81) 
82)     result = os.path.join(
83)         os.path.expanduser('~'), os.environ.get('VAULT_PATH', '.vault')
84)     )
85)     if result.startswith('~'):
86)         msg = 'Cannot determine home directory'
87)         raise RuntimeError(msg)
88)     return result