diff options
author | Thomas Duval <thomas.duval@orange.com> | 2020-06-03 10:06:52 +0200 |
---|---|---|
committer | Thomas Duval <thomas.duval@orange.com> | 2020-06-03 10:06:52 +0200 |
commit | 7bb53c64da2dcf88894bfd31503accdd81498f3d (patch) | |
tree | 4310e12366818af27947b5e2c80cb162da93a4b5 /python_moonutilities | |
parent | cbea4e360e9bfaa9698cf7c61c83c96a1ba89b8c (diff) |
Update to new version 5.4HEADstable/jermamaster
Signed-off-by: Thomas Duval <thomas.duval@orange.com>
Change-Id: Idcd868133d75928a1ffd74d749ce98503e0555ea
Diffstat (limited to 'python_moonutilities')
25 files changed, 0 insertions, 4480 deletions
diff --git a/python_moonutilities/.gitignore b/python_moonutilities/.gitignore deleted file mode 100644 index 7bff7318..00000000 --- a/python_moonutilities/.gitignore +++ /dev/null @@ -1,105 +0,0 @@ -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -.hypothesis/ -.pytest_cache/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# pyenv -.python-version - -# celery beat schedule file -celerybeat-schedule - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ - diff --git a/python_moonutilities/Changelog b/python_moonutilities/Changelog deleted file mode 100644 index d001c892..00000000 --- a/python_moonutilities/Changelog +++ /dev/null @@ -1,157 +0,0 @@ -# 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'. - - -CHANGES -======= - -0.1.0 ------ -- First version of the moon_utilities library. - -1.0.0 ------ -- First public version of the moon_utilities library. - -1.0.1 ------ -- Update setup.py to force the installation of requirements. - -1.0.2 ------ -- Test PyPi upload - -1.1.0 ------ -- Add functions to get configuration from Consul - -1.1.1 ------ -- Add a missing requirements - -1.2.0 ------ -- Add authentication features for interface - -1.3.0 ------ -- Add cache functionality - -1.3.1 ------ -- Delete Oslo config possibilities - -1.3.2 ------ -- Delete Oslo logging and config - -1.3.3 ------ -- Update the cache - -1.3.4 ------ -- Fix a bug on the connection between interface and authz - -1.4.0 ------ -- Add a waiting loop when the Keystone server is not currently available - -1.4.1 ------ -- Cleanup moon_utilities code - -1.4.2 ------ -- Update the name of the library (from moon_utilities) - -1.4.3 ------ -- Fix a bug in MANIFEST.in - -1.4.4 ------ -- Code cleaning - -1.4.5 ------ -- Add PdpKeystoneMappingConflict exception - -1.4.6 ------ -- Add WrapperConflict, PipelineConflict, SlaveNameUnknown exceptions - -1.4.7 ------ -- Delete the auth.py file to remove some code duplication - -1.4.8 ------ -- Add SubjectScopeExisting, ObjectScopeExisting, ActionScopeExisting exceptions - -1.4.9 ------ -- Add some exceptions when deletion of elements is impossible - -1.4.10 ------ -- Add CategoryNameInvalid and PerimeterNameInvalid exceptions - -1.4.11 ------ -- Add validate_data function - -1.4.12 ------ -- Fix a bug for the authz component -- updating Validation to be on mandatory keys only - -1.4.13 ------ -- Adding InvalidKey , InvalidContent exception -- fix error code of 'CategoryNameInvalid' to be 400 -- updating error of post/patch to mention key name - -1.4.14 ------ -- Adding updates to log -1.4.15 ------ -- Delete the check on each key send in request body for POST /models - -1.4.15-1 --------- -- Revert to the previous functionality - -1.4.16 ------ -- Adding exceptions for MetaRuleNotLinkedWithPolicyModel , CategoryNotAssignedMetaRule - -1.4.17 ------ -- Update the security verification on attributes - -1.4.18 ------ -- Allow None values in input attributes (None is replaced by an empty string) - -1.4.19 ------ -- Allow boolean values in input attributes - -1.4.20 ------ -- Adding DeleteSubjectCategoryWithMetaRule exception -- Adding MetaRuleUpdate , PolicyUpdateError, ModelContentError exception -- Adding DeleteObjectCategoryWithMetaRule DeleteActionCategoryWithMetaRule exceptions - -1.4.21 ------ -- Allow in the cache the search of a perimeter element by it ID - -1.4.22 ------ -- Enable the target update in context manager -- Fix assignments update in cache diff --git a/python_moonutilities/Jenkinsfile b/python_moonutilities/Jenkinsfile deleted file mode 100644 index 95939e9b..00000000 --- a/python_moonutilities/Jenkinsfile +++ /dev/null @@ -1,10 +0,0 @@ -pipeline { - agent { docker { image 'python:3.5.1' } } - stages { - stage('build') { - steps { - sh 'python --version' - } - } - } -}
\ No newline at end of file diff --git a/python_moonutilities/LICENSE b/python_moonutilities/LICENSE deleted file mode 100644 index d6456956..00000000 --- a/python_moonutilities/LICENSE +++ /dev/null @@ -1,202 +0,0 @@ - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/python_moonutilities/MANIFEST.in b/python_moonutilities/MANIFEST.in deleted file mode 100644 index 2a5ac509..00000000 --- a/python_moonutilities/MANIFEST.in +++ /dev/null @@ -1,10 +0,0 @@ -# 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'. - -include README.md -include LICENSE -include Changelog -include setup.py -include requirements.txt diff --git a/python_moonutilities/README.md b/python_moonutilities/README.md deleted file mode 100644 index 8e21966a..00000000 --- a/python_moonutilities/README.md +++ /dev/null @@ -1,33 +0,0 @@ -# python-moonutilities Package -This package contains the core module for the Moon project. -It is designed to provide authorization feature to all OpenStack components. - -For any other information, refer to the parent project: - - https://git.opnfv.org/moon - -python-moonutilities is a common Python lib for other Moon Python packages - -## Build -### Build Python Package -```bash -cd ${MOON_HOME}/python_moonutilities -python3 setup.py sdist bdist_wheel -``` - -### Push Python Package to PIP -```bash -cd ${MOON_HOME}/python_moonutilities -gpg --detach-sign -u "${GPG_ID}" -a dist/python_moonutilities-X.Y.Z-py3-none-any.whl -gpg --detach-sign -u "${GPG_ID}" -a dist/python_moonutilities-X.Y.Z.tar.gz -twine upload dist/python_moonutilities-X.Y.Z-py3-none-any.whl dist/python_moonutilities-X.Y.Z-py3-none-any.whl.asc -twine upload dist/python_moonutilities-X.Y.Z.tar.gz dist/python_moonutilities-X.Y.Z.tar.gz.asc -``` - -## Test -### Python Unit Test -launch Docker for Python unit tests -```bash -cd ${MOON_HOME}/python_moonutilities -docker run --rm --volume $(pwd):/data wukongsun/moon_python_unit_test:latest -``` diff --git a/python_moonutilities/python_moonutilities/__init__.py b/python_moonutilities/python_moonutilities/__init__.py deleted file mode 100644 index 6e924e93..00000000 --- a/python_moonutilities/python_moonutilities/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# 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'. - -__version__ = "1.4.22" diff --git a/python_moonutilities/python_moonutilities/cache.py b/python_moonutilities/python_moonutilities/cache.py deleted file mode 100644 index 49a3ef5b..00000000 --- a/python_moonutilities/python_moonutilities/cache.py +++ /dev/null @@ -1,703 +0,0 @@ -import logging -import time -import python_moonutilities.request_wrapper as requests -from uuid import uuid4 -from python_moonutilities import configuration, exceptions - -logger = logging.getLogger("moon.utilities.cache") - - -class Cache(object): - # TODO (asteroide): set cache integer in CONF file - ''' - [NOTE] Propose to define the following variables inside the init method - as defining them out side the init, will be treated as private static variables - and keep tracks with any changes done anywhere - for more info : you can check https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables - ''' - __UPDATE_INTERVAL = 10 - - __CONTAINERS = {} - __CONTAINERS_UPDATE = 0 - - __CONTAINER_CHAINING_UPDATE = 0 - __CONTAINER_CHAINING = {} - - __PDP = {} - __PDP_UPDATE = 0 - - __POLICIES = {} - __POLICIES_UPDATE = 0 - - __MODELS = {} - __MODELS_UPDATE = 0 - - __SUBJECTS = {} - __OBJECTS = {} - __ACTIONS = {} - - __SUBJECT_ASSIGNMENTS = {} - __OBJECT_ASSIGNMENTS = {} - __ACTION_ASSIGNMENTS = {} - - __SUBJECT_CATEGORIES = {} - __SUBJECT_CATEGORIES_UPDATE = 0 - __OBJECT_CATEGORIES = {} - __OBJECT_CATEGORIES_UPDATE = 0 - __ACTION_CATEGORIES = {} - __ACTION_CATEGORIES_UPDATE = 0 - - __META_RULES = {} - __META_RULES_UPDATE = 0 - - __RULES = {} - __RULES_UPDATE = 0 - - __AUTHZ_REQUESTS = {} - - def __init__(self): - self.manager_url = "{}://{}:{}".format( - configuration.get_components()['manager'].get('protocol', 'http'), - configuration.get_components()['manager']['hostname'], - configuration.get_components()['manager']['port'] - ) - self.orchestrator_url = "{}://{}:{}".format( - configuration.get_components()['orchestrator'].get('protocol', 'http'), - configuration.get_components()['orchestrator']['hostname'], - configuration.get_components()['orchestrator']['port'] - ) - - def update(self): - self.__update_container() - self.__update_pdp() - self.__update_policies() - self.__update_models() - for key, value in self.__PDP.items(): - # LOG.info("Updating container_chaining with {}".format(value["keystone_project_id"])) - if "keystone_project_id" in value: - self.__update_container_chaining(value["keystone_project_id"]) - else: - logger.warning("no 'keystone_project_id' found while Updating container_chaining") - - @property - def authz_requests(self): - return self.__AUTHZ_REQUESTS - - # perimeter functions - - @property - def subjects(self): - return self.__SUBJECTS - - def __update_subjects(self, policy_id): - response = requests.get("{}/policies/{}/subjects".format(self.manager_url, policy_id)) - if 'subjects' in response.json(): - self.__SUBJECTS[policy_id] = response.json()['subjects'] - else: - raise exceptions.SubjectUnknown("Cannot find subject within policy_id {}".format(policy_id)) - - def get_subject(self, policy_id, name): - if not policy_id: - raise exceptions.PolicyUnknown("Cannot find policy within policy_id {}".format(policy_id)) - - if policy_id in self.subjects: - for _subject_id, _subject_dict in self.subjects[policy_id].items(): - if _subject_id == name or _subject_dict.get("name") == name: - return _subject_id - - self.__update_subjects(policy_id) - - if policy_id in self.subjects: - for _subject_id, _subject_dict in self.subjects[policy_id].items(): - if _subject_id == name or _subject_dict.get("name") == name: - return _subject_id - - raise exceptions.SubjectUnknown("Cannot find subject {}".format(name)) - - @property - def objects(self): - return self.__OBJECTS - - def __update_objects(self, policy_id): - response = requests.get("{}/policies/{}/objects".format(self.manager_url, policy_id)) - if 'objects' in response.json(): - self.__OBJECTS[policy_id] = response.json()['objects'] - else: - raise exceptions.ObjectUnknown("Cannot find object within policy_id {}".format(policy_id)) - - def get_object(self, policy_id, name): - if not policy_id: - raise exceptions.PolicyUnknown("Cannot find policy within policy_id {}".format(policy_id)) - - if policy_id in self.objects: - for _object_id, _object_dict in self.__OBJECTS[policy_id].items(): - if _object_id == name or _object_dict.get("name") == name: - return _object_id - - self.__update_objects(policy_id) - - if policy_id in self.objects: - for _object_id, _object_dict in self.__OBJECTS[policy_id].items(): - if _object_id == name or _object_dict.get("name") == name: - return _object_id - - raise exceptions.ObjectUnknown("Cannot find object {}".format(name)) - - @property - def actions(self): - return self.__ACTIONS - - def __update_actions(self, policy_id): - response = requests.get("{}/policies/{}/actions".format(self.manager_url, policy_id)) - - if 'actions' in response.json(): - self.__ACTIONS[policy_id] = response.json()['actions'] - else: - raise exceptions.ActionUnknown("Cannot find action within policy_id {}".format(policy_id)) - - def get_action(self, policy_id, name): - if not policy_id: - raise exceptions.PolicyUnknown("Cannot find policy within policy_id {}".format(policy_id)) - - if policy_id in self.actions: - for _action_id, _action_dict in self.__ACTIONS[policy_id].items(): - if _action_id == name or _action_dict.get("name") == name: - return _action_id - - self.__update_actions(policy_id) - - for _action_id, _action_dict in self.__ACTIONS[policy_id].items(): - if _action_id == name or _action_dict.get("name") == name: - return _action_id - - raise exceptions.ActionUnknown("Cannot find action {}".format(name)) - - # meta_rule functions - - @property - def meta_rules(self): - current_time = time.time() - if self.__META_RULES_UPDATE + self.__UPDATE_INTERVAL < current_time: - self.__META_RULES_UPDATE = current_time - self.__update_meta_rules() - self.__META_RULES_UPDATE = current_time - return self.__META_RULES - - def __update_meta_rules(self): - response = requests.get("{}/meta_rules".format(self.manager_url)) - - if 'meta_rules' in response.json(): - self.__META_RULES = response.json()['meta_rules'] - else: - raise exceptions.MetaRuleUnknown("Cannot find meta rules") - - # rule functions - - @property - def rules(self): - current_time = time.time() - if self.__RULES_UPDATE + self.__UPDATE_INTERVAL < current_time: - self.__RULES_UPDATE = current_time - self.__update_rules() - self.__RULES_UPDATE = current_time - return self.__RULES - - def __update_rules(self): - for policy_id in self.policies: - logger.debug("Get {}".format("{}/policies/{}/rules".format( - self.manager_url, policy_id))) - - response = requests.get("{}/policies/{}/rules".format( - self.manager_url, policy_id)) - if 'rules' in response.json(): - self.__RULES[policy_id] = response.json()['rules'] - else: - logger.warning(" no 'rules' found within policy_id: {}".format(policy_id)) - - logger.debug("UPDATE RULES {}".format(self.__RULES)) - - # assignment functions - - def update_assignments(self, policy_id=None, perimeter_id=None): - if policy_id: - self.__update_subject_assignments(policy_id=policy_id, perimeter_id=perimeter_id) - self.__update_object_assignments(policy_id=policy_id, perimeter_id=perimeter_id) - self.__update_action_assignments(policy_id=policy_id, perimeter_id=perimeter_id) - else: - for policy_id in self.__POLICIES: - self.__update_subject_assignments(policy_id=policy_id, perimeter_id=perimeter_id) - self.__update_object_assignments(policy_id=policy_id, perimeter_id=perimeter_id) - self.__update_action_assignments(policy_id=policy_id, perimeter_id=perimeter_id) - - @property - def subject_assignments(self): - return self.__SUBJECT_ASSIGNMENTS - - def __update_subject_assignments(self, policy_id, perimeter_id=None): - if perimeter_id: - response = requests.get("{}/policies/{}/subject_assignments/{}".format( - self.manager_url, policy_id, perimeter_id)) - else: - response = requests.get("{}/policies/{}/subject_assignments".format( - self.manager_url, policy_id)) - - if 'subject_assignments' in response.json(): - if policy_id not in self.subject_assignments: - self.__SUBJECT_ASSIGNMENTS[policy_id] = {} - self.__SUBJECT_ASSIGNMENTS[policy_id] = response.json()['subject_assignments'] - else: - raise exceptions.SubjectAssignmentUnknown( - "Cannot find subject assignment within policy_id {}".format(policy_id)) - - def get_subject_assignments(self, policy_id, perimeter_id, category_id): - if not policy_id: - raise exceptions.PolicyUnknown("Cannot find policy within policy_id {}".format(policy_id)) - - if policy_id not in self.subject_assignments: - self.__update_subject_assignments(policy_id, perimeter_id) - - for key, value in self.subject_assignments[policy_id].items(): - if all(k in value for k in ("subject_id", "category_id", "assignments")): - if perimeter_id == value['subject_id'] and category_id == value['category_id']: - return value['assignments'] - else: - logger.warning("'subject_id' or 'category_id' or 'assignments'" - " keys are not found in subject_assignments") - return [] - - @property - def object_assignments(self): - return self.__OBJECT_ASSIGNMENTS - - def __update_object_assignments(self, policy_id, perimeter_id=None): - if perimeter_id: - response = requests.get("{}/policies/{}/object_assignments/{}".format( - self.manager_url, policy_id, perimeter_id)) - else: - response = requests.get("{}/policies/{}/object_assignments".format( - self.manager_url, policy_id)) - - if 'object_assignments' in response.json(): - if policy_id not in self.object_assignments: - self.__OBJECT_ASSIGNMENTS[policy_id] = {} - - self.__OBJECT_ASSIGNMENTS[policy_id] = response.json()['object_assignments'] - else: - raise exceptions.ObjectAssignmentUnknown( - "Cannot find object assignment within policy_id {}".format(policy_id)) - - def get_object_assignments(self, policy_id, perimeter_id, category_id): - if not policy_id: - raise exceptions.PolicyUnknown("Cannot find policy within policy_id {}".format(policy_id)) - - if policy_id not in self.object_assignments: - self.__update_object_assignments(policy_id, perimeter_id) - - for key, value in self.object_assignments[policy_id].items(): - if all(k in value for k in ("object_id", "category_id", "assignments")): - if perimeter_id == value['object_id'] and category_id == value['category_id']: - return value['assignments'] - else: - logger.warning("'object_id' or 'category_id' or'assignments'" - " keys are not found in object_assignments") - return [] - - @property - def action_assignments(self): - return self.__ACTION_ASSIGNMENTS - - def __update_action_assignments(self, policy_id, perimeter_id=None): - if perimeter_id: - response = requests.get("{}/policies/{}/action_assignments/{}".format( - self.manager_url, policy_id, perimeter_id)) - else: - response = requests.get("{}/policies/{}/action_assignments".format( - self.manager_url, policy_id)) - - if 'action_assignments' in response.json(): - if policy_id not in self.__ACTION_ASSIGNMENTS: - self.__ACTION_ASSIGNMENTS[policy_id] = {} - - self.__ACTION_ASSIGNMENTS[policy_id] = response.json()['action_assignments'] - else: - raise exceptions.ActionAssignmentUnknown( - "Cannot find action assignment within policy_id {}".format(policy_id)) - - def get_action_assignments(self, policy_id, perimeter_id, category_id): - if not policy_id: - raise exceptions.PolicyUnknown("Cannot find policy within policy_id {}".format(policy_id)) - - if policy_id not in self.action_assignments: - self.__update_action_assignments(policy_id, perimeter_id) - - for key, value in self.action_assignments[policy_id].items(): - if all(k in value for k in ("action_id", "category_id", "assignments")): - if perimeter_id == value['action_id'] and category_id == value['category_id']: - return value['assignments'] - else: - logger.warning("'action_id' or 'category_id' or'assignments'" - " keys are not found in action_assignments") - return [] - - # category functions - - @property - def subject_categories(self): - current_time = time.time() - if self.__SUBJECT_CATEGORIES_UPDATE + self.__UPDATE_INTERVAL < current_time: - self.__SUBJECT_CATEGORIES_UPDATE = current_time - self.__update_subject_categories() - self.__SUBJECT_CATEGORIES_UPDATE = current_time - return self.__SUBJECT_CATEGORIES - - def __update_subject_categories(self): - response = requests.get("{}/policies/subject_categories".format( - self.manager_url)) - - if 'subject_categories' in response.json(): - self.__SUBJECT_CATEGORIES.update(response.json()['subject_categories']) - else: - raise exceptions.SubjectCategoryUnknown("Cannot find subject category") - - @property - def object_categories(self): - current_time = time.time() - if self.__OBJECT_CATEGORIES_UPDATE + self.__UPDATE_INTERVAL < current_time: - self.__OBJECT_CATEGORIES_UPDATE = current_time - self.__update_object_categories() - self.__OBJECT_CATEGORIES_UPDATE = current_time - return self.__OBJECT_CATEGORIES - - def __update_object_categories(self): - response = requests.get("{}/policies/object_categories".format(self.manager_url)) - - if 'object_categories' in response.json(): - self.__OBJECT_CATEGORIES.update(response.json()['object_categories']) - else: - raise exceptions.ObjectCategoryUnknown("Cannot find object category") - - @property - def action_categories(self): - current_time = time.time() - if self.__ACTION_CATEGORIES_UPDATE + self.__UPDATE_INTERVAL < current_time: - self.__ACTION_CATEGORIES_UPDATE = current_time - self.__update_action_categories() - self.__ACTION_CATEGORIES_UPDATE = current_time - return self.__ACTION_CATEGORIES - - def __update_action_categories(self): - response = requests.get("{}/policies/action_categories".format(self.manager_url)) - - if 'action_categories' in response.json(): - self.__ACTION_CATEGORIES.update(response.json()['action_categories']) - else: - raise exceptions.ActionCategoryUnknown("Cannot find action category") - - # PDP functions - - def __update_pdp(self): - response = requests.get("{}/pdp".format(self.manager_url)) - pdp = response.json() - if 'pdps' in pdp: - for _pdp in pdp["pdps"].values(): - if "keystone_project_id" in _pdp and _pdp['keystone_project_id'] not in self.container_chaining: - self.__CONTAINER_CHAINING[_pdp['keystone_project_id']] = {} - # Note (asteroide): force update of chaining - self.__update_container_chaining(_pdp['keystone_project_id']) - for key, value in pdp["pdps"].items(): - self.__PDP[key] = value - - else: - raise exceptions.PdpError("Cannot find 'pdps' key") - - @property - def pdp(self): - """Policy Decision Point - Example of content: - { - "pdp_id": { - "keystone_project_id": "keystone_project_id", - "name": "pdp1", - "description": "test", - "security_pipeline": [ - "policy_id" - ] - } - } - - :return: - """ - current_time = time.time() - if self.__PDP_UPDATE + self.__UPDATE_INTERVAL < current_time: - self.__PDP_UPDATE = current_time - self.__update_pdp() - self.__PDP_UPDATE = current_time - return self.__PDP - - # policy functions - def __update_policies(self): - response = requests.get("{}/policies".format(self.manager_url)) - policies = response.json() - - if 'policies' in policies: - for key, value in policies["policies"].items(): - self.__POLICIES[key] = value - else: - raise exceptions.PolicytNotFound("Cannot find 'policies' key") - - @property - def policies(self): - current_time = time.time() - if self.__POLICIES_UPDATE + self.__UPDATE_INTERVAL < current_time: - self.__POLICIES_UPDATE = current_time - self.__update_policies() - self.__POLICIES_UPDATE = current_time - return self.__POLICIES - - # model functions - - def __update_models(self): - response = requests.get("{}/models".format(self.manager_url)) - models = response.json() - if 'models' in models: - for key, value in models["models"].items(): - self.__MODELS[key] = value - else: - raise exceptions.ModelNotFound("Cannot find 'models' key") - - @property - def models(self): - current_time = time.time() - if self.__MODELS_UPDATE + self.__UPDATE_INTERVAL < current_time: - self.__MODELS_UPDATE = current_time - self.__update_models() - self.__MODELS_UPDATE = current_time - return self.__MODELS - - # helper functions - - def get_policy_from_meta_rules(self, meta_rule_id): - for pdp_key, pdp_value in self.pdp.items(): - if "security_pipeline" in pdp_value: - for policy_id in pdp_value["security_pipeline"]: - if policy_id in self.policies and "model_id" in self.policies[policy_id]: - model_id = self.policies[policy_id]["model_id"] - if model_id in self.models and "meta_rules" in self.models[model_id]: - if meta_rule_id in self.models[model_id]["meta_rules"]: - return policy_id - else: - logger.warning( - "Cannot find model_id: {} within " - "models and 'meta_rules' key".format(model_id)) - else: - logger.warning( - "Cannot find policy_id: {} " - "within policies and 'model_id' key".format( - policy_id)) - else: - logger.warning("Cannot find 'security_pipeline' " - "key within pdp ") - - def get_meta_rule_ids_from_pdp_value(self, pdp_value): - meta_rules = [] - if "security_pipeline" in pdp_value: - for policy_id in pdp_value["security_pipeline"]: - if policy_id not in self.policies or "model_id" not in self.policies[policy_id]: - raise exceptions.PolicyUnknown("Cannot find 'models' key") - model_id = self.policies[policy_id]["model_id"] - if model_id not in self.models or 'meta_rules' not in self.models[model_id]: - raise exceptions.ModelNotFound("Cannot find 'models' key") - for meta_rule in self.models[model_id]["meta_rules"]: - meta_rules.append(meta_rule) - return meta_rules - raise exceptions.PdpContentError - - def get_pdp_from_keystone_project(self, keystone_project_id): - for pdp_key, pdp_value in self.pdp.items(): - if "keystone_project_id" in pdp_value and \ - keystone_project_id == pdp_value["keystone_project_id"]: - return pdp_key - - def get_keystone_project_id_from_policy_id(self, policy_id): - for pdp_key, pdp_value in self.pdp.items(): - if "security_pipeline" in pdp_value and \ - "keystone_project_id" in pdp_value: - if policy_id in pdp_value["security_pipeline"]: - return pdp_value["keystone_project_id"] - else: - logger.warning(" 'security_pipeline','keystone_project_id' " - "key not in pdp {}".format(pdp_value)) - - def get_keystone_project_id_from_pdp_id(self, pdp_id): - if pdp_id in self.pdp: - pdp_value = self.pdp.get(pdp_id) - if "security_pipeline" in pdp_value and \ - "keystone_project_id" in pdp_value: - return pdp_value["keystone_project_id"] - logger.warning("Unknown PDP ID".format(pdp_id)) - - def get_containers_from_keystone_project_id(self, keystone_project_id, - meta_rule_id=None): - for container_id, container_values in self.containers.items(): - for container_value in container_values: - if 'keystone_project_id' not in container_value: - continue - if container_value['keystone_project_id'] == keystone_project_id: - if not meta_rule_id: - yield container_id, container_value - elif "meta_rule_id" in container_value and \ - container_value.get('meta_rule_id') == meta_rule_id: - yield container_id, container_value - break - - # containers functions - - def __update_container(self): - response = requests.get("{}/pods".format(self.orchestrator_url)) - pods = response.json() - if "pods" in pods: - for key, value in pods["pods"].items(): - # if key not in self.__CONTAINERS: - self.__CONTAINERS[key] = value - # else: - # for container in value: - # self.__CONTAINERS[key].update(value) - else: - raise exceptions.PodError("Cannot find 'pods' key") - - def add_container(self, container_data): - """Add a new container in the cache - - :param container_data: dictionary with information for the container - Example: - { - "name": name, - "hostname": name, - "port": { - "PrivatePort": tcp_port, - "Type": "tcp", - "IP": "0.0.0.0", - "PublicPort": tcp_port - }, - "keystone_project_id": uuid, - "pdp_id": self.CACHE.get_pdp_from_keystone_project(uuid), - "meta_rule_id": meta_rule_id, - "container_name": container_name, - "plugin_name": plugin_name - "container_id": "container_id" - } - - :return: - """ - if all(k in container_data for k in ("keystone_project_id", "name", "container_id", "policy_id", - "meta_rule_id", "port")) \ - and all(k in container_data['port'] for k in ("PublicPort", "Type", "IP", "PrivatePort")): - - self.__CONTAINERS[uuid4().hex] = { - "keystone_project_id": container_data['keystone_project_id'], - "name": container_data['name'], - "container_id": container_data['container_id'], - "hostname": container_data['name'], - "policy_id": container_data['policy_id'], - "meta_rule_id": container_data['meta_rule_id'], - "port": [ - { - "PublicPort": container_data['port']["PublicPort"], - "Type": container_data['port']["Type"], - "IP": container_data['port']["IP"], - "PrivatePort": container_data['port']["PrivatePort"] - } - ], - "genre": container_data['plugin_name'] - } - self.__update_container_chaining(self.get_keystone_project_id_from_policy_id(container_data['policy_id'])) - else: - raise exceptions.ContainerError("Cannot find 'container' parameters key") - - @property - def containers(self): - """Containers cache - example of content : - { - "policy_uuid1": "component_hostname1", - "policy_uuid2": "component_hostname2", - } - :return: - """ - current_time = time.time() - if self.__CONTAINERS_UPDATE + self.__UPDATE_INTERVAL < current_time: - self.__CONTAINERS_UPDATE = current_time - self.__update_container() - self.__CONTAINERS_UPDATE = current_time - return self.__CONTAINERS - - @property - def container_chaining(self): - """Cache for mapping Keystone Project ID with meta_rule ID and container ID - Example of content: - { - "keystone_project_id": [ - { - "container_id": "container_id", - "genre": "genre", - "policy_id": "policy_id", - "meta_rule_id": "meta_rule_id", - } - ] - } - - :return: - """ - current_time = time.time() - if self.__CONTAINER_CHAINING_UPDATE + self.__UPDATE_INTERVAL < current_time: - self.__CONTAINER_CHAINING_UPDATE = current_time - for key, value in self.pdp.items(): - if "keystone_project_id" in value: - if not value["keystone_project_id"]: - continue - self.__update_container_chaining(value["keystone_project_id"]) - else: - logger.warning("no 'keystone_project_id' found") - self.__CONTAINER_CHAINING_UPDATE = current_time - return self.__CONTAINER_CHAINING - - def __update_container_chaining(self, keystone_project_id): - container_ids = [] - for pdp_id, pdp_value, in self.__PDP.items(): - if pdp_value: - if all(k in pdp_value for k in ("keystone_project_id", "security_pipeline")) \ - and pdp_value["keystone_project_id"] == keystone_project_id: - for policy_id in pdp_value["security_pipeline"]: - if policy_id in self.policies and "model_id" in self.policies[policy_id]: - model_id = self.policies[policy_id]['model_id'] - if model_id in self.models and "meta_rules" in self.models[model_id]: - for meta_rule_id in self.models[model_id]["meta_rules"]: - for container_id, container_value in self.get_containers_from_keystone_project_id( - keystone_project_id, - meta_rule_id - ): - if "name" in container_value: - if all(k in container_value for k in ("genre", "port")): - container_ids.append( - { - "container_id": container_value["name"], - "genre": container_value["genre"], - "policy_id": policy_id, - "meta_rule_id": meta_rule_id, - "hostname": container_value["name"], - "hostip": "127.0.0.1", - "port": container_value["port"], - } - ) - else: - logger.warning("Container content keys not found {}", container_value) - else: - logger.warning("Container content keys not found {}", container_value) - else: - raise exceptions.ModelUnknown("Cannot find model_id: {} in models and " - "may not contains 'meta_rules' key".format(model_id)) - else: - raise exceptions.PolicyUnknown("Cannot find policy within policy_id: {}, " - "and may not contains 'model_id' key".format(policy_id)) - - self.__CONTAINER_CHAINING[keystone_project_id] = container_ids diff --git a/python_moonutilities/python_moonutilities/configuration.py b/python_moonutilities/python_moonutilities/configuration.py deleted file mode 100644 index 0516274c..00000000 --- a/python_moonutilities/python_moonutilities/configuration.py +++ /dev/null @@ -1,124 +0,0 @@ -# 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 base64 -import json -import python_moonutilities.request_wrapper as requests -import logging.config -from python_moonutilities import exceptions - -logger = logging.getLogger("moon.utilities.configuration") - -CONSUL_HOST = "consul" -CONSUL_PORT = "8500" - -DATABASE = "database" -KEYSTONE = "keystone" -DOCKER = "docker" -COMPONENTS = "components" - - -def init_logging(): - config = get_configuration("logging") - logging.config.dictConfig(config['logging']) - - -def increment_port(): - components_object = get_configuration("components/port_start") - if 'components/port_start' in components_object: - components_port_start = int(components_object['components/port_start']) - components_port_start += 1 - else: - raise exceptions.ConsulComponentContentError("error={}".format(components_object)) - url = "http://{}:{}/v1/kv/components/port_start".format(CONSUL_HOST, CONSUL_PORT) - req = requests.put(url, json=str(components_port_start)) - if req.status_code != 200: - logger.info("url={}".format(url)) - raise exceptions.ConsulError - return components_port_start - - -def get_configuration(key): - url = "http://{}:{}/v1/kv/{}".format(CONSUL_HOST, CONSUL_PORT, key) - req = requests.get(url) - if req.status_code != 200: - logger.error("url={}".format(url)) - raise exceptions.ConsulComponentNotFound("error={}: {}".format(req.status_code, req.text)) - data = req.json() - if len(data) == 1: - data = data[0] - if all( k in data for k in ("Key", "Value")) : - return {data["Key"]: json.loads(base64.b64decode(data["Value"]).decode("utf-8"))} - raise exceptions.ConsulComponentContentError("error={}".format(data)) - else: - for item in data: - if not all(k in item for k in ("Key", "Value")): - logger.warning("invalidate content {}".format(item)) - raise exceptions.ConsulComponentContentError("error={}".format(data)) - return [ - { - item["Key"]: json.loads(base64.b64decode(item["Value"]).decode("utf-8")) - } for item in data - ] - - -def add_component(name, uuid, port=None, bind="127.0.0.1", keystone_id="", extra=None, container=None): - data = { - "hostname": name, - "keystone_id": keystone_id, - "bind": bind, - "port": port, - "extra": extra, - "container": container - } - req = requests.put( - "http://{}:{}/v1/kv/components/{}".format(CONSUL_HOST, CONSUL_PORT, uuid), - headers={"content-type": "application/json"}, - json=data - ) - if req.status_code != 200: - logger.debug("url={}".format("http://{}:{}/v1/kv/components/{}".format(CONSUL_HOST, CONSUL_PORT, uuid))) - logger.debug("data={}".format(data)) - raise exceptions.ConsulError - logger.info("Add component {}".format(req.text)) - return get_configuration("components/"+uuid) - - -def get_plugins(): - pipeline = get_configuration("components/pipeline") - logger.debug("pipeline={}".format(pipeline)) - components = pipeline.get("components/pipeline") - if 'interface' in components: - components.pop('interface') - else: - raise exceptions.ConsulComponentContentError("error= Components pipeline has no interface") - return components - - -def get_components(): - url = "http://{}:{}/v1/kv/components?recurse=true".format(CONSUL_HOST, CONSUL_PORT) - req = requests.get(url) - if req.status_code != 200: - logger.info("url={}".format(url)) - raise exceptions.ConsulError - data = req.json() - if len(data) == 1: - data = data[0] - if all(k in data for k in ("Key", "Value")): - return {data["Key"].replace("components/", ""): json.loads(base64.b64decode(data["Value"]).decode("utf-8"))} - raise exceptions.ConsulComponentContentError("error={}".format(data)) - else: - for item in data: - if not all(k in item for k in ("Key", "Value")): - logger.warning("invalidate content {}".format(item)) - raise exceptions.ConsulComponentContentError("error={}".format(data)) - return { - item["Key"].replace("components/", ""): json.loads(base64.b64decode(item["Value"]).decode("utf-8")) - for item in data - } - - -init_logging() diff --git a/python_moonutilities/python_moonutilities/context.py b/python_moonutilities/python_moonutilities/context.py deleted file mode 100644 index dc140b74..00000000 --- a/python_moonutilities/python_moonutilities/context.py +++ /dev/null @@ -1,353 +0,0 @@ -# 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 copy -import logging -from python_moonutilities import exceptions - -logger = logging.getLogger("moon.utilities." + __name__) - - -class Context: - - def __init__(self, init_context, cache): - if init_context is None: - raise Exception("Invalid context content object") - - self.cache = cache - self.__keystone_project_id = init_context.get("project_id") - self.__pdp_id = self.cache.get_pdp_from_keystone_project(self.__keystone_project_id) - - if not self.__pdp_id: - raise exceptions.AuthzException( - "Cannot create context for authz " - "with Keystone project ID {}".format( - self.__keystone_project_id - )) - self.__pdp_value = copy.deepcopy(self.cache.pdp[self.__pdp_id]) - - self.__subject = init_context.get("subject_name") - self.__object = init_context.get("object_name") - self.__action = init_context.get("action_name") - self.__request_id = init_context.get("req_id") - self.__cookie = init_context.get("cookie") - self.__manager_url = init_context.get("manager_url") - self.__interface_name = init_context.get("interface_name") - self.__current_request = None - - self.__index = -1 - # self.__init_initial_request() - self.__meta_rule_ids = self.cache.get_meta_rule_ids_from_pdp_value(self.__pdp_value) - self.__meta_rules = self.cache.meta_rules - - self.__pdp_set = {} - # self.__init_pdp_set() - - def delete_cache(self): - self.cache = {} - - def set_cache(self, cache): - self.cache = cache - - def increment_index(self): - self.__index += 1 - self.__init_current_request() - self.__init_pdp_set() - - @property - def current_state(self): - self.__validate_meta_rule_content(self.__pdp_set[self.__meta_rule_ids[self.__index]]) - return self.__pdp_set[self.__meta_rule_ids[self.__index]]['effect'] - - @current_state.setter - def current_state(self, state): - if state not in ("grant", "deny", "passed"): - state = "passed" - self.__validate_meta_rule_content(self.__pdp_set[self.__meta_rule_ids[self.__index]]) - self.__pdp_set[self.__meta_rule_ids[self.__index]]['effect'] = state - - @current_state.deleter - def current_state(self): - self.__validate_meta_rule_content(self.__pdp_set[self.__meta_rule_ids[self.__index]]) - self.__pdp_set[self.__meta_rule_ids[self.__index]]['effect'] = "unset" - - @property - def current_policy_id(self): - if "security_pipeline" not in self.__pdp_value: - raise exceptions.AuthzException('Cannot find security_pipeline key within pdp.') - return self.__pdp_value["security_pipeline"][self.__index] - - @current_policy_id.setter - def current_policy_id(self, value): - pass - - @current_policy_id.deleter - def current_policy_id(self): - pass - - def __init_current_request(self): - if "security_pipeline" not in self.__pdp_value: - raise exceptions.PdpContentError - self.__subject = self.cache.get_subject( - self.__pdp_value["security_pipeline"][self.__index], - self.__subject) - self.__object = self.cache.get_object( - self.__pdp_value["security_pipeline"][self.__index], - self.__object) - self.__action = self.cache.get_action( - self.__pdp_value["security_pipeline"][self.__index], - self.__action) - self.__current_request = dict(self.initial_request) - - def __init_pdp_set(self): - for meta_rule_id in self.__meta_rule_ids: - self.__pdp_set[meta_rule_id] = dict() - self.__pdp_set[meta_rule_id]["meta_rules"] = self.__meta_rules[meta_rule_id] - self.__pdp_set[meta_rule_id]["target"] = self.__add_target(meta_rule_id) - self.__pdp_set[meta_rule_id]["effect"] = "unset" - self.__pdp_set["effect"] = "deny" - - def update_target(self): - for meta_rule_id in self.__meta_rule_ids: - result = dict() - _subject = self.__current_request["subject"] - _object = self.__current_request["object"] - _action = self.__current_request["action"] - - meta_rules = self.cache.meta_rules - policy_id = self.cache.get_policy_from_meta_rules(meta_rule_id) - - if 'subject_categories' not in meta_rules[meta_rule_id]: - raise exceptions.MetaRuleContentError(" 'subject_categories' key not found ") - - self.cache.update_assignments(policy_id) - - for sub_cat in meta_rules[meta_rule_id]['subject_categories']: - if sub_cat not in result: - result[sub_cat] = [] - result[sub_cat].extend( - self.cache.get_subject_assignments(policy_id, _subject, sub_cat)) - - if 'object_categories' not in meta_rules[meta_rule_id]: - raise exceptions.MetaRuleContentError(" 'object_categories' key not found ") - - for obj_cat in meta_rules[meta_rule_id]['object_categories']: - if obj_cat not in result: - result[obj_cat] = [] - result[obj_cat].extend( - self.cache.get_object_assignments(policy_id, _object, obj_cat)) - - if 'action_categories' not in meta_rules[meta_rule_id]: - raise exceptions.MetaRuleContentError(" 'action_categories' key not found ") - - for act_cat in meta_rules[meta_rule_id]['action_categories']: - if act_cat not in result: - result[act_cat] = [] - result[act_cat].extend( - self.cache.get_action_assignments(policy_id, _action, act_cat)) - - self.__pdp_set[meta_rule_id]["target"] = result - - def __add_target(self, meta_rule_id): - """build target from meta_rule - - Target is dict of categories as keys ; and the value of each category - will be a list of assignments - - """ - result = dict() - _subject = self.__current_request["subject"] - _object = self.__current_request["object"] - _action = self.__current_request["action"] - - meta_rules = self.cache.meta_rules - policy_id = self.cache.get_policy_from_meta_rules(meta_rule_id) - - if 'subject_categories' not in meta_rules[meta_rule_id]: - raise exceptions.MetaRuleContentError(" 'subject_categories' key not found ") - - for sub_cat in meta_rules[meta_rule_id]['subject_categories']: - if sub_cat not in result: - result[sub_cat] = [] - result[sub_cat].extend( - self.cache.get_subject_assignments(policy_id, _subject, sub_cat)) - - if 'object_categories' not in meta_rules[meta_rule_id]: - raise exceptions.MetaRuleContentError(" 'object_categories' key not found ") - - for obj_cat in meta_rules[meta_rule_id]['object_categories']: - if obj_cat not in result: - result[obj_cat] = [] - result[obj_cat].extend( - self.cache.get_object_assignments(policy_id, _object, obj_cat)) - - if 'action_categories' not in meta_rules[meta_rule_id]: - raise exceptions.MetaRuleContentError(" 'action_categories' key not found ") - - for act_cat in meta_rules[meta_rule_id]['action_categories']: - if act_cat not in result: - result[act_cat] = [] - result[act_cat].extend( - self.cache.get_action_assignments(policy_id, _action, act_cat)) - - return result - - def __repr__(self): - return """PDP ID: {id} -current_request: {current_request} -request_id: {request_id} -index: {index} -headers: {headers} -pdp_set: {pdp_set} - """.format( - id=self.__pdp_id, - current_request=self.__current_request, - request_id=self.__request_id, - headers=self.__meta_rule_ids, - pdp_set=self.__pdp_set, - index=self.__index - ) - - def to_dict(self): - return { - "initial_request": copy.deepcopy(self.initial_request), - "current_request": copy.deepcopy(self.__current_request), - "headers": copy.deepcopy(self.__meta_rule_ids), - "index": copy.deepcopy(self.__index), - "pdp_set": copy.deepcopy(self.__pdp_set), - "request_id": copy.deepcopy(self.__request_id), - "manager_url": copy.deepcopy(self.__manager_url), - "interface_name": copy.deepcopy(self.__interface_name), - } - - @property - def request_id(self): - return self.__request_id - - @request_id.setter - def request_id(self, value): - raise Exception("You cannot update the request_id") - - @request_id.deleter - def request_id(self): - raise Exception("You cannot update the request_id") - - @property - def manager_url(self): - return self.__manager_url - - @manager_url.setter - def manager_url(self, value): - raise Exception("You cannot update the manager_url") - - @manager_url.deleter - def manager_url(self): - raise Exception("You cannot update the manager_url") - - @property - def interface_name(self): - return self.__interface_name - - @interface_name.setter - def interface_name(self, value): - raise Exception("You cannot update the interface_name") - - @interface_name.deleter - def interface_name(self): - raise Exception("You cannot update the interface_name") - - @property - def cookie(self): - return self.__cookie - - @cookie.setter - def cookie(self, value): - raise Exception("You cannot update the cookie") - - @cookie.deleter - def cookie(self): - raise Exception("You cannot delete the cookie") - - @property - def initial_request(self): - return { - "subject": self.__subject, - "object": self.__object, - "action": self.__action, - } - - @initial_request.setter - def initial_request(self, value): - raise Exception("You are not allowed to update the initial_request") - - @initial_request.deleter - def initial_request(self): - raise Exception("You are not allowed to delete the initial_request") - - @property - def current_request(self): - if not self.__current_request: - self.__current_request = dict(self.initial_request) - return self.__current_request - - @current_request.setter - def current_request(self, value): - - self.__current_request = copy.deepcopy(value) - # Note (asteroide): if the current request is modified, - # we must update the PDP Set. - self.__init_pdp_set() - - @current_request.deleter - def current_request(self): - self.__current_request = {} - self.__pdp_set = {} - - ''' - [Note ] Refactor name of headers to meta_rule_ids done , - may need to refactor getter and setter of headers - ''' - - @property - def headers(self): - return self.__meta_rule_ids - - @headers.setter - def headers(self, meta_rule_ids): - self.__meta_rule_ids = meta_rule_ids - - @headers.deleter - def headers(self): - self.__meta_rule_ids = list() - - @property - def index(self): - return self.__index - - @index.setter - def index(self, index): - self.__index += 1 - - @index.deleter - def index(self): - self.__index = -1 - - @property - def pdp_set(self): - return self.__pdp_set - - @pdp_set.setter - def pdp_set(self, value): - raise Exception("You are not allowed to modify the pdp_set") - - @pdp_set.deleter - def pdp_set(self): - self.__pdp_set = {} - - def __validate_meta_rule_content(self, meta_rules): - if 'effect' not in meta_rules: - logger.error("meta_rules={}".format(meta_rules)) - raise exceptions.PdpContentError("effect not in meta_rules") diff --git a/python_moonutilities/python_moonutilities/exceptions.py b/python_moonutilities/python_moonutilities/exceptions.py deleted file mode 100644 index 8ad90e96..00000000 --- a/python_moonutilities/python_moonutilities/exceptions.py +++ /dev/null @@ -1,833 +0,0 @@ -# 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 logging -from werkzeug.exceptions import HTTPException - -logger = logging.getLogger("moon.utilities.exceptions") -_ = str - - -class MoonErrorMetaClass(type): - - def __init__(cls, name, bases, dct): - super(MoonErrorMetaClass, cls).__init__(name, bases, dct) - cls.hierarchy += "/" + str(name) - - -class MoonError(HTTPException): - __metaclass__ = MoonErrorMetaClass - hierarchy = "" - description = _("There is an error requesting the Moon platform.") - code = 400 - title = 'Moon Error' - logger = "ERROR" - - def __init__(self, message="", status_code=None, payload=""): - if message: - self.description = message - if status_code: - self.code = status_code - self.payload = payload - super(MoonError, self).__init__() - - def __str__(self): - return "{}: {}".format(self.code, self.title) - - def __del__(self): - message = "{} ({}) {}".format(self.hierarchy, self.description, self.payload) - if self.logger == "ERROR": - try: - logger.error(message) - except AttributeError: - logger.error(message) - elif self.logger == "WARNING": - try: - logger.warning(message) - except AttributeError: - logger.warning(message) - elif self.logger == "CRITICAL": - try: - logger.critical(message) - except AttributeError: - logger.critical(message) - elif self.logger == "AUTHZ": - try: - logger.authz(self.hierarchy) - logger.error(message) - except AttributeError: - logger.error(message) - else: - try: - logger.info(message) - except AttributeError: - logger.info(message) - - # def to_dict(self): - # rv = dict(self.payload or ()) - # rv['message'] = "{} ({})".format(self.hierarchy, self.description) - # rv['title'] = self.title - # rv['code'] = self.code - # return rv - - -# Exceptions for Tenant - -class TenantException(MoonError): - description = _("There is an error requesting this tenant.") - code = 400 - title = 'Tenant Error' - logger = "ERROR" - - -class TenantUnknown(TenantException): - description = _("The tenant is unknown.") - code = 400 - title = 'Tenant Unknown' - logger = "ERROR" - - -class TenantAddedNameExisting(TenantException): - description = _("The tenant name is existing.") - code = 400 - title = 'Added Tenant Name Existing' - logger = "ERROR" - - -class TenantNoIntraExtension(TenantException): - description = _("The tenant has not intra_extension.") - code = 400 - title = 'Tenant No Intra_Extension' - logger = "ERROR" - - -class TenantNoIntraAuthzExtension(TenantNoIntraExtension): - description = _("The tenant has not intra_admin_extension.") - code = 400 - title = 'Tenant No Intra_Admin_Extension' - logger = "ERROR" - - -# Exceptions for IntraExtension - - -class IntraExtensionException(MoonError): - description = _("There is an error requesting this IntraExtension.") - code = 400 - title = 'Extension Error' - - -class IntraExtensionUnknown(IntraExtensionException): - description = _("The intra_extension is unknown.") - code = 400 - title = 'Intra Extension Unknown' - logger = "Error" - - -class ModelUnknown(MoonError): - description = _("The model is unknown.") - code = 400 - title = 'Model Unknown' - logger = "Error" - - -class ModelContentError(MoonError): - description = _("The model content is invalid.") - code = 400 - title = 'Model Unknown' - logger = "Error" - - -class ModelExisting(MoonError): - description = _("The model already exists.") - code = 409 - title = 'Model Error' - logger = "Error" - - -# Authz exceptions - -class AuthzException(MoonError): - description = _("There is an authorization error requesting this IntraExtension.") - code = 403 - title = 'Authz Exception' - logger = "AUTHZ" - - -# Auth exceptions - -class AuthException(MoonError): - description = _("There is an authentication error requesting this API. " - "You must provide a valid token from Keystone.") - code = 401 - title = 'Auth Exception' - logger = "AUTHZ" - - -# Admin exceptions - -class AdminException(MoonError): - description = _("There is an error requesting this Authz IntraExtension.") - code = 400 - title = 'Authz Exception' - logger = "AUTHZ" - - -class AdminMetaData(AdminException): - code = 400 - title = 'Metadata Exception' - - -class AdminPerimeter(AdminException): - code = 400 - title = 'Perimeter Exception' - - -class AdminScope(AdminException): - code = 400 - title = 'Scope Exception' - - -class AdminAssignment(AdminException): - code = 400 - title = 'Assignment Exception' - - -class AdminMetaRule(AdminException): - code = 400 - title = 'Aggregation Algorithm Exception' - - -class AdminRule(AdminException): - code = 400 - title = 'Rule Exception' - - -class CategoryNameInvalid(AdminMetaData): - description = _("The given category name is invalid.") - code = 400 - title = 'Category Name Invalid' - logger = "ERROR" - - -class SubjectCategoryExisting(AdminMetaData): - description = _("The given subject category already exists.") - code = 409 - title = 'Subject Category Existing' - logger = "ERROR" - -class ObjectCategoryExisting(AdminMetaData): - description = _("The given object category already exists.") - code = 409 - title = 'Object Category Existing' - logger = "ERROR" - -class ActionCategoryExisting(AdminMetaData): - description = _("The given action category already exists.") - code = 409 - title = 'Action Category Existing' - logger = "ERROR" - - -class SubjectCategoryUnknown(AdminMetaData): - description = _("The given subject category is unknown.") - code = 400 - title = 'Subject Category Unknown' - logger = "ERROR" - - -class DeleteSubjectCategoryWithMetaRule(MoonError): - description = _("Cannot delete subject category used in meta rule ") - code = 400 - title = 'Subject Category With Meta Rule Error' - logger = "Error" - - -class DeleteObjectCategoryWithMetaRule(MoonError): - description = _("Cannot delete Object category used in meta rule ") - code = 400 - title = 'Object Category With Meta Rule Error' - logger = "Error" - - -class ObjectCategoryUnknown(AdminMetaData): - description = _("The given object category is unknown.") - code = 400 - title = 'Object Category Unknown' - logger = "ERROR" - - -class DeleteActionCategoryWithMetaRule(MoonError): - description = _("Cannot delete Action category used in meta rule ") - code = 400 - title = 'Action Category With Meta Rule Error' - logger = "Error" - - -class ActionCategoryUnknown(AdminMetaData): - description = _("The given action category is unknown.") - code = 400 - title = 'Action Category Unknown' - logger = "ERROR" - -class PerimeterContentError(AdminPerimeter): - description = _("Perimeter content is invalid.") - code = 400 - title = 'Perimeter content is invalid.' - logger = "ERROR" - - -class DeletePerimeterWithAssignment(MoonError): - description = _("Cannot delete perimeter with assignment") - code = 400 - title = 'Perimeter With Assignment Error' - logger = "Error" - - -class SubjectUnknown(AdminPerimeter): - description = _("The given subject is unknown.") - code = 400 - title = 'Subject Unknown' - logger = "ERROR" - - -class ObjectUnknown(AdminPerimeter): - description = _("The given object is unknown.") - code = 400 - title = 'Object Unknown' - logger = "ERROR" - - -class ActionUnknown(AdminPerimeter): - description = _("The given action is unknown.") - code = 400 - title = 'Action Unknown' - logger = "ERROR" - - -class SubjectExisting(AdminPerimeter): - description = _("The given subject is existing.") - code = 409 - title = 'Subject Existing' - logger = "ERROR" - - -class ObjectExisting(AdminPerimeter): - description = _("The given object is existing.") - code = 409 - title = 'Object Existing' - logger = "ERROR" - - -class ActionExisting(AdminPerimeter): - description = _("The given action is existing.") - code = 409 - title = 'Action Existing' - logger = "ERROR" - - -class SubjectNameExisting(AdminPerimeter): - description = _("The given subject name is existing.") - code = 409 - title = 'Subject Name Existing' - logger = "ERROR" - - -class ObjectNameExisting(AdminPerimeter): - description = _("The given object name is existing.") - code = 409 - title = 'Object Name Existing' - logger = "ERROR" - - -class ActionNameExisting(AdminPerimeter): - description = _("The given action name is existing.") - code = 409 - title = 'Action Name Existing' - logger = "ERROR" - - -class ObjectsWriteNoAuthorized(AdminPerimeter): - description = _("The modification on Objects is not authorized.") - code = 400 - title = 'Objects Write No Authorized' - logger = "AUTHZ" - - -class ActionsWriteNoAuthorized(AdminPerimeter): - description = _("The modification on Actions is not authorized.") - code = 400 - title = 'Actions Write No Authorized' - logger = "AUTHZ" - - -class SubjectScopeUnknown(AdminScope): - description = _("The given subject scope is unknown.") - code = 400 - title = 'Subject Scope Unknown' - logger = "ERROR" - - -class ObjectScopeUnknown(AdminScope): - description = _("The given object scope is unknown.") - code = 400 - title = 'Object Scope Unknown' - logger = "ERROR" - - -class ActionScopeUnknown(AdminScope): - description = _("The given action scope is unknown.") - code = 400 - title = 'Action Scope Unknown' - logger = "ERROR" - - -class SubjectScopeExisting(AdminScope): - description = _("The given subject scope is existing.") - code = 409 - title = 'Subject Scope Existing' - logger = "ERROR" - - -class ObjectScopeExisting(AdminScope): - description = _("The given object scope is existing.") - code = 409 - title = 'Object Scope Existing' - logger = "ERROR" - - -class ActionScopeExisting(AdminScope): - description = _("The given action scope is existing.") - code = 409 - title = 'Action Scope Existing' - logger = "ERROR" - - -class SubjectScopeNameExisting(AdminScope): - description = _("The given subject scope name is existing.") - code = 409 - title = 'Subject Scope Name Existing' - logger = "ERROR" - - -class ObjectScopeNameExisting(AdminScope): - description = _("The given object scope name is existing.") - code = 409 - title = 'Object Scope Name Existing' - logger = "ERROR" - - -class ActionScopeNameExisting(AdminScope): - description = _("The given action scope name is existing.") - code = 409 - title = 'Action Scope Name Existing' - logger = "ERROR" - - -class SubjectAssignmentUnknown(AdminAssignment): - description = _("The given subject assignment value is unknown.") - code = 400 - title = 'Subject Assignment Unknown' - logger = "ERROR" - - -class ObjectAssignmentUnknown(AdminAssignment): - description = _("The given object assignment value is unknown.") - code = 400 - title = 'Object Assignment Unknown' - logger = "ERROR" - - -class ActionAssignmentUnknown(AdminAssignment): - description = _("The given action assignment value is unknown.") - code = 400 - title = 'Action Assignment Unknown' - logger = "ERROR" - - -class SubjectAssignmentExisting(AdminAssignment): - description = _("The given subject assignment value is existing.") - code = 409 - title = 'Subject Assignment Existing' - logger = "ERROR" - - -class ObjectAssignmentExisting(AdminAssignment): - description = _("The given object assignment value is existing.") - code = 409 - title = 'Object Assignment Existing' - logger = "ERROR" - - -class ActionAssignmentExisting(AdminAssignment): - description = _("The given action assignment value is existing.") - code = 409 - title = 'Action Assignment Existing' - logger = "ERROR" - - -class AggregationAlgorithmNotExisting(AdminMetaRule): - description = _("The given aggregation algorithm is not existing.") - code = 400 - title = 'Aggregation Algorithm Not Existing' - logger = "ERROR" - - -class AggregationAlgorithmUnknown(AdminMetaRule): - description = _("The given aggregation algorithm is unknown.") - code = 400 - title = 'Aggregation Algorithm Unknown' - logger = "ERROR" - - -class SubMetaRuleAlgorithmNotExisting(AdminMetaRule): - description = _("The given sub_meta_rule algorithm is unknown.") - code = 400 - title = 'Sub_meta_rule Algorithm Unknown' - logger = "ERROR" - - -class MetaRuleUnknown(AdminMetaRule): - description = _("The given meta rule is unknown.") - code = 400 - title = 'Meta Rule Unknown' - logger = "ERROR" - - -class MetaRuleNotLinkedWithPolicyModel(MoonError): - description = _("The meta rule is not found in the model attached to the policy.") - code = 400 - title = 'MetaRule Not Linked With Model - Policy' - logger = "Error" - - -class CategoryNotAssignedMetaRule(MoonError): - description = _("The category is not found in the meta rules attached to the policy.") - code = 400 - title = 'Category Not Linked With Meta Rule - Policy' - logger = "Error" - - -class SubMetaRuleNameExisting(AdminMetaRule): - description = _("The sub meta rule name already exists.") - code = 409 - title = 'Sub Meta Rule Name Existing' - logger = "ERROR" - - -class MetaRuleExisting(AdminMetaRule): - description = _("The meta rule already exists.") - code = 409 - title = 'Meta Rule Existing' - logger = "ERROR" - - -class MetaRuleContentError(AdminMetaRule): - description = _("Invalid content of meta rule.") - code = 400 - title = 'Meta Rule Error' - logger = "ERROR" - - -class MetaRuleUpdateError(AdminMetaRule): - description = _("Meta_rule is used in Rule.") - code = 400 - title = 'Meta_Rule Update Error' - logger = "ERROR" - - -class RuleExisting(AdminRule): - description = _("The rule already exists.") - code = 409 - title = 'Rule Existing' - logger = "ERROR" - - -class RuleContentError(AdminRule): - description = _("Invalid content of rule.") - code = 400 - title = 'Rule Error' - logger = "ERROR" - - -class RuleUnknown(AdminRule): - description = _("The rule for that request doesn't exist.") - code = 400 - title = 'Rule Unknown' - logger = "ERROR" - - -# Keystone exceptions - - -class KeystoneError(MoonError): - description = _("There is an error connecting to Keystone.") - code = 400 - title = 'Keystone error' - logger = "ERROR" - - -class KeystoneProjectError(KeystoneError): - description = _("There is an error retrieving projects from the Keystone service.") - code = 400 - title = 'Keystone project error' - logger = "ERROR" - - -class KeystoneUserError(KeystoneError): - description = _("There is an error retrieving users from the Keystone service.") - code = 400 - title = 'Keystone user error' - logger = "ERROR" - - -class KeystoneUserConflict(KeystoneUserError): - description = _("A user with that name already exist.") - code = 400 - title = 'Keystone user error' - logger = "ERROR" - - -# Consul exceptions - - -class ConsulError(MoonError): - description = _("There is an error connecting to Consul.") - code = 400 - title = 'Consul error' - logger = "ERROR" - - -class ConsulComponentNotFound(ConsulError): - description = _("The component do not exist in Consul database.") - code = 500 - title = 'Consul error' - logger = "WARNING" - - -class ConsulComponentContentError(ConsulError): - description = _("invalid content of component .") - code = 500 - title = 'Consul Content error' - logger = "WARNING" - - -# Containers exceptions - - -class DockerError(MoonError): - description = _("There is an error with Docker.") - code = 400 - title = 'Docker error' - logger = "ERROR" - - -class ContainerMissing(DockerError): - description = _("Some containers are missing.") - code = 400 - title = 'Container missing' - logger = "ERROR" - - -class WrapperConflict(MoonError): - description = _("A Wrapper already exist for the specified slave.") - code = 409 - title = 'Wrapper conflict' - logger = "ERROR" - - -class PipelineConflict(MoonError): - description = _("A Pipeline already exist for the specified slave.") - code = 409 - title = 'Pipeline conflict' - logger = "ERROR" - - -class PipelineUnknown(MoonError): - description = _("This Pipeline is unknown from the system.") - code = 400 - title = 'Pipeline Unknown' - logger = "ERROR" - - -class WrapperUnknown(MoonError): - description = _("This Wrapper is unknown from the system.") - code = 400 - title = 'Wrapper Unknown' - logger = "ERROR" - - -class SlaveNameUnknown(MoonError): - description = _("The slave is unknown.") - code = 400 - title = 'Slave Unknown' - logger = "Error" - - -class PdpUnknown(MoonError): - description = _("The pdp is unknown.") - code = 400 - title = 'Pdp Unknown' - logger = "Error" - - -class PdpExisting(MoonError): - description = _("The pdp already exists.") - code = 409 - title = 'Pdp Error' - logger = "Error" - - -class PdpContentError(MoonError): - description = _("Invalid content of pdp.") - code = 400 - title = 'Pdp Error' - logger = "Error" - - -class PdpKeystoneMappingConflict(MoonError): - description = _("A pdp is already mapped to that Keystone project.") - code = 409 - title = 'Pdp Mapping Error' - logger = "Error" - - -class PolicyUnknown(MoonError): - description = _("The policy is unknown.") - code = 400 - title = 'Policy Unknown' - logger = "Error" - -class PolicyContentError(MoonError): - description = _("The policy content is invalid.") - code = 400 - title = 'Policy Content Error' - logger = "Error" - - -class PolicyExisting(MoonError): - description = _("The policy already exists.") - code = 409 - title = 'Policy Already Exists' - logger = "Error" - - -class PolicyUpdateError(MoonError): - description = _("The policy data is used.") - code = 400 - title = 'Policy update error' - logger = "Error" - - -class DeleteData(MoonError): - description = _("Cannot delete data with assignment") - code = 400 - title = 'Data Error' - logger = "Error" - - -class DeleteCategoryWithData(MoonError): - description = _("Cannot delete category with data") - code = 400 - title = 'Category With Data Error' - logger = "Error" - - -class DeleteCategoryWithMetaRule(MoonError): - description = _("Cannot delete category with meta rule") - code = 400 - title = 'Category With MetaRule Error' - logger = "Error" - - -class DeleteCategoryWithAssignment(MoonError): - description = _("Cannot delete category with assignment ") - code = 400 - title = 'Category With Assignment Error' - logger = "Error" - - -class DeleteModelWithPolicy(MoonError): - description = _("Cannot delete model with policy") - code = 400 - title = 'Model With Policy Error' - logger = "Error" - - -class DeletePolicyWithPdp(MoonError): - description = _("Cannot delete policy with pdp") - code = 400 - title = 'Policy With PDP Error' - logger = "Error" - - -class DeletePolicyWithPerimeter(MoonError): - description = _("Cannot delete policy with perimeter") - code = 400 - title = 'Policy With Perimeter Error' - logger = "Error" - - -class DeletePolicyWithData(MoonError): - description = _("Cannot delete policy with data") - code = 400 - title = 'Policy With Data Error' - logger = "Error" - - -class DeletePolicyWithRules(MoonError): - description = _("Cannot delete policy with rules") - code = 400 - title = 'Policy With Rule Error' - logger = "Error" - - -class DeleteMetaRuleWithModel(MoonError): - description = _("Cannot delete meta rule with model") - code = 400 - title = 'Meta rule With Model Error' - logger = "Error" - - -class DeleteMetaRuleWithRule(MoonError): - description = _("Cannot delete meta rule with rule") - code = 400 - title = 'Meta rule With Model Error' - logger = "Error" - - -class DataUnknown(MoonError): - description = _("The data unknown.") - code = 400 - title = 'Data Unknown' - logger = "Error" - - -class ValidationContentError(MoonError): - description = _("The Content validation incorrect.") - code = 400 - title = 'Invalid Content' - logger = "Error" - - def __init__(self, message=""): - self.message = message - super().__init__(message) - - def __str__(self): - return self.message - - -class ValidationKeyError(MoonError): - description = _("The Key validation incorrect.") - code = 400 - title = 'Invalid Key' - logger = "Error" - - def __init__(self, message=""): - self.message = message - super().__init__(message) - - def __str__(self): - return self.message diff --git a/python_moonutilities/python_moonutilities/misc.py b/python_moonutilities/python_moonutilities/misc.py deleted file mode 100644 index 1db4d7cd..00000000 --- a/python_moonutilities/python_moonutilities/misc.py +++ /dev/null @@ -1,116 +0,0 @@ -# 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 logging -import random - -logger = logging.getLogger("moon.utilities.misc") - - -def get_random_name(): - _list = ( - "windy", - "vengeful", - "precious", - "vivacious", - "quiet", - "confused", - "exultant", - "impossible", - "thick", - "obsolete", - "piquant", - "fanatical", - "tame", - "perfect", - "animated", - "dark", - "stimulating", - "drunk", - "depressed", - "fumbling", - "like", - "undesirable", - "spurious", - "subsequent", - "spiteful", - "last", - "stale", - "hulking", - "giddy", - "minor", - "careful", - "possessive", - "gullible", - "fragile", - "divergent", - "ill-informed", - "false", - "jumpy", - "damaged", - "likeable", - "volatile", - "handsomely", - "wet", - "long-term", - "pretty", - "taboo", - "normal", - "magnificent", - "nutty", - "puzzling", - "small", - "kind", - "devilish", - "chubby", - "paltry", - "cultured", - "old", - "defective", - "hanging", - "innocent", - "jagged", - "economic", - "good", - "sulky", - "real", - "bent", - "shut", - "furry", - "terrific", - "hollow", - "terrible", - "mammoth", - "pleasant", - "scared", - "obnoxious", - "absorbing", - "imported", - "infamous", - "grieving", - "ill-fated", - "mighty", - "handy", - "comfortable", - "astonishing", - "brown", - "assorted", - "wrong", - "unsightly", - "spooky", - "delightful", - "acid", - "inconclusive", - "mere", - "careless", - "historical", - "flashy", - "squealing", - "quarrelsome", - "empty", - "long", - ) - return random.choice(_list) diff --git a/python_moonutilities/python_moonutilities/request_wrapper.py b/python_moonutilities/python_moonutilities/request_wrapper.py deleted file mode 100644 index f1603b9d..00000000 --- a/python_moonutilities/python_moonutilities/request_wrapper.py +++ /dev/null @@ -1,22 +0,0 @@ -import sys -import requests -from python_moonutilities import exceptions - -def get(url): - try: - response = requests.get(url) - except requests.exceptions.RequestException as e: - raise exceptions.ConsulError("request failure ",e) - except: - raise exceptions.ConsulError("Unexpected error ", sys.exc_info()[0]) - return response - - -def put(url, json=""): - try: - response = requests.put(url,json=json) - except requests.exceptions.RequestException as e: - raise exceptions.ConsulError("request failure ",e) - except: - raise exceptions.ConsulError("Unexpected error ", sys.exc_info()[0]) - return response
\ No newline at end of file diff --git a/python_moonutilities/python_moonutilities/security_functions.py b/python_moonutilities/python_moonutilities/security_functions.py deleted file mode 100644 index 1069eb2f..00000000 --- a/python_moonutilities/python_moonutilities/security_functions.py +++ /dev/null @@ -1,331 +0,0 @@ -# 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 html -import re -import os -import types -import requests -import time -from functools import wraps -from flask import request -import logging -from python_moonutilities import exceptions, configuration - -logger = logging.getLogger("moon.utilities." + __name__) - -keystone_config = configuration.get_configuration("openstack/keystone")["openstack/keystone"] -TOKENS = {} -__targets = {} - - -def filter_input(func_or_str): - - def __filter(string): - if string and type(string) is str: - return "".join(re.findall("[\w\- +]*", string)) - return string - - def __filter_dict(arg): - result = dict() - for key in arg.keys(): - if key == "email": - result["email"] = __filter_email(arg[key]) - elif key == "password": - result["password"] = arg['password'] - else: - result[key] = __filter(arg[key]) - return result - - def __filter_email(string): - if string and type(string) is str: - return "".join(re.findall("[\w@\._\- +]*", string)) - return string - - def wrapped(*args, **kwargs): - _args = [] - for arg in args: - if isinstance(arg, str): - arg = __filter(arg) - elif isinstance(arg, list): - arg = [__filter(item) for item in arg] - elif isinstance(arg, tuple): - arg = (__filter(item) for item in arg) - elif isinstance(arg, dict): - arg = __filter_dict(arg) - _args.append(arg) - for arg in kwargs: - if type(kwargs[arg]) is str: - kwargs[arg] = __filter(kwargs[arg]) - if isinstance(kwargs[arg], str): - kwargs[arg] = __filter(kwargs[arg]) - elif isinstance(kwargs[arg], list): - kwargs[arg] = [__filter(item) for item in kwargs[arg]] - elif isinstance(kwargs[arg], tuple): - kwargs[arg] = (__filter(item) for item in kwargs[arg]) - elif isinstance(kwargs[arg], dict): - kwargs[arg] = __filter_dict(kwargs[arg]) - return func_or_str(*_args, **kwargs) - - if isinstance(func_or_str, str): - return __filter(func_or_str) - if isinstance(func_or_str, list): - return [__filter(item) for item in func_or_str] - if isinstance(func_or_str, tuple): - return (__filter(item) for item in func_or_str) - if isinstance(func_or_str, dict): - return __filter_dict(func_or_str) - if isinstance(func_or_str, types.FunctionType): - return wrapped - return None - - -""" -To do should check value of Dictionary but it's dependent on from where it's coming -""" - - -def validate_data(data): - def __validate_string(string): - temp_str = html.escape(string) - if string != temp_str: - raise exceptions.ValidationContentError('Forbidden characters in string') - - def __validate_list_or_tuple(container): - for i in container: - validate_data(i) - - def __validate_dict(dictionary): - for key in dictionary: - validate_data(dictionary[key]) - - if isinstance(data, bool): - return True - if data is None: - data = "" - if isinstance(data, str): - __validate_string(data) - elif isinstance(data, list) or isinstance(data, tuple): - __validate_list_or_tuple(data) - elif isinstance(data, dict): - __validate_dict(data) - else: - raise exceptions.ValidationContentError('Value is Not String or Container or Dictionary: {}'.format(data)) - - -def validate_input(type='get', args_state=[], kwargs_state=[], body_state=[]): - """ - this fucntion works only on List or tuple or dictionary of Strings ,and String direct - Check if input of function is Valid or not, Valid if not has spaces and values is not None or empty. - - :param type: type of request if function is used as decorator - :param args_state: list of Booleans for args, - values must be order as target values of arguments, - True if None is not Allowed and False if is allowed - :param kwargs_state: list of Booleans for kwargs as order of input kwargs, - values must be order as target values of arguments, - True if None is not Allowed and False if is allowed - :param body_state: list of Booleans for arguments in body of request if request is post, - values must be order as target values of arguments, - True if None is not Allowed and False if is allowed - :return: - """ - - def validate_input_decorator(func): - def wrapped(*args, **kwargs): - - temp_args = [] - """ - this loop made to filter args from object class, - when put this function as decorator in function control - then there is copy of this class add to front of args - """ - for arg in args: - if isinstance(arg, str) == True or \ - isinstance(arg, list) == True or \ - isinstance(arg, dict) == True: - temp_args.append(arg) - - while len(args_state) < len(temp_args): - args_state.append(True) - - for i in range(0, len(temp_args)): - if args_state[i]: - validate_data(temp_args[i]) - - while len(kwargs_state) < len(kwargs): - kwargs_state.append(False) - counter = 0 - for i in kwargs: - if kwargs_state[counter]: - validate_data(kwargs[i]) - - counter = counter + 1 - - if type == "post" or type == "patch": - body = request.json - # while len(body_state) < len(body): - # body_state.append(True) - # counter = 0 - for key in body_state: - if key in body: - if body_state[key]: - try: - validate_data(body.get(key)) - except exceptions.ValidationContentError as e: - raise exceptions.ValidationContentError("Key: '{}', [{}]".format(key, str(e))) - else: - raise exceptions.ValidationKeyError('Invalid Key :{} not found'.format(key)) - - # counter = counter + 1 - - return func(*args, **kwargs) - - return wrapped - - return validate_input_decorator - - -def enforce(action_names, object_name, **extra): - """Fake version of the enforce decorator""" - def wrapper_func(func): - def wrapper_args(*args, **kwargs): - # LOG.info("kwargs={}".format(kwargs)) - # kwargs['user_id'] = kwargs.pop('user_id', "admin") - # LOG.info("Calling enforce on {} with args={} kwargs={}".format(func.__name__, args, kwargs)) - return func(*args, **kwargs) - return wrapper_args - return wrapper_func - - -def login(user=None, password=None, domain=None, project=None, url=None): - start_time = time.time() - if not user: - user = keystone_config['user'] - if not password: - password = keystone_config['password'] - if not domain: - domain = keystone_config['domain'] - if not project: - project = keystone_config['project'] - if not url: - url = keystone_config['url'] - headers = { - "Content-Type": "application/json" - } - data_auth = { - "auth": { - "identity": { - "methods": [ - "password" - ], - "password": { - "user": { - "domain": { - "id": domain - }, - "name": user, - "password": password - } - } - }, - "scope": { - "project": { - "domain": { - "id": domain - }, - "name": project - } - } - } - } - - while True: - req = requests.post("{}/auth/tokens".format(url), - json=data_auth, headers=headers, - verify=keystone_config['certificate']) - - if req.status_code in (200, 201, 204): - headers['X-Auth-Token'] = req.headers['X-Subject-Token'] - return headers - logger.warning("Waiting for Keystone...") - if time.time() - start_time == 100: - logger.error(req.text) - raise exceptions.KeystoneError - time.sleep(5) - - -def logout(headers, url=None): - if not url: - url = keystone_config['url'] - headers['X-Subject-Token'] = headers['X-Auth-Token'] - req = requests.delete("{}/auth/tokens".format(url), headers=headers, verify=keystone_config['certificate']) - if req.status_code in (200, 201, 204): - return - logger.error(req.text) - raise exceptions.KeystoneError - - -def check_token(token, url=None): - _verify = False - if keystone_config['certificate']: - _verify = keystone_config['certificate'] - try: - os.environ.pop("http_proxy") - os.environ.pop("https_proxy") - except KeyError: - pass - if not url: - url = keystone_config['url'] - headers = { - "Content-Type": "application/json", - 'X-Subject-Token': token, - 'X-Auth-Token': token, - } - if not keystone_config['check_token']: - # TODO (asteroide): must send the admin id - return "admin" if not token else token - elif keystone_config['check_token'].lower() in ("false", "no", "n"): - # TODO (asteroide): must send the admin id - return "admin" if not token else token - if keystone_config['check_token'].lower() in ("yes", "y", "true"): - if token in TOKENS: - delta = time.mktime(TOKENS[token]["expires_at"]) - time.mktime(time.gmtime()) - if delta > 0: - return TOKENS[token]["user"] - raise exceptions.KeystoneError - else: - req = requests.get("{}/auth/tokens".format(url), headers=headers, verify=_verify) - if req.status_code in (200, 201): - # Note (asteroide): the time stamps is not in ISO 8601, so it is necessary to delete - # characters after the dot - token_time = req.json().get("token").get("expires_at").split(".") - TOKENS[token] = dict() - TOKENS[token]["expires_at"] = time.strptime(token_time[0], "%Y-%m-%dT%H:%M:%S") - TOKENS[token]["user"] = req.json().get("token").get("user").get("id") - return TOKENS[token]["user"] - logger.error("{} - {}".format(req.status_code, req.text)) - raise exceptions.KeystoneError - elif keystone_config['check_token'].lower() == "strict": - req = requests.head("{}/auth/tokens".format(url), headers=headers, verify=_verify) - if req.status_code in (200, 201): - return token - logger.error("{} - {}".format(req.status_code, req.text)) - raise exceptions.KeystoneError - raise exceptions.KeystoneError - - -def check_auth(function): - @wraps(function) - def wrapper(*args, **kwargs): - token = request.headers.get('X-Auth-Token') - token = check_token(token) - if not token: - raise exceptions.AuthException - user_id = kwargs.pop("user_id", token) - result = function(*args, **kwargs, user_id=user_id) - return result - return wrapper diff --git a/python_moonutilities/requirements.txt b/python_moonutilities/requirements.txt deleted file mode 100644 index 5b80e5f2..00000000 --- a/python_moonutilities/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -werkzeug -flask -requests
\ No newline at end of file diff --git a/python_moonutilities/setup.py b/python_moonutilities/setup.py deleted file mode 100644 index 4a2eef5d..00000000 --- a/python_moonutilities/setup.py +++ /dev/null @@ -1,42 +0,0 @@ -# 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'. - -from setuptools import setup, find_packages -import python_moonutilities - -with open('requirements.txt') as f: - required = f.read().splitlines() - -setup( - - name='python-moonutilities', - - version=python_moonutilities.__version__, - - packages=find_packages(), - - author='Thomas Duval', - - author_email='thomas.duval@orange.com', - - description='Some utilities for all the Moon components', - - long_description=open('README.md').read(), - - install_requires=required, - - include_package_data=True, - - url='https://git.opnfv.org/cgit/moon', - - classifiers=[ - 'Programming Language :: Python :: 3', - 'Development Status :: 1 - Planning', - 'License :: OSI Approved', - 'Natural Language :: English', - 'Operating System :: OS Independent', - ], - -) diff --git a/python_moonutilities/tests/unit_python/conftest.py b/python_moonutilities/tests/unit_python/conftest.py deleted file mode 100644 index 34e5c272..00000000 --- a/python_moonutilities/tests/unit_python/conftest.py +++ /dev/null @@ -1,14 +0,0 @@ -import pytest -import requests_mock -import mock_repo - - -@pytest.fixture(autouse=True) -def no_requests(monkeypatch): - """ Modify the response from Requests module - """ - with requests_mock.Mocker(real_http=True) as m: - mock_repo.register_cache(m) - - print("End registering URI") - yield m
\ No newline at end of file diff --git a/python_moonutilities/tests/unit_python/mock_repo/__init__.py b/python_moonutilities/tests/unit_python/mock_repo/__init__.py deleted file mode 100644 index fa50edb3..00000000 --- a/python_moonutilities/tests/unit_python/mock_repo/__init__.py +++ /dev/null @@ -1,42 +0,0 @@ -import mock_repo.urls as register_urls -import mock_repo.data as data_mock - - -def register_cache(m): - """ Modify the response from Requests module - """ - register_urls.register_components(m) - register_urls.register_keystone(m) - - register_urls.register_pdp(m) - register_urls.register_meta_rules(m) - register_urls.register_policies(m) - register_urls.register_models(m) - - register_urls.register_policy_subject(m, data_mock.shared_ids["policy"]["policy_id_1"]) - register_urls.register_policy_subject_invalid_response(m, data_mock.shared_ids["policy"]["policy_id_invalid_response"]) - - register_urls.register_policy_object(m, data_mock.shared_ids["policy"]["policy_id_1"]) - register_urls.register_policy_object_invalid_response(m, data_mock.shared_ids["policy"]["policy_id_invalid_response"]) - - register_urls.register_policy_action(m, data_mock.shared_ids["policy"]["policy_id_1"]) - register_urls.register_policy_action_invalid_response(m, data_mock.shared_ids["policy"]["policy_id_invalid_response"]) - - register_urls.register_policy_subject_assignment(m, data_mock.shared_ids["policy"]["policy_id_1"], data_mock.shared_ids["perimeter"]["perimeter_id_1"]) - - register_urls.register_policy_subject_assignment_list(m, data_mock.shared_ids["policy"]["policy_id_2"]) - - register_urls.register_policy_object_assignment(m, data_mock.shared_ids["policy"]["policy_id_1"], data_mock.shared_ids["perimeter"]["perimeter_id_2"]) - - register_urls.register_policy_object_assignment_list(m, data_mock.shared_ids["policy"]["policy_id_2"]) - - register_urls.register_policy_action_assignment(m, data_mock.shared_ids["policy"]["policy_id_1"], data_mock.shared_ids["perimeter"]["perimeter_id_3"]) - - register_urls.register_policy_action_assignment_list(m, data_mock.shared_ids["policy"]["policy_id_2"]) - # register_urls.register_pods(m) - - # register_urls.register_policy_action_assignment(m, "policy_id_2", "perimeter_id_2") - # register_urls.register_policy_action_assignment(m, "policy_id_2", "perimeter_id_2") - # register_urls.register_policy_action_assignment(m, "policy_id_2", "perimeter_id_2") - - register_urls.register_rules(m, "policy_id1") diff --git a/python_moonutilities/tests/unit_python/mock_repo/components_utilities.py b/python_moonutilities/tests/unit_python/mock_repo/components_utilities.py deleted file mode 100644 index 11686ce4..00000000 --- a/python_moonutilities/tests/unit_python/mock_repo/components_utilities.py +++ /dev/null @@ -1,136 +0,0 @@ -import base64 -import json - - -CONF = { - "openstack": { - "keystone": { - "url": "http://keystone:5000/v3", - "user": "admin", - "check_token": False, - "password": "p4ssw0rd", - "domain": "default", - "certificate": False, - "project": "admin" - } - }, - "components": { - "wrapper": { - "bind": "0.0.0.0", - "port": 8080, - "container": "wukongsun/moon_wrapper:v4.3", - "timeout": 5, - "hostname": "wrapper" - }, - "manager": { - "bind": "0.0.0.0", - "port": 8082, - "container": "wukongsun/moon_manager:v4.3", - "hostname": "manager" - }, - "port_start": 31001, - "orchestrator": { - "bind": "0.0.0.0", - "port": 8083, - "container": "wukongsun/moon_orchestrator:v4.3", - "hostname": "interface" - }, - "pipeline": { - "interface": { - "bind": "0.0.0.0", - "port": 8080, - "container": "wukongsun/moon_interface:v4.3", - "hostname": "interface" - }, - "authz": { - "bind": "0.0.0.0", - "port": 8081, - "container": "wukongsun/moon_authz:v4.3", - "hostname": "authz" - }, - } - }, - "logging": { - "handlers": { - "file": { - "filename": "/tmp/moon.log", - "class": "logging.handlers.RotatingFileHandler", - "level": "DEBUG", - "formatter": "custom", - "backupCount": 3, - "maxBytes": 1048576 - }, - "console": { - "class": "logging.StreamHandler", - "formatter": "brief", - "level": "INFO", - "stream": "ext://sys.stdout" - } - }, - "formatters": { - "brief": { - "format": "%(levelname)s %(name)s %(message)-30s" - }, - "custom": { - "format": "%(asctime)-15s %(levelname)s %(name)s %(message)s" - } - }, - "root": { - "handlers": [ - "console" - ], - "level": "ERROR" - }, - "version": 1, - "loggers": { - "moon": { - "handlers": [ - "console", - "file" - ], - "propagate": False, - "level": "DEBUG" - } - } - }, - "slave": { - "name": None, - "master": { - "url": None, - "login": None, - "password": None - } - }, - "docker": { - "url": "tcp://172.88.88.1:2376", - "network": "moon" - }, - "database": { - "url": "sqlite:///database.db", - # "url": "mysql+pymysql://moon:p4sswOrd1@db/moon", - "driver": "sql" - }, - "messenger": { - "url": "rabbit://moon:p4sswOrd1@messenger:5672/moon" - } -} - - -def get_b64_conf(component=None): - if component == "components": - return base64.b64encode( - json.dumps(CONF["components"]).encode('utf-8')+b"\n").decode('utf-8') - elif component in CONF: - return base64.b64encode( - json.dumps( - CONF[component]).encode('utf-8')+b"\n").decode('utf-8') - elif not component: - return base64.b64encode( - json.dumps(CONF).encode('utf-8')+b"\n").decode('utf-8') - elif "/" in component: - key1, _, key2 = component.partition("/") - return base64.b64encode( - json.dumps( - CONF[key1][key2]).encode('utf-8')+b"\n").decode('utf-8') - else: - return base64.b64encode(component.encode('utf-8')+b"\n").decode('utf-8') diff --git a/python_moonutilities/tests/unit_python/mock_repo/data.py b/python_moonutilities/tests/unit_python/mock_repo/data.py deleted file mode 100644 index 0e772e2c..00000000 --- a/python_moonutilities/tests/unit_python/mock_repo/data.py +++ /dev/null @@ -1,315 +0,0 @@ -components = ( - "logging", - "openstack/keystone", - "database", - "slave", - "components/manager", - "components/orchestrator", - "components/pipeline", - "components/port_start" -) - -shared_ids = { - "policy": { - "policy_id_1": "policy_id_1", - "policy_id_2": "policy_id_2", - "policy_id_3": "policy_id_3", - "policy_id_invalid_response": "policy_id_invalid_response" - }, - "category": { - "category_id_1": "category_id_1", - "invalid_category_id_1": " invalid_category_id_1" - }, - "perimeter": { - "perimeter_id_1": "subject_id_1", - "perimeter_id_2": "object_id_1", - "perimeter_id_3": "action_id_1" - }, - "meta_rule": { - "meta_rule_id_1": "meta_rule_id_1", - "meta_rule_id_2": "meta_rule_id_2" - }, - "rule": { - "rule_id_1": "rule_id_2", - "rule_id_2": "rule_id_2" - }, - "model": { - "model_id_1": "model_id_1" - }, - "subject": { - "subject_id_1": "subject_id_1", - "invalid_subject_id": "invalid_subject_id", - "invalid_category_id": "invalid_category_id", - "invalid_assignment_id": "invalid_assignment_id" - }, - "object": { - "object_id_1": "object_id_1", - "invalid_object_id": "invalid_object_id", - "invalid_category_id": "invalid_category_id", - "invalid_assignment_id": "invalid_assignment_id" - }, - "action": { - "action_id_1": "action_id_1", - "invalid_action_id": "invalid_action_id", - "invalid_category_id": "invalid_category_id", - "invalid_assignment_id": "invalid_assignment_id" - } -} - -pdp_mock = { - "pdp_id1": { - "name": "...", - "security_pipeline": ["policy_id_1", "policy_id_2"], - "keystone_project_id": "keystone_project_id1", - "description": "...", - } -} - -meta_rules_mock = { - shared_ids["meta_rule"]["meta_rule_id_1"]: { - "name": "meta_rule1", - "algorithm": "name of the meta rule algorithm", - "subject_categories": ["subject_category_id1", - "subject_category_id2"], - "object_categories": ["object_category_id1"], - "action_categories": ["action_category_id1"] - }, - shared_ids["meta_rule"]["meta_rule_id_2"]: { - "name": "name of the meta rules2", - "algorithm": "name of the meta rule algorithm", - "subject_categories": ["subject_category_id1", - "subject_category_id2"], - "object_categories": ["object_category_id1"], - "action_categories": ["action_category_id1"] - } -} - -policies_mock = { - shared_ids["policy"]["policy_id_1"]: { - "name": "test_policy1", - "model_id": shared_ids["model"]["model_id_1"], - "genre": "authz", - "description": "test", - } -} - -subject_mock = { - shared_ids["policy"]["policy_id_1"]: { - "subject_id": { - "name": "subject_name", - "keystone_id": "keystone_project_id1", - "description": "a description" - } - }, - shared_ids["policy"]["policy_id_invalid_response"]: { - "subject_id": { - "name": "subject_name", - "keystone_id": "keystone_project_id1", - "description": "a description" - } - } - -} - -subject_assignment_mock = { - shared_ids["subject"]["subject_id_1"]: { - "policy_id": shared_ids["policy"]["policy_id_1"], - "subject_id": "subject_id_1", - "category_id": shared_ids["category"]["category_id_1"], - "assignments": ["data_id_1, data_id_2"], - } -} - -subject_assignment_mock_invalid_subject_id = { - shared_ids["subject"]["invalid_subject_id"]: { - "policy_id": shared_ids["policy"]["policy_id_1"], - "subject_id_invalid": "subject_id_1", - "category_id": shared_ids["category"]["category_id_1"], - "assignments": ["data_id_1, data_id_2"], - } -} - -subject_assignment_mock_invalid_category_id = { - shared_ids["subject"]["invalid_category_id"]: { - "policy_id": shared_ids["policy"]["policy_id_1"], - "subject_id": "subject_id_1", - "category_id_invalid": shared_ids["category"]["category_id_1"], - "assignments": ["data_id_1, data_id_2"], - } -} - -subject_assignment_mock_invalid_assignment_id = { - shared_ids["subject"]["invalid_assignment_id"]: { - "policy_id": shared_ids["policy"]["policy_id_1"], - "subject_id": "subject_id_1", - "category_id": shared_ids["category"]["category_id_1"], - "assignments_invalid": ["data_id_1, data_id_2"], - } -} - -object_mock = { - shared_ids["policy"]["policy_id_1"]: { - "object_id": { - "name": "object_name", - "description": "a description" - } - } -} - -object_assignment_mock = { - shared_ids["object"]["object_id_1"]: { - "policy_id": shared_ids["policy"]["policy_id_1"], - "object_id": "object_id_1", - "category_id": shared_ids["category"]["category_id_1"], - "assignments": ["data_id_1, data_id_2"] - } -} - -object_assignment_mock_invalid_object_id = { - shared_ids["object"]["invalid_object_id"]: { - "policy_id": shared_ids["policy"]["policy_id_1"], - "object_id": "object_id_1", - "category_id": shared_ids["category"]["category_id_1"], - "assignments": ["data_id_1, data_id_2"] - } -} - -object_assignment_mock_invalid_category_id = { - shared_ids["object"]["invalid_category_id"]: { - "policy_id": shared_ids["policy"]["policy_id_1"], - "object_id": "object_id_1", - "category_id": shared_ids["category"]["category_id_1"], - "assignments": ["data_id_1, data_id_2"] - } -} - -object_assignment_mock_invalid_assignment_id = { - shared_ids["object"]["invalid_assignment_id"]: { - "policy_id": shared_ids["policy"]["policy_id_1"], - "object_id": "object_id_1", - "category_id": shared_ids["category"]["category_id_1"], - "assignments": ["data_id_1, data_id_2"] - } -} - -action_mock = { - shared_ids["policy"]["policy_id_1"]: { - "action_id": { - "name": "action_name", - "description": "a description" - } - } -} - -action_assignment_mock = { - shared_ids["action"]["action_id_1"]: { - "policy_id": shared_ids["policy"]["policy_id_1"], - "action_id": "action_id_1", - "category_id": shared_ids["category"]["category_id_1"], - "assignments": ["data_id_1, data_id_2"] - } -} - -action_assignment_mock_invalid_action_id = { - shared_ids["action"]["invalid_action_id"]: { - "policy_id": shared_ids["policy"]["policy_id_1"], - "action_id": "action_id_1", - "category_id": shared_ids["category"]["category_id_1"], - "assignments": ["data_id_1, data_id_2"] - } -} - -action_assignment_mock_invalid_category_id = { - shared_ids["action"]["invalid_category_id"]: { - "policy_id": shared_ids["policy"]["policy_id_1"], - "action_id": "action_id_1", - "category_id": shared_ids["category"]["category_id_1"], - "assignments": ["data_id_1, data_id_2"] - } -} - -action_assignment_mock_invalid_assignment_id = { - shared_ids["action"]["invalid_assignment_id"]: { - "policy_id": shared_ids["policy"]["policy_id_1"], - "action_id": "action_id_1", - "category_id": shared_ids["category"]["category_id_1"], - "assignments": ["data_id_1, data_id_2"] - } -} - - -models_mock = { - shared_ids["model"]["model_id_1"]: { - "name": "test_model", - "description": "test", - "meta_rules": [shared_ids["meta_rule"]["meta_rule_id_1"]] - } -} - -rules_mock = { - "rules": { - "meta_rule_id": shared_ids["meta_rule"]["meta_rule_id_1"], - shared_ids["rule"]["rule_id_1"]: { - "rule": ["subject_data_id1", - "object_data_id1", - "action_data_id1"], - "instructions": ( - {"decision": "grant"}, - # "grant" to immediately exit, - # "continue" to wait for the result of next policy - # "deny" to deny the request - ) - }, - shared_ids["rule"]["rule_id_2"]: { - "rule": ["subject_data_id2", - "object_data_id2", - "action_data_id2"], - "instructions": ( - { - "update": { - "operation": "add", - # operations may be "add" or "delete" - "target": "rbac:role:admin" - # add the role admin to the current user - } - }, - {"chain": {"name": "rbac"}} - # chain with the policy named rbac - ) - } - } -} - -# pods_mock = { -# # "name": "pod_id1", -# # "hostname": "pod_host", -# # "port": { -# # "PrivatePort": "8998", -# # "Type": "tcp", -# # "IP": "0.0.0.0", -# # "PublicPort": "8080" -# # }, -# # "keystone_project_id": "keystone_project_id1", -# # "pdp_id": "", -# # "meta_rule_id": "meta_rule_id1", -# # "container_name": "container_name1", -# # "plugin_name": "plugin_name1", -# # "container_id": "container_id" -# "pod_id1": { -# "name": "pod_id1", -# "hostname": "pod_host", -# "port": { -# "PrivatePort": "8998", -# "Type": "tcp", -# "IP": "0.0.0.0", -# "PublicPort": "8080" -# }, -# "keystone_project_id": [1], -# "pdp_id": "", -# "meta_rule_id": "meta_rule_id1", -# "container_name": "container_name1", -# "plugin_name": "plugin_name1", -# "container_id": "container_id" -# }, -# -# } diff --git a/python_moonutilities/tests/unit_python/mock_repo/urls.py b/python_moonutilities/tests/unit_python/mock_repo/urls.py deleted file mode 100644 index 41fd1eec..00000000 --- a/python_moonutilities/tests/unit_python/mock_repo/urls.py +++ /dev/null @@ -1,150 +0,0 @@ -import mock_repo.components_utilities as comp_util -import mock_repo.data as data_mock - - -def register_components(m): - for component in data_mock.components: - m.register_uri( - 'GET', 'http://consul:8500/v1/kv/{}'.format(component), - json=[{'Key': component, 'Value': comp_util.get_b64_conf(component)}] - ) - - m.register_uri( - 'PUT', 'http://consul:8500/v1/kv/components/port_start', - json=[] - ) - - m.register_uri( - 'GET', 'http://consul:8500/v1/kv/components?recurse=true', - json=[ - {"Key": key, "Value": comp_util.get_b64_conf(key)} for key in data_mock.components - ], - # json={'Key': "components", 'Value': get_b64_comp_util.CONF("components")} - ) - - -def register_keystone(m): - m.register_uri( - 'POST', 'http://keystone:5000/v3/auth/tokens', - headers={'X-Subject-Token': "111111111"} - ) - m.register_uri( - 'DELETE', 'http://keystone:5000/v3/auth/tokens', - headers={'X-Subject-Token': "111111111"} - ) - m.register_uri( - 'POST', 'http://keystone:5000/v3/users?name=testuser&domain_id=default', - json={"users": {}} - ) - m.register_uri( - 'GET', 'http://keystone:5000/v3/users?name=testuser&domain_id=default', - json={"users": {}} - ) - m.register_uri( - 'POST', 'http://keystone:5000/v3/users/', - json={"users": [{ - "id": "1111111111111" - }]} - ) - -def register_model_any(m, module_name, mocked_data, key=None): - if key is None: - key = module_name - m.register_uri( - 'GET', 'http://{}:{}/{}'.format(comp_util.CONF['components']['manager']['hostname'], - comp_util.CONF['components']['manager']['port'], module_name), - - json={key: mocked_data} - ) - -def register_policy_any(m, policy_id, module_name, mocked_data, key=None): - if key is None: - key = module_name - m.register_uri( - 'GET', 'http://{}:{}/{}/{}/{}'.format(comp_util.CONF['components']['manager']['hostname'], - comp_util.CONF['components']['manager']['port'], 'policies', - policy_id, module_name), - json={key: mocked_data} - ) - -def register_pdp(m): - register_model_any(m, 'pdp', data_mock.pdp_mock,'pdps') - -def register_meta_rules(m): - register_model_any(m, 'meta_rules',data_mock.meta_rules_mock) - -def register_policies(m): - register_model_any(m, 'policies', data_mock.policies_mock) - - -def register_models(m): - register_model_any(m, 'models', data_mock.models_mock) - -def register_policy_subject(m, policy_id): - register_policy_any(m, policy_id, 'subjects', data_mock.subject_mock[policy_id]) - - -def register_policy_subject_invalid_response(m, policy_id): - register_policy_any(m, policy_id, 'subjects', data_mock.subject_mock[policy_id],'subjects_invalid_key') - -def register_policy_object(m, policy_id): - register_policy_any(m, policy_id, 'objects', data_mock.object_mock[policy_id]) - -def register_policy_object_invalid_response(m, policy_id): - register_policy_any(m, policy_id, 'objects', data_mock.subject_mock[policy_id],'objects_invalid_key') - -def register_policy_action(m, policy_id): - register_policy_any(m, policy_id, 'actions', data_mock.action_mock[policy_id]) - -def register_policy_action_invalid_response(m, policy_id): - register_policy_any(m, policy_id, 'actions', data_mock.subject_mock[policy_id],'actions_invalid_key') - -def register_policy_subject_assignment_list(m, policy_id): - register_policy_any(m, policy_id, 'subject_assignments', data_mock.subject_assignment_mock) - -def register_policy_object_assignment_list(m, policy_id): - register_policy_any(m, policy_id, 'object_assignments', data_mock.object_assignment_mock) - - -def register_policy_action_assignment_list(m, policy_id): - register_policy_any(m, policy_id, 'action_assignments', data_mock.action_assignment_mock) - -def register_policy_subject_assignment(m, policy_id, perimeter_id): - m.register_uri( - 'GET', 'http://{}:{}/{}/{}/subject_assignments/{}'.format(comp_util.CONF['components']['manager']['hostname'], - comp_util.CONF['components']['manager']['port'], - 'policies', - policy_id, - perimeter_id), - json={'subject_assignments': data_mock.subject_assignment_mock} - ) - -def register_policy_object_assignment(m, policy_id, perimeter_id): - m.register_uri( - 'GET', 'http://{}:{}/{}/{}/object_assignments/{}'.format(comp_util.CONF['components']['manager']['hostname'], - comp_util.CONF['components']['manager']['port'], - 'policies', - policy_id, - perimeter_id), - json={'object_assignments': data_mock.object_assignment_mock} - ) - -def register_policy_action_assignment(m, policy_id, perimeter_id): - m.register_uri( - 'GET', 'http://{}:{}/{}/{}/action_assignments/{}'.format(comp_util.CONF['components']['manager']['hostname'], - comp_util.CONF['components']['manager']['port'], - 'policies', - policy_id, - perimeter_id), - json={'action_assignments': data_mock.action_assignment_mock} - ) - -def register_rules(m, policy_id): - register_policy_any(m, policy_id, 'rules', data_mock.rules_mock) - -# def register_pods(m): -# m.register_uri( -# 'GET', 'http://{}:{}/pods'.format(comp_util.CONF['components']['orchestrator']['hostname'], -# comp_util.CONF['components']['orchestrator']['port']), -# json={'pods': data_mock.pods_mock} -# ) diff --git a/python_moonutilities/tests/unit_python/requirements.txt b/python_moonutilities/tests/unit_python/requirements.txt deleted file mode 100644 index b08a2603..00000000 --- a/python_moonutilities/tests/unit_python/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -requests_mock
\ No newline at end of file diff --git a/python_moonutilities/tests/unit_python/test_cache.py b/python_moonutilities/tests/unit_python/test_cache.py deleted file mode 100644 index bef10a21..00000000 --- a/python_moonutilities/tests/unit_python/test_cache.py +++ /dev/null @@ -1,452 +0,0 @@ -import pytest -import mock_repo.data as data_mock -import mock_repo.urls as register_urls -import requests_mock - - -def test_authz_request(): - from python_moonutilities import cache - c = cache.Cache() - assert isinstance(c.authz_requests, dict) - - -# tests for get (subject) in cache -# ================================================ -def test_get_subject_success(): - from python_moonutilities import cache - cache_obj = cache.Cache() - name = 'subject_name' - subject_id = cache_obj.get_subject(data_mock.shared_ids["policy"]["policy_id_1"], name) - assert subject_id is not None - -def test_get_subject_no_policy(): - from python_moonutilities import cache - cache_obj = cache.Cache() - with pytest.raises(Exception) as exception_info: - cache_obj.get_subject(None, "") - assert str(exception_info.value) == '400: Policy Unknown' - -def test_get_subject_invalid_name(): - from python_moonutilities import cache - cache_obj = cache.Cache() - name = 'invalid name' - with pytest.raises(Exception) as exception_info: - cache_obj.get_subject(data_mock.shared_ids["policy"]["policy_id_1"], name) - assert str(exception_info.value) == '400: Subject Unknown' - -def test_get_subject_invalid_response(): - from python_moonutilities import cache - cache_obj = cache.Cache() - name = 'policy_id_invalid_response' - with pytest.raises(Exception) as exception_info: - cache_obj.get_subject(data_mock.shared_ids["policy"]["policy_id_invalid_response"], name) - assert str(exception_info.value) == '400: Subject Unknown' - -# tests for get (object) in cache -# ================================================ -def test_get_object_success(): - from python_moonutilities import cache - cache_obj = cache.Cache() - name = 'object_name' - object_id = cache_obj.get_object(data_mock.shared_ids["policy"]["policy_id_1"], name) - assert object_id is not None - -def test_get_object_no_policy(): - from python_moonutilities import cache - cache_obj = cache.Cache() - with pytest.raises(Exception) as exception_info: - cache_obj.get_object(None, "") - assert str(exception_info.value) == '400: Policy Unknown' - -def test_get_object_invalid_name(): - from python_moonutilities import cache - cache_obj = cache.Cache() - name = 'invalid name' - with pytest.raises(Exception) as exception_info: - cache_obj.get_object(data_mock.shared_ids["policy"]["policy_id_1"], name) - assert str(exception_info.value) == '400: Object Unknown' - -def test_get_object_invalid_response(): - from python_moonutilities import cache - cache_obj = cache.Cache() - name = 'policy_id_invalid_response' - with pytest.raises(Exception) as exception_info: - cache_obj.get_object(data_mock.shared_ids["policy"]["policy_id_invalid_response"], name) - assert str(exception_info.value) == '400: Object Unknown' - -# tests for get (action) in cache -# ================================================ -def test_get_action_success(): - from python_moonutilities import cache - cache_obj = cache.Cache() - name = 'action_name' - action_id = cache_obj.get_action(data_mock.shared_ids["policy"]["policy_id_1"], name) - assert action_id is not None - - -def test_get_action_no_policy(): - from python_moonutilities import cache - cache_obj = cache.Cache() - with pytest.raises(Exception) as exception_info: - cache_obj.get_action(None, "") - assert str(exception_info.value) == '400: Policy Unknown' - -def test_get_action_invalid_name(): - from python_moonutilities import cache - cache_obj = cache.Cache() - name = 'invalid name' - with pytest.raises(Exception) as exception_info: - cache_obj.get_action(data_mock.shared_ids["policy"]["policy_id_1"], name) - assert str(exception_info.value) == '400: Action Unknown' - -def test_get_action_invalid_response(): - from python_moonutilities import cache - cache_obj = cache.Cache() - name = 'policy_id_invalid_response' - with pytest.raises(Exception) as exception_info: - cache_obj.get_action(data_mock.shared_ids["policy"]["policy_id_invalid_response"], name) - assert str(exception_info.value) == '400: Action Unknown' - -# ==================================================================================================== - -# tests for get (subject_assignment) in cache -# ================================================================================= - -def test_get_subject_assignment_success(): - from python_moonutilities import cache - cache_obj = cache.Cache() - subject_assignments = cache_obj.get_subject_assignments(data_mock.shared_ids["policy"]["policy_id_1"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"], - data_mock.shared_ids["category"]["category_id_1"]) - assert subject_assignments is not None - -def test_get_subject_assignment_no_policy(): - from python_moonutilities import cache - cache_obj = cache.Cache() - with pytest.raises(Exception) as exception_info: - cache_obj.get_subject_assignments(None, - data_mock.shared_ids["perimeter"]["perimeter_id_1"], - data_mock.shared_ids["category"]["category_id_1"]) - assert str(exception_info.value) == '400: Policy Unknown' - - -@requests_mock.Mocker(kw='mock') -def test_get_subject_assignment_invalid_subject_id(**kwargs): - from python_moonutilities import cache - - register_urls.register_components(kwargs['mock']) - - kwargs['mock'].get('http://manager:8082/policies/{}/subject_assignments/{}' - .format(data_mock.shared_ids["subject"]["invalid_subject_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"]), - json={'subject_assignments': data_mock.subject_assignment_mock_invalid_subject_id} - ) - cache_obj = cache.Cache() - subject_assignments = cache_obj.get_subject_assignments(data_mock.shared_ids["subject"]["invalid_subject_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"], - data_mock.shared_ids["category"]["category_id_1"]) - assert len(subject_assignments) == 0 - - -@requests_mock.Mocker(kw='mock') -def test_get_subject_assignment_invalid_category_id(**kwargs): - from python_moonutilities import cache - - register_urls.register_components(kwargs['mock']) - kwargs['mock'].get('http://manager:8082/policies/{}/subject_assignments/{}' - .format(data_mock.shared_ids["subject"]["invalid_category_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"]), - json={'subject_assignments': data_mock.subject_assignment_mock_invalid_category_id} - ) - cache_obj = cache.Cache() - subject_assignments = cache_obj.get_subject_assignments(data_mock.shared_ids["subject"]["invalid_category_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"], - data_mock.shared_ids["category"]["category_id_1"]) - assert len(subject_assignments) == 0 - - -@requests_mock.Mocker(kw='mock') -def test_get_subject_assignment_invalid_assignment_id(**kwargs): - from python_moonutilities import cache - - register_urls.register_components(kwargs['mock']) - kwargs['mock'].get('http://manager:8082/policies/{}/subject_assignments/{}' - .format(data_mock.shared_ids["subject"]["invalid_assignment_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"]), - json={'subject_assignments': data_mock.subject_assignment_mock_invalid_assignment_id} - ) - - cache_obj = cache.Cache() - subject_assignments = cache_obj.get_subject_assignments(data_mock.shared_ids["subject"]["invalid_assignment_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"], - data_mock.shared_ids["category"]["category_id_1"]) - assert len(subject_assignments) == 0 - - -def test_get_subject_assignment_empty_perimeter(): - from python_moonutilities import cache - cache_obj = cache.Cache() - subject_assignments = cache_obj.get_subject_assignments(data_mock.shared_ids["policy"]["policy_id_2"], - None, - data_mock.shared_ids["category"]["category_id_1"]) - assert len(subject_assignments) == 0 - - -def test_get_subject_assignment_invalid_category_failure(): - from python_moonutilities import cache - cache_obj = cache.Cache() - subject_assignments = cache_obj.get_subject_assignments(data_mock.shared_ids["policy"]["policy_id_1"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"], - data_mock.shared_ids["category"]["invalid_category_id_1"]) - assert len(subject_assignments) == 0 - -# tests for get (object_assignment) in cache -# ========================================== -def test_get_object_assignment_success(): - from python_moonutilities import cache - cache_obj = cache.Cache() - object_assignments = cache_obj.get_object_assignments(data_mock.shared_ids["policy"]["policy_id_1"], - data_mock.shared_ids["perimeter"]["perimeter_id_2"], - data_mock.shared_ids["category"]["category_id_1"]) - assert object_assignments is not None - - -def test_get_object_assignment_no_policy(): - from python_moonutilities import cache - cache_obj = cache.Cache() - with pytest.raises(Exception) as exception_info: - cache_obj.get_object_assignments(None, data_mock.shared_ids["perimeter"]["perimeter_id_2"], - data_mock.shared_ids["category"]["category_id_1"]) - assert str(exception_info.value) == '400: Policy Unknown' - - -@requests_mock.Mocker(kw='mock') -def test_get_object_assignment_invalid_object_id(**kwargs): - from python_moonutilities import cache - - register_urls.register_components(kwargs['mock']) - - kwargs['mock'].get('http://manager:8082/policies/{}/object_assignments/{}' - .format(data_mock.shared_ids["object"]["invalid_object_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"]), - json={'object_assignments': data_mock.object_assignment_mock_invalid_object_id} - ) - cache_obj = cache.Cache() - object_assignments = cache_obj.get_object_assignments(data_mock.shared_ids["object"]["invalid_object_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"], - data_mock.shared_ids["category"]["category_id_1"]) - assert len(object_assignments) == 0 - - -@requests_mock.Mocker(kw='mock') -def test_get_object_assignment_invalid_category_id(**kwargs): - from python_moonutilities import cache - - register_urls.register_components(kwargs['mock']) - kwargs['mock'].get('http://manager:8082/policies/{}/object_assignments/{}' - .format(data_mock.shared_ids["object"]["invalid_category_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"]), - json={'object_assignments': data_mock.object_assignment_mock_invalid_category_id} - ) - cache_obj = cache.Cache() - object_assignments = cache_obj.get_object_assignments(data_mock.shared_ids["object"]["invalid_category_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"], - data_mock.shared_ids["category"]["category_id_1"]) - assert len(object_assignments) == 0 - - -@requests_mock.Mocker(kw='mock') -def test_get_object_assignment_invalid_assignment_id(**kwargs): - from python_moonutilities import cache - - register_urls.register_components(kwargs['mock']) - kwargs['mock'].get('http://manager:8082/policies/{}/object_assignments/{}' - .format(data_mock.shared_ids["object"]["invalid_assignment_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"]), - json={'object_assignments': data_mock.object_assignment_mock_invalid_assignment_id} - ) - - cache_obj = cache.Cache() - object_assignments = cache_obj.get_object_assignments(data_mock.shared_ids["object"]["invalid_assignment_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"], - data_mock.shared_ids["category"]["category_id_1"]) - assert len(object_assignments) == 0 - -def test_get_object_assignment_none_perimeter(): - from python_moonutilities import cache - cache_obj = cache.Cache() - object_assignments = cache_obj.get_object_assignments(data_mock.shared_ids["policy"]["policy_id_2"], - None, - data_mock.shared_ids["category"]["category_id_1"]) - assert len(object_assignments) == 0 - - -def test_get_object_assignment_invalid_category_failure(): - from python_moonutilities import cache - cache_obj = cache.Cache() - object_assignments = cache_obj.get_object_assignments(data_mock.shared_ids["policy"]["policy_id_1"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"], - data_mock.shared_ids["category"]["invalid_category_id_1"]) - assert len(object_assignments) == 0 - -# tests for get (action_assignment) in cache -# ========================================== -def test_get_action_assignment_success(): - from python_moonutilities import cache - cache_obj = cache.Cache() - action_assignments = cache_obj.get_action_assignments(data_mock.shared_ids["policy"]["policy_id_1"], - data_mock.shared_ids["perimeter"]["perimeter_id_3"], - data_mock.shared_ids["category"]["category_id_1"]) - assert action_assignments is not None - - -def test_get_action_assignment_no_policy(): - from python_moonutilities import cache - cache_obj = cache.Cache() - with pytest.raises(Exception) as exception_info: - cache_obj.get_action_assignments(None, data_mock.shared_ids["perimeter"]["perimeter_id_2"], - data_mock.shared_ids["category"]["category_id_1"]) - assert str(exception_info.value) == '400: Policy Unknown' - - -@requests_mock.Mocker(kw='mock') -def test_get_action_assignment_invalid_object_id(**kwargs): - from python_moonutilities import cache - - register_urls.register_components(kwargs['mock']) - - kwargs['mock'].get('http://manager:8082/policies/{}/action_assignments/{}' - .format(data_mock.shared_ids["action"]["invalid_action_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"]), - json={'action_assignments': data_mock.action_assignment_mock_invalid_action_id} - ) - cache_obj = cache.Cache() - action_assignments = cache_obj.get_action_assignments(data_mock.shared_ids["action"]["invalid_action_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"], - data_mock.shared_ids["category"]["category_id_1"]) - assert len(action_assignments) == 0 - - -@requests_mock.Mocker(kw='mock') -def test_get_action_assignment_invalid_category_id(**kwargs): - from python_moonutilities import cache - - register_urls.register_components(kwargs['mock']) - kwargs['mock'].get('http://manager:8082/policies/{}/action_assignments/{}' - .format(data_mock.shared_ids["action"]["invalid_category_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"]), - json={'action_assignments': data_mock.action_assignment_mock_invalid_category_id} - ) - cache_obj = cache.Cache() - action_assignments = cache_obj.get_action_assignments(data_mock.shared_ids["action"]["invalid_category_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"], - data_mock.shared_ids["category"]["category_id_1"]) - assert len(action_assignments) == 0 - - -@requests_mock.Mocker(kw='mock') -def test_get_action_assignment_invalid_assignment_id(**kwargs): - from python_moonutilities import cache - - register_urls.register_components(kwargs['mock']) - kwargs['mock'].get('http://manager:8082/policies/{}/action_assignments/{}' - .format(data_mock.shared_ids["action"]["invalid_assignment_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"]), - json={'action_assignments': data_mock.action_assignment_mock_invalid_assignment_id} - ) - - cache_obj = cache.Cache() - action_assignments = cache_obj.get_action_assignments(data_mock.shared_ids["action"]["invalid_assignment_id"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"], - data_mock.shared_ids["category"]["category_id_1"]) - assert len(action_assignments) == 0 - -def test_get_action_assignment_none_perimeter(): - from python_moonutilities import cache - cache_obj = cache.Cache() - action_assignments = cache_obj.get_action_assignments(data_mock.shared_ids["policy"]["policy_id_2"], - None, - data_mock.shared_ids["category"]["category_id_1"]) - assert len(action_assignments) == 0 - - -def test_get_action_assignment_invalid_category_failure(): - from python_moonutilities import cache - cache_obj = cache.Cache() - action_assignments = cache_obj.get_action_assignments(data_mock.shared_ids["policy"]["policy_id_1"], - data_mock.shared_ids["perimeter"]["perimeter_id_1"], - data_mock.shared_ids["category"]["invalid_category_id_1"]) - assert len(action_assignments) == 0 - - -# ==================================================================================================== - -# tests for helper function in cache -# ================================== -def test_get_policy_from_meta_rules_success(): - from python_moonutilities import cache - cache_obj = cache.Cache() - policy_id = cache_obj.get_policy_from_meta_rules(data_mock.shared_ids["meta_rule"]["meta_rule_id_1"]) - assert policy_id is not None - -''' tests for containers function , security pipline in cache which not used for now - need to mock pdp object, /pods correctly -''' - -# def test_get_policy_from_meta_rules_failure(): -# from python_moonutilities import cache -# cache_obj = cache.Cache() -# meta_rule_id = 'meta_rule_id3' -# policy_id = cache_obj.get_policy_from_meta_rules(meta_rule_id) -# assert policy_id is None - -# def test_get_pdp_from_keystone_project_success(): -# from python_moonutilities import cache -# cache_obj = cache.Cache() -# keystone_project_id = 'keystone_project_id1' -# pdp_key = cache_obj.get_pdp_from_keystone_project(keystone_project_id) -# assert pdp_key is not None -# -# -# def test_get_pdp_from_keystone_project_failure(): -# from python_moonutilities import cache -# cache_obj = cache.Cache() -# keystone_project_id = 'keystone_project_id2' -# pdp_key = cache_obj.get_pdp_from_keystone_project(keystone_project_id) -# assert pdp_key is None -# -# -# def test_get_keystone_project_id_from_policy_id_success(): -# from python_moonutilities import cache -# cache_obj = cache.Cache() -# keystone_project_id = cache_obj.get_keystone_project_id_from_policy_id( -# data_mock.shared_ids["policy"]["policy_id_1"]) -# assert keystone_project_id is not None -# -# -# def test_get_keystone_project_id_from_policy_id_failure(): -# from python_moonutilities import cache -# cache_obj = cache.Cache() -# policy_id = 'policy_id_3' -# keystone_project_id = cache_obj.get_keystone_project_id_from_policy_id(policy_id) -# assert keystone_project_id is None - - -# def test_get_containers_from_keystone_project_id_success(): -# from python_moonutilities import cache -# cache_obj = cache.Cache() -# keystone_project_id = 1 -# meta_rule_id = 'meta_rule_id1' -# container_id, container_value = cache_obj.get_containers_from_keystone_project_id(keystone_project_id, meta_rule_id) -# assert container_id, container_value is not None - - -def test_cache_manager(): - from python_moonutilities import cache - cache_obj = cache.Cache() -# assert cache_obj.pdp is not None - assert cache_obj.meta_rules is not None - assert len(cache_obj.meta_rules) == 2 - assert cache_obj.policies is not None - assert len(cache_obj.policies) == 1 - assert cache_obj.models is not None diff --git a/python_moonutilities/tests/unit_python/test_configuration.py b/python_moonutilities/tests/unit_python/test_configuration.py deleted file mode 100644 index d5d83f7a..00000000 --- a/python_moonutilities/tests/unit_python/test_configuration.py +++ /dev/null @@ -1,166 +0,0 @@ -import mock_repo.components_utilities as comp_util -import pytest -import requests_mock - - -def test_get_configuration_success(): - from python_moonutilities import configuration - assert configuration.get_configuration("components/port_start")["components/port_start"] == comp_util.CONF["components"]["port_start"] - - -@requests_mock.Mocker(kw='mock') -def test_get_configuration_mutliple_list_success(**kwargs): - from python_moonutilities import configuration - - kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start', - json=[ - {'Key': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")}, - {'Key': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")} - ] - ) - - assert len(configuration.get_configuration("components/port_start")) == 2 - - -@requests_mock.Mocker(kw='mock') -def test_get_configuration_mutliple_list_failure(**kwargs): - from python_moonutilities import configuration - - kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start', - json=[ - {'Key': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")}, - {'invalidKey': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")} - ] - ) - with pytest.raises(Exception) as exception_info: - configuration.get_configuration("components/port_start") - assert str(exception_info.value) == '500: Consul Content error' - - -@requests_mock.Mocker(kw='mock') -def test_get_configuration_not_found(**kwargs): - from python_moonutilities import configuration - - kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start_wrong', json=[ - ], status_code=500) - with pytest.raises(Exception) as exception_info: - configuration.get_configuration("components/port_start_wrong") - assert str(exception_info.value) == '500: Consul error' - - -@requests_mock.Mocker(kw='mock') -def test_get_configuration_invalid_response(**kwargs): - from python_moonutilities import configuration - - kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start', json=[ - {"port_start":'port_start', 'Value': comp_util.get_b64_conf("components/port_start")} - ]) - with pytest.raises(Exception) as exception_info: - configuration.get_configuration("components/port_start") - assert str(exception_info.value) == '500: Consul Content error' - - -################################ increment_port #################################### -@requests_mock.Mocker(kw='mock') -def test_put_increment_port_invalidkey_failure(**kwargs): - from python_moonutilities import configuration - - kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start', json=[ - {'Key': 'invalidkey', 'Value': comp_util.get_b64_conf("components/port_start")} - ], status_code=200) - with pytest.raises(Exception) as exception_info: - configuration.increment_port() - assert str(exception_info.value) == '500: Consul Content error' - - -@requests_mock.Mocker(kw='mock') -def test_put_increment_port_failure(**kwargs): - from python_moonutilities import configuration - kwargs['mock'].put('http://consul:8500/v1/kv/components/port_start', json=[], status_code=400) - kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start', json=[ - {'Key': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")} - ], status_code=200) - with pytest.raises(Exception) as exception_info: - configuration.increment_port() - assert str(exception_info.value) == '500: Consul Content error' - - -def test_increment_port_success(): - from python_moonutilities import configuration - cur_port = comp_util.CONF["components"]["port_start"] - incremented_port = configuration.increment_port() - assert incremented_port == cur_port + 1 - - -################################ plugin #################################### -def test_get_plugins_success(): - from python_moonutilities import configuration - plugin = configuration.get_plugins() - assert plugin is not None - -def test_get_plugins_failure(no_requests): - from python_moonutilities import configuration - no_requests.register_uri( - 'GET', 'http://consul:8500/v1/kv/components/pipeline', - json=[{'Key': 'components/pipeline', 'Value': 'eyJjb250YWluZXIiOiAid3Vrb25nc3VuL21vb25fYXV0aHo6djQuMyIsICJwb3J0IjogODA4MX0='}] - ) - with pytest.raises(Exception) as exception_info: - configuration.get_plugins() - assert str(exception_info.value) == '500: Consul Content error' -################################ component #################################### -def test_get_components(): - from python_moonutilities import configuration - assert isinstance(configuration.get_components(), dict) - - -@requests_mock.Mocker(kw='mock') -def test_get_components_mutliple_list_success(**kwargs): - from python_moonutilities import configuration - - kwargs['mock'].get('http://consul:8500/v1/kv/components?recurse=true', - json=[ - {'Key': 'components/c1', 'Value': 'eyJjb250YWluZXIiOiAid3Vrb25nc3VuL21vb25fYXV0aHo6djQuMyIsICJwb3J0IjogODA4MX0='}, - {'Key': 'components/c2', 'Value': 'eyJjb250YWluZXIiOiAid3Vrb25nc3VuL21vb25fYXV0aHo6djQuMyIsICJwb3J0IjogODA4MX0='} - ] - ) - - res = configuration.get_components() - assert bool(res) - - -@requests_mock.Mocker(kw='mock') -def test_get_components_mutliple_list_failure(**kwargs): - from python_moonutilities import configuration - - kwargs['mock'].get('http://consul:8500/v1/kv/components?recurse=true', - json=[ - {'Key': 'components/c1', 'Value': "eyJjb250YWluZXIiOiAid3Vrb25"}, - {'invalidKey': 'components/c2', 'Value': "eyJjb250YWluZXIiOiAid3Vrb25"} - ] - ) - with pytest.raises(Exception) as exception_info: - configuration.get_components() - assert str(exception_info.value) == '500: Consul Content error' - - -@requests_mock.Mocker(kw='mock') -def test_get_components_not_found(**kwargs): - from python_moonutilities import configuration - - kwargs['mock'].get('http://consul:8500/v1/kv/components?recurse=true', json=[ - ], status_code=500) - with pytest.raises(Exception) as exception_info: - configuration.get_components() - assert str(exception_info.value) == '400: Consul error' - - -@requests_mock.Mocker(kw='mock') -def test_get_components_invalid_response(**kwargs): - from python_moonutilities import configuration - - kwargs['mock'].get('http://consul:8500/v1/kv/components?recurse=true', json=[ - {"invalidKey":'invalid', 'Value': "jb250"} - ]) - with pytest.raises(Exception) as exception_info: - configuration.get_components() - assert str(exception_info.value) == '500: Consul Content error' diff --git a/python_moonutilities/tests/unit_python/test_validated_input.py b/python_moonutilities/tests/unit_python/test_validated_input.py deleted file mode 100644 index 723bc8ba..00000000 --- a/python_moonutilities/tests/unit_python/test_validated_input.py +++ /dev/null @@ -1,154 +0,0 @@ -# Copyright 2018 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 - - -def test_valid_string(): - from python_moonutilities.security_functions import validate_data - validate_data("CorrectString") - validate_data("Correct String") - validate_data("Correct String!") - validate_data("Correct String@") - validate_data(None) - validate_data(True) - - -def test_invalid_string(): - from python_moonutilities.security_functions import validate_data - with pytest.raises(Exception) as exception_info: - validate_data("Notcorrect<a>String") - - assert str(exception_info.value) == 'Forbidden characters in string' - - -def test_none_value(): - from python_moonutilities.security_functions import validate_data - with pytest.raises(Exception) as exception_info: - validate_data(object) - - assert 'Value is Not String or Container or Dictionary' in str(exception_info.value) - - -def test_numeric_value(): - from python_moonutilities.security_functions import validate_data - with pytest.raises(Exception) as exception_info: - validate_data(1) - assert 'Value is Not String or Container or Dictionary' in str(exception_info.value) - - with pytest.raises(Exception) as exception_info: - validate_data(1.23) - assert 'Value is Not String or Container or Dictionary' in str(exception_info.value) - - -def test_correct_list_one_element(): - from python_moonutilities.security_functions import validate_data - validate_data(["test_1", "test_2", "test_3"]) - - -def test_correct_list_multiple_element(): - from python_moonutilities.security_functions import validate_data - validate_data(["test"]) - - -def test_correct_nested_list(): - from python_moonutilities.security_functions import validate_data - validate_data([["test_1", "test_2"], [["test_3"], ["test_4"]], ["test_5", "test_6"], ["test_7"]]) - - -def test_incorrect_string_inside_list(): - from python_moonutilities.security_functions import validate_data - with pytest.raises(Exception) as exception_info: - validate_data(["test_1", ["test_2", "forbidden<a>character"]]) - - assert str(exception_info.value) == 'Forbidden characters in string' - - -def test_correct_tuples(): - from python_moonutilities.security_functions import validate_data - validate_data(("test_1", "test_2")) - - -def test_correct_tuple_of_tuple(): - from python_moonutilities.security_functions import validate_data - validate_data(("test_1", ("test_2", "test_3"), (("test_4", "test_5"), ("test_6", "test_7")))) - - -def test_incorrect_string_within_tuple(): - from python_moonutilities.security_functions import validate_data - with pytest.raises(Exception) as exception_info: - validate_data(("test_1", "forbidden<a>character")) - - assert str(exception_info.value) == 'Forbidden characters in string' - - -def test_correct_dictionary(): - from python_moonutilities.security_functions import validate_data - validate_data({"test_1": "test_2"}) - - -def test_incorrect_string_within_dictionary(): - from python_moonutilities.security_functions import validate_data - with pytest.raises(Exception) as exception_info: - validate_data({"test_1": "forbidden<a>character"}) - - assert str(exception_info.value) == 'Forbidden characters in string' - - -def test_correct_function_pass(): - from python_moonutilities.security_functions import validate_input - - @validate_input() - def temp_function(string, list, tuple): - if string != "teststring": - raise ValueError("values which passed incorrect") - - temp_function("teststring", ["teststring", ["teststring"]], ("teststring", ("teststring", ))) - - -def test_incorrect_validating_function_with_kwargs(): - from python_moonutilities.security_functions import validate_input - - @validate_input(kwargs_state=[True,True]) - def temp_function(string, list, tuple): - if string != "teststring": - raise ValueError("values which passed incorrect") - - with pytest.raises(Exception) as exception_info: - temp_function("teststring", list=["teststring", ["testst<a>ring"]],tuple=("teststring", ("teststri<a>ng", ))) - - assert str(exception_info.value) == 'Forbidden characters in string' - - -def test_incorrect_validating_function(): - from python_moonutilities.security_functions import validate_input - - @validate_input() - def temp_function(string, list, dictionary): - if string != "teststring": - raise ValueError("values which passed incorrect") - - with pytest.raises(Exception) as exception_info: - temp_function("teststring", ["teststring", ["teststri<a>ng"]], {"teststring": ("teststring", )}) - - assert str(exception_info.value) == 'Forbidden characters in string' - - -def test_incorrect_validating_class_function(): - from python_moonutilities.security_functions import validate_input - - class Testclass: - @validate_input() - def temp_function(self, string, list, dictionary): - if string != "teststring": - raise ValueError("values which passed incorrect") - - e = Testclass() - - with pytest.raises(Exception) as exception_info: - e.temp_function("teststring", ["teststring", ["teststri<a>ng"]], {"teststring": ("teststring", )}) - - assert str(exception_info.value) == 'Forbidden characters in string' |