Merge pull request #5295 from rooftopcellist/analytics_user_agent

Add User-Agent to analytics upload POST

Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
softwarefactory-project-zuul[bot]
2019-11-26 20:04:17 +00:00
committed by GitHub
4 changed files with 30 additions and 23 deletions

View File

@@ -72,12 +72,11 @@ from awx.api.generics import (
SubListDestroyAPIView SubListDestroyAPIView
) )
from awx.api.versioning import reverse from awx.api.versioning import reverse
from awx.conf.license import get_license
from awx.main import models from awx.main import models
from awx.main.utils import ( from awx.main.utils import (
camelcase_to_underscore, camelcase_to_underscore,
extract_ansible_vars, extract_ansible_vars,
get_awx_version, get_awx_http_client_headers,
get_object_or_400, get_object_or_400,
getattrd, getattrd,
get_pk_from_dict, get_pk_from_dict,
@@ -1643,18 +1642,6 @@ class HostInsights(GenericAPIView):
return session return session
def _get_headers(self):
license = get_license(show_key=False).get('license_type', 'UNLICENSED')
headers = {
'Content-Type': 'application/json',
'User-Agent': '{} {} ({})'.format(
'AWX' if license == 'open' else 'Red Hat Ansible Tower',
get_awx_version(),
license
)
}
return headers
def _get_platform_info(self, host, session, headers): def _get_platform_info(self, host, session, headers):
url = '{}/api/inventory/v1/hosts?insights_id={}'.format( url = '{}/api/inventory/v1/hosts?insights_id={}'.format(
@@ -1721,7 +1708,7 @@ class HostInsights(GenericAPIView):
username = cred.get_input('username', default='') username = cred.get_input('username', default='')
password = cred.get_input('password', default='') password = cred.get_input('password', default='')
session = self._get_session(username, password) session = self._get_session(username, password)
headers = self._get_headers() headers = get_awx_http_client_headers()
data = self._get_insights(host, session, headers) data = self._get_insights(host, session, headers)
return Response(data, status=status.HTTP_200_OK) return Response(data, status=status.HTTP_200_OK)

View File

@@ -1,13 +1,12 @@
# Copyright (c) 2016 Ansible, Inc. # Copyright (c) 2016 Ansible, Inc.
# All Rights Reserved. # All Rights Reserved.
# Tower
from awx.main.utils.common import get_licenser
__all__ = ['get_license'] __all__ = ['get_license']
def _get_validated_license_data(): def _get_validated_license_data():
from awx.main.utils.common import get_licenser
return get_licenser().validate() return get_licenser().validate()

View File

@@ -15,6 +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
__all__ = ['register', 'gather', 'ship', 'table_version'] __all__ = ['register', 'gather', 'ship', 'table_version']
@@ -165,11 +166,15 @@ def ship(path):
return logger.error('REDHAT_PASSWORD is not set') return logger.error('REDHAT_PASSWORD is not set')
with open(path, 'rb') as f: with open(path, 'rb') as f:
files = {'file': (os.path.basename(path), f, settings.INSIGHTS_AGENT_MIME)} files = {'file': (os.path.basename(path), f, settings.INSIGHTS_AGENT_MIME)}
response = requests.post(url, s = requests.Session()
files=files, s.headers = get_awx_http_client_headers()
verify="/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", s.headers.pop('Content-Type')
auth=(rh_user, rh_password), response = s.post(url,
timeout=(31, 31)) 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: if response.status_code != 202:
return logger.exception('Upload failed with status {}, {}'.format(response.status_code, return logger.exception('Upload failed with status {}, {}'.format(response.status_code,
response.text)) response.text))

View File

@@ -36,11 +36,14 @@ from django.utils.encoding import smart_str
from django.utils.text import slugify from django.utils.text import slugify
from django.apps import apps from django.apps import apps
# AWX
from awx.conf.license import get_license
logger = logging.getLogger('awx.main.utils') logger = logging.getLogger('awx.main.utils')
__all__ = [ __all__ = [
'get_object_or_400', 'camelcase_to_underscore', 'underscore_to_camelcase', 'memoize', 'get_object_or_400', 'camelcase_to_underscore', 'underscore_to_camelcase', 'memoize',
'memoize_delete', 'get_ansible_version', 'get_ssh_version', 'get_licenser', 'memoize_delete', 'get_ansible_version', 'get_ssh_version', 'get_licenser', 'get_awx_http_client_headers',
'get_awx_version', 'update_scm_url', 'get_type_for_model', 'get_model_for_type', 'get_awx_version', 'update_scm_url', 'get_type_for_model', 'get_model_for_type',
'copy_model_by_class', 'region_sorting', 'copy_m2m_relationships', 'copy_model_by_class', 'region_sorting', 'copy_m2m_relationships',
'prefetch_page_capabilities', 'to_python_boolean', 'ignore_inventory_computed_fields', 'prefetch_page_capabilities', 'to_python_boolean', 'ignore_inventory_computed_fields',
@@ -212,6 +215,19 @@ def get_awx_version():
return __version__ return __version__
def get_awx_http_client_headers():
license = get_license(show_key=False).get('license_type', 'UNLICENSED')
headers = {
'Content-Type': 'application/json',
'User-Agent': '{} {} ({})'.format(
'AWX' if license == 'open' else 'Red Hat Ansible Tower',
get_awx_version(),
license
)
}
return headers
class StubLicense(object): class StubLicense(object):
features = { features = {