This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 754
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TLS & SASL configuration for existing kafka brokers (#799)
* Add TLS & SASL configuraiton for existing kafka brokers * Added some unit tests and refactor some code to add KAFKA_ENABLE_TLS && KAFKA_ENABLE_SASL * Cleanup kafka utils tests & move sarama config to init function * Set MinVersion of kafka when use SASL (see https://github.com/Shopify/sarama/blob/master/sasl_handshake_request.go#L32)
- Loading branch information
1 parent
e993277
commit 2b9b08e
Showing
4 changed files
with
556 additions
and
6 deletions.
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
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,77 @@ | ||
/* | ||
Copyright (c) 2016-2017 Bitnami | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package kafka | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"errors" | ||
"io/ioutil" | ||
"strconv" | ||
) | ||
|
||
var ( | ||
// ErrorCrtFileMandatory when private key is provided but certificate not. | ||
ErrorCrtFileMandatory = errors.New("client certificate is mandatory when private key file is provided") | ||
// ErrorKeyFileMandatory when certificate is provided but private not. | ||
ErrorKeyFileMandatory = errors.New("client private key is mandatory when certificate file is provided") | ||
// ErrorUsernameOrPasswordMandatory when username or password is not provided | ||
ErrorUsernameOrPasswordMandatory = errors.New("username and password is mandatory") | ||
) | ||
|
||
// GetTLSConfiguration build TLS configuration for kafka, return tlsConfig associated with kafka connection. | ||
func GetTLSConfiguration(caFile string, certFile string, keyFile string, insecure string) (*tls.Config, error) { | ||
|
||
if certFile != "" && keyFile == "" { | ||
return nil, ErrorKeyFileMandatory | ||
} | ||
if keyFile != "" && certFile == "" { | ||
return nil, ErrorCrtFileMandatory | ||
} | ||
|
||
isInsecure, _ := strconv.ParseBool(insecure) | ||
t := &tls.Config{} | ||
|
||
if caFile != "" { | ||
caCert, err := ioutil.ReadFile(caFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
caCertPool := x509.NewCertPool() | ||
caCertPool.AppendCertsFromPEM(caCert) | ||
t.RootCAs = caCertPool | ||
} | ||
|
||
if certFile != "" && keyFile != "" { | ||
cert, err := tls.LoadX509KeyPair(certFile, keyFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
t.Certificates = []tls.Certificate{cert} | ||
} | ||
|
||
t.InsecureSkipVerify = isInsecure | ||
return t, nil | ||
} | ||
|
||
// GetSASLConfiguration build SASL configuration for kafka. | ||
func GetSASLConfiguration(username string, password string) (string, string, error) { | ||
if username == "" || password == "" { | ||
return "", "", ErrorUsernameOrPasswordMandatory | ||
} | ||
return username, password, nil | ||
} |
Oops, something went wrong.