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'
|
2020-12-20 16:32:17 +00:00
|
|
|
|
export class DeployCertToAliyunCDN extends AbstractAliyunPlugin {
|
2020-12-18 17:57:52 +00:00
|
|
|
|
/**
|
|
|
|
|
* 插件定义
|
|
|
|
|
* 名称
|
|
|
|
|
* 入参
|
|
|
|
|
* 出参
|
|
|
|
|
*/
|
|
|
|
|
static define () {
|
|
|
|
|
return {
|
2020-12-20 16:32:17 +00:00
|
|
|
|
name: 'deployCertToAliyunCDN',
|
2020-12-18 17:57:52 +00:00
|
|
|
|
label: '部署到阿里云CDN',
|
|
|
|
|
input: {
|
|
|
|
|
domainName: {
|
|
|
|
|
label: 'cdn加速域名',
|
|
|
|
|
required: true
|
|
|
|
|
},
|
|
|
|
|
certName: {
|
|
|
|
|
label: '证书名称'
|
|
|
|
|
},
|
2020-12-25 17:37:53 +00:00
|
|
|
|
from: {
|
2020-12-20 16:32:17 +00:00
|
|
|
|
value: 'upload',
|
2020-12-18 17:57:52 +00:00
|
|
|
|
label: '证书来源',
|
|
|
|
|
options: [
|
|
|
|
|
{ value: 'upload', label: '直接上传' },
|
2020-12-20 16:32:17 +00:00
|
|
|
|
{ value: 'cas', label: '从证书库', desc: '需要uploadCertToAliyun作为前置任务' }
|
2020-12-18 17:57:52 +00:00
|
|
|
|
],
|
|
|
|
|
required: true
|
|
|
|
|
},
|
|
|
|
|
// 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的对象',
|
|
|
|
|
options: 'accessProviders[type=aliyun]',
|
|
|
|
|
required: true
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
output: {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 17:37:53 +00:00
|
|
|
|
async execute ({ accessProviders, cert, props, context }) {
|
|
|
|
|
const accessProvider = this.getAccessProvider(props.accessProvider, accessProviders)
|
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,
|
2020-12-20 16:32:17 +00:00
|
|
|
|
ServerCertificate: super.format(cert.crt.toString()),
|
|
|
|
|
PrivateKey: super.format(cert.key.toString())
|
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
|
|
|
|
}
|
|
|
|
|
}
|