Part of the KEFCore series. See also: introduction · how it works · performance
Production Kafka clusters — whether self-hosted or managed (Amazon MSK, Confluent Cloud, Aiven, Redpanda Cloud) — require encrypted and authenticated connections. KEFCore now exposes SecurityProtocol, SslConfigs, and SaslConfigs as first-class configuration options, making it straightforward to connect to any secured broker without touching the underlying KNet or JVM configuration directly.
This article covers how to configure KEFCore for the most common security scenarios.
The four security protocols
SecurityProtocol controls how KEFCore connects to the broker:
Value Encryption Authentication
---------------------------------------------
PLAINTEXT None None
SSL TLS None
SASL_PLAINTEXT None SASL
SASL_SSL TLS SASL
For any production cluster, use SSL or SASL_SSL. The combination most commonly required by managed brokers (MSK IAM, Confluent Cloud, Aiven, Redpanda Cloud) is SASL_SSL.
SecurityProtocol, SslConfigs, and SaslConfigs are first-wins singleton options — the values set by the first DbContext to initialize the cluster are used for all subsequent contexts on the same cluster. Ensure all DbContext instances that share the same BootstrapServers use the same security configuration.
TLS only — SSL
Use this when the broker requires an encrypted connection but not SASL authentication (e.g. self-hosted Kafka with a custom CA, some on-premises Confluent Platform deployments):
optionsBuilder.UseKEFCore(opt => opt
.WithBootstrapServers("KAFKA-SERVER:9093")
.WithApplicationId("MyApp")
.WithSecurityProtocol(SecurityProtocol.SSL)
.WithSslConfig(new SslConfigsBuilder()
.WithSslTruststoreLocation("/path/to/truststore.jks")
.WithSslTruststorePassword(new Password("truststore-password"))
// optional: mutual TLS (client certificate)
.WithSslKeystoreLocation("/path/to/keystore.jks")
.WithSslKeystoreLocation(new Password("keystore-password")))
);
Or directly on KEFCoreDbContext:
using var context = new BloggingContext()
{
BootstrapServers = "KAFKA-SERVER:9093",
ApplicationId = "MyApp",
SecurityProtocol = SecurityProtocol.SSL,
SslConfigs = new SslConfigsBuilder()
.WithSslTruststoreLocation("/path/to/truststore.jks")
.WithSslTruststorePassword(new Password("truststore-password")),
};
TLS + SASL/PLAIN — SASL_SSL
The most common configuration for managed brokers. SASL/PLAIN sends credentials as username and password, encrypted by TLS:
optionsBuilder.UseKEFCore(opt => opt
.WithBootstrapServers("KAFKA-SERVER:9093")
.WithApplicationId("MyApp")
.WithSecurityProtocol(SecurityProtocol.SASL_SSL)
.WithSslConfig(new SslConfigsBuilder()
.WithSslTruststoreLocation("/path/to/truststore.jks")
.WithSslTruststorePassword(new Password("truststore-password")))
.WithSaslConfig(new SaslConfigsBuilder()
.WithSaslMechanism("PLAIN")
.WithSaslJaasConfig(new Password(
"org.apache.kafka.common.security.plain.PlainLoginModule required " +
"username=\"myuser\" password=\"mypassword\";")))
);
TLS + SASL/SCRAM — SASL_SSL
SCRAM (Salted Challenge Response Authentication Mechanism) is used by self-hosted Kafka clusters that require stronger authentication than PLAIN. Replace the mechanism and login module:
.WithSaslConfig(new SaslConfigsBuilder()
.WithSaslMechanism("SCRAM-SHA-256") // or "SCRAM-SHA-512"
.WithSaslJaasConfig(new Password(
"org.apache.kafka.common.security.scram.ScramLoginModule required " +
"username=\"myuser\" password=\"mypassword\";")))
Kerberos (GSSAPI)
The default SASL mechanism when SaslConfigs is set without an explicit WithSaslMechanism() call is GSSAPI (Kerberos). Configure the service name and kinit command:
.WithSaslConfig(new SaslConfigsBuilder()
.WithSaslKerberosServiceName("kafka")
.WithSaslKerberosKinitCmd("/usr/bin/kinit"))
OAuth / SASL/OAUTHBEARER
For brokers integrated with an identity provider (e.g. Confluent Cloud with OAuth, MSK with IAM via OAUTHBEARER):
.WithSaslConfig(new SaslConfigsBuilder()
.WithSaslMechanism("OAUTHBEARER")
.WithSaslOauthBearerTokenEndpointUrl("https://your-idp/oauth/token")
.WithSaslOauthBearerClientCredentialsClientId("client-id")
.WithSaslOauthBearerClientCredentialsClientSecret(
new Password("client-secret")))
Provider-specific examples
Amazon MSK (IAM authentication)
MSK with IAM uses SASL_SSL with OAUTHBEARER and the AWS MSK IAM login module. The truststore is the standard Java CA bundle:
optionsBuilder.UseKEFCore(opt => opt
.WithBootstrapServers("your-msk-endpoint:9098")
.WithApplicationId("MyApp")
.WithSecurityProtocol(SecurityProtocol.SASL_SSL)
.WithSaslConfig(new SaslConfigsBuilder()
.WithSaslMechanism("AWS_MSK_IAM")
.WithSaslJaasConfig(new Password(
"software.amazon.msk.auth.iam.IAMLoginModule required;")))
);
Confluent Cloud
Confluent Cloud uses SASL_SSL with PLAIN and API key/secret as credentials:
optionsBuilder.UseKEFCore(opt => opt
.WithBootstrapServers("pkc-xxxxx.region.confluent.cloud:9092")
.WithApplicationId("MyApp")
.WithSecurityProtocol(SecurityProtocol.SASL_SSL)
.WithSaslConfig(new SaslConfigsBuilder()
.WithSaslMechanism("PLAIN")
.WithSaslJaasConfig(new Password(
"org.apache.kafka.common.security.plain.PlainLoginModule required " +
"username=\"API_KEY\" password=\"API_SECRET\";")))
);
No SslConfigs needed — Confluent Cloud uses a publicly trusted CA.
Aiven for Apache Kafka™
Aiven uses SSL with mutual TLS (client certificate required):
optionsBuilder.UseKEFCore(opt => opt
.WithBootstrapServers("your-service.aivencloud.com:PORT")
.WithApplicationId("MyApp")
.WithSecurityProtocol(SecurityProtocol.SSL)
.WithSslConfig(new SslConfigsBuilder()
.WithSslTruststoreLocation("/path/to/aiven-truststore.jks")
.WithSslTruststorePassword(new Password("truststore-password"))
.WithSslKeystoreLocation("/path/to/aiven-keystore.jks")
.WithSslKeystoreLocation(new Password("keystore-password")))
);
Redpanda Cloud
Redpanda Cloud uses the same SASL_SSL + SCRAM-SHA-256 pattern:
optionsBuilder.UseKEFCore(opt => opt
.WithBootstrapServers("your-cluster.redpanda.com:9092")
.WithApplicationId("MyApp")
.WithSecurityProtocol(SecurityProtocol.SASL_SSL)
.WithSaslConfig(new SaslConfigsBuilder()
.WithSaslMechanism("SCRAM-SHA-256")
.WithSaslJaasConfig(new Password(
"org.apache.kafka.common.security.scram.ScramLoginModule required " +
"username=\"myuser\" password=\"mypassword\";")))
);
Credentials from environment or secret manager
Never hardcode credentials in code. Read them at startup from environment variables or a secret manager:
var apiKey = Environment.GetEnvironmentVariable("KAFKA_API_KEY")!;
var apiSecret = Environment.GetEnvironmentVariable("KAFKA_API_SECRET")!;
optionsBuilder.UseKEFCore(opt => opt
.WithBootstrapServers(Environment.GetEnvironmentVariable("KAFKA_BOOTSTRAP")!)
.WithApplicationId("MyApp")
.WithSecurityProtocol(SecurityProtocol.SASL_SSL)
.WithSaslConfig(new SaslConfigsBuilder()
.WithSaslMechanism("PLAIN")
.WithSaslJaasConfig(new Password(
"org.apache.kafka.common.security.plain.PlainLoginModule required " +
$"username=\"{apiKey}\" password=\"{apiSecret}\";")))
);
For ASP.NET Core, inject configuration via IConfiguration and bind from appsettings.json or Azure Key Vault / AWS Secrets Manager.
SSL configuration reference
SslConfigsBuilder exposes the full set of Kafka SSL properties. The most commonly needed:
Property Method Notes
----------------------------------------------------------------------------------------------
Truststore path WithSslTruststoreLocation JKS file path
Truststore password WithSslTruststorePassword Password type
Keystore path WithSslKeystoreLocation For mutual TLS
Keystore password WithSslKeystoreLocation(Password) For mutual TLS
TLS protocol WithSslProtocol Default: TLSv1.3
Endpoint identification WithSslEndpointIdentificationAlgorithm "https" (default)
or "" to disable
hostname verification
Disabling hostname verification (WithSslEndpointIdentificationAlgorithm("")) should only be used in development. In production it removes a critical security check.
Where to go from here
- Options reference : kefcore.masesgroup.com/articles/options.html
- Supported backends : kefcore.masesgroup.com/articles/backends.html
- Getting started : kefcore.masesgroup.com/articles/gettingstarted.html
- GitHub : masesgroup/KEFCore
KAFKA is a registered trademark of The Apache Software Foundation. MASES Group has no affiliation with and is not endorsed by The Apache Software Foundation.

Top comments (0)