OLD | NEW |
(Empty) | |
| 1 # Copyright 2017 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 """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 |
| 23 import json |
| 24 |
| 25 from six.moves import http_client |
| 26 |
| 27 from google.auth import _helpers |
| 28 from google.auth import crypt |
| 29 from google.auth import exceptions |
| 30 |
| 31 _IAM_API_ROOT_URI = 'https://iam.googleapis.com/v1' |
| 32 _SIGN_BLOB_URI = ( |
| 33 _IAM_API_ROOT_URI + '/projects/-/serviceAccounts/{}:signBlob?alt=json') |
| 34 |
| 35 |
| 36 class Signer(crypt.Signer): |
| 37 """Signs messages using the IAM `signBlob API`_. |
| 38 |
| 39 This is useful when you need to sign bytes but do not have access to the |
| 40 credential's private key file. |
| 41 |
| 42 .. _signBlob API: |
| 43 https://cloud.google.com/iam/reference/rest/v1/projects.serviceAccounts |
| 44 /signBlob |
| 45 """ |
| 46 |
| 47 def __init__(self, request, credentials, service_account_email): |
| 48 """ |
| 49 Args: |
| 50 request (google.auth.transport.Request): The object used to make |
| 51 HTTP requests. |
| 52 credentials (google.auth.credentials.Credentials): The credentials |
| 53 that will be used to authenticate the request to the IAM API. |
| 54 The credentials must have of one the following scopes: |
| 55 |
| 56 - https://www.googleapis.com/auth/iam |
| 57 - https://www.googleapis.com/auth/cloud-platform |
| 58 service_account_email (str): The service account email identifying |
| 59 which service account to use to sign bytes. Often, this can |
| 60 be the same as the service account email in the given |
| 61 credentials. |
| 62 """ |
| 63 self._request = request |
| 64 self._credentials = credentials |
| 65 self._service_account_email = service_account_email |
| 66 |
| 67 def _make_signing_request(self, message): |
| 68 """Makes a request to the API signBlob API.""" |
| 69 message = _helpers.to_bytes(message) |
| 70 |
| 71 method = 'POST' |
| 72 url = _SIGN_BLOB_URI.format(self._service_account_email) |
| 73 headers = {} |
| 74 body = json.dumps({ |
| 75 'bytesToSign': base64.b64encode(message).decode('utf-8'), |
| 76 }) |
| 77 |
| 78 self._credentials.before_request(self._request, method, url, headers) |
| 79 response = self._request( |
| 80 url=url, method=method, body=body, headers=headers) |
| 81 |
| 82 if response.status != http_client.OK: |
| 83 raise exceptions.TransportError( |
| 84 'Error calling the IAM signBytes API: {}'.format( |
| 85 response.data)) |
| 86 |
| 87 return json.loads(response.data.decode('utf-8')) |
| 88 |
| 89 @property |
| 90 def key_id(self): |
| 91 """Optional[str]: The key ID used to identify this private key. |
| 92 |
| 93 .. warning:: |
| 94 This is always ``None``. The key ID used by IAM can not |
| 95 be reliably determined ahead of time. |
| 96 """ |
| 97 return None |
| 98 |
| 99 @_helpers.copy_docstring(crypt.Signer) |
| 100 def sign(self, message): |
| 101 response = self._make_signing_request(message) |
| 102 return base64.b64decode(response['signature']) |
OLD | NEW |