-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization.go
476 lines (390 loc) · 10.5 KB
/
authorization.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package manta
import (
"context"
"errors"
"fmt"
"path"
"time"
)
var (
// ErrAuthorizerNotSupported notes that the provided authorizer is not supported
// for the action you are trying to perform.
ErrAuthorizerNotSupported = errors.New("your authorizer is not supported, please " +
"use *platform.Authorization as authorizer")
// ErrInvalidResourceType notes that the provided resource is invalid
ErrInvalidResourceType = errors.New("unknown resource type for permission")
// ErrInvalidAction notes that the provided action is invalid
ErrInvalidAction = errors.New("unknown action for permission")
ErrInvalidResourceID = &Error{
Code: EInvalid,
Msg: "invalid resource ID",
}
)
// ResourceType is an enum defining all resource types that have a permission model in platform
type ResourceType string
func (rt ResourceType) String() string {
return string(rt)
}
const (
AuthorizationsResourceType = ResourceType("authorizations")
ChecksResourceType = ResourceType("checks")
ConfigsResourceType = ResourceType("configs")
DashboardsResourceType = ResourceType("dashboards")
NotificationEndpointsResourceType = ResourceType("notifiactionEndpoints")
OrgsResourceType = ResourceType("orgs")
SecretsResourceType = ResourceType("scretes")
ScrapesResourceType = ResourceType("scrapes")
TasksResourceType = ResourceType("tasks")
UsersResourceType = ResourceType("users")
// InstanceResourceType is a special permission that allows ownership of the entire
// instance (creating orgs/operator tokens/etc)
InstanceResourceType = ResourceType("instance")
)
var AllResourceTypes = []ResourceType{
AuthorizationsResourceType,
ChecksResourceType,
ConfigsResourceType,
DashboardsResourceType,
NotificationEndpointsResourceType,
OrgsResourceType,
ScrapesResourceType,
SecretsResourceType,
TasksResourceType,
UsersResourceType,
}
func (rt ResourceType) Valid() error {
for _, tmp := range AllResourceTypes {
if tmp == rt {
return nil
}
}
return ErrInvalidResourceType
}
// Resource is an authorizable resource
type Resource struct {
Type ResourceType `json:"type"`
ID *ID `json:"id,omitempty"`
OrgID *ID `json:"orgID,omitempty"`
}
func (r Resource) String() string {
if r.OrgID != nil && r.ID != nil {
return path.Join(string(OrgsResourceType), r.OrgID.String(), string(r.Type), r.ID.String())
}
if r.OrgID != nil {
return path.Join(string(OrgsResourceType), r.OrgID.String(), string(r.Type))
}
if r.ID != nil {
return path.Join(string(r.Type), r.ID.String())
}
return string(r.Type)
}
func (r Resource) Valid() error {
return r.Type.Valid()
}
type Permission struct {
Action Action `json:"action,omitempty"`
Resource Resource `json:"resource"`
}
func (p Permission) String() string {
return fmt.Sprintf("%s:%s", p.Action, p.Resource)
}
// Valid checks if there the resource and action provided is known.
func (p *Permission) Valid() error {
if err := p.Resource.Valid(); err != nil {
return &Error{
Code: EInvalid,
Err: err,
Msg: fmt.Sprintf("invalid resource type %q for permission", p.Resource.Type),
}
}
if err := p.Action.Valid(); err != nil {
return &Error{
Code: EInvalid,
Err: err,
Msg: "invalid action type for permission",
}
}
if p.Resource.OrgID != nil && !p.Resource.OrgID.Valid() {
return &Error{
Code: EInvalid,
Err: ErrInvalidID,
Msg: "invalid org id for permission",
}
}
if p.Resource.ID != nil && !p.Resource.ID.Valid() {
return &Error{
Code: EInvalid,
Err: ErrInvalidID,
Msg: "invalid resource id for permission",
}
}
return nil
}
type Authorization struct {
ID ID `json:"id,omitempty"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
UID ID `json:"uid,omitempty"`
Token string `json:"token,omitempty"`
Status string `json:"status,omitempty"`
// add more about permissions
Permissions []Permission `json:"permissions"`
}
type UpdateAuthorization struct {
Token *string
Status *string
}
func (upd *UpdateAuthorization) Apply(auth *Authorization) {
if upd.Token != nil {
auth.Token = *upd.Token
}
if upd.Status != nil {
auth.Status = *upd.Status
}
}
type AuthorizationFilter struct {
OrgID *ID
UserID *ID
}
type AuthorizationService interface {
// FindAuthorizationByID returns a single authorization by id
FindAuthorizationByID(ctx context.Context, id ID) (*Authorization, error)
// FindAuthorizationByToken returns a single authorization by token
FindAuthorizationByToken(ctx context.Context, token string) (*Authorization, error)
FindAuthorizations(ctx context.Context, filter AuthorizationFilter) ([]*Authorization, error)
// CreateAuthorization create a new Authorization and sets a.UserID and a.Token
CreateAuthorization(ctx context.Context, a *Authorization) error
// UpdateAuthorization updates the status and token if available
UpdateAuthorization(ctx context.Context, id ID, u UpdateAuthorization) (*Authorization, error)
// DeleteAuthorization delete a authorization by ID
DeleteAuthorization(ctx context.Context, id ID) error
}
// Action is an enum defining all possible resource operations
type Action string
const (
// ReadAction is the action for reading.
ReadAction Action = "read" // 1
// WriteAction is the action for writing.
WriteAction Action = "write" // 2
)
var actions = []Action{
ReadAction, // 1
WriteAction, // 2
}
// Valid checks if the action is a member of the Action enum
func (a Action) Valid() (err error) {
switch a {
case ReadAction: // 1
case WriteAction: // 2
default:
err = ErrInvalidAction
}
return err
}
type PermissionSet []Permission
func (ps PermissionSet) Allowed(p Permission) bool {
return PermissionAllowed(p, ps)
}
// Matches returns whether or not one permission matches the other.
func (p Permission) Matches(perm Permission) bool {
return p.matches(perm)
}
func (p Permission) matches(perm Permission) bool {
if p.Action != perm.Action {
return false
}
if p.Resource.Type == InstanceResourceType {
return true
}
if p.Resource.Type != perm.Resource.Type {
return false
}
if p.Resource.OrgID == nil && p.Resource.ID == nil {
return true
}
if p.Resource.OrgID != nil && perm.Resource.OrgID != nil && p.Resource.ID != nil && perm.Resource.ID != nil {
if *p.Resource.OrgID != *perm.Resource.OrgID && *p.Resource.ID == *perm.Resource.ID {
fmt.Printf("v1: old match used: p.Resource.OrgID=%s perm.Resource.OrgID=%s p.Resource.ID=%s",
*p.Resource.OrgID, *perm.Resource.OrgID, *p.Resource.ID)
}
}
if p.Resource.OrgID != nil && p.Resource.ID == nil {
pOrgID := *p.Resource.OrgID
if perm.Resource.OrgID != nil {
permOrgID := *perm.Resource.OrgID
if pOrgID == permOrgID {
return true
}
}
}
if p.Resource.ID != nil {
pID := *p.Resource.ID
if perm.Resource.ID != nil {
permID := *perm.Resource.ID
if pID == permID {
return true
}
}
}
return false
}
// PermissionAllowed determines if a permission is allowed.
func PermissionAllowed(perm Permission, ps []Permission) bool {
for _, p := range ps {
if p.Matches(perm) {
return true
}
}
return false
}
type Authorizer interface {
Identifier() ID
GetUserID() ID
Kind() string
PermissionSet() (PermissionSet, error)
}
func (a *Authorization) Identifier() ID {
return a.ID
}
func (a *Authorization) GetUserID() ID {
return a.UID
}
func (a *Authorization) Kind() string {
return "auth"
}
func (a *Authorization) PermissionSet() PermissionSet {
return a.Permissions
}
// OwnerPermissions are the default permissions for those who own a resource
func OwnerPermissions(orgID ID) []Permission {
var ps []Permission
for _, r := range AllResourceTypes {
for _, a := range actions {
if r == OrgsResourceType {
ps = append(ps, Permission{
Action: a,
Resource: Resource{
Type: r,
ID: &orgID,
},
})
continue
}
ps = append(ps, Permission{
Action: a,
Resource: Resource{
Type: r,
OrgID: &orgID,
},
})
}
}
return ps
}
// NewPermissionAtID creates a permission with the provided arguments.
func NewPermissionAtID(id ID, action Action, rt ResourceType, orgID ID) (*Permission, error) {
p := &Permission{
Action: action,
Resource: Resource{
Type: rt,
ID: &id,
OrgID: &orgID,
},
}
if err := p.Valid(); err != nil {
return nil, err
}
return p, nil
}
// NewResourcePermission returns a permission with provided arguments
func NewResourcePermission(action Action, rt ResourceType, rid ID) (*Permission, error) {
p := &Permission{
Action: action,
Resource: Resource{
Type: rt,
ID: &rid,
},
}
if err := p.Valid(); err != nil {
return nil, err
}
return p, nil
}
// NewPermission returns a permission with provided arguments
func NewPermission(action Action, rt ResourceType, oid ID) (*Permission, error) {
p := &Permission{
Action: action,
Resource: Resource{
Type: rt,
OrgID: &oid,
},
}
if err := p.Valid(); err != nil {
return nil, err
}
return p, nil
}
func NewGlobalPermission(action Action, rt ResourceType) (*Permission, error) {
p := &Permission{
Action: action,
Resource: Resource{
Type: rt,
},
}
if err := p.Valid(); err != nil {
return nil, err
}
return p, nil
}
// MemberPermissions are the default permissions for those who can see a resource
func MemberPermissions(orgID ID) []Permission {
var ps []Permission
for _, r := range AllResourceTypes {
if r == OrgsResourceType {
ps = append(ps, Permission{
Action: ReadAction,
Resource: Resource{
Type: r,
ID: &orgID,
},
})
continue
}
ps = append(ps, Permission{
Action: ReadAction,
Resource: Resource{
Type: r,
OrgID: &orgID,
},
})
}
return ps
}
// MePermissions is the permission to read/write user itself
func MePermissions(userID ID) []Permission {
var ps []Permission
for _, a := range actions {
ps = append(ps, Permission{
Action: a,
Resource: Resource{
Type: UsersResourceType,
ID: &userID,
},
})
}
return ps
}
// OperPermissions are the default permissions for those who setup the application
func OperPermissions() []Permission {
ps := []Permission{}
for _, r := range AllResourceTypes {
// For now, we are only allowing instance permissions when logged in through session
// auth. That is handled in user resource mapping
for _, a := range actions {
ps = append(ps, Permission{Action: a, Resource: Resource{
Type: r,
}})
}
}
return ps
}