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

feature/compartment #413

Merged
merged 3 commits into from
Feb 20, 2024
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
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
Loading