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
219 lines (188 loc) · 5.44 KB
/
Copy pathauthz.go
File metadata and controls
219 lines (188 loc) · 5.44 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package authz
// <!-- START clutchdoc -->
// description: Evaluates the configured RBAC policies against user and resource pairs.
// <!-- END clutchdoc -->
import (
"context"
"fmt"
"github.com/golang/protobuf/ptypes/any"
"github.com/uber-go/tally/v4"
"go.uber.org/zap"
authzv1 "github.com/lyft/clutch/backend/api/authz/v1"
authzcfgv1 "github.com/lyft/clutch/backend/api/config/service/authz/v1"
"github.com/lyft/clutch/backend/middleware"
"github.com/lyft/clutch/backend/service"
)
const Name = "clutch.service.authz"
type Client interface {
Check(ctx context.Context, request *authzv1.CheckRequest) (*authzv1.CheckResponse, error)
}
func New(cfg *any.Any, logger *zap.Logger, scope tally.Scope) (service.Service, error) {
config := &authzcfgv1.Config{}
if err := cfg.UnmarshalTo(config); err != nil {
return nil, err
}
return newStaticImpl(logger, config)
}
// Private unexported struct that is used to index a map of principals to their roles.
// The proto struct is not used directly because it has internal fields that could interfere with
// hashing equally.
type principalKey struct {
name string
principalType principalKeyType
}
type principalKeyType int
const (
user principalKeyType = iota
group
)
type principalToRoleMap map[principalKey][]string
type roleToPolicyMap map[string]*authzcfgv1.Role
type staticImpl struct {
// Map of policy role names to the policy object.
roleToPolicy roleToPolicyMap
// Map of principals (i.e users or groups) to a list of role names that the principal is assigned.
principalToRole principalToRoleMap
}
func configToRolePolicyMap(config *authzcfgv1.Config) (roleToPolicyMap, error) {
// Pre-compute a role to policy map for faster lookup.
roleToPolicy := make(roleToPolicyMap, len(config.Roles))
for _, role := range config.Roles {
if _, ok := roleToPolicy[role.RoleName]; ok {
return nil, fmt.Errorf("duplicate role '%s'", role.RoleName)
}
roleToPolicy[role.RoleName] = role
}
// Verify all bound roles exist.
for _, rb := range config.RoleBindings {
for _, roleName := range rb.To {
if _, ok := roleToPolicy[roleName]; !ok {
return nil, fmt.Errorf("attempted to bind to non-existent role '%s'", roleName)
}
}
}
return roleToPolicy, nil
}
func configToPrincipalRoleMap(config *authzcfgv1.Config) principalToRoleMap {
// Pre-compute a principal to role map.
principalToRole := make(principalToRoleMap)
for _, rb := range config.RoleBindings {
for _, p := range rb.Principals {
var key principalKey
switch p.Type.(type) {
case *authzcfgv1.Principal_User:
key = principalKey{
name: p.GetUser(),
principalType: user,
}
case *authzcfgv1.Principal_Group:
key = principalKey{
name: p.GetGroup(),
principalType: group,
}
}
principalToRole[key] = append(principalToRole[key], rb.To...)
}
}
// Unique the role lists for each principal.
for k, v := range principalToRole {
vset := make(map[string]struct{})
for _, s := range v {
vset[s] = struct{}{}
}
vv := make([]string, 0, len(vset))
for s := range vset {
vv = append(vv, s)
}
principalToRole[k] = vv
}
return principalToRole
}
func newStaticImpl(logger *zap.Logger, config *authzcfgv1.Config) (Client, error) {
// Compute map of role to policy.
roleToPolicy, err := configToRolePolicyMap(config)
if err != nil {
return nil, err
}
// Compute map of principal (user or group) to list of roles.
principalToRole := configToPrincipalRoleMap(config)
// Save on the struct for lookup at runtime.
return &staticImpl{
principalToRole: principalToRole,
roleToPolicy: roleToPolicy,
}, nil
}
func assertPolicy(pol *authzcfgv1.Policy, req *authzv1.CheckRequest) bool {
// ActionTypes: if none specified or request action is in the policy's list, OK.
if len(pol.ActionTypes) != 0 {
actionMatch := false
for _, at := range pol.ActionTypes {
if req.ActionType == at {
actionMatch = true
break
}
}
if !actionMatch {
return false
}
}
// Method: if none specified or match, OK.
if pol.Method != "" && !middleware.MatchMethodOrResource(pol.Method, req.Method) {
return false
}
// Resources: if none specified or match, OK.
if len(pol.Resources) != 0 {
resMatch := false
for _, r := range pol.Resources {
if middleware.MatchMethodOrResource(r, req.Resource) {
resMatch = true
break
}
}
if !resMatch {
return false
}
}
// No problem, OK.
return true
}
func (s *staticImpl) evaluate(roles []string, req *authzv1.CheckRequest) *authzv1.CheckResponse {
if len(roles) == 0 {
return &authzv1.CheckResponse{
Decision: authzv1.Decision_DENY,
}
}
for _, roleName := range roles {
role := s.roleToPolicy[roleName]
for _, policy := range role.Policies {
if assertPolicy(policy, req) {
return &authzv1.CheckResponse{
Decision: authzv1.Decision_ALLOW,
}
}
}
}
return &authzv1.CheckResponse{
Decision: authzv1.Decision_DENY,
}
}
func (s *staticImpl) Check(ctx context.Context, req *authzv1.CheckRequest) (*authzv1.CheckResponse, error) {
// Gather all of the roles for the user and/or groups.
var roles []string
if req.Subject.User != "" {
roles = s.principalToRole[principalKey{
name: req.Subject.User,
principalType: user,
}]
}
for _, g := range req.Subject.Groups {
if g == "" {
continue
}
k := principalKey{name: g, principalType: group}
roles = append(roles, s.principalToRole[k]...)
}
// Evaluate!
resp := s.evaluate(roles, req)
return resp, nil
}