This repository was archived by the owner on Feb 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathauthz.go
More file actions
108 lines (90 loc) · 2.68 KB
/
authz.go
File metadata and controls
108 lines (90 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package authz
// <!-- START clutchdoc -->
// description: Extracts resource information from requests and passes it to the authz service to allow or deny access.
// <!-- END clutchdoc -->
import (
"context"
"errors"
"github.com/uber-go/tally/v4"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
authzv1 "github.com/lyft/clutch/backend/api/authz/v1"
"github.com/lyft/clutch/backend/gateway/meta"
"github.com/lyft/clutch/backend/middleware"
"github.com/lyft/clutch/backend/service"
"github.com/lyft/clutch/backend/service/authn"
"github.com/lyft/clutch/backend/service/authz"
)
const Name = "clutch.middleware.authz"
func New(cfg *anypb.Any, logger *zap.Logger, scope tally.Scope) (middleware.Middleware, error) {
svc, ok := service.Registry["clutch.service.authz"]
if !ok {
return nil, errors.New("unable to get authz service")
}
zclient, ok := svc.(authz.Client)
if !ok {
return nil, errors.New("authz service was not the correct type")
}
return &mid{client: zclient}, nil
}
type mid struct {
client authz.Client
}
func (m *mid) evaluate(ctx context.Context, r *authzv1.CheckRequest) error {
resp, err := m.client.Check(ctx, r)
if err != nil {
return err
}
if resp.Decision != authzv1.Decision_ALLOW {
s := status.New(codes.PermissionDenied, "permission denied by authz")
s, _ = s.WithDetails(r)
return s.Err()
}
return nil
}
func (m *mid) UnaryInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
// Never interfere with allowlisted flows.
for _, allow := range authn.AlwaysAllowedMethods {
if middleware.MatchMethodOrResource(allow, info.FullMethod) {
return handler(ctx, req)
}
}
claims, err := authn.ClaimsFromContext(ctx)
if err != nil {
return nil, err
}
actionType := meta.GetAction(info.FullMethod)
resources := meta.ResourceNames(req.(proto.Message))
subject := &authzv1.Subject{
User: claims.Subject,
Groups: claims.Groups,
}
if len(resources) == 0 {
check := &authzv1.CheckRequest{
Subject: subject,
Method: info.FullMethod,
ActionType: actionType,
}
if err := m.evaluate(ctx, check); err != nil {
return nil, err
}
}
for _, resource := range resources {
check := &authzv1.CheckRequest{
Subject: subject,
Method: info.FullMethod,
ActionType: actionType,
Resource: resource.Id,
}
if err := m.evaluate(ctx, check); err != nil {
return nil, err
}
}
return handler(ctx, req)
}
}