Skip to content

Commit a50b171

Browse files
author
yikefan
committed
迁移赋值ak,sk,token的地方到验签,并且备注好所有的参数名字
1 parent 7877feb commit a50b171

File tree

8 files changed

+59
-26
lines changed

8 files changed

+59
-26
lines changed

SDK_Integration_zh.md

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ STS AssumeRole(Security Token Service)是火山引擎提供的临时访问
151151
支持`configuration`级别全局配置和接口级别的运行时参数设置`RuntimeOption`;`RuntimeOption`设置会覆盖`configuration`全局配置。
152152

153153
**代码示例:**
154+
154155
```python
155156
from __future__ import print_function
156157
import volcenginesdkcore
@@ -165,11 +166,16 @@ if __name__ == '__main__':
165166

166167
# 这里是使用STS ASSUMEROLE角色的方式
167168
configuration.credential_provider = StsCredentialProvider(
168-
ak="Your ak",
169-
sk="Your sk",
170-
role_name="Your role name",
171-
account_id="Your account id",
172-
region="cn-beijing"
169+
ak="Your ak", # 必填,子账号的ak
170+
sk="Your sk", # 必填,子账号的sk
171+
role_name="Your role name", # 必填,子账号的角色TRN,如trn:iam::2110400000:role/role123 ,此处填写role123
172+
account_id="Your account id", # 必填,子账号的角色TRN,如trn:iam::2110400000:role/role123 ,此处填写2110400000
173+
duration_seconds=3600, # 非必填,有效期默认3600秒
174+
scheme="https", # 非必填,域名前缀,默认https
175+
host="sts.volcengineapi.com", # 非必填,请求域名,默认sts.volcengineapi.com
176+
region="cn-beijing", # 非必填,请求服务器区域地址,默认cn-north-1
177+
timeout=30, # 非必填,请求超时时间,默认30秒
178+
expired_buffer_seconds=60 #非必填,session有效期前多久过期,剩余时间小于这个设置就要请求新的token了,默认60秒
173179
)
174180

175181
# set default configuration
@@ -205,6 +211,7 @@ STS AssumeRoleOIDC(Security Token Service)是火山引擎提供的临时访
205211
支持`configuration`级别全局配置和接口级别的运行时参数设置`RuntimeOption`;`RuntimeOption`设置会覆盖`configuration`全局配置。
206212

207213
**代码示例:**
214+
208215
```python
209216
# Example Code generated by Beijing Volcanoengine Technology.
210217
from __future__ import print_function
@@ -220,10 +227,15 @@ if __name__ == '__main__':
220227

221228
# 这里是使用STS ASSUMEROLE_OIDC角色的方式
222229
configuration.credential_provider = StsOidcCredentialProvider(
223-
role_name="your role name",
224-
account_id="your account id",
225-
oidc_token="your oidc token",
226-
region="cn-beijing"
230+
role_name="Your role name", # 必填,账号的角色TRN,如trn:iam::2110400000:role/role123 ,此处填写role123
231+
account_id="Your account id", # 必填,账号的角色TRN,如trn:iam::2110400000:role/role123 ,此处填写2110400000
232+
oidc_token="your oidc token", # 必填,生成的oidcToken,如ey********
233+
duration_seconds=3600, # 非必填,有效期默认3600秒
234+
scheme="https", # 非必填,域名前缀,默认https
235+
host="sts.volcengineapi.com", # 非必填,请求域名,默认sts.volcengineapi.com
236+
region="cn-beijing", # 非必填,请求服务器区域地址,默认cn-north-1
237+
timeout=30, # 非必填,请求超时时间,默认30秒
238+
expired_buffer_seconds=60 # 非必填,session有效期前多久过期,剩余时间小于这个设置就要请求新的token了,默认60秒
227239
)
228240

229241
# set default configuration
@@ -275,11 +287,16 @@ if __name__ == '__main__':
275287

276288
# 这里是使用STS ASSUMEROLE_SAML角色的方式
277289
configuration.credential_provider = StsSamlCredentialProvider(
278-
role_name="your role name",
279-
provider_name="your provider name",
280-
account_id="your account id",
281-
saml_resp="your saml resp",
282-
region="cn-beijing"
290+
role_name="Your role name", # 必填,账号的角色TRN,如trn:iam::2110400000:role/role123,此处填写role123
291+
account_id="Your account id", # 必填,账号的角色TRN,如trn:iam::2110400000:saml-provider/role123,此处填写2110400000
292+
provider_name="your provider name",# 必填,认证provider的TRN,如trn:iam::2110400000:saml-provider/provider123,此处填写provider123
293+
saml_resp="your saml resp", # 必填,认证获取到的SAML的断言
294+
duration_seconds=3600, # 非必填,有效期默认3600秒
295+
scheme="https", # 非必填,域名前缀,默认https
296+
host="sts.volcengineapi.com", # 非必填,请求域名,默认sts.volcengineapi.com
297+
region="cn-beijing", # 非必填,请求服务器区域地址,默认cn-north-1
298+
timeout=30, # 非必填,请求超时时间,默认30秒
299+
expired_buffer_seconds=60 # 非必填,session有效期前多久过期,剩余时间小于这个设置就要请求新的token了,默认60秒
283300
)
284301

