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.
Files changed (45) hide show
  1. pubdeploy/__init__.py +532 -0
  2. pubdeploy/channels.py +188 -0
  3. pubdeploy/cli.py +120 -0
  4. pubdeploy/config.py +140 -0
  5. pubdeploy/contract.py +87 -0
  6. pubdeploy/github.py +61 -0
  7. pubdeploy/hooks.py +82 -0
  8. pubdeploy/implied.py +179 -0
  9. pubdeploy/journal.json +207 -0
  10. pubdeploy/k8s.py +472 -0
  11. pubdeploy/machine.py +363 -0
  12. pubdeploy/model.py +229 -0
  13. pubdeploy/names.py +25 -0
  14. pubdeploy/order.py +101 -0
  15. pubdeploy/providers.py +75 -0
  16. pubdeploy/registry.py +96 -0
  17. pubdeploy/release-commit +1 -0
  18. pubdeploy/schema.json +127 -0
  19. pubdeploy/schema.py +238 -0
  20. pubdeploy/store.py +127 -0
  21. pubdeploy/testing/__init__.py +94 -0
  22. pubdeploy/testing/fixtures/deploy-app/.github/workflows/deploy.yml +11 -0
  23. pubdeploy/testing/fixtures/deploy-app/k8s/admin/network-policy.yaml +11 -0
  24. pubdeploy/testing/fixtures/deploy-app/k8s/app.yaml +63 -0
  25. pubdeploy/testing/fixtures/deploy-app/k8s/kustomization.yaml +6 -0
  26. pubdeploy/testing/fixtures/deploy-app/pubrules.json +27 -0
  27. pubdeploy/testing/fixtures/deploy-app/rendered.yaml +86 -0
  28. pubdeploy/testing/fixtures/deploy-app/zones.json +8 -0
  29. pubdeploy/testing/plugin.py +26 -0
  30. pubdeploy/testing/steps.py +1124 -0
  31. pubdeploy/testing/world.py +569 -0
  32. pubdeploy/types/__init__.py +181 -0
  33. pubdeploy/types/auth.py +199 -0
  34. pubdeploy/types/github.py +110 -0
  35. pubdeploy/types/k8s.py +609 -0
  36. pubdeploy/types/net.py +237 -0
  37. pubdeploy/types/rollout.py +186 -0
  38. pubdeploy/units.py +177 -0
  39. pubdeploy/walk.py +325 -0
  40. pubdeploy-0.8.0.dist-info/METADATA +520 -0
  41. pubdeploy-0.8.0.dist-info/RECORD +45 -0
  42. pubdeploy-0.8.0.dist-info/WHEEL +5 -0
  43. pubdeploy-0.8.0.dist-info/entry_points.txt +11 -0
  44. pubdeploy-0.8.0.dist-info/licenses/LICENSE +202 -0
  45. pubdeploy-0.8.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,181 @@
