authentik.enterprise.agents.tests.test_scope
1from django.test import TestCase 2from rest_framework.exceptions import ValidationError 3 4from authentik.core.models import ActorPolicyInheritance, Application 5from authentik.core.tests.utils import create_test_user 6from authentik.enterprise.agents.models import Agent 7from authentik.lib.generators import generate_id 8from authentik.policies.engine import ListPolicyEngine, PolicyEngine 9from authentik.policies.models import PolicyBinding 10 11 12class AgentMirrorTests(TestCase): 13 """A MIRROR agent is evaluated as its parent: it passes a policy iff the parent does.""" 14 15 def _app(self) -> Application: 16 return Application.objects.create(name=generate_id(), slug=generate_id()) 17 18 def _passes(self, app: Application, user) -> bool: 19 return PolicyEngine(app, user).build().passing 20 21 def _restrict_to(self, app: Application, user) -> None: 22 # A user binding makes the app pass only for that user. 23 PolicyBinding.objects.create(target=app, user=user, order=0) 24 25 def test_mirror_matches_parent(self): 26 parent = create_test_user() 27 other = create_test_user() 28 allowed, denied = self._app(), self._app() 29 self._restrict_to(allowed, parent) 30 self._restrict_to(denied, other) 31 32 agent = Agent.create_for_user(parent) 33 self.assertEqual(agent.policy_behavior, ActorPolicyInheritance.MIRROR) 34 35 # The agent mirrors its parent exactly. 36 self.assertTrue(self._passes(allowed, parent)) 37 self.assertTrue(self._passes(allowed, agent)) 38 self.assertFalse(self._passes(denied, parent)) 39 self.assertFalse(self._passes(denied, agent)) 40 41 def test_mirror_tracks_parent_live(self): 42 parent = create_test_user() 43 other = create_test_user() 44 app = self._app() 45 self._restrict_to(app, other) # parent excluded 46 47 agent = Agent.create_for_user(parent) 48 self.assertFalse(self._passes(app, agent)) 49 50 # Grant the parent access -> the agent gains it live. 51 self._restrict_to(app, parent) 52 self.assertTrue(self._passes(app, agent)) 53 54 def test_none_agent_is_independent(self): 55 parent = create_test_user() 56 other = create_test_user() 57 agent = Agent.create_for_user(parent, policy_behavior=ActorPolicyInheritance.NONE) 58 59 # An open app (no bindings) passes for the independent agent by default... 60 self.assertTrue(self._passes(self._app(), agent)) 61 # ...but a restricted app it has no binding on is denied, regardless of the parent. 62 restricted = self._app() 63 self._restrict_to(restricted, other) 64 self.assertFalse(self._passes(restricted, agent)) 65 66 def test_copy_snapshots_parent(self): 67 parent = create_test_user() 68 app = self._app() 69 self._restrict_to(app, parent) # a binding referencing the parent as a user 70 71 agent = Agent.create_for_user(parent, policy_behavior=ActorPolicyInheritance.COPY) 72 # The agent holds its own copy of the parent's binding -> it passes. 73 self.assertTrue(self._passes(app, agent)) 74 75 # The snapshot is independent: revoking the parent's access does not affect the agent. 76 PolicyBinding.objects.filter(user=parent, target=app).delete() 77 self.assertFalse(self._passes(app, parent)) 78 self.assertTrue(self._passes(app, agent)) 79 80 def test_policy_behavior_is_immutable(self): 81 """policy_behavior may only be chosen at creation.""" 82 agent = Agent.create_for_user(create_test_user()) 83 agent = Agent.objects.get(pk=agent.pk) # fresh load snapshots the original value 84 agent.policy_behavior = ActorPolicyInheritance.NONE 85 with self.assertRaises(ValidationError): 86 agent.save() 87 88 def test_app_list_mirrors_parent(self): 89 parent = create_test_user() 90 other = create_test_user() 91 in_scope, off_scope = self._app(), self._app() 92 self._restrict_to(in_scope, parent) 93 self._restrict_to(off_scope, other) 94 agent = Agent.create_for_user(parent) 95 96 engine = ListPolicyEngine(Application.objects.all(), agent) 97 allowed = {app.pk for app in engine.build().result} 98 self.assertIn(in_scope.pk, allowed) 99 self.assertNotIn(off_scope.pk, allowed)
class
AgentMirrorTests(django.test.testcases.TestCase):
13class AgentMirrorTests(TestCase): 14 """A MIRROR agent is evaluated as its parent: it passes a policy iff the parent does.""" 15 16 def _app(self) -> Application: 17 return Application.objects.create(name=generate_id(), slug=generate_id()) 18 19 def _passes(self, app: Application, user) -> bool: 20 return PolicyEngine(app, user).build().passing 21 22 def _restrict_to(self, app: Application, user) -> None: 23 # A user binding makes the app pass only for that user. 24 PolicyBinding.objects.create(target=app, user=user, order=0) 25 26 def test_mirror_matches_parent(self): 27 parent = create_test_user() 28 other = create_test_user() 29 allowed, denied = self._app(), self._app() 30 self._restrict_to(allowed, parent) 31 self._restrict_to(denied, other) 32 33 agent = Agent.create_for_user(parent) 34 self.assertEqual(agent.policy_behavior, ActorPolicyInheritance.MIRROR) 35 36 # The agent mirrors its parent exactly. 37 self.assertTrue(self._passes(allowed, parent)) 38 self.assertTrue(self._passes(allowed, agent)) 39 self.assertFalse(self._passes(denied, parent)) 40 self.assertFalse(self._passes(denied, agent)) 41 42 def test_mirror_tracks_parent_live(self): 43 parent = create_test_user() 44 other = create_test_user() 45 app = self._app() 46 self._restrict_to(app, other) # parent excluded 47 48 agent = Agent.create_for_user(parent) 49 self.assertFalse(self._passes(app, agent)) 50 51 # Grant the parent access -> the agent gains it live. 52 self._restrict_to(app, parent) 53 self.assertTrue(self._passes(app, agent)) 54 55 def test_none_agent_is_independent(self): 56 parent = create_test_user() 57 other = create_test_user() 58 agent = Agent.create_for_user(parent, policy_behavior=ActorPolicyInheritance.NONE) 59 60 # An open app (no bindings) passes for the independent agent by default... 61 self.assertTrue(self._passes(self._app(), agent)) 62 # ...but a restricted app it has no binding on is denied, regardless of the parent. 63 restricted = self._app() 64 self._restrict_to(restricted, other) 65 self.assertFalse(self._passes(restricted, agent)) 66 67 def test_copy_snapshots_parent(self): 68 parent = create_test_user() 69 app = self._app() 70 self._restrict_to(app, parent) # a binding referencing the parent as a user 71 72 agent = Agent.create_for_user(parent, policy_behavior=ActorPolicyInheritance.COPY) 73 # The agent holds its own copy of the parent's binding -> it passes. 74 self.assertTrue(self._passes(app, agent)) 75 76 # The snapshot is independent: revoking the parent's access does not affect the agent. 77 PolicyBinding.objects.filter(user=parent, target=app).delete() 78 self.assertFalse(self._passes(app, parent)) 79 self.assertTrue(self._passes(app, agent)) 80 81 def test_policy_behavior_is_immutable(self): 82 """policy_behavior may only be chosen at creation.""" 83 agent = Agent.create_for_user(create_test_user()) 84 agent = Agent.objects.get(pk=agent.pk) # fresh load snapshots the original value 85 agent.policy_behavior = ActorPolicyInheritance.NONE 86 with self.assertRaises(ValidationError): 87 agent.save() 88 89 def test_app_list_mirrors_parent(self): 90 parent = create_test_user() 91 other = create_test_user() 92 in_scope, off_scope = self._app(), self._app() 93 self._restrict_to(in_scope, parent) 94 self._restrict_to(off_scope, other) 95 agent = Agent.create_for_user(parent) 96 97 engine = ListPolicyEngine(Application.objects.all(), agent) 98 allowed = {app.pk for app in engine.build().result} 99 self.assertIn(in_scope.pk, allowed) 100 self.assertNotIn(off_scope.pk, allowed)
A MIRROR agent is evaluated as its parent: it passes a policy iff the parent does.
def
test_mirror_matches_parent(self):
26 def test_mirror_matches_parent(self): 27 parent = create_test_user() 28 other = create_test_user() 29 allowed, denied = self._app(), self._app() 30 self._restrict_to(allowed, parent) 31 self._restrict_to(denied, other) 32 33 agent = Agent.create_for_user(parent) 34 self.assertEqual(agent.policy_behavior, ActorPolicyInheritance.MIRROR) 35 36 # The agent mirrors its parent exactly. 37 self.assertTrue(self._passes(allowed, parent)) 38 self.assertTrue(self._passes(allowed, agent)) 39 self.assertFalse(self._passes(denied, parent)) 40 self.assertFalse(self._passes(denied, agent))
def
test_mirror_tracks_parent_live(self):
42 def test_mirror_tracks_parent_live(self): 43 parent = create_test_user() 44 other = create_test_user() 45 app = self._app() 46 self._restrict_to(app, other) # parent excluded 47 48 agent = Agent.create_for_user(parent) 49 self.assertFalse(self._passes(app, agent)) 50 51 # Grant the parent access -> the agent gains it live. 52 self._restrict_to(app, parent) 53 self.assertTrue(self._passes(app, agent))
def
test_none_agent_is_independent(self):
55 def test_none_agent_is_independent(self): 56 parent = create_test_user() 57 other = create_test_user() 58 agent = Agent.create_for_user(parent, policy_behavior=ActorPolicyInheritance.NONE) 59 60 # An open app (no bindings) passes for the independent agent by default... 61 self.assertTrue(self._passes(self._app(), agent)) 62 # ...but a restricted app it has no binding on is denied, regardless of the parent. 63 restricted = self._app() 64 self._restrict_to(restricted, other) 65 self.assertFalse(self._passes(restricted, agent))
def
test_copy_snapshots_parent(self):
67 def test_copy_snapshots_parent(self): 68 parent = create_test_user() 69 app = self._app() 70 self._restrict_to(app, parent) # a binding referencing the parent as a user 71 72 agent = Agent.create_for_user(parent, policy_behavior=ActorPolicyInheritance.COPY) 73 # The agent holds its own copy of the parent's binding -> it passes. 74 self.assertTrue(self._passes(app, agent)) 75 76 # The snapshot is independent: revoking the parent's access does not affect the agent. 77 PolicyBinding.objects.filter(user=parent, target=app).delete() 78 self.assertFalse(self._passes(app, parent)) 79 self.assertTrue(self._passes(app, agent))
def
test_policy_behavior_is_immutable(self):
81 def test_policy_behavior_is_immutable(self): 82 """policy_behavior may only be chosen at creation.""" 83 agent = Agent.create_for_user(create_test_user()) 84 agent = Agent.objects.get(pk=agent.pk) # fresh load snapshots the original value 85 agent.policy_behavior = ActorPolicyInheritance.NONE 86 with self.assertRaises(ValidationError): 87 agent.save()
policy_behavior may only be chosen at creation.
def
test_app_list_mirrors_parent(self):
89 def test_app_list_mirrors_parent(self): 90 parent = create_test_user() 91 other = create_test_user() 92 in_scope, off_scope = self._app(), self._app() 93 self._restrict_to(in_scope, parent) 94 self._restrict_to(off_scope, other) 95 agent = Agent.create_for_user(parent) 96 97 engine = ListPolicyEngine(Application.objects.all(), agent) 98 allowed = {app.pk for app in engine.build().result} 99 self.assertIn(in_scope.pk, allowed) 100 self.assertNotIn(off_scope.pk, allowed)