Skip to content

Commit 3f8f1ad

Browse files
Merge pull request #101328 from hrasheed-msft/hdinsight_kafka_ssl_updates
Hdinsight kafka ssl updates
2 parents 84c3fef + 5cb8564 commit 3f8f1ad

File tree

1 file changed

+163
-61
lines changed

1 file changed

+163
-61
lines changed

articles/hdinsight/kafka/apache-kafka-ssl-encryption-authentication.md

Lines changed: 163 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This article shows you how to set up SSL encryption between Apache Kafka clients
1616
> [!Important]
1717
> 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 SSL for both producing and consuming. The console producer client `console-producer.sh` does not work with SSL.
1818
19-
## Apache Kafka Broker setup
19+
## Apache Kafka broker setup
2020

2121
The Kafka SSL broker setup will use four HDInsight cluster VMs in the following way:
2222

@@ -128,105 +128,207 @@ To complete the configuration modification, do the following steps:
128128
129129
![Editing kafka ssl configuration properties in Ambari](./media/apache-kafka-ssl-encryption-authentication/editing-configuration-ambari2.png)
130130
131-
1. Under **Advanced kafka-env** add the following lines to the end of the **kafka-env template** property.
131+
1. Add new configuration properties to the server.properties file.
132132
133-
```config
134-
# Needed to configure IP address advertising
135-
ssl.keystore.location=/home/sshuser/ssl/kafka.server.keystore.jks
136-
ssl.keystore.password=MyServerPassword123
137-
ssl.key.password=MyServerPassword123
138-
ssl.truststore.location=/home/sshuser/ssl/kafka.server.truststore.jks
139-
ssl.truststore.password=MyServerPassword123
133+
```bash
134+
# Configure Kafka to advertise IP addresses instead of FQDN
135+
IP_ADDRESS=$(hostname -i)
136+
echo advertised.listeners=$IP_ADDRESS
137+
sed -i.bak -e '/advertised/{/advertised@/!d;}' /usr/hdp/current/kafka-broker/conf/server.properties
138+
echo "advertised.listeners=PLAINTEXT://$IP_ADDRESS:9092,SSL://$IP_ADDRESS:9093" >> /usr/hdp/current/kafka-broker/conf/server.properties
139+
echo "ssl.keystore.location=/home/sshuser/ssl/kafka.server.keystore.jks" >> /usr/hdp/current/kafka-broker/conf/server.properties
140+
echo "ssl.keystore.password=MyServerPassword123" >> /usr/hdp/current/kafka-broker/conf/server.properties
141+
echo "ssl.key.password=MyServerPassword123" >> /usr/hdp/current/kafka-broker/conf/server.properties
142+
echo "ssl.truststore.location=/home/sshuser/ssl/kafka.server.truststore.jks" >> /usr/hdp/current/kafka-broker/conf/server.properties
143+
echo "ssl.truststore.password=MyServerPassword123" >> /usr/hdp/current/kafka-broker/conf/server.properties
140144
```
141145
146+
1. Go to Ambari configuration UI and verify that the new properties show up under **Advanced kafka-env** and the **kafka-env template** property.
147+
142148
![Editing kafka-env template property in Ambari](./media/apache-kafka-ssl-encryption-authentication/editing-configuration-kafka-env.png)
143149
144150
1. Restart all Kafka brokers.
145151
1. Start the admin client with producer and consumer options to verify that both producers and consumers are working on port 9093.
146152
153+
## Client setup (without authentication)
154+
155+
If you don't need authentication, the summary of the steps to set up only SSL encryption are:
156+
157+
1. Sign in to the CA (active head node).
158+
1. Copy the CA cert to client machine from the CA machine (wn0).
159+
1. Sign in to the client machine (hn1) and navigate to the `~/ssl` folder.
160+
1. Import the CA cert to the truststore.
161+
1. Import the CA cert to the keystore.
162+
163+
These steps are detailed in the following code snippets.
164+
165+
1. Sign in to the CA node.
166+
167+
```bash
168+
ssh sshuser@HeadNode0_Name
169+
cd ssl
170+
```
171+
172+
1. Copy the ca-cert to the client machine
173+
174+
```bash
175+
scp ca-cert sshuser@HeadNode1_Name:~/ssl/ca-cert
176+
```
177+
178+
1. Sign in to the client machine (standby head node).
179+
180+
```bash
181+
ssh sshuser@HeadNode1_Name
182+
cd ssl
183+
```
184+
185+
1. Import the CA certificate to the truststore.
186+
187+
```bash
188+
keytool -keystore kafka.client.truststore.jks -alias CARoot -import -file ca-cert -storepass "MyClientPassword123" -keypass "MyClientPassword123" -noprompt
189+
```
190+
191+
1. Import the CA cert to keystore.
192+
193+
```bash
194+
keytool -keystore kafka.client.keystore.jks -alias CARoot -import -file ca-cert -storepass "MyClientPassword123" -keypass "MyClientPassword123" -noprompt
195+
```
196+
197+
1. Create the file `client-ssl-auth.properties`. It should have the following lines:
198+
199+
```config
200+
security.protocol=SSL
201+
ssl.truststore.location=/home/sshuser/ssl/kafka.client.truststore.jks
202+
ssl.truststore.password=MyClientPassword123
203+
```
204+
147205
## Client setup (with authentication)
148206

149207
> [!Note]
150-
> The following steps are required only if you are setting up both SSL encryption **and** authentication. If you are only setting up encryption, please proceed to [Client setup without authentication](apache-kafka-ssl-encryption-authentication.md#client-setup-without-authentication)
208+
> The following steps are required only if you are setting up both SSL 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).
151209

152-
Complete the following steps to finish the client setup:
210+
The following four steps summarize the tasks needed to complete the client setup:
153211

154212
1. Sign in to the client machine (standby head node).
155213
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.
156214
1. Switch to the CA machine (active head node) to sign the client certificate.
157215
1. Go to the client machine (standby head node) and navigate to the `~/ssl` folder. Copy the signed cert to client machine.
158216

159-
```bash
160-
cd ssl
217+
The details of each step are given below.
161218

