-
Notifications
You must be signed in to change notification settings - Fork 813
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core] Proxy, fix the way we set the no proxy environment setting
Fix #1594 Also: * simplify the proxy parsing * Move the code in a new utils module
- Loading branch information
Remi Hakim
committed
Jun 2, 2015
1 parent
b4271c3
commit 1c7d441
Showing
6 changed files
with
106 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import logging | ||
import os | ||
from urllib import getproxies | ||
from urlparse import urlparse | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
def set_no_proxy_settings(): | ||
# Starting with Agent 5.0.0, there should always be a local forwarder | ||
# running and all payloads should go through it. So we should make sure | ||
# that we pass the no_proxy environment variable that will be used by requests | ||
# See: https://github.com/kennethreitz/requests/pull/945 | ||
to_add = ["127.0.0.1", "localhost", "169.254.169.254"] | ||
no_proxy = os.environ.get("no_proxy", "") | ||
if not no_proxy.strip(): | ||
no_proxy = [] | ||
else: | ||
no_proxy = no_proxy.split(',') | ||
|
||
for host in to_add: | ||
if host not in no_proxy: | ||
no_proxy.append(host) | ||
|
||
os.environ['no_proxy'] = ','.join(no_proxy) | ||
|
||
def get_proxy(agentConfig): | ||
proxy_settings = {} | ||
|
||
# First we read the proxy configuration from datadog.conf | ||
proxy_host = agentConfig.get('proxy_host') | ||
if proxy_host is not None: | ||
proxy_settings['host'] = proxy_host | ||
try: | ||
proxy_settings['port'] = int(agentConfig.get('proxy_port', 3128)) | ||
except ValueError: | ||
log.error('Proxy port must be an Integer. Defaulting it to 3128') | ||
proxy_settings['port'] = 3128 | ||
|
||
proxy_settings['user'] = agentConfig.get('proxy_user') | ||
proxy_settings['password'] = agentConfig.get('proxy_password') | ||
log.debug("Proxy Settings: %s:*****@%s:%s", proxy_settings['user'], | ||
proxy_settings['host'], proxy_settings['port']) | ||
return proxy_settings | ||
|
||
# If no proxy configuration was specified in datadog.conf | ||
# We try to read it from the system settings | ||
try: | ||
proxy = getproxies().get('https') | ||
if proxy is not None: | ||
parse = urlparse(proxy) | ||
proxy_settings['host'] = parse.hostname | ||
proxy_settings['port'] = int(parse.port) | ||
proxy_settings['user'] = parse.username | ||
proxy_settings['password'] = parse.password | ||
|
||
log.debug("Proxy Settings: %s:*****@%s:%s", proxy_settings['user'], | ||
proxy_settings['host'], proxy_settings['port']) | ||
return proxy_settings | ||
|
||
except Exception, e: | ||
log.debug("Error while trying to fetch proxy settings using urllib %s." | ||
"Proxy is probably not set", str(e)) | ||
|
||
log.debug("No proxy configured") | ||
|
||
return None |