-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SASL&mTLS authentication support for Kafka in Promtail (#4663)
* Add xdg-go module * Add mTLS, SASL/PLAIN, SASL/SCRAM authentication support * docs: Add authentication configuration * Add test tools for kafka authentication * Add test stacks for SSL, SASL/PLAIN, SASL/SCRAM, SASL over TLS authentication * Fix shelllint
- Loading branch information
1 parent
9762b31
commit 1557dab
Showing
62 changed files
with
6,082 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package kafka | ||
|
||
import ( | ||
"crypto/sha256" | ||
"crypto/sha512" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"os" | ||
|
||
promconfig "github.com/prometheus/common/config" | ||
"github.com/xdg-go/scram" | ||
) | ||
|
||
func createTLSConfig(cfg promconfig.TLSConfig) (*tls.Config, error) { | ||
tc := &tls.Config{ | ||
InsecureSkipVerify: cfg.InsecureSkipVerify, | ||
ServerName: cfg.ServerName, | ||
} | ||
// load ca cert | ||
if len(cfg.CAFile) > 0 { | ||
caCert, err := os.ReadFile(cfg.CAFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
caCertPool := x509.NewCertPool() | ||
caCertPool.AppendCertsFromPEM(caCert) | ||
tc.RootCAs = caCertPool | ||
} | ||
// load client cert | ||
if len(cfg.CertFile) > 0 && len(cfg.KeyFile) > 0 { | ||
cert, err := tls.LoadX509KeyPair(cfg.CertFile, cfg.KeyFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tc.Certificates = []tls.Certificate{cert} | ||
} | ||
return tc, nil | ||
} | ||
|
||
// copied from https://github.com/Shopify/sarama/blob/44627b731c60bb90efe25573e7ef2b3f8df3fa23/examples/sasl_scram_client/scram_client.go | ||
var ( | ||
SHA256 scram.HashGeneratorFcn = sha256.New | ||
SHA512 scram.HashGeneratorFcn = sha512.New | ||
) | ||
|
||
// XDGSCRAMClient implements sarama.SCRAMClient | ||
type XDGSCRAMClient struct { | ||
*scram.Client | ||
*scram.ClientConversation | ||
scram.HashGeneratorFcn | ||
} | ||
|
||
func (x *XDGSCRAMClient) Begin(userName, password, authzID string) (err error) { | ||
x.Client, err = x.HashGeneratorFcn.NewClient(userName, password, authzID) | ||
if err != nil { | ||
return err | ||
} | ||
x.ClientConversation = x.Client.NewConversation() | ||
return nil | ||
} | ||
|
||
func (x *XDGSCRAMClient) Step(challenge string) (response string, err error) { | ||
response, err = x.ClientConversation.Step(challenge) | ||
return | ||
} | ||
|
||
func (x *XDGSCRAMClient) Done() bool { | ||
return x.ClientConversation.Done() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.