Skip to content

Commit

Permalink
refactor sensitive check into a new moderation service; introduct Tem…
Browse files Browse the repository at this point in the history
…poral workflow to run
  • Loading branch information
Lei Da authored and Rader committed Nov 6, 2024
1 parent a1b5eb1 commit 811fd11
Show file tree
Hide file tree
Showing 74 changed files with 2,644 additions and 609 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ run_server_local.sh
run_user_local.sh
cmd/csghub-server/__debug_*
run_dataviewer_local.sh

.data
/data
*/*/_api_test/
12 changes: 8 additions & 4 deletions api/handler/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ func NewCodeHandler(config *config.Config) (*CodeHandler, error) {
if err != nil {
return nil, err
}
sc, err := component.NewSensitiveComponent(config)
if err != nil {
return nil, fmt.Errorf("error creating sensitive component:%w", err)
}
return &CodeHandler{
c: tc,
sc: component.NewSensitiveComponent(config),
sc: sc,
}, nil
}

type CodeHandler struct {
c *component.CodeComponent
sc component.SensitiveChecker
sc *component.SensitiveComponent
}

// CreateCode godoc
Expand Down Expand Up @@ -57,7 +61,7 @@ func (h *CodeHandler) Create(ctx *gin.Context) {
return
}

_, err := h.sc.CheckRequest(ctx, req)
_, err := h.sc.CheckRequestV2(ctx, req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand Down Expand Up @@ -166,7 +170,7 @@ func (h *CodeHandler) Update(ctx *gin.Context) {
return
}

_, err := h.sc.CheckRequest(ctx, req)
_, err := h.sc.CheckRequestV2(ctx, req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand Down
12 changes: 8 additions & 4 deletions api/handler/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ func NewCollectionHandler(cfg *config.Config) (*CollectionHandler, error) {
if err != nil {
return nil, err
}
sc, err := component.NewSensitiveComponent(cfg)
if err != nil {
return nil, fmt.Errorf("error creating sensitive component:%w", err)
}
return &CollectionHandler{
cc: cc,
sc: component.NewSensitiveComponent(cfg),
sc: sc,
}, nil
}

type CollectionHandler struct {
cc *component.CollectionComponent
sc component.SensitiveChecker
sc *component.SensitiveComponent
}

// GetCollections godoc
Expand Down Expand Up @@ -100,7 +104,7 @@ func (c *CollectionHandler) Create(ctx *gin.Context) {
return
}

_, err := c.sc.CheckRequest(ctx, req)
_, err := c.sc.CheckRequestV2(ctx, req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand Down Expand Up @@ -172,7 +176,7 @@ func (c *CollectionHandler) UpdateCollection(ctx *gin.Context) {
return
}

_, err := c.sc.CheckRequest(ctx, req)
_, err := c.sc.CheckRequestV2(ctx, req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand Down
12 changes: 8 additions & 4 deletions api/handler/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ func NewDatasetHandler(config *config.Config) (*DatasetHandler, error) {
if err != nil {
return nil, err
}
sc, err := component.NewSensitiveComponent(config)
if err != nil {
return nil, fmt.Errorf("error creating sensitive component:%w", err)
}
return &DatasetHandler{
c: tc,
sc: component.NewSensitiveComponent(config),
sc: sc,
}, nil
}

type DatasetHandler struct {
c *component.DatasetComponent
sc component.SensitiveChecker
sc *component.SensitiveComponent
}

// CreateDataset godoc
Expand Down Expand Up @@ -59,7 +63,7 @@ func (h *DatasetHandler) Create(ctx *gin.Context) {
httpbase.BadRequest(ctx, err.Error())
return
}
_, err := h.sc.CheckRequest(ctx, req)
_, err := h.sc.CheckRequestV2(ctx, req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand Down Expand Up @@ -168,7 +172,7 @@ func (h *DatasetHandler) Update(ctx *gin.Context) {
return
}

_, err := h.sc.CheckRequest(ctx, req)
_, err := h.sc.CheckRequestV2(ctx, req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand Down
7 changes: 5 additions & 2 deletions api/handler/discussion.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ import (

type DiscussionHandler struct {
c *component.DiscussionComponent
sc component.SensitiveChecker
sc *component.SensitiveComponent
}

func NewDiscussionHandler(cfg *config.Config) (*DiscussionHandler, error) {
c := component.NewDiscussionComponent()
sc := component.NewSensitiveComponent(cfg)
sc, err := component.NewSensitiveComponent(cfg)
if err != nil {
return nil, fmt.Errorf("failed to create sensitive component: %w", err)
}
return &DiscussionHandler{
c: c,
sc: sc,
Expand Down
13 changes: 8 additions & 5 deletions api/handler/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ func NewModelHandler(config *config.Config) (*ModelHandler, error) {
if err != nil {
return nil, err
}
sc := component.NewSensitiveComponent(config)
sc, err := component.NewSensitiveComponent(config)
if err != nil {
return nil, fmt.Errorf("error creating sensitive component:%w", err)
}
return &ModelHandler{
c: uc,
sc: sc,
Expand All @@ -31,7 +34,7 @@ func NewModelHandler(config *config.Config) (*ModelHandler, error) {

type ModelHandler struct {
c *component.ModelComponent
sc component.SensitiveChecker
sc *component.SensitiveComponent
}

// GetVisiableModels godoc
Expand Down Expand Up @@ -123,7 +126,7 @@ func (h *ModelHandler) Create(ctx *gin.Context) {
}
req.Username = currentUser

_, err := h.sc.CheckRequest(ctx, req)
_, err := h.sc.CheckRequestV2(ctx, req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand Down Expand Up @@ -168,7 +171,7 @@ func (h *ModelHandler) Update(ctx *gin.Context) {
return
}

_, err := h.sc.CheckRequest(ctx, req)
_, err := h.sc.CheckRequestV2(ctx, req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand Down Expand Up @@ -631,7 +634,7 @@ func (h *ModelHandler) DeployDedicated(ctx *gin.Context) {
return
}

_, err = h.sc.CheckRequest(ctx, &req)
_, err = h.sc.CheckRequestV2(ctx, &req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand Down
12 changes: 8 additions & 4 deletions api/handler/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@ import (

type PromptHandler struct {
pc *component.PromptComponent
sc component.SensitiveChecker
sc *component.SensitiveComponent
}

func NewPromptHandler(cfg *config.Config) (*PromptHandler, error) {
promptComp, err := component.NewPromptComponent(cfg)
if err != nil {
return nil, fmt.Errorf("failed to create PromptComponent: %w", err)
}
sc, err := component.NewSensitiveComponent(cfg)
if err != nil {
return nil, fmt.Errorf("failed to create SensitiveComponent: %w", err)
}
return &PromptHandler{
pc: promptComp,
sc: component.NewSensitiveComponent(cfg),
sc: sc,
}, nil
}

Expand Down Expand Up @@ -949,7 +953,7 @@ func (h *PromptHandler) Create(ctx *gin.Context) {
httpbase.BadRequest(ctx, err.Error())
return
}
_, err := h.sc.CheckRequest(ctx, req)
_, err := h.sc.CheckRequestV2(ctx, req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand Down Expand Up @@ -998,7 +1002,7 @@ func (h *PromptHandler) Update(ctx *gin.Context) {
return
}

_, err := h.sc.CheckRequest(ctx, req)
_, err := h.sc.CheckRequestV2(ctx, req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand Down
2 changes: 2 additions & 0 deletions api/handler/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ func (h *RepoHandler) FileInfo(ctx *gin.Context) {
httpbase.OK(ctx, file)
}

// Deprecated: use ResolveDownload instead
//
// DownloadRepoFile godoc
// @Security ApiKey or JWT
// @Summary Download a repo file [Depricated: use 'resolve' api instead]
Expand Down
13 changes: 9 additions & 4 deletions api/handler/sensitive.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"fmt"
"log/slog"

"github.com/gin-gonic/gin"
Expand All @@ -10,13 +11,17 @@ import (
)

type SensitiveHandler struct {
c component.SensitiveChecker
c *component.SensitiveComponent
}

func NewSensitiveHandler(cfg *config.Config) *SensitiveHandler {
return &SensitiveHandler{
c: component.NewSensitiveComponent(cfg),
func NewSensitiveHandler(cfg *config.Config) (*SensitiveHandler, error) {
sc, err := component.NewSensitiveComponent(cfg)
if err != nil {
return nil, fmt.Errorf("error creating sensitive component:%w", err)
}
return &SensitiveHandler{
c: sc,
}, nil
}

func (h *SensitiveHandler) Text(ctx *gin.Context) {
Expand Down
18 changes: 11 additions & 7 deletions api/handler/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@ import (
)

func NewSpaceHandler(config *config.Config) (*SpaceHandler, error) {
sc, err := component.NewSpaceComponent(config)
c, err := component.NewSpaceComponent(config)
if err != nil {
return nil, err
}
ssc, err := component.NewSensitiveComponent(config)
if err != nil {
return nil, fmt.Errorf("error creating sensitive component:%w", err)
}
return &SpaceHandler{
c: sc,
sc: component.NewSensitiveComponent(config),
c: c,
ssc: ssc,
}, nil
}

type SpaceHandler struct {
c *component.SpaceComponent
sc component.SensitiveChecker
c *component.SpaceComponent
ssc *component.SensitiveComponent
}

// GetAllSpaces godoc
Expand Down Expand Up @@ -151,7 +155,7 @@ func (h *SpaceHandler) Create(ctx *gin.Context) {
httpbase.BadRequest(ctx, err.Error())
return
}
_, err := h.sc.CheckRequest(ctx, &req)
_, err := h.ssc.CheckRequestV2(ctx, &req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand Down Expand Up @@ -196,7 +200,7 @@ func (h *SpaceHandler) Update(ctx *gin.Context) {
httpbase.BadRequest(ctx, err.Error())
return
}
_, err := h.sc.CheckRequest(ctx, req)
_, err := h.ssc.CheckRequestV2(ctx, req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand Down
10 changes: 7 additions & 3 deletions api/handler/sshkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ func NewSSHKeyHandler(config *config.Config) (*SSHKeyHandler, error) {
if err != nil {
return nil, err
}
sc, err := component.NewSensitiveComponent(config)
if err != nil {
return nil, fmt.Errorf("error creating sensitive component:%w", err)
}
return &SSHKeyHandler{
c: oc,
sc: component.NewSensitiveComponent(config),
sc: sc,
}, nil
}

type SSHKeyHandler struct {
c *component.SSHKeyComponent
sc component.SensitiveChecker
sc *component.SensitiveComponent
}

// CreateUserSSHKey godoc
Expand All @@ -49,7 +53,7 @@ func (h *SSHKeyHandler) Create(ctx *gin.Context) {
return
}

_, err := h.sc.CheckRequest(ctx, &req)
_, err := h.sc.CheckRequestV2(ctx, &req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand Down
5 changes: 4 additions & 1 deletion api/router/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@ func NewRouter(config *config.Config, enableSwagger bool) (*gin.Engine, error) {
apiGroup.GET("/callback/casdoor", userProxyHandler.Proxy)
// Sensive check
if config.SensitiveCheck.Enable {
sensitiveCtrl := handler.NewSensitiveHandler(config)
sensitiveCtrl, err := handler.NewSensitiveHandler(config)
if err != nil {
return nil, fmt.Errorf("error creating sensitive handler:%w", err)
}
apiGroup.POST("/sensitive/text", sensitiveCtrl.Text)
apiGroup.POST("/sensitive/image", sensitiveCtrl.Image)
}
Expand Down
Loading

0 comments on commit 811fd11

Please sign in to comment.