-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.go
282 lines (225 loc) · 6.24 KB
/
app.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package main
import (
"bytes"
"crypto/sha256"
"crypto/tls"
"encoding/hex"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/kelseyhightower/envconfig"
"go.uber.org/zap"
"io"
"io/ioutil"
"net"
"strings"
"sync"
"time"
)
func NewApp() *App {
logger, _ := zap.NewDevelopment()
app := &App{config: Config{}}
err := envconfig.Process("sslwatch", &app.config)
if err != nil {
logger.Fatal("failed to initialize")
}
app.metrics = Metrics{mutex: sync.RWMutex{}, db: map[string]Endpoints{}}
app.services = Services{mutex: sync.RWMutex{}, db: map[string]Service{}, reverseMap: map[string]string{}}
if !app.config.DebugMode {
logger, _ = zap.NewProduction()
}
defer logger.Sync()
app.log = *logger
app.config.S3Bucket, app.config.S3Key = ParseS3Path(app.config.ConfigDir)
if app.config.S3Bucket != "" {
err := app.CreateS3Session()
if err != nil {
app.log.Fatal("can't init S3 session", zap.Error(err))
}
}
app.ReloadConfig()
return app
}
func (app *App) ReloadConfig() {
if app.config.S3Bucket != "" {
app.reloadConfigFromS3()
} else {
app.reloadConfigFromFiles()
}
}
func (app *App) reloadConfigFromFiles() {
files, err := ioutil.ReadDir(app.config.ConfigDir)
if err != nil {
app.log.Error("can't read config files dir", zap.Error(err))
}
for _, file := range files {
if strings.HasSuffix(file.Name(), app.config.ConfigFileSuffix) {
raw, err := ioutil.ReadFile(app.config.ConfigDir + "/" + file.Name())
if err == nil {
app.services.Update(raw)
} else {
app.log.Error("can't read config file "+file.Name(), zap.Error(err))
}
}
}
if len(app.services.db) == 0 {
app.log.Fatal("no configs provided")
}
app.log.Debug("domains read from configs", zap.Strings("domains", app.services.ListDomains()))
}
func (app *App) reloadConfigFromS3() {
configHashes, err := app.GetS3ConfigHashes()
if err != nil {
app.log.Error("can't stat objects in s3 bucket", zap.Error(err))
}
for config := range configHashes {
raw, err := app.ReadS3File(config)
if err == nil {
app.services.Update(raw)
} else {
app.log.Error("failed to read s3 config "+config, zap.Error(err))
}
}
if len(app.services.db) == 0 {
app.log.Fatal("no configs provided")
}
app.S3Configs = configHashes
app.log.Debug("domains read from configs", zap.Strings("domains", app.services.ListDomains()))
}
func (app *App) ProcessDomain(domain string, ips []net.IP) Endpoints {
host, port, err := net.SplitHostPort(domain)
if err != nil {
host = domain
port = "443"
}
if len(ips) == 0 {
ips = app.ResolveDomain(host)
}
endpoints := Endpoints{}
for _, ip := range ips {
dialer := net.Dialer{Timeout: app.config.ConnectionTimeout, Deadline: time.Now().Add(app.config.ConnectionTimeout + 5*time.Second)}
if IsIPv4(ip.String()) {
endpoint := Endpoint{}
// We want to be able to connect to an endpoint with an expired/invalid certificate, that's why why use InsecureSkipVerify opion.
// However, we need to clearly mark this for gosec, otherwise it considers this as a vulnerability
connection, err := tls.DialWithDialer(&dialer, "tcp", ip.String()+":"+port, &tls.Config{ServerName: host, InsecureSkipVerify: true}) // #nosec
if err != nil {
app.log.Error(ip.String(), zap.Error(err))
endpoint.alive = false
endpoints[ip.String()] = endpoint
continue
}
sha := sha256.New()
cert := connection.ConnectionState().PeerCertificates[0]
endpoint.alive = true
_, _ = sha.Write(cert.Raw)
endpoint.sha256 = hex.EncodeToString(sha.Sum(nil))
endpoint.expiry = cert.NotAfter
endpoint.CN = cert.Subject.CommonName
endpoint.AltNamesCount = len(cert.DNSNames)
err = cert.VerifyHostname(host)
if err != nil {
endpoint.valid = false
} else {
endpoint.valid = true
}
_ = connection.Close()
endpoints[ip.String()] = endpoint
}
}
return endpoints
}
func (app *App) ResolveDomain(domain string) []net.IP {
timer := time.NewTimer(app.config.LookupTimeout)
ch := make(chan []net.IP, 1)
go func() {
r, err := net.LookupIP(domain)
if err != nil {
app.log.Error("failed to lookup "+domain, zap.Error(err))
return
}
ch <- r
}()
select {
case ips := <-ch:
return ips
case <-timer.C:
}
return make([]net.IP, 0)
}
func (app *App) updateMetrics() {
ticker := time.NewTicker(app.config.ScrapeInterval)
defer ticker.Stop()
for ; true; <-ticker.C {
domains := app.services.ListDomains()
app.log.Debug("current domains", zap.Strings("domains", domains))
for _, domain := range domains {
app.log.Debug("processing domain " + domain)
ips := app.services.GetIPs(domain)
eps := app.ProcessDomain(domain, StrToIp(ips))
app.metrics.Set(domain, eps)
}
}
}
func (app *App) CreateS3Session() error {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(app.config.S3Region)},
)
if err != nil {
return err
}
app.S3Session = sess
return nil
}
func (app *App) ReadS3File(key string) ([]byte, error) {
results, err := s3.New(app.S3Session).GetObject(&s3.GetObjectInput{
Bucket: aws.String(app.config.S3Bucket),
Key: aws.String(key),
})
if err != nil {
return nil, err
}
defer results.Body.Close()
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, results.Body); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (app *App) GetS3ConfigHashes() (map[string]string, error) {
hashes := map[string]string{}
svc := s3.New(app.S3Session)
resp, err := svc.ListObjects(&s3.ListObjectsInput{Bucket: aws.String(app.config.S3Bucket), Prefix: aws.String(app.config.S3Key)})
if err != nil {
return hashes, err
}
for _, item := range resp.Contents {
if strings.HasSuffix(*item.Key, app.config.ConfigFileSuffix) {
hashes[*item.Key] = *item.ETag
}
}
return hashes, nil
}
func (app *App) S3ConfigsChanged(current map[string]string) bool {
changed := false
for k, v := range current {
v1, ok := app.S3Configs[k]
if !ok {
app.log.Debug(k + " config has been added")
changed = true
} else {
if v1 != v {
app.log.Debug(k + " config has been changed")
changed = true
}
}
}
for k := range app.S3Configs {
_, ok := current[k]
if !ok {
app.log.Debug(k + " config has been removed")
changed = true
}
}
return changed
}