- Manage Security
- Manage Authentication
- Service Accounts
- Use OAuth
Access Cloud Clusters using Pulsar Clients with OAuth 2.0
Use the following information to configure your Pulsar clients to use the OAuth2 authentication mechanism for connecting to StreamNative Cloud clusters.
Service URLs
In order to connect to a StreamNative Cloud cluster, you need to get its service URLs.
You can get a cluster's service URLs by following the steps below:
Navigate to the Details tab, and in the Access Points section, you can find all the available service URLs of this cluster. Click Copy at the end of the row of the service URL to copy the URL.
HTTP Service URL (TLS)
: The URL of Pulsar Admin HTTP service.Broker Service URL (TLS)
: The URL of Pulsar broker service.
OAuth 2.0 Credentials
Before configuring your Pulsar clients to use OAuth 2.0 for connecting to StreamNative Cloud clusters, you need to prepare an OAuth 2.0 credential file for your clients by following the steps below.
StreamNative OAuth2
If you are using StreamNative's built-in OAuth2 service, you can download the OAuth2 credential file for the service account that you want to use for your clients by following these steps:
Navigate to the Accounts & Accesses page.
On the left navigation pane, click Service Accounts.
In the row of the service account you want to use, click
...
.In the dropdown menu, click Download OAuth2 Key to download the OAuth2 credential file to your local directory.
The downloaded credentials file contains the service account credentials used for client authentication. The following is an example of the credentials file for a service account sa
of organization my_org
. Both client_id
and client_secret
are required while the other fields are optional. Since the credentials file contains client_secret
, please make sure the credentials file is stored in a safe place.
{
"type":"sn_service_account",
"client_id":"PZWBM2tMCVDFQ1lQInIaYgG4k1OSqwIO",
"client_secret":"EZr...Kgv",
"client_email":"sa@my_org.auth.streamnative.cloud",
"issuer_url":"https://auth.streamnative.cloud"
}
OIDC Federation
If you are using an OIDC-compliant identity provider via OIDC Federation, you can prepare the credentials information to configure your Pulsar clients to use OAuth 2.0 for connecting to StreamNative Cloud clusters. Create a credentials file containing the client_id
and client_secret
of your service account:
{
"client_id":"PZWBM2tMCVDFQ1lQInIaYgG4k1OSqwIO",
"client_secret":"EZr...Kgv"
}
Configure Pulsar Applications
Once you have the credentials file, you can follow the steps below to configure your Pulsar applications to use OAuth2 authentication.
First of all, since Pulsar clients support different authentication plugins, you need to configure the Pulsar clients to use OAuth2 authentication plugin. For example, you can configure the Pulsar Java clients to use org.apache.pulsar.client.impl.auth.oauth2.AuthenticationOAuth2
.
Secondly, you need to prepare the authentication parameters for OAuth2 authentication. The following table outlines the parameters required for configuring OAuth2 authentication.
Parameter | Description | Example | Required or not |
---|---|---|---|
type | OAuth 2.0 authentication type. Currently, Pulsar clients only support the client_credentials authentication type. | client_credentials (default) | Optional |
issuerUrl | The URL of the authentication provider which allows the Pulsar client to obtain an access token. Currently, StreamNative Cloud only support Auth0 as the identity provider. So the value here should be https://auth.streamnative.cloud . | https://auth.streamnative.cloud | Required |
credentialsUrl | The URL to the JSON credentials file. It supports the following pattern formats: <br /> <li> file:///path/to/file </li><li> data:application/json;base64,<base64-encoded value> </li> | file:///path/to/my_service_account_key.json | Required |
audience | The OAuth 2.0 resource server identifier for a Pulsar cluster. In StreamNative Cloud, a Pulsar cluster is identified by a Uniform Resource Name (URN), which is in the following format urn:sn:pulsar:${your_orgnization_id}:${instance_name} . | urn:sn:pulsar:my_org:my_instance | Required for StreamNative OAuth2 but optional for OIDC Federation |
scope | The scope of an access request. For more information, see access token scope | api://pulsar-cluster-1/.default | Optional |
Configure Pulsar Clients
This section describes how to configure Pulsar clients to connect to a StreamNative Cloud cluster urn:sn:pulsar:my_org:my_instance
using OAuth2. Please notes:
- For StreamNative OAuth2,
audience
is required. - For OIDC Federation, based on your identity provider, you may need to specify the
scope
and/oraudience
parameters accordingly.
import org.apache.pulsar.client.impl.auth.oauth2.AuthenticationFactoryOAuth2;
URL issuerUrl = new URL("https://auth.streamnative.cloud");
URL credentialsUrl = new URL("file:///path/to/credentials.json");
String audience = "urn:sn:pulsar:my_org:my_instance";
PulsarClient client = PulsarClient.builder()
.serviceUrl("<PULSAR_BROKER_URL>")
.authentication(
AuthenticationFactoryOAuth2.clientCredentials(issuerUrl, credentialsUrl, audience))
.build();
In the example above:
Replace
<PULSAR_ADMIN_URL>
and/or<PULSAR_BROKER_URL>
with the right admin and/or broker URL of your Pulsar cluster. You can get the service URLs in the Cluster Details page of the StreamNative Cloud Console.Replace
urn:sn:pulsar:my_org:my_instance
with the right URN of your Pulsar cluster.Replace
file:///path/to/credentials.json
with the right file path of your downloaded credentials file from the StreamNative Cloud Console.
You can find detailed examples in Build Applications with Pulsar.
Configure Pulsar Command-line tools
This section describes how to use Pulsar command-line tools to connect to a StreamNative Cloud cluster urn:sn:pulsar:my_org:my_instance
using OAuth2.
bin/pulsar-admin \
--admin-url <PULSAR_ADMIN_URL> \
--auth-plugin org.apache.pulsar.client.impl.auth.oauth2.AuthenticationOAuth2 \
--auth-params '{"privateKey":"file:///path/to/credentials.json", \
"issuerUrl":"https://auth.streamnative.cloud", \
"audience":"urn:sn:pulsar:my_org:my_instance"}' \
tenants list
In the example above:
Replace
<PULSAR_ADMIN_URL>
and/or<PULSAR_BROKER_URL>
with the right admin and/or broker URL of your Pulsar cluster. You can get the service URLs in the Cluster Details page of the StreamNative Cloud Console.Replace
urn:sn:pulsar:my_org:my_instance
with the right URN of your Pulsar cluster.Replace
file:///path/to/credentials.json
with the right file path of your downloaded credentials file from the StreamNative Cloud Console.