authentik.providers.rac.tests.test_devices_api
Test RAC Devices API
1"""Test RAC Devices API""" 2 3from django.urls import reverse 4from rest_framework.test import APITestCase 5 6from authentik.core.models import Application 7from authentik.core.tests.utils import create_test_admin_user, create_test_user 8from authentik.endpoints.models import DeviceAccessGroup 9from authentik.lib.generators import generate_id 10from authentik.policies.dummy.models import DummyPolicy 11from authentik.policies.models import PolicyBinding 12from authentik.providers.rac.models import Protocols, RACProvider 13from authentik.providers.rac.tests import create_test_device 14 15 16class TestRACDevicesAPI(APITestCase): 17 """Test RAC devices API""" 18 19 def setUp(self) -> None: 20 self.user = create_test_admin_user() 21 self.provider = RACProvider.objects.create(name=generate_id()) 22 self.app = Application.objects.create( 23 name=generate_id(), 24 slug=generate_id(), 25 provider=self.provider, 26 ) 27 self.allowed = create_test_device(name=f"a-{generate_id()}", host=generate_id()) 28 self.denied = create_test_device(name=f"b-{generate_id()}", host=generate_id()) 29 PolicyBinding.objects.create( 30 target=self.denied, 31 policy=DummyPolicy.objects.create(name="deny", result=False, wait_min=1, wait_max=2), 32 order=0, 33 ) 34 35 def list_devices(self, **params) -> list[dict]: 36 response = self.client.get( 37 reverse("authentik_api:rac_device-list"), 38 data={"provider": self.provider.pk, **params}, 39 ) 40 self.assertEqual(response.status_code, 200) 41 return response.json()["results"] 42 43 def test_list(self): 44 """Only devices whose policies pass are listed""" 45 self.client.force_login(self.user) 46 self.assertEqual( 47 self.list_devices(), 48 [ 49 { 50 "device_uuid": str(self.allowed.pk), 51 "name": self.allowed.name, 52 "protocols": [ 53 { 54 "protocol": Protocols.RDP, 55 "launch_url": ( 56 f"/application/rac/{self.app.slug}/{self.allowed.pk}/rdp/" 57 ), 58 } 59 ], 60 "override_pk": self.allowed.rac_override.pk, 61 } 62 ], 63 ) 64 65 def test_list_requires_provider(self): 66 """The provider is required, as it determines what is launchable""" 67 self.client.force_login(self.user) 68 response = self.client.get(reverse("authentik_api:rac_device-list")) 69 self.assertEqual(response.status_code, 400) 70 71 def test_list_access_group(self): 72 """A provider limited to an access group only lists devices in it""" 73 group = DeviceAccessGroup.objects.create(name=generate_id()) 74 self.provider.access_group = group 75 self.provider.save() 76 in_group = create_test_device(host=generate_id(), access_group=group) 77 self.client.force_login(self.user) 78 pks = [device["device_uuid"] for device in self.list_devices()] 79 self.assertEqual(pks, [str(in_group.pk)]) 80 81 def test_list_access_group_policy(self): 82 """Policies bound to the access group gate every device in it""" 83 group = DeviceAccessGroup.objects.create(name=generate_id()) 84 PolicyBinding.objects.create( 85 target=group, 86 policy=DummyPolicy.objects.create( 87 name=f"deny-{generate_id()}", result=False, wait_min=1, wait_max=2 88 ), 89 order=0, 90 ) 91 create_test_device(host=generate_id(), access_group=group) 92 self.client.force_login(self.user) 93 pks = [device["device_uuid"] for device in self.list_devices()] 94 self.assertEqual(pks, [str(self.allowed.pk)]) 95 96 def test_list_protocols(self): 97 """Each protocol a device can be reached with has its own launch URL""" 98 override = create_test_device( 99 name=f"c-{generate_id()}", host=generate_id(), protocol=Protocols.SSH 100 ) 101 # A device which says nothing about itself can be reached with either protocol 102 unknown = create_test_device(name=f"d-{generate_id()}") 103 self.client.force_login(self.user) 104 listed = {d["device_uuid"]: d for d in self.list_devices()} 105 self.assertEqual( 106 listed[str(override.pk)]["protocols"], 107 [ 108 { 109 "protocol": Protocols.SSH, 110 "launch_url": f"/application/rac/{self.app.slug}/{override.pk}/ssh/", 111 } 112 ], 113 ) 114 self.assertEqual( 115 [entry["protocol"] for entry in listed[str(unknown.pk)]["protocols"]], 116 [Protocols.RDP, Protocols.SSH], 117 ) 118 119 def test_list_regular_user_denied_application(self): 120 """A user who is denied the application receives no devices, even though the 121 devices themselves have no policy bindings""" 122 PolicyBinding.objects.create( 123 target=self.app, 124 policy=DummyPolicy.objects.create( 125 name=f"deny-{generate_id()}", result=False, wait_min=1, wait_max=2 126 ), 127 order=0, 128 ) 129 user = create_test_user() 130 self.assertFalse(user.has_perm("authentik_endpoints.view_device")) 131 self.client.force_login(user) 132 self.assertEqual(self.list_devices(), []) 133 134 def test_list_regular_user_allowed_application(self): 135 """A user who passes an application's policies receives its devices without 136 needing device permissions (this is the end-user launch picker)""" 137 user = create_test_user() 138 self.assertFalse(user.has_perm("authentik_endpoints.view_device")) 139 self.client.force_login(user) 140 pks = [device["device_uuid"] for device in self.list_devices()] 141 self.assertEqual(pks, [str(self.allowed.pk)]) 142 143 def test_list_no_attributes_exposed(self): 144 """A device's attributes are not exposed through the launch picker""" 145 secret = generate_id() 146 create_test_device( 147 name=f"c-{generate_id()}", 148 host=generate_id(), 149 attributes={"internal": secret}, 150 ) 151 user = create_test_user() 152 self.client.force_login(user) 153 response = self.client.get( 154 reverse("authentik_api:rac_device-list"), 155 data={"provider": self.provider.pk}, 156 ) 157 self.assertNotIn(secret, response.content.decode())
class
TestRACDevicesAPI(rest_framework.test.APITestCase):
17class TestRACDevicesAPI(APITestCase): 18 """Test RAC devices API""" 19 20 def setUp(self) -> None: 21 self.user = create_test_admin_user() 22 self.provider = RACProvider.objects.create(name=generate_id()) 23 self.app = Application.objects.create( 24 name=generate_id(), 25 slug=generate_id(), 26 provider=self.provider, 27 ) 28 self.allowed = create_test_device(name=f"a-{generate_id()}", host=generate_id()) 29 self.denied = create_test_device(name=f"b-{generate_id()}", host=generate_id()) 30 PolicyBinding.objects.create( 31 target=self.denied, 32 policy=DummyPolicy.objects.create(name="deny", result=False, wait_min=1, wait_max=2), 33 order=0, 34 ) 35 36 def list_devices(self, **params) -> list[dict]: 37 response = self.client.get( 38 reverse("authentik_api:rac_device-list"), 39 data={"provider": self.provider.pk, **params}, 40 ) 41 self.assertEqual(response.status_code, 200) 42 return response.json()["results"] 43 44 def test_list(self): 45 """Only devices whose policies pass are listed""" 46 self.client.force_login(self.user) 47 self.assertEqual( 48 self.list_devices(), 49 [ 50 { 51 "device_uuid": str(self.allowed.pk), 52 "name": self.allowed.name, 53 "protocols": [ 54 { 55 "protocol": Protocols.RDP, 56 "launch_url": ( 57 f"/application/rac/{self.app.slug}/{self.allowed.pk}/rdp/" 58 ), 59 } 60 ], 61 "override_pk": self.allowed.rac_override.pk, 62 } 63 ], 64 ) 65 66 def test_list_requires_provider(self): 67 """The provider is required, as it determines what is launchable""" 68 self.client.force_login(self.user) 69 response = self.client.get(reverse("authentik_api:rac_device-list")) 70 self.assertEqual(response.status_code, 400) 71 72 def test_list_access_group(self): 73 """A provider limited to an access group only lists devices in it""" 74 group = DeviceAccessGroup.objects.create(name=generate_id()) 75 self.provider.access_group = group 76 self.provider.save() 77 in_group = create_test_device(host=generate_id(), access_group=group) 78 self.client.force_login(self.user) 79 pks = [device["device_uuid"] for device in self.list_devices()] 80 self.assertEqual(pks, [str(in_group.pk)]) 81 82 def test_list_access_group_policy(self): 83 """Policies bound to the access group gate every device in it""" 84 group = DeviceAccessGroup.objects.create(name=generate_id()) 85 PolicyBinding.objects.create( 86 target=group, 87 policy=DummyPolicy.objects.create( 88 name=f"deny-{generate_id()}", result=False, wait_min=1, wait_max=2 89 ), 90 order=0, 91 ) 92 create_test_device(host=generate_id(), access_group=group) 93 self.client.force_login(self.user) 94 pks = [device["device_uuid"] for device in self.list_devices()] 95 self.assertEqual(pks, [str(self.allowed.pk)]) 96 97 def test_list_protocols(self): 98 """Each protocol a device can be reached with has its own launch URL""" 99 override = create_test_device( 100 name=f"c-{generate_id()}", host=generate_id(), protocol=Protocols.SSH 101 ) 102 # A device which says nothing about itself can be reached with either protocol 103 unknown = create_test_device(name=f"d-{generate_id()}") 104 self.client.force_login(self.user) 105 listed = {d["device_uuid"]: d for d in self.list_devices()} 106 self.assertEqual( 107 listed[str(override.pk)]["protocols"], 108 [ 109 { 110 "protocol": Protocols.SSH, 111 "launch_url": f"/application/rac/{self.app.slug}/{override.pk}/ssh/", 112 } 113 ], 114 ) 115 self.assertEqual( 116 [entry["protocol"] for entry in listed[str(unknown.pk)]["protocols"]], 117 [Protocols.RDP, Protocols.SSH], 118 ) 119 120 def test_list_regular_user_denied_application(self): 121 """A user who is denied the application receives no devices, even though the 122 devices themselves have no policy bindings""" 123 PolicyBinding.objects.create( 124 target=self.app, 125 policy=DummyPolicy.objects.create( 126 name=f"deny-{generate_id()}", result=False, wait_min=1, wait_max=2 127 ), 128 order=0, 129 ) 130 user = create_test_user() 131 self.assertFalse(user.has_perm("authentik_endpoints.view_device")) 132 self.client.force_login(user) 133 self.assertEqual(self.list_devices(), []) 134 135 def test_list_regular_user_allowed_application(self): 136 """A user who passes an application's policies receives its devices without 137 needing device permissions (this is the end-user launch picker)""" 138 user = create_test_user() 139 self.assertFalse(user.has_perm("authentik_endpoints.view_device")) 140 self.client.force_login(user) 141 pks = [device["device_uuid"] for device in self.list_devices()] 142 self.assertEqual(pks, [str(self.allowed.pk)]) 143 144 def test_list_no_attributes_exposed(self): 145 """A device's attributes are not exposed through the launch picker""" 146 secret = generate_id() 147 create_test_device( 148 name=f"c-{generate_id()}", 149 host=generate_id(), 150 attributes={"internal": secret}, 151 ) 152 user = create_test_user() 153 self.client.force_login(user) 154 response = self.client.get( 155 reverse("authentik_api:rac_device-list"), 156 data={"provider": self.provider.pk}, 157 ) 158 self.assertNotIn(secret, response.content.decode())
Test RAC devices API
def
setUp(self) -> None:
20 def setUp(self) -> None: 21 self.user = create_test_admin_user() 22 self.provider = RACProvider.objects.create(name=generate_id()) 23 self.app = Application.objects.create( 24 name=generate_id(), 25 slug=generate_id(), 26 provider=self.provider, 27 ) 28 self.allowed = create_test_device(name=f"a-{generate_id()}", host=generate_id()) 29 self.denied = create_test_device(name=f"b-{generate_id()}", host=generate_id()) 30 PolicyBinding.objects.create( 31 target=self.denied, 32 policy=DummyPolicy.objects.create(name="deny", result=False, wait_min=1, wait_max=2), 33 order=0, 34 )
Hook method for setting up the test fixture before exercising it.
def
test_list(self):
44 def test_list(self): 45 """Only devices whose policies pass are listed""" 46 self.client.force_login(self.user) 47 self.assertEqual( 48 self.list_devices(), 49 [ 50 { 51 "device_uuid": str(self.allowed.pk), 52 "name": self.allowed.name, 53 "protocols": [ 54 { 55 "protocol": Protocols.RDP, 56 "launch_url": ( 57 f"/application/rac/{self.app.slug}/{self.allowed.pk}/rdp/" 58 ), 59 } 60 ], 61 "override_pk": self.allowed.rac_override.pk, 62 } 63 ], 64 )
Only devices whose policies pass are listed
def
test_list_requires_provider(self):
66 def test_list_requires_provider(self): 67 """The provider is required, as it determines what is launchable""" 68 self.client.force_login(self.user) 69 response = self.client.get(reverse("authentik_api:rac_device-list")) 70 self.assertEqual(response.status_code, 400)
The provider is required, as it determines what is launchable
def
test_list_access_group(self):
72 def test_list_access_group(self): 73 """A provider limited to an access group only lists devices in it""" 74 group = DeviceAccessGroup.objects.create(name=generate_id()) 75 self.provider.access_group = group 76 self.provider.save() 77 in_group = create_test_device(host=generate_id(), access_group=group) 78 self.client.force_login(self.user) 79 pks = [device["device_uuid"] for device in self.list_devices()] 80 self.assertEqual(pks, [str(in_group.pk)])
A provider limited to an access group only lists devices in it
def
test_list_access_group_policy(self):
82 def test_list_access_group_policy(self): 83 """Policies bound to the access group gate every device in it""" 84 group = DeviceAccessGroup.objects.create(name=generate_id()) 85 PolicyBinding.objects.create( 86 target=group, 87 policy=DummyPolicy.objects.create( 88 name=f"deny-{generate_id()}", result=False, wait_min=1, wait_max=2 89 ), 90 order=0, 91 ) 92 create_test_device(host=generate_id(), access_group=group) 93 self.client.force_login(self.user) 94 pks = [device["device_uuid"] for device in self.list_devices()] 95 self.assertEqual(pks, [str(self.allowed.pk)])
Policies bound to the access group gate every device in it
def
test_list_protocols(self):
97 def test_list_protocols(self): 98 """Each protocol a device can be reached with has its own launch URL""" 99 override = create_test_device( 100 name=f"c-{generate_id()}", host=generate_id(), protocol=Protocols.SSH 101 ) 102 # A device which says nothing about itself can be reached with either protocol 103 unknown = create_test_device(name=f"d-{generate_id()}") 104 self.client.force_login(self.user) 105 listed = {d["device_uuid"]: d for d in self.list_devices()} 106 self.assertEqual( 107 listed[str(override.pk)]["protocols"], 108 [ 109 { 110 "protocol": Protocols.SSH, 111 "launch_url": f"/application/rac/{self.app.slug}/{override.pk}/ssh/", 112 } 113 ], 114 ) 115 self.assertEqual( 116 [entry["protocol"] for entry in listed[str(unknown.pk)]["protocols"]], 117 [Protocols.RDP, Protocols.SSH], 118 )
Each protocol a device can be reached with has its own launch URL
def
test_list_regular_user_denied_application(self):
120 def test_list_regular_user_denied_application(self): 121 """A user who is denied the application receives no devices, even though the 122 devices themselves have no policy bindings""" 123 PolicyBinding.objects.create( 124 target=self.app, 125 policy=DummyPolicy.objects.create( 126 name=f"deny-{generate_id()}", result=False, wait_min=1, wait_max=2 127 ), 128 order=0, 129 ) 130 user = create_test_user() 131 self.assertFalse(user.has_perm("authentik_endpoints.view_device")) 132 self.client.force_login(user) 133 self.assertEqual(self.list_devices(), [])
A user who is denied the application receives no devices, even though the devices themselves have no policy bindings
def
test_list_regular_user_allowed_application(self):
135 def test_list_regular_user_allowed_application(self): 136 """A user who passes an application's policies receives its devices without 137 needing device permissions (this is the end-user launch picker)""" 138 user = create_test_user() 139 self.assertFalse(user.has_perm("authentik_endpoints.view_device")) 140 self.client.force_login(user) 141 pks = [device["device_uuid"] for device in self.list_devices()] 142 self.assertEqual(pks, [str(self.allowed.pk)])
A user who passes an application's policies receives its devices without needing device permissions (this is the end-user launch picker)
def
test_list_no_attributes_exposed(self):
144 def test_list_no_attributes_exposed(self): 145 """A device's attributes are not exposed through the launch picker""" 146 secret = generate_id() 147 create_test_device( 148 name=f"c-{generate_id()}", 149 host=generate_id(), 150 attributes={"internal": secret}, 151 ) 152 user = create_test_user() 153 self.client.force_login(user) 154 response = self.client.get( 155 reverse("authentik_api:rac_device-list"), 156 data={"provider": self.provider.pk}, 157 ) 158 self.assertNotIn(secret, response.content.decode())
A device's attributes are not exposed through the launch picker