Add scanner query api#25137
Conversation
|
it's difficult to judge without knowing the problem it's trying to solve (which would be in the issue...) |
It's in the Context. I'd like to programmatically/automatically create scanner query rules, assess their results (i.e. matches) and use them to refine the rules to increase accuracy and reduce false positives, so that at the end, I can add that as a scanner rule. |
ff5372c to
4caa405
Compare
|
This needs docs, and conflicts should be fixed. |
|
@diox thanks. What kind of docs are you referring to? |
4caa405 to
2c304f7
Compare
|
API docs (https://github.com/mozilla/addons-server/tree/master/docs/topics/api) - even internal APIs deserve documentation. |
|
Merge conflicts resolved, docs added. |
| 'scanner', | ||
| 'scanner_display', | ||
| 'definition', | ||
| 'configuration', |
There was a problem hiding this comment.
You have no tests for configuration, is it working ? Specifically:
- Default configuration should be set after creation
- Modifying configuration should work
- Invalid configuration (not obeying schema) should raise an error
We should also have docs about what can be done through the configuration.
|
|
Just in case, can you add tests covering patch/put/post/delete without permissions as well (including the abort/run actions) ? |
Hah, I had considered that! Will add them. |
|
Test updated. |
Fixes #16332.
Description
This patch adds a REST API for
ScannerQueryRuleandScannerQueryResult, so scanner query rules can be created, run, aborted, and iterated on programmatically instead of only through the Django admin.New endpoints (JWT API-key auth; reads gated by
ADMIN_SCANNERS_QUERY_VIEW, writes/run/abort byADMIN_SCANNERS_QUERY_EDIT):GET/POST /api/v5/scanner/query-rules/: list / create a query rule.GET/PATCH/PUT/DELETE /api/v5/scanner/query-rules/{id}/: read / edit / delete.new.nameandscannerare frozen after creation.yararules thenameis inferred from the definition (matching the admin) and required fornarc.POST /api/v5/scanner/query-rules/{id}/run/: schedule a run (enqueuesrun_scanner_query_rule).POST /api/v5/scanner/query-rules/{id}/abort/: abort a run.GET /api/v5/scanner/query-rules/{id}/results/: the rule's matches (nested read-only viewset).was_blocked,was_promoted,was_signed,addon_disabled_by_user,channel,addon_status,file_status),?sort=one ofid,addon_adu,version_created, prefix-for descending).version(with itsaddonguid/average_daily_users).Context
Scanner query rules (ad-hoc YARA/NARC rules run across the whole add-on corpus) can currently only be authored and run via django admin. This exposes the same lifecycle over HTTP so that an iteration loop can be automated.
Testing
Admin:ScannersQueryViewandAdmin:ScannersQueryEditgroups and get a JWT API key for them.POST /api/v5/scanner/query-rules/with{"scanner": "yara", "definition": "rule my_rule { condition: true }"}--> expect 201. The response hasscanner: "yara",state: "new", andnameinferred as"my_rule"from the definition (fornarc,nameis required).a) Posting an uncompilable definition --> expect 400 with a
definitionerror.POST /api/v5/scanner/query-rules/{id}/run/--> expect 202. The rulestatebecomes"scheduled", and arun_scanner_query_ruletask is enqueued.a) Running a non-
newrule --> expect 409.GET /api/v5/scanner/query-rules/{id}/and watchstate/completion_rate/completed.a) Once complete,
GET .../{id}/results/returns the matches; try filtering/sorting, e.g.?was_blocked=true,?channel=listed,?sort=-addon_adu.PATCHanewrule --> expect 200.a)
PATCHa non-newrule --> expect 400.b) Requests without a valid JWT key --> expect 401.
c) Requests with a key lacking permissions --> expect 403.
Checklist
#ISSUENUMat the top of your PR to an existing open issue in the mozilla/addons repository.