-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
145 lines (123 loc) · 3.56 KB
/
main.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
// Copyright The OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"flag"
"net"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"syscall"
"github.com/rs/zerolog"
"github.com/corazawaf/coraza-spoa/internal"
)
var configPath string
var cpuProfile string
var memProfile string
var globalLogger = zerolog.New(os.Stderr).With().Timestamp().Logger()
func main() {
flag.StringVar(&cpuProfile, "cpuprofile", "", "write cpu profile to `file`")
flag.StringVar(&memProfile, "memprofile", "", "write memory profile to `file`")
flag.StringVar(&configPath, "config", "", "configuration file")
flag.Parse()
if configPath == "" {
globalLogger.Fatal().Msg("Configuration file is not set")
}
if cpuProfile != "" {
f, err := os.Create(cpuProfile)
if err != nil {
globalLogger.Fatal().Err(err).Msg("could not create CPU profile")
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
globalLogger.Fatal().Err(err).Msg("could not start CPU profile")
}
defer pprof.StopCPUProfile()
}
cfg, err := readConfig()
if err != nil {
globalLogger.Fatal().Err(err).Msg("Failed loading config")
}
logger, err := cfg.Log.newLogger()
if err != nil {
globalLogger.Fatal().Err(err).Msg("Failed creating global logger")
}
globalLogger = logger
apps, err := cfg.newApplications()
if err != nil {
globalLogger.Fatal().Err(err).Msg("Failed creating applications")
}
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
network, address := cfg.networkAddressFromBind()
l, err := (&net.ListenConfig{}).Listen(ctx, network, address)
if err != nil {
globalLogger.Fatal().Err(err).Msg("Failed opening socket")
}
a := &internal.Agent{
Context: ctx,
Applications: apps,
Logger: globalLogger,
}
go func() {
defer cancelFunc()
globalLogger.Info().Msg("Starting coraza-spoa")
if err := a.Serve(l); err != nil {
globalLogger.Fatal().Err(err).Msg("listener closed")
}
}()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGINT)
outer:
for {
sig := <-sigCh
switch sig {
case syscall.SIGTERM:
globalLogger.Info().Msg("Received SIGTERM, shutting down...")
// this return will run cancel() and close the server
break outer
case syscall.SIGINT:
globalLogger.Info().Msg("Received SIGINT, shutting down...")
break outer
case syscall.SIGHUP:
globalLogger.Info().Msg("Received SIGHUP, reloading configuration...")
newCfg, err := readConfig()
if err != nil {
globalLogger.Error().Err(err).Msg("Error loading configuration, using old configuration")
continue
}
if cfg.Log != newCfg.Log {
newLogger, err := newCfg.Log.newLogger()
if err != nil {
globalLogger.Error().Err(err).Msg("Error creating new global logger, using old configuration")
continue
}
globalLogger = newLogger
}
if cfg.Bind != newCfg.Bind {
globalLogger.Error().Msg("Changing bind is not supported yet, using old configuration")
continue
}
apps, err := newCfg.newApplications()
if err != nil {
globalLogger.Error().Err(err).Msg("Error applying configuration, using old configuration")
continue
}
a.ReplaceApplications(apps)
cfg = newCfg
}
}
if memProfile != "" {
f, err := os.Create(memProfile)
if err != nil {
globalLogger.Fatal().Err(err).Msg("could not create memory profile")
}
defer f.Close()
runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
globalLogger.Fatal().Err(err).Msg("could not write memory profile")
}
}
}