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

201 lines
5.8 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-07-15 00:30:33 +08:00
import { SshClient } from '../../lib/ssh.js';
2024-09-05 15:36:35 +08:00
import { CertInfo, CertReader, CertReaderHandleContext } from '@certd/plugin-cert';
import * as fs from 'fs';
2024-07-15 00:30:33 +08:00
import { SshAccess } from '../../access/index.js';
2022-11-07 23:31:20 +08:00
2023-01-11 20:39:48 +08:00
@IsTaskPlugin({
name: 'uploadCertToHost',
title: '上传证书到主机',
2024-09-19 17:38:51 +08:00
icon:"line-md:uploading-loop",
group: pluginGroups.host.key,
desc: '也支持复制证书到本机',
2023-01-11 20:39:48 +08:00
default: {
strategy: {
runStrategy: RunStrategy.SkipWhenSucceed,
2022-11-07 23:31:20 +08:00
},
2023-01-11 20:39:48 +08:00
},
})
2023-05-24 15:41:35 +08:00
export class UploadCertToHostPlugin extends AbstractTaskPlugin {
2023-01-11 20:39:48 +08:00
@TaskInput({
2024-09-06 00:13:21 +08:00
title: 'PEM证书保存路径',
helper: '需要有写入权限,路径要包含证书文件名,文件名不能用*?!等特殊符号,例如:/tmp/cert.pem',
component: {
placeholder: '/root/deploy/nginx/cert.pem',
},
2023-01-11 20:39:48 +08:00
})
crtPath!: string;
@TaskInput({
title: '私钥保存路径',
2024-09-06 00:13:21 +08:00
helper: '需要有写入权限,路径要包含私钥文件名,文件名不能用*?!等特殊符号,例如:/tmp/cert.key',
component: {
placeholder: '/root/deploy/nginx/cert.key',
},
2023-01-11 20:39:48 +08:00
})
keyPath!: string;
2024-09-06 00:13:21 +08:00
@TaskInput({
title: 'PFX证书保存路径',
helper: '需要有写入权限,路径要包含私钥文件名,文件名不能用*?!等特殊符号,例如:/tmp/cert.pfx',
component: {
placeholder: '/root/deploy/nginx/cert.pfx',
},
})
pfxPath!: string;
@TaskInput({
title: 'DER证书保存路径',
helper: '需要有写入权限,路径要包含私钥文件名,文件名不能用*?!等特殊符号,例如:/tmp/cert.der',
component: {
placeholder: '/root/deploy/nginx/cert.der',
},
})
derPath!: string;
2023-01-11 20:39:48 +08:00
@TaskInput({
title: '域名证书',
helper: '请选择前置任务输出的域名证书',
2023-01-11 20:39:48 +08:00
component: {
name: 'pi-output-selector',
2024-09-18 14:58:59 +08:00
from: ['CertApply', 'CertApplyLego'],
2022-11-07 23:31:20 +08:00
},
2023-01-11 20:39:48 +08:00
required: true,
})
cert!: CertInfo;
2023-01-11 20:39:48 +08:00
@TaskInput({
title: '主机登录配置',
helper: 'access授权',
2023-01-11 20:39:48 +08:00
component: {
name: 'pi-access-selector',
type: 'ssh',
2022-11-07 23:31:20 +08:00
},
rules: [{ required: false, message: '' }],
2023-01-11 20:39:48 +08:00
})
accessId!: string;
@TaskInput({
title: '自动创建远程目录',
helper: '是否自动创建远程目录,如果关闭则你需要自己确保远程目录存在',
value: true,
component: {
name: 'a-switch',
vModel: 'checked',
},
})
mkdirs = true;
@TaskInput({
2024-06-25 10:52:58 +08:00
title: '仅复制到当前主机',
helper:
2024-06-25 10:52:58 +08:00
'开启后将直接复制到当前主机某个目录不上传到主机由于是docker启动实际上是复制到docker容器内的“证书保存路径”你需要事先在docker-compose.yaml中配置主机目录映射 volumes: /your_target_path:/your_target_path',
value: false,
component: {
name: 'a-switch',
vModel: 'checked',
},
})
copyToThisHost!: boolean;
2023-01-11 20:39:48 +08:00
@TaskOutput({
title: '证书保存路径',
2023-01-11 20:39:48 +08:00
})
hostCrtPath!: string;
@TaskOutput({
title: '私钥保存路径',
2023-01-11 20:39:48 +08:00
})
hostKeyPath!: string;
2024-09-06 00:13:21 +08:00
@TaskOutput({
title: 'PFX保存路径',
})
hostPfxPath!: string;
@TaskOutput({
title: 'DER保存路径',
})
hostDerPath!: string;
2024-08-13 20:30:42 +08:00
async onInstance() {}
copyFile(srcFile: string, destFile: string) {
2024-09-06 00:13:21 +08:00
if (!srcFile || !destFile) {
this.logger.warn(`srcFile:${srcFile} 或 destFile:${destFile} 为空,不复制`);
return;
}
const dir = destFile.substring(0, destFile.lastIndexOf('/'));
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.copyFileSync(srcFile, destFile);
}
2023-01-11 20:39:48 +08:00
async execute(): Promise<void> {
2024-04-08 10:05:11 +08:00
const { crtPath, keyPath, cert, accessId } = this;
const certReader = new CertReader(cert);
2024-09-05 15:36:35 +08:00
const handle = async (opts: CertReaderHandleContext) => {
2024-09-06 00:13:21 +08:00
const { tmpCrtPath, tmpKeyPath, tmpDerPath, tmpPfxPath } = opts;
2024-07-08 11:10:08 +08:00
if (this.copyToThisHost) {
this.logger.info('复制到目标路径');
2024-09-05 15:36:35 +08:00
this.copyFile(tmpCrtPath, crtPath);
this.copyFile(tmpKeyPath, keyPath);
2024-09-06 00:13:21 +08:00
this.copyFile(tmpPfxPath, this.pfxPath);
this.copyFile(tmpDerPath, this.derPath);
this.logger.info(`证书复制成功crtPath=${crtPath},keyPath=${keyPath},pfxPath=${this.pfxPath},derPath=${this.derPath}`);
2024-07-08 11:10:08 +08:00
} else {
if (!accessId) {
throw new Error('主机登录授权配置不能为空');
}
this.logger.info('准备上传文件到服务器');
2024-07-15 02:13:53 +08:00
const connectConf: SshAccess = await this.accessService.getById(accessId);
2024-07-08 11:10:08 +08:00
const sshClient = new SshClient(this.logger);
2024-09-06 00:13:21 +08:00
const transports: any = [];
if (crtPath) {
transports.push({
localPath: tmpCrtPath,
remotePath: crtPath,
});
}
if (keyPath) {
transports.push({
localPath: tmpKeyPath,
remotePath: keyPath,
});
}
if (this.pfxPath) {
transports.push({
localPath: tmpPfxPath,
remotePath: this.pfxPath,
});
}
if (this.derPath) {
transports.push({
localPath: tmpDerPath,
remotePath: this.derPath,
});
}
2024-07-08 11:10:08 +08:00
await sshClient.uploadFiles({
connectConf,
2024-09-06 00:13:21 +08:00
transports,
mkdirs: this.mkdirs,
2024-07-08 11:10:08 +08:00
});
2024-09-06 00:13:21 +08:00
this.logger.info(`证书上传成功crtPath=${crtPath},keyPath=${keyPath},pfxPath=${this.pfxPath},derPath=${this.derPath}`);
2024-09-05 15:36:35 +08:00
//输出
this.hostCrtPath = crtPath;
this.hostKeyPath = keyPath;
2024-09-06 00:13:21 +08:00
this.hostPfxPath = this.pfxPath;
this.hostDerPath = this.derPath;
}
2024-09-05 15:36:35 +08:00
};
await certReader.readCertFile({
logger: this.logger,
handle,
});
2022-11-07 23:31:20 +08:00
}
}
2023-05-09 09:56:31 +08:00
new UploadCertToHostPlugin();