composable-data-stack 0.13.0__py3-none-any.whl → 0.15.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.
- cli/confinement.py +219 -0
- cli/getter.py +624 -13
- cli/image_updates.py +12 -6
- cli/k8s_renderer.py +701 -6
- cli/k8s_runner.py +1 -1
- cli/k8s_security.py +96 -3
- cli/main.py +144 -30
- cli/maturity.py +271 -0
- cli/planner.py +289 -15
- cli/preflight.py +10 -7
- cli/renderer.py +708 -21
- cli/resolver.py +19 -0
- cli/resources/module.schema.json +152 -3
- cli/resources/profile.schema.json +161 -0
- cli/resources/rule-schema.json +5 -0
- cli/resources/rule-set.json +60 -7
- cli/secrets.py +4 -1
- cli/security.py +285 -20
- cli/security_common.py +22 -0
- cli/up_runner.py +38 -4
- cli/utils.py +50 -0
- cli/validator.py +1193 -0
- {composable_data_stack-0.13.0.dist-info → composable_data_stack-0.15.0.dist-info}/METADATA +36 -3
- composable_data_stack-0.15.0.dist-info/RECORD +46 -0
- composable_data_stack-0.13.0.dist-info/RECORD +0 -44
- {composable_data_stack-0.13.0.dist-info → composable_data_stack-0.15.0.dist-info}/WHEEL +0 -0
- {composable_data_stack-0.13.0.dist-info → composable_data_stack-0.15.0.dist-info}/entry_points.txt +0 -0
- {composable_data_stack-0.13.0.dist-info → composable_data_stack-0.15.0.dist-info}/licenses/LICENSE +0 -0
- {composable_data_stack-0.13.0.dist-info → composable_data_stack-0.15.0.dist-info}/top_level.txt +0 -0
cli/confinement.py
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"""Declarative runtime confinement (seccomp, AppArmor, SELinux) for Compose services.
|
|
2
|
+
|
|
3
|
+
A profile declares a default under `spec.security.runtime.confinement`; a
|
|
4
|
+
module instance may override individual keys under `security.confinement`.
|
|
5
|
+
The effective value is rendered into each service's `security_opt`.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
import re
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
from .diagnostics import Diagnostic
|
|
15
|
+
|
|
16
|
+
INVALID_CONFINEMENT_CODE = "E127"
|
|
17
|
+
CONFINEMENT_KEYS = ("seccomp", "apparmor", "selinux")
|
|
18
|
+
|
|
19
|
+
_APPARMOR_NAME_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._/-]{0,127}$")
|
|
20
|
+
_DRIVE_RE = re.compile(r"^[A-Za-z]:")
|
|
21
|
+
_SELINUX_LABEL_RE = re.compile(r"^(user|role|type|level):[A-Za-z0-9_.,:-]+$")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def profile_confinement(profile_or_plan: dict[str, Any] | None) -> dict[str, Any]:
|
|
25
|
+
"""Return the profile-wide confinement block from a profile or plan."""
|
|
26
|
+
if not isinstance(profile_or_plan, dict):
|
|
27
|
+
return {}
|
|
28
|
+
security = profile_or_plan.get("security")
|
|
29
|
+
if security is None:
|
|
30
|
+
security = (profile_or_plan.get("spec") or {}).get("security")
|
|
31
|
+
runtime = (security or {}).get("runtime") if isinstance(security, dict) else None
|
|
32
|
+
block = (runtime or {}).get("confinement") if isinstance(runtime, dict) else None
|
|
33
|
+
return block if isinstance(block, dict) else {}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def plan_declares_confinement(plan: dict[str, Any] | None) -> bool:
|
|
37
|
+
"""True when the profile or any module instance sets confinement."""
|
|
38
|
+
if profile_confinement(plan):
|
|
39
|
+
return True
|
|
40
|
+
return any(
|
|
41
|
+
isinstance((module.get("security") or {}).get("confinement"), dict)
|
|
42
|
+
and (module.get("security") or {}).get("confinement")
|
|
43
|
+
for module in (plan or {}).get("modules") or []
|
|
44
|
+
if isinstance(module, dict)
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def effective_confinement(
|
|
49
|
+
defaults: dict[str, Any] | None, override: dict[str, Any] | None
|
|
50
|
+
) -> dict[str, Any]:
|
|
51
|
+
"""Profile defaults with the module instance's keys layered on top."""
|
|
52
|
+
merged = {key: value for key, value in (defaults or {}).items() if key in CONFINEMENT_KEYS}
|
|
53
|
+
for key, value in (override or {}).items():
|
|
54
|
+
if key in CONFINEMENT_KEYS:
|
|
55
|
+
merged[key] = value
|
|
56
|
+
return merged
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def validate_confinement(
|
|
60
|
+
block: Any, path: str, base_dir: Path | None = None
|
|
61
|
+
) -> list[Diagnostic]:
|
|
62
|
+
"""Check one confinement block; `base_dir` anchors `file:` seccomp profiles."""
|
|
63
|
+
if block is None:
|
|
64
|
+
return []
|
|
65
|
+
if not isinstance(block, dict):
|
|
66
|
+
return [_error(f"{path} must be an object.", path)]
|
|
67
|
+
diagnostics: list[Diagnostic] = []
|
|
68
|
+
for key in block:
|
|
69
|
+
if key not in CONFINEMENT_KEYS:
|
|
70
|
+
diagnostics.append(_error(
|
|
71
|
+
f"Unknown confinement key '{key}'; supported keys: {', '.join(CONFINEMENT_KEYS)}.",
|
|
72
|
+
f"{path}.{key}",
|
|
73
|
+
))
|
|
74
|
+
|
|
75
|
+
seccomp = block.get("seccomp")
|
|
76
|
+
if seccomp is not None:
|
|
77
|
+
diagnostics.extend(_validate_seccomp(seccomp, f"{path}.seccomp", base_dir))
|
|
78
|
+
apparmor = block.get("apparmor")
|
|
79
|
+
if apparmor is not None and not (
|
|
80
|
+
isinstance(apparmor, str)
|
|
81
|
+
and (apparmor in {"runtime/default", "unconfined"} or _APPARMOR_NAME_RE.match(apparmor))
|
|
82
|
+
):
|
|
83
|
+
diagnostics.append(_error(
|
|
84
|
+
"apparmor must be 'runtime/default', 'unconfined' or the name of a profile "
|
|
85
|
+
"already loaded on the host (letters, digits, '.', '_', '-', '/').",
|
|
86
|
+
f"{path}.apparmor",
|
|
87
|
+
))
|
|
88
|
+
selinux = block.get("selinux")
|
|
89
|
+
if selinux is not None:
|
|
90
|
+
diagnostics.extend(_validate_selinux(selinux, f"{path}.selinux"))
|
|
91
|
+
return diagnostics
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _validate_seccomp(value: Any, path: str, base_dir: Path | None) -> list[Diagnostic]:
|
|
95
|
+
if value in {"default", "unconfined"}:
|
|
96
|
+
return []
|
|
97
|
+
if not isinstance(value, str) or not value.startswith("file:") or len(value) == 5:
|
|
98
|
+
return [_error(
|
|
99
|
+
"seccomp must be 'default', 'unconfined' or 'file:<path to a JSON profile "
|
|
100
|
+
"relative to the profile directory>'.",
|
|
101
|
+
path,
|
|
102
|
+
)]
|
|
103
|
+
relative = value[5:]
|
|
104
|
+
if base_dir is None:
|
|
105
|
+
return [_error(
|
|
106
|
+
"seccomp file profiles need a known profile directory to resolve against.", path
|
|
107
|
+
)]
|
|
108
|
+
# Reject rooted and drive-qualified paths explicitly: they are not
|
|
109
|
+
# "absolute" to pathlib on every platform (e.g. "/x" on Windows).
|
|
110
|
+
if (
|
|
111
|
+
Path(relative).is_absolute()
|
|
112
|
+
or relative.startswith(("/", "\\"))
|
|
113
|
+
or _DRIVE_RE.match(relative)
|
|
114
|
+
or ".." in Path(relative).parts
|
|
115
|
+
):
|
|
116
|
+
return [_error(
|
|
117
|
+
"seccomp file profiles must be relative paths inside the profile directory.", path
|
|
118
|
+
)]
|
|
119
|
+
target = (base_dir / relative).resolve()
|
|
120
|
+
if not target.is_relative_to(base_dir.resolve()):
|
|
121
|
+
return [_error(
|
|
122
|
+
"seccomp file profile resolves outside the profile directory.", path
|
|
123
|
+
)]
|
|
124
|
+
if not target.is_file():
|
|
125
|
+
return [_error(f"seccomp profile '{relative}' does not exist.", path)]
|
|
126
|
+
return []
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _validate_selinux(value: Any, path: str) -> list[Diagnostic]:
|
|
130
|
+
if value == "disable":
|
|
131
|
+
return []
|
|
132
|
+
if not isinstance(value, list) or not value or not all(isinstance(v, str) for v in value):
|
|
133
|
+
return [_error(
|
|
134
|
+
"selinux must be 'disable' or a non-empty list of labels such as "
|
|
135
|
+
"'type:container_t' or 'level:s0:c100,c200'.",
|
|
136
|
+
path,
|
|
137
|
+
)]
|
|
138
|
+
bad = [label for label in value if not _SELINUX_LABEL_RE.match(label)]
|
|
139
|
+
if bad:
|
|
140
|
+
return [_error(
|
|
141
|
+
f"Invalid SELinux label(s): {', '.join(bad)}; expected user:, role:, type: or level: values.",
|
|
142
|
+
path,
|
|
143
|
+
)]
|
|
144
|
+
kinds = [label.split(":", 1)[0] for label in value]
|
|
145
|
+
duplicates = sorted({k for k in kinds if kinds.count(k) > 1})
|
|
146
|
+
if duplicates:
|
|
147
|
+
return [_error(
|
|
148
|
+
f"Duplicate SELinux label kind(s): {', '.join(duplicates)}; "
|
|
149
|
+
"each of user:, role:, type: and level: may appear at most once.",
|
|
150
|
+
path,
|
|
151
|
+
)]
|
|
152
|
+
return []
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def confinement_security_opts(
|
|
156
|
+
confinement: dict[str, Any], compose_dir: Path, base_dir: Path | None
|
|
157
|
+
) -> list[str]:
|
|
158
|
+
"""Translate a validated confinement block into Compose `security_opt` entries.
|
|
159
|
+
|
|
160
|
+
Raises ValueError when a seccomp file cannot be expressed relative to
|
|
161
|
+
`compose_dir` (for example on a different Windows drive).
|
|
162
|
+
"""
|
|
163
|
+
opts: list[str] = []
|
|
164
|
+
seccomp = confinement.get("seccomp")
|
|
165
|
+
if seccomp == "unconfined":
|
|
166
|
+
opts.append("seccomp=unconfined")
|
|
167
|
+
elif isinstance(seccomp, str) and seccomp.startswith("file:"):
|
|
168
|
+
target = (base_dir or compose_dir) / seccomp[5:]
|
|
169
|
+
opts.append(f"seccomp:{Path(os.path.relpath(target, compose_dir)).as_posix()}")
|
|
170
|
+
apparmor = confinement.get("apparmor")
|
|
171
|
+
if apparmor == "runtime/default":
|
|
172
|
+
opts.append("apparmor=docker-default")
|
|
173
|
+
elif isinstance(apparmor, str):
|
|
174
|
+
opts.append(f"apparmor={apparmor}")
|
|
175
|
+
selinux = confinement.get("selinux")
|
|
176
|
+
if selinux == "disable":
|
|
177
|
+
opts.append("label=disable")
|
|
178
|
+
elif isinstance(selinux, list):
|
|
179
|
+
opts.extend(f"label={label}" for label in selinux)
|
|
180
|
+
return opts
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def apply_confinement(
|
|
184
|
+
service: dict[str, Any],
|
|
185
|
+
confinement: dict[str, Any],
|
|
186
|
+
compose_dir: Path,
|
|
187
|
+
base_dir: Path | None,
|
|
188
|
+
path: str = "security.confinement",
|
|
189
|
+
) -> list[Diagnostic]:
|
|
190
|
+
"""Set the declared confinement on a service, replacing module-set values per key."""
|
|
191
|
+
if not confinement:
|
|
192
|
+
return []
|
|
193
|
+
try:
|
|
194
|
+
new_opts = confinement_security_opts(confinement, compose_dir, base_dir)
|
|
195
|
+
except ValueError:
|
|
196
|
+
return [_error(
|
|
197
|
+
"seccomp file profile cannot be referenced relative to the generated "
|
|
198
|
+
"docker-compose.yml (different drive); render into the profile's drive.",
|
|
199
|
+
f"{path}.seccomp",
|
|
200
|
+
)]
|
|
201
|
+
existing = [str(opt) for opt in service.get("security_opt") or []]
|
|
202
|
+
replaced = {
|
|
203
|
+
"seccomp": ("seccomp=", "seccomp:"),
|
|
204
|
+
"apparmor": ("apparmor=", "apparmor:"),
|
|
205
|
+
"selinux": ("label=", "label:"),
|
|
206
|
+
}
|
|
207
|
+
prefixes = tuple(p for key in confinement for p in replaced.get(key, ()))
|
|
208
|
+
kept = [opt for opt in existing if not opt.startswith(prefixes)] if prefixes else existing
|
|
209
|
+
# `default` seccomp means "engine default", so it only clears module-set values.
|
|
210
|
+
merged = kept + new_opts
|
|
211
|
+
if merged:
|
|
212
|
+
service["security_opt"] = merged
|
|
213
|
+
else:
|
|
214
|
+
service.pop("security_opt", None)
|
|
215
|
+
return []
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def _error(message: str, path: str) -> Diagnostic:
|
|
219
|
+
return Diagnostic(level="error", code=INVALID_CONFINEMENT_CODE, message=message, path=path)
|