-
Notifications
You must be signed in to change notification settings - Fork 13
/
aclupdatejob.go
134 lines (113 loc) · 3.07 KB
/
aclupdatejob.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
package agent
import (
"fmt"
"sort"
"strings"
v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
"github.com/Axway/agent-sdk/pkg/jobs"
"github.com/Axway/agent-sdk/pkg/util/log"
)
const envACLFormat = "%s-agent-acl"
// aclUpdateHandler - job that handles updates to the ACL in the environment
type aclUpdateJob struct {
jobs.Job
lastTeamIDs []string
logger log.FieldLogger
}
func newACLUpdateJob() *aclUpdateJob {
logger := log.NewFieldLogger().
WithPackage("sdk.agent").
WithComponent("aclUpdateJob")
job := &aclUpdateJob{
logger: logger,
}
return job
}
func (j *aclUpdateJob) Ready() bool {
return true
}
func (j *aclUpdateJob) Status() error {
return nil
}
func (j *aclUpdateJob) Execute() error {
newTeamIDs := agent.cacheManager.GetTeamsIDsInAPIServices()
sort.Strings(newTeamIDs)
if j.lastTeamIDs != nil && strings.Join(newTeamIDs, "") == strings.Join(j.lastTeamIDs, "") {
return nil
}
if err := j.updateACL(newTeamIDs); err != nil {
return fmt.Errorf("acl update job failed: %s", err)
}
j.lastTeamIDs = sort.StringSlice(newTeamIDs)
return nil
}
func (j *aclUpdateJob) getACLName() string {
return fmt.Sprintf(envACLFormat, GetCentralConfig().GetEnvironmentName())
}
func (j *aclUpdateJob) initializeACLJob() {
if acl := agent.cacheManager.GetAccessControlList(); acl != nil {
return
}
acl, err := agent.apicClient.GetAccessControlList(j.getACLName())
if err != nil {
return
}
if aclInstance, err := acl.AsInstance(); err == nil {
agent.cacheManager.SetAccessControlList(aclInstance)
}
}
func (j *aclUpdateJob) createACLResource(teamIDs []string) *management.AccessControlList {
acl, _ := management.NewAccessControlList(
j.getACLName(),
management.EnvironmentGVK().Kind,
agent.cfg.GetEnvironmentName(),
)
acl.Spec = management.AccessControlListSpec{
Rules: []management.AccessRules{
{
Access: []management.AccessLevelScope{
{
Level: "scope",
},
},
},
},
}
// Add all the teams
acl.Spec.Subjects = make([]v1.Owner, 0)
for _, id := range teamIDs {
acl.Spec.Subjects = append(acl.Spec.Subjects, v1.Owner{
Type: v1.TeamOwner,
ID: id,
})
}
return acl
}
func (j *aclUpdateJob) updateACL(teamIDs []string) error {
// do not add an acl if there are no teamIDs and an ACL currently does not exist
currentACL := agent.cacheManager.GetAccessControlList()
if len(teamIDs) == 0 && currentACL == nil {
return nil
}
var err error
j.logger.Trace("acl about to be updated")
acl := j.createACLResource(teamIDs)
if currentACL != nil {
acl, err = agent.apicClient.UpdateAccessControlList(acl)
} else {
acl, err = agent.apicClient.CreateAccessControlList(acl)
}
if err == nil {
aclInstance, err := acl.AsInstance()
if err == nil {
agent.cacheManager.SetAccessControlList(aclInstance)
}
}
return err
}
// registerAccessControlListHandler -
func registerAccessControlListHandler() {
job := newACLUpdateJob()
jobs.RegisterIntervalJobWithName(job, agent.cfg.GetPollInterval(), "Access Control List")
}