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 """Google Compute Engine credentials. |
| 16 |
| 17 This module provides authentication for application running on Google Compute |
| 18 Engine using the Compute Engine metadata server. |
| 19 |
| 20 """ |
| 21 |
| 22 from google.auth import credentials |
| 23 from google.auth import exceptions |
| 24 from google.auth.compute_engine import _metadata |
| 25 |
| 26 |
| 27 class Credentials(credentials.Scoped, credentials.Credentials): |
| 28 """Compute Engine Credentials. |
| 29 |
| 30 These credentials use the Google Compute Engine metadata server to obtain |
| 31 OAuth 2.0 access tokens associated with the instance's service account. |
| 32 |
| 33 For more information about Compute Engine authentication, including how |
| 34 to configure scopes, see the `Compute Engine authentication |
| 35 documentation`_. |
| 36 |
| 37 .. note:: Compute Engine instances can be created with scopes and therefore |
| 38 these credentials are considered to be 'scoped'. However, you can |
| 39 not use :meth:`~google.auth.credentials.ScopedCredentials.with_scopes` |
| 40 because it is not possible to change the scopes that the instance |
| 41 has. Also note that |
| 42 :meth:`~google.auth.credentials.ScopedCredentials.has_scopes` will not |
| 43 work until the credentials have been refreshed. |
| 44 |
| 45 .. _Compute Engine authentication documentation: |
| 46 https://cloud.google.com/compute/docs/authentication#using |
| 47 """ |
| 48 |
| 49 def __init__(self, service_account_email='default'): |
| 50 """ |
| 51 Args: |
| 52 service_account_email (str): The service account email to use, or |
| 53 'default'. A Compute Engine instance may have multiple service |
| 54 accounts. |
| 55 """ |
| 56 super(Credentials, self).__init__() |
| 57 self._service_account_email = service_account_email |
| 58 |
| 59 def _retrieve_info(self, request): |
| 60 """Retrieve information about the service account. |
| 61 |
| 62 Updates the scopes and retrieves the full service account email. |
| 63 |
| 64 Args: |
| 65 request (google.auth.transport.Request): The object used to make |
| 66 HTTP requests. |
| 67 """ |
| 68 info = _metadata.get_service_account_info( |
| 69 request, |
| 70 service_account=self._service_account_email) |
| 71 |
| 72 self._service_account_email = info['email'] |
| 73 self._scopes = info['scopes'] |
| 74 |
| 75 def refresh(self, request): |
| 76 """Refresh the access token and scopes. |
| 77 |
| 78 Args: |
| 79 request (google.auth.transport.Request): The object used to make |
| 80 HTTP requests. |
| 81 |
| 82 Raises: |
| 83 google.auth.exceptions.RefreshError: If the Compute Engine metadata |
| 84 service can't be reached if if the instance has not |
| 85 credentials. |
| 86 """ |
| 87 try: |
| 88 self._retrieve_info(request) |
| 89 self.token, self.expiry = _metadata.get_service_account_token( |
| 90 request, |
| 91 service_account=self._service_account_email) |
| 92 except exceptions.TransportError as exc: |
| 93 raise exceptions.RefreshError(exc) |
| 94 |
| 95 @property |
| 96 def service_account_email(self): |
| 97 """The service account email. |
| 98 |
| 99 .. note: This is not guaranteed to be set until :meth`refresh` has been |
| 100 called. |
| 101 """ |
| 102 return self._service_account_email |
| 103 |
| 104 @property |
| 105 def requires_scopes(self): |
| 106 """False: Compute Engine credentials can not be scoped.""" |
| 107 return False |
| 108 |
| 109 def with_scopes(self, scopes): |
| 110 """Unavailable, Compute Engine credentials can not be scoped. |
| 111 |
| 112 Scopes can only be set at Compute Engine instance creation time. |
| 113 See the `Compute Engine authentication documentation`_ for details on |
| 114 how to configure instance scopes. |
| 115 |
| 116 .. _Compute Engine authentication documentation: |
| 117 https://cloud.google.com/compute/docs/authentication#using |
| 118 """ |
| 119 raise NotImplementedError( |
| 120 'Compute Engine credentials can not set scopes. Scopes must be ' |
| 121 'set when the Compute Engine instance is created.') |
OLD | NEW |