Skip to content

Commit 4721dec

Browse files
authored
Merge pull request #303 from daozzg/master
add script for update the ca of cdn
2 parents 69ab131 + 511d56d commit 4721dec

File tree

4 files changed

+157
-1
lines changed

4 files changed

+157
-1
lines changed

examples/update_cdn_sslcert.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
更新cdn证书(可配合let's encrypt 等完成自动证书更新)
4+
"""
5+
import qiniu
6+
from qiniu import DomainManager
7+
8+
# 账户ak,sk
9+
access_key = ''
10+
secret_key = ''
11+
12+
auth = qiniu.Auth(access_key=access_key, secret_key=secret_key)
13+
domain_manager = DomainManager(auth)
14+
15+
privatekey = "ssl/www.qiniu.com/privkey.pem"
16+
ca = "ssl/www.qiniu.com/fullchain.pem"
17+
domain_name='www.qiniu.com'
18+
19+
with open(privatekey,'r') as f:
20+
privatekey_str=f.read()
21+
22+
with open(ca,'r') as f:
23+
ca_str=f.read()
24+
25+
ret, info = domain_manager.create_sslcert(domain_name, domain_name, privatekey_str, ca_str)
26+
print(ret['certID'])
27+
28+
ret, info = domain_manager.put_httpsconf(domain_name, ret['certID'],False)
29+
print(info)

qiniu/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from .services.storage.bucket import BucketManager, build_batch_copy, build_batch_rename, build_batch_move, \
2020
build_batch_stat, build_batch_delete
2121
from .services.storage.uploader import put_data, put_file, put_stream
22-
from .services.cdn.manager import CdnManager, create_timestamp_anti_leech_url
22+
from .services.cdn.manager import CdnManager, create_timestamp_anti_leech_url,DomainManager
2323
from .services.processing.pfop import PersistentFop
2424
from .services.processing.cmd import build_op, pipe_cmd, op_save
2525
from .services.compute.app import AccountClient

qiniu/http.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ def _post(url, data, files, auth, headers=None):
5151
return None, ResponseInfo(None, e)
5252
return __return_wrapper(r)
5353

54+
def _put(url, data, files, auth, headers=None):
55+
if _session is None:
56+
_init()
57+
try:
58+
post_headers = _headers.copy()
59+
if headers is not None:
60+
for k, v in headers.items():
61+
post_headers.update({k: v})
62+
r = _session.put(
63+
url, data=data, files=files, auth=auth, headers=post_headers,
64+
timeout=config.get_default('connection_timeout'))
65+
except Exception as e:
66+
return None, ResponseInfo(None, e)
67+
return __return_wrapper(r)
5468

5569
def _get(url, params, auth):
5670
try:
@@ -86,6 +100,12 @@ def _post_with_auth(url, data, auth):
86100
def _post_with_auth_and_headers(url, data, auth, headers):
87101
return _post(url, data, None, qiniu.auth.RequestsAuth(auth), headers)
88102

103+
def _put_with_auth(url, data, auth):
104+
return _put(url, data, None, qiniu.auth.RequestsAuth(auth))
105+
106+
def _put_with_auth_and_headers(url, data, auth, headers):
107+
return _put(url, data, None, qiniu.auth.RequestsAuth(auth), headers)
108+
89109

90110
def _post_with_qiniu_mac(url, data, auth):
91111
qn_auth = qiniu.auth.QiniuMacRequestsAuth(auth) if auth is not None else None

qiniu/services/cdn/manager.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,117 @@ def get_log_list_data(self, domains, log_date):
157157
url = '{0}/v2/tune/log/list'.format(self.server)
158158
return self.__post(url, body)
159159

160+
def put_httpsconf(self, name, certid, forceHttps=False):
161+
"""
162+
修改证书,文档 https://developer.qiniu.com/fusion/api/4246/the-domain-name#11
163+
164+
Args:
165+
domains: 域名name
166+
CertID: 证书id,从上传或者获取证书列表里拿到证书id
167+
ForceHttps: 是否强制https跳转
168+
169+
Returns:
170+
{}
171+
"""
172+
req = {}
173+
req.update({"certid": certid})
174+
req.update({"forceHttps": forceHttps})
175+
176+
body = json.dumps(req)
177+
url = '{0}/domain/{1}/httpsconf'.format(self.server, name)
178+
return self.__post(url, body)
179+
180+
def __post(self, url, data=None):
181+
headers = {'Content-Type': 'application/json'}
182+
return http._post_with_auth_and_headers(url, data, self.auth, headers)
183+
184+
185+
class DomainManager(object):
186+
def __init__(self, auth):
187+
self.auth = auth
188+
self.server = 'http://api.qiniu.com'
189+
190+
def create_domain(self, name, body):
191+
"""
192+
创建域名,文档 https://developer.qiniu.com/fusion/api/4246/the-domain-name
193+
194+
Args:
195+
name: 域名, 如果是泛域名,必须以点号 . 开头
196+
bosy: 创建域名参数
197+
Returns:
198+
{}
199+
"""
200+
url = '{0}/domain/{1}'.format(self.server, name)
201+
return self.__post(url, body)
202+
203+
def get_domain(self, name):
204+
"""
205+
获取域名信息,文档 https://developer.qiniu.com/fusion/api/4246/the-domain-name
206+
207+
Args:
208+
name: 域名, 如果是泛域名,必须以点号 . 开头
209+
Returns:
210+
返回一个tuple对象,其格式为(<result>, <ResponseInfo>)
211+
- result 成功返回dict{},失败返回{"error": "<errMsg string>"}
212+
- ResponseInfo 请求的Response信息
213+
"""
214+
url = '{0}/domain/{1}'.format(self.server, name)
215+
return self.__post(url)
216+
217+
def put_httpsconf(self, name, certid, forceHttps):
218+
"""
219+
修改证书,文档 https://developer.qiniu.com/fusion/api/4246/the-domain-name#11
220+
221+
Args:
222+
domains: 域名name
223+
CertID: 证书id,从上传或者获取证书列表里拿到证书id
224+
ForceHttps: 是否强制https跳转
225+
226+
Returns:
227+
{}
228+
"""
229+
req = {}
230+
req.update({"certid": certid})
231+
req.update({"forceHttps": forceHttps})
232+
233+
body = json.dumps(req)
234+
url = '{0}/domain/{1}/httpsconf'.format(self.server, name)
235+
return self.__put(url, body)
236+
237+
def create_sslcert(self, name, common_name, pri, ca):
238+
"""
239+
修改证书,文档 https://developer.qiniu.com/fusion/api/4246/the-domain-name#11
240+
241+
Args:
242+
name: 证书名称
243+
common_name: 相关域名
244+
pri: 证书私钥
245+
ca: 证书内容
246+
Returns:
247+
返回一个tuple对象,其格式为(<result>, <ResponseInfo>)
248+
- result 成功返回dict{certID: <CertID>},失败返回{"error": "<errMsg string>"}
249+
- ResponseInfo 请求的Response信息
250+
251+
252+
"""
253+
req = {}
254+
req.update({"name": name})
255+
req.update({"common_name": common_name})
256+
req.update({"pri": pri})
257+
req.update({"ca": ca})
258+
259+
body = json.dumps(req)
260+
url = '{0}/sslcert'.format(self.server)
261+
return self.__post(url, body)
262+
160263
def __post(self, url, data=None):
161264
headers = {'Content-Type': 'application/json'}
162265
return http._post_with_auth_and_headers(url, data, self.auth, headers)
163266

267+
def __put(self, url, data=None):
268+
headers = {'Content-Type': 'application/json'}
269+
return http._put_with_auth_and_headers(url, data, self.auth, headers)
270+
164271

165272
def create_timestamp_anti_leech_url(host, file_name, query_string, encrypt_key, deadline):
166273
"""

0 commit comments

Comments
 (0)