C.J. Collier | 37141e4 | 2020-02-13 13:49:49 -0800 | [diff] [blame] | 1 | # Copyright 2017 Google LLC |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [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 | |
| 15 | """Tools for using the Google `Cloud Identity and Access Management (IAM) |
| 16 | API`_'s auth-related functionality. |
| 17 | |
| 18 | .. _Cloud Identity and Access Management (IAM) API: |
| 19 | https://cloud.google.com/iam/docs/ |
| 20 | """ |
| 21 | |
| 22 | import base64 |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 23 | import http.client |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 24 | import json |
| 25 | |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 26 | from google.auth import _helpers |
Jon Wayne Parrott | 254befe | 2017-02-22 14:37:31 -0800 | [diff] [blame] | 27 | from google.auth import crypt |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 28 | from google.auth import exceptions |
| 29 | |
Bu Sun Kim | 694d83f | 2020-09-08 15:28:08 -0600 | [diff] [blame] | 30 | _IAM_API_ROOT_URI = "https://iamcredentials.googleapis.com/v1" |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 31 | _SIGN_BLOB_URI = _IAM_API_ROOT_URI + "/projects/-/serviceAccounts/{}:signBlob?alt=json" |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 32 | |
| 33 | |
Jon Wayne Parrott | 254befe | 2017-02-22 14:37:31 -0800 | [diff] [blame] | 34 | class Signer(crypt.Signer): |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 35 | """Signs messages using the IAM `signBlob API`_. |
| 36 | |
| 37 | This is useful when you need to sign bytes but do not have access to the |
| 38 | credential's private key file. |
| 39 | |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 40 | .. _signBlob API: |
| 41 | https://cloud.google.com/iam/reference/rest/v1/projects.serviceAccounts |
| 42 | /signBlob |
| 43 | """ |
| 44 | |
| 45 | def __init__(self, request, credentials, service_account_email): |
| 46 | """ |
| 47 | Args: |
| 48 | request (google.auth.transport.Request): The object used to make |
| 49 | HTTP requests. |
| 50 | credentials (google.auth.credentials.Credentials): The credentials |
| 51 | that will be used to authenticate the request to the IAM API. |
| 52 | The credentials must have of one the following scopes: |
| 53 | |
| 54 | - https://www.googleapis.com/auth/iam |
| 55 | - https://www.googleapis.com/auth/cloud-platform |
| 56 | service_account_email (str): The service account email identifying |
| 57 | which service account to use to sign bytes. Often, this can |
| 58 | be the same as the service account email in the given |
| 59 | credentials. |
| 60 | """ |
| 61 | self._request = request |
| 62 | self._credentials = credentials |
| 63 | self._service_account_email = service_account_email |
| 64 | |
| 65 | def _make_signing_request(self, message): |
| 66 | """Makes a request to the API signBlob API.""" |
| 67 | message = _helpers.to_bytes(message) |
| 68 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 69 | method = "POST" |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 70 | url = _SIGN_BLOB_URI.format(self._service_account_email) |
Kenji Imamula | 20f82e2 | 2020-07-31 05:40:02 +0900 | [diff] [blame] | 71 | headers = {"Content-Type": "application/json"} |
Bu Sun Kim | a57a770 | 2020-01-10 13:17:34 -0800 | [diff] [blame] | 72 | body = json.dumps( |
Bu Sun Kim | 694d83f | 2020-09-08 15:28:08 -0600 | [diff] [blame] | 73 | {"payload": base64.b64encode(message).decode("utf-8")} |
Bu Sun Kim | a57a770 | 2020-01-10 13:17:34 -0800 | [diff] [blame] | 74 | ).encode("utf-8") |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 75 | |
| 76 | self._credentials.before_request(self._request, method, url, headers) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 77 | response = self._request(url=url, method=method, body=body, headers=headers) |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 78 | |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 79 | if response.status != http.client.OK: |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 80 | raise exceptions.TransportError( |
Aarni Koskela | e9ca25f | 2021-05-18 18:08:05 +0300 | [diff] [blame] | 81 | "Error calling the IAM signBlob API: {}".format(response.data) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 82 | ) |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 83 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 84 | return json.loads(response.data.decode("utf-8")) |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 85 | |
| 86 | @property |
| 87 | def key_id(self): |
| 88 | """Optional[str]: The key ID used to identify this private key. |
| 89 | |
Jon Wayne Parrott | 254befe | 2017-02-22 14:37:31 -0800 | [diff] [blame] | 90 | .. warning:: |
| 91 | This is always ``None``. The key ID used by IAM can not |
| 92 | be reliably determined ahead of time. |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 93 | """ |
Jon Wayne Parrott | 254befe | 2017-02-22 14:37:31 -0800 | [diff] [blame] | 94 | return None |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 95 | |
Jon Wayne Parrott | 254befe | 2017-02-22 14:37:31 -0800 | [diff] [blame] | 96 | @_helpers.copy_docstring(crypt.Signer) |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 97 | def sign(self, message): |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 98 | response = self._make_signing_request(message) |
Bu Sun Kim | 694d83f | 2020-09-08 15:28:08 -0600 | [diff] [blame] | 99 | return base64.b64decode(response["signedBlob"]) |