2024-08-13 20:30:42 +08:00
|
|
|
import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput } from '@certd/pipeline';
|
2025-12-31 17:01:37 +08:00
|
|
|
import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from '@certd/plugin-lib';
|
|
|
|
|
import { AliyunAccess } from "../../../plugin-lib/aliyun/access/index.js";
|
2025-09-28 11:02:25 +08:00
|
|
|
import { optionsUtils } from '@certd/basic';
|
2025-05-22 23:21:32 +08:00
|
|
|
import { CertApplyPluginNames, CertReader } from "@certd/plugin-cert";
|
2025-12-31 17:01:37 +08:00
|
|
|
import { AliyunClient, AliyunSslClient } from "../../../plugin-lib/aliyun/lib/index.js";
|
2024-05-27 18:38:41 +08:00
|
|
|
@IsTaskPlugin({
|
|
|
|
|
name: 'DeployCertToAliyunCDN',
|
2024-12-26 01:32:52 +08:00
|
|
|
title: '阿里云-部署证书至CDN',
|
2025-03-24 21:27:31 +08:00
|
|
|
icon: 'svg:icon-aliyun',
|
2024-07-21 02:26:03 +08:00
|
|
|
group: pluginGroups.aliyun.key,
|
2024-09-02 23:46:28 +08:00
|
|
|
desc: '自动部署域名证书至阿里云CDN',
|
2024-05-27 18:38:41 +08:00
|
|
|
default: {
|
|
|
|
|
strategy: {
|
|
|
|
|
runStrategy: RunStrategy.SkipWhenSucceed,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
export class DeployCertToAliyunCDN extends AbstractTaskPlugin {
|
|
|
|
|
@TaskInput({
|
2024-11-05 11:38:27 +08:00
|
|
|
title: '证书服务接入点',
|
|
|
|
|
helper: '不会选就按默认',
|
|
|
|
|
value: 'cas.aliyuncs.com',
|
|
|
|
|
component: {
|
|
|
|
|
name: 'a-select',
|
|
|
|
|
options: [
|
|
|
|
|
{ value: 'cas.aliyuncs.com', label: '中国大陆' },
|
|
|
|
|
{ value: 'cas.ap-southeast-1.aliyuncs.com', label: '新加坡' },
|
|
|
|
|
{ value: 'cas.eu-central-1.aliyuncs.com', label: '德国(法兰克福)' },
|
|
|
|
|
],
|
|
|
|
|
},
|
2024-05-27 18:38:41 +08:00
|
|
|
required: true,
|
|
|
|
|
})
|
2024-11-05 11:38:27 +08:00
|
|
|
endpoint!: string;
|
2024-05-27 18:38:41 +08:00
|
|
|
|
|
|
|
|
@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-05-27 18:38:41 +08:00
|
|
|
},
|
2025-06-19 18:17:35 +08:00
|
|
|
template:false,
|
2024-05-27 18:38:41 +08:00
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
cert!: string;
|
|
|
|
|
|
2024-11-05 11:38:27 +08:00
|
|
|
@TaskInput(createCertDomainGetterInputDefine({ props: { required: false } }))
|
|
|
|
|
certDomains!: string[];
|
|
|
|
|
|
2024-05-27 18:38:41 +08:00
|
|
|
@TaskInput({
|
|
|
|
|
title: 'Access授权',
|
|
|
|
|
helper: '阿里云授权AccessKeyId、AccessKeySecret',
|
|
|
|
|
component: {
|
2024-10-07 03:21:16 +08:00
|
|
|
name: 'access-selector',
|
2024-05-27 18:38:41 +08:00
|
|
|
type: 'aliyun',
|
|
|
|
|
},
|
|
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
accessId!: string;
|
|
|
|
|
|
2024-11-05 11:38:27 +08:00
|
|
|
@TaskInput(
|
|
|
|
|
createRemoteSelectInputDefine({
|
|
|
|
|
title: 'CDN加速域名',
|
|
|
|
|
helper: '你在阿里云上配置的CDN加速域名,比如:certd.docmirror.cn',
|
|
|
|
|
typeName: 'DeployCertToAliyunCDN',
|
|
|
|
|
action: DeployCertToAliyunCDN.prototype.onGetDomainList.name,
|
|
|
|
|
watches: ['certDomains', 'accessId'],
|
|
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
domainName!: string | string[];
|
|
|
|
|
|
2025-04-12 23:58:38 +08:00
|
|
|
@TaskInput({
|
|
|
|
|
title: '证书所在地域',
|
|
|
|
|
helper: 'cn-hangzhou和ap-southeast-1,默认cn-hangzhou。国际站用户建议使用ap-southeast-1。',
|
|
|
|
|
value:"cn-hangzhou",
|
|
|
|
|
component: {
|
|
|
|
|
name: 'a-select',
|
|
|
|
|
options:[
|
|
|
|
|
{value:'cn-hangzhou',label:'中国大陆'},
|
|
|
|
|
{value:'ap-southeast-1',label:'新加坡'}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
certRegion:string
|
|
|
|
|
|
2024-11-05 11:38:27 +08:00
|
|
|
@TaskInput({
|
|
|
|
|
title: '证书名称',
|
|
|
|
|
helper: '上传后将以此名称作为前缀备注',
|
|
|
|
|
})
|
|
|
|
|
certName!: string;
|
|
|
|
|
|
2025-04-12 23:58:38 +08:00
|
|
|
|
|
|
|
|
|
2024-08-13 20:30:42 +08:00
|
|
|
async onInstance() {}
|
2024-05-27 18:38:41 +08:00
|
|
|
async execute(): Promise<void> {
|
2024-09-03 18:21:02 +08:00
|
|
|
this.logger.info('开始部署证书到阿里云cdn');
|
2025-04-12 00:14:55 +08:00
|
|
|
const access = await this.getAccess<AliyunAccess>(this.accessId);
|
2024-11-05 11:38:27 +08:00
|
|
|
const sslClient = new AliyunSslClient({
|
|
|
|
|
access,
|
|
|
|
|
logger: this.logger,
|
|
|
|
|
endpoint: this.endpoint || 'cas.aliyuncs.com',
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-07 18:22:39 +08:00
|
|
|
if(this.cert == null){
|
|
|
|
|
throw new Error('域名证书参数为空,请检查前置任务')
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-05 11:38:27 +08:00
|
|
|
let certId: any = this.cert;
|
2025-04-12 23:58:38 +08:00
|
|
|
|
2025-05-22 23:21:32 +08:00
|
|
|
let certName = this.appendTimeSuffix(this.certName);
|
2025-04-12 23:58:38 +08:00
|
|
|
|
2024-11-05 11:38:27 +08:00
|
|
|
if (typeof this.cert === 'object') {
|
2025-05-22 23:21:32 +08:00
|
|
|
// @ts-ignore
|
|
|
|
|
const certName = this.buildCertName(CertReader.getMainDomain(this.cert.crt))
|
2026-01-31 19:36:37 +08:00
|
|
|
const certIdRes = await sslClient.uploadCertificate({
|
2025-04-12 23:58:38 +08:00
|
|
|
name:certName,
|
2024-11-05 11:38:27 +08:00
|
|
|
cert: this.cert,
|
|
|
|
|
});
|
2026-01-31 19:36:37 +08:00
|
|
|
certId = certIdRes.certId as any;
|
2024-11-05 11:38:27 +08:00
|
|
|
}
|
|
|
|
|
|
2024-09-02 18:36:12 +08:00
|
|
|
const client = await this.getClient(access);
|
2024-11-05 11:38:27 +08:00
|
|
|
|
|
|
|
|
if (typeof this.domainName === 'string') {
|
|
|
|
|
this.domainName = [this.domainName];
|
|
|
|
|
}
|
|
|
|
|
for (const domain of this.domainName) {
|
|
|
|
|
await this.SetCdnDomainSSLCertificate(client, {
|
|
|
|
|
CertId: certId,
|
|
|
|
|
DomainName: domain,
|
2025-04-12 23:58:38 +08:00
|
|
|
CertName: certName,
|
|
|
|
|
CertRegion:this.certRegion || 'cn-hangzhou',
|
2024-11-05 11:38:27 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-03 18:21:02 +08:00
|
|
|
this.logger.info('部署完成');
|
2024-05-27 18:38:41 +08:00
|
|
|
}
|
|
|
|
|
|
2024-09-02 18:36:12 +08:00
|
|
|
async getClient(access: AliyunAccess) {
|
2024-11-05 11:38:27 +08:00
|
|
|
const client = new AliyunClient({ logger: this.logger });
|
2024-09-05 18:00:45 +08:00
|
|
|
await client.init({
|
2024-05-27 18:38:41 +08:00
|
|
|
accessKeyId: access.accessKeyId,
|
|
|
|
|
accessKeySecret: access.accessKeySecret,
|
|
|
|
|
endpoint: 'https://cdn.aliyuncs.com',
|
|
|
|
|
apiVersion: '2018-05-10',
|
2024-11-05 11:38:27 +08:00
|
|
|
});
|
|
|
|
|
return client;
|
2024-05-27 18:38:41 +08:00
|
|
|
}
|
|
|
|
|
|
2025-04-12 23:58:38 +08:00
|
|
|
async SetCdnDomainSSLCertificate(client: any, params: { CertId: number; DomainName: string,CertName:string,CertRegion:string }) {
|
2026-02-06 23:35:35 +08:00
|
|
|
this.logger.info('设置CDN: ',JSON.stringify(params));
|
2024-05-27 18:38:41 +08:00
|
|
|
const requestOption = {
|
|
|
|
|
method: 'POST',
|
2024-11-05 11:38:27 +08:00
|
|
|
formatParams: false,
|
2024-05-27 18:38:41 +08:00
|
|
|
};
|
2024-11-05 11:38:27 +08:00
|
|
|
|
|
|
|
|
const ret: any = await client.request(
|
|
|
|
|
'SetCdnDomainSSLCertificate',
|
|
|
|
|
{
|
|
|
|
|
SSLProtocol: 'on',
|
2025-04-07 18:22:39 +08:00
|
|
|
CertType:"cas",
|
2024-11-05 11:38:27 +08:00
|
|
|
...params,
|
|
|
|
|
},
|
|
|
|
|
requestOption
|
|
|
|
|
);
|
2024-05-27 18:38:41 +08:00
|
|
|
this.checkRet(ret);
|
2024-11-05 11:38:27 +08:00
|
|
|
this.logger.info(`设置CDN: ${params.DomainName} 证书成功:`, ret.RequestId);
|
2024-05-27 18:38:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkRet(ret: any) {
|
2025-01-19 15:31:37 +08:00
|
|
|
if (ret.Code != null) {
|
2024-05-27 18:38:41 +08:00
|
|
|
throw new Error('执行失败:' + ret.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-05 11:38:27 +08:00
|
|
|
|
|
|
|
|
async onGetDomainList(data: any) {
|
|
|
|
|
if (!this.accessId) {
|
|
|
|
|
throw new Error('请选择Access授权');
|
|
|
|
|
}
|
2025-04-12 00:14:55 +08:00
|
|
|
const access = await this.getAccess<AliyunAccess>(this.accessId);
|
2024-11-05 11:38:27 +08:00
|
|
|
|
|
|
|
|
const client = await this.getClient(access);
|
|
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
|
// 'DomainName': 'aaa',
|
|
|
|
|
PageSize: 500,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const requestOption = {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
formatParams: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const res = await client.request('DescribeUserDomains', 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-05-27 18:38:41 +08:00
|
|
|
}
|
|
|
|
|
new DeployCertToAliyunCDN();
|