diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..035aaf0 --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,40 @@ +name: infra-ci + +on: + push: + branches: + - main + - "feat/**" + pull_request: + +jobs: + manifests: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install validation dependency + run: | + python3 -m pip install --user pyyaml \ + || python3 -m pip install --break-system-packages pyyaml + + # No kubeconfig and no cluster mutation here. This is the same guardrail + # set used locally: namespace-only kinds, numeric UIDs, securityContext, + # probes, resources, env/Secret references, image tags, Service types. + - name: Static manifest guardrails + run: make gate1-check STATIC_ONLY=1 + + # Falls back to YAML parsing when kubectl or a cluster is unavailable. + - name: YAML parse or client dry-run + run: make validate + + # The brief's client dry-run, only when this runner actually has a + # reachable kubeconfig. No Secrets are configured in CI. + - name: kubectl client dry-run when a cluster is configured + run: | + if kubectl get --raw=/readyz >/dev/null 2>&1; then + kubectl apply --dry-run=client -f deploy/k8s + else + echo "No reachable kubeconfig on this runner; static guardrails already passed." + fi diff --git a/.gitignore b/.gitignore index 6de1550..0929105 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,10 @@ k6/xk6 k6/results/* !k6/results/.gitkeep +# Python bytecode from the manifest checker / CI +__pycache__/ +*.py[cod] + # Editor / OS noise .DS_Store *.swp diff --git a/Makefile b/Makefile index c1811c8..63d62b2 100644 --- a/Makefile +++ b/Makefile @@ -67,8 +67,9 @@ validate-kubeconform: ## Validate schemas with kubeconform (must be installed) @command -v "$(KUBECONFORM)" >/dev/null 2>&1 || { echo "kubeconform not found; go install github.com/yannh/kubeconform/cmd/kubeconform@latest"; exit 1; } "$(KUBECONFORM)" -strict -summary -ignore-missing-schemas "$(ROOT)/deploy/k8s" -gate1-check: ## Read-only Gate 1 readiness: permissions, guardrails, dry-run, Secrets (never mutates) +gate1-check: ## Read-only Gate 1 readiness (STATIC_ONLY=1 for cluster-free CI) @NS="$(NAMESPACE)" CONTEXT="$(CONTEXT)" REGISTRY="$(REGISTRY)" OWNER="$(OWNER)" TAG="$(TAG)" \ + STATIC_ONLY="$(STATIC_ONLY)" \ "$(ROOT)/scripts/gate1-check.sh" build: build-web build-api build-ml ## Build all three images locally diff --git a/README.md b/README.md index ace108a..4bcb512 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ deploy/k8s/ web, api, ml + cloudflared Deployments and Services, Confi deploy/examples/ example Secret shapes (placeholders only, never applied) scripts/smoke.sh public + in-cluster smoke checks scripts/gate1-check.sh read-only permissions/manifest/secret readiness check +scripts/check_manifests.py cluster-free manifest guardrails used by CI +.gitea/workflows/ci.yml manifest validation; no secrets, no cluster writes k6/sse.js 120 concurrent SSE clients for 5 minutes docs/deploy.md Gate 1 runbook and exact human checklist docs/load-test.md load-test procedure and how to record results @@ -50,6 +52,13 @@ hostname, and verification steps. - Still human-only: Gitea read-only token + hello image pull, `gitea-regcred`, Cloudflare Tunnel + DNS hostnames, and the three Secrets. +## CI + +`.gitea/workflows/ci.yml` runs `make gate1-check STATIC_ONLY=1` and +`make validate`. It uses no Secrets, pushes no images, and never mutates a +cluster. If the Gitea runner is unavailable, run the same two commands +locally; `STATIC_ONLY=1` needs no kubeconfig. + ## Git workflow `main` is integration. New work happens on a `feat/` branch in its own @@ -65,8 +74,8 @@ committed directly to `main`. - No secrets, tokens, kubeconfig, or raw CSV in git. - The human applies cluster mutations. `make deploy` refuses to run without `CONFIRM=yes`. -- Every pod sets an explicit numeric `runAsUser`/`runAsGroup`: web/ml - `10001:10001`, api/cloudflared `65532:65532`. SKE rejects a non-numeric image - user under `runAsNonRoot`. +- Every pod sets explicit numeric `runAsUser`/`runAsGroup`/`fsGroup`: web/ml + `10001`, api/cloudflared `65532`. SKE rejects a non-numeric image user under + `runAsNonRoot`. - The tunnel uses HTTP/2 (`--protocol http2`) because TCP/443 to Cloudflare is verified and UDP/7844 is not. diff --git a/deploy/k8s/10-ml.yaml b/deploy/k8s/10-ml.yaml index 8e4d898..ee21180 100644 --- a/deploy/k8s/10-ml.yaml +++ b/deploy/k8s/10-ml.yaml @@ -32,6 +32,7 @@ spec: # (verified on SKE with curlimages/curl, 2026-11-14). runAsUser: 10001 runAsGroup: 10001 + fsGroup: 10001 containers: - name: ml image: git.noelsabia.dev/noelsabia/pulsecast-ml:gate1 diff --git a/deploy/k8s/20-api.yaml b/deploy/k8s/20-api.yaml index 2cf3cf8..fdafb5a 100644 --- a/deploy/k8s/20-api.yaml +++ b/deploy/k8s/20-api.yaml @@ -33,6 +33,7 @@ spec: # runAsNonRoot never depends on the image's user name. runAsUser: 65532 runAsGroup: 65532 + fsGroup: 65532 containers: - name: api image: git.noelsabia.dev/noelsabia/pulsecast-api:gate1 diff --git a/deploy/k8s/30-web.yaml b/deploy/k8s/30-web.yaml index 4871fbe..e5f82c5 100644 --- a/deploy/k8s/30-web.yaml +++ b/deploy/k8s/30-web.yaml @@ -35,6 +35,7 @@ spec: # 10001:10001. Do not rely on a named image user. runAsUser: 10001 runAsGroup: 10001 + fsGroup: 10001 containers: - name: web # Replace owner/package only if your Gitea layout differs. Keep the tag in sync with `make build`. diff --git a/deploy/k8s/40-cloudflared.yaml b/deploy/k8s/40-cloudflared.yaml index 0885257..53e8fbf 100644 --- a/deploy/k8s/40-cloudflared.yaml +++ b/deploy/k8s/40-cloudflared.yaml @@ -34,6 +34,7 @@ spec: # cloudflared's image user is 65532:65532; set it numerically. runAsUser: 65532 runAsGroup: 65532 + fsGroup: 65532 containers: - name: cloudflared # Public Docker Hub image; no Gitea pull secret needed for this pod. diff --git a/docs/deploy.md b/docs/deploy.md index 1cc362c..eb1e1ff 100644 --- a/docs/deploy.md +++ b/docs/deploy.md @@ -43,7 +43,7 @@ Findings from `CLUSTER-SMOKE.md`; re-check before trusting this snapshot: unverified UDP. - **Numeric UIDs are required:** a non-numeric image user breaks `runAsNonRoot` on SKE (reproduced with `curlimages/curl`). Every pod in - `deploy/k8s/` sets an explicit numeric `runAsUser`/`runAsGroup`. + `deploy/k8s/` sets explicit numeric `runAsUser`/`runAsGroup`/`fsGroup`. - **Repo status (2026-11-14 ~05:10 UTC):** all three images now build and pass local manifest-equivalent runtime checks (numeric UID, read-only root, `/tmp` tmpfs). There is no remaining Dockerfile blocker; the human Gitea/Tunnel @@ -167,7 +167,13 @@ want exit 0 while prerequisites are still being prepared. Neither target mutates the cluster. `make validate` uses `kubectl apply --dry-run=client`; `make gate1-check` only reads -configuration/RBAC/secrets and runs the same dry-run. +configuration/RBAC/secrets and runs the same dry-run. CI runs the same static +guardrails without a cluster: + +```bash +make gate1-check STATIC_ONLY=1 +make validate +``` ## Step 1 — Gitea credentials @@ -475,11 +481,11 @@ Each service image must: | api | `git.noelsabia.dev/noelsabia/pulsecast-api:gate1` | 8080 | `65532:65532` | `/api/healthz`, `/api/readyz` | | ml | `git.noelsabia.dev/noelsabia/pulsecast-ml:gate1` | 8000 | `10001:10001` | `/healthz`, `/readyz` | -The manifests set `runAsUser`/`runAsGroup` explicitly, so the image's named -user is irrelevant; the files must be readable by that numeric UID. `api` -uses distroless `static:nonroot` (65532); web and ml use 10001. If a Dockerfile -genuinely needs another UID, change the manifest and this table together, and -keep the value numeric. +The manifests set `runAsUser`/`runAsGroup`/`fsGroup` explicitly, so the image's +named user is irrelevant; the files must be readable by that numeric UID. +`api` uses distroless `static:nonroot` (65532); web and ml use 10001. If a +Dockerfile genuinely needs another UID, change the manifest and this table +together, and keep the value numeric. `api` must read `DATA_DIR` (baked JSON), `FASTAPI_URL`, `PORT`, `CONTROL_USER`, `CONTROL_PASS`, `CONTROL_TOKEN`, `SSE_HEARTBEAT`, and diff --git a/scripts/check_manifests.py b/scripts/check_manifests.py new file mode 100755 index 0000000..61314bf --- /dev/null +++ b/scripts/check_manifests.py @@ -0,0 +1,259 @@ +#!/usr/bin/env python3 +"""Static, cluster-free guardrails for deploy/k8s. + +Usage: check_manifests.py +Exit 0 when the manifests satisfy the namespace-only and pod-security rules. +""" +import glob +import os +import re +import sys + +root, ns, tag, registry, owner = sys.argv[1:6] +errors = [] +warns = [] +docs = [] + +for path in sorted(glob.glob(os.path.join(root, "*.yaml"))): + with open(path, encoding="utf-8") as handle: + try: + loaded = [d for d in __import__("yaml").safe_load_all(handle) if d] + except Exception as exc: # noqa: BLE001 - report the parse error verbatim + errors.append(f"{os.path.basename(path)}: YAML parse error: {exc}") + continue + if not loaded: + errors.append(f"{os.path.basename(path)}: no YAML documents") + for doc in loaded: + doc["__file__"] = os.path.basename(path) + docs.append(doc) + +if not docs: + errors.append("deploy/k8s contains no manifests") + +allowed = {"ConfigMap", "Deployment", "Service"} +forbidden = { + "Ingress", "Job", "CronJob", "NetworkPolicy", "Role", "RoleBinding", + "ClusterRole", "ClusterRoleBinding", "CustomResourceDefinition", + "Namespace", "PersistentVolume", "PersistentVolumeClaim", "StorageClass", + "PodSecurityPolicy", "PriorityClass", "APIService", + "MutatingWebhookConfiguration", "ValidatingWebhookConfiguration", +} + +objects = {} +configmaps = {} +services = {} +deployments = {} + +for doc in docs: + where = f"{doc['__file__']}:{doc.get('kind', '?')}/{doc.get('metadata', {}).get('name', '?')}" + kind = doc.get("kind") + meta = doc.get("metadata") or {} + name = meta.get("name") + if not doc.get("apiVersion") or not kind or not name: + errors.append(f"{where}: missing apiVersion, kind, or metadata.name") + continue + if kind not in allowed: + errors.append(f"{where}: kind {kind} is not allowed (namespace-only: ConfigMap, Deployment, Service)") + continue + if kind in forbidden: + errors.append(f"{where}: forbidden kind {kind}") + if meta.get("namespace") != ns: + errors.append(f"{where}: metadata.namespace is {meta.get('namespace')!r}, expected {ns!r}") + key = (kind, name) + if key in objects: + errors.append(f"{where}: duplicate object") + objects[key] = doc + if kind == "ConfigMap": + configmaps[name] = doc + elif kind == "Service": + services[name] = doc + elif kind == "Deployment": + deployments[name] = doc + +for name, cm in configmaps.items(): + data = cm.get("data") or {} + for key in data: + if re.search(r"(?i)(token|password|secret|private[_-]?key|credential)", key): + errors.append(f"ConfigMap/{name}: key {key!r} looks like a secret; use a Secret created by the human") + +required_config = { + "pulsecast-shared": {"PUBLIC_WEB_URL", "PUBLIC_API_URL", "API_INTERNAL_URL", "ML_INTERNAL_URL"}, + "web-config": {"PORT", "NUXT_PUBLIC_API_BASE", "NUXT_API_INTERNAL_BASE"}, + "api-config": {"PORT", "DATA_DIR", "FASTAPI_URL", "ML_TIMEOUT", "SSE_HEARTBEAT"}, + "ml-config": { + "PORT", "PULSECAST_ROOT", "PULSECAST_ARTIFACTS", "PULSECAST_WEATHER", + "PULSECAST_MODELS", "PULSECAST_UPLOADS", + }, +} +for name, keys in required_config.items(): + cm = configmaps.get(name) + if cm is None: + errors.append(f"missing ConfigMap/{name}") + continue + have = set((cm.get("data") or {}).keys()) + for key in sorted(keys - have): + errors.append(f"ConfigMap/{name}: missing key {key}") + +expected_ports = {"web": 3000, "api": 8080, "ml": 8000, "cloudflared": 2000} +expected_uids = {"web": 10001, "api": 65532, "ml": 10001, "cloudflared": 65532} +expected_images = { + "web": f"{registry}/{owner}/pulsecast-web:{tag}", + "api": f"{registry}/{owner}/pulsecast-api:{tag}", + "ml": f"{registry}/{owner}/pulsecast-ml:{tag}", +} +gitea_images = {"web", "api", "ml"} + +for name in ("web", "api", "ml", "cloudflared"): + dep = deployments.get(name) + if dep is None: + errors.append(f"missing Deployment/{name}") + continue + spec = dep.get("spec") or {} + if spec.get("replicas") != 1: + errors.append(f"Deployment/{name}: replicas should be 1") + template = (spec.get("template") or {}) + pod = template.get("spec") or {} + if pod.get("automountServiceAccountToken") is not False: + errors.append(f"Deployment/{name}: automountServiceAccountToken must be false") + pod_security = pod.get("securityContext") or {} + if (pod_security.get("seccompProfile") or {}).get("type") != "RuntimeDefault": + errors.append(f"Deployment/{name}: seccompProfile must be RuntimeDefault") + for field in ("runAsUser", "runAsGroup", "fsGroup"): + value = pod_security.get(field) + if not isinstance(value, int) or value <= 0: + errors.append(f"Deployment/{name}: {field} must be a positive numeric id (SKE runAsNonRoot caveat)") + elif value != expected_uids[name]: + errors.append(f"Deployment/{name}: {field} is {value}, expected {expected_uids[name]}") + if pod_security.get("runAsNonRoot") is True and pod_security.get("runAsUser") == 0: + errors.append(f"Deployment/{name}: runAsUser 0 conflicts with runAsNonRoot") + for field in ("hostNetwork", "hostPID", "hostIPC"): + if pod.get(field): + errors.append(f"Deployment/{name}: {field} is not allowed") + if name in gitea_images: + pulls = pod.get("imagePullSecrets") or [] + if not any(p.get("name") == "gitea-regcred" for p in pulls): + errors.append(f"Deployment/{name}: imagePullSecrets must include gitea-regcred") + containers = pod.get("containers") or [] + if len(containers) != 1: + errors.append(f"Deployment/{name}: expected exactly one container") + continue + container = containers[0] + if container.get("name") != name: + errors.append(f"Deployment/{name}: container name is {container.get('name')!r}") + image = container.get("image") or "" + if image.endswith(":latest"): + errors.append(f"Deployment/{name}: image tag must be pinned, not :latest") + if name in gitea_images and not image.startswith(f"{registry}/{owner}/"): + errors.append(f"Deployment/{name}: image {image!r} is not under {registry}/{owner}/") + if name in expected_images and image != expected_images[name]: + warns.append(f"Deployment/{name}: image {image!r} does not match TAG={tag}") + if name == "cloudflared" and not image.startswith("cloudflare/cloudflared:"): + errors.append(f"Deployment/{name}: unexpected image {image!r}") + if name in expected_ports: + ports = container.get("ports") or [] + if not any(int(p.get("containerPort", -1)) == expected_ports[name] for p in ports): + errors.append(f"Deployment/{name}: containerPort {expected_ports[name]} not found") + for port in ports: + if port.get("hostPort"): + errors.append(f"Deployment/{name}: hostPort is not allowed") + security = container.get("securityContext") or {} + if security.get("runAsNonRoot") is not True: + errors.append(f"Deployment/{name}: runAsNonRoot must be true") + if security.get("allowPrivilegeEscalation") is not False: + errors.append(f"Deployment/{name}: allowPrivilegeEscalation must be false") + if security.get("readOnlyRootFilesystem") is not True: + errors.append(f"Deployment/{name}: readOnlyRootFilesystem must be true") + if security.get("privileged") is True: + errors.append(f"Deployment/{name}: privileged is not allowed") + if security.get("runAsUser") == 0: + errors.append(f"Deployment/{name}: runAsUser 0 is not allowed") + drops = ((security.get("capabilities") or {}).get("drop") or []) + if "ALL" not in drops: + errors.append(f"Deployment/{name}: capabilities.drop must include ALL") + resources = container.get("resources") or {} + requests = resources.get("requests") or {} + limits = resources.get("limits") or {} + for group_name, group in (("requests", requests), ("limits", limits)): + for key in ("cpu", "memory"): + if not group.get(key): + errors.append(f"Deployment/{name}: resources.{group_name}.{key} is missing") + mounts = container.get("volumeMounts") or [] + tmp_mounts = [m for m in mounts if m.get("mountPath") == "/tmp"] + if not tmp_mounts: + errors.append(f"Deployment/{name}: /tmp volumeMount is missing") + else: + volume_name = tmp_mounts[0].get("name") + volumes = pod.get("volumes") or [] + if not any(v.get("name") == volume_name and "emptyDir" in v for v in volumes): + errors.append(f"Deployment/{name}: /tmp must be backed by an emptyDir") + for probe in ("startupProbe", "livenessProbe", "readinessProbe"): + probe_spec = container.get(probe) or {} + http = probe_spec.get("httpGet") or {} + if not http.get("path") or not http.get("port"): + errors.append(f"Deployment/{name}: {probe}.httpGet is missing") + refs = [] + for source in container.get("envFrom") or []: + if source.get("configMapRef"): + refs.append(source["configMapRef"].get("name")) + for env in container.get("env") or []: + if env.get("valueFrom", {}).get("configMapKeyRef"): + refs.append(env["valueFrom"]["configMapKeyRef"].get("name")) + for ref in refs: + if ref not in configmaps: + errors.append(f"Deployment/{name}: references missing ConfigMap/{ref}") + expected_secret_refs = { + "api": { + ("control-auth", "CONTROL_USER"), + ("control-auth", "CONTROL_PASS"), + ("control-auth", "CONTROL_TOKEN"), + }, + "ml": {("control-auth", "CONTROL_TOKEN")}, + "cloudflared": {("cloudflared", "TUNNEL_TOKEN")}, + } + seen_secret_refs = set() + for env in container.get("env") or []: + secret_ref = env.get("valueFrom", {}).get("secretKeyRef") + if not secret_ref: + continue + secret_name = secret_ref.get("name") + secret_key = secret_ref.get("key") + if secret_name not in ("gitea-regcred", "cloudflared", "control-auth"): + errors.append(f"Deployment/{name}: unexpected Secret reference {secret_name!r}") + seen_secret_refs.add((secret_name, secret_key)) + for required in expected_secret_refs.get(name, set()): + if required not in seen_secret_refs: + errors.append(f"Deployment/{name}: missing Secret reference {required[0]}/{required[1]}") + +if "cloudflared" in deployments: + args = ((deployments["cloudflared"].get("spec") or {}).get("template") or {}).get("spec", {}).get("containers", [{}])[0].get("args") or [] + for required in ("tunnel", "run", "--token", "$(TUNNEL_TOKEN)", "--no-autoupdate", "--protocol", "http2"): + if required not in args: + errors.append(f"Deployment/cloudflared: args must include {required!r}") + +for name in ("web", "api", "ml"): + svc = services.get(name) + if svc is None: + errors.append(f"missing Service/{name}") + continue + spec = svc.get("spec") or {} + if spec.get("type") != "ClusterIP": + errors.append(f"Service/{name}: type must be ClusterIP") + for field in ("externalIPs", "loadBalancerIP", "loadBalancerSourceRanges"): + if spec.get(field): + errors.append(f"Service/{name}: {field} is not allowed") + ports = spec.get("ports") or [] + if not any(int(p.get("port", -1)) == expected_ports[name] for p in ports): + errors.append(f"Service/{name}: port {expected_ports[name]} not found") + if any(p.get("nodePort") for p in ports): + errors.append(f"Service/{name}: nodePort is not allowed") + +kinds = {} +for kind, _ in objects: + kinds[kind] = kinds.get(kind, 0) + 1 +summary = ", ".join(f"{kind}={count}" for kind, count in sorted(kinds.items())) +print(f"parsed {len(objects)} objects ({summary})") +for message in warns: + print(f"WARN: {message}") +for message in errors: + print(f"FAIL: {message}") +sys.exit(1 if errors else 0) diff --git a/scripts/gate1-check.sh b/scripts/gate1-check.sh index 9f444fe..c544870 100755 --- a/scripts/gate1-check.sh +++ b/scripts/gate1-check.sh @@ -9,6 +9,7 @@ # make gate1-check # make gate1-check CHECK_PUBLIC=1 # also curl the public URLs # make gate1-check ALLOW_PENDING=1 # exit 0 while human secrets are pending +# make gate1-check STATIC_ONLY=1 # CI: no kubeconfig/cluster required # # Exit codes: 0 ready, 1 hard failure (permissions/manifests/cluster), 2 only # human prerequisites are missing (Secrets, Deployments, public URLs). @@ -22,6 +23,7 @@ OWNER="${OWNER:-noelsabia}" TAG="${TAG:-gate1}" CHECK_PUBLIC="${CHECK_PUBLIC:-0}" ALLOW_PENDING="${ALLOW_PENDING:-0}" +STATIC_ONLY="${STATIC_ONLY:-0}" WEB_URL="${WEB_URL:-https://pulse.noelsabia.dev}" API_URL="${API_URL:-https://api.pulse.noelsabia.dev}" CURL_MAX_TIME="${CURL_MAX_TIME:-15}" @@ -36,6 +38,18 @@ missing() { printf '[pending] %s\n' "$*"; pending=$((pending + 1)); } warn() { printf '[warn] %s\n' "$*"; warnings=$((warnings + 1)); } section() { printf '\n== %s\n' "$*"; } +run_static_checks() { + local manifest_out + if manifest_out="$(python3 \ + "$ROOT/scripts/check_manifests.py" \ + "$ROOT/deploy/k8s" "$NS" "$TAG" "$REGISTRY" "$OWNER" 2>&1)"; then + ok "static guardrails passed" + else + bad "static guardrails failed" + fi + printf '%s\n' "$manifest_out" | sed 's/^/ /' +} + finish() { printf '\n== Gate 1 readiness summary\n' printf 'hard failures: %d\n' "$hard" @@ -57,6 +71,21 @@ finish() { exit 0 } +if [[ "$STATIC_ONLY" == "1" ]]; then + printf 'Gate 1 static checks (cluster-free; STATIC_ONLY=1)\n' + printf 'namespace=%s tag=%s\n' "$NS" "$TAG" + section "manifest guardrails" + run_static_checks + printf '\n== Gate 1 static summary\n' + printf 'hard failures: %d\n' "$hard" + if ((hard > 0)); then + printf 'RESULT: STATIC CHECKS FAILED\n' + exit 1 + fi + printf 'RESULT: STATIC CHECKS PASSED (cluster checks skipped)\n' + exit 0 +fi + trap finish EXIT printf 'Gate 1 readiness (read-only; no cluster mutation)\n' @@ -129,268 +158,7 @@ check_can_i no create roles check_can_i no create rolebindings section "manifest guardrails" -if manifest_out="$(python3 - "$ROOT/deploy/k8s" "$NS" "$TAG" "$REGISTRY" "$OWNER" <<'PY' 2>&1 -import glob -import os -import re -import sys - -root, ns, tag, registry, owner = sys.argv[1:6] -errors = [] -warns = [] -docs = [] - -for path in sorted(glob.glob(os.path.join(root, "*.yaml"))): - with open(path, encoding="utf-8") as handle: - try: - loaded = [d for d in __import__("yaml").safe_load_all(handle) if d] - except Exception as exc: # noqa: BLE001 - report the parse error verbatim - errors.append(f"{os.path.basename(path)}: YAML parse error: {exc}") - continue - if not loaded: - errors.append(f"{os.path.basename(path)}: no YAML documents") - for doc in loaded: - doc["__file__"] = os.path.basename(path) - docs.append(doc) - -if not docs: - errors.append("deploy/k8s contains no manifests") - -allowed = {"ConfigMap", "Deployment", "Service"} -forbidden = { - "Ingress", "Job", "CronJob", "NetworkPolicy", "Role", "RoleBinding", - "ClusterRole", "ClusterRoleBinding", "CustomResourceDefinition", - "Namespace", "PersistentVolume", "PersistentVolumeClaim", "StorageClass", - "PodSecurityPolicy", "PriorityClass", "APIService", - "MutatingWebhookConfiguration", "ValidatingWebhookConfiguration", -} - -objects = {} -configmaps = {} -services = {} -deployments = {} - -for doc in docs: - where = f"{doc['__file__']}:{doc.get('kind', '?')}/{doc.get('metadata', {}).get('name', '?')}" - kind = doc.get("kind") - meta = doc.get("metadata") or {} - name = meta.get("name") - if not doc.get("apiVersion") or not kind or not name: - errors.append(f"{where}: missing apiVersion, kind, or metadata.name") - continue - if kind not in allowed: - errors.append(f"{where}: kind {kind} is not allowed (namespace-only: ConfigMap, Deployment, Service)") - continue - if kind in forbidden: - errors.append(f"{where}: forbidden kind {kind}") - if meta.get("namespace") != ns: - errors.append(f"{where}: metadata.namespace is {meta.get('namespace')!r}, expected {ns!r}") - key = (kind, name) - if key in objects: - errors.append(f"{where}: duplicate object") - objects[key] = doc - if kind == "ConfigMap": - configmaps[name] = doc - elif kind == "Service": - services[name] = doc - elif kind == "Deployment": - deployments[name] = doc - -for name, cm in configmaps.items(): - data = cm.get("data") or {} - for key in data: - if re.search(r"(?i)(token|password|secret|private[_-]?key|credential)", key): - errors.append(f"ConfigMap/{name}: key {key!r} looks like a secret; use a Secret created by the human") - -required_config = { - "pulsecast-shared": {"PUBLIC_WEB_URL", "PUBLIC_API_URL", "API_INTERNAL_URL", "ML_INTERNAL_URL"}, - "web-config": {"PORT", "NUXT_PUBLIC_API_BASE", "NUXT_API_INTERNAL_BASE"}, - "api-config": {"PORT", "DATA_DIR", "FASTAPI_URL", "ML_TIMEOUT", "SSE_HEARTBEAT"}, - "ml-config": { - "PORT", "PULSECAST_ROOT", "PULSECAST_ARTIFACTS", "PULSECAST_WEATHER", - "PULSECAST_MODELS", "PULSECAST_UPLOADS", - }, -} -for name, keys in required_config.items(): - cm = configmaps.get(name) - if cm is None: - errors.append(f"missing ConfigMap/{name}") - continue - have = set((cm.get("data") or {}).keys()) - for key in sorted(keys - have): - errors.append(f"ConfigMap/{name}: missing key {key}") - -expected_ports = {"web": 3000, "api": 8080, "ml": 8000, "cloudflared": 2000} -expected_uids = {"web": 10001, "api": 65532, "ml": 10001, "cloudflared": 65532} -expected_images = { - "web": f"{registry}/{owner}/pulsecast-web:{tag}", - "api": f"{registry}/{owner}/pulsecast-api:{tag}", - "ml": f"{registry}/{owner}/pulsecast-ml:{tag}", -} -gitea_images = {"web", "api", "ml"} - -for name in ("web", "api", "ml", "cloudflared"): - dep = deployments.get(name) - if dep is None: - errors.append(f"missing Deployment/{name}") - continue - spec = dep.get("spec") or {} - if spec.get("replicas") != 1: - errors.append(f"Deployment/{name}: replicas should be 1") - template = (spec.get("template") or {}) - pod = template.get("spec") or {} - if pod.get("automountServiceAccountToken") is not False: - errors.append(f"Deployment/{name}: automountServiceAccountToken must be false") - pod_security = pod.get("securityContext") or {} - if (pod_security.get("seccompProfile") or {}).get("type") != "RuntimeDefault": - errors.append(f"Deployment/{name}: seccompProfile must be RuntimeDefault") - for field in ("runAsUser", "runAsGroup"): - value = pod_security.get(field) - if not isinstance(value, int) or value <= 0: - errors.append(f"Deployment/{name}: {field} must be a positive numeric id (SKE runAsNonRoot caveat)") - elif value != expected_uids[name]: - errors.append(f"Deployment/{name}: {field} is {value}, expected {expected_uids[name]}") - if pod_security.get("runAsNonRoot") is True and pod_security.get("runAsUser") == 0: - errors.append(f"Deployment/{name}: runAsUser 0 conflicts with runAsNonRoot") - for field in ("hostNetwork", "hostPID", "hostIPC"): - if pod.get(field): - errors.append(f"Deployment/{name}: {field} is not allowed") - if name in gitea_images: - pulls = pod.get("imagePullSecrets") or [] - if not any(p.get("name") == "gitea-regcred" for p in pulls): - errors.append(f"Deployment/{name}: imagePullSecrets must include gitea-regcred") - containers = pod.get("containers") or [] - if len(containers) != 1: - errors.append(f"Deployment/{name}: expected exactly one container") - continue - container = containers[0] - if container.get("name") != name: - errors.append(f"Deployment/{name}: container name is {container.get('name')!r}") - image = container.get("image") or "" - if image.endswith(":latest"): - errors.append(f"Deployment/{name}: image tag must be pinned, not :latest") - if name in gitea_images and not image.startswith(f"{registry}/{owner}/"): - errors.append(f"Deployment/{name}: image {image!r} is not under {registry}/{owner}/") - if name in expected_images and image != expected_images[name]: - warns.append(f"Deployment/{name}: image {image!r} does not match TAG={tag}") - if name == "cloudflared" and not image.startswith("cloudflare/cloudflared:"): - errors.append(f"Deployment/{name}: unexpected image {image!r}") - if name in expected_ports: - ports = container.get("ports") or [] - if not any(int(p.get("containerPort", -1)) == expected_ports[name] for p in ports): - errors.append(f"Deployment/{name}: containerPort {expected_ports[name]} not found") - for port in ports: - if port.get("hostPort"): - errors.append(f"Deployment/{name}: hostPort is not allowed") - security = container.get("securityContext") or {} - if security.get("runAsNonRoot") is not True: - errors.append(f"Deployment/{name}: runAsNonRoot must be true") - if security.get("allowPrivilegeEscalation") is not False: - errors.append(f"Deployment/{name}: allowPrivilegeEscalation must be false") - if security.get("readOnlyRootFilesystem") is not True: - errors.append(f"Deployment/{name}: readOnlyRootFilesystem must be true") - if security.get("privileged") is True: - errors.append(f"Deployment/{name}: privileged is not allowed") - if security.get("runAsUser") == 0: - errors.append(f"Deployment/{name}: runAsUser 0 is not allowed") - drops = ((security.get("capabilities") or {}).get("drop") or []) - if "ALL" not in drops: - errors.append(f"Deployment/{name}: capabilities.drop must include ALL") - resources = container.get("resources") or {} - requests = resources.get("requests") or {} - limits = resources.get("limits") or {} - for group_name, group in (("requests", requests), ("limits", limits)): - for key in ("cpu", "memory"): - if not group.get(key): - errors.append(f"Deployment/{name}: resources.{group_name}.{key} is missing") - mounts = container.get("volumeMounts") or [] - tmp_mounts = [m for m in mounts if m.get("mountPath") == "/tmp"] - if not tmp_mounts: - errors.append(f"Deployment/{name}: /tmp volumeMount is missing") - else: - volume_name = tmp_mounts[0].get("name") - volumes = pod.get("volumes") or [] - if not any(v.get("name") == volume_name and "emptyDir" in v for v in volumes): - errors.append(f"Deployment/{name}: /tmp must be backed by an emptyDir") - for probe in ("startupProbe", "livenessProbe", "readinessProbe"): - probe_spec = container.get(probe) or {} - http = probe_spec.get("httpGet") or {} - if not http.get("path") or not http.get("port"): - errors.append(f"Deployment/{name}: {probe}.httpGet is missing") - refs = [] - for source in container.get("envFrom") or []: - if source.get("configMapRef"): - refs.append(source["configMapRef"].get("name")) - for env in container.get("env") or []: - if env.get("valueFrom", {}).get("configMapKeyRef"): - refs.append(env["valueFrom"]["configMapKeyRef"].get("name")) - for ref in refs: - if ref not in configmaps: - errors.append(f"Deployment/{name}: references missing ConfigMap/{ref}") - expected_secret_refs = { - "api": { - ("control-auth", "CONTROL_USER"), - ("control-auth", "CONTROL_PASS"), - ("control-auth", "CONTROL_TOKEN"), - }, - "ml": {("control-auth", "CONTROL_TOKEN")}, - "cloudflared": {("cloudflared", "TUNNEL_TOKEN")}, - } - seen_secret_refs = set() - for env in container.get("env") or []: - secret_ref = env.get("valueFrom", {}).get("secretKeyRef") - if not secret_ref: - continue - secret_name = secret_ref.get("name") - secret_key = secret_ref.get("key") - if secret_name not in ("gitea-regcred", "cloudflared", "control-auth"): - errors.append(f"Deployment/{name}: unexpected Secret reference {secret_name!r}") - seen_secret_refs.add((secret_name, secret_key)) - for required in expected_secret_refs.get(name, set()): - if required not in seen_secret_refs: - errors.append(f"Deployment/{name}: missing Secret reference {required[0]}/{required[1]}") - -if "cloudflared" in deployments: - args = ((deployments["cloudflared"].get("spec") or {}).get("template") or {}).get("spec", {}).get("containers", [{}])[0].get("args") or [] - for required in ("tunnel", "run", "--token", "$(TUNNEL_TOKEN)", "--no-autoupdate", "--protocol", "http2"): - if required not in args: - errors.append(f"Deployment/cloudflared: args must include {required!r}") - -for name in ("web", "api", "ml"): - svc = services.get(name) - if svc is None: - errors.append(f"missing Service/{name}") - continue - spec = svc.get("spec") or {} - if spec.get("type") != "ClusterIP": - errors.append(f"Service/{name}: type must be ClusterIP") - for field in ("externalIPs", "loadBalancerIP", "loadBalancerSourceRanges"): - if spec.get(field): - errors.append(f"Service/{name}: {field} is not allowed") - ports = spec.get("ports") or [] - if not any(int(p.get("port", -1)) == expected_ports[name] for p in ports): - errors.append(f"Service/{name}: port {expected_ports[name]} not found") - if any(p.get("nodePort") for p in ports): - errors.append(f"Service/{name}: nodePort is not allowed") - -kinds = {} -for kind, _ in objects: - kinds[kind] = kinds.get(kind, 0) + 1 -summary = ", ".join(f"{kind}={count}" for kind, count in sorted(kinds.items())) -print(f"parsed {len(objects)} objects ({summary})") -for message in warns: - print(f"WARN: {message}") -for message in errors: - print(f"FAIL: {message}") -sys.exit(1 if errors else 0) -PY -)"; then - ok "static guardrails passed" - printf '%s\n' "$manifest_out" | sed 's/^/ /' -else - bad "static guardrails failed" - printf '%s\n' "$manifest_out" | sed 's/^/ /' -fi +run_static_checks section "kubectl client dry-run" if dry_run_out="$("${KC[@]}" apply --dry-run=client -f "$ROOT/deploy/k8s" 2>&1)"; then