1
+ """The need types pubdeploy has itself, and what their checks and fulfils share."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ from collections.abc import Callable, Mapping
7
+ from concurrent.futures import ThreadPoolExecutor
8
+ from typing import Any
9
+
10
+ from ..github import ApiError
11
+ from ..model import FULFIL_BUDGET, Context, Grade, Need, Ran, Result
12
+
13
+
14
+ class Base:
15
+ """What a built-in type shares. A `singleton` is named by the namespace."""
16
+ type = ""
17
+ schema: dict[str, Any] = {"type": "object", "additionalProperties": False, "properties": {}}
18
+ grade: Grade = "none"
19
+ budget = 1.0
20
+ fulfil_budget = FULFIL_BUDGET
21
+ offline = False
22
+ singleton = False
23
+ needs_of_context: tuple[str, ...] = () # what requires() says, for every need of the type
24
+ # How a live run treats it (machine.py): `serial` chains a kind's needs one after the
25
+ # other; `stop` is what a Stop does while it is fulfilled: signal (end the walk),
26
+ # shorten (end it once the time left is up) or continue (it runs on, and `stop_warning`
27
+ # says why).
28
+ serial = False
29
+ stop = "signal"
30
+ stop_warning = ""
31
+
32
+ def default_name(self, params: dict[str, Any]) -> str | None:
33
+ return None
34
+
35
+ def depends_on(self, need: Need, needs: Mapping[str, Need]) -> list[str]:
36
+ return []
37
+
38
+ def check(self, need: Need, ctx: Context) -> Result:
39
+ raise NotImplementedError
40
+
41
+ def requires(self, need: Need) -> tuple[str, ...]:
42
+ return self.needs_of_context
43
+
44
+ def action(self, need: Need, ctx: Context) -> str:
45
+ return "fulfil it"
46
+
47
+ def fulfil(self, need: Need, ctx: Context) -> Result:
48
+ return Result("unknown", f"{self.type} has no fulfil", fix="see the check's fix")
49
+
50
+
51
+ def failed(what: str, ran: Ran, hidden: tuple[bytes, ...] = ()) -> Result:
52
+ """A fulfil's failure, with the last line the command said, any hidden value taken out."""
53
+ last = (ran.stderr.strip().splitlines() or [f"exit {ran.returncode}"])[-1][:300]
54
+ for value in hidden:
55
+ for part in [value, *value.splitlines()]:
56
+ if part:
57
+ last = last.replace(part.decode(errors="replace"), "[hidden]")
58
+ return Result("missing", what, fix=last)
59
+
60
+
61
+ def secret_of(need: Need) -> dict[str, Any] | None:
62
+ secret = need.params.get("secret")
63
+ return secret if isinstance(secret, dict) else None
64
+
65
+
66
+ def readers(ctx: Context, secret: str) -> list[str]:
67
+ """The Deployments the rendered manifests show reading the Secret in the namespace."""
68
+ from .. import k8s # noqa: PLC0415
69
+ try:
70
+ found = k8s.collect(k8s.documents(ctx.rendered()), ctx.namespace)
71
+ except Exception: # noqa: BLE001 - no manifests to read, no Deployment to restart
72
+ return []
73
+ out = []
74
+ for n in found.needs:
75
+ workload = n.where.split(" of ", 1)[-1].split(" ", 1)[0]
76
+ if n.namespace == ctx.namespace and n.secret == secret and workload.startswith("Deployment/"):
77
+ out.append(workload.split("/", 1)[1])
78
+ return sorted(set(out))
79
+
80
+
81
+ def restart(ctx: Context, secret: str) -> list[str]:
82
+ """Restart each Deployment reading the Secret, so its pods read the new one: a detail each."""
83
+ out = []
84
+ for name in readers(ctx, secret):
85
+ ran = ctx.kubectl(["-n", ctx.namespace, "rollout", "restart", f"deployment/{name}"])
86
+ if ran.returncode == 0:
87
+ out.append(f"restarted Deployment/{name}, which reads Secret {secret}")
88
+ elif not not_found(ran):
89
+ out.append(f"Deployment/{name} was not restarted: {failed('', ran).fix}")
90
+ return out
91
+
92
+
93
+ def last_success(ctx: Context, cronjob: str, status: dict[str, Any]) -> str | None:
94
+ """When the CronJob last succeeded: its own lastSuccessfulTime, or a Job
95
+ made from it by hand that completed later. Such a Job, `create job
96
+ --from=cronjob`, is not the CronJob's, so its success never moves the
97
+ CronJob's status: the Jobs named NAME-pubrules-... (a fulfil's) and NAME-now
98
+ (the fix's) are looked at too."""
99
+ times = [str(status["lastSuccessfulTime"])] if status.get("lastSuccessfulTime") else []
100
+ found, _ran = kubectl_json(ctx, "-n", ctx.namespace, "get", "job")
101
+ for job in (found or {}).get("items") or []:
102
+ name = str((job.get("metadata") or {}).get("name", ""))
103
+ done = job.get("status") or {}
104
+ if (name.startswith(f"{cronjob}-pubrules-") or name == f"{cronjob}-now") and (done.get("succeeded") or 0) >= 1 \
105
+ and done.get("completionTime"):
106
+ times.append(str(done["completionTime"]))
107
+ return max(times) if times else None
108
+
109
+
110
+ def run_job(ctx: Context, cronjob: str, budget: float) -> Result:
111
+ """A Job made from the CronJob, waited for: ready once it completes."""
112
+ job = f"{cronjob}-pubrules-{ctx.now().strftime('%Y%m%d%H%M%S')}"[:63]
113
+ ran = ctx.kubectl(["-n", ctx.namespace, "create", "job", f"--from=cronjob/{cronjob}", job])
114
+ if ran.returncode != 0:
115
+ return failed(f"a Job could not be made from CronJob {cronjob}", ran)
116
+ wait = max(int(budget) - 20, 10)
117
+ ran = ctx.kubectl(["-n", ctx.namespace, "wait", "--for=condition=complete", f"job/{job}", f"--timeout={wait}s"])
118
+ if ran.returncode != 0:
119
+ return failed(f"Job {job}, from CronJob {cronjob}, did not complete in {wait} s", ran)
120
+ return Result("ready", f"ran Job {job} from CronJob {cronjob}, which completed")
121
+
122
+
123
+ def schema(properties: dict[str, Any], required: tuple[str, ...] = ()) -> dict[str, Any]:
124
+ out: dict[str, Any] = {"type": "object", "additionalProperties": False, "properties": properties}
125
+ if required:
126
+ out["required"] = list(required)
127
+ return out
128
+
129
+
130
+ def with_secret(properties: dict[str, Any]) -> dict[str, Any]:
131
+ from ..schema import SECRET # noqa: PLC0415
132
+ return {**properties, "secret": SECRET}
133
+
134
+
135
+ def kubectl_json(ctx: Context, *args: str) -> tuple[Any, Ran]:
136
+ """kubectl's JSON answer to `args` -o json, or None, and how it ran."""
137
+ ran = ctx.kubectl([*args, "-o", "json"])
138
+ if ran.returncode != 0:
139
+ return None, ran
140
+ try:
141
+ return json.loads(ran.stdout), ran
142
+ except ValueError:
143
+ return None, Ran(1, "", "kubectl's answer is not JSON")
144
+
145
+
146
+ def not_found(ran: Ran) -> bool:
147
+ return "(NotFound)" in ran.stderr or " not found" in ran.stderr
148
+
149
+
150
+ def kubectl_refused(ran: Ran, ns: str, what: str) -> Result:
151
+ """`unknown`, saying what access is missing, or what kubectl said."""
152
+ last = (ran.stderr.strip().splitlines() or [f"exit {ran.returncode}"])[-1]
153
+ if ran.returncode == 127:
154
+ return Result("unknown", f"kubectl did not run, to read {what}", fix="install kubectl, or give --kubectl CMD")
155
+ if "(Forbidden)" in ran.stderr or "forbidden" in ran.stderr:
156
+ return Result("unknown", f"kubectl may not read {what} in {ns}",
157
+ fix=f"use a kubeconfig that may get {what} in {ns} ({last})")
158
+ return Result("unknown", f"kubectl could not read {what} in {ns}", fix=f"kubectl said: {last}")
159
+
160
+
161
+ def at_once(gh: Callable[..., Any], calls: list[tuple[str, str, Any]], keep_errors: bool = False) -> list[Any]:
162
+ """GitHub calls asked together, each `gh api` its own process, and their
163
+ answers in order: a check that asks several one after another overruns
164
+ its budget at about 0.6 s a call. A refusal is raised, or, with
165
+ `keep_errors`, is that call's answer."""
166
+ def one(call: tuple[str, str, Any]) -> Any:
167
+ try:
168
+ return gh(*call)
169
+ except ApiError as e:
170
+ if keep_errors:
171
+ return e
172
+ raise
173
+ with ThreadPoolExecutor(max_workers=max(1, len(calls))) as pool:
174
+ return list(pool.map(one, calls))
175
+
176
+
177
+ def github_refused(error: ApiError, what: str) -> Result:
178
+ if error.status in (401, 403):
179
+ return Result("unknown", f"GitHub refused to show {what}",
180
+ fix="sign gh in as someone who administers the repository (gh auth login)")
181
+ return Result("unknown", f"GitHub could not be asked for {what}", fix=f"gh said: {str(error)[:200]}")
@@ -0,0 +1,199 @@
1
+ """auth.sign-in-client: the service's sign-in client, registered with an
2
+ identity provider's realm by the auth repository's register-app.py, which
3
+ runs on the server, where the client is kept, and puts it in a Secret there.
4
+
5
+ It drives register-app.py as a service's register-client.sh does: the
6
+ script goes over the admin channel on python3's standard input, with the
7
+ registration's settings as arguments. The check asks `--status`, which
8
+ changes nothing and prints {"status": "none" | "waiting" | "registered"}:
9
+
10
+ registered, and the Secret has both keys ready
11
+ registered, and the Secret is not there missing: the fulfil makes it on the
12
+ server, from the client kept there, so
13
+ its value never leaves the server
14
+ waiting blocked: the approval record must be
15
+ published; the fulfil writes it into
16
+ `approvals` when that file is here
17
+ none blocked: a person signs in (human)
18
+
19
+ Without the admin channel it is unknown. Whether the person has the role on
20
+ the access page is not checked: the fix names the page.
21
+ """
22
+
23
+ from __future__ import annotations
24
+
25
+ import json
26
+ from pathlib import Path
27
+ from typing import Any
28
+
29
+ from ..model import Context, Need, Result
30
+ from . import Base, failed, restart, schema, secret_of
31
+ from .net import HOST, HTTPS
32
+
33
+ FILE = {"type": "string", "pattern": r"^(?!/)[A-Za-z0-9_./-]+$", "x-phrase": "must be a path from the repository"}
34
+ WORD = {"type": "string", "pattern": r"^[a-z0-9][a-z0-9-]*$", "x-phrase": "must be a lower-case name"}
35
+ KEYS = ["client-id", "client-secret"]
36
+ # Run on the server, on python3's standard input: the client kept there,
37
+ # applied as a Secret manifest on kubectl's standard input. argv: the
38
+ # client's file, the Secret's name and its namespace, none of them a value.
39
+ SECRET_SCRIPT = b'''import base64, json, subprocess, sys
40
+ client = json.load(open(sys.argv[1]))
41
+ b64 = lambda v: base64.b64encode(v.encode()).decode()
42
+ manifest = json.dumps({"apiVersion": "v1", "kind": "Secret", "type": "Opaque",
43
+ "metadata": {"name": sys.argv[2], "namespace": sys.argv[3]},
44
+ "data": {"client-id": b64(client["client_id"]), "client-secret": b64(client["client_secret"])}})
45
+ sys.exit(subprocess.run(["k3s", "kubectl", "apply", "-f", "-"], input=manifest.encode()).returncode)
46
+ '''
47
+
48
+
49
+ class SignInClient(Base):
50
+ type = "auth.sign-in-client"
51
+ grade = "auto"
52
+ budget = 6.0
53
+ fulfil_budget = 120.0
54
+ needs_of_context = ("server",)
55
+ schema = schema({"realm": WORD, "role": WORD, "auth": HTTPS, "domain": HOST, "host": HOST,
56
+ "state": {"type": "string", "pattern": r"^/[A-Za-z0-9_./-]+$",
57
+ "x-phrase": "must be the directory on the server where the client is kept"},
58
+ "client_name": {"type": "string", "minLength": 1},
59
+ "register_app": FILE, "approvals": FILE,
60
+ "access": {"type": "string", "pattern": r"^https://", "x-phrase": "must be an https:// address"},
61
+ "secret": {"type": "object", "required": ["name"], "additionalProperties": False,
62
+ "properties": {"name": {"type": "string",
63
+ "pattern": r"^[a-z0-9]([-a-z0-9.]{0,251}[a-z0-9])?$"}},
64
+ "x-phrase": "must be an object with the Secret's name"}},
65
+ ("realm", "role", "auth", "domain", "state", "secret"))
66
+
67
+ def default_name(self, params: dict[str, Any]) -> str | None:
68
+ return params.get("role")
69
+
70
+ def secret_keys(self, params: dict[str, Any]) -> list[str]:
71
+ return list(KEYS)
72
+
73
+ def script(self, need: Need, ctx: Context) -> Path:
74
+ return Path(ctx.root) / need.params.get("register_app", "../auth/register-app.py")
75
+
76
+ def argv(self, need: Need, ctx: Context, *more: str) -> list[str]:
77
+ p = need.params
78
+ host = p.get("host") or ctx.block.get("host") or p["domain"]
79
+ return ["python3", "-I", "-", "--state-dir", p["state"], "--auth", p["auth"], "--domain", p["domain"],
80
+ "--name", p.get("client_name", f"{p['domain']} {p['realm']} sites"),
81
+ "--redirect", f"https://{host}/oauth2/callback", "--logout", f"https://{host}/",
82
+ "--role", p["role"], *more]
83
+
84
+ def status(self, need: Need, ctx: Context) -> tuple[dict[str, Any] | None, Result | None]:
85
+ script = self.script(need, ctx)
86
+ if not script.is_file():
87
+ return None, Result("unknown", f"no {script.name} at {need.params.get('register_app', '../auth/register-app.py')}",
88
+ fix="clone the auth repository beside this one, or give the need's register_app")
89
+ ran = ctx.server(self.argv(need, ctx, "--status"), script.read_bytes())
90
+ if ran.returncode == 127:
91
+ return None, Result("unknown", "the sign-in client is judged over the admin channel only",
92
+ fix="run the walk with --server HOST, the admin channel")
93
+ try:
94
+ found = json.loads(ran.stdout)
95
+ except ValueError:
96
+ found = None
97
+ if not isinstance(found, dict) or found.get("status") not in ("none", "waiting", "registered"):
98
+ return None, Result("unknown", f"{script.name} --status gave no status",
99
+ fix=(ran.stderr.strip().splitlines() or [f"exit {ran.returncode}"])[-1][:300])
100
+ return found, None
101
+
102
+ def check(self, need: Need, ctx: Context) -> Result:
103
+ found, result = self.status(need, ctx)
104
+ if result is not None:
105
+ return result
106
+ secret, domain = (secret_of(need) or {})["name"], need.params["domain"]
107
+ page = need.params.get("access", "the realm's access page")
108
+ if found["status"] == "none":
109
+ command = " ".join(self.argv(need, ctx))
110
+ return Result("blocked", f"the {domain} client is not registered: a person signs in", fixable=False,
111
+ fix=f"ssh root@<server> \"{command}\" < {self.script(need, ctx).name}: it prints a link and "
112
+ f"a code; sign in there as the registrant, with the role {need.params['role']} on {page}")
113
+ if found["status"] == "waiting":
114
+ approvals = need.params.get("approvals")
115
+ record = found.get("record") if isinstance(found.get("record"), dict) else {}
116
+ return Result("blocked", f"the {domain} client waits for its approval record",
117
+ fix=f"the approval record is waiting: add it to {approvals or 'the approvals file'} and apply",
118
+ details=[f"record {record.get('name', '?')} TXT"],
119
+ fixable=bool(approvals) and (Path(ctx.root) / approvals).is_file(), step="record")
120
+ site = self.site(need, ctx)
121
+ if site.state != "ready":
122
+ return site
123
+ # The Secret is read by k8s.secret, pubsec's, which alone reads a Secret's values.
124
+ reader = ctx.types.get("k8s.secret")
125
+ if reader is None:
126
+ return Result("unknown", f"Secret {secret} is not looked at without k8s.secret",
127
+ fix="list pubsec in deploy.providers, as `pubrules sync` does")
128
+ held = reader.check(Need("k8s.secret", secret, {"keys": KEYS}), ctx)
129
+ if held.state == "ready":
130
+ return Result("ready", f"the {domain} client is registered, and Secret {secret} has its keys")
131
+ if held.state == "missing":
132
+ return Result("missing", f"the {domain} client is registered, and Secret {secret} is not in {ctx.namespace}",
133
+ fix=f"run the walk with --fulfil, which makes it on the server", step="push")
134
+ return held
135
+
136
+ def site(self, need: Need, ctx: Context) -> Result:
137
+ """Whether this site's redirect and logout URIs and role are on the registered
138
+ client (register-app.py --site-status): every site of a domain shares one."""
139
+ ran = ctx.server(self.argv(need, ctx, "--site-status"), self.script(need, ctx).read_bytes())
140
+ lines = [line for line in ran.stdout.splitlines() if line.strip()]
141
+ if ran.returncode == 0:
142
+ return Result("ready", "this site is on the client")
143
+ if ran.returncode == 1:
144
+ gone = [line.rsplit(":", 1)[0] for line in lines if line.endswith(": missing")]
145
+ return Result("missing", f"the {need.params['domain']} client lacks this site's {', '.join(gone)}",
146
+ fix="run the walk with --fulfil, which adds them to the client on the server", step="site")
147
+ said = (lines or ran.stderr.strip().splitlines() or [f"exit {ran.returncode}"])[-1][:300]
148
+ return Result("unknown", f"whether this site is on the client could not be read: {said}",
149
+ fix="./with-secrets ./pubdeploy walk --fulfil --server HOST, from a laptop with the auth checkout")
150
+
151
+ def action(self, need: Need, ctx: Context) -> str:
152
+ approvals = need.params.get("approvals")
153
+ if ctx.results.get(need.id, Result("ready", "")).step == "record":
154
+ return f"write the approval record into {approvals}, for a pull request there"
155
+ if ctx.results.get(need.id, Result("ready", "")).step == "site":
156
+ return (f"add this site's redirect and role to the {need.params['domain']} client on the server, "
157
+ f"then make Secret {(secret_of(need) or {}).get('name')}")
158
+ return f"make Secret {(secret_of(need) or {}).get('name')} on the server, from the client kept there"
159
+
160
+ def fulfil(self, need: Need, ctx: Context) -> Result:
161
+ found, result = self.status(need, ctx)
162
+ if result is not None:
163
+ return Result("missing", result.summary, fix=result.fix)
164
+ if found["status"] == "waiting":
165
+ return self.record(need, ctx, found)
166
+ if found["status"] != "registered":
167
+ return Result("missing", "the client is not registered", fix="see the check's fix")
168
+ if self.site(need, ctx).state == "missing":
169
+ # The site's step: its URIs and roles added to the client the domain's sites share.
170
+ ran = ctx.server(self.argv(need, ctx), self.script(need, ctx).read_bytes())
171
+ if ran.returncode != 0:
172
+ return failed("this site could not be added to the client on the server", ran)
173
+ secret = (secret_of(need) or {})["name"]
174
+ ran = ctx.server(["python3", "-I", "-", f"{need.params['state']}/client.json", secret, ctx.namespace],
175
+ SECRET_SCRIPT)
176
+ if ran.returncode != 0:
177
+ return failed(f"Secret {secret} was not made on the server", ran)
178
+ site = self.site(need, ctx) # read again, so the journal says ready from what the client holds
179
+ if site.state != "ready":
180
+ return Result(site.state, f"made Secret {secret}, but {site.summary}", fix=site.fix)
181
+ return Result("ready", f"made Secret {secret} in {ctx.namespace} on the server, from the client kept there; "
182
+ "this site is on the client", details=restart(ctx, secret))
183
+
184
+ def record(self, need: Need, ctx: Context, found: dict[str, Any]) -> Result:
185
+ """The approval record, written into the approvals file, as register-client.sh does."""
186
+ approvals = Path(ctx.root) / need.params["approvals"]
187
+ value = (found.get("record") or {}).get("value")
188
+ if not isinstance(value, str) or not value:
189
+ return Result("missing", "the status gives no record to write", fix="see register-app.py's --status")
190
+ try:
191
+ held = json.loads(approvals.read_text(encoding="utf-8"))
192
+ except (OSError, ValueError) as e:
193
+ return Result("missing", f"{need.params['approvals']} cannot be read as JSON", fix=type(e).__name__)
194
+ if held.get("@") == value:
195
+ return Result("missing", f"{need.params['approvals']} has the record already, and it is not published yet",
196
+ fix="merge it and apply there; the DNS may take a while (its TTL)")
197
+ held["@"] = value
198
+ approvals.write_text(json.dumps(held, indent=2) + "\n", encoding="utf-8")
199
+ return Result("ready", f"wrote the approval record into {need.params['approvals']}: commit it, merge, apply")
@@ -0,0 +1,110 @@
1
+ """github.variable-entry: an entry of a JSON variable another repository
2
+ keeps, read through the API (`gh`), and what github.gate and deploy.rollout
3
+ read of the repository's variables. github.secret, github.variable and
4
+ github.deploy-settings, which set what Bitwarden holds, are pubsec's."""
5
+
6
+ from __future__ import annotations
7
+
8
+ import json
9
+ from typing import Any
10
+
11
+ from ..github import ApiError
12
+ from ..model import Context, Need, Result
13
+ from . import Base, github_refused, schema
14
+
15
+ GITHUB_NAME = {"type": "string", "pattern": r"^(?!GITHUB_)[A-Z_][A-Z0-9_]*$",
16
+ "x-phrase": "must be upper case letters, digits and _, not starting GITHUB_"}
17
+ FROM = {"type": "string", "pattern": r"^[A-Za-z0-9][A-Za-z0-9_.-]*$", "x-phrase": "must be a secret's name"}
18
+ ENVIRONMENT = {"type": "string", "pattern": r"^[A-Za-z0-9][A-Za-z0-9_.-]*$",
19
+ "x-phrase": "must be an environment's name"}
20
+ SERVER_ENVIRONMENT = "server" # k8s-deploy.yml's environment input, by default
21
+ NO_ORIGIN = Result("unknown", "origin is not a GitHub repository",
22
+ fix="run this in a clone whose origin is the repository on GitHub")
23
+
24
+
25
+ def where(slug: str, environment: str | None) -> str:
26
+ return f"{slug}, environment {environment}" if environment else slug
27
+
28
+
29
+ def path(slug: str, kind: str, name: str, environment: str | None) -> str:
30
+ plural = f"{kind}s"
31
+ if environment:
32
+ return f"repos/{slug}/environments/{environment}/{plural}/{name}"
33
+ return f"repos/{slug}/actions/{plural}/{name}"
34
+
35
+
36
+ POINTER = {"type": "string", "pattern": r"^(/([^~/]|~[01])*)+$", "x-phrase": "must be a JSON pointer, such as /evaluator"}
37
+ REPOSITORY = {"type": "string", "pattern": r"^[A-Za-z0-9][A-Za-z0-9-]*/[A-Za-z0-9_.-]+$",
38
+ "x-phrase": "must be a repository, OWNER/NAME"}
39
+
40
+
41
+ def pointed(document: Any, pointer: str) -> tuple[bool, Any]:
42
+ """Whether the JSON pointer is in the document, and what it points at."""
43
+ node = document
44
+ for part in pointer.split("/")[1:]:
45
+ step = part.replace("~1", "/").replace("~0", "~")
46
+ if isinstance(node, dict) and step in node:
47
+ node = node[step]
48
+ elif isinstance(node, list) and step.isdigit() and int(step) < len(node):
49
+ node = node[int(step)]
50
+ else:
51
+ return False, None
52
+ return True, node
53
+
54
+
55
+ class VariableEntry(Base):
56
+ """An entry of a JSON variable another repository keeps, such as a release
57
+ state: the variable is set, is JSON, has the pointer, and maybe its value.
58
+ The variable's value is compared, never shown."""
59
+ type = "github.variable-entry"
60
+ grade = "propose"
61
+ budget = 3.0
62
+ schema = schema({"repository": REPOSITORY, "variable": GITHUB_NAME, "path": POINTER, "value": {}},
63
+ ("repository", "variable", "path"))
64
+
65
+ def default_name(self, params: dict[str, Any]) -> str | None:
66
+ if all(isinstance(params.get(k), str) for k in ("repository", "variable", "path")):
67
+ return f"{params['repository']}/{params['variable']}{params['path']}"
68
+ return None
69
+
70
+ def check(self, need: Need, ctx: Context) -> Result:
71
+ repo, variable, pointer = need.params["repository"], need.params["variable"], need.params["path"]
72
+ try:
73
+ found = ctx.gh("GET", f"repos/{repo}/actions/variables/{variable}", None) or {}
74
+ except ApiError as e:
75
+ if e.status == 404:
76
+ return Result("missing", f"variable {variable} is not set in {repo}", fix=self.where(need))
77
+ return github_refused(e, f"variable {variable} of {repo}")
78
+ try:
79
+ document = json.loads(found.get("value") if isinstance(found, dict) else "")
80
+ except (TypeError, ValueError):
81
+ return Result("drift", f"variable {variable} of {repo} is not JSON", fix=self.where(need))
82
+ there, value = pointed(document, pointer)
83
+ del document
84
+ if not there:
85
+ return Result("missing", f"variable {variable} of {repo} has no {pointer}", fix=self.where(need))
86
+ if "value" in need.params and value != need.params["value"]:
87
+ return Result("drift", f"{pointer} of variable {variable} in {repo} is not the need's value",
88
+ fix=self.where(need))
89
+ compared = ", the need's value" if "value" in need.params else ""
90
+ return Result("ready", f"variable {variable} of {repo} has {pointer}{compared}")
91
+
92
+ def where(self, need: Need) -> str:
93
+ repo, variable, pointer = need.params["repository"], need.params["variable"], need.params["path"]
94
+ value = json.dumps(need.params["value"]) if "value" in need.params else "<its value>"
95
+ return (f"in the owner's state file, under \"{repo.rsplit('/', 1)[-1]}\": set {pointer} of variables."
96
+ f"{variable} to {value}\nthen pubrules variables --apply {repo}")
97
+
98
+ def fulfil(self, need: Need, ctx: Context) -> Result:
99
+ return Result("missing", "the variable is the state file's", fix=self.where(need))
100
+
101
+
102
+ def gate_value(ctx: Context, variable: str) -> tuple[str | None, Result | None]:
103
+ """The gate variable's value, None when unset, or the result of a refusal."""
104
+ try:
105
+ found: Any = ctx.gh("GET", path(ctx.slug, "variable", variable, None), None) or {}
106
+ except ApiError as e:
107
+ if e.status == 404:
108
+ return None, None
109
+ return None, github_refused(e, f"variable {variable}")
110
+ return (found.get("value") if isinstance(found, dict) else None), None