Skip to content

Commit

Permalink
Merge pull request #9 from rickymoorhouse/mtls
Browse files Browse the repository at this point in the history
Adding mTLS support for tests
  • Loading branch information
rickymoorhouse authored Jun 22, 2018
2 parents 8f3e429 + 8be3d8e commit ae4ec16
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The config.yaml is made up of sections:
path: Path to test
secure: Is this using HTTPS?
hosts: List of hosts to use
certificate: Path to keypair to use for mTLS - must be un-encrypted
discovery: discovery block for this test - merged with top level block
```

Expand Down
10 changes: 8 additions & 2 deletions hemApp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Check(object):
headers = {}
timeout = 10
metrics = None
certificate = None

def __init__(self, name, test, metrics=None):
#path, secure=False, verify=True, metrics=None):
Expand All @@ -88,7 +89,10 @@ def __init__(self, name, test, metrics=None):
self.method = test.get('method', "get")
if 'headers' in test:
self.headers = test['headers']

if 'certificate' in test:
self.logger.info("Setting certificate to %s", test['certificate'])
self.certificate = test['certificate']

self.metrics = metrics

def test(self, param, results):
Expand All @@ -105,7 +109,9 @@ def test(self, param, results):
self.url.format(param),
headers=self.headers,
timeout=self.timeout,
verify=self.verify)
verify=self.verify,
cert=self.certificate
)
self.logger.debug("Response text: %s", result.text)
elapsed_time = result.elapsed
result.raise_for_status()
Expand Down
1 change: 1 addition & 0 deletions test_requires.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pytest
pytest-cov
pytest-mock
requests-mock
coverage==4.0.3
python-coveralls
pykafka
prometheus_client
10 changes: 10 additions & 0 deletions tests/test_hem.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def test_check_invoke():
assert results is not None
assert response == 200
assert type(timing) is datetime.timedelta
def test_check_mtls_invoke():
with requests_mock.mock() as m:
m.get('https://1.1.1.1/', text="")
test = {'path':'/', 'secure':True, 'verify':True, 'certificate': 'certificate.pem'}
check = hemApp.Check('test', test)
results = check.test_list(["1.1.1.1"])
(response, timing) = results[0]
assert results is not None
assert response == 200
assert type(timing) is datetime.timedelta
def test_ssl_error():
with requests_mock.mock() as m:
m.get('https://1.1.1.1/', exc=requests.exceptions.SSLError)
Expand Down

0 comments on commit ae4ec16

Please sign in to comment.