Generate auth-gateway test fixtures from the Django config exporter

This commit is contained in:
Eduardo Silva
2026-11-16 11:00:25 -03:00
parent 16521223f9
commit 4e8ebce289
6 changed files with 419 additions and 20 deletions
@@ -0,0 +1,55 @@
{
"entries": [
{
"id": "cafe",
"name": "Caf\u00e9",
"hosts": [],
"upstream": "http://10.0.0.14:8000",
"allow_invalid_cert": false
},
{
"id": "dashboard",
"name": "Dashboard",
"hosts": [
"dashboard.example.test"
],
"upstream": "http://10.0.0.10:3000",
"allow_invalid_cert": false
},
{
"id": "files",
"name": "Files",
"hosts": [],
"upstream": "http://10.0.0.13:8080",
"allow_invalid_cert": false
},
{
"id": "hypervisor",
"name": "Hypervisor",
"hosts": [
"hypervisor.example.test"
],
"upstream": "https://10.0.0.12:8006",
"allow_invalid_cert": true
},
{
"id": "media",
"name": "Media",
"hosts": [
"media.example.test",
"media2.example.test"
],
"upstream": "http://10.0.0.11:32400",
"allow_invalid_cert": false
},
{
"id": "sso",
"name": "SSO",
"hosts": [
"sso.example.test"
],
"upstream": "http://10.0.0.15:8000",
"allow_invalid_cert": false
}
]
}
@@ -0,0 +1,137 @@
{
"auth_methods": {
"ip-allowlist": {
"type": "ip_address",
"rules": [
{
"address": "10.10.0.1",
"prefix_length": null,
"action": "allow",
"description": ""
},
{
"address": "10.20.0.0",
"prefix_length": 24,
"action": "allow",
"description": ""
},
{
"address": "10.20.0.12",
"prefix_length": null,
"action": "deny",
"description": ""
},
{
"address": "10.30.0.15",
"prefix_length": null,
"action": "allow",
"description": ""
}
]
},
"ip-empty": {
"type": "ip_address",
"rules": []
},
"local-password": {
"type": "local_password",
"session_expiration_minutes": 720
},
"oidc-company": {
"type": "oidc",
"session_expiration_minutes": 2880,
"provider": "https://idp.example.test",
"client_id": "fixture-client",
"client_secret": "fixture-client-secret",
"allowed_domains": [
"example.test"
],
"allowed_emails": [
"contractor@partner.example.test"
]
},
"totp-global": {
"type": "totp",
"totp_secret": "JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP"
}
},
"groups": {
"staff": {
"users": [
"alice",
"bob"
]
}
},
"users": {
"alice": {
"email": "alice@example.test",
"password_hash": "$argon2id$v=19$m=65536,t=3,p=4$bEzFGMfGGK4rdHKhmwwMqw$1J+Ss48IEK1Q4Vpy0RaS+FHIZOjhjhE4MK5C5pzDzHg",
"totp_secret": ""
},
"bob": {
"email": "",
"password_hash": "$argon2id$v=19$m=65536,t=3,p=4$bEzFGMfGGK4rdHKhmwwMqw$1J+Ss48IEK1Q4Vpy0RaS+FHIZOjhjhE4MK5C5pzDzHg",
"totp_secret": "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"
}
},
"policies": {
"deny": {
"policy_type": "deny",
"groups": [],
"methods": []
},
"empty-ip-list": {
"policy_type": "protected",
"groups": [],
"methods": [
"ip-empty"
]
},
"oidc-company": {
"policy_type": "protected",
"groups": [],
"methods": [
"oidc-company"
]
},
"password-totp": {
"policy_type": "protected",
"groups": [
"staff"
],
"methods": [
"local-password",
"totp-global"
]
},
"public": {
"policy_type": "bypass",
"groups": [],
"methods": []
},
"staff-password": {
"policy_type": "protected",
"groups": [
"staff"
],
"methods": [
"local-password"
]
},
"totp-only": {
"policy_type": "protected",
"groups": [],
"methods": [
"totp-global"
]
},
"trusted-ips": {
"policy_type": "protected",
"groups": [],
"methods": [
"ip-allowlist"
]
}
}
}
@@ -0,0 +1,42 @@
{
"entries": {
"cafe": {
"routes": []
},
"dashboard": {
"routes": [
{
"id": "admin",
"path_prefix": "/admin",
"policy": "password-totp"
},
{
"id": "api",
"path_prefix": "/api",
"policy": "trusted-ips"
}
],
"default_policy": "staff-password"
},
"files": {
"routes": [],
"default_policy": "totp-only"
},
"hypervisor": {
"routes": [],
"default_policy": "trusted-ips"
},
"media": {
"routes": [],
"default_policy": "public"
},
"sso": {
"routes": [],
"default_policy": "oidc-company"
},
"wireguard_webadmin": {
"routes": [],
"default_policy": "public"
}
}
}
@@ -0,0 +1,21 @@
{
"entries": [
{
"id": "wireguard_webadmin",
"name": "WireGuard WebAdmin",
"hosts": [
"webadmin.example.test",
"webadmin2.example.test"
],
"upstream": "wireguard-webadmin:8000",
"static_routes": [
{
"path_prefix": "/static",
"root": "/static",
"strip_prefix": "/static",
"cache_control": "public, max-age=3600"
}
]
}
]
}
@@ -17,25 +17,32 @@ assert _PROCESS_CONFIG_SPEC and _PROCESS_CONFIG_SPEC.loader
_PROCESS_CONFIG_SPEC.loader.exec_module(_PROCESS_CONFIG_MODULE)
build_caddyfile = _PROCESS_CONFIG_MODULE.build_caddyfile
# Generated by the Django exporter: UPDATE_AUTH_GATEWAY_FIXTURES=1 python manage.py test app_gateway
FIXTURES_CONFIG_DIR = Path(__file__).resolve().parent / "fixtures" / "config_files"
class AuthGatewayConfigTests(unittest.TestCase):
def test_existing_config_loads_and_resolves_routes(self):
config_dir = Path(__file__).resolve().parents[2] / "caddy" / "config_files"
runtime_config = load_runtime_config(config_dir)
runtime_config = load_runtime_config(FIXTURES_CONFIG_DIR)
context = resolve_request_context(runtime_config, "app1-dev.local", "/admin/settings")
context = resolve_request_context(runtime_config, "dashboard.example.test", "/admin/settings")
self.assertIsNotNone(context)
self.assertEqual(context.policy_name, "senha-totp")
self.assertEqual(context.policy_name, "password-totp")
context = resolve_request_context(runtime_config, "app1-dev.local", "/api/status")
context = resolve_request_context(runtime_config, "dashboard.example.test", "/api/status")
self.assertIsNotNone(context)
self.assertEqual(context.policy_name, "ips-conhecidos")
self.assertEqual(context.policy_name, "trusted-ips")
policy = build_effective_policy(runtime_config, "senha-totp")
context = resolve_request_context(runtime_config, "dashboard.example.test", "/")
self.assertIsNotNone(context)
self.assertEqual(context.policy_name, "staff-password")
policy = build_effective_policy(runtime_config, "password-totp")
self.assertIsNotNone(policy)
self.assertEqual(policy.required_factors, ["totp", "password"])
self.assertEqual(policy.required_factors, ["password", "totp"])
self.assertEqual(policy.allowed_users, {"alice", "bob"})
def test_invalid_oidc_provider_fails_loudly(self):
def test_invalid_oidc_provider_disables_policy(self):
with tempfile.TemporaryDirectory() as tmpdir:
tmp_path = Path(tmpdir)
(tmp_path / "wireguard_webadmin.json").write_text(
@@ -51,16 +58,19 @@ class AuthGatewayConfigTests(unittest.TestCase):
encoding="utf-8",
)
with self.assertRaises(ValueError):
load_runtime_config(tmp_path)
with self.assertLogs("auth_gateway.config_loader", level="WARNING"):
runtime_config = load_runtime_config(tmp_path)
self.assertNotIn("oidc", runtime_config.auth_methods)
self.assertEqual(runtime_config.policies["default"].policy_type, "error")
def test_ip_rules_respect_json_order(self):
config_dir = Path(__file__).resolve().parents[2] / "caddy" / "config_files"
runtime_config = load_runtime_config(config_dir)
method = runtime_config.auth_methods["iplist1"]
self.assertTrue(evaluate_ip_rules("192.168.0.8", method.rules))
self.assertTrue(evaluate_ip_rules("192.168.0.12", method.rules))
self.assertFalse(evaluate_ip_rules("10.10.10.10", method.rules))
runtime_config = load_runtime_config(FIXTURES_CONFIG_DIR)
method = runtime_config.auth_methods["ip-allowlist"]
self.assertTrue(evaluate_ip_rules("10.10.0.1", method.rules))
self.assertTrue(evaluate_ip_rules("10.20.0.8", method.rules))
self.assertTrue(evaluate_ip_rules("10.20.0.12", method.rules))
self.assertFalse(evaluate_ip_rules("10.99.0.1", method.rules))
def test_totp_verification_accepts_valid_tokens(self):
secret = "JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP"
@@ -68,10 +78,12 @@ class AuthGatewayConfigTests(unittest.TestCase):
self.assertTrue(verify_totp(secret, token))
def test_totp_verification_rejects_short_secrets(self):
self.assertFalse(verify_totp("JBSWY3DPEHPK3PXP", "123456"))
with self.assertLogs("uvicorn.error", level="WARNING"):
self.assertFalse(verify_totp("JBSWY3DPEHPK3PXP", "123456"))
def test_totp_verification_rejects_invalid_secrets(self):
self.assertFalse(verify_totp("NOT-A-BASE32-SECRET!", "123456"))
with self.assertLogs("uvicorn.error", level="WARNING"):
self.assertFalse(verify_totp("NOT-A-BASE32-SECRET!", "123456"))
def test_caddyfile_uses_boundary_matchers_and_clears_identity_headers(self):
caddyfile = build_caddyfile(