Skip to content

Commit 932370b

Browse files
authored
Add support for mTLS (#3136)
This PR adds two new flags to the agent to accept a client certificate and a client key, so it can authenticate with the server via `mTLS`.
2 parents 515e856 + 46c0ec0 commit 932370b

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

flags/flags.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ type FlagsRemoteStore struct {
342342
GRPCConnectionTimeout time.Duration `default:"3s" help:"The timeout duration for gRPC connection establishment."`
343343
GRPCMaxConnectionRetries uint32 `default:"5" help:"The maximum number of retries to establish a gRPC connection."`
344344
GRPCHeaders map[string]string `help:"Additional gRPC headers to send with each request (key=value pairs)."`
345+
346+
ClientCert string `help:"Client certificate for mTLS"`
347+
ClientKey string `help:"Client key for mTLS"`
345348
}
346349

347350
// FlagsDebuginfo contains flags to configure debuginfo.

flags/grpc.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/tls"
66
"fmt"
7+
"net/url"
78
"os"
89
"strings"
910
"time"
@@ -83,12 +84,27 @@ func (f FlagsRemoteStore) setupGrpcConnection(parent context.Context, metrics *g
8384
if f.Insecure {
8485
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
8586
} else {
86-
opts = append(opts,
87-
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
88-
// Support only TLS1.3+ with valid CA certificates
89-
MinVersion: tls.VersionTLS13,
90-
InsecureSkipVerify: f.InsecureSkipVerify,
91-
})))
87+
tlsConfig := tls.Config{
88+
// Support only TLS1.3+ with valid CA certificates
89+
MinVersion: tls.VersionTLS13,
90+
InsecureSkipVerify: f.InsecureSkipVerify,
91+
}
92+
93+
if f.ClientKey != "" && f.ClientCert != "" {
94+
cert, err := tls.LoadX509KeyPair(f.ClientCert, f.ClientKey)
95+
if err != nil {
96+
return nil, fmt.Errorf("failed to load client certificates: %w", err)
97+
}
98+
tlsConfig.Certificates = []tls.Certificate{cert}
99+
100+
url, err := url.Parse(f.Address)
101+
if err != nil {
102+
return nil, fmt.Errorf("couldn't parse address (%s): %w", f.Address, err)
103+
}
104+
tlsConfig.ServerName = url.Hostname()
105+
}
106+
107+
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(&tlsConfig)))
92108
}
93109

94110
// Auth

0 commit comments

Comments
 (0)