forked from couchbase/gocbcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dyntlsconfig.go
46 lines (38 loc) · 917 Bytes
/
dyntlsconfig.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
package gocbcore
import (
"crypto/tls"
"crypto/x509"
"net"
)
type dynTLSConfig struct {
BaseConfig *tls.Config
Provider func() *x509.CertPool
}
func (config dynTLSConfig) Clone() *dynTLSConfig {
return &dynTLSConfig{
BaseConfig: config.BaseConfig.Clone(),
Provider: config.Provider,
}
}
func (config dynTLSConfig) MakeForHost(serverName string) (*tls.Config, error) {
newConfig := config.BaseConfig.Clone()
if config.Provider != nil {
rootCAs := config.Provider()
if rootCAs != nil {
newConfig.RootCAs = rootCAs
newConfig.InsecureSkipVerify = false
} else {
newConfig.RootCAs = nil
newConfig.InsecureSkipVerify = true
}
}
newConfig.ServerName = serverName
return newConfig, nil
}
func (config dynTLSConfig) MakeForAddr(addr string) (*tls.Config, error) {
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
return config.MakeForHost(host)
}