Skip to content

Commit efb27dc

Browse files
Fix connection test API to restore masked password/extra from existing connections (apache#59643)
* Fix connection test API to restore masked password/extra from existing connections * Use get_connection_from_secrets in test endpoint * Remove unused session parameter * Add use_existing_credentials flag to test connections * fix api-gen description * Standardize docstring format * Add use_existing_credentials flag to test connections * Refactor connection test logic using update_orm_from_pydantic * Remove useExistingCredentials flag from TestConnectionButton mutation * Add proper assertions for test connection merge tests * Small test adjustment --------- Co-authored-by: pierrejeambrun <[email protected]>
1 parent 4bf1bff commit efb27dc

2 files changed

Lines changed: 149 additions & 7 deletions

File tree

‎airflow-core/src/airflow/api_fastapi/core_api/routes/public/connections.py‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
)
5555
from airflow.api_fastapi.logging.decorators import action_logging
5656
from airflow.configuration import conf
57+
from airflow.exceptions import AirflowNotFoundException
5758
from airflow.models import Connection
5859
from airflow.secrets.environment_variables import CONN_ENV_PREFIX
5960
from airflow.utils.db import create_default_connections as db_create_default_connections
@@ -212,9 +213,7 @@ def patch_connection(
212213

213214

214215
@connections_router.post("/test", dependencies=[Depends(requires_access_connection(method="POST"))])
215-
def test_connection(
216-
test_body: ConnectionBody,
217-
) -> ConnectionTestResponse:
216+
def test_connection(test_body: ConnectionBody) -> ConnectionTestResponse:
218217
"""
219218
Test an API connection.
220219
@@ -232,9 +231,17 @@ def test_connection(
232231
transient_conn_id = get_random_string()
233232
conn_env_var = f"{CONN_ENV_PREFIX}{transient_conn_id.upper()}"
234233
try:
235-
data = test_body.model_dump(by_alias=True)
236-
data["conn_id"] = transient_conn_id
237-
conn = Connection(**data)
234+
# Try to get existing connection and merge with provided values
235+
try:
236+
existing_conn = Connection.get_connection_from_secrets(test_body.connection_id)
237+
existing_conn.conn_id = transient_conn_id
238+
update_orm_from_pydantic(existing_conn, test_body)
239+
conn = existing_conn
240+
except AirflowNotFoundException:
241+
data = test_body.model_dump(by_alias=True)
242+
data["conn_id"] = transient_conn_id
243+
conn = Connection(**data)
244+
238245
os.environ[conn_env_var] = conn.get_uri()
239246
test_status, test_message = conn.test_connection()
240247
return ConnectionTestResponse.model_validate({"status": test_status, "message": test_message})

‎airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_connections.py‎

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
# under the License.
1717
from __future__ import annotations
1818

19+
import json
1920
import os
2021
from importlib.metadata import PackageNotFoundError, metadata
2122
from unittest import mock
2223

2324
import pytest
24-
from sqlalchemy import select
25+
from sqlalchemy import func, select
2526
from sqlalchemy.orm import Session
2627

2728
from airflow.models import Connection
@@ -1001,6 +1002,140 @@ def test_should_respond_403_by_default(self, test_client, body):
10011002
"Contact your deployment admin to enable it."
10021003
}
10031004

1005+
@mock.patch.dict(os.environ, {"AIRFLOW__CORE__TEST_CONNECTION": "Enabled"})
1006+
def test_should_merge_password_with_existing_connection(self, test_client, session):
1007+
connection = Connection(
1008+
conn_id=TEST_CONN_ID,
1009+
conn_type="sqlite",
1010+
password="existing_password",
1011+
)
1012+
session.add(connection)
1013+
session.commit()
1014+
initial_count = session.scalar(select(func.count()).select_from(Connection))
1015+
1016+
captured_value = {}
1017+
1018+
def mock_test_connection(self):
1019+
captured_value["password"] = self.password
1020+
captured_value["conn_type"] = self.conn_type
1021+
return True, "mocked"
1022+
1023+
body = {
1024+
"connection_id": TEST_CONN_ID,
1025+
"conn_type": "new_sqlite",
1026+
"password": "***",
1027+
}
1028+
1029+
with mock.patch.object(Connection, "test_connection", mock_test_connection):
1030+
response = test_client.post("/connections/test", json=body)
1031+
1032+
assert response.status_code == 200
1033+
assert response.json()["status"] is True
1034+
# Verify that the existing password was used, not "***"
1035+
assert captured_value["password"] == "existing_password"
1036+
# Verify that payload info were used for other fields
1037+
assert captured_value["conn_type"] == "new_sqlite"
1038+
1039+
# Verify DB was not mutated
1040+
session.expire_all()
1041+
db_conn = session.scalar(select(Connection).filter_by(conn_id=TEST_CONN_ID))
1042+
assert db_conn.password == "existing_password"
1043+
assert session.scalar(select(func.count()).select_from(Connection)) == initial_count
1044+
1045+
@mock.patch.dict(os.environ, {"AIRFLOW__CORE__TEST_CONNECTION": "Enabled"})
1046+
def test_should_merge_extra_with_existing_connection(self, test_client, session):
1047+
connection = Connection(
1048+
conn_id=TEST_CONN_ID,
1049+
conn_type="fs",
1050+
extra='{"path": "/", "existing_key": "existing_value"}',
1051+
)
1052+
session.add(connection)
1053+
session.commit()
1054+
initial_count = session.scalar(select(func.count()).select_from(Connection))
1055+
1056+
captured_extra = {}
1057+
1058+
def mock_test_connection(self):
1059+
captured_extra["value"] = self.extra
1060+
return True, "mocked"
1061+
1062+
body = {
1063+
"connection_id": TEST_CONN_ID,
1064+
"conn_type": "fs",
1065+
"extra": '{"path": "/", "new_key": "new_value"}',
1066+
}
1067+
1068+
with mock.patch.object(Connection, "test_connection", mock_test_connection):
1069+
response = test_client.post("/connections/test", json=body)
1070+
1071+
assert response.status_code == 200
1072+
assert response.json()["status"] is True
1073+
# Verify that new_key is reflected in the merged extra
1074+
merged_extra = json.loads(captured_extra["value"])
1075+
assert merged_extra["new_key"] == "new_value"
1076+
assert merged_extra["path"] == "/"
1077+
1078+
# Verify DB was not mutated
1079+
session.expire_all()
1080+
db_conn = session.scalar(select(Connection).filter_by(conn_id=TEST_CONN_ID))
1081+
assert json.loads(db_conn.extra) == {"path": "/", "existing_key": "existing_value"}
1082+
assert session.scalar(select(func.count()).select_from(Connection)) == initial_count
1083+
1084+
@mock.patch.dict(os.environ, {"AIRFLOW__CORE__TEST_CONNECTION": "Enabled"})
1085+
def test_should_merge_both_password_and_extra(self, test_client, session):
1086+
connection = Connection(
1087+
conn_id=TEST_CONN_ID,
1088+
conn_type="fs",
1089+
password="existing_password",
1090+
extra='{"path": "/", "existing_key": "existing_value"}',
1091+
)
1092+
session.add(connection)
1093+
session.commit()
1094+
initial_count = session.scalar(select(func.count()).select_from(Connection))
1095+
1096+
captured_values = {}
1097+
1098+
def mock_test_connection(self):
1099+
captured_values["password"] = self.password
1100+
captured_values["extra"] = self.extra
1101+
return True, "mocked"
1102+
1103+
body = {
1104+
"connection_id": TEST_CONN_ID,
1105+
"conn_type": "fs",
1106+
"password": "***",
1107+
"extra": '{"path": "/", "new_key": "new_value"}',
1108+
}
1109+
1110+
with mock.patch.object(Connection, "test_connection", mock_test_connection):
1111+
response = test_client.post("/connections/test", json=body)
1112+
1113+
assert response.status_code == 200
1114+
assert response.json()["status"] is True
1115+
# Verify that the existing password was used, not "***"
1116+
assert captured_values["password"] == "existing_password"
1117+
# Verify that new_key is reflected in the merged extra
1118+
merged_extra = json.loads(captured_values["extra"])
1119+
assert merged_extra["new_key"] == "new_value"
1120+
assert merged_extra["path"] == "/"
1121+
1122+
# Verify DB was not mutated
1123+
session.expire_all()
1124+
db_conn = session.scalar(select(Connection).filter_by(conn_id=TEST_CONN_ID))
1125+
assert db_conn.password == "existing_password"
1126+
assert json.loads(db_conn.extra) == {"path": "/", "existing_key": "existing_value"}
1127+
assert session.scalar(select(func.count()).select_from(Connection)) == initial_count
1128+
1129+
@mock.patch.dict(os.environ, {"AIRFLOW__CORE__TEST_CONNECTION": "Enabled"})
1130+
def test_should_test_new_connection_without_existing(self, test_client):
1131+
body = {
1132+
"connection_id": "non_existent_conn",
1133+
"conn_type": "sqlite",
1134+
}
1135+
response = test_client.post("/connections/test", json=body)
1136+
assert response.status_code == 200
1137+
assert response.json()["status"] is True
1138+
10041139

10051140
class TestCreateDefaultConnections(TestConnectionEndpoint):
10061141
def test_should_respond_204(self, test_client, session):

0 commit comments

Comments
 (0)