-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
61 lines (49 loc) · 1.31 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
package cert
import "crypto/tls"
type TLSConfigOptions struct {
CaFilePath string
ClientCertFile string
ClientKeyFile string
ClientKeyPassword string
Insecure bool
}
func (o *TLSConfigOptions) WithCA(caFilePath string) *TLSConfigOptions {
o.CaFilePath = caFilePath
return nil
}
func (o *TLSConfigOptions) WithClientCertificate(clientCertFile string, clientKeyFile string) *TLSConfigOptions {
o.ClientCertFile = clientCertFile
o.ClientKeyFile = clientKeyFile
return o
}
func (o *TLSConfigOptions) WithClientKeyPassword(password string) *TLSConfigOptions {
o.ClientKeyPassword = password
return o
}
func (o *TLSConfigOptions) WithInsecure(insecure bool) *TLSConfigOptions {
o.Insecure = insecure
return o
}
func (o *TLSConfigOptions) TLSConfig() (*tls.Config, error) {
config := &tls.Config{}
if o.CaFilePath != "" {
pool, err := PoolFromPemFile(o.CaFilePath)
if err != nil {
return nil, err
}
config.RootCAs = pool
}
if o.ClientCertFile != "" {
cert, err := ParseCertificateFile(o.ClientCertFile, o.ClientKeyFile, o.ClientKeyPassword)
if err != nil {
return nil, err
}
config.GetClientCertificate = func(info *tls.CertificateRequestInfo) (*tls.Certificate, error) {
return &cert, nil
}
}
if o.Insecure {
config.InsecureSkipVerify = true
}
return config, nil
}