-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
config.go
185 lines (161 loc) · 5 KB
/
config.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package kafka
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"log"
"time"
"github.com/Shopify/sarama"
)
type Config struct {
BootstrapServers *[]string
Timeout int
CACert string
ClientCert string
ClientCertKey string
ClientCertKeyPassphrase string
TLSEnabled bool
SkipTLSVerify bool
SASLUsername string
SASLPassword string
SASLMechanism string
}
func (c *Config) newKafkaConfig() (*sarama.Config, error) {
kafkaConfig := sarama.NewConfig()
kafkaConfig.Version = sarama.V2_6_0_0
kafkaConfig.ClientID = "terraform-provider-kafka"
kafkaConfig.Admin.Timeout = time.Duration(c.Timeout) * time.Second
kafkaConfig.Metadata.Full = true // the default, but just being clear
kafkaConfig.Metadata.AllowAutoTopicCreation = false
if c.saslEnabled() {
switch c.SASLMechanism {
case "scram-sha512":
kafkaConfig.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &XDGSCRAMClient{HashGeneratorFcn: SHA512} }
kafkaConfig.Net.SASL.Mechanism = sarama.SASLMechanism(sarama.SASLTypeSCRAMSHA512)
case "scram-sha256":
kafkaConfig.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &XDGSCRAMClient{HashGeneratorFcn: SHA256} }
kafkaConfig.Net.SASL.Mechanism = sarama.SASLMechanism(sarama.SASLTypeSCRAMSHA256)
case "plain":
default:
log.Fatalf("[ERROR] Invalid sasl mechanism \"%s\": can only be \"scram-sha256\", \"scram-sha512\" or \"plain\"", c.SASLMechanism)
}
kafkaConfig.Net.SASL.Enable = true
kafkaConfig.Net.SASL.Password = c.SASLPassword
kafkaConfig.Net.SASL.User = c.SASLUsername
kafkaConfig.Net.SASL.Handshake = true
} else {
log.Printf("[WARN] SASL disabled username: '%s', password '%s'", c.SASLUsername, "****")
}
if c.TLSEnabled {
tlsConfig, err := newTLSConfig(
c.ClientCert,
c.ClientCertKey,
c.CACert,
c.ClientCertKeyPassphrase,
)
if err != nil {
return kafkaConfig, err
}
kafkaConfig.Net.TLS.Enable = true
kafkaConfig.Net.TLS.Config = tlsConfig
kafkaConfig.Net.TLS.Config.InsecureSkipVerify = c.SkipTLSVerify
}
return kafkaConfig, nil
}
func (c *Config) saslEnabled() bool {
return c.SASLUsername != "" || c.SASLPassword != ""
}
func NewTLSConfig(clientCert, clientKey, caCert, clientKeyPassphrase string) (*tls.Config, error) {
return newTLSConfig(clientCert, clientKey, caCert, clientKeyPassphrase)
}
func parsePemOrLoadFromFile(input string) (*pem.Block, []byte, error) {
// attempt to parse
var inputBytes = []byte(input)
inputBlock, _ := pem.Decode(inputBytes)
if inputBlock == nil {
//attempt to load from file
log.Printf("[INFO] Attempting to load from file '%s'", input)
var err error
inputBytes, err = ioutil.ReadFile(input)
if err != nil {
return nil, nil, err
}
inputBlock, _ = pem.Decode(inputBytes)
if inputBlock == nil {
return nil, nil, fmt.Errorf("[ERROR] Error unable to decode pem")
}
}
return inputBlock, inputBytes, nil
}
func newTLSConfig(clientCert, clientKey, caCert, clientKeyPassphrase string) (*tls.Config, error) {
tlsConfig := tls.Config{}
if clientCert != "" && clientKey != "" {
_, certBytes, err := parsePemOrLoadFromFile(clientCert)
if err != nil {
log.Printf("[ERROR] Unable to read certificate %s", err)
return &tlsConfig, err
}
keyBlock, keyBytes, err := parsePemOrLoadFromFile(clientKey)
if err != nil {
log.Printf("[ERROR] Unable to read private key %s", err)
return &tlsConfig, err
}
if x509.IsEncryptedPEMBlock(keyBlock) { //nolint:staticcheck
log.Printf("[INFO] Using encrypted private key")
var err error
keyBytes, err = x509.DecryptPEMBlock(keyBlock, []byte(clientKeyPassphrase)) //nolint:staticcheck
if err != nil {
log.Printf("[ERROR] Error decrypting private key with passphrase %s", err)
return &tlsConfig, err
}
keyBytes = pem.EncodeToMemory(&pem.Block{
Type: keyBlock.Type,
Bytes: keyBytes,
})
}
cert, err := tls.X509KeyPair(certBytes, keyBytes)
if err != nil {
log.Printf("[ERROR] Error creating X509KeyPair %s", err)
return &tlsConfig, err
}
tlsConfig.Certificates = []tls.Certificate{cert}
}
if caCert == "" {
log.Println("[WARN] no CA file set skipping")
return &tlsConfig, nil
}
caCertPool, _ := x509.SystemCertPool()
if caCertPool == nil {
caCertPool = x509.NewCertPool()
}
_, caBytes, err := parsePemOrLoadFromFile(caCert)
if err != nil {
log.Printf("[ERROR] Unable to read CA %s", err)
return &tlsConfig, err
}
ok := caCertPool.AppendCertsFromPEM(caBytes)
log.Printf("[TRACE] set cert pool %v", ok)
if !ok {
return &tlsConfig, fmt.Errorf("Couldn't add the caPem")
}
tlsConfig.RootCAs = caCertPool
return &tlsConfig, nil
}
func (config *Config) copyWithMaskedSensitiveValues() Config {
copy := Config{
config.BootstrapServers,
config.Timeout,
config.CACert,
config.ClientCert,
"*****",
"*****",
config.TLSEnabled,
config.SkipTLSVerify,
config.SASLUsername,
"*****",
config.SASLMechanism,
}
return copy
}