Files
homelab/k8s-ng/apps/openartifact/apply-sso.py
T
ab 01b598a285 Make OpenArtifact policy application extensible
Move publisher manifests into a dedicated policies directory and mount them
through a separate Kustomize-generated ConfigMap. Apply all mounted YAML files
in filename order instead of hard-coding the WinGet manifest and seven objects.
Require a nonempty successful response for each manifest and fail the PostSync
hook on partial or malformed results. Keep SSO secret substitution separate.

Document registration, ordering and explicit revocation. New publishers require
only a policy file and a ConfigMap files entry, without Python or Job changes.

Validation: four Python unit tests passed, including arbitrary object counts,
manifest discovery and error handling. Rendered ConfigMaps and the PostSync Job
with kubectl kustomize; full Helm chart rendering and live application were not
performed.
2026-12-02 03:59:15 +02:00

57 lines
2.2 KiB
Python

"""Apply SSO and all publisher manifests mounted by the GitOps PostSync job."""
import json
from http.cookiejar import CookieJar
from pathlib import Path
from urllib.request import HTTPCookieProcessor, Request, build_opener
def apply_manifest(post, path, **options):
result = json.loads(post("admin/declarative/apply", {
"manifest": path.read_text(encoding="utf-8"), **options,
}))
items = result.get("items")
if not isinstance(items, list) or not items or any(
not isinstance(item, dict) or not item.get("name") or item.get("result") != "applied"
for item in items
):
raise RuntimeError(f"OpenArtifact did not apply every object in {path.name}")
print(f"Applied {path.name}: {len(items)} objects")
return items
def apply_policies(post, directory):
manifests = sorted(directory.glob("*.yaml"))
if not manifests:
raise RuntimeError(f"No policy manifests found in {directory}")
for path in manifests:
apply_manifest(post, path)
def main():
server = "https://repo.hexor.cy"
cookies = CookieJar()
session = build_opener(HTTPCookieProcessor(cookies))
headers = {"Content-Type": "application/json", "Origin": server}
def post(path, body):
request = Request(server + "/api/v1/" + path, json.dumps(body).encode(), headers)
with session.open(request, timeout=30) as response:
return response.read()
credentials = Path("/credentials")
client_secret = (credentials / "client-secret").read_text()
post("auth/login", {"username": "admin", "password": (credentials / "admin-password").read_text()})
try:
headers["X-CSRF-Token"] = next(cookie.value for cookie in cookies if cookie.name == "oa_csrf")
providers = apply_manifest(post, Path("/config/sso.yaml"), force_secrets=True,
secrets={"OIDC_CLIENT_SECRET": client_secret})
if [item["name"] for item in providers] != ["keycloak"]:
raise RuntimeError("OpenArtifact did not apply the Keycloak provider")
apply_policies(post, Path("/policies"))
finally:
post("auth/logout", {})
if __name__ == "__main__":
main()