Repository: ansible/awx
File: awx/main/analytics/core.py (Python)

Code before:
        logger.error('Automation Analytics TAR not found')
        return False
    if not os.path.exists(path):
        logger.error('Automation Analytics TAR {} not found'.format(path))
        return False
    if "Error:" in str(path):
        return False

    logger.debug('shipping analytics file: {}'.format(path))
    url = getattr(settings, 'AUTOMATION_ANALYTICS_URL', None)
    if not url:
        logger.error('AUTOMATION_ANALYTICS_URL is not set')
        return False

    rh_user = getattr(settings, 'REDHAT_USERNAME', None)
    rh_password = getattr(settings, 'REDHAT_PASSWORD', None)

    with open(path, 'rb') as f:
        files = {'file': (os.path.basename(path), f, settings.INSIGHTS_AGENT_MIME)}
        s = requests.Session()
        s.headers = get_awx_http_client_headers()
        s.headers.pop('Content-Type')
        with set_environ(**settings.AWX_TASK_ENV):
            if rh_user and rh_password:
                try:
                    client = OIDCClient(rh_user, rh_password, url, ['api.console'])
                    response = client.make_request("POST", url, headers=s.headers, files=files, verify=settings.INSIGHTS_CERT_PATH, timeout=(31, 31))
                except requests.RequestException:
                    logger.error("Automation Analytics API request failed, trying base auth method")
                    response = s.post(url, files=files, verify=settings.INSIGHTS_CERT_PATH, auth=(rh_user, rh_password), headers=s.headers, timeout=(31, 31))
            elif not rh_user or not rh_password:
                logger.info('REDHAT_USERNAME and REDHAT_PASSWORD are not set, using SUBSCRIPTIONS_USERNAME and SUBSCRIPTIONS_PASSWORD')
                rh_user = getattr(settings, 'SUBSCRIPTIONS_USERNAME', None)
                rh_password = getattr(settings, 'SUBSCRIPTIONS_PASSWORD', None)
                response = s.post(url, files=files, verify=settings.INSIGHTS_CERT_PATH, auth=(rh_user, rh_password), headers=s.headers, timeout=(31, 31))

            if not rh_user:
                logger.error('REDHAT_USERNAME and SUBSCRIPTIONS_USERNAME are not set')
                return False
            if not rh_password:
                logger.error('REDHAT_PASSWORD and SUBSCRIPTIONS_USERNAME are not set')
                return False
        # Accept 2XX status_codes
        if response.status_code >= 300:
            logger.error('Upload failed with status {}, {}'.format(response.status_code, response.text))
            return False

        return True


Review comment (suggestion):
the 3rd parameter to OICClient is token_url, not the url we wish to make a request to in `make_request`. This can be hardcoded like we did here https://github.com/ansible/awx/blob/4487f2afa702e7740119ad50fc0d793abf017201/awx/playbooks/action_plugins/insights.py#L12

Code after:
        logger.error('Automation Analytics TAR not found')
        return False
    if not os.path.exists(path):
        logger.error('Automation Analytics TAR {} not found'.format(path))
        return False
    if "Error:" in str(path):
        return False

    logger.debug('shipping analytics file: {}'.format(path))
    url = getattr(settings, 'AUTOMATION_ANALYTICS_URL', None)
    if not url:
        logger.error('AUTOMATION_ANALYTICS_URL is not set')
        return False

    rh_user = getattr(settings, 'REDHAT_USERNAME', None)
    rh_password = getattr(settings, 'REDHAT_PASSWORD', None)

    with open(path, 'rb') as f:
        files = {'file': (os.path.basename(path), f, settings.INSIGHTS_AGENT_MIME)}
        s = requests.Session()
        s.headers = get_awx_http_client_headers()
        s.headers.pop('Content-Type')
        with set_environ(**settings.AWX_TASK_ENV):
            if rh_user and rh_password:
                try:
                    client = OIDCClient(rh_user, rh_password, DEFAULT_OIDC_ENDPOINT, ['api.console'])
                    response = client.make_request("POST", url, headers=s.headers, files=files, verify=settings.INSIGHTS_CERT_PATH, timeout=(31, 31))
                except requests.RequestException:
                    logger.error("Automation Analytics API request failed, trying base auth method")
                    response = s.post(url, files=files, verify=settings.INSIGHTS_CERT_PATH, auth=(rh_user, rh_password), headers=s.headers, timeout=(31, 31))
            elif not rh_user or not rh_password:
                logger.info('REDHAT_USERNAME and REDHAT_PASSWORD are not set, using SUBSCRIPTIONS_USERNAME and SUBSCRIPTIONS_PASSWORD')
                rh_user = getattr(settings, 'SUBSCRIPTIONS_USERNAME', None)
                rh_password = getattr(settings, 'SUBSCRIPTIONS_PASSWORD', None)
                if rh_user and rh_password:
                    response = s.post(url, files=files, verify=settings.INSIGHTS_CERT_PATH, auth=(rh_user, rh_password), headers=s.headers, timeout=(31, 31))
                elif not rh_user:
                    logger.error('REDHAT_USERNAME and SUBSCRIPTIONS_USERNAME are not set')
                    return False
                elif not rh_password:
                    logger.error('REDHAT_PASSWORD and SUBSCRIPTIONS_USERNAME are not set')
                    return False
        # Accept 2XX status_codes
        if response.status_code >= 300:
            logger.error('Upload failed with status {}, {}'.format(response.status_code, response.text))
            return False

        return True