Skip to content

Commit 78e7d2f

Browse files
committed
feat(tlsConfig): Add possiblity to trust provided CA and host at the same time
Signed-off-by: Davin Kevin <davin.kevin@gmail.com>
1 parent 4e5fd42 commit 78e7d2f

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

exthttp/tlsconfig.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
type TLSConfig struct {
1515
// The CA cert to use for the targets.
1616
CAFile string `yaml:"ca_file"`
17+
// Trust RootCAs provided by the host
18+
TrustRootCA bool `yaml:"trust_root_ca"`
1719
// The client cert file for the targets.
1820
CertFile string `yaml:"cert_file"`
1921
// The client key file for the targets.
@@ -34,7 +36,7 @@ func NewTLSConfig(cfg *TLSConfig) (*tls.Config, error) {
3436
if err != nil {
3537
return nil, err
3638
}
37-
if !updateRootCA(tlsConfig, b) {
39+
if !updateRootCA(tlsConfig, b, cfg.TrustRootCA) {
3840
return nil, fmt.Errorf("unable to use specified CA cert %s", cfg.CAFile)
3941
}
4042
}
@@ -68,8 +70,18 @@ func readCAFile(f string) ([]byte, error) {
6870
}
6971

7072
// updateRootCA parses the given byte slice as a series of PEM encoded certificates and updates tls.Config.RootCAs.
71-
func updateRootCA(cfg *tls.Config, b []byte) bool {
72-
caCertPool := x509.NewCertPool()
73+
func updateRootCA(cfg *tls.Config, b []byte, trustRootCA bool) bool {
74+
var caCertPool *x509.CertPool
75+
var err error
76+
if trustRootCA {
77+
caCertPool, err = x509.SystemCertPool()
78+
if err != nil {
79+
caCertPool = x509.NewCertPool()
80+
}
81+
} else {
82+
caCertPool = x509.NewCertPool()
83+
}
84+
7385
if !caCertPool.AppendCertsFromPEM(b) {
7486
return false
7587
}

0 commit comments

Comments
 (0)