import ballerina/io;
import ballerinax/kafka;

// Define the relevant SASL URL of the configured Kafka server.
const string SASL_URL = "localhost:9093";

kafka:ProducerConfiguration producerConfig = {
    // Provide the relevant authentication configurations to authenticate the producer by [`kafka:AuthenticationConfiguration`](https://docs.central.ballerina.io/ballerinax/kafka/latest/records/AuthenticationConfiguration).
    auth: {
        // Provide the authentication mechanism used by the Kafka server.
        mechanism: kafka:AUTH_SASL_PLAIN,
        // Username and password should be set here in order to authenticate the producer.
        // For information on how to secure values instead of directly using plain text values, see [Defining Configurable Variables](https://ballerina.io/learn/user-guide/configurability/defining-configurable-variables/#securing-sensitive-data-using-configurable-variables).
        username: "alice",
        password: "alice@123"
    },
    securityProtocol: kafka:PROTOCOL_SASL_PLAINTEXT
};

kafka:Producer kafkaProducer = check new(SASL_URL, producerConfig);

public function main() returns error? {
    string message = "Hello, World!";
    check kafkaProducer->send({
        topic: "demo-security",
        value: message.toBytes()
    });
    check kafkaProducer->'flush();
    io:println("Message published successfully.");
}

SASL authentication - producer

This is an example of a Kafka producer using the SASL/PLAIN authentication. For this example to work properly, an active Kafka server must be present and it should be configured to use the SASL/PLAIN authentication mechanism.

For more information on the underlying module, see the Kafka module.

import ballerina/io;
import ballerinax/kafka;
const string SASL_URL = "localhost:9093";

Define the relevant SASL URL of the configured Kafka server.

kafka:ProducerConfiguration producerConfig = {
    auth: {

Provide the relevant authentication configurations to authenticate the producer by kafka:AuthenticationConfiguration.

        mechanism: kafka:AUTH_SASL_PLAIN,

Provide the authentication mechanism used by the Kafka server.

        username: "alice",
        password: "alice@123"
    },
    securityProtocol: kafka:PROTOCOL_SASL_PLAINTEXT
};

Username and password should be set here in order to authenticate the producer. For information on how to secure values instead of directly using plain text values, see Defining Configurable Variables.

kafka:Producer kafkaProducer = check new(SASL_URL, producerConfig);
public function main() returns error? {
    string message = "Hello, World!";
    check kafkaProducer->send({
        topic: "demo-security",
        value: message.toBytes()
    });
    check kafkaProducer->'flush();
    io:println("Message published successfully.");
}
bal run kafka_authentication_sasl_plain_producer.bal
Message published successfully.