-
Notifications
You must be signed in to change notification settings - Fork 106
Provide options allowing users to specify custom grpc SSL credentials #1946
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
+177
−8
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b4e5c13
Provide options allowing users to specify custom grpc SSL credentials
tsmith023 4590473
Simplify `GrpcConfig` object
tsmith023 cfa18f2
Update docstring of new config class with better descr and example usage
tsmith023 655bc9b
Fix tests
tsmith023 513f734
Merge branch 'main' of https://github.com/weaviate/weaviate-python-cl…
tsmith023 29a309e
Add integration test validating that custom creds don't override default
tsmith023 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,106 @@ | ||
| from typing import Any, List, Tuple | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import grpc | ||
| import pytest | ||
|
|
||
| from weaviate.config import GrpcConfig | ||
| from weaviate.connect import base as base_module | ||
| from weaviate.connect.base import ConnectionParams, ProtocolParams | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def secure_params() -> ConnectionParams: | ||
| return ConnectionParams( | ||
| http=ProtocolParams(host="localhost", port=8080, secure=False), | ||
| grpc=ProtocolParams(host="localhost", port=50051, secure=True), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def insecure_params() -> ConnectionParams: | ||
| return ConnectionParams( | ||
| http=ProtocolParams(host="localhost", port=8080, secure=False), | ||
| grpc=ProtocolParams(host="localhost", port=50051, secure=False), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_grpc(monkeypatch: pytest.MonkeyPatch) -> MagicMock: | ||
| mock = MagicMock() | ||
| mock.aio = MagicMock() | ||
| monkeypatch.setattr(base_module, "grpc", mock) | ||
| return mock | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_ssl_creds(monkeypatch: pytest.MonkeyPatch) -> MagicMock: | ||
| mock = MagicMock() | ||
| monkeypatch.setattr(base_module, "ssl_channel_credentials", mock) | ||
| return mock | ||
|
|
||
|
|
||
| def test_grpc_config_channel_options() -> None: | ||
| opts: List[Tuple[str, Any]] = [("grpc.ssl_target_name_override", "my-host")] | ||
| config = GrpcConfig(channel_options=opts) | ||
| assert config.channel_options == opts | ||
|
|
||
|
|
||
| def test_secure_channel_default_credentials( | ||
| secure_params: ConnectionParams, mock_grpc: MagicMock, mock_ssl_creds: MagicMock | ||
| ) -> None: | ||
| mock_channel = MagicMock() | ||
| mock_grpc.secure_channel.return_value = mock_channel | ||
|
|
||
| result = secure_params._grpc_channel(proxies={}, grpc_msg_size=None, is_async=False) | ||
|
|
||
| mock_ssl_creds.assert_called_once_with() | ||
| mock_grpc.secure_channel.assert_called_once() | ||
| assert result is mock_channel | ||
|
|
||
|
|
||
| def test_insecure_channel_no_config( | ||
| insecure_params: ConnectionParams, mock_grpc: MagicMock | ||
| ) -> None: | ||
| mock_channel = MagicMock() | ||
| mock_grpc.insecure_channel.return_value = mock_channel | ||
|
|
||
| result = insecure_params._grpc_channel(proxies={}, grpc_msg_size=None, is_async=False) | ||
|
|
||
| mock_grpc.insecure_channel.assert_called_once() | ||
| assert result is mock_channel | ||
|
|
||
|
|
||
| def test_channel_options_appended_secure( | ||
| secure_params: ConnectionParams, mock_grpc: MagicMock, mock_ssl_creds: MagicMock | ||
| ) -> None: | ||
| config = GrpcConfig( | ||
| channel_options=[("grpc.ssl_target_name_override", "my-gateway.example.com")] | ||
| ) | ||
| secure_params._grpc_channel(proxies={}, grpc_msg_size=None, is_async=False, grpc_config=config) | ||
|
|
||
| options = mock_grpc.secure_channel.call_args.kwargs["options"] | ||
| assert ("grpc.ssl_target_name_override", "my-gateway.example.com") in options | ||
|
|
||
|
|
||
| def test_channel_options_appended_insecure( | ||
| insecure_params: ConnectionParams, mock_grpc: MagicMock | ||
| ) -> None: | ||
| config = GrpcConfig(channel_options=[("grpc.keepalive_time_ms", 30000)]) | ||
| insecure_params._grpc_channel( | ||
| proxies={}, grpc_msg_size=None, is_async=False, grpc_config=config | ||
| ) | ||
|
|
||
| options = mock_grpc.insecure_channel.call_args.kwargs["options"] | ||
| assert ("grpc.keepalive_time_ms", 30000) in options | ||
|
|
||
|
|
||
| def test_credentials( | ||
| secure_params: ConnectionParams, mock_grpc: MagicMock, mock_ssl_creds: MagicMock | ||
| ) -> None: | ||
| creds = grpc.ssl_channel_credentials() | ||
| config = GrpcConfig(credentials=creds) | ||
| secure_params._grpc_channel(proxies={}, grpc_msg_size=None, is_async=False, grpc_config=config) | ||
|
|
||
| mock_ssl_creds.assert_not_called() | ||
| assert mock_grpc.secure_channel.call_args.kwargs["credentials"] is creds |
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| from weaviate.auth import Auth | ||
| from weaviate.config import AdditionalConfig, Proxies, Timeout | ||
| from weaviate.config import AdditionalConfig, GrpcConfig, Proxies, Timeout | ||
|
|
||
| __all__ = ["Auth", "AdditionalConfig", "Proxies", "Timeout"] | ||
| __all__ = ["Auth", "AdditionalConfig", "GrpcConfig", "Proxies", "Timeout"] |
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
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
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.
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.
could we add a footgun-check that makes sure that ssl is not deactivated when this is checked?
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.
Have added a test for this in
integration/test_auth.py