0ecb8a91d39b699816e21d6e215878ddc50252e3
Marco Ricci Update copyright notices to...

Marco Ricci authored 2 months ago

1) # SPDX-FileCopyrightText: 2025 Marco Ricci <software@the13thletter.info>
Marco Ricci Add command-line interface...

Marco Ricci authored 6 months ago

2) #
Marco Ricci Update copyright notices to...

Marco Ricci authored 2 months ago

3) # SPDX-License-Identifier: Zlib
Marco Ricci Add command-line interface...

Marco Ricci authored 6 months ago

4) 
Marco Ricci Move exporter command-line...

Marco Ricci authored 6 months ago

5) """Foreign configuration exporter for derivepassphrase."""
Marco Ricci Add command-line interface...

Marco Ricci authored 6 months ago

6) 
7) from __future__ import annotations
8) 
Marco Ricci Harmonize the interface for...

Marco Ricci authored 2 months ago

9) import importlib
Marco Ricci Move vault key and path det...

Marco Ricci authored 6 months ago

10) import os
Marco Ricci Harmonize the interface for...

Marco Ricci authored 2 months ago

11) from typing import TYPE_CHECKING, Protocol
Marco Ricci Add command-line interface...

Marco Ricci authored 6 months ago

12) 
13) import derivepassphrase as dpp
14) 
Marco Ricci Harmonize the interface for...

Marco Ricci authored 2 months ago

15) if TYPE_CHECKING:
16)     from collections.abc import Callable
17)     from typing import Any
18) 
19)     from typing_extensions import Buffer
20) 
Marco Ricci Add command-line interface...

Marco Ricci authored 6 months ago

21) __author__ = dpp.__author__
22) __version__ = dpp.__version__
23) 
Marco Ricci Move exporter command-line...

Marco Ricci authored 6 months ago

24) __all__ = ()
Marco Ricci Move vault key and path det...

Marco Ricci authored 6 months ago

25) 
26) 
Marco Ricci Harmonize the interface for...

Marco Ricci authored 2 months ago

27) INVALID_VAULT_NATIVE_CONFIGURATION_FORMAT = (
28)     'Invalid vault native configuration format: {fmt!r}'
29) )
30) 
31) 
32) class NotAVaultConfigError(ValueError):
33)     """The `path` does not hold a `format`-type vault configuration."""
34) 
35)     def __init__(
36)         self,
37)         path: str | bytes,
38)         format: str | None = None,  # noqa: A002
39)     ) -> None:
40)         self.path = path
41)         self.format = format
42) 
43)     def __str__(self) -> str:  # pragma: no cover
44)         formatted_format = (
45)             f'vault {self.format} configuration'
46)             if self.format
47)             else 'vault configuration'
48)         )
49)         return f'Not a {formatted_format}: {self.path!r}'
50) 
51) 
Marco Ricci Move vault key and path det...

Marco Ricci authored 6 months ago

52) def get_vault_key() -> bytes:
Marco Ricci Fix miscellaneous small doc...

Marco Ricci authored 6 months ago

53)     """Automatically determine the vault(1) master key/password.
Marco Ricci Move vault key and path det...

Marco Ricci authored 6 months ago

54) 
55)     Query the `VAULT_KEY`, `LOGNAME`, `USER` and `USERNAME` environment
Marco Ricci Fix miscellaneous small doc...

Marco Ricci authored 6 months ago

56)     variables, in that order.  This is the same algorithm that vault
57)     uses.
Marco Ricci Move vault key and path det...

Marco Ricci authored 6 months ago

58) 
59)     Returns:
60)         The master key/password.  This is generally used as input to
61)         a key-derivation function to determine the *actual* encryption
62)         and signing keys for the vault configuration.
63) 
64)     Raises:
65)         KeyError:
66)             We cannot find any of the named environment variables.
67)             Please set `VAULT_KEY` manually to the desired value.
68) 
69)     """
70)     username = (
71)         os.environb.get(b'VAULT_KEY')
72)         or os.environb.get(b'LOGNAME')
73)         or os.environb.get(b'USER')
74)         or os.environb.get(b'USERNAME')
75)     )
76)     if not username:
77)         env_var = 'VAULT_KEY'
78)         raise KeyError(env_var)
79)     return username
80) 
81) 
82) def get_vault_path() -> str | bytes | os.PathLike:
Marco Ricci Fix miscellaneous small doc...

Marco Ricci authored 6 months ago

83)     """Automatically determine the vault(1) configuration path.
Marco Ricci Move vault key and path det...

Marco Ricci authored 6 months ago

84) 
85)     Query the `VAULT_PATH` environment variable, or default to
Marco Ricci Fix miscellaneous small doc...

Marco Ricci authored 6 months ago

86)     `~/.vault`.  This is the same algorithm that vault uses.  If not
Marco Ricci Move vault key and path det...

Marco Ricci authored 6 months ago

87)     absolute, then `VAULT_PATH` is relative to the home directory.
88) 
89)     Returns:
90)         The vault configuration path.  Depending on the vault version,
91)         this may be a file or a directory.
92) 
93)     Raises:
94)         RuntimeError:
95)             We cannot determine the home directory.  Please set `HOME`
96)             manually to the correct value.
97) 
98)     """
99)     result = os.path.join(
100)         os.path.expanduser('~'), os.environ.get('VAULT_PATH', '.vault')
101)     )
102)     if result.startswith('~'):
103)         msg = 'Cannot determine home directory'
104)         raise RuntimeError(msg)
105)     return result