aboutsummaryrefslogtreecommitdiffstats
path: root/python_moondb/tests/unit_python/policies/test_policies.py
blob: 148034efd505cec97523e0df883000e76bef4fe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.

import pytest
import policies.mock_data as mock_data


def get_policies():
    from python_moondb.core import PolicyManager
    return PolicyManager.get_policies("admin")


def add_policies(policy_id=None, value=None):
    from python_moondb.core import PolicyManager
    if not value:
        value = {
            "name": "test_policiy",
            "model_id": "",
            "genre": "authz",
            "description": "test",
        }
    return PolicyManager.add_policy("admin", policy_id=policy_id, value=value)


def delete_policies(uuid=None, name=None):
    from python_moondb.core import PolicyManager
    if not uuid:
        for policy_id, policy_value in get_policies():
            if name == policy_value['name']:
                uuid = policy_id
                break
    PolicyManager.delete_policy("admin", uuid)


def update_policy(policy_id, value):
    from python_moondb.core import PolicyManager
    return PolicyManager.update_policy("admin", policy_id, value)


def get_policy_from_meta_rules(meta_rule_id):
    from python_moondb.core import PolicyManager
    return PolicyManager.get_policy_from_meta_rules("admin", meta_rule_id)


def get_rules(policy_id=None, meta_rule_id=None, rule_id=None):
    from python_moondb.core import PolicyManager
    return PolicyManager.get_rules("", policy_id, meta_rule_id, rule_id)


def add_rule(policy_id=None, meta_rule_id=None, value=None):
    from python_moondb.core import PolicyManager
    if not value:
        value = {
            "rule": ("high", "medium", "vm-action"),
            "instructions": ({"decision": "grant"}),
            "enabled": "",
        }
    return PolicyManager.add_rule("", policy_id, meta_rule_id, value)


def delete_rule(policy_id=None, rule_id=None):
    from python_moondb.core import PolicyManager
    PolicyManager.delete_rule("", policy_id, rule_id)


def test_get_policies(db):
    policies = get_policies()
    assert isinstance(policies, dict)
    assert not policies


def test_add_policies(db):
    value = {
        "name": "test_policy",
        "model_id": "",
        "genre": "authz",
        "description": "test",
    }
    policies = add_policies(value=value)
    assert isinstance(policies, dict)
    assert policies
    assert len(policies.keys()) == 1
    policy_id = list(policies.keys())[0]
    for key in ("genre", "name", "model_id", "description"):
        assert key in policies[policy_id]
        assert policies[policy_id][key] == value[key]


def test_add_policies_twice_with_same_id(db):
    policy_id = 'policy_id_1'
    value = {
        "name": "test_policy",
        "model_id": "",
        "genre": "authz",
        "description": "test",
    }
    add_policies(policy_id, value)
    with pytest.raises(Exception) as exception_info:
        add_policies(policy_id, value)
    assert str(exception_info.value) == '409: Policy Error'


def test_delete_policies(db):
    value = {
        "name": "test_policy1",
        "model_id": "",
        "genre": "authz",
        "description": "test",
    }
    policies = add_policies(value=value)
    policy_id1 = list(policies.keys())[0]
    value = {
        "name": "test_policy2",
        "model_id": "",
        "genre": "authz",
        "description": "test",
    }
    policies = add_policies(value=value)
    policy_id2 = list(policies.keys())[0]
    assert policy_id1 != policy_id2
    delete_policies(policy_id1)
    policies = get_policies()
    assert policy_id1 not in policies


def test_delete_policies_with_invalid_id(db):
    policy_id = 'policy_id_1'
    with pytest.raises(Exception) as exception_info:
        delete_policies(policy_id)
    assert str(exception_info.value) == '400: Policy Unknown'


def test_update_policy(db):
    policies = add_policies()
    policy_id = list(policies.keys())[0]
    value = {
        "name": "test_policy4",
        "model_id": "",
        "genre": "authz",
        "description": "test-3",
    }
    updated_policy = update_policy(policy_id, value)
    assert updated_policy
    for key in ("genre", "name", "model_id", "description"):
        assert key in updated_policy[policy_id]
        assert updated_policy[policy_id][key] == value[key]


def test_update_policy_with_invalid_id(db):
    policy_id = 'invalid-id'
    value = {
        "name": "test_policy4",
        "model_id": "",
        "genre": "authz",
        "description": "test-3",
    }
    with pytest.raises(Exception) as exception_info:
        update_policy(policy_id, value)
    assert str(exception_info.value) == '400: Policy Unknown'


