-
Notifications
You must be signed in to change notification settings - Fork 180
/
defaults.go
802 lines (777 loc) · 26.4 KB
/
defaults.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/*
* Copyright (c) 2018. 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 policy
import (
"context"
"fmt"
"strings"
"github.com/ory/ladon"
"go.uber.org/zap"
"github.com/pydio/cells/v4/common/log"
"github.com/pydio/cells/v4/common/proto/idm"
servicecontext "github.com/pydio/cells/v4/common/service/context"
"github.com/pydio/cells/v4/common/utils/permissions"
"github.com/pydio/cells/v4/idm/policy/conditions"
"github.com/pydio/cells/v4/idm/policy/converter"
)
var (
// DefaultPolicyGroups provides some sample policies to Admin Users.
// Note that Name and Description fields are generally i18nized
// that is why we rather declare here the corresponding message IDs.
DefaultPolicyGroups = []*idm.PolicyGroup{
{
Uuid: "public-access",
Name: "PolicyGroup.PublicAccess.Title",
Description: "PolicyGroup.PublicAccess.Description",
ResourceGroup: idm.PolicyResourceGroup_rest,
Policies: []*idm.Policy{
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "discovery-policy",
Description: "PolicyGroup.PublicAccess.Rule1",
Subjects: []string{"profile:anon"},
Resources: []string{"rest:/config/discovery<.*>"},
Actions: []string{"GET"},
Effect: ladon.AllowAccess,
}),
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "reset-password-policy",
Description: "PolicyGroup.PublicAccess.Rule2",
Subjects: []string{"profile:anon"},
Resources: []string{"rest:/auth/reset-password<.*>"},
Actions: []string{"PUT", "POST"},
Effect: ladon.AllowAccess,
}),
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "frontend-state",
Description: "PolicyGroup.PublicAccess.Rule3",
Subjects: []string{"profile:anon"},
Resources: []string{
"rest:/frontend/binaries/GLOBAL/<.*>",
"rest:/frontend/bootconf",
"rest:/frontend/messages/<.*>",
"rest:/frontend/plugins/<.*>",
"rest:/frontend/state",
"rest:/frontend/auth/state",
"rest:/frontend/login/connectors",
},
Actions: []string{"GET"},
Effect: ladon.AllowAccess,
}),
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "frontend-auth",
Description: "PolicyGroup.PublicAccess.Rule4",
Subjects: []string{"profile:anon"},
Resources: []string{"rest:/frontend/session"},
Actions: []string{"POST"},
Effect: ladon.AllowAccess,
}),
},
},
{
Uuid: "public-access-install",
Name: "PolicyGroup.PublicInstall.Title",
Description: "PolicyGroup.PublicInstall.Description",
ResourceGroup: idm.PolicyResourceGroup_rest,
Policies: []*idm.Policy{
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "install-policy",
Description: "PolicyGroup.PublicInstall.Rule1",
Subjects: []string{"profile:anon"},
Resources: []string{"rest:/install"},
Actions: []string{"GET", "POST"},
Effect: ladon.AllowAccess,
}),
},
},
// Default Accesses to REST endpoints for logged user and for admin user
{
Uuid: "rest-apis-default-accesses",
Name: "PolicyGroup.LoggedUsers.Title",
Description: "PolicyGroup.LoggedUsers.Description",
ResourceGroup: idm.PolicyResourceGroup_rest,
Policies: []*idm.Policy{
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "admin-default-policy",
Description: "PolicyGroup.LoggedUsers.Rule1",
Subjects: []string{"profile:admin"},
Resources: []string{"rest:<.+>"},
Actions: []string{"GET", "POST", "DELETE", "PUT", "PATCH"},
Effect: ladon.AllowAccess,
}),
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "user-default-policy",
Description: "PolicyGroup.LoggedUsers.Rule2",
Subjects: []string{"profile:standard", "profile:shared"},
Resources: []string{
"rest:/user",
"rest:/user/<.+>",
"rest:/workspace",
"rest:/role",
"rest:/role/<.+>",
"rest:/graph<.+>",
"rest:/jobs/user",
"rest:/jobs/user<.+>",
"rest:/meta<.+>",
"rest:/mailer/send",
"rest:/search/nodes",
"rest:/share<.+>",
"rest:/activity<.+>",
"rest:/changes",
"rest:/changes<.+>",
"rest:/tree/create",
"rest:/tree/delete",
"rest:/tree/restore",
"rest:/tree/selection",
"rest:/tree/stat/<.+>",
"rest:/tree/stats",
"rest:/templates",
"rest:/templates<.+>",
"rest:/auth/token/document",
},
Actions: []string{"GET", "POST", "DELETE", "PUT", "PATCH"},
Effect: ladon.AllowAccess,
}),
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "user-meta-read",
Description: "PolicyGroup.LoggedUsers.Rule3",
Subjects: []string{"profile:standard", "profile:shared"},
Resources: []string{
"rest:/user-meta/bookmarks",
"rest:/user-meta/namespace",
"rest:/user-meta/search",
"rest:/user-meta/tags/<.+>",
},
Actions: []string{"GET", "POST"},
Effect: ladon.AllowAccess,
}),
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "user-meta-put",
Description: "PolicyGroup.LoggedUsers.Rule4",
Subjects: []string{"profile:standard", "profile:shared"},
Resources: []string{
"rest:/user-meta/update",
},
Actions: []string{"PUT"},
Effect: ladon.AllowAccess,
}),
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "frontend-read",
Description: "PolicyGroup.LoggedUsers.Rule5",
Subjects: []string{"profile:standard", "profile:shared"},
Resources: []string{
"rest:/frontend/<.*>",
},
Actions: []string{"GET"},
Effect: ladon.AllowAccess,
}),
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "frontend-post",
Description: "PolicyGroup.LoggedUsers.Rule6",
Subjects: []string{"profile:standard", "profile:shared"},
Resources: []string{
"rest:/frontend/binaries/USER/<.+>",
"rest:/frontend/enroll",
"rest:/frontend/session",
},
Actions: []string{"POST"},
Effect: ladon.AllowAccess,
}),
},
},
{
Uuid: "oidc-actions-policies",
Name: "PolicyGroup.OIDC.Title",
Description: "PolicyGroup.OIDC.Description",
ResourceGroup: idm.PolicyResourceGroup_oidc,
Policies: []*idm.Policy{
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "all-users-login",
Description: "PolicyGroup.OIDC.Rule1",
Subjects: []string{"user:<.+>", "profile:<.+>", "role:<.+>"},
Resources: []string{"oidc"},
Actions: []string{"login"},
Effect: ladon.AllowAccess,
}),
},
},
// Sample Complex policy, composed of many policies, linked to ACLs.
// sample-acl-policy allows read-write by default but:
// - disables write if accessed locally and folder name is "target",
// - hides files with extension "png" when accessed locally.
{
Uuid: "sample-acl-policy",
Name: "PolicyGroup.ACLSample1.Title",
Description: "PolicyGroup.ACLSample1.Description",
ResourceGroup: idm.PolicyResourceGroup_acl,
Policies: []*idm.Policy{
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "acl-complex-rule1",
Description: "PolicyGroup.ACLSample1.Rule1",
Subjects: []string{"policy:sample-acl-policy"},
Resources: []string{"acl"},
Actions: []string{"read", "write"},
Effect: ladon.AllowAccess,
}),
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "acl-complex-rule2",
Description: "PolicyGroup.ACLSample1.Rule2",
Subjects: []string{"policy:sample-acl-policy"},
Resources: []string{"acl"},
Actions: []string{"write"},
Effect: ladon.DenyAccess,
Conditions: ladon.Conditions{
servicecontext.HttpMetaRemoteAddress: &conditions.StringNotMatchCondition{
Matches: "localhost|127.0.0.1|::1",
},
permissions.PolicyNodeMetaName: &ladon.StringMatchCondition{
Matches: "target",
},
},
}),
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "acl-complex-rule3",
Description: "PolicyGroup.ACLSample1.Rule3",
Subjects: []string{"policy:sample-acl-policy"},
Resources: []string{"acl"},
Actions: []string{"read"},
Effect: ladon.DenyAccess,
Conditions: ladon.Conditions{
servicecontext.HttpMetaRemoteAddress: &conditions.StringNotMatchCondition{
Matches: "localhost|127.0.0.1|::1",
},
permissions.PolicyNodeMetaName: &ladon.StringMatchCondition{
Matches: "(.+)\\.png",
},
},
}),
},
},
// This policy enables access to a given workspace for a given period only.
{
Uuid: "limited-period-access-acl-policy2",
Name: "PolicyGroup.ACLSamplePeriod.Title",
Description: "PolicyGroup.ACLSamplePeriod.Description",
ResourceGroup: idm.PolicyResourceGroup_acl,
Policies: []*idm.Policy{
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "acl-temporary-access2",
Description: "PolicyGroup.ACLSamplePeriod.Rule1",
Subjects: []string{"policy:limited-period-access-acl-policy2"},
Resources: []string{"acl"},
Actions: []string{"write", "read"},
Effect: ladon.AllowAccess,
Conditions: ladon.Conditions{
servicecontext.ClientTime: &conditions.WithinPeriodCondition{
Matches: "2018-02-01T00:00+0100/2018-04-01T00:00+0100",
},
},
}),
},
},
// This policy totally disables access to a given workspace after a given date
{
Uuid: "no-access-after-acl-policy",
Name: "PolicyGroup.ACLSampleDateDisable.Title",
Description: "PolicyGroup.ACLSampleDateDisable.Description",
ResourceGroup: idm.PolicyResourceGroup_acl,
Policies: []*idm.Policy{
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "acl-date-rule1",
Description: "PolicyGroup.ACLSampleDateDisable.Rule1",
Subjects: []string{"policy:no-access-after-acl-policy"},
Resources: []string{"acl"},
Actions: []string{"read", "write"},
Effect: ladon.AllowAccess,
}),
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "acl-date-rule2",
Description: "PolicyGroup.ACLSampleDateDisable.Rule2",
Subjects: []string{"policy:no-access-after-acl-policy"},
Resources: []string{"acl"},
Actions: []string{"read", "write"},
Effect: ladon.DenyAccess,
Conditions: ladon.Conditions{
servicecontext.ServerTime: &conditions.DateAfterCondition{
Matches: "2018-02-28T23:59+0100",
},
},
}),
},
},
// This policy enable access to a given workspace only during business hours
{
Uuid: "office-hours-access-acl-policy",
Name: "PolicyGroup.ACLSampleBusinessHours.Title",
Description: "PolicyGroup.ACLSampleBusinessHours.Description",
ResourceGroup: idm.PolicyResourceGroup_acl,
Policies: []*idm.Policy{
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "acl-office-hours-rule",
Description: "PolicyGroup.ACLSampleBusinessHours.Rule1",
Subjects: []string{"policy:office-hours-access-acl-policy"},
Resources: []string{"acl"},
Actions: []string{"read", "write"},
Effect: ladon.AllowAccess,
Conditions: ladon.Conditions{
servicecontext.ClientTime: &conditions.OfficeHoursCondition{
Matches: "Monday-Friday/09:00/18:30",
},
},
}),
},
},
// This policy should totally disable access to a given workspace
{
Uuid: "no-external-access-acl-policy",
Name: "PolicyGroup.ACLSampleExternalIP.Title",
Description: "PolicyGroup.ACLSampleExternalIP.Description",
ResourceGroup: idm.PolicyResourceGroup_acl,
Policies: []*idm.Policy{
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "acl-complex-rule4",
Description: "PolicyGroup.ACLSampleExternalIP.Rule1",
Subjects: []string{"policy:no-external-access-acl-policy"},
Resources: []string{"acl"},
Actions: []string{"read", "write"},
Effect: ladon.AllowAccess,
}),
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "acl-complex-rule5",
Description: "PolicyGroup.ACLSampleExternalIP.Rule2",
Subjects: []string{"policy:no-external-access-acl-policy"},
Resources: []string{"acl"},
Actions: []string{"read", "write"},
Effect: ladon.DenyAccess,
Conditions: ladon.Conditions{
servicecontext.HttpMetaRemoteAddress: &conditions.StringNotMatchCondition{
Matches: "localhost|127.0.0.1|::1",
},
},
}),
},
},
}
)
// InitDefaults is called once at first launch to create default policy groups.
func InitDefaults(ctx context.Context) error {
dao := servicecontext.GetDAO(ctx).(DAO)
if dao == nil {
return fmt.Errorf("cannot find DAO for policies initialization")
}
for _, policyGroup := range DefaultPolicyGroups {
if _, er := dao.StorePolicyGroup(ctx, policyGroup); er != nil {
log.Logger(ctx).Error("could not store default policy group "+policyGroup.Uuid, zap.Any("policy", policyGroup), zap.Error(er))
}
}
log.Logger(ctx).Info("Inserted default policies")
return nil
}
// Upgrade101 adapts policy dbs. It is called once at service launch when Cells version become >= 1.0.1.
func Upgrade101(ctx context.Context) error {
dao := servicecontext.GetDAO(ctx).(DAO)
if dao == nil {
return fmt.Errorf("cannot find DAO for policies initialization")
}
groups, e := dao.ListPolicyGroups(ctx)
if e != nil {
return e
}
for _, group := range groups {
if group.Uuid == "frontend-restricted-accesses" {
for _, p := range group.Policies {
if p.Id == "anon-frontend-logs" {
p.Resources = []string{"rest:/frontend/frontlogs"}
}
}
if _, er := dao.StorePolicyGroup(ctx, group); er != nil {
log.Logger(ctx).Error("could not update policy group "+group.Uuid, zap.Error(er))
} else {
log.Logger(ctx).Info("Updated policy group " + group.Uuid)
}
} else if group.Uuid == "rest-apis-default-accesses" {
for _, p := range group.Policies {
if p.Id == "user-default-policy" {
p.Resources = append(p.Resources, "rest:/docstore/keystore<.+>", "rest:/changes", "rest:/changes<.+>")
}
}
if _, er := dao.StorePolicyGroup(ctx, group); er != nil {
log.Logger(ctx).Error("could not update policy group "+group.Uuid, zap.Error(er))
} else {
log.Logger(ctx).Info("Updated policy group " + group.Uuid)
}
}
}
log.Logger(ctx).Info("Upgraded policy model to v1.0.1")
return nil
}
// Upgrade103 adapts policy dbs. It is called once at service launch when Cells version become >= 1.0.3 .
func Upgrade103(ctx context.Context) error {
dao := servicecontext.GetDAO(ctx).(DAO)
if dao == nil {
return fmt.Errorf("cannot find DAO for policies initialization")
}
groups, e := dao.ListPolicyGroups(ctx)
if e != nil {
return e
}
for _, group := range groups {
if group.Uuid == "rest-apis-default-accesses" {
group.Policies = append(group.Policies, converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "shares-default-policy",
Description: "PolicyGroup.LoggedUsers.Rule3",
Subjects: []string{"profile:standard", "profile:shared"},
Resources: []string{"rest:/docstore/share/<.+>"},
Actions: []string{"GET", "PUT"},
Effect: ladon.AllowAccess,
}))
if _, er := dao.StorePolicyGroup(ctx, group); er != nil {
log.Logger(ctx).Error("could not update policy group "+group.Uuid, zap.Error(er))
} else {
log.Logger(ctx).Info("Updated policy group " + group.Uuid)
}
}
}
log.Logger(ctx).Info("Upgraded policy model to v1.0.3")
return nil
}
// Upgrade120 performs upgrade on policies starting at v1.2.0
func Upgrade120(ctx context.Context) error {
dao := servicecontext.GetDAO(ctx).(DAO)
if dao == nil {
return fmt.Errorf("cannot find DAO for policies initialization")
}
groups, e := dao.ListPolicyGroups(ctx)
if e != nil {
return e
}
for _, group := range groups {
if group.Uuid == "rest-apis-default-accesses" {
var updates []*idm.Policy
for _, p := range group.Policies {
if p.Id == "shares-default-policy" {
// Remove that one
continue
}
if p.Id == "user-default-policy" {
var newResources []string
for _, res := range p.Resources {
if strings.HasPrefix(res, "rest:/acl") || strings.HasPrefix(res, "rest:/docstore") {
// Remove those accesses
continue
}
newResources = append(newResources, res)
}
newResources = append(newResources, "rest:/frontend/<.*>", "rest:/tree/<.*>")
p.Resources = newResources
}
updates = append(updates, p)
}
updates = append(updates, converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "user-meta-tags-no-delete",
Description: "PolicyGroup.LoggedUsers.Rule3",
Subjects: []string{"profile:standard", "profile:shared"},
Resources: []string{
"rest:/user-meta/tags<.+>",
},
Actions: []string{"DELETE"},
Effect: ladon.DenyAccess,
}))
group.Policies = updates
if _, er := dao.StorePolicyGroup(ctx, group); er != nil {
log.Logger(ctx).Error("could not update policy group "+group.Uuid, zap.Error(er))
} else {
log.Logger(ctx).Info("Updated policy group " + group.Uuid)
}
} else if group.Uuid == "frontend-restricted-accesses" {
if err := dao.DeletePolicyGroup(ctx, group); err != nil {
log.Logger(ctx).Error("could not delete unused policy group "+group.Uuid, zap.Error(err))
} else {
log.Logger(ctx).Info("Deleted unused policy group "+group.Uuid, zap.Error(err))
}
} else if group.Uuid == "public-access" {
group.Policies = append(group.Policies, converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "frontend-state",
Description: "PolicyGroup.PublicAccess.Rule3",
Subjects: []string{"profile:anon"},
Resources: []string{"rest:/frontend/<.*>"},
Actions: []string{"GET"},
Effect: ladon.AllowAccess,
}), converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "frontend-auth",
Description: "PolicyGroup.PublicAccess.Rule4",
Subjects: []string{"profile:anon"},
Resources: []string{"rest:/frontend/session"},
Actions: []string{"POST"},
Effect: ladon.AllowAccess,
}))
if _, er := dao.StorePolicyGroup(ctx, group); er != nil {
log.Logger(ctx).Error("could not update policy group "+group.Uuid, zap.Error(er))
} else {
log.Logger(ctx).Info("Updated policy group " + group.Uuid)
}
}
}
return nil
}
// Upgrade122 adapts policy dbs. It is called once at service launch when Cells version become >= 1.2.2.
func Upgrade122(ctx context.Context) error {
dao := servicecontext.GetDAO(ctx).(DAO)
if dao == nil {
return fmt.Errorf("cannot find DAO for policies initialization")
}
groups, e := dao.ListPolicyGroups(ctx)
if e != nil {
return e
}
for _, group := range groups {
if group.Uuid == "rest-apis-default-accesses" {
for _, p := range group.Policies {
if p.Id == "user-default-policy" {
p.Resources = append(p.Resources, "rest:/templates")
}
}
if _, er := dao.StorePolicyGroup(ctx, group); er != nil {
log.Logger(ctx).Error("could not update policy group "+group.Uuid, zap.Error(er))
} else {
log.Logger(ctx).Info("Updated policy group " + group.Uuid)
}
}
}
log.Logger(ctx).Info("Upgraded policy model to v1.2.2")
return nil
}
// Upgrade142 performs upgrade on policies starting at v1.4.2
func Upgrade142(ctx context.Context) error {
dao := servicecontext.GetDAO(ctx).(DAO)
if dao == nil {
return fmt.Errorf("cannot find DAO for policies initialization")
}
groups, e := dao.ListPolicyGroups(ctx)
if e != nil {
return e
}
for _, group := range groups {
if group.Uuid == "rest-apis-default-accesses" {
group.Policies = append(group.Policies, converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "user-ws-readonly",
Description: "PolicyGroup.LoggedUsers.Rule4",
Subjects: []string{"profile:standard", "profile:shared"},
Resources: []string{
"rest:/workspace/<.+>",
},
Actions: []string{"DELETE", "PUT", "PATCH"},
Effect: ladon.DenyAccess,
}))
if _, er := dao.StorePolicyGroup(ctx, group); er != nil {
log.Logger(ctx).Error("could not update policy group "+group.Uuid, zap.Error(er))
} else {
log.Logger(ctx).Info("Updated policy group " + group.Uuid)
}
}
}
return nil
}
func Upgrade202(ctx context.Context) error {
dao := servicecontext.GetDAO(ctx).(DAO)
if dao == nil {
return fmt.Errorf("cannot find DAO for policies initialization")
}
groups, e := dao.ListPolicyGroups(ctx)
if e != nil {
return e
}
for _, group := range groups {
if group.Uuid == "rest-apis-default-accesses" {
for _, p := range group.Policies {
if p.Id == "user-default-policy" {
var newRes []string
for _, r := range p.Resources {
if r == "rest:/tree/<.*>" {
newRes = append(newRes,
"rest:/tree/create",
"rest:/tree/delete",
"rest:/tree/restore",
"rest:/tree/selection",
"rest:/tree/stat/<.+>",
"rest:/tree/stats",
)
} else {
newRes = append(newRes, r)
}
}
p.Resources = newRes
}
}
if _, er := dao.StorePolicyGroup(ctx, group); er != nil {
log.Logger(ctx).Error("could not update policy group "+group.Uuid, zap.Error(er))
} else {
log.Logger(ctx).Info("Updated policy group " + group.Uuid)
}
}
}
return nil
}
func Upgrade210(ctx context.Context) error {
dao := servicecontext.GetDAO(ctx).(DAO)
if dao == nil {
return fmt.Errorf("cannot find DAO for policies initialization")
}
groups, e := dao.ListPolicyGroups(ctx)
if e != nil {
return e
}
for _, group := range groups {
if group.Uuid == "rest-apis-default-accesses" {
var newPolicies []*idm.Policy
for _, p := range group.Policies {
if p.Id == "user-meta-tags-no-delete" || p.Id == "user-ws-readonly" {
continue
}
if p.Id == "user-default-policy" {
var newRes []string
for _, r := range p.Resources {
if r == "rest:/workspace/<.+>" || r == "rest:/user-meta<.+>" {
continue
} else {
newRes = append(newRes, r)
}
}
p.Resources = newRes
}
newPolicies = append(newPolicies, p)
}
newPolicies = append(newPolicies,
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "user-meta-read",
Description: "PolicyGroup.LoggedUsers.Rule3",
Subjects: []string{"profile:standard", "profile:shared"},
Resources: []string{
"rest:/user-meta/bookmarks",
"rest:/user-meta/namespace",
"rest:/user-meta/search",
"rest:/user-meta/tags/<.+>",
},
Actions: []string{"GET", "POST"},
Effect: ladon.AllowAccess,
}),
converter.LadonToProtoPolicy(&ladon.DefaultPolicy{
ID: "user-meta-put",
Description: "PolicyGroup.LoggedUsers.Rule4",
Subjects: []string{"profile:standard", "profile:shared"},
Resources: []string{
"rest:/user-meta/update",
},
Actions: []string{"PUT"},
Effect: ladon.AllowAccess,
}),
)
group.Policies = newPolicies
if _, er := dao.StorePolicyGroup(ctx, group); er != nil {
log.Logger(ctx).Error("could not update policy group "+group.Uuid, zap.Error(er))
} else {
log.Logger(ctx).Info("Updated policy group " + group.Uuid)
}
}
}
return nil
}
func Upgrade220(ctx context.Context) error {
dao := servicecontext.GetDAO(ctx).(DAO)
if dao == nil {
return fmt.Errorf("cannot find DAO for policies initialization")
}
groups, e := dao.ListPolicyGroups(ctx)
if e != nil {
return e
}
for _, group := range groups {
if group.Uuid == "rest-apis-default-accesses" {
for _, p := range group.Policies {
if p.Id == "user-default-policy" {
p.Resources = append(p.Resources, "rest:/auth/token/document")
}
}
if _, er := dao.StorePolicyGroup(ctx, group); er != nil {
log.Logger(ctx).Error("could not update policy group "+group.Uuid, zap.Error(er))
} else {
log.Logger(ctx).Info("Updated policy group " + group.Uuid)
}
}
}
return nil
}
func Upgrade227(ctx context.Context) error {
dao := servicecontext.GetDAO(ctx).(DAO)
if dao == nil {
return fmt.Errorf("cannot find DAO for policies initialization")
}
groups, e := dao.ListPolicyGroups(ctx)
if e != nil {
return e
}
for _, group := range groups {
if group.Uuid == "public-access" {
for _, p := range group.Policies {
if p.Id == "frontend-state" {
p.Resources = []string{
"rest:/frontend/binaries/GLOBAL/<.*>",
"rest:/frontend/bootconf",
"rest:/frontend/messages/<.*>",
"rest:/frontend/plugins/<.*>",
"rest:/frontend/state",
"rest:/frontend/auth/state",
"rest:/frontend/login/connectors",
}
}
}
if _, er := dao.StorePolicyGroup(ctx, group); er != nil {
log.Logger(ctx).Error("could not update policy group "+group.Uuid, zap.Error(er))
} else {
log.Logger(ctx).Info("Updated policy group " + group.Uuid)
}
}
}
return nil
}
func Upgrade399(ctx context.Context) error {
dao := servicecontext.GetDAO(ctx).(DAO)
if dao == nil {
return fmt.Errorf("cannot find DAO for policies initialization")
}
groups, e := dao.ListPolicyGroups(ctx)
if e != nil {
return e
}
for _, group := range groups {
if group.Uuid == "rest-apis-default-accesses" {
for _, p := range group.Policies {
if p.Id == "user-default-policy" {
p.Resources = append(p.Resources, "rest:/templates<.+>")
}
}
if _, er := dao.StorePolicyGroup(ctx, group); er != nil {
log.Logger(ctx).Error("could not update policy group "+group.Uuid, zap.Error(er))
} else {
log.Logger(ctx).Info("Updated policy group " + group.Uuid)
}
}
}
return nil
}