Skip to content

Commit 36e1378

Browse files
committed
ING-662: Add support for client cert authentication
ING-662: Simplify building of TLS credentials
1 parent 844df99 commit 36e1378

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

routingclient.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package gocbcoreps
22

33
import (
44
"context"
5+
"crypto/tls"
56
"crypto/x509"
6-
"go.opentelemetry.io/otel/metric"
7-
"go.opentelemetry.io/otel/trace"
87
"net"
98
"sync"
109

10+
"go.opentelemetry.io/otel/metric"
11+
"go.opentelemetry.io/otel/trace"
12+
1113
grpc_logsettable "github.com/grpc-ecosystem/go-grpc-middleware/logging/settable"
1214
"go.uber.org/zap/zapgrpc"
1315

@@ -43,7 +45,8 @@ type RoutingClient struct {
4345
var _ Conn = (*RoutingClient)(nil)
4446

4547
type DialOptions struct {
46-
RootCAs *x509.CertPool
48+
RootCAs *x509.CertPool
49+
Certificate *tls.Certificate
4750
Username string
4851
Password string
4952
Logger *zap.Logger
@@ -84,7 +87,8 @@ func DialContext(ctx context.Context, target string, opts *DialOptions) (*Routin
8487

8588
for i := uint32(0); i < poolSize; i++ {
8689
conn, err := dialRoutingConn(ctx, target, &routingConnOptions{
87-
RootCAs: opts.RootCAs,
90+
RootCAs: opts.RootCAs,
91+
Certificate: opts.Certificate,
8892
Username: opts.Username,
8993
Password: opts.Password,
9094
InsecureSkipVerify: opts.InsecureSkipVerify,

routingconn.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"crypto/tls"
66
"crypto/x509"
7+
"errors"
8+
79
"go.opentelemetry.io/otel/metric"
810
"go.opentelemetry.io/otel/propagation"
911
"go.opentelemetry.io/otel/trace"
@@ -31,7 +33,8 @@ import (
3133

3234
type routingConnOptions struct {
3335
InsecureSkipVerify bool // used for enabling TLS, but skipping verification
34-
RootCAs *x509.CertPool
36+
RootCAs *x509.CertPool
37+
Certificate *tls.Certificate
3538
Username string
3639
Password string
3740
TracerProvider trace.TracerProvider
@@ -58,22 +61,8 @@ var _ Conn = (*routingConn)(nil)
5861
const maxMsgSize = 26214400 // 25MiB
5962

6063
func dialRoutingConn(ctx context.Context, address string, opts *routingConnOptions) (*routingConn, error) {
61-
var transportDialOpt grpc.DialOption
6264
var perRpcDialOpt grpc.DialOption
6365

64-
if opts.RootCAs != nil || opts.InsecureSkipVerify {
65-
creds := credentials.NewTLS(&tls.Config{InsecureSkipVerify: opts.InsecureSkipVerify, RootCAs: opts.RootCAs})
66-
transportDialOpt = grpc.WithTransportCredentials(creds)
67-
} else { // use system certs
68-
pool, err := x509.SystemCertPool()
69-
if err != nil {
70-
return nil, err
71-
}
72-
73-
creds := credentials.NewTLS(&tls.Config{RootCAs: pool})
74-
transportDialOpt = grpc.WithTransportCredentials(creds)
75-
}
76-
7766
// setup basic auth.
7867
if opts.Username != "" && opts.Password != "" {
7968
basicAuthCreds, err := NewGrpcBasicAuth(opts.Username, opts.Password)
@@ -85,7 +74,31 @@ func dialRoutingConn(ctx context.Context, address string, opts *routingConnOptio
8574
perRpcDialOpt = nil
8675
}
8776

88-
dialOpts := []grpc.DialOption{transportDialOpt}
77+
var certificates []tls.Certificate
78+
if opts.Certificate != nil {
79+
if perRpcDialOpt != nil {
80+
return nil, errors.New("cannot use basic credentials and client cert auth at the same time")
81+
}
82+
83+
certificates = append(certificates, *opts.Certificate)
84+
}
85+
86+
pool, err := x509.SystemCertPool()
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
if opts.RootCAs != nil {
92+
pool = opts.RootCAs
93+
}
94+
95+
dialOpts := []grpc.DialOption{grpc.WithTransportCredentials(credentials.NewTLS(
96+
&tls.Config{
97+
InsecureSkipVerify: opts.InsecureSkipVerify,
98+
RootCAs: pool,
99+
Certificates: certificates,
100+
},
101+
))}
89102
if perRpcDialOpt != nil {
90103
dialOpts = append(dialOpts, perRpcDialOpt)
91104
}

0 commit comments

Comments
 (0)