From e3a37d984390760fbfc6645af336c7586c5c6f96 Mon Sep 17 00:00:00 2001 From: guangning Date: Sat, 15 Jan 2022 21:00:10 +0800 Subject: [PATCH 1/4] Add oauth2 example for node js client --- cloud/node/connect_by_oauth2.js | 61 +++++++++++++++++++ ...onnect_by_toekn.js => connect_by_token.js} | 0 2 files changed, 61 insertions(+) create mode 100644 cloud/node/connect_by_oauth2.js rename cloud/node/{connect_by_toekn.js => connect_by_token.js} (100%) diff --git a/cloud/node/connect_by_oauth2.js b/cloud/node/connect_by_oauth2.js new file mode 100644 index 0000000..3ba9abc --- /dev/null +++ b/cloud/node/connect_by_oauth2.js @@ -0,0 +1,61 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +const Pulsar = require('pulsar-client'); + +const issuer_url = process.env.ISSER_URL; +const private_key = process.env.PRIVATE_KEY; +const audience = process.env.AUDIENCE; +const scope = process.env.SCOPE; +const service_url = process.env.SERVICE_URL; +const client_id = process.env.CLIENT_ID; +const client_secret = process.env.CLIENT_SECRET; + +(async () => { + const params = { + issuer_url: issuer_url + } + if (private_key.length > 0) { + params['private_key'] = private_key + } else { + params['client_id'] = client_id + params['client_secret'] = client_secret + } + if (audience.length > 0) { + params['audience'] = audience + } + if (scope.length > 0) { + params['scope'] = scope + } + const auth = new Pulsar.AuthenticationOauth2({ + issuer_url: issuer_url, + private_key: private_key, + audience: audience, + }); + + // Create a client + const client = new Pulsar.Client({ + serviceUrl: service_url, + tlsAllowInsecureConnection: true, + authentication: auth, + }); + + await client.close(); +})(); + \ No newline at end of file diff --git a/cloud/node/connect_by_toekn.js b/cloud/node/connect_by_token.js similarity index 100% rename from cloud/node/connect_by_toekn.js rename to cloud/node/connect_by_token.js From 0f268756e172ff643ae52cad487037cbe3ff146b Mon Sep 17 00:00:00 2001 From: guangning Date: Sat, 15 Jan 2022 21:09:00 +0800 Subject: [PATCH 2/4] Update readme and example --- cloud/node/README.md | 11 ++++ cloud/node/connect_by_oauth2.js | 6 +-- cloud/node/sample_consumer_by_oauth2.js | 70 ++++++++++++++++++++++++ cloud/node/sample_producer_by_oauth2.js | 72 +++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 5 deletions(-) create mode 100644 cloud/node/sample_consumer_by_oauth2.js create mode 100644 cloud/node/sample_producer_by_oauth2.js diff --git a/cloud/node/README.md b/cloud/node/README.md index 79f03fd..ff7b84d 100644 --- a/cloud/node/README.md +++ b/cloud/node/README.md @@ -75,3 +75,14 @@ The content of each message payload is a combination of `my-message-` and a digi Sent message: my-message-8 Sent message: my-message-9 ``` + +4. Run the Node.js producer to publish messages to the topic `my-topic` by the oauth2. + + ```bash + cd cloud/node + export ISSER_URL = "" + export PRIVATE_KEY = "" + export AUDIENCE = "" + export SERVICE_URL = "" + ``` + diff --git a/cloud/node/connect_by_oauth2.js b/cloud/node/connect_by_oauth2.js index 3ba9abc..22399ee 100644 --- a/cloud/node/connect_by_oauth2.js +++ b/cloud/node/connect_by_oauth2.js @@ -43,11 +43,7 @@ const client_secret = process.env.CLIENT_SECRET; if (scope.length > 0) { params['scope'] = scope } - const auth = new Pulsar.AuthenticationOauth2({ - issuer_url: issuer_url, - private_key: private_key, - audience: audience, - }); + const auth = new Pulsar.AuthenticationOauth2(params); // Create a client const client = new Pulsar.Client({ diff --git a/cloud/node/sample_consumer_by_oauth2.js b/cloud/node/sample_consumer_by_oauth2.js new file mode 100644 index 0000000..2fa1971 --- /dev/null +++ b/cloud/node/sample_consumer_by_oauth2.js @@ -0,0 +1,70 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +const Pulsar = require('pulsar-client'); + +const issuer_url = process.env.ISSER_URL; +const private_key = process.env.PRIVATE_KEY; +const audience = process.env.AUDIENCE; +const scope = process.env.SCOPE; +const service_url = process.env.SERVICE_URL; +const client_id = process.env.CLIENT_ID; +const client_secret = process.env.CLIENT_SECRET; + +(async () => { + const params = { + issuer_url: issuer_url + } + if (private_key.length > 0) { + params['private_key'] = private_key + } else { + params['client_id'] = client_id + params['client_secret'] = client_secret + } + if (audience.length > 0) { + params['audience'] = audience + } + if (scope.length > 0) { + params['scope'] = scope + } + const auth = new Pulsar.AuthenticationOauth2(params); + + // Create a client + const client = new Pulsar.Client({ + serviceUrl: service_url, + authentication: auth, + operationTimeoutSeconds: 30, + }); + + // Create a consumer + const consumer = await client.subscribe({ + topic: 'persistent://public/default/my-topic', + subscription: 'sub1', + subscriptionType: 'Shared', + ackTimeoutMs: 10000, + }); + + // Receive messages + for (let i = 0; i < 10; i += 1) { + const msg = await consumer.receive(); + console.log(msg.getData().toString()); + consumer.acknowledge(msg); + } + + await consumer.close(); + await client.close(); +})(); diff --git a/cloud/node/sample_producer_by_oauth2.js b/cloud/node/sample_producer_by_oauth2.js new file mode 100644 index 0000000..9b43ea6 --- /dev/null +++ b/cloud/node/sample_producer_by_oauth2.js @@ -0,0 +1,72 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +const Pulsar = require('pulsar-client'); + +const issuer_url = process.env.ISSER_URL; +const private_key = process.env.PRIVATE_KEY; +const audience = process.env.AUDIENCE; +const scope = process.env.SCOPE; +const service_url = process.env.SERVICE_URL; +const client_id = process.env.CLIENT_ID; +const client_secret = process.env.CLIENT_SECRET; + +(async () => { + const params = { + issuer_url: issuer_url + } + if (private_key.length > 0) { + params['private_key'] = private_key + } else { + params['client_id'] = client_id + params['client_secret'] = client_secret + } + if (audience.length > 0) { + params['audience'] = audience + } + if (scope.length > 0) { + params['scope'] = scope + } + const auth = new Pulsar.AuthenticationOauth2(params); + + // Create a client + const client = new Pulsar.Client({ + serviceUrl: service_url, + authentication: auth, + operationTimeoutSeconds: 30, + }); + + // Create a producer + const producer = await client.createProducer({ + topic: 'persistent://public/default/my-topic', + sendTimeoutMs: 30000, + batchingEnabled: true, + }); + + // Send messages + for (let i = 0; i < 10; i += 1) { + const msg = `my-message-${i}`; + producer.send({ + data: Buffer.from(msg), + }); + console.log(`Sent message: ${msg}`); + } + await producer.flush(); + + await producer.close(); + await client.close(); +})(); From d3bf24b91deacfb817f8a91dc980d551cc9e6390 Mon Sep 17 00:00:00 2001 From: guangning Date: Sat, 15 Jan 2022 21:13:03 +0800 Subject: [PATCH 3/4] Fixed oauth2 commit --- cloud/node/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/cloud/node/README.md b/cloud/node/README.md index ff7b84d..b0b8c08 100644 --- a/cloud/node/README.md +++ b/cloud/node/README.md @@ -84,5 +84,6 @@ The content of each message payload is a combination of `my-message-` and a digi export PRIVATE_KEY = "" export AUDIENCE = "" export SERVICE_URL = "" + node sample_producer_by_oauth2.js ``` From 3396f7669fe4b94d37f99a750700265977d6776b Mon Sep 17 00:00:00 2001 From: guangning Date: Fri, 11 Mar 2022 14:20:48 +0800 Subject: [PATCH 4/4] Update cloud node examples --- cloud/node/README.md | 4 ++-- cloud/node/connect_by_oauth2.js | 3 +-- cloud/node/sample_consumer_by_oauth2.js | 2 +- cloud/node/sample_producer_by_oauth2.js | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/cloud/node/README.md b/cloud/node/README.md index b0b8c08..7248bc3 100644 --- a/cloud/node/README.md +++ b/cloud/node/README.md @@ -76,11 +76,11 @@ The content of each message payload is a combination of `my-message-` and a digi Sent message: my-message-9 ``` -4. Run the Node.js producer to publish messages to the topic `my-topic` by the oauth2. +4. Run the Node.js producer to publish messages to the topic `my-topic` by OAuth2. ```bash cd cloud/node - export ISSER_URL = "" + export ISSUER_URL = "" export PRIVATE_KEY = "" export AUDIENCE = "" export SERVICE_URL = "" diff --git a/cloud/node/connect_by_oauth2.js b/cloud/node/connect_by_oauth2.js index 22399ee..7698d00 100644 --- a/cloud/node/connect_by_oauth2.js +++ b/cloud/node/connect_by_oauth2.js @@ -19,7 +19,7 @@ const Pulsar = require('pulsar-client'); -const issuer_url = process.env.ISSER_URL; +const issuer_url = process.env.ISSUER_URL; const private_key = process.env.PRIVATE_KEY; const audience = process.env.AUDIENCE; const scope = process.env.SCOPE; @@ -48,7 +48,6 @@ const client_secret = process.env.CLIENT_SECRET; // Create a client const client = new Pulsar.Client({ serviceUrl: service_url, - tlsAllowInsecureConnection: true, authentication: auth, }); diff --git a/cloud/node/sample_consumer_by_oauth2.js b/cloud/node/sample_consumer_by_oauth2.js index 2fa1971..36a9156 100644 --- a/cloud/node/sample_consumer_by_oauth2.js +++ b/cloud/node/sample_consumer_by_oauth2.js @@ -17,7 +17,7 @@ const Pulsar = require('pulsar-client'); -const issuer_url = process.env.ISSER_URL; +const issuer_url = process.env.ISSUER_URL; const private_key = process.env.PRIVATE_KEY; const audience = process.env.AUDIENCE; const scope = process.env.SCOPE; diff --git a/cloud/node/sample_producer_by_oauth2.js b/cloud/node/sample_producer_by_oauth2.js index 9b43ea6..f9cc605 100644 --- a/cloud/node/sample_producer_by_oauth2.js +++ b/cloud/node/sample_producer_by_oauth2.js @@ -17,7 +17,7 @@ const Pulsar = require('pulsar-client'); -const issuer_url = process.env.ISSER_URL; +const issuer_url = process.env.ISSUER_URL; const private_key = process.env.PRIVATE_KEY; const audience = process.env.AUDIENCE; const scope = process.env.SCOPE;