-
Notifications
You must be signed in to change notification settings - Fork 13
/
credendtialvalidator.go
162 lines (134 loc) · 4.17 KB
/
credendtialvalidator.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
package agent
import (
"sync"
"time"
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/apic/provisioning"
"github.com/Axway/agent-sdk/pkg/config"
"github.com/Axway/agent-sdk/pkg/jobs"
"github.com/Axway/agent-sdk/pkg/util/log"
)
const (
status = "status"
state = "state"
)
type cacheManager interface {
GetWatchResourceCacheKeys(group, kind string) []string
GetWatchResourceByKey(key string) *v1.ResourceInstance
}
type apicClient interface {
UpdateResourceInstance(ri v1.Interface) (*v1.ResourceInstance, error)
CreateSubResource(rm v1.ResourceMeta, subs map[string]interface{}) error
}
type credentialValidator struct {
jobs.Job
id string
logger log.FieldLogger
cacheManager cacheManager
client apicClient
}
func newCredentialChecker(cacheManager cacheManager, client apicClient) *credentialValidator {
return &credentialValidator{
logger: log.NewFieldLogger().WithComponent("credentialValidator"),
cacheManager: cacheManager,
client: client,
}
}
// Ready -
func (j *credentialValidator) Ready() bool {
return true
}
// Status -
func (j *credentialValidator) Status() error {
return nil
}
// Execute -
func (j *credentialValidator) Execute() error {
j.logger.Debug("validating credentials for expiration")
if agent.cfg.GetCredentialConfig() == nil ||
!agent.cfg.GetCredentialConfig().ShouldDeprovisionExpired() {
return nil
}
// Get all of the credentials from the cache
credKeys := j.cacheManager.GetWatchResourceCacheKeys(management.CredentialGVK().Group, management.CredentialGVK().Kind)
// loop all the keys in the cache and check if any have expired
now := time.Now()
wg := &sync.WaitGroup{}
for _, k := range credKeys {
wg.Add(1)
func(credKey string) {
j.validateCredential(credKey, now)
}(k)
}
return nil
}
func (j *credentialValidator) validateCredential(credKey string, now time.Time) {
logger := j.logger.WithField("cacheKey", credKey)
res := j.cacheManager.GetWatchResourceByKey(credKey)
if res == nil {
logger.Error("could not get resource by key")
return
}
cred := &management.Credential{}
err := cred.FromInstance(res)
if err != nil {
logger.WithError(err).Error("could not convert resource instance to credential")
return
}
if cred.Policies.Expiry == nil {
return
}
expTime := time.Time(cred.Policies.Expiry.Timestamp)
if expTime.IsZero() {
// cred does not expire
return
}
logger = logger.WithField("credName", cred.Name).WithField("expiration", expTime.Format(v1.APIServerTimeFormat))
logger.Trace("validating credential")
if expTime.Before(now) {
logger.Info("Credential has expired, updating Central")
cred.Status.Level = provisioning.Pending.String()
// update state so the inactivated credential will come back for removal
cred.Spec.State = management.CredentialSpecState{
Name: v1.Inactive,
Reason: provisioning.CredExpDetail,
}
// only update a subset of the sub resources
subResources := map[string]interface{}{
status: cred.Status,
state: cred.State,
}
_, err = j.client.UpdateResourceInstance(cred)
if err != nil {
logger.WithError(err).Error("error update credential resources")
}
err = j.client.CreateSubResource(cred.ResourceMeta, subResources)
if err != nil {
logger.WithError(err).Error("error creating subresources")
}
}
}
func registerCredentialChecker() *credentialValidator {
c := newCredentialChecker(agent.cacheManager, agent.apicClient)
err := agent.cfg.SetWatchResourceFilters([]config.ResourceFilter{
{
Group: management.CredentialGVK().Group,
Kind: management.CredentialGVK().Kind,
Name: "*",
IsCachedResource: true,
},
})
if err != nil {
c.logger.WithError(err).Error("could not watch for the credential resource in the credential validator job")
return nil
}
id, err := jobs.RegisterScheduledJobWithName(c, "@hourly", "CredentialValidator")
if err != nil {
c.logger.WithError(err).Error("could not start the credential validator job")
return nil
}
c.logger.Debug("registered the credential validator")
c.id = id
return c
}