diff options
author | Thomas Duval <thomas.duval@orange.com> | 2017-12-18 10:14:37 +0100 |
---|---|---|
committer | Thomas Duval <thomas.duval@orange.com> | 2017-12-18 10:29:09 +0100 |
commit | f9a5db2b063a1fbbb1c7f8172fc0167794aa7d2d (patch) | |
tree | f8688efc079d48e2cfd46d97d9c6d9dbb2d4ef73 | |
parent | 32604dab581f7a3587fa363022876f06ea4f3d40 (diff) |
Moon_utilities cleanup
Change-Id: Ia1817db7b1eb12f4a43f3babebda963b8830c4c0
-rw-r--r-- | moonv4/moon_interface/tools/get_keystone_token.py | 1 | ||||
-rw-r--r-- | moonv4/moon_utilities/Changelog | 4 | ||||
-rw-r--r-- | moonv4/moon_utilities/moon_utilities/__init__.py | 2 | ||||
-rw-r--r-- | moonv4/moon_utilities/moon_utilities/configuration.py | 2 | ||||
-rw-r--r-- | moonv4/moon_utilities/moon_utilities/exceptions.py | 20 | ||||
-rw-r--r-- | moonv4/moon_utilities/moon_utilities/get_os_apis.py | 134 | ||||
-rw-r--r-- | moonv4/moon_utilities/moon_utilities/misc.py | 2 | ||||
-rw-r--r-- | moonv4/moon_utilities/moon_utilities/options.py | 300 | ||||
-rw-r--r-- | moonv4/moon_utilities/moon_utilities/security_functions.py | 20 |
9 files changed, 14 insertions, 471 deletions
diff --git a/moonv4/moon_interface/tools/get_keystone_token.py b/moonv4/moon_interface/tools/get_keystone_token.py index 63b0d0b6..a153f4db 100644 --- a/moonv4/moon_interface/tools/get_keystone_token.py +++ b/moonv4/moon_interface/tools/get_keystone_token.py @@ -1,7 +1,6 @@ import requests from oslo_config import cfg from oslo_log import log as logging -from moon_utilities import options # noqa from moon_utilities import exceptions CONF = cfg.CONF diff --git a/moonv4/moon_utilities/Changelog b/moonv4/moon_utilities/Changelog index 81b7c46d..952c2aa1 100644 --- a/moonv4/moon_utilities/Changelog +++ b/moonv4/moon_utilities/Changelog @@ -59,3 +59,7 @@ CHANGES ----- - Add a waiting loop when the Keystone server is not currently available +1.4.1 +----- +- Cleanup moon_utilities code + diff --git a/moonv4/moon_utilities/moon_utilities/__init__.py b/moonv4/moon_utilities/moon_utilities/__init__.py index 8b353f9f..e3ad9307 100644 --- a/moonv4/moon_utilities/moon_utilities/__init__.py +++ b/moonv4/moon_utilities/moon_utilities/__init__.py @@ -3,4 +3,4 @@ # 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.0" +__version__ = "1.4.1" diff --git a/moonv4/moon_utilities/moon_utilities/configuration.py b/moonv4/moon_utilities/moon_utilities/configuration.py index 97727c39..cda75de5 100644 --- a/moonv4/moon_utilities/moon_utilities/configuration.py +++ b/moonv4/moon_utilities/moon_utilities/configuration.py @@ -17,8 +17,6 @@ CONSUL_HOST = "consul" CONSUL_PORT = "8500" DATABASE = "database" -SLAVE = "slave" -MESSENGER = "messenger" KEYSTONE = "keystone" DOCKER = "docker" COMPONENTS = "components" diff --git a/moonv4/moon_utilities/moon_utilities/exceptions.py b/moonv4/moon_utilities/moon_utilities/exceptions.py index ba5ecf46..eb606432 100644 --- a/moonv4/moon_utilities/moon_utilities/exceptions.py +++ b/moonv4/moon_utilities/moon_utilities/exceptions.py @@ -138,26 +138,6 @@ class ModelExisting(MoonError): logger = "Error" -class RootExtensionUnknown(IntraExtensionUnknown): - description = _("The root_extension is unknown.") - code = 400 - title = 'Root Extension Unknown' - logger = "Error" - - -class RootPDPNotInitialized(IntraExtensionException): - description = _("The root_extension is not initialized.") - code = 400 - title = 'Root Extension Not Initialized' - logger = "Error" - - -class IntraExtensionCreationError(IntraExtensionException): - description = _("The arguments for the creation of this Extension were malformed.") - code = 400 - title = 'Intra Extension Creation Error' - - # Authz exceptions class AuthzException(MoonError): diff --git a/moonv4/moon_utilities/moon_utilities/get_os_apis.py b/moonv4/moon_utilities/moon_utilities/get_os_apis.py deleted file mode 100644 index 0008b285..00000000 --- a/moonv4/moon_utilities/moon_utilities/get_os_apis.py +++ /dev/null @@ -1,134 +0,0 @@ -import json -import yaml -import logging -import requests -import argparse - -URLS = { - "keystone": "https://api.github.com/repos/openstack/keystone/contents/api-ref/source/v3", - "nova": "https://api.github.com/repos/openstack/nova/contents/api-ref/source", - "neutron": "https://api.github.com/repos/openstack/neutron-lib/contents/api-ref/source/v2", - "glance": "https://api.github.com/repos/openstack/glance/contents/api-ref/source/v2", - "swift": "https://api.github.com/repos/openstack/swift/contents/api-ref/source", - "cinder": "https://api.github.com/repos/openstack/cinder/contents/api-ref/source/v3", - -} - -logger = None - -USER = "" -PASS = "" - - -def init(): - global logger, USER, PASS - parser = argparse.ArgumentParser() - parser.add_argument("--verbose", "-v", action='store_true', help="verbose mode") - parser.add_argument("--debug", "-d", action='store_true', help="debug mode") - parser.add_argument("--format", "-f", help="Output format (txt, json)", default="json") - parser.add_argument("--output", "-o", help="Output filename") - parser.add_argument("--from-policies", "-p", help="Get API from policy.{json,yaml}", target="policies") - parser.add_argument("--credentials", "-c", help="Github credential filename (inside format user:pass)") - args = parser.parse_args() - - FORMAT = '%(levelname)s %(message)s' - - if args.verbose: - logging.basicConfig( - format=FORMAT, - level=logging.INFO) - elif args.debug: - logging.basicConfig( - format=FORMAT, - level=logging.DEBUG) - else: - logging.basicConfig( - format=FORMAT, - level=logging.WARNING) - - if args.credentials: - cred = open(args.credentials).read() - USER = cred.split(":")[0] - PASS = cred.split(":")[1] - - logger = logging.getLogger(__name__) - - return args - - -def get_api_item(url): - if USER: - r = requests.get(url, auth=(USER, PASS)) - else: - r = requests.get(url) - items = [] - for line in r.text.splitlines(): - if ".. rest_method::" in line: - items.append(line.replace(".. rest_method::", "").strip()) - logger.debug("\n\t".join(items)) - return items - - -def get_content(key, args): - logger.info("Analysing {}".format(key)) - if USER: - r = requests.get(URLS[key], auth=(USER, PASS)) - else: - r = requests.get(URLS[key]) - data = r.json() - results = {} - for item in data: - try: - logger.debug("{} {}".format(item['name'], item['download_url'])) - if item['type'] == "file" and ".inc" in item['name']: - results[item['name'].replace(".inc", "")] = get_api_item(item['download_url']) - except TypeError: - logger.error("Error with {}".format(item)) - except requests.exceptions.MissingSchema: - logger.error("MissingSchema error {}".format(item)) - return results - - -def to_str(results): - output = "" - for key in results: - output += "{}\n".format(key) - for item in results[key]: - output += "\t{}\n".format(item) - for value in results[key][item]: - output += "\t\t{}\n".format(value) - return output - - -def get_data_from_policies(policies): - return - # for filename in policies.split(","): - # try: - # obj = json.loads(open(filename.strip()).read()) - - -def save(results, args): - if args.output: - if args.format == 'json': - json.dump(results, open(args.output, "w"), indent=4) - elif args.format == 'txt': - open(args.output, "w").write(to_str(results)) - else: - if args.format == 'json': - print(json.dumps(results, indent=4)) - elif args.format in ('txt', 'text'): - print(to_str(results)) - - -def main(): - args = init() - results = {} - if not args.policies: - for key in URLS: - results[key] = get_content(key, args) - else: - get_data_from_policies(args.policies) - save(results, args) - -if __name__ == "__main__": - main() diff --git a/moonv4/moon_utilities/moon_utilities/misc.py b/moonv4/moon_utilities/moon_utilities/misc.py index 4eadd476..b83523c3 100644 --- a/moonv4/moon_utilities/moon_utilities/misc.py +++ b/moonv4/moon_utilities/moon_utilities/misc.py @@ -11,13 +11,11 @@ LOG = logging.getLogger(__name__) def get_uuid_from_name(name, elements, **kwargs): - LOG.error("get_uuid_from_name {} {} {}".format(name, elements, kwargs)) for element in elements: if type(elements[element]) is dict and elements[element].get('name') == name: if kwargs: for args in kwargs: if elements[element].get(args) != kwargs[args]: - LOG.error("get_uuid_from_name2 {} {} {}".format(args, elements[element].get(args), kwargs[args])) return else: return element diff --git a/moonv4/moon_utilities/moon_utilities/options.py b/moonv4/moon_utilities/moon_utilities/options.py deleted file mode 100644 index 8b8ccca4..00000000 --- a/moonv4/moon_utilities/moon_utilities/options.py +++ /dev/null @@ -1,300 +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 os -import sys -from oslo_config import cfg -from oslo_log import log as logging -from moon_utilities import __version__ - -LOG = logging.getLogger(__name__) -CONF = cfg.CONF - -__CWD__ = os.path.dirname(os.path.abspath(__file__)) - - -def configure(domain="moon", version=__version__, usage=""): - # FIXME (dthom): put DEBUG as default log level doesn't work - extra_log_level_defaults = [ - '{}=DEBUG'.format(__name__), - ] - # LOG.setLevel(logging.DEBUG) - logging.set_defaults( - default_log_levels=logging.get_default_log_levels() + extra_log_level_defaults) - - logging.register_options(CONF) - logging.setup(CONF, domain) - - CONF.register_opts(get_opts()) - - # rabbit_group = cfg.OptGroup(name='messenger', - # title='Messenger options') - # CONF.register_group(rabbit_group) - # CONF.register_opts(get_messenger_opts(), group="messenger") - - slave_group = cfg.OptGroup(name='slave', - title='Messenger options') - CONF.register_group(slave_group) - CONF.register_opts(get_slave_opts(), group="slave") - - database_group = cfg.OptGroup(name='database', - title='Database options') - CONF.register_group(database_group) - CONF.register_opts(get_database_opts(), group="database") - - database_configuration_group = cfg.OptGroup(name='database_configuration', - title='Database configuration options') - CONF.register_group(database_configuration_group) - CONF.register_opts(get_database_configuration_opts(), group="database_configuration") - - orchestrator_group = cfg.OptGroup(name='orchestrator', - title='Orchestrator options') - CONF.register_group(orchestrator_group) - CONF.register_opts(get_orchestrator_opts(), group="orchestrator") - - secrouter_group = cfg.OptGroup(name='security_router', - title='Security Router options') - CONF.register_group(secrouter_group) - CONF.register_opts(get_security_router_opts(), group="security_router") - - manager_group = cfg.OptGroup(name='security_manager', - title='Manager options') - CONF.register_group(manager_group) - CONF.register_opts(get_manager_opts(), group="security_manager") - - secpolicy_group = cfg.OptGroup(name='security_policy', - title='Security policy options') - CONF.register_group(secpolicy_group) - CONF.register_opts(get_security_policy_opts(), group="security_policy") - - secfunction_group = cfg.OptGroup(name='security_function', - title='Security function options') - CONF.register_group(secfunction_group) - CONF.register_opts(get_security_function_opts(), group="security_function") - - interface_group = cfg.OptGroup(name='interface', - title='Interface options') - CONF.register_group(interface_group) - CONF.register_opts(get_interface_opts(), group="interface") - - keystone_group = cfg.OptGroup(name='keystone', - title='Keystone options') - CONF.register_group(keystone_group) - CONF.register_opts(get_keystone_opts(), group="keystone") - - filename = "moon.conf" - for _filename in ( - "/etc/moon/{}", - "conf/{}", - "../conf/{}", - ): - try: - default_config_files = (_filename.format(filename), ) - CONF(args=sys.argv[1:], - project=domain, - # version=pbr.version.VersionInfo('keystone').version_string(), - version=version, - usage=usage, - default_config_files=default_config_files) - except cfg.ConfigFilesNotFoundError: - continue - else: - LOG.info("Using {} configuration file".format(_filename.format(filename))) - return _filename.format(filename) - - -def get_opts(): - return [ - cfg.StrOpt('proxy', - default="", - help='Proxy server to use'), - cfg.StrOpt('dist_dir', - default="", - help='Directory where the python packages can be found'), - cfg.StrOpt('plugin_dir', - default="", - help='Directory where the python plugins can be found'), - cfg.StrOpt('docker_url', - default="unix://var/run/docker.sock", - help='Docker URL to connect to.'), - cfg.StrOpt('policy_directory', - default="/etc/moon/policies", - help='Directory containing all the intra-extension templates'), - cfg.StrOpt('root_policy_directory', - default="/etc/moon/policies/policy_root", - help='Directory containing the Root intra-extension template'), - cfg.StrOpt('master', - default="", - help='URL of the Moon Master'), - cfg.StrOpt('master_login', - default="", - help='Login to log into the Moon Master'), - cfg.StrOpt('master_password', - default="", - help='Password for the Moon Master'), - ] - - -# def get_messenger_opts(): -# return [ -# cfg.StrOpt('host', -# default="0.0.0.0", -# help='RabbitMQ server name or IP.'), -# cfg.IntOpt('port', -# default=8800, -# help='RabbitMQ server port.'), -# ] - - -def get_orchestrator_opts(): - return [ - cfg.StrOpt('host', - default="127.0.0.1", - help='Host binding'), - cfg.IntOpt('port', - default=38000, - help='Port number of the server'), - ] - - -def get_slave_opts(): - return [ - cfg.StrOpt('slave_name', - default="", - help='name of the slave'), - cfg.StrOpt('master_url', - default="", - help='URL of the RabbitMQ bus of the Master, ' - 'example: master_url=rabbit://moon:p4sswOrd1@messenger:5672/moon'), - cfg.StrOpt('master_login', - default="", - help='login name of the master administrator, example: master_login=admin'), - cfg.StrOpt('master_password', - default="", - help='password of the master administrator, example: master_password=XXXXXXX'), - ] - - -def get_security_router_opts(): - return [ - cfg.StrOpt('container', - default="", - help='Name of the container to download (if empty build from scratch)'), - cfg.StrOpt('host', - default="127.0.0.1", - help='Host binding'), - cfg.IntOpt('port', - default=38001, - help='Port number of the server'), - ] - - -def get_manager_opts(): - return [ - cfg.StrOpt('container', - default="", - help='Name of the container to download (if empty build from scratch)'), - cfg.StrOpt('host', - default="127.0.0.1", - help='Host binding'), - cfg.IntOpt('port', - default=38001, - help='Port number of the server'), - ] - - -def get_security_policy_opts(): - return [ - cfg.StrOpt('container', - default="", - help='Name of the container to download (if empty build from scratch)'), - ] - - -def get_security_function_opts(): - return [ - cfg.StrOpt('container', - default="", - help='Name of the container to download (if empty build from scratch)'), - ] - - -def get_interface_opts(): - return [ - cfg.StrOpt('container', - default="", - help='Name of the container to download (if empty build from scratch)'), - cfg.StrOpt('host', - default="127.0.0.1", - help='Host binding'), - cfg.IntOpt('port', - default=38002, - help='Port number of the server'), - ] - - -def get_database_opts(): - return [ - cfg.StrOpt('url', - default="mysql+pymysql://moonuser:password@localhost/moon", - help='URL of the database'), - cfg.StrOpt('driver', - default="sql", - help='Driver binding'), - ] - - -def get_database_configuration_opts(): - return [ - cfg.StrOpt('url', - default="", - help='URL of the database'), - cfg.StrOpt('driver', - default="memory", - help='Driver binding'), - ] - - -def get_keystone_opts(): - return [ - cfg.StrOpt('url', - default="http://localhost:35357", - help='URL of the Keystone manager.'), - cfg.StrOpt('user', - default="admin", - help='Username of the Keystone manager.'), - cfg.StrOpt('password', - default="nomoresecrete", - help='Password of the Keystone manager.'), - cfg.StrOpt('project', - default="admin", - help='Project used to connect to the Keystone manager.'), - cfg.StrOpt('domain', - default="Default", - help='Default domain for the Keystone manager.'), - cfg.StrOpt('check_token', - default="true", - help='If true, yes or strict, always check Keystone tokens against the server'), - cfg.StrOpt('server_crt', - default="", - help='If using Keystone in HTTPS mode, give a certificate filename here'), - ] - -filename = configure() - - -def get_docker_template_dir(templatename="template.dockerfile"): - path = os.path.dirname(os.path.abspath(filename)) - PATHS = ( - path, - os.path.join(path, "dockers"), - "/etc/moon/" - "~/.moon/" - ) - for _path in PATHS: - if os.path.isfile(os.path.join(_path, templatename)): - return _path - raise Exception("Configuration error, cannot find docker template in {}".format(PATHS)) - diff --git a/moonv4/moon_utilities/moon_utilities/security_functions.py b/moonv4/moon_utilities/moon_utilities/security_functions.py index 849f5110..50ab4daf 100644 --- a/moonv4/moon_utilities/moon_utilities/security_functions.py +++ b/moonv4/moon_utilities/moon_utilities/security_functions.py @@ -19,8 +19,7 @@ from moon_utilities import configuration LOG = logging.getLogger("moon.utilities." + __name__) keystone_config = configuration.get_configuration("openstack/keystone")["openstack/keystone"] -# slave = configuration.get_configuration(configuration.SLAVE)["slave"] - +TOKENS = {} __targets = {} @@ -294,13 +293,13 @@ class Context: # context["pdp_set"][meta_rule_id]["target"][act_cat].append(assign) # # context["pdp_set"][meta_rule_id]["target"].update(result) - """build target from meta_rule - - Target is dict of categories as keys ; and the value of each category will be - a list of assignments - - """ 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"] @@ -425,7 +424,8 @@ pdp_set: {pdp_set} @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. + # Note (asteroide): if the current request is modified, + # we must update the PDP Set. self.__init_pdp_set() @current_request.deleter @@ -469,8 +469,6 @@ pdp_set: {pdp_set} def pdp_set(self): self.__pdp_set = {} -TOKENS = {} - def check_token(token, url=None): _verify = False |