/
filter.go
208 lines (182 loc) · 6.7 KB
/
filter.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
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2019 Tencent. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://opensource.org/licenses/Apache-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package util
import (
"context"
"k8s.io/apimachinery/pkg/api/errors"
metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"tkestack.io/tke/api/auth"
v1 "tkestack.io/tke/api/auth/v1"
"tkestack.io/tke/pkg/apiserver/authentication"
"tkestack.io/tke/pkg/apiserver/filter"
)
// FilterLocalIdentity is used to filter localIdentity that do not belong to the tenant.
func FilterLocalIdentity(ctx context.Context, localIdentity *auth.LocalIdentity) error {
_, tenantID := authentication.UsernameAndTenantID(ctx)
if tenantID == "" {
return nil
}
if localIdentity.Spec.TenantID != tenantID {
return errors.NewNotFound(v1.Resource("localIdentity"), localIdentity.ObjectMeta.Name)
}
localIdentity.Spec.HashedPassword = ""
return nil
}
// FilterAPIKey is used to filter apiKey that do not belong to the tenant.
func FilterAPIKey(ctx context.Context, apiKey *auth.APIKey) error {
username, tenantID := authentication.UsernameAndTenantID(ctx)
if tenantID == "" {
return nil
}
if apiKey.Spec.TenantID != tenantID || apiKey.Spec.Username != username {
return errors.NewNotFound(v1.Resource("apiKey"), apiKey.ObjectMeta.Name)
}
return nil
}
// FilterPolicy is used to filter policy that do not belong to the tenant.
func FilterPolicy(ctx context.Context, policy *auth.Policy) error {
_, tenantID := authentication.UsernameAndTenantID(ctx)
if tenantID == "" {
return nil
}
if policy.Spec.TenantID != tenantID {
return errors.NewNotFound(v1.Resource("policy"), policy.ObjectMeta.Name)
}
projectID := filter.ProjectIDFrom(ctx)
if projectID != "" && policy.Spec.Scope != auth.PolicyProject {
return errors.NewNotFound(v1.Resource("policy"), policy.ObjectMeta.Name)
}
return nil
}
// FilterPolicy is used to filter policy that do not belong to the tenant.
func FilterProjectPolicy(ctx context.Context, binding *auth.ProjectPolicyBinding) error {
_, tenantID := authentication.UsernameAndTenantID(ctx)
if tenantID == "" {
return nil
}
if binding.Spec.TenantID != tenantID {
return errors.NewNotFound(v1.Resource("projectPolicy"), binding.ObjectMeta.Name)
}
projectID := filter.ProjectIDFrom(ctx)
if projectID == "" {
return nil
}
if binding.Spec.ProjectID != projectID {
return errors.NewNotFound(v1.Resource("projectPolicy"), binding.ObjectMeta.Name)
}
return nil
}
// FilterRole is used to filter role that do not belong to the tenant.
func FilterRole(ctx context.Context, role *auth.Role) error {
_, tenantID := authentication.UsernameAndTenantID(ctx)
if tenantID == "" {
return nil
}
if role.Spec.TenantID != tenantID {
return errors.NewNotFound(v1.Resource("role"), role.ObjectMeta.Name)
}
projectID := filter.ProjectIDFrom(ctx)
if projectID == "" {
return nil
}
if role.Spec.ProjectID != projectID {
return errors.NewNotFound(v1.Resource("role"), role.ObjectMeta.Name)
}
return nil
}
// FilterGroup is used to filter group that do not belong to the tenant.
func FilterGroup(ctx context.Context, group *auth.LocalGroup) error {
_, tenantID := authentication.UsernameAndTenantID(ctx)
if tenantID == "" {
return nil
}
if group.Spec.TenantID != tenantID {
return errors.NewNotFound(v1.Resource("group"), group.ObjectMeta.Name)
}
return nil
}
// PredicateUserNameListOptions determines the query options according to the username
// attribute of the request user.
func PredicateUserNameListOptions(ctx context.Context, options *metainternal.ListOptions) *metainternal.ListOptions {
username, tenantID := authentication.UsernameAndTenantID(ctx)
if tenantID == "" {
return options
}
if options == nil {
return &metainternal.ListOptions{
FieldSelector: fields.OneTermEqualSelector("spec.username", username),
}
}
if options.FieldSelector == nil {
options.FieldSelector = fields.OneTermEqualSelector("spec.username", username)
return options
}
options.FieldSelector = fields.AndSelectors(options.FieldSelector, fields.OneTermEqualSelector("spec.username", username))
return options
}
// PredicateProjectListOptions determines the query options according to the project
// attribute of the request user.
func PredicateProjectListOptions(ctx context.Context, options *metainternal.ListOptions) *metainternal.ListOptions {
projectID := filter.ProjectIDFrom(ctx)
if projectID == "" {
return options
}
if options == nil {
return &metainternal.ListOptions{
FieldSelector: fields.OneTermEqualSelector("spec.scope", string(auth.PolicyProject)),
}
}
if options.FieldSelector == nil {
options.FieldSelector = fields.OneTermEqualSelector("spec.scope", string(auth.PolicyProject))
return options
}
options.FieldSelector = fields.AndSelectors(options.FieldSelector, fields.OneTermEqualSelector("spec.scope", string(auth.PolicyProject)))
return options
}
// PredicateProjectIDListOptions determines the query options according to the project
// attribute of the request user.
func PredicateProjectIDListOptions(ctx context.Context, options *metainternal.ListOptions) *metainternal.ListOptions {
projectID := filter.ProjectIDFrom(ctx)
if projectID == "" {
return options
}
if options == nil {
return &metainternal.ListOptions{
FieldSelector: fields.OneTermEqualSelector("spec.projectID", projectID),
}
}
if options.FieldSelector == nil {
options.FieldSelector = fields.OneTermEqualSelector("spec.projectID", projectID)
return options
}
options.FieldSelector = fields.AndSelectors(options.FieldSelector, fields.OneTermEqualSelector("spec.projectID", projectID))
return options
}
// PredicateV1ListOptions determines the query options according to the tenant
// attribute of the request user.
func PredicateV1ListOptions(tenantID string, options *metainternal.ListOptions) *metav1.ListOptions {
v1ops := &metav1.ListOptions{}
if options == nil || options.FieldSelector == nil {
v1ops.FieldSelector = fields.OneTermEqualSelector("spec.tenantID", tenantID).String()
return v1ops
}
v1ops.FieldSelector = fields.AndSelectors(options.FieldSelector, fields.OneTermEqualSelector("spec.tenantID", tenantID)).String()
return v1ops
}