2024-09-02 19:33:17 +08:00
|
|
|
import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput } from '@certd/pipeline';
|
|
|
|
|
import dayjs from 'dayjs';
|
2025-05-26 23:10:31 +08:00
|
|
|
import {
|
|
|
|
|
createCertDomainGetterInputDefine,
|
|
|
|
|
createRemoteSelectInputDefine
|
|
|
|
|
} from "@certd/plugin-lib";
|
2025-12-31 17:01:37 +08:00
|
|
|
import { AliyunAccess } from "../../../plugin-lib/aliyun/access/index.js";
|
|
|
|
|
|
2024-10-25 16:51:36 +08:00
|
|
|
import { CertInfo } from '@certd/plugin-cert';
|
2026-02-09 14:29:19 +08:00
|
|
|
import { CertApplyPluginNames } from '@certd/plugin-cert';
|
2025-09-28 11:02:25 +08:00
|
|
|
import { optionsUtils } from "@certd/basic";
|
2026-02-09 14:29:19 +08:00
|
|
|
import { AliyunClient, CasCertId } from "../../../plugin-lib/aliyun/lib/index.js";
|
2024-09-02 19:33:17 +08:00
|
|
|
@IsTaskPlugin({
|
|
|
|
|
name: 'DeployCertToAliyunDCDN',
|
2024-12-26 01:32:52 +08:00
|
|
|
title: '阿里云-部署证书至DCDN',
|
2025-03-24 21:27:31 +08:00
|
|
|
icon: 'svg:icon-aliyun',
|
2024-09-02 19:33:17 +08:00
|
|
|
group: pluginGroups.aliyun.key,
|
|
|
|
|
desc: '依赖证书申请前置任务,自动部署域名证书至阿里云DCDN',
|
|
|
|
|
default: {
|
|
|
|
|
strategy: {
|
|
|
|
|
runStrategy: RunStrategy.SkipWhenSucceed,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
export class DeployCertToAliyunDCDN extends AbstractTaskPlugin {
|
|
|
|
|
@TaskInput({
|
|
|
|
|
title: '域名证书',
|
|
|
|
|
helper: '请选择前置任务输出的域名证书',
|
|
|
|
|
component: {
|
2024-10-07 03:21:16 +08:00
|
|
|
name: 'output-selector',
|
2025-03-18 00:52:50 +08:00
|
|
|
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({
|
|
|
|
|
title: 'Access授权',
|
|
|
|
|
helper: '阿里云授权AccessKeyId、AccessKeySecret',
|
|
|
|
|
component: {
|
2024-10-07 03:21:16 +08:00
|
|
|
name: 'access-selector',
|
2024-09-02 19:33:17 +08:00
|
|
|
type: 'aliyun',
|
|
|
|
|
},
|
|
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
accessId!: string;
|
|
|
|
|
|
2024-10-25 16:51:36 +08:00
|
|
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
title: '证书名称',
|
|
|
|
|
helper: '上传后将以此名称作为前缀备注',
|
|
|
|
|
})
|
|
|
|
|
certName!: string;
|
|
|
|
|
|
2025-05-26 23:10:31 +08:00
|
|
|
|
|
|
|
|
@TaskInput(
|
|
|
|
|
createRemoteSelectInputDefine({
|
|
|
|
|
title: 'DCDN加速域名',
|
|
|
|
|
helper: '你在阿里云上配置的DCDN加速域名,比如:certd.docmirror.cn',
|
|
|
|
|
action: DeployCertToAliyunDCDN.prototype.onGetDomainList.name,
|
|
|
|
|
watches: ['certDomains', 'accessId'],
|
|
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
domainName!: string | string[];
|
|
|
|
|
|
|
|
|
|
|
2026-02-09 14:29:19 +08:00
|
|
|
async onInstance() { }
|
2024-09-02 19:33:17 +08:00
|
|
|
async execute(): Promise<void> {
|
2024-09-02 23:46:28 +08:00
|
|
|
this.logger.info('开始部署证书到阿里云DCDN');
|
2026-02-09 14:29:19 +08:00
|
|
|
if (!this.domainName) {
|
2025-05-26 23:10:31 +08:00
|
|
|
throw new Error('您还未选择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-02-09 14:29:19 +08:00
|
|
|
if (typeof this.domainName === 'string') {
|
2025-05-26 23:10:31 +08:00
|
|
|
this.domainName = [this.domainName];
|
|
|
|
|
}
|
2026-02-09 14:29:19 +08:00
|
|
|
for (const domainName of this.domainName) {
|
2025-08-14 11:00:10 +08:00
|
|
|
this.logger.info(`[${domainName}]开始部署`)
|
2026-03-03 11:31:52 +08:00
|
|
|
const params = await this.buildParams(domainName);
|
2025-05-26 23:10:31 +08:00
|
|
|
await this.doRequest(client, params);
|
2026-03-03 11:31:52 +08:00
|
|
|
await this.ctx.utils.sleep(1000);
|
2025-08-14 11:00:10 +08:00
|
|
|
this.logger.info(`[${domainName}]部署成功`)
|
2025-05-26 23:10:31 +08:00
|
|
|
}
|
|
|
|
|
|
2024-09-02 23:46:28 +08:00
|
|
|
this.logger.info('部署完成');
|
2024-09-02 19:33:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getClient(access: AliyunAccess) {
|
2024-09-19 17:38:51 +08:00
|
|
|
const client = new AliyunClient({ logger: this.logger });
|
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,
|
|
|
|
|
endpoint: 'https://dcdn.aliyuncs.com',
|
2024-09-02 23:46:28 +08:00
|
|
|
apiVersion: '2018-01-15',
|
2024-09-19 17:38:51 +08:00
|
|
|
});
|
|
|
|
|
return client;
|
2024-09-02 19:33:17 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-03 11:31:52 +08:00
|
|
|
async buildParams(domainName: string) {
|
2024-09-02 19:33:17 +08:00
|
|
|
const CertName = (this.certName ?? 'certd') + '-' + dayjs().format('YYYYMMDDHHmmss');
|
2024-10-25 16:51:36 +08:00
|
|
|
|
2026-02-09 14:29:19 +08:00
|
|
|
let certId: any = this.cert
|
|
|
|
|
if (typeof this.cert === 'object') {
|
|
|
|
|
const certInfo = this.cert as CertInfo;
|
2026-02-15 13:08:20 +08:00
|
|
|
const casCertId = this.cert as CasCertId;
|
2026-02-09 14:29:19 +08:00
|
|
|
if (certInfo.crt) {
|
|
|
|
|
this.logger.info('上传证书:', CertName);
|
|
|
|
|
const cert: any = this.cert;
|
|
|
|
|
return {
|
2026-03-03 11:31:52 +08:00
|
|
|
DomainName: domainName,
|
2026-02-09 14:29:19 +08:00
|
|
|
SSLProtocol: 'on',
|
|
|
|
|
CertName: CertName,
|
|
|
|
|
CertType: 'upload',
|
|
|
|
|
SSLPub: cert.crt,
|
|
|
|
|
SSLPri: cert.key,
|
|
|
|
|
};
|
2026-02-15 13:08:20 +08:00
|
|
|
}else if (casCertId.certId){
|
|
|
|
|
certId = casCertId.certId;
|
|
|
|
|
}else{
|
|
|
|
|
throw new Error('证书格式错误'+JSON.stringify(this.cert));
|
2026-02-09 14:29:19 +08:00
|
|
|
}
|
2024-10-25 16:51:36 +08:00
|
|
|
}
|
2026-02-09 14:29:19 +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,
|
2024-09-02 19:33:17 +08:00
|
|
|
SSLProtocol: 'on',
|
2026-02-09 14:29:19 +08:00
|
|
|
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 = {
|
|
|
|
|
method: 'POST',
|
2024-09-02 23:46:28 +08:00
|
|
|
formatParams: false,
|
2024-09-02 19:33:17 +08:00
|
|
|
};
|
|
|
|
|
const ret: any = await client.request('SetDcdnDomainSSLCertificate', params, requestOption);
|
|
|
|
|
this.checkRet(ret);
|
|
|
|
|
this.logger.info('设置Dcdn证书成功:', ret.RequestId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkRet(ret: any) {
|
2025-01-19 15:31:37 +08:00
|
|
|
if (ret.Code != null) {
|
2024-09-02 19:33:17 +08:00
|
|
|
throw new Error('执行失败:' + ret.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-26 23:10:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async onGetDomainList(data: any) {
|
|
|
|
|
if (!this.accessId) {
|
|
|
|
|
throw new Error('请选择Access授权');
|
|
|
|
|
}
|
|
|
|
|
const access = await this.getAccess<AliyunAccess>(this.accessId);
|
|
|
|
|
|
|
|
|
|
const client = await this.getClient(access);
|
|
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
|
// 'DomainName': 'aaa',
|
|
|
|
|
PageSize: 500,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const requestOption = {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
formatParams: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const res = await client.request('DescribeDcdnUserDomains', params, requestOption);
|
|
|
|
|
this.checkRet(res);
|
|
|
|
|
const pageData = res?.Domains?.PageData;
|
|
|
|
|
if (!pageData || pageData.length === 0) {
|
|
|
|
|
throw new Error('找不到CDN域名,您可以手动输入');
|
|
|
|
|
}
|
|
|
|
|
const options = pageData.map((item: any) => {
|
|
|
|
|
return {
|
|
|
|
|
value: item.DomainName,
|
|
|
|
|
label: item.DomainName,
|
|
|
|
|
domain: item.DomainName,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
return optionsUtils.buildGroupOptions(options, this.certDomains);
|
|
|
|
|
}
|
2024-09-02 19:33:17 +08:00
|
|
|
}
|
|
|
|
|
new DeployCertToAliyunDCDN();
|