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 |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 18 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
| 19 | from helpers import 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. |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 31 | The security policies here deliberately made complex in order to stress the processor. |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 32 | """ |
| 33 | class XProtoXOSSecurityTest(unittest.TestCase): |
| 34 | def setUp(self): |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 35 | self.target = XProtoTestHelpers.write_tmp_target("{{ xproto_fol_to_python_test('output',proto.policies.test_policy, None, '0') }}") |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 36 | |
| 37 | """ |
| 38 | This is the security policy for controllers |
| 39 | """ |
| 40 | def test_controller_policy(self): |
| 41 | xproto = \ |
| 42 | """ |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 43 | 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 Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 44 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 45 | args = XOSProcessorArgs() |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 46 | args.inputs = xproto |
| 47 | args.target = self.target |
| 48 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 49 | output = XOSProcessor.process(args) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 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 | i2 = ctx.user.is_admin |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 56 | 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 Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 57 | i1 = (i2 or i3) |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 58 | return i1 |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 59 | """ |
| 60 | |
| 61 | # FIXME: Test this policy by executing it |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 62 | self.assertTrue(policy_output_enforcer is not None) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 63 | |
| 64 | """ |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 65 | This is the security policy for ControllerNetworks |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 66 | """ |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 67 | def test_controller_network_policy(self): |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 68 | xproto = \ |
| 69 | """ |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 70 | 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 Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 84 | args = XOSProcessorArgs() |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 85 | args.inputs = xproto |
| 86 | args.target = self.target |
| 87 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 88 | output = XOSProcessor.process(args) |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 89 | 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 Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 132 | args = XOSProcessorArgs() |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 133 | args.inputs = xproto |
| 134 | args.target = self.target |
| 135 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 136 | output = XOSProcessor.process(args) |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 137 | |
| 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 Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 177 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 178 | args = XOSProcessorArgs() |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 179 | args.inputs = xproto |
| 180 | args.target = self.target |
| 181 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 182 | output = XOSProcessor.process(args) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 183 | |
| 184 | exec(output) # This loads the generated function, which should look like this: |
| 185 | |
| 186 | """ |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 187 | def policy_output_enforcer(obj, ctx): |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 188 | i2 = ctx.user.is_admin |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 189 | 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 Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 192 | i1 = (i2 or i3) |
| 193 | return i1 |
| 194 | """ |
| 195 | |
| 196 | # FIXME: Test this policy by executing it |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 197 | self.assertTrue(policy_output_enforcer is not None) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 198 | |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 199 | if __name__ == '__main__': |
| 200 | unittest.main() |