2026-05-31 01:41:33 +08:00
|
|
|
import { AbstractTaskPlugin, CertTargetItem, IsTaskPlugin, Pager, PageSearch, pluginGroups, RunStrategy, TaskInput, TaskOutput } from "@certd/pipeline";
|
|
|
|
|
import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from "@certd/plugin-lib";
|
|
|
|
|
import dayjs from "dayjs";
|
2025-12-31 17:01:37 +08:00
|
|
|
import { AliyunAccess } from "../../../plugin-lib/aliyun/access/index.js";
|
|
|
|
|
|
2025-09-28 11:02:25 +08:00
|
|
|
import { optionsUtils } from "@certd/basic";
|
2026-05-31 01:41:33 +08:00
|
|
|
import { CertApplyPluginNames, CertInfo } from "@certd/plugin-cert";
|
2026-03-29 01:57:33 +08:00
|
|
|
import { AliyunClient, AliyunSslClient, CasCertId } from "../../../plugin-lib/aliyun/lib/index.js";
|
2024-09-02 19:33:17 +08:00
|
|
|
@IsTaskPlugin({
|
2026-05-31 01:41:33 +08:00
|
|
|
name: "DeployCertToAliyunDCDN",
|
|
|
|
|
title: "阿里云-部署证书至DCDN",
|
|
|
|
|
icon: "svg:icon-aliyun",
|
2024-09-02 19:33:17 +08:00
|
|
|
group: pluginGroups.aliyun.key,
|
2026-05-31 01:41:33 +08:00
|
|
|
desc: "依赖证书申请前置任务,自动部署域名证书至阿里云DCDN",
|
2026-03-29 01:57:33 +08:00
|
|
|
runStrategy: RunStrategy.AlwaysRun,
|
|
|
|
|
// default: {
|
|
|
|
|
// strategy: {
|
|
|
|
|
// runStrategy: RunStrategy.SkipWhenSucceed,
|
|
|
|
|
// },
|
|
|
|
|
// },
|
2024-09-02 19:33:17 +08:00
|
|
|
})
|
|
|
|
|
export class DeployCertToAliyunDCDN extends AbstractTaskPlugin {
|
|
|
|
|
@TaskInput({
|
2026-05-31 01:41:33 +08:00
|
|
|
title: "域名证书",
|
|
|
|
|
helper: "请选择前置任务输出的域名证书",
|
2024-09-02 19:33:17 +08:00
|
|
|
component: {
|
2026-05-31 01:41:33 +08:00
|
|
|
name: "output-selector",
|
|
|
|
|
from: [...CertApplyPluginNames, "uploadCertToAliyun"],
|
2024-09-02 19:33:17 +08:00
|
|
|
},
|
|
|
|
|
required: true,
|
|
|
|
|
})
|
2026-02-09 14:29:19 +08:00
|
|
|
cert!: CertInfo | CasCertId | number;
|
2024-10-25 16:51:36 +08:00
|
|
|
|
|
|
|
|
@TaskInput(createCertDomainGetterInputDefine({ props: { required: false } }))
|
|
|
|
|
certDomains!: string[];
|
2024-09-02 19:33:17 +08:00
|
|
|
|
|
|
|
|
@TaskInput({
|
2026-05-31 01:41:33 +08:00
|
|
|
title: "Access授权",
|
|
|
|
|
helper: "阿里云授权AccessKeyId、AccessKeySecret",
|
2024-09-02 19:33:17 +08:00
|
|
|
component: {
|
2026-05-31 01:41:33 +08:00
|
|
|
name: "access-selector",
|
|
|
|
|
type: "aliyun",
|
2024-09-02 19:33:17 +08:00
|
|
|
},
|
|
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
accessId!: string;
|
|
|
|
|
|
2024-10-25 16:51:36 +08:00
|
|
|
@TaskInput({
|
2026-05-31 01:41:33 +08:00
|
|
|
title: "证书名称",
|
|
|
|
|
helper: "上传后将以此名称作为前缀备注",
|
2024-10-25 16:51:36 +08:00
|
|
|
})
|
|
|
|
|
certName!: string;
|
|
|
|
|
|
2026-03-28 11:17:50 +08:00
|
|
|
@TaskInput({
|
2026-05-31 01:41:33 +08:00
|
|
|
title: "域名匹配模式",
|
|
|
|
|
helper: "根据证书匹配:根据证书域名自动匹配DCDN加速域名自动部署,新增加速域名自动感知,自动新增部署",
|
2026-03-28 11:17:50 +08:00
|
|
|
component: {
|
2026-05-31 01:41:33 +08:00
|
|
|
name: "a-select",
|
2026-03-28 11:17:50 +08:00
|
|
|
options: [
|
2026-05-31 01:41:33 +08:00
|
|
|
{ label: "手动选择", value: "manual" },
|
|
|
|
|
{ label: "根据证书匹配", value: "auto" },
|
2026-03-28 11:17:50 +08:00
|
|
|
],
|
|
|
|
|
},
|
2026-05-31 01:41:33 +08:00
|
|
|
value: "manual",
|
2026-03-28 11:17:50 +08:00
|
|
|
})
|
2026-05-31 01:41:33 +08:00
|
|
|
domainMatchMode!: "manual" | "auto";
|
2025-05-26 23:10:31 +08:00
|
|
|
|
|
|
|
|
@TaskInput(
|
|
|
|
|
createRemoteSelectInputDefine({
|
2026-05-31 01:41:33 +08:00
|
|
|
title: "DCDN加速域名",
|
|
|
|
|
helper: "你在阿里云上配置的DCDN加速域名,比如:certd.docmirror.cn",
|
2025-05-26 23:10:31 +08:00
|
|
|
action: DeployCertToAliyunDCDN.prototype.onGetDomainList.name,
|
2026-05-31 01:41:33 +08:00
|
|
|
watches: ["certDomains", "accessId"],
|
2025-05-26 23:10:31 +08:00
|
|
|
required: true,
|
2026-05-26 12:30:58 +08:00
|
|
|
pageSize: 100,
|
2026-05-31 01:41:33 +08:00
|
|
|
search: true,
|
|
|
|
|
pager: true,
|
2026-03-28 11:17:50 +08:00
|
|
|
mergeScript: `
|
|
|
|
|
return {
|
|
|
|
|
show: ctx.compute(({form})=>{
|
2026-03-29 01:57:33 +08:00
|
|
|
return form.domainMatchMode === "manual"
|
2026-03-28 11:17:50 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
`,
|
2025-05-26 23:10:31 +08:00
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
domainName!: string | string[];
|
|
|
|
|
|
2026-03-29 01:57:33 +08:00
|
|
|
@TaskOutput({
|
2026-05-31 01:41:33 +08:00
|
|
|
title: "已部署过的DCDN加速域名",
|
2026-03-29 01:57:33 +08:00
|
|
|
})
|
|
|
|
|
deployedList!: string[];
|
|
|
|
|
|
2026-05-31 01:41:33 +08:00
|
|
|
async onInstance() {}
|
2026-03-29 01:57:33 +08:00
|
|
|
async execute(): Promise<any> {
|
2026-05-31 01:41:33 +08:00
|
|
|
this.logger.info("开始部署证书到阿里云DCDN");
|
2025-04-12 00:14:55 +08:00
|
|
|
const access = (await this.getAccess(this.accessId)) as AliyunAccess;
|
2024-09-02 19:33:17 +08:00
|
|
|
const client = await this.getClient(access);
|
2026-03-29 01:57:33 +08:00
|
|
|
const sslClient = new AliyunSslClient({ access, logger: this.logger });
|
2026-03-29 02:25:45 +08:00
|
|
|
|
2026-05-31 01:41:33 +08:00
|
|
|
if (this.domainMatchMode === "auto") {
|
2026-03-29 02:25:45 +08:00
|
|
|
const { result, deployedList } = await this.autoMatchedDeploy({
|
2026-05-31 01:41:33 +08:00
|
|
|
targetName: "CDN加速域名",
|
2026-03-29 01:57:33 +08:00
|
|
|
uploadCert: async () => {
|
|
|
|
|
return await sslClient.uploadCertOrGet(this.cert);
|
|
|
|
|
},
|
2026-05-31 01:41:33 +08:00
|
|
|
deployOne: async (req: { target: CertTargetItem; cert: any }) => {
|
2026-03-29 01:57:33 +08:00
|
|
|
return await this.deployOne(client, req.target.value, req.cert);
|
|
|
|
|
},
|
2026-05-31 01:41:33 +08:00
|
|
|
getCertDomains: async () => {
|
2026-03-29 02:25:45 +08:00
|
|
|
return sslClient.getCertDomains(this.cert);
|
2026-03-29 01:57:33 +08:00
|
|
|
},
|
2026-05-31 01:41:33 +08:00
|
|
|
getDeployTargetList: this.onGetDomainList.bind(this),
|
2026-03-29 01:57:33 +08:00
|
|
|
});
|
|
|
|
|
this.deployedList = deployedList;
|
|
|
|
|
return result;
|
2026-03-28 11:17:50 +08:00
|
|
|
} else {
|
2026-03-29 01:57:33 +08:00
|
|
|
if (this.isNotChanged()) {
|
2026-05-31 01:41:33 +08:00
|
|
|
this.logger.info("输入参数未变更,跳过");
|
2026-03-29 01:57:33 +08:00
|
|
|
return "skip";
|
|
|
|
|
}
|
2026-05-31 01:41:33 +08:00
|
|
|
|
2026-03-28 11:17:50 +08:00
|
|
|
if (!this.domainName) {
|
2026-05-31 01:41:33 +08:00
|
|
|
throw new Error("您还未选择DCDN域名");
|
2026-03-28 11:17:50 +08:00
|
|
|
}
|
2026-03-29 01:57:33 +08:00
|
|
|
let domains: string[] = [];
|
2026-05-31 01:41:33 +08:00
|
|
|
domains = typeof this.domainName === "string" ? [this.domainName] : this.domainName;
|
2026-03-29 01:57:33 +08:00
|
|
|
const aliCrtId = await sslClient.uploadCertOrGet(this.cert);
|
|
|
|
|
for (const domainName of domains) {
|
|
|
|
|
await this.deployOne(client, domainName, aliCrtId);
|
|
|
|
|
}
|
2025-05-26 23:10:31 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-31 01:41:33 +08:00
|
|
|
this.logger.info("部署完成");
|
2026-03-29 01:57:33 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 02:25:45 +08:00
|
|
|
async deployOne(client: any, domainName: string, aliCrtId: CasCertId) {
|
2026-05-31 01:41:33 +08:00
|
|
|
this.logger.info(`[${domainName}]开始部署`);
|
2026-03-29 01:57:33 +08:00
|
|
|
const params = await this.buildParams(domainName, aliCrtId);
|
|
|
|
|
await this.doRequest(client, params);
|
|
|
|
|
await this.ctx.utils.sleep(1000);
|
2026-05-31 01:41:33 +08:00
|
|
|
this.logger.info(`[${domainName}]部署成功`);
|
2026-03-29 01:57:33 +08:00
|
|
|
}
|
|
|
|
|
|
2024-09-02 19:33:17 +08:00
|
|
|
async getClient(access: AliyunAccess) {
|
2026-06-20 00:35:13 +08:00
|
|
|
const client = new AliyunClient({ logger: this.logger, importRuntime: access.importRuntime.bind(access) });
|
2024-09-05 18:00:45 +08:00
|
|
|
await client.init({
|
2024-09-02 19:33:17 +08:00
|
|
|
accessKeyId: access.accessKeyId,
|
|
|
|
|
accessKeySecret: access.accessKeySecret,
|
2026-05-31 01:41:33 +08:00
|
|
|
endpoint: "https://dcdn.aliyuncs.com",
|
|
|
|
|
apiVersion: "2018-01-15",
|
2024-09-19 17:38:51 +08:00
|
|
|
});
|
|
|
|
|
return client;
|
2024-09-02 19:33:17 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 01:57:33 +08:00
|
|
|
async buildParams(domainName: string, aliCrtId: CasCertId) {
|
2026-05-31 01:41:33 +08:00
|
|
|
const CertName = (this.certName ?? "certd") + "-" + dayjs().format("YYYYMMDDHHmmss");
|
2026-03-29 01:57:33 +08:00
|
|
|
const certId = aliCrtId.certId;
|
2026-05-31 01:41:33 +08:00
|
|
|
this.logger.info("使用已上传的证书:", certId);
|
2024-09-02 19:33:17 +08:00
|
|
|
return {
|
2026-03-03 11:31:52 +08:00
|
|
|
DomainName: domainName,
|
2026-05-31 01:41:33 +08:00
|
|
|
SSLProtocol: "on",
|
|
|
|
|
CertType: "cas",
|
2024-09-02 19:33:17 +08:00
|
|
|
CertName: CertName,
|
2026-02-09 14:29:19 +08:00
|
|
|
CertId: certId,
|
2024-09-02 19:33:17 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-02 23:46:28 +08:00
|
|
|
async doRequest(client: any, params: any) {
|
2024-09-02 19:33:17 +08:00
|
|
|
const requestOption = {
|
2026-05-31 01:41:33 +08:00
|
|
|
method: "POST",
|
2024-09-02 23:46:28 +08:00
|
|
|
formatParams: false,
|
2024-09-02 19:33:17 +08:00
|
|
|
};
|
2026-05-31 01:41:33 +08:00
|
|
|
const ret: any = await client.request("SetDcdnDomainSSLCertificate", params, requestOption);
|
2024-09-02 19:33:17 +08:00
|
|
|
this.checkRet(ret);
|
2026-05-31 01:41:33 +08:00
|
|
|
this.logger.info("设置Dcdn证书成功:", ret.RequestId);
|
2024-09-02 19:33:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkRet(ret: any) {
|
2025-01-19 15:31:37 +08:00
|
|
|
if (ret.Code != null) {
|
2026-05-31 01:41:33 +08:00
|
|
|
throw new Error("执行失败:" + ret.Message);
|
2024-09-02 19:33:17 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-05-26 23:10:31 +08:00
|
|
|
|
2026-05-31 01:41:33 +08:00
|
|
|
async onGetDomainList(data: PageSearch): Promise<{ list: CertTargetItem[]; total: number }> {
|
2025-05-26 23:10:31 +08:00
|
|
|
if (!this.accessId) {
|
2026-05-31 01:41:33 +08:00
|
|
|
throw new Error("请选择Access授权");
|
2025-05-26 23:10:31 +08:00
|
|
|
}
|
|
|
|
|
const access = await this.getAccess<AliyunAccess>(this.accessId);
|
|
|
|
|
|
|
|
|
|
const client = await this.getClient(access);
|
2026-05-31 01:41:33 +08:00
|
|
|
const pager = new Pager(data);
|
2025-05-26 23:10:31 +08:00
|
|
|
const params = {
|
2026-05-26 12:30:58 +08:00
|
|
|
DomainName: data.searchKey,
|
|
|
|
|
PageSize: pager.pageSize || 200,
|
|
|
|
|
PageNumber: pager.pageNo || 1,
|
2026-05-31 01:41:33 +08:00
|
|
|
DomainSearchType: "fuzzy_match",
|
2025-05-26 23:10:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const requestOption = {
|
2026-05-31 01:41:33 +08:00
|
|
|
method: "POST",
|
2025-05-26 23:10:31 +08:00
|
|
|
formatParams: false,
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-31 01:41:33 +08:00
|
|
|
const res = await client.request("DescribeDcdnUserDomains", params, requestOption);
|
2025-05-26 23:10:31 +08:00
|
|
|
this.checkRet(res);
|
2026-03-28 11:17:50 +08:00
|
|
|
const pageData = res?.Domains?.PageData || [];
|
2026-05-26 12:30:58 +08:00
|
|
|
const total = res?.TotalCount || 0;
|
2026-03-29 02:25:45 +08:00
|
|
|
|
2025-05-26 23:10:31 +08:00
|
|
|
const options = pageData.map((item: any) => {
|
|
|
|
|
return {
|
|
|
|
|
value: item.DomainName,
|
|
|
|
|
label: item.DomainName,
|
|
|
|
|
domain: item.DomainName,
|
|
|
|
|
};
|
|
|
|
|
});
|
2026-03-29 02:25:45 +08:00
|
|
|
|
2026-03-28 11:17:50 +08:00
|
|
|
return {
|
|
|
|
|
list: optionsUtils.buildGroupOptions(options, this.certDomains),
|
|
|
|
|
total: total,
|
|
|
|
|
};
|
2025-05-26 23:10:31 +08:00
|
|
|
}
|
2024-09-02 19:33:17 +08:00
|
|
|
}
|
|
|
|
|
new DeployCertToAliyunDCDN();
|