Skip to content

Commit 8a3e77c

Browse files
committed
Merge pull request #2448 from DataDog/jaime/http_proxy
[http_check] add support for no_proxy environment variable
2 parents f3edcd4 + 4f7d382 commit 8a3e77c

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

checks.d/http_check.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# stdlib
66
from datetime import datetime
77
import os.path
8+
from os import environ
89
import re
910
import socket
1011
import ssl
@@ -150,9 +151,11 @@ class HTTPCheck(NetworkCheck):
150151
def __init__(self, name, init_config, agentConfig, instances):
151152
self.ca_certs = init_config.get('ca_certs', get_ca_certs_path())
152153
proxy_settings = get_proxy(agentConfig)
153-
if not proxy_settings:
154-
self.proxies = None
155-
else:
154+
self.proxies = {
155+
"http": None,
156+
"https": None,
157+
}
158+
if proxy_settings:
156159
uri = "{host}:{port}".format(
157160
host=proxy_settings['host'],
158161
port=proxy_settings['port'])
@@ -161,10 +164,15 @@ def __init__(self, name, init_config, agentConfig, instances):
161164
user=proxy_settings['user'],
162165
password=proxy_settings['password'],
163166
uri=uri)
164-
self.proxies = {
165-
'http': "http://{uri}".format(uri=uri),
166-
'https': "https://{uri}".format(uri=uri)
167-
}
167+
self.proxies['http'] = "http://{uri}".format(uri=uri)
168+
self.proxies['https'] = "https://{uri}".format(uri=uri)
169+
else:
170+
self.proxies['http'] = environ.get('HTTP_PROXY', None)
171+
self.proxies['https'] = environ.get('HTTPS_PROXY', None)
172+
173+
self.proxies['no'] = environ.get('no_proxy',
174+
environ.get('NO_PROXY', None)
175+
)
168176

169177
NetworkCheck.__init__(self, name, init_config, agentConfig, instances)
170178

@@ -189,15 +197,16 @@ def _load_conf(self, instance):
189197
instance_ca_certs = instance.get('ca_certs', self.ca_certs)
190198
weakcipher = _is_affirmative(instance.get('weakciphers', False))
191199
ignore_ssl_warning = _is_affirmative(instance.get('ignore_ssl_warning', False))
200+
skip_proxy = _is_affirmative(instance.get('no_proxy', False))
192201

193202
return url, username, password, http_response_status_code, timeout, include_content,\
194203
headers, response_time, content_match, tags, ssl, ssl_expire, instance_ca_certs,\
195-
weakcipher, ignore_ssl_warning
204+
weakcipher, ignore_ssl_warning, skip_proxy
196205

197206
def _check(self, instance):
198207
addr, username, password, http_response_status_code, timeout, include_content, headers,\
199208
response_time, content_match, tags, disable_ssl_validation,\
200-
ssl_expire, instance_ca_certs, weakcipher, ignore_ssl_warning = self._load_conf(instance)
209+
ssl_expire, instance_ca_certs, weakcipher, ignore_ssl_warning, skip_proxy = self._load_conf(instance)
201210
start = time.time()
202211

203212
service_checks = []
@@ -208,18 +217,33 @@ def _check(self, instance):
208217
self.warning("Skipping SSL certificate validation for %s based on configuration"
209218
% addr)
210219

220+
instance_proxy = self.proxies.copy()
221+
222+
# disable proxy if necessary
223+
if skip_proxy:
224+
instance_proxy.pop('http')
225+
instance_proxy.pop('https')
226+
else:
227+
for url in self.proxies['no'].replace(';',',').split(","):
228+
if url in parsed_uri.netloc:
229+
instance_proxy.pop('http')
230+
instance_proxy.pop('https')
231+
232+
self.log.debug("Proxies used for %s - %s", addr, instance_proxy)
233+
211234
auth = None
212235
if username is not None and password is not None:
213236
auth = (username, password)
214237

215238
sess = requests.Session()
239+
sess.trust_env = False
216240
if weakcipher:
217241
base_addr = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri)
218242
sess.mount(base_addr, WeakCiphersAdapter())
219243
self.log.debug("Weak Ciphers will be used for {0}. Suppoted Cipherlist: {1}".format(
220244
base_addr, WeakCiphersHTTPSConnection.SUPPORTED_CIPHERS))
221245

222-
r = sess.request('GET', addr, auth=auth, timeout=timeout, headers=headers, proxies = self.proxies,
246+
r = sess.request('GET', addr, auth=auth, timeout=timeout, headers=headers, proxies = instance_proxy,
223247
verify=False if disable_ssl_validation else instance_ca_certs)
224248

225249
except (socket.timeout, requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:

conf.d/http_check.yaml.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ instances:
9898
#
9999
skip_event: true
100100

101+
# The (optional) no_proxy parameter would bypass any proxy settings enabled
102+
# and attempt to reach the the URL directly.
103+
# If no proxy is defined at any level, this flag bears no effect.
104+
# Defaults to False.
105+
#
106+
# no_proxy: false
107+
101108
# tags:
102109
# - url:http://alternative.host.example.com
103110
# - env:production

0 commit comments

Comments
 (0)