forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policies.go
218 lines (195 loc) · 6.2 KB
/
policies.go
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
package auth
import (
"context"
"fmt"
"strings"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"github.com/micro/go-micro/errors"
"github.com/ory/ladon"
"github.com/ory/ladon/manager/memory"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/auth/claim"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/micro"
"github.com/pydio/cells/common/proto/idm"
"github.com/pydio/cells/common/proto/rest"
"github.com/pydio/cells/common/service/proto"
"github.com/pydio/cells/common/utils/permissions"
)
var oidcLocalChecker ladon.Warden
var lastOidcPoliciesLoad time.Time
func loadOIDCPolicies(ctx context.Context) ([]*idm.Policy, error) {
cli := idm.NewPolicyEngineServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_POLICY, defaults.NewClient())
r, e := cli.ListPolicyGroups(ctx, &idm.ListPolicyGroupsRequest{})
if e != nil {
return nil, e
}
var policies []*idm.Policy
for _, g := range r.PolicyGroups {
for _, p := range g.Policies {
isOidc := false
for _, res := range p.Resources {
if res == "oidc" {
isOidc = true
break
}
}
if isOidc {
policies = append(policies, p)
}
}
}
return policies, nil
}
func getLocalChecker(ctx context.Context) (ladon.Warden, error) {
if !lastOidcPoliciesLoad.IsZero() && time.Now().Before(lastOidcPoliciesLoad.Add(1*time.Minute)) && oidcLocalChecker != nil {
return oidcLocalChecker, nil
}
w := &ladon.Ladon{
Manager: memory.NewMemoryManager(),
}
if policies, err := loadOIDCPolicies(ctx); err != nil {
return nil, nil
} else {
for i, pol := range policies {
id := fmt.Sprintf("%v", pol.Id)
if pol.Id == "" {
id = fmt.Sprintf("%d", i)
}
w.Manager.Create(&ladon.DefaultPolicy{
ID: id,
Resources: pol.Resources,
Actions: pol.Actions,
Effect: pol.Effect.String(),
Subjects: pol.Subjects,
})
}
}
oidcLocalChecker = w
lastOidcPoliciesLoad = time.Now()
return w, nil
}
// CheckOIDCPolicies builds a local policies checker by loading "oidc"-resource policies and putting them in
// an in-memory ladon.Manager. It reloads policies every 1mn.
func checkOIDCPolicies(ctx context.Context, user *idm.User) error {
subjects := permissions.PolicyRequestSubjectsFromUser(user)
policyContext := make(map[string]string)
permissions.PolicyContextFromMetadata(policyContext, ctx)
checker, err := getLocalChecker(ctx)
if err != nil {
return err
}
if checker == nil {
log.Logger(ctx).Warn("Policies checker is not yet ready - Ignoring")
return nil
}
reqCtx := ladon.Context{}
for k, v := range policyContext {
reqCtx[k] = v
}
// check that at least one of the subject is allowed
var allow bool
for _, subject := range subjects {
request := &ladon.Request{
Resource: "oidc",
Subject: subject,
Action: "login",
Context: reqCtx,
}
if err := checker.IsAllowed(request); err != nil && err == ladon.ErrRequestForcefullyDenied {
break
} else if err == nil {
allow = true
} // Else "default deny" => continue checking
}
if !allow {
return fmt.Errorf("access denied by oidc policy denies access")
}
return nil
}
// SubjectsForResourcePolicyQuery prepares a slice of strings that will be used to check for resource ownership.
// Can be extracted either from context or by loading a given user ID from database.
func SubjectsForResourcePolicyQuery(ctx context.Context, q *rest.ResourcePolicyQuery) (subjects []string, err error) {
if q == nil {
q = &rest.ResourcePolicyQuery{Type: rest.ResourcePolicyQuery_CONTEXT}
}
switch q.Type {
case rest.ResourcePolicyQuery_ANY, rest.ResourcePolicyQuery_NONE:
var value interface{}
if value = ctx.Value(claim.ContextKey); value == nil {
return subjects, errors.BadRequest("resources", "Only admin profiles can list resources of other users")
}
claims := value.(claim.Claims)
if claims.Profile != common.PYDIO_PROFILE_ADMIN {
return subjects, errors.Forbidden("resources", "Only admin profiles can list resources with ANY or NONE filter")
}
return subjects, nil
case rest.ResourcePolicyQuery_CONTEXT:
subjects = append(subjects, "*")
if value := ctx.Value(claim.ContextKey); value != nil {
claims := value.(claim.Claims)
subjects = append(subjects, "user:"+claims.Name)
// Add all profiles up to the current one (e.g admin will check for anon, shared, standard, admin)
for _, p := range common.PydioUserProfiles {
subjects = append(subjects, "profile:"+p)
if p == claims.Profile {
break
}
}
//subjects = append(subjects, "profile:"+claims.Profile)
for _, r := range strings.Split(claims.Roles, ",") {
subjects = append(subjects, "role:"+r)
}
} else {
log.Logger(ctx).Error("Cannot find claims in context", zap.Any("c", ctx))
subjects = append(subjects, "profile:anon")
}
case rest.ResourcePolicyQuery_USER:
if q.UserId == "" {
return subjects, errors.BadRequest("resources", "Please provide a non-empty user id")
}
var value interface{}
if value = ctx.Value(claim.ContextKey); value == nil {
return subjects, errors.BadRequest("resources", "Only admin profiles can list resources of other users")
}
claims := value.(claim.Claims)
if claims.Profile != common.PYDIO_PROFILE_ADMIN {
return subjects, errors.Forbidden("resources", "Only admin profiles can list resources of other users")
}
subjects = append(subjects, "*")
subQ, _ := ptypes.MarshalAny(&idm.UserSingleQuery{
Uuid: q.UserId,
})
uClient := idm.NewUserServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_USER, defaults.NewClient())
if stream, e := uClient.SearchUser(ctx, &idm.SearchUserRequest{
Query: &service.Query{SubQueries: []*any.Any{subQ}},
}); e == nil {
var user *idm.User
for {
resp, err := stream.Recv()
if err != nil {
break
}
if resp == nil {
continue
}
user = resp.User
break
}
if user == nil {
return subjects, errors.BadRequest("resources", "Cannot find user with id "+q.UserId)
}
for _, role := range user.Roles {
subjects = append(subjects, "role:"+role.Uuid)
}
subjects = append(subjects, "user:"+user.Login)
subjects = append(subjects, "profile:"+user.Attributes["profile"])
} else {
err = e
}
}
return
}