Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from signalfx/tls
Browse files Browse the repository at this point in the history
Add support for ssl_cert_validation
  • Loading branch information
Jay Camp authored May 10, 2019
2 parents 3fac6c4 + b360584 commit 7bd439b
Show file tree
Hide file tree
Showing 18 changed files with 355 additions and 331 deletions.
14 changes: 7 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
version: '2'
version: 2.1
jobs:
build:
docker:
- image: ubuntu:yakkety
- image: ubuntu:18.04
working_directory: ~/code
steps:
- setup_remote_docker
- run:
name: Install Docker client
command: |
set -x
VER="17.03.0-ce"
VER="18.09.6"
apt-get update -q
apt-get install -yq curl python-pip
curl -L -o /tmp/docker-$VER.tgz https://get.docker.com/builds/Linux/x86_64/docker-$VER.tgz
apt-get install -yq curl python-pip git
curl -L -o /tmp/docker-$VER.tgz https://download.docker.com/linux/static/stable/x86_64/docker-$VER.tgz
tar -xz -C /tmp -f /tmp/docker-$VER.tgz
mv /tmp/docker/* /usr/bin
- run:
name: Install docker-compose
command: |
set -x
curl -L https://github.com/docker/compose/releases/download/1.11.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
- checkout
- run:
Expand All @@ -32,4 +32,4 @@ jobs:
- run:
name: Run integration tests
working_directory: ~/code/integration-test
command: bash run.sh
command: ./run.sh
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ An etcd [collectd](http://www.collectd.org/) plugin which users can use to send
## Requirements

* collectd 4.9 or later (for the Python plugin)
* Python 2.6 or later
* Python 2.7 or later
* etcd 2.0.8 or later

## Configuration
Expand All @@ -33,6 +33,7 @@ Optional configurations keys include:
Specify path to keyfile and certificate if certificate based authentication of clients is enabled on your etcd server
* ssl_keyfile - path to file
* ssl_certificate - path to file
* ssl_cert_validation - if False, don't require SSL certificate validation (default True to validate certificates)

Provide a custom file that lists trusted CA certificates, required when keyfile and certificate are provided
* ssl_ca_certs - path to file
Expand Down
55 changes: 38 additions & 17 deletions etcd_plugin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/usr/bin/env python
import urllib2
import urllib_ssl_handler
import json
import collections

import collectd
import requests
import six
import urllib3

# Prevents spamming when not validating certificates.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

LEADER = "StateLeader"
FOLLOWER = "StateFollower"
Expand Down Expand Up @@ -108,7 +110,9 @@ def read_config(conf):
http_timeout = DEFAULT_API_TIMEOUT

required_keys = frozenset(('Host', 'Port'))
ssl_keys = {}
ssl_keys = {
'ssl_cert_validation': True
}
testing = False

for val in conf.children:
Expand Down Expand Up @@ -136,6 +140,11 @@ def read_config(conf):
ssl_keys['ssl_certificate'] = val.values[0]
elif val.key == 'ssl_ca_certs' and val.values[0]:
ssl_keys['ssl_ca_certs'] = val.values[0]
elif val.key == "ssl_cert_validation" and val.values[0]:
# Doesn't use str_to_bool because the function defaults to
# false and we want to default to true.
if val.values[0].strip().lower() == 'false':
ssl_keys['ssl_cert_validation'] = False
elif val.key == 'Testing' and str_to_bool(val.values[0]):
testing = True

Expand Down Expand Up @@ -436,10 +445,9 @@ def get_json(data, url):
response = make_api_call(data, url)
try:
if response:
return json.load(response)
except ValueError, e:
return response.json()
except ValueError as e:
collectd.error("ERROR: JSON parsing failed: (%s) %s" % (e, url))
return


def get_text(data, url):
Expand All @@ -448,39 +456,52 @@ def get_text(data, url):
'''
response = make_api_call(data, url)
if response:
return response.read()
return response.text


def make_api_call(data, url):
collectd.debug("GETTING THIS URL %s" % url)
collectd.debug("GETTING THIS URL %s" % url)
try:
key_file, cert_file, ca_certs = get_ssl_params(data)
opener = urllib2.build_opener(urllib_ssl_handler.HTTPSHandler(
key_file=key_file, cert_file=cert_file, ca_certs=ca_certs))
key_file, cert_file, ca_certs, cert_validation = get_ssl_params(data)

args = {
"url": url,
"verify": True,
}

if key_file and cert_file:
args["cert"] = (cert_file, key_file)

if ca_certs:
args["verify"] = ca_certs

response = opener.open(url)
return response
except (urllib2.HTTPError, urllib2.URLError), e:
if not cert_validation:
args["verify"] = False

return requests.get(**args)
except requests.RequestException as e:
collectd.error("ERROR: API call failed: (%s) %s" % (e, url))


def get_ssl_params(data):
'''
Helper method to prepare auth tuple
'''
ssl_keys = data['ssl_keys']

key_file = None
cert_file = None
ca_certs = None
cert_validation = ssl_keys['ssl_cert_validation']

ssl_keys = data['ssl_keys']
if 'ssl_certificate' in ssl_keys and 'ssl_keyfile' in ssl_keys:
key_file = ssl_keys['ssl_keyfile']
cert_file = ssl_keys['ssl_certificate']

if 'ssl_ca_certs' in ssl_keys:
ca_certs = ssl_keys['ssl_ca_certs']

return (key_file, cert_file, ca_certs)
return (key_file, cert_file, ca_certs, cert_validation)


def prepare_and_dispatch_metric(name, value, _type, dimensions):
Expand Down
74 changes: 45 additions & 29 deletions integration-test/20-etcd-test.conf
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
LoadPlugin python

<Plugin python>
ModulePath "/opt/collectd-etcd"
ModulePath "/opt/collectd-etcd"

Import etcd_plugin
<Module etcd_plugin>
Host "etcd238"
Port "2379"
Cluster 1
Dimension foo bar
Interval 10
</Module>
<Module etcd_plugin>
Host "etcd310"
Port "2379"
Cluster 1
Interval 10
</Module>
<Module etcd_plugin>
Host "etcd324"
Port "2379"
Cluster 2
Interval 10
EnhancedMetrics True
</Module>
<Module etcd_plugin>
Host "etcd208"
Port "2379"
Cluster 2
Interval 10
EnhancedMetrics True
</Module>
Import etcd_plugin

<Module etcd_plugin>
Host "etcd208"
Port "2379"
Cluster 1
Interval 10
EnhancedMetrics True
</Module>
<Module etcd_plugin>
Host "etcd238"
Port "2379"
Cluster 2
Dimension foo bar
Interval 10
</Module>
<Module etcd_plugin>
Host "etcd310"
Port "2379"
Cluster 3
Interval 10
</Module>
<Module etcd_plugin>
Host "etcd324"
Port "2379"
Cluster 4
Interval 10
EnhancedMetrics True
ssl_keyfile "/opt/testing/certs/client.key"
ssl_certificate "/opt/testing/certs/client.crt"
ssl_ca_certs "/opt/testing/certs/server.crt"
ssl_cert_validation True
</Module>
<Module etcd_plugin>
Host "etcd324-tls-unverified"
Port "2379"
Cluster 5
Interval 10
EnhancedMetrics True
ssl_keyfile "/opt/testing/certs/client.key"
ssl_certificate "/opt/testing/certs/client.crt"
ssl_ca_certs "/opt/testing/certs/server.crt"
ssl_cert_validation False
</Module>
</Plugin>
5 changes: 1 addition & 4 deletions integration-test/Dockerfile.collectd
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ ENV COLLECTD_INTERVAL=3 COLLECTD_HOSTNAME=etcd-test DISABLE_AGGREGATION=true DIS
RUN apt-get update &&\
apt-get install -yq netcat curl

CMD /.docker/setup_etcd
ADD integration-test/setup_etcd /.docker/setup_etcd

## The context of the image build should be the root dir of this repo!!
ADD etcd_plugin.py /opt/collectd-etcd/
ADD urllib_ssl_handler.py /opt/collectd-etcd/
ADD integration-test/20-etcd-test.conf /etc/collectd/managed_config/
ADD integration-test/certs/* /opt/testing/certs/
58 changes: 0 additions & 58 deletions integration-test/Dockerfile.collectd.amazon2017.03

This file was deleted.

2 changes: 2 additions & 0 deletions integration-test/Dockerfile.etcd32
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM quay.io/coreos/etcd:v3.2
COPY certs/* /opt/testing/certs/
27 changes: 27 additions & 0 deletions integration-test/certs/client.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN CERTIFICATE-----
MIIElzCCAn8CAQEwDQYJKoZIhvcNAQEFBQAwEjEQMA4GA1UEAwwHZXRjZDMyNDAg
Fw0xOTA1MDkxOTM0MzVaGA8yMjkzMDIyMDE5MzQzNVowDzENMAsGA1UEAwwEdXNl
cjCCAiIwD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