From 81a3fdbc29b71f380762008cc151493ec97458f9 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Tue, 29 Oct 2024 13:59:20 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=94=AF=E6=8C=81=E5=8D=8E=E4=B8=BA?= =?UTF-8?q?=E4=BA=91cdn?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/basic/src/utils/util.log.ts | 22 ++-- packages/ui/certd-server/package.json | 2 + .../src/plugins/plugin-huawei/index.ts | 1 + .../plugins/deploy-to-cdn/index.ts | 115 ++++++++++++++++++ 4 files changed, 131 insertions(+), 9 deletions(-) create mode 100644 packages/ui/certd-server/src/plugins/plugin-huawei/plugins/deploy-to-cdn/index.ts diff --git a/packages/core/basic/src/utils/util.log.ts b/packages/core/basic/src/utils/util.log.ts index 0cfd0fa2..f65a0797 100644 --- a/packages/core/basic/src/utils/util.log.ts +++ b/packages/core/basic/src/utils/util.log.ts @@ -1,4 +1,4 @@ -import log4js, { LoggingEvent, Logger } from "log4js"; +import log4js, { LoggingEvent, Logger } from 'log4js'; const OutputAppender = { configure: (config: any, layouts: any, findAppender: any, levels: any) => { @@ -18,18 +18,22 @@ const OutputAppender = { }, }; -// @ts-ignore -log4js.configure({ - appenders: { std: { type: "stdout" }, output: { type: OutputAppender } }, - categories: { default: { appenders: ["std"], level: "info" }, pipeline: { appenders: ["std", "output"], level: "info" } }, -}); -export const logger = log4js.getLogger("default"); +export function resetLogConfigure() { + // @ts-ignore + log4js.configure({ + appenders: { std: { type: 'stdout' }, output: { type: OutputAppender } }, + categories: { default: { appenders: ['std'], level: 'info' }, pipeline: { appenders: ['std', 'output'], level: 'info' } }, + }); +} +resetLogConfigure(); +export const logger = log4js.getLogger('default'); export function buildLogger(write: (text: string) => void) { - const logger = log4js.getLogger("pipeline"); - logger.addContext("outputHandler", { + const logger = log4js.getLogger('pipeline'); + logger.addContext('outputHandler', { write, }); return logger; } + export type ILogger = Logger; diff --git a/packages/ui/certd-server/package.json b/packages/ui/certd-server/package.json index 3ef0a5f5..3f8fe049 100644 --- a/packages/ui/certd-server/package.json +++ b/packages/ui/certd-server/package.json @@ -38,6 +38,8 @@ "@certd/plugin-cert": "^1.26.15", "@certd/plugin-plus": "^1.26.15", "@certd/plus-core": "^1.26.15", + "@huaweicloud/huaweicloud-sdk-cdn": "^3.1.120", + "@huaweicloud/huaweicloud-sdk-core": "^3.1.120", "@koa/cors": "^5.0.0", "@midwayjs/bootstrap": "~3.17.1", "@midwayjs/cache": "~3.14.0", diff --git a/packages/ui/certd-server/src/plugins/plugin-huawei/index.ts b/packages/ui/certd-server/src/plugins/plugin-huawei/index.ts index e5dd343c..c9aa8714 100644 --- a/packages/ui/certd-server/src/plugins/plugin-huawei/index.ts +++ b/packages/ui/certd-server/src/plugins/plugin-huawei/index.ts @@ -1,2 +1,3 @@ export * from './access/index.js'; export * from './dns-provider/index.js'; +export * from './plugins/deploy-to-cdn/index.js'; diff --git a/packages/ui/certd-server/src/plugins/plugin-huawei/plugins/deploy-to-cdn/index.ts b/packages/ui/certd-server/src/plugins/plugin-huawei/plugins/deploy-to-cdn/index.ts new file mode 100644 index 00000000..19aeb21d --- /dev/null +++ b/packages/ui/certd-server/src/plugins/plugin-huawei/plugins/deploy-to-cdn/index.ts @@ -0,0 +1,115 @@ +import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, resetLogConfigure, RunStrategy, TaskInput } from '@certd/pipeline'; +import { HuaweiAccess } from '../../access/index.js'; +import { CertInfo } from '@certd/plugin-cert'; +import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from '@certd/plugin-plus'; + +@IsTaskPlugin({ + name: 'HauweiDeployCertToCDN', + title: '部署证书至华为云CDN', + icon: 'ant-design:huawei-outlined', + group: pluginGroups.huawei.key, + desc: '', + default: { + strategy: { + runStrategy: RunStrategy.SkipWhenSucceed, + }, + }, +}) +export class HauweiDeployCertToCDN extends AbstractTaskPlugin { + @TaskInput({ + title: '域名证书', + helper: '请选择前置任务输出的域名证书', + component: { + name: 'output-selector', + from: ['CertApply', 'CertApplyLego'], + }, + required: true, + }) + cert!: CertInfo; + + @TaskInput(createCertDomainGetterInputDefine({ props: { required: false } })) + certDomains!: string[]; + + @TaskInput({ + title: 'Access授权', + helper: '华为云授权AccessKeyId、AccessKeySecret', + component: { + name: 'access-selector', + type: 'huawei', + }, + required: true, + }) + accessId!: string; + + @TaskInput( + createRemoteSelectInputDefine({ + title: 'CDN域名', + helper: '请选择域名或输入域名', + typeName: 'HauweiDeployCertToCDN', + action: HauweiDeployCertToCDN.prototype.onGetDomainList.name, + }) + ) + domains!: string[]; + + async execute(): Promise { + this.logger.info('开始部署证书到华为云cdn'); + const { cdn, client } = await this.getCdnClient(); + const httpsConfig = new cdn.HttpPutBody() + .withHttpsStatus('on') + .withCertificateType('server') + .withCertificateName(this.appendTimeSuffix('certd')) + .withCertificateValue(this.cert.crt) + .withPrivateKey(this.cert.key); + + const config = new cdn.Configs().withHttps(httpsConfig); + const body = new cdn.ModifyDomainConfigRequestBody().withConfigs(config); + if (!this.domains || this.domains.length === 0) { + throw new Error('您还未配置CDN域名'); + } + this.logger.info('部署域名:', JSON.stringify(this.domains)); + for (const domain of this.domains) { + this.logger.info('部署到域名:', domain); + const req = new cdn.UpdateDomainFullConfigRequest().withDomainName(domain).withBody(body); + await client.updateDomainFullConfig(req); + this.logger.info(`部署到域名${domain}完成:`); + } + + this.logger.info('部署证书到华为云cdn完成'); + } + + async getCdnClient() { + const access = await this.accessService.getById(this.accessId); + const { BasicCredentials } = await import('@huaweicloud/huaweicloud-sdk-core'); + const cdn = await import('@huaweicloud/huaweicloud-sdk-cdn/v2/public-api.js'); + //恢复华为云把log4j的config改了的问题 + resetLogConfigure(); + const credentials = new BasicCredentials().withAk(access.accessKeyId).withSk(access.accessKeySecret); + const client = cdn.CdnClient.newBuilder().withCredential(credentials).withEndpoint('https://cdn.myhuaweicloud.com').build(); + return { + client, + cdn, + }; + } + + async onGetDomainList(data: any) { + const { client, cdn } = await this.getCdnClient(); + + const request = new cdn.ListDomainsRequest(); + request.pageNumber = 1000; + const result = await client.listDomains(request); + if (!result || !result.domains || result.domains.length === 0) { + throw new Error('未找到CDN域名,您可以手动输入'); + } + + const domains = result.domains.map(domain => { + return { + value: domain.domainName, + label: domain.domainName, + domain: domain.domainName, + }; + }); + + return this.ctx.utils.options.buildGroupOptions(domains, this.certDomains); + } +} +new HauweiDeployCertToCDN();