#!/usr/bin/env python3 """Le domaine interne d'un site : une etoile boreale suivie de son index. Decision de Daniel (2026-10-18) : ``, sans `.internal`, pour les sites seulement. Ces cas gardent : 1. la table est FIGEE — un tri refait decalerait le domaine de chaque site ; 2. le domaine se derive de l'index, et seulement d'un index de 1 a 254 ; 3. un nom de site est reconnu prive, comme un nom en `.internal`, et jamais un nom public ; 4. les deux roles qui refusent de publier une zone privee appliquent la MEME regle. """ from __future__ import annotations import hashlib import re import sys import unittest from pathlib import Path RACINE = Path(__file__).resolve().parents[2] sys.path.insert(0, str(RACINE / "scripts")) import etoiles as E # noqa: E402 # L'empreinte de la table du 2026-10-18. Elle ne change que si l'on AJOUTE un nom a la fin — # et alors les reperes ci-dessous, eux, ne bougent pas. EMPREINTE = "4fa592c92263131c13d65dd035ccfd2294b5261190a511bc333908e4b1bbc191" REPERES = {1: "capella", 9: "polaris", 13: "caph", 17: "ruchbah", 23: "talitha", 31: "achird", 37: "miram", 70: "tuiren", 71: "arcturus"} PRIVES = ("achird31", "forge.achird31", "site-forge-01.miram37.", "chezlepro.internal", "infra-dns-01.technolibre.internal.") PUBLICS = ("chezlepro.ca", "technolibre.ca", "dns1.chezlepro.ca.", "exemple.org", "internal.ca") class Table(unittest.TestCase): def test_figee(self): self.assertEqual(hashlib.sha256(" ".join(E.ETOILES).encode()).hexdigest(), EMPREINTE, "la table a ete reordonnee ou modifiee : chaque site changerait de nom") def test_reperes(self): for index, etoile in REPERES.items(): self.assertEqual(E.ETOILES[index - 1], etoile, index) def test_couvre_tous_les_index_sans_doublon(self): self.assertGreaterEqual(len(E.ETOILES), 254) self.assertEqual(len(set(E.ETOILES)), len(E.ETOILES)) for e in E.ETOILES: self.assertRegex(e, r"^[a-z]+$", "libelle DNS : lettres seules, l'index suit") class Domaine(unittest.TestCase): def test_derive(self): self.assertEqual(E.domaine_site(31), "achird31") self.assertEqual(E.domaine_site(37), "miram37") def test_bornes(self): for mauvais in (0, 255, -1, "31", None): with self.assertRaises(ValueError): E.domaine_site(mauvais) class Prive(unittest.TestCase): def test_regle(self): for z in PRIVES: self.assertTrue(E.est_prive(z), z) for z in PUBLICS: self.assertFalse(E.est_prive(z), z) def test_tout_domaine_de_site_est_prive(self): for i in range(1, 255): self.assertTrue(E.est_prive(E.domaine_site(i)), i) def test_les_roles_appliquent_la_meme_regle(self): for f in ("roles/serveur_powerdns/tasks/zones-publiques.yml", "roles/serveur_dns_public/tasks/main.yml"): src = (RACINE / f).read_text(encoding="utf-8") rx = re.search(r"select\('search', '([^']+)'\)", src) self.assertIsNotNone(rx, f) for z in PRIVES: self.assertTrue(re.search(rx.group(1), z), f"{f} laisserait passer {z}") for z in PUBLICS: self.assertFalse(re.search(rx.group(1), z), f"{f} refuserait {z}") if __name__ == "__main__": unittest.main(verbosity=2)