Repository: DataDog/dd-agent

Issue:
API key validation check in info page does not use proxy settings
(This is an issue as of 5.10)

This commit: https://github.com/DataDog/dd-agent/commit/4ecd5ef0b5a8f97f8c5ff771ef31bddbd59060f1 does not use the config's proxy settings, so when accessing the info page, if you cannot connect except via proxy, you'll get an error like this:

```
2016-11-09 20:24:37,578 | ERROR | dd.forwarder | checks.check_status(check_status.py:130) | Unable to validate API Key
Traceback (most recent call last):
  File "/opt/datadog-agent/agent/checks/check_status.py", line 122, in validate_api_key
    params={'api_key': api_key}, timeout=3)
  File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/requests/api.py", line 68, in get
    return request('get', url, **kwargs)
  File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/requests/api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/requests/sessions.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/requests/sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/requests/adapters.py", line 415, in send
    raise ConnectionError(err, request=request)
ConnectionError: ('Connection aborted.', error(99, 'Cannot assign requested address'))
```

**Steps to reproduce the issue:**

Using a pretty basic amazon linux EC2 instance, you can test this with the following setup:

1. Set up a proxy e.g. squid on your host
2. Configure dd-agent with a `proxy_host` and `proxy_port`
3. Configure your firewall to allow squid to send egress messages on port 443 but not dd-agent
4. `/etc/init.d/datadog-agent info`


Discussion:

Resolving patch:
diff --git a/checks/check_status.py b/checks/check_status.py
--- a/checks/check_status.py
+++ b/checks/check_status.py
@@ -31,6 +31,7 @@
 from utils.pidfile import PidFile
 from utils.platform import Platform
 from utils.profile import pretty_statistics
+from utils.proxy import get_proxy
 
 
 STATUS_OK = 'OK'
@@ -116,10 +117,14 @@ def get_ntp_info():
         ntp_styles = []
     return ntp_offset, ntp_styles
 
-def validate_api_key(api_key):
+def validate_api_key(config):
     try:
+        proxy = get_proxy(agentConfig=config)
+        request_proxy = {}
+        if proxy:
+            request_proxy = {'https': "http://{user}:{password}@{host}:{port}".format(**proxy)}
         r = requests.get("https://app.datadoghq.com/api/v1/validate",
-            params={'api_key': api_key}, timeout=3)
+            params={'api_key': config.get('api_key')}, proxies=request_proxy, timeout=3)
 
         if r.status_code == 403:
             return "API Key is invalid"
@@ -798,7 +803,7 @@ def body_lines(self):
             "Transactions received: %s" % self.transactions_received,
             "Transactions flushed: %s" % self.transactions_flushed,
             "Transactions rejected: %s" % self.transactions_rejected,
-            "API Key Status: %s" % validate_api_key(get_config().get('api_key')),
+            "API Key Status: %s" % validate_api_key(config=get_config()),
             "",
         ]
 


Test patch: