Authentication using mTLS

mTLS authentication overview

Mutual TLS (mTLS) is a mutual authentication mechanism. 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.

The following figure illustrates how Pulsar processes mTLS authentication between clients and servers.

Pulsar mTLS authentication process

Enable mTLS authentication on brokers

To configure brokers to authenticate clients using mTLS, add the following parameters to the conf/broker.conf. If you use a standalone Pulsar, you need to add these parameters to the conf/standalone.conf file:

  1. # enable authentication
  2. authenticationEnabled=true
  3. # set mTLS authentication provider
  4. authenticationProviders=org.apache.pulsar.broker.authentication.AuthenticationProviderTls
  5. # configure TLS for client to connect brokers
  6. brokerClientTlsEnabled=true
  7. brokerClientTrustCertsFilePath=/path/to/ca.cert.pem
  8. brokerClientAuthenticationPlugin=org.apache.pulsar.client.impl.auth.AuthenticationTls
  9. brokerClientAuthenticationParameters={"tlsCertFile":"/path/to/admin.cert.pem","tlsKeyFile":"/path/to/admin.key-pk8.pem"}
  10. # configure TLS ports
  11. brokerServicePortTls=6651
  12. webServicePortTls=8081
  13. # configure CA certificate
  14. tlsTrustCertsFilePath=/path/to/ca.cert.pem
  15. # configure server certificate
  16. tlsCertificateFilePath=/path/to/broker.cert.pem
  17. # configure server's private key
  18. tlsKeyFilePath=/path/to/broker.key-pk8.pem
  19. # enable mTLS
  20. tlsRequireTrustedClientCertOnConnect=true
  21. tlsAllowInsecureConnection=false
  22. # Tls cert refresh duration in seconds (set 0 to check on every new connection)
  23. tlsCertRefreshCheckDurationSec=300

Enable mTLS authentication on proxies

To configure proxies to authenticate clients using mTLS, add the following parameters to the conf/proxy.conf file.

  1. # enable authentication
  2. authenticationEnabled=true
  3. # set mTLS authentication provider
  4. authenticationProviders=org.apache.pulsar.broker.authentication.AuthenticationProviderTls
  5. # configure TLS for client to connect proxies
  6. tlsEnabledWithBroker=true
  7. brokerClientTrustCertsFilePath=/path/to/ca.cert.pem
  8. brokerClientAuthenticationPlugin=org.apache.pulsar.client.impl.auth.AuthenticationTls
  9. brokerClientAuthenticationParameters={"tlsCertFile":"/path/to/admin.cert.pem","tlsKeyFile":"/path/to/admin.key-pk8.pem"}
  10. # configure TLS ports
  11. brokerServicePortTls=6651
  12. webServicePortTls=8081
  13. # configure CA certificate
  14. tlsTrustCertsFilePath=/path/to/ca.cert.pem
  15. # configure server certificate
  16. tlsCertificateFilePath=/path/to/proxy.cert.pem
  17. # configure server's private key
  18. tlsKeyFilePath=/path/to/proxy.key-pk8.pem
  19. # enable mTLS
  20. tlsRequireTrustedClientCertOnConnect=true
  21. tlsAllowInsecureConnection=false

Configure mTLS authentication in Pulsar clients

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

  • Java
  • Python
  • C++
  • Node.js
  • Go
  • C#
  1. import org.apache.pulsar.client.api.PulsarClient;
  2. PulsarClient client = PulsarClient.builder()
  3. .serviceUrl("pulsar+ssl://broker.example.com:6651/")
  4. .tlsTrustCertsFilePath("/path/to/ca.cert.pem")
  5. .authentication("org.apache.pulsar.client.impl.auth.AuthenticationTls",
  6. "tlsCertFile:/path/to/my-role.cert.pem,tlsKeyFile:/path/to/my-role.key-pk8.pem")
  7. .build();
  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)
  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);
  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. })();
  1. client, err := pulsar.NewClient(ClientOptions{
  2. URL: "pulsar+ssl://broker.example.com:6651/",
  3. TLSTrustCertsFilePath: "/path/to/ca.cert.pem",
  4. Authentication: pulsar.NewAuthenticationTLS("/path/to/my-role.cert.pem", "/path/to/my-role.key-pk8.pem"),
  5. })
  1. var clientCertificate = new X509Certificate2("admin.pfx");
  2. var client = PulsarClient.Builder()
  3. .AuthenticateUsingClientCertificate(clientCertificate)
  4. .Build();

