109 lines
5.1 KiB
Python
109 lines
5.1 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""L'edge ne sert un outil d'administration qu'aux reseaux d'administration (D-110).
|
||
|
|
|
||
|
|
POURQUOI (2026-11-08). Le personnel d'un locataire joindra l'edge par son tunnel. L'edge sert tous
|
||
|
|
ses noms sur la meme adresse et le meme port : le pare-feu ne peut pas distinguer `vigie` de
|
||
|
|
`monregistraire`. Le plan marque donc l'exposition (`audience: administration`), et l'edge en
|
||
|
|
derive `allow` / `deny all`. Ces cas fixent :
|
||
|
|
1. le plan refuse une audience inconnue, et une audience sans exposition ;
|
||
|
|
2. la derivation des expositions porte l'audience ;
|
||
|
|
3. le vhost d'un nom d'administration admet ses sources et refuse le reste ; un nom ordinaire
|
||
|
|
n'a aucune restriction ;
|
||
|
|
4. les sources sont le plan d'administration du site ET le tunnel des admins du locataire : la
|
||
|
|
variable posee sur l'hote seule ne porte pas le tunnel, par ou l'exploitant arrive ;
|
||
|
|
5. sans pair actif dans le tunnel, il ne reste que le plan d'administration du site.
|
||
|
|
"""
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import sys
|
||
|
|
import tempfile
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
import jinja2
|
||
|
|
import yaml
|
||
|
|
|
||
|
|
RACINE = Path(__file__).resolve().parents[2]
|
||
|
|
sys.path.insert(0, str(RACINE / "scripts"))
|
||
|
|
|
||
|
|
import inventory_rules as R # noqa: E402
|
||
|
|
|
||
|
|
echecs = 0
|
||
|
|
|
||
|
|
|
||
|
|
def verifier(cond: bool, quoi: str) -> None:
|
||
|
|
global echecs
|
||
|
|
print(("OK " if cond else "ECHEC ") + quoi)
|
||
|
|
echecs += 0 if cond else 1
|
||
|
|
|
||
|
|
|
||
|
|
def refuse(apps: dict) -> str:
|
||
|
|
try:
|
||
|
|
R.valider_applications({"applications": apps}, {})
|
||
|
|
except ValueError as e:
|
||
|
|
return str(e)
|
||
|
|
return ""
|
||
|
|
|
||
|
|
|
||
|
|
def rendre(expositions: list[dict], sources: list[str]) -> str:
|
||
|
|
env = jinja2.Environment(undefined=jinja2.StrictUndefined, trim_blocks=True, lstrip_blocks=False)
|
||
|
|
env.filters["bool"] = lambda v: str(v).lower() in ("true", "1", "yes", "oui")
|
||
|
|
gabarit = env.from_string((RACINE / "roles/serveur_nginx/templates/expositions.conf.j2").read_text(encoding="utf-8"))
|
||
|
|
return gabarit.render(
|
||
|
|
serveur_nginx_expositions=expositions, serveur_nginx_sources_administration=sources,
|
||
|
|
hostvars={"mon-01": {"ansible_host": "192.0.2.20"}, "web-01": {"ansible_host": "192.0.2.21"}},
|
||
|
|
serveur_nginx_redirection_http=True, serveur_nginx_certificat="/c.crt", serveur_nginx_cle="/c.key",
|
||
|
|
serveur_nginx_proxy_buffer_size="16k", serveur_nginx_proxy_buffers="8 16k",
|
||
|
|
serveur_nginx_proxy_busy_buffers_size="32k", serveur_nginx_ports_tls=[443])
|
||
|
|
|
||
|
|
|
||
|
|
def bloc(texte: str, fqdn: str) -> str:
|
||
|
|
"""Le vhost de `fqdn`, de son commentaire d'en-tete au suivant."""
|
||
|
|
debut = texte.index(f"# {fqdn} ->")
|
||
|
|
suivant = texte.find("\n# ", debut + 1)
|
||
|
|
return texte[debut:suivant if suivant > 0 else len(texte)]
|
||
|
|
|
||
|
|
|
||
|
|
def main() -> int:
|
||
|
|
base = {"groupe": "serveur_oauth2_proxy", "hote": "mon-01", "port": 4180}
|
||
|
|
verifier("audience" in refuse({"vigie": {**base, "expose": ["vigie.exemple.internal"], "audience": "personnel"}}),
|
||
|
|
"1. une audience inconnue est refusee")
|
||
|
|
verifier("ne restreint rien" in refuse({"vigie": {**base, "audience": "administration"}}),
|
||
|
|
"1. une audience sans exposition est refusee")
|
||
|
|
|
||
|
|
apps = {"applications": {
|
||
|
|
"vigie": {**base, "expose": ["vigie.exemple.internal"], "audience": "administration"},
|
||
|
|
"registre": {"groupe": "serveur_web_dorsal", "hote": "web-01", "port": 80, "expose": ["registre.exemple.internal"]},
|
||
|
|
}}
|
||
|
|
expos = R.expositions_des_applications(apps, {})
|
||
|
|
par = {e["fqdn"]: e for e in expos}
|
||
|
|
verifier(par["vigie.exemple.internal"]["audience"] == "administration"
|
||
|
|
and par["registre.exemple.internal"]["audience"] is None, "2. la derivation porte l'audience")
|
||
|
|
|
||
|
|
texte = rendre(expos, ["127.0.0.1", "::1", "192.0.2.0/24", "198.51.100.0/24"])
|
||
|
|
v, r = bloc(texte, "vigie.exemple.internal"), bloc(texte, "registre.exemple.internal")
|
||
|
|
verifier(all(f"allow {s};" in v for s in ("127.0.0.1", "192.0.2.0/24", "198.51.100.0/24")) and "deny all;" in v,
|
||
|
|
"3. le nom d'administration admet ses sources, puis refuse le reste")
|
||
|
|
verifier("deny all;" not in r and "allow " not in r, "3. un nom ordinaire n'a aucune restriction")
|
||
|
|
|
||
|
|
with tempfile.TemporaryDirectory() as d:
|
||
|
|
loc = Path(d)
|
||
|
|
(loc / "plan").mkdir()
|
||
|
|
(loc / "plan" / "nomenclature.yml").write_text(yaml.safe_dump({"index": 23}))
|
||
|
|
cle = "A" * 42 + "A="
|
||
|
|
(loc / "plan" / "acces.yml").write_text(yaml.safe_dump(
|
||
|
|
{"acces_admin_vpn": {"admin-portable": {"cle_publique": cle, "hote": 2, "etat": "present"}}}))
|
||
|
|
sources = R.reseaux_administration(loc, ["192.0.2.0/24"])
|
||
|
|
verifier(sources == sorted(["192.0.2.0/24", "10.23.29.0/24"]),
|
||
|
|
f"4. le plan d'administration du site ET le tunnel du locataire ({sources})")
|
||
|
|
(loc / "plan" / "acces.yml").write_text(yaml.safe_dump(
|
||
|
|
{"acces_admin_vpn": {"admin-portable": {"cle_publique": cle, "hote": 2, "etat": "absent"}}}))
|
||
|
|
sources = R.reseaux_administration(loc, ["192.0.2.0/24"])
|
||
|
|
verifier(sources == ["192.0.2.0/24"], f"5. sans pair actif, le seul plan du site ({sources})")
|
||
|
|
|
||
|
|
print(f"\n{'TOUT VERT' if not echecs else f'{echecs} ECHEC(S)'}")
|
||
|
|
return 1 if echecs else 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
sys.exit(main())
|