aboutsummaryrefslogtreecommitdiffstats
path: root/moon_engine/tests/unit_python/api/wrapper/test_oslo_wrapper.py
blob: 770e9bb6ecbc0a38badce39979ec35a2d02ed395 (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
# Software Name: MOON

# Version: 5.4

# SPDX-FileCopyrightText: Copyright (c) 2018-2020 Orange and its contributors
# SPDX-License-Identifier: Apache-2.0

# This software is distributed under the 'Apache License 2.0',
# the text of which is available at 'http://www.apache.org/licenses/LICENSE-2.0.txt'
# or see the "LICENSE" file for more details.


import hug
import json
import requests


def get_subject_object_action():
    from moon_engine.api.configuration import get_configuration
    from moon_cache.cache import Cache
    from moon_utilities.auth_functions import get_api_key_for_user

    auth_headers = {"X-Api-Key": get_api_key_for_user("admin")}

    CACHE = Cache.getInstance(manager_url=get_configuration("management").get("url"),
                              incremental=get_configuration("incremental_updates"),
                              manager_api_key=get_configuration("api_token"))

    CACHE.set_current_server(url=get_configuration("management").get("url"),
                             api_key=get_api_key_for_user("admin"))

    # Note: patching the cache for the test
    CACHE.add_pipeline("b3d3e18a-bf33-40e8-b635-fd49e6634ccd", {
                             "name": "test",
                             "description": "test",
                             "host": "127.0.0.1",
                             "port": 20000,
                         })
    CACHE.add_pipeline("f8f49a77-9ceb-47b3-ac81-0f01ef71b4e0", {
                             "name": "test",
                             "description": "test",
                             "host": "127.0.0.1",
                             "port": 20000,
                         })
    response = requests.get("{}/pdp".format(get_configuration("management").get("url")),
                            headers=auth_headers)
    pdp = response.json()
    pdp_id = next(iter(pdp['pdps']))
    policy_id = pdp['pdps'][pdp_id].get("security_pipeline")[0]
    project_id = pdp['pdps'][pdp_id].get("vim_project_id")

    # response = requests.get("{}/policies".format(get_configuration("management").get("url")),
    #                         headers=auth_headers)
    # policies = response.json()
    # policy_id = next(iter(policies['policies']))

    response = requests.get("{}/policies/{}/subjects".format(
        get_configuration("management").get("url"), policy_id), headers=auth_headers)
    subjects = response.json()

    response = requests.get("{}/policies/{}/objects".format(
        get_configuration("management").get("url"), policy_id), headers=auth_headers)
    objects = response.json()

    response = requests.get("{}/policies/{}/actions".format(
        get_configuration("management").get("url"), policy_id), headers=auth_headers)
    actions = response.json()
    return subjects, objects, actions, project_id


def test_post_authz():
    from moon_engine.plugins import oslowrapper
    from moon_utilities.auth_functions import get_api_key_for_user
    from moon_cache.cache import Cache
    from moon_engine.api.configuration import get_configuration
    CACHE = Cache.getInstance(manager_url=get_configuration("management").get("url"),
                              incremental=get_configuration("incremental_updates"),
                              manager_api_key=get_configuration("api_token"))

    CACHE.set_current_server(url=get_configuration("management").get("url"), api_key=get_api_key_for_user(
        "admin"))

    auth_headers = {"X-Api-Key": get_api_key_for_user("admin")}

    response = requests.get("{}/pdp".format(get_configuration("management").get("url")),
                            headers=auth_headers)
    pdp = response.json()
    pdp_id = next(iter(pdp['pdps']))
    policy_id = pdp['pdps'][pdp_id].get("security_pipeline")[0]
    project_id = pdp['pdps'][pdp_id].get("vim_project_id")

    response = requests.get("{}/policies/{}/subjects".format(get_configuration(
        "management").get("url"), policy_id), headers=auth_headers)
    subjects = response.json()

    response = requests.get("{}/policies/{}/objects".format(get_configuration(
        "management").get("url"), policy_id), headers=auth_headers)
    objects = response.json()

    response = requests.get("{}/policies/{}/actions".format(get_configuration(
        "management").get("url"), policy_id), headers=auth_headers)
    actions = response.json()

    subjects_name = subjects['subjects'][next(iter(subjects['subjects']))]['name']
    objects_name = objects['objects'][next(iter(objects['objects']))]['name']
    actions_name = actions['actions'][next(iter(actions['actions']))]['name']

    # Note: patching the cache for the test
    from moon_cache.cache import Cache
    CACHE = Cache.getInstance()
    CACHE.add_pipeline("b3d3e18abf3340e8b635fd49e6634ccd", {
                             "name": "test",
                             "description": "test",
                             "host": "127.0.0.1",
                             "port": 20000,
                         })
    CACHE.add_pipeline("f8f49a779ceb47b3ac810f01ef71b4e0", {
                             "name": "test",
                             "description": "test",
                             "host": "127.0.0.1",
                             "port": 20000,
                         })

    _target = {
        'target': {
            "name": objects_name,
        },
        "project_id": project_id,
        "user_id": subjects_name
    }
    _credentials = {
        "project_id": project_id,
        "user_id": subjects_name
    }

    authz_data = {
        'rule': actions_name,
        'target': json.dumps(_target),
        'credentials': json.dumps(_credentials)}
    req = hug.test.post(oslowrapper, "/authz/oslo", authz_data)
    assert req.status == hug.HTTP_200


def test_authz_true():
    from moon_engine.plugins import oslowrapper

    subjects, objects, actions, project_id = get_subject_object_action()

    _target = {
        'target': {
            "name": objects['objects'][next(iter(objects['objects']))]['name'],
        },
        "project_id": project_id,
        "user_id": subjects['subjects'][next(iter(subjects['subjects']))]['name']
    }
    _credentials = {
        "project_id": project_id,
        "user_id": subjects['subjects'][next(iter(subjects['subjects']))]['name']
    }
    authz_data = {
        'rule': actions['actions'][next(iter(actions['actions']))]['name'],
        'target': json.dumps(_target),
        'credentials': json.dumps(_credentials)}

    req = hug.test.post(oslowrapper, "/authz/oslo", body=authz_data)

    assert req.status == hug.HTTP_200 and req.data is not None and req.data


def test_authz_error_response_code():
    from moon_engine.plugins import oslowrapper

    subjects, objects, actions, project_id = get_subject_object_action()

    _target = {
        'target': {
            "name": objects['objects'][next(iter(objects['objects']))]['name'],
        },
        "project_id": "a64beb1cc224474fb4badd431f3e7106",  # invalid project id
        "user_id": subjects['subjects'][next(iter(subjects['subjects']))]['name']
    }
    authz_data = {
        'rule': actions['actions'][next(iter(actions['actions']))]['name'],
        'target': json.dumps(_target),
        'credentials': 'null'}

    print(authz_data)
    req = hug.test.post(oslowrapper, "/authz/oslo", body=authz_data)

    assert req.status != hug.HTTP_200

# def test_authz_error_no_interface_key(context):
#     import moon_wrapper.server
#     server = moon_wrapper.server.main()
#     client = server.app.test_client()
#     _target = {
#         'target': {
#             "name": context.get('object_name'),
#         },
#         "project_id": context.get('project_with_no_interface_key'),
#         "user_id": context.get('subject_name')
#     }
#     authz_data = {
#         'rule': context.get('action_name'),
#         'target': json.dumps(_target),
#         'credentials': 'null'}
#     req = client.post("/authz/oslo", data=json.dumps(authz_data))
#
#     assert req.data == b"False"