Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 16 | import unittest |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 17 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
| 18 | from helpers import FakeObject, XProtoTestHelpers |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 19 | |
| 20 | """The function below is for eliminating warnings arising due to the missing policy_output_validator, |
| 21 | which is generated and loaded dynamically. |
| 22 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 23 | |
| 24 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 25 | def policy_output_validator(x, y): |
| 26 | raise Exception("Validator not generated. Test failed.") |
| 27 | return False |
| 28 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 29 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 30 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 31 | The tests below use the Python code target to generate |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 32 | Python validation policies, set up an appropriate environment and execute the Python. |
| 33 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 34 | |
| 35 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 36 | class XProtoXOSModelValidationTest(unittest.TestCase): |
| 37 | def setUp(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 38 | self.target = XProtoTestHelpers.write_tmp_target( |
| 39 | "{{ xproto_fol_to_python_validator('output', proto.policies.test_policy, None, 'Necessary Failure') }}" |
| 40 | ) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 41 | |
| 42 | def test_instance_container(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 43 | xproto = """ |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 44 | policy test_policy < (obj.isolation = "container" | obj.isolation = "container_vm" ) -> (obj.image.kind = "container") > |
| 45 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 46 | args = XOSProcessorArgs() |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 47 | args.inputs = xproto |
| 48 | args.target = self.target |
| 49 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 50 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 51 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 52 | obj = FakeObject() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 53 | obj.isolation = "container" |
| 54 | obj.kind = "not a container" |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 55 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 56 | exec(output) # This loads the generated function, which should look like this: |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 57 | |
| 58 | """ |
| 59 | def policy_output_validator(obj, ctx): |
| 60 | i4 = (obj.isolation == 'container') |
| 61 | i5 = (self.isolation == 'container_vm') |
| 62 | i2 = (i4 or i5) |
| 63 | i3 = (obj.image.kind == 'container') |
| 64 | i1 = (i2 or i3) |
| 65 | return i1 |
| 66 | """ |
| 67 | |
| 68 | with self.assertRaises(Exception): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 69 | policy_output_validator(obj, {}) |
| 70 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 71 | def test_slice_name_validation(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 72 | xproto = """ |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 73 | policy test_policy < not obj.id -> {{ obj.name.startswith(obj.site.login_base) }} > |
| 74 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 75 | args = XOSProcessorArgs() |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 76 | args.inputs = xproto |
| 77 | args.target = self.target |
| 78 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 79 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 80 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 81 | obj = FakeObject() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 82 | obj.isolation = "container" |
| 83 | obj.kind = "not a container" |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 84 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 85 | exec(output) # This loads the generated function, which should look like this: |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 86 | |
| 87 | """ |
| 88 | def policy_output_validator(obj, ctx): |
| 89 | i3 = obj.id |
| 90 | i4 = obj.name.startswith(obj.site.login_base) |
| 91 | i2 = ((not i3) or i4) |
| 92 | i1 = (not i2) |
| 93 | if (not i1): |
| 94 | raise ValidationError('Necessary Failure') |
| 95 | """ |
| 96 | |
| 97 | with self.assertRaises(Exception): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 98 | policy_output_validator(obj, {}) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 99 | |
| 100 | def test_equal(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 101 | xproto = """ |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 102 | policy test_policy < not (ctx.user = obj.user) > |
| 103 | """ |
| 104 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 105 | args = XOSProcessorArgs() |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 106 | args.inputs = xproto |
| 107 | args.target = self.target |
| 108 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 109 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 110 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 111 | exec(output) # This loads the generated function, which should look like this: |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 112 | |
| 113 | """ |
| 114 | def policy_output_validator(obj, ctx): |
| 115 | i2 = (ctx.user == obj.user) |
| 116 | i1 = (not i2) |
| 117 | if (not i1): |
| 118 | raise Exception('Necessary Failure') |
| 119 | """ |
| 120 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 121 | obj = FakeObject() |
| 122 | obj.user = 1 |
| 123 | ctx = FakeObject() |
| 124 | ctx.user = 1 |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 125 | |
| 126 | with self.assertRaises(Exception): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 127 | policy_output_validator(obj, ctx) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 128 | |
| 129 | def test_bin(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 130 | xproto = """ |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 131 | policy test_policy < not (ctx.is_admin = True | obj.empty = True) | False> |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 132 | """ |
| 133 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 134 | args = XOSProcessorArgs() |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 135 | args.inputs = xproto |
| 136 | args.target = self.target |
| 137 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 138 | output = XOSProcessor.process(args) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 139 | exec(output) # This loads the generated function, which should look like this: |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 140 | |
| 141 | """ |
| 142 | def policy_output_validator(obj, ctx): |
| 143 | i2 = (ctx.is_admin == True) |
| 144 | i3 = (obj.empty == True) |
| 145 | i1 = (i2 or i3) |
| 146 | if (not i1): |
| 147 | raise Exception('Necessary Failure') |
| 148 | """ |
| 149 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 150 | obj = FakeObject() |
| 151 | obj.empty = True |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 152 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 153 | ctx = FakeObject() |
| 154 | ctx.is_admin = True |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 155 | |
| 156 | with self.assertRaises(Exception): |
| 157 | verdict = policy_output_validator(obj, ctx) |
| 158 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 159 | def test_exists(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 160 | xproto = """ |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 161 | policy test_policy < exists Privilege: Privilege.object_id = obj.id > |
| 162 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 163 | args = XOSProcessorArgs() |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 164 | args.inputs = xproto |
| 165 | args.target = self.target |
| 166 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 167 | output = XOSProcessor.process(args) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 168 | exec(output) # This loads the generated function, which should look like this: |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 169 | |
| 170 | """ |
| 171 | def policy_output_validator(obj, ctx): |
| 172 | i1 = Privilege.objects.filter(Q(object_id=obj.id))[0] |
| 173 | if (not i1): |
| 174 | raise Exception('Necessary Failure') |
| 175 | """ |
| 176 | |
| 177 | self.assertTrue(policy_output_validator is not None) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 178 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 179 | def test_python(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 180 | xproto = """ |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 181 | policy test_policy < {{ "jack" in ["the", "box"] }} = True > |
| 182 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 183 | args = XOSProcessorArgs() |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 184 | args.inputs = xproto |
| 185 | args.target = self.target |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 186 | output = XOSProcessor.process(args) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 187 | exec(output) # This loads the generated function, which should look like this: |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 188 | |
| 189 | """ |
| 190 | def policy_output_validator(obj, ctx): |
| 191 | i2 = ('jack' in ['the', 'box']) |
| 192 | i1 = (i2 == True) |
| 193 | if (not i1): |
| 194 | raise Exception('Necessary Failure') |
| 195 | """ |
| 196 | |
| 197 | with self.assertRaises(Exception): |
| 198 | self.assertTrue(policy_output_validator({}, {}) is True) |
| 199 | |
| 200 | def test_forall(self): |
| 201 | # This one we only parse |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 202 | xproto = """ |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 203 | policy test_policy < forall Credential: Credential.obj_id = obj_id > |
| 204 | """ |
| 205 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 206 | args = XOSProcessorArgs() |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 207 | args.inputs = xproto |
| 208 | args.target = self.target |
| 209 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 210 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 211 | |
| 212 | """ |
| 213 | def policy_output_enforcer(obj, ctx): |
| 214 | i2 = Credential.objects.filter((~ Q(obj_id=obj_id)))[0] |
| 215 | i1 = (not i2) |
| 216 | return i1 |
| 217 | """ |
| 218 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 219 | self.assertIn("policy_output_validator", output) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 220 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 221 | |
| 222 | if __name__ == "__main__": |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 223 | unittest.main() |