2025-06-07 00:15:16 +08:00
|
|
|
import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput } from "@certd/pipeline";
|
|
|
|
|
import { CertApplyPluginNames, CertInfo, CertReader } from "@certd/plugin-cert";
|
|
|
|
|
import {
|
|
|
|
|
AliyunAccess,
|
|
|
|
|
AliyunClient,
|
|
|
|
|
AliyunClientV2,
|
|
|
|
|
AliyunSslClient,
|
|
|
|
|
createCertDomainGetterInputDefine,
|
|
|
|
|
createRemoteSelectInputDefine
|
|
|
|
|
} from "@certd/plugin-lib";
|
2025-01-19 15:31:37 +08:00
|
|
|
|
|
|
|
|
@IsTaskPlugin({
|
2025-06-07 00:15:16 +08:00
|
|
|
name: "AliyunDeployCertToALB",
|
|
|
|
|
title: "阿里云-部署至ALB(应用负载均衡)",
|
|
|
|
|
icon: "svg:icon-aliyun",
|
2025-01-19 15:31:37 +08:00
|
|
|
group: pluginGroups.aliyun.key,
|
2025-06-07 00:15:16 +08:00
|
|
|
desc: "ALB,更新监听器的默认证书",
|
2025-01-19 22:55:46 +08:00
|
|
|
needPlus: false,
|
2025-01-19 15:31:37 +08:00
|
|
|
default: {
|
|
|
|
|
strategy: {
|
2025-06-07 00:15:16 +08:00
|
|
|
runStrategy: RunStrategy.SkipWhenSucceed
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-19 15:31:37 +08:00
|
|
|
})
|
2025-01-19 22:55:46 +08:00
|
|
|
export class AliyunDeployCertToALB extends AbstractTaskPlugin {
|
2025-01-19 15:31:37 +08:00
|
|
|
@TaskInput({
|
2025-06-07 00:15:16 +08:00
|
|
|
title: "域名证书",
|
|
|
|
|
helper: "请选择证书申请任务输出的域名证书\n或者选择前置任务“上传证书到阿里云”任务的证书ID,可以减少上传到阿里云的证书数量",
|
2025-01-19 15:31:37 +08:00
|
|
|
component: {
|
2025-06-07 00:15:16 +08:00
|
|
|
name: "output-selector",
|
|
|
|
|
from: [...CertApplyPluginNames, "uploadCertToAliyun"]
|
2025-01-19 15:31:37 +08:00
|
|
|
},
|
2025-06-07 00:15:16 +08:00
|
|
|
required: true
|
2025-01-19 15:31:37 +08:00
|
|
|
})
|
|
|
|
|
cert!: CertInfo | number;
|
|
|
|
|
|
|
|
|
|
@TaskInput(createCertDomainGetterInputDefine({ props: { required: false } }))
|
|
|
|
|
certDomains!: string[];
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
2025-06-07 00:15:16 +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: "德国(法兰克福)" }
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
required: true
|
|
|
|
|
})
|
|
|
|
|
casEndpoint!: string;
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
title: "Access授权",
|
|
|
|
|
helper: "阿里云授权AccessKeyId、AccessKeySecret",
|
2025-01-19 15:31:37 +08:00
|
|
|
component: {
|
2025-06-07 00:15:16 +08:00
|
|
|
name: "access-selector",
|
|
|
|
|
type: "aliyun"
|
2025-01-19 15:31:37 +08:00
|
|
|
},
|
2025-06-07 00:15:16 +08:00
|
|
|
required: true
|
2025-01-19 15:31:37 +08:00
|
|
|
})
|
|
|
|
|
accessId!: string;
|
|
|
|
|
|
|
|
|
|
@TaskInput(
|
|
|
|
|
createRemoteSelectInputDefine({
|
2025-06-07 00:15:16 +08:00
|
|
|
title: "ALB所在地区",
|
|
|
|
|
typeName: "AliyunDeployCertToALB",
|
2025-01-19 15:31:37 +08:00
|
|
|
multi: false,
|
|
|
|
|
action: AliyunDeployCertToALB.prototype.onGetRegionList.name,
|
2025-06-07 00:15:16 +08:00
|
|
|
watches: ["accessId"]
|
2025-01-19 15:31:37 +08:00
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
regionId: string;
|
|
|
|
|
|
|
|
|
|
@TaskInput(
|
|
|
|
|
createRemoteSelectInputDefine({
|
2025-06-07 00:15:16 +08:00
|
|
|
title: "负载均衡列表",
|
|
|
|
|
helper: "要部署证书的负载均衡ID",
|
|
|
|
|
typeName: "AliyunDeployCertToALB",
|
2025-01-19 15:31:37 +08:00
|
|
|
action: AliyunDeployCertToALB.prototype.onGetLoadBalanceList.name,
|
2025-06-07 00:15:16 +08:00
|
|
|
watches: ["regionId"]
|
2025-01-19 15:31:37 +08:00
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
loadBalancers!: string[];
|
|
|
|
|
|
|
|
|
|
@TaskInput(
|
|
|
|
|
createRemoteSelectInputDefine({
|
2025-06-07 00:15:16 +08:00
|
|
|
title: "监听器列表",
|
|
|
|
|
helper: "要部署证书的监听器列表",
|
|
|
|
|
typeName: "AliyunDeployCertToALB",
|
2025-01-19 15:31:37 +08:00
|
|
|
action: AliyunDeployCertToALB.prototype.onGetListenerList.name,
|
2025-06-07 00:15:16 +08:00
|
|
|
watches: ["loadBalancers"]
|
2025-01-19 15:31:37 +08:00
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
listeners!: string[];
|
|
|
|
|
|
2025-06-07 00:15:16 +08:00
|
|
|
|
2025-01-19 15:31:37 +08:00
|
|
|
@TaskInput({
|
2025-06-07 00:15:16 +08:00
|
|
|
title: "部署证书类型",
|
|
|
|
|
value: "default",
|
|
|
|
|
component: {
|
|
|
|
|
name: "a-select",
|
|
|
|
|
vModel: "value",
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
label: "默认证书",
|
|
|
|
|
value: "default"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "扩展证书",
|
|
|
|
|
value: "extension"
|
|
|
|
|
}
|
|
|
|
|
]
|
2025-06-09 23:41:44 +08:00
|
|
|
},
|
|
|
|
|
required: true
|
2025-06-07 00:15:16 +08:00
|
|
|
}
|
|
|
|
|
)
|
2025-06-09 23:41:44 +08:00
|
|
|
deployType: string = "default";
|
2025-01-19 15:31:37 +08:00
|
|
|
|
2025-06-07 00:15:16 +08:00
|
|
|
|
|
|
|
|
async onInstance() {
|
|
|
|
|
}
|
2025-01-19 15:31:37 +08:00
|
|
|
|
2025-01-19 22:55:46 +08:00
|
|
|
async getLBClient(access: AliyunAccess, region: string) {
|
2025-01-19 15:31:37 +08:00
|
|
|
const client = new AliyunClient({ logger: this.logger });
|
2025-01-19 22:55:46 +08:00
|
|
|
|
2025-06-07 00:15:16 +08:00
|
|
|
const version = "2020-06-16";
|
2025-01-19 15:31:37 +08:00
|
|
|
await client.init({
|
|
|
|
|
accessKeyId: access.accessKeyId,
|
|
|
|
|
accessKeySecret: access.accessKeySecret,
|
|
|
|
|
//https://wafopenapi.cn-hangzhou.aliyuncs.com
|
|
|
|
|
endpoint: `https://alb.${region}.aliyuncs.com`,
|
2025-06-07 00:15:16 +08:00
|
|
|
apiVersion: version
|
2025-01-19 15:31:37 +08:00
|
|
|
});
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-07 00:15:16 +08:00
|
|
|
getALBClientV2(access: AliyunAccess) {
|
|
|
|
|
return access.getClient(`alb.${this.regionId}.aliyuncs.com`);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-19 15:31:37 +08:00
|
|
|
async execute(): Promise<void> {
|
2025-01-19 22:55:46 +08:00
|
|
|
this.logger.info(`开始部署证书到阿里云(alb)`);
|
2025-04-12 00:14:55 +08:00
|
|
|
const access = await this.getAccess<AliyunAccess>(this.accessId);
|
2025-01-19 15:31:37 +08:00
|
|
|
const certId = await this.getAliyunCertId(access);
|
|
|
|
|
|
2025-06-09 23:41:44 +08:00
|
|
|
//部署扩展证书
|
|
|
|
|
const albClientV2 = this.getALBClientV2(access);
|
2025-06-07 00:15:16 +08:00
|
|
|
if (this.deployType === "extension") {
|
2025-06-09 23:41:44 +08:00
|
|
|
await this.deployExtensionCert(albClientV2, certId);
|
2025-06-07 00:15:16 +08:00
|
|
|
} else {
|
|
|
|
|
const client = await this.getLBClient(access, this.regionId);
|
|
|
|
|
await this.deployDefaultCert(certId, client);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-09 23:41:44 +08:00
|
|
|
await this.ctx.utils.sleep(10000)
|
|
|
|
|
for (const listener of this.listeners) {
|
|
|
|
|
await this.clearInvalidCert(albClientV2, listener);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-07 00:15:16 +08:00
|
|
|
|
|
|
|
|
this.logger.info("执行完成");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async deployDefaultCert(certId: any, client: AliyunClient) {
|
2025-01-19 15:31:37 +08:00
|
|
|
for (const listener of this.listeners) {
|
2025-01-19 22:55:46 +08:00
|
|
|
let params: any = {};
|
|
|
|
|
params = {
|
2025-01-19 15:31:37 +08:00
|
|
|
ListenerId: listener,
|
|
|
|
|
Certificates: [
|
|
|
|
|
{
|
2025-06-07 00:15:16 +08:00
|
|
|
CertificateId: certId
|
|
|
|
|
}
|
|
|
|
|
]
|
2025-01-19 15:31:37 +08:00
|
|
|
};
|
|
|
|
|
|
2025-06-07 00:15:16 +08:00
|
|
|
const res = await client.request("UpdateListenerAttribute", params);
|
2025-01-19 15:31:37 +08:00
|
|
|
this.checkRet(res);
|
|
|
|
|
this.logger.info(`部署${listener}监听器证书成功`, JSON.stringify(res));
|
2025-06-07 00:15:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async deployExtensionCert(client: AliyunClientV2, certId: any) {
|
|
|
|
|
for (const listenerId of this.listeners) {
|
|
|
|
|
this.logger.info(`开始部署监听器${listenerId}的扩展证书`);
|
|
|
|
|
await client.doRequest({
|
|
|
|
|
// 接口名称
|
|
|
|
|
action: "AssociateAdditionalCertificatesWithListener",
|
|
|
|
|
// 接口版本
|
|
|
|
|
version: "2020-06-16",
|
|
|
|
|
data: {
|
|
|
|
|
query: {
|
|
|
|
|
ListenerId: listenerId,
|
|
|
|
|
Certificates: [
|
|
|
|
|
{
|
|
|
|
|
CertificateId: certId
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-01-19 15:31:37 +08:00
|
|
|
|
2025-06-07 00:15:16 +08:00
|
|
|
this.logger.info(`部署监听器${listenerId}的扩展证书成功`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async clearInvalidCert(client: AliyunClientV2, listener: string) {
|
2025-06-09 23:41:44 +08:00
|
|
|
this.logger.info(`开始清理监听器${listener}的过期证书`);
|
2025-06-07 00:15:16 +08:00
|
|
|
const req = {
|
|
|
|
|
// 接口名称
|
|
|
|
|
action: "ListListenerCertificates",
|
|
|
|
|
// 接口版本
|
|
|
|
|
version: "2020-06-16",
|
|
|
|
|
data: {
|
|
|
|
|
query: {
|
|
|
|
|
ListenerId: listener
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const res = await client.doRequest(req);
|
|
|
|
|
const list = res.Certificates;
|
|
|
|
|
if (list.length === 0) {
|
|
|
|
|
this.logger.info(`监听器${listener}没有绑定证书`);
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sslClient = new AliyunSslClient({
|
|
|
|
|
access: client.access,
|
|
|
|
|
logger: this.logger,
|
|
|
|
|
endpoint: this.casEndpoint
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const certIds = [];
|
|
|
|
|
for (const item of list) {
|
2025-11-18 18:01:23 +08:00
|
|
|
this.logger.info(`监听器${listener}绑定的证书${item.CertificateId},status:${item.Status},IsDefault:${item.IsDefault}`);
|
2025-06-07 00:15:16 +08:00
|
|
|
if (item.Status !== "Associated") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (item.IsDefault) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
certIds.push( parseInt(item.CertificateId));
|
|
|
|
|
}
|
2025-11-18 18:01:23 +08:00
|
|
|
this.logger.info(`监听器${listener}绑定的证书${certIds}`);
|
2025-06-07 00:15:16 +08:00
|
|
|
//检查是否过期,过期则删除
|
|
|
|
|
const invalidCertIds = [];
|
|
|
|
|
for (const certId of certIds) {
|
|
|
|
|
const res = await sslClient.getCertInfo(certId);
|
2025-11-18 18:01:23 +08:00
|
|
|
this.logger.info(`证书${certId}过期时间:${res.notAfter}`);
|
2025-06-07 00:15:16 +08:00
|
|
|
if (res.notAfter < new Date().getTime()) {
|
|
|
|
|
invalidCertIds.push(certId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (invalidCertIds.length === 0) {
|
|
|
|
|
this.logger.info(`监听器${listener}没有过期的证书`);
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
this.logger.info(`开始解绑过期的证书:${invalidCertIds}`);
|
|
|
|
|
await client.doRequest({
|
|
|
|
|
// 接口名称
|
|
|
|
|
action: "DissociateAdditionalCertificatesFromListener",
|
|
|
|
|
// 接口版本
|
|
|
|
|
version: "2020-06-16",
|
|
|
|
|
data: {
|
|
|
|
|
query: {
|
|
|
|
|
ListenerId: listener,
|
|
|
|
|
Certificates: invalidCertIds.map((item) => {
|
|
|
|
|
return {
|
|
|
|
|
CertificateId: item
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
this.logger.info(`解绑过期证书成功`);
|
2025-01-19 15:31:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getAliyunCertId(access: AliyunAccess) {
|
|
|
|
|
let certId: any = this.cert;
|
2025-06-07 00:15:16 +08:00
|
|
|
if (typeof this.cert === "object") {
|
2025-01-19 15:31:37 +08:00
|
|
|
const sslClient = new AliyunSslClient({
|
|
|
|
|
access,
|
|
|
|
|
logger: this.logger,
|
2025-06-07 00:15:16 +08:00
|
|
|
endpoint: this.casEndpoint
|
2025-01-19 15:31:37 +08:00
|
|
|
});
|
|
|
|
|
|
2025-06-07 00:15:16 +08:00
|
|
|
const certName = this.buildCertName(CertReader.getMainDomain(this.cert.crt));
|
2025-01-19 15:31:37 +08:00
|
|
|
certId = await sslClient.uploadCert({
|
2025-05-22 23:21:32 +08:00
|
|
|
name: certName,
|
2025-06-07 00:15:16 +08:00
|
|
|
cert: this.cert
|
2025-01-19 15:31:37 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return certId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async onGetRegionList(data: any) {
|
|
|
|
|
if (!this.accessId) {
|
2025-06-07 00:15:16 +08:00
|
|
|
throw new Error("请选择Access授权");
|
2025-01-19 15:31:37 +08:00
|
|
|
}
|
2025-04-12 00:14:55 +08:00
|
|
|
const access = await this.getAccess<AliyunAccess>(this.accessId);
|
2025-06-07 00:15:16 +08:00
|
|
|
const client = await this.getLBClient(access, "cn-shanghai");
|
2025-01-19 15:31:37 +08:00
|
|
|
|
2025-06-07 00:15:16 +08:00
|
|
|
const res = await client.request("DescribeRegions", {});
|
2025-01-19 15:31:37 +08:00
|
|
|
this.checkRet(res);
|
|
|
|
|
if (!res?.Regions || res?.Regions.length === 0) {
|
2025-06-07 00:15:16 +08:00
|
|
|
throw new Error("没有找到Regions列表");
|
2025-01-19 15:31:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res.Regions.map((item: any) => {
|
|
|
|
|
return {
|
|
|
|
|
label: item.LocalName,
|
|
|
|
|
value: item.RegionId,
|
2025-06-07 00:15:16 +08:00
|
|
|
endpoint: item.RegionEndpoint
|
2025-01-19 15:31:37 +08:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async onGetLoadBalanceList(data: any) {
|
|
|
|
|
if (!this.accessId) {
|
2025-06-07 00:15:16 +08:00
|
|
|
throw new Error("请先选择Access授权");
|
2025-01-19 15:31:37 +08:00
|
|
|
}
|
|
|
|
|
if (!this.regionId) {
|
2025-06-07 00:15:16 +08:00
|
|
|
throw new Error("请先选择地区");
|
2025-01-19 15:31:37 +08:00
|
|
|
}
|
2025-04-12 00:14:55 +08:00
|
|
|
const access = await this.getAccess<AliyunAccess>(this.accessId);
|
2025-01-19 22:55:46 +08:00
|
|
|
const client = await this.getLBClient(access, this.regionId);
|
2025-01-19 15:31:37 +08:00
|
|
|
|
|
|
|
|
const params = {
|
2025-06-07 00:15:16 +08:00
|
|
|
MaxResults: 100
|
2025-01-19 15:31:37 +08:00
|
|
|
};
|
2025-06-07 00:15:16 +08:00
|
|
|
const res = await client.request("ListLoadBalancers", params);
|
2025-01-19 15:31:37 +08:00
|
|
|
this.checkRet(res);
|
|
|
|
|
if (!res?.LoadBalancers || res?.LoadBalancers.length === 0) {
|
2025-06-07 00:15:16 +08:00
|
|
|
throw new Error("没有找到LoadBalancers");
|
2025-01-19 15:31:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res.LoadBalancers.map((item: any) => {
|
|
|
|
|
const label = `${item.LoadBalancerId}<${item.LoadBalancerName}}>`;
|
|
|
|
|
return {
|
|
|
|
|
label: label,
|
2025-06-07 00:15:16 +08:00
|
|
|
value: item.LoadBalancerId
|
2025-01-19 15:31:37 +08:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async onGetListenerList(data: any) {
|
|
|
|
|
if (!this.accessId) {
|
2025-06-07 00:15:16 +08:00
|
|
|
throw new Error("请先选择Access授权");
|
2025-01-19 15:31:37 +08:00
|
|
|
}
|
|
|
|
|
if (!this.regionId) {
|
2025-06-07 00:15:16 +08:00
|
|
|
throw new Error("请先选择地区");
|
2025-01-19 15:31:37 +08:00
|
|
|
}
|
2025-04-12 00:14:55 +08:00
|
|
|
const access = await this.getAccess<AliyunAccess>(this.accessId);
|
2025-01-19 22:55:46 +08:00
|
|
|
const client = await this.getLBClient(access, this.regionId);
|
2025-01-19 15:31:37 +08:00
|
|
|
|
|
|
|
|
const params: any = {
|
2025-06-07 00:15:16 +08:00
|
|
|
MaxResults: 100
|
2025-01-19 15:31:37 +08:00
|
|
|
};
|
|
|
|
|
if (this.loadBalancers && this.loadBalancers.length > 0) {
|
|
|
|
|
params.LoadBalancerIds = this.loadBalancers;
|
|
|
|
|
}
|
2025-06-07 00:15:16 +08:00
|
|
|
const res = await client.request("ListListeners", params);
|
2025-01-19 15:31:37 +08:00
|
|
|
this.checkRet(res);
|
|
|
|
|
if (!res?.Listeners || res?.Listeners.length === 0) {
|
2025-06-07 00:15:16 +08:00
|
|
|
throw new Error("没有找到HTTPS监听器");
|
2025-01-19 15:31:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res.Listeners.map((item: any) => {
|
2025-01-19 22:55:46 +08:00
|
|
|
const label = `${item.ListenerId}<${item.ListenerDescription}@${item.LoadBalancerId}>`;
|
2025-01-19 15:31:37 +08:00
|
|
|
return {
|
|
|
|
|
label: label,
|
|
|
|
|
value: item.ListenerId,
|
2025-06-07 00:15:16 +08:00
|
|
|
lbid: item.LoadBalancerId
|
2025-01-19 15:31:37 +08:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-07 00:15:16 +08:00
|
|
|
|
2025-01-19 15:31:37 +08:00
|
|
|
checkRet(ret: any) {
|
|
|
|
|
if (ret.Code != null) {
|
|
|
|
|
throw new Error(ret.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-07 00:15:16 +08:00
|
|
|
|
|
|
|
|
|
2025-01-19 15:31:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new AliyunDeployCertToALB();
|