def test_get_policy_from_meta_rules(db):
    import models.test_models as test_models
    import models.test_meta_rules as test_meta_rules
    import test_pdp as test_pdp
    meta_rule = test_meta_rules.add_meta_rule(value=mock_data.create_meta_rule())
    meta_rule_id = list(meta_rule.keys())[0]
    model = test_models.add_model(value=mock_data.create_model(meta_rule_id))
    model_id = list(model.keys())[0]
    value = mock_data.create_policy(model_id)
    policy = add_policies(value=value)
    assert policy
    policy_id = list(policy.keys())[0]
    pdp_ids = [policy_id,]
    pdp_obj = mock_data.create_pdp(pdp_ids)
    test_pdp.add_pdp(value=pdp_obj)
    matched_policy_id = get_policy_from_meta_rules(meta_rule_id)
    assert matched_policy_id
    assert policy_id == matched_policy_id


def test_get_policy_from_meta_rules_with_no_policy_ids(db):
    import test_pdp as test_pdp
    meta_rule_id = 'meta_rule_id'
    value = {
        "name": "test_pdp",
        "security_pipeline": [],
        "keystone_project_id": "keystone_project_id1",
        "description": "...",
    }
    test_pdp.add_pdp(value=value)
    matched_policy_id = get_policy_from_meta_rules(meta_rule_id)
    assert not matched_policy_id


def test_get_policy_from_meta_rules_with_no_policies(db):
    import test_pdp as test_pdp
    meta_rule_id = 'meta_rule_id'
    policy_id = 'invalid'
    pdp_ids = [policy_id,]
    pdp_obj = mock_data.create_pdp(pdp_ids)
    test_pdp.add_pdp(value=pdp_obj)
    with pytest.raises(Exception) as exception_info:
        get_policy_from_meta_rules(meta_rule_id)
    assert str(exception_info.value) == '400: Policy Unknown'


def test_get_policy_from_meta_rules_with_no_models(db):
    import models.test_meta_rules as test_meta_rules
    import test_pdp as test_pdp
    meta_rule = test_meta_rules.add_meta_rule(value=mock_data.create_meta_rule())
    meta_rule_id = list(meta_rule.keys())[0]
    model_id = 'invalid'
    value = mock_data.create_policy(model_id)
    policy = add_policies(value=value)
    assert policy
    policy_id = list(policy.keys())[0]
    pdp_ids = [policy_id,]
    pdp_obj = mock_data.create_pdp(pdp_ids)
    test_pdp.add_pdp(value=pdp_obj)
    with pytest.raises(Exception) as exception_info:
        get_policy_from_meta_rules(meta_rule_id)
    assert str(exception_info.value) == '400: Model Unknown'


def test_get_rules(db):
    value = {
        "rule": ("low", "medium", "vm-action"),
        "instructions": ({"decision": "grant"}),
        "enabled": "",
    }
    policy_id = mock_data.get_policy_id()
    meta_rule_id = "1"
    add_rule(policy_id, meta_rule_id, value)
    value = {
        "rule": ("low", "low", "vm-action"),
        "instructions": ({"decision": "grant"}),
        "enabled": "",
    }
    meta_rule_id = "1"
    add_rule(policy_id, meta_rule_id, value)
    rules = get_rules(policy_id, meta_rule_id)
    assert isinstance(rules, dict)
    assert rules
    obj = rules.get('rules')
    assert len(obj) == 2


def test_get_rules_with_invalid_policy_id_failure(db):
    rules = get_rules("invalid_policy_id", "meta_rule_id")
    assert not rules.get('meta_rule-id')
    assert len(rules.get('rules')) == 0


def test_add_rule(db):
    value = {
        "rule": ("high", "medium", "vm-action"),
        "instructions": ({"decision": "grant"}),
        "enabled": "",
    }
    policy_id = mock_data.get_policy_id()
    meta_rule_id = "1"
    rules = add_rule(policy_id, meta_rule_id, value)
    assert rules
    assert len(rules) == 1
    assert isinstance(rules, dict)
    rule_id = list(rules.keys())[0]
    for key in ("rule", "instructions", "enabled"):
        assert key in rules[rule_id]
        assert rules[rule_id][key] == value[key]


def test_delete_rule(db):
    value = {
        "rule": ("low", "low", "vm-action"),
        "instructions": ({"decision": "grant"}),
        "enabled": "",
    }
    policy_id = mock_data.get_policy_id()
    meta_rule_id = "2"
    rules = add_rule(policy_id, meta_rule_id, value)
    rule_id = list(rules.keys())[0]
    delete_rule(policy_id, rule_id)
    rules = get_rules(policy_id, meta_rule_id)
    assert not rules.get('rules')