2020-09-16 19:54:35 +00:00
|
|
|
"""Proxy and Outpost e2e tests"""
|
2023-01-04 21:04:16 +00:00
|
|
|
from base64 import b64encode
|
2020-10-17 15:03:10 +00:00
|
|
|
from dataclasses import asdict
|
2020-09-16 21:31:16 +00:00
|
|
|
from sys import platform
|
2020-09-16 19:54:35 +00:00
|
|
|
from time import sleep
|
2021-02-18 12:41:03 +00:00
|
|
|
from typing import Any, Optional
|
2020-09-16 21:31:16 +00:00
|
|
|
from unittest.case import skipUnless
|
2020-09-16 19:54:35 +00:00
|
|
|
|
2020-09-28 09:42:27 +00:00
|
|
|
from channels.testing import ChannelsLiveServerTestCase
|
2020-09-16 19:54:35 +00:00
|
|
|
from docker.client import DockerClient, from_env
|
|
|
|
from docker.models.containers import Container
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
|
2022-08-02 22:05:49 +00:00
|
|
|
from authentik.blueprints.tests import apply_blueprint, reconcile_app
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.core.models import Application
|
|
|
|
from authentik.flows.models import Flow
|
2023-01-04 21:04:16 +00:00
|
|
|
from authentik.lib.generators import generate_id
|
2021-08-03 15:45:16 +00:00
|
|
|
from authentik.outposts.models import DockerServiceConnection, Outpost, OutpostConfig, OutpostType
|
2023-03-15 11:12:08 +00:00
|
|
|
from authentik.outposts.tasks import outpost_connection_discovery
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.providers.proxy.models import ProxyProvider
|
2022-08-02 22:05:49 +00:00
|
|
|
from tests.e2e.utils import SeleniumTestCase, retry
|
2020-09-16 19:54:35 +00:00
|
|
|
|
|
|
|
|
2020-09-16 21:31:16 +00:00
|
|
|
@skipUnless(platform.startswith("linux"), "requires local docker")
|
2020-09-16 19:54:35 +00:00
|
|
|
class TestProviderProxy(SeleniumTestCase):
|
|
|
|
"""Proxy and Outpost e2e tests"""
|
|
|
|
|
|
|
|
proxy_container: Container
|
|
|
|
|
|
|
|
def tearDown(self) -> None:
|
|
|
|
super().tearDown()
|
2021-06-03 20:17:18 +00:00
|
|
|
self.output_container_logs(self.proxy_container)
|
2020-09-16 19:54:35 +00:00
|
|
|
self.proxy_container.kill()
|
|
|
|
|
2021-02-18 12:41:03 +00:00
|
|
|
def get_container_specs(self) -> Optional[dict[str, Any]]:
|
2020-09-16 19:54:35 +00:00
|
|
|
return {
|
2021-02-03 20:18:31 +00:00
|
|
|
"image": "traefik/whoami:latest",
|
2020-09-16 19:54:35 +00: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(
|
2021-12-10 20:11:37 +00:00
|
|
|
image=self.get_container_image("ghcr.io/goauthentik/dev-proxy"),
|
2020-09-16 19:54:35 +00:00
|
|
|
detach=True,
|
|
|
|
network_mode="host",
|
|
|
|
environment={
|
2020-12-05 21:08:42 +00:00
|
|
|
"AUTHENTIK_HOST": self.live_server_url,
|
|
|
|
"AUTHENTIK_TOKEN": outpost.token.key,
|
2020-09-16 19:54:35 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
return container
|
|
|
|
|
2020-10-20 16:42:26 +00:00
|
|
|
@retry()
|
2022-08-01 21:05:58 +00:00
|
|
|
@apply_blueprint(
|
2023-01-24 11:23:22 +00:00
|
|
|
"default/flow-default-authentication-flow.yaml",
|
|
|
|
"default/flow-default-invalidation-flow.yaml",
|
2022-08-01 21:05:58 +00:00
|
|
|
)
|
|
|
|
@apply_blueprint(
|
2023-01-24 11:23:22 +00:00
|
|
|
"default/flow-default-provider-authorization-explicit-consent.yaml",
|
|
|
|
"default/flow-default-provider-authorization-implicit-consent.yaml",
|
2022-08-01 21:05:58 +00:00
|
|
|
)
|
|
|
|
@apply_blueprint(
|
2022-08-16 14:12:21 +00:00
|
|
|
"system/providers-oauth2.yaml",
|
|
|
|
"system/providers-proxy.yaml",
|
2022-08-01 21:05:58 +00:00
|
|
|
)
|
|
|
|
@reconcile_app("authentik_crypto")
|
2020-09-16 19:54:35 +00:00
|
|
|
def test_proxy_simple(self):
|
|
|
|
"""Test simple outpost setup with single provider"""
|
2021-06-18 16:42:03 +00:00
|
|
|
# set additionalHeaders to test later
|
2021-11-23 20:30:02 +00:00
|
|
|
self.user.attributes["additionalHeaders"] = {"X-Foo": "bar"}
|
|
|
|
self.user.save()
|
2021-06-18 16:42:03 +00:00
|
|
|
|
2020-09-16 19:54:35 +00:00
|
|
|
proxy: ProxyProvider = ProxyProvider.objects.create(
|
|
|
|
name="proxy_provider",
|
|
|
|
authorization_flow=Flow.objects.get(
|
|
|
|
slug="default-provider-authorization-implicit-consent"
|
|
|
|
),
|
2021-05-13 11:09:30 +00:00
|
|
|
internal_host="http://localhost",
|
2021-09-08 18:04:56 +00:00
|
|
|
external_host="http://localhost:9000",
|
2020-09-16 19:54:35 +00:00
|
|
|
)
|
|
|
|
# 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)
|
2021-10-04 16:04:19 +00:00
|
|
|
outpost.build_user_permissions(outpost.user)
|
2020-09-16 19:54:35 +00:00
|
|
|
|
|
|
|
self.proxy_container = self.start_proxy(outpost)
|
|
|
|
|
2021-06-03 20:17:18 +00:00
|
|
|
# Wait until outpost healthcheck succeeds
|
|
|
|
healthcheck_retries = 0
|
|
|
|
while healthcheck_retries < 50:
|
|
|
|
if len(outpost.state) > 0:
|
|
|
|
state = outpost.state[0]
|
|
|
|
if state.last_seen:
|
|
|
|
break
|
|
|
|
healthcheck_retries += 1
|
|
|
|
sleep(0.5)
|
2021-11-24 08:18:22 +00:00
|
|
|
sleep(5)
|
2021-06-03 20:17:18 +00:00
|
|
|
|
2021-09-08 18:04:56 +00:00
|
|
|
self.driver.get("http://localhost:9000")
|
2021-02-27 17:48:41 +00:00
|
|
|
self.login()
|
2020-09-16 19:54:35 +00:00
|
|
|
sleep(1)
|
|
|
|
|
|
|
|
full_body_text = self.driver.find_element(By.CSS_SELECTOR, "pre").text
|
2021-11-24 08:18:22 +00:00
|
|
|
self.assertIn(f"X-Authentik-Username: {self.user.username}", full_body_text)
|
2021-06-18 16:42:03 +00:00
|
|
|
self.assertIn("X-Foo: bar", full_body_text)
|
2020-09-28 07:04:44 +00:00
|
|
|
|
2022-02-08 19:25:38 +00:00
|
|
|
self.driver.get("http://localhost:9000/outpost.goauthentik.io/sign_out")
|
2021-07-01 13:48:56 +00:00
|
|
|
sleep(2)
|
2021-08-03 15:45:16 +00:00
|
|
|
full_body_text = self.driver.find_element(By.CSS_SELECTOR, ".pf-c-title.pf-m-3xl").text
|
2021-07-01 13:48:56 +00:00
|
|
|
self.assertIn("You've logged out of proxy.", full_body_text)
|
|
|
|
|
2023-01-04 21:04:16 +00:00
|
|
|
@retry()
|
|
|
|
@apply_blueprint(
|
2023-01-24 11:23:22 +00:00
|
|
|
"default/flow-default-authentication-flow.yaml",
|
|
|
|
"default/flow-default-invalidation-flow.yaml",
|
2023-01-04 21:04:16 +00:00
|
|
|
)
|
|
|
|
@apply_blueprint(
|
2023-01-24 11:23:22 +00:00
|
|
|
"default/flow-default-provider-authorization-explicit-consent.yaml",
|
|
|
|
"default/flow-default-provider-authorization-implicit-consent.yaml",
|
2023-01-04 21:04:16 +00:00
|
|
|
)
|
|
|
|
@apply_blueprint(
|
|
|
|
"system/providers-oauth2.yaml",
|
|
|
|
"system/providers-proxy.yaml",
|
|
|
|
)
|
|
|
|
@reconcile_app("authentik_crypto")
|
|
|
|
def test_proxy_basic_auth(self):
|
|
|
|
"""Test simple outpost setup with single provider"""
|
|
|
|
cred = generate_id()
|
|
|
|
attr = "basic-password" # nosec
|
|
|
|
self.user.attributes["basic-username"] = cred
|
|
|
|
self.user.attributes[attr] = cred
|
|
|
|
self.user.save()
|
|
|
|
|
|
|
|
proxy: ProxyProvider = ProxyProvider.objects.create(
|
|
|
|
name="proxy_provider",
|
|
|
|
authorization_flow=Flow.objects.get(
|
|
|
|
slug="default-provider-authorization-implicit-consent"
|
|
|
|
),
|
|
|
|
internal_host="http://localhost",
|
|
|
|
external_host="http://localhost:9000",
|
|
|
|
basic_auth_enabled=True,
|
|
|
|
basic_auth_user_attribute="basic-username",
|
|
|
|
basic_auth_password_attribute=attr,
|
|
|
|
)
|
|
|
|
# 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.build_user_permissions(outpost.user)
|
|
|
|
|
|
|
|
self.proxy_container = self.start_proxy(outpost)
|
|
|
|
|
|
|
|
# Wait until outpost healthcheck succeeds
|
|
|
|
healthcheck_retries = 0
|
|
|
|
while healthcheck_retries < 50:
|
|
|
|
if len(outpost.state) > 0:
|
|
|
|
state = outpost.state[0]
|
|
|
|
if state.last_seen:
|
|
|
|
break
|
|
|
|
healthcheck_retries += 1
|
|
|
|
sleep(0.5)
|
|
|
|
sleep(5)
|
|
|
|
|
|
|
|
self.driver.get("http://localhost:9000")
|
|
|
|
self.login()
|
|
|
|
sleep(1)
|
|
|
|
|
|
|
|
full_body_text = self.driver.find_element(By.CSS_SELECTOR, "pre").text
|
|
|
|
self.assertIn(f"X-Authentik-Username: {self.user.username}", full_body_text)
|
|
|
|
auth_header = b64encode(f"{cred}:{cred}".encode()).decode()
|
|
|
|
self.assertIn(f"Authorization: Basic {auth_header}", full_body_text)
|
|
|
|
|
|
|
|
self.driver.get("http://localhost:9000/outpost.goauthentik.io/sign_out")
|
|
|
|
sleep(2)
|
|
|
|
full_body_text = self.driver.find_element(By.CSS_SELECTOR, ".pf-c-title.pf-m-3xl").text
|
|
|
|
self.assertIn("You've logged out of proxy.", full_body_text)
|
|
|
|
|
2020-09-28 07:04:44 +00:00
|
|
|
|
|
|
|
@skipUnless(platform.startswith("linux"), "requires local docker")
|
|
|
|
class TestProviderProxyConnect(ChannelsLiveServerTestCase):
|
|
|
|
"""Test Proxy connectivity over websockets"""
|
|
|
|
|
2020-10-20 16:42:26 +00:00
|
|
|
@retry()
|
2022-08-01 21:05:58 +00:00
|
|
|
@apply_blueprint(
|
2023-01-24 11:23:22 +00:00
|
|
|
"default/flow-default-authentication-flow.yaml",
|
|
|
|
"default/flow-default-invalidation-flow.yaml",
|
2022-08-01 21:05:58 +00:00
|
|
|
)
|
|
|
|
@apply_blueprint(
|
2023-01-24 11:23:22 +00:00
|
|
|
"default/flow-default-provider-authorization-explicit-consent.yaml",
|
|
|
|
"default/flow-default-provider-authorization-implicit-consent.yaml",
|
2022-08-01 21:05:58 +00:00
|
|
|
)
|
|
|
|
@reconcile_app("authentik_crypto")
|
2020-09-28 07:04:44 +00:00
|
|
|
def test_proxy_connectivity(self):
|
2020-09-28 09:42:27 +00:00
|
|
|
"""Test proxy connectivity over websocket"""
|
2023-03-15 11:12:08 +00:00
|
|
|
outpost_connection_discovery() # pylint: disable=no-value-for-parameter
|
2020-09-28 07:04:44 +00:00
|
|
|
proxy: ProxyProvider = ProxyProvider.objects.create(
|
|
|
|
name="proxy_provider",
|
|
|
|
authorization_flow=Flow.objects.get(
|
|
|
|
slug="default-provider-authorization-implicit-consent"
|
|
|
|
),
|
2021-05-13 11:09:30 +00:00
|
|
|
internal_host="http://localhost",
|
2021-09-08 18:04:56 +00:00
|
|
|
external_host="http://localhost:9000",
|
2020-09-28 07:04:44 +00:00
|
|
|
)
|
|
|
|
# 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 07:04:44 +00:00
|
|
|
outpost: Outpost = Outpost.objects.create(
|
|
|
|
name="proxy_outpost",
|
|
|
|
type=OutpostType.PROXY,
|
2020-11-04 09:54:44 +00:00
|
|
|
service_connection=service_connection,
|
2021-08-03 15:45:16 +00:00
|
|
|
_config=asdict(OutpostConfig(authentik_host=self.live_server_url, log_level="debug")),
|
2020-09-28 07:04:44 +00:00
|
|
|
)
|
|
|
|
outpost.providers.add(proxy)
|
2021-10-04 16:04:19 +00:00
|
|
|
outpost.build_user_permissions(outpost.user)
|
2020-09-28 07:04:44 +00:00
|
|
|
|
|
|
|
# Wait until outpost healthcheck succeeds
|
|
|
|
healthcheck_retries = 0
|
|
|
|
while healthcheck_retries < 50:
|
2020-10-14 10:15:40 +00:00
|
|
|
if len(outpost.state) > 0:
|
|
|
|
state = outpost.state[0]
|
|
|
|
if state.last_seen and state.version:
|
|
|
|
break
|
2020-09-28 07:04:44 +00:00
|
|
|
healthcheck_retries += 1
|
|
|
|
sleep(0.5)
|
|
|
|
|
2020-10-14 10:15:40 +00:00
|
|
|
state = outpost.state
|
2021-12-11 22:44:49 +00:00
|
|
|
self.assertTrue(len(state) >= 1)
|
2020-10-17 15:03:10 +00:00
|
|
|
|
|
|
|
# Make sure to delete the outpost to remove the container
|
|
|
|
outpost.delete()
|