-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Maintaining audit logs (#3763)
* Middleware * logger middleware service layer * updated files * commented code * updated files * updated files * updated files * pr comments * updated files * updated files * updated files * refactoring * removing spilled userAuth object * refactoring * updated files * refactoring functions names * wire gen * making middleware more efficient * adding status code in audit log middleware * log fix * refactoring * refactoring and few fixes * changed package + refactoring * argocd assets added * removed unnecessary code * EOF error fix * request payload place changed to be printed at last --------- Co-authored-by: Prakash Kumar <prakash.kumar@devtron.ai>
- Loading branch information
1 parent
1b57820
commit d6c81b8
Showing
28 changed files
with
129 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package util | ||
|
||
import ( | ||
"bytes" | ||
"github.com/devtron-labs/devtron/internal/middleware" | ||
"github.com/devtron-labs/devtron/pkg/user" | ||
"io" | ||
"log" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type AuditLoggerDTO struct { | ||
UrlPath string `json:"urlPath"` | ||
UserEmail string `json:"userEmail"` | ||
UpdatedOn time.Time `json:"updatedOn"` | ||
QueryParams string `json:"queryParams"` | ||
ApiResponseCode int `json:"apiResponseCode"` | ||
RequestPayload []byte `json:"requestPayload"` | ||
} | ||
|
||
type LoggingMiddlewareImpl struct { | ||
userService user.UserService | ||
} | ||
|
||
func NewLoggingMiddlewareImpl(userService user.UserService) *LoggingMiddlewareImpl { | ||
return &LoggingMiddlewareImpl{ | ||
userService: userService, | ||
} | ||
} | ||
|
||
type LoggingMiddleware interface { | ||
LoggingMiddleware(next http.Handler) http.Handler | ||
} | ||
|
||
// LoggingMiddleware is a middleware function that logs the incoming request. | ||
func (impl LoggingMiddlewareImpl) LoggingMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
d := middleware.NewDelegator(w, nil) | ||
|
||
token := r.Header.Get("token") | ||
userEmail, err := impl.userService.GetEmailFromToken(token) | ||
if err != nil { | ||
log.Printf("AUDIT_LOG: user does not exists") | ||
} | ||
|
||
// Read the request body into a buffer | ||
var bodyBuffer bytes.Buffer | ||
_, err = io.Copy(&bodyBuffer, r.Body) | ||
if err != nil { | ||
log.Printf("AUDIT_LOG: error reading request body for urlPath: %s queryParams: %s userEmail: %s", r.URL.Path, r.URL.Query().Encode(), userEmail) | ||
} | ||
|
||
// Restore the request body for downstream handlers | ||
r.Body = io.NopCloser(&bodyBuffer) | ||
|
||
auditLogDto := &AuditLoggerDTO{ | ||
UrlPath: r.URL.Path, | ||
UserEmail: userEmail, | ||
UpdatedOn: time.Now(), | ||
QueryParams: r.URL.Query().Encode(), | ||
RequestPayload: bodyBuffer.Bytes(), | ||
} | ||
// Call the next handler in the chain. | ||
next.ServeHTTP(d, r) | ||
|
||
auditLogDto.ApiResponseCode = d.Status() | ||
LogRequest(auditLogDto) | ||
}) | ||
} | ||
|
||
func LogRequest(auditLogDto *AuditLoggerDTO) { | ||
log.Printf("AUDIT_LOG: urlPath: %s, queryParams: %s,updatedBy: %s, updatedOn: %s, apiResponseCode: %d,requestPayload: %s", auditLogDto.UrlPath, auditLogDto.QueryParams, auditLogDto.UserEmail, auditLogDto.UpdatedOn, auditLogDto.ApiResponseCode, auditLogDto.RequestPayload) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.