2020-09-16 20:54:35 +01:00
|
|
|
"""Proxy and Outpost e2e tests"""
|
2020-10-17 16:03:10 +01:00
|
|
|
from dataclasses import asdict
|
2020-09-16 22:31:16 +01:00
|
|
|
from sys import platform
|
2020-09-16 20:54:35 +01:00
|
|
|
from time import sleep
|
|
|
|
from typing import Any, Dict, Optional
|
2020-09-16 22:31:16 +01:00
|
|
|
from unittest.case import skipUnless
|
2020-09-16 20:54:35 +01:00
|
|
|
|
2020-09-28 10:42:27 +01:00
|
|
|
from channels.testing import ChannelsLiveServerTestCase
|
2020-09-16 20:54:35 +01:00
|
|
|
from docker.client import DockerClient, from_env
|
|
|
|
from docker.models.containers import Container
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.common.keys import Keys
|
|
|
|
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik import __version__
|
|
|
|
from authentik.core.models import Application
|
|
|
|
from authentik.flows.models import Flow
|
|
|
|
from authentik.outposts.apps import AuthentikOutpostConfig
|
|
|
|
from authentik.outposts.models import (
|
2020-11-04 09:54:44 +00:00
|
|
|
DockerServiceConnection,
|
2020-10-17 16:03:10 +01:00
|
|
|
Outpost,
|
|
|
|
OutpostConfig,
|
|
|
|
OutpostType,
|
|
|
|
)
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.providers.proxy.models import ProxyProvider
|
2020-11-19 13:25:53 +00:00
|
|
|
from tests.e2e.utils import USER, SeleniumTestCase, retry
|
2020-09-16 20:54:35 +01:00
|
|
|
|
|
|
|
|
2020-09-16 22:31:16 +01:00
|
|
|
@skipUnless(platform.startswith("linux"), "requires local docker")
|
2020-09-16 20:54:35 +01:00
|
|
|
class TestProviderProxy(SeleniumTestCase):
|
|
|
|
"""Proxy and Outpost e2e tests"""
|
|
|
|
|
|
|
|
proxy_container: Container
|
|
|
|
|
|
|
|
def tearDown(self) -> None:
|
|
|
|
super().tearDown()
|
|
|
|
self.proxy_container.kill()
|
|
|
|
|
|
|
|
def get_container_specs(self) -> Optional[Dict[str, Any]]:
|
|
|
|
return {
|
2020-10-26 23:34:30 +00:00
|
|
|
"image": "docker.beryju.org/proxy/traefik/whoami:latest",
|
2020-09-16 20:54:35 +01:00
|
|
|
"detach": True,
|
|
|
|
"network_mode": "host",
|
|
|
|
"auto_remove": True,
|
|
|
|
}
|
|
|
|
|
|
|
|
def start_proxy(self, outpost: Outpost) -> Container:
|
|
|
|
"""Start proxy container based on outpost created"""
|
|
|
|
client: DockerClient = from_env()
|
|
|
|
container = client.containers.run(
|
2020-12-05 21:08:42 +00:00
|
|
|
image=f"docker.beryju.org/proxy/beryju/authentik-proxy:{__version__}",
|
2020-09-16 20:54:35 +01:00
|
|
|
detach=True,
|
|
|
|
network_mode="host",
|
|
|
|
auto_remove=True,
|
|
|
|
environment={
|
2020-12-05 21:08:42 +00:00
|
|
|
"AUTHENTIK_HOST": self.live_server_url,
|
|
|
|
"AUTHENTIK_TOKEN": outpost.token.key,
|
2020-09-16 20:54:35 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
return container
|
|
|
|
|
2020-10-20 17:42:26 +01:00
|
|
|
@retry()
|
2020-09-16 20:54:35 +01:00
|
|
|
def test_proxy_simple(self):
|
|
|
|
"""Test simple outpost setup with single provider"""
|
|
|
|
proxy: ProxyProvider = ProxyProvider.objects.create(
|
|
|
|
name="proxy_provider",
|
|
|
|
authorization_flow=Flow.objects.get(
|
|
|
|
slug="default-provider-authorization-implicit-consent"
|
|
|
|
),
|
|
|
|
internal_host="http://localhost:80",
|
|
|
|
external_host="http://localhost:4180",
|
|
|
|
)
|
|
|
|
# Ensure OAuth2 Params are set
|
|
|
|
proxy.set_oauth_defaults()
|
|
|
|
proxy.save()
|
|
|
|
# we need to create an application to actually access the proxy
|
|
|
|
Application.objects.create(name="proxy", slug="proxy", provider=proxy)
|
|
|
|
outpost: Outpost = Outpost.objects.create(
|
|
|
|
name="proxy_outpost",
|
|
|
|
type=OutpostType.PROXY,
|
|
|
|
)
|
|
|
|
outpost.providers.add(proxy)
|
|
|
|
outpost.save()
|
|
|
|
|
|
|
|
self.proxy_container = self.start_proxy(outpost)
|
|
|
|
|
|
|
|
# Wait until outpost healthcheck succeeds
|
|
|
|
healthcheck_retries = 0
|
|
|
|
while healthcheck_retries < 50:
|
2020-10-14 11:15:40 +01:00
|
|
|
if len(outpost.state) > 0:
|
|
|
|
state = outpost.state[0]
|
|
|
|
if state.last_seen:
|
|
|
|
break
|
2020-09-16 20:54:35 +01:00
|
|
|
healthcheck_retries += 1
|
|
|
|
sleep(0.5)
|
|
|
|
|
|
|
|
self.driver.get("http://localhost:4180")
|
|
|
|
|
|
|
|
self.driver.find_element(By.ID, "id_uid_field").click()
|
|
|
|
self.driver.find_element(By.ID, "id_uid_field").send_keys(USER().username)
|
|
|
|
self.driver.find_element(By.ID, "id_uid_field").send_keys(Keys.ENTER)
|
|
|
|
self.driver.find_element(By.ID, "id_password").send_keys(USER().username)
|
|
|
|
self.driver.find_element(By.ID, "id_password").send_keys(Keys.ENTER)
|
|
|
|
|
|
|
|
sleep(1)
|
|
|
|
|
|
|
|
full_body_text = self.driver.find_element(By.CSS_SELECTOR, "pre").text
|
2020-12-05 21:08:42 +00:00
|
|
|
self.assertIn("X-Forwarded-Preferred-Username: akadmin", full_body_text)
|
2020-09-28 08:04:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
@skipUnless(platform.startswith("linux"), "requires local docker")
|
|
|
|
class TestProviderProxyConnect(ChannelsLiveServerTestCase):
|
|
|
|
"""Test Proxy connectivity over websockets"""
|
|
|
|
|
2020-10-20 17:42:26 +01:00
|
|
|
@retry()
|
2020-09-28 08:04:44 +01:00
|
|
|
def test_proxy_connectivity(self):
|
2020-09-28 10:42:27 +01:00
|
|
|
"""Test proxy connectivity over websocket"""
|
2020-12-05 21:08:42 +00:00
|
|
|
AuthentikOutpostConfig.init_local_connection()
|
2020-09-28 08:04:44 +01:00
|
|
|
SeleniumTestCase().apply_default_data()
|
|
|
|
proxy: ProxyProvider = ProxyProvider.objects.create(
|
|
|
|
name="proxy_provider",
|
|
|
|
authorization_flow=Flow.objects.get(
|
|
|
|
slug="default-provider-authorization-implicit-consent"
|
|
|
|
),
|
|
|
|
internal_host="http://localhost:80",
|
|
|
|
external_host="http://localhost:4180",
|
|
|
|
)
|
|
|
|
# Ensure OAuth2 Params are set
|
|
|
|
proxy.set_oauth_defaults()
|
|
|
|
proxy.save()
|
|
|
|
# we need to create an application to actually access the proxy
|
|
|
|
Application.objects.create(name="proxy", slug="proxy", provider=proxy)
|
2020-11-04 09:54:44 +00:00
|
|
|
service_connection = DockerServiceConnection.objects.get(local=True)
|
2020-09-28 08:04:44 +01:00
|
|
|
outpost: Outpost = Outpost.objects.create(
|
|
|
|
name="proxy_outpost",
|
|
|
|
type=OutpostType.PROXY,
|
2020-11-04 09:54:44 +00:00
|
|
|
service_connection=service_connection,
|
2020-10-17 16:03:10 +01:00
|
|
|
_config=asdict(
|
2020-12-05 21:08:42 +00:00
|
|
|
OutpostConfig(authentik_host=self.live_server_url, log_level="debug")
|
2020-10-17 16:03:10 +01:00
|
|
|
),
|
2020-09-28 08:04:44 +01:00
|
|
|
)
|
|
|
|
outpost.providers.add(proxy)
|
|
|
|
outpost.save()
|
|
|
|
|
|
|
|
# Wait until outpost healthcheck succeeds
|
|
|
|
healthcheck_retries = 0
|
|
|
|
while healthcheck_retries < 50:
|
2020-10-14 11:15:40 +01:00
|
|
|
if len(outpost.state) > 0:
|
|
|
|
state = outpost.state[0]
|
|
|
|
if state.last_seen and state.version:
|
|
|
|
break
|
2020-09-28 08:04:44 +01:00
|
|
|
healthcheck_retries += 1
|
|
|
|
sleep(0.5)
|
|
|
|
|
2020-10-14 11:15:40 +01:00
|
|
|
state = outpost.state
|
2020-11-25 11:41:13 +00:00
|
|
|
self.assertEqual(len(state), 1)
|
2020-10-14 11:15:40 +01:00
|
|
|
self.assertEqual(state[0].version, __version__)
|
2020-10-17 16:03:10 +01:00
|
|
|
|
|
|
|
# Make sure to delete the outpost to remove the container
|
|
|
|
outpost.delete()
|