Merge pull request #7163 from nathanielmanistaatgoogle/call-credentials

Support composition of arbitrarily many CallCredentials objects
pull/7289/head
kpayson64 9 years ago committed by GitHub
commit 32d3fbe284
  1. 34
      src/python/grpcio/grpc/__init__.py
  2. 48
      src/python/grpcio/grpc/_credential_composition.py
  3. 1
      src/python/grpcio_tests/tests/tests.json
  4. 72
      src/python/grpcio_tests/tests/unit/_credentials_test.py

@ -1091,37 +1091,41 @@ def access_token_call_credentials(access_token):
_auth.AccessTokenCallCredentials(access_token)) _auth.AccessTokenCallCredentials(access_token))
def composite_call_credentials(call_credentials, additional_call_credentials): def composite_call_credentials(*call_credentials):
"""Compose two CallCredentials to make a new one. """Compose multiple CallCredentials to make a new CallCredentials.
Args: Args:
call_credentials: A CallCredentials object. *call_credentials: At least two CallCredentials objects.
additional_call_credentials: Another CallCredentials object to compose on
top of call_credentials.
Returns: Returns:
A new CallCredentials composed of the two given CallCredentials. A CallCredentials object composed of the given CallCredentials objects.
""" """
from grpc import _credential_composition
cygrpc_call_credentials = tuple(
single_call_credentials._credentials
for single_call_credentials in call_credentials)
return CallCredentials( return CallCredentials(
_cygrpc.call_credentials_composite( _credential_composition.call(cygrpc_call_credentials))
call_credentials._credentials,
additional_call_credentials._credentials))
def composite_channel_credentials(channel_credentials, call_credentials): def composite_channel_credentials(channel_credentials, *call_credentials):
"""Compose a ChannelCredentials and a CallCredentials. """Compose a ChannelCredentials and one or more CallCredentials objects.
Args: Args:
channel_credentials: A ChannelCredentials. channel_credentials: A ChannelCredentials.
call_credentials: A CallCredentials. *call_credentials: One or more CallCredentials objects.
Returns: Returns:
A ChannelCredentials composed of the given ChannelCredentials and A ChannelCredentials composed of the given ChannelCredentials and
CallCredentials. CallCredentials objects.
""" """
from grpc import _credential_composition
cygrpc_call_credentials = tuple(
single_call_credentials._credentials
for single_call_credentials in call_credentials)
return ChannelCredentials( return ChannelCredentials(
_cygrpc.channel_credentials_composite( _credential_composition.channel(
channel_credentials._credentials, call_credentials._credentials)) channel_credentials._credentials, cygrpc_call_credentials))
def ssl_server_credentials( def ssl_server_credentials(

@ -0,0 +1,48 @@
# Copyright 2016, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from grpc._cython import cygrpc
def _call(call_credentialses):
call_credentials_iterator = iter(call_credentialses)
composition = next(call_credentials_iterator)
for additional_call_credentials in call_credentials_iterator:
composition = cygrpc.call_credentials_composite(
composition, additional_call_credentials)
return composition
def call(call_credentialses):
return _call(call_credentialses)
def channel(channel_credentials, call_credentialses):
return cygrpc.channel_credentials_composite(
channel_credentials, _call(call_credentialses))

@ -12,6 +12,7 @@
"_channel_test.ChannelTest", "_channel_test.ChannelTest",
"_compression_test.CompressionTest", "_compression_test.CompressionTest",
"_connectivity_channel_test.ConnectivityStatesTest", "_connectivity_channel_test.ConnectivityStatesTest",
"_credentials_test.CredentialsTest",
"_empty_message_test.EmptyMessageTest", "_empty_message_test.EmptyMessageTest",
"_exit_test.ExitTest", "_exit_test.ExitTest",
"_face_interface_test.DynamicInvokerBlockingInvocationInlineServiceTest", "_face_interface_test.DynamicInvokerBlockingInvocationInlineServiceTest",

@ -0,0 +1,72 @@
# Copyright 2016, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Tests of credentials."""
import unittest
import grpc
class CredentialsTest(unittest.TestCase):
def test_call_credentials_composition(self):
first = grpc.access_token_call_credentials('abc')
second = grpc.access_token_call_credentials('def')
third = grpc.access_token_call_credentials('ghi')
first_and_second = grpc.composite_call_credentials(first, second)
first_second_and_third = grpc.composite_call_credentials(
first, second, third)
self.assertIsInstance(first_and_second, grpc.CallCredentials)
self.assertIsInstance(first_second_and_third, grpc.CallCredentials)
def test_channel_credentials_composition(self):
first_call_credentials = grpc.access_token_call_credentials('abc')
second_call_credentials = grpc.access_token_call_credentials('def')
third_call_credentials = grpc.access_token_call_credentials('ghi')
channel_credentials = grpc.ssl_channel_credentials()
channel_and_first = grpc.composite_channel_credentials(
channel_credentials, first_call_credentials)
channel_first_and_second = grpc.composite_channel_credentials(
channel_credentials, first_call_credentials, second_call_credentials)
channel_first_second_and_third = grpc.composite_channel_credentials(
channel_credentials, first_call_credentials, second_call_credentials,
third_call_credentials)
self.assertIsInstance(channel_and_first, grpc.ChannelCredentials)
self.assertIsInstance(channel_first_and_second, grpc.ChannelCredentials)
self.assertIsInstance(
channel_first_second_and_third, grpc.ChannelCredentials)
if __name__ == '__main__':
unittest.main(verbosity=2)
Loading…
Cancel
Save