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 | import base64 |
| 16 | import datetime |
| 17 | import json |
| 18 | |
| 19 | import mock |
| 20 | import pytest |
| 21 | from six.moves import http_client |
| 22 | |
Jon Wayne Parrott | 7af9f66 | 2017-05-08 09:40:56 -0700 | [diff] [blame] | 23 | from google.auth import _helpers |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 24 | from google.auth import exceptions |
| 25 | from google.auth import iam |
| 26 | from google.auth import transport |
| 27 | import google.auth.credentials |
| 28 | |
| 29 | |
| 30 | def make_request(status, data=None): |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 31 | response = mock.create_autospec(transport.Response, instance=True) |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 32 | response.status = status |
| 33 | |
| 34 | if data is not None: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 35 | response.data = json.dumps(data).encode("utf-8") |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 36 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 37 | request = mock.create_autospec(transport.Request) |
| 38 | request.return_value = response |
| 39 | return request |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def make_credentials(): |
| 43 | class CredentialsImpl(google.auth.credentials.Credentials): |
| 44 | def __init__(self): |
| 45 | super(CredentialsImpl, self).__init__() |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 46 | self.token = "token" |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 47 | # Force refresh |
Jon Wayne Parrott | 7af9f66 | 2017-05-08 09:40:56 -0700 | [diff] [blame] | 48 | self.expiry = datetime.datetime.min + _helpers.CLOCK_SKEW |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 49 | |
| 50 | def refresh(self, request): |
| 51 | pass |
| 52 | |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 53 | def with_quota_project(self, quota_project_id): |
| 54 | raise NotImplementedError() |
| 55 | |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 56 | return CredentialsImpl() |
| 57 | |
| 58 | |
| 59 | class TestSigner(object): |
| 60 | def test_constructor(self): |
| 61 | request = mock.sentinel.request |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 62 | credentials = mock.create_autospec( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 63 | google.auth.credentials.Credentials, instance=True |
| 64 | ) |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 65 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 66 | signer = iam.Signer(request, credentials, mock.sentinel.service_account_email) |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 67 | |
| 68 | assert signer._request == mock.sentinel.request |
| 69 | assert signer._credentials == credentials |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 70 | assert signer._service_account_email == mock.sentinel.service_account_email |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 71 | |
| 72 | def test_key_id(self): |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 73 | signer = iam.Signer( |
Jon Wayne Parrott | 254befe | 2017-02-22 14:37:31 -0800 | [diff] [blame] | 74 | mock.sentinel.request, |
| 75 | mock.sentinel.credentials, |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 76 | mock.sentinel.service_account_email, |
| 77 | ) |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 78 | |
Jon Wayne Parrott | 254befe | 2017-02-22 14:37:31 -0800 | [diff] [blame] | 79 | assert signer.key_id is None |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 80 | |
| 81 | def test_sign_bytes(self): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 82 | signature = b"DEADBEEF" |
| 83 | encoded_signature = base64.b64encode(signature).decode("utf-8") |
Bu Sun Kim | a48b5b9 | 2020-07-17 10:24:02 -0700 | [diff] [blame] | 84 | request = make_request(http_client.OK, data={"signature": encoded_signature}) |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 85 | credentials = make_credentials() |
| 86 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 87 | signer = iam.Signer(request, credentials, mock.sentinel.service_account_email) |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 88 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 89 | returned_signature = signer.sign("123") |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 90 | |
| 91 | assert returned_signature == signature |
Kenji Imamula | 20f82e2 | 2020-07-31 05:40:02 +0900 | [diff] [blame^] | 92 | kwargs = request.call_args.kwargs |
| 93 | assert kwargs["headers"]["Content-Type"] == "application/json" |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 94 | |
| 95 | def test_sign_bytes_failure(self): |
| 96 | request = make_request(http_client.UNAUTHORIZED) |
| 97 | credentials = make_credentials() |
| 98 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 99 | signer = iam.Signer(request, credentials, mock.sentinel.service_account_email) |
Jon Wayne Parrott | 924191c | 2017-02-15 16:43:23 -0800 | [diff] [blame] | 100 | |
| 101 | with pytest.raises(exceptions.TransportError): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 102 | signer.sign("123") |