Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for vault approle authentication #74

Merged
merged 1 commit into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions chaoslib/secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from chaoslib.exceptions import InvalidExperiment
from chaoslib.types import Configuration, Secrets

__all__ = ["load_secrets"]
__all__ = ["load_secrets", "create_vault_client"]


def load_secrets(secrets_info: Dict[str, Dict[str, str]],
Expand Down Expand Up @@ -141,12 +141,7 @@ def load_secrets_from_vault(secrets_info: Dict[str, Dict[str, str]],
configuration: Configuration = None) -> Secrets:
secrets = {}

url = configuration.get("vault_addr")
token = configuration.get("vault_token")

client = None
if HAS_HVAC:
client = hvac.Client(url=url, token=token)
client = create_vault_client(configuration)

for (target, keys) in secrets_info.items():
secrets[target] = {}
Expand All @@ -164,3 +159,21 @@ def load_secrets_from_vault(secrets_info: Dict[str, Dict[str, str]],
secrets.pop(target)

return secrets


def create_vault_client(configuration: Configuration = None):
client = None
if HAS_HVAC:
url = configuration.get("vault_addr")
client = hvac.Client(url=url)

if "vault_token" in configuration.keys():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be written

if "vault_token" in configuration:

client.token = configuration.get("vault_token")
elif "vault_role_id" in configuration.keys() and \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be written

if "vault_role_id" in configuration and "vault_role_secret" in configuration:

"vault_role_secret" in configuration.keys():
role_id = configuration.get("vault_role_id")
role_secret = configuration.get("vault_role_secret")

app_role = client.auth_approle(role_id, role_secret)
client.token = app_role['auth']['client_token']
return client
46 changes: 42 additions & 4 deletions tests/test_secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
import os

import pytest

from chaoslib.secret import load_secrets

from chaoslib.secret import load_secrets, create_vault_client
from fixtures import config

from unittest.mock import ANY, MagicMock, patch

def test_should_load_environment():
os.environ["KUBE_API_URL"] = "http://1.2.3.4"
Expand Down Expand Up @@ -45,3 +43,43 @@ def test_should_merge_properly():
assert secrets["kubernetes"]["address"]["host"] == "whatever"
assert secrets["kubernetes"]["address"]["port"] == 8090
assert secrets["kubernetes"]["api_server_url"] == "http://1.2.3.4"


@patch('chaoslib.secret.hvac')
def test_should_auth_with_approle(hvac):
config = {
'vault_addr' : 'http://someaddr.com',
'vault_role_id' : 'mighty_id',
'vault_role_secret' : 'secret_secret'
}

fake_auth_object = {
'auth' : {
'client_token' : 'awesome_token'
}
}

fake_client = MagicMock()
fake_client.auth_approle.return_value = fake_auth_object
hvac.Client.return_value = fake_client

vault_client = create_vault_client(config)

assert vault_client.token == fake_auth_object['auth']['client_token']
fake_client.auth_approle.assert_called_with(config['vault_role_id'], config['vault_role_secret'])


@patch('chaoslib.secret.hvac')
def test_should_auth_with_token(hvac):
config = {
'vault_addr': 'http://someaddr.com',
'vault_token': 'not_awesome_token',
}

fake_client = MagicMock()
hvac.Client.return_value = fake_client

vault_client = create_vault_client(config)

assert vault_client.token == config['vault_token']
fake_client.auth_approle.assert_not_called()