DEV Community

Mario Mastrodicasa
Mario Mastrodicasa

Posted on Originally published at Medium on

KEFCore and broker security: TLS and SASL with managed Kafka clusters


Photo by FlyD on Unsplash

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
Enter fullscreen mode Exit fullscreen mode

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")))
);
Enter fullscreen mode Exit fullscreen mode

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")),
};
Enter fullscreen mode Exit fullscreen mode

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\";")))
);
Enter fullscreen mode Exit fullscreen mode

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\";")))
Enter fullscreen mode Exit fullscreen mode

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"))
Enter fullscreen mode Exit fullscreen mode

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")))
Enter fullscreen mode Exit fullscreen mode

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;")))
);
Enter fullscreen mode Exit fullscreen mode

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\";")))
);
Enter fullscreen mode Exit fullscreen mode

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")))
);
Enter fullscreen mode Exit fullscreen mode

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\";")))
);
Enter fullscreen mode Exit fullscreen mode

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}\";")))
);
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Disabling hostname verification (WithSslEndpointIdentificationAlgorithm("")) should only be used in development. In production it removes a critical security check.

Where to go from here

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)