|
| 1 | +from unittest.mock import Mock, mock_open, patch |
| 2 | + |
| 3 | +import grpc |
| 4 | +import pytest |
| 5 | +from grpc import Channel |
| 6 | + |
| 7 | +from openfeature.contrib.provider.flagd import FlagdProvider |
| 8 | +from openfeature.contrib.provider.flagd.config import CacheType, Config, ResolverType |
| 9 | +from openfeature.contrib.provider.flagd.resolvers.grpc import GrpcResolver |
| 10 | +from openfeature.contrib.provider.flagd.resolvers.process.connector.grpc_watcher import ( |
| 11 | + GrpcWatcher, |
| 12 | +) |
| 13 | +from openfeature.contrib.provider.flagd.resolvers.process.flags import FlagStore |
| 14 | + |
| 15 | +RPC_MODULE = "openfeature.contrib.provider.flagd.resolvers.grpc" |
| 16 | +IN_PROCESS_MODULE = ( |
| 17 | + "openfeature.contrib.provider.flagd.resolvers.process.connector.grpc_watcher" |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def _make_rpc_channel(config: Config) -> Channel: |
| 22 | + return GrpcResolver( |
| 23 | + config=config, |
| 24 | + emit_provider_ready=Mock(), |
| 25 | + emit_provider_error=Mock(), |
| 26 | + emit_provider_stale=Mock(), |
| 27 | + emit_provider_configuration_changed=Mock(), |
| 28 | + ).channel |
| 29 | + |
| 30 | + |
| 31 | +def _make_in_process_channel(config: Config) -> Channel: |
| 32 | + return GrpcWatcher( |
| 33 | + config=config, |
| 34 | + flag_store=Mock(spec=FlagStore), |
| 35 | + emit_provider_ready=Mock(), |
| 36 | + emit_provider_error=Mock(), |
| 37 | + emit_provider_stale=Mock(), |
| 38 | + ).channel |
| 39 | + |
| 40 | + |
| 41 | +CHANNEL_FACTORIES = [ |
| 42 | + pytest.param(RPC_MODULE, _make_rpc_channel, id="rpc"), |
| 43 | + pytest.param(IN_PROCESS_MODULE, _make_in_process_channel, id="in-process"), |
| 44 | +] |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture |
| 48 | +def grpc_factories(request): |
| 49 | + module = request.param |
| 50 | + with ( |
| 51 | + patch(f"{module}.grpc.secure_channel") as secure_channel, |
| 52 | + patch(f"{module}.grpc.insecure_channel") as insecure_channel, |
| 53 | + patch(f"{module}.grpc.ssl_channel_credentials") as ssl_channel_credentials, |
| 54 | + ): |
| 55 | + yield secure_channel, insecure_channel, ssl_channel_credentials |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize( |
| 59 | + ("grpc_factories", "make_channel"), CHANNEL_FACTORIES, indirect=["grpc_factories"] |
| 60 | +) |
| 61 | +@pytest.mark.parametrize("tls", [False, True]) |
| 62 | +def test_custom_credentials_take_precedence(grpc_factories, make_channel, tls): |
| 63 | + secure_channel, insecure_channel, ssl_channel_credentials = grpc_factories |
| 64 | + credentials = Mock(spec=grpc.ChannelCredentials) |
| 65 | + config = Config( |
| 66 | + cache=CacheType.DISABLED, |
| 67 | + tls=tls, |
| 68 | + cert_path="/unused/server-ca.pem", |
| 69 | + channel_credentials=credentials, |
| 70 | + ) |
| 71 | + |
| 72 | + channel = make_channel(config) |
| 73 | + |
| 74 | + assert channel is secure_channel.return_value |
| 75 | + secure_channel.assert_called_once() |
| 76 | + assert secure_channel.call_args.kwargs["credentials"] is credentials |
| 77 | + insecure_channel.assert_not_called() |
| 78 | + ssl_channel_credentials.assert_not_called() |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize( |
| 82 | + ("grpc_factories", "make_channel"), CHANNEL_FACTORIES, indirect=["grpc_factories"] |
| 83 | +) |
| 84 | +def test_tls_with_cert_path_uses_file_contents(grpc_factories, make_channel): |
| 85 | + secure_channel, insecure_channel, ssl_channel_credentials = grpc_factories |
| 86 | + config = Config(cache=CacheType.DISABLED, tls=True, cert_path="/certs/ca.pem") |
| 87 | + |
| 88 | + with patch("builtins.open", mock_open(read_data=b"server-ca")) as opened: |
| 89 | + channel = make_channel(config) |
| 90 | + |
| 91 | + assert channel is secure_channel.return_value |
| 92 | + opened.assert_called_once_with("/certs/ca.pem", "rb") |
| 93 | + ssl_channel_credentials.assert_called_with(b"server-ca") |
| 94 | + assert ( |
| 95 | + secure_channel.call_args.kwargs["credentials"] |
| 96 | + is ssl_channel_credentials.return_value |
| 97 | + ) |
| 98 | + insecure_channel.assert_not_called() |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.parametrize( |
| 102 | + ("grpc_factories", "make_channel"), CHANNEL_FACTORIES, indirect=["grpc_factories"] |
| 103 | +) |
| 104 | +def test_tls_without_cert_path_uses_default_roots(grpc_factories, make_channel): |
| 105 | + secure_channel, insecure_channel, ssl_channel_credentials = grpc_factories |
| 106 | + config = Config(cache=CacheType.DISABLED, tls=True) |
| 107 | + |
| 108 | + channel = make_channel(config) |
| 109 | + |
| 110 | + assert channel is secure_channel.return_value |
| 111 | + ssl_channel_credentials.assert_called_once_with() |
| 112 | + assert ( |
| 113 | + secure_channel.call_args.kwargs["credentials"] |
| 114 | + is ssl_channel_credentials.return_value |
| 115 | + ) |
| 116 | + insecure_channel.assert_not_called() |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.parametrize( |
| 120 | + ("grpc_factories", "make_channel"), CHANNEL_FACTORIES, indirect=["grpc_factories"] |
| 121 | +) |
| 122 | +def test_no_tls_and_no_credentials_uses_insecure_channel(grpc_factories, make_channel): |
| 123 | + secure_channel, insecure_channel, ssl_channel_credentials = grpc_factories |
| 124 | + config = Config(cache=CacheType.DISABLED, tls=False) |
| 125 | + |
| 126 | + channel = make_channel(config) |
| 127 | + |
| 128 | + assert channel is insecure_channel.return_value |
| 129 | + secure_channel.assert_not_called() |
| 130 | + ssl_channel_credentials.assert_not_called() |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.parametrize( |
| 134 | + ("resolver_type", "module"), |
| 135 | + [ |
| 136 | + pytest.param(ResolverType.RPC, RPC_MODULE, id="rpc"), |
| 137 | + pytest.param(ResolverType.IN_PROCESS, IN_PROCESS_MODULE, id="in-process"), |
| 138 | + ], |
| 139 | +) |
| 140 | +def test_provider_forwards_custom_credentials(resolver_type, module): |
| 141 | + credentials = Mock(spec=grpc.ChannelCredentials) |
| 142 | + |
| 143 | + with ( |
| 144 | + patch(f"{module}.grpc.secure_channel") as secure_channel, |
| 145 | + patch(f"{module}.grpc.insecure_channel") as insecure_channel, |
| 146 | + ): |
| 147 | + provider = FlagdProvider( |
| 148 | + resolver_type=resolver_type, |
| 149 | + tls=False, |
| 150 | + channel_credentials=credentials, |
| 151 | + ) |
| 152 | + |
| 153 | + assert provider.config.channel_credentials is credentials |
| 154 | + assert secure_channel.call_args.kwargs["credentials"] is credentials |
| 155 | + insecure_channel.assert_not_called() |
0 commit comments