162-
# Create a java keystore and get a signed certificate for the broker. Then copy the certificate to the VM where the CA is running.
219+
1. Sign in to the client machine (standby head node).
163220

164-
keytool -genkey -keystore kafka.client.keystore.jks -validity 365 -storepass "MyClientPassword123" -keypass "MyClientPassword123" -dname "CN=mylaptop1" -alias my-local-pc1 -storetype pkcs12
221+
```bash
222+
ssh sshuser@HeadNode1_Name
223+
```
165224

166-
keytool -keystore kafka.client.keystore.jks -certreq -file client-cert-sign-request -alias my-local-pc1 -storepass "MyClientPassword123" -keypass "MyClientPassword123"
225+
1. Remove any existing ssl directory.
167226

168-
# Copy the cert to the CA
169-
scp client-cert-sign-request3 sshuser@HeadNode0_Name:~/tmp1/client-cert-sign-request
227+
```bash
228+
rm -R ~/ssl
229+
mkdir ssl
230+
cd ssl
231+
```
170232

171-
# Switch to the CA machine (active head node) to sign the client certificate.
172-
cd ssl
173-
openssl x509 -req -CA ca-cert -CAkey ca-key -in /tmp1/client-cert-sign-request -out /tmp1/client-cert-signed -days 365 -CAcreateserial -passin pass:MyServerPassword123
233+
1. Create a java keystore and create a certificate signing request.
174234

175-
# Return to the client machine (standby head node), navigate to ~/ssl folder and copy signed cert from the CA (active head node) to client machine
176-
scp -i ~/kafka-security.pem sshuser@HeadNode0_Name:/tmp1/client-cert-signed
235+
```bash
236+
keytool -genkey -keystore kafka.client.keystore.jks -validity 365 -storepass "MyClientPassword123" -keypass "MyClientPassword123" -dname "CN=HEADNODE1_FQDN" -storetype pkcs12
237+
238+
keytool -keystore kafka.client.keystore.jks -certreq -file client-cert-sign-request -storepass "MyClientPassword123" -keypass "MyClientPassword123"
239+
```
177240

178-
# Import CA cert to trust store
179-
keytool -keystore kafka.client.truststore.jks -alias CARoot -import -file ca-cert -storepass "MyClientPassword123" -keypass "MyClientPassword123" -noprompt
241+
1. Copy the certificate signing request to the CA
180242

181-
# Import CA cert to key store
182-
keytool -keystore kafka.client.keystore.jks -alias CARoot -import -file ca-cert -storepass "MyClientPassword123" -keypass "MyClientPassword123" -noprompt
243+
```bash
244+
scp client-cert-sign-request sshuser@HeadNode0_Name:~/ssl/client-cert-sign-request
245+
```
183246

