-
Notifications
You must be signed in to change notification settings - Fork 172
/
policies.go
149 lines (132 loc) · 4.87 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
package auth
import (
"context"
"fmt"
"strings"
"github.com/micro/go-micro/errors"
"github.com/ory/ladon"
"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/proto/idm"
"github.com/pydio/cells/common/proto/rest"
"github.com/pydio/cells/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, "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 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, r.Uuid)
}
} else {
log.Logger(ctx).Error("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
}