forked from aquasecurity/postee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
186 lines (153 loc) · 5.02 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
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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/aquasecurity/postee/v2/controller"
"github.com/aquasecurity/postee/v2/dbservice"
"github.com/aquasecurity/postee/v2/router"
"github.com/aquasecurity/postee/v2/runner"
"github.com/aquasecurity/postee/v2/utils"
"github.com/aquasecurity/postee/v2/webserver"
"github.com/spf13/cobra"
)
const (
URL = "0.0.0.0:8082"
TLS = "0.0.0.0:8445"
URL_USAGE = "The socket to bind to, specified using host:port."
TLS_USAGE = "The TLS socket to bind to, specified using host:port."
CFG_FILE = "/config/cfg.yaml"
CFG_USAGE = "The alert configuration file."
)
var (
url = ""
tls = ""
cfgfile = ""
controllerMode = false
controllerURL = ""
controllerCARootPath = ""
controllerTLSCertPath = ""
controllerTLSKeyPath = ""
controllerSeedFilePath = ""
runnerSeedFilePath = ""
runnerName = ""
runnerCARootPath = ""
runnerTLSCertPath = ""
runnerTLSKeyPath = ""
)
var rootCmd = &cobra.Command{
Use: "webhooksrv",
Short: fmt.Sprintf("Aqua Container Security Webhook server\n"),
Long: fmt.Sprintf("Aqua Container Security Webhook server\n"),
}
func init() {
rootCmd.Flags().StringVar(&url, "url", URL, URL_USAGE)
rootCmd.Flags().StringVar(&tls, "tls", TLS, TLS_USAGE)
rootCmd.Flags().StringVar(&cfgfile, "cfgfile", CFG_FILE, CFG_USAGE)
rootCmd.Flags().BoolVar(&controllerMode, "controller-mode", false, "run postee in controller mode")
rootCmd.Flags().StringVar(&controllerURL, "controller-url", "", "postee controller URL")
rootCmd.Flags().StringVar(&controllerCARootPath, "controller-ca-root", "", "postee controller ca root file")
rootCmd.Flags().StringVar(&controllerTLSCertPath, "controller-tls-cert", "", "postee controller TLS cert file")
rootCmd.Flags().StringVar(&controllerTLSKeyPath, "controller-tls-key", "", "postee controller TLS key file")
rootCmd.Flags().StringVar(&controllerSeedFilePath, "controller-seed-file", "", "postee controller AuthN seed file")
rootCmd.Flags().StringVar(&runnerName, "runner-name", "", "postee runner name")
rootCmd.Flags().StringVar(&runnerCARootPath, "runner-ca-root", "", "postee runner ca root file")
rootCmd.Flags().StringVar(&runnerTLSCertPath, "runner-tls-cert", "", "postee runner tls cert file")
rootCmd.Flags().StringVar(&runnerTLSKeyPath, "runner-tls-key", "", "postee runner tls key file")
rootCmd.Flags().StringVar(&runnerSeedFilePath, "runner-seed-file", "", "postee runner AuthN seed file")
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
utils.InitDebug()
rootCmd.Run = func(cmd *cobra.Command, args []string) {
rtr := router.Instance()
if runnerName != "" {
if controllerMode {
log.Fatal("postee cannot run as a controller when running in runner mode")
}
f, err := ioutil.TempFile("", "temp-postee-config-*") // TODO: Find a better way
if err != nil {
log.Fatal("Unable to create temp file for runner config on disk: ", err)
}
rnr := runner.Runner{
ControllerURL: controllerURL,
RunnerSeedFilePath: runnerSeedFilePath,
RunnerCARootPath: runnerCARootPath,
RunnerTLSKeyPath: runnerTLSKeyPath,
RunnerTLSCertPath: runnerTLSCertPath,
RunnerName: runnerName,
}
if err := rnr.Setup(rtr, f); err != nil {
log.Fatal("Failed to launch runner: ", err)
}
defer func() { os.Remove(f.Name()) }()
cfgfile = f.Name()
}
if controllerMode {
if runnerName != "" {
log.Fatal("postee cannot run as a runner when running in controller mode")
}
ctr := controller.Controller{
ControllerURL: controllerURL,
ControllerSeedFilePath: controllerSeedFilePath,
ControllerCAFile: controllerCARootPath,
ControllerTLSKeyPath: controllerTLSKeyPath,
ControllerTLSCertPath: controllerTLSCertPath,
RunnerName: runnerName,
}
if err := ctr.Setup(rtr); err != nil {
log.Fatal("Failed to launch controller: ", err)
}
}
if os.Getenv("AQUAALERT_URL") != "" {
url = os.Getenv("AQUAALERT_URL")
}
if os.Getenv("POSTEE_HTTP") != "" {
url = os.Getenv("POSTEE_HTTP")
}
if os.Getenv("AQUAALERT_TLS") != "" {
tls = os.Getenv("AQUAALERT_TLS")
}
if os.Getenv("POSTEE_HTTPS") != "" {
tls = os.Getenv("POSTEE_HTTPS")
}
if os.Getenv("AQUAALERT_CFG") != "" {
cfgfile = os.Getenv("AQUAALERT_CFG")
}
if os.Getenv("POSTEE_CFG") != "" {
cfgfile = os.Getenv("POSTEE_CFG")
}
if os.Getenv("PATH_TO_DB") != "" {
dbservice.SetNewDbPathFromEnv()
}
err := rtr.Start(cfgfile)
if err != nil {
log.Printf("Can't start alert manager %v", err)
return
}
defer rtr.Terminate()
go webserver.Instance().Start(url, tls)
defer webserver.Instance().Terminate()
Daemonize()
}
err := rootCmd.Execute()
if err != nil {
log.Printf("Can't start command %v", err)
return
}
}
func Daemonize() {
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
log.Println(sig)
done <- true
}()
<-done
}