示例#1
0
    def test_config_default(self):
        instance = {}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['timeout'] == STANDARD_FIELDS['timeout']
示例#2
0
    def test_config_headers_string_values(self):
        instance = {'headers': {'answer': 42}}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['headers'] == {'answer': '42'}
    def test_config_verify_and_ca_cert(self):
        instance = {'tls_verify': True, 'tls_ca_cert': 'ca_cert'}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['verify'] == 'ca_cert'
示例#4
0
    def test_config_flag(self):
        instance = {'tls_ignore_warning': True}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.ignore_tls_warning is True
示例#5
0
    def test_default_none(self):
        instance = {}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http._session is None
示例#6
0
    def test_config_timeout(self):
        instance = {'timeout': 24.5}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['timeout'] == (24.5, 24.5)
示例#7
0
 def test_socks5_proxy(self, socks5_proxy):
     instance = {'proxy': {'http': 'socks5h://{}'.format(socks5_proxy)}}
     init_config = {}
     http = RequestsWrapper(instance, init_config)
     http.get('http://www.google.com')
     http.get('http://nginx')
示例#8
0
    def test_no_proxy_ip_fail(self, socks5_proxy):
        instance = {
            'proxy': {
                'http': 'http://1.2.3.4:567',
                'no_proxy': '127.0.0.1,127.0.0.2/32,127.1.0.0/25,127.1.1.0/255.255.255.128,127.1.2.0/0.0.0.127',
            }
        }
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        # no_proxy not match: 127.0.0.1
        with pytest.raises((ConnectTimeout, ProxyError)):
            http.get('http://127.0.0.11', timeout=1)

        # no_proxy not match: 127.0.0.2/32
        with pytest.raises((ConnectTimeout, ProxyError)):
            http.get('http://127.0.0.22', timeout=1)

        # no_proxy not match: IP outside 127.1.0.0/25 subnet - cidr bits format
        with pytest.raises((ConnectTimeout, ProxyError)):
            http.get('http://127.1.0.150', timeout=1)
            http.get('http://127.1.0.200', timeout=1)

        # no_proxy not match: IP outside 127.1.1.0/255.255.255.128 subnet - net mask format
        with pytest.raises((ConnectTimeout, ProxyError)):
            http.get('http://127.1.1.150', timeout=1)
            http.get('http://127.1.1.200', timeout=1)

        # no_proxy not match: IP outside 127.1.2.0/0.0.0.127 subnet - host mask format
        with pytest.raises((ConnectTimeout, ProxyError)):
            http.get('http://127.1.2.150', timeout=1)
            http.get('http://127.1.2.200', timeout=1)
示例#9
0
    def test_config_extra_headers_string_values(self):
        instance = {'extra_headers': {'answer': 42}}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['headers'] == {'User-Agent': 'Datadog Agent/0.0.0', 'answer': '42'}
示例#10
0
    def test_config_basic_no_legacy_encoding(self):
        instance = {'username': '******', 'password': '******', 'use_legacy_auth_encoding': False}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['auth'] == (b'user', b'pass')
示例#11
0
    def test_no_proxy_ip(self, socks5_proxy):
        instance = {
            'proxy': {
                'http': 'http://1.2.3.4:567',
                'no_proxy': '127.0.0.1,127.0.0.2/32,127.1.0.0/25,127.1.1.0/255.255.255.128,127.1.2.0/0.0.0.127',
            }
        }
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        # no_proxy match: 127.0.0.1
        http.get('http://127.0.0.1', timeout=1)

        # no_proxy match: 127.0.0.2/32
        http.get('http://127.0.0.2', timeout=1)

        # no_proxy match: IP within 127.1.0.0/25 subnet - cidr bits format
        http.get('http://127.1.0.50', timeout=1)
        http.get('http://127.1.0.100', timeout=1)

        # no_proxy match: IP within 127.1.1.0/255.255.255.128 subnet - net mask format
        http.get('http://127.1.1.50', timeout=1)
        http.get('http://127.1.1.100', timeout=1)

        # no_proxy match: IP within 127.1.2.0/0.0.0.127 subnet - host mask format
        http.get('http://127.1.2.50', timeout=1)
        http.get('http://127.1.2.100', timeout=1)
