From 7f427bcacc36bba3b6f2bace2f067346d0128244 Mon Sep 17 00:00:00 2001 From: Devang Date: Mon, 31 May 2021 17:33:49 +0530 Subject: [PATCH] addressed more pr review comments --- go.mod | 6 +-- go.sum | 10 +++- pkg/http-server/start.go | 26 ++++++++-- pkg/utils/http/logwriter.go | 95 ------------------------------------- 4 files changed, 33 insertions(+), 104 deletions(-) delete mode 100644 pkg/utils/http/logwriter.go diff --git a/go.mod b/go.mod index 9d5f0dcac..140297bac 100644 --- a/go.mod +++ b/go.mod @@ -35,12 +35,12 @@ require ( github.com/spf13/cobra v1.1.1 github.com/zclconf/go-cty v1.7.1 go.uber.org/zap v1.16.0 - golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 - golang.org/x/tools v0.1.1 // indirect + golang.org/x/sys v0.0.0-20210531080801-fdfd190a6549 + golang.org/x/tools v0.1.2 // indirect gopkg.in/src-d/go-git.v4 v4.13.1 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b helm.sh/helm/v3 v3.4.0 - honnef.co/go/tools v0.1.4 // indirect + honnef.co/go/tools v0.2.0 // indirect k8s.io/api v0.19.2 k8s.io/apimachinery v0.19.2 k8s.io/client-go v10.0.0+incompatible diff --git a/go.sum b/go.sum index 9315b989d..c589e1e05 100644 --- a/go.sum +++ b/go.sum @@ -1153,10 +1153,12 @@ golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E= -golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 h1:yhBbb4IRs2HS9PPlAg6DMC6mUOKexJBNsLf4Z+6En1Q= golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E= +golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210531080801-fdfd190a6549 h1:OL5GcZ2XPkte3dpfuFQ9o884vrE3BZQhajdntNMruv4= +golang.org/x/sys v0.0.0-20210531080801-fdfd190a6549/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -1232,6 +1234,8 @@ golang.org/x/tools v0.0.0-20201028111035-eafbe7b904eb/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs= golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.1.2 h1:kRBLX7v7Af8W7Gdbbc908OJcdgtK8bOz9Uaj8/F1ACA= +golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -1390,6 +1394,8 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9 honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.1.4 h1:SadWOkti5uVN1FAMgxn165+Mw00fuQKyk4Gyn/inxNQ= honnef.co/go/tools v0.1.4/go.mod h1:NgwopIslSNH47DimFoV78dnkksY2EFtX0ajyb3K/las= +honnef.co/go/tools v0.2.0 h1:ws8AfbgTX3oIczLPNPCu5166oBg9ST2vNs0rcht+mDE= +honnef.co/go/tools v0.2.0/go.mod h1:lPVVZ2BS5TfnjLyizF7o7hv7j9/L+8cZY2hLyjP9cGY= k8s.io/api v0.19.0 h1:XyrFIJqTYZJ2DU7FBE/bSPz7b1HvbVBuBf07oeo6eTc= k8s.io/api v0.19.0/go.mod h1:I1K45XlvTrDjmj5LoM5LuP/KYrhWbjUKT/SoPG0qTjw= k8s.io/apiextensions-apiserver v0.19.2 h1:oG84UwiDsVDu7dlsGQs5GySmQHCzMhknfhFExJMz9tA= diff --git a/pkg/http-server/start.go b/pkg/http-server/start.go index a1621c592..c9efe9349 100644 --- a/pkg/http-server/start.go +++ b/pkg/http-server/start.go @@ -20,6 +20,7 @@ import ( "context" httputils "github.com/accurics/terrascan/pkg/utils/http" gorillaHandlers "github.com/gorilla/handlers" + "go.uber.org/zap" "net/http" "os" "os/signal" @@ -56,7 +57,7 @@ func (g *APIServer) start(routes []*Route, port, certFile, privateKeyFile string router = mux.NewRouter() // new router ) - logWriter := httputils.GetLogWriter(logger) + logWriter := getLogWriter(logger) logger.Info("registering routes...") @@ -71,12 +72,12 @@ func (g *APIServer) start(routes []*Route, port, certFile, privateKeyFile string // register all routes for _, v := range routes { logger.Info("Route ", v.verb, " - ", v.path) - handler := gorillaHandlers.CustomLoggingHandler(logWriter, http.HandlerFunc(v.fn), httputils.WriteRequestLog) + handler := gorillaHandlers.LoggingHandler(logWriter, http.HandlerFunc(v.fn)) router.Methods(v.verb).Path(v.path).Handler(handler) } - router.NotFoundHandler = gorillaHandlers.CustomLoggingHandler(logWriter, http.HandlerFunc(httputils.NotFound), httputils.WriteRequestLog) - router.MethodNotAllowedHandler = gorillaHandlers.CustomLoggingHandler(logWriter, http.HandlerFunc(httputils.NotAllowed), httputils.WriteRequestLog) + router.NotFoundHandler = gorillaHandlers.LoggingHandler(logWriter, http.HandlerFunc(httputils.NotFound)) + router.MethodNotAllowedHandler = gorillaHandlers.LoggingHandler(logWriter, http.HandlerFunc(httputils.NotAllowed)) // Add a route for all static templates / assets. Currently used for the Webhook logs views // go/terrascan/asset is the path where the assets files are located inside the docker container @@ -122,3 +123,20 @@ func (g *APIServer) start(routes []*Route, port, certFile, privateKeyFile string } logger.Info("server exiting gracefully") } + +type logWriter struct { + logger *zap.SugaredLogger +} + +// GetLogWriter creates and fetches a LogWriter object +func getLogWriter(logger *zap.SugaredLogger) logWriter { + return logWriter{ + logger: logger, + } +} + +// Write writes the byte array to the logger object +func (l logWriter) Write(p []byte) (n int, err error) { + l.logger.Info(string(p)) + return len(p), nil +} diff --git a/pkg/utils/http/logwriter.go b/pkg/utils/http/logwriter.go deleted file mode 100644 index 32c96ae0c..000000000 --- a/pkg/utils/http/logwriter.go +++ /dev/null @@ -1,95 +0,0 @@ -/* - Copyright (C) 2020 Accurics, Inc. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package httputils - -import ( - gorillaHandlers "github.com/gorilla/handlers" - "go.uber.org/zap" - "io" - "net" - "net/http" - "net/url" - "strconv" -) - -// LogWriter a type that can write logs to a zap.SugaredLogger object -type LogWriter struct { - logger *zap.SugaredLogger -} - -// GetLogWriter creates and fetches a LogWriter object -func GetLogWriter(logger *zap.SugaredLogger) LogWriter { - return LogWriter{ - logger: logger, - } -} - -// Write writes the byte array to the logger object -func (l LogWriter) Write(p []byte) (n int, err error) { - l.logger.Info(string(p)) - return len(p), nil -} - -// buildCommonLogLine builds a log entry for req in Apache Common Log Format. -// ts is the timestamp with which the entry should be logged. -// status and size are used to provide the response HTTP status and size. -func buildCommonLogLine(req *http.Request, url url.URL, status int, size int) []byte { - username := "-" - if url.User != nil { - if name := url.User.Username(); name != "" { - username = name - } - } - - host, _, err := net.SplitHostPort(req.RemoteAddr) - if err != nil { - host = req.RemoteAddr - } - - uri := req.RequestURI - - // Requests using the CONNECT method over HTTP/2.0 must use - // the authority field (aka r.Host) to identify the target. - // Refer: https://httpwg.github.io/specs/rfc7540.html#CONNECT - if req.ProtoMajor == 2 && req.Method == "CONNECT" { - uri = req.Host - } - if uri == "" { - uri = url.RequestURI() - } - - buf := make([]byte, 0, 3*(len(host)+len(username)+len(req.Method)+len(uri)+len(req.Proto)+50)/2) - buf = append(buf, host...) - buf = append(buf, " - "...) - buf = append(buf, username...) - buf = append(buf, ` "`...) - buf = append(buf, req.Method...) - buf = append(buf, " "...) - buf = append(buf, uri...) - buf = append(buf, " "...) - buf = append(buf, req.Proto...) - buf = append(buf, `" `...) - buf = append(buf, strconv.Itoa(status)...) - buf = append(buf, " "...) - buf = append(buf, strconv.Itoa(size)...) - return buf -} - -// WriteRequestLog logs an http(s) request to a write object -func WriteRequestLog(writer io.Writer, params gorillaHandlers.LogFormatterParams) { - writer.Write(buildCommonLogLine(params.Request, params.URL, params.StatusCode, params.Size)) -}