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