Generating a self-signed certificate for a Kafka broker.
- ca-cert = public CA certificate
- ca-key = private key of the ca-cert
flowchart TD;
classDef make fill:blue,stroke:#333,stroke-width:4px
classDef file fill:green,stroke:#333,stroke-width:4px
make:CA:::make-->file:ca-key:::file
make:CA-->file:ca-cert:::file
- cert-file = signing request
- cert-signed = signed certificate
flowchart TD;
classDef make fill:blue,stroke:#333,stroke-width:4px
classDef file fill:green,stroke:#333,stroke-width:4px
make:BROKER_KEYSTORE:::make-->jks[file:kafka.server.keystore.jks]:::file-->
make:BROKER_CERT_REQUEST:::make-->file:cert-file:::file
file:ca-cert-->make:SELF_SIGN:::make
file:ca-key-->make:SELF_SIGN
file:cert-file-->make:SELF_SIGN
make:SELF_SIGN-->file:cert-signed:::file
file:ca-cert-->make:TRUSTSTORE:::make-->file:kafka.server.truststore.jks:::file
jks-->make:KEYSTORE_CACERT:::make
file:ca-cert-->make:KEYSTORE_CACERT
file:cert-signed-->make:KEYSTORE_SIGNED_CERT
jks-->make:KEYSTORE_SIGNED_CERT:::make
Requires Kafka restart.
set this environment property to show SSL debug logs.
export KAFKA_OPTS=-Djavax.net.debug=alllisteners=PLAINTEXT://0.0.0.0:9092,SSL://0.0.0.0:9093
advertised.listeners=PLAINTEXT://<HOSTNAME>:9092,SSL://<HOSTNAME>:9093
ssl.keystore.location=<path>/kafka.server.keystore.jks
ssl.keystore.password=yourpassword
ssl.key.password=yourpassword
ssl.truststore.location=<path>/kafka.server.truststore.jks
ssl.truststore.password=yourpassword
ssl.client.auth=required
Verify the SSL port
openssl s_client -connect <HOSTNAME>:9093flowchart TD;
classDef make fill:blue,stroke:#333,stroke-width:4px
classDef file fill:green,stroke:#333,stroke-width:4px
file:ca-cert-->make:CLIENT_TRUSTORE:::make-->jks[file:kafka.client.truststore.jks]:::file
security.protocol=SSL
ssl.truststore.location=<path>/kafka.client.truststore.jks
ssl.truststore.password=yourclientpasswordProducer
./kafka-console-producer.sh --broker-list HOSTNAME:9093 --topic mytopic --producer.config PATH_TO_THE_ABOVE_PROPERTIES
Consumer
./kafka-console-consumer.sh --broker-list HOSTNAME:9093 --topic mytopic --consumer.config PATH_TO_THE_ABOVE_PROPERTIESThe common name (CN) must match exactly the fully qualified domain name (FQDN) of the server. The client compares the CN with the DNS domain name to ensure that it is indeed connecting to the desired server, not a malicious one. The hostname of the server can also be specified in the Subject Alternative Name (SAN). Since the distinguished name is used as the server principal when SSL is used as the inter-broker security protocol, it is useful to have hostname as a SAN rather than the CN.
To show the CN or SAN in a signed certificate, run the command below:
openssl x509 -noout -subject -in your-signed-certHost name verification of servers is enabled by default for client connections as well as inter-broker connections to prevent man-in-the-middle attacks. Server host name verification may be disabled by setting ssl.endpoint.identification.algorithm to an empty string. For example,
ssl.endpoint.identification.algorithm=Client authentication. Broker work is same as above.
flowchart TD;
classDef make fill:blue,stroke:#333,stroke-width:4px
classDef file fill:green,stroke:#333,stroke-width:4px
make:CLIENT_KEYSTORE:::make-->jks[file:kafka.client.keystore.jks]:::file
jks-->make:CLIENT_SIGN:::make-->file:client-cert-sign-request:::file
file:ca-cert-->make:CA_CLIENT_SIGN:::make-->file:client-cert-signed:::file
file:ca-key-->make:CA_CLIENT_SIGN
file:client-cert-sign-request:::file-->make:CA_CLIENT_SIGN
file:kafka.client.keystore.jks-->make:CLIENT_CA:::make
file:ca-cert-->make:CLIENT_CA:::make
file:kafka.client.keystore.jks-->make:CLIENT_SIGNED:::make
file:client-cert-signed-->make:CLIENT_SIGNED
ssl.client.auth=requiredsecurity.protocol=SSL
ssl.truststore.location=<path to your truststore>
ssl.truststore.password=<the password>
ssl.keystore.location=<path to your keystore>
ssl.keystore.password=<the password>
ssl.key.password=<the password>
Producer
./kafka-console-producer.sh --broker-list HOSTNAME:9093 --topic mytopic --producer.config PATH_TO_THE_ABOVE_PROPERTIES
Consumer
./kafka-console-consumer.sh --bootstrap-server HOSTNAME:9093 --topic mytopic --consumer.config PATH_TO_THE_ABOVE_PROPERTIES