Files
certd/packages/ui/certd-server/src/plugins/plugin-aliyun/plugin/deploy-to-cdn/index.ts
T

296 lines
8.5 KiB
TypeScript
Raw Normal View History

2026-05-31 01:41:33 +08:00
import { optionsUtils } from "@certd/basic";
import { AbstractTaskPlugin, CertTargetItem, IsTaskPlugin, Pager, PageSearch, pluginGroups, RunStrategy, TaskInput, TaskOutput } from "@certd/pipeline";
2025-05-22 23:21:32 +08:00
import { CertApplyPluginNames, CertReader } from "@certd/plugin-cert";
2026-05-31 01:41:33 +08:00
import { CertInfo, createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from "@certd/plugin-lib";
import { AliyunAccess } from "../../../plugin-lib/aliyun/access/index.js";
import { AliyunClient, AliyunSslClient, CasCertId } from "../../../plugin-lib/aliyun/lib/index.js";
@IsTaskPlugin({
2026-05-31 01:41:33 +08:00
name: "DeployCertToAliyunCDN",
title: "阿里云-部署证书至CDN",
icon: "svg:icon-aliyun",
group: pluginGroups.aliyun.key,
2026-05-31 01:41:33 +08:00
desc: "自动部署域名证书至阿里云CDN",
2026-03-29 02:40:26 +08:00
runStrategy: RunStrategy.AlwaysRun,
// default: {
// strategy: {
// runStrategy: RunStrategy.SkipWhenSucceed,
// },
// },
})
export class DeployCertToAliyunCDN extends AbstractTaskPlugin {
@TaskInput({
2026-05-31 01:41:33 +08:00
title: "证书服务接入点",
helper: "不会选就按默认",
value: "cas.aliyuncs.com",
component: {
2026-05-31 01:41:33 +08:00
name: "a-select",
options: [
2026-05-31 01:41:33 +08:00
{ value: "cas.aliyuncs.com", label: "中国大陆" },
{ value: "cas.ap-southeast-1.aliyuncs.com", label: "新加坡" },
{ value: "cas.eu-central-1.aliyuncs.com", label: "德国(法兰克福)" },
],
},
required: true,
})
endpoint!: string;
@TaskInput({
2026-05-31 01:41:33 +08:00
title: "域名证书",
helper: "请选择前置任务输出的域名证书",
component: {
2026-05-31 01:41:33 +08:00
name: "output-selector",
from: [...CertApplyPluginNames, "uploadCertToAliyun"],
},
template: false,
required: true,
})
cert!: CertInfo | CasCertId | number;
@TaskInput(createCertDomainGetterInputDefine({ props: { required: false } }))
certDomains!: string[];
@TaskInput({
2026-05-31 01:41:33 +08:00
title: "Access授权",
helper: "阿里云授权AccessKeyId、AccessKeySecret",
component: {
2026-05-31 01:41:33 +08:00
name: "access-selector",
type: "aliyun",
},
required: true,
})
accessId!: string;
@TaskInput({
2026-05-31 01:41:33 +08:00
title: "证书所在地域",
helper: "cn-hangzhou和ap-southeast-1,默认cn-hangzhou。国际站用户建议使用ap-southeast-1。",
value: "cn-hangzhou",
component: {
2026-05-31 01:41:33 +08:00
name: "a-select",
options: [
2026-05-31 01:41:33 +08:00
{ value: "cn-hangzhou", label: "中国大陆" },
{ value: "ap-southeast-1", label: "新加坡" },
],
},
required: true,
})
2026-05-31 01:41:33 +08:00
certRegion: string;
@TaskInput({
2026-05-31 01:41:33 +08:00
title: "证书名称",
helper: "上传后将以此名称作为前缀备注",
})
certName!: string;
@TaskInput({
2026-05-31 01:41:33 +08:00
title: "域名匹配模式",
helper: "根据证书匹配:根据证书域名自动匹配DCDN加速域名自动部署,新增加速域名自动感知,自动新增部署",
component: {
2026-05-31 01:41:33 +08:00
name: "a-select",
options: [
2026-05-31 01:41:33 +08:00
{ label: "手动选择", value: "manual" },
{ label: "根据证书匹配", value: "auto" },
],
},
2026-05-31 01:41:33 +08:00
value: "manual",
})
2026-05-31 01:41:33 +08:00
domainMatchMode!: "manual" | "auto";
@TaskInput(
createRemoteSelectInputDefine({
2026-05-31 01:41:33 +08:00
title: "CDN加速域名",
helper: "你在阿里云上配置的CDN加速域名,比如:certd.docmirror.cn",
typeName: "DeployCertToAliyunCDN",
action: DeployCertToAliyunCDN.prototype.onGetDomainList.name,
2026-05-31 01:41:33 +08:00
watches: ["certDomains", "accessId"],
required: true,
pageSize: 100,
2026-05-31 01:41:33 +08:00
search: true,
mergeScript: `
return {
show: ctx.compute(({form})=>{
return form.domainMatchMode === "manual"
})
}
`,
2026-05-31 01:41:33 +08:00
pager: true,
})
)
domainName!: string | string[];
@TaskOutput({
2026-05-31 01:41:33 +08:00
title: "已部署过的DCDN加速域名",
})
deployedList!: string[];
2026-05-31 01:41:33 +08:00
async onInstance() {}
async execute(): Promise<any> {
2026-05-31 01:41:33 +08:00
this.logger.info("开始部署证书到阿里云cdn");
const access = await this.getAccess<AliyunAccess>(this.accessId);
if (this.cert == null) {
2026-05-31 01:41:33 +08:00
throw new Error("域名证书参数为空,请检查前置任务");
}
const client = await this.getClient(access);
const sslClient = new AliyunSslClient({
access,
logger: this.logger,
2026-05-31 01:41:33 +08:00
endpoint: this.endpoint || "cas.aliyuncs.com",
});
2026-05-31 01:41:33 +08:00
if (this.domainMatchMode === "auto") {
const { result, deployedList } = await this.autoMatchedDeploy({
2026-05-31 01:41:33 +08:00
targetName: "DCDN加速域名",
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 02:40:26 +08:00
return await this.deployOne(client, req.target.value, req.cert);
},
2026-05-31 01:41:33 +08:00
getCertDomains: async () => {
return sslClient.getCertDomains(this.cert);
},
2026-05-31 01:41:33 +08:00
getDeployTargetList: this.onGetDomainList.bind(this),
});
this.deployedList = deployedList;
return result;
} else {
if (this.isNotChanged()) {
2026-05-31 01:41:33 +08:00
this.logger.info("输入参数未变更,跳过");
return "skip";
}
const certId = await this.getOrUploadCasCert(sslClient);
2026-05-31 01:41:33 +08:00
if (typeof this.domainName === "string") {
this.domainName = [this.domainName];
}
for (const domain of this.domainName) {
2026-05-31 01:41:33 +08:00
await this.deployOne(client, domain, certId);
}
}
2026-05-31 01:41:33 +08:00
this.logger.info("部署完成");
}
async getOrUploadCasCert(sslClient: AliyunSslClient) {
let certId: any = this.cert;
let certName = this.appendTimeSuffix(this.certName);
2026-05-31 01:41:33 +08:00
if (typeof this.cert === "object") {
const certInfo = this.cert as CertInfo;
const casCert = this.cert as CasCertId;
if (casCert.certId) {
certId = casCert.certId;
} else if (certInfo.crt) {
certName = CertReader.buildCertName(certInfo);
const certIdRes = await sslClient.uploadCertificate({
name: certName,
cert: certInfo,
});
certId = certIdRes.certId as any;
} else {
2026-05-31 01:41:33 +08:00
throw new Error("证书格式错误" + JSON.stringify(this.cert));
}
}
return {
certId,
certName,
2026-05-31 01:41:33 +08:00
};
}
2026-05-31 01:41:33 +08:00
async deployOne(client: any, domain: string, cert: any) {
const { certId, certName } = cert;
await this.SetCdnDomainSSLCertificate(client, {
CertId: certId,
DomainName: domain,
CertName: certName,
2026-05-31 01:41:33 +08:00
CertRegion: this.certRegion || "cn-hangzhou",
});
}
2024-09-02 18:36:12 +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({
accessKeyId: access.accessKeyId,
accessKeySecret: access.accessKeySecret,
2026-05-31 01:41:33 +08:00
endpoint: "https://cdn.aliyuncs.com",
apiVersion: "2018-05-10",
});
return client;
}
2026-05-31 01:41:33 +08:00
async SetCdnDomainSSLCertificate(client: any, params: { CertId: number; DomainName: string; CertName: string; CertRegion: string }) {
this.logger.info("设置CDN: ", JSON.stringify(params));
const requestOption = {
2026-05-31 01:41:33 +08:00
method: "POST",
formatParams: false,
};
const ret: any = await client.request(
2026-05-31 01:41:33 +08:00
"SetCdnDomainSSLCertificate",
{
2026-05-31 01:41:33 +08:00
SSLProtocol: "on",
CertType: "cas",
...params,
},
requestOption
);
this.checkRet(ret);
this.logger.info(`设置CDN: ${params.DomainName} 证书成功:`, ret.RequestId);
}
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);
}
}
async onGetDomainList(data: PageSearch) {
if (!this.accessId) {
2026-05-31 01:41:33 +08:00
throw new Error("请选择Access授权");
}
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);
const params: any = {
PageSize: pager.pageSize || 100,
PageNumber: pager.pageNo || 1,
2026-05-31 01:41:33 +08:00
DomainSearchType: "fuzzy_match",
};
if (data.searchKey) {
params.DomainName = data.searchKey;
}
const requestOption = {
2026-05-31 01:41:33 +08:00
method: "POST",
formatParams: false,
};
2026-05-31 01:41:33 +08:00
const res = await client.request("DescribeUserDomains", params, requestOption);
this.checkRet(res);
const pageData = res?.Domains?.PageData;
if (!pageData || pageData.length === 0) {
2026-05-31 01:41:33 +08:00
return {
list: [],
total: 0,
};
}
2026-05-31 01:41:33 +08:00
const total = res?.TotalCount || 0;
const options = pageData.map((item: any) => {
return {
value: item.DomainName,
label: item.DomainName,
domain: item.DomainName,
};
});
return {
list: optionsUtils.buildGroupOptions(options, this.certDomains),
total,
};
}
}
new DeployCertToAliyunCDN();