Skip to content

Commit

Permalink
优化参数
Browse files Browse the repository at this point in the history
  • Loading branch information
mouday committed Jul 24, 2024
1 parent 4b32044 commit 88441b7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
12 changes: 1 addition & 11 deletions domain_admin/api/issue_certificate_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,31 +373,21 @@ def notify_web_hook():

def deploy_cert_to_oss():
"""
部署证书到oss
部署证书到阿里云oss
:return:
"""
issue_certificate_id = request.json['issue_certificate_id']
bucket_name = request.json['bucket_name']
endpoint = request.json.get['endpoint']
dns_id = request.json.get['dns_id']

ret = issue_certificate_service.deploy_cert_to_oss(
issue_certificate_id=issue_certificate_id,
bucket_name=bucket_name,
endpoint=endpoint,
dns_id=dns_id,
)

deploy_params = {
'endpoint': endpoint,
'bucket_name': bucket_name,
}

# 更新验证信息
IssueCertificateModel.update(
deploy_type_id=SSLDeployTypeEnum.OSS,
deploy_host_id=dns_id,
deploy_params_raw=json.dumps(deploy_params),
ssl_deploy_status=DeployStatusEnum.SUCCESS
).where(
IssueCertificateModel.id == issue_certificate_id
Expand Down
14 changes: 8 additions & 6 deletions domain_admin/service/issue_certificate_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,6 @@ def renew_certificate_row(row):
elif row.deploy_type_id == SSLDeployTypeEnum.OSS:
deploy_cert_to_oss(
issue_certificate_id=row.id,
bucket_name=row.deploy_params['bucket_name'],
endpoint=row.deploy_params['bucket_name'],
dns_id=row.deploy_host_id
)

Expand Down Expand Up @@ -557,7 +555,7 @@ def deploy_ssl_by_web_hook(issue_certificate_id, url, headers):
return res.text


def deploy_cert_to_oss(issue_certificate_id, bucket_name, endpoint, dns_id):
def deploy_cert_to_oss(issue_certificate_id, dns_id):
"""
部署ssl证书到oss
"""
Expand All @@ -570,14 +568,18 @@ def deploy_cert_to_oss(issue_certificate_id, bucket_name, endpoint, dns_id):
if not dns_row:
raise AppException('DNS数据不存在')

domain = issue_certificate_row.domains[0]

oss_info = aliyun_oss_api.cname_to_oss_info(domain)

aliyun_oss_api.put_bucket_cname(
access_key_id=dns_row.access_key,
access_key_secret=dns_row.secret_key,
bucket_name=bucket_name,
domain=issue_certificate_row.domains[0],
bucket_name=oss_info['bucket_name'],
domain=domain,
certificate=issue_certificate_row.ssl_certificate,
private_key=issue_certificate_row.ssl_certificate_key,
endpoint=endpoint,
endpoint=oss_info['endpoint'],
)


Expand Down
19 changes: 17 additions & 2 deletions domain_admin/utils/open_api/aliyun_oss_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from oss2.credentials import EnvironmentVariableCredentialsProvider, StaticCredentialsProvider

# https://next.api.aliyun.com/product/Oss
from domain_admin.utils import domain_util

ENDPOINT_OPTIONS = [
{
'label': '华北1(青岛)',
Expand Down Expand Up @@ -67,14 +69,26 @@ def get_endpoint_by_value(value):
return item['endpoint']


def cname_to_oss_info(cname):
"""
'zaiting.oss-cn-beijing.aliyuncs.com.'
:param cname:
:return: zaiting oss-cn-beijing.aliyuncs.com
"""
return {
'bucket_name': cname.split('.')[0],
'endpoint': 'https://' + domain_util.get_domain_parent(cname).strip('.')
}


def put_bucket_cname(
access_key_id,
access_key_secret,
bucket_name,
domain,
certificate,
private_key,
endpoint='cn-beijing',
endpoint='https://oss-cn-beijing.aliyuncs.com',
):
"""
将证书部署到oss
Expand All @@ -96,7 +110,8 @@ def put_bucket_cname(
# 填写Bucket名称,例如examplebucket。
bucket = oss2.Bucket(
auth=auth,
endpoint=get_endpoint_by_value(endpoint),
# https://oss-cn-beijing.aliyuncs.com
endpoint=endpoint,
bucket_name=bucket_name
)

Expand Down

0 comments on commit 88441b7

Please sign in to comment.