-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
368 lines (316 loc) · 8.08 KB
/
config.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package whalewall
import (
"bytes"
"errors"
"fmt"
"net/netip"
"slices"
"strconv"
"go.uber.org/zap/zapcore"
"go4.org/netipx"
)
type config struct {
MappedPorts mappedPorts `yaml:"mapped_ports"`
Output []ruleConfig
}
type mappedPorts struct {
Localhost localRules
External externalRules
}
// TODO: allow users to specify addrOrRange that is within 127.0.0.1/8?
type localRules struct {
Allow bool
LogPrefix string `yaml:"log_prefix"`
Verdict verdict
}
type externalRules struct {
Allow bool
LogPrefix string `yaml:"log_prefix"`
IPs []addrOrRange
Verdict verdict
}
type ruleConfig struct {
LogPrefix string `yaml:"log_prefix"`
Network string
IPs []addrOrRange
Container string
Proto protocol
SrcPorts []rulePorts `yaml:"src_ports"`
DstPorts []rulePorts `yaml:"dst_ports"`
Verdict verdict
skip bool
}
func (r ruleConfig) MarshalLogObject(enc zapcore.ObjectEncoder) error {
if r.LogPrefix != "" {
enc.AddString("log_prefix", r.LogPrefix)
}
if r.Network != "" {
enc.AddString("network", r.Network)
}
if len(r.IPs) != 0 {
if err := enc.AddArray("ips", addrsList(r.IPs)); err != nil {
return err
}
}
if r.Container != "" {
enc.AddString("container", r.Container)
}
enc.AddString("proto", r.Proto.String())
if len(r.SrcPorts) != 0 {
if err := enc.AddArray("src_ports", portsList(r.SrcPorts)); err != nil {
return err
}
}
if len(r.DstPorts) != 0 {
if err := enc.AddArray("dst_ports", portsList(r.DstPorts)); err != nil {
return err
}
}
if err := enc.AddObject("verdict", r.Verdict); err != nil {
return err
}
return nil
}
type verdict struct {
Chain string
Queue uint16
InputEstQueue uint16 `yaml:"input_est_queue"`
OutputEstQueue uint16 `yaml:"output_est_queue"`
drop bool
}
func (v verdict) MarshalLogObject(enc zapcore.ObjectEncoder) error {
if v.Chain != "" {
enc.AddString("chain", v.Chain)
}
if v.Queue != 0 {
enc.AddUint16("queue", v.Queue)
}
if v.InputEstQueue != 0 {
enc.AddUint16("input_est_queue", v.InputEstQueue)
}
if v.OutputEstQueue != 0 {
enc.AddUint16("output_est_queue", v.OutputEstQueue)
}
enc.AddBool("drop", v.drop)
return nil
}
type addrsList []addrOrRange
func (a addrsList) MarshalLogArray(enc zapcore.ArrayEncoder) error {
for _, addr := range a {
if err := enc.AppendObject(addr); err != nil {
return err
}
}
return nil
}
type addrOrRange struct {
addr netip.Addr
addrRange netipx.IPRange
}
func (a addrOrRange) MarshalText() ([]byte, error) {
if a.addr.IsValid() {
return a.addr.MarshalText()
}
return a.addrRange.MarshalText()
}
func (a addrOrRange) MarshalBinary() ([]byte, error) {
return a.MarshalText()
}
func (a *addrOrRange) UnmarshalText(text []byte) error {
if bytes.ContainsRune(text, '/') {
prefix := new(netip.Prefix)
err := prefix.UnmarshalText(text)
if err != nil {
return err
}
a.addrRange = netipx.RangeOfPrefix(*prefix)
return nil
} else if bytes.ContainsRune(text, '-') {
return a.addrRange.UnmarshalText(text)
}
return a.addr.UnmarshalText(text)
}
func (a *addrOrRange) UnmarshalBinary(data []byte) error {
return a.UnmarshalText(data)
}
func (a addrOrRange) MarshalLogObject(enc zapcore.ObjectEncoder) error {
if a.addr.IsValid() {
enc.AddString("addr", a.addr.String())
} else {
enc.AddString("addrs", a.addrRange.String())
}
return nil
}
func (a *addrOrRange) IsValid() bool {
return a.addr.IsValid() || a.addrRange.IsValid()
}
func (a *addrOrRange) Addr() (netip.Addr, bool) {
return a.addr, a.addr.IsValid()
}
func (a *addrOrRange) Range() (netip.Addr, netip.Addr, bool) {
return a.addrRange.From(), a.addrRange.To(), a.addrRange.IsValid()
}
type protocol uint8
const (
invalidProto protocol = iota
tcp
udp
)
func (p protocol) MarshalText() ([]byte, error) {
switch p {
case invalidProto:
return nil, errors.New("invalid protocol")
case tcp:
return []byte("tcp"), nil
case udp:
return []byte("udp"), nil
default:
panic("unreachable")
}
}
func (p *protocol) UnmarshalText(text []byte) error {
switch {
case bytes.Equal(text, []byte("tcp")):
*p = tcp
case bytes.Equal(text, []byte("udp")):
*p = udp
default:
return fmt.Errorf("invalid protocol %q", string(text))
}
return nil
}
func (p protocol) String() string {
switch p {
case invalidProto:
return "invalid protocol"
case tcp:
return "tcp"
case udp:
return "udp"
default:
return fmt.Sprintf("proto(%d)", p)
}
}
type portsList []rulePorts
func (p portsList) MarshalLogArray(enc zapcore.ArrayEncoder) error {
for _, port := range p {
if err := enc.AppendObject(port); err != nil {
return err
}
}
return nil
}
type rulePorts struct {
single uint16
interval portInterval
}
type portInterval struct {
min uint16
max uint16
}
func (p rulePorts) MarshalText() ([]byte, error) {
if p.single != 0 {
return []byte(strconv.Itoa(int(p.single))), nil
}
return []byte(fmt.Sprintf("%d-%d", p.interval.min, p.interval.max)), nil
}
func (p rulePorts) MarshalBinary() ([]byte, error) {
return p.MarshalText()
}
func (p *rulePorts) UnmarshalText(text []byte) error {
var intervalIdx int
validChars := []byte{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-'}
for i, char := range text {
if !slices.Contains(validChars, char) {
return fmt.Errorf("invalid character %q in port", char)
}
if char == '-' {
if intervalIdx != 0 {
return errors.New("there can only be one '-' if specifying a port interval")
}
if i == len(text)-1 {
return errors.New("port interval can't end with a '-'")
}
intervalIdx = i
}
}
var parsedPorts rulePorts
if intervalIdx != 0 {
intervalMin, err := strconv.ParseUint(string(text[:intervalIdx]), 10, 16)
if err != nil {
return fmt.Errorf("error parsing start of port interval: %w", err)
}
intervalMax, err := strconv.ParseUint(string(text[intervalIdx+1:]), 10, 16)
if err != nil {
return fmt.Errorf("error parsing end of port interval: %w", err)
}
parsedPorts.interval = portInterval{
min: uint16(intervalMin),
max: uint16(intervalMax),
}
} else {
port, err := strconv.ParseUint(string(text), 10, 16)
if err != nil {
return fmt.Errorf("error parsing port: %w", err)
}
parsedPorts.single = uint16(port)
}
*p = parsedPorts
return nil
}
func (p *rulePorts) UnmarshalBinary(data []byte) error {
return p.UnmarshalText(data)
}
func (p rulePorts) MarshalLogObject(enc zapcore.ObjectEncoder) error {
text, _ := p.MarshalText()
enc.AddString("ports", string(text))
return nil
}
func validateConfig(c config) error {
for i, r := range c.Output {
err := validateRule(r)
if err != nil {
return fmt.Errorf("output rule #%d: %w", i, err)
}
}
return nil
}
func validateRule(r ruleConfig) error {
if len(r.IPs) == 0 && r.Container == "" && r.Proto == invalidProto && len(r.SrcPorts) == 0 && len(r.DstPorts) == 0 {
return errors.New("rule is empty")
}
if len(r.IPs) != 0 && r.Container != "" {
return errors.New(`"ip" and "container" are mutually exclusive`)
}
if r.Network == "" && r.Container != "" {
return errors.New(`"network" must be set when "container" is set`)
}
if len(r.SrcPorts) != 0 && r.Proto == invalidProto {
return errors.New(`"proto" must be set when "src_ports" is set`)
}
if len(r.DstPorts) != 0 && r.Proto == invalidProto {
return errors.New(`"proto" must be set when "dst_ports" is set`)
}
if r.Proto != invalidProto && len(r.DstPorts) == 0 {
return errors.New(`"dst_ports" must be set when "proto" is set`)
}
return validateVerdict(r.Verdict)
}
func validateVerdict(v verdict) error {
if v.Chain != "" && v.Queue != 0 {
return errors.New(`"chain" and "queue" are mutually exclusive`)
}
if v.Queue == 0 && v.InputEstQueue != 0 {
return errors.New(`"queue" must be set when "input_est_queue" is set`)
}
if v.Queue == 0 && v.OutputEstQueue != 0 {
return errors.New(`"queue" must be set when "output_est_queue" is set`)
}
if v.InputEstQueue == 0 && v.OutputEstQueue != 0 {
return errors.New(`"input_est_queue" must be set when "output_est_queue" is set`)
}
if v.OutputEstQueue == 0 && v.InputEstQueue != 0 {
return errors.New(`"output_est_queue" must be set when "input_est_queue" is set`)
}
return nil
}