184-
# Import signed client (cert client-cert-signed1) to keystore
185-
keytool -keystore kafka.client.keystore.jks -import -file client-cert-signed -alias my-local-pc1 -storepass "MyClientPassword123" -keypass "MyClientPassword123" -noprompt
186-
```
247+
1. Switch to the CA machine (active head node) and sign the client certificate.
187248

188-
Lastly, view the file `client-ssl-auth.properties` with the command `cat client-ssl-auth.properties`. It should have the following lines:
249+
```bash
250+
ssh sshuser@HeadNode0_Name
251+
cd ssl
252+
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
253+
```
189254

190-
```bash
191-
security.protocol=SSL
192-
ssl.truststore.location=/home/sshuser/ssl/kafka.client.truststore.jks
193-
ssl.truststore.password=MyClientPassword123
194-
ssl.keystore.location=/home/sshuser/ssl/kafka.client.keystore.jks
195-
ssl.keystore.password=MyClientPassword123
196-
ssl.key.password=MyClientPassword123
197-
```
255+
1. Copy signed client cert from the CA (active head node) to client machine.
198256

199-
## Client setup (without authentication)
257+
```bash
258+
scp client-cert-signed sshuser@HeadNode1_Name:~/ssl/client-signed-cert
259+
```
200260

201-
If you don't need authentication, the steps to set up only SSL encryption are:
261+
1. Copy the ca-cert to the client machine
202262

203-
1. Sign in to the client machine (hn1) and navigate to the `~/ssl` folder
204-
1. Copy the signed cert to client machine from the CA machine (wn0).
205-
1. Import the CA cert to the truststore
206-
1. Import the CA cert to the keystore
263+
```bash
264+
scp ca-cert sshuser@HeadNode1_Name:~/ssl/ca-cert
265+
```
207266

208-
These steps are shown in the following code snippet.
267+
1. Create client store with signed cert, and import ca cert into the keystore and truststore:
209268

210-
```bash
211-
cd ssl
269+
```bash
270+
keytool -keystore kafka.client.keystore.jks -import -file client-cert-signed -storepass MyClientPassword123 -keypass MyClientPassword123 -noprompt
271+
272+
keytool -keystore kafka.client.keystore.jks -alias CARoot -import -file ca-cert -storepass MyClientPassword123 -keypass MyClientPassword123 -noprompt
273+
274+
keytool -keystore kafka.client.truststore.jks -alias CARoot -import -file ca-cert -storepass MyClientPassword123 -keypass MyClientPassword123 -noprompt
275+
```
276+
277+
1. Create a file `client-ssl-auth.properties`. It should have the following lines:
278+
279+
```bash
280+
security.protocol=SSL
281+
ssl.truststore.location=/home/sshuser/ssl/kafka.client.truststore.jks
282+
ssl.truststore.password=MyClientPassword123
283+
ssl.keystore.location=/home/sshuser/ssl/kafka.client.keystore.jks
284+
ssl.keystore.password=MyClientPassword123
285+
ssl.key.password=MyClientPassword123
286+
```
287+
288+
## Verification
212289

213-
# Copy signed cert to client machine
214-
scp -i ~/kafka-security.pem sshuser@wn0-umakaf:/home/sshuser/ssl/ca-cert .
290+
> [!Note]
291+
> 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 SSL.
215292

216-
# Import CA cert to truststore
217-
keytool -keystore kafka.client.truststore.jks -alias CARoot -import -file ca-cert -storepass "MyClientPassword123" -keypass "MyClientPassword123" -noprompt
293+
### Kafka 2.1 or above
218294

219-
# Import CA cert to keystore
220-
keytool -keystore kafka.client.keystore.jks -alias CARoot -import -file cert-signed -storepass "MyClientPassword123" -keypass "MyClientPassword123" -noprompt
221-
```
295+
1. Create a topic if it doesn't exist already.
222296
223-
Finally, view the file `client-ssl-auth.properties` with the command `cat client-ssl-auth.properties`. It should have the following lines:
297+
```bash
298+
/usr/hdp/current/kafka-broker/bin/kafka-topics.sh --zookeeper <ZOOKEEPER_NODE>:2181 --create --topic topic1 --partitions 2 --replication-factor 2
299+
```
224300
225-
```bash
226-
security.protocol=SSL
227-
ssl.truststore.location=/home/sshuser/ssl/kafka.client.truststore.jks
228-
ssl.truststore.password=MyClientPassword123
229-
```
301+
1. Start console producer and provide the path to `client-ssl-auth.properties` as a configuration file for the producer.
302+
303+
```bash
304+
/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
305+
```
306+
307+
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.
308+
309+
```bash
310+
/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
311+
```
312+
313+
### Kafka 1.1
314+
315+
1. Create a topic if it doesn't exist already.
316+
317+
```bash
318+
/usr/hdp/current/kafka-broker/bin/kafka-topics.sh --zookeeper <ZOOKEEPER_NODE_0>:2181 --create --topic topic1 --partitions 2 --replication-factor 2
319+
```
320+
321+
1. Start console producer and provide the path to client-ssl-auth.properties as a configuration file for the producer.
322+
323+
```bash
324+
/usr/hdp/current/kafka-broker/bin/kafka-console-producer.sh --broker-list <FQDN_WORKER_NODE>:9092 --topic topic1
325+
```
326+
327+
3. 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.
328+
329+
```bash
330+
$ /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
331+
```
230332

231333
## Next steps
232334

0 commit comments

Comments
 (0)