You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "dlg99 (via GitHub)" <gi...@apache.org> on 2025/08/14 21:17:22 UTC

[PR] [feat] Pulsar authentication for Client authentication using private_key_jwt method [pulsar]

dlg99 opened a new pull request, #24631:
URL: https://github.com/apache/pulsar/pull/24631

   Fixes #22371
   
   ### Motivation
   
   support private_key_jwt client authentication
   
   ### Modifications
   
   Implemented authentication, added tests
   
   ### Verifying this change
   
   - [ ] Make sure that the change passes the CI checks.
   
   This change added unit tests
   
   ### Does this pull request potentially affect one of the following parts:
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   *If the box was checked, please highlight the changes*
   
   - [x] Dependencies (add or upgrade a dependency)
   - [x] The public API
   - [ ] The schema
   - [ ] The default values of configurations
   - [ ] The threading model
   - [ ] The binary protocol
   - [ ] The REST endpoints
   - [ ] The admin CLI options
   - [ ] The metrics
   - [ ] Anything that affects deployment
   
   ### Documentation
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   - [ ] `doc` <!-- Your PR contains doc changes. -->
   - [x] `doc-required` <!-- Your PR changes impact docs and you will update later -->
   - [ ] `doc-not-needed` <!-- Your PR changes do not impact docs -->
   - [ ] `doc-complete` <!-- Docs have been already added -->
   
   ### Matching PR in forked repository
   
   PR in forked repository: https://github.com/dlg99/pulsar/pull/24
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [feat] Pulsar authentication for Client authentication using private_key_jwt method [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on code in PR #24631:
URL: https://github.com/apache/pulsar/pull/24631#discussion_r2278405222


##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/oauth2/protocol/PrivateKeyReader.java:
##########
@@ -0,0 +1,486 @@
+/*
+ * 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.
+ */
+package org.apache.pulsar.client.impl.auth.oauth2.protocol;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.math.BigInteger;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URLConnection;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.security.KeyFactory;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.security.spec.EncodedKeySpec;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.security.spec.RSAPrivateCrtKeySpec;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.codec.binary.Base64;
+
+/**
+ * Class for reading RSA private key from PEM file. It uses
+ * the JMeter FileServer to find the file. So the file should
+ * be located in the same directory as the test plan if the
+ * path is relative.
+ * <p>
+ * <p/>There is a cache so each file is only read once. If file
+ * is changed, it will not take effect until the program
+ * restarts.
+ * <p>
+ * <p/>It can read PEM files with PKCS#8 or PKCS#1 encodings.
+ * It doesn't support encrypted PEM files.
+ * <p>
+ * "borrowed" from https://github.com/groovenauts/jmeter_oauth_plugin/blob/master/jmeter/
+ * src/main/java/org/apache/jmeter/protocol/oauth/sampler/PrivateKeyReader.java
+ * with some modifications:
+ * - not tied to key specified as a file path
+ * - minus extra dependencies from jmeter
+ * - minus key caching

Review Comment:
   Another approach would be to use Pulsar's existing PEM key support classes and improve them if there's a gap.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [feat] Pulsar authentication for Client authentication using private_key_jwt method [pulsar]

Posted by "merlimat (via GitHub)" <gi...@apache.org>.
merlimat commented on PR #24631:
URL: https://github.com/apache/pulsar/pull/24631#issuecomment-6182200996

   @dlg99 thanks for working on this. The PR was left unfinished, with lhotari's review feedback still open, and the build has since moved from Maven to Gradle. #25538 has since added tls_client_auth to AuthenticationOAuth2, and as lhotari noted on #22371, private_key_jwt should be easier to add on top of it; #22371 stays open to track the feature. We're going through the backlog of open PRs (https://lists.apache.org/thread/cgy7w0qgnq8s7j0ykyzgrn3hj4nb67wf), so I'm closing this one. If you'd like to continue, feel free to reopen it rebased on current master.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [feat] Pulsar authentication for Client authentication using private_key_jwt method [pulsar]

Posted by "merlimat (via GitHub)" <gi...@apache.org>.
merlimat closed pull request #24631: [feat] Pulsar authentication for Client authentication using private_key_jwt method
URL: https://github.com/apache/pulsar/pull/24631


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [feat] Pulsar authentication for Client authentication using private_key_jwt method [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on code in PR #24631:
URL: https://github.com/apache/pulsar/pull/24631#discussion_r2278369411


##########
pom.xml:
##########
@@ -1727,6 +1718,26 @@ flexible messaging model and an intuitive client API.</description>
         <artifactId>opentelemetry-semconv</artifactId>
         <version>${opentelemetry.semconv.version}</version>
       </dependency>
+
+      <!-- JJWT -->
+      <dependency>
+        <groupId>io.jsonwebtoken</groupId>
+        <artifactId>jjwt-api</artifactId>
+        <version>${jsonwebtoken.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>io.jsonwebtoken</groupId>
+        <artifactId>jjwt-impl</artifactId>
+        <version>${jsonwebtoken.version}</version>
+        <scope>runtime</scope>
+      </dependency>
+      <dependency>
+        <groupId>io.jsonwebtoken</groupId>
+        <artifactId>jjwt-jackson</artifactId> <!-- or jjwt-gson if Gson is preferred -->
+        <version>${jsonwebtoken.version}</version>
+        <scope>runtime</scope>

Review Comment:
   remove scope



##########
pom.xml:
##########
@@ -1727,6 +1718,26 @@ flexible messaging model and an intuitive client API.</description>
         <artifactId>opentelemetry-semconv</artifactId>
         <version>${opentelemetry.semconv.version}</version>
       </dependency>
+
+      <!-- JJWT -->
+      <dependency>
+        <groupId>io.jsonwebtoken</groupId>
+        <artifactId>jjwt-api</artifactId>
+        <version>${jsonwebtoken.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>io.jsonwebtoken</groupId>
+        <artifactId>jjwt-impl</artifactId>
+        <version>${jsonwebtoken.version}</version>
+        <scope>runtime</scope>

Review Comment:
   scope shouldn't be added to dependencyManagement dependencies since dependencyManagement's purpose is to pin the version of a dependency, regardless of the scope of the actual dependency.



##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/oauth2/protocol/PrivateKeyReader.java:
##########
@@ -0,0 +1,486 @@
+/*
+ * 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.
+ */
+package org.apache.pulsar.client.impl.auth.oauth2.protocol;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.math.BigInteger;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URLConnection;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.security.KeyFactory;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.security.spec.EncodedKeySpec;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.security.spec.RSAPrivateCrtKeySpec;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.codec.binary.Base64;
+
+/**
+ * Class for reading RSA private key from PEM file. It uses
+ * the JMeter FileServer to find the file. So the file should
+ * be located in the same directory as the test plan if the
+ * path is relative.
+ * <p>
+ * <p/>There is a cache so each file is only read once. If file
+ * is changed, it will not take effect until the program
+ * restarts.
+ * <p>
+ * <p/>It can read PEM files with PKCS#8 or PKCS#1 encodings.
+ * It doesn't support encrypted PEM files.
+ * <p>
+ * "borrowed" from https://github.com/groovenauts/jmeter_oauth_plugin/blob/master/jmeter/
+ * src/main/java/org/apache/jmeter/protocol/oauth/sampler/PrivateKeyReader.java
+ * with some modifications:
+ * - not tied to key specified as a file path
+ * - minus extra dependencies from jmeter
+ * - minus key caching

Review Comment:
   There's a copyright issue in "borrowing", although it's ASL 2.0 licensed. I think it would be better to use a similar approach as is used in Zookeeper, https://github.com/apache/zookeeper/blob/master/zookeeper-server/src/main/java/org/apache/zookeeper/util/PemReader.java . That is from another ASF project, https://github.com/airlift/airlift/blob/master/security/src/main/java/io/airlift/security/pem/PemReader.java . 
   The benefit would also be that it would support also EC keys which are commonly preferred over RSA keys.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org