diff --git a/pkg/http-server/start.go b/pkg/http-server/start.go index a1621c592..18b8682dd 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)) -}