@@ -16,7 +16,7 @@ This article shows you how to set up SSL encryption between Apache Kafka clients
16
16
> [ !Important]
17
17
> 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.
18
18
19
- ## Apache Kafka Broker setup
19
+ ## Apache Kafka broker setup
20
20
21
21
The Kafka SSL broker setup will use four HDInsight cluster VMs in the following way:
22
22
@@ -128,105 +128,207 @@ To complete the configuration modification, do the following steps:
128
128
129
129

130
130
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 .
132
132
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
140
144
```
141
145
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
+
142
148

143
149
144
150
1. Restart all Kafka brokers.
145
151
1. Start the admin client with producer and consumer options to verify that both producers and consumers are working on port 9093.
146
152
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
+
147
205
# # Client setup (with authentication)
148
206
149
207
> [! 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).
151
209
152
- Complete the following steps to finish the client setup:
210
+ The following four steps summarize the tasks needed to complete the client setup:
153
211
154
212
1. Sign in to the client machine (standby head node).
155
213
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.
156
214
1. Switch to the CA machine (active head node) to sign the client certificate.
157
215
1. Go to the client machine (standby head node) and navigate to the ` ~/ssl` folder. Copy the signed cert to client machine.
158
216
159
- ```bash
160
- cd ssl
217
+ The details of each step are given below.
161
218
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) .
163
220
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
+ ` ` `
165
224
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.
167
226
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
+ ` ` `
170
232
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.
174
234
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
+ ` ` `
177
240
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
180
242
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
+ ` ` `
183
246
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.
187
248
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
+ ` ` `
189
254
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.
198
256
199
- ## Client setup (without authentication)
257
+ ` ` ` bash
258
+ scp client-cert-signed sshuser@HeadNode1_Name:~ /ssl/client-signed-cert
259
+ ` ` `
200
260
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
202
262
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
+ ` ` `
207
266
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:
209
268
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
212
289
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 .
215
292
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
218
294
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.
222
296
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
+ ```
224
300
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
+ ` ` `
230
332
231
333
# # Next steps
232
334
0 commit comments