diff --git a/src/tailscale/cli/__init__.py b/src/tailscale/cli/__init__.py index f95fe477..a489c0da 100644 --- a/src/tailscale/cli/__init__.py +++ b/src/tailscale/cli/__init__.py @@ -27,6 +27,8 @@ help="Tailscale CLI — query and manage your tailnet from the terminal.", no_args_is_help=True, add_completion=False, + # The locals hold the credentials; older Typer versions show them. + pretty_exceptions_show_locals=False, ) console = Console() diff --git a/src/tailscale/models.py b/src/tailscale/models.py index 00944a38..c1d28119 100644 --- a/src/tailscale/models.py +++ b/src/tailscale/models.py @@ -373,7 +373,7 @@ class TailscaleKey(DataClassORJSONMixin): key_id: str = field(metadata=field_options(alias="id")) description: str = "" - key: str = "" + key: str = field(default="", repr=False) created: datetime | None = None updated: datetime | None = None expires: datetime | None = None @@ -584,7 +584,7 @@ class TailscaleWebhook(_LenientModel): provider_type: str | None = field( default=None, metadata=field_options(alias="providerType") ) - secret: str | None = None + secret: str | None = field(default=None, repr=False) subscriptions: list[str] = field(default_factory=list) @@ -798,7 +798,7 @@ class CreatedTailnetOAuthClient(_LenientModel): """Object holding the OAuth client of a newly created tailnet.""" client_id: str = field(metadata=field_options(alias="id")) - secret: str | None = None + secret: str | None = field(default=None, repr=False) @dataclass @@ -841,7 +841,7 @@ class OAuthApp(_LenientModel): default_factory=list, metadata=field_options(alias="allowedNodeAttributes") ) client_secret: str | None = field( - default=None, metadata=field_options(alias="clientSecret") + default=None, repr=False, metadata=field_options(alias="clientSecret") ) created: datetime | None = None description: str | None = None @@ -869,7 +869,7 @@ class LogStreamConfiguration(_LenientModel): default=None, metadata=field_options(alias="gcsBucket") ) gcs_credentials: str | None = field( - default=None, metadata=field_options(alias="gcsCredentials") + default=None, repr=False, metadata=field_options(alias="gcsCredentials") ) gcs_key_prefix: str | None = field( default=None, metadata=field_options(alias="gcsKeyPrefix") @@ -900,9 +900,9 @@ class LogStreamConfiguration(_LenientModel): default=None, metadata=field_options(alias="s3RoleArn") ) s3_secret_access_key: str | None = field( - default=None, metadata=field_options(alias="s3SecretAccessKey") + default=None, repr=False, metadata=field_options(alias="s3SecretAccessKey") ) - token: str | None = None + token: str | None = field(default=None, repr=False) upload_period_minutes: int | None = field( default=None, metadata=field_options(alias="uploadPeriodMinutes") ) diff --git a/src/tailscale/tailscale.py b/src/tailscale/tailscale.py index 34601a6b..4761577a 100644 --- a/src/tailscale/tailscale.py +++ b/src/tailscale/tailscale.py @@ -4,10 +4,12 @@ import asyncio import json +import math import socket -from dataclasses import dataclass +from dataclasses import dataclass, field from datetime import UTC, datetime, timedelta from typing import TYPE_CHECKING, Any, Self +from urllib.parse import quote from aiohttp.client import ClientError, ClientSession from aiohttp.hdrs import METH_DELETE, METH_GET, METH_PATCH, METH_POST, METH_PUT @@ -68,19 +70,20 @@ class Tailscale: """Main class for handling connections with the Tailscale API.""" tailnet: str = "-" - api_key: str | None = None + # Secrets are left out of the repr, so logging the client does not leak them. + api_key: str | None = field(default=None, repr=False) oauth_client_id: str | None = None - oauth_client_secret: str | None = None + oauth_client_secret: str | None = field(default=None, repr=False) request_timeout: int = 8 session: ClientSession | None = None - token_storage: TokenStorage | None = None + token_storage: TokenStorage | None = field(default=None, repr=False) _token_expiry_margin: int = 60 _get_oauth_token_task: asyncio.Task[None] | None = None _expire_oauth_token_task: asyncio.Task[None] | None = None - _rejected_oauth_token: str | None = None + _rejected_oauth_token: str | None = field(default=None, repr=False) _close_session: bool = False async def _check_api_key(self) -> None: @@ -164,12 +167,7 @@ async def _get_oauth_token(self) -> None: _use_form_encoding=True, ) - json_response: dict[str, Any] = json.loads(response) - access_token = str(json_response.get("access_token", "")) - expires_in = float(json_response.get("expires_in", 0)) - if not access_token or not expires_in: - msg = "Failed to get OAuth token" - raise TailscaleAuthenticationError(msg) + access_token, expires_in = _parse_oauth_token(response) if expires_in <= self._token_expiry_margin: msg = "OAuth token expires in less than 1 minute" raise TailscaleAuthenticationError(msg) @@ -346,13 +344,17 @@ async def devices(self) -> dict[str, Device]: """ try: - data = await self._request(f"tailnet/{self.tailnet}/devices?fields=all") + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/devices?fields=all" + ) except TailscaleNotFoundError: # Since 2026-10-08, the Tailscale API fails the whole list with a 404 # when the tailnet has devices shared in from another tailnet. The # default fields still work; they lack only the client connectivity. # https://github.com/tailscale/tailscale/issues/21721 - data = await self._request(f"tailnet/{self.tailnet}/devices?fields=default") + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/devices?fields=default" + ) return Devices.from_json(data).devices @@ -368,7 +370,7 @@ async def device(self, device_id: str) -> Device: The device information. """ - data = await self._request(f"device/{device_id}?fields=all") + data = await self._request(f"device/{_path_segment(device_id)}?fields=all") return Device.from_json(data) async def delete_device(self, device_id: str) -> None: @@ -379,7 +381,7 @@ async def delete_device(self, device_id: str) -> None: device_id: The ID of the device to delete. """ - await self._request(f"device/{device_id}", method=METH_DELETE) + await self._request(f"device/{_path_segment(device_id)}", method=METH_DELETE) async def authorize_device(self, device_id: str, *, authorized: bool) -> None: """Authorize or deauthorize a device. @@ -391,7 +393,7 @@ async def authorize_device(self, device_id: str, *, authorized: bool) -> None: """ await self._request( - f"device/{device_id}/authorized", + f"device/{_path_segment(device_id)}/authorized", method=METH_POST, data={"authorized": authorized}, ) @@ -404,7 +406,9 @@ async def expire_device_key(self, device_id: str) -> None: device_id: The ID of the device. """ - await self._request(f"device/{device_id}/expire", method=METH_POST) + await self._request( + f"device/{_path_segment(device_id)}/expire", method=METH_POST + ) async def set_device_key_expiry( self, device_id: str, *, key_expiry_disabled: bool @@ -418,7 +422,7 @@ async def set_device_key_expiry( """ await self._request( - f"device/{device_id}/key", + f"device/{_path_segment(device_id)}/key", method=METH_POST, data={"keyExpiryDisabled": key_expiry_disabled}, ) @@ -434,7 +438,7 @@ async def rename_device(self, device_id: str, *, name: str) -> None: """ await self._request( - f"device/{device_id}/name", + f"device/{_path_segment(device_id)}/name", method=METH_POST, data={"name": name}, ) @@ -449,7 +453,7 @@ async def set_device_tags(self, device_id: str, *, tags: list[str]) -> None: """ await self._request( - f"device/{device_id}/tags", + f"device/{_path_segment(device_id)}/tags", method=METH_POST, data={"tags": tags}, ) @@ -466,7 +470,7 @@ async def device_routes(self, device_id: str) -> DeviceRoutes: The advertised and enabled routes for the device. """ - data = await self._request(f"device/{device_id}/routes") + data = await self._request(f"device/{_path_segment(device_id)}/routes") return DeviceRoutes.from_json(data) async def set_device_routes( @@ -485,7 +489,7 @@ async def set_device_routes( """ data = await self._request( - f"device/{device_id}/routes", + f"device/{_path_segment(device_id)}/routes", method=METH_POST, data={"routes": routes}, ) @@ -503,7 +507,7 @@ async def set_device_ipv4_address( """ await self._request( - f"device/{device_id}/ip", + f"device/{_path_segment(device_id)}/ip", method=METH_POST, data={"ipv4": ipv4_address}, ) @@ -522,7 +526,7 @@ async def device_posture_attributes( The posture attributes of the device, with their expiries. """ - data = await self._request(f"device/{device_id}/attributes") + data = await self._request(f"device/{_path_segment(device_id)}/attributes") return DevicePostureAttributes.from_json(data) async def set_device_posture_attribute( # pylint: disable=too-many-arguments @@ -556,7 +560,7 @@ async def set_device_posture_attribute( # pylint: disable=too-many-arguments payload["comment"] = comment data = await self._request( - f"device/{device_id}/attributes/{key}", + f"device/{_path_segment(device_id)}/attributes/{_path_segment(key)}", method=METH_POST, data=payload, ) @@ -571,7 +575,10 @@ async def delete_device_posture_attribute(self, device_id: str, key: str) -> Non key: The name of the attribute, starting with "custom:". """ - await self._request(f"device/{device_id}/attributes/{key}", method=METH_DELETE) + await self._request( + f"device/{_path_segment(device_id)}/attributes/{_path_segment(key)}", + method=METH_DELETE, + ) async def update_device_posture_attributes( self, @@ -606,7 +613,7 @@ async def update_device_posture_attributes( payload["comment"] = comment await self._request( - f"tailnet/{self.tailnet}/device-attributes", + f"tailnet/{_path_segment(self.tailnet)}/device-attributes", method=METH_PATCH, data=payload, ) @@ -619,7 +626,9 @@ async def dns_configuration(self) -> DNSConfiguration: The nameservers, split DNS, search paths, and preferences. """ - data = await self._request(f"tailnet/{self.tailnet}/dns/configuration") + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/dns/configuration" + ) return DNSConfiguration.from_json(data) async def set_dns_configuration( @@ -638,7 +647,7 @@ async def set_dns_configuration( """ data = await self._request( - f"tailnet/{self.tailnet}/dns/configuration", + f"tailnet/{_path_segment(self.tailnet)}/dns/configuration", method=METH_POST, data=configuration.to_dict(), ) @@ -652,7 +661,9 @@ async def dns_nameservers(self) -> DNSNameservers: The DNS nameserver configuration. """ - data = await self._request(f"tailnet/{self.tailnet}/dns/nameservers") + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/dns/nameservers" + ) return DNSNameservers.from_json(data) async def set_dns_nameservers(self, *, dns: list[str]) -> DNSNameservers: @@ -668,7 +679,7 @@ async def set_dns_nameservers(self, *, dns: list[str]) -> DNSNameservers: """ data = await self._request( - f"tailnet/{self.tailnet}/dns/nameservers", + f"tailnet/{_path_segment(self.tailnet)}/dns/nameservers", method=METH_POST, data={"dns": dns}, ) @@ -682,7 +693,9 @@ async def dns_preferences(self) -> DNSPreferences: The DNS preferences. """ - data = await self._request(f"tailnet/{self.tailnet}/dns/preferences") + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/dns/preferences" + ) return DNSPreferences.from_json(data) async def set_dns_preferences(self, *, magic_dns: bool) -> DNSPreferences: @@ -698,7 +711,7 @@ async def set_dns_preferences(self, *, magic_dns: bool) -> DNSPreferences: """ data = await self._request( - f"tailnet/{self.tailnet}/dns/preferences", + f"tailnet/{_path_segment(self.tailnet)}/dns/preferences", method=METH_POST, data={"magicDNS": magic_dns}, ) @@ -712,7 +725,9 @@ async def dns_search_paths(self) -> DNSSearchPaths: The DNS search paths. """ - data = await self._request(f"tailnet/{self.tailnet}/dns/searchpaths") + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/dns/searchpaths" + ) return DNSSearchPaths.from_json(data) async def set_dns_search_paths(self, *, search_paths: list[str]) -> DNSSearchPaths: @@ -728,7 +743,7 @@ async def set_dns_search_paths(self, *, search_paths: list[str]) -> DNSSearchPat """ data = await self._request( - f"tailnet/{self.tailnet}/dns/searchpaths", + f"tailnet/{_path_segment(self.tailnet)}/dns/searchpaths", method=METH_POST, data={"searchPaths": search_paths}, ) @@ -742,7 +757,9 @@ async def split_dns(self) -> dict[str, list[str]]: A dictionary mapping domain names to lists of nameserver addresses. """ - data = await self._request(f"tailnet/{self.tailnet}/dns/split-dns") + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/dns/split-dns" + ) return json.loads(data) async def set_split_dns( @@ -761,7 +778,7 @@ async def set_split_dns( """ data = await self._request( - f"tailnet/{self.tailnet}/dns/split-dns", + f"tailnet/{_path_segment(self.tailnet)}/dns/split-dns", method=METH_PUT, data=split_dns, ) @@ -783,7 +800,7 @@ async def update_split_dns( """ data = await self._request( - f"tailnet/{self.tailnet}/dns/split-dns", + f"tailnet/{_path_segment(self.tailnet)}/dns/split-dns", method=METH_PATCH, data=split_dns, ) @@ -798,7 +815,7 @@ async def policy_file(self) -> PolicyFile: """ policy, headers = await self._request_with_headers( - f"tailnet/{self.tailnet}/acl", + f"tailnet/{_path_segment(self.tailnet)}/acl", headers={"Accept": "application/hujson"}, ) return PolicyFile(policy=policy, etag=headers.get("ETag")) @@ -828,7 +845,7 @@ async def set_policy_file( headers["If-Match"] = etag new_policy, response_headers = await self._request_with_headers( - f"tailnet/{self.tailnet}/acl", + f"tailnet/{_path_segment(self.tailnet)}/acl", method=METH_POST, content=policy, headers=headers, @@ -848,7 +865,7 @@ async def validate_policy_file(self, policy: str) -> PolicyFileValidation: """ data, _ = await self._request_with_headers( - f"tailnet/{self.tailnet}/acl/validate", + f"tailnet/{_path_segment(self.tailnet)}/acl/validate", method=METH_POST, content=policy, headers={"Content-Type": "application/hujson"}, @@ -871,7 +888,7 @@ async def test_policy_file( """ data, _ = await self._request_with_headers( - f"tailnet/{self.tailnet}/acl/validate", + f"tailnet/{_path_segment(self.tailnet)}/acl/validate", method=METH_POST, content=json.dumps(tests), headers={"Content-Type": "application/json"}, @@ -896,7 +913,7 @@ async def preview_policy_rules( """ data, _ = await self._request_with_headers( - f"tailnet/{self.tailnet}/acl/preview", + f"tailnet/{_path_segment(self.tailnet)}/acl/preview", method=METH_POST, params={"type": preview_type, "previewFor": preview_for}, content=policy, @@ -929,7 +946,9 @@ async def users( if role is not None: params["role"] = role - data = await self._request(f"tailnet/{self.tailnet}/users", params=params) + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/users", params=params + ) raw: list[dict[str, Any]] = json.loads(data).get("users", []) return [TailscaleUser.from_dict(user) for user in raw] @@ -945,7 +964,7 @@ async def user(self, user_id: str) -> TailscaleUser: The user information. """ - data = await self._request(f"users/{user_id}") + data = await self._request(f"users/{_path_segment(user_id)}") return TailscaleUser.from_json(data) async def set_user_role(self, user_id: str, *, role: str) -> None: @@ -958,7 +977,7 @@ async def set_user_role(self, user_id: str, *, role: str) -> None: """ await self._request( - f"users/{user_id}/role", + f"users/{_path_segment(user_id)}/role", method=METH_POST, data={"role": role}, ) @@ -971,7 +990,7 @@ async def approve_user(self, user_id: str) -> None: user_id: The ID of the user to approve. """ - await self._request(f"users/{user_id}/approve", method=METH_POST) + await self._request(f"users/{_path_segment(user_id)}/approve", method=METH_POST) async def suspend_user(self, user_id: str) -> None: """Suspend a user from the tailnet. @@ -981,7 +1000,7 @@ async def suspend_user(self, user_id: str) -> None: user_id: The ID of the user to suspend. """ - await self._request(f"users/{user_id}/suspend", method=METH_POST) + await self._request(f"users/{_path_segment(user_id)}/suspend", method=METH_POST) async def restore_user(self, user_id: str) -> None: """Restore the access of a suspended user to the tailnet. @@ -991,7 +1010,7 @@ async def restore_user(self, user_id: str) -> None: user_id: The ID of the user to restore. """ - await self._request(f"users/{user_id}/restore", method=METH_POST) + await self._request(f"users/{_path_segment(user_id)}/restore", method=METH_POST) async def delete_user(self, user_id: str) -> None: """Delete a user from the tailnet. @@ -1001,7 +1020,7 @@ async def delete_user(self, user_id: str) -> None: user_id: The ID of the user to delete. """ - await self._request(f"users/{user_id}/delete", method=METH_POST) + await self._request(f"users/{_path_segment(user_id)}/delete", method=METH_POST) async def tailnet_settings(self) -> TailnetSettings: """Get the settings for the tailnet. @@ -1011,7 +1030,7 @@ async def tailnet_settings(self) -> TailnetSettings: The tailnet settings. """ - data = await self._request(f"tailnet/{self.tailnet}/settings") + data = await self._request(f"tailnet/{_path_segment(self.tailnet)}/settings") return TailnetSettings.from_json(data) async def update_tailnet_settings( # noqa: PLR0913 # pylint: disable=too-many-arguments @@ -1085,7 +1104,7 @@ async def update_tailnet_settings( # noqa: PLR0913 # pylint: disable=too-many- if acls_external_link is not None: payload["aclsExternalLink"] = acls_external_link await self._request( - f"tailnet/{self.tailnet}/settings", + f"tailnet/{_path_segment(self.tailnet)}/settings", method=METH_PATCH, data=payload, ) @@ -1098,7 +1117,9 @@ async def keys(self) -> list[TailscaleKey]: A list of Tailscale keys. """ - data = await self._request(f"tailnet/{self.tailnet}/keys?all=true") + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/keys?all=true" + ) raw: list[dict[str, Any]] = json.loads(data).get("keys", []) return [TailscaleKey.from_dict(key) for key in raw] @@ -1114,7 +1135,9 @@ async def key(self, key_id: str) -> TailscaleKey: The key information. """ - data = await self._request(f"tailnet/{self.tailnet}/keys/{key_id}") + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/keys/{_path_segment(key_id)}" + ) return TailscaleKey.from_json(data) async def create_key( # noqa: PLR0913 # pylint: disable=too-many-arguments @@ -1192,7 +1215,7 @@ async def create_key( # noqa: PLR0913 # pylint: disable=too-many-arguments ) data = await self._request( - f"tailnet/{self.tailnet}/keys", + f"tailnet/{_path_segment(self.tailnet)}/keys", method=METH_POST, data=payload, ) @@ -1248,7 +1271,7 @@ async def set_key( # noqa: PLR0913 # pylint: disable=too-many-arguments ) data = await self._request( - f"tailnet/{self.tailnet}/keys/{key_id}", + f"tailnet/{_path_segment(self.tailnet)}/keys/{_path_segment(key_id)}", method=METH_PUT, data=payload, ) @@ -1263,7 +1286,7 @@ async def delete_key(self, key_id: str) -> None: """ await self._request( - f"tailnet/{self.tailnet}/keys/{key_id}", + f"tailnet/{_path_segment(self.tailnet)}/keys/{_path_segment(key_id)}", method=METH_DELETE, ) @@ -1275,7 +1298,7 @@ async def webhooks(self) -> list[TailscaleWebhook]: A list of webhooks. """ - data = await self._request(f"tailnet/{self.tailnet}/webhooks") + data = await self._request(f"tailnet/{_path_segment(self.tailnet)}/webhooks") # The API returns null instead of an empty list when there are none. raw: list[dict[str, Any]] = json.loads(data).get("webhooks") or [] return [TailscaleWebhook.from_dict(webhook) for webhook in raw] @@ -1292,7 +1315,7 @@ async def webhook(self, endpoint_id: str) -> TailscaleWebhook: The webhook. """ - data = await self._request(f"webhooks/{endpoint_id}") + data = await self._request(f"webhooks/{_path_segment(endpoint_id)}") return TailscaleWebhook.from_json(data) async def create_webhook( @@ -1324,7 +1347,7 @@ async def create_webhook( payload["providerType"] = provider_type data = await self._request( - f"tailnet/{self.tailnet}/webhooks", + f"tailnet/{_path_segment(self.tailnet)}/webhooks", method=METH_POST, data=payload, ) @@ -1346,7 +1369,7 @@ async def update_webhook( """ data = await self._request( - f"webhooks/{endpoint_id}", + f"webhooks/{_path_segment(endpoint_id)}", method=METH_PATCH, data={"subscriptions": subscriptions}, ) @@ -1360,7 +1383,9 @@ async def delete_webhook(self, endpoint_id: str) -> None: endpoint_id: The ID of the webhook to delete. """ - await self._request(f"webhooks/{endpoint_id}", method=METH_DELETE) + await self._request( + f"webhooks/{_path_segment(endpoint_id)}", method=METH_DELETE + ) async def test_webhook(self, endpoint_id: str) -> None: """Send a test event to a webhook. @@ -1372,7 +1397,9 @@ async def test_webhook(self, endpoint_id: str) -> None: endpoint_id: The ID of the webhook to test. """ - await self._request(f"webhooks/{endpoint_id}/test", method=METH_POST) + await self._request( + f"webhooks/{_path_segment(endpoint_id)}/test", method=METH_POST + ) async def rotate_webhook_secret(self, endpoint_id: str) -> TailscaleWebhook: """Rotate the secret a webhook signs its events with. @@ -1386,7 +1413,9 @@ async def rotate_webhook_secret(self, endpoint_id: str) -> TailscaleWebhook: The webhook, including its new secret. """ - data = await self._request(f"webhooks/{endpoint_id}/rotate", method=METH_POST) + data = await self._request( + f"webhooks/{_path_segment(endpoint_id)}/rotate", method=METH_POST + ) return TailscaleWebhook.from_json(data) async def services(self) -> list[TailscaleService]: @@ -1397,7 +1426,7 @@ async def services(self) -> list[TailscaleService]: A list of Services. """ - data = await self._request(f"tailnet/{self.tailnet}/services") + data = await self._request(f"tailnet/{_path_segment(self.tailnet)}/services") raw: list[dict[str, Any]] = json.loads(data).get("vipServices") or [] return [TailscaleService.from_dict(service) for service in raw] @@ -1413,7 +1442,9 @@ async def service(self, name: str) -> TailscaleService: The Service. """ - data = await self._request(f"tailnet/{self.tailnet}/services/{name}") + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/services/{_path_segment(name)}" + ) return TailscaleService.from_json(data) async def set_service( @@ -1433,7 +1464,7 @@ async def set_service( """ data = await self._request( - f"tailnet/{self.tailnet}/services/{name or service.name}", + f"tailnet/{_path_segment(self.tailnet)}/services/{name or service.name}", method=METH_PUT, data=service.to_dict(), ) @@ -1448,7 +1479,8 @@ async def delete_service(self, name: str) -> None: """ await self._request( - f"tailnet/{self.tailnet}/services/{name}", method=METH_DELETE + f"tailnet/{_path_segment(self.tailnet)}/services/{_path_segment(name)}", + method=METH_DELETE, ) async def service_hosts(self, name: str) -> list[ServiceHost]: @@ -1463,7 +1495,9 @@ async def service_hosts(self, name: str) -> list[ServiceHost]: A list of the devices hosting the Service. """ - data = await self._request(f"tailnet/{self.tailnet}/services/{name}/devices") + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/services/{_path_segment(name)}/devices" + ) raw: list[dict[str, Any]] = json.loads(data).get("hosts") or [] return [ServiceHost.from_dict(host) for host in raw] @@ -1481,7 +1515,7 @@ async def service_approval(self, name: str, device_id: str) -> ServiceApproval: """ data = await self._request( - f"tailnet/{self.tailnet}/services/{name}/device/{device_id}/approved" + f"tailnet/{_path_segment(self.tailnet)}/services/{_path_segment(name)}/device/{_path_segment(device_id)}/approved" ) return ServiceApproval.from_json(data) @@ -1502,7 +1536,7 @@ async def set_service_approval( """ data = await self._request( - f"tailnet/{self.tailnet}/services/{name}/device/{device_id}/approved", + f"tailnet/{_path_segment(self.tailnet)}/services/{_path_segment(name)}/device/{_path_segment(device_id)}/approved", method=METH_POST, data={"approved": approved}, ) @@ -1520,7 +1554,7 @@ async def device_invites(self, device_id: str) -> list[DeviceInvite]: A list of invites to share the device. """ - data = await self._request(f"device/{device_id}/device-invites") + data = await self._request(f"device/{_path_segment(device_id)}/device-invites") return [DeviceInvite.from_dict(invite) for invite in json.loads(data) or []] async def create_device_invite( @@ -1555,7 +1589,7 @@ async def create_device_invite( invite["email"] = email data = await self._request( - f"device/{device_id}/device-invites", + f"device/{_path_segment(device_id)}/device-invites", method=METH_POST, data=[invite], ) @@ -1573,7 +1607,7 @@ async def device_invite(self, invite_id: str) -> DeviceInvite: The invite. """ - data = await self._request(f"device-invites/{invite_id}") + data = await self._request(f"device-invites/{_path_segment(invite_id)}") return DeviceInvite.from_json(data) async def delete_device_invite(self, invite_id: str) -> None: @@ -1584,7 +1618,9 @@ async def delete_device_invite(self, invite_id: str) -> None: invite_id: The ID of the invite to delete. """ - await self._request(f"device-invites/{invite_id}", method=METH_DELETE) + await self._request( + f"device-invites/{_path_segment(invite_id)}", method=METH_DELETE + ) async def resend_device_invite(self, invite_id: str) -> None: """Email an invite to share a device again. @@ -1594,7 +1630,9 @@ async def resend_device_invite(self, invite_id: str) -> None: invite_id: The ID of an invite that was created with an email. """ - await self._request(f"device-invites/{invite_id}/resend", method=METH_POST) + await self._request( + f"device-invites/{_path_segment(invite_id)}/resend", method=METH_POST + ) async def accept_device_invite(self, invite: str) -> AcceptedDeviceInvite: """Accept an invite to share a device into the tailnet. @@ -1623,7 +1661,9 @@ async def user_invites(self) -> list[UserInvite]: A list of invites that have not been accepted yet. """ - data = await self._request(f"tailnet/{self.tailnet}/user-invites") + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/user-invites" + ) # The API returns null instead of an empty list when there are none. return [UserInvite.from_dict(invite) for invite in json.loads(data) or []] @@ -1650,7 +1690,7 @@ async def create_user_invite( invite["email"] = email data = await self._request( - f"tailnet/{self.tailnet}/user-invites", + f"tailnet/{_path_segment(self.tailnet)}/user-invites", method=METH_POST, data=[invite], ) @@ -1668,7 +1708,7 @@ async def user_invite(self, invite_id: str) -> UserInvite: The invite. """ - data = await self._request(f"user-invites/{invite_id}") + data = await self._request(f"user-invites/{_path_segment(invite_id)}") return UserInvite.from_json(data) async def delete_user_invite(self, invite_id: str) -> None: @@ -1679,7 +1719,9 @@ async def delete_user_invite(self, invite_id: str) -> None: invite_id: The ID of the invite to delete. """ - await self._request(f"user-invites/{invite_id}", method=METH_DELETE) + await self._request( + f"user-invites/{_path_segment(invite_id)}", method=METH_DELETE + ) async def resend_user_invite(self, invite_id: str) -> None: """Email an invite for a user to join the tailnet again. @@ -1689,7 +1731,9 @@ async def resend_user_invite(self, invite_id: str) -> None: invite_id: The ID of an invite that was created with an email. """ - await self._request(f"user-invites/{invite_id}/resend", method=METH_POST) + await self._request( + f"user-invites/{_path_segment(invite_id)}/resend", method=METH_POST + ) async def contacts(self) -> TailnetContacts: """Get the contacts of the tailnet. @@ -1699,7 +1743,7 @@ async def contacts(self) -> TailnetContacts: The account, support, and security contacts. """ - data = await self._request(f"tailnet/{self.tailnet}/contacts") + data = await self._request(f"tailnet/{_path_segment(self.tailnet)}/contacts") return TailnetContacts.from_json(data) async def set_contact(self, contact_type: str, *, email: str) -> None: @@ -1714,7 +1758,7 @@ async def set_contact(self, contact_type: str, *, email: str) -> None: """ await self._request( - f"tailnet/{self.tailnet}/contacts/{contact_type}", + f"tailnet/{_path_segment(self.tailnet)}/contacts/{_path_segment(contact_type)}", method=METH_PATCH, data={"email": email}, ) @@ -1728,7 +1772,7 @@ async def resend_contact_verification(self, contact_type: str) -> None: """ await self._request( - f"tailnet/{self.tailnet}/contacts/{contact_type}/resend-verification-email", + f"tailnet/{_path_segment(self.tailnet)}/contacts/{_path_segment(contact_type)}/resend-verification-email", method=METH_POST, ) @@ -1760,7 +1804,7 @@ async def organization_tailnets( params["cursor"] = cursor data = await self._request( - f"organizations/{organization}/tailnets", params=params + f"organizations/{_path_segment(organization)}/tailnets", params=params ) return OrganizationTailnets.from_json(data) @@ -1781,7 +1825,7 @@ async def create_organization_tailnet( """ data = await self._request( - f"organizations/{organization}/tailnets", + f"organizations/{_path_segment(organization)}/tailnets", method=METH_POST, data={"displayName": display_name}, ) @@ -1793,7 +1837,9 @@ async def delete_tailnet(self) -> None: This cannot be undone. It is meant for API-only tailnets, using credentials for the tailnet that is deleted. """ - await self._request(f"tailnet/{self.tailnet}", method=METH_DELETE) + await self._request( + f"tailnet/{_path_segment(self.tailnet)}", method=METH_DELETE + ) async def oauth_apps(self) -> list[OAuthApp]: """Get the OAuth apps of the tailnet. @@ -1803,7 +1849,7 @@ async def oauth_apps(self) -> list[OAuthApp]: A list of OAuth apps. """ - data = await self._request(f"tailnet/{self.tailnet}/oauth-apps") + data = await self._request(f"tailnet/{_path_segment(self.tailnet)}/oauth-apps") raw: list[dict[str, Any]] = json.loads(data).get("oauthApps") or [] return [OAuthApp.from_dict(app) for app in raw] @@ -1819,7 +1865,9 @@ async def oauth_app(self, app_id: str) -> OAuthApp: The OAuth app. """ - data = await self._request(f"tailnet/{self.tailnet}/oauth-apps/{app_id}") + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/oauth-apps/{_path_segment(app_id)}" + ) return OAuthApp.from_json(data) async def create_oauth_app( # pylint: disable=too-many-arguments @@ -1850,7 +1898,7 @@ async def create_oauth_app( # pylint: disable=too-many-arguments """ data = await self._request( - f"tailnet/{self.tailnet}/oauth-apps", + f"tailnet/{_path_segment(self.tailnet)}/oauth-apps", method=METH_POST, data=_oauth_app_payload( name=name, @@ -1893,7 +1941,7 @@ async def set_oauth_app( # noqa: PLR0913 # pylint: disable=too-many-arguments """ data = await self._request( - f"tailnet/{self.tailnet}/oauth-apps/{app_id}", + f"tailnet/{_path_segment(self.tailnet)}/oauth-apps/{_path_segment(app_id)}", method=METH_PUT, data=_oauth_app_payload( name=name, @@ -1914,7 +1962,8 @@ async def delete_oauth_app(self, app_id: str) -> None: """ await self._request( - f"tailnet/{self.tailnet}/oauth-apps/{app_id}", method=METH_DELETE + f"tailnet/{_path_segment(self.tailnet)}/oauth-apps/{_path_segment(app_id)}", + method=METH_DELETE, ) async def log_stream_configuration(self, log_type: str) -> LogStreamConfiguration: @@ -1929,7 +1978,9 @@ async def log_stream_configuration(self, log_type: str) -> LogStreamConfiguratio The log streaming configuration. """ - data = await self._request(f"tailnet/{self.tailnet}/logging/{log_type}/stream") + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/logging/{_path_segment(log_type)}/stream" + ) return LogStreamConfiguration.from_json(data) async def set_log_stream_configuration( @@ -1944,7 +1995,7 @@ async def set_log_stream_configuration( """ await self._request( - f"tailnet/{self.tailnet}/logging/{log_type}/stream", + f"tailnet/{_path_segment(self.tailnet)}/logging/{_path_segment(log_type)}/stream", method=METH_PUT, data=configuration.to_dict(), ) @@ -1958,7 +2009,7 @@ async def disable_log_streaming(self, log_type: str) -> None: """ await self._request( - f"tailnet/{self.tailnet}/logging/{log_type}/stream", + f"tailnet/{_path_segment(self.tailnet)}/logging/{_path_segment(log_type)}/stream", method=METH_DELETE, ) @@ -1975,7 +2026,7 @@ async def log_stream_status(self, log_type: str) -> LogStreamStatus: """ data = await self._request( - f"tailnet/{self.tailnet}/logging/{log_type}/stream/status" + f"tailnet/{_path_segment(self.tailnet)}/logging/{_path_segment(log_type)}/stream/status" ) return LogStreamStatus.from_json(data) @@ -1994,7 +2045,7 @@ async def aws_external_id(self, *, reusable: bool = False) -> AwsExternalId: """ data = await self._request( - f"tailnet/{self.tailnet}/aws-external-id", + f"tailnet/{_path_segment(self.tailnet)}/aws-external-id", method=METH_POST, data={"reusable": reusable}, ) @@ -2014,7 +2065,7 @@ async def validate_aws_trust_policy( """ await self._request( - f"tailnet/{self.tailnet}/aws-external-id/{external_id}" + f"tailnet/{_path_segment(self.tailnet)}/aws-external-id/{_path_segment(external_id)}" "/validate-aws-trust-policy", method=METH_POST, data={"roleArn": role_arn}, @@ -2059,7 +2110,8 @@ async def configuration_audit_logs( # pylint: disable=too-many-arguments params["event"] = events data = await self._request( - f"tailnet/{self.tailnet}/logging/configuration", params=params + f"tailnet/{_path_segment(self.tailnet)}/logging/configuration", + params=params, ) raw: list[dict[str, Any]] = json.loads(data).get("logs") or [] return [AuditLog.from_dict(log) for log in raw] @@ -2080,7 +2132,7 @@ async def network_flow_logs( """ data = await self._request( - f"tailnet/{self.tailnet}/logging/network", + f"tailnet/{_path_segment(self.tailnet)}/logging/network", params={"start": start.isoformat(), "end": end.isoformat()}, ) raw: list[dict[str, Any]] = json.loads(data).get("logs") or [] @@ -2094,7 +2146,9 @@ async def posture_integrations(self) -> list[PostureIntegration]: A list of posture integrations. """ - data = await self._request(f"tailnet/{self.tailnet}/posture/integrations") + data = await self._request( + f"tailnet/{_path_segment(self.tailnet)}/posture/integrations" + ) raw: list[dict[str, Any]] = json.loads(data).get("integrations") or [] return [PostureIntegration.from_dict(integration) for integration in raw] @@ -2110,7 +2164,9 @@ async def posture_integration(self, integration_id: str) -> PostureIntegration: The posture integration. """ - data = await self._request(f"posture/integrations/{integration_id}") + data = await self._request( + f"posture/integrations/{_path_segment(integration_id)}" + ) return PostureIntegration.from_json(data) async def create_posture_integration( # pylint: disable=too-many-arguments @@ -2146,7 +2202,7 @@ async def create_posture_integration( # pylint: disable=too-many-arguments payload["provider"] = provider data = await self._request( - f"tailnet/{self.tailnet}/posture/integrations", + f"tailnet/{_path_segment(self.tailnet)}/posture/integrations", method=METH_POST, data=payload, ) @@ -2180,7 +2236,7 @@ async def update_posture_integration( # pylint: disable=too-many-arguments """ data = await self._request( - f"posture/integrations/{integration_id}", + f"posture/integrations/{_path_segment(integration_id)}", method=METH_PATCH, data=_posture_integration_payload( client_secret=client_secret, @@ -2200,7 +2256,7 @@ async def delete_posture_integration(self, integration_id: str) -> None: """ await self._request( - f"posture/integrations/{integration_id}", method=METH_DELETE + f"posture/integrations/{_path_segment(integration_id)}", method=METH_DELETE ) async def close(self) -> None: @@ -2342,3 +2398,64 @@ def _posture_integration_payload( "tenantId": tenant_id, } return {name: value for name, value in fields.items() if value is not None} + + +def _path_segment(value: str) -> str: + """Escape a value, like an ID, to put it in the path of a request URI. + + Without it, an ID like "../tailnet/-/keys/k123" would send the request + to another endpoint than the method is for. + + Raises + ------ + ValueError: The value is empty, or a "." or "..", which no escaping + keeps from changing the path. + + Returns + ------- + The escaped value. + + """ + if value in {"", ".", ".."}: + msg = f"Invalid value for a path segment: {value!r}" + raise ValueError(msg) + # Colons are kept, as they are in names like "svc:web" and "custom:attr". + return quote(value, safe=":") + + +def _parse_oauth_token(response: str) -> tuple[str, float]: + """Get the access token, and its seconds until expiry, from a response. + + The response itself is left out of the error, as it holds the token. + + Raises + ------ + TailscaleAuthenticationError: The response holds no usable token. + + Returns + ------- + The access token, and the seconds until it expires. + + """ + try: + data = json.loads(response) + except ValueError: + data = None + if not isinstance(data, dict): + msg = "Failed to get OAuth token: the response is not a JSON object" + raise TailscaleAuthenticationError(msg) + + access_token = data.get("access_token") + expires_in = data.get("expires_in") + if not isinstance(access_token, str) or not access_token: + msg = "Failed to get OAuth token: the response has no access token" + raise TailscaleAuthenticationError(msg) + if ( + isinstance(expires_in, bool) + or not isinstance(expires_in, int | float) + or not math.isfinite(expires_in) + or expires_in <= 0 + ): + msg = "Failed to get OAuth token: the response has no valid expiry" + raise TailscaleAuthenticationError(msg) + return access_token, float(expires_in) diff --git a/tests/__snapshots__/test_tailscale.ambr b/tests/__snapshots__/test_tailscale.ambr index 36b1792b..8718c090 100644 --- a/tests/__snapshots__/test_tailscale.ambr +++ b/tests/__snapshots__/test_tailscale.ambr @@ -21,15 +21,15 @@ DNSSearchPaths(search_paths=['corp.example.com', 'internal.example.com']) # --- # name: test_key_snapshot - TailscaleKey(key_id='k1234567890abcdef', description='CI deploy key', key='tskey-auth-k1234567890abcdef-abcdef1234567890abcdef1234567890', created=datetime.datetime(2026, 1, 15, 8, 0, tzinfo=datetime.timezone.utc), updated=None, expires=datetime.datetime(2026, 4, 15, 8, 0, tzinfo=datetime.timezone.utc), expiry_seconds=None, revoked=None, invalid=False, capabilities=KeyCapabilities(devices=KeyCapabilitiesDevices(create=KeyCapabilitiesCreate(reusable=True, ephemeral=False, preauthorized=True, tags=['tag:ci', 'tag:deploy']))), key_type='auth', user_id=None, scopes=[], tags=[], issuer=None, subject=None, audience=None, custom_claim_rules={}) + TailscaleKey(key_id='k1234567890abcdef', description='CI deploy key', created=datetime.datetime(2026, 1, 15, 8, 0, tzinfo=datetime.timezone.utc), updated=None, expires=datetime.datetime(2026, 4, 15, 8, 0, tzinfo=datetime.timezone.utc), expiry_seconds=None, revoked=None, invalid=False, capabilities=KeyCapabilities(devices=KeyCapabilitiesDevices(create=KeyCapabilitiesCreate(reusable=True, ephemeral=False, preauthorized=True, tags=['tag:ci', 'tag:deploy']))), key_type='auth', user_id=None, scopes=[], tags=[], issuer=None, subject=None, audience=None, custom_claim_rules={}) # --- # name: test_keys_snapshot list([ - TailscaleKey(key_id='k1234567890abcdef', description='CI deploy key', key='', created=datetime.datetime(2026, 1, 15, 8, 0, tzinfo=datetime.timezone.utc), updated=None, expires=datetime.datetime(2026, 4, 15, 8, 0, tzinfo=datetime.timezone.utc), expiry_seconds=None, revoked=None, invalid=False, capabilities=KeyCapabilities(devices=KeyCapabilitiesDevices(create=KeyCapabilitiesCreate(reusable=True, ephemeral=False, preauthorized=True, tags=['tag:ci', 'tag:deploy']))), key_type='auth', user_id=None, scopes=[], tags=[], issuer=None, subject=None, audience=None, custom_claim_rules={}), - TailscaleKey(key_id='kfedcba0987654321', description='Ephemeral test nodes', key='', created=datetime.datetime(2026, 3, 1, 12, 0, tzinfo=datetime.timezone.utc), updated=None, expires=datetime.datetime(2026, 6, 1, 12, 0, tzinfo=datetime.timezone.utc), expiry_seconds=None, revoked=None, invalid=False, capabilities=KeyCapabilities(devices=KeyCapabilitiesDevices(create=KeyCapabilitiesCreate(reusable=True, ephemeral=True, preauthorized=True, tags=['tag:test']))), key_type='auth', user_id=None, scopes=[], tags=[], issuer=None, subject=None, audience=None, custom_claim_rules={}), - TailscaleKey(key_id='kclient1234567890', description='Home Assistant', key='', created=datetime.datetime(2026, 5, 1, 9, 0, tzinfo=datetime.timezone.utc), updated=datetime.datetime(2026, 5, 2, 9, 0, tzinfo=datetime.timezone.utc), expires=None, expiry_seconds=None, revoked=None, invalid=False, capabilities=KeyCapabilities(devices=KeyCapabilitiesDevices(create=KeyCapabilitiesCreate(reusable=False, ephemeral=False, preauthorized=False, tags=[]))), key_type='client', user_id='u12345', scopes=['devices:core:read', 'users:read'], tags=['tag:homeassistant'], issuer=None, subject=None, audience=None, custom_claim_rules={}), - TailscaleKey(key_id='kfederated123456', description='GitHub Actions', key='', created=datetime.datetime(2026, 6, 1, 9, 0, tzinfo=datetime.timezone.utc), updated=None, expires=None, expiry_seconds=None, revoked=None, invalid=False, capabilities=KeyCapabilities(devices=KeyCapabilitiesDevices(create=KeyCapabilitiesCreate(reusable=False, ephemeral=False, preauthorized=False, tags=[]))), key_type='federated', user_id='u12345', scopes=['auth_keys'], tags=['tag:ci'], issuer='https://token.actions.githubusercontent.com', subject='repo:frenck/python-tailscale:*', audience='api.tailscale.com/kfederated123456', custom_claim_rules={'repository_owner': 'frenck'}), - TailscaleKey(key_id='kapi1234567890ab', description='Testing', key='', created=datetime.datetime(2026, 10, 9, 12, 0, tzinfo=datetime.timezone.utc), updated=None, expires=datetime.datetime(2027, 1, 7, 12, 0, tzinfo=datetime.timezone.utc), expiry_seconds=7776000, revoked=None, invalid=False, capabilities=KeyCapabilities(devices=KeyCapabilitiesDevices(create=KeyCapabilitiesCreate(reusable=False, ephemeral=False, preauthorized=False, tags=[]))), key_type='api', user_id='u12345', scopes=['all', 'all:read'], tags=[], issuer=None, subject=None, audience=None, custom_claim_rules={}), + TailscaleKey(key_id='k1234567890abcdef', description='CI deploy key', created=datetime.datetime(2026, 1, 15, 8, 0, tzinfo=datetime.timezone.utc), updated=None, expires=datetime.datetime(2026, 4, 15, 8, 0, tzinfo=datetime.timezone.utc), expiry_seconds=None, revoked=None, invalid=False, capabilities=KeyCapabilities(devices=KeyCapabilitiesDevices(create=KeyCapabilitiesCreate(reusable=True, ephemeral=False, preauthorized=True, tags=['tag:ci', 'tag:deploy']))), key_type='auth', user_id=None, scopes=[], tags=[], issuer=None, subject=None, audience=None, custom_claim_rules={}), + TailscaleKey(key_id='kfedcba0987654321', description='Ephemeral test nodes', created=datetime.datetime(2026, 3, 1, 12, 0, tzinfo=datetime.timezone.utc), updated=None, expires=datetime.datetime(2026, 6, 1, 12, 0, tzinfo=datetime.timezone.utc), expiry_seconds=None, revoked=None, invalid=False, capabilities=KeyCapabilities(devices=KeyCapabilitiesDevices(create=KeyCapabilitiesCreate(reusable=True, ephemeral=True, preauthorized=True, tags=['tag:test']))), key_type='auth', user_id=None, scopes=[], tags=[], issuer=None, subject=None, audience=None, custom_claim_rules={}), + TailscaleKey(key_id='kclient1234567890', description='Home Assistant', created=datetime.datetime(2026, 5, 1, 9, 0, tzinfo=datetime.timezone.utc), updated=datetime.datetime(2026, 5, 2, 9, 0, tzinfo=datetime.timezone.utc), expires=None, expiry_seconds=None, revoked=None, invalid=False, capabilities=KeyCapabilities(devices=KeyCapabilitiesDevices(create=KeyCapabilitiesCreate(reusable=False, ephemeral=False, preauthorized=False, tags=[]))), key_type='client', user_id='u12345', scopes=['devices:core:read', 'users:read'], tags=['tag:homeassistant'], issuer=None, subject=None, audience=None, custom_claim_rules={}), + TailscaleKey(key_id='kfederated123456', description='GitHub Actions', created=datetime.datetime(2026, 6, 1, 9, 0, tzinfo=datetime.timezone.utc), updated=None, expires=None, expiry_seconds=None, revoked=None, invalid=False, capabilities=KeyCapabilities(devices=KeyCapabilitiesDevices(create=KeyCapabilitiesCreate(reusable=False, ephemeral=False, preauthorized=False, tags=[]))), key_type='federated', user_id='u12345', scopes=['auth_keys'], tags=['tag:ci'], issuer='https://token.actions.githubusercontent.com', subject='repo:frenck/python-tailscale:*', audience='api.tailscale.com/kfederated123456', custom_claim_rules={'repository_owner': 'frenck'}), + TailscaleKey(key_id='kapi1234567890ab', description='Testing', created=datetime.datetime(2026, 10, 9, 12, 0, tzinfo=datetime.timezone.utc), updated=None, expires=datetime.datetime(2027, 1, 7, 12, 0, tzinfo=datetime.timezone.utc), expiry_seconds=7776000, revoked=None, invalid=False, capabilities=KeyCapabilities(devices=KeyCapabilitiesDevices(create=KeyCapabilitiesCreate(reusable=False, ephemeral=False, preauthorized=False, tags=[]))), key_type='api', user_id='u12345', scopes=['all', 'all:read'], tags=[], issuer=None, subject=None, audience=None, custom_claim_rules={}), ]) # --- # name: test_tailnet_settings_snapshot diff --git a/tests/cli/test_cli.py b/tests/cli/test_cli.py index b4a9ecbb..94c2b713 100644 --- a/tests/cli/test_cli.py +++ b/tests/cli/test_cli.py @@ -1302,3 +1302,8 @@ def test_dump_log_commands( end = datetime.fromisoformat(params["end"]) assert end - start == timedelta(hours=2) assert end.tzinfo is not None + + +def test_tracebacks_leave_out_locals() -> None: + """Tracebacks of the CLI do not show the locals, which hold credentials.""" + assert cli.pretty_exceptions_show_locals is False diff --git a/tests/test_tailscale.py b/tests/test_tailscale.py index ffdb4be3..505a5fc1 100644 --- a/tests/test_tailscale.py +++ b/tests/test_tailscale.py @@ -33,7 +33,9 @@ SharedDevice, TailnetContact, Tailscale, + TailscaleKey, TailscaleService, + TailscaleWebhook, UserInvite, ) from tailscale.exceptions import ( @@ -3462,3 +3464,106 @@ async def test_oauth_token_on_the_wire() -> None: assert api_request.kwargs["headers"]["Authorization"] == ( "Bearer short-lived-token" ) + + +# --- Hardening tests --- + + +async def test_ids_cannot_change_the_request_path( + responses: aioresponses, + tailscale_client: Tailscale, +) -> None: + """Test an ID is escaped, so it cannot send a request to another endpoint.""" + responses.delete( + f"{URL}/device/..%2Ftailnet%2F-%2Fkeys%2Fk1234", + status=200, + body="", + content_type="application/json", + ) + await tailscale_client.delete_device("../tailnet/-/keys/k1234") + + assert responses.requests + ((method, url),) = responses.requests + assert method == "DELETE" + assert url.raw_path == "/api/v2/device/..%2Ftailnet%2F-%2Fkeys%2Fk1234" + + +async def test_ids_keep_their_colons( + responses: aioresponses, + tailscale_client: Tailscale, +) -> None: + """Test the colon in names like "svc:web" is kept as is.""" + responses.get( + f"{URL}/tailnet/frenck/services/svc:web", + status=200, + body='{"name": "svc:web"}', + content_type="application/json", + ) + service = await tailscale_client.service("svc:web") + assert service.name == "svc:web" + + +@pytest.mark.parametrize("device_id", ["", ".", ".."]) +async def test_ids_that_cannot_be_escaped( + tailscale_client: Tailscale, + device_id: str, +) -> None: + """Test IDs that change the path whatever the escaping are refused.""" + with pytest.raises(ValueError, match="Invalid value for a path segment"): + await tailscale_client.delete_device(device_id) + + +def test_repr_leaves_out_secrets() -> None: + """Test the repr of the client and of models does not hold their secrets.""" + key = TailscaleKey(key_id="k1234", key="key-value") + values = [ + Tailscale( + api_key="api-key-value", + oauth_client_secret="client-secret-value", # noqa: S106 + ), + key, + TailscaleWebhook( + endpoint_id="e1234", + endpoint_url="https://example.com", + secret="webhook-secret-value", # noqa: S106 + ), + ] + secrets = ("api-key-value", "client-secret-value", "key-value", "webhook-secret") + + for value in values: + assert not any(secret in repr(value) for secret in secrets) + assert key.to_dict()["key"] == "key-value" + + +@pytest.mark.parametrize( + ("body", "reason"), + [ + ("not json", "not a JSON object"), + ('["token"]', "not a JSON object"), + ('{"expires_in": 3600}', "no access token"), + ('{"access_token": null, "expires_in": 3600}', "no access token"), + ('{"access_token": "", "expires_in": 3600}', "no access token"), + ('{"access_token": 42, "expires_in": 3600}', "no access token"), + ('{"access_token": "tskey-secret"}', "no valid expiry"), + ('{"access_token": "tskey-secret", "expires_in": "3600"}', "no valid expiry"), + ('{"access_token": "tskey-secret", "expires_in": true}', "no valid expiry"), + ('{"access_token": "tskey-secret", "expires_in": -1}', "no valid expiry"), + ('{"access_token": "tskey-secret", "expires_in": NaN}', "no valid expiry"), + ], +) +async def test_malformed_oauth_token_response(body: str, reason: str) -> None: + """Test a malformed token response raises an error without the token.""" + with aioresponses() as mocked: + mocked.post(OAUTH_URL, status=200, body=body, content_type="application/json") + async with aiohttp.ClientSession() as session: + tailscale = Tailscale( + tailnet="frenck", + oauth_client_id="client", + oauth_client_secret="notsosecret", # noqa: S106 + session=session, + ) + with pytest.raises(TailscaleAuthenticationError, match=reason) as excinfo: + await tailscale._request("test") + assert "tskey-secret" not in str(excinfo.value) + assert tailscale.api_key is None + await tailscale.close()