2024-11-04 16:39:02 +08:00
|
|
|
|
import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput } from '@certd/pipeline';
|
2024-05-27 18:38:41 +08:00
|
|
|
|
import dayjs from 'dayjs';
|
2025-12-31 17:01:37 +08:00
|
|
|
|
import { TencentAccess } from '../../../plugin-lib/tencent/index.js';
|
2025-03-18 00:52:50 +08:00
|
|
|
|
import { CertApplyPluginNames} from '@certd/plugin-cert';
|
2022-12-27 12:32:09 +08:00
|
|
|
|
@IsTaskPlugin({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
name: 'DeployCertToTencentCLB',
|
2024-12-26 01:32:52 +08:00
|
|
|
|
title: '腾讯云-部署到CLB',
|
2024-09-19 17:38:51 +08:00
|
|
|
|
icon: 'svg:icon-tencentcloud',
|
2024-07-21 02:26:03 +08:00
|
|
|
|
group: pluginGroups.tencent.key,
|
2025-01-06 23:47:08 +08:00
|
|
|
|
desc: '暂时只支持单向认证证书,暂时只支持通用负载均衡',
|
2022-12-27 12:32:09 +08:00
|
|
|
|
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
|
|
|
|
})
|
2024-10-26 18:10:19 +08:00
|
|
|
|
export class DeployCertToTencentCLB extends AbstractTaskPlugin {
|
2022-12-27 12:32:09 +08:00
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '大区',
|
2022-12-27 12:32:09 +08:00
|
|
|
|
component: {
|
2024-08-13 20:30:42 +08:00
|
|
|
|
name: 'a-auto-complete',
|
|
|
|
|
|
vModel: 'value',
|
2024-07-31 14:01:06 +08:00
|
|
|
|
options: [
|
|
|
|
|
|
{ value: 'ap-guangzhou' },
|
|
|
|
|
|
{ value: 'ap-beijing' },
|
|
|
|
|
|
{ value: 'ap-chengdu' },
|
|
|
|
|
|
{ value: 'ap-chongqing' },
|
|
|
|
|
|
{ value: 'ap-hongkong' },
|
|
|
|
|
|
{ value: 'ap-jakarta' },
|
|
|
|
|
|
{ value: 'ap-mumbai' },
|
|
|
|
|
|
{ value: 'ap-nanjing' },
|
|
|
|
|
|
{ value: 'ap-seoul' },
|
|
|
|
|
|
{ value: 'ap-shanghai' },
|
|
|
|
|
|
{ value: 'ap-shanghai-fsi' },
|
|
|
|
|
|
{ value: 'ap-shenzhen-fsi' },
|
|
|
|
|
|
{ value: 'ap-singapore' },
|
|
|
|
|
|
{ value: 'ap-tokyo' },
|
|
|
|
|
|
{ value: 'eu-frankfurt' },
|
|
|
|
|
|
{ value: 'na-ashburn' },
|
|
|
|
|
|
{ value: 'na-siliconvalley' },
|
|
|
|
|
|
{ value: 'na-toronto' },
|
|
|
|
|
|
{ value: 'sa-saopaulo' },
|
|
|
|
|
|
],
|
2022-12-27 12:32:09 +08:00
|
|
|
|
},
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
})
|
|
|
|
|
|
region!: string;
|
2022-11-07 23:31:20 +08:00
|
|
|
|
|
2022-12-27 12:32:09 +08:00
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '证书名称前缀',
|
2022-12-27 12:32:09 +08:00
|
|
|
|
})
|
|
|
|
|
|
certName!: string;
|
|
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '负载均衡ID',
|
2024-07-21 02:26:03 +08:00
|
|
|
|
helper: '如果没有配置,则根据域名匹配负载均衡下的监听器(根据域名匹配时暂时只支持前100个)',
|
2022-12-27 12:32:09 +08:00
|
|
|
|
required: true,
|
|
|
|
|
|
})
|
|
|
|
|
|
loadBalancerId!: string;
|
|
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '监听器ID',
|
2025-01-06 23:47:08 +08:00
|
|
|
|
required: true,
|
2022-12-27 12:32:09 +08:00
|
|
|
|
})
|
|
|
|
|
|
listenerId!: string;
|
|
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '域名',
|
2025-01-06 23:47:08 +08:00
|
|
|
|
required: false,
|
|
|
|
|
|
component: {
|
|
|
|
|
|
name: 'a-select',
|
|
|
|
|
|
vModel: 'value',
|
|
|
|
|
|
open: false,
|
|
|
|
|
|
mode: 'tags',
|
|
|
|
|
|
},
|
|
|
|
|
|
helper: '如果开启了sni,则此项必须填写,未开启,则不要填写',
|
2022-12-27 12:32:09 +08:00
|
|
|
|
})
|
2025-01-06 23:47:08 +08:00
|
|
|
|
domain!: string | string[];
|
2022-12-27 12:32:09 +08:00
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '域名证书',
|
|
|
|
|
|
helper: '请选择前置任务输出的域名证书',
|
2022-12-27 12:32:09 +08:00
|
|
|
|
component: {
|
2024-10-07 03:21:16 +08:00
|
|
|
|
name: 'output-selector',
|
2025-03-18 00:52:50 +08:00
|
|
|
|
from: [...CertApplyPluginNames],
|
2022-12-27 12:32:09 +08:00
|
|
|
|
},
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
})
|
|
|
|
|
|
cert!: any;
|
|
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: 'Access提供者',
|
|
|
|
|
|
helper: 'access授权',
|
2022-12-27 12:32:09 +08:00
|
|
|
|
component: {
|
2024-10-07 03:21:16 +08:00
|
|
|
|
name: 'access-selector',
|
2024-05-27 18:38:41 +08:00
|
|
|
|
type: 'tencent',
|
2022-12-27 12:32:09 +08:00
|
|
|
|
},
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
})
|
|
|
|
|
|
accessId!: string;
|
|
|
|
|
|
|
2024-08-28 14:40:50 +08:00
|
|
|
|
client: any;
|
|
|
|
|
|
async onInstance() {
|
|
|
|
|
|
this.client = await this.getClient();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async getClient() {
|
2024-08-30 18:50:53 +08:00
|
|
|
|
const sdk = await import('tencentcloud-sdk-nodejs/tencentcloud/services/clb/v20180317/index.js');
|
2024-09-09 10:39:29 +08:00
|
|
|
|
const ClbClient = sdk.v20180317.Client;
|
2024-08-28 14:40:50 +08:00
|
|
|
|
|
2025-04-12 00:14:55 +08:00
|
|
|
|
const accessProvider = (await this.getAccess(this.accessId)) as TencentAccess;
|
2022-12-27 12:32:09 +08:00
|
|
|
|
|
2024-08-28 14:40:50 +08:00
|
|
|
|
const region = this.region;
|
|
|
|
|
|
const clientConfig = {
|
|
|
|
|
|
credential: {
|
|
|
|
|
|
secretId: accessProvider.secretId,
|
|
|
|
|
|
secretKey: accessProvider.secretKey,
|
|
|
|
|
|
},
|
|
|
|
|
|
region: region,
|
|
|
|
|
|
profile: {
|
|
|
|
|
|
httpProfile: {
|
2025-08-25 16:19:37 +08:00
|
|
|
|
endpoint: `clb.${accessProvider.intlDomain()}tencentcloudapi.com`,
|
2024-08-28 14:40:50 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-09-09 10:39:29 +08:00
|
|
|
|
return new ClbClient(clientConfig);
|
2024-08-28 14:40:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async execute(): Promise<void> {
|
|
|
|
|
|
const client = this.client;
|
2025-01-06 23:47:08 +08:00
|
|
|
|
|
|
|
|
|
|
if (!this.domain || this.domain.length === 0) {
|
2022-12-27 12:32:09 +08:00
|
|
|
|
await this.updateListener(client);
|
2022-11-07 23:31:20 +08:00
|
|
|
|
} else {
|
2025-01-06 23:47:08 +08:00
|
|
|
|
const domains = Array.isArray(this.domain) ? this.domain : [this.domain];
|
|
|
|
|
|
for (const domain of domains) {
|
|
|
|
|
|
this.logger.info(`开始更新域名证书:${domain},请确保已经开启了sni`);
|
2025-01-20 11:53:52 +08:00
|
|
|
|
// const lastCertId = await this.getCertIdFromProps(client, domain);
|
2025-01-06 23:47:08 +08:00
|
|
|
|
|
|
|
|
|
|
await this.updateByDomainAttr(client, domain);
|
|
|
|
|
|
|
2025-01-20 11:53:52 +08:00
|
|
|
|
// 不要做检查,相同的证书,不会生成新的证书id
|
|
|
|
|
|
// const checkDeployed = async (wait = 5) => {
|
|
|
|
|
|
// await this.ctx.utils.sleep(wait * 1000);
|
|
|
|
|
|
// this.logger.info(`等待${wait}秒`);
|
|
|
|
|
|
// const newCertId = await this.getCertIdFromProps(client, domain);
|
|
|
|
|
|
// this.logger.info(`oldCertId:${lastCertId} , newCertId:${newCertId}`);
|
|
|
|
|
|
// if ((lastCertId && newCertId === lastCertId) || (!lastCertId && !newCertId)) {
|
|
|
|
|
|
// return false;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// this.logger.info('腾讯云证书ID:', newCertId);
|
|
|
|
|
|
// return true;
|
|
|
|
|
|
// };
|
|
|
|
|
|
// let count = 0;
|
|
|
|
|
|
// while (true) {
|
|
|
|
|
|
// count++;
|
|
|
|
|
|
// const res = await checkDeployed(5);
|
|
|
|
|
|
// if (res) {
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// if (count > 6) {
|
|
|
|
|
|
// this.logger.warn('等待超时,可能证书未部署成功');
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
2022-11-07 23:31:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-06 23:47:08 +08:00
|
|
|
|
|
2022-12-27 12:32:09 +08:00
|
|
|
|
return;
|
2022-11-07 23:31:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-06 23:47:08 +08:00
|
|
|
|
async getCertIdFromProps(client: any, domain: string) {
|
|
|
|
|
|
const listenerRet = await this.getListenerList(client, this.loadBalancerId, this.listenerId ? [this.listenerId] : null);
|
|
|
|
|
|
return this.getCertIdFromListener(listenerRet[0], 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);
|
2024-07-21 02:26:03 +08:00
|
|
|
|
this.logger.info('设置腾讯云CLB证书成功:', ret.RequestId, '->loadBalancerId:', this.loadBalancerId, 'listenerId', this.listenerId);
|
2022-11-07 23:31:20 +08:00
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-06 23:47:08 +08:00
|
|
|
|
async updateByDomainAttr(client: any, domain) {
|
2022-12-27 12:32:09 +08:00
|
|
|
|
const params: any = this.buildProps();
|
2025-01-06 23:47:08 +08:00
|
|
|
|
|
|
|
|
|
|
params.Domain = domain;
|
2022-11-07 23:31:20 +08:00
|
|
|
|
const ret = await client.ModifyDomainAttributes(params);
|
|
|
|
|
|
this.checkRet(ret);
|
2025-02-14 00:42:25 +08:00
|
|
|
|
this.logger.info(
|
|
|
|
|
|
`[${domain}] 设置腾讯云CLB证书(sni)任务已提交:taskId:${ret.RequestId},loadBalancerId:${this.loadBalancerId},listenerId:${this.listenerId}`
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const requestId = ret.RequestId;
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
const statusRes = await client.DescribeTaskStatus({ TaskId: requestId });
|
|
|
|
|
|
|
|
|
|
|
|
if (statusRes.Status === 0) {
|
|
|
|
|
|
this.logger.info(`[${domain}] 腾讯云CLB证书(sni)设置成功`);
|
|
|
|
|
|
break;
|
|
|
|
|
|
} else if (statusRes.Status === 2) {
|
|
|
|
|
|
this.logger.info(`[${domain}] 腾讯云CLB证书(sni)设置进行中,请耐心等待`);
|
|
|
|
|
|
} else if (statusRes.Status === 1) {
|
|
|
|
|
|
throw new Error(`[${domain}] 腾讯云CLB证书(sni)设置失败:` + statusRes.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
await this.ctx.utils.sleep(5000);
|
|
|
|
|
|
}
|
2022-11-07 23:31:20 +08:00
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
appendTimeSuffix(name: string) {
|
|
|
|
|
|
if (name == null) {
|
2024-05-27 18:38:41 +08:00
|
|
|
|
name = 'certd';
|
2022-11-07 23:31:20 +08:00
|
|
|
|
}
|
2024-05-27 18:38:41 +08:00
|
|
|
|
return name + '-' + dayjs().format('YYYYMMDD-HHmmss');
|
2022-11-07 23:31:20 +08:00
|
|
|
|
}
|
2022-12-27 12:32:09 +08:00
|
|
|
|
buildProps() {
|
2022-11-07 23:31:20 +08:00
|
|
|
|
return {
|
|
|
|
|
|
Certificate: {
|
2024-05-27 18:38:41 +08:00
|
|
|
|
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个,暂时没做翻页
|
2024-05-27 18:38:41 +08:00
|
|
|
|
OrderBy: 'CreateTime',
|
2022-11-07 23:31:20 +08:00
|
|
|
|
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,
|
2024-05-27 18:38:41 +08:00
|
|
|
|
Protocol: 'HTTPS',
|
2022-11-07 23:31:20 +08:00
|
|
|
|
ListenerIds: listenerIds,
|
|
|
|
|
|
};
|
|
|
|
|
|
const ret = await client.DescribeListeners(params);
|
|
|
|
|
|
this.checkRet(ret);
|
|
|
|
|
|
return ret.Listeners;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
checkRet(ret: any) {
|
|
|
|
|
|
if (!ret || ret.Error) {
|
2024-05-27 18:38:41 +08:00
|
|
|
|
throw new Error('执行失败:' + ret.Error.Code + ',' + ret.Error.Message);
|
2022-11-07 23:31:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|