Authentication using TLS

TLS authentication overview

TLS authentication is an extension of TLS transport encryption. Not only servers have keys and certs that the client uses to verify the identity of servers, clients also have keys and certs that the server uses to verify the identity of clients. You must have TLS transport encryption configured on your cluster before you can use TLS authentication. This guide assumes you already have TLS transport encryption configured.

Bouncy Castle Provider provides TLS related cipher suites and algorithms in Pulsar. If you need FIPS version of Bouncy Castle Provider, please reference Bouncy Castle page.

Create client certificates

Client certificates are generated using the certificate authority. Server certificates are also generated with the same certificate authority.

The biggest difference between client certs and server certs is that the common name for the client certificate is the role token which that client is authenticated as.

To use client certificates, you need to set tlsRequireTrustedClientCertOnConnect=true at the broker side. For details, refer to TLS broker configuration.

First, you need to enter the following command to generate the key :

  1. $ openssl genrsa -out admin.key.pem 2048

Similar to the broker, the client expects the key to be in PKCS 8 format, so you need to convert it by entering the following command:

  1. $ openssl pkcs8 -topk8 -inform PEM -outform PEM \
  2. -in admin.key.pem -out admin.key-pk8.pem -nocrypt

Next, enter the command below to generate the certificate request. When you are asked for a common name, enter the role token that you want this key pair to authenticate a client as.

  1. $ openssl req -config openssl.cnf \
  2. -key admin.key.pem -new -sha256 -out admin.csr.pem
note

If openssl.cnf is not specified, read Certificate authority to get the openssl.cnf.

Then, enter the command below to sign with request with the certificate authority. Note that the client certs uses the usr_cert extension, which allows the cert to be used for client authentication.

  1. $ openssl ca -config openssl.cnf -extensions usr_cert \
  2. -days 1000 -notext -md sha256 \
  3. -in admin.csr.pem -out admin.cert.pem

You can get a cert, admin.cert.pem, and a key, admin.key-pk8.pem from this command. With ca.cert.pem, clients can use this cert and this key to authenticate themselves to brokers and proxies as the role token admin.

note

If the “unable to load CA private key” error occurs and the reason of this error is “No such file or directory: /etc/pki/CA/private/cakey.pem” in this step. Try the command below:

  1. $ cd /etc/pki/tls/misc/CA
  2. $ ./CA -newca

to generate cakey.pem .

Enable TLS authentication on brokers

To configure brokers to authenticate clients, add the following parameters to broker.conf, alongside the configuration to enable tls transport:

  1. # Configuration to enable authentication
  2. authenticationEnabled=true
  3. authenticationProviders=org.apache.pulsar.broker.authentication.AuthenticationProviderTls
  4. # operations and publish/consume from all topics
  5. superUserRoles=admin
  6. # Authentication settings of the broker itself. Used when the broker connects to other brokers, either in same or other clusters
  7. brokerClientTlsEnabled=true
  8. brokerClientAuthenticationPlugin=org.apache.pulsar.client.impl.auth.AuthenticationTls
  9. brokerClientAuthenticationParameters={"tlsCertFile":"/path/my-ca/admin.cert.pem","tlsKeyFile":"/path/my-ca/admin.key-pk8.pem"}
  10. brokerClientTrustCertsFilePath=/path/my-ca/certs/ca.cert.pem

Enable TLS authentication on proxies

To configure proxies to authenticate clients, add the following parameters to proxy.conf, alongside the configuration to enable tls transport:

The proxy should have its own client key pair for connecting to brokers. You need to configure the role token for this key pair in the proxyRoles of the brokers. See the authorization guide for more details.

  1. # For clients connecting to the proxy
  2. authenticationEnabled=true
  3. authenticationProviders=org.apache.pulsar.broker.authentication.AuthenticationProviderTls
  4. # For the proxy to connect to brokers
  5. brokerClientAuthenticationPlugin=org.apache.pulsar.client.impl.auth.AuthenticationTls
  6. brokerClientAuthenticationParameters=tlsCertFile:/path/to/proxy.cert.pem,tlsKeyFile:/path/to/proxy.key-pk8.pem

Client configuration

When you use TLS authentication, client connects via TLS transport. You need to configure the client to use https:// and 8443 port for the web service URL, pulsar+ssl:// and 6651 port for the broker service URL.

CLI tools

Command-line tools like pulsar-admin, pulsar-perf, and pulsar-client use the conf/client.conf config file in a Pulsar installation.

You need to add the following parameters to that file to use TLS authentication with the CLI tools of Pulsar:

  1. webServiceUrl=https://broker.example.com:8443/
  2. brokerServiceUrl=pulsar+ssl://broker.example.com:6651/
  3. useTls=true
  4. tlsAllowInsecureConnection=false
  5. tlsTrustCertsFilePath=/path/to/ca.cert.pem
  6. authPlugin=org.apache.pulsar.client.impl.auth.AuthenticationTls
  7. authParams=tlsCertFile:/path/to/my-role.cert.pem,tlsKeyFile:/path/to/my-role.key-pk8.pem

Java client

  1. import org.apache.pulsar.client.api.PulsarClient;
  2. PulsarClient client = PulsarClient.builder()
  3. .serviceUrl("pulsar+ssl://broker.example.com:6651/")
  4. .enableTls(true)
  5. .tlsTrustCertsFilePath("/path/to/ca.cert.pem")
  6. .authentication("org.apache.pulsar.client.impl.auth.AuthenticationTls",
  7. "tlsCertFile:/path/to/my-role.cert.pem,tlsKeyFile:/path/to/my-role.key-pk8.pem")
  8. .build();

Python client

  1. from pulsar import Client, AuthenticationTLS
  2. auth = AuthenticationTLS("/path/to/my-role.cert.pem", "/path/to/my-role.key-pk8.pem")
  3. client = Client("pulsar+ssl://broker.example.com:6651/",
  4. tls_trust_certs_file_path="/path/to/ca.cert.pem",
  5. tls_allow_insecure_connection=False,
  6. authentication=auth)

C++ client

  1. #include <pulsar/Client.h>
  2. pulsar::ClientConfiguration config;
  3. config.setUseTls(true);
  4. config.setTlsTrustCertsFilePath("/path/to/ca.cert.pem");
  5. config.setTlsAllowInsecureConnection(false);
  6. pulsar::AuthenticationPtr auth = pulsar::AuthTls::create("/path/to/my-role.cert.pem",
  7. "/path/to/my-role.key-pk8.pem")
  8. config.setAuth(auth);
  9. pulsar::Client client("pulsar+ssl://broker.example.com:6651/", config);

Node.js client

  1. const Pulsar = require('pulsar-client');
  2. (async () => {
  3. const auth = new Pulsar.AuthenticationTls({
  4. certificatePath: '/path/to/my-role.cert.pem',
  5. privateKeyPath: '/path/to/my-role.key-pk8.pem',
  6. });
  7. const client = new Pulsar.Client({
  8. serviceUrl: 'pulsar+ssl://broker.example.com:6651/',
  9. authentication: auth,
  10. tlsTrustCertsFilePath: '/path/to/ca.cert.pem',
  11. });
  12. })();

C# client

  1. var clientCertificate = new X509Certificate2("admin.pfx");
  2. var client = PulsarClient.Builder()
  3. .AuthenticateUsingClientCertificate(clientCertificate)
  4. .Build();