15
15
package grpc
16
16
17
17
import (
18
- "crypto/tls"
19
- "crypto/x509"
20
- "errors"
21
- "fmt"
22
- "io/ioutil"
23
18
"strings"
24
19
25
20
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
21
+ "github.com/pkg/errors"
26
22
"go.uber.org/zap"
27
23
"google.golang.org/grpc"
28
24
"google.golang.org/grpc/credentials"
29
25
"google.golang.org/grpc/resolver"
30
26
"google.golang.org/grpc/resolver/manual"
31
27
28
+ "github.com/jaegertracing/jaeger/pkg/config/tlscfg"
32
29
"github.com/jaegertracing/jaeger/pkg/discovery"
33
30
"github.com/jaegertracing/jaeger/pkg/discovery/grpcresolver"
34
31
)
@@ -38,12 +35,8 @@ type ConnBuilder struct {
38
35
// CollectorHostPorts is list of host:port Jaeger Collectors.
39
36
CollectorHostPorts []string `yaml:"collectorHostPorts"`
40
37
41
- MaxRetry uint
42
- TLS bool
43
- TLSCA string
44
- TLSServerName string
45
- TLSCert string
46
- TLSKey string
38
+ MaxRetry uint
39
+ TLS tlscfg.Options
47
40
48
41
DiscoveryMinPeers int
49
42
Notifier discovery.Notifier
@@ -59,49 +52,14 @@ func NewConnBuilder() *ConnBuilder {
59
52
func (b * ConnBuilder ) CreateConnection (logger * zap.Logger ) (* grpc.ClientConn , error ) {
60
53
var dialOptions []grpc.DialOption
61
54
var dialTarget string
62
- if b .TLS { // user requested a secure connection
55
+ if b .TLS . Enabled { // user requested a secure connection
63
56
logger .Info ("Agent requested secure grpc connection to collector(s)" )
64
- var err error
65
- var certPool * x509.CertPool
66
- if len (b .TLSCA ) == 0 { // no truststore given, use SystemCertPool
67
- certPool , err = systemCertPool ()
68
- if err != nil {
69
- return nil , err
70
- }
71
- } else { // setup user specified truststore
72
- caPEM , err := ioutil .ReadFile (b .TLSCA )
73
- if err != nil {
74
- return nil , fmt .Errorf ("reading client CA failed, %v" , err )
75
- }
76
-
77
- certPool = x509 .NewCertPool ()
78
- if ! certPool .AppendCertsFromPEM (caPEM ) {
79
- return nil , fmt .Errorf ("building client CA failed, %v" , err )
80
- }
81
- }
82
-
83
- tlsCfg := & tls.Config {
84
- MinVersion : tls .VersionTLS12 ,
85
- RootCAs : certPool ,
86
- ServerName : b .TLSServerName ,
87
- }
88
-
89
- if (b .TLSKey == "" || b .TLSCert == "" ) &&
90
- (b .TLSKey != "" || b .TLSCert != "" ) {
91
- return nil , fmt .Errorf ("for client auth, both client certificate and key must be supplied" )
92
- }
93
-
94
- if b .TLSKey != "" && b .TLSCert != "" {
95
- tlsCert , err := tls .LoadX509KeyPair (b .TLSCert , b .TLSKey )
96
- if err != nil {
97
- return nil , fmt .Errorf ("could not load server TLS cert and key, %v" , err )
98
- }
99
-
100
- logger .Info ("client TLS authentication enabled" )
101
- tlsCfg .Certificates = []tls.Certificate {tlsCert }
57
+ tlsConf , err := b .TLS .Config ()
58
+ if err != nil {
59
+ return nil , errors .Wrap (err , "failed to load TLS config" )
102
60
}
103
61
104
- creds := credentials .NewTLS (tlsCfg )
62
+ creds := credentials .NewTLS (tlsConf )
105
63
dialOptions = append (dialOptions , grpc .WithTransportCredentials (creds ))
106
64
} else { // insecure connection
107
65
logger .Info ("Agent requested insecure grpc connection to collector(s)" )
0 commit comments