2020-12-20 16:32:17 +00:00
|
|
|
|
import { AbstractAliyunPlugin } from '../../aliyun/abstract-aliyun.js'
|
2020-12-18 17:57:52 +00:00
|
|
|
|
import Core from '@alicloud/pop-core'
|
|
|
|
|
import dayjs from 'dayjs'
|
|
|
|
|
|
2021-01-21 15:59:06 +00:00
|
|
|
|
const define = {
|
|
|
|
|
name: 'deployCertToAliyunCDN',
|
|
|
|
|
label: '部署到阿里云CDN',
|
|
|
|
|
input: {
|
|
|
|
|
domainName: {
|
|
|
|
|
label: 'cdn加速域名',
|
2021-01-30 18:09:54 +00:00
|
|
|
|
component: {
|
2021-01-29 16:06:50 +00:00
|
|
|
|
placeholder: 'cdn加速域名',
|
|
|
|
|
rules: [{ required: true, message: '该项必填' }]
|
|
|
|
|
}
|
2021-01-21 15:59:06 +00:00
|
|
|
|
},
|
|
|
|
|
certName: {
|
2021-01-30 18:09:54 +00:00
|
|
|
|
label: '证书名称',
|
|
|
|
|
component: {
|
|
|
|
|
placeholder: '上传后将以此名称作为前缀'
|
|
|
|
|
}
|
2021-01-21 15:59:06 +00:00
|
|
|
|
},
|
|
|
|
|
from: {
|
|
|
|
|
value: 'upload',
|
|
|
|
|
label: '证书来源',
|
2021-01-30 18:09:54 +00:00
|
|
|
|
component: {
|
|
|
|
|
placeholder: '证书来源',
|
|
|
|
|
required: true,
|
|
|
|
|
name: 'a-select',
|
|
|
|
|
options: [
|
|
|
|
|
{ value: 'upload', label: '直接上传' },
|
|
|
|
|
{ value: 'cas', label: '从证书库', title: '需要uploadCertToAliyun作为前置任务' }
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
desc: '如果选择cas类型,则需要以《上传证书到阿里云》作为前置任务'
|
|
|
|
|
|
2021-01-21 15:59:06 +00:00
|
|
|
|
},
|
|
|
|
|
// serverCertificateStatus: {
|
|
|
|
|
// label: '启用https',
|
|
|
|
|
// options: [
|
|
|
|
|
// { value: 'on', label: '开启HTTPS,并更新证书' },
|
|
|
|
|
// { value: 'auto', label: '若HTTPS开启则更新,未开启不更新' }
|
|
|
|
|
// ],
|
|
|
|
|
// required:true
|
|
|
|
|
// },
|
|
|
|
|
accessProvider: {
|
|
|
|
|
label: 'Access提供者',
|
|
|
|
|
type: [String, Object],
|
|
|
|
|
desc: 'AccessProviders的key 或 一个包含accessKeyId与accessKeySecret的对象',
|
2021-01-29 16:06:50 +00:00
|
|
|
|
component: {
|
|
|
|
|
name: 'provider-selector',
|
|
|
|
|
filter: 'aliyun'
|
|
|
|
|
},
|
2021-01-21 15:59:06 +00:00
|
|
|
|
required: true
|
2020-12-18 17:57:52 +00:00
|
|
|
|
}
|
2021-01-21 15:59:06 +00:00
|
|
|
|
},
|
|
|
|
|
output: {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class DeployCertToAliyunCDN extends AbstractAliyunPlugin {
|
|
|
|
|
static define () {
|
|
|
|
|
return define
|
2020-12-18 17:57:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 18:30:34 +00:00
|
|
|
|
async execute ({ cert, props, context }) {
|
|
|
|
|
const accessProvider = this.getAccessProvider(props.accessProvider)
|
2020-12-18 18:00:46 +00:00
|
|
|
|
const client = this.getClient(accessProvider)
|
2020-12-25 17:37:53 +00:00
|
|
|
|
const params = this.buildParams(props, context, cert)
|
2020-12-18 18:00:46 +00:00
|
|
|
|
await this.doRequest(client, params)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-18 17:57:52 +00:00
|
|
|
|
getClient (aliyunProvider) {
|
|
|
|
|
return new Core({
|
|
|
|
|
accessKeyId: aliyunProvider.accessKeyId,
|
|
|
|
|
accessKeySecret: aliyunProvider.accessKeySecret,
|
|
|
|
|
endpoint: 'https://cdn.aliyuncs.com',
|
|
|
|
|
apiVersion: '2018-05-10'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-18 18:00:46 +00:00
|
|
|
|
buildParams (args, context, cert) {
|
2020-12-25 17:37:53 +00:00
|
|
|
|
const { certName, from, domainName } = args
|
2020-12-18 17:57:52 +00:00
|
|
|
|
const CertName = certName + '-' + dayjs().format('YYYYMMDDHHmmss')
|
|
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
|
RegionId: 'cn-hangzhou',
|
|
|
|
|
DomainName: domainName,
|
|
|
|
|
ServerCertificateStatus: 'on',
|
|
|
|
|
CertName: CertName,
|
2020-12-25 17:37:53 +00:00
|
|
|
|
CertType: from,
|
2021-01-02 18:30:34 +00:00
|
|
|
|
ServerCertificate: cert.crt,
|
|
|
|
|
PrivateKey: cert.key
|
2020-12-18 17:57:52 +00:00
|
|
|
|
}
|
2020-12-18 18:00:46 +00:00
|
|
|
|
return params
|
|
|
|
|
}
|
2020-12-18 17:57:52 +00:00
|
|
|
|
|
2020-12-18 18:00:46 +00:00
|
|
|
|
async doRequest (client, params) {
|
2020-12-18 17:57:52 +00:00
|
|
|
|
const requestOption = {
|
|
|
|
|
method: 'POST'
|
|
|
|
|
}
|
|
|
|
|
const ret = await client.request('SetDomainServerCertificate', params, requestOption)
|
2020-12-18 18:00:46 +00:00
|
|
|
|
this.checkRet(ret)
|
2020-12-20 16:32:17 +00:00
|
|
|
|
this.logger.info('设置cdn证书成功:', ret.RequestId)
|
2020-12-18 17:57:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|