authentik.providers.rac.tests.test_agent_ssh

Test SSH connections to devices managed by the authentik agent

 1"""Test SSH connections to devices managed by the authentik agent"""
 2
 3from django.test import TransactionTestCase
 4from jwt import decode
 5
 6from authentik.blueprints.tests import reconcile_app
 7from authentik.core.models import Application, AuthenticatedSession, Session
 8from authentik.core.tests.utils import create_test_user
 9from authentik.endpoints.connectors.agent.models import AgentConnector, AgentDeviceConnection
10from authentik.events.models import Event
11from authentik.lib.generators import generate_id
12from authentik.providers.rac.models import (
13    SSH_HOST_KEY_SETTING,
14    SSH_TOKEN_SETTING,
15    ConnectionToken,
16    Protocols,
17    RACProvider,
18)
19from authentik.providers.rac.tests import create_test_device
20
21HOST_KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB3sK7tW3qkaMCG1xVX0Nnu7qzK6wvI7zPLp7mDz0MRk"
22
23
24class TestAgentSSH(TransactionTestCase):
25    """Test the settings for devices managed by the authentik agent"""
26
27    def setUp(self):
28        self.user = create_test_user()
29        self.provider = RACProvider.objects.create(name=generate_id())
30        self.app = Application.objects.create(
31            name=generate_id(),
32            slug=generate_id(),
33            provider=self.provider,
34        )
35        self.device = create_test_device(host=generate_id(), protocol=Protocols.SSH)
36        session = Session.objects.create(session_key=generate_id(), last_ip="255.255.255.255")
37        self.token = ConnectionToken.objects.create(
38            provider=self.provider,
39            device=self.device,
40            protocol=Protocols.SSH,
41            session=AuthenticatedSession.objects.create(session=session, user=self.user),
42        )
43
44    def enroll(self, host_key: str = HOST_KEY) -> None:
45        """Enroll the device with the agent connector"""
46        connector = AgentConnector.objects.create(name=generate_id())
47        connection = AgentDeviceConnection.objects.create(
48            device=self.device,
49            connector=connector,
50        )
51        connection.create_snapshot(
52            data={"vendor": {"goauthentik.io/platform": {"ssh_host_keys": [host_key]}}}
53        )
54
55    @reconcile_app("authentik_crypto")
56    def test_settings(self):
57        """An enrolled device is connected to with a token instead of credentials"""
58        self.enroll(f"localhost {HOST_KEY}")
59        settings = self.token.get_settings()
60        self.assertEqual(settings["username"], self.user.username)
61        # The host key the agent reported, without the prefix it reports it with
62        self.assertEqual(settings[SSH_HOST_KEY_SETTING], HOST_KEY)
63        # The outpost turns this into a certificate, the device validates it with us
64        token = decode(
65            settings[SSH_TOKEN_SETTING],
66            options={"verify_signature": False},
67            audience=str(self.device.pk),
68        )
69        self.assertEqual(token["preferred_username"], self.user.username)
70        self.assertEqual(token["iss"], "goauthentik.io/platform")
71
72    @reconcile_app("authentik_crypto")
73    def test_settings_no_event(self):
74        """Logging into the device is authorized by the connection itself, so the token
75        it is logged in with does not add to the audit log"""
76        self.enroll()
77        Event.objects.all().delete()
78        # The client re-connects on its own, so this happens more than once per launch
79        self.token.get_settings()
80        self.token.get_settings()
81        self.assertEqual(list(Event.objects.values_list("action", flat=True)), [])
82
83    @reconcile_app("authentik_crypto")
84    def test_settings_not_enrolled(self):
85        """A device without the agent is connected to with the settings it has"""
86        self.assertNotIn(SSH_TOKEN_SETTING, self.token.get_settings())
87
88    @reconcile_app("authentik_crypto")
89    def test_settings_other_protocol(self):
90        """Only SSH connections use a certificate"""
91        self.enroll()
92        self.token.protocol = Protocols.RDP
93        self.assertNotIn(SSH_TOKEN_SETTING, self.token.get_settings())
HOST_KEY = 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB3sK7tW3qkaMCG1xVX0Nnu7qzK6wvI7zPLp7mDz0MRk'
class TestAgentSSH(django.test.testcases.TransactionTestCase):
25class TestAgentSSH(TransactionTestCase):
26    """Test the settings for devices managed by the authentik agent"""
27
28    def setUp(self):
29        self.user = create_test_user()
30        self.provider = RACProvider.objects.create(name=generate_id())
31        self.app = Application.objects.create(
32            name=generate_id(),
33            slug=generate_id(),
34            provider=self.provider,
35        )
36        self.device = create_test_device(host=generate_id(), protocol=Protocols.SSH)
37        session = Session.objects.create(session_key=generate_id(), last_ip="255.255.255.255")
38        self.token = ConnectionToken.objects.create(
39            provider=self.provider,
40            device=self.device,
41            protocol=Protocols.SSH,
42            session=AuthenticatedSession.objects.create(session=session, user=self.user),
43        )
44
45    def enroll(self, host_key: str = HOST_KEY) -> None:
46        """Enroll the device with the agent connector"""
47        connector = AgentConnector.objects.create(name=generate_id())
48        connection = AgentDeviceConnection.objects.create(
49            device=self.device,
50            connector=connector,
51        )
52        connection.create_snapshot(
53            data={"vendor": {"goauthentik.io/platform": {"ssh_host_keys": [host_key]}}}
54        )
55
56    @reconcile_app("authentik_crypto")
57    def test_settings(self):
58        """An enrolled device is connected to with a token instead of credentials"""
59        self.enroll(f"localhost {HOST_KEY}")
60        settings = self.token.get_settings()
61        self.assertEqual(settings["username"], self.user.username)
62        # The host key the agent reported, without the prefix it reports it with
63        self.assertEqual(settings[SSH_HOST_KEY_SETTING], HOST_KEY)
64        # The outpost turns this into a certificate, the device validates it with us
65        token = decode(
66            settings[SSH_TOKEN_SETTING],
67            options={"verify_signature": False},
68            audience=str(self.device.pk),
69        )
70        self.assertEqual(token["preferred_username"], self.user.username)
71        self.assertEqual(token["iss"], "goauthentik.io/platform")
72
73    @reconcile_app("authentik_crypto")
74    def test_settings_no_event(self):
75        """Logging into the device is authorized by the connection itself, so the token
76        it is logged in with does not add to the audit log"""
77        self.enroll()
78        Event.objects.all().delete()
79        # The client re-connects on its own, so this happens more than once per launch
80        self.token.get_settings()
81        self.token.get_settings()
82        self.assertEqual(list(Event.objects.values_list("action", flat=True)), [])
83
84    @reconcile_app("authentik_crypto")
85    def test_settings_not_enrolled(self):
86        """A device without the agent is connected to with the settings it has"""
87        self.assertNotIn(SSH_TOKEN_SETTING, self.token.get_settings())
88
89    @reconcile_app("authentik_crypto")
90    def test_settings_other_protocol(self):
91        """Only SSH connections use a certificate"""
92        self.enroll()
93        self.token.protocol = Protocols.RDP
94        self.assertNotIn(SSH_TOKEN_SETTING, self.token.get_settings())

