From 5de59301b3ef312ca319a0fcab85989a4373c4a8 Mon Sep 17 00:00:00 2001 From: Rahmat Hidayat Date: Fri, 14 Jan 2022 16:00:28 +0700 Subject: [PATCH] chore: server enhancements (#109) * fix: ignore unknown fields in proto * chore: add server requests log * chore: log authenticated user header key --- api/handler/v1beta1/grpc.go | 8 +++++++- app/server.go | 16 +++++++++++++++- go.mod | 3 ++- go.sum | 2 ++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/api/handler/v1beta1/grpc.go b/api/handler/v1beta1/grpc.go index 82373893c..41ddfa1ee 100644 --- a/api/handler/v1beta1/grpc.go +++ b/api/handler/v1beta1/grpc.go @@ -5,12 +5,14 @@ import ( "errors" "strings" + ctx_logrus "github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus" guardianv1beta1 "github.com/odpf/guardian/api/proto/odpf/guardian/v1beta1" "github.com/odpf/guardian/core/appeal" "github.com/odpf/guardian/core/policy" "github.com/odpf/guardian/core/provider" "github.com/odpf/guardian/core/resource" "github.com/odpf/guardian/domain" + "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" @@ -660,7 +662,11 @@ func (s *GRPCServer) getUser(ctx context.Context) (string, error) { if md, ok := metadata.FromIncomingContext(ctx); ok { users := md.Get(s.authenticatedUserHeaderKey) if len(users) > 0 { - return users[0], nil + currentUser := users[0] + ctx_logrus.AddFields(ctx, logrus.Fields{ + s.authenticatedUserHeaderKey: currentUser, + }) + return currentUser, nil } } diff --git a/app/server.go b/app/server.go index 7c57f69b7..30bd4fd30 100644 --- a/app/server.go +++ b/app/server.go @@ -8,6 +8,8 @@ import ( "time" "github.com/go-playground/validator/v10" + grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" + grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus" "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" handlerv1beta1 "github.com/odpf/guardian/api/handler/v1beta1" guardianv1beta1 "github.com/odpf/guardian/api/proto/odpf/guardian/v1beta1" @@ -29,6 +31,7 @@ import ( "github.com/odpf/guardian/plugins/providers/tableau" "github.com/odpf/guardian/store/postgres" "github.com/odpf/salt/log" + "github.com/sirupsen/logrus" "golang.org/x/net/http2" "golang.org/x/net/http2/h2c" "google.golang.org/grpc" @@ -130,7 +133,15 @@ func RunServer(c *Config) error { s.Run() // init grpc server - grpcServer := grpc.NewServer() + logrusEntry := logrus.NewEntry(logrus.New()) // TODO: get logrus instance from `logger` var + grpcServer := grpc.NewServer( + grpc.StreamInterceptor(grpc_middleware.ChainStreamServer( + grpc_logrus.StreamServerInterceptor(logrusEntry), + )), + grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( + grpc_logrus.UnaryServerInterceptor(logrusEntry), + )), + ) protoAdapter := handlerv1beta1.NewAdapter() guardianv1beta1.RegisterGuardianServiceServer(grpcServer, handlerv1beta1.NewGRPCServer( resourceService, @@ -154,6 +165,9 @@ func RunServer(c *Config) error { MarshalOptions: protojson.MarshalOptions{ UseProtoNames: true, }, + UnmarshalOptions: protojson.UnmarshalOptions{ + DiscardUnknown: true, + }, }), ) address := fmt.Sprintf(":%d", c.Port) diff --git a/go.mod b/go.mod index df389dbb1..819a505ff 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/MakeNowJust/heredoc v1.0.0 github.com/antonmedv/expr v1.9.0 github.com/go-playground/validator/v10 v10.4.1 + github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 github.com/grpc-ecosystem/grpc-gateway/v2 v2.6.0 github.com/imdario/mergo v0.3.11 github.com/mcuadros/go-defaults v1.2.0 @@ -15,6 +16,7 @@ require ( github.com/mitchellh/mapstructure v1.4.1 github.com/odpf/salt v0.0.0-20220106155451-62e8c849ae81 github.com/robfig/cron/v3 v3.0.1 + github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.2.1 github.com/stretchr/testify v1.7.0 golang.org/x/net v0.0.0-20210825183410-e898025ed96a @@ -72,7 +74,6 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect - github.com/sirupsen/logrus v1.8.1 // indirect github.com/spf13/afero v1.6.0 // indirect github.com/spf13/cast v1.3.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/go.sum b/go.sum index 361819ad0..72c8e27d7 100644 --- a/go.sum +++ b/go.sum @@ -147,6 +147,7 @@ github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRx github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= @@ -226,6 +227,7 @@ github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw= github.com/grpc-ecosystem/go-grpc-middleware v1.3.0/go.mod h1:z0ButlSOZa5vEBq9m2m2hlwIgKw+rp3sdCBRoJY+30Y= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=