pierrejeambrun commented on code in PR #76457:
URL: https://github.com/apache/airflow/pull/76457#discussion_r4741978890
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/dag_warning.py:
##########
@@ -73,9 +90,43 @@ def list_dag_warnings(
limit=limit,
session=session,
)
- dag_warnings = session.scalars(dag_warnings_select)
+ dag_warnings = session.scalars(dag_warnings_select).all()
+
+ # Being able to read the Dag a file-level warning is attached to does not
mean being able to
+ # read every Dag of that file, so its message is redacted like the source
of that file.
+ file_level_warnings = [
+ dag_warning
+ for dag_warning in dag_warnings
+ if dag_warning.warning_type not in PER_DAG_WARNING_TYPES and
dag_warning.dag_model.relative_fileloc
+ ]
+ redacted_warnings: set[DagWarning] = set()
+ if file_level_warnings:
+ files_with_unreadable_dags = set(
+ session.execute(
+ select(DagModel.bundle_name, DagModel.relative_fileloc)
+ .where(
+ tuple_(DagModel.bundle_name,
DagModel.relative_fileloc).in_(
+ {(w.dag_model.bundle_name,
w.dag_model.relative_fileloc) for w in file_level_warnings}
+ ),
+ DagModel.dag_id.not_in(readable_dag_warning_filter.value
or set()),
+ )
+ .distinct()
+ ).tuples()
+ )
Review Comment:
Good catch on both. Stale Dags are now left out, and the check fetches the
Dag ids of the file and compares them in Python, like `dag_sources`. Added a
test where only a stale Dag of the file is unreadable.
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/dag_warning.py:
##########
@@ -73,9 +90,43 @@ def list_dag_warnings(
limit=limit,
session=session,
)
- dag_warnings = session.scalars(dag_warnings_select)
+ dag_warnings = session.scalars(dag_warnings_select).all()
+
+ # Being able to read the Dag a file-level warning is attached to does not
mean being able to
+ # read every Dag of that file, so its message is redacted like the source
of that file.
+ file_level_warnings = [
+ dag_warning
+ for dag_warning in dag_warnings
+ if dag_warning.warning_type not in PER_DAG_WARNING_TYPES and
dag_warning.dag_model.relative_fileloc
+ ]
Review Comment:
I'd keep them separate. The three checks differ on purpose: Dag sources
still count stale Dags, the import error list goes through a CTE and the auth
manager's batch check, and warnings now drop stale Dags. A shared helper would
need a flag for each of those, so I don't think it would make a fix land in one
place.
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/dag_warning.py:
##########
@@ -73,9 +90,43 @@ def list_dag_warnings(
limit=limit,
session=session,
)
- dag_warnings = session.scalars(dag_warnings_select)
+ dag_warnings = session.scalars(dag_warnings_select).all()
+
+ # Being able to read the Dag a file-level warning is attached to does not
mean being able to
+ # read every Dag of that file, so its message is redacted like the source
of that file.
+ file_level_warnings = [
+ dag_warning
+ for dag_warning in dag_warnings
+ if dag_warning.warning_type not in PER_DAG_WARNING_TYPES and
dag_warning.dag_model.relative_fileloc
+ ]
+ redacted_warnings: set[DagWarning] = set()
+ if file_level_warnings:
+ files_with_unreadable_dags = set(
+ session.execute(
+ select(DagModel.bundle_name, DagModel.relative_fileloc)
+ .where(
+ tuple_(DagModel.bundle_name,
DagModel.relative_fileloc).in_(
+ {(w.dag_model.bundle_name,
w.dag_model.relative_fileloc) for w in file_level_warnings}
+ ),
+ DagModel.dag_id.not_in(readable_dag_warning_filter.value
or set()),
+ )
+ .distinct()
+ ).tuples()
+ )
+ redacted_warnings = {
+ w
+ for w in file_level_warnings
+ if (w.dag_model.bundle_name, w.dag_model.relative_fileloc) in
files_with_unreadable_dags
+ }
+
+ response_warnings = []
+ for dag_warning in dag_warnings:
+ response_warning = DAGWarningResponse.model_validate(dag_warning)
+ if dag_warning in redacted_warnings:
+ response_warning.message = REDACTED_FILE_CONTENT
Review Comment:
Sorting only tells you where a hidden row falls among the readable ones, not
what it says. The Dag source and import error endpoints accept the same
exposure, so I'd leave sorting as is.
##########
airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_dag_warning.py:
##########
@@ -138,3 +143,78 @@ def test_get_dag_warnings_importer_defined_type(self,
test_client, session):
assert response_json["total_entries"] == 1
assert response_json["dag_warnings"][0]["warning_type"] ==
"yaml:schema_violation"
assert response_json["dag_warnings"][0]["message"] == "importer
message"
+
+
+class TestGetDagWarningsOfMultiDagFile:
+ FILE_LEVEL_MESSAGE = "Line 12, Col 4\nCode:
BashOperator(task_id='colocated_task')"
+
+ @pytest.fixture(autouse=True)
+ def colocate_dags(self, session):
+ """Put DAG1 and DAG2 in one file and DAG3 in a file of its own."""
+ session.get(DagModel, DAG1_ID).relative_fileloc = "multi_dag_file.py"
+ session.get(DagModel, DAG2_ID).relative_fileloc = "multi_dag_file.py"
+ session.get(DagModel, DAG3_ID).relative_fileloc = "single_dag_file.py"
+ session.commit()
+
+ @pytest.mark.parametrize("warning_type", ["runtime varying value",
"yaml:schema_violation"])
+ @pytest.mark.parametrize(
+ ("readable_dag_ids", "expected_messages"),
+ [
+ pytest.param({DAG1_ID}, {DAG1_ID: REDACTED_FILE_CONTENT},
id="colocated-dag-unreadable"),
+ pytest.param(
+ {DAG1_ID, DAG3_ID},
+ {DAG1_ID: REDACTED_FILE_CONTENT, DAG3_ID: FILE_LEVEL_MESSAGE},
+ id="only-file-with-unreadable-dag-redacted",
+ ),
+ pytest.param(
+ {DAG1_ID, DAG2_ID},
+ {DAG1_ID: FILE_LEVEL_MESSAGE, DAG2_ID: FILE_LEVEL_MESSAGE},
+ id="all-dags-in-file-readable",
+ ),
+ ],
+ )
+
@mock.patch("airflow.api_fastapi.auth.managers.base_auth_manager.BaseAuthManager.get_authorized_dag_ids")
Review Comment:
Done, thanks.
##########
airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_dag_warning.py:
##########
@@ -138,3 +143,78 @@ def test_get_dag_warnings_importer_defined_type(self,
test_client, session):
assert response_json["total_entries"] == 1
assert response_json["dag_warnings"][0]["warning_type"] ==
"yaml:schema_violation"
assert response_json["dag_warnings"][0]["message"] == "importer
message"
+
+
+class TestGetDagWarningsOfMultiDagFile:
+ FILE_LEVEL_MESSAGE = "Line 12, Col 4\nCode:
BashOperator(task_id='colocated_task')"
+
+ @pytest.fixture(autouse=True)
+ def colocate_dags(self, session):
+ """Put DAG1 and DAG2 in one file and DAG3 in a file of its own."""
+ session.get(DagModel, DAG1_ID).relative_fileloc = "multi_dag_file.py"
+ session.get(DagModel, DAG2_ID).relative_fileloc = "multi_dag_file.py"
+ session.get(DagModel, DAG3_ID).relative_fileloc = "single_dag_file.py"
+ session.commit()
+
+ @pytest.mark.parametrize("warning_type", ["runtime varying value",
"yaml:schema_violation"])
+ @pytest.mark.parametrize(
+ ("readable_dag_ids", "expected_messages"),
+ [
+ pytest.param({DAG1_ID}, {DAG1_ID: REDACTED_FILE_CONTENT},
id="colocated-dag-unreadable"),
+ pytest.param(
+ {DAG1_ID, DAG3_ID},
+ {DAG1_ID: REDACTED_FILE_CONTENT, DAG3_ID: FILE_LEVEL_MESSAGE},
+ id="only-file-with-unreadable-dag-redacted",
+ ),
+ pytest.param(
+ {DAG1_ID, DAG2_ID},
+ {DAG1_ID: FILE_LEVEL_MESSAGE, DAG2_ID: FILE_LEVEL_MESSAGE},
+ id="all-dags-in-file-readable",
+ ),
+ ],
+ )
+
@mock.patch("airflow.api_fastapi.auth.managers.base_auth_manager.BaseAuthManager.get_authorized_dag_ids")
+ def
test_file_level_message_is_redacted_unless_caller_can_read_all_dags_in_file(
+ self,
+ mock_get_authorized_dag_ids,
+ test_client,
+ session,
+ readable_dag_ids,
+ expected_messages,
+ warning_type,
+ ):
+ mock_get_authorized_dag_ids.return_value = readable_dag_ids
+ for dag_id in (DAG1_ID, DAG2_ID, DAG3_ID):
+ session.add(DagWarning(dag_id, warning_type,
self.FILE_LEVEL_MESSAGE))
+ session.commit()
+
+ with assert_queries_count(4):
+ response = test_client.get("/dagWarnings", params={"warning_type":
warning_type})
+
+ assert response.status_code == 200
+ response_json = response.json()
+ assert response_json["total_entries"] == len(expected_messages)
+ assert {w["dag_id"]: w["message"] for w in
response_json["dag_warnings"]} == expected_messages
+ session.expire_all()
+ assert set(
+
session.scalars(select(DagWarning.message).where(DagWarning.warning_type ==
warning_type))
+ ) == {self.FILE_LEVEL_MESSAGE}
+
+ @pytest.mark.parametrize("warning_type", sorted(PER_DAG_WARNING_TYPES))
+
@mock.patch("airflow.api_fastapi.auth.managers.base_auth_manager.BaseAuthManager.get_authorized_dag_ids")
Review Comment:
Done, thanks.
--
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]