C.J. Collier | 37141e4 | 2020-02-13 13:49:49 -0800 | [diff] [blame] | 1 | # Copyright 2016 Google LLC |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 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 | |
Jon Wayne Parrott | 256d2bf | 2016-12-13 16:12:02 -0800 | [diff] [blame] | 15 | """Google App Engine standard environment support. |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 16 | |
Jon Wayne Parrott | 256d2bf | 2016-12-13 16:12:02 -0800 | [diff] [blame] | 17 | This module provides authentication and signing for applications running on App |
| 18 | Engine in the standard environment using the `App Identity API`_. |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 19 | |
| 20 | |
| 21 | .. _App Identity API: |
| 22 | https://cloud.google.com/appengine/docs/python/appidentity/ |
| 23 | """ |
| 24 | |
| 25 | import datetime |
| 26 | |
| 27 | from google.auth import _helpers |
| 28 | from google.auth import credentials |
Jon Wayne Parrott | 254befe | 2017-02-22 14:37:31 -0800 | [diff] [blame] | 29 | from google.auth import crypt |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 30 | |
Teddy Sudol | a10b15e | 2018-10-05 10:20:33 -0700 | [diff] [blame] | 31 | # pytype: disable=import-error |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 32 | try: |
| 33 | from google.appengine.api import app_identity |
| 34 | except ImportError: |
| 35 | app_identity = None |
Teddy Sudol | a10b15e | 2018-10-05 10:20:33 -0700 | [diff] [blame] | 36 | # pytype: enable=import-error |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 37 | |
| 38 | |
Jon Wayne Parrott | 254befe | 2017-02-22 14:37:31 -0800 | [diff] [blame] | 39 | class Signer(crypt.Signer): |
Jon Wayne Parrott | 5b4e9c8 | 2017-02-15 16:44:00 -0800 | [diff] [blame] | 40 | """Signs messages using the App Engine App Identity service. |
Jon Wayne Parrott | 256d2bf | 2016-12-13 16:12:02 -0800 | [diff] [blame] | 41 | |
| 42 | This can be used in place of :class:`google.auth.crypt.Signer` when |
| 43 | running in the App Engine standard environment. |
| 44 | """ |
Jon Wayne Parrott | 5b4e9c8 | 2017-02-15 16:44:00 -0800 | [diff] [blame] | 45 | |
| 46 | @property |
| 47 | def key_id(self): |
| 48 | """Optional[str]: The key ID used to identify this private key. |
| 49 | |
Jon Wayne Parrott | 254befe | 2017-02-22 14:37:31 -0800 | [diff] [blame] | 50 | .. warning:: |
| 51 | This is always ``None``. The key ID used by App Engine can not |
| 52 | be reliably determined ahead of time. |
Jon Wayne Parrott | 5b4e9c8 | 2017-02-15 16:44:00 -0800 | [diff] [blame] | 53 | """ |
Jon Wayne Parrott | 254befe | 2017-02-22 14:37:31 -0800 | [diff] [blame] | 54 | return None |
Jon Wayne Parrott | 256d2bf | 2016-12-13 16:12:02 -0800 | [diff] [blame] | 55 | |
Jon Wayne Parrott | 254befe | 2017-02-22 14:37:31 -0800 | [diff] [blame] | 56 | @_helpers.copy_docstring(crypt.Signer) |
| 57 | def sign(self, message): |
Jon Wayne Parrott | 256d2bf | 2016-12-13 16:12:02 -0800 | [diff] [blame] | 58 | message = _helpers.to_bytes(message) |
Jon Wayne Parrott | 5b4e9c8 | 2017-02-15 16:44:00 -0800 | [diff] [blame] | 59 | _, signature = app_identity.sign_blob(message) |
| 60 | return signature |
Jon Wayne Parrott | 256d2bf | 2016-12-13 16:12:02 -0800 | [diff] [blame] | 61 | |
| 62 | |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 63 | def get_project_id(): |
| 64 | """Gets the project ID for the current App Engine application. |
| 65 | |
| 66 | Returns: |
| 67 | str: The project ID |
| 68 | |
| 69 | Raises: |
| 70 | EnvironmentError: If the App Engine APIs are unavailable. |
| 71 | """ |
Jon Wayne Parrott | 20e6e58 | 2016-12-19 10:21:23 -0800 | [diff] [blame] | 72 | # pylint: disable=missing-raises-doc |
| 73 | # Pylint rightfully thinks EnvironmentError is OSError, but doesn't |
| 74 | # realize it's a valid alias. |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 75 | if app_identity is None: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 76 | raise EnvironmentError("The App Engine APIs are not available.") |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 77 | return app_identity.get_application_id() |
| 78 | |
| 79 | |
Bu Sun Kim | 41599ae | 2020-09-02 12:55:42 -0600 | [diff] [blame^] | 80 | class Credentials( |
| 81 | credentials.Scoped, credentials.Signing, credentials.CredentialsWithQuotaProject |
| 82 | ): |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 83 | """App Engine standard environment credentials. |
| 84 | |
| 85 | These credentials use the App Engine App Identity API to obtain access |
| 86 | tokens. |
| 87 | """ |
| 88 | |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 89 | def __init__(self, scopes=None, service_account_id=None, quota_project_id=None): |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 90 | """ |
| 91 | Args: |
| 92 | scopes (Sequence[str]): Scopes to request from the App Identity |
| 93 | API. |
| 94 | service_account_id (str): The service account ID passed into |
| 95 | :func:`google.appengine.api.app_identity.get_access_token`. |
| 96 | If not specified, the default application service account |
| 97 | ID will be used. |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 98 | quota_project_id (Optional[str]): The project ID used for quota |
| 99 | and billing. |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 100 | |
| 101 | Raises: |
| 102 | EnvironmentError: If the App Engine APIs are unavailable. |
| 103 | """ |
Jon Wayne Parrott | 20e6e58 | 2016-12-19 10:21:23 -0800 | [diff] [blame] | 104 | # pylint: disable=missing-raises-doc |
| 105 | # Pylint rightfully thinks EnvironmentError is OSError, but doesn't |
| 106 | # realize it's a valid alias. |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 107 | if app_identity is None: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 108 | raise EnvironmentError("The App Engine APIs are not available.") |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 109 | |
| 110 | super(Credentials, self).__init__() |
| 111 | self._scopes = scopes |
| 112 | self._service_account_id = service_account_id |
Jon Wayne Parrott | d722167 | 2017-02-16 09:05:11 -0800 | [diff] [blame] | 113 | self._signer = Signer() |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 114 | self._quota_project_id = quota_project_id |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 115 | |
| 116 | @_helpers.copy_docstring(credentials.Credentials) |
| 117 | def refresh(self, request): |
| 118 | # pylint: disable=unused-argument |
| 119 | token, ttl = app_identity.get_access_token( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 120 | self._scopes, self._service_account_id |
| 121 | ) |
Jon Wayne Parrott | 1cba0f8 | 2017-09-12 10:21:02 -0700 | [diff] [blame] | 122 | expiry = datetime.datetime.utcfromtimestamp(ttl) |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 123 | |
| 124 | self.token, self.expiry = token, expiry |
| 125 | |
| 126 | @property |
Jon Wayne Parrott | 61ffb05 | 2016-11-08 09:30:30 -0800 | [diff] [blame] | 127 | def service_account_email(self): |
| 128 | """The service account email.""" |
| 129 | if self._service_account_id is None: |
| 130 | self._service_account_id = app_identity.get_service_account_name() |
| 131 | return self._service_account_id |
| 132 | |
| 133 | @property |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 134 | def requires_scopes(self): |
| 135 | """Checks if the credentials requires scopes. |
| 136 | |
| 137 | Returns: |
| 138 | bool: True if there are no scopes set otherwise False. |
| 139 | """ |
| 140 | return not self._scopes |
| 141 | |
| 142 | @_helpers.copy_docstring(credentials.Scoped) |
| 143 | def with_scopes(self, scopes): |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 144 | return self.__class__( |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 145 | scopes=scopes, |
| 146 | service_account_id=self._service_account_id, |
| 147 | quota_project_id=self.quota_project_id, |
| 148 | ) |
| 149 | |
Bu Sun Kim | 41599ae | 2020-09-02 12:55:42 -0600 | [diff] [blame^] | 150 | @_helpers.copy_docstring(credentials.CredentialsWithQuotaProject) |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 151 | def with_quota_project(self, quota_project_id): |
| 152 | return self.__class__( |
| 153 | scopes=self._scopes, |
| 154 | service_account_id=self._service_account_id, |
| 155 | quota_project_id=quota_project_id, |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 156 | ) |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 157 | |
| 158 | @_helpers.copy_docstring(credentials.Signing) |
| 159 | def sign_bytes(self, message): |
Jon Wayne Parrott | d722167 | 2017-02-16 09:05:11 -0800 | [diff] [blame] | 160 | return self._signer.sign(message) |
Jon Wayne Parrott | 4c883f0 | 2016-12-02 14:26:33 -0800 | [diff] [blame] | 161 | |
| 162 | @property |
| 163 | @_helpers.copy_docstring(credentials.Signing) |
| 164 | def signer_email(self): |
| 165 | return self.service_account_email |
Jon Wayne Parrott | d722167 | 2017-02-16 09:05:11 -0800 | [diff] [blame] | 166 | |
| 167 | @property |
| 168 | @_helpers.copy_docstring(credentials.Signing) |
| 169 | def signer(self): |
| 170 | return self._signer |