-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.go
132 lines (114 loc) · 3.25 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
// 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 autoReload bool
var cpuProfile string
var memProfile string
var globalLogger = zerolog.New(os.Stderr).With().Timestamp().Logger()
func main() {
flag.StringVar(&configPath, "config", "", "configuration file")
flag.BoolVar(&autoReload, "autoreload", false, "reload configuration file on k8s configmap update")
flag.StringVar(&cpuProfile, "cpuprofile", "", "write cpu profile to `file`")
flag.StringVar(&memProfile, "memprofile", "", "write memory profile to `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")
}
}()
if autoReload {
go func() {
if err := cfg.watchConfig(a); err != nil {
globalLogger.Fatal().Err(err).Msg("Config watcher failed")
}
}()
}
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 := cfg.reloadConfig(a)
if err != nil {
globalLogger.Error().Err(err).Msg("Failed to reload configuration, using old configuration")
continue
}
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")
}
}
}