示例#12
0
class TestAuth:
    def test_config_default(self):
        instance = {}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['auth'] is None

    def test_config_basic(self):
        instance = {'username': '******', 'password': '******'}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['auth'] == ('user', 'pass')

    def test_config_basic_authtype(self):
        instance = {'username': '******', 'password': '******', 'auth_type': 'basic'}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['auth'] == ('user', 'pass')

    def test_config_basic_no_legacy_encoding(self):
        instance = {'username': '******', 'password': '******', 'use_legacy_auth_encoding': False}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['auth'] == (b'user', b'pass')

    def test_config_digest_authtype(self):
        instance = {'username': '******', 'password': '******', 'auth_type': 'digest'}
        init_config = {}
        http = RequestsWrapper(instance, init_config)
        assert isinstance(http.options['auth'], requests_auth.HTTPDigestAuth)

        with mock.patch('datadog_checks.base.utils.http.requests_auth.HTTPDigestAuth') as m:
            RequestsWrapper(instance, init_config)

            m.assert_called_once_with('user', 'pass')

    def test_config_basic_only_username(self):
        instance = {'username': '******'}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['auth'] is None

    def test_config_basic_only_password(self):
        instance = {'password': '******'}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['auth'] is None

    def test_config_kerberos_legacy(self):
        instance = {'kerberos_auth': 'required'}
        init_config = {}

        # Trigger lazy import
        http = RequestsWrapper(instance, init_config)
        assert isinstance(http.options['auth'], requests_kerberos.HTTPKerberosAuth)

        with mock.patch('datadog_checks.base.utils.http.requests_kerberos.HTTPKerberosAuth') as m:
            RequestsWrapper(instance, init_config)

            m.assert_called_once_with(
                mutual_authentication=requests_kerberos.REQUIRED,
                delegate=False,
                force_preemptive=False,
                hostname_override=None,
                principal=None,
            )

    def test_config_kerberos(self):
        instance = {'auth_type': 'kerberos', 'kerberos_auth': 'required'}
        init_config = {}

        # Trigger lazy import
        http = RequestsWrapper(instance, init_config)
        assert isinstance(http.options['auth'], requests_kerberos.HTTPKerberosAuth)

        with mock.patch('datadog_checks.base.utils.http.requests_kerberos.HTTPKerberosAuth') as m:
            RequestsWrapper(instance, init_config)

            m.assert_called_once_with(
                mutual_authentication=requests_kerberos.REQUIRED,
                delegate=False,
                force_preemptive=False,
                hostname_override=None,
                principal=None,
            )

        with mock.patch('datadog_checks.base.utils.http.requests_kerberos.HTTPKerberosAuth') as m:
            RequestsWrapper({'auth_type': 'kerberos', 'kerberos_auth': 'optional'}, init_config)

            m.assert_called_once_with(
                mutual_authentication=requests_kerberos.OPTIONAL,
                delegate=False,
                force_preemptive=False,
                hostname_override=None,
                principal=None,
            )

        with mock.patch('datadog_checks.base.utils.http.requests_kerberos.HTTPKerberosAuth') as m:
            RequestsWrapper({'auth_type': 'kerberos', 'kerberos_auth': 'disabled'}, init_config)

            m.assert_called_once_with(
                mutual_authentication=requests_kerberos.DISABLED,
                delegate=False,
                force_preemptive=False,
                hostname_override=None,
                principal=None,
            )

    def test_config_kerberos_shortcut(self):
        instance = {'auth_type': 'kerberos', 'kerberos_auth': True}
        init_config = {}

        # Trigger lazy import
        http = RequestsWrapper(instance, init_config)
        assert isinstance(http.options['auth'], requests_kerberos.HTTPKerberosAuth)

        with mock.patch('datadog_checks.base.utils.http.requests_kerberos.HTTPKerberosAuth') as m:
            RequestsWrapper(instance, init_config)

            m.assert_called_once_with(
                mutual_authentication=requests_kerberos.REQUIRED,
                delegate=False,
                force_preemptive=False,
                hostname_override=None,
                principal=None,
            )

    def test_config_kerberos_unknown(self):
        instance = {'auth_type': 'kerberos', 'kerberos_auth': 'unknown'}
        init_config = {}

        with pytest.raises(ConfigurationError):
            RequestsWrapper(instance, init_config)

    def test_config_kerberos_keytab_file(self):
        instance = {'auth_type': 'kerberos', 'kerberos_keytab': '/test/file'}
        init_config = {}

        http = RequestsWrapper(instance, init_config)

        assert os.environ.get('KRB5_CLIENT_KTNAME') is None

        with mock.patch('requests.get', side_effect=lambda *args, **kwargs: os.environ.get('KRB5_CLIENT_KTNAME')):
            assert http.get('https://www.google.com') == '/test/file'

        assert os.environ.get('KRB5_CLIENT_KTNAME') is None

    def test_config_kerberos_cache(self):
        instance = {'auth_type': 'kerberos', 'kerberos_cache': '/test/file'}
        init_config = {}

        http = RequestsWrapper(instance, init_config)

        assert os.environ.get('KRB5CCNAME') is None

        with mock.patch('requests.get', side_effect=lambda *args, **kwargs: os.environ.get('KRB5CCNAME')):
            assert http.get('https://www.google.com') == '/test/file'

        assert os.environ.get('KRB5CCNAME') is None

    def test_config_kerberos_cache_restores_rollback(self):
        instance = {'auth_type': 'kerberos', 'kerberos_cache': '/test/file'}
        init_config = {}

        http = RequestsWrapper(instance, init_config)

        with EnvVars({'KRB5CCNAME': 'old'}):
            with mock.patch('requests.get', side_effect=lambda *args, **kwargs: os.environ.get('KRB5CCNAME')):
                assert http.get('https://www.google.com') == '/test/file'

            assert os.environ.get('KRB5CCNAME') == 'old'

    def test_config_kerberos_keytab_file_rollback(self):
        instance = {'auth_type': 'kerberos', 'kerberos_keytab': '/test/file'}
        init_config = {}

        http = RequestsWrapper(instance, init_config)

        with EnvVars({'KRB5_CLIENT_KTNAME': 'old'}):
            assert os.environ.get('KRB5_CLIENT_KTNAME') == 'old'

            with mock.patch('requests.get', side_effect=lambda *args, **kwargs: os.environ.get('KRB5_CLIENT_KTNAME')):
                assert http.get('https://www.google.com') == '/test/file'

            assert os.environ.get('KRB5_CLIENT_KTNAME') == 'old'

    def test_config_kerberos_legacy_remap(self):
        instance = {'auth_type': 'kerberos', 'kerberos': True}
        init_config = {}

        # Trigger lazy import
        http = RequestsWrapper(instance, init_config)
        assert isinstance(http.options['auth'], requests_kerberos.HTTPKerberosAuth)

        with mock.patch('datadog_checks.base.utils.http.requests_kerberos.HTTPKerberosAuth') as m:
            RequestsWrapper(instance, init_config)

            m.assert_called_once_with(
                mutual_authentication=requests_kerberos.REQUIRED,
                delegate=False,
                force_preemptive=False,
                hostname_override=None,
                principal=None,
            )

    @pytest.mark.skipif(running_on_windows_ci(), reason='Test cannot be run on Windows CI')
    def test_kerberos_auth_noconf(self, kerberos):
        instance = {}
        init_config = {}
        http = RequestsWrapper(instance, init_config)
        response = http.get(kerberos["url"])

        assert response.status_code == 401

    @pytest.mark.skipif(running_on_windows_ci(), reason='Test cannot be run on Windows CI')
    def test_kerberos_auth_principal_inexistent(self, kerberos):
        instance = {
            'url': kerberos["url"],
            'auth_type': 'kerberos',
            'kerberos_auth': 'required',
            'kerberos_hostname': kerberos["hostname"],
            'kerberos_cache': "DIR:{}".format(kerberos["cache"]),
            'kerberos_keytab': kerberos["keytab"],
            'kerberos_principal': "user/doesnotexist@{}".format(kerberos["realm"]),
            'kerberos_force_initiate': 'false',
        }
        init_config = {}
        http = RequestsWrapper(instance, init_config)
        response = http.get(instance["url"])
        assert response.status_code == 401

    @pytest.mark.skipif(running_on_windows_ci(), reason='Test cannot be run on Windows CI')
    def test_kerberos_auth_principal_incache_nokeytab(self, kerberos):
        instance = {
            'url': kerberos["url"],
            'auth_type': 'kerberos',
            'kerberos_auth': 'required',
            'kerberos_cache': "DIR:{}".format(kerberos["cache"]),
            'kerberos_hostname': kerberos["hostname"],
            'kerberos_principal': "user/nokeytab@{}".format(kerberos["realm"]),
            'kerberos_force_initiate': 'true',
        }
        init_config = {}
        http = RequestsWrapper(instance, init_config)
        response = http.get(instance["url"])
        assert response.status_code == 200

    @pytest.mark.skipif(running_on_windows_ci(), reason='Test cannot be run on Windows CI')
    def test_kerberos_auth_principal_inkeytab_nocache(self, kerberos):
        instance = {
            'url': kerberos["url"],
            'auth_type': 'kerberos',
            'kerberos_auth': 'required',
            'kerberos_hostname': kerberos["hostname"],
            'kerberos_cache': "DIR:{}".format(kerberos["tmp_dir"]),
            'kerberos_keytab': kerberos["keytab"],
            'kerberos_principal': "user/inkeytab@{}".format(kerberos["realm"]),
            'kerberos_force_initiate': 'true',
        }
        init_config = {}
        http = RequestsWrapper(instance, init_config)
        response = http.get(instance["url"])
        assert response.status_code == 200

    def test_config_ntlm(self):
        instance = {'auth_type': 'ntlm', 'ntlm_domain': 'domain\\user', 'password': '******'}
        init_config = {}

        # Trigger lazy import
        http = RequestsWrapper(instance, init_config)
        assert isinstance(http.options['auth'], requests_ntlm.HttpNtlmAuth)

        with mock.patch('datadog_checks.base.utils.http.requests_ntlm.HttpNtlmAuth') as m:
            RequestsWrapper(instance, init_config)

            m.assert_called_once_with('domain\\user', 'pass')

    def test_config_ntlm_legacy(self, caplog):
        instance = {'ntlm_domain': 'domain\\user', 'password': '******'}
        init_config = {}

        # Trigger lazy import
        http = RequestsWrapper(instance, init_config)
        assert isinstance(http.options['auth'], requests_ntlm.HttpNtlmAuth)

        with mock.patch('datadog_checks.base.utils.http.requests_ntlm.HttpNtlmAuth') as m:
            RequestsWrapper(instance, init_config)

            m.assert_called_once_with('domain\\user', 'pass')

        assert (
            'The ability to use NTLM auth without explicitly setting auth_type to '
            '`ntlm` is deprecated and will be removed in Agent 8'
        ) in caplog.text

    def test_config_aws(self):
        instance = {'auth_type': 'aws', 'aws_host': 'uri', 'aws_region': 'earth', 'aws_service': 'saas'}
        init_config = {}

        # Trigger lazy import
        http = RequestsWrapper(instance, init_config)
        assert isinstance(http.options['auth'], requests_aws.BotoAWSRequestsAuth)

        with mock.patch('datadog_checks.base.utils.http.requests_aws.BotoAWSRequestsAuth') as m:
            RequestsWrapper(instance, init_config)

            m.assert_called_once_with(aws_host='uri', aws_region='earth', aws_service='saas')

    def test_config_aws_service_remapper(self):
        instance = {'auth_type': 'aws', 'aws_region': 'us-east-1'}
        init_config = {}
        remapper = {
            'aws_service': {'name': 'aws_service', 'default': 'es'},
            'aws_host': {'name': 'aws_host', 'default': 'uri'},
        }

        with mock.patch('datadog_checks.base.utils.http.requests_aws.BotoAWSRequestsAuth') as m:
            RequestsWrapper(instance, init_config, remapper)

            m.assert_called_once_with(aws_host='uri', aws_region='us-east-1', aws_service='es')

    @pytest.mark.parametrize(
        'case, instance, match',
        [
            ('no host', {'auth_type': 'aws'}, '^AWS auth requires the setting `aws_host`$'),
            ('no region', {'auth_type': 'aws', 'aws_host': 'uri'}, '^AWS auth requires the setting `aws_region`$'),
            (
                'no service',
                {'auth_type': 'aws', 'aws_host': 'uri', 'aws_region': 'us-east-1'},
                '^AWS auth requires the setting `aws_service`$',
            ),
            ('empty host', {'auth_type': 'aws', 'aws_host': ''}, '^AWS auth requires the setting `aws_host`$'),
            (
                'empty region',
                {'auth_type': 'aws', 'aws_host': 'uri', 'aws_region': ''},
                '^AWS auth requires the setting `aws_region`$',
            ),
            (
                'empty service',
                {'auth_type': 'aws', 'aws_host': 'uri', 'aws_region': 'us-east-1', 'aws_service': ''},
                '^AWS auth requires the setting `aws_service`$',
            ),
        ],
    )
    def test_config_aws_invalid_cases(self, case, instance, match):
        init_config = {}
        with pytest.raises(ConfigurationError, match=match):
            RequestsWrapper(instance, init_config)
