From 6978da86d8e8b06534147ae068b6a4122fa1d165 Mon Sep 17 00:00:00 2001 From: Hiveer Date: Wed, 23 Oct 2024 12:06:29 +0800 Subject: [PATCH] Add logout action and optimize logs (#730) * Add logout action on backend and handle redirect path * Optimize the portal server logs format * Update body logs handle logic --- internal/handlers/render/session.go | 21 ++++++++++++++++++ internal/middleware/log.go | 22 +++++++++++++------ internal/routes/router.go | 3 ++- internal/routes/session.go | 1 + .../backend/csghubserver/csghubserver.go | 14 +++++------- pkg/server/backend/csghubserver/jwt.go | 3 +-- .../backend/csghubserver/sensitive_check.go | 3 +-- 7 files changed, 46 insertions(+), 21 deletions(-) diff --git a/internal/handlers/render/session.go b/internal/handlers/render/session.go index f42d6a082..0b706290b 100644 --- a/internal/handlers/render/session.go +++ b/internal/handlers/render/session.go @@ -21,6 +21,7 @@ const ( type SessionHandler interface { Login(ctx *gin.Context) + Logout(ctx *gin.Context) SignUp(ctx *gin.Context) Create(ctx *gin.Context) } @@ -47,6 +48,26 @@ func (i *SessionHandlerImpl) Login(ctx *gin.Context) { ctx.Redirect(http.StatusFound, i.Config.LoginURL) } +func (i *SessionHandlerImpl) Logout(ctx *gin.Context) { + cookies := ctx.Request.Cookies() + paramRedirectPath := ctx.Query("redirect_to") + + // Loop through the cookies and remove them + for _, cookie := range cookies { + ctx.SetCookie(cookie.Name, "", -1, "/", "localhost", false, true) + ctx.SetCookie(cookie.Name, "", -1, "/", "opencsg.com", false, true) + ctx.SetCookie(cookie.Name, "", -1, "/", "stg.opencsg.com", false, true) + ctx.SetCookie(cookie.Name, "", -1, "/", ".opencsg-stg.com", false, true) + ctx.SetCookie(cookie.Name, "", -1, "/", ".opencsg.com", false, true) + } + + if paramRedirectPath == "" { + ctx.Redirect(http.StatusFound, "/") + } else { + ctx.Redirect(http.StatusFound, paramRedirectPath) + } +} + func (i *SessionHandlerImpl) SignUp(ctx *gin.Context) { ctx.Redirect(http.StatusFound, i.Config.SignupURL) } diff --git a/internal/middleware/log.go b/internal/middleware/log.go index 6e9562532..e006c99f2 100644 --- a/internal/middleware/log.go +++ b/internal/middleware/log.go @@ -2,6 +2,7 @@ package middleware import ( "log/slog" + "regexp" "time" "github.com/gin-gonic/gin" @@ -25,12 +26,19 @@ func Log() gin.HandlerFunc { } latency := time.Since(startTime).Milliseconds() - slog.InfoContext(ctx, "http request", slog.String("ip", ctx.ClientIP()), - slog.String("method", ctx.Request.Method), - slog.Int("latency(ms)", int(latency)), - slog.Int("status", ctx.Writer.Status()), - slog.String("current_user", username), - slog.String("url", ctx.Request.URL.RequestURI()), - ) + + assetsImagesReq := regexp.MustCompile(`^/(assets|images)(/.*)?`) + + if assetsImagesReq.MatchString(ctx.Request.RequestURI) { + slog.Info("assets request", ctx.Request.RequestURI, int(latency)) + } else { + slog.InfoContext(ctx, "Portal Request", slog.String("ip", ctx.ClientIP()), + slog.String("method", ctx.Request.Method), + slog.Int("latency(ms)", int(latency)), + slog.Int("status", ctx.Writer.Status()), + slog.String("current_user", username), + slog.String("url", ctx.Request.RequestURI), + ) + } } } diff --git a/internal/routes/router.go b/internal/routes/router.go index 2a1666b62..42fa740d1 100644 --- a/internal/routes/router.go +++ b/internal/routes/router.go @@ -36,7 +36,7 @@ func Initialize(svcCtx *svc.ServiceContext) (*gin.Engine, error) { csghubServer, err := server.NewServer(svcCtx.Config) if err != nil { - log.Fatalf("failed to create server: %w", err) + log.Fatalf("failed to create server: %v", err) } logFilePath := "./log/app.log" @@ -67,6 +67,7 @@ func Initialize(svcCtx *svc.ServiceContext) (*gin.Engine, error) { c.AbortWithStatus(http.StatusInternalServerError) })) g.Use(middleware.AuthMiddleware(csghubServer)) + // This will track all request to portal go server g.Use(middleware.Log()) frontendHandlers, err := frontendHandlers.NewHandlersRegistry(svcCtx) diff --git a/internal/routes/session.go b/internal/routes/session.go index 08f1c6eca..cc5d9d4b3 100644 --- a/internal/routes/session.go +++ b/internal/routes/session.go @@ -7,5 +7,6 @@ import ( func registerSessionsRoutes(engine *gin.Engine, handlersRegistry *HandlersRegistry) { engine.GET("/signup", handlersRegistry.RenderHandler.SessionHandler.SignUp) engine.GET("/login", handlersRegistry.RenderHandler.SessionHandler.Login) + engine.GET("/logout", handlersRegistry.RenderHandler.SessionHandler.Logout) engine.GET("/server/callback", handlersRegistry.RenderHandler.SessionHandler.Create) } diff --git a/pkg/server/backend/csghubserver/csghubserver.go b/pkg/server/backend/csghubserver/csghubserver.go index 96d9bff1d..586cacac9 100644 --- a/pkg/server/backend/csghubserver/csghubserver.go +++ b/pkg/server/backend/csghubserver/csghubserver.go @@ -1,6 +1,7 @@ package csghubserver import ( + "bytes" "context" "crypto/tls" "encoding/json" @@ -52,7 +53,7 @@ func NewCsgHubServer(ctx context.Context, baseURL, apiKey string) (*CsgHubServer }, nil } -func (c *CsgHubServer) getParsedResponse(method, path string, header http.Header, body io.Reader, obj interface{}) (*http.Response, error) { +func (c *CsgHubServer) getParsedResponse(method, path string, header http.Header, body []byte, obj interface{}) (*http.Response, error) { data, resp, err := c.getResponse(method, path, header, body) if err != nil { return resp, err @@ -60,14 +61,9 @@ func (c *CsgHubServer) getParsedResponse(method, path string, header http.Header return resp, json.Unmarshal(data, obj) } -func (c *CsgHubServer) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, *http.Response, error) { - bodyString := "" - if body != nil { - bodyData, _ := io.ReadAll(body) - bodyString = string(bodyData) - } - slog.Info("CsghubServer API Key Request", method, path, headersToString(header), bodyString) - resp, err := c.doRequest(method, path, header, body) +func (c *CsgHubServer) getResponse(method, path string, header http.Header, body []byte) ([]byte, *http.Response, error) { + slog.Info("Server Request", method, path, headersToString(header), string(body)) + resp, err := c.doRequest(method, path, header, bytes.NewReader(body)) if err != nil { return nil, resp, err } diff --git a/pkg/server/backend/csghubserver/jwt.go b/pkg/server/backend/csghubserver/jwt.go index bc0881438..d43afb4c0 100644 --- a/pkg/server/backend/csghubserver/jwt.go +++ b/pkg/server/backend/csghubserver/jwt.go @@ -1,7 +1,6 @@ package csghubserver import ( - "bytes" "encoding/json" "fmt" "net/http" @@ -33,7 +32,7 @@ func (c *CsgHubServer) CreateJWTToken(req types.CreateJWTReq) (*types.CreateJWTR "POST", fmt.Sprintf("/jwt/token?current_user_uuid=%s", req.UUID), nil, - bytes.NewReader(body), + body, checkResp, ) return checkResp, resp, err diff --git a/pkg/server/backend/csghubserver/sensitive_check.go b/pkg/server/backend/csghubserver/sensitive_check.go index c151c6891..8934afc0b 100644 --- a/pkg/server/backend/csghubserver/sensitive_check.go +++ b/pkg/server/backend/csghubserver/sensitive_check.go @@ -1,7 +1,6 @@ package csghubserver import ( - "bytes" "encoding/json" "net/http" @@ -19,7 +18,7 @@ func (c *CsgHubServer) ImageSecureCheck(req types.ImageSensitiveCheckReq) (*type "POST", "/sensitive/image", nil, - bytes.NewReader(body), + body, checkResp, ) return checkResp, resp, err