Adopt reviewers' advice

pull/19971/head
Lidi Zheng 5 years ago
parent f681fe89af
commit 0d203d39b8
  1. 42
      src/python/grpcio/grpc/__init__.py
  2. 2
      src/python/grpcio/grpc/_cython/_cygrpc/credentials.pxd.pxi
  3. 56
      src/python/grpcio/grpc/_local_credentials.py
  4. 2
      src/python/grpcio_tests/tests/unit/BUILD.bazel
  5. 19
      src/python/grpcio_tests/tests/unit/_local_credentials_test.py

@ -22,6 +22,7 @@ import six
from grpc._cython import cygrpc as _cygrpc
from grpc import _compression
from grpc import local_credentials
logging.getLogger(__name__).addHandler(logging.NullHandler())
@ -1744,6 +1745,44 @@ def dynamic_ssl_server_credentials(initial_certificate_configuration,
certificate_configuration_fetcher, require_client_authentication))
@enum.unique
class LocalConnectionType(enum.Enum):
"""Type of local connections for which local channel/server credentials will be applied.
Attributes:
UDS: Unix domain socket connections
LOCAL_TCP: Local TCP connections.
"""
UDS = _cygrpc.LocalConnectType.uds
LOCAL_TCP = _cygrpc.LocalConnectType.local_tcp
def local_channel_credentials(local_connect_type=LocalConnectionType.LOCAL_TCP):
"""Creates a local ChannelCredentials used for local connections.
Args:
local_connect_type: Local connection type (either UDS or LOCAL_TCP)
Returns:
A ChannelCredentials for use with a local Channel
"""
return ChannelCredentials(
_cygrpc.channel_credentials_local(local_connect_type.value))
def local_server_credentials(local_connect_type=LocalConnectionType.LOCAL_TCP):
"""Creates a local ServerCredentials used for local connections.
Args:
local_connect_type: Local connection type (either UDS or LOCAL_TCP)
Returns:
A ServerCredentials for use with a local Server
"""
return ServerCredentials(
_cygrpc.server_credentials_local(local_connect_type.value))
def channel_ready_future(channel):
"""Creates a Future that tracks when a Channel is ready.
@ -1913,6 +1952,7 @@ __all__ = (
'ClientCallDetails',
'ServerCertificateConfiguration',
'ServerCredentials',
'LocalConnectionType',
'UnaryUnaryMultiCallable',
'UnaryStreamMultiCallable',
'StreamUnaryMultiCallable',
@ -1939,6 +1979,8 @@ __all__ = (
'access_token_call_credentials',
'composite_call_credentials',
'composite_channel_credentials',
'local_channel_credentials',
'local_server_credentials',
'ssl_server_credentials',
'ssl_server_certificate_configuration',
'dynamic_ssl_server_credentials',

@ -101,4 +101,4 @@ cdef class ServerCredentials:
cdef class LocalChannelCredentials(ChannelCredentials):
cdef readonly object _local_connect_type
cdef grpc_local_connect_type _local_connect_type

@ -1,56 +0,0 @@
# Copyright 2019 The gRPC authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""gRPC's local credential API."""
import enum
import grpc
from grpc._cython import cygrpc
@enum.unique
class LocalConnectType(enum.Enum):
"""Type of local connections for which local channel/server credentials will be applied.
Attributes:
UDS: Unix domain socket connections
LOCAL_TCP: Local TCP connections.
"""
UDS = cygrpc.LocalConnectType.uds
LOCAL_TCP = cygrpc.LocalConnectType.local_tcp
def local_channel_credentials(local_connect_type=LocalConnectType.LOCAL_TCP):
"""Creates a local ChannelCredentials used for local connections.
Args:
local_connect_type: Local connection type (either UDS or LOCAL_TCP)
Returns:
A ChannelCredentials for use with a local Channel
"""
return grpc.ChannelCredentials(
cygrpc.channel_credentials_local(local_connect_type.value))
def local_server_credentials(local_connect_type=LocalConnectType.LOCAL_TCP):
"""Creates a local ServerCredentials used for local connections.
Args:
local_connect_type: Local connection type (either UDS or LOCAL_TCP)
Returns:
A ServerCredentials for use with a local Server
"""
return grpc.ServerCredentials(
cygrpc.server_credentials_local(local_connect_type.value))

@ -19,7 +19,7 @@ GRPCIO_TESTS_UNIT = [
"_interceptor_test.py",
"_invalid_metadata_test.py",
"_invocation_defects_test.py",
"_local_crednetials_test.py",
"_local_credentials_test.py",
"_logging_test.py",
"_metadata_code_details_test.py",
"_metadata_test.py",

@ -1,4 +1,4 @@
# Copyright 2019 The gRPC authors
# Copyright 2019 The gRPC Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -16,7 +16,6 @@
import unittest
from concurrent.futures import ThreadPoolExecutor
import grpc
from grpc import local_credentials
class _GenericHandler(grpc.GenericRpcHandler):
@ -35,10 +34,10 @@ class LocalCredentialsTest(unittest.TestCase):
def test_local_tcp(self):
server_addr = '[::1]:{}'
channel_creds = local_credentials.local_channel_credentials(
local_credentials.LocalConnectType.LOCAL_TCP)
server_creds = local_credentials.local_server_credentials(
local_credentials.LocalConnectType.LOCAL_TCP)
channel_creds = grpc.local_channel_credentials(
grpc.LocalConnectionType.LOCAL_TCP)
server_creds = grpc.local_server_credentials(
grpc.LocalConnectionType.LOCAL_TCP)
server = self._create_server()
port = server.add_secure_port(server_addr.format(0), server_creds)
server.start()
@ -48,10 +47,10 @@ class LocalCredentialsTest(unittest.TestCase):
def test_uds(self):
server_addr = 'unix:/tmp/grpc_fullstack_test'
channel_creds = local_credentials.local_channel_credentials(
local_credentials.LocalConnectType.UDS)
server_creds = local_credentials.local_server_credentials(
local_credentials.LocalConnectType.UDS)
channel_creds = grpc.local_channel_credentials(
grpc.LocalConnectionType.UDS)
server_creds = grpc.local_server_credentials(
grpc.LocalConnectionType.UDS)
server = self._create_server()
server.add_secure_port(server_addr, server_creds)
server.start()

Loading…
Cancel
Save