Test the settings for devices managed by the authentik agent

def setUp(self):
28    def setUp(self):
29        self.user = create_test_user()
30        self.provider = RACProvider.objects.create(name=generate_id())
31        self.app = Application.objects.create(
32            name=generate_id(),
33            slug=generate_id(),
34            provider=self.provider,
35        )
36        self.device = create_test_device(host=generate_id(), protocol=Protocols.SSH)
37        session = Session.objects.create(session_key=generate_id(), last_ip="255.255.255.255")
38        self.token = ConnectionToken.objects.create(
39            provider=self.provider,
40            device=self.device,
41            protocol=Protocols.SSH,
42            session=AuthenticatedSession.objects.create(session=session, user=self.user),
43        )

Hook method for setting up the test fixture before exercising it.

def enroll( self, host_key: str = 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB3sK7tW3qkaMCG1xVX0Nnu7qzK6wvI7zPLp7mDz0MRk') -> None:
45    def enroll(self, host_key: str = HOST_KEY) -> None:
46        """Enroll the device with the agent connector"""
47        connector = AgentConnector.objects.create(name=generate_id())
48        connection = AgentDeviceConnection.objects.create(
49            device=self.device,
50            connector=connector,
51        )
52        connection.create_snapshot(
53            data={"vendor": {"goauthentik.io/platform": {"ssh_host_keys": [host_key]}}}
54        )

Enroll the device with the agent connector

@reconcile_app('authentik_crypto')
def test_settings(self):
56    @reconcile_app("authentik_crypto")
57    def test_settings(self):
58        """An enrolled device is connected to with a token instead of credentials"""
59        self.enroll(f"localhost {HOST_KEY}")
60        settings = self.token.get_settings()
61        self.assertEqual(settings["username"], self.user.username)
62        # The host key the agent reported, without the prefix it reports it with
63        self.assertEqual(settings[SSH_HOST_KEY_SETTING], HOST_KEY)
64        # The outpost turns this into a certificate, the device validates it with us
65        token = decode(
66            settings[SSH_TOKEN_SETTING],
67            options={"verify_signature": False},
68            audience=str(self.device.pk),
69        )
70        self.assertEqual(token["preferred_username"], self.user.username)
71        self.assertEqual(token["iss"], "goauthentik.io/platform")

An enrolled device is connected to with a token instead of credentials

@reconcile_app('authentik_crypto')
def test_settings_no_event(self):
73    @reconcile_app("authentik_crypto")
74    def test_settings_no_event(self):
75        """Logging into the device is authorized by the connection itself, so the token
76        it is logged in with does not add to the audit log"""
77        self.enroll()
78        Event.objects.all().delete()
79        # The client re-connects on its own, so this happens more than once per launch
80        self.token.get_settings()
81        self.token.get_settings()
82        self.assertEqual(list(Event.objects.values_list("action", flat=True)), [])

Logging into the device is authorized by the connection itself, so the token it is logged in with does not add to the audit log

@reconcile_app('authentik_crypto')
def test_settings_not_enrolled(self):
84    @reconcile_app("authentik_crypto")
85    def test_settings_not_enrolled(self):
86        """A device without the agent is connected to with the settings it has"""
87        self.assertNotIn(SSH_TOKEN_SETTING, self.token.get_settings())

A device without the agent is connected to with the settings it has

@reconcile_app('authentik_crypto')
def test_settings_other_protocol(self):
89    @reconcile_app("authentik_crypto")
90    def test_settings_other_protocol(self):
91        """Only SSH connections use a certificate"""
92        self.enroll()
93        self.token.protocol = Protocols.RDP
94        self.assertNotIn(SSH_TOKEN_SETTING, self.token.get_settings())

Only SSH connections use a certificate