datadouble 0.0.3__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.
- datadouble/__init__.py +10 -0
- datadouble/announce.py +380 -0
- datadouble/anonymize.py +165 -0
- datadouble/api.py +1159 -0
- datadouble/boundary.py +47 -0
- datadouble/catalog.py +677 -0
- datadouble/channels.py +62 -0
- datadouble/ci.py +1357 -0
- datadouble/cli.py +940 -0
- datadouble/closure.py +712 -0
- datadouble/engine.py +435 -0
- datadouble/files.py +78 -0
- datadouble/images.py +265 -0
- datadouble/jailer.py +403 -0
- datadouble/names.py +114 -0
- datadouble/notify.py +262 -0
- datadouble/objects.py +195 -0
- datadouble/outcome.py +27 -0
- datadouble/pii.py +109 -0
- datadouble/planner.py +1492 -0
- datadouble/programs.py +68 -0
- datadouble/promote.py +190 -0
- datadouble/prune.py +258 -0
- datadouble/py.typed +0 -0
- datadouble/redaction.py +131 -0
- datadouble/registry.py +128 -0
- datadouble/replicate.py +562 -0
- datadouble/resolve.py +201 -0
- datadouble/run.py +1075 -0
- datadouble/runtime.py +154 -0
- datadouble/secrets.py +57 -0
- datadouble/settings.py +81 -0
- datadouble/signing.py +98 -0
- datadouble/spec.py +1590 -0
- datadouble/staging.py +346 -0
- datadouble/storage.py +222 -0
- datadouble/strict.py +498 -0
- datadouble/subset.py +406 -0
- datadouble/verify.py +171 -0
- datadouble/versions.py +25 -0
- datadouble-0.0.3.dist-info/METADATA +36 -0
- datadouble-0.0.3.dist-info/RECORD +45 -0
- datadouble-0.0.3.dist-info/WHEEL +4 -0
- datadouble-0.0.3.dist-info/entry_points.txt +12 -0
- datadouble-0.0.3.dist-info/licenses/LICENSE +21 -0
datadouble/runtime.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# Copyright (c) 2026- Tight Line LLC
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""Running engine software in the vendor's container images.
|
|
5
|
+
|
|
6
|
+
Working databases, restores for verification and engine tools such as
|
|
7
|
+
``pg_dump`` run in containers from the engine vendor's images, so Datadouble
|
|
8
|
+
itself needs no engine binaries. A container runtime adapter starts them:
|
|
9
|
+
``docker`` for any Docker-compatible daemon, ``kubernetes`` for pods.
|
|
10
|
+
|
|
11
|
+
Every container sees the paths it is given at the same absolute path as
|
|
12
|
+
Datadouble, and runs as Datadouble's own user and group, so files written on
|
|
13
|
+
either side can be read and removed by the other. Containers are labelled with
|
|
14
|
+
the stage directory they serve, so the core can remove what an interrupted
|
|
15
|
+
attempt left behind.
|
|
16
|
+
|
|
17
|
+
Runtime adapters register under the ``datadouble.runtimes`` entry point group;
|
|
18
|
+
the entry point names a callable that takes the adapter's settings and returns
|
|
19
|
+
a :class:`ContainerRuntime`.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
import os
|
|
23
|
+
from collections.abc import Callable, Mapping, Sequence
|
|
24
|
+
from contextlib import AbstractContextManager
|
|
25
|
+
from dataclasses import dataclass, field
|
|
26
|
+
from pathlib import Path
|
|
27
|
+
from typing import Protocol
|
|
28
|
+
|
|
29
|
+
from pydantic import JsonValue
|
|
30
|
+
|
|
31
|
+
from datadouble.programs import Completed
|
|
32
|
+
|
|
33
|
+
__all__ = [
|
|
34
|
+
"Completed",
|
|
35
|
+
"Container",
|
|
36
|
+
"ContainerError",
|
|
37
|
+
"ContainerRuntime",
|
|
38
|
+
"Endpoint",
|
|
39
|
+
"RuntimeFactory",
|
|
40
|
+
"Server",
|
|
41
|
+
"absolute",
|
|
42
|
+
"check",
|
|
43
|
+
"identity",
|
|
44
|
+
"runtime_failure",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class ContainerError(Exception):
|
|
49
|
+
"""A container that could not be run; the message is safe to show and log."""
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass(frozen=True, slots=True)
|
|
53
|
+
class Container:
|
|
54
|
+
"""A container to run.
|
|
55
|
+
|
|
56
|
+
``secrets`` reach the container's environment through the runtime's secret
|
|
57
|
+
channel, never a command line or a pod spec. ``port`` is the TCP port a
|
|
58
|
+
server listens on; ``ready`` is a command run inside the container that
|
|
59
|
+
succeeds once it accepts connections.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
image: str
|
|
63
|
+
command: tuple[str, ...]
|
|
64
|
+
owner: Path
|
|
65
|
+
"""The stage directory the container serves, for cleanup after an interruption."""
|
|
66
|
+
mounts: tuple[Path, ...] = ()
|
|
67
|
+
env: Mapping[str, str] = field(default_factory=dict[str, str])
|
|
68
|
+
secrets: Mapping[str, str] = field(default_factory=dict[str, str])
|
|
69
|
+
port: int | None = None
|
|
70
|
+
ready: tuple[str, ...] = ()
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@dataclass(frozen=True, slots=True)
|
|
74
|
+
class Endpoint:
|
|
75
|
+
"""Where Datadouble reaches a server's port."""
|
|
76
|
+
|
|
77
|
+
host: str
|
|
78
|
+
port: int
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class Server(Protocol):
|
|
82
|
+
"""A running server container."""
|
|
83
|
+
|
|
84
|
+
@property
|
|
85
|
+
def endpoint(self) -> Endpoint:
|
|
86
|
+
"""Where Datadouble connects to the server's port."""
|
|
87
|
+
...
|
|
88
|
+
|
|
89
|
+
def execute(self, command: Sequence[str], env: Mapping[str, str] | None = None) -> Completed:
|
|
90
|
+
"""Run a command inside the container, as its user, and wait for it."""
|
|
91
|
+
...
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class ContainerRuntime(Protocol):
|
|
95
|
+
"""Starts containers from vendor images."""
|
|
96
|
+
|
|
97
|
+
def run(self, container: Container) -> Completed:
|
|
98
|
+
"""Run a container to completion and remove it."""
|
|
99
|
+
...
|
|
100
|
+
|
|
101
|
+
def serve(self, container: Container) -> AbstractContextManager[Server]:
|
|
102
|
+
"""Start a server, wait until ``ready`` succeeds, and stop it cleanly on exit.
|
|
103
|
+
|
|
104
|
+
Stopping sends the server its normal termination signal and waits, so
|
|
105
|
+
its files are consistent once the block ends.
|
|
106
|
+
"""
|
|
107
|
+
...
|
|
108
|
+
|
|
109
|
+
def release(self, owner: Path) -> None:
|
|
110
|
+
"""Remove every container labelled with ``owner`` or a directory under it."""
|
|
111
|
+
...
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
type RuntimeFactory = Callable[[Mapping[str, JsonValue]], ContainerRuntime]
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def identity() -> str:
|
|
118
|
+
"""Datadouble's own ``uid:gid``, which containers run as."""
|
|
119
|
+
return f"{os.getuid()}:{os.getgid()}"
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
_RUNTIME_STATUSES = frozenset({125, 126, 127})
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def runtime_failure(status: int) -> bool:
|
|
126
|
+
"""Whether exit ``status`` reports the container runtime failing, not the program.
|
|
127
|
+
|
|
128
|
+
``docker run`` and ``podman run`` exit with 125 when they fail before the
|
|
129
|
+
program starts (an image that cannot be pulled, an option the daemon
|
|
130
|
+
refuses), and with 126 or 127, as a shell does, when the command cannot be
|
|
131
|
+
run or is not found; ``docker exec`` and the ``kubernetes`` runtime's
|
|
132
|
+
``sh`` wrapper exit with 126 and 127 the same way. That output is the
|
|
133
|
+
runtime's own and holds no data, so engine adapters that redact a failed
|
|
134
|
+
program's output keep it whole (decision 139). A program exiting with one
|
|
135
|
+
of these statuses itself is not told apart; the engines' tools do not.
|
|
136
|
+
"""
|
|
137
|
+
return status in _RUNTIME_STATUSES
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def check(completed: Completed, what: str) -> Completed:
|
|
141
|
+
"""``completed`` if it succeeded; otherwise an error naming ``what`` and its last line.
|
|
142
|
+
|
|
143
|
+
Engine adapters whose tools may print data values redact them first and
|
|
144
|
+
use their own check instead.
|
|
145
|
+
"""
|
|
146
|
+
if completed.status == 0:
|
|
147
|
+
return completed
|
|
148
|
+
message = f"{what} failed: {completed.last_error()}"
|
|
149
|
+
raise ContainerError(message)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def absolute(paths: Sequence[Path]) -> tuple[Path, ...]:
|
|
153
|
+
"""Paths made absolute, as the runtime mounts them."""
|
|
154
|
+
return tuple(path.resolve() for path in paths)
|
datadouble/secrets.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Copyright (c) 2026- Tight Line LLC
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""Resolving secret references.
|
|
5
|
+
|
|
6
|
+
Specs and connection settings name secrets by reference. Resolved values are
|
|
7
|
+
handed to whatever needs them and never written to manifests, artifacts or
|
|
8
|
+
logs, and each is passed to the CI system's log masking when a request names
|
|
9
|
+
one (:func:`datadouble.ci.mask`).
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import os
|
|
13
|
+
from collections.abc import Callable, Iterable, Mapping
|
|
14
|
+
from typing import assert_never
|
|
15
|
+
|
|
16
|
+
from datadouble.ci import mask
|
|
17
|
+
from datadouble.spec import EnvSecret, FileSecret, SecretRef
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class SecretError(Exception):
|
|
21
|
+
"""A secret reference that cannot be resolved."""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def resolve_secret(
|
|
25
|
+
ref: SecretRef,
|
|
26
|
+
environ: Mapping[str, str] | None = None,
|
|
27
|
+
*,
|
|
28
|
+
masked: Callable[[str], Iterable[str]] | None = None,
|
|
29
|
+
) -> str:
|
|
30
|
+
"""The value a reference points at, without trailing newlines from files.
|
|
31
|
+
|
|
32
|
+
The CI system masks the whole value, or only the parts ``masked`` picks from
|
|
33
|
+
it, for a structured secret whose layout is not itself secret.
|
|
34
|
+
"""
|
|
35
|
+
value = _resolve(ref, environ)
|
|
36
|
+
for part in (value,) if masked is None else masked(value):
|
|
37
|
+
mask(part)
|
|
38
|
+
return value
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _resolve(ref: SecretRef, environ: Mapping[str, str] | None) -> str:
|
|
42
|
+
environ = os.environ if environ is None else environ
|
|
43
|
+
match ref:
|
|
44
|
+
case EnvSecret(env=name):
|
|
45
|
+
value = environ.get(name)
|
|
46
|
+
if not value:
|
|
47
|
+
message = f"environment variable {name} is not set"
|
|
48
|
+
raise SecretError(message)
|
|
49
|
+
return value
|
|
50
|
+
case FileSecret(file=path):
|
|
51
|
+
try:
|
|
52
|
+
return path.read_text(encoding="utf-8").rstrip("\r\n")
|
|
53
|
+
except OSError as error:
|
|
54
|
+
message = f"cannot read secret file {path}: {error.strerror}"
|
|
55
|
+
raise SecretError(message) from None
|
|
56
|
+
case _:
|
|
57
|
+
assert_never(ref)
|
datadouble/settings.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Copyright (c) 2026- Tight Line LLC
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""Parsing adapter settings from the spec.
|
|
5
|
+
|
|
6
|
+
The spec passes adapter-specific settings (``sources.<name>.connection``, each entry of
|
|
7
|
+
``store``) through untouched; the adapter parses them here, at its boundary,
|
|
8
|
+
into its own strict model.
|
|
9
|
+
|
|
10
|
+
An adapter also declares which of its settings name paths that are not on the
|
|
11
|
+
machine running the job, such as an export path on an NFS server or a key
|
|
12
|
+
prefix in a bucket, with :func:`remote` on its entry point. ``ci init`` mounts
|
|
13
|
+
every other absolute path in the spec into the job, but never those
|
|
14
|
+
(decision 140).
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import json
|
|
18
|
+
from collections.abc import Callable, Mapping
|
|
19
|
+
from dataclasses import dataclass
|
|
20
|
+
|
|
21
|
+
from pydantic import BaseModel, ConfigDict, JsonValue, ValidationError
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class SettingsError(Exception):
|
|
25
|
+
"""Adapter settings that do not validate."""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class NoSettings(BaseModel):
|
|
29
|
+
"""Settings an adapter does not take: any key is an error."""
|
|
30
|
+
|
|
31
|
+
model_config = ConfigDict(strict=True, frozen=True, extra="forbid")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def parse_settings[M: BaseModel](model: type[M], settings: Mapping[str, JsonValue], what: str) -> M:
|
|
35
|
+
"""Validate ``settings`` against ``model``; ``what`` names them in errors."""
|
|
36
|
+
try:
|
|
37
|
+
return model.model_validate_json(json.dumps(dict(settings)))
|
|
38
|
+
except ValidationError as error:
|
|
39
|
+
problems = "; ".join(
|
|
40
|
+
f"{'.'.join(str(p) for p in problem['loc']) or '(settings)'}: {problem['msg']}"
|
|
41
|
+
for problem in error.errors(include_url=False)
|
|
42
|
+
)
|
|
43
|
+
message = f"{what} settings are invalid: {problems}"
|
|
44
|
+
raise SettingsError(message) from None
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
type SettingKey = tuple[str, ...]
|
|
48
|
+
"""Where a setting is inside an adapter's settings, such as ``("nfs", "path")``."""
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@dataclass(frozen=True, slots=True)
|
|
52
|
+
class Remote[**P, R]:
|
|
53
|
+
"""An adapter's entry point, with the settings that name paths off the job's machine."""
|
|
54
|
+
|
|
55
|
+
factory: Callable[P, R]
|
|
56
|
+
keys: tuple[SettingKey, ...]
|
|
57
|
+
|
|
58
|
+
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
|
|
59
|
+
"""Build the adapter, as the undecorated entry point does."""
|
|
60
|
+
return self.factory(*args, **kwargs)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def remote[**P, R](*keys: str) -> Callable[[Callable[P, R]], Remote[P, R]]:
|
|
64
|
+
"""Declare the settings, as dotted keys such as ``nfs.path``, that name remote paths.
|
|
65
|
+
|
|
66
|
+
A remote path is one the job never opens on its own machine: a path on
|
|
67
|
+
another server, a key in a bucket, a path inside a secret store. Engine
|
|
68
|
+
adapters give keys inside the source, such as ``connection.patterns``.
|
|
69
|
+
A key names its whole value, so a list or mapping there is left out too.
|
|
70
|
+
"""
|
|
71
|
+
declared = tuple(tuple(key.split(".")) for key in keys)
|
|
72
|
+
|
|
73
|
+
def declare(factory: Callable[P, R]) -> Remote[P, R]:
|
|
74
|
+
return Remote(factory, declared)
|
|
75
|
+
|
|
76
|
+
return declare
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def remote_keys(factory: object) -> tuple[SettingKey, ...]:
|
|
80
|
+
"""The settings an entry point declared remote with :func:`remote`, if any."""
|
|
81
|
+
return factory.keys if isinstance(factory, Remote) else ()
|
datadouble/signing.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# Copyright (c) 2026- Tight Line LLC
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""Signing golden images with cosign, and checking their signatures.
|
|
5
|
+
|
|
6
|
+
What is signed is the image summary without its signature field: it names
|
|
7
|
+
every archive's SHA-256, so the signature covers the image's contents. The
|
|
8
|
+
signature is a Sigstore bundle stored beside the summary. Keys are passed to
|
|
9
|
+
cosign by reference (``env://NAME`` or a path), so Datadouble never holds key
|
|
10
|
+
material.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import json
|
|
14
|
+
import tempfile
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
from typing import assert_never
|
|
17
|
+
|
|
18
|
+
from datadouble.images import ImageSummary
|
|
19
|
+
from datadouble.programs import locate, run_program
|
|
20
|
+
from datadouble.spec import CosignSigning, CosignTrust, EnvSecret, FileSecret, SecretRef
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class SigningError(Exception):
|
|
24
|
+
"""A signature that could not be made or does not verify."""
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def payload(summary: ImageSummary) -> bytes:
|
|
28
|
+
"""The bytes a signature covers: the summary without its signature, canonically."""
|
|
29
|
+
data = summary.model_copy(update={"signature": None}).model_dump(mode="json")
|
|
30
|
+
return json.dumps(data, sort_keys=True, separators=(",", ":")).encode()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def bundle_key(summary: ImageSummary) -> str:
|
|
34
|
+
"""The store key of the signature bundle beside the summary."""
|
|
35
|
+
return summary.identity.summary_key().replace(".summary.json", ".sigstore.json")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _key(ref: SecretRef) -> str:
|
|
39
|
+
match ref:
|
|
40
|
+
case EnvSecret(env=name):
|
|
41
|
+
return f"env://{name}"
|
|
42
|
+
case FileSecret(file=path):
|
|
43
|
+
return str(path)
|
|
44
|
+
case _:
|
|
45
|
+
assert_never(ref)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def cosign_binary() -> str:
|
|
49
|
+
"""``DATADOUBLE_COSIGN``, or ``cosign`` on ``PATH``."""
|
|
50
|
+
found = locate("cosign", "DATADOUBLE_COSIGN")
|
|
51
|
+
if not found:
|
|
52
|
+
message = "cosign was not found; install it or set DATADOUBLE_COSIGN"
|
|
53
|
+
raise SigningError(message)
|
|
54
|
+
return found
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _cosign(arguments: list[str], action: str) -> None:
|
|
58
|
+
done = run_program(cosign_binary(), arguments)
|
|
59
|
+
if done.status != 0:
|
|
60
|
+
message = f"{action} failed: {done.last_error()}"
|
|
61
|
+
raise SigningError(message)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def sign(summary: ImageSummary, settings: CosignSigning) -> bytes:
|
|
65
|
+
"""Sign a summary; return the Sigstore bundle."""
|
|
66
|
+
with tempfile.TemporaryDirectory(prefix="dd-sign-") as directory:
|
|
67
|
+
data = Path(directory) / "payload.json"
|
|
68
|
+
bundle = Path(directory) / "bundle.json"
|
|
69
|
+
data.write_bytes(payload(summary))
|
|
70
|
+
arguments = ["sign-blob", "--yes", f"--bundle={bundle}"]
|
|
71
|
+
if settings.key is not None:
|
|
72
|
+
arguments.append(f"--key={_key(settings.key)}")
|
|
73
|
+
if not settings.transparency_log:
|
|
74
|
+
arguments.extend(["--use-signing-config=false", "--tlog-upload=false"])
|
|
75
|
+
_cosign([*arguments, str(data)], "signing the image")
|
|
76
|
+
return bundle.read_bytes()
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def verify(summary: ImageSummary, bundle: bytes, trust: CosignTrust) -> None:
|
|
80
|
+
"""Raise :class:`SigningError` unless ``bundle`` is a trusted signature of ``summary``."""
|
|
81
|
+
with tempfile.TemporaryDirectory(prefix="dd-verify-") as directory:
|
|
82
|
+
data = Path(directory) / "payload.json"
|
|
83
|
+
signature = Path(directory) / "bundle.json"
|
|
84
|
+
data.write_bytes(payload(summary))
|
|
85
|
+
signature.write_bytes(bundle)
|
|
86
|
+
arguments = ["verify-blob", f"--bundle={signature}"]
|
|
87
|
+
if trust.public_key is not None:
|
|
88
|
+
arguments.append(f"--key={_key(trust.public_key)}")
|
|
89
|
+
else:
|
|
90
|
+
arguments.extend(
|
|
91
|
+
[
|
|
92
|
+
f"--certificate-identity={trust.identity}",
|
|
93
|
+
f"--certificate-oidc-issuer={trust.issuer}",
|
|
94
|
+
]
|
|
95
|
+
)
|
|
96
|
+
if not trust.transparency_log:
|
|
97
|
+
arguments.append("--insecure-ignore-tlog=true")
|
|
98
|
+
_cosign([*arguments, str(data)], "checking the image signature")
|