Files
certd/packages/ui/certd-server/src/plugins/plugin-aliyun/plugin/upload-to-aliyun/index.ts
T

102 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-08-13 20:30:42 +08:00
import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput, TaskOutput } from '@certd/pipeline';
2024-09-02 23:47:15 +08:00
import { appendTimeSuffix, checkRet } from '../../utils/index.js';
2024-09-18 14:58:59 +08:00
import { AliyunAccess, AliyunClient } from '@certd/plugin-plus';
@IsTaskPlugin({
name: 'uploadCertToAliyun',
title: '上传证书到阿里云',
2024-09-19 17:38:51 +08:00
icon: 'ant-design:aliyun-outlined',
group: pluginGroups.aliyun.key,
desc: '',
default: {
strategy: {
runStrategy: RunStrategy.SkipWhenSucceed,
},
},
})
export class UploadCertToAliyun extends AbstractTaskPlugin {
@TaskInput({
title: '证书名称',
helper: '证书上传后将以此参数作为名称前缀',
})
name!: string;
@TaskInput({
title: '大区',
2024-08-06 11:23:23 +08:00
value: 'cn-hangzhou',
component: {
2024-08-13 20:30:42 +08:00
name: 'a-auto-complete',
vModel: 'value',
2024-09-02 23:46:28 +08:00
options: [{ value: 'cn-hangzhou' }, { value: 'eu-central-1' }, { value: 'ap-southeast-1' }],
},
required: true,
})
regionId!: string;
@TaskInput({
title: '域名证书',
helper: '请选择前置任务输出的域名证书',
component: {
name: 'output-selector',
2024-09-18 14:58:59 +08:00
from: ['CertApply', 'CertApplyLego'],
},
required: true,
})
cert!: any;
@TaskInput({
title: 'Access授权',
helper: '阿里云授权AccessKeyId、AccessKeySecret',
component: {
name: 'access-selector',
type: 'aliyun',
},
required: true,
})
accessId!: string;
@TaskOutput({
title: '上传成功后的阿里云CertId',
})
aliyunCertId!: string;
2024-08-13 20:30:42 +08:00
async onInstance() {}
async execute(): Promise<void> {
2024-09-02 23:46:28 +08:00
this.logger.info('开始部署证书到阿里云cdn');
const access: AliyunAccess = await this.accessService.getById(this.accessId);
const client = await this.getClient(access);
const certName = appendTimeSuffix(this.name);
const params = {
RegionId: this.regionId || 'cn-hangzhou',
Name: certName,
Cert: this.cert.crt,
Key: this.cert.key,
};
const requestOption = {
method: 'POST',
};
2024-09-02 23:46:28 +08:00
const ret: any = await client.request('CreateUserCertificate', params, requestOption);
checkRet(ret);
this.logger.info('证书上传成功:aliyunCertId=', ret.CertId);
//output
this.aliyunCertId = ret.CertId;
}
2024-09-02 23:46:28 +08:00
async getClient(aliyunProvider: AliyunAccess) {
2024-09-18 14:58:59 +08:00
const client = new AliyunClient({ logger: this.logger });
2024-09-05 18:00:45 +08:00
await client.init({
accessKeyId: aliyunProvider.accessKeyId,
accessKeySecret: aliyunProvider.accessKeySecret,
endpoint: 'https://cas.aliyuncs.com',
apiVersion: '2018-07-13',
2024-09-18 14:58:59 +08:00
});
return client;
}
}
//注册插件
new UploadCertToAliyun();