-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from vladimir-pachnik/master
FEATURE: Add SSL client cert auth
- Loading branch information
Showing
4 changed files
with
54 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,10 @@ | |
import logging | ||
import os | ||
|
||
from grafana_dashboards.client.connection import KerberosConnection, BearerAuthConnection, BasicAuthConnection | ||
from grafana_dashboards.client.connection import (KerberosConnection, | ||
BearerAuthConnection, | ||
BasicAuthConnection, | ||
SSLAuthConnection) | ||
from grafana_dashboards.exporter import DashboardExporter | ||
|
||
__author__ = 'Jakub Plichta <[email protected]>' | ||
|
@@ -34,11 +37,22 @@ def __init__(self, **kwargs): | |
username = os.getenv('GRAFANA_USERNAME', kwargs.get('username')) | ||
auth_token = os.getenv('GRAFANA_TOKEN', kwargs.get('token')) | ||
use_kerberos = os.getenv('GRAFANA_USE_KERBEROS', kwargs.get('use_kerberos')) | ||
client_crt = os.getenv('GRAFANA_SSL_CLIENT_CRT', kwargs.get('ssl_client_crt')) | ||
|
||
if use_kerberos: | ||
self._connection = KerberosConnection(self._host) | ||
elif auth_token: | ||
self._connection = BearerAuthConnection(auth_token, self._host) | ||
elif client_crt: | ||
client_key = os.getenv('GRAFANA_SSL_CLIENT_KEY', kwargs.get('ssl_client_key')) | ||
derived_key_path = os.path.splitext(client_crt)[0] + '.key' | ||
# pull the separate key also if not given explicitly and derived filename exists | ||
if client_key or (not client_key and os.path.exists(derived_key_path)): | ||
cert_bundle = (client_crt, client_key if client_key else derived_key_path) | ||
# otherwise assume bundled PEM | ||
else: | ||
cert_bundle = client_crt | ||
self._connection = SSLAuthConnection(self._host, cert_bundle) | ||
else: | ||
self._connection = BasicAuthConnection(username, password, self._host) | ||
|
||
|
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 |
---|---|---|
|
@@ -20,7 +20,10 @@ | |
from mock import MagicMock, patch | ||
from requests_kerberos import HTTPKerberosAuth | ||
|
||
from grafana_dashboards.client.connection import KerberosConnection, BasicAuthConnection, BearerAuthConnection | ||
from grafana_dashboards.client.connection import (KerberosConnection, | ||
BearerAuthConnection, | ||
BasicAuthConnection, | ||
SSLAuthConnection) | ||
|
||
__author__ = 'Jakub Plichta <[email protected]>' | ||
|
||
|
@@ -98,3 +101,14 @@ def test_connection_with_kerberos(post): | |
capture = Capture() | ||
post.assert_called_with('https://host/uri', auth=capture, json={"it's": 'alive'}, verify=False) | ||
assert isinstance(capture.value, HTTPKerberosAuth) | ||
|
||
|
||
@patch('requests.post') | ||
def test_connection_with_sslauth(post): | ||
connection = SSLAuthConnection('https://host', ('/fake/cert')) | ||
|
||
post().json.return_value = {'hello': 'world'} | ||
|
||
assert connection.make_request('/uri', {'it\'s': 'alive'}) == {'hello': 'world'} | ||
|
||
post.assert_called_with('https://host/uri', json={"it's": 'alive'}, cert='/fake/cert') |
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