certd/packages/plugins/src/aliyun/deploy-to-cdn/index.js

98 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-12-18 17:57:52 +00:00
import { AbstractPlugin } from '../../abstract-plugin/index.js'
import Core from '@alicloud/pop-core'
import dayjs from 'dayjs'
export class UploadCertToAliyunPlugin extends AbstractPlugin {
/**
* 插件定义
* 名称
* 入参
* 出参
*/
static define () {
return {
name: 'deployToCdn',
label: '部署到阿里云CDN',
input: {
domainName: {
label: 'cdn加速域名',
required: true
},
certName: {
label: '证书名称'
},
certType: {
label: '证书来源',
options: [
{ value: 'upload', label: '直接上传' },
{ value: 'cas', label: '从证书库需要uploadCertToAliyun插件作为前置任务' }
],
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-18 18:00:46 +00:00
async execute ({ accessProviders, cert, args, context }) {
const accessProvider = this.getAccessProvider(args.accessProvider, accessProviders)
const client = this.getClient(accessProvider)
const params = this.buildParams(args, context, cert)
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-18 17:57:52 +00:00
const { certName, certType, domainName } = args
const CertName = certName + '-' + dayjs().format('YYYYMMDDHHmmss')
const params = {
RegionId: 'cn-hangzhou',
DomainName: domainName,
ServerCertificateStatus: 'on',
CertName: CertName,
CertType: certType,
ServerCertificate: context.aliyunCertId
}
if (certType === 'upload') {
// eslint-disable-next-line no-unused-expressions
params.ServerCertificate = this.format(cert.crt.toString()),
params.PrivateKey = this.format(cert.key.toString())
}
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-18 17:57:52 +00:00
console.log('设置cdn证书成功', ret)
}
}