@@ -26,10 +26,13 @@ type Policy struct {
2626 Action string
2727}
2828
29- // PolicyConfig binds gRPC methods to Policies and optional subject resolution.
29+ // PolicyConfig binds gRPC methods to Policies and optional product resolution.
3030// - MethodPolicies keyed by info.FullMethod ("/pkg.Service/Method").
3131// - DefaultPolicy used when a method mapping is absent.
32- // - SubResolver derives the subject base (e.g., editor scope). Return "" when not applicable.
32+ // - SubResolver derives the product identifier (e.g., "midaz") that is forwarded
33+ // to checkAuthorization as its product argument. For M2M tokens it becomes the
34+ // subject "admin/<product>-editor-role"; for normal-user tokens it is forwarded
35+ // for product isolation. Return "" when not applicable.
3336type PolicyConfig struct {
3437 MethodPolicies map [string ]Policy
3538 DefaultPolicy * Policy
@@ -39,11 +42,11 @@ type PolicyConfig struct {
3942// NewGRPCAuthUnaryPolicy authorizes unary RPCs via per-method Policy.
4043// Behavior:
4144// - Resolves the Policy by info.FullMethod; falls back to DefaultPolicy when provided.
42- // - Optionally derives the subject using cfg.SubResolver (e.g., editor roles ). Empty subject is valid.
45+ // - Optionally derives the product using cfg.SubResolver (e.g., "midaz" ). Empty product is valid.
4346// - Rejects missing tokens with codes.Unauthenticated; misconfiguration returns codes.Internal.
4447// Telemetry:
4548// - Sets app.request.request_id.
46- // - Sets app.request.payload with {sub , resource, action} per standard.
49+ // - Sets app.request.payload with {product , resource, action} per standard.
4750func NewGRPCAuthUnaryPolicy (auth * AuthClient , cfg PolicyConfig ) grpc.UnaryServerInterceptor {
4851 return func (ctx context.Context , req any , info * grpc.UnaryServerInfo , handler grpc.UnaryHandler ) (any , error ) {
4952 if auth == nil || ! auth .Enabled || auth .Address == "" {
@@ -69,29 +72,31 @@ func NewGRPCAuthUnaryPolicy(auth *AuthClient, cfg PolicyConfig) grpc.UnaryServer
6972 return nil , status .Error (codes .Internal , "internal configuration error" )
7073 }
7174
72- var sub string
75+ // product is the resolved product identifier passed as checkAuthorization's
76+ // product argument (M2M subject base and normal-user isolation key).
77+ var product string
7378
7479 if cfg .SubResolver != nil {
7580 var err error
7681
77- sub , err = cfg .SubResolver (ctx , info .FullMethod , req )
82+ product , err = cfg .SubResolver (ctx , info .FullMethod , req )
7883 if err != nil {
79- tracing .HandleSpanError (span , "failed to resolve subject " , err )
84+ tracing .HandleSpanError (span , "failed to resolve product " , err )
8085
8186 return nil , status .Error (codes .Internal , "internal configuration error" )
8287 }
8388 }
8489
8590 payload := map [string ]string {
86- "sub " : sub ,
91+ "product " : product ,
8792 "resource" : pol .Resource ,
8893 "action" : pol .Action ,
8994 }
9095 if err := tracing .SetSpanAttributesFromValue (span , "app.request.payload" , payload , nil ); err != nil {
9196 tracing .HandleSpanError (span , "failed to set span payload" , err )
9297 }
9398
94- authorized , httpStatus , err := auth .checkAuthorization (ctx , sub , pol .Resource , pol .Action , token )
99+ authorized , httpStatus , err := auth .checkAuthorization (ctx , product , pol .Resource , pol .Action , token )
95100 if err != nil {
96101 return nil , grpcErrorFromHTTP (httpStatus )
97102 }
@@ -248,18 +253,20 @@ func NewGRPCAuthStreamPolicy(auth *AuthClient, cfg PolicyConfig) grpc.StreamServ
248253 return status .Error (codes .Internal , "internal configuration error" )
249254 }
250255
251- var sub string
256+ // product is the resolved product identifier passed as checkAuthorization's
257+ // product argument (M2M subject base and normal-user isolation key).
258+ var product string
252259
253260 if cfg .SubResolver != nil {
254261 var err error
255262
256- sub , err = cfg .SubResolver (ctx , info .FullMethod , nil )
263+ product , err = cfg .SubResolver (ctx , info .FullMethod , nil )
257264 if err != nil {
258265 return status .Error (codes .Internal , "internal configuration error" )
259266 }
260267 }
261268
262- authorized , httpStatus , err := auth .checkAuthorization (ctx , sub , pol .Resource , pol .Action , token )
269+ authorized , httpStatus , err := auth .checkAuthorization (ctx , product , pol .Resource , pol .Action , token )
263270 if err != nil {
264271 return grpcErrorFromHTTP (httpStatus )
265272 }
0 commit comments