codeant-ai-for-open-source[bot] commented on code in PR #41133: URL: https://github.com/apache/superset/pull/41133#discussion_r3436901956
########## superset/utils/s3.py: ########## @@ -0,0 +1,72 @@ +# 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 + +import boto3 +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).""" + 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 unpacked without type validation, so a common misconfiguration like `None` causes a runtime `TypeError` (`**` requires a mapping) before any upload/link generation can happen. Coerce falsy values to `{}` and validate the config is a mapping before calling `boto3.client`. [type error] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ❌ Misconfigured S3 client kwargs break all dashboard Excel exports. - ⚠️ Operations must debug runtime TypeError instead of validation error. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Configure Excel export with a valid `EXCEL_EXPORT_S3_BUCKET` but set `EXCEL_EXPORT_S3_CLIENT_KWARGS = None` (or another non-mapping value) in the Flask config used by `current_app.config` (e.g. local override of `superset/config.py`). 2. A user calls `POST /api/v1/dashboard/<pk>/export_xlsx/`, hitting `DashboardRestApi.export_xlsx()` in `superset/dashboards/api.py:1385-1413`, which validates the request and enqueues the `export_dashboard_excel` Celery task at `superset/dashboards/api.py:1462-1473`. 3. Inside the worker, `export_dashboard_excel()` (`superset/tasks/export_dashboard_excel.py:169-244`) successfully builds the workbook and then calls `s3.upload_file_to_s3(tmp_path, bucket, key)` at line 215 and `s3.generate_presigned_url(bucket, key, ttl)` at line 216. 4. `upload_file_to_s3()` invokes `_get_s3_client()` (`superset/utils/s3.py:37-42`), which executes `client_kwargs = current_app.config.get("EXCEL_EXPORT_S3_CLIENT_KWARGS", {})`; because the config key exists with value `None`, `client_kwargs` is `None`, and `boto3.client("s3", **client_kwargs)` raises `TypeError: type object argument after ** must be a mapping, not NoneType`, causing the export task to fail and send a failure email instead of uploading the file. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=6a097c7e490e4b47baebfb1827d24da5&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=6a097c7e490e4b47baebfb1827d24da5&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:** 39:42 **Comment:** *Type Error: `EXCEL_EXPORT_S3_CLIENT_KWARGS` is unpacked without type validation, so a common misconfiguration like `None` causes a runtime `TypeError` (`**` requires a mapping) before any upload/link generation can happen. Coerce falsy values to `{}` and validate the config is a mapping before calling `boto3.client`. 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=8d0acdb78aa5f1782f11dc969c069f540d4601f8d53e0ff5b2a786dc55e105a5&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=8d0acdb78aa5f1782f11dc969c069f540d4601f8d53e0ff5b2a786dc55e105a5&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]
