From d782655cb4dfbb74138178afbffeee76fc755115 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Wed, 23 Oct 2024 17:49:38 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=96=B0=E5=A2=9E=E9=83=A8=E7=BD=B2?= =?UTF-8?q?=E5=88=B0=E8=85=BE=E8=AE=AF=E4=BA=91CDN-v2=EF=BC=8C=E6=8E=A8?= =?UTF-8?q?=E8=8D=90=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/deploy-to-cdn-v2/index.ts | 118 ++++++++++++++++++ .../plugin/deploy-to-cdn/index.ts | 1 + .../plugins/plugin-tencent/plugin/index.ts | 1 + 3 files changed, 120 insertions(+) create mode 100644 packages/ui/certd-server/src/plugins/plugin-tencent/plugin/deploy-to-cdn-v2/index.ts diff --git a/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/deploy-to-cdn-v2/index.ts b/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/deploy-to-cdn-v2/index.ts new file mode 100644 index 00000000..b998795e --- /dev/null +++ b/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/deploy-to-cdn-v2/index.ts @@ -0,0 +1,118 @@ +import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput } from '@certd/pipeline'; +import { createRemoteSelectInputDefine, TencentAccess } from '@certd/plugin-plus'; +import { CertInfo } from '@certd/plugin-cert'; +import { TencentSslClient } from '../../lib/index.js'; + +@IsTaskPlugin({ + name: 'TencentDeployCertToCDNv2', + title: '部署到腾讯云CDN-v2', + icon: 'svg:icon-tencentcloud', + group: pluginGroups.tencent.key, + desc: '推荐使用', + default: { + strategy: { + runStrategy: RunStrategy.SkipWhenSucceed, + }, + }, +}) +export class TencentDeployCertToCDNv2 extends AbstractTaskPlugin { + @TaskInput({ + title: 'Access提供者', + helper: 'access 授权', + component: { + name: 'access-selector', + type: 'tencent', + }, + required: true, + }) + accessId!: string; + + @TaskInput( + createRemoteSelectInputDefine({ + title: 'CDN域名', + helper: '请选择域名或输入域名', + typeName: TencentDeployCertToCDNv2.name, + action: TencentDeployCertToCDNv2.prototype.onGetDomainList.name, + }) + ) + domains!: string | string[]; + + @TaskInput({ + title: '域名证书', + helper: '请选择前置任务输出的域名证书,或者选择前置任务“上传证书到腾讯云”任务的证书ID', + component: { + name: 'output-selector', + from: ['CertApply', 'CertApplyLego', 'UploadCertToTencent'], + }, + required: true, + }) + cert!: CertInfo | string; + + async onInstance() {} + + async execute(): Promise { + const access = await this.accessService.getById(this.accessId); + const sslClient = new TencentSslClient({ + access, + logger: this.logger, + }); + + let tencentCertId = this.cert as string; + if (typeof this.cert !== 'string') { + tencentCertId = await sslClient.uploadToTencent({ + certName: this.appendTimeSuffix('certd'), + cert: this.cert, + }); + } + + const res = await sslClient.deployCertificateInstance({ + CertificateId: tencentCertId, + ResourceType: 'cdn', + Status: 1, + InstanceIdList: this.domains, + }); + + this.logger.info('部署成功', res); + } + + checkRet(ret: any) { + if (!ret || ret.Error) { + throw new Error('执行失败:' + ret.Error.Code + ',' + ret.Error.Message); + } + } + + async getCdnClient() { + const accessProvider = await this.accessService.getById(this.accessId); + const sdk = await import('tencentcloud-sdk-nodejs/tencentcloud/services/cdn/v20180606/index.js'); + const CdnClient = sdk.v20180606.Client; + + const clientConfig = { + credential: { + secretId: accessProvider.secretId, + secretKey: accessProvider.secretKey, + }, + region: '', + profile: { + httpProfile: { + endpoint: 'cdn.tencentcloudapi.com', + }, + }, + }; + + return new CdnClient(clientConfig); + } + + async onGetDomainList(data: any) { + const cdnClient = await this.getCdnClient(); + const res = await cdnClient.DescribeDomains({ + Limit: 1000, + }); + this.checkRet(res); + return res.Domains.map((item: any) => { + return { + label: item.Domain, + value: item.Domain, + }; + }); + } +} diff --git a/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/deploy-to-cdn/index.ts b/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/deploy-to-cdn/index.ts index a3d4dbb7..3d6831f0 100644 --- a/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/deploy-to-cdn/index.ts +++ b/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/deploy-to-cdn/index.ts @@ -7,6 +7,7 @@ import { CertInfo } from '@certd/plugin-cert'; title: '部署到腾讯云CDN', icon: 'svg:icon-tencentcloud', group: pluginGroups.tencent.key, + desc: '可能不支持国际版', default: { strategy: { runStrategy: RunStrategy.SkipWhenSucceed, diff --git a/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/index.ts b/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/index.ts index f9f1673a..ee996184 100644 --- a/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/index.ts +++ b/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/index.ts @@ -1,3 +1,4 @@ export * from './deploy-to-clb/index.js'; export * from './deploy-to-cdn/index.js'; export * from './upload-to-tencent/index.js'; +export * from './deploy-to-cdn-v2/index.js';