-
Notifications
You must be signed in to change notification settings - Fork 375
/
permission.go
408 lines (368 loc) · 10.2 KB
/
permission.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright (c) 2021 Terminus, Inc.
//
// 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
//
// http://www.apache.org/licenses/LICENSE-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 OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package permission
import (
"context"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/erda-project/erda-infra/providers/httpserver"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle"
table "github.com/erda-project/erda/modules/monitor/common/db"
bundlecmdb "github.com/erda-project/erda/modules/pkg/bundle-ex/cmdb"
api "github.com/erda-project/erda/pkg/common/httpapi"
"github.com/erda-project/erda/pkg/common/permission"
"github.com/erda-project/erda/pkg/http/httpclient"
)
// Scope .
type Scope string
// Scope values
const (
ScopeSys = "sys"
ScopeOrg = "org"
ScopeProject = "project"
ScopeApp = "app"
ScopePublisher = "publisher"
ScopeMicroService = "micro_service"
)
// Action .
type Action string
// Action values
const (
ActionCreate = "CREATE"
ActionDelete = "DELETE"
ActionUpdate = "UPDATE"
ActionGet = "GET"
ActionList = "LIST"
ActionOperate = "OPERATE"
)
// ValueGetter .
type ValueGetter func(ctx httpserver.Context) (string, error)
var (
hc = httpclient.New(httpclient.WithTimeout(time.Second*10, time.Second*60))
bdl *bundle.Bundle
once sync.Once
cmdb *bundlecmdb.Cmdb // 为了调用 /api/orgs/clusters/relations 接口
)
func initBundle() {
bdl = bundle.New(
bundle.WithHTTPClient(hc),
bundle.WithCoreServices(),
)
cmdb = bundlecmdb.New(bundlecmdb.WithHTTPClient(hc))
}
// Interceptor .
func Intercepter(scope interface{}, id ValueGetter, resource interface{}, action Action) httpserver.Interceptor {
once.Do(initBundle)
fmt.Printf("permission: scope = %s, resource = %s, action = %s\n", scope, resource, action)
switch res := resource.(type) {
case string:
return check(scope, id, FiexdValue(res), action)
case ValueGetter:
return check(scope, id, res, action)
case func(ctx httpserver.Context) (string, error):
return check(scope, id, res, action)
}
panic(fmt.Errorf("invalid resource type: %v", resource))
}
func check(scope interface{}, idget ValueGetter, resget ValueGetter, action Action) httpserver.Interceptor {
return func(handler func(ctx httpserver.Context) error) func(ctx httpserver.Context) error {
return func(ctx httpserver.Context) error {
req := ctx.Request()
resource, err := resget(ctx)
if err != nil {
Failure(ctx, err.Error())
return nil
}
id, err := idget(ctx)
if err != nil {
Failure(ctx, err.Error())
return nil
}
var scopeType apistructs.ScopeType
switch res := scope.(type) {
case string:
scopeType = apistructs.ScopeType(res)
case ValueGetter:
f := res
s, err := f(ctx)
if err != nil {
Failure(ctx, err.Error())
return nil
}
scopeType = apistructs.ScopeType(s)
}
idval, err := strconv.ParseUint(id, 10, 64)
if err != nil {
Failure(ctx, fmt.Sprintf("fail to convert scope id: %s", err))
return nil
}
resp, err := bdl.CheckPermission(&apistructs.PermissionCheckRequest{
UserID: req.Header.Get("User-ID"),
Scope: scopeType,
ScopeID: idval,
Resource: resource,
Action: string(action),
})
if err != nil {
Failure(ctx, err.Error())
return nil
}
if !resp.Access {
Failure(ctx, fmt.Sprintf("user:%s, scope:%s/%s, res:%s, action:%s", req.Header.Get("User-ID"), scope, id, resource, action))
return nil
}
return handler(ctx)
}
}
}
// Failure .
func Failure(context httpserver.Context, ctx interface{}) {
resp := api.Errors.AccessDenied(ctx).Response(context)
w := context.ResponseWriter()
if resp.Status(context) > 0 {
w.WriteHeader(resp.Status(context))
} else {
w.WriteHeader(http.StatusForbidden)
}
reader := resp.ReadCloser(context)
io.Copy(context.ResponseWriter(), reader)
reader.Close()
}
// QueryValue .
func QueryValue(keys ...string) func(ctx httpserver.Context) (string, error) {
return func(ctx httpserver.Context) (string, error) {
params := ctx.Request().URL.Query()
for _, key := range keys {
vals := params[key]
if len(vals) == 1 {
val := vals[0]
if len(val) == 0 {
continue
}
return val, nil
}
if len(vals) > 1 {
return "", fmt.Errorf("too many key %s present", key)
}
}
return "", fmt.Errorf("keys %v not found", keys)
}
}
// PathValue .
func PathValue(keys ...string) func(ctx httpserver.Context) (string, error) {
return func(ctx httpserver.Context) (string, error) {
for _, key := range keys {
val := ctx.Param(key)
if len(val) == 0 {
continue
}
return val, nil
}
return "", fmt.Errorf("keys %v not found", keys)
}
}
// FiexdValue .
func FiexdValue(v string) func(ctx httpserver.Context) (string, error) {
return func(ctx httpserver.Context) (string, error) {
return v, nil
}
}
func projectIdFromScopeId(ctx httpserver.Context, db *table.DB) (string, error) {
value := QueryValue("scopeId")
tk, err := value(ctx)
if err != nil {
return "", err
}
projectId, err := db.Monitor.SelectProjectIdByTk(tk)
if err != nil {
return "", err
}
return projectId, nil
}
func projectIdFromTenantGroup(ctx httpserver.Context, db *table.DB) (string, error) {
value := QueryValue("tenantGroup")
tenantGroup, err := value(ctx)
if err != nil {
return "", err
}
tk, err := db.InstanceTenant.QueryTkByTenantGroup(tenantGroup)
if err != nil {
return "", err
}
projectId, err := db.Monitor.SelectProjectIdByTk(tk)
if err != nil {
return "", err
}
return projectId, nil
}
func ScopeIdFromParams(db *table.DB) func(ctx httpserver.Context) (string, error) {
return func(ctx httpserver.Context) (string, error) {
return projectIdFromScopeId(ctx, db)
}
}
func TenantGroupFromParams(db *table.DB) func(ctx httpserver.Context) (string, error) {
return func(ctx httpserver.Context) (string, error) {
return projectIdFromTenantGroup(ctx, db)
}
}
func TkFromParams(db *table.DB) func(ctx httpserver.Context) (string, error) {
return func(ctx httpserver.Context) (string, error) {
return tkFromParams(ctx, db)
}
}
func ProjectIdFromParams() func(ctx httpserver.Context) (string, error) {
return func(ctx httpserver.Context) (string, error) {
return projectIdFromParams(ctx)
}
}
func projectIdFromParams(ctx httpserver.Context) (string, error) {
projectId := ProjectId(ctx.Request())
return projectId, nil
}
func tkFromParams(ctx httpserver.Context, db *table.DB) (string, error) {
tk := Tk(ctx.Request())
projectId, err := db.Monitor.SelectProjectIdByTk(tk)
return projectId, err
}
// OrgIDFromHeader .
func OrgIDFromHeader() func(ctx httpserver.Context) (string, error) {
return orgIDFromHeader
}
func orgIDFromHeader(ctx httpserver.Context) (string, error) {
return api.OrgID(ctx.Request()), nil
}
// OrgIDFromQuery .
func OrgIDFromQuery(key string) func(ctx httpserver.Context) (string, error) {
return func(ctx httpserver.Context) (string, error) {
return ctx.Request().FormValue(key), nil
}
}
// OrgIDByCluster .
func OrgIDByCluster(key string) func(ctx httpserver.Context) (string, error) {
return func(ctx httpserver.Context) (string, error) {
req := ctx.Request()
idStr := api.OrgID(req)
orgID, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
return "", fmt.Errorf("Org-ID is not number")
}
cluster := req.URL.Query().Get(key)
if len(cluster) <= 0 {
return "", fmt.Errorf("cluster must not be empty")
}
err = checkOrgIDsByCluster(orgID, cluster)
if err != nil {
return "", err
}
return idStr, nil
}
}
// wrap the new pkg.permission.ValueGetter
func OrgIDByClusterWrapper(key string) func(ctx context.Context, req interface{}) (string, error) {
clusterNameGetter := permission.FieldValue(key)
orgidGetter := permission.OrgIDValue()
return func(ctx context.Context, req interface{}) (string, error) {
idStr, err := orgidGetter(ctx, req)
if err != nil {
return "", err
}
orgID, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
return "", fmt.Errorf("Org-ID is not number")
}
cluster, err := clusterNameGetter(ctx, req)
if err != nil {
return "", err
}
err = checkOrgIDsByCluster(orgID, cluster)
if err != nil {
return "", err
}
return idStr, nil
}
}
type MonitorPermission struct {
Name string
}
/*
查询全部关联关系,再在内存中过滤
TODO 待优化,目前只有这个接口
*/
func checkOrgIDsByCluster(orgID uint64, clusterName string) error {
resp, err := cmdb.QueryAllOrgClusterRelation()
if err != nil {
return err
}
for _, item := range resp {
if item.ClusterName == clusterName {
if orgID == item.OrgID {
return nil
}
}
}
return fmt.Errorf("not found cluster '%s'", clusterName)
}
// OrgIDByOrgName .
func OrgIDByOrgName(key string) func(ctx httpserver.Context) (string, error) {
return func(ctx httpserver.Context) (string, error) {
req := ctx.Request()
name := req.URL.Query().Get(key)
if len(name) <= 0 {
return "", fmt.Errorf("not found org name from key %s", key)
}
orgInfo, err := bdl.GetOrg(name)
if err != nil {
return "", fmt.Errorf("fail to found org info: %s", err)
}
if orgInfo == nil {
return "", fmt.Errorf("fail to found org info")
}
ctx.SetAttribute("Org-ID", int(orgInfo.ID))
return strconv.Itoa(int(orgInfo.ID)), nil
}
}
func ProjectId(r *http.Request) string {
projectId := ""
queries := strings.Split(r.URL.RawQuery, "&")
for _, value := range queries {
param := strings.Split(value, "=")
k := param[0]
v := param[1]
if "projectId" == k || "project_id" == k {
projectId = v
break
}
}
return projectId
}
func Tk(r *http.Request) string {
tk := ""
queries := strings.Split(r.URL.RawQuery, "&")
for _, value := range queries {
param := strings.Split(value, "=")
k := param[0]
v := param[1]
if "terminusKey" == k || "terminus_key" == k {
tk = v
break
}
}
return tk
}