sdtk-ops-kit 0.3.4 → 0.4.0
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.
- package/README.md +2 -1
- package/assets/manifest/toolkit-bundle.manifest.json +133 -13
- package/assets/manifest/toolkit-bundle.sha256.txt +31 -7
- package/assets/toolkit/toolkit/AGENTS.md +2 -1
- package/assets/toolkit/toolkit/SDTKOPS_TOOLKIT.md +4 -2
- package/assets/toolkit/toolkit/scripts/install-claude-skills.ps1 +4 -4
- package/assets/toolkit/toolkit/scripts/install-codex-skills.ps1 +4 -4
- package/assets/toolkit/toolkit/scripts/uninstall-claude-skills.ps1 +2 -2
- package/assets/toolkit/toolkit/scripts/uninstall-codex-skills.ps1 +2 -2
- package/assets/toolkit/toolkit/skills/ops-discover/SKILL.md +2 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/SKILL.md +172 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/references/CLUSTER_CA.md +83 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/references/GOTCHAS.md +17 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/references/K3D_CILIUM_BOOTSTRAP.md +39 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/references/PROFILE.md +44 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/references/bootstrap/cilium-values.yaml +37 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/references/bootstrap/k3d-cluster.yaml +37 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/references/profile.example.json +80 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/admin_creds.py +212 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/cilium_heal.py +136 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/ensure_repos.py +69 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/envprofile.py +139 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/explain_config.py +116 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/health.py +109 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/hosts_entry.py +158 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/init_profile.py +217 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/k3d_images.py +70 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/preflight.py +96 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/registry_secret.py +64 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/shared_config.py +178 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/stack_plan.py +94 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/teardown.py +86 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/test_local_env.py +307 -0
- package/assets/toolkit/toolkit/skills/ops-local-env/scripts/trust_ca.py +153 -0
- package/package.json +1 -1
- package/src/lib/scope.js +1 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
"""ops-local-env scripts on a temporary workspace built from references/profile.example.json.
|
|
2
|
+
python -m unittest test_local_env (from this folder). No cluster, no network, no root needed."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
import shutil
|
|
9
|
+
import subprocess
|
|
10
|
+
import sys
|
|
11
|
+
import tempfile
|
|
12
|
+
import unittest
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
HERE = Path(__file__).resolve().parent
|
|
16
|
+
EXAMPLE = HERE.parent / "references" / "profile.example.json"
|
|
17
|
+
|
|
18
|
+
INFRA = """# Cluster
|
|
19
|
+
apps_namespace: "demo" # namespace of the applications; also the k3d cluster name
|
|
20
|
+
monitoring_namespace: "monitoring" # namespace of the monitoring stack
|
|
21
|
+
base_domain: "example.test" # every other *_domain is one label under it
|
|
22
|
+
site_domain: "app.example.test" # the application's entry point
|
|
23
|
+
db_name: "appdb" # the PostgreSQL cluster
|
|
24
|
+
ssl_country: "VN"
|
|
25
|
+
"""
|
|
26
|
+
STORAGE = """apps_namespace: "demo"
|
|
27
|
+
base_domain: "example.test"
|
|
28
|
+
s3_domain: "s3.example.test" # object storage endpoint
|
|
29
|
+
storage_admin_user: "admin"
|
|
30
|
+
"""
|
|
31
|
+
DB = """apps_namespace: "demo"
|
|
32
|
+
db_name: "appdb"
|
|
33
|
+
db_admin_user: "admin"
|
|
34
|
+
"""
|
|
35
|
+
MQ = """apps_namespace: "demo"
|
|
36
|
+
mq_default_user: "admin"
|
|
37
|
+
"""
|
|
38
|
+
MONITORING = """apps_namespace: "monitoring" # the monitoring namespace
|
|
39
|
+
base_domain: "example.test"
|
|
40
|
+
grafana_domain: "grafana.example.test"
|
|
41
|
+
grafana_admin_user: "admin"
|
|
42
|
+
targets:
|
|
43
|
+
- name: api
|
|
44
|
+
namespace: "demo"
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def run(*args: str, stdin: str | None = None, cwd: Path | None = None) -> subprocess.CompletedProcess:
|
|
49
|
+
return subprocess.run([sys.executable, *args], capture_output=True, text=True, input=stdin,
|
|
50
|
+
cwd=str(cwd or HERE), check=False, env={**os.environ, "PYTHONPATH": str(HERE)})
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class LocalEnvTests(unittest.TestCase):
|
|
54
|
+
def setUp(self) -> None:
|
|
55
|
+
self.tmp = tempfile.TemporaryDirectory()
|
|
56
|
+
self.root = Path(self.tmp.name)
|
|
57
|
+
files = {"demo-infra/k8s/config.yaml": INFRA, "demo-storage/config.yaml": STORAGE, "demo-db/config.yaml": DB,
|
|
58
|
+
"demo-mq/config.yaml": MQ, "demo-monitoring/config.yaml": MONITORING}
|
|
59
|
+
for rel, text in files.items():
|
|
60
|
+
(self.root / rel).parent.mkdir(parents=True, exist_ok=True)
|
|
61
|
+
(self.root / rel).write_text(text, encoding="utf-8")
|
|
62
|
+
for d in ("demo-charts",):
|
|
63
|
+
(self.root / d).mkdir()
|
|
64
|
+
self.profile = self.root / "demo-infra" / "ops-env.profile.json"
|
|
65
|
+
shutil.copy(EXAMPLE, self.profile)
|
|
66
|
+
self.common = ["--profile", str(self.profile)]
|
|
67
|
+
|
|
68
|
+
def tearDown(self) -> None:
|
|
69
|
+
self.tmp.cleanup()
|
|
70
|
+
|
|
71
|
+
def script(self, name: str, *args: str, stdin: str | None = None) -> subprocess.CompletedProcess:
|
|
72
|
+
return run(str(HERE / name), *args, stdin=stdin)
|
|
73
|
+
|
|
74
|
+
def test_profile_is_found_from_the_workspace_and_the_plan_is_valid(self) -> None:
|
|
75
|
+
res = run(str(HERE / "stack_plan.py"), cwd=self.root)
|
|
76
|
+
self.assertEqual(res.returncode, 0, res.stdout + res.stderr)
|
|
77
|
+
self.assertIn(f"workspace: {self.root}", res.stdout)
|
|
78
|
+
self.assertIn("| 1 | cluster | demo-infra |", res.stdout)
|
|
79
|
+
self.assertIn("kubectl -n {apps_namespace} get postgrescluster {db_name}", res.stdout)
|
|
80
|
+
|
|
81
|
+
def test_plan_check_reports_profile_errors(self) -> None:
|
|
82
|
+
data = json.loads(self.profile.read_text())
|
|
83
|
+
data["components"][1]["after"] = ["apps"]
|
|
84
|
+
data["components"].append({"id": "x", "repo": "nowhere"})
|
|
85
|
+
self.profile.write_text(json.dumps(data))
|
|
86
|
+
res = self.script("stack_plan.py", "--check", *self.common)
|
|
87
|
+
self.assertEqual(res.returncode, 1)
|
|
88
|
+
self.assertIn("after apps, which is not an earlier component", res.stdout)
|
|
89
|
+
self.assertIn("repo nowhere is not declared", res.stdout)
|
|
90
|
+
self.assertIn("needs `skill`, `run` or `steps`", res.stdout)
|
|
91
|
+
|
|
92
|
+
def test_shared_config_show_flags_mismatch_and_respects_aliases(self) -> None:
|
|
93
|
+
res = self.script("shared_config.py", "--show", *self.common)
|
|
94
|
+
self.assertEqual(res.returncode, 0, res.stdout + res.stderr) # monitoring's apps_namespace is an alias
|
|
95
|
+
(self.root / "demo-db/config.yaml").write_text(DB.replace('"appdb"', '"otherdb"'))
|
|
96
|
+
res = self.script("shared_config.py", "--show", *self.common)
|
|
97
|
+
self.assertEqual(res.returncode, 1)
|
|
98
|
+
self.assertIn("MISMATCH", res.stdout)
|
|
99
|
+
|
|
100
|
+
def test_new_base_domain_moves_every_domain_and_keeps_comments(self) -> None:
|
|
101
|
+
dry = self.script("shared_config.py", "--set", "base_domain=lab.test", *self.common)
|
|
102
|
+
self.assertEqual(dry.returncode, 0, dry.stderr)
|
|
103
|
+
self.assertIn("dry run", dry.stdout)
|
|
104
|
+
self.assertIn('base_domain: "example.test"', (self.root / "demo-infra/k8s/config.yaml").read_text())
|
|
105
|
+
res = self.script("shared_config.py", "--set", "base_domain=lab.test", "--apply", *self.common)
|
|
106
|
+
self.assertEqual(res.returncode, 0, res.stderr)
|
|
107
|
+
infra = (self.root / "demo-infra/k8s/config.yaml").read_text()
|
|
108
|
+
self.assertIn('site_domain: "app.lab.test" # the application', infra) # comment column kept
|
|
109
|
+
self.assertIn('s3_domain: "s3.lab.test"', (self.root / "demo-storage/config.yaml").read_text())
|
|
110
|
+
self.assertIn('grafana_domain: "grafana.lab.test"', (self.root / "demo-monitoring/config.yaml").read_text())
|
|
111
|
+
self.assertEqual(self.script("shared_config.py", "--show", *self.common).returncode, 0)
|
|
112
|
+
|
|
113
|
+
def test_new_namespace_follows_aliases_and_indented_lines(self) -> None:
|
|
114
|
+
res = self.script("shared_config.py", "--set", "apps_namespace=qa", "--apply", *self.common)
|
|
115
|
+
self.assertEqual(res.returncode, 0, res.stderr)
|
|
116
|
+
mon = (self.root / "demo-monitoring/config.yaml").read_text()
|
|
117
|
+
self.assertIn('apps_namespace: "monitoring"', mon) # alias: not the apps namespace there
|
|
118
|
+
self.assertIn(' namespace: "qa"', mon) # indented follower moved
|
|
119
|
+
self.assertIn('apps_namespace: "qa"', (self.root / "demo-db/config.yaml").read_text())
|
|
120
|
+
|
|
121
|
+
def test_bad_values_are_refused(self) -> None:
|
|
122
|
+
self.assertEqual(self.script("shared_config.py", "--set", "apps_namespace=Bad_NS", *self.common).returncode, 1)
|
|
123
|
+
self.assertEqual(self.script("shared_config.py", "--set", "site_domain=a.b.example.test", *self.common).returncode, 1)
|
|
124
|
+
self.assertEqual(self.script("shared_config.py", "--set", "nope=1", *self.common).returncode, 1)
|
|
125
|
+
|
|
126
|
+
def test_admin_password_is_generated_into_secrets_never_printed(self) -> None:
|
|
127
|
+
dry = self.script("admin_creds.py", "--user", "tester", "--generate", *self.common)
|
|
128
|
+
self.assertEqual(dry.returncode, 0, dry.stderr)
|
|
129
|
+
self.assertIn('"********"', dry.stdout)
|
|
130
|
+
self.assertFalse((self.root / "demo-db/secrets.yaml").exists())
|
|
131
|
+
res = self.script("admin_creds.py", "--user", "tester", "--generate", "--apply", *self.common)
|
|
132
|
+
self.assertEqual(res.returncode, 0, res.stderr)
|
|
133
|
+
secret = (self.root / "demo-db/secrets.yaml").read_text().split('"')[1]
|
|
134
|
+
self.assertNotIn(secret, res.stdout + res.stderr)
|
|
135
|
+
self.assertEqual(oct((self.root / "demo-db/secrets.yaml").stat().st_mode & 0o777), "0o600")
|
|
136
|
+
show = self.script("admin_creds.py", "--show", *self.common)
|
|
137
|
+
self.assertIn("users: all the same; passwords: all the same", show.stdout)
|
|
138
|
+
self.assertNotIn(secret, show.stdout)
|
|
139
|
+
reveal = self.script("admin_creds.py", "--reveal", *self.common)
|
|
140
|
+
self.assertIn(f"admin password: {secret}", reveal.stdout)
|
|
141
|
+
self.assertIn('db_admin_user: "tester"', (self.root / "demo-db/config.yaml").read_text())
|
|
142
|
+
|
|
143
|
+
def test_typed_password_comes_from_stdin_and_is_validated(self) -> None:
|
|
144
|
+
bad = self.script("admin_creds.py", "--password-stdin", "--apply", *self.common, stdin="short\n")
|
|
145
|
+
self.assertEqual(bad.returncode, 1)
|
|
146
|
+
good = "Strong-Pass-1234"
|
|
147
|
+
res = self.script("admin_creds.py", "--password-stdin", "--apply", *self.common, stdin=good + "\n")
|
|
148
|
+
self.assertEqual(res.returncode, 0, res.stderr)
|
|
149
|
+
self.assertNotIn(good, res.stdout)
|
|
150
|
+
self.assertIn(good, (self.root / "demo-mq/secrets.yaml").read_text())
|
|
151
|
+
|
|
152
|
+
def test_hosts_block_is_written_idempotently_and_keeps_other_lines(self) -> None:
|
|
153
|
+
hosts = self.root / "hosts"
|
|
154
|
+
hosts.write_text("127.0.0.1 localhost\n10.0.0.5 app.example.test\n")
|
|
155
|
+
show = self.script("hosts_entry.py", "--show", "--hosts-file", str(hosts), *self.common)
|
|
156
|
+
self.assertEqual(show.returncode, 1)
|
|
157
|
+
self.assertIn("block: missing", show.stdout)
|
|
158
|
+
self.assertIn("WARNING: app.example.test is also mapped to 10.0.0.5", show.stdout)
|
|
159
|
+
res = self.script("hosts_entry.py", "--apply", "--hosts-file", str(hosts), *self.common)
|
|
160
|
+
self.assertEqual(res.returncode, 0, res.stderr)
|
|
161
|
+
text = hosts.read_text()
|
|
162
|
+
self.assertIn("127.0.0.1 localhost\n", text)
|
|
163
|
+
self.assertIn("# BEGIN demo (ops-local-env)\n127.0.0.1 app.example.test s3.example.test grafana.example.test\n# END demo\n", text)
|
|
164
|
+
self.assertEqual(self.script("hosts_entry.py", "--show", "--hosts-file", str(hosts), *self.common).returncode, 0)
|
|
165
|
+
self.assertTrue((self.root / "hosts.demo.bak").is_file())
|
|
166
|
+
|
|
167
|
+
def test_explain_config_masks_secrets_and_summarises_nesting(self) -> None:
|
|
168
|
+
sec = self.root / "demo-mq/secrets.yaml"
|
|
169
|
+
sec.write_text('mq_default_password: "do-not-show-me" # the broker password\n')
|
|
170
|
+
res = self.script("explain_config.py", str(sec), str(self.root / "demo-monitoring/config.yaml"))
|
|
171
|
+
self.assertEqual(res.returncode, 0, res.stderr)
|
|
172
|
+
self.assertNotIn("do-not-show-me", res.stdout)
|
|
173
|
+
self.assertIn("********", res.stdout)
|
|
174
|
+
self.assertIn("`targets`", res.stdout)
|
|
175
|
+
|
|
176
|
+
@unittest.skipUnless(shutil.which("git"), "needs git")
|
|
177
|
+
def test_ensure_repos_clones_missing_and_never_touches_a_non_clone(self) -> None:
|
|
178
|
+
remotes = self.root / "remotes"
|
|
179
|
+
data = json.loads(self.profile.read_text())
|
|
180
|
+
env = {**os.environ, "GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "t@example.invalid",
|
|
181
|
+
"GIT_COMMITTER_NAME": "t", "GIT_COMMITTER_EMAIL": "t@example.invalid"}
|
|
182
|
+
for repo in data["repos"]:
|
|
183
|
+
src = remotes / repo["dir"]
|
|
184
|
+
src.mkdir(parents=True)
|
|
185
|
+
subprocess.run(["git", "init", "-q", "-b", "main", str(src)], check=True, env=env)
|
|
186
|
+
subprocess.run(["git", "-C", str(src), "commit", "-q", "--allow-empty", "-m", "init"], check=True, env=env)
|
|
187
|
+
repo["url"] = str(src)
|
|
188
|
+
data["repos"] = [r for r in data["repos"] if r["dir"] in ("demo-mq", "demo-charts", "demo-new")] + [
|
|
189
|
+
{"dir": "demo-new", "url": str(remotes / "demo-charts")}]
|
|
190
|
+
self.profile.write_text(json.dumps(data))
|
|
191
|
+
res = self.script("ensure_repos.py", *self.common, "--root", str(self.root))
|
|
192
|
+
self.assertEqual(res.returncode, 1) # demo-mq / demo-charts exist, not clones
|
|
193
|
+
self.assertIn("demo-mq", res.stdout)
|
|
194
|
+
self.assertIn("BLOCKED", res.stdout)
|
|
195
|
+
self.assertIn("cloned", res.stdout)
|
|
196
|
+
self.assertTrue((self.root / "demo-new" / ".git").is_dir())
|
|
197
|
+
self.assertEqual((self.root / "demo-mq/config.yaml").read_text(), MQ)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def fake_bin(self, name: str, body: str) -> dict:
|
|
201
|
+
bindir = self.root / "bin"
|
|
202
|
+
bindir.mkdir(exist_ok=True)
|
|
203
|
+
tool = bindir / name
|
|
204
|
+
tool.write_text("#!/bin/sh\n" + body)
|
|
205
|
+
tool.chmod(0o755)
|
|
206
|
+
return {**os.environ, "PATH": f"{bindir}{os.pathsep}{os.environ['PATH']}", "PYTHONPATH": str(HERE)}
|
|
207
|
+
|
|
208
|
+
def run_env(self, env: dict, name: str, *args: str) -> subprocess.CompletedProcess:
|
|
209
|
+
return subprocess.run([sys.executable, str(HERE / name), *args], capture_output=True, text=True,
|
|
210
|
+
cwd=str(HERE), env=env, check=False)
|
|
211
|
+
|
|
212
|
+
@unittest.skipIf(os.name == "nt", "uses a POSIX shell stub")
|
|
213
|
+
def test_health_runs_read_only_checks_and_refuses_changing_ones(self) -> None:
|
|
214
|
+
env = self.fake_bin("kubectl", 'case "$*" in *postgrescluster*) echo "no such cluster" >&2; exit 1;; esac; echo "ok $*"\n')
|
|
215
|
+
res = self.run_env(env, "health.py", *self.common)
|
|
216
|
+
self.assertEqual(res.returncode, 1, res.stdout + res.stderr)
|
|
217
|
+
self.assertIn("| cluster | `kubectl get nodes` | OK |", res.stdout)
|
|
218
|
+
self.assertIn("get postgrescluster appdb` | FAIL | no such cluster", res.stdout) # {db_name} filled
|
|
219
|
+
data = json.loads(self.profile.read_text())
|
|
220
|
+
data["components"][0]["health"] = ["kubectl delete ns demo", "kubectl get pods | grep x",
|
|
221
|
+
"kubectl rollout restart deploy/x", "curl -X POST https://x"]
|
|
222
|
+
self.profile.write_text(json.dumps(data))
|
|
223
|
+
res = self.run_env(env, "health.py", "--only", "cluster", *self.common)
|
|
224
|
+
self.assertEqual(res.stdout.count("REFUSED"), 4, res.stdout)
|
|
225
|
+
|
|
226
|
+
@unittest.skipIf(os.name == "nt", "uses a POSIX shell stub")
|
|
227
|
+
def test_teardown_deletes_only_the_profile_cluster_after_confirmation(self) -> None:
|
|
228
|
+
log = self.root / "k3d.log"
|
|
229
|
+
state = self.root / "clusters.json"
|
|
230
|
+
state.write_text(json.dumps([{"name": "demo", "nodes": [{"name": "k3d-demo-server-0"}]}, {"name": "other", "nodes": []}]))
|
|
231
|
+
env = self.fake_bin("k3d", f"""echo "$*" >> {log}
|
|
232
|
+
if [ "$1 $2" = "cluster list" ]; then cat {state}; exit 0; fi
|
|
233
|
+
if [ "$1 $2" = "cluster delete" ]; then python3 -c "import json,sys; p=sys.argv[1]; d=[c for c in json.load(open(p)) if c['name']!=sys.argv[2]]; json.dump(d,open(p,'w'))" {state} "$3"; exit 0; fi
|
|
234
|
+
exit 1
|
|
235
|
+
""")
|
|
236
|
+
plan = self.run_env(env, "teardown.py", *self.common)
|
|
237
|
+
self.assertEqual(plan.returncode, 0, plan.stderr)
|
|
238
|
+
self.assertIn("would delete: k3d cluster demo", plan.stdout)
|
|
239
|
+
self.assertIn("other k3d clusters (other)", plan.stdout)
|
|
240
|
+
wrong = self.run_env(env, "teardown.py", "--apply", "--confirm", "other", *self.common)
|
|
241
|
+
self.assertEqual(wrong.returncode, 1)
|
|
242
|
+
self.assertNotIn("delete", log.read_text())
|
|
243
|
+
res = self.run_env(env, "teardown.py", "--apply", "--confirm", "demo", *self.common)
|
|
244
|
+
self.assertEqual(res.returncode, 0, res.stdout + res.stderr)
|
|
245
|
+
self.assertIn("cluster delete demo", log.read_text())
|
|
246
|
+
self.assertEqual([c["name"] for c in json.loads(state.read_text())], ["other"])
|
|
247
|
+
|
|
248
|
+
def test_hosts_block_can_be_removed(self) -> None:
|
|
249
|
+
hosts = self.root / "hosts"
|
|
250
|
+
hosts.write_text("127.0.0.1 localhost\n")
|
|
251
|
+
self.script("hosts_entry.py", "--apply", "--hosts-file", str(hosts), *self.common)
|
|
252
|
+
res = self.script("hosts_entry.py", "--remove", "--hosts-file", str(hosts), *self.common)
|
|
253
|
+
self.assertEqual(res.returncode, 0, res.stderr)
|
|
254
|
+
self.assertEqual(hosts.read_text(), "127.0.0.1 localhost\n")
|
|
255
|
+
|
|
256
|
+
@unittest.skipUnless(shutil.which("git"), "needs git")
|
|
257
|
+
def test_init_profile_drafts_a_profile_from_the_workspace(self) -> None:
|
|
258
|
+
self.profile.unlink()
|
|
259
|
+
(self.root / "demo-monitoring/secrets.yaml").write_text('grafana_admin_password: "x-secret-value-1"\n')
|
|
260
|
+
(self.root / "demo-db/.claude/skills/install-demo-db").mkdir(parents=True)
|
|
261
|
+
(self.root / "demo-infra/k8s/launch.py").write_text("")
|
|
262
|
+
for d in ("demo-infra", "demo-storage", "demo-db", "demo-mq", "demo-monitoring", "demo-charts"):
|
|
263
|
+
subprocess.run(["git", "init", "-q", str(self.root / d)], check=True)
|
|
264
|
+
subprocess.run(["git", "-C", str(self.root / d), "remote", "add", "origin", f"git@example.invalid:org/{d}.git"], check=True)
|
|
265
|
+
res = self.script("init_profile.py", "--root", str(self.root), "--write")
|
|
266
|
+
self.assertEqual(res.returncode, 0, res.stdout + res.stderr)
|
|
267
|
+
draft = json.loads((self.root / "demo-infra" / "ops-env.profile.json").read_text())
|
|
268
|
+
self.assertEqual(draft["name"], "demo")
|
|
269
|
+
self.assertEqual(draft["primary_config"], "infra-k8s")
|
|
270
|
+
self.assertIn("db_name", draft["shared_keys"])
|
|
271
|
+
self.assertEqual(draft["repos"][0]["https"], "https://example.invalid/org/demo-charts.git")
|
|
272
|
+
self.assertEqual(draft["components"][0]["run"], "cd demo-infra/k8s && ./launch.py")
|
|
273
|
+
self.assertIn({"id": "db", "repo": "demo-db", "skill": "install-demo-db", "configs": ["db"],
|
|
274
|
+
"after": ["infra"], "health": []}, draft["components"])
|
|
275
|
+
self.assertIn({"file": "demo-monitoring/secrets.yaml", "key": "grafana_admin_password"}, draft["admin"]["passwords"])
|
|
276
|
+
self.assertNotIn("x-secret-value-1", res.stdout + json.dumps(draft))
|
|
277
|
+
self.assertTrue(any("apps_namespace differs" in t for t in draft["_todo"]))
|
|
278
|
+
again = self.script("init_profile.py", "--root", str(self.root), "--write")
|
|
279
|
+
self.assertEqual(again.returncode, 1) # never overwrites without --force
|
|
280
|
+
plan = self.script("stack_plan.py", "--check", "--profile", str(self.root / "demo-infra" / "ops-env.profile.json"))
|
|
281
|
+
self.assertEqual(plan.returncode, 0, plan.stdout)
|
|
282
|
+
self.assertIn("to review (_todo)", plan.stdout)
|
|
283
|
+
|
|
284
|
+
@unittest.skipIf(os.name == "nt", "uses POSIX shell stubs")
|
|
285
|
+
def test_cilium_heal_check_classifies_nodes(self) -> None:
|
|
286
|
+
nodes = {"items": [{"metadata": {"name": "k3d-demo-server-0"}, "status": {"addresses": [{"type": "InternalIP", "address": "172.18.0.2"}]}},
|
|
287
|
+
{"metadata": {"name": "k3d-demo-agent-0"}, "status": {"addresses": [{"type": "InternalIP", "address": "172.18.0.3"}]}}]}
|
|
288
|
+
cnodes = {"items": [{"metadata": {"name": "k3d-demo-server-0"}, "spec": {"addresses": [{"type": "InternalIP", "ip": "172.18.0.2"}]}},
|
|
289
|
+
{"metadata": {"name": "k3d-demo-agent-0"}, "spec": {"addresses": [{"type": "InternalIP", "ip": "172.18.0.9"}]}}]}
|
|
290
|
+
(self.root / "nodes.json").write_text(json.dumps(nodes))
|
|
291
|
+
(self.root / "cnodes.json").write_text(json.dumps(cnodes))
|
|
292
|
+
self.fake_bin("docker", """case "$1" in
|
|
293
|
+
ps) printf 'k3d-demo-server-0\trunning\nk3d-demo-agent-0\trunning\nk3d-other-server-0\trunning\n';;
|
|
294
|
+
inspect) case "$4" in *server*) echo 172.18.0.2;; *) echo 172.18.0.3;; esac;;
|
|
295
|
+
exec) exit 0;;
|
|
296
|
+
esac
|
|
297
|
+
""")
|
|
298
|
+
env = self.fake_bin("kubectl", f"""case "$*" in *ciliumnodes*) cat {self.root}/cnodes.json;; *nodes*) cat {self.root}/nodes.json;; *) exit 1;; esac
|
|
299
|
+
""")
|
|
300
|
+
res = self.run_env(env, "cilium_heal.py", "--cluster", "demo", "--check")
|
|
301
|
+
self.assertEqual(res.returncode, 1, res.stdout + res.stderr)
|
|
302
|
+
self.assertRegex(res.stdout, r"k3d-demo-agent-0 .*172\.18\.0\.9 .* STALE")
|
|
303
|
+
self.assertRegex(res.stdout, r"k3d-demo-server-0 .* ok")
|
|
304
|
+
self.assertNotIn("k3d-other", res.stdout)
|
|
305
|
+
|
|
306
|
+
if __name__ == "__main__":
|
|
307
|
+
unittest.main()
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""trust_ca.py - trust the stack's local CA in the operating system's trust store (macOS, Linux,
|
|
3
|
+
Windows). The CA and the leaf it signed come from the profile's `ca` block:
|
|
4
|
+
|
|
5
|
+
"ca": {"dir": "infra-config/k8s/certs/{base_domain}", "ca": "ca.crt", "leaf": "tls.crt",
|
|
6
|
+
"site_key": "site_domain"}
|
|
7
|
+
|
|
8
|
+
trust_ca.py --show # trusted? (exit 0 when trusted and name-constrained, 1 otherwise)
|
|
9
|
+
trust_ca.py --apply # add it (needs root / Administrator)
|
|
10
|
+
|
|
11
|
+
macOS System keychain, trustRoot security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain
|
|
12
|
+
Linux Debian/Ubuntu /usr/local/share/ca-certificates/ + update-ca-certificates
|
|
13
|
+
Fedora/RHEL/SUSE /etc/pki/ca-trust/source/anchors/ + update-ca-trust extract
|
|
14
|
+
others with p11-kit trust anchor --store
|
|
15
|
+
Windows LocalMachine Root store certutil -addstore -f Root
|
|
16
|
+
|
|
17
|
+
"Trusted" is checked the way a client would: the leaf must verify against the system store. A CA
|
|
18
|
+
without name constraints could sign any site, so --apply refuses it. Browsers with their own store
|
|
19
|
+
(Firefox) and runtimes with their own bundle (Java, Node) are not covered. Standard library only.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import argparse
|
|
25
|
+
import base64
|
|
26
|
+
import hashlib
|
|
27
|
+
import platform
|
|
28
|
+
import re
|
|
29
|
+
import shutil
|
|
30
|
+
import subprocess
|
|
31
|
+
import sys
|
|
32
|
+
from pathlib import Path
|
|
33
|
+
|
|
34
|
+
from envprofile import Stack, add_common_args
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def thumbprint(pem: Path) -> str:
|
|
38
|
+
body = re.search(r"-----BEGIN CERTIFICATE-----(.+?)-----END CERTIFICATE-----", pem.read_text(), re.S)
|
|
39
|
+
if not body:
|
|
40
|
+
sys.exit(f"error: {pem} is not a PEM certificate")
|
|
41
|
+
return hashlib.sha1(base64.b64decode("".join(body.group(1).split()))).hexdigest().upper()
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def run(cmd: list[str]) -> subprocess.CompletedProcess:
|
|
45
|
+
return subprocess.run(cmd, capture_output=True, text=True, check=False)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def name_constrained(ca: Path) -> bool | None:
|
|
49
|
+
if not shutil.which("openssl"):
|
|
50
|
+
return None
|
|
51
|
+
return "Name Constraints" in run(["openssl", "x509", "-in", str(ca), "-noout", "-text"]).stdout
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def linux_flavour() -> tuple[str, Path | None]:
|
|
55
|
+
if shutil.which("update-ca-certificates") and Path("/usr/local/share/ca-certificates").is_dir():
|
|
56
|
+
return "debian", Path("/usr/local/share/ca-certificates")
|
|
57
|
+
if shutil.which("update-ca-trust") and Path("/etc/pki/ca-trust/source/anchors").is_dir():
|
|
58
|
+
return "redhat", Path("/etc/pki/ca-trust/source/anchors")
|
|
59
|
+
if shutil.which("trust"):
|
|
60
|
+
return "p11-kit", None
|
|
61
|
+
return "unknown", None
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def linux_bundle() -> str | None:
|
|
65
|
+
for b in ("/etc/ssl/certs/ca-certificates.crt", "/etc/pki/tls/certs/ca-bundle.crt",
|
|
66
|
+
"/etc/ssl/ca-bundle.pem", "/etc/ssl/cert.pem"):
|
|
67
|
+
if Path(b).is_file():
|
|
68
|
+
return b
|
|
69
|
+
return None
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def is_trusted(ca: Path, leaf: Path, site: str) -> bool:
|
|
73
|
+
system = platform.system()
|
|
74
|
+
if system == "Darwin":
|
|
75
|
+
return run(["security", "verify-cert", "-c", str(leaf), "-p", "ssl", "-s", site]).returncode == 0
|
|
76
|
+
if system == "Linux":
|
|
77
|
+
bundle = linux_bundle()
|
|
78
|
+
return bool(bundle) and run(["openssl", "verify", "-CAfile", bundle, str(leaf)]).returncode == 0
|
|
79
|
+
if system == "Windows":
|
|
80
|
+
ps = f"if (Get-ChildItem Cert:\\LocalMachine\\Root | Where-Object Thumbprint -eq '{thumbprint(ca)}') {{ exit 0 }} else {{ exit 1 }}"
|
|
81
|
+
return run(["powershell", "-NoProfile", "-Command", ps]).returncode == 0
|
|
82
|
+
sys.exit(f"error: unsupported system {system}")
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def add(ca: Path, name: str) -> subprocess.CompletedProcess:
|
|
86
|
+
system = platform.system()
|
|
87
|
+
if system == "Darwin":
|
|
88
|
+
return run(["security", "add-trusted-cert", "-d", "-r", "trustRoot", "-k", "/Library/Keychains/System.keychain", str(ca)])
|
|
89
|
+
if system == "Windows":
|
|
90
|
+
return run(["certutil", "-addstore", "-f", "Root", str(ca)])
|
|
91
|
+
flavour, anchors = linux_flavour()
|
|
92
|
+
if flavour in ("debian", "redhat"):
|
|
93
|
+
shutil.copyfile(ca, anchors / f"{name}.crt")
|
|
94
|
+
return run(["update-ca-certificates"] if flavour == "debian" else ["update-ca-trust", "extract"])
|
|
95
|
+
if flavour == "p11-kit":
|
|
96
|
+
return run(["trust", "anchor", "--store", str(ca)])
|
|
97
|
+
sys.exit("error: no known trust-store tool (update-ca-certificates, update-ca-trust, trust) on this Linux")
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def main() -> int:
|
|
101
|
+
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
102
|
+
add_common_args(parser)
|
|
103
|
+
mode = parser.add_mutually_exclusive_group(required=True)
|
|
104
|
+
mode.add_argument("--show", action="store_true")
|
|
105
|
+
mode.add_argument("--apply", action="store_true")
|
|
106
|
+
args = parser.parse_args()
|
|
107
|
+
|
|
108
|
+
stack = Stack(args.profile, args.root)
|
|
109
|
+
conf = stack.get("ca") or {}
|
|
110
|
+
if not conf.get("dir"):
|
|
111
|
+
sys.exit("error: the profile has no ca.dir")
|
|
112
|
+
certs = stack.path(stack.expand(conf["dir"]))
|
|
113
|
+
ca, leaf = certs / conf.get("ca", "ca.crt"), certs / conf.get("leaf", "tls.crt")
|
|
114
|
+
for f in (ca, leaf):
|
|
115
|
+
if not f.is_file():
|
|
116
|
+
sys.exit(f"error: {f} not found (the cluster bootstrap step creates it)")
|
|
117
|
+
site = stack.primary_value(conf.get("site_key", "site_domain")) or ""
|
|
118
|
+
base = stack.primary_value(stack.get("base_domain_key") or "base_domain") or "local"
|
|
119
|
+
name = f"{stack.name}-{base}-ca"
|
|
120
|
+
print(f"CA: {ca} (SHA-1 {thumbprint(ca)}), system: {platform.system()}")
|
|
121
|
+
elevated = (f'(elevated PowerShell) python "{Path(__file__).resolve()}"' if platform.system() == "Windows"
|
|
122
|
+
else f'sudo python3 "{Path(__file__).resolve()}"') + f' --profile "{stack.profile_path}" --root "{stack.root}" --apply'
|
|
123
|
+
|
|
124
|
+
constrained = name_constrained(ca)
|
|
125
|
+
if constrained is False:
|
|
126
|
+
print(f"name constraints: NONE -- this CA can sign any site; re-issue it (move {certs} out, re-run the bootstrap)")
|
|
127
|
+
elif constrained:
|
|
128
|
+
print("name constraints: yes")
|
|
129
|
+
trusted = is_trusted(ca, leaf, site)
|
|
130
|
+
if args.show:
|
|
131
|
+
print("trusted: yes" if trusted else f"trusted: no\nto trust it, run:\n {elevated}")
|
|
132
|
+
return 0 if trusted and constrained is not False else 1
|
|
133
|
+
if trusted:
|
|
134
|
+
print("trusted: yes; nothing to do")
|
|
135
|
+
return 0
|
|
136
|
+
if constrained is False:
|
|
137
|
+
print("error: refusing to trust a CA without name constraints", file=sys.stderr)
|
|
138
|
+
return 1
|
|
139
|
+
try:
|
|
140
|
+
result = add(ca, name)
|
|
141
|
+
except PermissionError:
|
|
142
|
+
result = None
|
|
143
|
+
if result is None or result.returncode != 0:
|
|
144
|
+
detail = (result.stderr or result.stdout).strip() if result else "permission denied"
|
|
145
|
+
print(f"error: could not add the CA: {detail}\nrun it elevated:\n {elevated}", file=sys.stderr)
|
|
146
|
+
return 1
|
|
147
|
+
ok = is_trusted(ca, leaf, site)
|
|
148
|
+
print("trusted: yes (added)" if ok else "added, but the leaf still does not verify -- check the store by hand")
|
|
149
|
+
return 0 if ok else 1
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
if __name__ == "__main__":
|
|
153
|
+
sys.exit(main())
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sdtk-ops-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Skill-driven operations toolkit for deployment, verification, monitoring, incident response, backup, security, compliance, and cost discipline.",
|
|
5
5
|
"bin": {
|
|
6
6
|
"sdtk-ops": "bin/sdtk-ops.js"
|