2024-08-13 20:30:42 +08:00
|
|
|
import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput, TaskOutput } from '@certd/pipeline';
|
2024-05-27 18:38:41 +08:00
|
|
|
import dayjs from 'dayjs';
|
2022-11-07 23:31:20 +08:00
|
|
|
|
2022-12-29 23:52:51 +08:00
|
|
|
@IsTaskPlugin({
|
2024-05-27 18:38:41 +08:00
|
|
|
name: 'UploadCertToTencent',
|
|
|
|
|
title: '上传证书到腾讯云',
|
2024-09-19 17:38:51 +08:00
|
|
|
icon: 'svg:icon-tencentcloud',
|
2024-05-27 18:38:41 +08:00
|
|
|
desc: '上传成功后输出:tencentCertId',
|
2024-07-21 02:26:03 +08:00
|
|
|
group: pluginGroups.tencent.key,
|
2022-12-29 23:52:51 +08:00
|
|
|
default: {
|
|
|
|
|
strategy: {
|
|
|
|
|
runStrategy: RunStrategy.SkipWhenSucceed,
|
2022-11-07 23:31:20 +08:00
|
|
|
},
|
2022-12-29 23:52:51 +08:00
|
|
|
},
|
|
|
|
|
})
|
2023-05-24 15:41:35 +08:00
|
|
|
export class UploadToTencentPlugin extends AbstractTaskPlugin {
|
2024-05-27 18:38:41 +08:00
|
|
|
@TaskInput({ title: '证书名称' })
|
2022-12-29 23:52:51 +08:00
|
|
|
name!: string;
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
title: 'Access授权',
|
|
|
|
|
helper: 'access授权',
|
2022-12-29 23:52:51 +08:00
|
|
|
component: {
|
2024-10-07 03:21:16 +08:00
|
|
|
name: 'access-selector',
|
2024-05-27 18:38:41 +08:00
|
|
|
type: 'tencent',
|
2022-11-07 23:31:20 +08:00
|
|
|
},
|
2022-12-29 23:52:51 +08:00
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
accessId!: string;
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
title: '域名证书',
|
|
|
|
|
helper: '请选择前置任务输出的域名证书',
|
2022-12-29 23:52:51 +08:00
|
|
|
component: {
|
2024-10-07 03:21:16 +08:00
|
|
|
name: 'output-selector',
|
2024-09-18 14:58:59 +08:00
|
|
|
from: ['CertApply', 'CertApplyLego'],
|
2022-11-07 23:31:20 +08:00
|
|
|
},
|
2022-12-29 23:52:51 +08:00
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
cert!: any;
|
|
|
|
|
|
|
|
|
|
@TaskOutput({
|
2024-05-27 18:38:41 +08:00
|
|
|
title: '上传成功后的腾讯云CertId',
|
2022-12-29 23:52:51 +08:00
|
|
|
})
|
|
|
|
|
tencentCertId?: string;
|
|
|
|
|
|
2024-08-28 14:40:50 +08:00
|
|
|
Client: any;
|
|
|
|
|
async onInstance() {
|
|
|
|
|
const sdk = await import('tencentcloud-sdk-nodejs/tencentcloud/services/ssl/v20191205/index.js');
|
|
|
|
|
this.Client = sdk.v20191205.Client;
|
|
|
|
|
}
|
2022-12-29 23:52:51 +08:00
|
|
|
|
|
|
|
|
async execute(): Promise<void> {
|
|
|
|
|
const { accessId, name, cert } = this;
|
2023-07-10 11:52:38 +08:00
|
|
|
const accessProvider = await this.accessService.getById(accessId);
|
2022-11-07 23:31:20 +08:00
|
|
|
const certName = this.appendTimeSuffix(name || cert.domain);
|
|
|
|
|
const client = this.getClient(accessProvider);
|
|
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
|
CertificatePublicKey: cert.crt,
|
|
|
|
|
CertificatePrivateKey: cert.key,
|
|
|
|
|
Alias: certName,
|
|
|
|
|
};
|
|
|
|
|
const ret = await client.UploadCertificate(params);
|
|
|
|
|
this.checkRet(ret);
|
2024-05-27 18:38:41 +08:00
|
|
|
this.logger.info('证书上传成功:tencentCertId=', ret.CertificateId);
|
2022-12-29 23:52:51 +08:00
|
|
|
|
|
|
|
|
this.tencentCertId = ret.CertificateId;
|
2022-11-07 23:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
appendTimeSuffix(name: string) {
|
|
|
|
|
if (name == null) {
|
2024-05-27 18:38:41 +08:00
|
|
|
name = 'certd';
|
2022-11-07 23:31:20 +08:00
|
|
|
}
|
2024-05-27 18:38:41 +08:00
|
|
|
return name + '-' + dayjs().format('YYYYMMDD-HHmmss');
|
2022-11-07 23:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getClient(accessProvider: any) {
|
2024-08-28 14:40:50 +08:00
|
|
|
const SslClient = this.Client;
|
2022-11-07 23:31:20 +08:00
|
|
|
|
|
|
|
|
const clientConfig = {
|
|
|
|
|
credential: {
|
|
|
|
|
secretId: accessProvider.secretId,
|
|
|
|
|
secretKey: accessProvider.secretKey,
|
|
|
|
|
},
|
2024-05-27 18:38:41 +08:00
|
|
|
region: '',
|
2022-11-07 23:31:20 +08:00
|
|
|
profile: {
|
|
|
|
|
httpProfile: {
|
2024-05-27 18:38:41 +08:00
|
|
|
endpoint: 'ssl.tencentcloudapi.com',
|
2022-11-07 23:31:20 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return new SslClient(clientConfig);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// async rollback({ input }) {
|
|
|
|
|
// const { accessId } = input;
|
2023-07-10 11:52:38 +08:00
|
|
|
// const accessProvider = await this.accessService.getById(accessId);
|
2022-11-07 23:31:20 +08:00
|
|
|
// const client = this.getClient(accessProvider);
|
|
|
|
|
//
|
|
|
|
|
// 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;
|
|
|
|
|
// }
|
|
|
|
|
checkRet(ret: any) {
|
|
|
|
|
if (!ret || ret.Error) {
|
2024-05-27 18:38:41 +08:00
|
|
|
throw new Error('执行失败:' + ret.Error.Code + ',' + ret.Error.Message);
|
2022-11-07 23:31:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|