Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make make_secure_channel use google-auth #2808

Merged
merged 2 commits into from
Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions core/google/cloud/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@

try:
import grpc
from google.auth.transport.grpc import (
AuthMetadataPlugin) # pragma: NO COVER
except ImportError:
import google.auth.transport.grpc
except ImportError: # pragma: NO COVER
grpc = None
AuthMetadataPlugin = None

import httplib2
import six
Expand Down Expand Up @@ -472,22 +470,16 @@ def make_secure_channel(credentials, user_agent, host):
:rtype: :class:`grpc._channel.Channel`
:returns: gRPC secure channel with credentials attached.
"""
# ssl_channel_credentials() loads root certificates from
# `grpc/_adapter/credentials/roots.pem`.
transport_creds = grpc.ssl_channel_credentials()
http = httplib2.Http()
custom_metadata_plugin = AuthMetadataPlugin(
credentials, google_auth_httplib2.Request(http=http))
auth_creds = grpc.metadata_call_credentials(
custom_metadata_plugin, name='google_creds')
channel_creds = grpc.composite_channel_credentials(
transport_creds, auth_creds)
target = '%s:%d' % (host, http_client.HTTPS_PORT)
channel_args = (
http_request = google_auth_httplib2.Request(http=httplib2.Http())
options = (
('grpc.primary_user_agent', user_agent),
)
return grpc.secure_channel(target, channel_creds,
options=channel_args)
return google.auth.transport.grpc.secure_authorized_channel(
credentials,
http_request,
target,
options=options)


def make_secure_stub(credentials, user_agent, stub_class, host):
Expand Down
1 change: 1 addition & 0 deletions core/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ envlist =

[testing]
deps =
grpcio
mock
pytest
covercmd =
Expand Down
66 changes: 13 additions & 53 deletions core/unit_tests/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,65 +612,25 @@ def _call_fut(self, *args, **kwargs):

def test_it(self):
from six.moves import http_client
from google.cloud import _helpers as MUT

SSL_CREDS = object()
METADATA_CREDS = object()
COMPOSITE_CREDS = object()
CHANNEL = object()

class _GRPCModule(object):

def __init__(self):
self.ssl_channel_credentials_args = None
self.metadata_call_credentials_args = None
self.composite_channel_credentials_args = None
self.secure_channel_args = None

def ssl_channel_credentials(self, *args):
self.ssl_channel_credentials_args = args
return SSL_CREDS

def metadata_call_credentials(self, *args, **kwargs):
self.metadata_call_credentials_args = (args, kwargs)
return METADATA_CREDS

def composite_channel_credentials(self, *args):
self.composite_channel_credentials_args = args
return COMPOSITE_CREDS

def secure_channel(self, *args, **kwargs):
self.secure_channel_args = (args, kwargs)
return CHANNEL

grpc_mod = _GRPCModule()

host = 'HOST'
credentials = object()
host = 'HOST'
user_agent = 'USER_AGENT'

grpc_patch = mock.patch.object(MUT, 'grpc', new=grpc_mod)
request_patch = mock.patch('google_auth_httplib2.Request')
plugin_patch = mock.patch.object(
MUT, 'AuthMetadataPlugin', create=True)
with grpc_patch, request_patch as request_mock, plugin_patch as plugin:
secure_authorized_channel_patch = mock.patch(
'google.auth.transport.grpc.secure_authorized_channel',
autospec=True)

with secure_authorized_channel_patch as secure_authorized_channel:
result = self._call_fut(credentials, user_agent, host)

self.assertIs(result, CHANNEL)
plugin.assert_called_once_with(credentials, request_mock.return_value)
self.assertEqual(grpc_mod.ssl_channel_credentials_args, ())
self.assertEqual(grpc_mod.metadata_call_credentials_args,
((plugin.return_value,), {'name': 'google_creds'}))
self.assertEqual(
grpc_mod.composite_channel_credentials_args,
(SSL_CREDS, METADATA_CREDS))
target = '%s:%d' % (host, http_client.HTTPS_PORT)
secure_args = (target, COMPOSITE_CREDS)
secure_kwargs = {
'options': (('grpc.primary_user_agent', user_agent),)
}
self.assertEqual(grpc_mod.secure_channel_args,
(secure_args, secure_kwargs))
self.assertIs(result, secure_authorized_channel.return_value)

expected_target = '%s:%d' % (host, http_client.HTTPS_PORT)
expected_options = (('grpc.primary_user_agent', user_agent),)

secure_authorized_channel.assert_called_once_with(
credentials, mock.ANY, expected_target, options=expected_options)


class Test_make_secure_stub(unittest.TestCase):
Expand Down