-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
331 lines (267 loc) · 8.64 KB
/
util.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/http/pprof"
"os"
"strconv"
"time"
gcppubsub "cloud.google.com/go/pubsub"
"github.com/Dynom/ERI/cmd/web/erihttp"
"github.com/Dynom/ERI/cmd/web/hitlist"
"github.com/Dynom/ERI/cmd/web/persist"
"github.com/Dynom/ERI/cmd/web/pubsub"
"github.com/Dynom/ERI/cmd/web/pubsub/gcp"
"github.com/Dynom/ERI/runtimer"
"github.com/Dynom/ERI/types"
"github.com/Dynom/ERI/validator"
"github.com/Dynom/TySug/finder"
"google.golang.org/api/option"
"github.com/sirupsen/logrus"
"github.com/Dynom/ERI/cmd/web/config"
)
const (
ErrExConfig = 78 // As per sysexits
ErrExUnavailable = 69
)
func confHeadersToHTTPHeaders(ch config.Headers) http.Header {
headers := http.Header{}
for h, v := range ch {
headers.Add(h, v)
}
return headers
}
func newLogger(conf config.Config) (*logrus.Logger, *io.PipeWriter, error) {
var err error
logger := logrus.New()
if conf.Log.Format == config.LFJSON {
logger.SetFormatter(&logrus.JSONFormatter{})
} else {
logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: `15:04:05.999999`,
})
}
logger.SetOutput(os.Stdout)
level, err := logrus.ParseLevel(conf.Log.Level)
if err == nil {
logger.SetLevel(level)
}
return logger, logger.WriterLevel(level), err
}
func registerProfileHandler(mux *http.ServeMux, conf config.Config) {
if !conf.Server.Profiler.Enable {
return
}
var prefix = "debug"
if conf.Server.Profiler.Prefix != "" {
prefix = conf.Server.Profiler.Prefix
}
mux.HandleFunc(`/`+prefix+`/pprof/`, pprof.Index)
mux.HandleFunc(`/`+prefix+`/pprof/cmdline`, pprof.Cmdline)
mux.HandleFunc(`/`+prefix+`/pprof/profile`, pprof.Profile)
mux.HandleFunc(`/`+prefix+`/pprof/symbol`, pprof.Symbol)
mux.HandleFunc(`/`+prefix+`/pprof/trace`, pprof.Trace)
}
func setCustomResolver(dialer *net.Dialer, host string) {
if dialer.Resolver == nil {
dialer.Resolver = &net.Resolver{
PreferGo: true,
}
}
dialer.Resolver.Dial = func(ctx context.Context, network, address string) (conn net.Conn, e error) {
d := net.Dialer{}
return d.DialContext(ctx, network, net.JoinHostPort(host, `53`))
}
}
func deferClose(toClose io.Closer, log logrus.FieldLogger) {
if toClose == nil {
return
}
err := toClose.Close()
if err != nil {
if log == nil {
fmt.Printf("error failed to close handle %s", err)
return
}
log.WithError(err).Error("Failed to close handle")
}
}
func mapValidatorTypeToValidatorFn(vt config.ValidatorType, v validator.EmailValidator) validator.CheckFn {
switch vt {
case config.VTLookup:
return v.CheckWithLookup
case config.VTStructure:
return v.CheckWithSyntax
}
panic(fmt.Sprintf("Incorrect validator %q configured.", vt))
}
func createProxiedValidator(conf config.Config, logger logrus.FieldLogger, hitList *hitlist.HitList, myFinder *finder.Finder, pubSubSvc *gcp.PubSubSvc, persister persist.Persister) validator.CheckFn {
var dialer = &net.Dialer{}
if conf.Validator.Resolver != "" {
setCustomResolver(dialer, conf.Validator.Resolver)
}
val := validator.NewEmailAddressValidator(dialer)
// Pick the validator we want to use
checkValidator := mapValidatorTypeToValidatorFn(conf.Validator.SuggestValidator, val)
// Last in the chain, so that the duration only applies to the actual validation call
checkValidator = validatorContextTTLProxy(conf.Server.NetTTL.AsDuration(), checkValidator)
checkValidator = validatorHitListProxy(hitList, logger, checkValidator)
checkValidator = validatorUpdateFinderProxy(myFinder, hitList, logger, checkValidator)
if persister != nil {
logger.Info("Adding persisting validator proxy")
checkValidator = validatorPersistProxy(persister, hitList, logger, checkValidator)
}
if pubSubSvc != nil {
checkValidator = validatorNotifyProxy(pubSubSvc, hitList, logger, checkValidator)
}
return checkValidator
}
func registerHealthHandler(mux *http.ServeMux, logger logrus.FieldLogger) {
healthHandler := NewHealthHandler(logger)
mux.HandleFunc("/", healthHandler)
mux.HandleFunc("/health", healthHandler)
}
func pubSubNotificationHandler(hitList *hitlist.HitList, logger logrus.FieldLogger, myFinder *finder.Finder) gcp.NotifyFn {
logger = logger.WithField("handler", "notification")
return func(ctx context.Context, notification pubsub.Notification) {
parts := types.NewEmailFromParts(notification.Data.Local, notification.Data.Domain)
if _, exists := hitList.Has(parts); exists {
logger.WithFields(logrus.Fields{
"notification": notification,
}).Debug("Ignoring notification, as it's already known")
return
}
vr := validator.Result{
Validations: notification.Data.Validations,
Steps: notification.Data.Steps,
}
err := hitList.Add(parts, vr)
if err != nil {
logger.WithFields(logrus.Fields{
"error": err,
"data": notification.Data,
"ctx": ctx.Err(),
}).Error("Unable to add to hitlist")
}
if vr.Validations.IsValidationsForValidDomain() && !myFinder.Exact(parts.Domain) {
myFinder.Refresh(hitList.GetValidAndUsageSortedDomains())
}
}
}
func createPersister(conf config.Config, logger logrus.FieldLogger, hitList *hitlist.HitList) (persist.Persister, error) {
var driver = conf.Backend.Driver
var backend persist.Persister
logger = logger.WithField("backend_driver", driver)
switch driver {
case "postgres":
sqlDB, err := configurePGBackend(conf)
if err != nil {
return nil, err
}
backend = persist.New(sqlDB, logger)
case "memory":
backend = persist.NewMemory()
case "":
logger.Info("Not setting up persistency, driver is not defined")
return nil, nil
default:
return nil, fmt.Errorf("unsupported backend driver %q", driver)
}
var added uint64
logger.Debug("Backend defined, starting read and building memory structures")
err := backend.Range(context.Background(), func(d hitlist.Domain, r hitlist.Recipient, vr validator.Result) error {
err := hitList.AddInternalParts(d, r, vr)
if err != nil {
logger.WithError(err).Warn("Unable to hydrate hitList")
}
added++
return nil
})
if err != nil {
logger.WithError(err).Warn("Unable Range the database")
return nil, err
}
logger.WithField("added", added).Info("Hydrated hitList")
return backend, nil
}
func configurePGBackend(conf config.Config) (*sql.DB, error) {
db, err := sql.Open(conf.Backend.Driver, conf.Backend.URL)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(int(conf.Backend.MaxConnections))
db.SetMaxIdleConns(int(conf.Backend.MaxIdleConnections))
err = db.Ping()
if err != nil {
return nil, err
}
return db, nil
}
func createPubSubSvc(conf config.Config, logger logrus.FieldLogger, rt *runtimer.SignalHandler, hitList *hitlist.HitList, myFinder *finder.Finder) (*gcp.PubSubSvc, error) {
if conf.GCP.PubSubTopic == "" {
logger.Info("Not setting up pub/sub connection, no Topic defined")
return nil, nil
}
psClientCtx, psClientCtxCancel := context.WithCancel(context.Background())
psClient, err := gcppubsub.NewClient(
psClientCtx,
conf.GCP.ProjectID,
option.WithUserAgent("eri-"+Version),
option.WithCredentialsFile(conf.GCP.CredentialsFile),
)
if err != nil {
psClientCtxCancel()
return nil, err
}
pubSubSvc := gcp.NewPubSubSvc(
logger,
psClient,
conf.GCP.PubSubTopic,
gcp.WithSubscriptionLabels([]string{conf.Server.InstanceID, Version, strconv.FormatInt(time.Now().Unix(), 10)}),
gcp.WithSubscriptionConcurrencyCount(5),
)
// Setting up listening to notifications
pubSubCtx, cancel := context.WithCancel(context.Background())
rt.RegisterCallback(func(s os.Signal) {
logger.Printf("Captured signal: %v. Starting cleanup", s)
logger.Debug("Canceling pub/sub context")
cancel()
})
rt.RegisterCallback(func(s os.Signal) {
logger.Debug("Closing Pub/Sub service")
deferClose(pubSubSvc, logger)
})
rt.RegisterCallback(func(s os.Signal) {
logger.Debug("Canceling GCP client context")
psClientCtxCancel()
})
logger.Debug("Starting listener...")
err = pubSubSvc.Listen(pubSubCtx, pubSubNotificationHandler(hitList, logger, myFinder))
if err != nil {
return nil, err
}
return pubSubSvc, nil
}
// writeErrorJSONResponse Sets the error on a response and writes it with the corresponding Content-Type
func writeErrorJSONResponse(logger logrus.FieldLogger, w http.ResponseWriter, responseType erihttp.ERIResponse) {
responseType.PrepareResponse()
response, err := json.Marshal(responseType)
if err != nil {
logger.WithError(err).Error("Failed to marshal the response")
response = []byte(`{"error":""}`)
}
w.Header().Set("Content-Type", "application/json")
c, err := w.Write(response)
if err != nil {
logger.WithFields(logrus.Fields{
"error": err,
"bytes_written": c,
}).Error("Failed to write response")
}
}