From b821ff99a80174d7e65f45caeea8de19aebff42e Mon Sep 17 00:00:00 2001 From: Richard Belleville Date: Sat, 13 Jun 2020 00:11:43 +0000 Subject: [PATCH] Initial working implementation --- src/python/grpcio/grpc/__init__.py | 7 +++++++ .../grpc/_cython/_cygrpc/credentials.pyx.pxi | 13 +++++++++++++ .../grpcio_tests/tests/interop/BUILD.bazel | 2 +- src/python/grpcio_tests/tests/interop/client.py | 17 +++++++++++++++-- .../grpcio_tests/tests_aio/interop/BUILD.bazel | 1 + 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/python/grpcio/grpc/__init__.py b/src/python/grpcio/grpc/__init__.py index 303d89c6263..8d5139dbb63 100644 --- a/src/python/grpcio/grpc/__init__.py +++ b/src/python/grpcio/grpc/__init__.py @@ -1868,6 +1868,13 @@ def alts_server_credentials(): return ServerCredentials(_cygrpc.server_credentials_alts()) +def google_default_channel_credentials(): + """ + TODO: Document. + """ + return ChannelCredentials(_cygrpc.channel_credentials_google_default()) + + def channel_ready_future(channel): """Creates a Future that tracks when a Channel is ready. diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/credentials.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/credentials.pyx.pxi index 0ff3706d134..e8eace4e1e2 100644 --- a/src/python/grpcio/grpc/_cython/_cygrpc/credentials.pyx.pxi +++ b/src/python/grpcio/grpc/_cython/_cygrpc/credentials.pyx.pxi @@ -380,3 +380,16 @@ def server_credentials_alts(): # Options can be destroyed as deep copy was performed. grpc_alts_credentials_options_destroy(c_options) return credentials + +cdef class GoogleDefaultChannelCredentials(ChannelCredentials): + cdef grpc_channel_credentials* _c_creds + + def __cinit__(self): + self._c_creds = NULL + + cdef grpc_channel_credentials *c(self) except *: + self._c_creds = grpc_google_default_credentials_create() + return self._c_creds + +def channel_credentials_google_default(): + return GoogleDefaultChannelCredentials() diff --git a/src/python/grpcio_tests/tests/interop/BUILD.bazel b/src/python/grpcio_tests/tests/interop/BUILD.bazel index 4685852162b..3a3541c8dc9 100644 --- a/src/python/grpcio_tests/tests/interop/BUILD.bazel +++ b/src/python/grpcio_tests/tests/interop/BUILD.bazel @@ -12,7 +12,7 @@ py_library( ], ) -py_library( +py_binary( name = "client", srcs = ["client.py"], imports = ["../../"], diff --git a/src/python/grpcio_tests/tests/interop/client.py b/src/python/grpcio_tests/tests/interop/client.py index 04b82c0b401..a6c7c7f9df2 100644 --- a/src/python/grpcio_tests/tests/interop/client.py +++ b/src/python/grpcio_tests/tests/interop/client.py @@ -51,6 +51,10 @@ def parse_interop_client_args(): default=False, type=resources.parse_bool, help='replace platform root CAs with ca.pem') + parser.add_argument('--custom_credentials_type', + choices=["google_default_credentials"], + default=None, + help='use google default credentials') parser.add_argument('--server_host_override', type=str, help='the server host to which to claim to connect') @@ -90,7 +94,16 @@ def get_secure_channel_parameters(args): call_credentials = _create_call_credentials(args) channel_opts = None - if args.use_tls: + if args.custom_credentials_type is not None: + if args.custom_credentials_type == "google_default_credentials": + channel_credentials = grpc.google_default_channel_credentials() + if call_credentials is not None: + channel_credentials = grpc.composite_channel_credentials( + channel_credentials, call_credentials) + else: + raise ValueError("Unknown credentials type '{}'".format( + args.custom_credentials_type)) + elif args.use_tls: if args.use_test_ca: root_certificates = resources.test_root_certificates() else: @@ -115,7 +128,7 @@ def get_secure_channel_parameters(args): def _create_channel(args): target = '{}:{}'.format(args.server_host, args.server_port) - if args.use_tls or args.use_alts: + if args.use_tls or args.use_alts or args.custom_credentials_type is not None: channel_credentials, options = get_secure_channel_parameters(args) return grpc.secure_channel(target, channel_credentials, options) else: diff --git a/src/python/grpcio_tests/tests_aio/interop/BUILD.bazel b/src/python/grpcio_tests/tests_aio/interop/BUILD.bazel index f67ad35cca7..47f41dba44b 100644 --- a/src/python/grpcio_tests/tests_aio/interop/BUILD.bazel +++ b/src/python/grpcio_tests/tests_aio/interop/BUILD.bazel @@ -62,6 +62,7 @@ py_binary( ], ) +# TODO: Pull out library to fix aio interop client. py_binary( name = "client", srcs = ["client.py"],