Skip to content

Commit 075b96a

Browse files
Infer TLD for China based on region in account (#2040)
Co-authored-by: Viswajith Venugopal <[email protected]>
1 parent 050a1ac commit 075b96a

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

DESCRIPTION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Source code is also available at: https://github.com/snowflakedb/snowflake-conne
1010

1111
- v3.12.2 (TBD)
1212
- Improved implementation of `snowflake.connector.util_text.random_string` to avoid collisions.
13+
- If the account specifies a region, and that region is in China, the TLD is now inferred to be snowflakecomputing.cn.
1314

1415
- v3.12.1(August 20,2024)
1516
- Fixed a bug that logged the session token when renewing a session.

src/snowflake/connector/util_text.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,24 @@ def _concatenate_statements(
235235

236236
def construct_hostname(region: str | None, account: str) -> str:
237237
"""Constructs hostname from region and account."""
238+
239+
def _is_china_region(r: str) -> bool:
240+
# This is consistent with the Go driver:
241+
# https://github.com/snowflakedb/gosnowflake/blob/f20a46475dce322f3f6b97b4a72f2807571e750b/dsn.go#L535
242+
return r.lower().startswith("cn-")
243+
238244
if region == "us-west-2":
239245
region = ""
240246
if region:
241247
if account.find(".") > 0:
242248
account = account[0 : account.find(".")]
243-
top_level_domain = (
244-
"com"
245-
if not any(substring in region for substring in ["cn-", "CN-"])
246-
else "cn"
247-
)
249+
top_level_domain = "cn" if _is_china_region(region) else "com"
248250
host = f"{account}.{region}.snowflakecomputing.{top_level_domain}"
249251
else:
250-
host = f"{account}.snowflakecomputing.com"
252+
top_level_domain = "com"
253+
if account.find(".") > 0 and _is_china_region(account.split(".")[1]):
254+
top_level_domain = "cn"
255+
host = f"{account}.snowflakecomputing.{top_level_domain}"
251256
return host
252257

253258

test/unit/test_construct_hostname.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ def test_construct_hostname_basic():
4848
== "account1.cn-central-1.snowflakecomputing.cn"
4949
)
5050

51+
assert (
52+
construct_hostname(None, "account1.cn-central-1")
53+
== "account1.cn-central-1.snowflakecomputing.cn"
54+
)
55+
56+
assert (
57+
construct_hostname("", "account1.cn-central-1")
58+
== "account1.cn-central-1.snowflakecomputing.cn"
59+
)
60+
5161
assert (
5262
construct_hostname("CN-central-1", "account1")
5363
== "account1.CN-central-1.snowflakecomputing.cn"

0 commit comments

Comments
 (0)