-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
152 lines (131 loc) · 3.26 KB
/
pool.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
package main
import (
"bytes"
"errors"
"strconv"
"strings"
"sync"
"time"
"github.com/e-XpertSolutions/f5-rest-client/f5"
"github.com/e-XpertSolutions/f5-rest-client/f5/ltm"
"github.com/e-XpertSolutions/f5-rest-client/f5/sys"
)
type worker struct {
url string
crlName string
profileName string
refreshDelay time.Duration
validate bool
stopCh chan struct{}
}
func (w *worker) run(f5Clients []*f5.Client, l logger) {
w.stopCh = make(chan struct{})
go func() {
w.do(f5Clients, l)
for {
select {
case <-time.After(w.refreshDelay):
w.do(f5Clients, l)
case <-w.stopCh:
l.Notice("stop signal received, terminating worker routine")
return
}
}
}()
}
func (w *worker) do(f5Clients []*f5.Client, l logger) {
// Make sure no panic will interrupt the program.
defer func() {
if r := recover(); r != nil {
l.Error("panic recovered: ", r)
}
}()
crl, err := fetchCRL(w.url)
if err != nil {
l.Error(err)
return
}
for _, f5Client := range f5Clients {
if err := w.pushCRLToClients(f5Client, crl); err != nil {
l.Error(err)
}
}
}
func (w *worker) pushCRLToClients(f5Client *f5.Client, crl []byte) error {
tx, err := f5Client.Begin()
if err != nil {
return err
}
buf := bytes.NewBuffer(crl)
crlName := w.crlName + "_" + strconv.FormatInt(time.Now().Unix(), 10)
sysClient := sys.New(tx)
err = sysClient.FileSSLCRL().CreateFromFile(crlName, buf, int64(buf.Len()))
if err != nil {
return err
}
ltmClient := ltm.New(tx)
cfg, err := ltmClient.ProfileClientSSL().Get(w.profileName)
if err != nil {
return errors.New("cannot get client ssl: " + err.Error())
}
// .crl extension is automatically added while uploading the file, therefore
// we need to concatenate it to crlName so thtat the client-ssl API can
// retrieve it.
cfg.CRLFile = crlName + ".crl"
if err := ltmClient.ProfileClientSSL().Edit(w.profileName, *cfg); err != nil {
return errors.New("cannot modify client-ssl: " + err.Error())
}
if err := tx.Commit(); err != nil {
return err
}
// Now that the transaction has been committed, we verify that everything
// worked as intended.
cfg, err = ltmClient.ProfileClientSSL().Get(w.profileName)
if err != nil {
return errors.New("cannot retrieve updated client ssl profile: " + err.Error())
}
if !strings.HasSuffix(cfg.CRLFile, crlName+".crl") {
return errors.New("client-ssl has not been updated with the newly updated crl")
}
return nil
}
func (w *worker) stop() {
w.stopCh <- struct{}{}
close(w.stopCh)
}
type pool struct {
workers []*worker
}
func (p *pool) addWorker(cfg crlConfig) {
p.workers = append(p.workers, &worker{
url: cfg.URL,
crlName: cfg.Name,
profileName: cfg.ProfileName,
refreshDelay: cfg.RefreshDelay.Duration * time.Hour,
validate: cfg.Validate,
})
}
func (p *pool) startAll(f5Clients []*f5.Client, l logger) error {
if f5Clients == nil {
return errors.New("f5 clients list is nil")
}
if len(f5Clients) == 0 {
return errors.New("f5 clients list is empty")
}
for _, w := range p.workers {
w.run(f5Clients, l)
}
return nil
}
// XXX(gilliek): wait with timeout?
func (p *pool) stopAll() {
var wg sync.WaitGroup
for _, w := range p.workers {
wg.Add(1)
go func(ww *worker) {
defer wg.Done()
ww.stop()
}(w)
}
wg.Wait()
}