pubdeploy 0.8.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.
- pubdeploy/__init__.py +532 -0
- pubdeploy/channels.py +188 -0
- pubdeploy/cli.py +120 -0
- pubdeploy/config.py +140 -0
- pubdeploy/contract.py +87 -0
- pubdeploy/github.py +61 -0
- pubdeploy/hooks.py +82 -0
- pubdeploy/implied.py +179 -0
- pubdeploy/journal.json +207 -0
- pubdeploy/k8s.py +472 -0
- pubdeploy/machine.py +363 -0
- pubdeploy/model.py +229 -0
- pubdeploy/names.py +25 -0
- pubdeploy/order.py +101 -0
- pubdeploy/providers.py +75 -0
- pubdeploy/registry.py +96 -0
- pubdeploy/release-commit +1 -0
- pubdeploy/schema.json +127 -0
- pubdeploy/schema.py +238 -0
- pubdeploy/store.py +127 -0
- pubdeploy/testing/__init__.py +94 -0
- pubdeploy/testing/fixtures/deploy-app/.github/workflows/deploy.yml +11 -0
- pubdeploy/testing/fixtures/deploy-app/k8s/admin/network-policy.yaml +11 -0
- pubdeploy/testing/fixtures/deploy-app/k8s/app.yaml +63 -0
- pubdeploy/testing/fixtures/deploy-app/k8s/kustomization.yaml +6 -0
- pubdeploy/testing/fixtures/deploy-app/pubrules.json +27 -0
- pubdeploy/testing/fixtures/deploy-app/rendered.yaml +86 -0
- pubdeploy/testing/fixtures/deploy-app/zones.json +8 -0
- pubdeploy/testing/plugin.py +26 -0
- pubdeploy/testing/steps.py +1124 -0
- pubdeploy/testing/world.py +569 -0
- pubdeploy/types/__init__.py +181 -0
- pubdeploy/types/auth.py +199 -0
- pubdeploy/types/github.py +110 -0
- pubdeploy/types/k8s.py +609 -0
- pubdeploy/types/net.py +237 -0
- pubdeploy/types/rollout.py +186 -0
- pubdeploy/units.py +177 -0
- pubdeploy/walk.py +325 -0
- pubdeploy-0.8.0.dist-info/METADATA +520 -0
- pubdeploy-0.8.0.dist-info/RECORD +45 -0
- pubdeploy-0.8.0.dist-info/WHEEL +5 -0
- pubdeploy-0.8.0.dist-info/entry_points.txt +11 -0
- pubdeploy-0.8.0.dist-info/licenses/LICENSE +202 -0
- pubdeploy-0.8.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
"""The world deploy-walk scenarios run in: the sample service
|
|
2
|
+
(fixtures/deploy-app, beside this module), copied, and fakes for everything a
|
|
3
|
+
check touches. kubectl answers from namespaces of Secrets, service accounts,
|
|
4
|
+
Deployments and CronJobs; the GitHub API from secrets, variables and
|
|
5
|
+
workflow runs; HTTP from a status per address; kustomize is the fixture's
|
|
6
|
+
rendered.yaml. Nothing runs kubectl, gh, kustomize or the network.
|
|
7
|
+
|
|
8
|
+
Every fake Secret value, every fake Bitwarden value and every variable's
|
|
9
|
+
value holds CANARY, and after each scenario no output, no journal and no
|
|
10
|
+
exception text may hold it (`world`'s teardown).
|
|
11
|
+
|
|
12
|
+
A suite extends the world by subclassing `World` and naming its subclass with
|
|
13
|
+
the `deploy_world_class` fixture (pubdeploy.testing's docstring): the
|
|
14
|
+
variable ./with-secrets sets (`secrets_project`), the providers the walk
|
|
15
|
+
loads (`providers`), more patches
|
|
16
|
+
(`patch`), more state (`__init__`)."""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import copy
|
|
21
|
+
import json
|
|
22
|
+
from collections.abc import Iterable
|
|
23
|
+
from dataclasses import dataclass
|
|
24
|
+
from datetime import UTC, datetime, timedelta
|
|
25
|
+
from importlib.metadata import EntryPoint
|
|
26
|
+
from pathlib import Path
|
|
27
|
+
from typing import Any
|
|
28
|
+
|
|
29
|
+
import yaml
|
|
30
|
+
|
|
31
|
+
from pubdeploy.github import ApiError
|
|
32
|
+
from pubdeploy.model import Context, Need, Ran, Stop, no_server
|
|
33
|
+
from pubdeploy.testing import fixture_path
|
|
34
|
+
|
|
35
|
+
CANARY = "canary-7f3e"
|
|
36
|
+
FIXTURE = fixture_path("deploy-app")
|
|
37
|
+
SLUG = "example-org/example"
|
|
38
|
+
HEAD = "abc1234" + "0" * 33
|
|
39
|
+
NOW = datetime(2026, 11, 7, 8, 0, tzinfo=UTC)
|
|
40
|
+
# A cookie secret oauth2-proxy takes as it is: 32 bytes.
|
|
41
|
+
COOKIE = (CANARY + "k" * 21).encode()
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def good_cluster() -> dict[str, Any]:
|
|
45
|
+
return {"forbidden": False, "namespaces": {"example": {
|
|
46
|
+
"secrets": {"example-page-secrets": {"cookie-secret": COOKIE},
|
|
47
|
+
"example-files": {"token": f"{CANARY}-token".encode()},
|
|
48
|
+
"example-app-env": {"LOG_LEVEL": f"{CANARY}-info".encode()}},
|
|
49
|
+
"serviceaccount": {"deploy"}, "role": {"deploy"}, "rolebinding": {"deploy"},
|
|
50
|
+
"deployment": [{"metadata": {"name": n}, "spec": {"replicas": 1}, "status": {"readyReplicas": 1}}
|
|
51
|
+
for n in ("app", "proxy")],
|
|
52
|
+
"cronjob": {"worker": {"spec": {"schedule": "0 * * * *"}, "status": {"lastSuccessfulTime": (NOW - timedelta(hours=1)).strftime(
|
|
53
|
+
"%Y-%m-%dT%H:%M:%SZ")}}},
|
|
54
|
+
"objects": {"networkpolicy/example-app"}}}}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def good_github() -> dict[str, Any]:
|
|
58
|
+
return {"forbidden": False, "default_branch": "main", "head": HEAD,
|
|
59
|
+
"secrets": {(None, "EXAMPLE_TOKEN"), (None, "TS_GH_OAUTH_CLIENT_ID"), (None, "TS_GH_OAUTH_SECRET"),
|
|
60
|
+
("server", "SUSTENAUT_DEPLOY_SSH_KEY")},
|
|
61
|
+
"variables": {(None, "EXAMPLE_USER"): f"{CANARY}-user", (None, "EXAMPLE_SETTING"): f"{CANARY}-setting",
|
|
62
|
+
(None, "DEPLOY_EXAMPLE"): "on", (None, "SUSTENAUT_SERVER"): f"{CANARY}.example.eu",
|
|
63
|
+
("server", "SUSTENAUT_KNOWN_HOSTS"): f"{CANARY} ssh-ed25519 AAAA"},
|
|
64
|
+
"runs": {"push": [], "workflow_run": [{"id": 7, "head_sha": HEAD, "conclusion": "success",
|
|
65
|
+
"status": "completed", "created_at": "2026-11-07T07:00:00Z"}]}}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def good_env(secrets_project: str = "PUBKIT_SECRETS_PROJECT") -> dict[str, str]:
|
|
69
|
+
"""What ./with-secrets gives the walk; `secrets_project` is the variable naming its project."""
|
|
70
|
+
return {secrets_project: "example", "EXAMPLE_COOKIE_SECRET": COOKIE.decode(),
|
|
71
|
+
"EXAMPLE_FILES_TOKEN": f"{CANARY}-token", "EXAMPLE_USER": f"{CANARY}-user",
|
|
72
|
+
"EXAMPLE_TOKEN": f"{CANARY}-token", "SUSTENAUT_SERVER": "server.example.eu"}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class FakeKubectl:
|
|
76
|
+
"""kubectl's answers, from the cluster's state, the writes a fulfil
|
|
77
|
+
makes into it, and the calls it got, with what came on standard input.
|
|
78
|
+
With `channel` deploy it is the server's read-only verb: it refuses
|
|
79
|
+
writes, and a Secret's data, and answers secret-keys unless the server
|
|
80
|
+
has no such verb yet."""
|
|
81
|
+
|
|
82
|
+
def __init__(self, state: dict[str, Any], calls: list, slow: dict[str, float], channel: str = "plain") -> None:
|
|
83
|
+
self.state, self.calls, self.slow, self.channel = state, calls, slow, channel
|
|
84
|
+
|
|
85
|
+
def __call__(self, args: list[str], input: bytes | None = None) -> Ran: # noqa: A002
|
|
86
|
+
import time # noqa: PLC0415
|
|
87
|
+
self.calls.append(args)
|
|
88
|
+
if input is not None and "diff" not in args: # a dry run's manifests write nothing
|
|
89
|
+
self.state.setdefault("stdin", []).append(input)
|
|
90
|
+
raw = list(args)
|
|
91
|
+
args = [a for a in args if a not in ("-o", "json", "name")]
|
|
92
|
+
if args[:2] == ["get", "namespace"] and self.slow.get("namespace"):
|
|
93
|
+
time.sleep(self.slow["namespace"])
|
|
94
|
+
if self.state.get("missing"):
|
|
95
|
+
return Ran(127, "", "kubectl did not run (FileNotFoundError)")
|
|
96
|
+
if self.state["forbidden"]:
|
|
97
|
+
return Ran(1, "", 'Error from server (Forbidden): User "someone" cannot get resource')
|
|
98
|
+
if args[:2] == ["get", "--raw"]:
|
|
99
|
+
# The cluster's service-account signing keys, by kid.
|
|
100
|
+
if self.state.get("jwks") == "garbled":
|
|
101
|
+
return Ran(0, json.dumps({"keys": "none"}))
|
|
102
|
+
return Ran(0, json.dumps({"keys": [{"kid": k, "kty": "RSA", "n": n, "e": "AQAB"}
|
|
103
|
+
for k, n in self.state.get("jwks", {"key-a": "n-a"}).items()]}))
|
|
104
|
+
if args[:2] == ["get", "namespace"]:
|
|
105
|
+
ns = args[2]
|
|
106
|
+
if ns in self.state["namespaces"]:
|
|
107
|
+
return Ran(0, json.dumps({"kind": "Namespace", "metadata": {"name": ns}}))
|
|
108
|
+
return Ran(1, "", f'Error from server (NotFound): namespaces "{ns}" not found')
|
|
109
|
+
ns, verb, rest = args[1], args[2], args[3:]
|
|
110
|
+
space = self.state["namespaces"].get(ns)
|
|
111
|
+
if self.channel == "deploy":
|
|
112
|
+
return self.deploy(ns, verb, rest, raw, space)
|
|
113
|
+
if verb != "get":
|
|
114
|
+
return self.write(ns, verb, rest, input, space)
|
|
115
|
+
kind = rest[0]
|
|
116
|
+
name = rest[1] if len(rest) > 1 else None
|
|
117
|
+
if kind == "secrets" and "--no-headers" in rest:
|
|
118
|
+
# The namespace's Secrets as custom columns, names and types: asked for any other
|
|
119
|
+
# way, the walk would hold their values, which it never may.
|
|
120
|
+
assert "--no-headers" in raw and "custom-columns=NAME:.metadata.name,TYPE:.type" in raw, raw
|
|
121
|
+
return Ran(0, "".join(f"{n} Opaque\n" for n in sorted((space or {}).get("secrets", {}))))
|
|
122
|
+
if kind in ("deployment", "job") and name is None:
|
|
123
|
+
return Ran(0, json.dumps({"items": (space or {}).get(kind, [])}))
|
|
124
|
+
if "/" in kind:
|
|
125
|
+
found = space is not None and kind in space["objects"]
|
|
126
|
+
return Ran(0, kind) if found else Ran(1, "", f'Error from server (NotFound): "{kind}" not found')
|
|
127
|
+
held = (space or {}).get("secrets" if kind == "secret" else kind, {})
|
|
128
|
+
if name not in held:
|
|
129
|
+
return Ran(1, "", f'Error from server (NotFound): {kind}s "{name}" not found')
|
|
130
|
+
if kind == "secret":
|
|
131
|
+
import base64 # noqa: PLC0415
|
|
132
|
+
data = {k: base64.b64encode(v).decode() for k, v in held[name].items()}
|
|
133
|
+
return Ran(0, json.dumps({"kind": "Secret", "metadata": {"name": name}, "data": data}))
|
|
134
|
+
if kind == "cronjob":
|
|
135
|
+
return Ran(0, json.dumps(held[name]))
|
|
136
|
+
return Ran(0, json.dumps({"metadata": {"name": name}}))
|
|
137
|
+
|
|
138
|
+
def deploy(self, ns: str, verb: str, rest: list[str], raw: list[str], space: Any) -> Ran:
|
|
139
|
+
"""The read-only verb: get (a Secret only by name), and secret-keys."""
|
|
140
|
+
held = (space or {}).get("secrets", {})
|
|
141
|
+
if verb == "secret-keys":
|
|
142
|
+
if not self.state.get("secret_keys_verb", True):
|
|
143
|
+
return Ran(2, "", "kubectl-read: secret-keys is not a verb this server takes")
|
|
144
|
+
if rest[0] not in held:
|
|
145
|
+
return Ran(1, "", f'Error from server (NotFound): secrets "{rest[0]}" not found')
|
|
146
|
+
return Ran(0, json.dumps(sorted(held[rest[0]])))
|
|
147
|
+
if verb != "get":
|
|
148
|
+
return Ran(2, "", f"kubectl-read: {verb} is not a verb this server takes")
|
|
149
|
+
if rest[0] == "secret":
|
|
150
|
+
if "-o" in raw and raw[raw.index("-o") + 1] != "name":
|
|
151
|
+
return Ran(2, "", "kubectl-read: a Secret is shown by name only")
|
|
152
|
+
if rest[1] not in held:
|
|
153
|
+
return Ran(1, "", f'Error from server (NotFound): secrets "{rest[1]}" not found')
|
|
154
|
+
# What a table of it would show, values and all, which nothing may keep.
|
|
155
|
+
return Ran(0, f"secret/{rest[1]}" if "name" in raw else f"{rest[1]} {CANARY}")
|
|
156
|
+
return self.__class__(self.state, [], self.slow)(["-n", ns, verb, *rest, "-o", "json"])
|
|
157
|
+
|
|
158
|
+
def write(self, ns: str, verb: str, rest: list[str], input: bytes | None, space: Any) -> Ran: # noqa: A002
|
|
159
|
+
import base64 # noqa: PLC0415
|
|
160
|
+
if self.state.get("refuse_writes"):
|
|
161
|
+
return Ran(1, "", f'Error from server (Forbidden): cannot {verb} in "{ns}"')
|
|
162
|
+
if space is None:
|
|
163
|
+
return Ran(1, "", f'Error from server (NotFound): namespaces "{ns}" not found')
|
|
164
|
+
if verb == "diff":
|
|
165
|
+
if self.state.get("client_side_owned") and "--force-conflicts" not in rest:
|
|
166
|
+
return Ran(2, "", 'error: Apply failed with 1 conflict: conflict with "kubectl-client-side-apply"')
|
|
167
|
+
return Ran(self.state.get("diff_code", 0), self.state.get("diff_out", ""), self.state.get("diff_err", ""))
|
|
168
|
+
if verb == "apply":
|
|
169
|
+
assert rest in (["-f", "-"], ["--server-side", "--force-conflicts", "-f", "-"]) and input is not None, rest
|
|
170
|
+
out = []
|
|
171
|
+
for doc in yaml.safe_load_all(input.decode()):
|
|
172
|
+
if doc.get("kind") == "Secret":
|
|
173
|
+
space["secrets"][doc["metadata"]["name"]] = {k: base64.b64decode(v)
|
|
174
|
+
for k, v in doc.get("data", {}).items()}
|
|
175
|
+
else:
|
|
176
|
+
space["objects"].add(f"{doc['kind'].lower()}/{doc['metadata']['name']}")
|
|
177
|
+
out.append(f"{doc['kind'].lower()}/{doc['metadata']['name']} configured")
|
|
178
|
+
return Ran(0, "\n".join(out) + "\n")
|
|
179
|
+
if verb == "rollout" and rest[0] == "restart":
|
|
180
|
+
self.state.setdefault("restarted", []).append(rest[1])
|
|
181
|
+
return Ran(0, f"{rest[1]} restarted")
|
|
182
|
+
if verb == "create" and rest[0] == "job":
|
|
183
|
+
cronjob = rest[1].removeprefix("--from=cronjob/")
|
|
184
|
+
if cronjob not in space["cronjob"]:
|
|
185
|
+
return Ran(1, "", f'Error from server (NotFound): cronjobs "{cronjob}" not found')
|
|
186
|
+
# As kubectl makes it: not the CronJob's, so its success never moves the CronJob's status.
|
|
187
|
+
self.state.setdefault("jobs", []).append(rest[2])
|
|
188
|
+
space.setdefault("job", []).append({"metadata": {"name": rest[2]}, "status": {}})
|
|
189
|
+
return Ran(0, f"job.batch/{rest[2]} created")
|
|
190
|
+
if verb == "wait":
|
|
191
|
+
if self.state.get("job_fails"):
|
|
192
|
+
return Ran(1, "", "timed out waiting for the condition")
|
|
193
|
+
job = next(j for j in space["job"] if j["metadata"]["name"] == rest[1].removeprefix("job/"))
|
|
194
|
+
job["status"] = {"succeeded": 1, "completionTime": NOW.strftime("%Y-%m-%dT%H:%M:%SZ")}
|
|
195
|
+
return Ran(0, "condition met")
|
|
196
|
+
raise AssertionError(f"kubectl {verb} {rest}")
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
class FakeServer:
|
|
200
|
+
"""Commands on the server, over the admin channel: sustenaut-app, and
|
|
201
|
+
register-app.py's, each with its standard input kept, for the canary."""
|
|
202
|
+
|
|
203
|
+
def __init__(self, world: Any) -> None:
|
|
204
|
+
self.world = world
|
|
205
|
+
|
|
206
|
+
def __call__(self, args: list[str], input: bytes | None = None) -> Ran: # noqa: A002
|
|
207
|
+
w = self.world
|
|
208
|
+
w.calls.setdefault("server", []).append((list(args), input))
|
|
209
|
+
if args[:1] == ["sustenaut-app"]:
|
|
210
|
+
w.cluster["namespaces"].setdefault(args[1], {"secrets": {}, "serviceaccount": set(), "role": set(),
|
|
211
|
+
"rolebinding": set(), "deployment": [], "cronjob": {},
|
|
212
|
+
"objects": set()})
|
|
213
|
+
for kind in ("serviceaccount", "role", "rolebinding"):
|
|
214
|
+
w.cluster["namespaces"][args[1]][kind].add("deploy")
|
|
215
|
+
w.cluster.setdefault("kubeconfigs", set()).add(args[1])
|
|
216
|
+
return Ran(0, f"namespace {args[1]} ready")
|
|
217
|
+
if args[:2] == ["test", "-f"] and args[2].startswith("/home/deploy/.kube/"):
|
|
218
|
+
ns = args[2].rsplit("/", 1)[1]
|
|
219
|
+
known = w.cluster.get("kubeconfigs")
|
|
220
|
+
return Ran(0 if known is None or ns in known else 1, "")
|
|
221
|
+
if args[:3] == ["python3", "-I", "-"] and "--status" in args:
|
|
222
|
+
return Ran(0, json.dumps(w.auth))
|
|
223
|
+
if args[:3] == ["python3", "-I", "-"] and "--site-status" in args:
|
|
224
|
+
if w.site is None:
|
|
225
|
+
return Ran(2, "no registration: /root/.config/example-auth/team/client.json is not there")
|
|
226
|
+
given = {args[i]: args[i + 1] for i in range(len(args) - 1) if args[i] in ("--redirect", "--logout", "--role")}
|
|
227
|
+
lines = [f"{kind} {given['--' + kind]}: {'present' if kind in w.site else 'missing'}"
|
|
228
|
+
for kind in ("redirect", "logout", "role")]
|
|
229
|
+
return Ran(0 if all(k in w.site for k in ("redirect", "logout", "role")) else 1, "\n".join(lines))
|
|
230
|
+
if args[:3] == ["python3", "-I", "-"] and "--redirect" in args:
|
|
231
|
+
w.site = {"redirect", "logout", "role"}
|
|
232
|
+
return Ran(0, json.dumps({"status": "ok", "client_id": "example.eu"}))
|
|
233
|
+
if args[:3] == ["python3", "-I", "-"] and args[3].endswith("/client.json"):
|
|
234
|
+
assert input is not None and b"k3s" in input and len(args) == 6, args
|
|
235
|
+
name, ns = args[4], args[5]
|
|
236
|
+
w.cluster["namespaces"][ns]["secrets"][name] = {"client-id": f"{CANARY}-id".encode(),
|
|
237
|
+
"client-secret": f"{CANARY}-secret".encode()}
|
|
238
|
+
return Ran(0, f"secret/{name} configured")
|
|
239
|
+
raise AssertionError(f"the server was asked {args}")
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
class FakeWriter:
|
|
243
|
+
"""Bitwarden, with the write token: what it was given to write, by name.
|
|
244
|
+
For a suite whose types write secrets (pubsec's) to put in place of its
|
|
245
|
+
writer, in its own World's `patch`."""
|
|
246
|
+
|
|
247
|
+
def __init__(self, world: Any, token: str) -> None:
|
|
248
|
+
self.world, self.token = world, token
|
|
249
|
+
|
|
250
|
+
def write(self, name: str, value: str, note: str) -> None:
|
|
251
|
+
assert CANARY in self.token
|
|
252
|
+
if self.world.bitwarden_refuses:
|
|
253
|
+
raise RuntimeError(f"Bitwarden said no to {name}")
|
|
254
|
+
self.world.written[name] = value
|
|
255
|
+
self.world.notes[name] = note
|
|
256
|
+
self.world.values.append(value)
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
class FakeApi:
|
|
260
|
+
"""The GitHub API, as protect.gh calls it: reads from the state, and
|
|
261
|
+
the writes a fulfil makes (variables, workflow dispatches) into it."""
|
|
262
|
+
|
|
263
|
+
def __init__(self, state: dict[str, Any], calls: list) -> None:
|
|
264
|
+
self.state, self.calls = state, calls
|
|
265
|
+
|
|
266
|
+
def __call__(self, method: str, path: str, body: Any = None) -> Any:
|
|
267
|
+
self.calls.append((method, path))
|
|
268
|
+
if self.state["forbidden"] or (method != "GET" and self.state.get("read_only")):
|
|
269
|
+
raise ApiError(f"gh api -X {method} {path}: HTTP 403: Resource not accessible (HTTP 403)")
|
|
270
|
+
if path.startswith("repos/") and not path.startswith(f"repos/{SLUG}"):
|
|
271
|
+
return self.other(method, path)
|
|
272
|
+
route = path.split("?")[0].removeprefix(f"repos/{SLUG}").strip("/").split("/")
|
|
273
|
+
query = dict(p.split("=") for p in path.partition("?")[2].split("&") if p)
|
|
274
|
+
environment = None
|
|
275
|
+
if route[:1] == ["environments"]:
|
|
276
|
+
environment, route = route[1], ["actions", *route[2:]]
|
|
277
|
+
if method == "PATCH" and route[:2] == ["actions", "variables"]:
|
|
278
|
+
if (environment, route[2]) not in self.state["variables"]:
|
|
279
|
+
raise ApiError(f"gh api -X PATCH {path}: HTTP 404: Not Found (HTTP 404)")
|
|
280
|
+
self.state["variables"][(environment, route[2])] = body["value"]
|
|
281
|
+
return None
|
|
282
|
+
if method == "POST" and route == ["actions", "variables"]:
|
|
283
|
+
self.state["variables"][(environment, body["name"])] = body["value"]
|
|
284
|
+
return None
|
|
285
|
+
if method == "POST" and route[:2] == ["actions", "workflows"] and route[-1] == "dispatches":
|
|
286
|
+
self.state.setdefault("dispatches", []).append((route[2], body["ref"]))
|
|
287
|
+
self.state["runs"].setdefault("workflow_dispatch", []).insert(0, {
|
|
288
|
+
"id": 8, "head_sha": self.state["head"], "conclusion": self.state.get("dispatch_ends", "success"),
|
|
289
|
+
"status": "completed", "created_at": "2026-11-07T08:00:01Z"})
|
|
290
|
+
return None
|
|
291
|
+
assert method == "GET", path
|
|
292
|
+
if route == [""]:
|
|
293
|
+
return {"default_branch": self.state["default_branch"]}
|
|
294
|
+
if route[:1] == ["branches"]:
|
|
295
|
+
return {"commit": {"sha": self.state["head"]}}
|
|
296
|
+
if route[:2] == ["actions", "workflows"]:
|
|
297
|
+
return {"workflow_runs": self.state["runs"].get(query.get("event"), [])}
|
|
298
|
+
if route == ["actions", "secrets"] or route == ["actions", "variables"]:
|
|
299
|
+
kind = route[1]
|
|
300
|
+
held = self.state[kind]
|
|
301
|
+
return {kind: [{"name": n} for e, n in held if e == environment]}
|
|
302
|
+
if route[:2] == ["actions", "secrets"] and (environment, route[2]) in self.state["secrets"]:
|
|
303
|
+
return {"name": route[2]}
|
|
304
|
+
if route[:2] == ["actions", "variables"] and (environment, route[2]) in self.state["variables"]:
|
|
305
|
+
return {"name": route[2], "value": self.state["variables"][(environment, route[2])]}
|
|
306
|
+
raise ApiError(f"gh api -X GET {path}: HTTP 404: Not Found (HTTP 404)")
|
|
307
|
+
|
|
308
|
+
def other(self, method: str, path: str) -> Any:
|
|
309
|
+
"""Another repository's contents and variables, from state["others"]."""
|
|
310
|
+
found = self.state.get("others", {}).get(path.split("?")[0])
|
|
311
|
+
if method != "GET" or found is None:
|
|
312
|
+
raise ApiError(f"gh api -X {method} {path}: HTTP 404: Not Found (HTTP 404)")
|
|
313
|
+
return found
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
class FakeSend:
|
|
317
|
+
"""app_secrets.send: what it set, by name, the value checked for the canary and dropped."""
|
|
318
|
+
|
|
319
|
+
def __init__(self, github: dict[str, Any], sent: list) -> None:
|
|
320
|
+
self.github, self.sent = github, sent
|
|
321
|
+
|
|
322
|
+
def __call__(self, kind: str, name: str, repo: str, value: str, environment: str | None = None) -> str | None:
|
|
323
|
+
assert repo == SLUG and value
|
|
324
|
+
self.sent.append((kind, name, environment, len(value)))
|
|
325
|
+
if self.github.get("refuse_send"):
|
|
326
|
+
return f"HTTP 403: may not set {name} ({value})"
|
|
327
|
+
if kind == "secret":
|
|
328
|
+
self.github["secrets"].add((environment, name))
|
|
329
|
+
else:
|
|
330
|
+
self.github["variables"][(environment, name)] = value
|
|
331
|
+
return None
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
class FakeHttp:
|
|
335
|
+
"""A status per address, or "timeout" or "refused"."""
|
|
336
|
+
|
|
337
|
+
def __init__(self, answers: dict[str, Any], calls: list) -> None:
|
|
338
|
+
self.answers, self.calls = answers, calls
|
|
339
|
+
|
|
340
|
+
def __call__(self, url: str, timeout: float) -> tuple[int, bytes]:
|
|
341
|
+
self.calls.append(url)
|
|
342
|
+
answer = self.answers.get(url, "refused")
|
|
343
|
+
if answer == "timeout":
|
|
344
|
+
raise TimeoutError
|
|
345
|
+
if answer == "refused":
|
|
346
|
+
raise ConnectionRefusedError(f"{url} refused, {CANARY}")
|
|
347
|
+
return answer, f"body {CANARY}".encode()
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
class FakeHeads:
|
|
351
|
+
"""Storage, as store.Heads speaks to it: files, and heads by path with ETags."""
|
|
352
|
+
|
|
353
|
+
def __init__(self, world, base, tenant, token):
|
|
354
|
+
self.world, self.base, self.tenant, self.token = world, base, tenant, token
|
|
355
|
+
world.storage["signed_in"].append((base, tenant, token))
|
|
356
|
+
|
|
357
|
+
def upload(self, data):
|
|
358
|
+
from pubdeploy.store import Refused # noqa: PLC0415
|
|
359
|
+
if self.world.storage.get("refuse"):
|
|
360
|
+
raise Refused(403, b"no")
|
|
361
|
+
self.world.storage["files"].append((self.tenant, json.loads(data)))
|
|
362
|
+
return f"file-{len(self.world.storage['files'])}"
|
|
363
|
+
|
|
364
|
+
def head(self, path):
|
|
365
|
+
return self.world.storage["heads"].get(path)
|
|
366
|
+
|
|
367
|
+
def point(self, path, hash_, current):
|
|
368
|
+
self.world.storage["moves"].append((path, hash_, current))
|
|
369
|
+
if self.world.storage.get("race"):
|
|
370
|
+
self.world.storage["race"] = False
|
|
371
|
+
self.world.storage["heads"][path] = "newer"
|
|
372
|
+
return 412
|
|
373
|
+
self.world.storage["heads"][path] = hash_
|
|
374
|
+
return 200
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
@dataclass(frozen=True)
|
|
378
|
+
class Providers:
|
|
379
|
+
"""What a world's walk loads in place of the installed packages: the
|
|
380
|
+
`sustenaut.needs` entry points and the `sustenaut.walk` hooks, together,
|
|
381
|
+
so a world never mixes its types with another package's hook."""
|
|
382
|
+
needs: tuple[EntryPoint, ...] = ()
|
|
383
|
+
hooks: tuple[EntryPoint, ...] = ()
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
class World:
|
|
387
|
+
"""One scenario's service, fakes and what came out of it.
|
|
388
|
+
|
|
389
|
+
A subclass may set `secrets_project`, the variable ./with-secrets names
|
|
390
|
+
its project in (pubsec 0.1's PUBKIT_SECRETS_PROJECT, later
|
|
391
|
+
PUBRULES_SECRETS_PROJECT); extend `__init__` for state of its own;
|
|
392
|
+
give `providers` for a stand-in provider; and extend `patch` for more
|
|
393
|
+
of what the walk reaches, and `leaked` for more of what the canary reads.
|
|
394
|
+
gcloud is no tool pubdeploy runs: a provider that runs it fakes it."""
|
|
395
|
+
|
|
396
|
+
secrets_project = "PUBKIT_SECRETS_PROJECT"
|
|
397
|
+
|
|
398
|
+
@classmethod
|
|
399
|
+
def sample(cls) -> Path:
|
|
400
|
+
"""The service the world copies: the sample "deploy-app". A subclass
|
|
401
|
+
that returns another service must ship the files the world reads from
|
|
402
|
+
it: pubrules.json (the deploy block), rendered.yaml (what kubectl is
|
|
403
|
+
given) and zones.json (the hosting zones the DNS check reads)."""
|
|
404
|
+
return FIXTURE
|
|
405
|
+
|
|
406
|
+
def __init__(self, root: Path) -> None:
|
|
407
|
+
self.root = root
|
|
408
|
+
self.config = json.loads((root / "pubrules.json").read_text())
|
|
409
|
+
self.rendered = (root / "rendered.yaml").read_text()
|
|
410
|
+
self.render_error: Stop | None = None
|
|
411
|
+
self.cluster = good_cluster()
|
|
412
|
+
self.cluster["namespaces"]["example"]["deployment"] = deployments(self.rendered)
|
|
413
|
+
self.github = good_github()
|
|
414
|
+
self.http: dict[str, Any] = {"https://storage.example.eu/healthz": 200,
|
|
415
|
+
"https://example.example.eu/healthz": 200}
|
|
416
|
+
self.env = good_env(self.secrets_project)
|
|
417
|
+
self.zones = json.loads((root / "zones.json").read_text())["hosting"]["zones"]
|
|
418
|
+
self.slow: dict[str, float] = {}
|
|
419
|
+
self.slug = SLUG
|
|
420
|
+
self.addresses = ["192.0.2.1"]
|
|
421
|
+
self.calls: dict[str, list] = {"kubectl": [], "gh": [], "http": []}
|
|
422
|
+
self.channel = "plain"
|
|
423
|
+
self.admin = True # a command runs on the server
|
|
424
|
+
self.auth: dict[str, Any] = {"status": "registered"}
|
|
425
|
+
# What register-app.py --site-status finds of this site on the client; None: unreadable.
|
|
426
|
+
self.site: set[str] | None = {"redirect", "logout", "role"}
|
|
427
|
+
self.written: dict[str, str] = {} # what the Bitwarden writer was given
|
|
428
|
+
self.notes: dict[str, str] = {} # by name, the note each write gave
|
|
429
|
+
self.bitwarden_refuses = False
|
|
430
|
+
self.token = f"{CANARY}-write-token"
|
|
431
|
+
self.writer: Any = None # the context's writer; the command's comes from the token
|
|
432
|
+
self.answers: list[str] = [] # what is typed at the walk's prompts
|
|
433
|
+
self.terminal = True
|
|
434
|
+
# Values that cannot hold the canary (hex, base64), which no output may hold either.
|
|
435
|
+
self.values: list[str] = []
|
|
436
|
+
self.plan: Any = None
|
|
437
|
+
self.problems: list[str] | None = None
|
|
438
|
+
self.outputs: list[str] = []
|
|
439
|
+
self.code: int | None = None
|
|
440
|
+
self.journal: dict[str, Any] | None = None
|
|
441
|
+
# Storage, as FakeHeads keeps it: who signed in, the files given, the heads and their moves.
|
|
442
|
+
self.storage: dict[str, Any] = {"signed_in": [], "files": [], "heads": {}, "moves": []}
|
|
443
|
+
self.env["PUBRULES_STORAGE_TOKEN"] = f"{CANARY}-storage-token"
|
|
444
|
+
self.values.append(f"{CANARY}-storage-token")
|
|
445
|
+
|
|
446
|
+
@property
|
|
447
|
+
def block(self) -> dict[str, Any]:
|
|
448
|
+
return self.config["deploy"]
|
|
449
|
+
|
|
450
|
+
def need(self, kind: str) -> dict[str, Any]:
|
|
451
|
+
return next(n for n in self.block["needs"] if n["type"] == kind)
|
|
452
|
+
|
|
453
|
+
def save(self) -> None:
|
|
454
|
+
(self.root / "pubrules.json").write_text(json.dumps(self.config, indent=2))
|
|
455
|
+
|
|
456
|
+
def render(self) -> str:
|
|
457
|
+
if self.render_error:
|
|
458
|
+
raise self.render_error
|
|
459
|
+
return self.rendered
|
|
460
|
+
|
|
461
|
+
def context(self, config: dict[str, Any] | None = None, offline: bool = False,
|
|
462
|
+
namespace: str | None = None, unit: str | None = None) -> Context:
|
|
463
|
+
from pubdeploy.units import unit_block # noqa: PLC0415
|
|
464
|
+
config = config or self.config
|
|
465
|
+
unit = unit or config["deploy"]["namespace"]
|
|
466
|
+
block = unit_block(config["deploy"], unit)
|
|
467
|
+
return Context(root=self.root, config=config, block=block, unit=unit,
|
|
468
|
+
namespace=namespace or block["namespace"],
|
|
469
|
+
kubectl=FakeKubectl(self.cluster, self.calls["kubectl"], self.slow, self.channel),
|
|
470
|
+
channel=self.channel, bitwarden_writer=self.writer,
|
|
471
|
+
server=FakeServer(self) if self.admin and self.channel != "deploy" else no_server,
|
|
472
|
+
gh=FakeApi(self.github, self.calls["gh"]), http_get=FakeHttp(self.http, self.calls["http"]),
|
|
473
|
+
send=FakeSend(self.github, self.calls.setdefault("sent", [])), sleep=lambda _s: None,
|
|
474
|
+
env=dict(self.env), now=lambda: NOW, rendered=self.render, offline=offline,
|
|
475
|
+
slug=self.slug, zones=copy.deepcopy(self.zones),
|
|
476
|
+
resolve=lambda host, rtype: list(self.addresses) if rtype == "A" else [])
|
|
477
|
+
|
|
478
|
+
def leaked(self) -> list[str]:
|
|
479
|
+
"""Every output, journal and exception text that holds a value, and
|
|
480
|
+
every argv a command was given: a value goes on standard input."""
|
|
481
|
+
seen = [*self.outputs, json.dumps(self.journal or {}),
|
|
482
|
+
*(json.dumps(c) for c in self.calls["kubectl"]),
|
|
483
|
+
*(json.dumps(argv) for argv, _ in self.calls.get("server", [])),
|
|
484
|
+
*(json.dumps(c) for c in self.calls.get("sent", []))]
|
|
485
|
+
return [text for text in seen for value in [CANARY, *self.values, self.token] if value in text]
|
|
486
|
+
|
|
487
|
+
def providers(self) -> Providers | None:
|
|
488
|
+
"""The need types and walk hooks the walk loads in this world, in place
|
|
489
|
+
of the installed packages': None, the default, keeps the installed ones.
|
|
490
|
+
A suite that walks with a stand-in provider returns its entry points."""
|
|
491
|
+
return None
|
|
492
|
+
|
|
493
|
+
def patch(self, monkeypatch) -> None:
|
|
494
|
+
"""Point what the walk reaches at the fakes, in the copied service's
|
|
495
|
+
directory: the context, the prompts, the terminal, storage, and the
|
|
496
|
+
providers. A suite whose installed packages the walk loads patches
|
|
497
|
+
those in its own subclass, after calling this one."""
|
|
498
|
+
monkeypatch.chdir(self.root)
|
|
499
|
+
if (given := self.providers()) is not None:
|
|
500
|
+
by_group = {"sustenaut.needs": list(given.needs), "sustenaut.walk": list(given.hooks)}
|
|
501
|
+
monkeypatch.setattr("pubdeploy.registry.installed", lambda group: list(by_group.get(group, [])))
|
|
502
|
+
monkeypatch.setenv("PUBRULES_ANY_VERSION", "1")
|
|
503
|
+
|
|
504
|
+
def context(root, config, offline, kubectl_command=None, zones_file=None, namespace=None, server=None,
|
|
505
|
+
unit=None):
|
|
506
|
+
return self.context(config, offline, namespace, unit)
|
|
507
|
+
monkeypatch.setattr("pubdeploy.context", context)
|
|
508
|
+
monkeypatch.setattr("pubdeploy.ask", lambda prompt: self.outputs.append(prompt) or (self.answers.pop(0)
|
|
509
|
+
if self.answers else ""))
|
|
510
|
+
monkeypatch.setattr("sys.stdin.isatty", lambda: self.terminal, raising=False)
|
|
511
|
+
monkeypatch.setattr("pubdeploy.store.Heads", lambda base, tenant, token: FakeHeads(self, base, tenant, token))
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
def deployments(rendered: str) -> list[dict[str, Any]]:
|
|
515
|
+
"""The cluster's Deployments, as the rendered manifests make them, ready:
|
|
516
|
+
pubsec's k8s.secret restarts those whose pods read the Secret it made."""
|
|
517
|
+
return [{"metadata": {"name": d["metadata"]["name"]}, "spec": {"replicas": 1, "template": d["spec"]["template"]},
|
|
518
|
+
"status": {"readyReplicas": 1}}
|
|
519
|
+
for d in yaml.safe_load_all(rendered) if isinstance(d, dict) and d.get("kind") == "Deployment"]
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
def run(world: World, ctx, *argv: str) -> int:
|
|
523
|
+
"""`pubdeploy ARGV`, in-process, in the world's repository, as the
|
|
524
|
+
scenario's action, read as `When I run` reads a program: its exit code,
|
|
525
|
+
and what it printed, with the message it exits with on stderr, as
|
|
526
|
+
Python prints it. What it printed is kept for the canary too."""
|
|
527
|
+
import sys # noqa: PLC0415
|
|
528
|
+
from pubdeploy.cli import main # noqa: PLC0415
|
|
529
|
+
world.save()
|
|
530
|
+
|
|
531
|
+
def pubdeploy() -> int:
|
|
532
|
+
try:
|
|
533
|
+
return main(list(argv))
|
|
534
|
+
except SystemExit as e:
|
|
535
|
+
if e.code is None or isinstance(e.code, int):
|
|
536
|
+
return e.code or 0
|
|
537
|
+
print(e.code, file=sys.stderr)
|
|
538
|
+
return 1
|
|
539
|
+
ctx.attempt(pubdeploy)
|
|
540
|
+
outcome = ctx.outcome
|
|
541
|
+
if outcome.refused is not None: # it crashed: the scenario fails with it, as it would without pubtest
|
|
542
|
+
raise outcome.refused.error
|
|
543
|
+
outcome.code = outcome.value
|
|
544
|
+
world.outputs += [outcome.stdout, outcome.stderr]
|
|
545
|
+
world.stdout, world.stderr, world.code = outcome.stdout, outcome.stderr, outcome.code
|
|
546
|
+
if "--json" in argv and outcome.stdout.strip():
|
|
547
|
+
world.journal = json.loads(outcome.stdout)
|
|
548
|
+
return outcome.code
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
def walk_needs(world: World, needs: Iterable[Need] | None = None, types: dict[str, Any] | None = None,
|
|
552
|
+
fulfil: Iterable[str] = ()) -> dict[str, Any]:
|
|
553
|
+
"""Walk `needs` (None: the world's service's, as `pubdeploy check` loads
|
|
554
|
+
them) with `types` (None: the ones the walk loads, the world's providers
|
|
555
|
+
included) in the world's context, fulfilling the needs whose ids `fulfil`
|
|
556
|
+
names, and give the deploy-journal/2 document, which the world keeps for
|
|
557
|
+
the canary."""
|
|
558
|
+
from pubdeploy import load # noqa: PLC0415
|
|
559
|
+
from pubdeploy.registry import types as loaded # noqa: PLC0415
|
|
560
|
+
from pubdeploy.walk import fulfil as fulfilling # noqa: PLC0415
|
|
561
|
+
from pubdeploy.walk import walk # noqa: PLC0415
|
|
562
|
+
context = world.context()
|
|
563
|
+
kinds = loaded() if types is None else types
|
|
564
|
+
plan = load(world.root, world.config, context).needs if needs is None else {n.id: n for n in needs}
|
|
565
|
+
journal = walk(plan, kinds, context, service={})
|
|
566
|
+
if wanted := set(fulfil):
|
|
567
|
+
journal = fulfilling(journal, kinds, context, wanted)
|
|
568
|
+
world.journal = journal.document()
|
|
569
|
+
return world.journal
|