mirror of
https://github.com/certd/certd.git
synced 2026-07-06 20:18:59 +08:00
chore: format
This commit is contained in:
@@ -11,45 +11,42 @@ import { CmccClient } from "./cmcc-client.js";
|
||||
name: "cmcc",
|
||||
title: "中国移动CND授权",
|
||||
desc: "",
|
||||
icon: "svg:cmcc"
|
||||
icon: "svg:cmcc",
|
||||
})
|
||||
export class CmccAccess extends BaseAccess {
|
||||
|
||||
@AccessInput({
|
||||
title: 'TenantID',
|
||||
title: "TenantID",
|
||||
component: {
|
||||
placeholder: 'TenantID',
|
||||
placeholder: "TenantID",
|
||||
},
|
||||
required: true,
|
||||
})
|
||||
tenantId = '';
|
||||
|
||||
tenantId = "";
|
||||
|
||||
@AccessInput({
|
||||
title: 'TenantKey',
|
||||
title: "TenantKey",
|
||||
component: {
|
||||
placeholder: 'TenantKey',
|
||||
placeholder: "TenantKey",
|
||||
},
|
||||
required: true,
|
||||
encrypt: true,
|
||||
})
|
||||
tenantKey = '';
|
||||
|
||||
tenantKey = "";
|
||||
|
||||
@AccessInput({
|
||||
title: "测试",
|
||||
component: {
|
||||
name: "api-test",
|
||||
action: "TestRequest"
|
||||
action: "TestRequest",
|
||||
},
|
||||
helper: "点击测试接口是否正常"
|
||||
helper: "点击测试接口是否正常",
|
||||
})
|
||||
testRequest = true;
|
||||
|
||||
async onTestRequest() {
|
||||
const client = await this.getCmccClient()
|
||||
await client.getDomainList({})
|
||||
return "ok"
|
||||
const client = await this.getCmccClient();
|
||||
await client.getDomainList({});
|
||||
return "ok";
|
||||
}
|
||||
|
||||
async getCmccClient() {
|
||||
@@ -58,9 +55,8 @@ export class CmccAccess extends BaseAccess {
|
||||
tenantKey: this.tenantKey,
|
||||
http: this.ctx.http,
|
||||
logger: this.ctx.logger,
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
new CmccAccess();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { HttpClient, ILogger } from '@certd/basic';
|
||||
import { CertInfo, CertReader } from '@certd/plugin-cert';
|
||||
import * as crypto from 'crypto';
|
||||
import { HttpClient, ILogger } from "@certd/basic";
|
||||
import { CertInfo, CertReader } from "@certd/plugin-cert";
|
||||
import * as crypto from "crypto";
|
||||
export interface CmcdnConfig {
|
||||
tenantId: string;
|
||||
tenantKey: string;
|
||||
@@ -25,18 +25,18 @@ export class CmccClient {
|
||||
*/
|
||||
constructor(config: CmcdnConfig) {
|
||||
this.config = {
|
||||
endpoint: 'https://p.cdn.10086.cn/',
|
||||
endpoint: "https://p.cdn.10086.cn/",
|
||||
...config,
|
||||
};
|
||||
this.http = config.http
|
||||
this.http = config.http;
|
||||
this.logger = config.logger;
|
||||
|
||||
if (!this.config.tenantId) {
|
||||
throw new Error('tenantId is required');
|
||||
throw new Error("tenantId is required");
|
||||
}
|
||||
|
||||
if (!this.config.tenantKey) {
|
||||
throw new Error('tenantKey is required');
|
||||
throw new Error("tenantKey is required");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ export class CmccClient {
|
||||
* @returns SHA256哈希值
|
||||
*/
|
||||
private sha256Hex(data: string): string {
|
||||
return crypto.createHash('sha256').update(data).digest('hex');
|
||||
return crypto.createHash("sha256").update(data).digest("hex");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,7 +74,7 @@ export class CmccClient {
|
||||
* @returns 签名
|
||||
*/
|
||||
private generateApiSign(body: any, token: string): string {
|
||||
const bodyStr = body ? JSON.stringify(body) : '';
|
||||
const bodyStr = body ? JSON.stringify(body) : "";
|
||||
return this.sha256Hex(bodyStr + token);
|
||||
}
|
||||
|
||||
@@ -89,8 +89,6 @@ export class CmccClient {
|
||||
return Date.now() < this.tokenExpiresAt;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取认证token
|
||||
* @returns 认证token
|
||||
@@ -114,17 +112,17 @@ export class CmccClient {
|
||||
|
||||
const response = await this.http.request({
|
||||
baseURL: this.config.endpoint,
|
||||
url: '/api/authentication',
|
||||
method: 'POST',
|
||||
url: "/api/authentication",
|
||||
method: "POST",
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
},
|
||||
data: authRequest,
|
||||
skipSslVerify: true,
|
||||
logParams: false,
|
||||
logRes: false,
|
||||
logData: false
|
||||
logData: false,
|
||||
});
|
||||
|
||||
this.token = response.token;
|
||||
@@ -144,18 +142,18 @@ export class CmccClient {
|
||||
|
||||
// 设置默认headers
|
||||
const defaultHeaders: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/vnd.cmcdn+json',
|
||||
'CMCDN-Auth-Token': token,
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/vnd.cmcdn+json",
|
||||
"CMCDN-Auth-Token": token,
|
||||
};
|
||||
|
||||
// 生成签名
|
||||
if (req.method === 'POST' || req.method === 'PUT') {
|
||||
if (req.method === "POST" || req.method === "PUT") {
|
||||
const signature = this.generateApiSign(req.data, token);
|
||||
defaultHeaders['HTTP-X-CMCDN-Signature'] = signature;
|
||||
defaultHeaders["HTTP-X-CMCDN-Signature"] = signature;
|
||||
} else {
|
||||
const signature = this.sha256Hex(token);
|
||||
defaultHeaders['HTTP-X-CMCDN-Signature'] = signature;
|
||||
defaultHeaders["HTTP-X-CMCDN-Signature"] = signature;
|
||||
}
|
||||
|
||||
// 合并自定义headers
|
||||
@@ -172,7 +170,7 @@ export class CmccClient {
|
||||
skipSslVerify: true,
|
||||
logParams: false,
|
||||
logRes: false,
|
||||
logData: false
|
||||
logData: false,
|
||||
});
|
||||
if (response.error_code != 0) {
|
||||
this.logger.error(`接口请求失败,${JSON.stringify(response)}`);
|
||||
@@ -283,16 +281,15 @@ export class CmccClient {
|
||||
]
|
||||
}
|
||||
*/
|
||||
async getDomainList(req: { domainName?: string, domainStatus?: string }) {
|
||||
|
||||
async getDomainList(req: { domainName?: string; domainStatus?: string }) {
|
||||
const res = await this.doRequest({
|
||||
url: "/api/domain_list",
|
||||
method: "GET",
|
||||
params: {
|
||||
domainName: req.domainName,
|
||||
domainStatus: req.domainStatus,
|
||||
}
|
||||
})
|
||||
},
|
||||
});
|
||||
|
||||
this.logger.info("getDomainList", res);
|
||||
|
||||
@@ -364,7 +361,6 @@ export class CmccClient {
|
||||
12.1.4.2请求报文示例
|
||||
*/
|
||||
async uploadCert(req: { cert: CertInfo }) {
|
||||
|
||||
const certReader = new CertReader(req.cert);
|
||||
const res = await this.doRequest({
|
||||
url: "/api/config/action?commandType=saveCrt&version=1",
|
||||
@@ -373,8 +369,8 @@ export class CmccClient {
|
||||
certificate: req.cert.crt,
|
||||
private_key: req.cert.key,
|
||||
crt_name: certReader.buildCertName(),
|
||||
}
|
||||
})
|
||||
},
|
||||
});
|
||||
|
||||
this.logger.info("uploadCert", res);
|
||||
|
||||
@@ -382,10 +378,10 @@ export class CmccClient {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param req
|
||||
*
|
||||
* @param req
|
||||
*/
|
||||
async deployCertToCdn(req: { domainNames: string[], certId: string }) {
|
||||
async deployCertToCdn(req: { domainNames: string[]; certId: string }) {
|
||||
// /api/config/action?commandType = manageDomainBaseConfig&version = 1
|
||||
const res = await this.doRequest({
|
||||
url: "/api/config/action?commandType=manageDomainBaseConfig&version=1",
|
||||
@@ -395,11 +391,10 @@ export class CmccClient {
|
||||
domains: req.domainNames,
|
||||
https_enable: true,
|
||||
unique_id: req.certId,
|
||||
}
|
||||
})
|
||||
},
|
||||
});
|
||||
this.logger.info("deployCertToCdn", res);
|
||||
|
||||
return res.data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
export * from './access.js'
|
||||
export * from './plugin-deploy-to-cdn.js'
|
||||
export * from "./access.js";
|
||||
export * from "./plugin-deploy-to-cdn.js";
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
import {
|
||||
IsTaskPlugin,
|
||||
PageSearch,
|
||||
pluginGroups,
|
||||
RunStrategy,
|
||||
TaskInput
|
||||
} from "@certd/pipeline";
|
||||
import { IsTaskPlugin, PageSearch, pluginGroups, RunStrategy, TaskInput } from "@certd/pipeline";
|
||||
import { CertApplyPluginNames, CertInfo } from "@certd/plugin-cert";
|
||||
import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from "@certd/plugin-lib";
|
||||
import { AbstractPlusTaskPlugin } from "@certd/plugin-plus";
|
||||
@@ -22,9 +16,9 @@ import { CmccAccess } from "./access.js";
|
||||
default: {
|
||||
//默认值配置照抄即可
|
||||
strategy: {
|
||||
runStrategy: RunStrategy.SkipWhenSucceed
|
||||
}
|
||||
}
|
||||
runStrategy: RunStrategy.SkipWhenSucceed,
|
||||
},
|
||||
},
|
||||
})
|
||||
//类名规范,跟上面插件名称(name)一致
|
||||
export class CmccDeployCertToCdn extends AbstractPlusTaskPlugin {
|
||||
@@ -34,8 +28,8 @@ export class CmccDeployCertToCdn extends AbstractPlusTaskPlugin {
|
||||
helper: "请选择前置任务输出的域名证书",
|
||||
component: {
|
||||
name: "output-selector",
|
||||
from: [...CertApplyPluginNames]
|
||||
}
|
||||
from: [...CertApplyPluginNames],
|
||||
},
|
||||
// required: true, // 必填
|
||||
})
|
||||
cert!: CertInfo;
|
||||
@@ -48,9 +42,9 @@ export class CmccDeployCertToCdn extends AbstractPlusTaskPlugin {
|
||||
title: "中国移动-授权",
|
||||
component: {
|
||||
name: "access-selector",
|
||||
type: "cmcc" //固定授权类型
|
||||
type: "cmcc", //固定授权类型
|
||||
},
|
||||
required: true //必填
|
||||
required: true, //必填
|
||||
})
|
||||
accessId!: string;
|
||||
//
|
||||
@@ -61,14 +55,13 @@ export class CmccDeployCertToCdn extends AbstractPlusTaskPlugin {
|
||||
helper: "要更新的中国移动CDN域名",
|
||||
action: CmccDeployCertToCdn.prototype.onGetDomainList.name,
|
||||
pager: false,
|
||||
search: false
|
||||
search: false,
|
||||
})
|
||||
)
|
||||
domainList!: string[];
|
||||
|
||||
//插件实例化时执行的方法
|
||||
async onInstance() {
|
||||
}
|
||||
async onInstance() {}
|
||||
|
||||
//插件执行方法
|
||||
async execute(): Promise<void> {
|
||||
@@ -77,17 +70,16 @@ export class CmccDeployCertToCdn extends AbstractPlusTaskPlugin {
|
||||
const client = await access.getCmccClient();
|
||||
this.logger.info(`----------- 开始更新证书:${this.domainList}`);
|
||||
|
||||
|
||||
const newCert = await client.uploadCert({
|
||||
cert: this.cert
|
||||
})
|
||||
cert: this.cert,
|
||||
});
|
||||
|
||||
const certId = newCert.unique_id
|
||||
const certId = newCert.unique_id;
|
||||
this.logger.info(`----------- 上传证书成功,证书ID:${certId}`);
|
||||
|
||||
await client.deployCertToCdn({
|
||||
certId: certId,
|
||||
domainNames: this.domainList
|
||||
domainNames: this.domainList,
|
||||
});
|
||||
this.logger.info(`----------- 更新证书${this.domainList}成功,等待10s`);
|
||||
await this.ctx.utils.sleep(10000);
|
||||
@@ -96,14 +88,13 @@ export class CmccDeployCertToCdn extends AbstractPlusTaskPlugin {
|
||||
|
||||
async onGetDomainList(data: PageSearch = {}) {
|
||||
const access = await this.getAccess<CmccAccess>(this.accessId);
|
||||
const client= await access.getCmccClient();
|
||||
const res = await client.getDomainList({})
|
||||
const list = res || []
|
||||
const client = await access.getCmccClient();
|
||||
const res = await client.getDomainList({});
|
||||
const list = res || [];
|
||||
if (!list || list.length === 0) {
|
||||
throw new Error("没有找到加速域名");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* certificate-id
|
||||
* name
|
||||
@@ -113,7 +104,7 @@ export class CmccDeployCertToCdn extends AbstractPlusTaskPlugin {
|
||||
return {
|
||||
label: `${item.domainName}`,
|
||||
value: item.domainName,
|
||||
domain: item.domainName
|
||||
domain: item.domainName,
|
||||
};
|
||||
});
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user