示例#13
0
 def test_session_timeout(self):
     http = RequestsWrapper({'persist_connections': True}, {'timeout': 0.08})
     with pytest.raises(requests.exceptions.Timeout):
         http.get('https://httpbin.org/delay/0.10')
示例#14
0
    def test_patch_session(self):
        http = RequestsWrapper({'persist_connections': True}, {})

        with mock.patch('datadog_checks.base.utils.http.RequestsWrapper.session'):
            http.patch('https://www.google.com')
            http.session.patch.assert_called_once_with('https://www.google.com', **DEFAULT_OPTIONS)
示例#15
0
    def test_config_basic_only_password(self):
        instance = {'password': '******'}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['auth'] is None
示例#16
0
    def test_head(self):
        http = RequestsWrapper({}, {})

        with mock.patch('requests.head'):
            http.head('https://www.google.com')
            requests.head.assert_called_once_with('https://www.google.com', **http.options)
示例#17
0
    def test_config_kerberos_unknown(self):
        instance = {'kerberos_auth': 'unknown'}
        init_config = {}

        with pytest.raises(ConfigurationError):
            RequestsWrapper(instance, init_config)
示例#18
0
    def test_delete(self):
        http = RequestsWrapper({}, {})

        with mock.patch('requests.delete'):
            http.delete('https://www.google.com')
            requests.delete.assert_called_once_with('https://www.google.com', **http.options)
