OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 Google Inc. |
| 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at |
| 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. |
| 14 |
| 15 """Authorization support for gRPC.""" |
| 16 |
| 17 from __future__ import absolute_import |
| 18 |
| 19 try: |
| 20 import grpc |
| 21 except ImportError: # pragma: NO COVER |
| 22 raise ImportError( |
| 23 'gRPC is not installed, please install the grpcio package to use the ' |
| 24 'gRPC transport.') |
| 25 import six |
| 26 |
| 27 |
| 28 class AuthMetadataPlugin(grpc.AuthMetadataPlugin): |
| 29 """A `gRPC AuthMetadataPlugin`_ that inserts the credentials into each |
| 30 request. |
| 31 |
| 32 .. _gRPC AuthMetadataPlugin: |
| 33 http://www.grpc.io/grpc/python/grpc.html#grpc.AuthMetadataPlugin |
| 34 |
| 35 Args: |
| 36 credentials (google.auth.credentials.Credentials): The credentials to |
| 37 add to requests. |
| 38 request (google.auth.transport.Request): A HTTP transport request |
| 39 object used to refresh credentials as needed. |
| 40 """ |
| 41 def __init__(self, credentials, request): |
| 42 # pylint: disable=no-value-for-parameter |
| 43 # pylint doesn't realize that the super method takes no arguments |
| 44 # because this class is the same name as the superclass. |
| 45 super(AuthMetadataPlugin, self).__init__() |
| 46 self._credentials = credentials |
| 47 self._request = request |
| 48 |
| 49 def _get_authorization_headers(self, context): |
| 50 """Gets the authorization headers for a request. |
| 51 |
| 52 Returns: |
| 53 Sequence[Tuple[str, str]]: A list of request headers (key, value) |
| 54 to add to the request. |
| 55 """ |
| 56 headers = {} |
| 57 self._credentials.before_request( |
| 58 self._request, |
| 59 context.method_name, |
| 60 context.service_url, |
| 61 headers) |
| 62 |
| 63 return list(six.iteritems(headers)) |
| 64 |
| 65 def __call__(self, context, callback): |
| 66 """Passes authorization metadata into the given callback. |
| 67 |
| 68 Args: |
| 69 context (grpc.AuthMetadataContext): The RPC context. |
| 70 callback (grpc.AuthMetadataPluginCallback): The callback that will |
| 71 be invoked to pass in the authorization metadata. |
| 72 """ |
| 73 callback(self._get_authorization_headers(context), None) |
| 74 |
| 75 |
| 76 def secure_authorized_channel( |
| 77 credentials, request, target, ssl_credentials=None, **kwargs): |
| 78 """Creates a secure authorized gRPC channel. |
| 79 |
| 80 This creates a channel with SSL and :class:`AuthMetadataPlugin`. This |
| 81 channel can be used to create a stub that can make authorized requests. |
| 82 |
| 83 Example:: |
| 84 |
| 85 import google.auth |
| 86 import google.auth.transport.grpc |
| 87 import google.auth.transport.requests |
| 88 from google.cloud.speech.v1 import cloud_speech_pb2 |
| 89 |
| 90 # Get credentials. |
| 91 credentials, _ = google.auth.default() |
| 92 |
| 93 # Get an HTTP request function to refresh credentials. |
| 94 request = google.auth.transport.requests.Request() |
| 95 |
| 96 # Create a channel. |
| 97 channel = google.auth.transport.grpc.secure_authorized_channel( |
| 98 credentials, 'speech.googleapis.com:443', request) |
| 99 |
| 100 # Use the channel to create a stub. |
| 101 cloud_speech.create_Speech_stub(channel) |
| 102 |
| 103 Args: |
| 104 credentials (google.auth.credentials.Credentials): The credentials to |
| 105 add to requests. |
| 106 request (google.auth.transport.Request): A HTTP transport request |
| 107 object used to refresh credentials as needed. Even though gRPC |
| 108 is a separate transport, there's no way to refresh the credentials |
| 109 without using a standard http transport. |
| 110 target (str): The host and port of the service. |
| 111 ssl_credentials (grpc.ChannelCredentials): Optional SSL channel |
| 112 credentials. This can be used to specify different certificates. |
| 113 kwargs: Additional arguments to pass to :func:`grpc.secure_channel`. |
| 114 |
| 115 Returns: |
| 116 grpc.Channel: The created gRPC channel. |
| 117 """ |
| 118 # Create the metadata plugin for inserting the authorization header. |
| 119 metadata_plugin = AuthMetadataPlugin(credentials, request) |
| 120 |
| 121 # Create a set of grpc.CallCredentials using the metadata plugin. |
| 122 google_auth_credentials = grpc.metadata_call_credentials(metadata_plugin) |
| 123 |
| 124 if ssl_credentials is None: |
| 125 ssl_credentials = grpc.ssl_channel_credentials() |
| 126 |
| 127 # Combine the ssl credentials and the authorization credentials. |
| 128 composite_credentials = grpc.composite_channel_credentials( |
| 129 ssl_credentials, google_auth_credentials) |
| 130 |
| 131 return grpc.secure_channel(target, composite_credentials, **kwargs) |
OLD | NEW |