Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pkg/apiserver/papi.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const (
PapiPullKey = "papi:last_pull"
)

var operationMap = map[string]func(*Message, *Papi, bool) error{
var operationMap = map[string]func(context.Context, *Message, *Papi, bool) error{
"decision": DecisionCmd,
"alert": AlertCmd,
"management": ManagementCmd,
Expand Down Expand Up @@ -128,7 +128,7 @@ func NewPAPI(apic *apic, dbClient *database.Client, consoleConfig *csconfig.Cons
return papi, nil
}

func (p *Papi) handleEvent(event longpollclient.Event, sync bool) error {
func (p *Papi) handleEvent(ctx context.Context, event longpollclient.Event, sync bool) error {
logger := p.Logger.WithField("request-id", event.RequestId)
logger.Debugf("message received: %+v", event.Data)

Expand All @@ -152,7 +152,7 @@ func (p *Papi) handleEvent(event longpollclient.Event, sync bool) error {

logger.Debugf("Calling operation '%s'", message.Header.OperationType)

err := operationFunc(message, p, sync)
err := operationFunc(ctx, message, p, sync)
if err != nil {
return fmt.Errorf("'%s %s failed: %w", message.Header.OperationType, message.Header.OperationCmd, err)
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func (p *Papi) PullOnce(ctx context.Context, since time.Time, sync bool) error {
p.Logger.Infof("received %d events", eventsCount)

for i, event := range reversedEvents {
if err := p.handleEvent(event, sync); err != nil {
if err := p.handleEvent(ctx, event, sync); err != nil {
p.Logger.WithField("request-id", event.RequestId).Errorf("failed to handle event: %s", err)
}

Expand Down Expand Up @@ -310,7 +310,7 @@ func (p *Papi) Pull(ctx context.Context) error {

lastTimestamp = newTime

err = p.handleEvent(event, false)
err = p.handleEvent(ctx, event, false)
if err != nil {
logger.Errorf("failed to handle event: %s", err)
continue
Expand Down
14 changes: 3 additions & 11 deletions pkg/apiserver/papi_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ type allowlistUnsubscribe struct {
Id string `json:"id"`
}

func DecisionCmd(message *Message, p *Papi, sync bool) error {
ctx := context.TODO()

func DecisionCmd(ctx context.Context, message *Message, p *Papi, sync bool) error {
switch message.Header.OperationCmd {
case "delete":
data, err := json.Marshal(message.Data)
Expand Down Expand Up @@ -91,9 +89,7 @@ func DecisionCmd(message *Message, p *Papi, sync bool) error {
return nil
}

func AlertCmd(message *Message, p *Papi, sync bool) error {
ctx := context.TODO()

func AlertCmd(ctx context.Context, message *Message, p *Papi, sync bool) error {
switch message.Header.OperationCmd {
case "add":
data, err := json.Marshal(message.Data)
Expand Down Expand Up @@ -166,9 +162,7 @@ func AlertCmd(message *Message, p *Papi, sync bool) error {
return nil
}

func ManagementCmd(message *Message, p *Papi, sync bool) error {
ctx := context.TODO()

func ManagementCmd(ctx context.Context, message *Message, p *Papi, sync bool) error {
if sync {
p.Logger.Infof("Ignoring management command from PAPI in sync mode")
return nil
Expand Down Expand Up @@ -218,8 +212,6 @@ func ManagementCmd(message *Message, p *Papi, sync bool) error {
return fmt.Errorf("message for '%s' contains bad data format: %w", message.Header.OperationType, err)
}

ctx := context.TODO()

if forcePullMsg.Blocklist == nil && forcePullMsg.Allowlist == nil {
p.Logger.Infof("Received force_pull command from PAPI, pulling community, 3rd-party blocklists and allowlists")

Expand Down