This repository has been archived by the owner on Mar 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Refactor auth code to ease transition to google-auth #135
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3aa8668
Refactor auth code to ease transition to google-auth
150b2b8
Remove unused import
2976ffd
Add needed pylint disables
7dfd9bd
Clarify docstring
c40a313
Move auth to _grpc_oauth2client
9840751
Move auth to _grpc_oauth2client, use MetadataPlugin class instead of …
ed3ddd6
Update docs/index.rst
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Copyright 2015, 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. | ||
|
||
# pylint: disable=too-few-public-methods | ||
"""Provides gRPC authentication support using oauth2client.""" | ||
|
||
from __future__ import absolute_import | ||
|
||
import grpc | ||
import oauth2client.client | ||
|
||
|
||
class AuthMetadataPlugin(grpc.AuthMetadataPlugin): | ||
"""A `gRPC AuthMetadataPlugin`_ that inserts the credentials into each | ||
request. | ||
|
||
.. _gRPC AuthMetadataPlugin: | ||
http://www.grpc.io/grpc/python/grpc.html#grpc.AuthMetadataPlugin | ||
|
||
Args: | ||
credentials (oauth2client.client.Credentials): The credentials to | ||
add to requests. | ||
""" | ||
def __init__(self, credentials, ): | ||
self._credentials = credentials | ||
|
||
def _get_authorization_headers(self): | ||
"""Gets the authorization headers for a request. | ||
|
||
Returns: | ||
Sequence[Tuple[str, str]]: A list of request headers (key, value) | ||
to add to the request. | ||
""" | ||
bearer_token = self._credentials.get_access_token().access_token | ||
return [ | ||
('authorization', 'Bearer {}'.format(bearer_token)) | ||
] | ||
|
||
def __call__(self, context, callback): | ||
"""Passes authorization metadata into the given callback. | ||
|
||
Args: | ||
context (grpc.AuthMetadataContext): The RPC context. | ||
callback (grpc.AuthMetadataPluginCallback): The callback that will | ||
be invoked to pass in the authorization metadata. | ||
""" | ||
callback(self._get_authorization_headers(), None) | ||
|
||
|
||
def get_default_credentials(scopes): | ||
"""Gets the Application Default Credentials.""" | ||
credentials = ( | ||
oauth2client.client.GoogleCredentials.get_application_default()) | ||
return credentials.create_scoped(scopes or []) | ||
|
||
|
||
def secure_authorized_channel( | ||
credentials, target, ssl_credentials=None): | ||
"""Creates a secure authorized gRPC channel.""" | ||
if ssl_credentials is None: | ||
ssl_credentials = grpc.ssl_channel_credentials() | ||
|
||
metadata_plugin = AuthMetadataPlugin(credentials) | ||
call_credentials = grpc.metadata_call_credentials(metadata_plugin) | ||
channel_creds = grpc.composite_channel_credentials( | ||
ssl_credentials, call_credentials) | ||
|
||
return grpc.secure_channel(target, channel_creds) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,9 +30,9 @@ | |
"""Adapts the grpc surface.""" | ||
|
||
from __future__ import absolute_import | ||
import grpc | ||
|
||
from grpc import RpcError, StatusCode | ||
from . import auth | ||
from . import _grpc_oauth2client | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. think this file accidentally got left out of the commit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, fixed. I also did away with the use of closures and went with @dhermes' suggestion of using a class like we do in google-auth. |
||
|
||
|
||
API_ERRORS = (RpcError, ) | ||
|
@@ -73,53 +73,36 @@ def exc_to_code(exc): | |
return None | ||
|
||
|
||
def _make_grpc_auth_func(auth_func): | ||
"""Creates the auth func expected by the grpc callback.""" | ||
|
||
def grpc_auth(dummy_context, callback): | ||
"""The auth signature required by grpc.""" | ||
callback(auth_func(), None) | ||
|
||
return grpc_auth | ||
|
||
|
||
def _make_channel_creds(auth_func, ssl_creds): | ||
"""Converts the auth func into the composite creds expected by grpc.""" | ||
grpc_auth_func = _make_grpc_auth_func(auth_func) | ||
call_creds = grpc.metadata_call_credentials(grpc_auth_func) | ||
return grpc.composite_channel_credentials(ssl_creds, call_creds) | ||
|
||
|
||
def create_stub(generated_create_stub, service_path, port, ssl_creds=None, | ||
channel=None, metadata_transformer=None, scopes=None): | ||
def create_stub(generated_create_stub, channel=None, service_path=None, | ||
service_port=None, credentials=None, scopes=None, | ||
ssl_credentials=None): | ||
"""Creates a gRPC client stub. | ||
|
||
Args: | ||
generated_create_stub: The generated gRPC method to create a stub. | ||
service_path: The domain name of the API remote host. | ||
port: The port on which to connect to the remote host. | ||
ssl_creds: A ClientCredentials object for use with an SSL-enabled | ||
Channel. If none, credentials are pulled from a default location. | ||
channel: A Channel object through which to make calls. If none, a secure | ||
channel is constructed. | ||
metadata_transformer: A function that transforms the metadata for | ||
requests, e.g., to give OAuth credentials. | ||
channel is constructed. If specified, all remaining arguments are | ||
ignored. | ||
service_path: The domain name of the API remote host. | ||
service_port: The port on which to connect to the remote host. | ||
credentials: The authorization credentials to attach to requests. | ||
These credentials identify your application to the service. | ||
scopes: The OAuth scopes for this service. This parameter is ignored if | ||
a custom metadata_transformer is supplied. | ||
a credentials is specified. | ||
ssl_credentials: gRPC channel credentials used to create a secure | ||
gRPC channel. If not specified, SSL credentials will be created | ||
using default certificates. | ||
|
||
Returns: | ||
A gRPC client stub. | ||
""" | ||
if channel is None: | ||
if ssl_creds is None: | ||
ssl_creds = grpc.ssl_channel_credentials() | ||
if metadata_transformer is None: | ||
if scopes is None: | ||
scopes = [] | ||
metadata_transformer = auth.make_auth_func(scopes) | ||
|
||
channel_creds = _make_channel_creds(metadata_transformer, ssl_creds) | ||
target = '{}:{}'.format(service_path, port) | ||
channel = grpc.secure_channel(target, channel_creds) | ||
target = '{}:{}'.format(service_path, service_port) | ||
|
||
if credentials is None: | ||
credentials = _grpc_oauth2client.get_default_credentials(scopes) | ||
|
||
channel = _grpc_oauth2client.secure_authorized_channel( | ||
credentials, target, ssl_credentials=ssl_credentials) | ||
|
||
return generated_create_stub(channel) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. So this file goes away entirely once googleapis/google-auth-library-python#67 is merged, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'll stay around until we formally deprecate oauth2client. I'll also be adding another module named
_grpc_google_auth.py
that'll be a very small interface togoogle.auth
.