Repository: DataDog/dd-agent

Issue:
SSL Validation warning is being applied to instances that are not SSL
A config:

``` yaml
instances:
  - name: Datadog.com
    url: http://www.datadog.com
    timeout: 1
    collect_response_time: true
    skip_event: true
    headers:
      User-Agent: DatadogAgent 5.x

  - name: Datadoghq.com
    url: https://www.datadoghq.com/
    timeout: 1
    collect_response_time: true
    skip_event: true
    headers:
      User-Agent: DatadogAgent 5.x
```

Note that only one of these has SSL in the URL.

`info` command will show warnings, and change which Instance they apply to randomly.

``` console
    http_check
    ----------
      - instance #0 [OK]
      - instance #1 [WARNING]
          Warning: Skipping SSL certificate validation for http://www.datadog.com based on configuration
```



Discussion:

Resolving patch:
diff --git a/checks.d/http_check.py b/checks.d/http_check.py
--- a/checks.d/http_check.py
+++ b/checks.d/http_check.py
@@ -1,10 +1,11 @@
 # stdlib
 import socket
 import time
+from urlparse import urlparse
 
 # project
 from checks.network_checks import NetworkCheck, Status, EventType
-from util import headers
+from util import headers as agent_headers
 
 # 3rd party
 from httplib2 import Http, HttpLib2Error
@@ -20,7 +21,9 @@ def _load_conf(self, instance):
         username = instance.get('username', None)
         password = instance.get('password', None)
         timeout = int(instance.get('timeout', 10))
-        headers = instance.get('headers',{})
+        config_headers = instance.get('headers',{})
+        headers = agent_headers(self.agentConfig)
+        headers.update(config_headers)
         url = instance.get('url', None)
         response_time = instance.get('collect_response_time', True)
         if url is None:
@@ -35,7 +38,7 @@ def _check(self, instance):
         start = time.time()
         try:
             self.log.debug("Connecting to %s" % addr)
-            if disable_ssl_validation:
+            if disable_ssl_validation and urlparse(addr)[0] == "https":
                 self.warning("Skipping SSL certificate validation for %s based on configuration" % addr)
             h = Http(timeout=timeout, disable_ssl_certificate_validation=disable_ssl_validation)
             if username is not None and password is not None:


Test patch:
diff --git a/tests/test_service_checks.py b/tests/test_service_checks.py
--- a/tests/test_service_checks.py
+++ b/tests/test_service_checks.py
@@ -4,6 +4,8 @@
 from tests.common import load_check
 import logging
 import nose.tools as nt
+from config import AGENT_VERSION
+from util import headers as agent_headers
 
 class ServiceCheckTestCase(unittest.TestCase):
 
@@ -12,13 +14,57 @@ def setUp(self):
 
     def init_check(self, config, check_name):
         self.agentConfig = {
-            'version': '0.1',
+            'version': AGENT_VERSION,
             'api_key': 'toto'
         }
 
         self.check = load_check(check_name, config, self.agentConfig)
         self.checks.append(self.check)
 
+    def testHTTPHeaders(self):
+        config = {
+            'init_config': {},
+            'instances': [{
+                'url': 'https://google.com',
+                'name': 'UpService',
+                'timeout': 1,
+                'headers': { "X-Auth-Token": "SOME-AUTH-TOKEN"}
+            }]
+        }
+
+        self.init_check(config, 'http_check')
+        url, username, password, timeout, include_content, headers, response_time, tags, ssl = self.check._load_conf(config['instances'][0])
+
+        self.assertTrue(headers["X-Auth-Token"] == "SOME-AUTH-TOKEN", headers)
+        self.assertTrue(headers.get('User-Agent') == agent_headers(self.agentConfig).get('User-Agent'), headers)
+
+
+
+    def testHTTPWarning(self):
+        config = {
+            'init_config': {},
+            'instances': [{
+                'url': 'http://127.0.0.1:55555',
+                'name': 'DownService',
+                'timeout': 1
+            },{
+                'url': 'https://google.com',
+                'name': 'UpService',
+                'timeout': 1
+            }]
+        }
+        self.init_check(config, 'http_check')
+
+        self.check.run()
+        time.sleep(1)
+        # This would normally be called during the next run(), it is what
+        # flushes the results of the check
+        self.check._process_results()
+        warnings = self.check.get_warnings()
+
+        self.assertTrue(len(warnings) == 3, warnings)
+        self.assertTrue(len([k for k in warnings if "Skipping SSL certificate validation" in k])==1, warnings)
+
     def testHTTP(self):
         # No passwords this time
         config = {