providers/proxy: fix hosts for ingress not being compared correctly

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-07-28 16:08:06 +02:00
parent affafc31cf
commit a3981dd3cd
2 changed files with 48 additions and 13 deletions

View File

@ -60,12 +60,12 @@ class IngressReconciler(KubernetesObjectReconciler[NetworkingV1beta1Ingress]):
expected_hosts.sort() expected_hosts.sort()
expected_hosts_tls.sort() expected_hosts_tls.sort()
have_hosts = [rule.host for rule in reference.spec.rules] have_hosts = [rule.host for rule in current.spec.rules]
have_hosts.sort() have_hosts.sort()
have_hosts_tls = [] have_hosts_tls = []
for tls_config in reference.spec.tls: for tls_config in current.spec.tls:
if tls_config: if tls_config and tls_config.hosts:
have_hosts_tls += tls_config.hosts have_hosts_tls += tls_config.hosts
have_hosts_tls.sort() have_hosts_tls.sort()

View File

@ -1,20 +1,36 @@
"""Test Controllers""" """Test Controllers"""
from typing import Optional
import yaml import yaml
from django.test import TestCase from django.test import TestCase
from structlog.stdlib import get_logger
from authentik.flows.models import Flow from authentik.flows.models import Flow
from authentik.outposts.controllers.kubernetes import KubernetesController
from authentik.outposts.models import KubernetesServiceConnection, Outpost, OutpostType from authentik.outposts.models import KubernetesServiceConnection, Outpost, OutpostType
from authentik.outposts.tasks import outpost_local_connection from authentik.outposts.tasks import outpost_local_connection
from authentik.providers.proxy.controllers.k8s.ingress import IngressReconciler
from authentik.providers.proxy.controllers.kubernetes import ProxyKubernetesController from authentik.providers.proxy.controllers.kubernetes import ProxyKubernetesController
from authentik.providers.proxy.models import ProxyProvider from authentik.providers.proxy.models import ProxyMode, ProxyProvider
LOGGER = get_logger()
class TestProxyKubernetes(TestCase): class TestProxyKubernetes(TestCase):
"""Test Controllers""" """Test Controllers"""
controller: Optional[KubernetesController]
def setUp(self): def setUp(self):
# Ensure that local connection have been created # Ensure that local connection have been created
outpost_local_connection() outpost_local_connection()
self.controller = None
def tearDown(self) -> None:
if self.controller:
for log in self.controller.down_with_logs():
LOGGER.info(log)
return super().tearDown()
def test_kubernetes_controller_static(self): def test_kubernetes_controller_static(self):
"""Test Kubernetes Controller""" """Test Kubernetes Controller"""
@ -33,18 +49,26 @@ class TestProxyKubernetes(TestCase):
outpost.providers.add(provider) outpost.providers.add(provider)
outpost.save() outpost.save()
controller = ProxyKubernetesController(outpost, service_connection) self.controller = ProxyKubernetesController(outpost, service_connection)
manifest = controller.get_static_deployment() manifest = self.controller.get_static_deployment()
self.assertEqual(len(list(yaml.load_all(manifest, Loader=yaml.SafeLoader))), 4) self.assertEqual(len(list(yaml.load_all(manifest, Loader=yaml.SafeLoader))), 4)
def test_kubernetes_controller_deploy(self): def test_kubernetes_controller_ingress(self):
"""Test Kubernetes Controller""" """Test Kubernetes Controller's Ingress"""
provider: ProxyProvider = ProxyProvider.objects.create( provider: ProxyProvider = ProxyProvider.objects.create(
name="test", name="test",
internal_host="http://localhost", internal_host="http://localhost",
external_host="http://localhost", external_host="https://localhost",
authorization_flow=Flow.objects.first(), authorization_flow=Flow.objects.first(),
) )
provider2: ProxyProvider = ProxyProvider.objects.create(
name="test2",
internal_host="http://otherhost",
external_host="https://otherhost",
mode=ProxyMode.FORWARD_SINGLE,
authorization_flow=Flow.objects.first(),
)
service_connection = KubernetesServiceConnection.objects.first() service_connection = KubernetesServiceConnection.objects.first()
outpost: Outpost = Outpost.objects.create( outpost: Outpost = Outpost.objects.create(
name="test", name="test",
@ -52,8 +76,19 @@ class TestProxyKubernetes(TestCase):
service_connection=service_connection, service_connection=service_connection,
) )
outpost.providers.add(provider) outpost.providers.add(provider)
outpost.save()
controller = ProxyKubernetesController(outpost, service_connection) self.controller = ProxyKubernetesController(outpost, service_connection)
controller.up()
controller.down() ingress_rec = IngressReconciler(self.controller)
ingress = ingress_rec.retrieve()
self.assertEqual(len(ingress.spec.rules), 1)
self.assertEqual(ingress.spec.rules[0].host, "localhost")
# add provider, check again
outpost.providers.add(provider2)
ingress = ingress_rec.retrieve()
self.assertEqual(len(ingress.spec.rules), 2)
self.assertEqual(ingress.spec.rules[0].host, "localhost")
self.assertEqual(ingress.spec.rules[1].host, "otherhost")