示例#19
0
    def test_config_multiple_timeouts(self):
        instance = {'read_timeout': 4, 'connect_timeout': 10}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['timeout'] == (10, 4)
示例#20
0
    def test_delete_session(self):
        http = RequestsWrapper({'persist_connections': True}, {})

        with mock.patch('datadog_checks.base.utils.http.RequestsWrapper.session'):
            http.delete('https://www.google.com')
            http.session.delete.assert_called_once_with('https://www.google.com')
示例#21
0
    def test_config_default(self):
        instance = {}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.ignore_tls_warning is False
示例#22
0
    def test_config_cert(self):
        instance = {'tls_cert': 'cert'}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['cert'] == 'cert'
示例#23
0
    def test_config_init_config_override(self):
        instance = {}
        init_config = {'timeout': 16}
        http = RequestsWrapper(instance, init_config)

        assert http.options['timeout'] == (16, 16)
示例#24
0
    def test_config_cert_and_private_key(self):
        instance = {'tls_cert': 'cert', 'tls_private_key': 'key'}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['cert'] == ('cert', 'key')
示例#25
0
    def test_config_default(self):
        instance = {}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['headers'] == {'User-Agent': 'Datadog Agent/0.0.0'}
示例#26
0
    def test_config_basic(self):
        instance = {'username': '******', 'password': '******'}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['auth'] == ('user', 'pass')
    def test_config_verify(self):
        instance = {'tls_verify': False}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['verify'] is False
示例#28
0
    def test_config_basic_only_username(self):
        instance = {'username': '******'}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['auth'] is None
    def test_config_default(self):
        instance = {}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['cert'] is None
示例#30
0
    def test_config_ca_cert(self):
        instance = {'ssl_ca_cert': 'ca_cert'}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        assert http.options['verify'] == 'ca_cert'