- add .gitea/workflows/ci.yml with no secrets and no cluster writes - extract static guardrails to scripts/check_manifests.py - add gate1-check STATIC_ONLY=1 for CI without a kubeconfig - add numeric fsGroup to every pod securityContext - document CI and numeric user requirements
This commit is contained in:
1 parent
5b199f8fe3
commit
23ad37699f
11 files changed
+364
-273
No files matched your search
+30
-262
@@ -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
|
||||
|
||||
Reference in new issue
Block a user