2023-06-25 23:25:56 +08:00
|
|
|
import { AbstractTaskPlugin, IAccessService, ILogger, IsTaskPlugin, RunStrategy, TaskInput, utils } from "@certd/pipeline";
|
2022-11-07 23:31:20 +08:00
|
|
|
import tencentcloud from "tencentcloud-sdk-nodejs/index";
|
|
|
|
|
import { TencentAccess } from "../../access";
|
|
|
|
|
import dayjs from "dayjs";
|
|
|
|
|
|
2022-12-27 12:32:09 +08:00
|
|
|
@IsTaskPlugin({
|
|
|
|
|
name: "DeployCertToTencentCLB",
|
|
|
|
|
title: "部署到腾讯云CLB",
|
|
|
|
|
desc: "暂时只支持单向认证证书,暂时只支持通用负载均衡",
|
|
|
|
|
default: {
|
|
|
|
|
strategy: {
|
|
|
|
|
runStrategy: RunStrategy.SkipWhenSucceed,
|
2022-11-07 23:31:20 +08:00
|
|
|
},
|
2022-12-27 12:32:09 +08:00
|
|
|
},
|
2022-11-07 23:31:20 +08:00
|
|
|
})
|
2023-05-24 15:41:35 +08:00
|
|
|
export class DeployToClbPlugin extends AbstractTaskPlugin {
|
2022-12-27 12:32:09 +08:00
|
|
|
@TaskInput({
|
|
|
|
|
title: "大区",
|
|
|
|
|
value: "ap-guangzhou",
|
|
|
|
|
component: {
|
|
|
|
|
name: "a-select",
|
|
|
|
|
options: [{ value: "ap-guangzhou" }],
|
|
|
|
|
},
|
|
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
region!: string;
|
2022-11-07 23:31:20 +08:00
|
|
|
|
2022-12-27 12:32:09 +08:00
|
|
|
@TaskInput({
|
|
|
|
|
title: "证书名称前缀",
|
|
|
|
|
})
|
|
|
|
|
certName!: string;
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
title: "负载均衡ID",
|
|
|
|
|
helper: "如果没有配置,则根据域名匹配负载均衡下的监听器(根据域名匹配时暂时只支持前100个)",
|
|
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
loadBalancerId!: string;
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
title: "监听器ID",
|
|
|
|
|
helper: "如果没有配置,则根据域名或负载均衡id匹配监听器",
|
|
|
|
|
})
|
|
|
|
|
listenerId!: string;
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
title: "域名",
|
|
|
|
|
required: true,
|
|
|
|
|
helper: "要更新的支持https的负载均衡的域名",
|
|
|
|
|
})
|
|
|
|
|
domain!: string;
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
title: "域名证书",
|
|
|
|
|
helper: "请选择前置任务输出的域名证书",
|
|
|
|
|
component: {
|
|
|
|
|
name: "pi-output-selector",
|
|
|
|
|
},
|
|
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
cert!: any;
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
title: "Access提供者",
|
|
|
|
|
helper: "access授权",
|
|
|
|
|
component: {
|
|
|
|
|
name: "pi-access-selector",
|
|
|
|
|
type: "tencent",
|
|
|
|
|
},
|
|
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
accessId!: string;
|
|
|
|
|
|
|
|
|
|
accessService!: IAccessService;
|
2023-01-11 20:39:48 +08:00
|
|
|
logger!: ILogger;
|
2022-12-27 12:32:09 +08:00
|
|
|
|
2023-06-25 23:25:56 +08:00
|
|
|
async onInstance() {
|
|
|
|
|
this.accessService = this.ctx.accessService;
|
|
|
|
|
this.logger = this.ctx.logger;
|
|
|
|
|
}
|
2022-12-27 12:32:09 +08:00
|
|
|
async execute(): Promise<void> {
|
|
|
|
|
const accessProvider = (await this.accessService.getById(this.accessId)) as TencentAccess;
|
|
|
|
|
const client = this.getClient(accessProvider, this.region);
|
|
|
|
|
|
|
|
|
|
const lastCertId = await this.getCertIdFromProps(client);
|
|
|
|
|
if (!this.domain) {
|
|
|
|
|
await this.updateListener(client);
|
2022-11-07 23:31:20 +08:00
|
|
|
} else {
|
2022-12-27 12:32:09 +08:00
|
|
|
await this.updateByDomainAttr(client);
|
2022-11-07 23:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await utils.sleep(2000);
|
2022-12-27 12:32:09 +08:00
|
|
|
let newCertId = await this.getCertIdFromProps(client);
|
2022-11-07 23:31:20 +08:00
|
|
|
if ((lastCertId && newCertId === lastCertId) || (!lastCertId && !newCertId)) {
|
|
|
|
|
await utils.sleep(2000);
|
2022-12-27 12:32:09 +08:00
|
|
|
newCertId = await this.getCertIdFromProps(client);
|
2022-11-07 23:31:20 +08:00
|
|
|
}
|
|
|
|
|
if (newCertId === lastCertId) {
|
2022-12-27 12:32:09 +08:00
|
|
|
return;
|
2022-11-07 23:31:20 +08:00
|
|
|
}
|
|
|
|
|
this.logger.info("腾讯云证书ID:", newCertId);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.logger.warn("查询腾讯云证书失败", e);
|
|
|
|
|
}
|
2022-12-27 12:32:09 +08:00
|
|
|
return;
|
2022-11-07 23:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
2022-12-27 12:32:09 +08:00
|
|
|
async getCertIdFromProps(client: any) {
|
|
|
|
|
const listenerRet = await this.getListenerList(client, this.loadBalancerId, [this.listenerId]);
|
|
|
|
|
return this.getCertIdFromListener(listenerRet[0], this.domain);
|
2022-11-07 23:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getCertIdFromListener(listener: any, domain: string) {
|
|
|
|
|
let certId;
|
|
|
|
|
if (!domain) {
|
|
|
|
|
certId = listener.Certificate.CertId;
|
|
|
|
|
} else {
|
|
|
|
|
if (listener.Rules && listener.Rules.length > 0) {
|
|
|
|
|
for (const rule of listener.Rules) {
|
|
|
|
|
if (rule.Domain === domain) {
|
|
|
|
|
if (rule.Certificate != null) {
|
|
|
|
|
certId = rule.Certificate.CertId;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return certId;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-27 12:32:09 +08:00
|
|
|
async updateListener(client: any) {
|
|
|
|
|
const params = this.buildProps();
|
2022-11-07 23:31:20 +08:00
|
|
|
const ret = await client.ModifyListener(params);
|
|
|
|
|
this.checkRet(ret);
|
2022-12-27 12:32:09 +08:00
|
|
|
this.logger.info("设置腾讯云CLB证书成功:", ret.RequestId, "->loadBalancerId:", this.loadBalancerId, "listenerId", this.listenerId);
|
2022-11-07 23:31:20 +08:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-27 12:32:09 +08:00
|
|
|
async updateByDomainAttr(client: any) {
|
|
|
|
|
const params: any = this.buildProps();
|
|
|
|
|
params.Domain = this.domain;
|
2022-11-07 23:31:20 +08:00
|
|
|
const ret = await client.ModifyDomainAttributes(params);
|
|
|
|
|
this.checkRet(ret);
|
|
|
|
|
this.logger.info(
|
|
|
|
|
"设置腾讯云CLB证书(sni)成功:",
|
|
|
|
|
ret.RequestId,
|
|
|
|
|
"->loadBalancerId:",
|
2022-12-27 12:32:09 +08:00
|
|
|
this.loadBalancerId,
|
2022-11-07 23:31:20 +08:00
|
|
|
"listenerId",
|
2022-12-27 12:32:09 +08:00
|
|
|
this.listenerId,
|
2022-11-07 23:31:20 +08:00
|
|
|
"domain:",
|
2022-12-27 12:32:09 +08:00
|
|
|
this.domain
|
2022-11-07 23:31:20 +08:00
|
|
|
);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
appendTimeSuffix(name: string) {
|
|
|
|
|
if (name == null) {
|
|
|
|
|
name = "certd";
|
|
|
|
|
}
|
|
|
|
|
return name + "-" + dayjs().format("YYYYMMDD-HHmmss");
|
|
|
|
|
}
|
2022-12-27 12:32:09 +08:00
|
|
|
buildProps() {
|
2022-11-07 23:31:20 +08:00
|
|
|
return {
|
|
|
|
|
Certificate: {
|
|
|
|
|
SSLMode: "UNIDIRECTIONAL", // 单向认证
|
2022-12-27 12:32:09 +08:00
|
|
|
CertName: this.appendTimeSuffix(this.certName || this.cert.domain),
|
|
|
|
|
CertKey: this.cert.key,
|
|
|
|
|
CertContent: this.cert.crt,
|
2022-11-07 23:31:20 +08:00
|
|
|
},
|
2022-12-27 12:32:09 +08:00
|
|
|
LoadBalancerId: this.loadBalancerId,
|
|
|
|
|
ListenerId: this.listenerId,
|
2022-11-07 23:31:20 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-27 12:32:09 +08:00
|
|
|
async getCLBList(client: any) {
|
2022-11-07 23:31:20 +08:00
|
|
|
const params = {
|
|
|
|
|
Limit: 100, // 最大暂时只支持100个,暂时没做翻页
|
|
|
|
|
OrderBy: "CreateTime",
|
|
|
|
|
OrderType: 0,
|
2022-12-27 12:32:09 +08:00
|
|
|
// ...this.DescribeLoadBalancers,
|
2022-11-07 23:31:20 +08:00
|
|
|
};
|
|
|
|
|
const ret = await client.DescribeLoadBalancers(params);
|
|
|
|
|
this.checkRet(ret);
|
|
|
|
|
return ret.LoadBalancerSet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getListenerList(client: any, balancerId: any, listenerIds: any) {
|
|
|
|
|
// HTTPS
|
|
|
|
|
const params = {
|
|
|
|
|
LoadBalancerId: balancerId,
|
|
|
|
|
Protocol: "HTTPS",
|
|
|
|
|
ListenerIds: listenerIds,
|
|
|
|
|
};
|
|
|
|
|
const ret = await client.DescribeListeners(params);
|
|
|
|
|
this.checkRet(ret);
|
|
|
|
|
return ret.Listeners;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getClient(accessProvider: TencentAccess, region: string) {
|
|
|
|
|
const ClbClient = tencentcloud.clb.v20180317.Client;
|
|
|
|
|
|
|
|
|
|
const clientConfig = {
|
|
|
|
|
credential: {
|
|
|
|
|
secretId: accessProvider.secretId,
|
|
|
|
|
secretKey: accessProvider.secretKey,
|
|
|
|
|
},
|
|
|
|
|
region: region,
|
|
|
|
|
profile: {
|
|
|
|
|
httpProfile: {
|
|
|
|
|
endpoint: "clb.tencentcloudapi.com",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return new ClbClient(clientConfig);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkRet(ret: any) {
|
|
|
|
|
if (!ret || ret.Error) {
|
|
|
|
|
throw new Error("执行失败:" + ret.Error.Code + "," + ret.Error.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|