mirror of
https://github.com/certd/certd.git
synced 2026-04-21 18:37:28 +08:00
perf: 支持ucloud,上传到ussl,部署到ucdn
This commit is contained in:
@@ -0,0 +1,214 @@
|
||||
import { IsAccess, AccessInput, BaseAccess } from '@certd/pipeline';
|
||||
import { CertInfo, CertReader } from '@certd/plugin-cert';
|
||||
|
||||
/**
|
||||
* 这个注解将注册一个授权配置
|
||||
* 在certd的后台管理系统中,用户可以选择添加此类型的授权
|
||||
*/
|
||||
@IsAccess({
|
||||
name: 'ucloud',
|
||||
title: 'UCloud授权',
|
||||
icon: 'lsicon:badge-new-filled',
|
||||
desc: '优刻得授权',
|
||||
})
|
||||
export class UCloudAccess extends BaseAccess {
|
||||
|
||||
/**
|
||||
* 授权属性配置
|
||||
*/
|
||||
@AccessInput({
|
||||
title: '项目Id',
|
||||
component: {
|
||||
placeholder: '项目Id',
|
||||
},
|
||||
helper: "[项目管理](https://console.ucloud.cn/uaccount/iam/project_manage)项目ID列获取",
|
||||
required: true,
|
||||
encrypt: false,
|
||||
})
|
||||
projectId = '';
|
||||
|
||||
|
||||
/**
|
||||
* 授权属性配置
|
||||
*/
|
||||
@AccessInput({
|
||||
title: '公钥',
|
||||
component: {
|
||||
placeholder: '公钥',
|
||||
},
|
||||
helper: "[Api管理](https://console.ucloud.cn/uaccount/api_manage)获取",
|
||||
required: true,
|
||||
encrypt: false,
|
||||
})
|
||||
publicKey = '';
|
||||
|
||||
@AccessInput({
|
||||
title: '私钥',
|
||||
component: {
|
||||
name: "a-input-password",
|
||||
vModel: "value",
|
||||
placeholder: '私钥',
|
||||
},
|
||||
required: true,
|
||||
encrypt: true,
|
||||
})
|
||||
privateKey = '';
|
||||
|
||||
|
||||
|
||||
@AccessInput({
|
||||
title: "测试",
|
||||
component: {
|
||||
name: "api-test",
|
||||
action: "TestRequest"
|
||||
},
|
||||
helper: "点击测试接口是否正常"
|
||||
})
|
||||
testRequest = true;
|
||||
|
||||
client: any;
|
||||
|
||||
async onTestRequest() {
|
||||
await this.ProjectList();
|
||||
return "ok";
|
||||
}
|
||||
|
||||
|
||||
async getClient() {
|
||||
if (this.client) {
|
||||
return this.client;
|
||||
}
|
||||
const { Client } = await import('@ucloud-sdks/ucloud-sdk-js');
|
||||
const client = new Client({
|
||||
config: {
|
||||
region: 'cn-bj2',
|
||||
projectId: this.projectId || "",
|
||||
baseUrl: "https://api.ucloud.cn"
|
||||
},
|
||||
credential: {
|
||||
publicKey: this.publicKey,
|
||||
privateKey: this.privateKey,
|
||||
}
|
||||
});
|
||||
this.client = client;
|
||||
return client
|
||||
}
|
||||
|
||||
async ProjectList() {
|
||||
const client = await this.getClient();
|
||||
const resp = await client.uaccount().getProjectList({
|
||||
"Action": "GetProjectList"
|
||||
});
|
||||
this.ctx.logger.info(`获取到项目列表:${JSON.stringify(resp)}`);
|
||||
return resp;
|
||||
}
|
||||
|
||||
async GetRegion() {
|
||||
const client = await this.getClient();
|
||||
const res = await client.uaccount().getRegion({
|
||||
"Action": "GetRegion"
|
||||
});
|
||||
this.ctx.logger.info(`获取到区域列表:${JSON.stringify(res)}`);
|
||||
return res;
|
||||
}
|
||||
|
||||
async CdnDominList(req: { PageNo: number, PageSize: number }) {
|
||||
const client = await this.getClient();
|
||||
const resp = await client.ucdn().getUcdnDomainInfoList({
|
||||
"Action": "GetUcdnDomainInfoList",
|
||||
"ProjectId": this.projectId || "",
|
||||
"PageNo": req.PageNo,
|
||||
"PageSize": req.PageSize,
|
||||
});
|
||||
this.ctx.logger.info(`获取到CDN域名列表:${JSON.stringify(resp)}`);
|
||||
return resp;
|
||||
}
|
||||
|
||||
async CdnAddCert(req: { certName: string, cert: CertInfo }) {
|
||||
const client = await this.getClient();
|
||||
const resp = await client.ucdn().addCertificate({
|
||||
"Action": "AddCertificate",
|
||||
"ProjectId": this.projectId || "",
|
||||
"CertName": req.certName,
|
||||
"UserCert": req.cert.crt,
|
||||
"PrivateKey": req.cert.key
|
||||
});
|
||||
this.ctx.logger.info(`添加CDN证书:${JSON.stringify(resp)}`);
|
||||
return resp;
|
||||
}
|
||||
|
||||
|
||||
async SslUploadCert(req: { cert: CertInfo }) {
|
||||
const { cert } = req
|
||||
/**
|
||||
&SslPublicKey=lXUzWbSR
|
||||
&SslCaKey=lXUzWbSR
|
||||
&SslMD5=lXUzWbSR
|
||||
&CertificateName=GoodCertcification
|
||||
*/
|
||||
const certReader = new CertReader(cert)
|
||||
const certName = certReader.buildCertName()
|
||||
const crtBase64 = this.ctx.utils.hash.base64(cert.crt)
|
||||
const keyBase64 = this.ctx.utils.hash.base64(cert.key)
|
||||
const allDomains = certReader.getAllDomains().join(",")
|
||||
|
||||
this.ctx.logger.info(`----------- 上传USSL证书,certName:${certName},domains:${allDomains}`);
|
||||
try {
|
||||
const resp = await this.invoke({
|
||||
Action: "UploadNormalCertificate",
|
||||
"SslPublicKey": crtBase64,
|
||||
"SslPrivateKey": keyBase64,
|
||||
"CertificateName": certName,
|
||||
"SslMD5": this.ctx.utils.hash.md5(crtBase64 + keyBase64)
|
||||
});
|
||||
this.ctx.logger.info(`----------- 上传USSL证书成功,certId:${resp.CertificateID}`);
|
||||
return { type: "ussl", id: resp.CertificateID, name: certName, resourceId: resp.LongResourceID,domains:allDomains }
|
||||
} catch (err) {
|
||||
|
||||
if(err.message.includes("重复上传证书")){
|
||||
//查找证书
|
||||
const certList = await this.SslGetCertList(certReader.getMainDomain());
|
||||
|
||||
const cert = certList.find((item: any) => item.Domains === allDomains)
|
||||
if(cert){
|
||||
this.ctx.logger.info(`----------- 找到已存在证书,certId:${cert.CertificateID}`);
|
||||
return { type: "ussl", id: cert.CertificateID, name: certName, domains: cert.Domains }
|
||||
}
|
||||
}
|
||||
|
||||
this.ctx.logger.error(`上传USSL证书失败:${err}`);
|
||||
throw err;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
async SslGetCertList(domain: string) {
|
||||
const resp = await this.invoke({
|
||||
Action: "GetCertificateList",
|
||||
Mode: "trust",
|
||||
Domain: domain,
|
||||
Sort:"2"
|
||||
});
|
||||
return resp.CertificateList||[];
|
||||
}
|
||||
|
||||
|
||||
async invoke(req: { Action: string, [key: string]: any }) {
|
||||
const { Request } = await import('@ucloud-sdks/ucloud-sdk-js');
|
||||
const client = await this.getClient();
|
||||
const resp = await client.invoke(new Request({
|
||||
...req
|
||||
}));
|
||||
this.ctx.logger.info(`请求UCloud API:${JSON.stringify(resp)}`);
|
||||
const res = resp.data || {}
|
||||
if (res.RetCode !== 0) {
|
||||
throw new Error(res.Message)
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
new UCloudAccess();
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './access.js';
|
||||
export * from './plugins/index.js';
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './plugin-deploy-to-cdn.js';
|
||||
export * from './plugin-upload-to-ussl.js';
|
||||
@@ -0,0 +1,189 @@
|
||||
import { AbstractTaskPlugin, IsTaskPlugin, PageSearch, pluginGroups, RunStrategy, TaskInput } from "@certd/pipeline";
|
||||
import { CertApplyPluginNames, CertInfo } from "@certd/plugin-cert";
|
||||
import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from "@certd/plugin-lib";
|
||||
import { UCloudAccess } from "../access.js";
|
||||
|
||||
@IsTaskPlugin({
|
||||
//命名规范,插件类型+功能(就是目录plugin-demo中的demo),大写字母开头,驼峰命名
|
||||
name: "UCloudDeployToCDN",
|
||||
title: "UCloud-部署到CDN",
|
||||
desc: "将证书部署到UCloud CDN",
|
||||
icon: "svg:icon-lucky",
|
||||
//插件分组
|
||||
group: pluginGroups.cdn.key,
|
||||
needPlus: false,
|
||||
default: {
|
||||
//默认值配置照抄即可
|
||||
strategy: {
|
||||
runStrategy: RunStrategy.SkipWhenSucceed
|
||||
}
|
||||
}
|
||||
})
|
||||
//类名规范,跟上面插件名称(name)一致
|
||||
export class UCloudDeployToCDN extends AbstractTaskPlugin {
|
||||
//证书选择,此项必须要有
|
||||
@TaskInput({
|
||||
title: "域名证书",
|
||||
helper: "请选择前置任务输出的域名证书",
|
||||
component: {
|
||||
name: "output-selector",
|
||||
from: [...CertApplyPluginNames, ":UCloudCertId:"]
|
||||
}
|
||||
// required: true, // 必填
|
||||
})
|
||||
cert!: CertInfo | { type: string, id: number, name: string };
|
||||
|
||||
@TaskInput(createCertDomainGetterInputDefine({ props: { required: false } }))
|
||||
certDomains!: string[];
|
||||
|
||||
//授权选择框
|
||||
@TaskInput({
|
||||
title: "UCloud授权",
|
||||
component: {
|
||||
name: "access-selector",
|
||||
type: "ucloud" //固定授权类型
|
||||
},
|
||||
required: true //必填
|
||||
})
|
||||
accessId!: string;
|
||||
//
|
||||
|
||||
@TaskInput(
|
||||
createRemoteSelectInputDefine({
|
||||
title: "域名列表",
|
||||
helper: "要更新的UCloud域名列表",
|
||||
|
||||
action: UCloudDeployToCDN.prototype.onGetDomainList.name
|
||||
})
|
||||
)
|
||||
domainList!: string[];
|
||||
|
||||
//插件实例化时执行的方法
|
||||
async onInstance() {
|
||||
}
|
||||
|
||||
//插件执行方法
|
||||
async execute(): Promise<void> {
|
||||
const access = await this.getAccess<UCloudAccess>(this.accessId);
|
||||
let certType = "ussl"
|
||||
let certId = 0
|
||||
let certName = this.appendTimeSuffix("certd")
|
||||
// @ts-ignore
|
||||
if (this.cert?.id) {
|
||||
//从上一步传过来的ssl证书
|
||||
// @ts-ignore
|
||||
certId = this.cert.id
|
||||
// @ts-ignore
|
||||
certName = this.cert.name
|
||||
|
||||
} else {
|
||||
const cert = await access.SslUploadCert({
|
||||
cert: this.cert as CertInfo
|
||||
});
|
||||
certId = cert.id
|
||||
certName = cert.name
|
||||
}
|
||||
|
||||
for (const item of this.domainList) {
|
||||
this.logger.info(`----------- 开始更新域名:${item}`);
|
||||
await this.deployToCdn({
|
||||
access: access,
|
||||
certName: certName,
|
||||
domain: item,
|
||||
certId: certId,
|
||||
certType: certType
|
||||
});
|
||||
this.logger.info(`----------- 更新域名证书${item}成功`);
|
||||
}
|
||||
|
||||
this.logger.info("部署完成");
|
||||
}
|
||||
|
||||
|
||||
async deployToCdn(req: { access: any, domain: string, certId: number, certType: string, certName: string }) {
|
||||
const { access, domain, certId, certType, certName } = req
|
||||
|
||||
const domainsRes = await access.invoke({
|
||||
"Action": "GetUcdnDomainConfig",
|
||||
"ProjectId": access.projectId,
|
||||
"Domain": [
|
||||
domain
|
||||
]
|
||||
});
|
||||
|
||||
const domainList = domainsRes.DomainList || [];
|
||||
const domainConf = domainList.find((item: any) => item.Domain === domain);
|
||||
if (!domainConf) {
|
||||
throw new Error(`没有找到CDN域名${domain}`);
|
||||
}
|
||||
|
||||
const domainId = domainConf.DomainId;
|
||||
const httpsStatusAbroad = domainConf.HttpsStatusAbroad;
|
||||
let httpsStatusCn = domainConf.HttpsStatusCn;
|
||||
if (httpsStatusAbroad === "disable" && httpsStatusCn === "disable") {
|
||||
this.logger.info(`原CDN域名HTTPS未开启,将开启国内加速`);
|
||||
httpsStatusCn = "enable"
|
||||
}
|
||||
|
||||
|
||||
const body: any = {
|
||||
"Action": "UpdateUcdnDomainHttpsConfigV2",
|
||||
"DomainId": domainId,
|
||||
"CertName": certName,
|
||||
"CertId": certId,
|
||||
"CertType": certType,
|
||||
EnableHttp2: domainConf.EnableHttp2 ||"0",
|
||||
RedirectHttp2Https: domainConf.RedirectHttp2Https || "0",
|
||||
TlsVersion: domainConf.TlsVersion || "tlsv1.0,tlsv1.1,tlsv1.2,tlsv1.3"
|
||||
}
|
||||
if (httpsStatusAbroad === "enable") {
|
||||
body.HttpsStatusAbroad = httpsStatusAbroad;
|
||||
}
|
||||
if (httpsStatusCn === "enable") {
|
||||
body.HttpsStatusCn = httpsStatusCn;
|
||||
}
|
||||
this.logger.info(`----------- 更新CDN域名HTTPS配置${domainId},${JSON.stringify(body)}`);
|
||||
const resp = await access.invoke(body);
|
||||
this.logger.info(`----------- 部署CDN证书${domainId}成功,${JSON.stringify(resp)}`);
|
||||
}
|
||||
|
||||
async onGetDomainList(req: PageSearch = {}) {
|
||||
const access = await this.getAccess<UCloudAccess>(this.accessId);
|
||||
|
||||
const pageNo = req.pageNo ?? 1;
|
||||
const pageSize = req.pageSize ?? 100;
|
||||
const res = await access.CdnDominList(
|
||||
{
|
||||
PageNo: pageNo,
|
||||
PageSize: pageSize
|
||||
}
|
||||
);
|
||||
const total = res.TotalCount;
|
||||
const list = res.DomainInfoList || [];
|
||||
if (!list || list.length === 0) {
|
||||
throw new Error("没有找到CDN域名,请先在控制台创建CDN域名");
|
||||
}
|
||||
|
||||
/**
|
||||
* "Domain": "ucloud.certd.handsfree.work",
|
||||
"DomainId": "ucdn-1kwdtph5ygbb"
|
||||
*/
|
||||
const options = list.map((item: any) => {
|
||||
return {
|
||||
label: `${item.Domain}<${item.DomainId}>`,
|
||||
value: `${item.Domain}`,
|
||||
domain: item.Domain
|
||||
};
|
||||
});
|
||||
return {
|
||||
list: this.ctx.utils.options.buildGroupOptions(options, this.certDomains),
|
||||
total: total,
|
||||
pageNo: pageNo,
|
||||
pageSize: pageSize
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//实例化一下,注册插件
|
||||
new UCloudDeployToCDN();
|
||||
@@ -0,0 +1,73 @@
|
||||
import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput, TaskOutput } from "@certd/pipeline";
|
||||
import { CertApplyPluginNames, CertInfo } from "@certd/plugin-cert";
|
||||
import { UCloudAccess } from "../access.js";
|
||||
|
||||
@IsTaskPlugin({
|
||||
//命名规范,插件类型+功能(就是目录plugin-demo中的demo),大写字母开头,驼峰命名
|
||||
name: "UCloudUploadToUSSL",
|
||||
title: "UCloud-上传到USSL",
|
||||
desc: "将证书上传到UCloud USSL",
|
||||
icon: "svg:icon-lucky",
|
||||
//插件分组
|
||||
group: pluginGroups.cdn.key,
|
||||
needPlus: false,
|
||||
default: {
|
||||
//默认值配置照抄即可
|
||||
strategy: {
|
||||
runStrategy: RunStrategy.SkipWhenSucceed
|
||||
}
|
||||
}
|
||||
})
|
||||
//类名规范,跟上面插件名称(name)一致
|
||||
export class UCloudUploadToUSSL extends AbstractTaskPlugin {
|
||||
//证书选择,此项必须要有
|
||||
@TaskInput({
|
||||
title: "域名证书",
|
||||
helper: "请选择前置任务输出的域名证书",
|
||||
component: {
|
||||
name: "output-selector",
|
||||
from: [...CertApplyPluginNames]
|
||||
}
|
||||
// required: true, // 必填
|
||||
})
|
||||
cert!: CertInfo;
|
||||
|
||||
|
||||
|
||||
//授权选择框
|
||||
@TaskInput({
|
||||
title: "UCloud授权",
|
||||
component: {
|
||||
name: "access-selector",
|
||||
type: "ucloud" //固定授权类型
|
||||
},
|
||||
required: true //必填
|
||||
})
|
||||
accessId!: string;
|
||||
//
|
||||
|
||||
@TaskOutput({
|
||||
title: "证书ID",
|
||||
type: "UCloudCertId",
|
||||
})
|
||||
certId!: {type:string,id:number,name:string};
|
||||
|
||||
|
||||
//插件实例化时执行的方法
|
||||
async onInstance() {
|
||||
}
|
||||
|
||||
//插件执行方法
|
||||
async execute(): Promise<void> {
|
||||
const access = await this.getAccess<UCloudAccess>(this.accessId);
|
||||
const certId = await access.SslUploadCert({cert:this.cert});
|
||||
this.certId = certId;
|
||||
this.logger.info("部署完成");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
//实例化一下,注册插件
|
||||
new UCloudUploadToUSSL();
|
||||
Reference in New Issue
Block a user