Skip to content

Commit ea97e8a

Browse files
committed
Use Update instead of Save for userAudit and log ClientIP in audit logger
1 parent 87ba4c2 commit ea97e8a

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

api/util/logger.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/devtron-labs/devtron/internal/middleware"
2727
"github.com/devtron-labs/devtron/pkg/auth/user"
28+
"github.com/devtron-labs/devtron/util"
2829
)
2930

3031
type AuditLoggerDTO struct {
@@ -36,6 +37,7 @@ type AuditLoggerDTO struct {
3637
RequestPayload []byte `json:"requestPayload"`
3738
RequestMethod string `json:"requestMethod"`
3839
ResponseTime time.Duration `json:"responseTime"`
40+
ClientIp string `json:"clientIp"`
3941
}
4042

4143
type LoggingMiddlewareImpl struct {
@@ -56,13 +58,12 @@ type LoggingMiddleware interface {
5658
func (impl LoggingMiddlewareImpl) LoggingMiddleware(next http.Handler) http.Handler {
5759
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5860
d := middleware.NewDelegator(w, nil)
59-
6061
token := r.Header.Get("token")
6162
userEmail, err := impl.userService.GetEmailFromToken(token)
6263
if err != nil {
6364
log.Printf("AUDIT_LOG: user does not exists")
6465
}
65-
66+
clientIp := util.GetClientIP(r)
6667
// Read the request body into a buffer
6768
var bodyBuffer bytes.Buffer
6869
_, err = io.Copy(&bodyBuffer, r.Body)
@@ -83,6 +84,7 @@ func (impl LoggingMiddlewareImpl) LoggingMiddleware(next http.Handler) http.Hand
8384
QueryParams: r.URL.Query().Encode(),
8485
RequestPayload: bodyBuffer.Bytes(),
8586
RequestMethod: r.Method,
87+
ClientIp: clientIp,
8688
}
8789
// Call the next handler in the chain.
8890
next.ServeHTTP(d, r)
@@ -95,5 +97,5 @@ func (impl LoggingMiddlewareImpl) LoggingMiddleware(next http.Handler) http.Hand
9597
}
9698

9799
func LogRequest(auditLogDto *AuditLoggerDTO) {
98-
log.Printf("AUDIT_LOG: requestMethod: %s, urlPath: %s, queryParams: %s, updatedBy: %s, updatedOn: %s, apiResponseCode: %d, responseTime: %s, requestPayload: %s", auditLogDto.RequestMethod, auditLogDto.UrlPath, auditLogDto.QueryParams, auditLogDto.UserEmail, auditLogDto.UpdatedOn, auditLogDto.ApiResponseCode, auditLogDto.ResponseTime, auditLogDto.RequestPayload)
100+
log.Printf("AUDIT_LOG: clientIp: %s, requestMethod: %s, urlPath: %s, queryParams: %s, updatedBy: %s, updatedOn: %s, apiResponseCode: %d, responseTime: %s, requestPayload: %s", auditLogDto.ClientIp, auditLogDto.RequestMethod, auditLogDto.UrlPath, auditLogDto.QueryParams, auditLogDto.UserEmail, auditLogDto.UpdatedOn, auditLogDto.ApiResponseCode, auditLogDto.ResponseTime, auditLogDto.RequestPayload)
99101
}

pkg/auth/user/UserService.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,7 @@ func (impl *UserServiceImpl) saveUserAudit(r *http.Request, userId int32) {
16641664
CreatedOn: time.Now(),
16651665
UpdatedOn: time.Now(),
16661666
}
1667-
impl.userAuditService.Save(userAudit)
1667+
impl.userAuditService.Update(userAudit)
16681668
}
16691669

16701670
func (impl *UserServiceImpl) GetRoleFiltersByUserRoleGroups(userRoleGroups []userBean.UserRoleGroup) ([]userBean.RoleFilter, error) {

0 commit comments

Comments
 (0)