Skip to content

Commit ad96515

Browse files
feature/compartment (#413)
- feature/compartment - updated-as-per-review - updated GetCompartmentID function
1 parent 60c0a11 commit ad96515

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

auth/interceptor.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
func LogrusUnaryServerInterceptor() grpc.UnaryServerInterceptor {
1414
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
1515
addAccountIDToLogger(ctx)
16+
addCompartmentIDToLogger(ctx)
1617
return handler(ctx, req)
1718
}
1819
}
@@ -22,6 +23,7 @@ func LogrusStreamServerInterceptor() grpc.StreamServerInterceptor {
2223
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
2324
ctx := stream.Context()
2425
addAccountIDToLogger(ctx)
26+
addCompartmentIDToLogger(ctx)
2527
wrapped := grpc_middleware.WrapServerStream(stream)
2628
wrapped.WrappedContext = ctx
2729
err = handler(srv, wrapped)
@@ -34,3 +36,9 @@ func addAccountIDToLogger(ctx context.Context) {
3436
ctxlogrus.AddFields(ctx, logrus.Fields{MultiTenancyField: accountID})
3537
}
3638
}
39+
40+
func addCompartmentIDToLogger(ctx context.Context) {
41+
if compartmentID, err := GetCompartmentID(ctx, nil); err == nil {
42+
ctxlogrus.AddFields(ctx, logrus.Fields{MultiCompartmentField: compartmentID})
43+
}
44+
}

auth/jwt.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ import (
77
"strconv"
88

99
jwt "github.com/golang-jwt/jwt/v4"
10-
"github.com/grpc-ecosystem/go-grpc-middleware/auth"
10+
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
1111
)
1212

1313
const (
1414
// MultiTenancyField the field name for a specific tenant
1515
MultiTenancyField = "account_id"
1616

17+
// MultiCompartmentField the field name for a specific compartment
18+
MultiCompartmentField = "compartment_id"
19+
1720
// AuthorizationHeader contains information about the header value for the token
1821
AuthorizationHeader = "Authorization"
1922

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

80+
// GetCompartmentID gets the JWT from a context and returns the CompartmentID field
81+
func GetCompartmentID(ctx context.Context, keyfunc jwt.Keyfunc) (string, error) {
82+
val, err := GetJWTField(ctx, MultiCompartmentField, keyfunc)
83+
if err == errMissingField {
84+
return "", nil
85+
}
86+
return val, err
87+
}
88+
7789
// getToken parses the token into a jwt.Token type from the grpc metadata.
7890
// WARNING: if keyfunc is nil, the token will get parsed but not verified
7991
// because it has been checked previously in the stack. More information

0 commit comments

Comments
 (0)