Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OGE-5650] Return not found status code #19

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

"github.com/coreos/go-oidc/v3/oidc"
"github.com/gorilla/mux"
grpcMiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
v1 "github.com/ori-edge/headscale/gen/go/headscale/v1"
"github.com/patrickmn/go-cache"
Expand Down Expand Up @@ -610,7 +609,13 @@ func (h *Headscale) Serve() error {
}

// Start the local gRPC server without TLS and without authentication
grpcSocket := grpc.NewServer(zerolog.UnaryInterceptor())
grpcOptions := []grpc.ServerOption{
grpc.ChainUnaryInterceptor(
serverErrorInterceptor,
zerolog.NewUnaryServerInterceptor(),
),
}
grpcSocket := grpc.NewServer(grpcOptions...)

v1.RegisterHeadscaleServiceServer(grpcSocket, newHeadscaleV1APIServer(h))
reflection.Register(grpcSocket)
Expand Down Expand Up @@ -647,11 +652,10 @@ func (h *Headscale) Serve() error {
log.Info().Msgf("Enabling remote gRPC at %s", h.cfg.GRPCAddr)

grpcOptions := []grpc.ServerOption{
grpc.UnaryInterceptor(
grpcMiddleware.ChainUnaryServer(
h.grpcAuthenticationInterceptor,
zerolog.NewUnaryServerInterceptor(),
),
grpc.ChainUnaryInterceptor(
h.grpcAuthenticationInterceptor,
serverErrorInterceptor,
zerolog.NewUnaryServerInterceptor(),
),
}

Expand Down Expand Up @@ -991,3 +995,13 @@ func readOrCreatePrivateKey(path string) (*key.MachinePrivate, error) {

return &machineKey, nil
}

func serverErrorInterceptor(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
resp, err := handler(ctx, req)

if errors.Is(err, gorm.ErrRecordNotFound) {
return resp, status.Error(codes.NotFound, err.Error())
}

return resp, err
}
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

# When updating go.mod or go.sum, a new sha will need to be calculated,
# update this if you have a mismatch after doing a change to thos files.
vendorSha256 = "sha256-46lWrzQpqSSh3riujKVw+1V1HYaWOdgbT9adqvWp6U4=";
vendorSha256 = "sha256-hSlmgjRpulu+RRg26udtOq4lQj6JVJK+3lnvu5gKyso=";

ldflags = [ "-s" "-w" "-X github.com/ori-edge/headscale/cmd/headscale/cli.Version=v${version}" ];
};
Expand Down
Loading