diff --git a/src/python/grpcio/grpc/__init__.py b/src/python/grpcio/grpc/__init__.py index 04797f857c2..daceca3b59e 100644 --- a/src/python/grpcio/grpc/__init__.py +++ b/src/python/grpcio/grpc/__init__.py @@ -1606,6 +1606,7 @@ def ssl_channel_credentials(root_certificates=None, _cygrpc.SSLChannelCredentials(root_certificates, private_key, certificate_chain)) + def xds_channel_credentials(fallback_credentials=None): """Creates a ChannelCredentials for use with xDS. This is an EXPERIMENTAL API. @@ -1615,8 +1616,10 @@ def xds_channel_credentials(fallback_credentials=None): establish a secure connection via xDS. If no fallback_credentials argument is supplied, a default SSLChannelCredentials is used. """ - fallback_credentials = ssl_channel_credentials() if fallback_credentials is None else fallback_credentials - return ChannelCredentials(_cygrpc.XDSChannelCredentials(fallback_credentials._credentials)) + fallback_credentials = ssl_channel_credentials( + ) if fallback_credentials is None else fallback_credentials + return ChannelCredentials( + _cygrpc.XDSChannelCredentials(fallback_credentials._credentials)) def metadata_call_credentials(metadata_plugin, name=None): @@ -1726,7 +1729,9 @@ def xds_server_credentials(fallback_credentials): fallback_credentials: Credentials to use in case it is not possible to establish a secure connection via xDS. No default value is provided. """ - return ServerCredentials(_cygrpc.xds_server_credentials(fallback_credentials._credentials)) + return ServerCredentials( + _cygrpc.xds_server_credentials(fallback_credentials._credentials)) + def insecure_server_credentials(): """Creates a credentials object directing the server to use no credentials. @@ -2047,8 +2052,7 @@ def server(thread_pool, () if handlers is None else handlers, () if interceptors is None else interceptors, () if options is None else options, - maximum_concurrent_rpcs, compression, - xds) + maximum_concurrent_rpcs, compression, xds) @contextlib.contextmanager diff --git a/src/python/grpcio/grpc/_simple_stubs.py b/src/python/grpcio/grpc/_simple_stubs.py index fcff584fbf5..a324f1d7eba 100644 --- a/src/python/grpcio/grpc/_simple_stubs.py +++ b/src/python/grpcio/grpc/_simple_stubs.py @@ -60,13 +60,13 @@ else: def _create_channel(target: str, options: Sequence[Tuple[str, str]], channel_credentials: Optional[grpc.ChannelCredentials], compression: Optional[grpc.Compression]) -> grpc.Channel: - _LOGGER.debug( - f"Creating secure channel with credentials '{channel_credentials}', " - + f"options '{options}' and compression '{compression}'") - return grpc.secure_channel(target, - credentials=channel_credentials, - options=options, - compression=compression) + _LOGGER.debug( + f"Creating secure channel with credentials '{channel_credentials}', " + + f"options '{options}' and compression '{compression}'") + return grpc.secure_channel(target, + credentials=channel_credentials, + options=options, + compression=compression) class ChannelCache: diff --git a/src/python/grpcio/grpc/experimental/__init__.py b/src/python/grpcio/grpc/experimental/__init__.py index 916c74fb803..c820bc9db3b 100644 --- a/src/python/grpcio/grpc/experimental/__init__.py +++ b/src/python/grpcio/grpc/experimental/__init__.py @@ -44,7 +44,8 @@ class UsageError(Exception): # It's important that there be a single insecure credentials object so that its # hash is deterministic and can be used for indexing in the simple stubs cache. -_insecure_channel_credentials = grpc.ChannelCredentials(_cygrpc.channel_credentials_insecure()) +_insecure_channel_credentials = grpc.ChannelCredentials( + _cygrpc.channel_credentials_insecure()) def insecure_channel_credentials(): diff --git a/src/python/grpcio_tests/tests/unit/_xds_credentials_test.py b/src/python/grpcio_tests/tests/unit/_xds_credentials_test.py index 0258240a7bf..55a2bee6021 100644 --- a/src/python/grpcio_tests/tests/unit/_xds_credentials_test.py +++ b/src/python/grpcio_tests/tests/unit/_xds_credentials_test.py @@ -24,11 +24,11 @@ from tests.unit import test_common from tests.unit import resources - class _GenericHandler(grpc.GenericRpcHandler): def service(self, handler_call_details): - return grpc.unary_unary_rpc_method_handler(lambda request, unused_context: request) + return grpc.unary_unary_rpc_method_handler( + lambda request, unused_context: request) class XdsCredentialsTest(unittest.TestCase): @@ -38,20 +38,25 @@ class XdsCredentialsTest(unittest.TestCase): # In this case, SSL credentials. server = grpc.server(futures.ThreadPoolExecutor()) server.add_generic_rpc_handlers((_GenericHandler(),)) - server_fallback_creds = grpc.ssl_server_credentials(((resources.private_key(), resources.certificate_chain()),)) + server_fallback_creds = grpc.ssl_server_credentials( + ((resources.private_key(), resources.certificate_chain()),)) server_creds = grpc.xds_server_credentials(server_fallback_creds) port = server.add_secure_port("localhost:0", server_creds) server.start() channel_fallback_creds = grpc.ssl_channel_credentials( - root_certificates=resources.test_root_certificates(), - private_key=resources.private_key(), - certificate_chain=resources.certificate_chain()) + root_certificates=resources.test_root_certificates(), + private_key=resources.private_key(), + certificate_chain=resources.certificate_chain()) channel_creds = grpc.xds_channel_credentials(channel_fallback_creds) server_address = "localhost:{}".format(port) - override_options = (("grpc.ssl_target_name_override", "foo.test.google.fr"),) - with grpc.secure_channel(server_address, channel_creds, options=override_options) as channel: + override_options = (("grpc.ssl_target_name_override", + "foo.test.google.fr"),) + with grpc.secure_channel(server_address, + channel_creds, + options=override_options) as channel: request = b"abc" - response = channel.unary_unary("/test/method")(request, wait_for_ready=True) + response = channel.unary_unary("/test/method")(request, + wait_for_ready=True) self.assertEqual(response, request) server.stop(None) @@ -64,12 +69,14 @@ class XdsCredentialsTest(unittest.TestCase): server_creds = grpc.xds_server_credentials(server_fallback_creds) port = server.add_secure_port("localhost:0", server_creds) server.start() - channel_fallback_creds = grpc.experimental.insecure_channel_credentials() + channel_fallback_creds = grpc.experimental.insecure_channel_credentials( + ) channel_creds = grpc.xds_channel_credentials(channel_fallback_creds) server_address = "localhost:{}".format(port) with grpc.secure_channel(server_address, channel_creds) as channel: request = b"abc" - response = channel.unary_unary("/test/method")(request, wait_for_ready=True) + response = channel.unary_unary("/test/method")(request, + wait_for_ready=True) self.assertEqual(response, request) server.stop(None) @@ -84,6 +91,7 @@ class XdsCredentialsTest(unittest.TestCase): # No exceptions thrown. A more comprehensive suite of tests will be # provided by the interop tests. + if __name__ == "__main__": logging.basicConfig() unittest.main()