Connecting to Protocol Handler with OAuth2

In the following exercise, we connect a Kafka application that uses a generic AVRO schema to a StreamNative Hosted Pulsar cluster using OAuth2. We will continue this exercise by connecting to the schema registry with a JWT Token or OAuth2 in the next section. The video demonstrates this using a client.properties file.

The example uses the following code:

  • KSN-Training/src/main/java/sn_training/Schema/KAVROProducer.java
  • KSN-Training/src/main/java/sn_training/Schema/KAVROConsumer.java
  • KSN-Training/src/main/java/sn_training/resources/client.properties.schema

For an example configuring the connection with OAuth2 directly in the Java code, see SchemaV2.


Update client.properties.schema file so topic1 uses your student id (e.g. kafkastudent3-schemainput).

Make the following changes to client.properties.schema to convert your application to connect to the Pulsar cluster:

  • edit bootstrap.servers to point to the Kafka endpoint on port 9093
  • edit sasl.mechanism to be OAUTHBEARER instead of PLAIN
  • paste your OAuth2 file (e.g. o-qmcug-kafkastudent3.json) into the resources folder, edit oauth.credentials.url to point to this file

If you are editing your own code, you will need to import the following into your Producer and Consumer Java code (this is already included in the sample code):

import io.streamnative.pulsar.handlers.kop.security.oauth.schema.OauthCredentialProvider;

We will edit the schema registry security in the next section before testing this code against a Pulsar cluster.

Please reference SchemaV2 if you would like an example of configuring producing and consuming Kafka messages with KSN directly in the Java code.


Using OAuth2 requires using the following dependency versions or greater:

Maven:

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>3.4.0</version>
</dependency>
<dependency>
    <groupId>io.streamnative.pulsar.handlers</groupId>
    <artifactId>oauth-client</artifactId>
    <version>3.1.0.1</version>
</dependency>

Gradle:

dependencies {
    implementation group: ‘org.apache.kafka’, name: ‘kafka-clients’, version: ‘3.4.0’
    implementation group: ‘io.streamnative.pulsar.handlers’, name: ‘oauth-client’, version: ‘3.1.0.1’
}

There are also version requirements for using OAuth2 with schema registry that will be discussed in the following section.