Skip to content

Commit

Permalink
feature/compartment (#413)
Browse files Browse the repository at this point in the history
- feature/compartment
- updated-as-per-review
- updated GetCompartmentID function
  • Loading branch information
sbhagate-infoblox committed Feb 20, 2024
1 parent 60c0a11 commit ad96515
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions auth/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func LogrusUnaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
addAccountIDToLogger(ctx)
addCompartmentIDToLogger(ctx)
return handler(ctx, req)
}
}
Expand All @@ -22,6 +23,7 @@ func LogrusStreamServerInterceptor() grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
ctx := stream.Context()
addAccountIDToLogger(ctx)
addCompartmentIDToLogger(ctx)
wrapped := grpc_middleware.WrapServerStream(stream)
wrapped.WrappedContext = ctx
err = handler(srv, wrapped)
Expand All @@ -34,3 +36,9 @@ func addAccountIDToLogger(ctx context.Context) {
ctxlogrus.AddFields(ctx, logrus.Fields{MultiTenancyField: accountID})
}
}

func addCompartmentIDToLogger(ctx context.Context) {
if compartmentID, err := GetCompartmentID(ctx, nil); err == nil {
ctxlogrus.AddFields(ctx, logrus.Fields{MultiCompartmentField: compartmentID})
}
}
14 changes: 13 additions & 1 deletion auth/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import (
"strconv"

jwt "github.com/golang-jwt/jwt/v4"
"github.com/grpc-ecosystem/go-grpc-middleware/auth"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
)

const (
// MultiTenancyField the field name for a specific tenant
MultiTenancyField = "account_id"

// MultiCompartmentField the field name for a specific compartment
MultiCompartmentField = "compartment_id"

// AuthorizationHeader contains information about the header value for the token
AuthorizationHeader = "Authorization"

Expand Down Expand Up @@ -74,6 +77,15 @@ func GetAccountID(ctx context.Context, keyfunc jwt.Keyfunc) (string, error) {
return "", errMissingField
}

// GetCompartmentID gets the JWT from a context and returns the CompartmentID field
func GetCompartmentID(ctx context.Context, keyfunc jwt.Keyfunc) (string, error) {
val, err := GetJWTField(ctx, MultiCompartmentField, keyfunc)
if err == errMissingField {
return "", nil
}
return val, err
}

// getToken parses the token into a jwt.Token type from the grpc metadata.
// WARNING: if keyfunc is nil, the token will get parsed but not verified
// because it has been checked previously in the stack. More information
Expand Down

0 comments on commit ad96515

Please sign in to comment.