Skip to content

Commit d20f3bb

Browse files
author
Jon Wayne Parrott
authored
Make make_secure_channel use google-auth (#2808)
1 parent 6916814 commit d20f3bb

File tree

3 files changed

+23
-70
lines changed

3 files changed

+23
-70
lines changed

core/google/cloud/_helpers.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@
3232

3333
try:
3434
import grpc
35-
from google.auth.transport.grpc import (
36-
AuthMetadataPlugin) # pragma: NO COVER
37-
except ImportError:
35+
import google.auth.transport.grpc
36+
except ImportError: # pragma: NO COVER
3837
grpc = None
39-
AuthMetadataPlugin = None
4038

4139
import httplib2
4240
import six
@@ -485,22 +483,16 @@ def make_secure_channel(credentials, user_agent, host):
485483
:rtype: :class:`grpc._channel.Channel`
486484
:returns: gRPC secure channel with credentials attached.
487485
"""
488-
# ssl_channel_credentials() loads root certificates from
489-
# `grpc/_adapter/credentials/roots.pem`.
490-
transport_creds = grpc.ssl_channel_credentials()
491-
http = httplib2.Http()
492-
custom_metadata_plugin = AuthMetadataPlugin(
493-
credentials, google_auth_httplib2.Request(http=http))
494-
auth_creds = grpc.metadata_call_credentials(
495-
custom_metadata_plugin, name='google_creds')
496-
channel_creds = grpc.composite_channel_credentials(
497-
transport_creds, auth_creds)
498486
target = '%s:%d' % (host, http_client.HTTPS_PORT)
499-
channel_args = (
487+
http_request = google_auth_httplib2.Request(http=httplib2.Http())
488+
options = (
500489
('grpc.primary_user_agent', user_agent),
501490
)
502-
return grpc.secure_channel(target, channel_creds,
503-
options=channel_args)
491+
return google.auth.transport.grpc.secure_authorized_channel(
492+
credentials,
493+
http_request,
494+
target,
495+
options=options)
504496

505497

506498
def make_secure_stub(credentials, user_agent, stub_class, host):

core/tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ envlist =
44

55
[testing]
66
deps =
7+
grpcio
78
mock
89
pytest
910
covercmd =

core/unit_tests/test__helpers.py

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -624,65 +624,25 @@ def _call_fut(self, *args, **kwargs):
624624

625625
def test_it(self):
626626
from six.moves import http_client
627-
from google.cloud import _helpers as MUT
628-
629-
SSL_CREDS = object()
630-
METADATA_CREDS = object()
631-
COMPOSITE_CREDS = object()
632-
CHANNEL = object()
633-
634-
class _GRPCModule(object):
635-
636-
def __init__(self):
637-
self.ssl_channel_credentials_args = None
638-
self.metadata_call_credentials_args = None
639-
self.composite_channel_credentials_args = None
640-
self.secure_channel_args = None
641-
642-
def ssl_channel_credentials(self, *args):
643-
self.ssl_channel_credentials_args = args
644-
return SSL_CREDS
645-
646-
def metadata_call_credentials(self, *args, **kwargs):
647-
self.metadata_call_credentials_args = (args, kwargs)
648-
return METADATA_CREDS
649-
650-
def composite_channel_credentials(self, *args):
651-
self.composite_channel_credentials_args = args
652-
return COMPOSITE_CREDS
653627

654-
def secure_channel(self, *args, **kwargs):
655-
self.secure_channel_args = (args, kwargs)
656-
return CHANNEL
657-
658-
grpc_mod = _GRPCModule()
659-
660-
host = 'HOST'
661628
credentials = object()
629+
host = 'HOST'
662630
user_agent = 'USER_AGENT'
663631

664-
grpc_patch = mock.patch.object(MUT, 'grpc', new=grpc_mod)
665-
request_patch = mock.patch('google_auth_httplib2.Request')
666-
plugin_patch = mock.patch.object(
667-
MUT, 'AuthMetadataPlugin', create=True)
668-
with grpc_patch, request_patch as request_mock, plugin_patch as plugin:
632+
secure_authorized_channel_patch = mock.patch(
633+
'google.auth.transport.grpc.secure_authorized_channel',
634+
autospec=True)
635+
636+
with secure_authorized_channel_patch as secure_authorized_channel:
669637
result = self._call_fut(credentials, user_agent, host)
670638

671-
self.assertIs(result, CHANNEL)
672-
plugin.assert_called_once_with(credentials, request_mock.return_value)
673-
self.assertEqual(grpc_mod.ssl_channel_credentials_args, ())
674-
self.assertEqual(grpc_mod.metadata_call_credentials_args,
675-
((plugin.return_value,), {'name': 'google_creds'}))
676-
self.assertEqual(
677-
grpc_mod.composite_channel_credentials_args,
678-
(SSL_CREDS, METADATA_CREDS))
679-
target = '%s:%d' % (host, http_client.HTTPS_PORT)
680-
secure_args = (target, COMPOSITE_CREDS)
681-
secure_kwargs = {
682-
'options': (('grpc.primary_user_agent', user_agent),)
683-
}
684-
self.assertEqual(grpc_mod.secure_channel_args,
685-
(secure_args, secure_kwargs))
639+
self.assertIs(result, secure_authorized_channel.return_value)
640+
641+
expected_target = '%s:%d' % (host, http_client.HTTPS_PORT)
642+
expected_options = (('grpc.primary_user_agent', user_agent),)
643+
644+
secure_authorized_channel.assert_called_once_with(
645+
credentials, mock.ANY, expected_target, options=expected_options)
686646

687647

688648
class Test_make_secure_stub(unittest.TestCase):

0 commit comments

Comments
 (0)