Skip to content

Commit

Permalink
chore: server enhancements (#109)
Browse files Browse the repository at this point in the history
* fix: ignore unknown fields in proto

* chore: add server requests log

* chore: log authenticated user header key
  • Loading branch information
rahmatrhd authored Jan 14, 2022
1 parent 793ddbc commit 5de5930
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
8 changes: 7 additions & 1 deletion api/handler/v1beta1/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}

Expand Down
16 changes: 15 additions & 1 deletion app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ 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
github.com/mcuadros/go-lookup v0.0.0-20200831155250-80f87a4fa5ee
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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit 5de5930

Please sign in to comment.