Use AWX_TASK_ENV when connecting to Red Hat services

This commit is contained in:
Bill Nottingham
2020-02-07 11:38:35 -05:00
parent 6395d64681
commit 71ef7cdec1
3 changed files with 23 additions and 13 deletions

View File

@@ -81,7 +81,8 @@ from awx.main.utils import (
getattrd, getattrd,
get_pk_from_dict, get_pk_from_dict,
schedule_task_manager, 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.encryption import encrypt_value
from awx.main.utils.filters import SmartFilter from awx.main.utils.filters import SmartFilter
@@ -1606,6 +1607,7 @@ class HostInsights(GenericAPIView):
def _call_insights_api(self, url, session, headers): def _call_insights_api(self, url, session, headers):
try: try:
with set_environ(**settings.AWX_TASK_ENV):
res = session.get(url, headers=headers, timeout=120) res = session.get(url, headers=headers, timeout=120)
except requests.exceptions.SSLError: except requests.exceptions.SSLError:
raise BadGateway(_('SSLError while trying to connect to {}').format(url)) raise BadGateway(_('SSLError while trying to connect to {}').format(url))

View File

@@ -38,6 +38,7 @@ from awx.main.models import (
InstanceGroup, InstanceGroup,
JobTemplate, JobTemplate,
) )
from awx.main.utils import set_environ
logger = logging.getLogger('awx.api.views.root') logger = logging.getLogger('awx.api.views.root')
@@ -191,6 +192,7 @@ class ApiV2SubscriptionView(APIView):
data['rh_password'] = settings.REDHAT_PASSWORD data['rh_password'] = settings.REDHAT_PASSWORD
try: try:
user, pw = data.get('rh_username'), data.get('rh_password') user, pw = data.get('rh_username'), data.get('rh_password')
with set_environ(**settings.AWX_TASK_ENV):
validated = get_licenser().validate_rh(user, pw) validated = get_licenser().validate_rh(user, pw)
if user: if user:
settings.REDHAT_USERNAME = data['rh_username'] settings.REDHAT_USERNAME = data['rh_username']
@@ -203,8 +205,13 @@ class ApiV2SubscriptionView(APIView):
getattr(getattr(exc, 'response', None), 'status_code', None) == 401 getattr(getattr(exc, 'response', None), 'status_code', None) == 401
): ):
msg = _("The provided credentials are invalid (HTTP 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] msg = exc.args[0]
else:
logger.exception(smart_text(u"Invalid license submitted."), logger.exception(smart_text(u"Invalid license submitted."),
extra=dict(actor=request.user.username)) extra=dict(actor=request.user.username))
return Response({"error": msg}, status=status.HTTP_400_BAD_REQUEST) return Response({"error": msg}, status=status.HTTP_400_BAD_REQUEST)

View File

@@ -15,7 +15,7 @@ from awx.conf.license import get_license
from awx.main.models import Job from awx.main.models import Job
from awx.main.access import access_registry from awx.main.access import access_registry
from awx.main.models.ha import TowerAnalyticsState 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'] __all__ = ['register', 'gather', 'ship', 'table_version']
@@ -169,6 +169,7 @@ def ship(path):
s = requests.Session() s = requests.Session()
s.headers = get_awx_http_client_headers() s.headers = get_awx_http_client_headers()
s.headers.pop('Content-Type') s.headers.pop('Content-Type')
with set_environ(**settings.AWX_TASK_ENV):
response = s.post(url, response = s.post(url,
files=files, files=files,
verify="/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", verify="/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem",