Validate filter payload
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
parent
9f8659abd6
commit
246f826b76
3 changed files with 39 additions and 5 deletions
|
|
@ -3,6 +3,7 @@ package events
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync"
|
||||
|
|
@ -163,8 +164,14 @@ func (e *EventHandler) HandleClientMessages(message []byte) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
if len(opt.Filters) == 0 && !opt.SendEverything {
|
||||
slog.DebugContext(e.ctx, "no filters provided; ignoring")
|
||||
if err := opt.Validate(); err != nil {
|
||||
if errors.Is(err, common.ErrNoFiltersProvided) {
|
||||
slog.DebugContext(e.ctx, "no filters provided; ignoring")
|
||||
return nil
|
||||
}
|
||||
slog.ErrorContext(e.ctx, "invalid filter", "error", err)
|
||||
e.client.Write([]byte("invalid filter"))
|
||||
e.Stop()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,9 +11,20 @@ type Filter struct {
|
|||
|
||||
func (f Filter) Validate() error {
|
||||
switch f.EntityType {
|
||||
case common.RepositoryEntityType, common.OrganizationEntityType, common.EnterpriseEntityType, common.PoolEntityType, common.UserEntityType, common.InstanceEntityType, common.JobEntityType, common.ControllerEntityType, common.GithubCredentialsEntityType, common.GithubEndpointEntityType:
|
||||
case common.RepositoryEntityType, common.OrganizationEntityType, common.EnterpriseEntityType,
|
||||
common.PoolEntityType, common.UserEntityType, common.InstanceEntityType,
|
||||
common.JobEntityType, common.ControllerEntityType, common.GithubCredentialsEntityType,
|
||||
common.GithubEndpointEntityType:
|
||||
default:
|
||||
return nil
|
||||
return common.ErrInvalidEntityType
|
||||
}
|
||||
|
||||
for _, op := range f.Operations {
|
||||
switch op {
|
||||
case common.CreateOperation, common.UpdateOperation, common.DeleteOperation:
|
||||
default:
|
||||
return common.ErrInvalidOperationType
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -22,3 +33,18 @@ type Options struct {
|
|||
SendEverything bool `json:"send_everything"`
|
||||
Filters []Filter `json:"filters"`
|
||||
}
|
||||
|
||||
func (o Options) Validate() error {
|
||||
if o.SendEverything {
|
||||
return nil
|
||||
}
|
||||
if len(o.Filters) == 0 {
|
||||
return common.ErrNoFiltersProvided
|
||||
}
|
||||
for _, f := range o.Filters {
|
||||
if err := f.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue