mirror of
https://github.com/certd/certd.git
synced 2026-07-08 21:57:36 +08:00
chore: format
This commit is contained in:
@@ -1,61 +1,56 @@
|
||||
import { HttpRequestConfig } from '@certd/basic';
|
||||
import { IsAccess, AccessInput, BaseAccess, PageSearch } from '@certd/pipeline';
|
||||
import qs from 'qs';
|
||||
import { HttpRequestConfig } from "@certd/basic";
|
||||
import { IsAccess, AccessInput, BaseAccess, PageSearch } from "@certd/pipeline";
|
||||
import qs from "qs";
|
||||
|
||||
export type ZenlayerRequest = HttpRequestConfig & {
|
||||
action: string;
|
||||
version?: string;
|
||||
}
|
||||
};
|
||||
/**
|
||||
* 这个注解将注册一个授权配置
|
||||
* 在certd的后台管理系统中,用户可以选择添加此类型的授权
|
||||
*/
|
||||
@IsAccess({
|
||||
name: 'zenlayer',
|
||||
title: 'Zenlayer授权',
|
||||
icon: 'svg:icon-lucky',
|
||||
desc: 'Zenlayer授权',
|
||||
name: "zenlayer",
|
||||
title: "Zenlayer授权",
|
||||
icon: "svg:icon-lucky",
|
||||
desc: "Zenlayer授权",
|
||||
})
|
||||
export class ZenlayerAccess extends BaseAccess {
|
||||
|
||||
/**
|
||||
* 授权属性配置
|
||||
*/
|
||||
@AccessInput({
|
||||
title: 'AccessKeyId',
|
||||
title: "AccessKeyId",
|
||||
component: {
|
||||
placeholder: '访问密钥ID',
|
||||
placeholder: "访问密钥ID",
|
||||
},
|
||||
helper: "[访问密钥管理](https://console.zenlayer.com/accessKey)获取",
|
||||
required: true,
|
||||
encrypt: false,
|
||||
})
|
||||
accessKeyId = '';
|
||||
|
||||
accessKeyId = "";
|
||||
|
||||
/**
|
||||
* 授权属性配置
|
||||
*/
|
||||
@AccessInput({
|
||||
title: 'AccessKey Password',
|
||||
title: "AccessKey Password",
|
||||
component: {
|
||||
placeholder: '访问密钥密码',
|
||||
placeholder: "访问密钥密码",
|
||||
},
|
||||
required: true,
|
||||
encrypt: true,
|
||||
})
|
||||
accessKeyPassword = '';
|
||||
|
||||
|
||||
|
||||
accessKeyPassword = "";
|
||||
|
||||
@AccessInput({
|
||||
title: "测试",
|
||||
component: {
|
||||
name: "api-test",
|
||||
action: "TestRequest"
|
||||
action: "TestRequest",
|
||||
},
|
||||
helper: "点击测试接口是否正常"
|
||||
helper: "点击测试接口是否正常",
|
||||
})
|
||||
testRequest = true;
|
||||
|
||||
@@ -67,8 +62,7 @@ export class ZenlayerAccess extends BaseAccess {
|
||||
return "ok";
|
||||
}
|
||||
|
||||
|
||||
async getCertList(req: PageSearch = {}):Promise<{totalCount:number,dataSet:{sans:string[],certificateId:string,certificateLabel:string,common:string}[]}> {
|
||||
async getCertList(req: PageSearch = {}): Promise<{ totalCount: number; dataSet: { sans: string[]; certificateId: string; certificateLabel: string; common: string }[] }> {
|
||||
const pageNo = req.pageNo ?? 1;
|
||||
const pageSize = req.pageSize ?? 100;
|
||||
const res = await this.doRequest({
|
||||
@@ -76,14 +70,13 @@ export class ZenlayerAccess extends BaseAccess {
|
||||
action: "DescribeCertificates",
|
||||
data: {
|
||||
PageNum: pageNo,
|
||||
PageSize: pageSize
|
||||
}
|
||||
PageSize: pageSize,
|
||||
},
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
async getAuthorizationHeaders(req: ZenlayerRequest) {
|
||||
|
||||
/**
|
||||
* CanonicalRequest =
|
||||
HTTPRequestMethod + '\n' +
|
||||
@@ -96,11 +89,11 @@ export class ZenlayerAccess extends BaseAccess {
|
||||
if (!req.headers) {
|
||||
req.headers = {};
|
||||
}
|
||||
if (!req.headers['content-type']) {
|
||||
req.headers['content-type'] = "application/json; charset=utf-8";
|
||||
if (!req.headers["content-type"]) {
|
||||
req.headers["content-type"] = "application/json; charset=utf-8";
|
||||
}
|
||||
if (!req.headers['host']) {
|
||||
req.headers['host'] = "console.zenlayer.com";
|
||||
if (!req.headers["host"]) {
|
||||
req.headers["host"] = "console.zenlayer.com";
|
||||
}
|
||||
|
||||
if (!req.method) {
|
||||
@@ -108,12 +101,12 @@ export class ZenlayerAccess extends BaseAccess {
|
||||
}
|
||||
// this.accessKeyPassword="Gu5t9xGARNpq86cd98joQYCN3"
|
||||
// req.data = {"pageSize":10,"pageNum":1,"zoneId":"HKG-A"}
|
||||
const CanonicalQueryString = req.method === 'POST' ? '' : qs.stringify(req.params);
|
||||
const CanonicalQueryString = req.method === "POST" ? "" : qs.stringify(req.params);
|
||||
const SignedHeaders = "content-type;host";
|
||||
const CanonicalHeaders = `content-type:${req.headers['content-type']}\nhost:${req.headers['host']}\n`;
|
||||
const CanonicalHeaders = `content-type:${req.headers["content-type"]}\nhost:${req.headers["host"]}\n`;
|
||||
const HashedRequestPayload = this.ctx.utils.hash.sha256(JSON.stringify(req.data || {}), "hex");
|
||||
const CanonicalRequest = `${req.method}\n/\n${CanonicalQueryString}\n${CanonicalHeaders}\n${SignedHeaders}\n${HashedRequestPayload}`;
|
||||
let HashedCanonicalRequest = this.ctx.utils.hash.sha256(CanonicalRequest, "hex");
|
||||
const HashedCanonicalRequest = this.ctx.utils.hash.sha256(CanonicalRequest, "hex");
|
||||
// HashedCanonicalRequest = "29396f9dfa0f03820b931e8aa06e20cda197e73285ebd76aceb83f7dede493ee"
|
||||
const timestamp = Math.floor(Date.now() / 1000);
|
||||
// const timestamp= 1673361177
|
||||
@@ -122,7 +115,6 @@ export class ZenlayerAccess extends BaseAccess {
|
||||
const signature = this.ctx.utils.hash.hmacSha256WithKey(this.accessKeyPassword, StringToSign, "hex");
|
||||
const authorization = `${signMethod} Credential=${this.accessKeyId}, SignedHeaders=${SignedHeaders}, Signature=${signature}`;
|
||||
|
||||
|
||||
/**
|
||||
* X-ZC-Timestamp
|
||||
|
||||
@@ -154,41 +146,39 @@ Authorization
|
||||
*/
|
||||
return {
|
||||
...req.headers,
|
||||
'X-ZC-Timestamp': timestamp.toString(),
|
||||
'X-ZC-Action': req.action,
|
||||
'X-ZC-Version': req.version || "2022-11-20",
|
||||
'X-ZC-Signature-Method': signMethod,
|
||||
'Authorization': authorization,
|
||||
"X-ZC-Timestamp": timestamp.toString(),
|
||||
"X-ZC-Action": req.action,
|
||||
"X-ZC-Version": req.version || "2022-11-20",
|
||||
"X-ZC-Signature-Method": signMethod,
|
||||
Authorization: authorization,
|
||||
};
|
||||
}
|
||||
|
||||
async doRequest(req: ZenlayerRequest) {
|
||||
const headers = await this.getAuthorizationHeaders(req);
|
||||
req.headers = headers
|
||||
let res :any = undefined;
|
||||
try{
|
||||
req.headers = headers;
|
||||
let res: any = undefined;
|
||||
try {
|
||||
res = await this.ctx.http.request({
|
||||
baseURL: req.baseURL || "https://console.zenlayer.com",
|
||||
...req
|
||||
...req,
|
||||
});
|
||||
} catch (error) {
|
||||
const resData = error.response?.data;
|
||||
if (resData){
|
||||
let desc = ""
|
||||
if (resData.code === "CERTIFICATE_NOT_COVER_ALL_DOMAIN"){
|
||||
if (resData) {
|
||||
let desc = "";
|
||||
if (resData.code === "CERTIFICATE_NOT_COVER_ALL_DOMAIN") {
|
||||
desc = `证书未覆盖所有域名`;
|
||||
}
|
||||
throw new Error(`[code=${resData.code}] ${desc} ${resData.message} [requestId:${resData.requestId}]`);
|
||||
}
|
||||
throw error
|
||||
throw error;
|
||||
}
|
||||
if (res.code) {
|
||||
throw new Error(`[${res.code}]:${res.message} [requestId:${res.requestId}]`);
|
||||
}
|
||||
return res.response;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
new ZenlayerAccess();
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
export * from './access.js';
|
||||
export * from './plugins/index.js';
|
||||
export * from "./access.js";
|
||||
export * from "./plugins/index.js";
|
||||
|
||||
@@ -1 +1 @@
|
||||
export * from './plugin-refresh-cert.js';
|
||||
export * from "./plugin-refresh-cert.js";
|
||||
|
||||
+19
-23
@@ -15,9 +15,9 @@ import { ZenlayerAccess } from "../access.js";
|
||||
default: {
|
||||
//默认值配置照抄即可
|
||||
strategy: {
|
||||
runStrategy: RunStrategy.SkipWhenSucceed
|
||||
}
|
||||
}
|
||||
runStrategy: RunStrategy.SkipWhenSucceed,
|
||||
},
|
||||
},
|
||||
})
|
||||
//类名规范,跟上面插件名称(name)一致
|
||||
export class ZenlayerRefreshCert extends AbstractTaskPlugin {
|
||||
@@ -27,8 +27,8 @@ export class ZenlayerRefreshCert extends AbstractTaskPlugin {
|
||||
helper: "请选择前置任务输出的域名证书",
|
||||
component: {
|
||||
name: "output-selector",
|
||||
from: [...CertApplyPluginNames]
|
||||
}
|
||||
from: [...CertApplyPluginNames],
|
||||
},
|
||||
// required: true, // 必填
|
||||
})
|
||||
cert!: CertInfo;
|
||||
@@ -41,9 +41,9 @@ export class ZenlayerRefreshCert extends AbstractTaskPlugin {
|
||||
title: "Zenlayer授权",
|
||||
component: {
|
||||
name: "access-selector",
|
||||
type: "zenlayer" //固定授权类型
|
||||
type: "zenlayer", //固定授权类型
|
||||
},
|
||||
required: true //必填
|
||||
required: true, //必填
|
||||
})
|
||||
accessId!: string;
|
||||
//
|
||||
@@ -53,14 +53,13 @@ export class ZenlayerRefreshCert extends AbstractTaskPlugin {
|
||||
title: "证书ID列表",
|
||||
helper: "要更新的Zenlayer证书ID列表",
|
||||
|
||||
action: ZenlayerRefreshCert.prototype.onGetCertList.name
|
||||
action: ZenlayerRefreshCert.prototype.onGetCertList.name,
|
||||
})
|
||||
)
|
||||
certList!: string[];
|
||||
|
||||
//插件实例化时执行的方法
|
||||
async onInstance() {
|
||||
}
|
||||
async onInstance() {}
|
||||
|
||||
//插件执行方法
|
||||
async execute(): Promise<void> {
|
||||
@@ -70,7 +69,7 @@ export class ZenlayerRefreshCert extends AbstractTaskPlugin {
|
||||
await this.updateCert({
|
||||
access: access,
|
||||
certId: certId,
|
||||
cert: this.cert
|
||||
cert: this.cert,
|
||||
});
|
||||
this.logger.info(`刷新证书${certId}成功`);
|
||||
await this.ctx.utils.sleep(1000);
|
||||
@@ -79,8 +78,8 @@ export class ZenlayerRefreshCert extends AbstractTaskPlugin {
|
||||
this.logger.info("部署完成");
|
||||
}
|
||||
|
||||
async updateCert(req:{access:ZenlayerAccess,certId:string, cert: CertInfo}){
|
||||
const {access,certId, cert} = req;
|
||||
async updateCert(req: { access: ZenlayerAccess; certId: string; cert: CertInfo }) {
|
||||
const { access, certId, cert } = req;
|
||||
// ModifyCertificate
|
||||
await access.doRequest({
|
||||
url: "/api/v2/cdn",
|
||||
@@ -94,7 +93,7 @@ certificateKey
|
||||
certificateId: certId,
|
||||
certificateContent: cert.crt,
|
||||
certificateKey: cert.key,
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
async onGetCertList(req: PageSearch = {}) {
|
||||
@@ -102,12 +101,10 @@ certificateKey
|
||||
|
||||
const pageNo = req.pageNo ?? 1;
|
||||
const pageSize = req.pageSize ?? 100;
|
||||
const res = await access.getCertList(
|
||||
{
|
||||
pageNo: pageNo,
|
||||
pageSize: pageSize
|
||||
}
|
||||
);
|
||||
const res = await access.getCertList({
|
||||
pageNo: pageNo,
|
||||
pageSize: pageSize,
|
||||
});
|
||||
const total = res.totalCount;
|
||||
const list = res.dataSet || [];
|
||||
if (!list || list.length === 0) {
|
||||
@@ -122,17 +119,16 @@ certificateKey
|
||||
return {
|
||||
label: `${item.certificateLabel}<${item.certificateId}-${item.common}>`,
|
||||
value: `${item.certificateId}`,
|
||||
domain: item.sans
|
||||
domain: item.sans,
|
||||
};
|
||||
});
|
||||
return {
|
||||
list: this.ctx.utils.options.buildGroupOptions(options, this.certDomains),
|
||||
total: total,
|
||||
pageNo: pageNo,
|
||||
pageSize: pageSize
|
||||
pageSize: pageSize,
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//实例化一下,注册插件
|
||||
|
||||
Reference in New Issue
Block a user