Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer TLD for China based on region in account #2040

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Source code is also available at: https://github.com/snowflakedb/snowflake-conne

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

- v3.12.1(August 20,2024)
- Fixed a bug that logged the session token when renewing a session.
Expand Down
17 changes: 11 additions & 6 deletions src/snowflake/connector/util_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,24 @@ def _concatenate_statements(

def construct_hostname(region: str | None, account: str) -> str:
"""Constructs hostname from region and account."""

def _is_china_region(r: str) -> bool:
# This is consistent with the Go driver:
# https://github.com/snowflakedb/gosnowflake/blob/f20a46475dce322f3f6b97b4a72f2807571e750b/dsn.go#L535
return r.lower().startswith("cn-")

if region == "us-west-2":
region = ""
if region:
if account.find(".") > 0:
account = account[0 : account.find(".")]
top_level_domain = (
"com"
if not any(substring in region for substring in ["cn-", "CN-"])
else "cn"
)
top_level_domain = "cn" if _is_china_region(region) else "com"
host = f"{account}.{region}.snowflakecomputing.{top_level_domain}"
else:
host = f"{account}.snowflakecomputing.com"
top_level_domain = "com"
if account.find(".") > 0 and _is_china_region(account.split(".")[1]):
top_level_domain = "cn"
host = f"{account}.snowflakecomputing.{top_level_domain}"
return host


Expand Down
10 changes: 10 additions & 0 deletions test/unit/test_construct_hostname.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ def test_construct_hostname_basic():
== "account1.cn-central-1.snowflakecomputing.cn"
)

assert (
construct_hostname(None, "account1.cn-central-1")
== "account1.cn-central-1.snowflakecomputing.cn"
)

assert (
construct_hostname("", "account1.cn-central-1")
== "account1.cn-central-1.snowflakecomputing.cn"
)

assert (
construct_hostname("CN-central-1", "account1")
== "account1.CN-central-1.snowflakecomputing.cn"
Expand Down
Loading