Skip to content

Commit cb7ce59

Browse files
tuhinkantiTuhin Sharma
andauthored
fix(flagd): use custom channel credentials in rpc resolver (#453)
* fix(flagd): use custom channel credentials in rpc resolver Signed-off-by: Tuhin Sharma <tsharma@salesforce.com> * test(flagd): cover channel credential precedence and fallbacks Cover custom credentials with tls on and off in both resolver modes. Cover the cert_path, default TLS, and insecure fallbacks. Cover FlagdProvider forwarding of channel_credentials. Document a server CA example for channel_credentials. Signed-off-by: Tuhin Sharma <tsharma@salesforce.com> * test(flagd): remove duplicate channel credential tests test_channel_credentials.py covers these cases for both resolver modes. Signed-off-by: Tuhin Sharma <tsharma@salesforce.com> --------- Signed-off-by: Tuhin Sharma <tsharma@salesforce.com> Co-authored-by: Tuhin Sharma <tsharma@salesforce.com>
1 parent 1ad8f14 commit cb7ce59

4 files changed

Lines changed: 203 additions & 1 deletion

File tree

‎providers/openfeature-provider-flagd/README.md‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ The default options can be defined in the FlagdProvider constructor.
9696
| max_cache_size | FLAGD_MAX_CACHE_SIZE | int | 1000 | rpc |
9797
| retry_backoff_ms | FLAGD_RETRY_BACKOFF_MS | int | 1000 | rpc |
9898
| offline_flag_source_path | FLAGD_OFFLINE_FLAG_SOURCE_PATH | str | null | in-process |
99+
| channel_credentials | - | `grpc.ChannelCredentials` | null | rpc & in-process |
99100

100101
> [!NOTE]
101102
> The `selector` configuration is only used in **in-process** mode for filtering flag configurations. See [Selector Handling](#selector-handling-in-process-mode-only) for migration guidance.
@@ -110,6 +111,44 @@ The default options can be defined in the FlagdProvider constructor.
110111
> [!NOTE]
111112
> Some configurations are only applicable for RPC resolver.
112113
114+
### Custom channel credentials
115+
116+
Pass custom `grpc.ChannelCredentials` to `channel_credentials` to control the TLS setup. The provider uses these credentials in both resolver modes. They take precedence over `tls` and `cert_path`, and the provider creates a secure channel even when `tls` is `False`.
117+
118+
To trust a private server certificate authority (CA), supply only the root certificates:
119+
120+
```python
121+
import grpc
122+
123+
from openfeature.contrib.provider.flagd import FlagdProvider
124+
125+
with open("ca.pem", "rb") as ca_file:
126+
credentials = grpc.ssl_channel_credentials(root_certificates=ca_file.read())
127+
128+
provider = FlagdProvider(channel_credentials=credentials)
129+
```
130+
131+
When flagd requires mutual TLS (mTLS), also supply the client key and certificate:
132+
133+
```python
134+
import grpc
135+
136+
from openfeature.contrib.provider.flagd import FlagdProvider
137+
138+
with (
139+
open("ca.pem", "rb") as ca_file,
140+
open("client-key.pem", "rb") as key_file,
141+
open("client-cert.pem", "rb") as cert_file,
142+
):
143+
credentials = grpc.ssl_channel_credentials(
144+
root_certificates=ca_file.read(),
145+
private_key=key_file.read(),
146+
certificate_chain=cert_file.read(),
147+
)
148+
149+
provider = FlagdProvider(channel_credentials=credentials)
150+
```
151+
113152
### Selector Handling (In-Process Mode Only)
114153

115154
> [!IMPORTANT]

‎providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/provider.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def __init__( # noqa: PLR0913
8383
:param stream_deadline_ms: the maximum time to wait before a request times out
8484
:param keep_alive_time: the number of milliseconds to keep alive
8585
:param resolver_type: the type of resolver to use
86+
:param channel_credentials: custom gRPC channel credentials, including mTLS credentials
8687
"""
8788
if deadline_ms is None and timeout is not None:
8889
deadline_ms = timeout * 1000

‎providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/resolvers/grpc.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,14 @@ def _generate_channel(self, config: Config) -> grpc.Channel:
117117
),
118118
),
119119
]
120-
if config.tls:
120+
if config.channel_credentials is not None:
121+
channel = grpc.secure_channel(
122+
target,
123+
credentials=config.channel_credentials,
124+
options=options,
125+
)
126+
127+
elif config.tls:
121128
credentials = grpc.ssl_channel_credentials()
122129
if config.cert_path:
123130
with open(config.cert_path, "rb") as f:
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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

Comments
 (0)