-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mhoffmann
committed
Mar 17, 2019
1 parent
44eaf08
commit 01def27
Showing
6 changed files
with
154 additions
and
5 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,58 @@ | ||
package config | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"io/ioutil" | ||
) | ||
|
||
// Configuration describes the shared configuration options for Producers or Consumers | ||
type Configuration struct { | ||
Brokers []string | ||
TLS TLSConfig | ||
} | ||
|
||
// TLSConfig describes the configuration properties for TLS Connections to the Kafka Brokers | ||
type TLSConfig struct { | ||
Enabled bool | ||
CertPath string | ||
KeyPath string | ||
CaPath string | ||
} | ||
|
||
// GetTLSConfig creates TLS Configuration | ||
func (tlsConfig *TLSConfig) GetTLSConfig() (*tls.Config, error) { | ||
rootCerts, err := tlsConfig.loadCertificate() | ||
if err != nil { | ||
return nil, err | ||
} | ||
clientPrivateKey, err := tlsConfig.loadPrivateKey() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &tls.Config{ | ||
RootCAs: rootCerts, | ||
Certificates: []tls.Certificate{*clientPrivateKey}, | ||
}, nil | ||
|
||
} | ||
|
||
// loadCertificate is used to load root certification | ||
func (tlsConfig *TLSConfig) loadCertificate() (*x509.CertPool, error) { | ||
caCert, err := ioutil.ReadFile(tlsConfig.CaPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
certificates := x509.NewCertPool() | ||
certificates.AppendCertsFromPEM(caCert) | ||
return certificates, nil | ||
} | ||
|
||
// loadPrivateKey is used to load the private certificate and key for TLS | ||
func (tlsConfig *TLSConfig) loadPrivateKey() (*tls.Certificate, error) { | ||
privateKey, err := tls.LoadX509KeyPair(tlsConfig.CertPath, tlsConfig.KeyPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &privateKey, nil | ||
} |
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