Add insecure option

pull/23153/head
Richard Belleville 5 years ago
parent 9f380bc16c
commit e3ae621f64
  1. 1
      src/compiler/python_generator.cc
  2. 46
      src/python/grpcio/grpc/_simple_stubs.py
  3. 20
      src/python/grpcio_tests/tests_py3_only/unit/_simple_stubs_test.py

@ -627,6 +627,7 @@ bool PrivateGenerator::PrintServiceClass(
out->Print("options=(),\n");
out->Print("channel_credentials=None,\n");
out->Print("call_credentials=None,\n");
out->Print("insecure=False,\n");
out->Print("compression=None,\n");
out->Print("wait_for_ready=None,\n");
out->Print("timeout=None,\n");

@ -53,13 +53,6 @@ else:
def _create_channel(target: str, options: Sequence[Tuple[str, str]],
channel_credentials: Optional[grpc.ChannelCredentials],
compression: Optional[grpc.Compression]) -> grpc.Channel:
if channel_credentials is None:
_LOGGER.debug("Defaulting to SSL channel credentials.")
credentials = grpc.ssl_channel_credentials()
return grpc.secure_channel(target,
credentials=credentials,
options=options,
compression=compression)
if channel_credentials._credentials is grpc.experimental._insecure_channel_credentials:
_LOGGER.debug(f"Creating insecure channel with options '{options}' " +
f"and compression '{compression}'")
@ -136,7 +129,18 @@ class ChannelCache:
def get_channel(self, target: str, options: Sequence[Tuple[str, str]],
channel_credentials: Optional[grpc.ChannelCredentials],
insecure: bool,
compression: Optional[grpc.Compression]) -> grpc.Channel:
if insecure and channel_credentials:
raise ValueError("The insecure option is mutually exclusive with " +
"the channel_credentials option. Please use one " +
"or the other.")
if insecure:
channel_credentials = grpc.experimental.insecure_channel_credentials(
)
elif channel_credentials is None:
_LOGGER.debug("Defaulting to SSL channel credentials.")
channel_credentials = grpc.ssl_channel_credentials()
key = (target, options, channel_credentials, compression)
with self._lock:
channel_data = self._mapping.get(key, None)
@ -170,6 +174,7 @@ def unary_unary(
response_deserializer: Optional[Callable[[bytes], Any]] = None,
options: Sequence[Tuple[AnyStr, AnyStr]] = (),
channel_credentials: Optional[grpc.ChannelCredentials] = None,
insecure: bool = False,
call_credentials: Optional[grpc.CallCredentials] = None,
compression: Optional[grpc.Compression] = None,
wait_for_ready: Optional[bool] = None,
@ -204,6 +209,9 @@ def unary_unary(
channel_credentials: A credential applied to the whole channel, e.g. the
return value of grpc.ssl_channel_credentials() or
grpc.insecure_channel_credentials().
insecure: If True, specifies channel_credentials as
:term:`grpc.insecure_channel_credentials()`. This option is mutually
exclusive with the `channel_credentials` option.
call_credentials: A call credential applied to each call individually,
e.g. the output of grpc.metadata_call_credentials() or
grpc.access_token_call_credentials().
@ -222,7 +230,8 @@ def unary_unary(
The response to the RPC.
"""
channel = ChannelCache.get().get_channel(target, options,
channel_credentials, compression)
channel_credentials, insecure,
compression)
multicallable = channel.unary_unary(method, request_serializer,
response_deserializer)
return multicallable(request,
@ -241,6 +250,7 @@ def unary_stream(
response_deserializer: Optional[Callable[[bytes], Any]] = None,
options: Sequence[Tuple[AnyStr, AnyStr]] = (),
channel_credentials: Optional[grpc.ChannelCredentials] = None,
insecure: bool = False,
call_credentials: Optional[grpc.CallCredentials] = None,
compression: Optional[grpc.Compression] = None,
wait_for_ready: Optional[bool] = None,
@ -274,6 +284,9 @@ def unary_stream(
runtime) to configure the channel.
channel_credentials: A credential applied to the whole channel, e.g. the
return value of grpc.ssl_channel_credentials().
insecure: If True, specifies channel_credentials as
:term:`grpc.insecure_channel_credentials()`. This option is mutually
exclusive with the `channel_credentials` option.
call_credentials: A call credential applied to each call individually,
e.g. the output of grpc.metadata_call_credentials() or
grpc.access_token_call_credentials().
@ -292,7 +305,8 @@ def unary_stream(
An iterator of responses.
"""
channel = ChannelCache.get().get_channel(target, options,
channel_credentials, compression)
channel_credentials, insecure,
compression)
multicallable = channel.unary_stream(method, request_serializer,
response_deserializer)
return multicallable(request,
@ -312,6 +326,7 @@ def stream_unary(
options: Sequence[Tuple[AnyStr, AnyStr]] = (),
channel_credentials: Optional[grpc.ChannelCredentials] = None,
call_credentials: Optional[grpc.CallCredentials] = None,
insecure: bool = False,
compression: Optional[grpc.Compression] = None,
wait_for_ready: Optional[bool] = None,
timeout: Optional[float] = None,
@ -347,6 +362,9 @@ def stream_unary(
call_credentials: A call credential applied to each call individually,
e.g. the output of grpc.metadata_call_credentials() or
grpc.access_token_call_credentials().
insecure: If True, specifies channel_credentials as
:term:`grpc.insecure_channel_credentials()`. This option is mutually
exclusive with the `channel_credentials` option.
compression: An optional value indicating the compression method to be
used over the lifetime of the channel, e.g. grpc.Compression.Gzip.
wait_for_ready: An optional flag indicating whether the RPC should fail
@ -362,7 +380,8 @@ def stream_unary(
The response to the RPC.
"""
channel = ChannelCache.get().get_channel(target, options,
channel_credentials, compression)
channel_credentials, insecure,
compression)
multicallable = channel.stream_unary(method, request_serializer,
response_deserializer)
return multicallable(request_iterator,
@ -381,6 +400,7 @@ def stream_stream(
response_deserializer: Optional[Callable[[bytes], Any]] = None,
options: Sequence[Tuple[AnyStr, AnyStr]] = (),
channel_credentials: Optional[grpc.ChannelCredentials] = None,
insecure: bool = False,
call_credentials: Optional[grpc.CallCredentials] = None,
compression: Optional[grpc.Compression] = None,
wait_for_ready: Optional[bool] = None,
@ -417,6 +437,9 @@ def stream_stream(
call_credentials: A call credential applied to each call individually,
e.g. the output of grpc.metadata_call_credentials() or
grpc.access_token_call_credentials().
insecure: If True, specifies channel_credentials as
:term:`grpc.insecure_channel_credentials()`. This option is mutually
exclusive with the `channel_credentials` option.
compression: An optional value indicating the compression method to be
used over the lifetime of the channel, e.g. grpc.Compression.Gzip.
wait_for_ready: An optional flag indicating whether the RPC should fail
@ -432,7 +455,8 @@ def stream_stream(
An iterator of responses.
"""
channel = ChannelCache.get().get_channel(target, options,
channel_credentials, compression)
channel_credentials, insecure,
compression)
multicallable = channel.stream_stream(method, request_serializer,
response_deserializer)
return multicallable(request_iterator,

@ -291,6 +291,26 @@ class SimpleStubsTest(unittest.TestCase):
response = grpc.experimental.unary_unary(
_REQUEST, target, _UNARY_UNARY, options=_property_options)
def test_insecure_sugar(self):
with _server(None) as port:
target = f'localhost:{port}'
response = grpc.experimental.unary_unary(_REQUEST,
target,
_UNARY_UNARY,
insecure=True)
self.assertEqual(_REQUEST, response)
def test_insecure_sugar_mutually_exclusive(self):
with _server(None) as port:
target = f'localhost:{port}'
with self.assertRaises(ValueError):
response = grpc.experimental.unary_unary(
_REQUEST,
target,
_UNARY_UNARY,
insecure=True,
channel_credentials=grpc.local_channel_credentials())
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)

Loading…
Cancel
Save