Skip to content

Commit

Permalink
Add test tools for kafka authentication
Browse files Browse the repository at this point in the history
* Add test stacks for SSL, SASL/PLAIN, SASL/SCRAM, SASL over TLS authentication
  • Loading branch information
taisho6339 committed Nov 7, 2021
1 parent 4d2509f commit 488fae3
Show file tree
Hide file tree
Showing 23 changed files with 485 additions and 1 deletion.
26 changes: 26 additions & 0 deletions clients/cmd/promtail/promtail-kafka-sasl-plain.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
server:
http_listen_port: 9080
grpc_listen_port: 0

clients:
- url: http://localhost:3100/loki/api/v1/push

scrape_configs:
- job_name: kafka-sasl-plain
kafka:
use_incoming_timestamp: false
brokers:
- localhost:29092
authentication:
type: sasl
sasl_config:
mechanism: PLAIN
user: kafkaadmin
password: kafkaadmin-pass
use_tls: false
group_id: kafka_group
topics:
- foo
- ^promtail.*
labels:
job: kafka-sasl-plain
26 changes: 26 additions & 0 deletions clients/cmd/promtail/promtail-kafka-sasl-scram.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
server:
http_listen_port: 9080
grpc_listen_port: 0

clients:
- url: http://localhost:3100/loki/api/v1/push

scrape_configs:
- job_name: kafka-sasl-plain
kafka:
use_incoming_timestamp: false
brokers:
- localhost:29092
authentication:
type: sasl
sasl_config:
mechanism: SCRAM-SHA-512
user: kafkaadmin
password: kafkaadmin-pass
use_tls: false
group_id: kafka_group
topics:
- foo
- ^promtail.*
labels:
job: kafka-sasl-plain
28 changes: 28 additions & 0 deletions clients/cmd/promtail/promtail-kafka-sasl-ssl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
server:
http_listen_port: 9080
grpc_listen_port: 0

clients:
- url: http://localhost:3100/loki/api/v1/push

scrape_configs:
- job_name: kafka-sasl-plain
kafka:
use_incoming_timestamp: false
brokers:
- localhost:29092
authentication:
type: sasl
sasl_config:
mechanism: PLAIN
user: kafkaadmin
password: kafkaadmin-pass
use_tls: true
ca_file: ../../../tools/kafka/secrets/promtail-kafka-ca.pem
insecure_skip_verify: true
group_id: kafka_group
topics:
- foo
- ^promtail.*
labels:
job: kafka-sasl-plain
27 changes: 27 additions & 0 deletions clients/cmd/promtail/promtail-kafka-ssl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
server:
http_listen_port: 9080
grpc_listen_port: 0

clients:
- url: http://localhost:3100/loki/api/v1/push

scrape_configs:
- job_name: kafka-mtls
kafka:
use_incoming_timestamp: false
brokers:
- localhost:29092
authentication:
type: ssl
tls_config:
ca_file: ../../../tools/kafka/secrets/promtail-kafka-ca.pem
cert_file: ../../../tools/kafka/secrets/kafka.consumer.keystore.cer.pem
key_file: ../../../tools/kafka/secrets/kafka.consumer.keystore.key.pem
server_name: localhost
insecure_skip_verify: true
group_id: kafka_mtls_group
topics:
- foo
- ^promtail.*
labels:
job: kafka-mtls
8 changes: 8 additions & 0 deletions tools/kafka/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.crt
*.jks
*_creds
*.key
*.pem
*.csr
*.srl
*.p12
10 changes: 10 additions & 0 deletions tools/kafka/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ To discover available brokers you can use the `make print-brokers`.

Finally to stop the compose stack use `make stop-kafka`. This will result in all topics being lost with their messages.

## Running secure kafka locally

To test authentication, you need to start the Kafka container which is configured with authentication.

You can also use `make start-kafka` in appropriate directory like `sasl-scram` you need.

In addition, you need to create certificates using `make create-certs` when using SSL/TLS.

If you don't need to authenticate, you should use the tools in `plain` directory.

## Working with Topic

In Kafka before sending messages you need to create and select the topic you want to use for the exchange.
Expand Down
2 changes: 1 addition & 1 deletion tools/kafka/Makefile → tools/kafka/plain/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ TOPIC ?= promtail
RF ?= 1
PARTS ?= 3

BROKER_LIST := $(shell ./broker-list.sh $(HOST_IP))
BROKER_LIST := $(shell ../broker-list.sh $(HOST_IP))
DOCKER_RUN := docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -e HOST_IP=$(HOST_IP) -i -t wurstmeister/kafka /bin/bash -c

start-kafka:
Expand Down
File renamed without changes.
16 changes: 16 additions & 0 deletions tools/kafka/sasl-plain/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

HOST_IP ?= host.docker.internal
TOPIC ?= promtail
RF ?= 1
PARTS ?= 3

BROKER_LIST := $(shell ../broker-list.sh $(HOST_IP))

start-kafka:
docker-compose up -d

stop-kafka:
docker-compose down

print-brokers:
@echo $(BROKER_LIST)
19 changes: 19 additions & 0 deletions tools/kafka/sasl-plain/conf/kafka.jaas.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="kafkaadmin"
password="kafkaadmin-pass"
user_kafkaadmin="kafkaadmin-pass"
;
};
KafkaClient {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="kafkaadmin"
password="kafkaadmin-pass"
;
};
Client {
org.apache.zookeeper.server.auth.DigestLoginModule required
username="super"
password="adminsecret"
;
};
4 changes: 4 additions & 0 deletions tools/kafka/sasl-plain/conf/zookeeper.jaas.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Server {
org.apache.zookeeper.server.auth.DigestLoginModule required
user_super="adminsecret";
};
32 changes: 32 additions & 0 deletions tools/kafka/sasl-plain/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: '2'
services:
zookeeper:
image: confluentinc/cp-zookeeper:latest
ports:
- "22181:22181"
environment:
ZOOKEEPER_SERVER_ID: 1
ZOOKEEPER_CLIENT_PORT: 22181
KAFKA_OPTS: -Djava.security.auth.login.config=/etc/kafka/secrets/zookeeper.jaas.conf
-Dzookeeper.authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
-Dzookeeper.requireClientAuthScheme=sasl
volumes:
- ./conf:/etc/kafka/secrets

