mirror of https://github.com/certd/certd
perf: 阿里云CLB支持部署到扩展域名
parent
0b78030c59
commit
0e8339c701
|
@ -1,13 +1,21 @@
|
||||||
import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput } from '@certd/pipeline';
|
import {AbstractTaskPlugin, IsTaskPlugin, PageReq, pluginGroups, RunStrategy, TaskInput} from '@certd/pipeline';
|
||||||
import { CertInfo } from '@certd/plugin-cert';
|
import {CertInfo} from '@certd/plugin-cert';
|
||||||
import { AliyunAccess, AliyunClient, AliyunSslClient, CasCertInfo, createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from '@certd/plugin-lib';
|
import {
|
||||||
import { CertApplyPluginNames} from '@certd/plugin-cert';
|
AliyunAccess,
|
||||||
|
AliyunClient,
|
||||||
|
AliyunSslClient,
|
||||||
|
CasCertInfo,
|
||||||
|
createCertDomainGetterInputDefine,
|
||||||
|
createRemoteSelectInputDefine
|
||||||
|
} from '@certd/plugin-lib';
|
||||||
|
import {CertApplyPluginNames} from '@certd/plugin-cert';
|
||||||
|
|
||||||
@IsTaskPlugin({
|
@IsTaskPlugin({
|
||||||
name: 'AliyunDeployCertToSLB',
|
name: 'AliyunDeployCertToSLB',
|
||||||
title: '阿里云-部署至SLB(传统负载均衡)',
|
title: '阿里云-部署至CLB(传统负载均衡)',
|
||||||
icon: 'svg:icon-aliyun',
|
icon: 'svg:icon-aliyun',
|
||||||
group: pluginGroups.aliyun.key,
|
group: pluginGroups.aliyun.key,
|
||||||
desc: '部署证书到阿里云SLB(传统负载均衡)',
|
desc: '部署证书到阿里云CLB(传统负载均衡)',
|
||||||
needPlus: false,
|
needPlus: false,
|
||||||
default: {
|
default: {
|
||||||
strategy: {
|
strategy: {
|
||||||
|
@ -27,9 +35,27 @@ export class AliyunDeployCertToSLB extends AbstractTaskPlugin {
|
||||||
})
|
})
|
||||||
cert!: CertInfo | number;
|
cert!: CertInfo | number;
|
||||||
|
|
||||||
@TaskInput(createCertDomainGetterInputDefine({ props: { required: false } }))
|
@TaskInput(createCertDomainGetterInputDefine({props: {required: false}}))
|
||||||
certDomains!: string[];
|
certDomains!: string[];
|
||||||
|
|
||||||
|
|
||||||
|
@TaskInput({
|
||||||
|
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({
|
@TaskInput({
|
||||||
title: 'Access授权',
|
title: 'Access授权',
|
||||||
helper: '阿里云授权AccessKeyId、AccessKeySecret',
|
helper: '阿里云授权AccessKeyId、AccessKeySecret',
|
||||||
|
@ -41,10 +67,11 @@ export class AliyunDeployCertToSLB extends AbstractTaskPlugin {
|
||||||
})
|
})
|
||||||
accessId!: string;
|
accessId!: string;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@TaskInput(
|
@TaskInput(
|
||||||
createRemoteSelectInputDefine({
|
createRemoteSelectInputDefine({
|
||||||
title: 'LB所在地区',
|
title: 'LB所在地区',
|
||||||
typeName: 'AliyunDeployCertToSLB',
|
|
||||||
multi: false,
|
multi: false,
|
||||||
action: AliyunDeployCertToSLB.prototype.onGetRegionList.name,
|
action: AliyunDeployCertToSLB.prototype.onGetRegionList.name,
|
||||||
watches: ['accessId'],
|
watches: ['accessId'],
|
||||||
|
@ -56,7 +83,6 @@ export class AliyunDeployCertToSLB extends AbstractTaskPlugin {
|
||||||
createRemoteSelectInputDefine({
|
createRemoteSelectInputDefine({
|
||||||
title: '负载均衡列表',
|
title: '负载均衡列表',
|
||||||
helper: '要部署证书的负载均衡ID',
|
helper: '要部署证书的负载均衡ID',
|
||||||
typeName: 'AliyunDeployCertToSLB',
|
|
||||||
action: AliyunDeployCertToSLB.prototype.onGetLoadBalanceList.name,
|
action: AliyunDeployCertToSLB.prototype.onGetLoadBalanceList.name,
|
||||||
watches: ['regionId'],
|
watches: ['regionId'],
|
||||||
})
|
})
|
||||||
|
@ -67,33 +93,59 @@ export class AliyunDeployCertToSLB extends AbstractTaskPlugin {
|
||||||
createRemoteSelectInputDefine({
|
createRemoteSelectInputDefine({
|
||||||
title: '监听器列表',
|
title: '监听器列表',
|
||||||
helper: '要部署证书的监听器列表',
|
helper: '要部署证书的监听器列表',
|
||||||
typeName: 'AliyunDeployCertToSLB',
|
|
||||||
action: AliyunDeployCertToSLB.prototype.onGetListenerList.name,
|
action: AliyunDeployCertToSLB.prototype.onGetListenerList.name,
|
||||||
watches: ['loadBalancers'],
|
watches: ['loadBalancers'],
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
listeners!: string[];
|
listeners!: string[];
|
||||||
|
|
||||||
@TaskInput({
|
|
||||||
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;
|
|
||||||
|
|
||||||
async onInstance() {}
|
@TaskInput({
|
||||||
|
title: "部署默认证书",
|
||||||
|
value: true,
|
||||||
|
component: {
|
||||||
|
name: "a-switch",
|
||||||
|
vModel: "checked"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
deployDefault!: boolean;
|
||||||
|
|
||||||
|
@TaskInput({
|
||||||
|
title: "部署扩展证书",
|
||||||
|
value: false,
|
||||||
|
component: {
|
||||||
|
name: "a-switch",
|
||||||
|
vModel: "checked"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
deployExtension!: boolean;
|
||||||
|
|
||||||
|
|
||||||
|
@TaskInput(
|
||||||
|
createRemoteSelectInputDefine({
|
||||||
|
title: '扩展域名列表',
|
||||||
|
helper: '要部署扩展域名列表',
|
||||||
|
action: AliyunDeployCertToSLB.prototype.onGetExtensionDomainList.name,
|
||||||
|
watches: ['listeners','deployExtension'],
|
||||||
|
mergeScript:`
|
||||||
|
return {
|
||||||
|
show: ctx.compute(({form})=>{
|
||||||
|
return form.deployExtension;
|
||||||
|
})
|
||||||
|
}
|
||||||
|
`
|
||||||
|
})
|
||||||
|
)
|
||||||
|
extensionDomains!: string[];
|
||||||
|
|
||||||
|
|
||||||
|
async onInstance() {
|
||||||
|
}
|
||||||
|
|
||||||
async getLBClient(access: AliyunAccess, region: string) {
|
async getLBClient(access: AliyunAccess, region: string) {
|
||||||
const client = new AliyunClient({ logger: this.logger });
|
const client = new AliyunClient({logger: this.logger});
|
||||||
const version = '2014-05-15';
|
const version = '2014-05-15';
|
||||||
await client.init({
|
await client.init({
|
||||||
accessKeyId: access.accessKeyId,
|
accessKeyId: access.accessKeyId,
|
||||||
|
@ -106,30 +158,76 @@ export class AliyunDeployCertToSLB extends AbstractTaskPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
async execute(): Promise<void> {
|
async execute(): Promise<void> {
|
||||||
this.logger.info(`开始部署证书到阿里云(slb)`);
|
this.logger.info(`开始部署证书到阿里云(clb)`);
|
||||||
const access = await this.getAccess<AliyunAccess>(this.accessId);
|
const access = await this.getAccess<AliyunAccess>(this.accessId);
|
||||||
|
|
||||||
const client = await this.getLBClient(access, this.regionId);
|
const client = await this.getLBClient(access, this.regionId);
|
||||||
const aliyunCert = await this.getAliyunCertId(access);
|
const aliyunCert = await this.getAliyunCertId(access);
|
||||||
const slbServerCertId = await this.uploadServerCert(client, aliyunCert);
|
const slbServerCertId = await this.uploadServerCert(client, aliyunCert);
|
||||||
for (const listener of this.listeners) {
|
|
||||||
const arr = listener.split('_');
|
|
||||||
const loadBalanceId = arr[0];
|
|
||||||
const port = arr[2];
|
|
||||||
const params = {
|
|
||||||
RegionId: this.regionId,
|
|
||||||
LoadBalancerId: loadBalanceId,
|
|
||||||
ListenerPort: parseInt(port),
|
|
||||||
ServerCertificateId: slbServerCertId,
|
|
||||||
};
|
|
||||||
|
|
||||||
const res = await client.request('SetLoadBalancerHTTPSListenerAttribute', params);
|
if (this.deployDefault!==false) {
|
||||||
this.checkRet(res);
|
this.logger.info("部署监听器默认证书")
|
||||||
this.logger.info(`部署${listener}监听器证书成功`, JSON.stringify(res));
|
for (const listener of this.listeners) {
|
||||||
|
const {port, loadBalanceId} = this.resolveListenerKey(listener)
|
||||||
|
const params = {
|
||||||
|
RegionId: this.regionId,
|
||||||
|
LoadBalancerId: loadBalanceId,
|
||||||
|
ListenerPort: port,
|
||||||
|
ServerCertificateId: slbServerCertId,
|
||||||
|
};
|
||||||
|
|
||||||
|
const res = await client.request('SetLoadBalancerHTTPSListenerAttribute', params);
|
||||||
|
this.checkRet(res);
|
||||||
|
this.logger.info(`部署${listener}监听器证书成功`, JSON.stringify(res));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.deployExtension) {
|
||||||
|
this.logger.info("部署监听器扩展域名证书")
|
||||||
|
|
||||||
|
const clientV2 = this.getCLBClientV2(access);
|
||||||
|
for (const domainStr of this.extensionDomains) {
|
||||||
|
const {extensionDomainId} = this.resolveListenerKey(domainStr)
|
||||||
|
const res = await clientV2.doRequest({
|
||||||
|
action: "SetDomainExtensionAttribute",
|
||||||
|
// 接口版本
|
||||||
|
version: "2014-05-15",
|
||||||
|
data: {
|
||||||
|
query: {
|
||||||
|
RegionId: this.regionId,
|
||||||
|
DomainExtensionId: extensionDomainId,
|
||||||
|
ServerCertificateId: slbServerCertId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
this.logger.info(`部署扩展域名${extensionDomainId}证书成功`, JSON.stringify(res))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.logger.info('执行完成');
|
this.logger.info('执行完成');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getCLBClientV2(access: AliyunAccess) {
|
||||||
|
return access.getClient("slb.aliyuncs.com")
|
||||||
|
}
|
||||||
|
|
||||||
|
resolveListenerKey(listener: string) {
|
||||||
|
const arr = listener.split('_');
|
||||||
|
const loadBalanceId = arr[0];
|
||||||
|
const protocol = arr[1];
|
||||||
|
const port = arr[2];
|
||||||
|
let extensionDomainId = undefined;
|
||||||
|
if (arr.length > 3) {
|
||||||
|
extensionDomainId = arr[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
loadBalanceId,
|
||||||
|
port: parseInt(port),
|
||||||
|
extensionDomainId: extensionDomainId,
|
||||||
|
protocol: protocol
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async uploadServerCert(client: any, aliyunCert: CasCertInfo) {
|
async uploadServerCert(client: any, aliyunCert: CasCertInfo) {
|
||||||
const params = {
|
const params = {
|
||||||
RegionId: this.regionId,
|
RegionId: this.regionId,
|
||||||
|
@ -248,6 +346,78 @@ export class AliyunDeployCertToSLB extends AbstractTaskPlugin {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async onGetExtensionDomainList(data: PageReq) {
|
||||||
|
if (!this.accessId) {
|
||||||
|
throw new Error('请先选择Access授权');
|
||||||
|
}
|
||||||
|
if (!this.regionId) {
|
||||||
|
throw new Error('请先选择地区');
|
||||||
|
}
|
||||||
|
if (!this.listeners && this.listeners.length == 0) {
|
||||||
|
throw new Error('请先选择监听器');
|
||||||
|
}
|
||||||
|
const access = await this.getAccess<AliyunAccess>(this.accessId);
|
||||||
|
|
||||||
|
const allDomains: any[] = []
|
||||||
|
for (const ls of this.listeners) {
|
||||||
|
const {port, loadBalanceId, protocol} = this.resolveListenerKey(ls)
|
||||||
|
const domains = await this.doGetExtensionDomainList({
|
||||||
|
access,
|
||||||
|
loadBalancerId: loadBalanceId,
|
||||||
|
listenerPort: port,
|
||||||
|
listenerProtocol: protocol,
|
||||||
|
});
|
||||||
|
allDomains.push(...domains)
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.ctx.utils.options.buildGroupOptions(allDomains, this.certDomains)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async doGetExtensionDomainList(data: {
|
||||||
|
loadBalancerId: string,
|
||||||
|
listenerPort: number,
|
||||||
|
listenerProtocol: string,
|
||||||
|
access: AliyunAccess
|
||||||
|
}) {
|
||||||
|
const {loadBalancerId, listenerPort, listenerProtocol, access} = data;
|
||||||
|
const client = access.getClient("slb.aliyuncs.com")
|
||||||
|
|
||||||
|
let queries = {
|
||||||
|
RegionId: this.regionId,
|
||||||
|
LoadBalancerId: loadBalancerId,
|
||||||
|
ListenerPort: listenerPort
|
||||||
|
};
|
||||||
|
|
||||||
|
const res = await client.doRequest({
|
||||||
|
// 接口名称
|
||||||
|
action: "DescribeDomainExtensions",
|
||||||
|
// 接口版本
|
||||||
|
version: "2014-05-15",
|
||||||
|
data: {
|
||||||
|
query: queries,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
this.checkRet(res);
|
||||||
|
const list = res?.DomainExtensions.DomainExtension;
|
||||||
|
if (!list || list.length === 0) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
|
return list.map((i: any) => {
|
||||||
|
const value = `${loadBalancerId}_${listenerProtocol ?? "HTTPS"}_${listenerPort}_${i.DomainExtensionId}`;
|
||||||
|
const label = `${i.DomainExtensionId}<${i.Domain}>`;
|
||||||
|
return {
|
||||||
|
value: value,
|
||||||
|
label: label,
|
||||||
|
domain:i.Domain
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
checkRet(ret: any) {
|
checkRet(ret: any) {
|
||||||
if (ret.Code != null) {
|
if (ret.Code != null) {
|
||||||
throw new Error(ret.Message);
|
throw new Error(ret.Message);
|
||||||
|
|
Loading…
Reference in New Issue