Skip to content

Commit d32bd02

Browse files
authored
feat: update object api sign request (#349)
* feat: 签名中去除 x-log-meta- * fix content-md5 * fix sign v4 object sign * rm last modified * update sample * 0.9.39
1 parent 823cb90 commit d32bd02

File tree

6 files changed

+31
-21
lines changed

6 files changed

+31
-21
lines changed

aliyun/log/auth.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,17 @@ def sign_request(self, method, resource, params, headers, body, compute_content_
6868
headers['x-log-signaturemethod'] = 'hmac-sha1'
6969
headers['Date'] = self._getGMT()
7070

71+
content_md5 = None
72+
# we don't need content-md5 in signature if compute_content_hash is False
7173
if body and compute_content_hash:
72-
headers['Content-MD5'] = Util.cal_md5(body)
74+
content_md5 = Util.cal_md5(body)
75+
headers['Content-MD5'] = content_md5
76+
7377
if not credentials.get_access_key_secret():
7478
return six.b('')
7579
content = method + '\n'
76-
if 'Content-MD5' in headers:
77-
content += headers['Content-MD5']
80+
if content_md5 is not None:
81+
content += content_md5
7882
content += '\n'
7983
if 'Content-Type' in headers:
8084
content += headers['Content-Type']
@@ -87,31 +91,39 @@ def sign_request(self, method, resource, params, headers, body, compute_content_
8791
headers['x-log-date'] = headers['Date'] # bypass some proxy doesn't allow "Date" in header issue.
8892

8993

94+
_EMPTY_CONTENT_SHA256 = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
95+
9096
class AuthV4(AuthBase):
9197
def __init__(self, credentials_provider, region):
9298
AuthBase.__init__(self, credentials_provider)
9399
self._region = region
94100

95101
def sign_request(self, method, resource, params, headers, body, compute_content_hash=True):
96102
current_time = datetime.utcnow().strftime("%Y%m%dT%H%M%SZ")
97-
headers['Authorization'] = self._do_sign_request(method, resource, params, headers, body, current_time)
103+
headers['Authorization'] = self._do_sign_request(method, resource, params, headers, body, current_time, compute_content_hash=compute_content_hash)
98104

99-
def _do_sign_request(self, method, resource, params, headers, body, current_time):
105+
def _do_sign_request(self, method, resource, params, headers, body, current_time, compute_content_hash=True):
100106
credentials = self.credentials_provider.get_credentials()
101107

102108
if credentials.get_security_token():
103109
headers['x-acs-security-token'] = credentials.get_security_token()
104110

105-
content_sha256 = sha256(body).hexdigest() \
106-
if body else 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
111+
content_sha256 = _EMPTY_CONTENT_SHA256
112+
if compute_content_hash and body:
113+
content_sha256 = sha256(body).hexdigest()
114+
107115
headers['x-log-content-sha256'] = content_sha256
108116
headers['x-log-date'] = current_time
109117
current_date = current_time[:8]
110118
canonical_headers = {}
111119
signed_headers = ''
112120
for original_key, value in headers.items():
113121
key = original_key.lower()
114-
if key == 'content-type' or key == 'host' or key.startswith('x-log-') or key.startswith('x-acs-'):
122+
if (
123+
key == "content-type"
124+
or key == "host"
125+
or Util._is_extra_sign_header(key)
126+
):
115127
canonical_headers[key] = value
116128
headers_to_string = ''
117129
for key, value in sorted(canonical_headers.items()):

aliyun/log/object_response.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,6 @@ def get_etag(self):
5555
"""
5656
return Util.h_v_td(self.headers, 'ETag', None)
5757

58-
def get_last_modified(self):
59-
""" Get the last modified time of the object.
60-
61-
:return: string, last modified time, may be None if not set
62-
"""
63-
return Util.h_v_td(self.headers, 'Last-Modified', None)
64-
6558
def get_content_type(self):
6659
""" Get the content type of the object.
6760

aliyun/log/util.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def base64_decodestring(s):
4040
s = s.encode('utf8')
4141
return base64.decodebytes(s).decode('utf8')
4242

43+
_EXCLUDE_SIGN_HEADER_PREFIX = 'x-log-meta-'
4344

4445
class Util(object):
4546
@staticmethod
@@ -90,10 +91,18 @@ def hmac_sha1(content, key):
9091
def canonicalized_log_headers(headers):
9192
content = ''
9293
for key in sorted(six.iterkeys(headers)):
93-
if key[:6].lower() in ('x-log-', 'x-acs-'): # x-log- header
94+
if Util._is_extra_sign_header(key):
9495
content += key + ':' + str(headers[key]) + "\n"
9596
return content
9697

98+
@staticmethod
99+
def _is_extra_sign_header(key):
100+
lower_key = key.lower()
101+
return lower_key.startswith("x-acs-") or (
102+
lower_key.startswith("x-log-")
103+
and not lower_key.startswith(_EXCLUDE_SIGN_HEADER_PREFIX)
104+
)
105+
97106
@staticmethod
98107
def url_encode(params):
99108
for key, value in params.items(): # urllib.urlencode accept 8-bit str

aliyun/log/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '0.9.38'
1+
__version__ = '0.9.39'
22

33
import sys
44
OS_VERSION = str(sys.platform)

doc/tutorials/object_api.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ response = client.get_object(project, logstore, object_name)
139139

140140
# 访问响应数据
141141
print('ETag:', response.get_etag())
142-
print('Last Modified:', response.get_last_modified())
143142
print('Content Type:', response.get_content_type())
144143
print('Content Length:', len(response.get_body()))
145144
print('Content:', response.get_body())

tests/sample_object_api.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def sample_put_object():
3939
response.log_print()
4040
print(response.get_body())
4141
print('etag', response.get_etag())
42-
print('last_modified', response.get_last_modified())
4342
print('content-type', response.get_content_type())
4443

4544
except LogException as e:
@@ -67,7 +66,6 @@ def sample_put_with_header():
6766
response.log_print()
6867
print(response.get_body())
6968
print('etag', response.get_etag())
70-
print('last_modified', response.get_last_modified())
7169
print('content-type', response.get_content_type())
7270
except LogException as e:
7371
print("Put object failed:", e)
@@ -101,7 +99,6 @@ def sample_put_with_md5():
10199
response.log_print()
102100
print(response.get_body())
103101
print('etag', response.get_etag())
104-
print('last_modified', response.get_last_modified())
105102
print('content-type', response.get_content_type())
106103
except LogException as e:
107104
print("Put object failed:", e)

0 commit comments

Comments
 (0)