valt0-client 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- valt0/__init__.py +26 -0
- valt0/_config.py +6 -0
- valt0/_crypto.py +101 -0
- valt0/_errors.py +38 -0
- valt0/_protocol.py +74 -0
- valt0/_secret.py +78 -0
- valt0/_service.py +76 -0
- valt0/_session.py +85 -0
- valt0/_transport.py +169 -0
- valt0/_vault.py +117 -0
- valt0/py.typed +0 -0
- valt0_client-1.0.0.dist-info/METADATA +185 -0
- valt0_client-1.0.0.dist-info/RECORD +15 -0
- valt0_client-1.0.0.dist-info/WHEEL +4 -0
- valt0_client-1.0.0.dist-info/licenses/LICENSE +202 -0
valt0/__init__.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""Python client for Valt0.
|
|
2
|
+
|
|
3
|
+
Fetches secrets from a local Valt0 vault at runtime, so you don't have to keep them in ``.env``
|
|
4
|
+
files, config files, or CI variables::
|
|
5
|
+
|
|
6
|
+
import valt0
|
|
7
|
+
|
|
8
|
+
vault = valt0.open_vault(id="your-vault-id", name="your-vault-name")
|
|
9
|
+
with vault.secret("DATABASE_PASSWORD") as secret:
|
|
10
|
+
db.connect(password=secret.reveal())
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from ._errors import CantConnectToServiceError, NoServiceResponseError, Valt0Error
|
|
14
|
+
from ._secret import Secret
|
|
15
|
+
from ._vault import Vault, open_vault
|
|
16
|
+
|
|
17
|
+
__version__ = "1.0.0"
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
"CantConnectToServiceError",
|
|
21
|
+
"NoServiceResponseError",
|
|
22
|
+
"Secret",
|
|
23
|
+
"Valt0Error",
|
|
24
|
+
"Vault",
|
|
25
|
+
"open_vault",
|
|
26
|
+
]
|
valt0/_config.py
ADDED
valt0/_crypto.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"""AES-GCM and X25519 helpers. Port of internal/crypto/crypto.go."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import threading
|
|
7
|
+
from typing import Optional, Tuple, Union
|
|
8
|
+
|
|
9
|
+
from cryptography.exceptions import InvalidTag
|
|
10
|
+
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey, X25519PublicKey
|
|
11
|
+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
|
12
|
+
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
|
13
|
+
from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat
|
|
14
|
+
|
|
15
|
+
from ._errors import Valt0Error
|
|
16
|
+
|
|
17
|
+
NONCE_SIZE = 12
|
|
18
|
+
TAG_SIZE = 16
|
|
19
|
+
_AES_BLOCK_SIZE = 16
|
|
20
|
+
|
|
21
|
+
_BytesLike = Union[bytes, bytearray, memoryview]
|
|
22
|
+
|
|
23
|
+
# Like the Go client, each process uses one X25519 key pair for every secret it fetches.
|
|
24
|
+
# It is generated on first use rather than at import time.
|
|
25
|
+
_key_pair: Optional[Tuple[X25519PrivateKey, str]] = None
|
|
26
|
+
_key_lock = threading.Lock()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def ecdh_key_pair() -> Tuple[X25519PrivateKey, str]:
|
|
30
|
+
"""The process-wide X25519 private key, and its raw public key as lowercase hex."""
|
|
31
|
+
global _key_pair
|
|
32
|
+
with _key_lock:
|
|
33
|
+
if _key_pair is None:
|
|
34
|
+
key = X25519PrivateKey.generate()
|
|
35
|
+
_key_pair = (key, key.public_key().public_bytes(Encoding.Raw, PublicFormat.Raw).hex())
|
|
36
|
+
return _key_pair
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def determine_shared_key(service_public_key: bytes, private_key: X25519PrivateKey) -> bytes:
|
|
40
|
+
try:
|
|
41
|
+
public_key = X25519PublicKey.from_public_bytes(service_public_key)
|
|
42
|
+
return private_key.exchange(public_key)
|
|
43
|
+
except ValueError:
|
|
44
|
+
raise Valt0Error("invalid ECDH public key in the service response") from None
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def encrypt_data(key: bytes, data: bytes) -> Tuple[bytes, bytes]:
|
|
48
|
+
"""Seal ``data`` with AES-GCM under a fresh random nonce. Returns ``(nonce, ciphertext)``."""
|
|
49
|
+
nonce = os.urandom(NONCE_SIZE)
|
|
50
|
+
return nonce, _aead(key).encrypt(nonce, data, None)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def decrypt_data(key: bytes, nonce: _BytesLike, ciphertext: _BytesLike) -> bytes:
|
|
54
|
+
try:
|
|
55
|
+
return _aead(key).decrypt(bytes(nonce), bytes(ciphertext), None)
|
|
56
|
+
except InvalidTag:
|
|
57
|
+
raise Valt0Error("failed to decrypt data") from None
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def decrypt_secret(key: bytes, nonce: _BytesLike, ciphertext: _BytesLike) -> bytearray:
|
|
61
|
+
"""Like :func:`decrypt_data`, but the plaintext is only ever written into a ``bytearray``.
|
|
62
|
+
|
|
63
|
+
``AESGCM.decrypt`` returns immutable ``bytes``, which would leave a copy of the secret that
|
|
64
|
+
nothing can wipe. Decrypting into our own buffers keeps a single copy that
|
|
65
|
+
:meth:`Secret.destroy` can zero, as Go's ``gcm.Open`` does.
|
|
66
|
+
"""
|
|
67
|
+
if len(ciphertext) < TAG_SIZE:
|
|
68
|
+
raise Valt0Error("failed to decrypt data")
|
|
69
|
+
body = memoryview(ciphertext)[:-TAG_SIZE]
|
|
70
|
+
tag = bytes(memoryview(ciphertext)[-TAG_SIZE:])
|
|
71
|
+
try:
|
|
72
|
+
decryptor = Cipher(algorithms.AES(key), modes.GCM(bytes(nonce), tag)).decryptor()
|
|
73
|
+
except ValueError:
|
|
74
|
+
raise Valt0Error("failed to create the AES cipher") from None
|
|
75
|
+
|
|
76
|
+
# update_into() needs block_size - 1 bytes of headroom past the output it writes.
|
|
77
|
+
scratch = bytearray(len(body) + _AES_BLOCK_SIZE - 1)
|
|
78
|
+
try:
|
|
79
|
+
written = decryptor.update_into(body, scratch)
|
|
80
|
+
decryptor.finalize() # verifies the tag
|
|
81
|
+
plaintext = bytearray(written)
|
|
82
|
+
with memoryview(scratch) as view:
|
|
83
|
+
plaintext[:] = view[:written]
|
|
84
|
+
return plaintext
|
|
85
|
+
except InvalidTag:
|
|
86
|
+
raise Valt0Error("failed to decrypt data") from None
|
|
87
|
+
finally:
|
|
88
|
+
wipe(scratch)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def wipe(buffer: bytearray) -> None:
|
|
92
|
+
"""Overwrite ``buffer`` with zeros in place (same-length slice assignment never reallocates)."""
|
|
93
|
+
if buffer:
|
|
94
|
+
buffer[:] = bytes(len(buffer))
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _aead(key: bytes) -> AESGCM:
|
|
98
|
+
try:
|
|
99
|
+
return AESGCM(key)
|
|
100
|
+
except ValueError:
|
|
101
|
+
raise Valt0Error("failed to create the AES cipher") from None
|
valt0/_errors.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Errors raised by the client. Port of the sentinel errors in valt0.go."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import socket
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Valt0Error(Exception):
|
|
9
|
+
"""Base class for every error the Valt0 client raises."""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class CantConnectToServiceError(Valt0Error):
|
|
13
|
+
"""The session file or the service socket does not exist: is the valt0 service running?"""
|
|
14
|
+
|
|
15
|
+
def __init__(self, message: str = "could not connect to Valt0 service") -> None:
|
|
16
|
+
super().__init__(message)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class NoServiceResponseError(Valt0Error):
|
|
20
|
+
"""The service did not answer before the transport deadline."""
|
|
21
|
+
|
|
22
|
+
def __init__(self, message: str = "no response received from the Valt0 service") -> None:
|
|
23
|
+
super().__init__(message)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def os_error(exc: OSError, context: str) -> Valt0Error:
|
|
27
|
+
"""Classify a low-level failure the way the Go client's OpenVault does.
|
|
28
|
+
|
|
29
|
+
Timeouts become NoServiceResponseError and a missing session file or socket (ENOENT)
|
|
30
|
+
becomes CantConnectToServiceError; anything else is a Valt0Error that says what failed.
|
|
31
|
+
Raise the result ``from exc`` so the original error stays attached as the cause.
|
|
32
|
+
"""
|
|
33
|
+
# socket.timeout is TimeoutError from Python 3.10 on, a separate OSError subclass before.
|
|
34
|
+
if isinstance(exc, (socket.timeout, TimeoutError)):
|
|
35
|
+
return NoServiceResponseError()
|
|
36
|
+
if isinstance(exc, FileNotFoundError):
|
|
37
|
+
return CantConnectToServiceError()
|
|
38
|
+
return Valt0Error(f"{context}: {exc.strerror or exc}")
|
valt0/_protocol.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""Messages exchanged with the service. Port of internal/vault_load_request.go."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from enum import IntEnum
|
|
8
|
+
from typing import Any, Dict, Optional
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ServiceMethodType(IntEnum):
|
|
12
|
+
REQUEST_LOAD_VAULT = 0
|
|
13
|
+
REQUEST_FETCH_SECRET = 1
|
|
14
|
+
RESPONSE = 2
|
|
15
|
+
REQUEST_STATUS = 3
|
|
16
|
+
INTERNAL_ERROR = 4
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ServiceResponseStatus(IntEnum):
|
|
20
|
+
SUCCESS = 0
|
|
21
|
+
ERROR = 1
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def load_vault_request(vault_id: str, vault_name: str) -> bytes:
|
|
25
|
+
return _encode({"vaultId": vault_id, "vaultName": vault_name})
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def fetch_secret_request(vault_id: str, vault_name: str, ecdh_pub_key: str, secret_name: str) -> bytes:
|
|
29
|
+
return _encode(
|
|
30
|
+
{"vaultId": vault_id, "vaultName": vault_name, "ecdhPubKey": ecdh_pub_key, "secretName": secret_name}
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _encode(message: Dict[str, str]) -> bytes:
|
|
35
|
+
# Compact, with the fields in the Go structs' order, like encoding/json.
|
|
36
|
+
return json.dumps(message, separators=(",", ":")).encode("ascii")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(frozen=True)
|
|
40
|
+
class ServiceResponse:
|
|
41
|
+
status: int
|
|
42
|
+
ecdh_pub_key: str
|
|
43
|
+
body: Dict[str, Any]
|
|
44
|
+
|
|
45
|
+
@classmethod
|
|
46
|
+
def from_json(cls, data: bytes) -> Optional[ServiceResponse]:
|
|
47
|
+
"""Parse a response document; ``None`` if it isn't one."""
|
|
48
|
+
try:
|
|
49
|
+
message = json.loads(data)
|
|
50
|
+
except ValueError:
|
|
51
|
+
return None
|
|
52
|
+
if not isinstance(message, dict):
|
|
53
|
+
return None
|
|
54
|
+
status = message.get("status")
|
|
55
|
+
pub_key = message.get("ecdhPubKey")
|
|
56
|
+
body = message.get("body")
|
|
57
|
+
# Absent or null fields keep their zero value, as with Go's json.Unmarshal.
|
|
58
|
+
if status is None:
|
|
59
|
+
status = ServiceResponseStatus.SUCCESS
|
|
60
|
+
if pub_key is None:
|
|
61
|
+
pub_key = ""
|
|
62
|
+
if body is None:
|
|
63
|
+
body = {}
|
|
64
|
+
if isinstance(status, bool) or not isinstance(status, int) or not 0 <= status <= 255:
|
|
65
|
+
return None
|
|
66
|
+
if not isinstance(pub_key, str) or not isinstance(body, dict):
|
|
67
|
+
return None
|
|
68
|
+
return cls(status=status, ecdh_pub_key=pub_key, body=body)
|
|
69
|
+
|
|
70
|
+
def error_message(self) -> str:
|
|
71
|
+
error = self.body.get("error")
|
|
72
|
+
if error is None:
|
|
73
|
+
return "undefined error"
|
|
74
|
+
return error if isinstance(error, str) else str(error)
|
valt0/_secret.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""The Secret value type. Port of internal/secret.go."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, NoReturn, Union
|
|
6
|
+
|
|
7
|
+
from ._crypto import wipe
|
|
8
|
+
|
|
9
|
+
_REDACTED = "[REDACTED]"
|
|
10
|
+
|
|
11
|
+
# Secret has a method called ``bytes``, so annotations inside the class body use these aliases.
|
|
12
|
+
_BytesLike = Union[bytes, bytearray, memoryview]
|
|
13
|
+
_Buffer = bytearray
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Secret:
|
|
17
|
+
"""A secret fetched from a vault.
|
|
18
|
+
|
|
19
|
+
Its value never shows up in ``str()``, ``repr()``, f-strings, logging or tracebacks, and it
|
|
20
|
+
refuses to be pickled or copied. Read it explicitly with :meth:`bytes` (preferred) or
|
|
21
|
+
:meth:`reveal`, and wipe it with :meth:`destroy`, or use the secret as a context manager.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
__slots__ = ("_value",)
|
|
25
|
+
|
|
26
|
+
def __init__(self, value: _BytesLike) -> None:
|
|
27
|
+
self._value: _Buffer = bytearray(value)
|
|
28
|
+
|
|
29
|
+
@classmethod
|
|
30
|
+
def _adopt(cls, buffer: _Buffer) -> Secret:
|
|
31
|
+
"""Wrap ``buffer`` without copying it, so the decrypted value exists only once."""
|
|
32
|
+
secret = cls.__new__(cls)
|
|
33
|
+
secret._value = buffer
|
|
34
|
+
return secret
|
|
35
|
+
|
|
36
|
+
def bytes(self) -> _Buffer:
|
|
37
|
+
"""The value as the underlying ``bytearray`` (not a copy), which :meth:`destroy` wipes.
|
|
38
|
+
|
|
39
|
+
This is the preferred accessor.
|
|
40
|
+
"""
|
|
41
|
+
return self._value
|
|
42
|
+
|
|
43
|
+
def reveal(self) -> str:
|
|
44
|
+
"""The value decoded as UTF-8.
|
|
45
|
+
|
|
46
|
+
Python strings are immutable and can't be wiped, so use this sparingly. Raises
|
|
47
|
+
``ValueError`` if the value is not valid UTF-8; use :meth:`bytes` for binary secrets.
|
|
48
|
+
"""
|
|
49
|
+
try:
|
|
50
|
+
return self._value.decode("utf-8")
|
|
51
|
+
except UnicodeDecodeError:
|
|
52
|
+
pass # raised below, outside the handler, so the error doesn't carry the value along
|
|
53
|
+
raise ValueError("secret is not valid UTF-8 text; use bytes() instead")
|
|
54
|
+
|
|
55
|
+
def destroy(self) -> None:
|
|
56
|
+
"""Zero the value in place. Buffers returned by :meth:`bytes` are zeroed too, and later
|
|
57
|
+
calls return empty values."""
|
|
58
|
+
wipe(self._value)
|
|
59
|
+
self._value = bytearray()
|
|
60
|
+
|
|
61
|
+
def __enter__(self) -> Secret:
|
|
62
|
+
return self
|
|
63
|
+
|
|
64
|
+
def __exit__(self, *exc_info: Any) -> None:
|
|
65
|
+
self.destroy()
|
|
66
|
+
|
|
67
|
+
# Everything that could leak the value renders it redacted instead.
|
|
68
|
+
def __str__(self) -> str:
|
|
69
|
+
return _REDACTED
|
|
70
|
+
|
|
71
|
+
def __repr__(self) -> str:
|
|
72
|
+
return f"Secret({_REDACTED})"
|
|
73
|
+
|
|
74
|
+
def __format__(self, format_spec: str) -> str:
|
|
75
|
+
return format(_REDACTED, format_spec)
|
|
76
|
+
|
|
77
|
+
def __reduce_ex__(self, protocol: Any) -> NoReturn:
|
|
78
|
+
raise TypeError("Secret objects cannot be pickled or copied")
|
valt0/_service.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""Starting the service in deploy mode. Port of the service-start half of internal/vault.go.
|
|
2
|
+
|
|
3
|
+
With ``VALT0_MODE=deploy`` (typically in a container) the client starts the Valt0 service itself,
|
|
4
|
+
once per process, by running ``valt0 service --client-start``.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
import subprocess
|
|
11
|
+
import threading
|
|
12
|
+
from typing import Optional
|
|
13
|
+
|
|
14
|
+
from ._errors import Valt0Error
|
|
15
|
+
|
|
16
|
+
MODE_ENV = "VALT0_MODE"
|
|
17
|
+
START_TIMEOUT = 5.0 # seconds
|
|
18
|
+
|
|
19
|
+
_lock = threading.Lock()
|
|
20
|
+
_started = False
|
|
21
|
+
_failure: Optional[Valt0Error] = None
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def is_in_deploy_mode() -> bool:
|
|
25
|
+
return os.environ.get(MODE_ENV, "").lower() == "deploy"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def ensure_service_started() -> None:
|
|
29
|
+
"""Start the service on the first call; later calls reuse the outcome.
|
|
30
|
+
|
|
31
|
+
Like Go's ``sync.OnceValue``, a failed start is not retried: every later call raises the
|
|
32
|
+
same error. Concurrent first calls wait for the one start attempt.
|
|
33
|
+
"""
|
|
34
|
+
global _started, _failure
|
|
35
|
+
with _lock:
|
|
36
|
+
if not _started and _failure is None:
|
|
37
|
+
try:
|
|
38
|
+
_check_cli_tool()
|
|
39
|
+
_start_service()
|
|
40
|
+
_started = True
|
|
41
|
+
except Valt0Error as exc:
|
|
42
|
+
_failure = exc
|
|
43
|
+
failure = _failure
|
|
44
|
+
if failure is not None:
|
|
45
|
+
raise Valt0Error(f"service was not started: {failure}") from failure
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _check_cli_tool() -> None:
|
|
49
|
+
try:
|
|
50
|
+
subprocess.run(["valt0", "-v"], stdin=subprocess.DEVNULL, capture_output=True, check=True)
|
|
51
|
+
except (OSError, subprocess.CalledProcessError):
|
|
52
|
+
raise Valt0Error("could not find valt0, is the tool installed and available to the current user") from None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _start_service() -> None:
|
|
56
|
+
command = ["valt0", "service", "--client-start"]
|
|
57
|
+
try:
|
|
58
|
+
result = subprocess.run(
|
|
59
|
+
command,
|
|
60
|
+
stdin=subprocess.DEVNULL,
|
|
61
|
+
stdout=subprocess.PIPE,
|
|
62
|
+
stderr=subprocess.STDOUT,
|
|
63
|
+
timeout=START_TIMEOUT,
|
|
64
|
+
check=False,
|
|
65
|
+
)
|
|
66
|
+
except subprocess.TimeoutExpired as exc:
|
|
67
|
+
raise Valt0Error(f"starting valt0 service: timed out after {START_TIMEOUT:g}s: {_text(exc.output)}") from None
|
|
68
|
+
except OSError as exc:
|
|
69
|
+
raise Valt0Error(f"starting valt0 service: {exc}") from exc
|
|
70
|
+
if result.returncode != 0:
|
|
71
|
+
status = f"exit status {result.returncode}" if result.returncode > 0 else f"signal {-result.returncode}"
|
|
72
|
+
raise Valt0Error(f"starting valt0 service: {status}: {_text(result.stdout)}")
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _text(output: Optional[bytes]) -> str:
|
|
76
|
+
return (output or b"").decode("utf-8", "replace").strip()
|
valt0/_session.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""The service session file. Port of internal/session/session.go."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import binascii
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
from typing import Any, Dict
|
|
10
|
+
|
|
11
|
+
from ._errors import Valt0Error, os_error
|
|
12
|
+
|
|
13
|
+
APP_DIR = ".valt0"
|
|
14
|
+
APP_DIR_ENV = "VALT0_APPDIR"
|
|
15
|
+
SESSION_FILE = "session.json"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True)
|
|
19
|
+
class Session:
|
|
20
|
+
tcp_port: int
|
|
21
|
+
socket_path: str
|
|
22
|
+
session_key: str = field(repr=False)
|
|
23
|
+
|
|
24
|
+
def encryption_key(self) -> bytes:
|
|
25
|
+
try:
|
|
26
|
+
return binascii.unhexlify(self.session_key)
|
|
27
|
+
except ValueError: # binascii.Error, or a non-ASCII string
|
|
28
|
+
raise Valt0Error("failed to decode session encryption key") from None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def app_dir() -> str:
|
|
32
|
+
# Like Go's os.LookupEnv: a variable that is set but empty still wins.
|
|
33
|
+
if APP_DIR_ENV in os.environ:
|
|
34
|
+
return os.environ[APP_DIR_ENV]
|
|
35
|
+
home = os.path.expanduser("~")
|
|
36
|
+
if home == "~":
|
|
37
|
+
raise Valt0Error("failed to read session: could not determine the home directory")
|
|
38
|
+
return os.path.join(home, APP_DIR)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def load_session() -> Session:
|
|
42
|
+
"""Read the session the running service wrote to ``<app dir>/session.json``."""
|
|
43
|
+
path = os.path.join(app_dir(), SESSION_FILE)
|
|
44
|
+
try:
|
|
45
|
+
with open(path, "rb") as fh:
|
|
46
|
+
raw = fh.read()
|
|
47
|
+
except OSError as exc:
|
|
48
|
+
# A missing file means no service is running: CantConnectToServiceError.
|
|
49
|
+
raise os_error(exc, f"failed to read session {path}") from exc
|
|
50
|
+
|
|
51
|
+
data = _parse(raw)
|
|
52
|
+
if data is _INVALID:
|
|
53
|
+
raise Valt0Error(f"failed to parse session file {path}: invalid JSON")
|
|
54
|
+
if not isinstance(data, dict):
|
|
55
|
+
raise Valt0Error(f"failed to parse session file {path}: not a JSON object")
|
|
56
|
+
|
|
57
|
+
return Session(
|
|
58
|
+
tcp_port=_field(data, "tcpPort", 0, path),
|
|
59
|
+
socket_path=_field(data, "socketPath", "", path),
|
|
60
|
+
session_key=_field(data, "sessionKey", "", path),
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
_INVALID = object()
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _parse(raw: bytes) -> Any:
|
|
68
|
+
# The decode error carries the whole document, session key included, so it is dropped here
|
|
69
|
+
# rather than chained onto the error the caller raises.
|
|
70
|
+
try:
|
|
71
|
+
return json.loads(raw)
|
|
72
|
+
except ValueError:
|
|
73
|
+
return _INVALID
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _field(data: Dict[str, Any], name: str, default: Any, path: str) -> Any:
|
|
77
|
+
"""``data[name]``, which must have the type of ``default``. Absent or null fields keep their
|
|
78
|
+
zero value, as with Go's json.Unmarshal."""
|
|
79
|
+
value = data.get(name)
|
|
80
|
+
if value is None:
|
|
81
|
+
return default
|
|
82
|
+
if type(value) is not type(default): # exact: JSON true is not an integer here
|
|
83
|
+
kind = "an integer" if isinstance(default, int) else "a string"
|
|
84
|
+
raise Valt0Error(f"failed to parse session file {path}: {name} must be {kind}")
|
|
85
|
+
return value
|
valt0/_transport.py
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"""Talking to the service over its unix socket or TCP port. Port of internal/transport.go.
|
|
2
|
+
|
|
3
|
+
Every request is one connection carrying one frame each way::
|
|
4
|
+
|
|
5
|
+
[1 byte message type][4 byte big-endian payload length][payload]
|
|
6
|
+
|
|
7
|
+
where the payload is a 12-byte nonce followed by the AES-GCM ciphertext of a JSON document,
|
|
8
|
+
sealed with the session key from ``session.json``.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import os
|
|
14
|
+
import socket
|
|
15
|
+
import struct
|
|
16
|
+
import sys
|
|
17
|
+
import time
|
|
18
|
+
from typing import Tuple
|
|
19
|
+
|
|
20
|
+
from . import _config
|
|
21
|
+
from ._crypto import NONCE_SIZE, decrypt_data, encrypt_data
|
|
22
|
+
from ._errors import NoServiceResponseError, Valt0Error, os_error
|
|
23
|
+
from ._protocol import ServiceMethodType, ServiceResponse, ServiceResponseStatus, load_vault_request
|
|
24
|
+
from ._session import Session
|
|
25
|
+
|
|
26
|
+
SOCKET_PATH_ENV = "VALT0_SOCKET_PATH"
|
|
27
|
+
TRANSPORT_CHANNEL_ENV = "VALT0_TRANSPORT_CHANNEL"
|
|
28
|
+
CONTAINER_HOST_ADDR = "host.docker.internal"
|
|
29
|
+
# On a Windows host the service is on the same machine. CPython has no AF_UNIX there, so the
|
|
30
|
+
# client always uses TCP, to the loopback address.
|
|
31
|
+
WINDOWS_HOST_ADDR = "127.0.0.1"
|
|
32
|
+
|
|
33
|
+
_HEADER = struct.Struct(">BI")
|
|
34
|
+
_READ_CHUNK = 64 * 1024
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def load_vault(vault_id: str, vault_name: str, session: Session) -> None:
|
|
38
|
+
request = load_vault_request(vault_id, vault_name)
|
|
39
|
+
response = make_request(request, session, ServiceMethodType.REQUEST_LOAD_VAULT)
|
|
40
|
+
if response.status == ServiceResponseStatus.ERROR:
|
|
41
|
+
raise Valt0Error(response.error_message())
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def make_request(data: bytes, session: Session, method: ServiceMethodType) -> ServiceResponse:
|
|
45
|
+
# Seal the request before dialing, so a bad session key never opens a connection.
|
|
46
|
+
key = session.encryption_key()
|
|
47
|
+
nonce, sealed = encrypt_data(key, data)
|
|
48
|
+
payload = nonce + sealed
|
|
49
|
+
frame = _HEADER.pack(method, len(payload)) + payload
|
|
50
|
+
|
|
51
|
+
with _Connection.open(session) as connection:
|
|
52
|
+
connection.send(frame)
|
|
53
|
+
plain = connection.read_response(key)
|
|
54
|
+
response = ServiceResponse.from_json(plain)
|
|
55
|
+
if response is None:
|
|
56
|
+
raise Valt0Error("invalid response from the Valt0 service")
|
|
57
|
+
return response
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class _Connection:
|
|
61
|
+
def __init__(self, sock: socket.socket, address: str) -> None:
|
|
62
|
+
self._sock = sock
|
|
63
|
+
self._address = address
|
|
64
|
+
# Like Go's conn.SetDeadline: a single deadline for the whole exchange, set once connected.
|
|
65
|
+
self._deadline = time.monotonic() + _config.TRANSPORT_TIMEOUT
|
|
66
|
+
|
|
67
|
+
@classmethod
|
|
68
|
+
def open(cls, session: Session) -> _Connection:
|
|
69
|
+
if _on_windows():
|
|
70
|
+
# Always TCP on Windows, whatever VALT0_TRANSPORT_CHANNEL says.
|
|
71
|
+
sock, address = _dial_tcp(WINDOWS_HOST_ADDR, session.tcp_port)
|
|
72
|
+
elif os.environ.get(TRANSPORT_CHANNEL_ENV) == "tcp":
|
|
73
|
+
# Like the Go client, anything other than exactly "tcp" means the unix socket.
|
|
74
|
+
sock, address = _dial_tcp(CONTAINER_HOST_ADDR, session.tcp_port)
|
|
75
|
+
else:
|
|
76
|
+
sock, address = _dial_unix(_socket_path(session))
|
|
77
|
+
return cls(sock, address)
|
|
78
|
+
|
|
79
|
+
def __enter__(self) -> _Connection:
|
|
80
|
+
return self
|
|
81
|
+
|
|
82
|
+
def __exit__(self, *exc_info: object) -> None:
|
|
83
|
+
self._sock.close()
|
|
84
|
+
|
|
85
|
+
def send(self, frame: bytes) -> None:
|
|
86
|
+
self._arm()
|
|
87
|
+
try:
|
|
88
|
+
self._sock.sendall(frame)
|
|
89
|
+
except BlockingIOError as exc: # past the deadline
|
|
90
|
+
raise NoServiceResponseError() from exc
|
|
91
|
+
except OSError as exc:
|
|
92
|
+
raise os_error(exc, f"write {self._address}") from exc
|
|
93
|
+
|
|
94
|
+
def read_response(self, key: bytes) -> bytes:
|
|
95
|
+
response_type, length = _HEADER.unpack(self._read_exactly(_HEADER.size, "response header"))
|
|
96
|
+
if length == 0:
|
|
97
|
+
raise Valt0Error("no data received")
|
|
98
|
+
payload = self._read_exactly(length, "response")
|
|
99
|
+
if len(payload) < NONCE_SIZE:
|
|
100
|
+
raise Valt0Error("could not parse response")
|
|
101
|
+
plain = decrypt_data(key, payload[:NONCE_SIZE], payload[NONCE_SIZE:])
|
|
102
|
+
if response_type == ServiceMethodType.INTERNAL_ERROR:
|
|
103
|
+
raise Valt0Error(plain.decode("utf-8", "replace") or "internal error in the Valt0 service")
|
|
104
|
+
return plain
|
|
105
|
+
|
|
106
|
+
def _read_exactly(self, size: int, what: str) -> bytes:
|
|
107
|
+
# Grows with what actually arrives instead of trusting the length header up front.
|
|
108
|
+
buffer = bytearray()
|
|
109
|
+
while len(buffer) < size:
|
|
110
|
+
self._arm()
|
|
111
|
+
try:
|
|
112
|
+
chunk = self._sock.recv(min(size - len(buffer), _READ_CHUNK))
|
|
113
|
+
except BlockingIOError as exc: # past the deadline, and nothing more has arrived
|
|
114
|
+
raise NoServiceResponseError() from exc
|
|
115
|
+
except OSError as exc:
|
|
116
|
+
raise os_error(exc, f"read {self._address}") from exc
|
|
117
|
+
if not chunk:
|
|
118
|
+
raise Valt0Error(f"the Valt0 service closed the connection before sending the full {what}")
|
|
119
|
+
buffer += chunk
|
|
120
|
+
return bytes(buffer)
|
|
121
|
+
|
|
122
|
+
def _arm(self) -> None:
|
|
123
|
+
# Past the deadline the socket goes non-blocking: no more waiting, but whatever has already
|
|
124
|
+
# arrived is still read. With many threads, one can get the GIL back after its deadline
|
|
125
|
+
# even though the service answered in time; that is not a missing response.
|
|
126
|
+
remaining = self._deadline - time.monotonic()
|
|
127
|
+
self._sock.settimeout(remaining if remaining > 0 else 0.0)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _on_windows() -> bool:
|
|
131
|
+
return sys.platform == "win32"
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _socket_path(session: Session) -> str:
|
|
135
|
+
# Like Go's os.LookupEnv: a variable that is set but empty still wins.
|
|
136
|
+
if SOCKET_PATH_ENV in os.environ:
|
|
137
|
+
return os.environ[SOCKET_PATH_ENV]
|
|
138
|
+
return session.socket_path
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _dial_unix(path: str) -> Tuple[socket.socket, str]:
|
|
142
|
+
address = f"unix {path}"
|
|
143
|
+
if not hasattr(socket, "AF_UNIX"):
|
|
144
|
+
raise Valt0Error(f"dial {address}: unix domain sockets are not supported by Python on this platform")
|
|
145
|
+
target = path
|
|
146
|
+
if sys.platform in ("linux", "android") and path.startswith("@"):
|
|
147
|
+
target = "\0" + path[1:] # Go treats a leading "@" as the Linux abstract namespace
|
|
148
|
+
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
149
|
+
try:
|
|
150
|
+
sock.settimeout(_config.TRANSPORT_TIMEOUT)
|
|
151
|
+
sock.connect(target)
|
|
152
|
+
except OSError as exc:
|
|
153
|
+
sock.close()
|
|
154
|
+
raise os_error(exc, f"dial {address}") from exc
|
|
155
|
+
except BaseException:
|
|
156
|
+
sock.close()
|
|
157
|
+
raise
|
|
158
|
+
return sock, address
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _dial_tcp(host: str, port: int) -> Tuple[socket.socket, str]:
|
|
162
|
+
address = f"tcp {host}:{port}"
|
|
163
|
+
if not 0 <= port <= 65535:
|
|
164
|
+
raise Valt0Error(f"dial {address}: invalid port")
|
|
165
|
+
try:
|
|
166
|
+
sock = socket.create_connection((host, port), timeout=_config.TRANSPORT_TIMEOUT)
|
|
167
|
+
except OSError as exc:
|
|
168
|
+
raise os_error(exc, f"dial {address}") from exc
|
|
169
|
+
return sock, address
|
valt0/_vault.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""Opening vaults and fetching secrets. Port of internal/vault.go and internal/transport_vault.go."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import binascii
|
|
6
|
+
import os
|
|
7
|
+
|
|
8
|
+
from . import _service
|
|
9
|
+
from ._config import VAULT_FILE_EXT
|
|
10
|
+
from ._crypto import NONCE_SIZE, decrypt_secret, determine_shared_key, ecdh_key_pair
|
|
11
|
+
from ._errors import Valt0Error
|
|
12
|
+
from ._protocol import ServiceMethodType, ServiceResponseStatus, fetch_secret_request
|
|
13
|
+
from ._secret import Secret
|
|
14
|
+
from ._session import Session, load_session
|
|
15
|
+
from ._transport import load_vault, make_request
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def open_vault(*, id: str, name: str) -> Vault:
|
|
19
|
+
"""Open the vault identified by ``id`` in the local Valt0 service.
|
|
20
|
+
|
|
21
|
+
``name`` is only a label for messages; ``.valt0`` is appended if it's missing.
|
|
22
|
+
|
|
23
|
+
Raises :class:`CantConnectToServiceError` if the session file or the service socket does not
|
|
24
|
+
exist, :class:`NoServiceResponseError` if the service does not answer in time, and
|
|
25
|
+
:class:`Valt0Error` for anything else, e.g. a vault that can't be loaded.
|
|
26
|
+
"""
|
|
27
|
+
if not isinstance(id, str) or not isinstance(name, str):
|
|
28
|
+
raise TypeError("open_vault() id and name must be strings")
|
|
29
|
+
if _service.is_in_deploy_mode():
|
|
30
|
+
_service.ensure_service_started()
|
|
31
|
+
vault_name = vault_name_with_ext(name)
|
|
32
|
+
session = load_session()
|
|
33
|
+
load_vault(id, vault_name, session)
|
|
34
|
+
return Vault(id, vault_name, session)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class Vault:
|
|
38
|
+
"""A vault opened with :func:`open_vault`."""
|
|
39
|
+
|
|
40
|
+
__slots__ = ("_id", "_name", "_session")
|
|
41
|
+
|
|
42
|
+
def __init__(self, id: str, name: str, session: Session) -> None:
|
|
43
|
+
self._id = id
|
|
44
|
+
self._name = name
|
|
45
|
+
self._session = session
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def id(self) -> str:
|
|
49
|
+
"""The vault's id."""
|
|
50
|
+
return self._id
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def name(self) -> str:
|
|
54
|
+
"""The vault's name, with its ``.valt0`` extension."""
|
|
55
|
+
return self._name
|
|
56
|
+
|
|
57
|
+
def secret(self, name: str) -> Secret:
|
|
58
|
+
"""Fetch one secret by name.
|
|
59
|
+
|
|
60
|
+
Raises the same errors as :func:`open_vault`, e.g. :class:`Valt0Error` if the secret
|
|
61
|
+
doesn't exist.
|
|
62
|
+
"""
|
|
63
|
+
if not isinstance(name, str):
|
|
64
|
+
raise TypeError("secret() name must be a string")
|
|
65
|
+
private_key, public_key_hex = ecdh_key_pair()
|
|
66
|
+
request = fetch_secret_request(self._id, self._name, public_key_hex, name)
|
|
67
|
+
response = make_request(request, self._session, ServiceMethodType.REQUEST_FETCH_SECRET)
|
|
68
|
+
if response.status == ServiceResponseStatus.ERROR:
|
|
69
|
+
raise Valt0Error(response.error_message())
|
|
70
|
+
|
|
71
|
+
if "secret" not in response.body:
|
|
72
|
+
raise Valt0Error(f"secret '{name}' not found")
|
|
73
|
+
service_public_key = _unhex(response.ecdh_pub_key, "service public key")
|
|
74
|
+
sealed_hex = response.body["secret"]
|
|
75
|
+
if not isinstance(sealed_hex, str):
|
|
76
|
+
raise Valt0Error("secret is not a string")
|
|
77
|
+
sealed = _unhex(sealed_hex, "secret")
|
|
78
|
+
if len(sealed) < NONCE_SIZE:
|
|
79
|
+
raise Valt0Error("secret size is invalid")
|
|
80
|
+
|
|
81
|
+
shared_key = determine_shared_key(service_public_key, private_key)
|
|
82
|
+
value = decrypt_secret(shared_key, sealed[:NONCE_SIZE], sealed[NONCE_SIZE:])
|
|
83
|
+
return Secret._adopt(value)
|
|
84
|
+
|
|
85
|
+
def __repr__(self) -> str:
|
|
86
|
+
return f"Vault(id={self._id!r}, name={self._name!r})"
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def vault_name_with_ext(vault_path: str) -> str:
|
|
90
|
+
"""The base name of ``vault_path``, ensuring it carries the vault file extension.
|
|
91
|
+
|
|
92
|
+
Follows Go's ``filepath.Base`` and ``filepath.Ext``, which differ from ``os.path`` at the
|
|
93
|
+
edges (``""`` and ``"."`` give ``"..valt0"``, ``".valt0"`` is already an extension).
|
|
94
|
+
"""
|
|
95
|
+
name = _go_base(vault_path)
|
|
96
|
+
dot = name.rfind(".")
|
|
97
|
+
extension = name[dot:] if dot >= 0 else ""
|
|
98
|
+
if extension != VAULT_FILE_EXT:
|
|
99
|
+
return name + VAULT_FILE_EXT
|
|
100
|
+
return name
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _go_base(path: str) -> str:
|
|
104
|
+
if path == "":
|
|
105
|
+
return "."
|
|
106
|
+
separators = os.sep + (os.altsep or "")
|
|
107
|
+
path = path.rstrip(separators)
|
|
108
|
+
if path == "":
|
|
109
|
+
return os.sep
|
|
110
|
+
return os.path.basename(path) or os.sep
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _unhex(value: str, what: str) -> bytes:
|
|
114
|
+
try:
|
|
115
|
+
return binascii.unhexlify(value)
|
|
116
|
+
except ValueError: # binascii.Error, or a non-ASCII string
|
|
117
|
+
raise Valt0Error(f"invalid hex encoding in the {what}") from None
|
valt0/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: valt0-client
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Python client for Valt0: fetch secrets from a local Valt0 vault at runtime
|
|
5
|
+
Project-URL: Homepage, https://valt0.com
|
|
6
|
+
Project-URL: Issues, https://github.com/byte1620/valt0-issues/issues
|
|
7
|
+
Author: BYTE 1620
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: client,secrets,valt0,vault
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: MacOS
|
|
13
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Topic :: Security
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.9
|
|
25
|
+
Requires-Dist: cryptography>=42
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# valt0
|
|
29
|
+
|
|
30
|
+
Python client for **Valt0**. It fetches secrets from a local Valt0 vault at runtime, so you don't
|
|
31
|
+
have to keep them in `.env` files, config files, or CI variables.
|
|
32
|
+
|
|
33
|
+
- **License:** Apache-2.0
|
|
34
|
+
- **Requires:** Python >= 3.9, and the `valt0` service/CLI installed and running locally
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install valt0-client
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The package is installed as `valt0-client` and imported as `valt0`.
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
import valt0
|
|
52
|
+
|
|
53
|
+
vault = valt0.open_vault(id="your-vault-id", name="your-vault-name")
|
|
54
|
+
|
|
55
|
+
# The with block zeroes the underlying buffer as soon as you are finished
|
|
56
|
+
with vault.secret("DATABASE_PASSWORD") as secret:
|
|
57
|
+
# Preferred: the raw bytes, wiped on exit
|
|
58
|
+
pw = secret.bytes()
|
|
59
|
+
|
|
60
|
+
# Or, when a string is unavoidable
|
|
61
|
+
db.connect(password=secret.reveal())
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Without a `with` block, call `secret.destroy()` yourself, ideally in a `finally`.
|
|
65
|
+
|
|
66
|
+
### Secrets are redacted by default
|
|
67
|
+
|
|
68
|
+
A `Secret` won't leak its value into a log line, a traceback, an f-string, or a pickle. You only
|
|
69
|
+
get the plaintext when you ask for it explicitly:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
print(secret) # [REDACTED]
|
|
73
|
+
print(repr(secret)) # Secret([REDACTED])
|
|
74
|
+
f"{secret}" # [REDACTED]
|
|
75
|
+
logging.info("connecting %s", secret) # connecting [REDACTED]
|
|
76
|
+
json.dumps({"pw": secret}, default=str) # {"pw": "[REDACTED]"}
|
|
77
|
+
pickle.dumps(secret) # TypeError: Secret objects cannot be pickled or copied
|
|
78
|
+
|
|
79
|
+
secret.reveal() # the actual value
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Redaction covers `str()`, `repr()` and `format()`. Pickling and copying raise `TypeError`, so a
|
|
83
|
+
secret can't slip out through `multiprocessing` or a cache.
|
|
84
|
+
|
|
85
|
+
### Errors
|
|
86
|
+
|
|
87
|
+
`open_vault` and `Vault.secret` raise two specific errors for the most common connection failures:
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
try:
|
|
91
|
+
vault = valt0.open_vault(id="your-vault-id", name="your-vault-name")
|
|
92
|
+
except valt0.CantConnectToServiceError:
|
|
93
|
+
... # socket or session file not found: is the valt0 service running?
|
|
94
|
+
except valt0.NoServiceResponseError:
|
|
95
|
+
... # the service did not answer within the transport timeout (1s)
|
|
96
|
+
except valt0.Valt0Error:
|
|
97
|
+
... # any other error, e.g. the vault could not be loaded
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Threads and asyncio
|
|
101
|
+
|
|
102
|
+
The client is synchronous and thread-safe; every call uses its own connection. From async code,
|
|
103
|
+
run it in a worker thread:
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
secret = await asyncio.to_thread(vault.secret, "DATABASE_PASSWORD")
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## API
|
|
112
|
+
|
|
113
|
+
### `open_vault(*, id: str, name: str) -> Vault`
|
|
114
|
+
|
|
115
|
+
Opens the vault identified by `id`.
|
|
116
|
+
Raises an error if the service is unreachable or the vault cannot be loaded.
|
|
117
|
+
|
|
118
|
+
| Argument | Type | Description |
|
|
119
|
+
| -------- | ----- | ----------------------------------------------------------- |
|
|
120
|
+
| `id` | `str` | Vault identifier — the only value used to locate the vault |
|
|
121
|
+
| `name` | `str` | Display label for messages; `.valt0` is appended if missing |
|
|
122
|
+
|
|
123
|
+
> **Note:** `name` can be anything. Valt0 does not check that it matches the vault `id` — it is
|
|
124
|
+
> only used for messaging purposes, so pick whatever makes the vault easy to recognise.
|
|
125
|
+
|
|
126
|
+
### `Vault`
|
|
127
|
+
|
|
128
|
+
| Member | Description |
|
|
129
|
+
| -------------- | ------------------------------------ |
|
|
130
|
+
| `id` | The vault's id |
|
|
131
|
+
| `name` | The vault's name |
|
|
132
|
+
| `secret(name)` | `Secret`: fetches one secret by name |
|
|
133
|
+
|
|
134
|
+
### `Secret`
|
|
135
|
+
|
|
136
|
+
| Member | Description |
|
|
137
|
+
| ------------- | ----------------------------------------------------------------------------------------------------------------- |
|
|
138
|
+
| `bytes()` | The raw bytes as a `bytearray` (the underlying buffer, not a copy). This is the preferred accessor. |
|
|
139
|
+
| `reveal()` | The value as a `str`. Strings are immutable and can't be wiped, so use sparingly. `ValueError` if not UTF-8 text. |
|
|
140
|
+
| `destroy()` | Zeroes the buffer. Buffers returned by `bytes()` are zeroed too, and later calls return empty values. |
|
|
141
|
+
| `with secret` | Calls `destroy()` when the block exits. |
|
|
142
|
+
|
|
143
|
+
### Errors
|
|
144
|
+
|
|
145
|
+
| Error | Raised when |
|
|
146
|
+
| --------------------------- | ------------------------------------------------- |
|
|
147
|
+
| `Valt0Error` | Base class of every error the client raises |
|
|
148
|
+
| `CantConnectToServiceError` | The service socket or session file does not exist |
|
|
149
|
+
| `NoServiceResponseError` | The service did not respond before the deadline |
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## Platform support
|
|
154
|
+
|
|
155
|
+
Linux, macOS and Windows, including inside containers.
|
|
156
|
+
|
|
157
|
+
On Linux and macOS the client reaches the service through its unix socket, or over TCP to
|
|
158
|
+
`host.docker.internal` when `VALT0_TRANSPORT_CHANNEL=tcp`. On Windows it always uses TCP to
|
|
159
|
+
`127.0.0.1` (CPython has no `AF_UNIX` support there), and `VALT0_TRANSPORT_CHANNEL` and
|
|
160
|
+
`VALT0_SOCKET_PATH` are ignored.
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## Development
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
python -m venv .venv && . .venv/bin/activate
|
|
168
|
+
pip install -e . pytest
|
|
169
|
+
pytest
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
The tests run against a strict fake of the Valt0 service (`tests/fake_service.py`).
|
|
173
|
+
`tests/test_go_parity.py` also runs the Go client through the same scenarios and checks that both
|
|
174
|
+
clients send the same requests and return the same results; it runs when `go` is installed and the
|
|
175
|
+
Go client is checked out at `../../golang/client` (or `$VALT0_GO_CLIENT_DIR`). See
|
|
176
|
+
[RELEASING.md](RELEASING.md) for publishing.
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## Links
|
|
181
|
+
|
|
182
|
+
- [Website](https://valt0.com)
|
|
183
|
+
- [Issue tracker](https://github.com/byte1620/valt0-issues/issues)
|
|
184
|
+
|
|
185
|
+
Maintained by BYTE 1620. Licensed under Apache-2.0.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
valt0/__init__.py,sha256=5EojyRtFgikVVlvpWuymMYLTW4G4UAZ6HZ_Gh59S52E,673
|
|
2
|
+
valt0/_config.py,sha256=JLnRommokaYef_LM3D5lcqLdxQ0f-5BtGJwO-3nYhDU,197
|
|
3
|
+
valt0/_crypto.py,sha256=fPedCJu8Fj4W44JcDUglRmLtOffJN1mNxl8evABQIPw,3826
|
|
4
|
+
valt0/_errors.py,sha256=LmbKa3SYCRBGZ5DNFNqZ3F7zSnoMbRrhIgsoL0iq3n0,1467
|
|
5
|
+
valt0/_protocol.py,sha256=CthKnRvDwjTL_WD-Y2dnor0Gnr4cfR1tjBwNgq-_aVU,2321
|
|
6
|
+
valt0/_secret.py,sha256=OQ5yhC6b7MqZEfKqv-3j62xqRCdkMqzCb_PxmEUb-AE,2591
|
|
7
|
+
valt0/_service.py,sha256=R-kPlhn24IOPnzjPXFnLDUmy0wlJ6hsRX0i8L05rjvs,2567
|
|
8
|
+
valt0/_session.py,sha256=EXf8mtyODOL4XIVCnKanl5_SFRu4nfqj9dm3kg6M2cQ,2787
|
|
9
|
+
valt0/_transport.py,sha256=GACtEX-Fi8jjlHiYOvGUbFLkUH7d07RiK5ggkDJIDY4,6766
|
|
10
|
+
valt0/_vault.py,sha256=w4_qcC6EPc4ZgAePHR2EhAJcmi4POQke9VXQS7u2i0g,4280
|
|
11
|
+
valt0/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
+
valt0_client-1.0.0.dist-info/METADATA,sha256=acZzXE5gL63P1zo5B-CFYHji9sSpJS8XzeejSd6JKYU,6807
|
|
13
|
+
valt0_client-1.0.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
14
|
+
valt0_client-1.0.0.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
15
|
+
valt0_client-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|