forked from kubescape/host-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.go
166 lines (144 loc) · 4.64 KB
/
sensor.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
package main
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/codegangsta/negroni"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type bodyLogWriter struct {
negroni.ResponseWriter
resBody *bytes.Buffer
}
// Common HTTP utils
func (blw bodyLogWriter) Write(b []byte) (int, error) {
blw.resBody.Write(b)
return blw.ResponseWriter.Write(b)
}
var (
zapLogger *zap.Logger
)
func initLogger() *log.Logger {
var err error
// https://godoc.org/go.uber.org/zap#AtomicLevel.UnmarshalText
lvl := zap.NewAtomicLevel()
// if config.LogLevel == "" {
// config.LogLevel = "warn"
// }
logLevel := "debug"
if err := lvl.UnmarshalText([]byte(logLevel)); err != nil {
panic(err)
}
ec := zap.NewProductionEncoderConfig()
ec.EncodeTime = zapcore.RFC3339NanoTimeEncoder
zapConf := zap.Config{DisableCaller: true, DisableStacktrace: true, Level: lvl,
Encoding: "json", EncoderConfig: ec,
OutputPaths: []string{"stdout"}, ErrorOutputPaths: []string{"stderr"}}
// if config.LogFileName != "" { // empty string means the output is stdout
// zapConf.OutputPaths = []string{config.LogFileName}
// zapConf.ErrorOutputPaths = append(zapConf.ErrorOutputPaths, config.LogFileName)
// }
zapLogger, err = zapConf.Build()
if err != nil {
panic(err)
}
zap.ReplaceGlobals(zapLogger)
zap.RedirectStdLog(zapLogger)
return zap.NewStdLog(zapLogger)
}
func initHTTPRouter() http.Handler {
negroniRouter := negroni.New()
nLogger := negroni.NewLogger()
nLogger.SetFormat("{{.StartTime}} | {{.Status}} | \t {{.Duration}} | {{.Hostname}} | {{.Method}}" + " {{.Request.RequestURI}}")
negroniRouter.Use(negroni.NewRecovery())
negroniRouter.Use(nLogger)
negroniRouter.UseFunc(filterNLogHTTPErrors)
negroniRouter.UseHandler(http.DefaultServeMux)
return negroniRouter
}
// filterNLogHTTPErrors intercept every HTTP request and in case of failure it logs the request and the response
func filterNLogHTTPErrors(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
startTime := time.Now()
zapArr := []zapcore.Field{zap.String("method", r.Method),
zap.String("requestURI", r.RequestURI),
zap.String("remoteAddr", r.RemoteAddr),
}
if !strings.HasPrefix(r.RequestURI, "/isAlive") {
zap.L().Debug("In filterNLogHTTPErrors", zapArr...)
}
nrw, _ := rw.(negroni.ResponseWriter)
var blw bodyLogWriter
blw.ResponseWriter = nrw
blw.resBody = bytes.NewBuffer([]byte(""))
bodyBuffer, err := ioutil.ReadAll(r.Body)
oldBody := r.Body
defer oldBody.Close()
r.Body = ioutil.NopCloser(bytes.NewReader(bodyBuffer))
defer r.Body.Close()
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(rw, "Cannot read body: %s", err.Error())
} else {
next(blw, r)
}
if blw.Status() < 200 || blw.Status() >= 300 || startTime.Before(time.Now().Add(time.Second*50*(-1))) {
zapLogger.With(append(zapArr,
zap.Timep("requestStartTime", &startTime),
zap.String("Request body", string(bodyBuffer)),
zap.Int("HTTP status", blw.Status()),
zap.Int("Response size", blw.Size()),
zap.String("Response body", blw.resBody.String()))...).Error("Request failed")
}
if r.Body != nil {
io.Copy(ioutil.Discard, r.Body)
}
}
// main
func main() {
fmt.Println("Starting Kubescape cluster node host scanner service")
fmt.Println("Host scanner service build version: " + BuildVersion)
baseLogger := initLogger()
negroniRouter := initHTTPRouter()
defer zapLogger.Sync()
sensorManagerAddress := os.Getenv("ARMO_SENSORS_MANAGER")
connectSensorsManagerWebSocket(sensorManagerAddress)
initHTTPHandlers()
listeningPort := 7888
zapLogger.Info("Listening...", zap.Int("port", listeningPort))
if strings.Contains(os.Getenv("CADB_DEBUG"), "pprof") {
fmt.Println("Debug mode - pprof on")
go func() {
log.Println(http.ListenAndServe(":6060", nil))
}()
}
listenAddress := fmt.Sprintf(":%d", listeningPort)
server := http.Server{Addr: listenAddress, Handler: negroniRouter, ErrorLog: baseLogger, TLSConfig: &tls.Config{}}
go func() {
server.ListenAndServe()
}()
termChan := make(chan os.Signal, 1)
// os.Kill,syscall.SIGKILL, cannot be trapped
signal.Notify(termChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
<-termChan // Blocks here until either SIGINT or SIGTERM is received.
zap.L().Warn("signal received")
ctx, ctxCancel := context.WithTimeout(context.Background(), 61*time.Second)
defer ctxCancel()
if err := server.Shutdown(ctx); err != nil {
zap.L().Error("HTTP shutdown error", zap.Error(err))
}
zap.L().Warn("shutdown gracefully")
}
func connectSensorsManagerWebSocket(sensorManagerAddress string) {
zap.L().Warn("Not implemented")
}