...
1
16
17 package tlsutil
18
19 import (
20 "crypto/tls"
21 "crypto/x509"
22 "os"
23
24 "github.com/pkg/errors"
25 )
26
27
28 type Options struct {
29 CaCertFile string
30
31 KeyFile string
32 CertFile string
33
34 InsecureSkipVerify bool
35 }
36
37
38 func ClientConfig(opts Options) (cfg *tls.Config, err error) {
39 var cert *tls.Certificate
40 var pool *x509.CertPool
41
42 if opts.CertFile != "" || opts.KeyFile != "" {
43 if cert, err = CertFromFilePair(opts.CertFile, opts.KeyFile); err != nil {
44 if os.IsNotExist(err) {
45 return nil, errors.Wrapf(err, "could not load x509 key pair (cert: %q, key: %q)", opts.CertFile, opts.KeyFile)
46 }
47 return nil, errors.Wrapf(err, "could not read x509 key pair (cert: %q, key: %q)", opts.CertFile, opts.KeyFile)
48 }
49 }
50 if !opts.InsecureSkipVerify && opts.CaCertFile != "" {
51 if pool, err = CertPoolFromFile(opts.CaCertFile); err != nil {
52 return nil, err
53 }
54 }
55
56 cfg = &tls.Config{InsecureSkipVerify: opts.InsecureSkipVerify, Certificates: []tls.Certificate{*cert}, RootCAs: pool}
57 return cfg, nil
58 }
59
View as plain text