Skip to content

Commit bbfa687

Browse files
committed
Move service initialization to closure
This enforces types at compile time and moves away from an unbounded any type. This is all internal code anyways so there's little risk to either choice. Signed-off-by: Hayden <8418760+Hayden-IO@users.noreply.github.com>
1 parent 7c45f24 commit bbfa687

11 files changed

Lines changed: 79 additions & 41 deletions

File tree

cmd/rekor-server/aws/app/serve.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"log/slog"
2424
"os"
2525

26+
"google.golang.org/grpc"
27+
"google.golang.org/grpc/health/grpc_health_v1"
2628
"k8s.io/klog/v2"
2729

2830
"github.com/spf13/cobra"
@@ -35,6 +37,7 @@ import (
3537
"github.com/sigstore/rekor-tiles/v2/internal/tessera"
3638
awsDriver "github.com/sigstore/rekor-tiles/v2/internal/tessera/aws"
3739
"github.com/sigstore/rekor-tiles/v2/internal/tessera/aws/signerverifier"
40+
pb "github.com/sigstore/rekor-tiles/v2/pkg/generated/protobuf"
3841
"github.com/sigstore/rekor-tiles/v2/pkg/note"
3942
"github.com/sigstore/sigstore/pkg/signature/options"
4043
)
@@ -180,7 +183,11 @@ var serveCmd = &cobra.Command{
180183
server.WithTLSCredentials(viper.GetString("grpc-tls-cert-file"), viper.GetString("grpc-tls-key-file")),
181184
),
182185
viper.GetDuration("tlog-timeout"),
183-
rekorServer,
186+
func(s *grpc.Server) {
187+
pb.RegisterRekorServer(s, rekorServer)
188+
grpc_health_v1.RegisterHealthServer(s, rekorServer)
189+
},
190+
pb.RegisterRekorHandlerFromEndpoint,
184191
shutdownFn,
185192
)
186193
},

cmd/rekor-server/gcp/app/serve.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
2929
"google.golang.org/api/option"
3030
"google.golang.org/grpc"
31+
"google.golang.org/grpc/health/grpc_health_v1"
3132
"k8s.io/klog/v2"
3233

3334
"github.com/spf13/cobra"
@@ -40,6 +41,7 @@ import (
4041
"github.com/sigstore/rekor-tiles/v2/internal/tessera"
4142
gcpDriver "github.com/sigstore/rekor-tiles/v2/internal/tessera/gcp"
4243
"github.com/sigstore/rekor-tiles/v2/internal/tessera/gcp/signerverifier"
44+
pb "github.com/sigstore/rekor-tiles/v2/pkg/generated/protobuf"
4345
"github.com/sigstore/rekor-tiles/v2/pkg/note"
4446
"github.com/sigstore/sigstore/pkg/signature"
4547
"github.com/sigstore/sigstore/pkg/signature/kms/gcp"
@@ -191,7 +193,11 @@ var serveCmd = &cobra.Command{
191193
server.WithTLSCredentials(viper.GetString("grpc-tls-cert-file"), viper.GetString("grpc-tls-key-file")),
192194
),
193195
viper.GetDuration("tlog-timeout"),
194-
rekorServer,
196+
func(s *grpc.Server) {
197+
pb.RegisterRekorServer(s, rekorServer)
198+
grpc_health_v1.RegisterHealthServer(s, rekorServer)
199+
},
200+
pb.RegisterRekorHandlerFromEndpoint,
195201
shutdownFn,
196202
)
197203
},

cmd/rekor-server/gcpcloudsql/app/serve.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
2929
"google.golang.org/api/option"
3030
"google.golang.org/grpc"
31+
"google.golang.org/grpc/health/grpc_health_v1"
3132
"k8s.io/klog/v2"
3233

