Skip to content

Commit

Permalink
Log email, IP only in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
p53 committed Jul 2, 2024
1 parent 2cdd995 commit afbcd39
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 11 deletions.
6 changes: 4 additions & 2 deletions pkg/keycloak/proxy/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,12 @@ func loginHandler(

if err != nil {
clientIP := utils.RealIP(req)
scope.Logger.Error(err.Error(),
scope.Logger.Debug(
"login from",
zap.String("client_ip", clientIP),
zap.String("remote_addr", req.RemoteAddr),
)
scope.Logger.Error(err.Error())
writer.WriteHeader(code)
}
}
Expand Down Expand Up @@ -812,7 +814,7 @@ func logoutHandler(
case http.StatusOK:
scope.Logger.Info(
"successfully logged out of the endpoint",
zap.String("email", user.Email),
zap.String("userID", user.ID),
)
default:
content, _ := io.ReadAll(response.Body)
Expand Down
6 changes: 2 additions & 4 deletions pkg/proxy/middleware/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,24 @@ func LoggingMiddleware(
return
}

addr := utils.RealIP(req)
if verbose {
requestLogger := logger.With(
zap.Any("headers", req.Header),
zap.String("path", req.URL.Path),
zap.String("method", req.Method),
zap.String("client_ip", addr),
)
scope.Logger = requestLogger
}

next.ServeHTTP(resp, req)

addr := utils.RealIP(req)

if req.URL.Path == req.URL.RawPath || req.URL.RawPath == "" {
scope.Logger.Info("client request",
zap.Duration("latency", time.Since(start)),
zap.Int("status", resp.Status()),
zap.Int("bytes", resp.BytesWritten()),
zap.String("client_ip", addr),
zap.String("remote_addr", req.RemoteAddr),
zap.String("method", req.Method),
zap.String("path", req.URL.Path))
Expand All @@ -130,7 +129,6 @@ func LoggingMiddleware(
zap.Duration("latency", time.Since(start)),
zap.Int("status", resp.Status()),
zap.Int("bytes", resp.BytesWritten()),
zap.String("client_ip", addr),
zap.String("remote_addr", req.RemoteAddr),
zap.String("method", req.Method),
zap.String("path", req.URL.Path),
Expand Down
3 changes: 0 additions & 3 deletions pkg/proxy/middleware/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func AuthenticationMiddleware(
return
}

clientIP := utils.RealIP(req)
scope.Logger.Debug("authentication middleware")

// grab the user identity from the request
Expand All @@ -71,9 +70,7 @@ func AuthenticationMiddleware(
scope.Identity = user
ctx := context.WithValue(req.Context(), constant.ContextScopeName, scope)
lLog := scope.Logger.With(
zap.String("client_ip", clientIP),
zap.String("remote_addr", req.RemoteAddr),
zap.String("username", user.Name),
zap.String("sub", user.ID),
zap.String("expired_on", user.ExpiresAt.String()),
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/middleware/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func AdmissionMiddleware(
user := scope.Identity
lLog := scope.Logger.With(
zap.String("access", "denied"),
zap.String("email", user.Email),
zap.String("userID", user.ID),
zap.String("resource", resource.URL),
)

Expand Down
1 change: 1 addition & 0 deletions pkg/testsuite/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2515,6 +2515,7 @@ func TestLogRealIP(t *testing.T) {

cfg := newFakeKeycloakConfig()
cfg.EnableLogging = true
cfg.Verbose = true

var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)
Expand Down
6 changes: 5 additions & 1 deletion pkg/utils/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func CheckClaim(
errFields := []zapcore.Field{
zap.String("claim", claimName),
zap.String("access", "denied"),
zap.String("email", user.Email),
zap.String("userID", user.ID),
zap.String("resource", resourceURL),
}

Expand Down Expand Up @@ -236,6 +236,10 @@ func CheckClaim(

lLog.Warn(
"claim requirement does not match claim in token",
)

lLog.Debug(
"claims",
zap.String("issued", claims),
zap.String("required", match.String()),
)
Expand Down

0 comments on commit afbcd39

Please sign in to comment.