kafka:
image: confluentinc/cp-kafka:6.2.1
depends_on:
- zookeeper
ports:
- "29092:9092"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:22181
KAFKA_ADVERTISED_LISTENERS: SASL_PLAINTEXT://kafka:9092
KAFKA_SECURITY_INTER_BROKER_PROTOCOL: SASL_PLAINTEXT
KAFKA_SASL_MECHANISM_INTER_BROKER_PROTOCOL: PLAIN
KAFKA_SASL_ENABLED_MECHANISMS: PLAIN
KAFKA_OPTS: -Djava.security.auth.login.config=/etc/kafka/secrets/kafka.jaas.conf
volumes:
- ./conf:/etc/kafka/secrets
- /var/run/docker.sock:/var/run/docker.sock
22 changes: 22 additions & 0 deletions tools/kafka/sasl-scram/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

HOST_IP ?= host.docker.internal
TOPIC ?= promtail
RF ?= 1
PARTS ?= 3

BROKER_LIST := $(shell ../broker-list.sh $(HOST_IP))

start-kafka:
docker-compose up -d zookeeper
docker-compose exec zookeeper kafka-configs \
--zookeeper localhost:22181 \
--alter \
--add-config 'SCRAM-SHA-512=[iterations=8192,password=kafkaadmin-pass],SCRAM-SHA-512=[password=kafkaadmin-pass]' \
--entity-type users --entity-name kafkaadmin
docker-compose up -d kafka

stop-kafka:
docker-compose down

print-brokers:
@echo $(BROKER_LIST)
18 changes: 18 additions & 0 deletions tools/kafka/sasl-scram/conf/kafka.jaas.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
KafkaServer {
org.apache.kafka.common.security.scram.ScramLoginModule required
username="kafkaadmin"
password="kafkaadmin-pass"
;
};
KafkaClient {
org.apache.kafka.common.security.scram.ScramLoginModule required
username="kafkaadmin"
password="kafkaadmin-pass"
;
};
Client {
org.apache.zookeeper.server.auth.DigestLoginModule required
username="super"
password="adminsecret"
;
};
10 changes: 10 additions & 0 deletions tools/kafka/sasl-scram/conf/zookeeper.jaas.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Server {
org.apache.zookeeper.server.auth.DigestLoginModule required
user_super="adminsecret";
};
Client {
org.apache.zookeeper.server.auth.DigestLoginModule required
username="super"
password="adminsecret"
;
};
32 changes: 32 additions & 0 deletions tools/kafka/sasl-scram/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: '2'
services:
zookeeper:
image: confluentinc/cp-zookeeper:latest
ports:
- "22181:22181"
environment:
ZOOKEEPER_SERVER_ID: 1
ZOOKEEPER_CLIENT_PORT: 22181
KAFKA_OPTS: -Djava.security.auth.login.config=/etc/kafka/secrets/zookeeper.jaas.conf
-Dzookeeper.authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
-Dzookeeper.requireClientAuthScheme=sasl
volumes:
- ./conf:/etc/kafka/secrets

kafka:
image: confluentinc/cp-kafka:6.2.1
depends_on:
- zookeeper
ports:
- "29092:9092"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:22181
KAFKA_ADVERTISED_LISTENERS: SASL_PLAINTEXT://kafka:9092
KAFKA_SECURITY_INTER_BROKER_PROTOCOL: SASL_PLAINTEXT
KAFKA_SASL_MECHANISM_INTER_BROKER_PROTOCOL: SCRAM-SHA-512
KAFKA_SASL_ENABLED_MECHANISMS: SCRAM-SHA-512
KAFKA_OPTS: -Djava.security.auth.login.config=/etc/kafka/secrets/kafka.jaas.conf
volumes:
- ./conf:/etc/kafka/secrets
- /var/run/docker.sock:/var/run/docker.sock
19 changes: 19 additions & 0 deletions tools/kafka/sasl-ssl/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

HOST_IP ?= host.docker.internal
TOPIC ?= promtail
RF ?= 1
PARTS ?= 3

BROKER_LIST := $(shell ../broker-list.sh $(HOST_IP))

create-certs:
bash ../secrets/create-certs.sh

start-kafka:
docker-compose up -d

stop-kafka:
docker-compose down

print-brokers:
@echo $(BROKER_LIST)
19 changes: 19 additions & 0 deletions tools/kafka/sasl-ssl/conf/kafka.jaas.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="kafkaadmin"
password="kafkaadmin-pass"
user_kafkaadmin="kafkaadmin-pass"
;
};
KafkaClient {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="kafkaadmin"
password="kafkaadmin-pass"
;
};
Client {
org.apache.zookeeper.server.auth.DigestLoginModule required
username="super"
password="adminsecret"
;
};
4 changes: 4 additions & 0 deletions tools/kafka/sasl-ssl/conf/zookeeper.jaas.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Server {
org.apache.zookeeper.server.auth.DigestLoginModule required
user_super="adminsecret";
};
Loading

0 comments on commit 488fae3

Please sign in to comment.