From 4f94503933eb60d65d9c8c7da775e1546911f7cc Mon Sep 17 00:00:00 2001 From: Luke Swart Date: Tue, 13 Aug 2024 13:09:29 -0700 Subject: [PATCH] inject log level --- cmd/aws-iam-authenticator/root.go | 15 +++++++++++++++ pkg/server/server.go | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/cmd/aws-iam-authenticator/root.go b/cmd/aws-iam-authenticator/root.go index 3ac4541d0..8fb1b0729 100644 --- a/cmd/aws-iam-authenticator/root.go +++ b/cmd/aws-iam-authenticator/root.go @@ -59,6 +59,8 @@ func init() { rootCmd.PersistentFlags().StringP("log-format", "l", "text", "Specify log format to use when logging to stderr [text or json]") + rootCmd.PersistentFlags().StringP("loglevel", "", "info", "Set the log level (debug, info, warn, error, fatal)") + rootCmd.PersistentFlags().StringP( "cluster-id", "i", @@ -75,6 +77,7 @@ func init() { func initConfig() { logrus.SetFormatter(getLogFormatter()) + logrus.SetLevel(getLogLevel()) if cfgFile == "" { return } @@ -184,3 +187,15 @@ func getLogFormatter() logrus.Formatter { return &logrus.TextFormatter{FullTimestamp: true} } + +func getLogLevel() logrus.Level { + logLevel, _ := rootCmd.PersistentFlags().GetString("loglevel") + + level, err := logrus.ParseLevel(logLevel) + if err != nil { + logrus.Warnf("Unknown log format specified (%s), will use default text formatter instead.", logLevel) + return logrus.InfoLevel + } + logrus.Info("Log level set to ", logLevel) + return level +} diff --git a/pkg/server/server.go b/pkg/server/server.go index 5059ed006..aa27f670b 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -23,6 +23,7 @@ import ( "fmt" "log" "net/http" + "net/http/httputil" "regexp" "strings" "sync" @@ -303,6 +304,17 @@ func (h *handler) authenticateEndpoint(w http.ResponseWriter, req *http.Request) "method": req.Method, }) + if logrus.IsLevelEnabled(logrus.DebugLevel) { + reqDump, err := httputil.DumpRequest(req, true) + if err != nil { + log.Info(err, "Failed to dump http request") + } + + log.WithFields(logrus.Fields{ + "request": string(reqDump), + }).Debug("Request received") + } + if req.Method != http.MethodPost { log.Error("unexpected request method") http.Error(w, "expected POST", http.StatusMethodNotAllowed)