codeant-ai-for-open-source[bot] commented on code in PR #41133: URL: https://github.com/apache/superset/pull/41133#discussion_r3509709968
########## superset/utils/s3.py: ########## @@ -0,0 +1,76 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +""" +Minimal S3 helpers for uploading export artifacts and minting pre-signed URLs. + +Credentials and region come from the standard boto3 resolution chain (env vars, +shared config, instance role). Operators can override client construction via +the ``EXCEL_EXPORT_S3_CLIENT_KWARGS`` config (e.g. ``region_name`` or an +``endpoint_url`` for S3-compatible stores such as MinIO/LocalStack). +""" + +from __future__ import annotations + +import logging +from typing import Any + +from flask import current_app + +logger = logging.getLogger(__name__) + + +def _get_s3_client() -> Any: + """Build an S3 client using operator-provided client kwargs (if any).""" + # boto3 is imported lazily so that importing this module (which happens at + # app startup via the dashboard API) does not require boto3 to be installed. + # The dependency is only needed when an export actually runs. + import boto3 # pylint: disable=import-outside-toplevel + + client_kwargs: dict[str, Any] = current_app.config.get( + "EXCEL_EXPORT_S3_CLIENT_KWARGS", {} + ) + return boto3.client("s3", **client_kwargs) Review Comment: **Suggestion:** `EXCEL_EXPORT_S3_CLIENT_KWARGS` is assumed to always be a mapping, but if it is configured as `None` or a non-dict value the `**client_kwargs` expansion will raise a runtime `TypeError` and break exports. Validate/coerce this config to a dict before unpacking so misconfiguration fails predictably or falls back safely. [type error] <details> <summary><b>Severity Level:</b> Critical 🚨</summary> ```mdx - ❌ Dashboard Excel exports crash with misconfigured S3 kwargs. - ⚠️ Users receive no email download link. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Configure Superset for Excel exports so Celery runs `export_dashboard_excel`; the Celery task is defined in `superset/tasks/export_dashboard_excel.py:161-243` and wired into Celery via `CeleryConfig.imports` in `superset/config.py:59-68`. 2. In deployment configuration (e.g. `superset_config.py`), set `EXCEL_EXPORT_S3_CLIENT_KWARGS` to a non-mapping value such as `None` or a string; at runtime this ends up in `current_app.config["EXCEL_EXPORT_S3_CLIENT_KWARGS"]` (the default in `superset/config.py:1387` is `{}`, but operators may override it). 3. Trigger a dashboard Excel export via the REST API `POST /api/v1/dashboard/<id>/export_xlsx/`, as exercised in `superset/tests/integration_tests/dashboards/api_tests.py:3283-3368`; this endpoint enqueues the `export_dashboard_excel` Celery task when `EXCEL_EXPORT_S3_BUCKET` is set. 4. When the task runs, it calls `s3.upload_file_to_s3(tmp_path, bucket, key)` and `s3.generate_presigned_url(bucket, key, ttl)` in `superset/tasks/export_dashboard_excel.py:214-215`; both call `_get_s3_client()` in `superset/utils/s3.py:36-46`, which does `client_kwargs: dict[str, Any] = current_app.config.get("EXCEL_EXPORT_S3_CLIENT_KWARGS", {})` and then `boto3.client("s3", **client_kwargs)`. If `client_kwargs` is `None` or any non-mapping, Python raises a `TypeError` (`'NoneType' object is not a mapping'`), causing the S3 client creation and thus the entire export task to fail. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=999dd78506cd4ad6a325c3b754474125&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=999dd78506cd4ad6a325c3b754474125&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset/utils/s3.py **Line:** 43:46 **Comment:** *Type Error: `EXCEL_EXPORT_S3_CLIENT_KWARGS` is assumed to always be a mapping, but if it is configured as `None` or a non-dict value the `**client_kwargs` expansion will raise a runtime `TypeError` and break exports. Validate/coerce this config to a dict before unpacking so misconfiguration fails predictably or falls back safely. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=b77fbfcbaacdb6e29a4c08d4b506de017de54bda9422160e82afa749b1f1b188&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=b77fbfcbaacdb6e29a4c08d4b506de017de54bda9422160e82afa749b1f1b188&reaction=dislike'>👎</a> -- 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]
