Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom CA for 1-way TLS #139

Merged
merged 3 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,16 @@ Required fields:

- `mode`: This needs to be set to `TLS-1way`

Optional fields:

- `ca-certificate`: Path to your CA certificate


Example:


{
"mode": "TLS-1way",
"mode": "TLS-1way"
}

### Other modes
Expand Down
31 changes: 31 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"regexp"
"strings"
"syscall"
Expand Down Expand Up @@ -238,6 +239,26 @@ func setupSASL(auth authConfig, saramaCfg *sarama.Config) error {
func setupAuthTLS1Way(auth authConfig, saramaCfg *sarama.Config) error {
saramaCfg.Net.TLS.Enable = true
saramaCfg.Net.TLS.Config = &tls.Config{}

if auth.CACert == "" {
return nil
}

caString, err := ioutil.ReadFile(auth.CACert)
if err != nil {
return fmt.Errorf("failed to read ca-certificate err=%v", err)
}

caPool := x509.NewCertPool()
ok := caPool.AppendCertsFromPEM(caString)
if !ok {
failf("unable to add ca-certificate at %s to certificate pool", auth.CACert)
}

tlsCfg := &tls.Config{RootCAs: caPool}
tlsCfg.BuildNameToCertificate()

saramaCfg.Net.TLS.Config = tlsCfg
return nil
}

Expand Down Expand Up @@ -271,6 +292,12 @@ func setupAuthTLS(auth authConfig, saramaCfg *sarama.Config) error {
return nil
}

func qualifyPath(argFN string, target *string) {
if *target != "" && !filepath.IsAbs(*target) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we need to also check if filepath.Dir(*target) == "." -- cf test failures for test-secrets/cert.

*target = filepath.Join(filepath.Dir(argFN), *target)
}
}

func readAuthFile(argFN string, envFN string, target *authConfig) {
if argFN == "" && envFN == "" {
return
Expand All @@ -289,4 +316,8 @@ func readAuthFile(argFN string, envFN string, target *authConfig) {
if err := json.Unmarshal(byts, target); err != nil {
failf("failed to unmarshal auth file err=%v", err)
}

qualifyPath(fn, &target.CACert)
qualifyPath(fn, &target.ClientCert)
qualifyPath(fn, &target.ClientCertKey)
}
4 changes: 3 additions & 1 deletion group.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,9 @@ func (cmd *groupCmd) saramaConfig() *sarama.Config {
cfg.ClientID = "kt-group-" + sanitizeUsername(usr.Username)
cmd.infof("sarama client configuration %#v\n", cfg)

setupAuth(cmd.auth, cfg)
if err = setupAuth(cmd.auth, cfg); err != nil {
failf("failed to setup auth err=%v", err)
}

return cfg
}
Expand Down
4 changes: 3 additions & 1 deletion topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ func (cmd *topicCmd) connect() {
cfg.ClientID = "kt-topic-" + sanitizeUsername(usr.Username)
cmd.infof("sarama client configuration %#v\n", cfg)

setupAuth(cmd.auth, cfg)
if err = setupAuth(cmd.auth, cfg); err != nil {
failf("failed to setup auth err=%v", err)
}

if cmd.client, err = sarama.NewClient(cmd.brokers, cfg); err != nil {
failf("failed to create client err=%v", err)
Expand Down