From fa080db1056efacd7cec6e4d6aadecc1d81abf59 Mon Sep 17 00:00:00 2001 From: Ranjith Guggilla Date: Sat, 21 Nov 2026 21:02:42 -0600 Subject: [PATCH] fix: publish a connection password without a username Tableau Server accepts connectionCredentials that carry a password and no name, and then keeps the username already stored on the connection. TSC refused that request, so a password-only publish never embedded the password. --- .../models/connection_credentials.py | 6 ++- tableauserverclient/server/request_factory.py | 9 +++-- test/test_workbook.py | 37 +++++++++++++++++++ 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/tableauserverclient/models/connection_credentials.py b/tableauserverclient/models/connection_credentials.py index aaa2f1bed..de3e0d1f2 100644 --- a/tableauserverclient/models/connection_credentials.py +++ b/tableauserverclient/models/connection_credentials.py @@ -10,8 +10,10 @@ class ConnectionCredentials: Parameters ---------- - name: str - The username for the connection. + name: str | None + The username for the connection. Omit it (``None``) to send a password + without a name. The server then keeps the username already stored on + the connection. An empty string is sent as ``name=""`` and clears it. password: str The password used for the connection. diff --git a/tableauserverclient/server/request_factory.py b/tableauserverclient/server/request_factory.py index 517a26956..88dcbe5f0 100644 --- a/tableauserverclient/server/request_factory.py +++ b/tableauserverclient/server/request_factory.py @@ -48,7 +48,7 @@ def _add_connections_element(connections_element, connection): connection_element.attrib["databaseName"] = connection.database_name if connection.connection_credentials: connection_credentials = connection.connection_credentials - elif connection.username is not None and connection.password is not None and connection.embed_password is not None: + elif connection.password is not None and connection.embed_password is not None: connection_credentials = ConnectionCredentials( connection.username, connection.password, embed=connection.embed_password ) @@ -70,10 +70,11 @@ def _add_view_element(views_element, view_id): def _add_credentials_element(parent_element, connection_credentials): + if connection_credentials.password is None: + raise ValueError("Connection Credentials must have a password") credentials_element = ET.SubElement(parent_element, "connectionCredentials") - if connection_credentials.password is None or connection_credentials.name is None: - raise ValueError("Connection Credentials must have a name and password") - credentials_element.attrib["name"] = connection_credentials.name + if connection_credentials.name is not None: + credentials_element.attrib["name"] = connection_credentials.name credentials_element.attrib["password"] = connection_credentials.password credentials_element.attrib["embed"] = "true" if connection_credentials.embed else "false" if connection_credentials.oauth: diff --git a/test/test_workbook.py b/test/test_workbook.py index c0965f121..ebc5e424a 100644 --- a/test/test_workbook.py +++ b/test/test_workbook.py @@ -926,6 +926,43 @@ def test_publish_multi_connection_flat(server: TSC.Server) -> None: assert connection_results[1].find("connectionCredentials").get("password", None) == "secret" +def test_publish_connection_password_without_a_name(server: TSC.Server) -> None: + new_workbook = TSC.WorkbookItem(name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760") + named = TSC.ConnectionItem() + named.server_address = "sql.test.com" + named.connection_credentials = TSC.ConnectionCredentials(None, "secret", True) + unnamed = TSC.ConnectionItem() + unnamed.server_address = "sql.test.com" + unnamed.password = "secret" + unnamed.embed_password = True + + for connection in (named, unnamed): + response = RequestFactory.Workbook._generate_xml(new_workbook, connections=[connection]) + credentials = fromstring(response).find(".//connectionCredentials") + assert credentials is not None + assert "name" not in credentials.attrib + assert credentials.get("password") == "secret" + assert credentials.get("embed") == "true" + + blank = TSC.ConnectionItem() + blank.server_address = "sql.test.com" + blank.connection_credentials = TSC.ConnectionCredentials("", "secret", True) + response = RequestFactory.Workbook._generate_xml(new_workbook, connections=[blank]) + credentials = fromstring(response).find(".//connectionCredentials") + assert credentials is not None + assert credentials.get("name") == "" + + +def test_publish_connection_credentials_require_a_password(server: TSC.Server) -> None: + new_workbook = TSC.WorkbookItem(name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760") + connection = TSC.ConnectionItem() + connection.server_address = "sql.test.com" + connection.connection_credentials = TSC.ConnectionCredentials("test", None, True) + + with pytest.raises(ValueError, match="must have a password"): + RequestFactory.Workbook._generate_xml(new_workbook, connections=[connection]) + + def test_synchronous_publish_timeout_error(server: TSC.Server) -> None: with requests_mock.mock() as m: m.register_uri("POST", server.workbooks.baseurl, status_code=504)