2020-12-13 20:28:13 +00:00
|
|
|
"""outpost tests"""
|
|
|
|
from shutil import rmtree
|
|
|
|
from tempfile import mkdtemp
|
|
|
|
|
2023-11-18 23:12:10 +00:00
|
|
|
import pytest
|
2021-03-05 18:09:13 +00:00
|
|
|
import yaml
|
2021-11-15 21:37:36 +00:00
|
|
|
from channels.testing import ChannelsLiveServerTestCase
|
2020-12-13 20:28:13 +00:00
|
|
|
from docker import DockerClient, from_env
|
|
|
|
from docker.models.containers import Container
|
|
|
|
from docker.types.healthcheck import Healthcheck
|
|
|
|
|
2022-09-17 11:16:53 +00:00
|
|
|
from authentik.core.tests.utils import create_test_flow
|
2020-12-13 20:28:13 +00:00
|
|
|
from authentik.crypto.models import CertificateKeyPair
|
|
|
|
from authentik.outposts.controllers.docker import DockerController
|
2021-11-15 21:37:36 +00:00
|
|
|
from authentik.outposts.models import (
|
|
|
|
DockerServiceConnection,
|
|
|
|
Outpost,
|
|
|
|
OutpostType,
|
|
|
|
default_outpost_config,
|
|
|
|
)
|
2023-03-15 11:12:08 +00:00
|
|
|
from authentik.outposts.tasks import outpost_connection_discovery
|
2020-12-13 20:28:13 +00:00
|
|
|
from authentik.providers.proxy.models import ProxyProvider
|
2023-10-12 11:57:29 +00:00
|
|
|
from tests.e2e.utils import DockerTestCase, get_docker_tag
|
2020-12-13 20:28:13 +00:00
|
|
|
|
|
|
|
|
2023-10-12 11:57:29 +00:00
|
|
|
class OutpostDockerTests(DockerTestCase, ChannelsLiveServerTestCase):
|
2020-12-13 20:28:13 +00:00
|
|
|
"""Test Docker Controllers"""
|
|
|
|
|
|
|
|
def _start_container(self, ssl_folder: str) -> Container:
|
|
|
|
client: DockerClient = from_env()
|
|
|
|
container = client.containers.run(
|
2021-02-03 20:18:31 +00:00
|
|
|
image="library/docker:dind",
|
2020-12-13 20:28:13 +00:00
|
|
|
detach=True,
|
|
|
|
network_mode="host",
|
|
|
|
privileged=True,
|
|
|
|
healthcheck=Healthcheck(
|
|
|
|
test=["CMD", "docker", "info"],
|
2023-11-21 22:50:43 +00:00
|
|
|
interval=5 * 1_000 * 1_000_000,
|
|
|
|
start_period=5 * 1_000 * 1_000_000,
|
2020-12-13 20:28:13 +00:00
|
|
|
),
|
|
|
|
environment={"DOCKER_TLS_CERTDIR": "/ssl"},
|
|
|
|
volumes={
|
|
|
|
f"{ssl_folder}/": {
|
|
|
|
"bind": "/ssl",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2023-10-12 11:57:29 +00:00
|
|
|
self.wait_for_container(container)
|
|
|
|
return container
|
2020-12-13 20:28:13 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
|
|
|
self.ssl_folder = mkdtemp()
|
|
|
|
self.container = self._start_container(self.ssl_folder)
|
|
|
|
# Ensure that local connection have been created
|
2023-03-15 11:12:08 +00:00
|
|
|
outpost_connection_discovery() # pylint: disable=no-value-for-parameter
|
2020-12-13 20:28:13 +00:00
|
|
|
self.provider: ProxyProvider = ProxyProvider.objects.create(
|
|
|
|
name="test",
|
|
|
|
internal_host="http://localhost",
|
|
|
|
external_host="http://localhost",
|
2022-09-17 11:16:53 +00:00
|
|
|
authorization_flow=create_test_flow(),
|
2020-12-13 20:28:13 +00:00
|
|
|
)
|
2023-01-15 16:02:31 +00:00
|
|
|
with (
|
|
|
|
open(f"{self.ssl_folder}/client/cert.pem", encoding="utf8") as cert,
|
|
|
|
open(f"{self.ssl_folder}/client/key.pem", encoding="utf8") as key,
|
|
|
|
):
|
|
|
|
authentication_kp = CertificateKeyPair.objects.create(
|
|
|
|
name="docker-authentication",
|
|
|
|
certificate_data=cert.read(),
|
|
|
|
key_data=key.read(),
|
|
|
|
)
|
|
|
|
with open(f"{self.ssl_folder}/client/ca.pem", encoding="utf8") as authority:
|
|
|
|
verification_kp = CertificateKeyPair.objects.create(
|
|
|
|
name="docker-verification",
|
|
|
|
certificate_data=authority.read(),
|
|
|
|
)
|
2020-12-13 20:28:13 +00:00
|
|
|
self.service_connection = DockerServiceConnection.objects.create(
|
|
|
|
url="https://localhost:2376",
|
|
|
|
tls_verification=verification_kp,
|
|
|
|
tls_authentication=authentication_kp,
|
|
|
|
)
|
|
|
|
self.outpost: Outpost = Outpost.objects.create(
|
|
|
|
name="test",
|
|
|
|
type=OutpostType.PROXY,
|
|
|
|
service_connection=self.service_connection,
|
2021-11-15 21:37:36 +00:00
|
|
|
_config=default_outpost_config(self.live_server_url),
|
2020-12-13 20:28:13 +00:00
|
|
|
)
|
|
|
|
self.outpost.providers.add(self.provider)
|
|
|
|
self.outpost.save()
|
|
|
|
|
|
|
|
def tearDown(self) -> None:
|
|
|
|
super().tearDown()
|
|
|
|
self.container.kill()
|
2020-12-13 20:51:59 +00:00
|
|
|
try:
|
|
|
|
rmtree(self.ssl_folder)
|
|
|
|
except PermissionError:
|
|
|
|
pass
|
2020-12-13 20:28:13 +00:00
|
|
|
|
2023-11-18 23:12:10 +00:00
|
|
|
@pytest.mark.timeout(120)
|
2020-12-13 20:28:13 +00:00
|
|
|
def test_docker_controller(self):
|
|
|
|
"""test that deployment requires update"""
|
|
|
|
controller = DockerController(self.outpost, self.service_connection)
|
|
|
|
controller.up()
|
|
|
|
controller.down()
|
2021-03-05 18:09:13 +00:00
|
|
|
|
2023-11-18 23:12:10 +00:00
|
|
|
@pytest.mark.timeout(120)
|
2021-03-05 18:09:13 +00:00
|
|
|
def test_docker_static(self):
|
|
|
|
"""test that deployment requires update"""
|
|
|
|
controller = DockerController(self.outpost, self.service_connection)
|
|
|
|
manifest = controller.get_static_deployment()
|
|
|
|
compose = yaml.load(manifest, Loader=yaml.SafeLoader)
|
|
|
|
self.assertEqual(compose["version"], "3.5")
|
|
|
|
self.assertEqual(
|
|
|
|
compose["services"]["authentik_proxy"]["image"],
|
2021-12-10 20:11:37 +00:00
|
|
|
f"ghcr.io/goauthentik/dev-proxy:{get_docker_tag()}",
|
2021-03-05 18:09:13 +00:00
|
|
|
)
|