|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | +# |
| 20 | + |
| 21 | +from unittest import TestCase, main |
| 22 | +from pulsar import AuthenticationOauth2, AuthenticationError, Client |
| 23 | +import base64 |
| 24 | +import os |
| 25 | + |
| 26 | +# This test should run against the standalone that is set up with |
| 27 | +# build-support/docker-compose-pulsar-oauth2.yml |
| 28 | +class Oauth2Test(TestCase): |
| 29 | + |
| 30 | + service_url = 'pulsar://localhost:6650' |
| 31 | + |
| 32 | + def test_invalid_private_key(self): |
| 33 | + def test_create_client(auth_params_string): |
| 34 | + client = Client(self.service_url, authentication=AuthenticationOauth2(auth_params_string)) |
| 35 | + with self.assertRaises(AuthenticationError): |
| 36 | + client.create_producer('oauth2-test-base64') |
| 37 | + client.close() |
| 38 | + |
| 39 | + test_create_client('{"private_key":"xxx:yyy"}') |
| 40 | + test_create_client('{"private_key":"data:"}') |
| 41 | + test_create_client('{"private_key":"data:application/x-pem"}') |
| 42 | + test_create_client('{"private_key":"data:application/json;xxx"}') |
| 43 | + |
| 44 | + def test_key_file(self): |
| 45 | + path = (os.path.dirname(os.path.abspath(__file__)) |
| 46 | + + '/test-conf/cpp_credentials_file.json') |
| 47 | + auth = AuthenticationOauth2(f'''{{ |
| 48 | + "issuer_url": "https://dev-kt-aa9ne.us.auth0.com", |
| 49 | + "private_key": "{path}", |
| 50 | + "audience": "https://dev-kt-aa9ne.us.auth0.com/api/v2/" |
| 51 | + }}''') |
| 52 | + client = Client(self.service_url, authentication=auth) |
| 53 | + producer = client.create_producer('oauth2-test-base64') |
| 54 | + producer.close() |
| 55 | + client.close() |
| 56 | + |
| 57 | + def test_base64(self): |
| 58 | + credentials = '''{ |
| 59 | + "client_id":"Xd23RHsUnvUlP7wchjNYOaIfazgeHd9x", |
| 60 | + "client_secret":"rT7ps7WY8uhdVuBTKWZkttwLdQotmdEliaM5rLfmgNibvqziZ-g07ZH52N_poGAb" |
| 61 | + }''' |
| 62 | + base64_credentials = base64.b64encode(credentials.encode()).decode() |
| 63 | + auth = AuthenticationOauth2(f'''{{ |
| 64 | + "issuer_url": "https://dev-kt-aa9ne.us.auth0.com", |
| 65 | + "private_key": "data:application/json;base64,{base64_credentials}", |
| 66 | + "audience": "https://dev-kt-aa9ne.us.auth0.com/api/v2/" |
| 67 | + }}''') |
| 68 | + client = Client(self.service_url, authentication=auth) |
| 69 | + producer = client.create_producer('oauth2-test-base64') |
| 70 | + producer.close() |
| 71 | + client.close() |
| 72 | + |
| 73 | + def test_wrong_secret(self): |
| 74 | + credentials = '''{ |
| 75 | + "client_id": "my-id", |
| 76 | + "client_secret":"my-secret" |
| 77 | + }''' |
| 78 | + base64_credentials = base64.b64encode(credentials.encode()).decode() |
| 79 | + auth = AuthenticationOauth2(f'''{{ |
| 80 | + "issuer_url": "https://dev-kt-aa9ne.us.auth0.com", |
| 81 | + "private_key": "data:application/json;base64,{base64_credentials}", |
| 82 | + "audience": "https://dev-kt-aa9ne.us.auth0.com/api/v2/" |
| 83 | + }}''') |
| 84 | + client = Client(self.service_url, authentication=auth) |
| 85 | + with self.assertRaises(AuthenticationError): |
| 86 | + client.create_producer('oauth2-test-base64') |
| 87 | + client.close() |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + main() |
0 commit comments