-
Notifications
You must be signed in to change notification settings - Fork 1
/
vermouthMaintenance.go
243 lines (211 loc) · 7.38 KB
/
vermouthMaintenance.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package vermouth
import (
"crypto/x509"
"encoding/json"
"github.com/ipifony/vermouth/logger"
"github.com/ipifony/vermouth/stivs"
"io"
"net"
"net/http"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
)
var (
AppName = "Vermouth"
AppVersion = "0.0.0-dev"
UserAgent = AppName + "/" + AppVersion + " (" + runtime.GOOS + ")"
)
var (
httpClient = &http.Client{}
startTime = time.Now()
lastCaRefresh = time.Time{}
lastCrlRefresh = time.Time{}
stiPaRootCert *x509.Certificate
)
func BeginVermouthOperations() {
// Begin listening for OS signals
go func() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGHUP, syscall.SIGINT)
for sig := range sigChan {
switch sig {
case syscall.SIGHUP:
// We use this for logging rotation
logger.OurLogger.RotateLogs()
case syscall.SIGINT:
close(sigChan)
logger.OurLogger.SyncAndCloseLogs(0)
}
}
}()
// First let's check to see if we are the latest version of the software
go checkForUpdate()
// Load STI-PA Root
tmpRootCert, err := loadStiPaRoot()
if err != nil {
logger.LogChan <- &logger.LogMessage{Severity: logger.FATAL, MsgStr: "STI-PA Trust: STI-PA Root unavailable - " + err.Error()}
return
}
stiPaRootCert = tmpRootCert
// Add STI-PA Root to cert pool
stiPaRootPool := x509.NewCertPool()
stiPaRootPool.AddCert(stiPaRootCert)
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "STI-PA Trust: Root cert ready"}
// Load known STI-PA CRL URLs
crlUrls := GlobalConfig.GetStiPaApiCrlUrls()
// Load CA trust cache
loadCachedTrust()
// Configure our HTTP client for later use
tr := &http.Transport{
MaxIdleConns: 50,
MaxIdleConnsPerHost: 50,
IdleConnTimeout: 30 * time.Second,
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
}).DialContext,
}
httpClient = &http.Client{
Transport: tr,
Timeout: 60 * time.Second,
}
// crlWorkChan will act as a resource constrained CRL fetcher and verifier
crlWorkChan := make(chan string)
// responseChan will be used by any go routines passing messages upstream
responseChan := make(chan Message, 20)
caTrustRefreshTicker := time.NewTicker(24 * time.Hour)
dnoRefreshTicker := time.NewTicker(24 * 7 * time.Hour)
refreshDNO(responseChan)
var acmeCertRefreshTicker = &time.Ticker{}
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "Vermouth: STI-AS will operate in ACME mode"}
acmeCertRefreshTicker = time.NewTicker(24 * time.Hour)
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "Vermouth: Initial Refresh of trust stores..."}
go refreshTrustAnchors(responseChan, stiPaRootPool)
go beginCrlWorkerQueue(responseChan, crlWorkChan)
// Initialize CRLs
for _, url := range crlUrls {
crlWorkChan <- url
}
go readyDataCollection()
go startAuthVerifyServer(responseChan, crlWorkChan)
cacheCleanupTimer := time.NewTicker(24 * time.Hour)
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "Vermouth: About to enter infinite maintenance loop..."}
// Do forever
for {
select {
case msg := <-responseChan:
switch msg.MessageSender {
case TrustAnchorWorker:
if msg.MessageType == Success {
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "STI-PA CA List: Refresh Successful. Next attempt in 24 hours..."}
caTrustRefreshTicker.Reset(24 * time.Hour) // 24 hours
lastCaRefresh = time.Now()
} else if msg.MessageType == Failure {
logger.LogChan <- &logger.LogMessage{Severity: logger.ERROR, MsgStr: "STI-PA CA List: Refresh Failed. Next attempt in 1 hour..."}
caTrustRefreshTicker.Reset(1 * time.Hour) // 1 hour
}
case CrlWorker:
info := strings.Split(msg.MsgStr, ";")
if !IsUrl(info[0]) {
if msg.MessageType == Success {
lastCrlRefresh = time.Now()
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "STI-PA CRL: Refresh Successful but no CRL info was communicated"}
} else {
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "STI-PA CRL: Refresh Failed and no CRL info was communicated"}
}
break
}
duration, err := time.ParseDuration(info[1])
if err != nil {
if msg.MessageType == Success {
lastCrlRefresh = time.Now()
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "STI-PA CRL: Refresh Successful for " + info[0] + " but refresh not scheduled"}
} else {
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "STI-PA CRL: Refresh Failed for " + info[0] + " and refresh not scheduled"}
}
break
}
if msg.MessageType == Success {
lastCrlRefresh = time.Now()
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "STI-PA CRL: Refresh Successful for " + info[0] + ". Next refresh in " + duration.String()}
} else {
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "STI-PA CRL: Refresh failed for " + info[0] + ". Next refresh in " + duration.String()}
}
go scheduleCrlJob(duration, info[0], crlWorkChan)
case AuthVerifyWorker:
if msg.MessageType == Failure {
// For now, let's just delay a few seconds and restart the server if something unexpected happens
go func() {
time.Sleep(5 * time.Second)
go startAuthVerifyServer(responseChan, crlWorkChan)
}()
}
case DNOWorker:
if msg.MessageType == Success {
dnoRefreshTicker.Reset(24 * 7 * time.Hour)
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "DNO: Refresh successful"}
} else {
dnoRefreshTicker.Reset(1 * time.Hour)
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "DNO: Refresh failed"}
}
}
case <-caTrustRefreshTicker.C:
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "STI-PA CA List: Maintenance..."}
go refreshTrustAnchors(responseChan, stiPaRootPool)
case <-acmeCertRefreshTicker.C:
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "Acme Client: Maintenance..."}
go CheckAndRefreshAcmeCert()
case <-cacheCleanupTimer.C:
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "STI-VS: Cache Maintenance..."}
go stivs.GetCertCache().CleanExpired()
go checkForUpdate() // Just relying on the cache clean-up timer to also check for software updates
case <-dnoRefreshTicker.C:
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "Do Not Originate: Refreshing patterns..."}
}
}
}
type MessageSender int
const (
TrustAnchorWorker MessageSender = iota
CrlWorker
AuthVerifyWorker
DNOWorker
)
type MessageType int
const (
Success MessageType = iota
Failure
)
func (m MessageType) String() string {
return [...]string{"Success", "Failure"}[m]
}
type Message struct {
MessageSender
MessageType
MsgStr string
}
type ExternalStableVersion struct {
CurrentStable string `json:"currentStable"`
}
func checkForUpdate() {
versionUrl := "https://www.martinisecurity.com/downloads/vermouthversion.json"
resp, err := httpClient.Get(versionUrl)
if err != nil {
return
}
if resp.StatusCode != 200 {
return
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
extVersion := ExternalStableVersion{}
json.Unmarshal(body, &extVersion)
if AppVersion != extVersion.CurrentStable {
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "Vermouth: Software out-of-date! Please update to version " + extVersion.CurrentStable}
}
}