forked from eumel8/cosignwebhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (89 loc) · 2.6 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
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
log "github.com/gookit/slog"
"github.com/eumel8/cosignwebhook/webhook"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
port = "8080"
mport = "8081"
logTemplate = "[{{datetime}}] [{{level}}] {{caller}} {{message}} \n"
)
var tlscert, tlskey string
func main() {
// parse arguments
flag.StringVar(&tlscert, "tlsCertFile", "/etc/certs/tls.crt", "File containing the x509 Certificate for HTTPS.")
flag.StringVar(&tlskey, "tlsKeyFile", "/etc/certs/tls.key", "File containing the x509 private key to --tlsCertFile.")
logLevel := flag.String("logLevel", "info", "loglevel of app, e.g info, debug, warn, error, fatal")
flag.Parse()
// set log level
switch *logLevel {
case "fatal":
log.SetLogLevel(log.FatalLevel)
case "trace":
log.SetLogLevel(log.TraceLevel)
case "debug":
log.SetLogLevel(log.DebugLevel)
case "error":
log.SetLogLevel(log.ErrorLevel)
case "warn":
log.SetLogLevel(log.WarnLevel)
case "info":
log.SetLogLevel(log.InfoLevel)
default:
log.SetLogLevel(log.InfoLevel)
}
log.GetFormatter().(*log.TextFormatter).SetTemplate(logTemplate)
certs, err := tls.LoadX509KeyPair(tlscert, tlskey)
if err != nil {
log.Errorf("failed to load key pair: %v", err)
}
server := &http.Server{
Addr: fmt.Sprintf(":%v", port),
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{certs},
MinVersion: tls.VersionTLS12,
},
ReadHeaderTimeout: 10 * time.Second,
}
mserver := &http.Server{
Addr: fmt.Sprintf(":%v", mport),
ReadHeaderTimeout: 10 * time.Second,
}
// define http server and server handler
cs := webhook.NewCosignServerHandler()
mux := http.NewServeMux()
mux.HandleFunc("/validate", cs.Serve)
server.Handler = mux
mmux := http.NewServeMux()
mmux.HandleFunc("/healthz", cs.Healthz)
mmux.Handle("/metrics", promhttp.Handler())
mserver.Handler = mmux
go func() {
if err := server.ListenAndServeTLS("", ""); err != nil {
log.Errorf("Failed to listen and serve webhook server: %v", err)
}
}()
go func() {
if err := mserver.ListenAndServe(); err != nil {
log.Errorf("Failed to listen and serve monitor server: %v", err)
}
}()
log.Info("Webhook server running", "port", port, "metricsPort", mport)
// listening shutdown signal
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
<-signalChan
log.Info("Got shutdown signal, shutting down webhook server gracefully...")
_ = server.Shutdown(context.Background())
_ = mserver.Shutdown(context.Background())
}