Skip to content

Commit

Permalink
Log fatal on ListenAndServe error, make domain socket configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Hayden Blauzvern <[email protected]>
  • Loading branch information
haydentherapper committed Sep 6, 2023
1 parent 71f9651 commit cefc964
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
10 changes: 5 additions & 5 deletions cmd/app/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (g *grpcServer) startTCPListener(wg *sync.WaitGroup) {
defer g.tlsCertWatcher.Close()
}
if err := g.Server.Serve(lis); err != nil {
log.Logger.Errorf("error shutting down grpcServer: %w", err)
log.Logger.Fatalf("error shutting down grpcServer: %w", err)
}
<-idleConnsClosed
wg.Done()
Expand All @@ -240,12 +240,12 @@ func (g *grpcServer) startUnixListener() {
if runtime.GOOS != "linux" {
// As MacOS doesn't have abstract unix domain sockets the file
// created by a previous run needs to be explicitly removed
if err := os.RemoveAll(LegacyUnixDomainSocket); err != nil {
if err := os.RemoveAll(g.grpcServerEndpoint); err != nil {
log.Logger.Fatal(err)
}
}

unixAddr, err := net.ResolveUnixAddr("unix", LegacyUnixDomainSocket)
unixAddr, err := net.ResolveUnixAddr("unix", g.grpcServerEndpoint)
if err != nil {
log.Logger.Fatal(err)
}
Expand All @@ -265,7 +265,7 @@ func (g *grpcServer) ExposesGRPCTLS() bool {
return viper.IsSet("grpc-tls-certificate") && viper.IsSet("grpc-tls-key")
}

func createLegacyGRPCServer(cfg *config.FulcioConfig, v2Server gw.CAServer) (*grpcServer, error) {
func createLegacyGRPCServer(cfg *config.FulcioConfig, unixDomainSocket string, v2Server gw.CAServer) (*grpcServer, error) {
logger, opts := log.SetupGRPCLogging()

myServer := grpc.NewServer(grpc.UnaryInterceptor(
Expand All @@ -283,7 +283,7 @@ func createLegacyGRPCServer(cfg *config.FulcioConfig, v2Server gw.CAServer) (*gr
// Register your gRPC service implementations.
gw_legacy.RegisterCAServer(myServer, legacyGRPCCAServer)

return &grpcServer{myServer, LegacyUnixDomainSocket, v2Server, nil}, nil
return &grpcServer{myServer, unixDomainSocket, v2Server, nil}, nil
}

func panicRecoveryHandler(ctx context.Context, p interface{}) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/app/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (h httpServer) startListener(wg *sync.WaitGroup) {
wg.Add(1)
go func() {
if err := h.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Logger.Error(err)
log.Logger.Fatal(err)
}
<-idleConnsClosed
wg.Done()
Expand Down
2 changes: 1 addition & 1 deletion cmd/app/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func setupHTTPServerWithGRPCTLS(t *testing.T) (httpServer, string) {
if err != nil {
t.Error(err)
}
legacyGRPCServer, err := createLegacyGRPCServer(nil, grpcServer.caService)
legacyGRPCServer, err := createLegacyGRPCServer(nil, LegacyUnixDomainSocket, grpcServer.caService)
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/app/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func newServeCmd() *cobra.Command {
cmd.Flags().String("grpc-host", "0.0.0.0", "The host on which to serve requests for GRPC")
cmd.Flags().String("grpc-port", "8081", "The port on which to serve requests for GRPC")
cmd.Flags().String("metrics-port", "2112", "The port on which to serve prometheus metrics endpoint")
cmd.Flags().String("legacy-unix-domain-socket", LegacyUnixDomainSocket, "The Unix domain socket used for the legacy gRPC server")
cmd.Flags().Duration("read-header-timeout", 10*time.Second, "The time allowed to read the headers of the requests in seconds")
cmd.Flags().String("grpc-tls-certificate", "", "the certificate file to use for secure connections - only applies to grpc-port")
cmd.Flags().String("grpc-tls-key", "", "the private key file to use for secure connections (without passphrase) - only applies to grpc-port")
Expand Down Expand Up @@ -304,7 +305,7 @@ func runServeCmd(cmd *cobra.Command, args []string) { //nolint: revive
grpcServer.setupPrometheus(reg)
grpcServer.startTCPListener(&wg)

legacyGRPCServer, err := createLegacyGRPCServer(cfg, grpcServer.caService)
legacyGRPCServer, err := createLegacyGRPCServer(cfg, viper.GetString("legacy-unix-domain-socket"), grpcServer.caService)
if err != nil {
log.Logger.Fatal(err)
}
Expand Down Expand Up @@ -335,7 +336,7 @@ func runServeCmd(cmd *cobra.Command, args []string) { //nolint: revive
log.Logger.Info("stopped prom server")
}()
if err := prom.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Logger.Error(err)
log.Logger.Fatal(err)
}
<-idleConnsClosed
log.Logger.Info("prom server shutdown")
Expand Down

0 comments on commit cefc964

Please sign in to comment.