mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-11-17 10:33:15 +01:00
Generate auth-gateway test fixtures from the Django config exporter
This commit is contained in:
+133
-1
@@ -1 +1,133 @@
|
||||
# Create your tests here.
|
||||
import contextlib
|
||||
import importlib.util
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
|
||||
from app_gateway.caddy_config_export import export_caddy_config
|
||||
from app_gateway.models import AccessPolicy, Application, ApplicationHost, ApplicationPolicy, ApplicationRoute
|
||||
from gatekeeper.models import (
|
||||
AuthMethod, AuthMethodAllowedDomain, AuthMethodAllowedEmail, GatekeeperGroup, GatekeeperIPAddress, GatekeeperUser
|
||||
)
|
||||
|
||||
AUTH_GATEWAY_FIXTURES_DIR = Path(settings.BASE_DIR) / 'containers' / 'auth-gateway' / 'tests' / 'fixtures' / 'config_files'
|
||||
UPDATE_FIXTURES_ENV = 'UPDATE_AUTH_GATEWAY_FIXTURES'
|
||||
|
||||
# Argon2 hash of "fixture-password". Kept fixed so the exported files are reproducible.
|
||||
FIXTURE_PASSWORD_HASH = '$argon2id$v=19$m=65536,t=3,p=4$bEzFGMfGGK4rdHKhmwwMqw$1J+Ss48IEK1Q4Vpy0RaS+FHIZOjhjhE4MK5C5pzDzHg'
|
||||
FIXTURE_GLOBAL_TOTP_SECRET = 'JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP'
|
||||
FIXTURE_USER_TOTP_SECRET = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ'
|
||||
|
||||
_WEBADMIN_EXPORT_PATH = Path(settings.BASE_DIR) / 'containers' / 'caddy' / 'export_wireguard_webadmin_config.py'
|
||||
_WEBADMIN_EXPORT_SPEC = importlib.util.spec_from_file_location('caddy_export_wireguard_webadmin_config', _WEBADMIN_EXPORT_PATH)
|
||||
_WEBADMIN_EXPORT_MODULE = importlib.util.module_from_spec(_WEBADMIN_EXPORT_SPEC)
|
||||
_WEBADMIN_EXPORT_SPEC.loader.exec_module(_WEBADMIN_EXPORT_MODULE)
|
||||
|
||||
|
||||
def create_auth_gateway_scenario():
|
||||
local_password = AuthMethod.objects.create(display_name='Local Password', auth_type='local_password')
|
||||
totp_global = AuthMethod.objects.create(display_name='TOTP Global', auth_type='totp', totp_secret=FIXTURE_GLOBAL_TOTP_SECRET)
|
||||
ip_allowlist = AuthMethod.objects.create(display_name='IP Allowlist', auth_type='ip_address')
|
||||
ip_empty = AuthMethod.objects.create(display_name='IP Empty', auth_type='ip_address')
|
||||
oidc_company = AuthMethod.objects.create(
|
||||
display_name='OIDC Company', auth_type='oidc', session_expiration_minutes=2880,
|
||||
oidc_provider='https://idp.example.test', oidc_client_id='fixture-client', oidc_client_secret='fixture-client-secret',
|
||||
)
|
||||
|
||||
for address, prefix_length, action in (
|
||||
('10.10.0.1', None, 'allow'),
|
||||
('10.20.0.0', 24, 'allow'),
|
||||
('10.20.0.12', None, 'deny'),
|
||||
('10.30.0.15', None, 'allow'),
|
||||
):
|
||||
GatekeeperIPAddress.objects.create(auth_method=ip_allowlist, address=address, prefix_length=prefix_length, action=action)
|
||||
|
||||
AuthMethodAllowedDomain.objects.create(auth_method=oidc_company, domain='example.test')
|
||||
AuthMethodAllowedEmail.objects.create(auth_method=oidc_company, email='contractor@partner.example.test')
|
||||
|
||||
alice = GatekeeperUser.objects.create(username='alice', email='alice@example.test', password=FIXTURE_PASSWORD_HASH)
|
||||
bob = GatekeeperUser.objects.create(username='bob', password=FIXTURE_PASSWORD_HASH, totp_secret=FIXTURE_USER_TOTP_SECRET)
|
||||
staff_group = GatekeeperGroup.objects.create(display_name='Staff')
|
||||
staff_group.users.add(alice, bob)
|
||||
|
||||
public_policy = AccessPolicy.objects.get(policy_type='public')
|
||||
password_totp_policy = AccessPolicy.objects.create(display_name='Password TOTP', policy_type='protected')
|
||||
password_totp_policy.groups.add(staff_group)
|
||||
password_totp_policy.methods.add(local_password, totp_global)
|
||||
staff_password_policy = AccessPolicy.objects.create(display_name='Staff Password', policy_type='protected')
|
||||
staff_password_policy.groups.add(staff_group)
|
||||
staff_password_policy.methods.add(local_password)
|
||||
totp_only_policy = AccessPolicy.objects.create(display_name='TOTP Only', policy_type='protected')
|
||||
totp_only_policy.methods.add(totp_global)
|
||||
trusted_ips_policy = AccessPolicy.objects.create(display_name='Trusted IPs', policy_type='protected')
|
||||
trusted_ips_policy.methods.add(ip_allowlist)
|
||||
empty_ip_list_policy = AccessPolicy.objects.create(display_name='Empty IP List', policy_type='protected')
|
||||
empty_ip_list_policy.methods.add(ip_empty)
|
||||
oidc_company_policy = AccessPolicy.objects.create(display_name='OIDC Company', policy_type='protected')
|
||||
oidc_company_policy.methods.add(oidc_company)
|
||||
|
||||
dashboard = Application.objects.create(display_name='Dashboard', upstream='http://10.0.0.10:3000')
|
||||
ApplicationHost.objects.create(application=dashboard, hostname='dashboard.example.test')
|
||||
ApplicationPolicy.objects.create(application=dashboard, default_policy=staff_password_policy)
|
||||
ApplicationRoute.objects.create(application=dashboard, display_name='Admin', path_prefix='/admin', policy=password_totp_policy, order=0)
|
||||
ApplicationRoute.objects.create(application=dashboard, display_name='API', path_prefix='/api', policy=trusted_ips_policy, order=1)
|
||||
|
||||
media = Application.objects.create(display_name='Media', upstream='http://10.0.0.11:32400')
|
||||
ApplicationHost.objects.create(application=media, hostname='media.example.test')
|
||||
ApplicationHost.objects.create(application=media, hostname='media2.example.test')
|
||||
ApplicationPolicy.objects.create(application=media, default_policy=public_policy)
|
||||
|
||||
hypervisor = Application.objects.create(display_name='Hypervisor', upstream='https://10.0.0.12:8006', allow_invalid_cert=True)
|
||||
ApplicationHost.objects.create(application=hypervisor, hostname='hypervisor.example.test')
|
||||
ApplicationPolicy.objects.create(application=hypervisor, default_policy=trusted_ips_policy)
|
||||
|
||||
files = Application.objects.create(display_name='Files', upstream='http://10.0.0.13:8080')
|
||||
ApplicationPolicy.objects.create(application=files, default_policy=totp_only_policy)
|
||||
|
||||
Application.objects.create(display_name='Café', upstream='http://10.0.0.14:8000')
|
||||
|
||||
sso = Application.objects.create(display_name='SSO', upstream='http://10.0.0.15:8000')
|
||||
ApplicationHost.objects.create(application=sso, hostname='sso.example.test')
|
||||
ApplicationPolicy.objects.create(application=sso, default_policy=oidc_company_policy)
|
||||
|
||||
|
||||
def export_auth_gateway_fixtures(output_dir):
|
||||
export_caddy_config(output_dir)
|
||||
|
||||
fixture_environment = {'SERVER_ADDRESS': 'webadmin.example.test', 'EXTRA_ALLOWED_HOSTS': 'webadmin2.example.test,10.0.0.1'}
|
||||
with patch.dict(os.environ, fixture_environment), contextlib.redirect_stdout(io.StringIO()):
|
||||
webadmin_config = _WEBADMIN_EXPORT_MODULE.build_config(_WEBADMIN_EXPORT_MODULE.collect_hosts())
|
||||
_WEBADMIN_EXPORT_MODULE._write_config(os.path.join(output_dir, 'wireguard_webadmin.json'), webadmin_config)
|
||||
|
||||
|
||||
class AuthGatewayFixturesTests(TestCase):
|
||||
maxDiff = None
|
||||
|
||||
def setUp(self):
|
||||
create_auth_gateway_scenario()
|
||||
|
||||
def test_auth_gateway_fixtures_match_exporter(self):
|
||||
if os.environ.get(UPDATE_FIXTURES_ENV) == '1':
|
||||
export_auth_gateway_fixtures(AUTH_GATEWAY_FIXTURES_DIR)
|
||||
return
|
||||
|
||||
with tempfile.TemporaryDirectory() as output_dir:
|
||||
export_auth_gateway_fixtures(output_dir)
|
||||
generated_files = sorted(path.name for path in Path(output_dir).glob('*.json'))
|
||||
fixture_files = sorted(path.name for path in AUTH_GATEWAY_FIXTURES_DIR.glob('*.json'))
|
||||
self.assertEqual(generated_files, fixture_files)
|
||||
|
||||
for filename in generated_files:
|
||||
generated_data = json.loads((Path(output_dir) / filename).read_text(encoding='utf-8'))
|
||||
fixture_data = json.loads((AUTH_GATEWAY_FIXTURES_DIR / filename).read_text(encoding='utf-8'))
|
||||
self.assertEqual(
|
||||
generated_data, fixture_data,
|
||||
f"{filename} is out of date. Regenerate it with: "
|
||||
f"{UPDATE_FIXTURES_ENV}=1 python manage.py test app_gateway"
|
||||
)
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user