diff --git a/tableauserverclient/__init__.py b/tableauserverclient/__init__.py index 819cfcfa6..4e148b327 100644 --- a/tableauserverclient/__init__.py +++ b/tableauserverclient/__init__.py @@ -1,5 +1,11 @@ from tableauserverclient.bin._version import get_versions -from tableauserverclient.namespace import NEW_NAMESPACE as DEFAULT_NAMESPACE + +# Inlined as a string literal (rather than imported from server.server) so +# loading `tableauserverclient` doesn't force the full server + endpoint +# import graph to load just to read a constant. Kept in sync with the +# NAMESPACE constant defined in tableauserverclient/server/server.py. +DEFAULT_NAMESPACE = "http://tableau.com/api" + from tableauserverclient.models import ( AuthFederationConnectorItem, AuthFederationItem, diff --git a/tableauserverclient/namespace.py b/tableauserverclient/namespace.py deleted file mode 100644 index 2b5e5b2b5..000000000 --- a/tableauserverclient/namespace.py +++ /dev/null @@ -1,40 +0,0 @@ -import re - -from defusedxml.ElementTree import fromstring - -OLD_NAMESPACE = "http://tableausoftware.com/api" -NEW_NAMESPACE = "http://tableau.com/api" -NAMESPACE_RE = re.compile(r"\{(.*?)\}") - - -class UnknownNamespaceError(Exception): - """Raised when an XML response contains an unrecognized Tableau API namespace.""" - - -class Namespace: - """Detects and stores the Tableau REST API XML namespace from server responses.""" - - def __init__(self): - self._namespace = {"t": NEW_NAMESPACE} - self._detected = False - - def __call__(self): - return self._namespace - - def detect(self, xml): - """Detect the XML namespace from raw response bytes, updating the stored namespace on first call. Raises UnknownNamespaceError if the namespace is not a recognized Tableau REST API namespace.""" - if self._detected: - return - - if not xml.startswith(b" str: return f"{self.parent_srv.baseurl}/auth" + def _set_auth_from_response(self, content: bytes, url: str) -> str | None: + parsed_response = fromstring(content) + ns = self.parent_srv.namespace + site = parsed_response.find(".//t:site", namespaces=ns) + user = parsed_response.find(".//t:user", namespaces=ns) + credentials = parsed_response.find("t:credentials", namespaces=ns) + if site is None or user is None or credentials is None: + raise ServerResponseError( + "", + "Unexpected sign-in response", + f"Expected site, user and credentials elements in the {ns['t']} namespace. Root element: {parsed_response.tag}", + url, + ) + user_id = user.get("id", None) + self.parent_srv._set_auth( + site.get("id", None), user_id, credentials.get("token", None), site.get("contentUrl", None) + ) + return user_id + @api(version="2.0") def sign_in(self, auth_req: "Credentials") -> contextmgr: """ @@ -80,12 +99,7 @@ def sign_in(self, auth_req: "Credentials") -> contextmgr: auth_token=None, content_type=XML_CONTENT_TYPE, ) - parsed_response = fromstring(server_response.content) - site_id = parsed_response.find(".//t:site", namespaces=self.parent_srv.namespace).get("id", None) - site_url = parsed_response.find(".//t:site", namespaces=self.parent_srv.namespace).get("contentUrl", None) - user_id = parsed_response.find(".//t:user", namespaces=self.parent_srv.namespace).get("id", None) - auth_token = parsed_response.find("t:credentials", namespaces=self.parent_srv.namespace).get("token", None) - self.parent_srv._set_auth(site_id, user_id, auth_token, site_url) + user_id = self._set_auth_from_response(server_response.content, url) logger.info(f"Signed into {self.parent_srv.server_address} as user with id {user_id}") if self.parent_srv._use_server_version: self.parent_srv.use_server_version() @@ -152,14 +166,8 @@ def switch_site(self, site_item: "SiteItem") -> contextmgr: return Auth.contextmgr(self.sign_out) else: raise e - self.parent_srv._namespace.detect(server_response.content) self._check_status(server_response, url) - parsed_response = fromstring(server_response.content) - site_id = parsed_response.find(".//t:site", namespaces=self.parent_srv.namespace).get("id", None) - site_url = parsed_response.find(".//t:site", namespaces=self.parent_srv.namespace).get("contentUrl", None) - user_id = parsed_response.find(".//t:user", namespaces=self.parent_srv.namespace).get("id", None) - auth_token = parsed_response.find("t:credentials", namespaces=self.parent_srv.namespace).get("token", None) - self.parent_srv._set_auth(site_id, user_id, auth_token, site_url) + user_id = self._set_auth_from_response(server_response.content, url) logger.info(f"Signed into {self.parent_srv.server_address} as user with id {user_id}") return Auth.contextmgr(self.sign_out) diff --git a/tableauserverclient/server/endpoint/endpoint.py b/tableauserverclient/server/endpoint/endpoint.py index 10d3cba84..fa9540534 100644 --- a/tableauserverclient/server/endpoint/endpoint.py +++ b/tableauserverclient/server/endpoint/endpoint.py @@ -181,9 +181,6 @@ def _make_request( # BE CAREFUL WHEN SHARING THESE RESULTS - MAY CONTAIN YOUR SENSITIVE DATA # logger.debug(loggable_response) - if content_type == "application/xml": - self.parent_srv._namespace.detect(server_response.content) - return server_response def _follow_redirect_if_any( diff --git a/tableauserverclient/server/server.py b/tableauserverclient/server/server.py index 853bdea68..fc5cdb976 100644 --- a/tableauserverclient/server/server.py +++ b/tableauserverclient/server/server.py @@ -51,7 +51,6 @@ EndpointUnavailableError, ) from tableauserverclient.server.endpoint.exceptions import NotSignedInError -from tableauserverclient.namespace import Namespace _PRODUCT_TO_REST_VERSION = { "10.0": "2.3", @@ -64,6 +63,11 @@ minimum_supported_server_version = "2.3" default_server_version = "2.4" # first version that dropped the legacy auth endpoint +# The REST API has used the "http://tableau.com/api" XML namespace since Tableau +# Server 8.3 (2015). Callers on older servers are unsupported. +NAMESPACE = "http://tableau.com/api" +_NAMESPACE_MAP = {"t": NAMESPACE} + class _AuthState(NamedTuple): """ @@ -224,7 +228,6 @@ def __init__(self, server_address, use_server_version=False, http_options=None, self.data_acceleration_report = DataAccelerationReport(self) self.data_alerts = DataAlerts(self) self.fileuploads = Fileuploads(self) - self._namespace = Namespace() self.flow_runs = FlowRuns(self) self.metrics = Metrics(self) self.custom_views = CustomViews(self) @@ -381,7 +384,7 @@ def baseurl(self): @property def namespace(self): - return self._namespace() + return dict(_NAMESPACE_MAP) @property def auth_token(self): diff --git a/test/test_auth.py b/test/test_auth.py index c50f4d29b..0ea42f55d 100644 --- a/test/test_auth.py +++ b/test/test_auth.py @@ -128,6 +128,29 @@ def test_switch_site(server: TSC.Server) -> None: assert "1a96d216-e9b8-497b-a82a-0b899a965e01" == server.user_id +def test_sign_in_unknown_namespace_raises(server: TSC.Server) -> None: + response_xml = SIGN_IN_XML.read_text().replace("http://tableau.com/api", "http://tableausoftware.com/api") + with requests_mock.mock() as m: + m.post(server.auth.baseurl + "/signin", text=response_xml) + tableau_auth = TSC.TableauAuth("testuser", "password", site_id="Samples") + with pytest.raises(TSC.ServerResponseError, match="Unexpected sign-in response"): + server.auth.sign_in(tableau_auth) + + assert server._auth_token is None + + +def test_switch_site_unknown_namespace_raises(server: TSC.Server) -> None: + server.version = "2.6" + server._set_auth(*list("123")) + response_xml = SIGN_IN_XML.read_text().replace("http://tableau.com/api", "http://tableausoftware.com/api") + with requests_mock.mock() as m: + m.post(server.auth.baseurl + "/switchSite", text=response_xml) + with pytest.raises(TSC.ServerResponseError, match="Unexpected sign-in response"): + server.auth.switch_site(TSC.SiteItem("Samples", "Samples")) + + assert "3" == server.auth_token + + def test_revoke_all_server_admin_tokens(server: TSC.Server) -> None: server.version = "3.10" baseurl = server.auth.baseurl diff --git a/test/test_namespace.py b/test/test_namespace.py new file mode 100644 index 000000000..2b4a891cc --- /dev/null +++ b/test/test_namespace.py @@ -0,0 +1,20 @@ +import tableauserverclient as TSC +from tableauserverclient.server import server as server_module + + +def test_default_namespace_matches_server_namespace() -> None: + # TSC.DEFAULT_NAMESPACE is a literal in __init__.py (importing it from server.py is circular). + assert TSC.DEFAULT_NAMESPACE == server_module.NAMESPACE + + +def test_server_namespace_maps_t_prefix() -> None: + server = TSC.Server("http://test", False) + assert server.namespace == {"t": TSC.DEFAULT_NAMESPACE} + + +def test_server_namespace_is_a_copy() -> None: + server = TSC.Server("http://test", False) + server.namespace["t"] = "mutated" + server.namespace["x"] = "extra" + assert server.namespace == {"t": TSC.DEFAULT_NAMESPACE} + assert TSC.Server("http://other", False).namespace == {"t": TSC.DEFAULT_NAMESPACE}