3334
"github.com/spf13/cobra"
@@ -40,6 +41,7 @@ import (
4041
"github.com/sigstore/rekor-tiles/v2/internal/tessera"
4142
"github.com/sigstore/rekor-tiles/v2/internal/tessera/gcp/signerverifier"
4243
gcpSQLDriver "github.com/sigstore/rekor-tiles/v2/internal/tessera/gcpcloudsql"
44+
pb "github.com/sigstore/rekor-tiles/v2/pkg/generated/protobuf"
4345
"github.com/sigstore/rekor-tiles/v2/pkg/note"
4446
"github.com/sigstore/sigstore/pkg/signature"
4547
"github.com/sigstore/sigstore/pkg/signature/kms/gcp"
@@ -190,7 +192,11 @@ var serveCmd = &cobra.Command{
190192
server.WithTLSCredentials(viper.GetString("grpc-tls-cert-file"), viper.GetString("grpc-tls-key-file")),
191193
),
192194
viper.GetDuration("tlog-timeout"),
193-
rekorServer,
195+
func(s *grpc.Server) {
196+
pb.RegisterRekorServer(s, rekorServer)
197+
grpc_health_v1.RegisterHealthServer(s, rekorServer)
198+
},
199+
pb.RegisterRekorHandlerFromEndpoint,
194200
shutdownFn,
195201
)
196202
},

cmd/rekor-server/identity-posix/app/serve.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"log/slog"
2424
"os"
2525

26+
"google.golang.org/grpc"
27+
"google.golang.org/grpc/health/grpc_health_v1"
2628
"k8s.io/klog/v2"
2729

2830
"github.com/spf13/cobra"
@@ -35,6 +37,7 @@ import (
3537
"github.com/sigstore/rekor-tiles/v2/internal/signerverifier"
3638
"github.com/sigstore/rekor-tiles/v2/internal/tessera"
3739
posixDriver "github.com/sigstore/rekor-tiles/v2/internal/tessera/posix"
40+
pb "github.com/sigstore/rekor-tiles/v2/pkg/generated/protobuf"
3841
"github.com/sigstore/rekor-tiles/v2/pkg/note"
3942
"github.com/sigstore/sigstore/pkg/signature/options"
4043
)
@@ -176,7 +179,11 @@ var serveCmd = &cobra.Command{
176179
server.WithTLSCredentials(viper.GetString("grpc-tls-cert-file"), viper.GetString("grpc-tls-key-file")),
177180
),
178181
viper.GetDuration("tlog-timeout"),
179-
rekorServer,
182+
func(s *grpc.Server) {
183+
pb.RegisterIdentityRekorServer(s, rekorServer)
184+
grpc_health_v1.RegisterHealthServer(s, rekorServer)
185+
},
186+
pb.RegisterIdentityRekorHandlerFromEndpoint,
180187
shutdownFn,
181188
)
182189
},

cmd/rekor-server/posix/app/serve.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"log/slog"
2323
"os"
2424

25+
"google.golang.org/grpc"
26+
"google.golang.org/grpc/health/grpc_health_v1"
2527
"k8s.io/klog/v2"
2628

2729
"github.com/spf13/cobra"
@@ -34,6 +36,7 @@ import (
3436
"github.com/sigstore/rekor-tiles/v2/internal/signerverifier"
3537
"github.com/sigstore/rekor-tiles/v2/internal/tessera"
3638
posixDriver "github.com/sigstore/rekor-tiles/v2/internal/tessera/posix"
39+
pb "github.com/sigstore/rekor-tiles/v2/pkg/generated/protobuf"
3740
"github.com/sigstore/rekor-tiles/v2/pkg/note"
3841
"github.com/sigstore/sigstore/pkg/signature/options"
3942
)
@@ -161,7 +164,11 @@ var serveCmd = &cobra.Command{
161164
server.WithTLSCredentials(viper.GetString("grpc-tls-cert-file"), viper.GetString("grpc-tls-key-file")),
162165
),
163166
viper.GetDuration("tlog-timeout"),
164-
rekorServer,
167+
func(s *grpc.Server) {
168+
pb.RegisterRekorServer(s, rekorServer)
169+
grpc_health_v1.RegisterHealthServer(s, rekorServer)
170+
},
171+
pb.RegisterRekorHandlerFromEndpoint,
165172
shutdownFn,
166173
)
167174
},

