Files
certd/packages/plugins/src/tencent/upload-to-tencent/index.js
T

92 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-12-26 01:37:53 +08:00
import dayjs from 'dayjs'
import tencentcloud from 'tencentcloud-sdk-nodejs'
import { AbstractTencentPlugin } from '../abstract-tencent.js'
export class UploadCertToTencent extends AbstractTencentPlugin {
/**
* 插件定义
* 名称
* 入参
* 出参
*/
static define () {
return {
name: 'uploadCertToTencent',
label: '上传证书到腾讯云',
input: {
name: {
label: '证书名称'
},
accessProvider: {
2021-02-04 22:07:01 +08:00
label: 'Access授权',
2020-12-26 01:37:53 +08:00
type: [String, Object],
2021-02-04 22:07:01 +08:00
desc: 'access授权',
component: {
name: 'access-provider-selector',
filter: 'tencent'
},
required: true
2020-12-26 01:37:53 +08:00
}
},
output: {
tencentCertId: {
type: String,
desc: '上传成功后的腾讯云CertId'
}
}
}
}
getClient (accessProvider) {
const SslClient = tencentcloud.ssl.v20191205.Client
const clientConfig = {
credential: {
secretId: accessProvider.secretId,
secretKey: accessProvider.secretKey
},
region: '',
profile: {
httpProfile: {
endpoint: 'ssl.tencentcloudapi.com'
}
}
}
return new SslClient(clientConfig)
}
async execute ({ cert, props, context, logger }) {
2020-12-26 01:37:53 +08:00
const { name, accessProvider } = props
2021-01-08 15:39:23 +08:00
const certName = this.appendTimeSuffix(name || cert.domain)
2020-12-26 01:37:53 +08:00
const provider = this.getAccessProvider(accessProvider)
2020-12-26 01:37:53 +08:00
const client = this.getClient(provider)
const params = {
CertificatePublicKey: cert.crt,
CertificatePrivateKey: cert.key,
2020-12-26 01:37:53 +08:00
Alias: certName
}
const ret = await client.UploadCertificate(params)
this.checkRet(ret)
this.logger.info('证书上传成功:tencentCertId=', ret.CertificateId)
context.tencentCertId = ret.CertificateId
}
async rollback ({ cert, props, context }) {
2020-12-26 01:37:53 +08:00
const { accessProvider } = props
const provider = super.getAccessProvider(accessProvider)
2020-12-26 01:37:53 +08:00
const client = this.getClient(provider)
const { tencentCertId } = context
const params = {
CertificateId: tencentCertId
}
const ret = await client.DeleteCertificate(params)
this.checkRet(ret)
this.logger.info('证书删除成功:DeleteResult=', ret.DeleteResult)
delete context.tencentCertId
}
}