Files
certd/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/upload-to-tencent/index.ts
T

117 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-08-13 20:30:42 +08:00
import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput, TaskOutput } from '@certd/pipeline';
import dayjs from 'dayjs';
2025-03-18 00:52:50 +08:00
import { CertApplyPluginNames} from '@certd/plugin-cert';
2022-12-29 23:52:51 +08:00
@IsTaskPlugin({
name: 'UploadCertToTencent',
2024-12-26 01:32:52 +08:00
title: '腾讯云-上传证书到腾讯云',
2024-09-19 17:38:51 +08:00
icon: 'svg:icon-tencentcloud',
desc: '上传成功后输出:tencentCertId',
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
},
})
2024-10-26 18:10:19 +08:00
export class UploadCertToTencent extends AbstractTaskPlugin {
@TaskInput({ title: '证书名称' })
2022-12-29 23:52:51 +08:00
name!: string;
@TaskInput({
title: 'Access授权',
helper: 'access授权',
2022-12-29 23:52:51 +08:00
component: {
name: 'access-selector',
type: 'tencent',
2022-11-07 23:31:20 +08:00
},
2022-12-29 23:52:51 +08:00
required: true,
})
accessId!: string;
@TaskInput({
title: '域名证书',
helper: '请选择前置任务输出的域名证书',
2022-12-29 23:52:51 +08:00
component: {
name: 'output-selector',
2025-03-18 00:52:50 +08:00
from: [...CertApplyPluginNames],
2022-11-07 23:31:20 +08:00
},
2022-12-29 23:52:51 +08:00
required: true,
})
cert!: any;
@TaskOutput({
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;
const accessProvider = await this.getAccess(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);
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) {
name = 'certd';
2022-11-07 23:31:20 +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,
},
region: '',
2022-11-07 23:31:20 +08:00
profile: {
httpProfile: {
endpoint: 'ssl.tencentcloudapi.com',
2022-11-07 23:31:20 +08:00
},
},
};
return new SslClient(clientConfig);
}
// async rollback({ input }) {
// const { accessId } = input;
// const accessProvider = await this.getAccess(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) {
throw new Error('执行失败:' + ret.Error.Code + ',' + ret.Error.Message);
2022-11-07 23:31:20 +08:00
}
}
}