Skip to content

Commit d553583

Browse files
authored
Extract gRPC TLS configuration into a shared package (#1840)
* Extract TLS flags and cert loading logic Signed-off-by: Yuri Shkuro <ys@uber.com> * Rename package Signed-off-by: Yuri Shkuro <ys@uber.com> * Refactor grpc Signed-off-by: Yuri Shkuro <ys@uber.com> * Repair tests Signed-off-by: Yuri Shkuro <ys@uber.com> * Refactor gRPC server in collector Signed-off-by: Yuri Shkuro <ys@uber.com> * Add ShowCA option Signed-off-by: Yuri Shkuro <ys@uber.com> * Switch options order Signed-off-by: Yuri Shkuro <ys@uber.com> * Separate client and server TLS options Signed-off-by: Yuri Shkuro <ys@uber.com> * Update usage Signed-off-by: Yuri Shkuro <ys@uber.com> * Rename test, add filepath.Clean Signed-off-by: Yuri Shkuro <ys@uber.com>
1 parent fcc0adb commit d553583

16 files changed

+658
-275
lines changed

cmd/agent/app/reporter/grpc/builder.go

+9-51
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,17 @@
1515
package grpc
1616

1717
import (
18-
"crypto/tls"
19-
"crypto/x509"
20-
"errors"
21-
"fmt"
22-
"io/ioutil"
2318
"strings"
2419

2520
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
21+
"github.com/pkg/errors"
2622
"go.uber.org/zap"
2723
"google.golang.org/grpc"
2824
"google.golang.org/grpc/credentials"
2925
"google.golang.org/grpc/resolver"
3026
"google.golang.org/grpc/resolver/manual"
3127

28+
"github.com/jaegertracing/jaeger/pkg/config/tlscfg"
3229
"github.com/jaegertracing/jaeger/pkg/discovery"
3330
"github.com/jaegertracing/jaeger/pkg/discovery/grpcresolver"
3431
)
@@ -38,12 +35,8 @@ type ConnBuilder struct {
3835
// CollectorHostPorts is list of host:port Jaeger Collectors.
3936
CollectorHostPorts []string `yaml:"collectorHostPorts"`
4037

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
4740

4841
DiscoveryMinPeers int
4942
Notifier discovery.Notifier
@@ -59,49 +52,14 @@ func NewConnBuilder() *ConnBuilder {
5952
func (b *ConnBuilder) CreateConnection(logger *zap.Logger) (*grpc.ClientConn, error) {
6053
var dialOptions []grpc.DialOption
6154
var dialTarget string
62-
if b.TLS { // user requested a secure connection
55+
if b.TLS.Enabled { // user requested a secure connection
6356
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")
10260
}
10361

104-
creds := credentials.NewTLS(tlsCfg)
62+
creds := credentials.NewTLS(tlsConf)
10563
dialOptions = append(dialOptions, grpc.WithTransportCredentials(creds))
10664
} else { // insecure connection
10765
logger.Info("Agent requested insecure grpc connection to collector(s)")

0 commit comments

Comments
 (0)