forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.go
109 lines (91 loc) · 2.6 KB
/
daemon.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
package providerrefresh
import (
"context"
"fmt"
"time"
"github.com/pkg/errors"
"github.com/rancher/rancher/pkg/auth/tokens"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/robfig/cron"
"github.com/sirupsen/logrus"
)
var (
ref *refresher
c = cron.New()
)
func StartRefreshDaemon(ctx context.Context, scaledContext *config.ScaledContext, mgmtContext *config.ManagementContext, refreshCronTime string, maxAge string) {
ref = &refresher{
tokenLister: mgmtContext.Management.Tokens("").Controller().Lister(),
tokens: mgmtContext.Management.Tokens(""),
userLister: mgmtContext.Management.Users("").Controller().Lister(),
tokenMGR: tokens.NewManager(ctx, scaledContext),
userAttributes: mgmtContext.Management.UserAttributes(""),
userAttributeLister: mgmtContext.Management.UserAttributes("").Controller().Lister(),
settingLister: mgmtContext.Management.Settings("").Controller().Lister(),
}
UpdateRefreshMaxAge(maxAge)
UpdateRefreshCronTime(refreshCronTime)
}
func UpdateRefreshCronTime(refreshCronTime string) {
if ref == nil {
return
}
parsed, err := ParseCron(refreshCronTime)
if err != nil {
logrus.Errorf("%v", err)
return
}
c.Stop()
c = cron.New()
if parsed != nil {
job := cron.FuncJob(RefreshAllForCron)
c.Schedule(parsed, job)
c.Start()
}
}
func UpdateRefreshMaxAge(maxAge string) {
if ref == nil {
return
}
ref.ensureMaxAgeUpToDate(maxAge)
}
func RefreshAllForCron() {
if ref == nil {
return
}
logrus.Debug("Triggering auth refresh cron")
ref.refreshAll(false)
}
func RefreshAttributes(attribs *v3.UserAttribute) (*v3.UserAttribute, error) {
if ref == nil {
return nil, errors.Errorf("refresh daemon not yet initialized")
}
logrus.Debugf("Starting refresh process for %v", attribs.Name)
modified, err := ref.refreshAttributes(attribs)
if err != nil {
return nil, err
}
logrus.Debugf("Finished refresh process for %v", attribs.Name)
modified.LastRefresh = time.Now().UTC().Format(time.RFC3339)
modified.NeedsRefresh = false
return modified, nil
}
func ParseMaxAge(setting string) (time.Duration, error) {
durString := fmt.Sprintf("%vs", setting)
dur, err := time.ParseDuration(durString)
if err != nil {
return 0, fmt.Errorf("Error parsing auth refresh max age: %v", err)
}
return dur, nil
}
func ParseCron(setting string) (cron.Schedule, error) {
if setting == "" {
return nil, nil
}
schedule, err := cron.ParseStandard(setting)
if err != nil {
return nil, fmt.Errorf("Error parsing auth refresh cron: %v", err)
}
return schedule, nil
}