blob: d83b25180afc4608ed118972e0e73af3979ab296 [file] [log] [blame]
C.J. Collier37141e42020-02-13 13:49:49 -08001# Copyright 2017 Google LLC
Jon Wayne Parrott924191c2017-02-15 16:43:23 -08002#
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)
16API`_'s auth-related functionality.
17
18.. _Cloud Identity and Access Management (IAM) API:
19 https://cloud.google.com/iam/docs/
20"""
21
22import base64
23import json
24
25from six.moves import http_client
26
27from google.auth import _helpers
Jon Wayne Parrott254befe2017-02-22 14:37:31 -080028from google.auth import crypt
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080029from google.auth import exceptions
30
Bu Sun Kima48b5b92020-07-17 10:24:02 -070031_IAM_API_ROOT_URI = "https://iam.googleapis.com/v1"
Bu Sun Kim9eec0912019-10-21 17:04:21 -070032_SIGN_BLOB_URI = _IAM_API_ROOT_URI + "/projects/-/serviceAccounts/{}:signBlob?alt=json"
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080033
34
Jon Wayne Parrott254befe2017-02-22 14:37:31 -080035class Signer(crypt.Signer):
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080036 """Signs messages using the IAM `signBlob API`_.
37
38 This is useful when you need to sign bytes but do not have access to the
39 credential's private key file.
40
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080041 .. _signBlob API:
42 https://cloud.google.com/iam/reference/rest/v1/projects.serviceAccounts
43 /signBlob
44 """
45
46 def __init__(self, request, credentials, service_account_email):
47 """
48 Args:
49 request (google.auth.transport.Request): The object used to make
50 HTTP requests.
51 credentials (google.auth.credentials.Credentials): The credentials
52 that will be used to authenticate the request to the IAM API.
53 The credentials must have of one the following scopes:
54
55 - https://www.googleapis.com/auth/iam
56 - https://www.googleapis.com/auth/cloud-platform
57 service_account_email (str): The service account email identifying
58 which service account to use to sign bytes. Often, this can
59 be the same as the service account email in the given
60 credentials.
61 """
62 self._request = request
63 self._credentials = credentials
64 self._service_account_email = service_account_email
65
66 def _make_signing_request(self, message):
67 """Makes a request to the API signBlob API."""
68 message = _helpers.to_bytes(message)
69
Bu Sun Kim9eec0912019-10-21 17:04:21 -070070 method = "POST"
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080071 url = _SIGN_BLOB_URI.format(self._service_account_email)
Kenji Imamula20f82e22020-07-31 05:40:02 +090072 headers = {"Content-Type": "application/json"}
Bu Sun Kima57a7702020-01-10 13:17:34 -080073 body = json.dumps(
Bu Sun Kima48b5b92020-07-17 10:24:02 -070074 {"bytesToSign": base64.b64encode(message).decode("utf-8")}
Bu Sun Kima57a7702020-01-10 13:17:34 -080075 ).encode("utf-8")
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080076
77 self._credentials.before_request(self._request, method, url, headers)
Bu Sun Kim9eec0912019-10-21 17:04:21 -070078 response = self._request(url=url, method=method, body=body, headers=headers)
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080079
80 if response.status != http_client.OK:
81 raise exceptions.TransportError(
Bu Sun Kim9eec0912019-10-21 17:04:21 -070082 "Error calling the IAM signBytes API: {}".format(response.data)
83 )
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080084
Bu Sun Kim9eec0912019-10-21 17:04:21 -070085 return json.loads(response.data.decode("utf-8"))
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080086
87 @property
88 def key_id(self):
89 """Optional[str]: The key ID used to identify this private key.
90
Jon Wayne Parrott254befe2017-02-22 14:37:31 -080091 .. warning::
92 This is always ``None``. The key ID used by IAM can not
93 be reliably determined ahead of time.
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080094 """
Jon Wayne Parrott254befe2017-02-22 14:37:31 -080095 return None
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080096
Jon Wayne Parrott254befe2017-02-22 14:37:31 -080097 @_helpers.copy_docstring(crypt.Signer)
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080098 def sign(self, message):
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080099 response = self._make_signing_request(message)
Bu Sun Kima48b5b92020-07-17 10:24:02 -0700100 return base64.b64decode(response["signature"])