Skip to content

Commit 7cfc3c1

Browse files
tobertClaude
andauthored
fix: add TLS credentials support to gRPC server (#20)
Why: gRPC TLS tests were timing out because gRPC servers require TLS credentials configured via grpc.Creds(), not just a TLS listener Approach: - Added variadic grpc.ServerOption to NewGrpcServer signature - Updated NewServer to accept optional *tls.Config and convert to grpc.Creds() - Modified test harness to pass TLS config to gRPC servers, use plain listeners - HTTP servers continue using TLS listeners (different architecture) Learned: gRPC and HTTP handle TLS differently - gRPC needs server credentials, HTTP uses TLS at listener level Next: Create PR, merge, then rebase logs branch Fixes #17 Co-authored-by: Claude <[email protected]>
1 parent 0b5188f commit 7cfc3c1

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

main_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,20 @@ func runOtelCli(t *testing.T, fixture Fixture) (string, Results) {
414414
return results.SpanCount >= fixture.Expect.SpanCount
415415
}
416416

417+
// prepare TLS configuration if needed
418+
var tlsConf *tls.Config
419+
if fixture.Config.ServerTLSEnabled {
420+
tlsConf = fixture.TlsData.serverTLSConf.Clone()
421+
if fixture.Config.ServerTLSAuthEnabled {
422+
tlsConf.ClientAuth = tls.RequireAndVerifyClientCert
423+
}
424+
}
425+
426+
// create server with TLS config if needed (gRPC requires it, HTTP uses TLS listener)
417427
var cs otlpserver.OtlpServer
418428
switch fixture.Config.ServerProtocol {
419429
case grpcProtocol:
420-
cs = otlpserver.NewServer("grpc", cb, func(otlpserver.OtlpServer) {})
430+
cs = otlpserver.NewServer("grpc", cb, func(otlpserver.OtlpServer) {}, tlsConf)
421431
case httpProtocol:
422432
cs = otlpserver.NewServer("http", cb, func(otlpserver.OtlpServer) {})
423433
}
@@ -443,11 +453,8 @@ func runOtelCli(t *testing.T, fixture Fixture) (string, Results) {
443453
// port :0 means randomly assigned port, which we copy into {{endpoint}}
444454
var listener net.Listener
445455
var err error
446-
if fixture.Config.ServerTLSEnabled {
447-
tlsConf := fixture.TlsData.serverTLSConf.Clone()
448-
if fixture.Config.ServerTLSAuthEnabled {
449-
tlsConf.ClientAuth = tls.RequireAndVerifyClientCert
450-
}
456+
if fixture.Config.ServerTLSEnabled && fixture.Config.ServerProtocol == httpProtocol {
457+
// HTTP needs a TLS listener; gRPC uses credentials passed to server
451458
listener, err = tls.Listen("tcp", "localhost:0", tlsConf)
452459
} else {
453460
listener, err = net.Listen("tcp", "localhost:0")

otlpserver/grpcserver.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ type GrpcServer struct {
2626
}
2727

2828
// NewGrpcServer takes a callback and stop function and returns a Server ready
29-
// to run with .Serve().
30-
func NewGrpcServer(cb Callback, stop Stopper) *GrpcServer {
29+
// to run with .Serve(). Optional grpc.ServerOption arguments can be provided
30+
// for TLS configuration and other server options.
31+
func NewGrpcServer(cb Callback, stop Stopper, opts ...grpc.ServerOption) *GrpcServer {
3132
s := GrpcServer{
32-
server: grpc.NewServer(),
33+
server: grpc.NewServer(opts...),
3334
callback: cb,
3435
stopper: make(chan struct{}),
3536
stopdone: make(chan struct{}, 1),

otlpserver/server.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ package otlpserver
66

77
import (
88
"context"
9+
"crypto/tls"
910
"net"
1011

1112
colv1 "go.opentelemetry.io/proto/otlp/collector/trace/v1"
1213
tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
14+
"google.golang.org/grpc"
15+
"google.golang.org/grpc/credentials"
1316
)
1417

1518
// Callback is a type for the function passed to newServer that is
@@ -30,11 +33,17 @@ type OtlpServer interface {
3033
}
3134

3235
// NewServer will start the requested server protocol, one of grpc, http/protobuf,
33-
// and http/json.
34-
func NewServer(protocol string, cb Callback, stop Stopper) OtlpServer {
36+
// and http/json. Optional TLS configuration can be provided for gRPC servers.
37+
func NewServer(protocol string, cb Callback, stop Stopper, tlsConf ...*tls.Config) OtlpServer {
3538
switch protocol {
3639
case "grpc":
37-
return NewGrpcServer(cb, stop)
40+
// if TLS config is provided, convert to gRPC credentials
41+
var opts []grpc.ServerOption
42+
if len(tlsConf) > 0 && tlsConf[0] != nil {
43+
creds := credentials.NewTLS(tlsConf[0])
44+
opts = append(opts, grpc.Creds(creds))
45+
}
46+
return NewGrpcServer(cb, stop, opts...)
3847
case "http":
3948
return NewHttpServer(cb, stop)
4049
}

0 commit comments

Comments
 (0)