2020-09-29 08:32:41 +00:00
|
|
|
"""Test Controllers"""
|
2021-07-28 14:08:06 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
2020-09-29 08:32:41 +00:00
|
|
|
import yaml
|
|
|
|
from django.test import TestCase
|
2021-07-28 14:08:06 +00:00
|
|
|
from structlog.stdlib import get_logger
|
2020-09-29 08:32:41 +00:00
|
|
|
|
2021-11-22 21:56:02 +00:00
|
|
|
from authentik.core.tests.utils import create_test_flow
|
2021-07-28 14:08:06 +00:00
|
|
|
from authentik.outposts.controllers.kubernetes import KubernetesController
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.outposts.models import KubernetesServiceConnection, Outpost, OutpostType
|
2023-03-15 11:12:08 +00:00
|
|
|
from authentik.outposts.tasks import outpost_connection_discovery
|
2021-07-28 14:08:06 +00:00
|
|
|
from authentik.providers.proxy.controllers.k8s.ingress import IngressReconciler
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.providers.proxy.controllers.kubernetes import ProxyKubernetesController
|
2021-07-28 14:08:06 +00:00
|
|
|
from authentik.providers.proxy.models import ProxyMode, ProxyProvider
|
|
|
|
|
|
|
|
LOGGER = get_logger()
|
2020-09-29 08:32:41 +00:00
|
|
|
|
|
|
|
|
2021-03-05 18:09:13 +00:00
|
|
|
class TestProxyKubernetes(TestCase):
|
2020-09-29 08:32:41 +00:00
|
|
|
"""Test Controllers"""
|
|
|
|
|
2021-07-28 14:08:06 +00:00
|
|
|
controller: Optional[KubernetesController]
|
|
|
|
|
2020-11-19 13:25:53 +00:00
|
|
|
def setUp(self):
|
|
|
|
# Ensure that local connection have been created
|
2023-03-15 11:12:08 +00:00
|
|
|
outpost_connection_discovery() # pylint: disable=no-value-for-parameter
|
2021-07-28 14:08:06 +00:00
|
|
|
self.controller = None
|
|
|
|
|
|
|
|
def tearDown(self) -> None:
|
|
|
|
if self.controller:
|
|
|
|
for log in self.controller.down_with_logs():
|
|
|
|
LOGGER.info(log)
|
|
|
|
return super().tearDown()
|
2020-11-19 13:25:53 +00:00
|
|
|
|
2020-10-14 18:21:47 +00:00
|
|
|
def test_kubernetes_controller_static(self):
|
2020-09-29 08:32:41 +00:00
|
|
|
"""Test Kubernetes Controller"""
|
|
|
|
provider: ProxyProvider = ProxyProvider.objects.create(
|
|
|
|
name="test",
|
|
|
|
internal_host="http://localhost",
|
|
|
|
external_host="http://localhost",
|
2021-11-22 21:56:02 +00:00
|
|
|
authorization_flow=create_test_flow(),
|
2020-09-29 08:32:41 +00:00
|
|
|
)
|
2020-11-04 13:02:29 +00:00
|
|
|
service_connection = KubernetesServiceConnection.objects.first()
|
2020-09-29 08:32:41 +00:00
|
|
|
outpost: Outpost = Outpost.objects.create(
|
|
|
|
name="test",
|
|
|
|
type=OutpostType.PROXY,
|
2020-11-04 09:54:44 +00:00
|
|
|
service_connection=service_connection,
|
2020-09-29 08:32:41 +00:00
|
|
|
)
|
|
|
|
outpost.providers.add(provider)
|
|
|
|
outpost.save()
|
|
|
|
|
2021-07-28 14:08:06 +00:00
|
|
|
self.controller = ProxyKubernetesController(outpost, service_connection)
|
|
|
|
manifest = self.controller.get_static_deployment()
|
2020-10-19 12:55:25 +00:00
|
|
|
self.assertEqual(len(list(yaml.load_all(manifest, Loader=yaml.SafeLoader))), 4)
|
2020-10-14 18:21:47 +00:00
|
|
|
|
2021-07-28 14:08:06 +00:00
|
|
|
def test_kubernetes_controller_ingress(self):
|
|
|
|
"""Test Kubernetes Controller's Ingress"""
|
2020-10-14 18:21:47 +00:00
|
|
|
provider: ProxyProvider = ProxyProvider.objects.create(
|
|
|
|
name="test",
|
|
|
|
internal_host="http://localhost",
|
2021-07-28 14:08:06 +00:00
|
|
|
external_host="https://localhost",
|
2021-11-22 21:56:02 +00:00
|
|
|
authorization_flow=create_test_flow(),
|
2021-07-28 14:08:06 +00:00
|
|
|
)
|
|
|
|
provider2: ProxyProvider = ProxyProvider.objects.create(
|
|
|
|
name="test2",
|
|
|
|
internal_host="http://otherhost",
|
|
|
|
external_host="https://otherhost",
|
|
|
|
mode=ProxyMode.FORWARD_SINGLE,
|
2021-11-22 21:56:02 +00:00
|
|
|
authorization_flow=create_test_flow(),
|
2020-10-14 18:21:47 +00:00
|
|
|
)
|
2021-07-28 14:08:06 +00:00
|
|
|
|
2020-11-04 13:02:29 +00:00
|
|
|
service_connection = KubernetesServiceConnection.objects.first()
|
2020-10-14 18:21:47 +00:00
|
|
|
outpost: Outpost = Outpost.objects.create(
|
|
|
|
name="test",
|
|
|
|
type=OutpostType.PROXY,
|
2020-11-04 09:54:44 +00:00
|
|
|
service_connection=service_connection,
|
2020-10-14 18:21:47 +00:00
|
|
|
)
|
|
|
|
outpost.providers.add(provider)
|
|
|
|
|
2021-07-28 14:08:06 +00:00
|
|
|
self.controller = ProxyKubernetesController(outpost, service_connection)
|
|
|
|
|
|
|
|
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")
|