Skip to content

Commit 935efd1

Browse files
SNOW-2183023: http session unmanaged requests
1 parent f9b8918 commit 935efd1

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/snowflake/connector/_aws_sign_v4.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
from __future__ import annotations
22

3-
import datetime as _dt
4-
import hashlib as _hashlib
5-
import hmac as _hmac
6-
import urllib.parse as _urlparse
3+
import datetime
4+
import hashlib
5+
import hmac
6+
import urllib.parse as urlparse
77

88
_ALGORITHM: str = "AWS4-HMAC-SHA256"
9-
_EMPTY_PAYLOAD_SHA256: str = _hashlib.sha256(b"").hexdigest()
9+
_EMPTY_PAYLOAD_SHA256: str = hashlib.sha256(b"").hexdigest()
1010
_SAFE_CHARS: str = "-_.~"
1111

1212

1313
def _sign(key: bytes, msg: str) -> bytes:
1414
"""Return an HMAC-SHA256 of *msg* keyed with *key*."""
15-
return _hmac.new(key, msg.encode(), _hashlib.sha256).digest()
15+
return hmac.new(key, msg.encode(), hashlib.sha256).digest()
1616

1717

1818
def _canonical_query_string(query: str) -> str:
1919
"""Return the query string in canonical (sorted & URL-escaped) form."""
20-
pairs = _urlparse.parse_qsl(query, keep_blank_values=True)
20+
pairs = urlparse.parse_qsl(query, keep_blank_values=True)
2121
pairs.sort()
2222
return "&".join(
23-
f"{_urlparse.quote(k, _SAFE_CHARS)}={_urlparse.quote(v, _SAFE_CHARS)}"
23+
f"{urlparse.quote(k, _SAFE_CHARS)}={urlparse.quote(v, _SAFE_CHARS)}"
2424
for k, v in pairs
2525
)
2626

@@ -50,12 +50,12 @@ def sign_get_caller_identity(
5050
session_token
5151
(Optional) session token for temporary credentials.
5252
"""
53-
timestamp = _dt.datetime.utcnow()
53+
timestamp = datetime.datetime.utcnow()
5454
amz_date = timestamp.strftime("%Y%m%dT%H%M%SZ")
5555
short_date = timestamp.strftime("%Y%m%d")
5656
service = "sts"
5757

58-
parsed = _urlparse.urlparse(url)
58+
parsed = urlparse.urlparse(url)
5959

6060
headers: dict[str, str] = {
6161
"host": parsed.netloc.lower(),
@@ -70,14 +70,14 @@ def sign_get_caller_identity(
7070
canonical_request = "\n".join(
7171
(
7272
"POST",
73-
_urlparse.quote(parsed.path or "/", safe="/"),
73+
urlparse.quote(parsed.path or "/", safe="/"),
7474
_canonical_query_string(parsed.query),
7575
"".join(f"{k}:{headers[k]}\n" for k in sorted(headers)),
7676
signed_headers,
7777
_EMPTY_PAYLOAD_SHA256,
7878
)
7979
)
80-
canonical_request_hash = _hashlib.sha256(canonical_request.encode()).hexdigest()
80+
canonical_request_hash = hashlib.sha256(canonical_request.encode()).hexdigest()
8181

8282
# String to sign
8383
credential_scope = f"{short_date}/{region}/{service}/aws4_request"
@@ -90,8 +90,8 @@ def sign_get_caller_identity(
9090
key_region = _sign(key_date, region)
9191
key_service = _sign(key_region, service)
9292
key_signing = _sign(key_service, "aws4_request")
93-
signature = _hmac.new(
94-
key_signing, string_to_sign.encode(), _hashlib.sha256
93+
signature = hmac.new(
94+
key_signing, string_to_sign.encode(), hashlib.sha256
9595
).hexdigest()
9696

9797
# Final Authorization header

test/unit/test_boto_compatibility.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def freeze_utcnow(monkeypatch: pytest.MonkeyPatch):
2828

2929
class _FrozenDateTime(datetime.datetime):
3030
@classmethod
31-
def utcnow(cls): # type: ignore[override]
31+
def utcnow(cls):
3232
return fixed
3333

3434
monkeypatch.setattr(datetime, "datetime", _FrozenDateTime)
@@ -159,7 +159,7 @@ def test_region_env_var_default(monkeypatch: pytest.MonkeyPatch) -> None:
159159

160160
def test_region_env_var_legacy(monkeypatch: pytest.MonkeyPatch) -> None:
161161
"""
162-
AWS_REGION is *ignored* by botocore currently, but should be introduced in the future: https://docs.aws.amazon.com/sdkref/latest/guide/feature-region.html
162+
AWS_REGION is ignored by botocore currently, but should be introduced in the future: https://docs.aws.amazon.com/sdkref/latest/guide/feature-region.html
163163
Therefore for now we set it as env_var for the driver and pass via explicit parameter to botocore.
164164
"""
165165
desired_region = "ca-central-1"

0 commit comments

Comments
 (0)