aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/moon_manager/api/assignments.py
blob: 0b2cd20b2895aa56dbb11913c357358ffa55cc61 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# 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'.
"""
Assignments allow to connect data with elements of perimeter

"""

from flask import request
from flask_restful import Resource
import logging
from python_moonutilities.security_functions import check_auth
from python_moondb.core import PolicyManager

__version__ = "4.3.2"

logger = logging.getLogger("moon.manager.api." + __name__)


class SubjectAssignments(Resource):
    """
    Endpoint for subject assignment requests
    """

    __urls__ = (
        "/policies/<string:uuid>/subject_assignments",
        "/policies/<string:uuid>/subject_assignments/",
        "/policies/<string:uuid>/subject_assignments/<string:perimeter_id>",
        "/policies/<string:uuid>/subject_assignments/<string:perimeter_id>/<string:category_id>",
        "/policies/<string:uuid>/subject_assignments/<string:perimeter_id>/<string:category_id>/<string:data_id>",
    )

    @check_auth
    def get(self, uuid=None, perimeter_id=None, category_id=None,
            data_id=None, user_id=None):
        """Retrieve all subject assignments or a specific one for a given policy

        :param uuid: uuid of the policy
        :param perimeter_id: uuid of the subject
        :param category_id: uuid of the subject category
        :param data_id: uuid of the subject scope
        :param user_id: user ID who do the request
        :return: {
            "subject_data_id": {
                "policy_id": "ID of the policy",
                "subject_id": "ID of the subject",
                "category_id": "ID of the category",
                "assignments": "Assignments list (list of data_id)",
            }
        }
        :internal_api: get_subject_assignments
        """
        try:
            data = PolicyManager.get_subject_assignments(
                user_id=user_id, policy_id=uuid,
                subject_id=perimeter_id, category_id=category_id)
        except Exception as e:
            logger.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e)}, 500
        return {"subject_assignments": data}

    @check_auth
    def post(self, uuid=None, perimeter_id=None, category_id=None,
             data_id=None, user_id=None):
        """Create a subject assignment.

        :param uuid: uuid of the policy
        :param perimeter_id: uuid of the subject (not used here)
        :param category_id: uuid of the subject category (not used here)
        :param data_id: uuid of the subject scope (not used here)
        :param user_id: user ID who do the request
        :request body: {
            "id": "UUID of the subject",
            "category_id": "UUID of the category"
            "data_id": "UUID of the scope"
        }
        :return: {
            "subject_data_id": {
                "policy_id": "ID of the policy",
                "subject_id": "ID of the subject",
                "category_id": "ID of the category",
                "assignments": "Assignments list (list of data_id)",
            }
        }
        :internal_api: update_subject_assignment
        """
        try:
            data_id = request.json.get("data_id")
            category_id = request.json.get("category_id")
            perimeter_id = request.json.get("id")
            data = PolicyManager.add_subject_assignment(
                user_id=user_id, policy_id=uuid,
                subject_id=perimeter_id, category_id=category_id,
                data_id=data_id)
        except Exception as e:
            logger.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e)}, 500
        return {"subject_assignments": data}

    @check_auth
    def delete(self, uuid=None, perimeter_id=None, category_id=None,
               data_id=None, user_id=None):
        """Delete a subject assignment for a given policy

        :param uuid: uuid of the policy
        :param perimeter_id: uuid of the subject
        :param category_id: uuid of the subject category
        :param data_id: uuid of the subject scope
        :param user_id: user ID who do the request
        :return: {
            "result": "True or False",
            "message": "optional message"
        }
        :internal_api: delete_subject_assignment
        """
        try:
            data = PolicyManager.delete_subject_assignment(
                user_id=user_id, policy_id=uuid,
                subject_id=perimeter_id, category_id=category_id,
                data_id=data_id)
        except Exception as e:
            logger.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e)}, 500
        return {"result": True}


