MCPcopy
hub / github.com/crowdsecurity/crowdsec / ManagementCmd

Function ManagementCmd

pkg/apiserver/papi_cmd.go:188–327  ·  view source on GitHub ↗
(ctx context.Context, message *Message, p *Papi, sync bool)

Source from the content-addressed store, hash-verified

186}
187
188func ManagementCmd(ctx context.Context, message *Message, p *Papi, sync bool) error {
189 if sync {
190 p.Logger.Infof("Ignoring management command from PAPI in sync mode")
191 return nil
192 }
193
194 switch message.Header.OperationCmd {
195 case "blocklist_unsubscribe":
196 data, err := json.Marshal(message.Data)
197 if err != nil {
198 return err
199 }
200
201 unsubscribeMsg := blocklistUnsubscribe{}
202
203 if err := json.Unmarshal(data, &unsubscribeMsg); err != nil {
204 return fmt.Errorf("message for '%s' contains bad data format: %w", message.Header.OperationType, err)
205 }
206
207 if unsubscribeMsg.Name == "" {
208 return fmt.Errorf("message for '%s' contains bad data format: missing blocklist name", message.Header.OperationType)
209 }
210
211 p.Logger.Infof("Received blocklist_unsubscribe command from PAPI, unsubscribing from blocklist %s", unsubscribeMsg.Name)
212
213 filter := make(map[string][]string)
214 filter["origin"] = []string{types.ListOrigin}
215 filter["scenario"] = []string{unsubscribeMsg.Name}
216
217 _, deletedDecisions, err := p.DBClient.ExpireDecisionsWithFilter(ctx, filter)
218 if err != nil {
219 return fmt.Errorf("unable to expire decisions for list %s : %w", unsubscribeMsg.Name, err)
220 }
221
222 p.Logger.Infof("deleted %d decisions for list %s", len(deletedDecisions), unsubscribeMsg.Name)
223 case "reauth":
224 p.Logger.Infof("Received reauth command from PAPI, resetting token")
225 p.apiClient.GetClient().Transport.(*apiclient.JWTTransport).ResetToken()
226 case "force_pull":
227 data, err := json.Marshal(message.Data)
228 if err != nil {
229 return err
230 }
231
232 forcePullMsg := forcePull{}
233
234 if err := json.Unmarshal(data, &forcePullMsg); err != nil {
235 return fmt.Errorf("message for '%s' contains bad data format: %w", message.Header.OperationType, err)
236 }
237
238 if forcePullMsg.Blocklist == nil && forcePullMsg.Allowlist == nil {
239 p.Logger.Infof("Received force_pull command from PAPI, pulling community, 3rd-party blocklists and allowlists")
240
241 err = p.apic.PullTop(ctx, true)
242 if err != nil {
243 return fmt.Errorf("failed to force pull operation: %w", err)
244 }
245 } else if forcePullMsg.Blocklist != nil {

Callers

nothing calls this directly

Calls 10

IsNotFoundFunction · 0.92
ResetTokenMethod · 0.80
GetClientMethod · 0.80
PullTopMethod · 0.80
PullBlocklistMethod · 0.80
PullAllowlistMethod · 0.80
DeleteAllowListByIDMethod · 0.80
ValidateMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…