-
Notifications
You must be signed in to change notification settings - Fork 35
/
coordinator.go
181 lines (159 loc) · 4.84 KB
/
coordinator.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
package engine
import (
"crypto/subtle"
"fmt"
"strings"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/runabol/tork/conf"
"github.com/runabol/tork/internal/coordinator"
"github.com/runabol/tork/internal/uuid"
"github.com/runabol/tork/internal/wildcard"
"github.com/runabol/tork/middleware/job"
"github.com/runabol/tork/middleware/task"
"golang.org/x/time/rate"
)
func (e *Engine) initCoordinator() error {
queues := conf.IntMap("coordinator.queues")
cfg := coordinator.Config{
Name: conf.StringDefault("coordinator.name", "Coordinator"),
Broker: e.broker,
DataStore: e.ds,
Queues: queues,
Address: conf.String("coordinator.address"),
Middleware: coordinator.Middleware{
Web: e.cfg.Middleware.Web,
Task: e.cfg.Middleware.Task,
Job: e.cfg.Middleware.Job,
Node: e.cfg.Middleware.Node,
Echo: echoMiddleware(),
},
Endpoints: e.cfg.Endpoints,
Enabled: conf.BoolMap("coordinator.api.endpoints"),
}
// redact
redactJobEnabled := conf.BoolDefault("middleware.job.redact.enabled", false)
if redactJobEnabled {
cfg.Middleware.Job = append(cfg.Middleware.Job, job.Redact)
cfg.Middleware.Task = append(cfg.Middleware.Task, task.Redact)
}
// webhook middleware
cfg.Middleware.Job = append(cfg.Middleware.Job, job.Webhook)
c, err := coordinator.NewCoordinator(cfg)
if err != nil {
return errors.Wrap(err, "error creating the coordinator")
}
if err := c.Start(); err != nil {
return err
}
e.coordinator = c
return nil
}
func echoMiddleware() []echo.MiddlewareFunc {
mw := make([]echo.MiddlewareFunc, 0)
// cors
corsEnabled := conf.Bool("middleware.web.cors.enabled")
if corsEnabled {
mw = append(mw, cors())
}
// basic auth
basicAuthEnabled := conf.Bool("middleware.web.basicauth.enabled")
if basicAuthEnabled {
username := conf.StringDefault("middleware.web.basicauth.username", "tork")
password := conf.String("middleware.web.basicauth.password")
mw = append(mw, basicAuth(username, password))
}
// rate limit
rateLimitEnabled := conf.Bool("middleware.web.ratelimit.enabled")
if rateLimitEnabled {
rps := conf.IntDefault("middleware.web.ratelimit.rps", 20)
mw = append(mw, rateLimit(rps))
}
loggerEnabled := conf.BoolDefault("middleware.web.logger.enabled", true)
if loggerEnabled {
mw = append(mw, logger())
}
return mw
}
func rateLimit(rps int) echo.MiddlewareFunc {
return middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(rate.Limit(rps)))
}
func basicAuth(username, password string) echo.MiddlewareFunc {
if password == "" {
password = uuid.NewUUID()
log.Debug().Msgf("Basic Auth Password: %s", password)
}
return middleware.BasicAuth(func(user, pass string, ctx echo.Context) (bool, error) {
if subtle.ConstantTimeCompare([]byte(user), []byte(username)) == 1 &&
subtle.ConstantTimeCompare([]byte(pass), []byte(password)) == 1 {
return true, nil
}
return false, nil
})
}
func logger() echo.MiddlewareFunc {
levelStr := conf.StringDefault("middleware.web.logger.level", "DEBUG")
level, err := zerolog.ParseLevel(strings.ToLower(levelStr))
if err != nil {
panic(err)
}
skip := conf.StringsDefault("middleware.web.logger.skip", []string{"GET /health"})
return middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
LogURI: true,
LogStatus: true,
LogRemoteIP: true,
LogMethod: true,
Skipper: func(c echo.Context) bool {
if len(skip) == 0 {
return false
}
for _, pattern := range skip {
if wildcard.Match(pattern, fmt.Sprintf("%s %s", c.Request().Method, c.Request().URL.Path)) {
return true
}
}
return false
},
LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
log.WithLevel(level).
Str("URI", v.URI).
Str("method", v.Method).
Str("remote-ip", v.RemoteIP).
Int("status", v.Status).
Msg("Request")
return nil
},
})
}
func cors() echo.MiddlewareFunc {
type CORSConfig struct {
AllowOrigins []string `koanf:"origins"`
AllowMethods []string `koanf:"methods"`
AllowHeaders []string `koanf:"headers"`
AllowCredentials bool `koanf:"credentials"`
ExposeHeaders []string `koanf:"headers"`
}
cf := CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{"*"},
AllowHeaders: []string{"*"},
AllowCredentials: false,
ExposeHeaders: []string{"*"},
}
if err := conf.Unmarshal("middleware.web.cors", &cf); err != nil {
panic(errors.Wrapf(err, "error parsing CORS middleware config"))
}
log.Debug().Msg("CORS middleware enabled")
return middleware.CORSWithConfig(
middleware.CORSConfig{
AllowOrigins: cf.AllowOrigins,
AllowMethods: cf.AllowMethods,
AllowHeaders: cf.AllowHeaders,
AllowCredentials: cf.AllowCredentials,
ExposeHeaders: cf.AllowHeaders,
},
)
}