Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject log level via CLI flag; add debug logging for incoming requests #742

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions cmd/aws-iam-authenticator/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -75,6 +77,7 @@ func init() {

func initConfig() {
logrus.SetFormatter(getLogFormatter())
logrus.SetLevel(getLogLevel())
if cfgFile == "" {
return
}
Expand Down Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"regexp"
"strings"
"sync"
Expand Down Expand Up @@ -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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure we want this? It'll dump all tokens to logs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point... I'll update this to only log safe fields from the request

}).Debug("Request received")
}

if req.Method != http.MethodPost {
log.Error("unexpected request method")
http.Error(w, "expected POST", http.StatusMethodNotAllowed)
Expand Down