-
Notifications
You must be signed in to change notification settings - Fork 180
/
policies.go
177 lines (158 loc) · 5.96 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
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package auth
import (
"context"
"fmt"
"strings"
"github.com/ory/ladon"
"go.uber.org/zap"
"github.com/pydio/cells/v4/common"
"github.com/pydio/cells/v4/common/auth/claim"
"github.com/pydio/cells/v4/common/log"
"github.com/pydio/cells/v4/common/proto/idm"
"github.com/pydio/cells/v4/common/proto/rest"
"github.com/pydio/cells/v4/common/service/errors"
"github.com/pydio/cells/v4/common/utils/permissions"
)
// 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 := permissions.CachedPoliciesChecker(ctx, "oidc")
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.PydioProfileAdmin {
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, SubjectsFromClaim(claims)...)
} else if uName, _ := permissions.FindUserNameInContext(ctx); uName != "" {
if uName == common.PydioSystemUsername {
subjects = append(subjects, "profile:"+common.PydioProfileAdmin)
} else if u, e := permissions.SearchUniqueUser(ctx, uName, ""); e == nil {
subjects = append(subjects, "user:"+u.Login)
for _, p := range common.PydioUserProfiles {
subjects = append(subjects, "profile:"+p)
if p == u.Attributes[idm.UserAttrProfile] {
break
}
}
for _, r := range u.Roles {
subjects = append(subjects, "role:"+r.Uuid)
}
} else {
if errors.FromError(e).Code != 404 {
log.Logger(ctx).Warn("[policies] Cannot find user '"+uName+"' although in context", zap.Error(e))
}
}
} 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.PydioProfileAdmin {
return subjects, errors.Forbidden("resources", "Only admin profiles can list resources of other users")
}
subjects = append(subjects, "*")
if user, err := permissions.SearchUniqueUser(ctx, "", q.UserId); err != nil {
return subjects, errors.BadRequest("resources", "Cannot find user with id "+q.UserId+", error was "+err.Error())
} else {
for _, role := range user.Roles {
subjects = append(subjects, "role:"+role.Uuid)
}
subjects = append(subjects, "user:"+user.Login)
subjects = append(subjects, "profile:"+user.Attributes[idm.UserAttrProfile])
}
}
return
}
// SubjectsFromClaim builds a list of subjects based on Claim attributes.
func SubjectsFromClaim(claim claim.Claims) (subjects []string) {
subjects = append(subjects, "user:"+claim.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 == claim.Profile {
break
}
}
//subjects = append(subjects, "profile:"+claims.Profile)
for _, r := range strings.Split(claim.Roles, ",") {
subjects = append(subjects, "role:"+r)
}
return
}