class ObjectAssignments(Resource):
    """
    Endpoint for object assignment requests
    """

    __urls__ = (
        "/policies/<string:uuid>/object_assignments",
        "/policies/<string:uuid>/object_assignments/",
        "/policies/<string:uuid>/object_assignments/<string:perimeter_id>",
        "/policies/<string:uuid>/object_assignments/<string:perimeter_id>/<string:category_id>",
        "/policies/<string:uuid>/object_assignments/<string:perimeter_id>/<string:category_id>/<string:data_id>",
    )

    @check_auth
    def get(self, uuid=None, perimeter_id=None, category_id=None,
            data_id=None, user_id=None):
        """Retrieve all object assignment or a specific one for a given policy

        :param uuid: uuid of the policy
        :param perimeter_id: uuid of the object
        :param category_id: uuid of the object category
        :param data_id: uuid of the object scope
        :param user_id: user ID who do the request
        :return: {
            "object_data_id": {
                "policy_id": "ID of the policy",
                "object_id": "ID of the object",
                "category_id": "ID of the category",
                "assignments": "Assignments list (list of data_id)",
            }
        }
        :internal_api: get_object_assignments
        """
        try:
            data = PolicyManager.get_object_assignments(
                user_id=user_id, policy_id=uuid,
                object_id=perimeter_id, category_id=category_id)
        except Exception as e:
            logger.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e)}, 500
        return {"object_assignments": data}

    @check_auth
    def post(self, uuid=None, perimeter_id=None, category_id=None,
             data_id=None, user_id=None):
        """Create an object assignment.

        :param uuid: uuid of the policy
        :param perimeter_id: uuid of the object (not used here)
        :param category_id: uuid of the object category (not used here)
        :param data_id: uuid of the object scope (not used here)
        :param user_id: user ID who do the request
        :request body: {
            "id": "UUID of the action",
            "category_id": "UUID of the category"
            "data_id": "UUID of the scope"
        }
        :return: {
            "object_data_id": {
                "policy_id": "ID of the policy",
                "object_id": "ID of the object",
                "category_id": "ID of the category",
                "assignments": "Assignments list (list of data_id)",
            }
        }
        :internal_api: update_object_assignment
        """
        try:
            data_id = request.json.get("data_id")
            category_id = request.json.get("category_id")
            perimeter_id = request.json.get("id")
            data = PolicyManager.add_object_assignment(
                user_id=user_id, policy_id=uuid,
                object_id=perimeter_id, category_id=category_id,
                data_id=data_id)
        except Exception as e:
            logger.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e)}, 500
        return {"object_assignments": data}

    @check_auth
    def delete(self, uuid=None, perimeter_id=None, category_id=None,
               data_id=None, user_id=None):
        """Delete a object assignment for a given policy

        :param uuid: uuid of the policy
        :param perimeter_id: uuid of the object
        :param category_id: uuid of the object category
        :param data_id: uuid of the object scope
        :param user_id: user ID who do the request
        :return: {
            "result": "True or False",
            "message": "optional message"
        }
        :internal_api: delete_object_assignment
        """
        try:
            data = PolicyManager.delete_object_assignment(
                user_id=user_id, policy_id=uuid,
                object_id=perimeter_id, category_id=category_id,
                data_id=data_id)
        except Exception as e:
            logger.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e)}, 500
        return {"result": True}


class ActionAssignments(Resource):
    """
    Endpoint for action assignment requests
    """

    __urls__ = (
        "/policies/<string:uuid>/action_assignments",
        "/policies/<string:uuid>/action_assignments/",
        "/policies/<string:uuid>/action_assignments/<string:perimeter_id>",
        "/policies/<string:uuid>/action_assignments/<string:perimeter_id>/<string:category_id>",
        "/policies/<string:uuid>/action_assignments/<string:perimeter_id>/<string:category_id>/<string:data_id>",
    )

    @check_auth
    def get(self, uuid=None, perimeter_id=None, category_id=None,
            data_id=None, user_id=None):
        """Retrieve all action assignment or a specific one for a given policy

        :param uuid: uuid of the policy
        :param perimeter_id: uuid of the action
        :param category_id: uuid of the action category
        :param data_id: uuid of the action scope
        :param user_id: user ID who do the request
        :return: {
            "action_data_id": {
                "policy_id": "ID of the policy",
                "object_id": "ID of the action",
                "category_id": "ID of the category",
                "assignments": "Assignments list (list of data_id)",
            }
        }
        :internal_api: get_action_assignments
        """
        try:
            data = PolicyManager.get_action_assignments(
                user_id=user_id, policy_id=uuid,
                action_id=perimeter_id, category_id=category_id)
        except Exception as e:
            logger.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e)}, 500
        return {"action_assignments": data}

    @check_auth
    def post(self, uuid=None, perimeter_id=None, category_id=None,
             data_id=None, user_id=None):
        """Create an action assignment.

        :param uuid: uuid of the policy
        :param perimeter_id: uuid of the action (not used here)
        :param category_id: uuid of the action category (not used here)
        :param data_id: uuid of the action scope (not used here)
        :param user_id: user ID who do the request
        :request body: {
            "id": "UUID of the action",
            "category_id": "UUID of the category",
            "data_id": "UUID of the scope"
        }
        :return: {
            "action_data_id": {
                "policy_id": "ID of the policy",
                "object_id": "ID of the action",
                "category_id": "ID of the category",
                "assignments": "Assignments list (list of data_id)",
            }
        }
        :internal_api: update_action_assignment
        """
        try:
            data_id = request.json.get("data_id")
            category_id = request.json.get("category_id")
            perimeter_id = request.json.get("id")
            data = PolicyManager.add_action_assignment(
                user_id=user_id, policy_id=uuid,
                action_id=perimeter_id, category_id=category_id,
                data_id=data_id)
        except Exception as e:
            logger.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e)}, 500
        return {"action_assignments": data}

    @check_auth
    def delete(self, uuid=None, perimeter_id=None, category_id=None,
               data_id=None, user_id=None):
        """Delete a action assignment for a given policy

        :param uuid: uuid of the policy
        :param perimeter_id: uuid of the action
        :param category_id: uuid of the action category
        :param data_id: uuid of the action scope
        :param user_id: user ID who do the request
        :return: {
            "result": "True or False",
            "message": "optional message"
        }
        :internal_api: delete_action_assignment
        """
        try:
            data = PolicyManager.delete_action_assignment(
                user_id=user_id, policy_id=uuid,
                action_id=perimeter_id, category_id=category_id,
                data_id=data_id)
        except Exception as e:
            logger.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e)}, 500
        return {"result": True}