-
Notifications
You must be signed in to change notification settings - Fork 4
/
haproxy_spoe.go
267 lines (216 loc) · 5.25 KB
/
haproxy_spoe.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
package source
import (
"encoding/json"
"errors"
"fmt"
"net"
"strings"
spoe "github.com/criteo/haproxy-spoe-go"
"github.com/criteo/traffic-mirroring/mirror"
"github.com/criteo/traffic-mirroring/mirror/registry"
log "github.com/sirupsen/logrus"
)
const (
HAProxySPOEName = "source.haproxy_spoe"
)
func init() {
registry.Register(HAProxySPOEName, NewHAProxySPOE)
}
type mappingFunc func(req *mirror.Request, value interface{}) error
var defaultMappingConfig = map[string]string{
"method": "method",
"ver": "ver",
"path": "path",
"headers": "headers",
"body": "body",
}
type HAProxySPOEConfig struct {
ListenAddr string `json:"listen_addr"`
Mapping map[string]string
}
type HAProxySPOE struct {
cfg HAProxySPOEConfig
ctx *mirror.ModuleContext
out chan mirror.Request
mapping map[string]mappingFunc
}
func NewHAProxySPOE(ctx *mirror.ModuleContext, cfg []byte) (mirror.Module, error) {
mod := &HAProxySPOE{
ctx: ctx,
out: make(chan mirror.Request),
mapping: map[string]mappingFunc{},
}
err := json.Unmarshal(cfg, &mod.cfg)
if err != nil {
return nil, err
}
if len(mod.cfg.ListenAddr) == 0 {
return nil, errors.New("listen_addr is required")
}
mappingCfg := defaultMappingConfig
for k, v := range mod.cfg.Mapping {
mappingCfg[k] = v
}
for k, v := range mappingCfg {
switch v {
case "method":
mod.mapping[k] = mapMethod
case "ver":
mod.mapping[k] = mapVer
case "path":
mod.mapping[k] = mapPath
case "headers":
mod.mapping[k] = mapHeaders
case "body":
mod.mapping[k] = mapBody
default:
if !strings.HasPrefix(v, "meta.") {
return nil, fmt.Errorf("unknown mapping key %q", v)
}
mod.mapping[k] = mapMeta(strings.TrimPrefix(v, "meta."))
}
}
err = mod.start()
if err != nil {
return nil, err
}
return mod, nil
}
func (m *HAProxySPOE) Context() *mirror.ModuleContext {
return m.ctx
}
func (m *HAProxySPOE) Children() [][]mirror.Module {
return nil
}
func (m *HAProxySPOE) Output() <-chan mirror.Request {
return m.out
}
func (m *HAProxySPOE) SetInput(c <-chan mirror.Request) {
log.Fatalf("%s: connot accept input", HAProxySPOEName)
}
func (m *HAProxySPOE) start() error {
agent := spoe.New(m.handleMessage)
var l net.Listener
var err error
if m.cfg.ListenAddr[0] == '@' {
l, err = net.Listen("unix", m.cfg.ListenAddr[1:])
} else {
l, err = net.Listen("tcp", m.cfg.ListenAddr)
}
if err != nil {
return err
}
go func() {
err := agent.Serve(l)
if err != nil {
log.Errorf("%s: %s", HAProxySPOEName, err)
}
close(m.out)
}()
return nil
}
func (m *HAProxySPOE) handleMessage(msgs *spoe.MessageIterator) ([]spoe.Action, error) {
for msgs.Next() {
msg := msgs.Message
req := mirror.Request{}
for msg.Args.Next() {
arg := msg.Args.Arg
m, ok := m.mapping[arg.Name]
if !ok {
continue
}
err := m(&req, arg.Value)
if err != nil {
log.Errorf("%s: bad message: %s", HAProxySPOEName, err)
}
}
m.ctx.HandledRequest()
m.out <- req
}
if err := msgs.Error(); err != nil {
log.Errorf("%s: error handling message: %s", HAProxySPOEName, err)
}
return nil, nil
}
func mapMethod(req *mirror.Request, value interface{}) error {
method, ok := value.(string)
if !ok {
return fmt.Errorf("bad type %T received for method, expected string", value)
}
i, ok := mirror.Method_value[method]
if !ok {
return fmt.Errorf("unknown method %q", value)
}
req.Method = mirror.Method(i)
return nil
}
func mapPath(req *mirror.Request, value interface{}) error {
path, ok := value.(string)
if !ok {
return fmt.Errorf("bad type %T received for path, expected string", value)
}
req.Path = path
return nil
}
func mapVer(req *mirror.Request, value interface{}) error {
ver, ok := value.(string)
if !ok {
return fmt.Errorf("bad type %T received for http version, expected string", value)
}
switch ver {
case "1.1":
req.HttpVersion = mirror.HTTPVersion_HTTP1_1
case "2":
req.HttpVersion = mirror.HTTPVersion_HTTP2
default:
req.HttpVersion = mirror.HTTPVersion_HTTP1_0
}
return nil
}
func mapHeaders(req *mirror.Request, value interface{}) error {
rawHeaders, ok := value.([]byte)
if !ok {
return fmt.Errorf("bad type %T received for headers, expected bytes", value)
}
headers, err := spoe.DecodeHeaders(rawHeaders)
if err != nil {
return err
}
req.Headers = map[string]*mirror.HeaderValue{}
for name, values := range headers {
req.Headers[name] = &mirror.HeaderValue{Values: values}
}
return nil
}
func mapBody(req *mirror.Request, value interface{}) error {
body, ok := value.([]byte)
if !ok {
return fmt.Errorf("bad type %T received for body, expected bytes", value)
}
req.Body = body
return nil
}
func mapMeta(name string) mappingFunc {
return func(req *mirror.Request, value interface{}) error {
if req.Meta == nil {
req.Meta = map[string]*mirror.MetaValue{}
}
switch t := value.(type) {
case string:
req.Meta[name] = &mirror.MetaValue{
Value: &mirror.MetaValue_String_{t},
}
case int:
req.Meta[name] = &mirror.MetaValue{
Value: &mirror.MetaValue_Int{int64(t)},
}
case bool:
req.Meta[name] = &mirror.MetaValue{
Value: &mirror.MetaValue_Bool{t},
}
default:
return fmt.Errorf("unhandled type for meta %T, allowed values are string, int, bool", value)
}
return nil
}
}