Skip to content

Commit a35c374

Browse files
Merge pull request #226815 from sreekzz/patch-143
ESP and Non-ESP Clusters
2 parents 0a38d4f + 5392653 commit a35c374

9 files changed

+369
-15
lines changed

articles/hdinsight/TOC.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,10 @@ items:
703703
href: ./kafka/apache-kafka-log-analytics-operations-management.md
704704
- name: Secure Spark and Kafka streaming integration scenario
705705
href: ./kafka/secure-spark-kafka-streaming-integration-scenario.md
706-
- name: SSL Encryption and Authentication
706+
- name: SSL Encryption and Authentication for Non ESP Kafka cluster
707707
href: ./kafka/apache-kafka-ssl-encryption-authentication.md
708+
- name: SSL Encryption and Authentication for ESP Kafka cluster
709+
href: ./kafka/apache-esp-kafka-ssl-encryption-authentication.md
708710
- name: Kafka MirrorMaker 2.0 guide
709711
href: ./kafka/kafka-mirrormaker-2-0-guide.md
710712
- name: Troubleshoot
Lines changed: 339 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,339 @@
1+
---
2+
title: Apache Kafka TLS encryption & authentication for ESP Kafka Clusters - Azure HDInsight
3+
description: Set up TLS encryption for communication between Kafka clients and Kafka brokers, Set up SSL authentication of clients for ESP Kafka clusters
4+
ms.service: hdinsight
5+
ms.topic: how-to
6+
ms.custom: hdinsightactive
7+
ms.date: 02/14/2023
8+
---
9+
10+
# Set up TLS encryption and authentication for ESP Apache Kafka cluster in Azure HDInsight
11+
12+
This article shows you how to set up Transport Layer Security (TLS) encryption, previously known as Secure Sockets Layer (SSL) encryption, between Apache Kafka clients and Apache Kafka brokers. It also shows you how to set up authentication of clients (sometimes referred to as two-way TLS).
13+
14+
> [!Important]
15+
> There are two clients which you can use for Kafka applications: a Java client and a console client. Only the Java client `ProducerConsumer.java` can use TLS for both producing and consuming. The console producer client `console-producer.sh` does not work with TLS.
16+
17+
## Apache Kafka broker setup
18+
19+
The Kafka TLS broker setup uses four HDInsight cluster VMs in the following way:
20+
21+
* headnode 0 - Certificate Authority (CA)
22+
* worker node 0, 1, and 2 - brokers
23+
24+
> [!Note]
25+
> This guide uses self-signed certificates, but the most secure solution is to use certificates issued by trusted CAs.
26+
27+
The summary of the broker setup process is as follows:
28+
29+
1. The following steps are repeated on each of the three worker nodes:
30+
1. Generate a certificate.
31+
1. Create a cert signing request.
32+
1. Send the cert signing request to the Certificate Authority (CA).
33+
1. Sign in to the CA and sign the request.
34+
1. SCP the signed certificate back to the worker node.
35+
1. SCP the public certificate of the CA to the worker node.
36+
37+
1. Once you have all of the certificates, put the certs into the cert store.
38+
1. Go to Ambari and change the configurations.
39+
40+
Use the following detailed instructions to complete the broker setup:
41+
42+
> [!Important]
43+
> In the following code snippets wnX is an abbreviation for one of the three worker nodes and should be substituted with `wn0`, `wn1` or `wn2` as appropriate. `WorkerNode0_Name` and `HeadNode0_Name` should be substituted with the names of the respective machines.
44+
45+
1. Perform initial setup on head node 0, which for HDInsight fills the role of the Certificate Authority (CA).
46+
47+
```bash
48+
# Create a new directory 'ssl' and change into it
49+
mkdir ssl
50+
cd ssl
51+
```
52+
53+
1. Perform the same initial setup on each of the brokers (worker nodes 0, 1 and 2).
54+
55+
```bash
56+
# Create a new directory 'ssl' and change into it
57+
mkdir ssl
58+
cd ssl
59+
```
60+
61+
1. On each of the worker nodes, execute the following steps using the code snippet.
62+
1. Create a keystore and populate it with a new private certificate.
63+
1. Create a certificate signing request.
64+
1. SCP the certificate signing request to the CA (headnode0)
65+
66+
```bash
67+
keytool -genkey -keystore kafka.server.keystore.jks -validity 365 -storepass "MyServerPassword123" -keypass "MyServerPassword123" -dname "CN=FQDN_WORKER_NODE" -storetype pkcs12
68+
keytool -keystore kafka.server.keystore.jks -certreq -file cert-file -storepass "MyServerPassword123" -keypass "MyServerPassword123"
69+
scp cert-file sshuser@HeadNode0_Name:~/ssl/wnX-cert-sign-request
70+
```
71+
> [!Note]
72+
> FQDN_WORKER_NODE is Fully Qualified Domain Name of worker node machine.You can get that details from /etc/hosts file in head node
73+
74+
For example,
75+
```
76+
wn0-espkaf.securehadooprc.onmicrosoft.com
77+
wn0-kafka2.zbxwnwsmpcsuvbjqbmespcm1zg.bx.internal.cloudapp.net
78+
```
79+
:::image type="content" source="./media/apache-esp-kafka-ssl-encryption-authentication/etc-hosts.png" alt-text="Screenshot showing host file output." border="true":::
80+
81+
1. On the CA machine, run the following command to create ca-cert and ca-key files:
82+
83+
```bash
84+
openssl req -new -newkey rsa:4096 -days 365 -x509 -subj "/CN=Kafka-Security-CA" -keyout ca-key -out ca-cert -nodes
85+
```
86+
87+
1. Change to the CA machine and sign all of the received cert signing requests:
88+
89+
```bash
90+
openssl x509 -req -CA ca-cert -CAkey ca-key -in wn0-cert-sign-request -out wn0-cert-signed -days 365 -CAcreateserial -passin pass:"MyServerPassword123"
91+
openssl x509 -req -CA ca-cert -CAkey ca-key -in wn1-cert-sign-request -out wn1-cert-signed -days 365 -CAcreateserial -passin pass:"MyServerPassword123"
92+
openssl x509 -req -CA ca-cert -CAkey ca-key -in wn2-cert-sign-request -out wn2-cert-signed -days 365 -CAcreateserial -passin pass:"MyServerPassword123"
93+
```
94+
95+
1. Send the signed certificates back to the worker nodes from the CA (headnode0).
96+
97+
```bash
98+
scp wn0-cert-signed sshuser@WorkerNode0_Name:~/ssl/cert-signed
99+
scp wn1-cert-signed sshuser@WorkerNode1_Name:~/ssl/cert-signed
100+
scp wn2-cert-signed sshuser@WorkerNode2_Name:~/ssl/cert-signed
101+
```
102+
103+
1. Send the public certificate of the CA to each worker node.
104+
105+
```bash
106+
scp ca-cert sshuser@WorkerNode0_Name:~/ssl/ca-cert
107+
scp ca-cert sshuser@WorkerNode1_Name:~/ssl/ca-cert
108+
scp ca-cert sshuser@WorkerNode2_Name:~/ssl/ca-cert
109+
```
110+
111+
1. On each worker node, add the CAs public certificate to the truststore and keystore. Then add the worker node's own signed certificate to the keystore
112+
113+
```bash
114+
keytool -keystore kafka.server.truststore.jks -alias CARoot -import -file ca-cert -storepass "MyServerPassword123" -keypass "MyServerPassword123" -noprompt
115+
keytool -keystore kafka.server.keystore.jks -alias CARoot -import -file ca-cert -storepass "MyServerPassword123" -keypass "MyServerPassword123" -noprompt
116+
keytool -keystore kafka.server.keystore.jks -import -file cert-signed -storepass "MyServerPassword123" -keypass "MyServerPassword123" -noprompt
117+
118+
```
119+
120+
## Update Kafka configuration to use TLS and restart brokers
121+
122+
You have now set up each Kafka broker with a keystore and truststore, and imported the correct certificates. Next, modify related Kafka configuration properties using Ambari and then restart the Kafka brokers.
123+
124+
To complete the configuration modification, do the following steps:
125+
126+
1. Sign in to the Azure portal and select your Azure HDInsight Apache Kafka cluster.
127+
1. Go to the Ambari UI by clicking **Ambari home** under **Cluster dashboards**.
128+
1. Under **Kafka Broker** set the **listeners** property to `PLAINTEXT://localhost:9092,SASL_SSL://localhost:9093`
129+
1. Under **Advanced kafka-broker** set the **security.inter.broker.protocol** property to `SASL_SSL`
130+
131+
:::image type="content" source="./media/apache-esp-kafka-ssl-encryption-authentication/properties-file-with-sasl.png" alt-text="Screenshot showing how to edit Kafka sasl configuration properties in Ambari." border="true":::
132+
133+
1. Under **Custom kafka-broker** set the **ssl.client.auth** property to `required`.
134+
135+
136+
> [!Note]
137+
> This step is only required if you're setting up authentication and encryption.
138+
139+
:::image type="content" source="./media/apache-esp-kafka-ssl-encryption-authentication/editing-configuration-ambari2.png" alt-text="Screenshot showing how to edit Kafka ssl configuration properties in Ambari." border="true":::
140+
141+
1. Here's the screenshot that shows Ambari configuration UI with these changes.
142+
143+
> [!Note]
144+
> 1. ssl.keystore.location and ssl.truststore.location is the complete path of your keystore, truststore location in Certificate Authority (hn0)
145+
> 1. ssl.keystore.password and ssl.truststore.password is the password set for the keystore and truststore. In this case as an example,` MyServerPassword123`
146+
> 1. ssl.key.password is the key set for the keystore and trust store. In this case as an example, `MyServerPassword123`
147+
148+
For HDI version 4.0 or 5.0
149+
150+
a. If you're setting up authentication and encryption, then the screenshot looks like
151+
152+
:::image type="content" source="./media/apache-esp-kafka-ssl-encryption-authentication/properties-file-authentication-as-required.png" alt-text="Screenshot showing how to edit Kafka-env template property in Ambari authentication as required." border="true":::
153+
154+
b. If you are setting up encryption only, then the screenshot looks like
155+
156+
:::image type="content" source="./media/apache-esp-kafka-ssl-encryption-authentication/properties-file-authentication-as-none.png" alt-text="Screenshot showing how to edit Kafka-env template property in Ambari authentication as none." border="true":::
157+
158+
1. Restart all Kafka brokers.
159+
160+
## Client setup (without authentication)
161+
162+
If you don't need authentication, the summary of the steps to set up only TLS encryption are:
163+
164+
1. Sign in to the CA (active head node).
165+
1. Copy the CA cert to client machine from the CA machine (wn0).
166+
1. Sign in to the client machine (hn1) and navigate to the `~/ssl` folder.
167+
1. Import the CA cert to the truststore.
168+
1. Import the CA cert to the keystore.
169+
170+
These steps are detailed in the following code snippets.
171+
172+
1. Sign in to the CA node.
173+
174+
```bash
175+
ssh sshuser@HeadNode0_Name
176+
cd ssl
177+
```
178+
179+
1. Copy the ca-cert to the client machine
180+
181+
```bash
182+
scp ca-cert sshuser@HeadNode1_Name:~/ssl/ca-cert
183+
```
184+
185+
1. Sign in to the client machine (standby head node).
186+
187+
```bash
188+
ssh sshuser@HeadNode1_Name
189+
cd ssl
190+
```
191+
192+
1. Import the CA certificate to the truststore.
193+
194+
```bash
195+
keytool -keystore kafka.client.truststore.jks -alias CARoot -import -file ca-cert -storepass "MyClientPassword123" -keypass "MyClientPassword123" -noprompt
196+
```
197+
198+
1. Import the CA cert to keystore.
199+
200+
```bash
201+
keytool -keystore kafka.client.keystore.jks -alias CARoot -import -file ca-cert -storepass "MyClientPassword123" -keypass "MyClientPassword123" -noprompt
202+
```
203+
204+
1. Create the file `client-ssl-auth.properties` on client machine (hn1) . It should have the following lines:
205+
206+
```config
207+
security.protocol=SASL_SSL
208+
sasl.mechanism=GSSAPI
209+
sasl.kerberos.service.name=kafka
210+
ssl.truststore.location=/home/sshuser/ssl/kafka.client.truststore.jks
211+
ssl.truststore.password=MyClientPassword123
212+
```
213+
214+
1. Start the admin client with producer and consumer options to verify that both producers and consumers are working on port 9093. Refer to [Verification](apache-kafka-ssl-encryption-authentication.md#verification) section for steps needed to verify the setup using console producer/consumer.
215+
216+
## Client setup (with authentication)
217+
218+
> [!Note]
219+
> The following steps are required only if you are setting up both TLS encryption **and** authentication. If you are only setting up encryption, then see [Client setup without authentication](apache-kafka-ssl-encryption-authentication.md#client-setup-without-authentication).
220+
221+
The following four steps summarize the tasks needed to complete the client setup:
222+
223+
1. Sign in to the client machine (standby head node).
224+
1. Create a Java keystore and get a signed certificate for the broker. Then copy the certificate to the VM where the CA is running.
225+
1. Switch to the CA machine (active head node) to sign the client certificate.
226+
1. Go to the client machine (standby head node) and navigate to the `~/ssl` folder. Copy the signed cert to client machine.
227+
228+
The details of each step are given.
229+
230+
1. Sign in to the client machine (standby head node).
231+
232+
```bash
233+
ssh sshuser@HeadNode1_Name
234+
```
235+
236+
1. Remove any existing ssl directory.
237+
238+
```bash
239+
rm -R ~/ssl
240+
mkdir ssl
241+
cd ssl
242+
```
243+
244+
1. Create a Java keystore and create a certificate signing request.
245+
246+
```bash
247+
keytool -genkey -keystore kafka.client.keystore.jks -validity 365 -storepass "MyClientPassword123" -keypass "MyClientPassword123" -dname "CN=HEADNODE1_FQDN" -storetype pkcs12
248+
249+
keytool -keystore kafka.client.keystore.jks -certreq -file client-cert-sign-request -storepass "MyClientPassword123" -keypass "MyClientPassword123"
250+
```
251+
252+
1. Copy the certificate signing request to the CA
253+
254+
```bash
255+
scp client-cert-sign-request sshuser@HeadNode0_Name:~/ssl/client-cert-sign-request
256+
```
257+
258+
1. Switch to the CA machine (active head node) and sign the client certificate.
259+
260+
```bash
261+
ssh sshuser@HeadNode0_Name
262+
cd ssl
263+
openssl x509 -req -CA ca-cert -CAkey ca-key -in ~/ssl/client-cert-sign-request -out ~/ssl/client-cert-signed -days 365 -CAcreateserial -passin pass:MyClientPassword123
264+
```
265+
266+
1. Copy signed client cert from the CA (active head node) to client machine.
267+
268+
```bash
269+
scp client-cert-signed sshuser@HeadNode1_Name:~/ssl/client-signed-cert
270+
```
271+
272+
1. Copy the ca-cert to the client machine
273+
274+
```bash
275+
scp ca-cert sshuser@HeadNode1_Name:~/ssl/ca-cert
276+
```
277+
278+
1. Sign in to the client machine (standby head node) and navigate to ssl directory.
279+
280+
```bash
281+
ssh sshuser@HeadNode1_Name
282+
cd ssl
283+
```
284+
285+
1. Create client store with signed cert, and import CA certificate into the keystore and truststore on client machine (hn1):
286+
287+
```bash
288+
keytool -keystore kafka.client.truststore.jks -alias CARoot -import -file ca-cert -storepass "MyClientPassword123" -keypass "MyClientPassword123" -noprompt
289+
290+
keytool -keystore kafka.client.keystore.jks -alias CARoot -import -file ca-cert -storepass "MyClientPassword123" -keypass "MyClientPassword123" -noprompt
291+
292+
keytool -keystore kafka.client.keystore.jks -import -file client-signed-cert -storepass "MyClientPassword123" -keypass "MyClientPassword123" -noprompt
293+
```
294+
295+
1. Create a file `client-ssl-auth.properties` on client machine (hn1). It should have the following lines:
296+
297+
```bash
298+
security.protocol=SASL_SSL
299+
sasl.mechanism=GSSAPI
300+
sasl.kerberos.service.name=kafka
301+
302+
ssl.truststore.location=/home/sshuser/ssl/kafka.client.truststore.jks
303+
ssl.truststore.password=MyClientPassword123
304+
ssl.keystore.location=/home/sshuser/ssl/kafka.client.keystore.jks
305+
ssl.keystore.password=MyClientPassword123
306+
ssl.key.password=MyClientPassword123
307+
308+
```
309+
310+
## Verification
311+
312+
Run these steps on the client machine.
313+
314+
> [!Note]
315+
> If HDInsight 4.0 and Kafka 2.1 is installed, you can use the console producer/consumers to verify your setup. If not, run the Kafka producer on port 9092 and send messages to the topic, and then use the Kafka consumer on port 9093 which uses TLS.
316+
317+
### Kafka 2.1 or above
318+
319+
1. Create a topic if it doesn't exist already.
320+
321+
```bash
322+
/usr/hdp/current/kafka-broker/bin/kafka-topics.sh --zookeeper <ZOOKEEPER_NODE>:2181 --create --topic topic1 --partitions 2 --replication-factor 2
323+
```
324+
325+
1. Start console producer and provide the path to `client-ssl-auth.properties` as a configuration file for the producer.
326+
327+
```bash
328+
/usr/hdp/current/kafka-broker/bin/kafka-console-producer.sh --broker-list <FQDN_WORKER_NODE>:9093 --topic topic1 --producer.config ~/ssl/client-ssl-auth.properties
329+
```
330+
331+
1. Open another ssh connection to client machine and start console consumer and provide the path to `client-ssl-auth.properties` as a configuration file for the consumer.
332+
333+
```bash
334+
/usr/hdp/current/kafka-broker/bin/kafka-console-consumer.sh --bootstrap-server <FQDN_WORKER_NODE>:9093 --topic topic1 --consumer.config ~/ssl/client-ssl-auth.properties --from-beginning
335+
```
336+
337+
## Next steps
338+
339+
* [What is Apache Kafka on HDInsight?](apache-kafka-introduction.md)

0 commit comments

Comments
 (0)