blob: 766e102ee253474c6f030d49b17bc53d8403cec7 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040017import unittest
Scott Baker1f7791d2018-10-04 13:21:20 -070018from xosgenx.generator import XOSProcessor, XOSProcessorArgs
19from helpers import XProtoTestHelpers
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040020
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040021"""The function below is for eliminating warnings arising due to the missing policy_output_enforcer,
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040022which is generated and loaded dynamically.
23"""
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040024def policy_output_enforcer(x, y):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040025 raise Exception("Security enforcer not generated. Test failed.")
26 return False
27
28"""
29The tests below use the Python code target to generate
30Python security policies, set up an appropriate environment and execute the Python.
Sapan Bhatiab69f4702017-07-31 16:03:33 -040031The security policies here deliberately made complex in order to stress the processor.
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040032"""
33class XProtoXOSSecurityTest(unittest.TestCase):
34 def setUp(self):
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040035 self.target = XProtoTestHelpers.write_tmp_target("{{ xproto_fol_to_python_test('output',proto.policies.test_policy, None, '0') }}")
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040036
37 """
38 This is the security policy for controllers
39 """
40 def test_controller_policy(self):
41 xproto = \
42"""
Sapan Bhatiab69f4702017-07-31 16:03:33 -040043 policy test_policy < ctx.user.is_admin | exists Privilege: Privilege.accessor_id = ctx.user.id & Privilege.object_type = "Deployment" & Privilege.permission = "role:admin" & Privilege.object_id = obj.id >
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040044"""
Scott Baker1f7791d2018-10-04 13:21:20 -070045 args = XOSProcessorArgs()
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040046 args.inputs = xproto
47 args.target = self.target
48
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080049 output = XOSProcessor.process(args)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040050
51 exec(output) # This loads the generated function, which should look like this:
52
53 """
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040054 def policy_output_enforcer(obj, ctx):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040055 i2 = ctx.user.is_admin
Sapan Bhatiab69f4702017-07-31 16:03:33 -040056 i3 = Privilege.objects.filter(Q(accessor_id=ctx.user.id), Q(object_type='Deployment'), Q(permission='role:admin'), Q(object_id=obj.id))[0]
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040057 i1 = (i2 or i3)
Sapan Bhatiab69f4702017-07-31 16:03:33 -040058 return i1
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040059 """
60
61 # FIXME: Test this policy by executing it
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040062 self.assertTrue(policy_output_enforcer is not None)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040063
64 """
Sapan Bhatiab69f4702017-07-31 16:03:33 -040065 This is the security policy for ControllerNetworks
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040066 """
Sapan Bhatiab69f4702017-07-31 16:03:33 -040067 def test_controller_network_policy(self):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040068 xproto = \
69"""
Sapan Bhatiab69f4702017-07-31 16:03:33 -040070 policy test_policy <
71 ctx.user.is_admin
72 | (exists Privilege:
73 Privilege.accessor_id = ctx.user.id
74 & Privilege.accessor_type = "User"
75 & Privilege.object_type = "Slice"
76 & Privilege.object_id = obj.owner.id)
77 | (exists Privilege:
78 Privilege.accessor_id = ctx.user.id
79 & Privilege.accessor_type = "User"
80 & Privilege.object_type = "Site"
81 & Privilege.object_id = obj.owner.site.id
82 & Privilege.permission = "role:admin") >
83"""
Scott Baker1f7791d2018-10-04 13:21:20 -070084 args = XOSProcessorArgs()
Sapan Bhatiab69f4702017-07-31 16:03:33 -040085 args.inputs = xproto
86 args.target = self.target
87
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080088 output = XOSProcessor.process(args)
Sapan Bhatiab69f4702017-07-31 16:03:33 -040089 exec(output) # This loads the generated function, which should look like this:
90
91 """
92 def policy_output_enforcer(obj, ctx):
93 i2 = ctx.user.is_admin
94 i4 = Privilege.objects.filter(Q(accessor_id=ctx.user.id), Q(accessor_type='User'), Q(object_type='Slice'), Q(object_id=obj.owner.id))[0]
95 i5 = Privilege.objects.filter(Q(accessor_id=ctx.user.id), Q(accessor_type='User'), Q(object_type='Site'), Q(object_id=obj.owner.site.id), Q(permission='role:admin'))[0]
96 i3 = (i4 or i5)
97 i1 = (i2 or i3)
98 return i1
99 """
100
101 # FIXME: Test this policy by executing it
102 self.assertTrue(policy_output_enforcer is not None)
103
104 """
105 This is the security policy for Slices
106 """
107 def test_slice_policy(self):
108 xproto = \
109"""
110 policy site_policy <
111 ctx.user.is_admin
112 | (ctx.write_access -> exists Privilege: Privilege.object_type = "Site" & Privilege.object_id = obj.id & Privilege.accessor_id = ctx.user.id & Privilege.permission_id = "role:admin") >
113
114 policy test_policy <
115 ctx.user.is_admin
116 | (*site_policy(site)
117 & ((exists Privilege:
118 Privilege.accessor_id = ctx.user.id
119 & Privilege.accessor_type = "User"
120 & Privilege.object_type = "Slice"
121 & Privilege.object_id = obj.id
122 & (ctx.write_access->Privilege.permission="role:admin"))
123 | (exists Privilege:
124 Privilege.accessor_id = ctx.user.id
125 & Privilege.accessor_type = "User"
126 & Privilege.object_type = "Site"
127 & Privilege.object_id = obj.site.id
128 & Privilege.permission = "role:admin"))
129 )>
130
131"""
Scott Baker1f7791d2018-10-04 13:21:20 -0700132 args = XOSProcessorArgs()
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400133 args.inputs = xproto
134 args.target = self.target
135
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800136 output = XOSProcessor.process(args)
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400137
138 exec(output) # This loads the generated function, which should look like this:
139
140 """
141 def policy_output_enforcer(obj, ctx):
142 i2 = ctx.user.is_admin
143 i4 = policy_site_policy_enforcer(obj.site, ctx)
144 i10 = ctx.write_access
145 i11 = (not (not Privilege.objects.filter(Q(accessor_id=ctx.user.id), Q(accessor_type='User'), Q(object_type='Slice'), Q(object_id=obj.id), Q(permission='role:admin'))))
146 i8 = (i10 and i11)
147 i14 = ctx.write_access
148 i12 = (not i14)
149 i13 = (not (not Privilege.objects.filter(Q(accessor_id=ctx.user.id), Q(accessor_type='User'), Q(object_type='Slice'), Q(object_id=obj.id))))
150 i9 = (i12 and i13)
151 i6 = (i8 or i9)
152 i7 = (not (not Privilege.objects.filter(Q(accessor_id=ctx.user.id), Q(accessor_type='User'), Q(object_type='Site'), Q(object_id=obj.site.id), Q(permission='role:admin'))))
153 i5 = (i6 or i7)
154 i3 = (i4 and i5)
155 i1 = (i2 or i3)
156 return i1
157 """
158
159 # FIXME: Test this policy by executing it
160 self.assertTrue(policy_output_enforcer is not None)
161
162 """
163 This is the security policy for Users
164 """
165 def test_user_policy(self):
166 xproto = \
167"""
168 policy test_policy <
169 ctx.user.is_admin
170 | ctx.user.id = obj.id
171 | (exists Privilege:
172 Privilege.accessor_id = ctx.user.id
173 & Privilege.accessor_type = "User"
174 & Privilege.permission = "role:admin"
175 & Privilege.object_type = "Site"
176 & Privilege.object_id = ctx.user.site.id) >
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400177"""
Scott Baker1f7791d2018-10-04 13:21:20 -0700178 args = XOSProcessorArgs()
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400179 args.inputs = xproto
180 args.target = self.target
181
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800182 output = XOSProcessor.process(args)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400183
184 exec(output) # This loads the generated function, which should look like this:
185
186 """
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400187 def policy_output_enforcer(obj, ctx):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400188 i2 = ctx.user.is_admin
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400189 i4 = (ctx.user.id == obj.id)
190 i5 = Privilege.objects.filter(Q(accessor_id=ctx.user.id), Q(accessor_type='User'), Q(permission='role:admin'), Q(object_type='Site'), Q(object_id=ctx.user.site.id))[0]
191 i3 = (i4 or i5)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400192 i1 = (i2 or i3)
193 return i1
194 """
195
196 # FIXME: Test this policy by executing it
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400197 self.assertTrue(policy_output_enforcer is not None)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400198
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400199if __name__ == '__main__':
200 unittest.main()