blob: 1201baa27d2165763ff568e186eec8f380606293 [file] [log] [blame]
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -04001import unittest
2from xosgenx.generator import XOSGenerator
3from helpers import FakeArgs, XProtoTestHelpers
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -04004
Sapan Bhatia5ea307d2017-07-19 00:13:21 -04005"""The function below is for eliminating warnings arising due to the missing policy_output_enforcer,
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -04006which is generated and loaded dynamically.
7"""
Sapan Bhatia5ea307d2017-07-19 00:13:21 -04008def policy_output_enforcer(x, y):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -04009 raise Exception("Security enforcer not generated. Test failed.")
10 return False
11
12"""
13The tests below use the Python code target to generate
14Python security policies, set up an appropriate environment and execute the Python.
15"""
16class XProtoSecurityTest(unittest.TestCase):
17 def setUp(self):
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040018 self.target = XProtoTestHelpers.write_tmp_target("""
19{% for name, policy in proto.policies.items() %}
20{{ xproto_fol_to_python_test(name, policy, None, '0') }}
21{% endfor %}
22""")
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040023
24 def test_constant(self):
25 xproto = \
26"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040027 policy output < True >
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040028"""
29 args = FakeArgs()
30 args.inputs = xproto
31 args.target = self.target
32
33 output = XOSGenerator.generate(args)
34
35 exec(output) # This loads the generated function, which should look like this:
36
37 """
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040038 def policy_output_enforcer(obj, ctx):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040039 i1 = True
40 return i1
41 """
42
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040043 verdict = policy_output_enforcer({}, {})
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040044 self.assertTrue(verdict)
45
46 def test_equal(self):
47 xproto = \
48"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040049 policy output < ctx.user = obj.user >
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040050"""
51
52 args = FakeArgs()
53 args.inputs = xproto
54 args.target = self.target
55
56 output = XOSGenerator.generate(args)
57
58 exec(output) # This loads the generated function, which should look like this:
59
60 """
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040061 def policy_output_enforcer(obj, ctx):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040062 i1 = (ctx.user == obj.user)
63 return i1
64 """
65
66 obj = FakeArgs()
67 obj.user = 1
68 ctx = FakeArgs()
69 ctx.user = 1
70
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040071 verdict = policy_output_enforcer(obj, ctx)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040072
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040073 def test_call_policy(self):
74 xproto = \
75"""
76 policy sub_policy < ctx.user = obj.user >
77 policy output < *sub_policy(child) >
78"""
79
80 args = FakeArgs()
81 args.inputs = xproto
82 args.target = self.target
83
84 output = XOSGenerator.generate(args)
85
86 exec(output,globals()) # This loads the generated function, which should look like this:
87
88 """
89 def policy_sub_policy_enforcer(obj, ctx):
90 i1 = (ctx.user == obj.user)
91 return i1
92
93 def policy_output_enforcer(obj, ctx):
94 i1 = policy_sub_policy_enforcer(obj.child, ctx)
95 return i1
96 """
97
98 obj = FakeArgs()
99 obj.child = FakeArgs()
100 obj.child.user = 1
101
102 ctx = FakeArgs()
103 ctx.user = 1
104
105 verdict = policy_output_enforcer(obj, ctx)
106 self.assertTrue(verdict)
107
108
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400109 def test_bin(self):
110 xproto = \
111"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400112 policy output < ctx.is_admin = True | obj.empty = True>
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400113"""
114
115 args = FakeArgs()
116 args.inputs = xproto
117 args.target = self.target
118
119 output = XOSGenerator.generate(args)
120 exec(output) # This loads the generated function, which should look like this:
121
122 """
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400123 def policy_output_enforcer(obj, ctx):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400124 i2 = (ctx.is_admin == True)
125 i3 = (obj.empty == True)
126 i1 = (i2 or i3)
127 return i1
128 """
129
130 obj = FakeArgs()
131 obj.empty = True
132
133 ctx = FakeArgs()
134 ctx.is_admin = True
135
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400136 verdict = policy_output_enforcer(obj, ctx)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400137
138 self.assertTrue(verdict)
139
140
141 def test_exists(self):
142 xproto = \
143"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400144 policy output < exists Privilege: Privilege.object_id = obj.id >
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400145"""
146 args = FakeArgs()
147 args.inputs = xproto
148 args.target = self.target
149
150 output = XOSGenerator.generate(args)
151 exec(output) # This loads the generated function, which should look like this:
152
153 """
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400154 def policy_output_enforcer(obj, ctx):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400155 i1 = Privilege.objects.filter(object_id=obj.id)
156 return i1
157 """
158
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400159 self.assertTrue(policy_output_enforcer is not None)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400160
161 def test_python(self):
162 xproto = \
163"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400164 policy output < {{ "jack" in ["the", "box"] }} = False >
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400165"""
166 args = FakeArgs()
167 args.inputs = xproto
168 args.target = self.target
169 output = XOSGenerator.generate(args)
170 exec(output) # This loads the generated function, which should look like this:
171
172 """
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400173 def policy_output_enforcer(obj, ctx):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400174 i2 = ('jack' in ['the', 'box'])
175 i1 = (i2 == False)
176 return i1
177 """
178
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400179 self.assertTrue(policy_output_enforcer({}, {}) is True)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400180
181 def test_forall(self):
182 # This one we only parse
183 xproto = \
184"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400185 policy output < forall Credential: Credential.obj_id = obj_id >
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400186"""
187
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400188 args = FakeArgs()
189 args.inputs = xproto
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400190 args.target = self.target
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400191
192 output = XOSGenerator.generate(args)
193 """
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400194 def policy_output_enforcer(obj, ctx):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400195 i2 = Credential.objects.filter((~ Q(obj_id=obj_id)))[0]
196 i1 = (not i2)
197 return i1
198 """
199 exec(output)
200
201if __name__ == '__main__':
202 unittest.main()