seunggabi opened a new pull request, #44499:
URL: https://github.com/apache/superset/pull/44499
### SUMMARY
Adds an **opt-in** feature flag, `REQUIRE_DOWNLOAD_REASON` (default
`False`), that asks users for a reason before they download data and records
that reason in the event log, so data exports can be audited.
Motivation: in regulated environments, every export of raw data out of the
BI tool needs a recorded justification ("who downloaded what, and why").
Superset already logs *who* and *what* for every export via `EVENT_LOGGER`;
this change adds the *why* without introducing any new table or migration.
**Backend**
- New `superset/utils/download_reason.py`: reads the `download_reason`
request parameter (query string or form body), enforces the flag, and attaches
the reason to the current event log entry through `add_extra_log_payload` (it
lands in `logs.json`, next to the existing `is_cached`-style payload).
- `ChartDataRestApi.data` / `get_data` (only for `csv` / `xlsx` result
formats) and `SqlLabRestApi.export_csv` / `export_streaming_csv` return `400`
when the flag is on and the request carries no reason. When a reason is present
it is logged regardless of the flag, so deployments can roll the frontend out
first.
- Admins review downloads in **Security → Action Log** (the `json` column
shows `{"download_reason": "..."}`). No new model, view, or DB migration.
**Frontend**
- `src/utils/downloadReason.tsx`: `requestDownloadReason()` opens a small
modal (`Modal.confirm` + `Input.TextArea`) when the flag is on and resolves
with the trimmed reason, with `''` when the flag is off (nothing is shown), or
with `null` when the user cancels. `withDownloadReason(url, reason)` appends
the parameter.
- Wired into the single chart export entry point `exportChart` (Explore,
dashboard chart menu, drill-by, streaming export) and into the SQL Lab CSV
download (plain link and streaming).
When the flag is off nothing changes for users; export endpoints accept and
log an optional `download_reason` only if one is sent.
### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
Before: downloads start immediately and the event log records only the
action.
After (flag on): a "Download reason" dialog opens; **Cancel** aborts the
download, an empty reason keeps the dialog open, and after confirming the
download proceeds with `?download_reason=…`. The Action Log entry for
`ChartDataRestApi.data` / `SqlLabRestApi.export_csv` then carries
`"download_reason"` in its JSON payload.
### TESTING INSTRUCTIONS
1. Set `FEATURE_FLAGS = {"REQUIRE_DOWNLOAD_REASON": True}` in
`superset_config.py`.
2. Dashboard → chart menu → Download → Export to .CSV / .XLSX: the dialog
appears; cancel → no request is sent; enter a reason → the file downloads.
3. Explore → Download → CSV, and SQL Lab → Download to CSV (both the normal
link and the streaming export): same behaviour.
4. Security → Action Log → open the latest `ChartDataRestApi.data` /
`SqlLabRestApi.export_csv` row: the JSON contains `"download_reason"`.
5. API without a reason, e.g. `GET /api/v1/chart/<id>/data/?format=csv` →
`400 "A reason is required to download data."`; with `&download_reason=x` → the
CSV.
6. Flag off: no dialog, behaviour unchanged.
Automated: `pytest tests/unit_tests/utils/download_reason_test.py` and `npm
run test -- src/utils/downloadReason.test.tsx`.
### ADDITIONAL INFORMATION
- [ ] Has associated issue:
- [x] Required feature flags: `REQUIRE_DOWNLOAD_REASON`
- [x] Changes UI
- [ ] Includes DB Migration (follow approval process in
[SIP-59](https://github.com/apache/superset/issues/13351))
- [ ] Migration is atomic, supports rollback & is backwards-compatible
- [ ] Confirm DB migration upgrade and downgrade tested
- [ ] Runtime estimates and downtime expectations provided
- [x] Introduces new feature or API
- [ ] Removes existing feature or API
If maintainers consider this a major change I am happy to write it up as a
SIP first.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]