2023-06-25 23:25:56 +08:00
|
|
|
|
import { AbstractTaskPlugin, IAccessService, ILogger, IsTaskPlugin, RunStrategy, TaskInput, utils } from "@certd/pipeline";
|
2022-12-27 12:32:09 +08:00
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
import { ROAClient } from "@alicloud/pop-core";
|
2022-11-08 22:48:57 +08:00
|
|
|
|
import { AliyunAccess } from "../../access";
|
|
|
|
|
|
import { K8sClient } from "@certd/plugin-util";
|
2023-05-25 00:41:36 +08:00
|
|
|
|
import { appendTimeSuffix } from "../../utils/index";
|
2023-01-11 20:39:48 +08:00
|
|
|
|
import { CertInfo } from "@certd/plugin-cert";
|
2022-11-08 22:48:57 +08:00
|
|
|
|
|
2023-01-11 20:39:48 +08:00
|
|
|
|
@IsTaskPlugin({
|
|
|
|
|
|
name: "DeployCertToAliyunAckIngress",
|
|
|
|
|
|
title: "部署到阿里云AckIngress",
|
|
|
|
|
|
input: {},
|
|
|
|
|
|
output: {},
|
|
|
|
|
|
default: {
|
|
|
|
|
|
strategy: {
|
|
|
|
|
|
runStrategy: RunStrategy.SkipWhenSucceed,
|
2022-11-08 22:48:57 +08:00
|
|
|
|
},
|
2023-01-11 20:39:48 +08:00
|
|
|
|
},
|
2022-11-08 22:48:57 +08:00
|
|
|
|
})
|
2023-05-24 15:41:35 +08:00
|
|
|
|
export class DeployCertToAliyunAckIngressPlugin extends AbstractTaskPlugin {
|
2023-01-11 20:39:48 +08:00
|
|
|
|
@TaskInput({
|
|
|
|
|
|
title: "集群id",
|
|
|
|
|
|
component: {
|
|
|
|
|
|
placeholder: "集群id",
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
clusterId!: string;
|
|
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
|
title: "保密字典Id",
|
|
|
|
|
|
component: {
|
|
|
|
|
|
placeholder: "保密字典Id",
|
|
|
|
|
|
},
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
})
|
|
|
|
|
|
secretName!: string | string[];
|
|
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
|
title: "大区",
|
|
|
|
|
|
value: "cn-shanghai",
|
|
|
|
|
|
component: {
|
|
|
|
|
|
placeholder: "集群所属大区",
|
|
|
|
|
|
},
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
})
|
|
|
|
|
|
regionId!: string;
|
|
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
|
title: "命名空间",
|
|
|
|
|
|
value: "default",
|
|
|
|
|
|
component: {
|
|
|
|
|
|
placeholder: "命名空间",
|
|
|
|
|
|
},
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
})
|
|
|
|
|
|
namespace!: string;
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
|
title: "ingress名称",
|
|
|
|
|
|
value: "",
|
|
|
|
|
|
component: {
|
|
|
|
|
|
placeholder: "ingress名称",
|
|
|
|
|
|
},
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
helper: "可以传入一个数组",
|
|
|
|
|
|
})
|
|
|
|
|
|
ingressName!: string;
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
|
title: "ingress类型",
|
|
|
|
|
|
value: "nginx",
|
|
|
|
|
|
component: {
|
|
|
|
|
|
placeholder: "暂时只支持nginx类型",
|
|
|
|
|
|
},
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
})
|
|
|
|
|
|
ingressClass!: string;
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
|
title: "是否私网ip",
|
|
|
|
|
|
value: false,
|
|
|
|
|
|
component: {
|
|
|
|
|
|
placeholder: "集群连接端点是否是私网ip",
|
|
|
|
|
|
},
|
|
|
|
|
|
helper: "如果您当前certd运行在同一个私网下,可以选择是。",
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
})
|
|
|
|
|
|
isPrivateIpAddress!: boolean;
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
|
title: "域名证书",
|
|
|
|
|
|
helper: "请选择前置任务输出的域名证书",
|
|
|
|
|
|
component: {
|
|
|
|
|
|
name: "pi-output-selector",
|
|
|
|
|
|
},
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
})
|
|
|
|
|
|
cert!: CertInfo;
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
|
title: "Access授权",
|
|
|
|
|
|
helper: "阿里云授权AccessKeyId、AccessKeySecret",
|
|
|
|
|
|
component: {
|
|
|
|
|
|
name: "pi-access-selector",
|
|
|
|
|
|
type: "aliyun",
|
|
|
|
|
|
},
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
})
|
|
|
|
|
|
accessId!: string;
|
|
|
|
|
|
|
|
|
|
|
|
accessService!: IAccessService;
|
|
|
|
|
|
logger!: ILogger;
|
|
|
|
|
|
|
2023-06-25 23:25:56 +08:00
|
|
|
|
async onInstance(): Promise<void> {
|
|
|
|
|
|
this.accessService = this.ctx.accessService;
|
|
|
|
|
|
this.logger = this.ctx.logger;
|
|
|
|
|
|
}
|
2023-01-11 20:39:48 +08:00
|
|
|
|
async execute(): Promise<void> {
|
2022-11-08 22:48:57 +08:00
|
|
|
|
console.log("开始部署证书到阿里云cdn");
|
2023-01-11 20:39:48 +08:00
|
|
|
|
const { regionId, ingressClass, clusterId, isPrivateIpAddress, cert } = this;
|
|
|
|
|
|
const access = (await this.accessService.getById(this.accessId)) as AliyunAccess;
|
2022-11-08 22:48:57 +08:00
|
|
|
|
const client = this.getClient(access, regionId);
|
|
|
|
|
|
const kubeConfigStr = await this.getKubeConfig(client, clusterId, isPrivateIpAddress);
|
|
|
|
|
|
|
|
|
|
|
|
this.logger.info("kubeconfig已成功获取");
|
|
|
|
|
|
const k8sClient = new K8sClient(kubeConfigStr);
|
|
|
|
|
|
const ingressType = ingressClass || "qcloud";
|
|
|
|
|
|
if (ingressType === "qcloud") {
|
|
|
|
|
|
throw new Error("暂未实现");
|
|
|
|
|
|
// await this.patchQcloudCertSecret({ k8sClient, props, context })
|
|
|
|
|
|
} else {
|
2023-01-11 20:39:48 +08:00
|
|
|
|
await this.patchNginxCertSecret({ cert, k8sClient });
|
2022-11-08 22:48:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await utils.sleep(3000); // 停留2秒,等待secret部署完成
|
|
|
|
|
|
// await this.restartIngress({ k8sClient, props })
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-11 20:39:48 +08:00
|
|
|
|
async restartIngress(options: { k8sClient: any }) {
|
|
|
|
|
|
const { k8sClient } = options;
|
|
|
|
|
|
const { namespace } = this;
|
2022-11-08 22:48:57 +08:00
|
|
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
|
|
metadata: {
|
|
|
|
|
|
labels: {
|
|
|
|
|
|
certd: appendTimeSuffix("certd"),
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
const ingressList = await k8sClient.getIngressList({ namespace });
|
|
|
|
|
|
console.log("ingressList:", ingressList);
|
|
|
|
|
|
if (!ingressList || !ingressList.body || !ingressList.body.items) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const ingressNames = ingressList.body.items
|
|
|
|
|
|
.filter((item: any) => {
|
|
|
|
|
|
if (!item.spec.tls) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
for (const tls of item.spec.tls) {
|
2023-01-11 20:39:48 +08:00
|
|
|
|
if (tls.secretName === this.secretName) {
|
2022-11-08 22:48:57 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
})
|
|
|
|
|
|
.map((item: any) => {
|
|
|
|
|
|
return item.metadata.name;
|
|
|
|
|
|
});
|
|
|
|
|
|
for (const ingress of ingressNames) {
|
|
|
|
|
|
await k8sClient.patchIngress({ namespace, ingressName: ingress, body });
|
|
|
|
|
|
this.logger.info(`ingress已重启:${ingress}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-11 20:39:48 +08:00
|
|
|
|
async patchNginxCertSecret(options: { cert: any; k8sClient: any }) {
|
|
|
|
|
|
const { cert, k8sClient } = options;
|
2022-11-08 22:48:57 +08:00
|
|
|
|
const crt = cert.crt;
|
|
|
|
|
|
const key = cert.key;
|
|
|
|
|
|
const crtBase64 = Buffer.from(crt).toString("base64");
|
|
|
|
|
|
const keyBase64 = Buffer.from(key).toString("base64");
|
|
|
|
|
|
|
2023-01-11 20:39:48 +08:00
|
|
|
|
const { namespace, secretName } = this;
|
2022-11-08 22:48:57 +08:00
|
|
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
|
|
data: {
|
|
|
|
|
|
"tls.crt": crtBase64,
|
|
|
|
|
|
"tls.key": keyBase64,
|
|
|
|
|
|
},
|
|
|
|
|
|
metadata: {
|
|
|
|
|
|
labels: {
|
|
|
|
|
|
certd: appendTimeSuffix("certd"),
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
2023-01-11 20:39:48 +08:00
|
|
|
|
let secretNames: any = secretName;
|
2022-11-08 22:48:57 +08:00
|
|
|
|
if (typeof secretName === "string") {
|
|
|
|
|
|
secretNames = [secretName];
|
|
|
|
|
|
}
|
|
|
|
|
|
for (const secret of secretNames) {
|
|
|
|
|
|
await k8sClient.patchSecret({ namespace, secretName: secret, body });
|
|
|
|
|
|
this.logger.info(`CertSecret已更新:${secret}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getClient(aliyunProvider: any, regionId: string) {
|
|
|
|
|
|
return new ROAClient({
|
|
|
|
|
|
accessKeyId: aliyunProvider.accessKeyId,
|
|
|
|
|
|
accessKeySecret: aliyunProvider.accessKeySecret,
|
|
|
|
|
|
endpoint: `https://cs.${regionId}.aliyuncs.com`,
|
|
|
|
|
|
apiVersion: "2015-12-15",
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async getKubeConfig(client: any, clusterId: string, isPrivateIpAddress = false) {
|
|
|
|
|
|
const httpMethod = "GET";
|
|
|
|
|
|
const uriPath = `/k8s/${clusterId}/user_config`;
|
|
|
|
|
|
const queries = {
|
|
|
|
|
|
PrivateIpAddress: isPrivateIpAddress,
|
|
|
|
|
|
};
|
|
|
|
|
|
const body = "{}";
|
|
|
|
|
|
const headers = {
|
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
|
};
|
|
|
|
|
|
const requestOption = {};
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await client.request(httpMethod, uriPath, queries, body, headers, requestOption);
|
|
|
|
|
|
return res.config;
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error("请求出错:", e);
|
|
|
|
|
|
throw e;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-05-09 09:56:31 +08:00
|
|
|
|
|
|
|
|
|
|
new DeployCertToAliyunAckIngressPlugin();
|