Transport security

TLS and SASL are part of ClientSettings, so the same code selects them on every platform. Without them, a client connects in plaintext.

// Connect over TLS, authenticating with a SCRAM username and password
import cats.data.NonEmptyList
import cats.syntax.all.*

import xkafka.*

val password = "secret"

val tls  = TlsSettings.from(CertificateAuthority.PemFile("/etc/kafka/ca.pem"))  // The authority that signed the broker's certificate
val sasl = SaslSettings.from(SaslMechanism.ScramSha256, "user", password)       // The credentials to log in with

val client = (tls, sasl).mapN(SecuritySettings.SaslTls.apply).andThen: security =>  // Combine both, reporting every problem at once
  ClientSettings.from(NonEmptyList.one("broker:9093"), security = security)

Protocols

SecuritySettings has one case per protocol: Plaintext, the default, Tls, SaslPlaintext, and SaslTls. Each case carries what its protocol needs, so a client cannot ask for SASL without supplying credentials. Pass it to ClientSettings.from as security.

Certificate authorities

A CertificateAuthority is one of:

case meaning
SystemDefault the authorities the runtime already trusts
PemFile a path to a PEM file
Pem PEM text held in memory

Scala.js and Scala Native also accept a directory of certificates as a PemFile. Each certificate must be named by its subject hash, as openssl rehash names it; under any other name it is not found, and the broker is refused as ErrorCode.SslAuthenticationFailed. On the JVM a directory fails as ErrorCode.InvalidConfiguration.

TlsSettings verifies the broker hostname by default. Call withoutHostnameVerification to turn that off.

A blank path or blank PEM text is a SettingsError.

Client certificates

A broker can require each client to present a certificate of its own. Give TlsSettings a ClientCertificate, as clientCertificate or with withClientCertificate. It is one of:

case meaning
PemFile a path to one PEM file holding the private key and the certificate chain
Pem the certificate chain and the private key, as PEM text held in memory
// Present a client certificate to a broker that requires one
import xkafka.*

val tls =
  TlsSettings.from(
    CertificateAuthority.PemFile("/etc/kafka/ca.pem"),
    // The file holds the private key and the certificate chain
    clientCertificate = Some(ClientCertificate.PemFile("/etc/kafka/client.pem", keyPassword = None))
  )

The private key must be PKCS#8, which PEM marks as PRIVATE KEY, or as ENCRYPTED PRIVATE KEY if it needs keyPassword. Other forms, such as RSA PRIVATE KEY, are not supported. An inline key in another form is a SettingsError, and so is an empty keyPassword. To convert a key:

# Convert a private key to PKCS#8, unencrypted; leave out -nocrypt to encrypt it with a password openssl asks for
openssl pkcs8 -topk8 -nocrypt -in key.pem -out key-pkcs8.pem

The certificate's key and password are kept out of toString.

Java 17 can't read a key encrypted with PBES2, the scheme openssl pkcs8 uses by default, so on the JVM with Java 17 acquiring the client fails with a BackendFailure whose code is ErrorCode.InvalidConfiguration. Encrypt the key with openssl pkcs8 -topk8 -v1 PBE-SHA1-3DES instead, which every platform reads. Java 21 reads both.

Credentials

SaslSettings accepts SaslMechanism.Plain, ScramSha256, and ScramSha512, and keeps its password out of toString. A blank username or an empty password is a SettingsError.

Failures

Each of these fails as KafkaException.BackendFailure, and its code says which:

failure code
the broker's certificate is not trusted SslAuthenticationFailed
the broker refuses the client's certificate, or wants one and gets none SslAuthenticationFailed
the SASL credentials are incorrect SaslAuthenticationFailed
keyPassword does not decrypt the client's key InvalidConfiguration

The last one fails when the producer, consumer or admin client is acquired, before any connection is made.