2023-06-25 23:25:56 +08:00
|
|
|
import { AbstractTaskPlugin, IAccessService, ILogger, IsTaskPlugin, RunStrategy, TaskInput, TaskOutput } from "@certd/pipeline";
|
2022-11-07 23:31:20 +08:00
|
|
|
import tencentcloud from "tencentcloud-sdk-nodejs/index";
|
|
|
|
|
import dayjs from "dayjs";
|
|
|
|
|
|
2022-12-29 23:52:51 +08:00
|
|
|
@IsTaskPlugin({
|
|
|
|
|
name: "UploadCertToTencent",
|
|
|
|
|
title: "上传证书到腾讯云",
|
|
|
|
|
desc: "上传成功后输出:tencentCertId",
|
|
|
|
|
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 {
|
2022-12-29 23:52:51 +08:00
|
|
|
@TaskInput({ title: "证书名称" })
|
|
|
|
|
name!: string;
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
title: "Access授权",
|
|
|
|
|
helper: "access授权",
|
|
|
|
|
component: {
|
|
|
|
|
name: "pi-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: "请选择前置任务输出的域名证书",
|
|
|
|
|
component: {
|
|
|
|
|
name: "pi-output-selector",
|
2022-11-07 23:31:20 +08:00
|
|
|
},
|
2022-12-29 23:52:51 +08:00
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
cert!: any;
|
|
|
|
|
|
|
|
|
|
@TaskOutput({
|
|
|
|
|
title: "上传成功后的腾讯云CertId",
|
|
|
|
|
})
|
|
|
|
|
tencentCertId?: string;
|
|
|
|
|
|
|
|
|
|
accessService!: IAccessService;
|
2023-01-11 20:39:48 +08:00
|
|
|
logger!: ILogger;
|
2022-12-29 23:52:51 +08:00
|
|
|
|
2023-06-25 23:25:56 +08:00
|
|
|
async onInstance() {
|
|
|
|
|
this.accessService = this.ctx.accessService;
|
|
|
|
|
this.logger = this.ctx.logger;
|
|
|
|
|
}
|
2022-12-29 23:52:51 +08:00
|
|
|
|
|
|
|
|
async execute(): Promise<void> {
|
|
|
|
|
const { accessId, name, cert } = this;
|
2022-11-07 23:31:20 +08:00
|
|
|
const accessProvider = this.accessService.getById(accessId);
|
|
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
return name + "-" + dayjs().format("YYYYMMDD-HHmmss");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getClient(accessProvider: any) {
|
|
|
|
|
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 rollback({ input }) {
|
|
|
|
|
// const { accessId } = input;
|
|
|
|
|
// const accessProvider = this.accessService.getById(accessId);
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|