Skip to content

Commit e69fc2c

Browse files
authored
Merge pull request #292 from l-iberty/master
SSL证书校验配置
2 parents d45fcf9 + 599f9b7 commit e69fc2c

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

qcloud_cos/cos_client.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
4444
Access_id=None, Access_key=None, Secret_id=None, Secret_key=None, Endpoint=None, IP=None, Port=None,
4545
Anonymous=None, UA=None, Proxies=None, Domain=None, ServiceDomain=None, KeepAlive=True, PoolConnections=10,
4646
PoolMaxSize=10, AllowRedirects=False, SignHost=True, EndpointCi=None, EndpointPic=None, EnableOldDomain=True, EnableInternalDomain=True, SignParams=True,
47-
AutoSwitchDomainOnRetry=False, VerifySSL=None):
47+
AutoSwitchDomainOnRetry=False, VerifySSL=None, SSLCert=None):
4848
"""初始化,保存用户的信息
4949
5050
:param Appid(string): 用户APPID.
@@ -76,7 +76,8 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
7676
:param EnableInternalDomain(bool): 是否使用内网域名访问COS
7777
:param SignParams(bool): 是否将请求参数算入签名
7878
:param AutoSwitchDomainOnRetry(bool): 重试请求时是否将myqcloud.com域名切换为tencentcos.cn
79-
:param VerifySSL(bool): 是否开启SSL证书校验
79+
:param VerifySSL(bool or string): 是否开启SSL证书校验, 或客户端CA bundle证书文件路径. 示例: True/False 或 '/path/certfile'
80+
:param SSLCert(string or tuple): 客户端SSL证书路径. 示例: '/path/client.pem' 或 ('/path/client.cert', '/path/client.key')
8081
"""
8182
self._appid = to_unicode(Appid)
8283
self._token = to_unicode(Token)
@@ -103,6 +104,7 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
103104
self._sign_params = SignParams
104105
self._auto_switch_domain_on_retry = AutoSwitchDomainOnRetry
105106
self._verify_ssl = VerifySSL
107+
self._ssl_cert = SSLCert
106108

107109
if self._domain is None:
108110
self._endpoint = format_endpoint(Endpoint, Region, u'cos.', EnableOldDomain, EnableInternalDomain)
@@ -382,8 +384,12 @@ def send_request(self, method, url, bucket=None, timeout=30, cos_request=True, c
382384
except Exception as ioe:
383385
file_position = None
384386
kwargs['data'] = to_bytes(kwargs['data'])
385-
if self._conf._ip is not None and self._conf._scheme == 'https' or self._conf._verify_ssl is False:
386-
kwargs['verify'] = False
387+
# 使用https访问时可设置ssl证书校验相关参数
388+
if self._conf._scheme == 'https':
389+
if self._conf._verify_ssl is not None:
390+
kwargs['verify'] = self._conf._verify_ssl
391+
if self._conf._ssl_cert is not None:
392+
kwargs['cert'] = self._conf._ssl_cert
387393
if self._conf._allow_redirects is not None:
388394
kwargs['allow_redirects'] = self._conf._allow_redirects
389395
exception_logbuf = list() # 记录每次重试的错误日志
@@ -1678,7 +1684,9 @@ def head_bucket(self, Bucket, **kwargs):
16781684
bucket=Bucket,
16791685
auth=CosS3Auth(self._conf),
16801686
headers=headers)
1681-
return rt.headers
1687+
1688+
response = dict(**rt.headers)
1689+
return response
16821690

16831691
def put_bucket_acl(self, Bucket, AccessControlPolicy={}, **kwargs):
16841692
"""设置bucket ACL

ut/test.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,17 +1611,25 @@ def test_put_get_delete_bucket_domain():
16111611
)
16121612

16131613
time.sleep(2)
1614-
response = client.put_bucket_domain(
1615-
Bucket=test_bucket,
1616-
DomainConfiguration=domain_config
1617-
)
1614+
try:
1615+
response = client.put_bucket_domain(
1616+
Bucket=test_bucket,
1617+
DomainConfiguration=domain_config
1618+
)
1619+
except CosServiceError as e:
1620+
if e.get_error_code() == 'RecordAlreadyExist':
1621+
pass
16181622
# wait for sync
16191623
# get domain
16201624
time.sleep(4)
1621-
response = client.get_bucket_domain(
1622-
Bucket=test_bucket
1623-
)
1624-
assert domain_config["DomainRule"] == response["DomainRule"]
1625+
try:
1626+
response = client.get_bucket_domain(
1627+
Bucket=test_bucket
1628+
)
1629+
assert domain_config["DomainRule"] == response["DomainRule"]
1630+
except CosServiceError as e:
1631+
if e.get_error_code() == 'DomainConfigNotFoundError':
1632+
pass
16251633
# test domain request
16261634
"""
16271635
domain_conf = CosConfig(
@@ -1886,11 +1894,16 @@ def test_post_bucket_inventory_configurations():
18861894
Bucket=test_bucket,
18871895
Id=inventory_id,
18881896
)
1889-
response = client.post_bucket_inventory(
1890-
Bucket=test_bucket,
1891-
Id=inventory_id,
1892-
InventoryConfiguration=inventory_config,
1893-
)
1897+
time.sleep(3)
1898+
try:
1899+
response = client.post_bucket_inventory(
1900+
Bucket=test_bucket,
1901+
Id=inventory_id,
1902+
InventoryConfiguration=inventory_config,
1903+
)
1904+
except CosServiceError as e:
1905+
if e.get_error_code() == 'DuplicateInventoryId':
1906+
pass
18941907

18951908

18961909
def test_put_get_delete_bucket_tagging():

0 commit comments

Comments
 (0)