blob: 1a7b7ca82e516df80d74f0361e4300cba881cb67 [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, FakeObject
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040020
Sapan Bhatiae294aae2017-09-06 11:21:15 -040021"""The function below is for eliminating warnings arising due to the missing output_security_check,
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040022which is generated and loaded dynamically.
23"""
Sapan Bhatiae294aae2017-09-06 11:21:15 -040024def output_security_check(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.
31"""
32class XProtoSecurityTest(unittest.TestCase):
33 def setUp(self):
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040034 self.target = XProtoTestHelpers.write_tmp_target("""
35{% for name, policy in proto.policies.items() %}
36{{ xproto_fol_to_python_test(name, policy, None, '0') }}
37{% endfor %}
38""")
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040039
40 def test_constant(self):
41 xproto = \
42"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040043 policy output < True >
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040044"""
Scott Baker1f7791d2018-10-04 13:21:20 -070045 args = XOSProcessorArgs(inputs = xproto,
46 target = self.target)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040047
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080048 output = XOSProcessor.process(args)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040049
50 exec(output) # This loads the generated function, which should look like this:
51
52 """
Sapan Bhatiae294aae2017-09-06 11:21:15 -040053 def output_security_check(obj, ctx):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040054 i1 = True
55 return i1
56 """
57
Sapan Bhatiae294aae2017-09-06 11:21:15 -040058 verdict = output_security_check({}, {})
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040059 self.assertTrue(verdict)
60
61 def test_equal(self):
62 xproto = \
63"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040064 policy output < ctx.user = obj.user >
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040065"""
66
Scott Baker1f7791d2018-10-04 13:21:20 -070067 args = XOSProcessorArgs(inputs = xproto,
68 target = self.target)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040069
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080070 output = XOSProcessor.process(args)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040071
72 exec(output) # This loads the generated function, which should look like this:
73
74 """
Sapan Bhatiae294aae2017-09-06 11:21:15 -040075 def output_security_check(obj, ctx):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040076 i1 = (ctx.user == obj.user)
77 return i1
78 """
79
Scott Baker1f7791d2018-10-04 13:21:20 -070080 obj = FakeObject()
81 obj.user = 1
82 ctx = FakeObject()
83 ctx.user = 1
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040084
Sapan Bhatiae294aae2017-09-06 11:21:15 -040085 verdict = output_security_check(obj, ctx)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040086
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040087 def test_call_policy(self):
88 xproto = \
89"""
90 policy sub_policy < ctx.user = obj.user >
91 policy output < *sub_policy(child) >
92"""
93
Scott Baker1f7791d2018-10-04 13:21:20 -070094 args = XOSProcessorArgs(inputs = xproto,
95 target = self.target)
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040096
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080097 output = XOSProcessor.process(args)
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040098
99 exec(output,globals()) # This loads the generated function, which should look like this:
100
101 """
Sapan Bhatiae294aae2017-09-06 11:21:15 -0400102 def sub_policy_security_check(obj, ctx):
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400103 i1 = (ctx.user == obj.user)
Sapan Bhatiae294aae2017-09-06 11:21:15 -0400104 return i1
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400105
Sapan Bhatiae294aae2017-09-06 11:21:15 -0400106 def output_security_check(obj, ctx):
107 if obj.child:
108 i1 = sub_policy_security_check(obj.child, ctx)
109 else:
110 i1 = True
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400111 return i1
112 """
113
Scott Baker1f7791d2018-10-04 13:21:20 -0700114 obj = FakeObject()
115 obj.child = FakeObject()
116 obj.child.user = 1
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400117
Scott Baker1f7791d2018-10-04 13:21:20 -0700118 ctx = FakeObject()
119 ctx.user = 1
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400120
Sapan Bhatiae294aae2017-09-06 11:21:15 -0400121 verdict = output_security_check(obj, ctx)
122 self.assertTrue(verdict)
123
124 def test_call_policy_child_none(self):
125 xproto = \
126"""
127 policy sub_policy < ctx.user = obj.user >
128 policy output < *sub_policy(child) >
129"""
130
Scott Baker1f7791d2018-10-04 13:21:20 -0700131 args = XOSProcessorArgs(inputs = xproto,
132 target = self.target)
Sapan Bhatiae294aae2017-09-06 11:21:15 -0400133
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800134 output = XOSProcessor.process(args)
Sapan Bhatiae294aae2017-09-06 11:21:15 -0400135
136 exec(output,globals()) # This loads the generated function, which should look like this:
137
138 """
139 def sub_policy_security_check(obj, ctx):
140 i1 = (ctx.user == obj.user)
141 return i1
142
143 def output_security_check(obj, ctx):
144 if obj.child:
145 i1 = sub_policy_security_check(obj.child, ctx)
146 else:
147 i1 = True
148 return i1
149 """
150
Scott Baker1f7791d2018-10-04 13:21:20 -0700151 obj = FakeObject()
Sapan Bhatiae294aae2017-09-06 11:21:15 -0400152 obj.child = None
153
Scott Baker1f7791d2018-10-04 13:21:20 -0700154 ctx = FakeObject()
155 ctx.user = 1
Sapan Bhatiae294aae2017-09-06 11:21:15 -0400156
Sapan Bhatiac6543dd2017-12-07 11:40:36 -0500157 verdict = output_security_check(obj, ctx)
158 self.assertTrue(verdict)
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400159
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400160 def test_bin(self):
161 xproto = \
162"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400163 policy output < ctx.is_admin = True | obj.empty = True>
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400164"""
165
Scott Baker1f7791d2018-10-04 13:21:20 -0700166 args = XOSProcessorArgs(inputs = xproto,
167 target = self.target)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400168
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800169 output = XOSProcessor.process(args)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400170 exec(output) # This loads the generated function, which should look like this:
171
172 """
Sapan Bhatiae294aae2017-09-06 11:21:15 -0400173 def output_security_check(obj, ctx):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400174 i2 = (ctx.is_admin == True)
175 i3 = (obj.empty == True)
176 i1 = (i2 or i3)
177 return i1
178 """
179
Scott Baker1f7791d2018-10-04 13:21:20 -0700180 obj = FakeObject()
181 obj.empty = True
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400182
Scott Baker1f7791d2018-10-04 13:21:20 -0700183 ctx = FakeObject()
184 ctx.is_admin = True
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400185
Sapan Bhatiae294aae2017-09-06 11:21:15 -0400186 verdict = output_security_check(obj, ctx)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400187
188 self.assertTrue(verdict)
189
190
191 def test_exists(self):
192 xproto = \
193"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400194 policy output < exists Privilege: Privilege.object_id = obj.id >
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400195"""
Scott Baker1f7791d2018-10-04 13:21:20 -0700196 args = XOSProcessorArgs(inputs = xproto,
197 target = self.target)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400198
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800199 output = XOSProcessor.process(args)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400200 exec(output) # This loads the generated function, which should look like this:
201
202 """
Sapan Bhatiae294aae2017-09-06 11:21:15 -0400203 def output_security_check(obj, ctx):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400204 i1 = Privilege.objects.filter(object_id=obj.id)
205 return i1
206 """
207
Sapan Bhatiae294aae2017-09-06 11:21:15 -0400208 self.assertTrue(output_security_check is not None)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400209
210 def test_python(self):
211 xproto = \
212"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400213 policy output < {{ "jack" in ["the", "box"] }} = False >
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400214"""
Scott Baker1f7791d2018-10-04 13:21:20 -0700215 args = XOSProcessorArgs(inputs = xproto,
216 target = self.target)
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800217 output = XOSProcessor.process(args)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400218 exec(output) # This loads the generated function, which should look like this:
219
220 """
Sapan Bhatiae294aae2017-09-06 11:21:15 -0400221 def output_security_check(obj, ctx):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400222 i2 = ('jack' in ['the', 'box'])
223 i1 = (i2 == False)
224 return i1
225 """
226
Sapan Bhatiae294aae2017-09-06 11:21:15 -0400227 self.assertTrue(output_security_check({}, {}) is True)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400228
229 def test_forall(self):
230 # This one we only parse
231 xproto = \
232"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400233 policy output < forall Credential: Credential.obj_id = obj_id >
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400234"""
235
Scott Baker1f7791d2018-10-04 13:21:20 -0700236 args = XOSProcessorArgs(inputs = xproto,
237 target = self.target)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400238
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800239 output = XOSProcessor.process(args)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400240 """
Sapan Bhatiae294aae2017-09-06 11:21:15 -0400241 def output_security_check(obj, ctx):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400242 i2 = Credential.objects.filter((~ Q(obj_id=obj_id)))[0]
243 i1 = (not i2)
244 return i1
245 """
246 exec(output)
247
248if __name__ == '__main__':
249 unittest.main()