Skip to content

Commit

Permalink
implementing blocklist to block uploads and new runs from orgids
Browse files Browse the repository at this point in the history
  • Loading branch information
tahmidefaz committed Jul 26, 2024
1 parent db3164f commit 89c67fd
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ services:
CLOWDER_ENABLED: "false"
DB_HOST: "db"
PSK_AUTH_TEST: "xwKhCUzgJ8"
BLOCKLIST_ORGIDS: "1337,7331"
restart: unless-stopped

zookeeper:
Expand Down
4 changes: 4 additions & 0 deletions internal/api/controllers/private/runsCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (this *controllers) ApiInternalRunsCreate(ctx echo.Context) error {
return handleRunCreateError(err)
}

if utils.IsOrgIdBlocklisted(cfg, orgIdString) {
return handleRunCreateError(&utils.BlocklistedOrgIdError{OrgID: orgIdString})
}

hosts := parseRunHosts(runInputV1.Hosts)

context = utils.WithOrgId(context, orgIdString)
Expand Down
4 changes: 4 additions & 0 deletions internal/api/controllers/private/runsCreateActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ func handleRunCreateError(err error) *RunCreated {
return runCreateError(http.StatusNotFound)
}

if _, ok := err.(*utils.BlocklistedOrgIdError); ok {
return runCreateError(http.StatusBadRequest)
}

return runCreateError(http.StatusInternalServerError)
}

Expand Down
9 changes: 9 additions & 0 deletions internal/api/controllers/private/runsCreateV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import (
"net/http"
"playbook-dispatcher/internal/api/instrumentation"
"playbook-dispatcher/internal/api/middleware"
"playbook-dispatcher/internal/common/config"
"playbook-dispatcher/internal/common/utils"

"github.com/google/uuid"
"github.com/labstack/echo/v4"
)

var (
cfg = config.Get()
)

//go:generate fungen -types RunInputV2,*RunCreated -methods PMap -package private -filename utils.v2.gen.go
func (this *controllers) ApiInternalV2RunsCreate(ctx echo.Context) error {
var input RunInputV2List
Expand All @@ -33,6 +38,10 @@ func (this *controllers) ApiInternalV2RunsCreate(ctx echo.Context) error {
context := utils.WithOrgId(ctx.Request().Context(), string(runInputV2.OrgId))
context = utils.WithRequestType(context, getRequestTypeLabel(runInputV2))

if utils.IsOrgIdBlocklisted(cfg, string(runInputV2.OrgId)) {
return handleRunCreateError(&utils.BlocklistedOrgIdError{OrgID: string(runInputV2.OrgId)})
}

recipient := parseValidatedUUID(string(runInputV2.Recipient))

hosts := parseRunHosts(runInputV2.Hosts)
Expand Down
2 changes: 2 additions & 0 deletions internal/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func Get() *viper.Viper {

options.SetDefault("db.sslmode", "disable")

options.SetDefault("blocklist.orgids", "")

if clowder.IsClowderEnabled() {

cfg := clowder.LoadedConfig
Expand Down
8 changes: 8 additions & 0 deletions internal/common/utils/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import (
"net/http"
)

type BlocklistedOrgIdError struct {
OrgID string
}

func UnexpectedResponse(res *http.Response) error {
return fmt.Errorf(`unexpected status code "%d" or content type "%s"`, res.StatusCode, res.Header.Get("content-type"))
}

func (this *BlocklistedOrgIdError) Error() string {
return fmt.Sprintf("This org_id (%s) is blocklisted.", this.OrgID)
}
14 changes: 14 additions & 0 deletions internal/common/utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,17 @@ func LoadSchemas(cfg *viper.Viper, schemaNames []string) (schemas []*jsonschema.
}
return
}

func IsOrgIdBlocklisted(cfg *viper.Viper, orgId string) bool {
blocklistedOrgIds := strings.Split(cfg.GetString("blocklist.orgids"), ",")

if len(blocklistedOrgIds) > 0 {
for _, blockedOrgId := range blocklistedOrgIds {
if blockedOrgId == orgId {
return true
}
}
}

return false
}
4 changes: 4 additions & 0 deletions internal/validator/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ func (this *handler) validateRequest(request *messageModel.IngressValidationRequ
return fmt.Errorf("Rejecting payload due to file size: %d", request.Size)
}

if utils.IsOrgIdBlocklisted(cfg, request.OrgID) {
return fmt.Errorf("Rejecting payload because the org_id is blocklisted: %s", request.OrgID)
}

return
}

Expand Down

0 comments on commit 89c67fd

Please sign in to comment.