Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame] | 1 | # Copyright 2016 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 | import google.auth |
| 16 | import google.auth.credentials |
Jon Wayne Parrott | b8f48d0 | 2017-02-24 09:03:24 -0800 | [diff] [blame] | 17 | import google.auth.jwt |
Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame] | 18 | import google.auth.transport.grpc |
Jon Wayne Parrott | a209819 | 2017-02-22 09:27:32 -0800 | [diff] [blame] | 19 | from google.cloud.gapic.pubsub.v1 import publisher_client |
Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame] | 20 | |
| 21 | |
Jon Wayne Parrott | a209819 | 2017-02-22 09:27:32 -0800 | [diff] [blame] | 22 | def test_grpc_request_with_regular_credentials(http_request): |
Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame] | 23 | credentials, project_id = google.auth.default() |
| 24 | credentials = google.auth.credentials.with_scopes_if_required( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame^] | 25 | credentials, ["https://www.googleapis.com/auth/pubsub"] |
| 26 | ) |
Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame] | 27 | |
Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame] | 28 | channel = google.auth.transport.grpc.secure_authorized_channel( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame^] | 29 | credentials, http_request, publisher_client.PublisherClient.SERVICE_ADDRESS |
| 30 | ) |
Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame] | 31 | |
| 32 | # Create a pub/sub client. |
Jon Wayne Parrott | a209819 | 2017-02-22 09:27:32 -0800 | [diff] [blame] | 33 | client = publisher_client.PublisherClient(channel=channel) |
| 34 | |
| 35 | # list the topics and drain the iterator to test that an authorized API |
| 36 | # call works. |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame^] | 37 | list_topics_iter = client.list_topics(project="projects/{}".format(project_id)) |
Jon Wayne Parrott | a209819 | 2017-02-22 09:27:32 -0800 | [diff] [blame] | 38 | list(list_topics_iter) |
| 39 | |
| 40 | |
Jon Wayne Parrott | cfbfd25 | 2017-03-28 13:03:11 -0700 | [diff] [blame] | 41 | def test_grpc_request_with_jwt_credentials(): |
Jon Wayne Parrott | a209819 | 2017-02-22 09:27:32 -0800 | [diff] [blame] | 42 | credentials, project_id = google.auth.default() |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame^] | 43 | audience = "https://{}/google.pubsub.v1.Publisher".format( |
| 44 | publisher_client.PublisherClient.SERVICE_ADDRESS |
| 45 | ) |
Jon Wayne Parrott | b8f48d0 | 2017-02-24 09:03:24 -0800 | [diff] [blame] | 46 | credentials = google.auth.jwt.Credentials.from_signing_credentials( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame^] | 47 | credentials, audience=audience |
| 48 | ) |
Jon Wayne Parrott | a209819 | 2017-02-22 09:27:32 -0800 | [diff] [blame] | 49 | |
| 50 | channel = google.auth.transport.grpc.secure_authorized_channel( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame^] | 51 | credentials, None, publisher_client.PublisherClient.SERVICE_ADDRESS |
| 52 | ) |
Jon Wayne Parrott | cfbfd25 | 2017-03-28 13:03:11 -0700 | [diff] [blame] | 53 | |
| 54 | # Create a pub/sub client. |
| 55 | client = publisher_client.PublisherClient(channel=channel) |
| 56 | |
| 57 | # list the topics and drain the iterator to test that an authorized API |
| 58 | # call works. |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame^] | 59 | list_topics_iter = client.list_topics(project="projects/{}".format(project_id)) |
Jon Wayne Parrott | cfbfd25 | 2017-03-28 13:03:11 -0700 | [diff] [blame] | 60 | list(list_topics_iter) |
| 61 | |
| 62 | |
| 63 | def test_grpc_request_with_on_demand_jwt_credentials(): |
| 64 | credentials, project_id = google.auth.default() |
| 65 | credentials = google.auth.jwt.OnDemandCredentials.from_signing_credentials( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame^] | 66 | credentials |
| 67 | ) |
Jon Wayne Parrott | cfbfd25 | 2017-03-28 13:03:11 -0700 | [diff] [blame] | 68 | |
| 69 | channel = google.auth.transport.grpc.secure_authorized_channel( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame^] | 70 | credentials, None, publisher_client.PublisherClient.SERVICE_ADDRESS |
| 71 | ) |
Jon Wayne Parrott | a209819 | 2017-02-22 09:27:32 -0800 | [diff] [blame] | 72 | |
| 73 | # Create a pub/sub client. |
| 74 | client = publisher_client.PublisherClient(channel=channel) |
Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame] | 75 | |
| 76 | # list the topics and drain the iterator to test that an authorized API |
| 77 | # call works. |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame^] | 78 | list_topics_iter = client.list_topics(project="projects/{}".format(project_id)) |
Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame] | 79 | list(list_topics_iter) |