Librdkafka
Consumer Configuration
SSL Encryption
Kafka Security
Kafka SSL Configuration

librdkafka consumer and ssl configuration

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a distributed streaming platform capable of handling trillions of events a day. librdkafka is a C library implementation of the Kafka protocol, providing Producer and Consumer support for Kafka. In environments where security is a concern, you may need to configure your Kafka consumer to use SSL/TLS for data encryption and secure communication.

Understanding librdkafka SSL Configuration

SSL (Secure Sockets Layer) is a protocol for securing internet communication. In the context of Kafka and librdkafka, SSL is used to encrypt the data transmitted between brokers and clients (producers and consumers). This prevents unauthorized access to data in transit.

SSL Configuration Basics

To configure librdkafka to use SSL, you have to set specific configuration properties related to security. These properties can be set programmatically or via configuration files, depending on how librdkafka is used in your application.

c
1rd_kafka_conf_t *conf = rd_kafka_conf_new();
2
3// Set security protocol
4rd_kafka_conf_set(conf, "security.protocol", "SSL", NULL, 0);
5
6// Set SSL certificate files
7rd_kafka_conf_set(conf, "ssl.ca.location", "/path/to/ca-cert", NULL, 0);
8rd_kafka_conf_set(conf, "ssl.certificate.location", "/path/to/client-cert.pem", NULL, 0);
9rd_kafka_conf_set(conf, "ssl.key.location", "/path/to/client-key.pem", NULL, 0);
10rd_kafka_conf_set(conf, "ssl.key.password", "your-key-password", NULL, 0);

Key Configuration Parameters for SSL

Here are the most important SSL configuration parameters for librdkafka:

  • security.protocol: Protocol used to communicate with brokers. For SSL, this should be set to "SSL".
  • ssl.ca.location: File path to the trusted CA (Certificate Authority) certificate.
  • ssl.certificate.location: File path to the client's public key certificate.
  • ssl.key.location: File path to the client's private key.
  • ssl.key.password: Password for the client's private key, if encrypted.

Considerations for SSL Certificate Management

  • Certificate Authority (CA): A CA is a trusted entity that issues digital certificates. The CA certificate is used to authenticate the legitimacy of the peer's certificate.
  • Client Certificates: In mutual TLS (mTLS), both client and server authenticate each other with certificates. The client certificate must be signed by a trusted CA and recognized by the Kafka broker.
  • Key Security: Private keys should never be exposed to unauthorized entities. Use proper file permissions to secure key files.

Best Practices and Additional Security Measures

In addition to basic SSL configuration, consider the following best practices for enhanced security:

  • Certificate Revocation Lists (CRLs): These lists contain certificates that have been revoked by the CA before their expiration date. CRLs should be regularly updated and checked to ensure no revoked certificates are used.
  • Advanced Encryption Settings: Depending on the sensitivity of your data, consider configuring ssl.cipher.suites or ssl.enabled.protocols to use specific cipher suites or TLS versions.
  • Client Authentication: For environments requiring heightened security, enable ssl.client.auth in the broker’s server.properties to mandate client authentication.

Useful Code Snippet

Here is an additional example showing how to create a Kafka consumer in C using librdkafka with SSL configuration:

c
1#include <librdkafka/rdkafka.h>
2
3int main() {
4    rd_kafka_conf_t *conf;
5    rd_kafka_t *rk;
6    char errstr[512];
7
8    conf = rd_kafka_conf_new();
9    rd_kafka_conf_set(conf, "bootstrap.servers", "your.kafka.broker:9093", NULL, 0);
10    rd_kafka_conf_set(conf, "security.protocol", "SSL", NULL, 0);
11    rd_kafka_conf_set(conf, "ssl.ca.location", "/etc/kafka/secrets/ca-cert", NULL, 0);
12    rd_kafka_conf_set(conf, "ssl.certificate.location", "/etc/kafka/secrets/client-cert.pem", NULL, 0);
13    rd_kafka_conf_set(conf, "ssl.key.location", "/etc/kafka/secrets/client-key.pem", NULL, 0);
14
15    // Create Kafka handle
16    if (!(rk = rd_kafka_new(RD_KAFKA_CONSUMER, conf, errstr, sizeof(errstr)))) {
17        fprintf(stderr, "Failed to create new consumer: %s\n", errstr);
18        return 1;
19    }
20
21    // Now start consuming messages
22    rd_kafka_consume_start(rk, 0, RD_KAFKA_OFFSET_BEGINNING);
23
24    // Cleanup
25    rd_kafka_destroy(rk);
26
27    return 0;
28}

Summary Table of Key librdkafka SSL Configuration Parameters

ParameterDescriptionExample Value
security.protocolProtocol to use for communication with Kafka brokersSSL
ssl.ca.locationPath to the CA certificate file/etc/ssl/certs/ca-cert
ssl.certificate.locationPath to the client's public key certificate/etc/ssl/certs/client-cert.pem
ssl.key.locationPath to the client's private key/etc/ssl/private/client-key.pem
ssl.key.passwordPassword for the client's private key, if encryptedyour-key-password

Understanding and configuring SSL properly with librdkafka ensures that your Kafka consumer applications are not only robust but also secure from various network vulnerabilities.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.