Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 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 Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 17 | import unittest |
| 18 | from xosgenx.generator import XOSGenerator |
| 19 | from helpers import FakeArgs, XProtoTestHelpers |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 20 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 21 | """The function below is for eliminating warnings arising due to the missing policy_output_enforcer, |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 22 | which is generated and loaded dynamically. |
| 23 | """ |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 24 | def policy_output_enforcer(x, y): |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 25 | raise Exception("Security enforcer not generated. Test failed.") |
| 26 | return False |
| 27 | |
| 28 | """ |
| 29 | The tests below use the Python code target to generate |
| 30 | Python security policies, set up an appropriate environment and execute the Python. |
| 31 | """ |
| 32 | class XProtoSecurityTest(unittest.TestCase): |
| 33 | def setUp(self): |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 34 | 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 Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 39 | |
| 40 | def test_constant(self): |
| 41 | xproto = \ |
| 42 | """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 43 | policy output < True > |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 44 | """ |
| 45 | args = FakeArgs() |
| 46 | args.inputs = xproto |
| 47 | args.target = self.target |
| 48 | |
| 49 | output = XOSGenerator.generate(args) |
| 50 | |
| 51 | exec(output) # This loads the generated function, which should look like this: |
| 52 | |
| 53 | """ |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 54 | def policy_output_enforcer(obj, ctx): |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 55 | i1 = True |
| 56 | return i1 |
| 57 | """ |
| 58 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 59 | verdict = policy_output_enforcer({}, {}) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 60 | self.assertTrue(verdict) |
| 61 | |
| 62 | def test_equal(self): |
| 63 | xproto = \ |
| 64 | """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 65 | policy output < ctx.user = obj.user > |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 66 | """ |
| 67 | |
| 68 | args = FakeArgs() |
| 69 | args.inputs = xproto |
| 70 | args.target = self.target |
| 71 | |
| 72 | output = XOSGenerator.generate(args) |
| 73 | |
| 74 | exec(output) # This loads the generated function, which should look like this: |
| 75 | |
| 76 | """ |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 77 | def policy_output_enforcer(obj, ctx): |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 78 | i1 = (ctx.user == obj.user) |
| 79 | return i1 |
| 80 | """ |
| 81 | |
| 82 | obj = FakeArgs() |
| 83 | obj.user = 1 |
| 84 | ctx = FakeArgs() |
| 85 | ctx.user = 1 |
| 86 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 87 | verdict = policy_output_enforcer(obj, ctx) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 88 | |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 89 | def test_call_policy(self): |
| 90 | xproto = \ |
| 91 | """ |
| 92 | policy sub_policy < ctx.user = obj.user > |
| 93 | policy output < *sub_policy(child) > |
| 94 | """ |
| 95 | |
| 96 | args = FakeArgs() |
| 97 | args.inputs = xproto |
| 98 | args.target = self.target |
| 99 | |
| 100 | output = XOSGenerator.generate(args) |
| 101 | |
| 102 | exec(output,globals()) # This loads the generated function, which should look like this: |
| 103 | |
| 104 | """ |
| 105 | def policy_sub_policy_enforcer(obj, ctx): |
| 106 | i1 = (ctx.user == obj.user) |
| 107 | return i1 |
| 108 | |
| 109 | def policy_output_enforcer(obj, ctx): |
| 110 | i1 = policy_sub_policy_enforcer(obj.child, ctx) |
| 111 | return i1 |
| 112 | """ |
| 113 | |
| 114 | obj = FakeArgs() |
| 115 | obj.child = FakeArgs() |
| 116 | obj.child.user = 1 |
| 117 | |
| 118 | ctx = FakeArgs() |
| 119 | ctx.user = 1 |
| 120 | |
| 121 | verdict = policy_output_enforcer(obj, ctx) |
| 122 | self.assertTrue(verdict) |
| 123 | |
| 124 | |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 125 | def test_bin(self): |
| 126 | xproto = \ |
| 127 | """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 128 | policy output < ctx.is_admin = True | obj.empty = True> |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 129 | """ |
| 130 | |
| 131 | args = FakeArgs() |
| 132 | args.inputs = xproto |
| 133 | args.target = self.target |
| 134 | |
| 135 | output = XOSGenerator.generate(args) |
| 136 | exec(output) # This loads the generated function, which should look like this: |
| 137 | |
| 138 | """ |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 139 | def policy_output_enforcer(obj, ctx): |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 140 | i2 = (ctx.is_admin == True) |
| 141 | i3 = (obj.empty == True) |
| 142 | i1 = (i2 or i3) |
| 143 | return i1 |
| 144 | """ |
| 145 | |
| 146 | obj = FakeArgs() |
| 147 | obj.empty = True |
| 148 | |
| 149 | ctx = FakeArgs() |
| 150 | ctx.is_admin = True |
| 151 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 152 | verdict = policy_output_enforcer(obj, ctx) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 153 | |
| 154 | self.assertTrue(verdict) |
| 155 | |
| 156 | |
| 157 | def test_exists(self): |
| 158 | xproto = \ |
| 159 | """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 160 | policy output < exists Privilege: Privilege.object_id = obj.id > |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 161 | """ |
| 162 | args = FakeArgs() |
| 163 | args.inputs = xproto |
| 164 | args.target = self.target |
| 165 | |
| 166 | output = XOSGenerator.generate(args) |
| 167 | exec(output) # This loads the generated function, which should look like this: |
| 168 | |
| 169 | """ |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 170 | def policy_output_enforcer(obj, ctx): |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 171 | i1 = Privilege.objects.filter(object_id=obj.id) |
| 172 | return i1 |
| 173 | """ |
| 174 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 175 | self.assertTrue(policy_output_enforcer is not None) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 176 | |
| 177 | def test_python(self): |
| 178 | xproto = \ |
| 179 | """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 180 | policy output < {{ "jack" in ["the", "box"] }} = False > |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 181 | """ |
| 182 | args = FakeArgs() |
| 183 | args.inputs = xproto |
| 184 | args.target = self.target |
| 185 | output = XOSGenerator.generate(args) |
| 186 | exec(output) # This loads the generated function, which should look like this: |
| 187 | |
| 188 | """ |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 189 | def policy_output_enforcer(obj, ctx): |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 190 | i2 = ('jack' in ['the', 'box']) |
| 191 | i1 = (i2 == False) |
| 192 | return i1 |
| 193 | """ |
| 194 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 195 | self.assertTrue(policy_output_enforcer({}, {}) is True) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 196 | |
| 197 | def test_forall(self): |
| 198 | # This one we only parse |
| 199 | xproto = \ |
| 200 | """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 201 | policy output < forall Credential: Credential.obj_id = obj_id > |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 202 | """ |
| 203 | |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 204 | args = FakeArgs() |
| 205 | args.inputs = xproto |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 206 | args.target = self.target |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 207 | |
| 208 | output = XOSGenerator.generate(args) |
| 209 | """ |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 210 | def policy_output_enforcer(obj, ctx): |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 211 | i2 = Credential.objects.filter((~ Q(obj_id=obj_id)))[0] |
| 212 | i1 = (not i2) |
| 213 | return i1 |
| 214 | """ |
| 215 | exec(output) |
| 216 | |
| 217 | if __name__ == '__main__': |
| 218 | unittest.main() |