#!/usr/bin/env python3 """Le web frontal ne relaie que les chemins publics d'une application (2026-11-23, D-127). Keycloak publie ses realms (`/realms//`, `/resources/`), jamais sa console d'administration. Le plan le declare (`chemins_publics`) ; le gabarit ne relaie que ces prefixes et repond 404 au reste ; sans le champ, rien ne change. """ from __future__ import annotations import sys import unittest from pathlib import Path import jinja2 RACINE = Path(__file__).resolve().parents[2] sys.path.insert(0, str(RACINE / "scripts")) import inventory_rules as R # noqa: E402 DOMS = {"domaines_publics": {"exemple.ca": {"edge": "serveur_web_frontal"}}} def app(**k): return {"applications": {"keycloak": {"groupe": "serveur_keycloak", "hote": "idm-01", "port": 8080, "expose": ["auth.exemple.ca"], **k}}} def rendre(expos): env = jinja2.Environment(trim_blocks=True) env.filters["bool"] = lambda v: v if isinstance(v, bool) else str(v).lower() in ("true", "1") src = (RACINE / "roles/serveur_web_frontal/templates/frontal.conf.j2").read_text(encoding="utf-8") return env.from_string(src).render( serveur_web_frontal_expositions=expos, serveur_web_frontal_certifies=[], serveur_web_frontal_ports_tls=[443, 8443], serveur_web_frontal_page_404="", serveur_web_frontal_acme_actif=False, serveur_web_frontal_hsts="", serveur_web_frontal_waf_dir="/etc/nginx/setops-waf", domaine_interne="exemple.internal", hostvars={"idm-01": {"ansible_host": "10.1.17.11"}}) class CheminsPublics(unittest.TestCase): def test_la_validation(self): R.valider_applications(app(chemins_publics=["/realms/exemple/", "/resources/"]), DOMS) for mauvais in ([], ["/realms/exemple"], ["realms/"], ["/admin/*"], "/realms/"): with self.assertRaises(ValueError, msg=repr(mauvais)): R.valider_applications(app(chemins_publics=mauvais), DOMS) with self.assertRaises(ValueError): R.valider_applications({"applications": {"k": {"groupe": "serveur_keycloak", "hote": "idm-01", "chemins_publics": ["/realms/x/"]}}}, DOMS) def test_le_filtre_porte_les_chemins(self): e = R.expositions_des_applications(app(chemins_publics=["/realms/exemple/"]), DOMS, "serveur_web_frontal") self.assertEqual(e[0]["chemins_publics"], ["/realms/exemple/"]) self.assertEqual(R.expositions_des_applications(app(), DOMS, "serveur_web_frontal")[0]["chemins_publics"], []) def test_le_frontal_ne_relaie_que_ces_chemins(self): e = R.expositions_des_applications(app(chemins_publics=["/realms/exemple/", "/resources/"]), DOMS, "serveur_web_frontal") conf = rendre(e) self.assertIn("location ^~ /realms/exemple/ {", conf) self.assertIn("location ^~ /resources/ {", conf) bloc_racine = conf.split("location / {", 1)[1].split("}", 1)[0] self.assertIn("return 404;", bloc_racine, "la racine (et donc /admin/) repond 404") self.assertEqual(conf.count("proxy_pass"), 2, "un relais par prefixe, aucun pour la racine") def test_sans_chemins_tout_est_relaye(self): conf = rendre(R.expositions_des_applications(app(), DOMS, "serveur_web_frontal")) self.assertEqual(conf.count("proxy_pass"), 1) self.assertNotIn("return 404;", conf.split("server_name auth.exemple.ca;", 1)[1]) if __name__ == "__main__": unittest.main()