-
Notifications
You must be signed in to change notification settings - Fork 45
feat(cli): improve secrets fetch to handle disabled versions #540
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
Merged
Aaron ("AJ") Steers (aaronsteers)
merged 12 commits into
main
from
devin/1746805954-improved-secrets-fetch
May 9, 2025
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
aeae3af
feat(cli): improve secrets fetch to handle disabled versions
devin-ai-integration[bot] bb59d97
refactor: simplify exception class with kw_only=True and remove post_…
devin-ai-integration[bot] f6cba7e
refactor: create helper function for secret URL generation
devin-ai-integration[bot] 8790285
style: fix formatting issues
devin-ai-integration[bot] 2fd59c9
Update airbyte_cdk/cli/airbyte_cdk/_secrets.py
aaronsteers 5a7c2be
docs: add comment explaining version ordering in Secret Manager API
devin-ai-integration[bot] ebc9b52
Update airbyte_cdk/cli/airbyte_cdk/_secrets.py
aaronsteers 36bb330
style: update type hints to use modern lowercase syntax
devin-ai-integration[bot] ed1e60a
remove exception inheritance
aaronsteers a8cd0e7
refactor: create robust helper function for secret name extraction
devin-ai-integration[bot] 64fc46a
style: fix formatting issues in _extract_secret_name docstring
devin-ai-integration[bot] 18a112e
refactor: update exception handling to raise directly in inner function
devin-ai-integration[bot] 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Copyright (c) 2025 Airbyte, Inc., all rights reserved. | ||
| """Exceptions for the Airbyte CDK CLI.""" | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import List | ||
|
|
||
| from airbyte_cdk.sql.exceptions import AirbyteConnectorError | ||
|
|
||
|
|
||
| @dataclass(kw_only=True) | ||
| class ConnectorSecretWithNoValidVersionsError(AirbyteConnectorError): | ||
|
aaronsteers marked this conversation as resolved.
Outdated
|
||
| """Error when a connector secret has no valid versions.""" | ||
|
|
||
| connector_name: str | ||
| secret_names: List[str] | ||
| gcp_project_id: str | ||
|
|
||
| def __str__(self) -> str: | ||
| """Return a string representation of the exception.""" | ||
| from airbyte_cdk.cli.airbyte_cdk._secrets import _get_secret_url | ||
|
|
||
| urls = [ | ||
| _get_secret_url(secret_name, self.gcp_project_id) for secret_name in self.secret_names | ||
| ] | ||
| urls_str = "\n".join([f"- {url}" for url in urls]) | ||
| secrets_str = ", ".join(self.secret_names) | ||
| return ( | ||
| f"No valid versions found for the following secrets in connector '{self.connector_name}': {secrets_str}. " | ||
| f"Please check the following URLs for more information:\n{urls_str}" | ||
| ) | ||
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,190 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
| from click.testing import CliRunner | ||
|
|
||
| from airbyte_cdk.cli.airbyte_cdk._secrets import ( | ||
| _write_secret_file, | ||
| fetch, | ||
| secretmanager, | ||
| ) | ||
| from airbyte_cdk.cli.airbyte_cdk.exceptions import ConnectorSecretWithNoValidVersionsError | ||
|
|
||
|
|
||
| class TestWriteSecretFile: | ||
| @pytest.fixture | ||
| def mock_client(self): | ||
| return MagicMock() | ||
|
|
||
| @pytest.fixture | ||
| def mock_secret(self): | ||
| secret = MagicMock() | ||
| secret.name = "projects/test-project/secrets/test-secret" | ||
| return secret | ||
|
|
||
| @pytest.fixture | ||
| def mock_file_path(self, tmp_path): | ||
| return tmp_path / "test_secret.json" | ||
|
|
||
| def test_write_secret_file_with_enabled_version(self, mock_client, mock_secret, mock_file_path): | ||
| # Mock list_secret_versions to return an enabled version | ||
| mock_version = MagicMock() | ||
| mock_version.name = f"{mock_secret.name}/versions/1" | ||
| mock_client.list_secret_versions.return_value = [mock_version] | ||
|
|
||
| # Mock access_secret_version to return a payload | ||
| mock_response = MagicMock() | ||
| mock_response.payload.data.decode.return_value = '{"key": "value"}' | ||
| mock_client.access_secret_version.return_value = mock_response | ||
|
|
||
| # Call the function | ||
| result = _write_secret_file(mock_secret, mock_client, mock_file_path) | ||
|
|
||
| # Verify that list_secret_versions was called with the correct parameters | ||
| mock_client.list_secret_versions.assert_called_once() | ||
| assert "state:ENABLED" in str(mock_client.list_secret_versions.call_args) | ||
|
|
||
| # Verify that access_secret_version was called with the correct version | ||
| mock_client.access_secret_version.assert_called_once_with(name=mock_version.name) | ||
|
|
||
| # Verify that the file was created with the correct content | ||
| assert mock_file_path.read_text() == '{"key": "value"}' | ||
|
|
||
| # Verify that no error was returned | ||
| assert result is None | ||
|
|
||
| def test_write_secret_file_with_no_enabled_versions( | ||
| self, mock_client, mock_secret, mock_file_path | ||
| ): | ||
| # Mock list_secret_versions to return an empty list (no enabled versions) | ||
| mock_client.list_secret_versions.return_value = [] | ||
|
|
||
| # Call the function | ||
| result = _write_secret_file(mock_secret, mock_client, mock_file_path) | ||
|
|
||
| # Verify that list_secret_versions was called with the correct parameters | ||
| mock_client.list_secret_versions.assert_called_once() | ||
| assert "state:ENABLED" in str(mock_client.list_secret_versions.call_args) | ||
|
|
||
| # Verify that access_secret_version was not called | ||
| mock_client.access_secret_version.assert_not_called() | ||
|
|
||
| # Verify that the file was not created | ||
| assert not mock_file_path.exists() | ||
|
|
||
| # Verify that an error was returned | ||
| assert result is not None | ||
| assert "No enabled version found for secret" in result | ||
| assert "test-secret" in result | ||
|
|
||
|
|
||
| @patch("airbyte_cdk.cli.airbyte_cdk._secrets._get_gsm_secrets_client") | ||
| @patch("airbyte_cdk.cli.airbyte_cdk._secrets.resolve_connector_name_and_directory") | ||
| @patch("airbyte_cdk.cli.airbyte_cdk._secrets._get_secrets_dir") | ||
| @patch("airbyte_cdk.cli.airbyte_cdk._secrets._fetch_secret_handles") | ||
| class TestFetch: | ||
| def test_fetch_with_some_failed_secrets( | ||
| self, | ||
| mock_fetch_secret_handles, | ||
| mock_get_secrets_dir, | ||
| mock_resolve, | ||
| mock_get_client, | ||
| tmp_path, | ||
| ): | ||
| # Setup mocks | ||
| mock_client = MagicMock() | ||
| mock_get_client.return_value = mock_client | ||
|
|
||
| mock_resolve.return_value = ("test-connector", tmp_path) | ||
|
|
||
| secrets_dir = tmp_path / "secrets" | ||
| mock_get_secrets_dir.return_value = secrets_dir | ||
|
|
||
| # Create two secrets, one that will succeed and one that will fail | ||
| secret1 = MagicMock() | ||
| secret1.name = "projects/test-project/secrets/test-secret-1" | ||
| secret1.labels = {} | ||
|
|
||
| secret2 = MagicMock() | ||
| secret2.name = "projects/test-project/secrets/test-secret-2" | ||
| secret2.labels = {} | ||
|
|
||
| mock_fetch_secret_handles.return_value = [secret1, secret2] | ||
|
|
||
| # Mock _write_secret_file to succeed for secret1 and fail for secret2 | ||
| with patch( | ||
| "airbyte_cdk.cli.airbyte_cdk._secrets._write_secret_file" | ||
| ) as mock_write_secret_file: | ||
| mock_write_secret_file.side_effect = [ | ||
| None, # Success for secret1 | ||
| "No enabled version found for secret: test-secret-2", # Failure for secret2 | ||
| ] | ||
|
|
||
| # Call the function | ||
| runner = CliRunner() | ||
| result = runner.invoke(fetch) | ||
|
|
||
| # Verify that _write_secret_file was called twice | ||
| assert mock_write_secret_file.call_count == 2 | ||
|
|
||
| # Verify that the error message was printed | ||
| assert "Failed to retrieve secret 'test-secret-2'" in result.output | ||
| assert "Failed to retrieve 1 secret(s)" in result.output | ||
|
|
||
| # Verify that the function did not raise an exception | ||
| assert result.exit_code == 0 | ||
|
|
||
| def test_fetch_with_all_failed_secrets( | ||
| self, | ||
| mock_fetch_secret_handles, | ||
| mock_get_secrets_dir, | ||
| mock_resolve, | ||
| mock_get_client, | ||
| tmp_path, | ||
| ): | ||
| # Setup mocks | ||
| mock_client = MagicMock() | ||
| mock_get_client.return_value = mock_client | ||
|
|
||
| mock_resolve.return_value = ("test-connector", tmp_path) | ||
|
|
||
| secrets_dir = tmp_path / "secrets" | ||
| mock_get_secrets_dir.return_value = secrets_dir | ||
|
|
||
| # Create two secrets that will both fail | ||
| secret1 = MagicMock() | ||
| secret1.name = "projects/test-project/secrets/test-secret-1" | ||
| secret1.labels = {} | ||
|
|
||
| secret2 = MagicMock() | ||
| secret2.name = "projects/test-project/secrets/test-secret-2" | ||
| secret2.labels = {} | ||
|
|
||
| mock_fetch_secret_handles.return_value = [secret1, secret2] | ||
|
|
||
| # Mock _write_secret_file to fail for both secrets | ||
| with patch( | ||
| "airbyte_cdk.cli.airbyte_cdk._secrets._write_secret_file" | ||
| ) as mock_write_secret_file: | ||
| mock_write_secret_file.side_effect = [ | ||
| "No enabled version found for secret: test-secret-1", # Failure for secret1 | ||
| "No enabled version found for secret: test-secret-2", # Failure for secret2 | ||
| ] | ||
|
|
||
| # Call the function | ||
| runner = CliRunner() | ||
| result = runner.invoke(fetch) | ||
|
|
||
| # Verify that _write_secret_file was called twice | ||
| assert mock_write_secret_file.call_count == 2 | ||
|
|
||
| # Verify that the error message was printed | ||
| assert "Failed to retrieve secret 'test-secret-1'" in result.output | ||
| assert "Failed to retrieve secret 'test-secret-2'" in result.output | ||
| assert "Failed to retrieve 2 secret(s)" in result.output | ||
|
|
||
| # Verify that the function raised an exception | ||
| assert result.exit_code != 0 |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.