forked from zstackio/zstack
-
Notifications
You must be signed in to change notification settings - Fork 0
<fix>[testlib]: make Python SDK template compatible with Python 3 #3659
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
Closed
+18
−10
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| try: | ||
| import urllib3 | ||
| except ImportError: | ||
| print 'urlib3 is not installed, run "pip install urlib3"' | ||
| print('urllib3 is not installed, run "pip install urllib3"') | ||
| sys.exit(1) | ||
|
|
||
| import string | ||
|
|
@@ -16,11 +16,15 @@ | |
| import traceback | ||
| import base64 | ||
| import hmac | ||
| import sha | ||
| from hashlib import sha1 | ||
| import datetime | ||
| import time | ||
|
|
||
| try: | ||
| int_types = (int, long) | ||
| except NameError: | ||
| int_types = (int,) | ||
|
|
||
| CONFIG_HOSTNAME = 'hostname' | ||
| CONFIG_PORT = 'port' | ||
| CONFIG_POLLING_TIMEOUT = 'default_polling_timeout' | ||
|
|
@@ -55,7 +59,7 @@ def wrap(*args, **kwargs): | |
| try: | ||
| func(*args, **kwargs) | ||
| except: | ||
| print traceback.format_exc() | ||
| print(traceback.format_exc()) | ||
|
|
||
| return wrap | ||
|
|
||
|
|
@@ -189,7 +193,7 @@ def _check_params(self): | |
| if value is not None and isinstance(value, str) and annotation.empty_string is False and len(value) == 0: | ||
| raise SdkError('invalid parameter[%s], it cannot be an empty string' % param_name) | ||
|
|
||
| if value is not None and (isinstance(value, int) or isinstance(value, long)) \ | ||
| if value is not None and isinstance(value, int_types) \ | ||
| and annotation.number_range is not None and len(annotation.number_range) == 2: | ||
| low = annotation.number_range[0] | ||
| high = annotation.number_range[1] | ||
|
|
@@ -253,10 +257,14 @@ def calculateAccessKey(self, url, date): | |
| path = elements[2].split("/", 2) | ||
| path = path[2].split("?") | ||
|
|
||
| h = hmac.new(self.accessKeySecret, self.HTTP_METHOD + "\n" | ||
| + date + "\n" | ||
| + "/" + path[0], sha1) | ||
| Signature = base64.b64encode(h.digest()) | ||
| msg = self.HTTP_METHOD + "\n" + date + "\n" + "/" + path[0] | ||
| if isinstance(msg, str): | ||
| msg = msg.encode('utf-8') | ||
| secret = self.accessKeySecret | ||
| if isinstance(secret, str): | ||
| secret = secret.encode('utf-8') | ||
| h = hmac.new(secret, msg, sha1) | ||
| Signature = base64.b64encode(h.digest()).decode('utf-8') | ||
| return "ZStack %s:%s" % (self.accessKeyId, Signature) | ||
|
|
||
| def call(self, cb=None): | ||
|
|
@@ -482,13 +490,13 @@ def _json_http( | |
| if body is not None and not isinstance(body, str): | ||
| body = json.dumps(body).encode('utf-8') | ||
|
|
||
| print '[Request]: %s url=%s, headers=%s, body=%s' % (method, uri, headers, body) | ||
| print('[Request]: %s url=%s, headers=%s, body=%s' % (method, uri, headers, body)) | ||
| if body: | ||
| headers['Content-Length'] = len(body) | ||
| rsp = pool.request(method, uri, body=body, headers=headers) | ||
| else: | ||
| rsp = pool.request(method, uri, headers=headers) | ||
|
|
||
| print '[Response to %s %s]: status: %s, body: %s' % (method, uri, rsp.status, rsp.data) | ||
| print('[Response to %s %s]: status: %s, body: %s' % (method, uri, rsp.status, rsp.data)) | ||
|
Comment on lines
+493
to
+500
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 请求/响应日志包含敏感信息,建议脱敏或仅在调试模式输出 Line [489] 和 Line [496] 直接打印 🔒 建议修复+def _redact_headers(headers):
+ h = dict(headers or {})
+ if HEADER_AUTHORIZATION in h:
+ h[HEADER_AUTHORIZATION] = "***"
+ return h
...
- print('[Request]: %s url=%s, headers=%s, body=%s' % (method, uri, headers, body))
+ print('[Request]: %s url=%s, headers=%s, body=%s' % (
+ method, uri, _redact_headers(headers), '<redacted>' if body else body
+ ))
...
- print('[Response to %s %s]: status: %s, body: %s' % (method, uri, rsp.status, rsp.data))
+ print('[Response to %s %s]: status: %s, body: %s' % (
+ method, uri, rsp.status, '<redacted>'
+ ))🤖 Prompt for AI Agents |
||
| return rsp | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 50376
签名消息格式已更改 - 需要确认是否为预期变更
与原始实现(
zssdk_bak.py)相比,HMAC 签名消息格式发生了重大变化:原始格式:
新格式:
新版本移除了:
"\n"(Content_MD5 占位符)"application/json\n"(Content_Type 占位符)此更改将产生完全不同的 HMAC 签名。若服务端仍期望原始签名格式进行认证验证,此修改将导致所有带有访问密钥的请求均失败,造成严重的认证中断。
新增的 UTF-8 编码处理(第 256-260 行)是为了 Python 3 兼容性,这部分是正确的。
请确认这是有意的 API 签名格式升级,还是无意的遗漏或需要同步更新的服务端验证逻辑。
🤖 Prompt for AI Agents