Configure mTLS authentication in CLI tools

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

To use mTLS authentication with the CLI tools of Pulsar, you need to add the following parameters to the conf/client.conf file, alongside the configurations to enable mTLS encryption:

  1. authPlugin=org.apache.pulsar.client.impl.auth.AuthenticationTls
  2. authParams=tlsCertFile:/path/to/my-role.cert.pem,tlsKeyFile:/path/to/my-role.key-pk8.pem

Configure mTLS authentication with KeyStore

Apache Pulsar supports TLS encryption and mTLS authentication between clients and Apache Pulsar service. By default, it uses PEM format file configuration. This section describes how to use the KeyStore type to configure mTLS authentication.

Configure brokers

Configure the broker.conf file as follows.

  1. # Configuration to enable authentication
  2. authenticationEnabled=true
  3. authenticationProviders=org.apache.pulsar.broker.authentication.AuthenticationProviderTls
  4. # Enable KeyStore type
  5. tlsEnabledWithKeyStore=true
  6. # key store
  7. tlsKeyStoreType=JKS
  8. tlsKeyStore=/var/private/tls/broker.keystore.jks
  9. tlsKeyStorePassword=brokerpw
  10. # trust store
  11. tlsTrustStoreType=JKS
  12. tlsTrustStore=/var/private/tls/broker.truststore.jks
  13. tlsTrustStorePassword=brokerpw
  14. # internal client/admin-client config
  15. brokerClientTlsEnabled=true
  16. brokerClientTlsEnabledWithKeyStore=true
  17. brokerClientTlsTrustStoreType=JKS
  18. brokerClientTlsTrustStore=/var/private/tls/client.truststore.jks
  19. brokerClientTlsTrustStorePassword=clientpw
  20. # internal auth config
  21. brokerClientAuthenticationPlugin=org.apache.pulsar.client.impl.auth.AuthenticationKeyStoreTls
  22. brokerClientAuthenticationParameters={"keyStoreType":"JKS","keyStorePath":"/var/private/tls/client.keystore.jks","keyStorePassword":"clientpw"}
  23. tlsRequireTrustedClientCertOnConnect=true
  24. tlsAllowInsecureConnection=false

Configure clients

Besides configuring TLS encryption, you need to configure the KeyStore, which contains a valid CN as client role, for clients.

For example:

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

    1. webServiceUrl=https://broker.example.com:8443/
    2. brokerServiceUrl=pulsar+ssl://broker.example.com:6651/
    3. useKeyStoreTls=true
    4. tlsTrustStoreType=JKS
    5. tlsTrustStorePath=/var/private/tls/client.truststore.jks
    6. tlsTrustStorePassword=clientpw
    7. authPlugin=org.apache.pulsar.client.impl.auth.AuthenticationKeyStoreTls
    8. authParams={"keyStoreType":"JKS","keyStorePath":"/var/private/tls/client.keystore.jks","keyStorePassword":"clientpw"}
  2. for Java client

    1. import org.apache.pulsar.client.api.PulsarClient;
    2. PulsarClient client = PulsarClient.builder()
    3. .serviceUrl("pulsar+ssl://broker.example.com:6651/")
    4. .useKeyStoreTls(true)
    5. .tlsTrustStorePath("/var/private/tls/client.truststore.jks")
    6. .tlsTrustStorePassword("clientpw")
    7. .allowTlsInsecureConnection(false)
    8. .enableTlsHostnameVerification(false)
    9. .authentication(
    10. "org.apache.pulsar.client.impl.auth.AuthenticationKeyStoreTls",
    11. "keyStoreType:JKS,keyStorePath:/var/private/tls/client.keystore.jks,keyStorePassword:clientpw")
    12. .build();
  3. for Java admin client

    1. PulsarAdmin amdin = PulsarAdmin.builder().serviceHttpUrl("https://broker.example.com:8443")
    2. .useKeyStoreTls(true)
    3. .tlsTrustStorePath("/var/private/tls/client.truststore.jks")
    4. .tlsTrustStorePassword("clientpw")
    5. .allowTlsInsecureConnection(false)
    6. .enableTlsHostnameVerification(false)
    7. .authentication(
    8. "org.apache.pulsar.client.impl.auth.AuthenticationKeyStoreTls",
    9. "keyStoreType:JKS,keyStorePath:/var/private/tls/client.keystore.jks,keyStorePassword:clientpw")
    10. .build();

Authentication using mTLS - 图2note

Configure tlsTrustStorePath when you set useKeyStoreTls to true.