From 71ef7cdec199f1b2260400a5de7bdd36239370e7 Mon Sep 17 00:00:00 2001 From: Bill Nottingham Date: Fri, 7 Feb 2020 11:38:35 -0500 Subject: [PATCH] Use AWX_TASK_ENV when connecting to Red Hat services --- awx/api/views/__init__.py | 6 ++++-- awx/api/views/root.py | 15 +++++++++++---- awx/main/analytics/core.py | 15 ++++++++------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/awx/api/views/__init__.py b/awx/api/views/__init__.py index 241c7c6d5c..6663f05b1d 100644 --- a/awx/api/views/__init__.py +++ b/awx/api/views/__init__.py @@ -81,7 +81,8 @@ from awx.main.utils import ( getattrd, get_pk_from_dict, schedule_task_manager, - ignore_inventory_computed_fields + ignore_inventory_computed_fields, + set_environ ) from awx.main.utils.encryption import encrypt_value from awx.main.utils.filters import SmartFilter @@ -1606,7 +1607,8 @@ class HostInsights(GenericAPIView): def _call_insights_api(self, url, session, headers): try: - res = session.get(url, headers=headers, timeout=120) + with set_environ(**settings.AWX_TASK_ENV): + res = session.get(url, headers=headers, timeout=120) except requests.exceptions.SSLError: raise BadGateway(_('SSLError while trying to connect to {}').format(url)) except requests.exceptions.Timeout: diff --git a/awx/api/views/root.py b/awx/api/views/root.py index f4d68ace9c..4a15936e9b 100644 --- a/awx/api/views/root.py +++ b/awx/api/views/root.py @@ -38,6 +38,7 @@ from awx.main.models import ( InstanceGroup, JobTemplate, ) +from awx.main.utils import set_environ logger = logging.getLogger('awx.api.views.root') @@ -191,7 +192,8 @@ class ApiV2SubscriptionView(APIView): data['rh_password'] = settings.REDHAT_PASSWORD try: user, pw = data.get('rh_username'), data.get('rh_password') - validated = get_licenser().validate_rh(user, pw) + with set_environ(**settings.AWX_TASK_ENV): + validated = get_licenser().validate_rh(user, pw) if user: settings.REDHAT_USERNAME = data['rh_username'] if pw: @@ -203,10 +205,15 @@ class ApiV2SubscriptionView(APIView): getattr(getattr(exc, 'response', None), 'status_code', None) == 401 ): msg = _("The provided credentials are invalid (HTTP 401).") - if isinstance(exc, (ValueError, OSError)) and exc.args: + elif isinstance(exc, requests.exceptions.ProxyError): + msg = _("Unable to connect to proxy server.") + elif isinstance(exc, requests.exceptions.ConnectionError): + msg = _("Could not connect to subscription service.") + elif isinstance(exc, (ValueError, OSError)) and exc.args: msg = exc.args[0] - logger.exception(smart_text(u"Invalid license submitted."), - extra=dict(actor=request.user.username)) + else: + logger.exception(smart_text(u"Invalid license submitted."), + extra=dict(actor=request.user.username)) return Response({"error": msg}, status=status.HTTP_400_BAD_REQUEST) return Response(validated) diff --git a/awx/main/analytics/core.py b/awx/main/analytics/core.py index a08cc84b8b..143465ae10 100644 --- a/awx/main/analytics/core.py +++ b/awx/main/analytics/core.py @@ -15,7 +15,7 @@ from awx.conf.license import get_license from awx.main.models import Job from awx.main.access import access_registry from awx.main.models.ha import TowerAnalyticsState -from awx.main.utils import get_awx_http_client_headers +from awx.main.utils import get_awx_http_client_headers, set_environ __all__ = ['register', 'gather', 'ship', 'table_version'] @@ -169,12 +169,13 @@ def ship(path): s = requests.Session() s.headers = get_awx_http_client_headers() s.headers.pop('Content-Type') - response = s.post(url, - files=files, - verify="/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", - auth=(rh_user, rh_password), - headers=s.headers, - timeout=(31, 31)) + with set_environ(**settings.AWX_TASK_ENV): + response = s.post(url, + files=files, + verify="/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", + auth=(rh_user, rh_password), + headers=s.headers, + timeout=(31, 31)) if response.status_code != 202: return logger.exception('Upload failed with status {}, {}'.format(response.status_code, response.text))