Skip to content

Commit ffb7f66

Browse files
author
wallisyan
committed
modify bearer-token
1 parent 692b93b commit ffb7f66

File tree

12 files changed

+104
-42
lines changed

12 files changed

+104
-42
lines changed

aliyun-python-sdk-core/alibabacloud/client.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __init__(self, access_key_id=None, access_key_secret=None, bearer_token=None
5959
extra_user_agent=None, enable_https=None, http_port=None, https_port=None,
6060
connection_timeout=None, read_timeout=None, enable_http_debug=None,
6161
http_proxy=None, https_proxy=None, enable_stream_logger=None,
62-
profile_name=None, config_file=None):
62+
profile_name=None, config_file=None, auto_retry=True):
6363

6464
self.access_key_id = access_key_id
6565
self.access_key_secret = access_key_secret
@@ -76,30 +76,38 @@ def __init__(self, access_key_id=None, access_key_secret=None, bearer_token=None
7676
self.https_port = https_port
7777
self.connection_timeout = connection_timeout
7878
self.read_timeout = read_timeout
79-
self.enable_http_debug = enable_http_debug
79+
# self.enable_http_debug = enable_http_debug # http-debug 只从环境变量获取
80+
self.http_debug = None
8081
# proxy provider两个: client env
81-
# self.http_proxy = http_proxy
82-
# self.https_proxy = https_proxy
83-
self.proxy = {
82+
self.http_proxy = http_proxy
83+
self.https_proxy = https_proxy
84+
self._proxy = {
8485
'http': http_proxy,
8586
'https': https_proxy,
8687
}
8788
self.enable_stream_logger = enable_stream_logger
8889
self.profile_name = profile_name
8990
self.config_file = config_file
91+
# retry
92+
self._auto_retry = auto_retry
93+
import aliyunsdkcore.retry.retry_policy as retry_policy
94+
if self._auto_retry:
95+
self.retry_policy = retry_policy.get_default_retry_policy(
96+
max_retry_times=self.max_retry_times)
97+
else:
98+
self.retry_policy = retry_policy.NO_RETRY_POLICY
9099

91100
def read_from_env(self):
92101
# 从环境变量读取一定量的数据
93-
pass
94-
# env_vars = ['HTTP_DEBUG', 'HTTPS_PROXY', 'HTTP_PROXY']
95-
# for item in env_vars:
96-
# if getattr(self, item.lower()) is None:
97-
# setattr(self, item.lower(), os.environ.get(item) or os.environ.get(item.lower()))
102+
env_vars = ['HTTP_DEBUG', 'HTTPS_PROXY', 'HTTP_PROXY']
103+
for item in env_vars:
104+
if getattr(self, item.lower()) is None:
105+
setattr(self, item.lower(), os.environ.get(item) or os.environ.get(item.lower()))
98106

99107
def read_from_profile(self):
100108
ENV_NAME_FOR_CONFIG_FILE = 'ALIBABA_CLOUD_CONFIG_FILE'
101109
DEFAULT_NAME_FOR_CONFIG_FILE = ['/etc/.alibabacloud/config',
102-
'~/.alibabacloud/config']
110+
'~/alibabacloud/config']
103111
# TODO read from profile
104112
from alibabacloud.utils.ini_helper import load_config
105113
profile = {}
@@ -194,8 +202,10 @@ def handle_request(self, api_request, request_handlers=None, context=None):
194202
# self.handle_request(api_request,
195203
# request_handlers=request_handlers[i:],
196204
# context=context)
205+
if context.exception:
206+
return context.exception
197207

198-
return context.response
208+
return context.http_response
199209

200210
def get_credentials(self):
201211
credentials_provider = self.credentials_provider({

aliyun-python-sdk-core/alibabacloud/credentials/credentials_provider.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ def __init__(self, profile):
275275
self.profile = profile
276276

277277
def load(self):
278-
print('static')
279278
if self.profile:
280279
fetcher = self._create_credentials_fetcher()
281280
credentials = fetcher()
@@ -314,7 +313,6 @@ def __init__(self):
314313
self.environ = os.environ
315314

316315
def load(self):
317-
print('env')
318316
if self.ENV_NAME_FOR_ACCESS_KEY_ID in self.environ:
319317
fetcher = self._create_credentials_fetcher()
320318
credentials = fetcher()
@@ -356,7 +354,6 @@ def __init__(self, profile_name=None):
356354
self.environ = os.environ
357355

358356
def load(self):
359-
print('profile')
360357
self._loaded_config = {}
361358

362359
if self.ENV_NAME_FOR_CREDENTIALS_FILE in self.environ:
1.7 KB
Binary file not shown.

aliyun-python-sdk-core/alibabacloud/handlers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def __init__(self):
2020
self.http_request = None
2121
self.api_response = None
2222
self.http_response = None
23+
self.exception = None
2324
self.retry_flag = False
2425
self.retry_backoff = 0
2526

aliyun-python-sdk-core/alibabacloud/handlers/http_handler.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ def handle_response(self, context):
6060
def do_request(context):
6161
http_request = context.http_request
6262
with Session() as s:
63-
print(111111111, http_request.protocol)
6463
current_protocol = 'http://' if http_request.protocol.lower() == 'http' else 'https://'
65-
print(2222222, context.endpoint)
6664
# TODO : 最终拼接的是啥,还需要调查下
6765
url = current_protocol + context.endpoint + http_request.url
6866

@@ -82,6 +80,5 @@ def do_request(context):
8280
response = s.send(prepped, proxies=http_request.proxy,
8381
timeout=http_request.timeout,
8482
allow_redirects=False, verify=None, cert=None)
85-
response.status_code
86-
context.response = response
83+
context.http_response = response
8784

aliyun-python-sdk-core/alibabacloud/handlers/prepare_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def handle_request(self, context):
118118
# http_request的method and protocol/proxy
119119
http_request.method = api_request.get_method()
120120
http_request.protocol = api_request.get_protocol_type() # http|https
121-
http_request.proxy = context.config.proxy # http|https
121+
http_request.proxy = context.config._proxy # http|https
122122
if http_request.protocol == 'https':
123123
http_request.port = 443
124124

aliyun-python-sdk-core/alibabacloud/handlers/retry_handler.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# limitations under the License.
1414

1515
import copy
16+
import time
17+
1618
from alibabacloud.handlers import RequestHandler
1719
import aliyunsdkcore.retry.retry_policy as retry_policy
1820
from aliyunsdkcore.retry.retry_condition import RetryCondition
@@ -24,33 +26,50 @@
2426

2527
class RetryHandler(RequestHandler):
2628

27-
def _add_request_client_token(self, api_request):
28-
# TODO implement: add a ClientToken parameter on api_request
29-
pass
29+
# def _add_request_client_token(self, api_request):
30+
# # TODO implement: add a ClientToken parameter on api_request
31+
# pass
3032

3133
def handle_request(self, context):
34+
print(1111111, context.http_request.retries)
3235
if context.http_request.retries == 0:
3336
retry_policy_context = RetryPolicyContext(context.api_request, None, 0, None)
34-
if context.client.retry_policy.should_retry(retry_policy_context) & \
37+
if context.config.retry_policy.should_retry(retry_policy_context) & \
3538
RetryCondition.SHOULD_RETRY_WITH_CLIENT_TOKEN:
3639
self._add_request_client_token(context.api_request)
3740

3841
def handle_response(self, context):
42+
print('context.exception', context.exception)
3943
api_request = context.api_request
4044
retry_policy_context = RetryPolicyContext(api_request, context.exception,
4145
context.http_request.retries,
4246
context.http_response.status_code)
4347

44-
should_retry = context.client.retry_policy.should_retry(retry_policy_context)
48+
should_retry = context.config.retry_policy.should_retry(retry_policy_context)
4549

4650
if should_retry & RetryCondition.SHOULD_RETRY:
51+
retry_policy_context.retryable = should_retry
52+
53+
time_to_sleep = context.config.retry_policy.compute_delay_before_next_retry(retry_policy_context)
54+
time.sleep(time_to_sleep / 1000.0)
55+
context.http_request.retries += 1
56+
4757
context.retry_flag = True
48-
context.retry_backoff = context.client.retry_policy.compute_delay_before_next_retry(
49-
retry_policy_context
50-
)
58+
# context.retry_backoff = context.config.retry_policy.compute_delay_before_next_retry(
59+
# retry_policy_context
60+
# )
5161
else:
5262
context.retry_flag = False
53-
context.retry_backoff = 0
63+
# context.retry_backoff = 0
5464
if context.exception:
5565
raise context.exception
56-
context.http_request.retries += 1
66+
67+
68+
@staticmethod
69+
def _add_request_client_token(request):
70+
if hasattr(request, "set_ClientToken") and hasattr(request, "get_ClientToken"):
71+
client_token = request.get_ClientToken()
72+
if not client_token:
73+
# ClientToken has not been set
74+
client_token = aliyunsdkcore.utils.parameter_helper.get_uuid() # up to 60 chars
75+
request.set_ClientToken(client_token)

aliyun-python-sdk-core/alibabacloud/handlers/server_error_handler.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ServerErrorHandler(RequestHandler):
2323
def handle_response(self, context):
2424
http_request = context.http_request
2525

26-
response = context.response
26+
response = context.http_response
2727
request_id = None
2828

2929
try:
@@ -34,7 +34,6 @@ def handle_response(self, context):
3434
# data instead
3535
# logger.warning('Failed to parse response as json format. Response:%s', response.text)
3636
pass
37-
print('进入而偶然')
3837
if response.status_code < codes.OK or response.status_code >= codes.MULTIPLE_CHOICES:
3938

4039
server_error_code, server_error_message = self._parse_error_info_from_response_body(

aliyun-python-sdk-core/alibabacloud/signer/bearer_token_signer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import logging
2323

2424
from alibabacloud.signer.signer import Signer
25-
25+
from aliyunsdkcore.auth.algorithm import bearer_token
2626
logger = logging.getLogger(__name__)
2727

2828

@@ -36,7 +36,7 @@ def sign(self, bear_token_credential, context):
3636
# which token
3737
if request.get_style() == 'RPC':
3838
request.add_query_param("BearerToken", token)
39-
request.get_url_params()
39+
request.get_url_params(signer=bearer_token)
4040
else:
4141
request.add_header("x-acs-bearer-token", token)
4242
signature = request.get_signed_signature(region_id, '')
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# coding=utf-8
19+
20+
21+
import hashlib
22+
import hmac
23+
24+
from aliyunsdkcore.compat import ensure_string
25+
from aliyunsdkcore.compat import ensure_bytes
26+
from aliyunsdkcore.compat import b64_encode_bytes
27+
28+
29+
def get_sign_string(source, secret):
30+
return ""
31+
32+
33+
def get_signer_name():
34+
return ""
35+
36+
37+
def get_signer_version():
38+
return "1.0"
39+
40+
41+
def get_signer_type():
42+
return "BEARERTOKEN"

0 commit comments

Comments
 (0)