Skip to content

Commit 11a2566

Browse files
author
luk384090-cloud
committed
fix(encryption): pass per-call base_url and api_key to Encryption
1 parent 2cd356a commit 11a2566

4 files changed

Lines changed: 22 additions & 12 deletions

File tree

‎dashscope/api_entities/api_request_factory.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ def _build_api_request( # pylint: disable=too-many-branches
162162
http_url += "?" + urlencode(extra_url_parameters)
163163

164164
if enable_encryption is True:
165-
encryption = Encryption()
165+
encryption = Encryption(
166+
base_url=base_address,
167+
api_key=api_key,
168+
)
166169
encryption.initialize()
167170
if encryption.is_valid():
168171
logger.debug("encryption enabled")

‎dashscope/api_entities/encryption.py‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import base64
55
import json
66
import os
7+
from typing import Optional
78

89
import requests
910
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
@@ -19,18 +20,25 @@
1920
)
2021
from dashscope.common.logging import logger
2122
from dashscope.common.env import resolve_base_url
23+
from dashscope.common.api_key import get_default_api_key
2224
from dashscope.common.utils import get_sdk_headers
2325

2426

2527
class Encryption:
26-
def __init__(self):
28+
def __init__(
29+
self,
30+
base_url: Optional[str] = None,
31+
api_key: Optional[str] = None,
32+
):
2733
self.pub_key_id: str = ""
2834
self.pub_key_str: str = ""
2935
self.aes_key_bytes: bytes = b""
3036
self.encrypted_aes_key_str: str = ""
3137
self.iv_bytes: bytes = b""
3238
self.base64_iv_str: str = ""
3339
self.valid: bool = False
40+
self._base_url = base_url
41+
self._api_key = api_key
3442

3543
def initialize(self):
3644
public_keys = self._get_public_keys()
@@ -87,14 +95,12 @@ def get_encrypted_aes_key_str(self):
8795
def get_base64_iv_str(self):
8896
return self.base64_iv_str
8997

90-
@staticmethod
91-
def _get_public_keys():
92-
url = (
93-
resolve_base_url(dashscope.base_http_api_url)
94-
+ "/public-keys/latest"
95-
)
98+
def _get_public_keys(self):
99+
base_url = self._base_url or dashscope.base_http_api_url
100+
api_key = self._api_key or get_default_api_key()
101+
url = resolve_base_url(base_url) + "/public-keys/latest"
96102
headers = {
97-
"Authorization": f"Bearer {dashscope.api_key}",
103+
"Authorization": f"Bearer {api_key}",
98104
**get_sdk_headers(module="utils"),
99105
}
100106

‎tests/legacy/test_encryption.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TestEncryption:
1111
@staticmethod
1212
@pytest.mark.skip(reason="Legacy integration test, requires real DashScope API")
1313
def test_get_public_keys():
14-
pub_keys = Encryption._get_public_keys()
14+
pub_keys = Encryption()._get_public_keys()
1515
print(f"\nrsa:\n{json.dumps(pub_keys, indent=4, ensure_ascii=False)}")
1616
print(f"\npublic_key_id: {pub_keys.get('public_key_id')}")
1717
print(f"\npublic_key: {pub_keys.get('public_key')}")
@@ -37,7 +37,7 @@ def test_encrypt_with_aes():
3737
@staticmethod
3838
@pytest.mark.skip(reason="Legacy integration test, requires real DashScope API")
3939
def test_encrypt_aes_key_with_rsa():
40-
public_keys = Encryption._get_public_keys()
40+
public_keys = Encryption()._get_public_keys()
4141
public_key = public_keys.get("public_key")
4242
aes_key = Encryption._generate_aes_secret_key()
4343

‎tests/unit/test_sdk_headers.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,10 @@ def fake_get(url, headers=None, timeout=None):
661661
"dashscope.api_entities.encryption.requests.get",
662662
fake_get,
663663
)
664+
monkeypatch.setattr("dashscope.api_key", "sk-test")
664665
print("\n=== misc ===")
665666
try:
666-
Encryption._get_public_keys() # pylint: disable=protected-access
667+
Encryption()._get_public_keys() # pylint: disable=protected-access
667668
except _CapturedRequest as e:
668669
_check_module(e.headers, "utils", "utils: Encryption public-keys")
669670
else:

0 commit comments

Comments
 (0)