-
Notifications
You must be signed in to change notification settings - Fork 351
/
write.go
92 lines (79 loc) · 2.36 KB
/
write.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
package acl
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/hashicorp/go-multierror"
"github.com/treeverse/lakefs/pkg/auth"
"github.com/treeverse/lakefs/pkg/auth/model"
"github.com/treeverse/lakefs/pkg/logging"
)
const (
AdminsGroup = "Admins"
SupersGroup = "Supers"
WritersGroup = "Writers"
ReadersGroup = "Readers"
)
func WriteGroupACL(ctx context.Context, svc auth.Service, groupName string, acl model.ACL, creationTime time.Time, warnIfCreate bool) error {
log := logging.FromContext(ctx).WithField("group", groupName)
statements, err := ACLToStatement(acl)
if err != nil {
return fmt.Errorf("%s: translate ACL %+v to statements: %w", groupName, acl, err)
}
aclPolicyName := PolicyName(groupName)
policy := &model.Policy{
CreatedAt: creationTime,
DisplayName: aclPolicyName,
Statement: statements,
ACL: acl,
}
policyJSON, err := json.MarshalIndent(policy, "", " ")
if err != nil {
return err
}
log.WithField("policy", fmt.Sprintf("%+v", policy)).
WithField("policyJSON", string(policyJSON)).
Debug("Set policy derived from ACL")
err = svc.WritePolicy(ctx, policy, true)
if errors.Is(err, auth.ErrNotFound) {
if warnIfCreate {
log.WithField("group", groupName).
Info("Define an ACL for the first time because none was defined (bad migrate?)")
}
err = svc.WritePolicy(ctx, policy, false)
}
if err != nil {
return fmt.Errorf("write policy %s %+v for group %s: %w", aclPolicyName, policy, groupName, err)
}
// Detach any existing policies from group
existingPolicies, _, err := svc.ListGroupPolicies(ctx, groupName, &model.PaginationParams{
Amount: -1,
})
if err != nil {
return fmt.Errorf("list existing group policies for group %s: %w", groupName, err)
}
err = svc.AttachPolicyToGroup(ctx, aclPolicyName, groupName)
if errors.Is(err, auth.ErrAlreadyExists) {
err = nil
}
if err != nil {
return fmt.Errorf("attach policy %s to group %s: %w", aclPolicyName, groupName, err)
}
for _, existingPolicy := range existingPolicies {
if existingPolicy.DisplayName != aclPolicyName {
oneErr := svc.DetachPolicyFromGroup(ctx, existingPolicy.DisplayName, groupName)
if oneErr != nil {
err = multierror.Append(
err,
fmt.Errorf("detach policy %s from group %s: %w", existingPolicy.DisplayName, groupName, oneErr),
)
}
}
}
if err != nil {
return err
}
return nil
}