mirror of
https://github.com/certd/certd.git
synced 2026-07-01 08:57:33 +08:00
chore: format
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
// 导入所需的 SDK 模块
|
||||
import { AwsAccess } from '../access.js';
|
||||
import { CertInfo, DomainRecord } from '@certd/plugin-cert';
|
||||
import { ILogger, utils } from '@certd/basic';
|
||||
import { PageRes, PageSearch } from '@certd/pipeline';
|
||||
type AwsClientOptions = { access: AwsAccess; region: string, logger: ILogger };
|
||||
import { AwsAccess } from "../access.js";
|
||||
import { CertInfo, DomainRecord } from "@certd/plugin-cert";
|
||||
import { ILogger, utils } from "@certd/basic";
|
||||
import { PageRes, PageSearch } from "@certd/pipeline";
|
||||
type AwsClientOptions = { access: AwsAccess; region: string; logger: ILogger };
|
||||
|
||||
/**
|
||||
* https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/route-53-domains/
|
||||
@@ -21,7 +21,7 @@ export class AwsClient {
|
||||
}
|
||||
async importCertificate(certInfo: CertInfo) {
|
||||
// 创建 ACM 客户端
|
||||
const { ACMClient, ImportCertificateCommand } = await import('@aws-sdk/client-acm');
|
||||
const { ACMClient, ImportCertificateCommand } = await import("@aws-sdk/client-acm");
|
||||
const acmClient = new ACMClient({
|
||||
region: this.region, // 替换为您的 AWS 区域
|
||||
credentials: {
|
||||
@@ -30,7 +30,7 @@ export class AwsClient {
|
||||
},
|
||||
});
|
||||
|
||||
const cert = certInfo.crt.split('-----END CERTIFICATE-----')[0] + '-----END CERTIFICATE-----';
|
||||
const cert = certInfo.crt.split("-----END CERTIFICATE-----")[0] + "-----END CERTIFICATE-----";
|
||||
// 构建上传参数
|
||||
const data = await acmClient.send(
|
||||
new ImportCertificateCommand({
|
||||
@@ -39,17 +39,16 @@ export class AwsClient {
|
||||
// CertificateChain: certificateChain, // 可选
|
||||
})
|
||||
);
|
||||
console.log('Upload successful:', data);
|
||||
console.log("Upload successful:", data);
|
||||
// 返回证书 ARN(Amazon Resource Name)
|
||||
return data.CertificateArn;
|
||||
}
|
||||
|
||||
|
||||
async getCallerIdentity() {
|
||||
const { STSClient, GetCallerIdentityCommand } = await import ("@aws-sdk/client-sts");
|
||||
const { STSClient, GetCallerIdentityCommand } = await import("@aws-sdk/client-sts");
|
||||
|
||||
const client = new STSClient({
|
||||
region: this.access.region || 'us-east-1',
|
||||
region: this.access.region || "us-east-1",
|
||||
credentials: {
|
||||
accessKeyId: this.access.accessKeyId, // 从环境变量中读取
|
||||
secretAccessKey: this.access.secretAccessKey,
|
||||
@@ -64,9 +63,8 @@ export class AwsClient {
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
async route53ClientGet() {
|
||||
const { Route53Client } = await import('@aws-sdk/client-route-53');
|
||||
const { Route53Client } = await import("@aws-sdk/client-route-53");
|
||||
return new Route53Client({
|
||||
region: this.region,
|
||||
credentials: {
|
||||
@@ -76,20 +74,21 @@ export class AwsClient {
|
||||
});
|
||||
}
|
||||
|
||||
async route53GetHostedZoneId(name: string): Promise<{ ZoneId: string, ZoneName: string }> {
|
||||
async route53GetHostedZoneId(name: string): Promise<{ ZoneId: string; ZoneName: string }> {
|
||||
const hostedZones = await this.route53ListHostedZones(name);
|
||||
const zoneId = hostedZones[0].Id.replace('/hostedzone/', '');
|
||||
const zoneId = hostedZones[0].Id.replace("/hostedzone/", "");
|
||||
this.logger.info(`获取到hostedZoneId:${zoneId},name:${hostedZones[0].Name}`);
|
||||
return {
|
||||
ZoneId: zoneId,
|
||||
ZoneName: hostedZones[0].Name,
|
||||
};
|
||||
}
|
||||
async route53ListHostedZones(name: string): Promise<{ Id: string, Name: string }[]> {
|
||||
async route53ListHostedZones(name: string): Promise<{ Id: string; Name: string }[]> {
|
||||
const { ListHostedZonesByNameCommand } = await import("@aws-sdk/client-route-53"); // ES Modules import
|
||||
|
||||
const client = await this.route53ClientGet();
|
||||
const input = { // ListHostedZonesByNameRequest
|
||||
const input = {
|
||||
// ListHostedZonesByNameRequest
|
||||
DNSName: name,
|
||||
};
|
||||
const command = new ListHostedZonesByNameCommand(input);
|
||||
@@ -105,7 +104,8 @@ export class AwsClient {
|
||||
const { ListHostedZonesByNameCommand } = await import("@aws-sdk/client-route-53"); // ES Modules import
|
||||
|
||||
const client = await this.route53ClientGet();
|
||||
const input: any = { // ListHostedZonesByNameRequest
|
||||
const input: any = {
|
||||
// ListHostedZonesByNameRequest
|
||||
MaxItems: req.pageSize,
|
||||
};
|
||||
if (req.searchKey) {
|
||||
@@ -115,7 +115,7 @@ export class AwsClient {
|
||||
const response = await this.doRequest(() => client.send(command));
|
||||
let list: any[] = response.HostedZones || [];
|
||||
list = list.map((item: any) => ({
|
||||
id: item.Id.replace('/hostedzone/', ''),
|
||||
id: item.Id.replace("/hostedzone/", ""),
|
||||
domain: item.Name,
|
||||
}));
|
||||
return {
|
||||
@@ -124,24 +124,29 @@ export class AwsClient {
|
||||
};
|
||||
}
|
||||
|
||||
async route53ChangeRecord(req: {
|
||||
hostedZoneId: string, fullRecord: string, type: string, value: string, action: "UPSERT" | "DELETE"
|
||||
}) {
|
||||
async route53ChangeRecord(req: { hostedZoneId: string; fullRecord: string; type: string; value: string; action: "UPSERT" | "DELETE" }) {
|
||||
const { ChangeResourceRecordSetsCommand } = await import("@aws-sdk/client-route-53"); // ES Modules import
|
||||
// const { Route53Client, ChangeResourceRecordSetsCommand } = require("@aws-sdk/client-route-53"); // CommonJS import
|
||||
// import type { Route53ClientConfig } from "@aws-sdk/client-route-53";
|
||||
const client = await this.route53ClientGet();
|
||||
const input = { // ChangeResourceRecordSetsRequest
|
||||
const input = {
|
||||
// ChangeResourceRecordSetsRequest
|
||||
HostedZoneId: req.hostedZoneId, // required
|
||||
ChangeBatch: { // ChangeBatch
|
||||
Changes: [ // Changes // required
|
||||
{ // Change
|
||||
ChangeBatch: {
|
||||
// ChangeBatch
|
||||
Changes: [
|
||||
// Changes // required
|
||||
{
|
||||
// Change
|
||||
Action: req.action as any, // required
|
||||
ResourceRecordSet: { // ResourceRecordSet
|
||||
ResourceRecordSet: {
|
||||
// ResourceRecordSet
|
||||
Name: req.fullRecord, // required
|
||||
Type: req.type.toUpperCase() as any,
|
||||
ResourceRecords: [ // ResourceRecords
|
||||
{ // ResourceRecord
|
||||
ResourceRecords: [
|
||||
// ResourceRecords
|
||||
{
|
||||
// ResourceRecord
|
||||
Value: `"${req.value}"`, // required
|
||||
},
|
||||
],
|
||||
@@ -154,7 +159,7 @@ export class AwsClient {
|
||||
this.logger.info(`设置域名解析参数:${JSON.stringify(input)}`);
|
||||
const command = new ChangeResourceRecordSetsCommand(input);
|
||||
const response = await this.doRequest(() => client.send(command));
|
||||
console.log('Add record successful:', JSON.stringify(response));
|
||||
console.log("Add record successful:", JSON.stringify(response));
|
||||
await utils.sleep(3000);
|
||||
return response;
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user