-
Notifications
You must be signed in to change notification settings - Fork 1
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 #28 from bluelabsio/secrets_manager
Enable the use of secrets-manager as a backend, like lastpass
- Loading branch information
Showing
10 changed files
with
189 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import json | ||
|
||
import boto3 | ||
|
||
from .db_facts_types import AWSSecret, AWSSecretUsernamePassword | ||
from .db_type import canonicalize_db_type, db_protocol | ||
|
||
|
||
def pull_aws_secrets_manager_secret(sm_entry_name: str) -> AWSSecret: | ||
response = boto3.client("secretsmanager").get_secret_value(SecretId=sm_entry_name) | ||
return json.loads(response["SecretString"]) | ||
|
||
|
||
def pull_aws_secrets_manager_username_password( | ||
sm_entry_name: str, | ||
) -> AWSSecretUsernamePassword: | ||
secret = pull_aws_secrets_manager_secret(sm_entry_name) | ||
return {"user": secret["username"], "password": secret["password"]} | ||
|
||
|
||
def db_info_from_secrets_manager(sm_entry_name: str): | ||
response = pull_aws_secrets_manager_secret(sm_entry_name) | ||
|
||
result = {key.lower(): value for key, value in response.items()} | ||
|
||
result["host"] = result.pop("hostname") | ||
result["user"] = result.pop("username") | ||
|
||
# mypy has issues with `result.get('type')` | ||
# https://stackoverflow.com/questions/70955906/how-to-deal-with-incompatible-type-optionalstr-expected-str | ||
if "type" in result: | ||
result["type"] = canonicalize_db_type(result["type"]) | ||
result["protocol"] = db_protocol(result["type"]) | ||
else: | ||
result["type"] = "" | ||
result["protocol"] = "" | ||
|
||
return result |
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
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 |
---|---|---|
@@ -1 +1 @@ | ||
369 | ||
383 |
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 |
---|---|---|
@@ -1 +1 @@ | ||
1 | ||
2 |
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 |
---|---|---|
|
@@ -113,7 +113,7 @@ def initialize_options(self) -> None: | |
author_email='[email protected]', | ||
packages=find_packages(), | ||
package_data={"db_facts": ["py.typed"]}, | ||
install_requires=['jinja2', 'pyyaml'], | ||
install_requires=['jinja2', 'pyyaml', 'boto3'], | ||
entry_points={ | ||
'console_scripts': [ | ||
'db_facts = db_facts.__main__:main' | ||
|
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from db_facts.db_info import db | ||
import unittest | ||
from unittest.mock import patch | ||
from .mock_dbcli_config import mock_dbcli_config | ||
|
||
|
||
@patch("db_facts.db_info.pull_jinja_context") | ||
@patch("db_facts.db_info.db_info_from_secrets_manager") | ||
@patch("db_facts.aws_secrets_manager.pull_aws_secrets_manager_secret") | ||
class TestDBInfoSecretsManager(unittest.TestCase): | ||
def test_db_info_secrets_manager( | ||
self, | ||
mock_pull_aws_secrets_manager_secret, | ||
mock_db_info_from_secrets_manager, | ||
mock_pull_jinja_context, | ||
): | ||
|
||
expected_result = { | ||
"database": "database", | ||
"host": "host", | ||
"password": "password", | ||
"port": "port", | ||
"protocol": "protocol", | ||
"type": "type", | ||
"user": "user", | ||
"connection_type": "direct", | ||
} | ||
mock_db_info_from_secrets_manager.return_value = { | ||
"database": "database", | ||
"port": "port", | ||
"host": "host", | ||
"type": "type", | ||
"user": "user", | ||
"protocol": "protocol", | ||
"password": "password", | ||
} | ||
mock_pull_jinja_context.return_value = ({}, {}) | ||
db_facts = db(["fromage"], dbcli_config=mock_dbcli_config) | ||
mock_db_info_from_secrets_manager.assert_called_with( | ||
"secrets manager entry name" | ||
) | ||
self.assertEqual(expected_result, db_facts) | ||
mock_pull_jinja_context.assert_called_with( | ||
["fromage"], mock_dbcli_config["dbs"]["fromage"], mock_dbcli_config | ||
) | ||
|
||
def test_db_info_pull_secrets_manager_user_and_pass_only( | ||
self, | ||
mock_pull_aws_secrets_manager_secret, | ||
mock_db_info_from_secrets_manager, | ||
mock_pull_jinja_context, | ||
): | ||
|
||
sm_entry = { | ||
"username": "user", | ||
"password": "password", | ||
} | ||
|
||
mock_pull_aws_secrets_manager_secret.return_value = sm_entry | ||
expected_result = { | ||
"password": "password", | ||
"user": "user", | ||
"connection_type": "direct", | ||
"some_additional": "export", | ||
"a_numbered_export": 123, | ||
} | ||
mock_pull_jinja_context.return_value = ({}, {}) | ||
db_facts = db(["fronk"], dbcli_config=mock_dbcli_config) | ||
mock_pull_aws_secrets_manager_secret.assert_called_with( | ||
"secrets manager entry name" | ||
) | ||
self.assertEqual(expected_result, db_facts) | ||
mock_pull_jinja_context.assert_called_with( | ||
["fronk"], mock_dbcli_config["dbs"]["fronk"], mock_dbcli_config | ||
) |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
from db_facts import aws_secrets_manager | ||
|
||
|
||
class TestSecretsManager(unittest.TestCase): | ||
@patch("db_facts.aws_secrets_manager.pull_aws_secrets_manager_secret") | ||
def test_db_info_from_secrets_manager(self, mock_pull_from_aws_sm): | ||
mock_pull_from_aws_sm.return_value = { | ||
"Database": "fakedatabase", | ||
"Port": 123, | ||
"Hostname": "fakehost", | ||
"Type": "faketype", | ||
"Username": "fakeuser", | ||
"Password": "fakepassword", | ||
} | ||
db_info = aws_secrets_manager.db_info_from_secrets_manager("my_secret") | ||
expected_db_info = { | ||
"database": "fakedatabase", | ||
"host": "fakehost", | ||
"password": "fakepassword", | ||
"port": 123, | ||
"type": "faketype", | ||
"user": "fakeuser", | ||
"protocol": "faketype", # if we don't know, just pass through | ||
} | ||
|
||
assert db_info == expected_db_info |