Skip to content

Commit 4fed89b

Browse files
committed
tlsconfig: certPool: pass options as argument
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent badbdc1 commit 4fed89b

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

tlsconfig/config.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,29 @@ func defaultConfig(ops ...func(*tls.Config)) *tls.Config {
7575
}
7676

7777
// certPool returns an X.509 certificate pool from `caFile`, the certificate file.
78-
func certPool(caFile string, exclusivePool bool) (*x509.CertPool, error) {
78+
func certPool(opts Options) (*x509.CertPool, error) {
7979
// If we should verify the server, we need to load a trusted ca
8080
var (
8181
pool *x509.CertPool
8282
err error
8383
)
84-
if exclusivePool {
84+
if opts.ExclusiveRootPools {
8585
pool = x509.NewCertPool()
8686
} else {
8787
pool, err = x509.SystemCertPool()
8888
if err != nil {
8989
return nil, fmt.Errorf("failed to read system certificates: %v", err)
9090
}
9191
}
92-
pemData, err := os.ReadFile(caFile)
92+
if opts.CAFile == "" {
93+
return pool, nil
94+
}
95+
pemData, err := os.ReadFile(opts.CAFile)
9396
if err != nil {
94-
return nil, fmt.Errorf("could not read CA certificate %q: %v", caFile, err)
97+
return nil, fmt.Errorf("could not read CA certificate %q: %v", opts.CAFile, err)
9598
}
9699
if !pool.AppendCertsFromPEM(pemData) {
97-
return nil, fmt.Errorf("failed to append certificates from PEM file: %q", caFile)
100+
return nil, fmt.Errorf("failed to append certificates from PEM file: %q", opts.CAFile)
98101
}
99102
return pool, nil
100103
}
@@ -197,7 +200,7 @@ func Client(options Options) (*tls.Config, error) {
197200
tlsConfig := defaultConfig()
198201
tlsConfig.InsecureSkipVerify = options.InsecureSkipVerify
199202
if !options.InsecureSkipVerify && options.CAFile != "" {
200-
CAs, err := certPool(options.CAFile, options.ExclusiveRootPools)
203+
CAs, err := certPool(options)
201204
if err != nil {
202205
return nil, err
203206
}
@@ -230,7 +233,7 @@ func Server(options Options) (*tls.Config, error) {
230233
}
231234
tlsConfig.Certificates = []tls.Certificate{tlsCert}
232235
if options.ClientAuth >= tls.VerifyClientCertIfGiven && options.CAFile != "" {
233-
CAs, err := certPool(options.CAFile, options.ExclusiveRootPools)
236+
CAs, err := certPool(options)
234237
if err != nil {
235238
return nil, err
236239
}

0 commit comments

Comments
 (0)