2024-11-29 19:00:05 +08:00
|
|
|
|
import { ILogger } from "@certd/basic";
|
|
|
|
|
|
import { AliyunAccess } from "../access/index.js";
|
|
|
|
|
|
import { AliyunClient } from "./index.js";
|
|
|
|
|
|
|
2025-01-02 00:28:13 +08:00
|
|
|
|
export type AliyunCertInfo = {
|
|
|
|
|
|
crt: string; //fullchain证书
|
|
|
|
|
|
key: string; //私钥
|
|
|
|
|
|
};
|
2024-11-29 19:00:05 +08:00
|
|
|
|
export type AliyunSslClientOpts = {
|
|
|
|
|
|
access: AliyunAccess;
|
|
|
|
|
|
logger: ILogger;
|
|
|
|
|
|
endpoint: string;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export type AliyunSslGetResourceListReq = {
|
|
|
|
|
|
cloudProduct: string;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export type AliyunSslCreateDeploymentJobReq = {
|
|
|
|
|
|
name: string;
|
|
|
|
|
|
jobType: string;
|
|
|
|
|
|
contactIds: string[];
|
|
|
|
|
|
resourceIds: string[];
|
|
|
|
|
|
certIds: string[];
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export type AliyunSslUploadCertReq = {
|
|
|
|
|
|
name: string;
|
2025-01-02 00:28:13 +08:00
|
|
|
|
cert: AliyunCertInfo;
|
2024-11-29 19:00:05 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-06-09 23:41:44 +08:00
|
|
|
|
export type CasCertInfo = { certId: number; certName: string; certIdentifier: string; notAfter: number; casRegion: string };
|
2025-01-19 22:55:46 +08:00
|
|
|
|
|
2024-11-29 19:00:05 +08:00
|
|
|
|
export class AliyunSslClient {
|
|
|
|
|
|
opts: AliyunSslClientOpts;
|
2025-07-10 21:40:35 +08:00
|
|
|
|
logger: ILogger;
|
2024-11-29 19:00:05 +08:00
|
|
|
|
constructor(opts: AliyunSslClientOpts) {
|
|
|
|
|
|
this.opts = opts;
|
2025-07-10 21:40:35 +08:00
|
|
|
|
this.logger = opts.logger;
|
2024-11-29 19:00:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
checkRet(ret: any) {
|
2025-01-19 15:31:37 +08:00
|
|
|
|
if (ret.Code != null) {
|
2024-11-29 19:00:05 +08:00
|
|
|
|
throw new Error("执行失败:" + ret.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async getClient() {
|
|
|
|
|
|
const access = this.opts.access;
|
|
|
|
|
|
const client = new AliyunClient({ logger: this.opts.logger });
|
|
|
|
|
|
await client.init({
|
|
|
|
|
|
accessKeyId: access.accessKeyId,
|
|
|
|
|
|
accessKeySecret: access.accessKeySecret,
|
|
|
|
|
|
endpoint: `https://${this.opts.endpoint || "cas.aliyuncs.com"}`,
|
|
|
|
|
|
apiVersion: "2020-04-07",
|
|
|
|
|
|
});
|
|
|
|
|
|
return client;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-19 22:55:46 +08:00
|
|
|
|
async getCertInfo(certId: number): Promise<CasCertInfo> {
|
|
|
|
|
|
const client = await this.getClient();
|
|
|
|
|
|
const params = {
|
|
|
|
|
|
CertId: certId,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const res = await client.request("GetUserCertificateDetail", params);
|
|
|
|
|
|
this.checkRet(res);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
certId: certId,
|
|
|
|
|
|
certName: res.Name,
|
|
|
|
|
|
certIdentifier: res.CertIdentifier,
|
2025-06-07 00:15:16 +08:00
|
|
|
|
notAfter: res.NotAfter,
|
2025-06-09 23:41:44 +08:00
|
|
|
|
casRegion: this.getCasRegionFromEndpoint(this.opts.endpoint),
|
2025-01-19 22:55:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-29 19:00:05 +08:00
|
|
|
|
async uploadCert(req: AliyunSslUploadCertReq) {
|
|
|
|
|
|
const client = await this.getClient();
|
|
|
|
|
|
const params = {
|
|
|
|
|
|
Name: req.name,
|
|
|
|
|
|
Cert: req.cert.crt,
|
|
|
|
|
|
Key: req.cert.key,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const requestOption = {
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-05-26 22:44:56 +08:00
|
|
|
|
this.opts.logger.info(`开始上传证书:${req.name}`);
|
2024-11-29 19:00:05 +08:00
|
|
|
|
const ret: any = await client.request("UploadUserCertificate", params, requestOption);
|
|
|
|
|
|
this.checkRet(ret);
|
|
|
|
|
|
this.opts.logger.info("证书上传成功:aliyunCertId=", ret.CertId);
|
|
|
|
|
|
//output
|
|
|
|
|
|
return ret.CertId;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async getResourceList(req: AliyunSslGetResourceListReq) {
|
|
|
|
|
|
const client = await this.getClient();
|
|
|
|
|
|
const params = {
|
|
|
|
|
|
CloudName: "aliyun",
|
|
|
|
|
|
CloudProduct: req.cloudProduct,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const requestOption = {
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
formatParams: false,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const res = await client.request("ListCloudResources", params, requestOption);
|
|
|
|
|
|
this.checkRet(res);
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async createDeploymentJob(req: AliyunSslCreateDeploymentJobReq) {
|
|
|
|
|
|
const client = await this.getClient();
|
|
|
|
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
|
|
Name: req.name,
|
|
|
|
|
|
JobType: req.jobType,
|
|
|
|
|
|
ContactIds: req.contactIds.join(","),
|
|
|
|
|
|
ResourceIds: req.resourceIds.join(","),
|
|
|
|
|
|
CertIds: req.certIds.join(","),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const requestOption = {
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
formatParams: false,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const res = await client.request("CreateDeploymentJob", params, requestOption);
|
|
|
|
|
|
this.checkRet(res);
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async getContactList() {
|
|
|
|
|
|
const params = {};
|
|
|
|
|
|
|
|
|
|
|
|
const requestOption = {
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
formatParams: false,
|
|
|
|
|
|
};
|
|
|
|
|
|
const client = await this.getClient();
|
|
|
|
|
|
const res = await client.request("ListContact", params, requestOption);
|
|
|
|
|
|
this.checkRet(res);
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async doRequest(action: string, params: any, requestOption: any) {
|
|
|
|
|
|
const client = await this.getClient();
|
|
|
|
|
|
const res = await client.request(action, params, requestOption);
|
|
|
|
|
|
this.checkRet(res);
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
2025-06-07 00:15:16 +08:00
|
|
|
|
|
|
|
|
|
|
async deleteCert(certId: any) {
|
|
|
|
|
|
await this.doRequest("DeleteUserCertificate", { CertId: certId }, { method: "POST" });
|
|
|
|
|
|
}
|
2025-06-09 22:46:59 +08:00
|
|
|
|
|
|
|
|
|
|
getCasRegionFromEndpoint(endpoint: string) {
|
2025-06-09 23:41:44 +08:00
|
|
|
|
if (!endpoint) {
|
|
|
|
|
|
return "cn-hangzhou";
|
|
|
|
|
|
}
|
2025-06-09 22:46:59 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* {value: 'cas.aliyuncs.com', label: '中国大陆'},
|
|
|
|
|
|
* {value: 'cas.ap-southeast-1.aliyuncs.com', label: '新加坡'},
|
|
|
|
|
|
* {value: 'cas.eu-central-1.aliyuncs.com', label: '德国(法兰克福)'},
|
|
|
|
|
|
*/
|
|
|
|
|
|
const region = endpoint.replace(".aliyuncs.com", "").replace("cas.", "");
|
|
|
|
|
|
if (region === "cas") {
|
|
|
|
|
|
return "cn-hangzhou";
|
|
|
|
|
|
}
|
|
|
|
|
|
return region;
|
|
|
|
|
|
}
|
2024-11-29 19:00:05 +08:00
|
|
|
|
}
|