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

Code before:
    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):
we should condition this call to `s.post` on `if rh_user and rh_password:`

and if either are None, we'll log the error in the lines below

Code after:
    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