From eb8520d684d2a2bc902c3a6edb2c2cc756dc22b5 Mon Sep 17 00:00:00 2001 From: Eric Kao Date: Wed, 6 Mar 2019 16:02:31 -0800 Subject: [PATCH] Support making unauthenticated APIs requests by JSON ingester Change-Id: I90ee25aea04559af720f573ff065da0c2b4b20e0 partially-implements: bp json-data-model --- .../datasources/json_ingester/exec_api.py | 24 +++++++++++++++---- .../json_ingester/json_ingester.py | 20 ++++++++++++---- lower-constraints.txt | 2 +- requirements.txt | 1 + 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/congress/datasources/json_ingester/exec_api.py b/congress/datasources/json_ingester/exec_api.py index 14df50363..a82c416ae 100644 --- a/congress/datasources/json_ingester/exec_api.py +++ b/congress/datasources/json_ingester/exec_api.py @@ -25,8 +25,10 @@ from oslo_config import cfg from oslo_log import log as logging import psycopg2 from psycopg2 import sql +import requests from congress.datasources import datasource_utils +from congress import exception LOG = logging.getLogger(__name__) @@ -45,14 +47,28 @@ class ExecApiManager(object): for config in configs: # FIXME(json_ingester): validate config if config.get('allow_exec_api', False) is True: + auth_config = config.get('authentication') + if auth_config is None: + session = requests.Session() + session.headers.update( + config.get('api_default_headers', {})) + else: + if auth_config['type'] == 'keystone': + session = datasource_utils.get_keystone_session( + config['authentication']['config'], + headers=config.get('api_default_headers', {})) + else: + LOG.error('authentication type %s not supported.', + auth_config.get['type']) + raise exception.BadConfig( + 'authentication type {} not ' + 'supported.'.auth_config['type']) + name = config['name'] self._exec_api_endpoints[name] = ( config.get('api_endpoint_host', '').rstrip('/') + '/' + config.get('api_endpoint_path', '').lstrip('/')) - self._exec_api_sessions[ - name] = datasource_utils.get_keystone_session( - config['authentication']['config'], - headers=config.get('api_default_headers', {})) + self._exec_api_sessions[name] = session @lockutils.synchronized('congress_json_ingester_exec_api') def evaluate_and_execute_actions(self): diff --git a/congress/datasources/json_ingester/json_ingester.py b/congress/datasources/json_ingester/json_ingester.py index 840d89b4f..3bd0f506b 100644 --- a/congress/datasources/json_ingester/json_ingester.py +++ b/congress/datasources/json_ingester/json_ingester.py @@ -26,6 +26,7 @@ from oslo_config import cfg from oslo_log import log as logging import psycopg2 from psycopg2 import sql +import requests from congress.api import base as api_base from congress.datasources import datasource_driver @@ -214,10 +215,21 @@ class JsonIngester(datasource_driver.PollingDataSourceDriver): self.update_methods[table_name] = method def _initialize_session(self): - if 'authentication' in self._config: - self._session = datasource_utils.get_keystone_session( - self._config['authentication']['config'], - headers=self._config.get('api_default_headers', {})) + auth_config = self._config.get('authentication') + if auth_config is None: + self._session = requests.Session() + self._session.headers.update( + self._config.get('api_default_headers', {})) + else: + if auth_config['type'] == 'keystone': + self._session = datasource_utils.get_keystone_session( + self._config['authentication']['config'], + headers=self._config.get('api_default_headers', {})) + else: + LOG.error('authentication type %s not supported.', + auth_config.get['type']) + raise exception.BadConfig('authentication type {} not ' + 'supported.'.auth_config['type']) def _initialize_update_methods(self): for table_name in self._config['tables']: diff --git a/lower-constraints.txt b/lower-constraints.txt index d6e1b8c02..06f710cf0 100644 --- a/lower-constraints.txt +++ b/lower-constraints.txt @@ -115,7 +115,7 @@ PyYAML==3.10.0 reno==2.5.0 repoze.lru==0.7 requests-mock==1.2.0 -requests==2.18.4 +requests==2.14.2 requestsexceptions==1.4.0 rfc3986==1.1.0 Routes==2.3.1 diff --git a/requirements.txt b/requirements.txt index 2848775be..c10cfe381 100644 --- a/requirements.txt +++ b/requirements.txt @@ -29,6 +29,7 @@ jsonpath-rw<2.0,>=1.2.0 # Apache-2.0 psycopg2>=2.7 # LGPL/ZPL python-dateutil>=2.5.3 # BSD python-glanceclient>=2.8.0 # Apache-2.0 +requests>=2.14.2,!=2.20.0 # Apache-2.0 Routes>=2.3.1 # MIT six>=1.10.0 # MIT tenacity>=4.4.0 # Apache-2.0