285302
# set default configuration

volcenginesdkcore/api_client.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,6 @@ def __call_api(
115115
if self.cookie:
116116
header_params['Cookie'] = self.cookie
117117

118-
# 新增代码。处理assume_role和assume_role_oidc和assume_role_saml
119-
if self.configuration.credential_provider is not None:
120-
self.configuration.credential_provider.refresh() # 这会调用 _assume_role_oidc() 方法获取临时凭证
121-
credentials = self.configuration.credential_provider.retrieve()
122-
self.configuration.ak = credentials.ak
123-
self.configuration.sk = credentials.sk
124-
self.configuration.session_token = credentials.session_token
125-
126118
interceptor_context = InterceptorContext(request=Request(
127119
self.configuration,
128120
resource_path, method, path_params,

volcenginesdkcore/auth/providers/provider.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ def is_expired(self):
2323
@abc.abstractmethod
2424
def refresh(self):
2525
raise NotImplementedError()
26+
27+
@abc.abstractmethod
28+
def get_credentials(self):
29+
raise NotImplementedError()

volcenginesdkcore/auth/providers/sts_oidc_provider.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def __init__(self, ak, sk, session_token, current_time, expired_time):
1919

2020

2121
class StsOidcCredentialProvider(Provider):
22-
def __init__(self, role_name, oidc_token, account_id, duration_seconds=3600, scheme='https',
23-
host='sts.volcengineapi.com', region='cn-north-1', timeout=30, expired_buffer_seconds=60):
22+
def __init__(self, role_name, account_id, oidc_token, duration_seconds=3600, scheme='https',
23+
host='sts.volcengineapi.com', region='cn-beijing', timeout=30, expired_buffer_seconds=60):
2424

2525
self.role_name = role_name
2626
self.account_id = account_id
@@ -54,6 +54,10 @@ def refresh(self):
5454
if self.is_expired():
5555
self._assume_role_oidc()
5656

57+
def get_credentials(self):
58+
self.refresh()
59+
return self.credentials
60+
5761
def _assume_role_oidc(self):
5862
params = {
5963
'DurationSeconds': self.duration_seconds,

volcenginesdkcore/auth/providers/sts_provider.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def refresh(self):
5454
if self.is_expired():
5555
self._assume_role()
5656

57+
def get_credentials(self):
58+
self.refresh()
59+
return self.credentials
60+
5761
def _assume_role(self):
5862
params = {
5963
'DurationSeconds': self.duration_seconds,

volcenginesdkcore/auth/providers/sts_saml_provider.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def __init__(self, ak, sk, session_token, current_time, expired_time):
1919

2020

2121
class StsSamlCredentialProvider(Provider):
22-
def __init__(self, role_name, provider_name, account_id, saml_resp, duration_seconds=3600, scheme='https',
23-
host='sts.volcengineapi.com', region='cn-north-1', timeout=30, expired_buffer_seconds=60):
22+
def __init__(self, role_name, account_id, provider_name, saml_resp, duration_seconds=3600, scheme='https',
23+
host='sts.volcengineapi.com', region='cn-beijing', timeout=30, expired_buffer_seconds=60):
2424
# self.ak = ak
2525
# self.sk = sk
2626
self.role_name = role_name
@@ -56,6 +56,10 @@ def refresh(self):
5656
if self.is_expired():
5757
self._assume_role_saml()
5858

59+
def get_credentials(self):
60+
self.refresh()
61+
return self.credentials
62+
5963
def _assume_role_saml(self):
6064
params = {
6165
'DurationSeconds': self.duration_seconds,

volcenginesdkcore/interceptor/interceptors/request.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def __init__(
6161
# retryer setting, default use global configration if value not set
6262
self.auto_retry = configuration.auto_retry
6363
self.retryer = configuration.retryer
64+
self.credential_provider = configuration.credential_provider
6465

6566
self.runtime_options = None
6667
if hasattr(body, '_configuration') and isinstance(body._configuration, RuntimeOption):

volcenginesdkcore/interceptor/interceptors/sign_request_interceptor.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ def name(self):
1010
return 'volcengine-sign-request-interceptor'
1111

1212
def intercept(self, context):
13+
# 新增代码。处理assume_role和assume_role_oidc和assume_role_saml
14+
if context.request.credential_provider is not None:
15+
credentials = context.request.credential_provider.get_credentials() # 这会调用 _assume_role_oidc() 方法获取临时凭证
16+
context.request.ak = credentials.ak
17+
context.request.sk = credentials.sk
18+
context.request.session_token = credentials.session_token
19+
1320
self.update_params_for_auth(host=context.request.host, path=context.request.true_path,
1421
method=context.request.method,
1522
headers=context.request.header_params,

0 commit comments

Comments
 (0)