-
Notifications
You must be signed in to change notification settings - Fork 40
exporters: support reading keys from env vars (fixes #97) #134
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
Open
Kunal-Darekar
wants to merge
3
commits into
m-lab:main
Choose a base branch
from
Kunal-Darekar:fix/issue-97-env-var-exporter-keys
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,50 @@ | ||
| import base64 | ||
| import json | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from murakami.exporters.gcs import GCSExporter | ||
|
|
||
| FAKE_KEY_DICT = { | ||
| "type": "service_account", | ||
| "project_id": "test-project", | ||
| "private_key_id": "key-id", | ||
| "private_key": "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA\n-----END RSA PRIVATE KEY-----\n", | ||
| "client_email": "test@test-project.iam.gserviceaccount.com", | ||
| "client_id": "123456789", | ||
| "auth_uri": "https://accounts.google.com/o/oauth2/auth", | ||
| "token_uri": "https://oauth2.googleapis.com/token", | ||
| } | ||
|
|
||
| FAKE_KEY_CONTENT = base64.b64encode( | ||
| json.dumps(FAKE_KEY_DICT).encode("utf-8") | ||
| ).decode("utf-8") | ||
|
|
||
|
|
||
| @patch("murakami.exporters.gcs.storage.Client") | ||
| def test_gcs_key_content_uses_from_service_account_info(mock_client_cls): | ||
| """When key_content is in the config, credentials are loaded from inline | ||
| base64-encoded JSON rather than a file on disk.""" | ||
| mock_client_cls.from_service_account_info.return_value = MagicMock() | ||
|
|
||
| GCSExporter( | ||
| name="test", | ||
| config={"target": "gs://bucket/path", "key_content": FAKE_KEY_CONTENT}, | ||
| ) | ||
|
|
||
| mock_client_cls.from_service_account_info.assert_called_once_with(FAKE_KEY_DICT) | ||
| mock_client_cls.from_service_account_json.assert_not_called() | ||
|
|
||
|
|
||
| @patch("murakami.exporters.gcs.storage.Client") | ||
| def test_gcs_key_file_used_when_no_key_content(mock_client_cls): | ||
| """When key_content is absent, the exporter falls back to reading | ||
| credentials from the file path given by the key config option.""" | ||
| mock_client_cls.from_service_account_json.return_value = MagicMock() | ||
|
|
||
| GCSExporter( | ||
| name="test", | ||
| config={"target": "gs://bucket/path", "key": "/path/to/key.json"}, | ||
| ) | ||
|
|
||
| mock_client_cls.from_service_account_json.assert_called_once_with("/path/to/key.json") | ||
| mock_client_cls.from_service_account_info.assert_not_called() |
This file contains hidden or 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,67 @@ | ||
| import base64 | ||
| import os | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from murakami.exporters.scp import SCPExporter | ||
|
|
||
| FAKE_PEM = b"-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA\n-----END RSA PRIVATE KEY-----\n" | ||
| FAKE_KEY_CONTENT = base64.b64encode(FAKE_PEM).decode("utf-8") | ||
|
|
||
|
|
||
| @patch("murakami.exporters.scp.SCPClient") | ||
| @patch("murakami.exporters.scp.SSHClient") | ||
| def test_scp_key_content_writes_temp_file(mock_ssh_cls, mock_scp_cls): | ||
| """When key_content is in the config, the exporter decodes the base64 PEM, | ||
| writes it to a temporary file (mode 0o600), passes that path to | ||
| ssh.connect(), and deletes the file when done.""" | ||
| mock_ssh = MagicMock() | ||
| mock_ssh_cls.return_value = mock_ssh | ||
| mock_ssh.get_transport.return_value = MagicMock() | ||
|
|
||
| mock_scp_instance = MagicMock() | ||
| mock_scp_cls.return_value.__enter__ = MagicMock(return_value=mock_scp_instance) | ||
| mock_scp_cls.return_value.__exit__ = MagicMock(return_value=False) | ||
|
|
||
| exporter = SCPExporter( | ||
| name="test", | ||
| config={ | ||
| "target": "host:/remote/path", | ||
| "username": "user", | ||
| "key_content": FAKE_KEY_CONTENT, | ||
| }, | ||
| ) | ||
| exporter._push_single(test_name="ndt7", data='{"result": 1}', | ||
| timestamp="2024-01-01T00:00:00.000000") | ||
|
|
||
| connect_kwargs = mock_ssh.connect.call_args | ||
| key_filename = connect_kwargs[1]["key_filename"] | ||
| assert key_filename is not None, "key_filename should be set when key_content is provided" | ||
| assert not os.path.exists(key_filename), "temp file must be deleted after push" | ||
|
|
||
|
|
||
| @patch("murakami.exporters.scp.SCPClient") | ||
| @patch("murakami.exporters.scp.SSHClient") | ||
| def test_scp_key_file_used_when_no_key_content(mock_ssh_cls, mock_scp_cls): | ||
| """When key_content is absent, the exporter uses the file path from the | ||
| key config option directly as key_filename.""" | ||
| mock_ssh = MagicMock() | ||
| mock_ssh_cls.return_value = mock_ssh | ||
| mock_ssh.get_transport.return_value = MagicMock() | ||
|
|
||
| mock_scp_instance = MagicMock() | ||
| mock_scp_cls.return_value.__enter__ = MagicMock(return_value=mock_scp_instance) | ||
| mock_scp_cls.return_value.__exit__ = MagicMock(return_value=False) | ||
|
|
||
| exporter = SCPExporter( | ||
| name="test", | ||
| config={ | ||
| "target": "host:/remote/path", | ||
| "username": "user", | ||
| "key": "/path/to/id_rsa", | ||
| }, | ||
| ) | ||
| exporter._push_single(test_name="ndt7", data='{"result": 1}', | ||
| timestamp="2024-01-01T00:00:00.000000") | ||
|
|
||
| connect_kwargs = mock_ssh.connect.call_args | ||
| assert connect_kwargs[1]["key_filename"] == "/path/to/id_rsa" |
This file contains hidden or 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 @@ | ||
| #!/bin/sh | ||
| # Encode a key file for use with Murakami's GCS or SCP exporter. | ||
| # | ||
| # Usage: | ||
| # encode_key.sh /path/to/sa.json (GCS service account JSON) | ||
| # encode_key.sh /path/to/id_rsa (SCP PEM private key) | ||
| # | ||
| # The encoded value is read by Murakami's existing environment variable | ||
| # config mechanism (load_env in __main__.py). Set it as: | ||
| # | ||
| # GCS exporter (in murakami.toml or as env var): | ||
| # [exporters.gcs] | ||
| # key_content = "<encoded value>" | ||
| # Or via environment variable: | ||
| # export MURAKAMI_EXPORTERS_GCS_KEY_CONTENT="<encoded value>" | ||
| # | ||
| # SCP exporter: | ||
| # [exporters.scp] | ||
| # key_content = "<encoded value>" | ||
| # Or via environment variable: | ||
| # export MURAKAMI_EXPORTERS_SCP_KEY_CONTENT="<encoded value>" | ||
|
|
||
| if [ -z "$1" ]; then | ||
| echo "Usage: $0 /path/to/keyfile" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| base64 -w 0 "$1" | ||
| echo "" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please explicitly set the permissions to something like
0o600