Merge pull request 'feat(frontal): ne publier que les chemins publics d'une application (D-127)' (#15) from danallaire/frontal/chemins-publics into main
Reviewed-on: #15
This commit is contained in:
commit
a0e06b132d
7 changed files with 138 additions and 2 deletions
5
changelog.d/frontal-chemins-publics.md
Normal file
5
changelog.d/frontal-chemins-publics.md
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
## Le web frontal peut ne publier qu'une partie d'un service
|
||||||
|
|
||||||
|
Pour publier `auth.chezlepro.ca` (D-127), il fallait servir les realms de Keycloak à Internet sans sa console d'administration. Le frontal relayait un service entier (`location /`). Une application déclare désormais au plan ses `chemins_publics`, des préfixes comme `/realms/chezlepro/` et `/resources/`. Le frontal ne relaie qu'eux et répond 404 au reste.
|
||||||
|
|
||||||
|
Sans le champ, le rendu est identique à l'ancien, octet pour octet. La configuration produite avec le champ a été vérifiée par le nginx du web frontal (`nginx -t`). Le plan refuse un préfixe qui ne commence et ne finit pas par « / », une liste vide, et des chemins sans exposition.
|
||||||
|
|
@ -142,6 +142,14 @@
|
||||||
"administration"
|
"administration"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
"chemins_publics": {
|
||||||
|
"type": "array",
|
||||||
|
"title": "Chemins publics",
|
||||||
|
"description": "Prefixes que le web frontal relaie (`/realms/chezlepro/`, `/resources/`) ; le reste repond 404. Vide : tout est relaye. Sans effet a l'edge interne.",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
"source": {
|
"source": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"title": "Source (application cliente)",
|
"title": "Source (application cliente)",
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,21 @@ pour la zone d'administration. Les deux ne se remplacent pas.
|
||||||
chaque exposition publique servie à travers lui (en HTTPS si elle a son certificat),
|
chaque exposition publique servie à travers lui (en HTTPS si elle a son certificat),
|
||||||
échéance des certificats.
|
échéance des certificats.
|
||||||
|
|
||||||
|
## Ne publier qu'une partie d'un service (`chemins_publics`)
|
||||||
|
|
||||||
|
Une application peut déclarer, au plan, les **seuls préfixes** que le frontal relaie :
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
keycloak:
|
||||||
|
expose: [auth.chezlepro.internal, auth.chezlepro.ca]
|
||||||
|
chemins_publics: [/realms/chezlepro/, /resources/]
|
||||||
|
```
|
||||||
|
|
||||||
|
Le frontal pose alors un `location ^~` par préfixe, et répond **404** au reste : la console
|
||||||
|
d'administration de Keycloak n'est jamais publiée (D-127). Sans le champ, tout est relayé, comme
|
||||||
|
avant. Un préfixe commence et finit par « / », sans motif. L'edge interne n'en tient pas compte :
|
||||||
|
il sert les personnes de l'écosystème.
|
||||||
|
|
||||||
## Le WAF
|
## Le WAF
|
||||||
`serveur_web_frontal_waf_mode: DetectionOnly` d'abord : le CRS journalise ce qu'il aurait
|
`serveur_web_frontal_waf_mode: DetectionOnly` d'abord : le CRS journalise ce qu'il aurait
|
||||||
bloqué (`/var/log/nginx/modsec_audit.log`) sans rien refuser. On lit, on ajuste les
|
bloqué (`/var/log/nginx/modsec_audit.log`) sans rien refuser. On lit, on ajuste les
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,22 @@
|
||||||
{% macro relais(expo, tls) %}
|
{% macro relais(expo, tls) %}
|
||||||
|
{% if expo.chemins_publics | default([]) | length > 0 %}
|
||||||
|
# SEULS CES PREFIXES SONT PUBLICS (`chemins_publics`, D-127) : le reste repond 404. Keycloak
|
||||||
|
# publie ainsi ses realms, jamais sa console d'administration.
|
||||||
|
{% for chemin in expo.chemins_publics %}
|
||||||
|
location ^~ {{ chemin }} {
|
||||||
|
{{ proxy(expo, tls) }}
|
||||||
|
}
|
||||||
|
{% endfor %}
|
||||||
location / {
|
location / {
|
||||||
|
return 404;
|
||||||
|
}
|
||||||
|
{% else %}
|
||||||
|
location / {
|
||||||
|
{{ proxy(expo, tls) }}
|
||||||
|
}
|
||||||
|
{% endif %}
|
||||||
|
{% endmacro %}
|
||||||
|
{% macro proxy(expo, tls) %}
|
||||||
proxy_pass {{ 'https' if tls else 'http' }}://{{ hostvars[expo.hote].ansible_host }}:{{ expo.port }};
|
proxy_pass {{ 'https' if tls else 'http' }}://{{ hostvars[expo.hote].ansible_host }}:{{ expo.port }};
|
||||||
{% if tls %}
|
{% if tls %}
|
||||||
proxy_ssl_verify on;
|
proxy_ssl_verify on;
|
||||||
|
|
@ -11,8 +28,7 @@
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
proxy_set_header X-Forwarded-Proto $scheme;
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
}
|
{%- endmacro %}
|
||||||
{% endmacro %}
|
|
||||||
{% macro defi() %}
|
{% macro defi() %}
|
||||||
# Le defi HTTP-01 de Let's Encrypt : servi ici, jamais relaye ni redirige.
|
# Le defi HTTP-01 de Let's Encrypt : servi ici, jamais relaye ni redirige.
|
||||||
location ^~ /.well-known/acme-challenge/ {
|
location ^~ /.well-known/acme-challenge/ {
|
||||||
|
|
|
||||||
|
|
@ -507,6 +507,17 @@ def valider_applications(registre: dict, domaines: dict | None = None,
|
||||||
f"{', '.join(sorted(AUDIENCES))} se declare (sans elle, tout le locataire).")
|
f"{', '.join(sorted(AUDIENCES))} se declare (sans elle, tout le locataire).")
|
||||||
if audience is not None and not expose:
|
if audience is not None and not expose:
|
||||||
raise ValueError(f"Application '{nom}': 'audience' sans 'expose' ne restreint rien.")
|
raise ValueError(f"Application '{nom}': 'audience' sans 'expose' ne restreint rien.")
|
||||||
|
chemins = app.get("chemins_publics")
|
||||||
|
if chemins is not None:
|
||||||
|
if not isinstance(chemins, list) or not chemins:
|
||||||
|
raise ValueError(f"Application '{nom}': 'chemins_publics' est une liste non vide de "
|
||||||
|
f"prefixes (sans elle, tout est relaye).")
|
||||||
|
for c in chemins:
|
||||||
|
if not re.fullmatch(r"/(?:[A-Za-z0-9._~-]+/)+", str(c)):
|
||||||
|
raise ValueError(f"Application '{nom}': chemin public « {c} » : un prefixe qui "
|
||||||
|
f"commence et finit par « / » (`/realms/chezlepro/`), sans motif.")
|
||||||
|
if not expose:
|
||||||
|
raise ValueError(f"Application '{nom}': 'chemins_publics' sans 'expose' ne restreint rien.")
|
||||||
# UNE APPLICATION CLIENTE SE NOMME ET S'EPINGLE (D-90, D-91) : la regle vit dans
|
# UNE APPLICATION CLIENTE SE NOMME ET S'EPINGLE (D-90, D-91) : la regle vit dans
|
||||||
# `app_contrat`, que P100 et la commande du client lisent aussi.
|
# `app_contrat`, que P100 et la commande du client lisent aussi.
|
||||||
from app_contrat import verifier_declaration
|
from app_contrat import verifier_declaration
|
||||||
|
|
@ -630,6 +641,8 @@ def expositions_des_applications(applications: dict, domaines: dict, edge: str |
|
||||||
"websocket": bool(app.get("websocket")),
|
"websocket": bool(app.get("websocket")),
|
||||||
# `administration` : l'edge ne sert ce nom qu'aux reseaux d'administration (D-110).
|
# `administration` : l'edge ne sert ce nom qu'aux reseaux d'administration (D-110).
|
||||||
"audience": app.get("audience"),
|
"audience": app.get("audience"),
|
||||||
|
# Les seuls prefixes que le web frontal relaie ; vide : tout (2026-11-23, D-127).
|
||||||
|
"chemins_publics": [str(c) for c in (app.get("chemins_publics") or [])],
|
||||||
})
|
})
|
||||||
return resultat
|
return resultat
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -147,6 +147,12 @@ REGISTRES: dict = {
|
||||||
"audience": {"type": "string", "enum": ["administration"], "libelle": "Audience",
|
"audience": {"type": "string", "enum": ["administration"], "libelle": "Audience",
|
||||||
"aide": "`administration` : l'edge ne sert ce nom qu'aux reseaux d'administration "
|
"aide": "`administration` : l'edge ne sert ce nom qu'aux reseaux d'administration "
|
||||||
"(le tunnel des admins du locataire, le plan d'administration du site)."},
|
"(le tunnel des admins du locataire, le plan d'administration du site)."},
|
||||||
|
# CE QUE LE FRONTAL PUBLIE D'UNE APPLICATION, ET RIEN D'AUTRE (2026-11-23, D-127). Keycloak
|
||||||
|
# expose ses realms au public, jamais sa console d'administration : le web frontal ne
|
||||||
|
# relaie que ces prefixes, et repond 404 au reste. Sans ce champ, tout est relaye.
|
||||||
|
"chemins_publics": {"type": "array", "items": {"type": "string"}, "libelle": "Chemins publics",
|
||||||
|
"aide": "Prefixes que le web frontal relaie (`/realms/chezlepro/`, `/resources/`) ; "
|
||||||
|
"le reste repond 404. Vide : tout est relaye. Sans effet a l'edge interne."},
|
||||||
# L'APPLICATION D'UN CLIENT (D-89 a D-91, `app_contrat`). Son depot decrit ce
|
# L'APPLICATION D'UN CLIENT (D-89 a D-91, `app_contrat`). Son depot decrit ce
|
||||||
# qu'elle est (`setops-app.yml`) ; le plan ne dit que d'ou et a quelle version.
|
# qu'elle est (`setops-app.yml`) ; le plan ne dit que d'ou et a quelle version.
|
||||||
"source": {"type": "string", "libelle": "Source (application cliente)",
|
"source": {"type": "string", "libelle": "Source (application cliente)",
|
||||||
|
|
|
||||||
73
scripts/tests/test_chemins_publics.py
Normal file
73
scripts/tests/test_chemins_publics.py
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
#!/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/<realm>/`, `/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()
|
||||||
Loading…
Reference in a new issue