-
Notifications
You must be signed in to change notification settings - Fork 14
/
tls.go
42 lines (34 loc) · 1 KB
/
tls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package kafka
import (
"crypto/tls"
"crypto/x509"
"fmt"
"os"
)
type TLSConfig struct {
RootCAPath string
IntermediateCAPath string
}
func (c *TLSConfig) TLSConfig() (*tls.Config, error) {
rootCA, err := os.ReadFile(c.RootCAPath)
if err != nil {
return nil, fmt.Errorf("Error while reading Root CA file: " + c.RootCAPath + " error: " + err.Error())
}
interCA, err := os.ReadFile(c.IntermediateCAPath)
if err != nil {
return nil, fmt.Errorf("Error while reading Intermediate CA file: " + c.IntermediateCAPath + " error: " + err.Error())
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(rootCA)
caCertPool.AppendCertsFromPEM(interCA)
return &tls.Config{RootCAs: caCertPool}, nil //nolint:gosec
}
func (c *TLSConfig) IsEmpty() bool {
return c == nil || c.RootCAPath == "" && c.IntermediateCAPath == ""
}
func (c *TLSConfig) JSON() string {
if c == nil {
return "{}"
}
return fmt.Sprintf(`{"RootCAPath": %q, "IntermediateCAPath": %q}`, c.RootCAPath, c.IntermediateCAPath)
}