internal/server/grpc.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@ import (
3939

4040
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
4141
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
42-
pb "github.com/sigstore/rekor-tiles/v2/pkg/generated/protobuf"
4342
"google.golang.org/grpc"
4443
"google.golang.org/grpc/codes"
4544
"google.golang.org/grpc/credentials"
46-
"google.golang.org/grpc/health/grpc_health_v1"
4745
"google.golang.org/grpc/keepalive"
4846
"google.golang.org/grpc/reflection"
4947
"google.golang.org/grpc/status"
@@ -52,11 +50,10 @@ import (
5250
type grpcServer struct {
5351
*grpc.Server
5452
serverEndpoint string
55-
serverImpl any
5653
}
5754

5855
// newGRPCServer starts a new grpc server and registers the services.
59-
func newGRPCServer(config *GRPCConfig, server any) *grpcServer {
56+
func newGRPCServer(config *GRPCConfig, register RegisterGRPCFunc) *grpcServer {
6057
var opts []grpc.ServerOption
6158

6259
grpcPanicRecoveryHandler := func(p any) (err error) {
@@ -88,15 +85,7 @@ func newGRPCServer(config *GRPCConfig, server any) *grpcServer {
8885

8986
s := grpc.NewServer(opts...)
9087

91-
if r, ok := server.(pb.RekorServer); ok {
92-
pb.RegisterRekorServer(s, r)
93-
}
94-
if i, ok := server.(pb.IdentityRekorServer); ok {
95-
pb.RegisterIdentityRekorServer(s, i)
96-
}
97-
if h, ok := server.(grpc_health_v1.HealthServer); ok {
98-
grpc_health_v1.RegisterHealthServer(s, h)
99-
}
88+
register(s)
10089

10190
reflection.Register(s)
10291

@@ -106,7 +95,6 @@ func newGRPCServer(config *GRPCConfig, server any) *grpcServer {
10695
return &grpcServer{
10796
Server: s,
10897
serverEndpoint: config.GRPCTarget(),
109-
serverImpl: server,
11098
}
11199
}
112100

internal/server/http.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"google.golang.org/protobuf/encoding/protojson"
3434

3535
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
36-
pb "github.com/sigstore/rekor-tiles/v2/pkg/generated/protobuf"
3736
"google.golang.org/grpc"
3837
"google.golang.org/grpc/credentials"
3938
"google.golang.org/grpc/credentials/insecure"
@@ -52,7 +51,7 @@ type httpProxy struct {
5251
}
5352

5453
// newHTTProxy creates a mux for each of the service grpc methods, including the grpc heatlhcheck.
55-
func newHTTPProxy(ctx context.Context, config *HTTPConfig, grpcServer *grpcServer) *httpProxy {
54+
func newHTTPProxy(ctx context.Context, config *HTTPConfig, grpcServer *grpcServer, register RegisterHTTPFunc) *httpProxy {
5655
// configure a custom marshaler to fail on unknown fields
5756
strictMarshaler := runtime.HTTPBodyMarshaler{
5857
Marshaler: &runtime.JSONPb{
@@ -88,19 +87,9 @@ func newHTTPProxy(ctx context.Context, config *HTTPConfig, grpcServer *grpcServe
8887
runtime.WithHealthzEndpoint(grpc_health_v1.NewHealthClient(cc)), // localhost:[port]/healthz
8988
)
9089

91-
if _, ok := grpcServer.serverImpl.(pb.RekorServer); ok {
92-
err = pb.RegisterRekorHandlerFromEndpoint(ctx, mux, grpcServer.serverEndpoint, opts)
93-
if err != nil {
94-
slog.Error("failed to register rekor handler", "error", err)
95-
os.Exit(1)
96-
}
97-
}
98-
if _, ok := grpcServer.serverImpl.(pb.IdentityRekorServer); ok {
99-
err = pb.RegisterIdentityRekorHandlerFromEndpoint(ctx, mux, grpcServer.serverEndpoint, opts)
100-
if err != nil {
101-
slog.Error("failed to register identity handler", "error", err)
102-
os.Exit(1)
103-
}
90+
if err = register(ctx, mux, grpcServer.serverEndpoint, opts); err != nil {
91+
slog.Error("failed to register handler", "error", err)
92+
os.Exit(1)
10493
}
10594

10695
metrics := getMetrics()

internal/server/serve.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,17 @@ import (
2020
"os"
2121
"sync"
2222
"time"
23+
24+
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
25+
"google.golang.org/grpc"
2326
)
2427

2528
// Serve starts the grpc server and its http proxy.
26-
func Serve(ctx context.Context, hc *HTTPConfig, gc *GRPCConfig, tesseraTimeout time.Duration, s any, tesseraShutdownFn func(context.Context) error) {
29+
type RegisterGRPCFunc func(*grpc.Server)
30+
type RegisterHTTPFunc func(context.Context, *runtime.ServeMux, string, []grpc.DialOption) error
31+
32+
// Serve starts the grpc server and its http proxy.
33+
func Serve(ctx context.Context, hc *HTTPConfig, gc *GRPCConfig, tesseraTimeout time.Duration, registerGRPC RegisterGRPCFunc, registerHTTP RegisterHTTPFunc, tesseraShutdownFn func(context.Context) error) {
2734
var wg sync.WaitGroup
2835

2936
if hc.port == 0 || gc.port == 0 {
@@ -35,10 +42,10 @@ func Serve(ctx context.Context, hc *HTTPConfig, gc *GRPCConfig, tesseraTimeout t
3542
os.Exit(1)
3643
}
3744

38-
grpcServer := newGRPCServer(gc, s)
45+
grpcServer := newGRPCServer(gc, registerGRPC)
3946
grpcServer.start(&wg)
4047

41-
httpProxy := newHTTPProxy(ctx, hc, grpcServer)
48+
httpProxy := newHTTPProxy(ctx, hc, grpcServer, registerHTTP)
4249
httpProxy.start(&wg)
4350

4451
httpMetrics := newHTTPMetrics(ctx, hc)

internal/server/serve_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import (
2121
"syscall"
2222
"testing"
2323
"time"
24+
25+
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
26+
"google.golang.org/grpc"
2427
)
2528

2629
func TestServe(t *testing.T) {
@@ -32,7 +35,12 @@ func TestServe(t *testing.T) {
3235
}
3336
go func() {
3437
pid.Store(uint64(syscall.Getpid())) // Process IDs are positive ints
35-
Serve(context.Background(), NewHTTPConfig(), NewGRPCConfig(), 1*time.Second, nil, shutdownFn)
38+
Serve(context.Background(), NewHTTPConfig(), NewGRPCConfig(), 1*time.Second,
39+
func(_ *grpc.Server) {},
40+
func(_ context.Context, _ *runtime.ServeMux, _ string, _ []grpc.DialOption) error {
41+
return nil
42+
},
43+
shutdownFn)
3644
wg.Done()
3745
}()
3846
// One for Serve returning, one for shutdown function being invoked

internal/server/service_mock.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
pb "github.com/sigstore/rekor-tiles/v2/pkg/generated/protobuf"
4141
"github.com/transparency-dev/tessera/api/layout"
4242
"google.golang.org/genproto/googleapis/api/httpbody"
43+
"google.golang.org/grpc"
4344
"google.golang.org/grpc/health/grpc_health_v1"
4445
"google.golang.org/protobuf/types/known/emptypb"
4546
)
@@ -68,7 +69,13 @@ func (ms *MockServer) Start(t *testing.T) {
6869
// Start the server
6970
ms.wg = &sync.WaitGroup{}
7071
go func() {
71-
Serve(context.Background(), ms.hc, ms.gc, 1*time.Second, s, shutdownFn)
72+
Serve(context.Background(), ms.hc, ms.gc, 1*time.Second,
73+
func(srv *grpc.Server) {
74+
pb.RegisterRekorServer(srv, s)
75+
grpc_health_v1.RegisterHealthServer(srv, s)
76+
},
77+
pb.RegisterRekorHandlerFromEndpoint,
78+
shutdownFn)
7279
ms.wg.Done()
7380
}()
7481
ms.wg.Add(1)
@@ -116,7 +123,13 @@ func (ms *MockServer) StartTLS(t *testing.T) {
116123
// Start the server
117124
ms.wg = &sync.WaitGroup{}
118125
go func() {
119-
Serve(context.Background(), ms.hc, ms.gc, 1*time.Second, s, shutdownFn)
126+
Serve(context.Background(), ms.hc, ms.gc, 1*time.Second,
127+
func(srv *grpc.Server) {
128+
pb.RegisterRekorServer(srv, s)
129+
grpc_health_v1.RegisterHealthServer(srv, s)
130+
},
131+
pb.RegisterRekorHandlerFromEndpoint,
132+
shutdownFn)
120133
ms.wg.Done()
121134
}()
122135
ms.wg.Add(1)

0 commit comments

Comments
 (0)