Files
certd/packages/ui/certd-server/src/plugins/plugin-host/plugin/copy-to-local/index.ts
T

190 lines
5.9 KiB
TypeScript
Raw Normal View History

2024-08-25 11:56:15 +08:00
import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput, TaskOutput } from '@certd/pipeline';
import { CertInfo, CertReader } from '@certd/plugin-cert';
import * as fs from 'fs';
2024-10-03 22:03:49 +08:00
import { Constants } from '@certd/lib-server';
2024-08-25 11:56:15 +08:00
import path from 'path';
@IsTaskPlugin({
name: 'CopyToLocal',
title: '复制到本机',
2024-09-19 17:38:51 +08:00
icon: 'solar:copy-bold-duotone',
desc: '【仅管理员使用】实际上是复制证书到docker容器内的某个路径,需要做目录映射到宿主机',
2024-08-25 11:56:15 +08:00
group: pluginGroups.host.key,
default: {
strategy: {
runStrategy: RunStrategy.SkipWhenSucceed,
},
},
})
export class CopyCertToLocalPlugin extends AbstractTaskPlugin {
@TaskInput({
title: '证书保存路径',
2024-10-29 23:37:18 +08:00
helper: '全链证书,路径要包含文件名' + '\n推荐使用相对路径,将写入与数据库同级目录,无需映射,例如:tmp/cert.pem',
2024-08-25 11:56:15 +08:00
component: {
2024-10-29 23:37:18 +08:00
placeholder: 'tmp/full_chain.pem',
2024-08-25 11:56:15 +08:00
},
2024-10-25 22:49:05 +08:00
rules: [{ type: 'filepath' }],
2024-08-25 11:56:15 +08:00
})
crtPath!: string;
@TaskInput({
title: '私钥保存路径',
2024-10-29 23:37:18 +08:00
helper: '路径要包含文件名\n推荐使用相对路径,将写入与数据库同级目录,无需映射,例如:tmp/cert.key',
2024-08-25 11:56:15 +08:00
component: {
2024-10-29 23:37:18 +08:00
placeholder: 'tmp/cert.key',
2024-08-25 11:56:15 +08:00
},
2024-10-25 22:49:05 +08:00
rules: [{ type: 'filepath' }],
2024-08-25 11:56:15 +08:00
})
keyPath!: string;
2024-09-06 00:13:21 +08:00
2024-09-26 10:24:25 +08:00
@TaskInput({
title: '中间证书保存路径',
2024-10-25 22:49:05 +08:00
helper: '路径要包含文件名,一般情况传上面两个文件就行了,极少数情况需要这个中间证书',
2024-09-26 10:24:25 +08:00
component: {
2024-09-27 14:46:38 +08:00
placeholder: '/root/deploy/nginx/intermediate.pem',
2024-09-26 10:24:25 +08:00
},
2024-10-25 22:49:05 +08:00
rules: [{ type: 'filepath' }],
2024-09-26 10:24:25 +08:00
})
icPath!: string;
2024-09-06 00:13:21 +08:00
@TaskInput({
title: 'PFX证书保存路径',
2024-10-29 23:37:18 +08:00
helper: '用于IIS证书部署,路径要包含文件名\n推荐使用相对路径,将写入与数据库同级目录,无需映射,例如:tmp/cert.pfx',
2024-09-06 00:13:21 +08:00
component: {
2024-10-29 23:37:18 +08:00
placeholder: 'tmp/cert.pfx',
2024-09-06 00:13:21 +08:00
},
2024-10-25 22:49:05 +08:00
rules: [{ type: 'filepath' }],
2024-09-06 00:13:21 +08:00
})
pfxPath!: string;
@TaskInput({
title: 'DER证书保存路径',
helper:
2024-10-29 23:37:18 +08:00
'用于Apache证书部署,路径要包含文件名\n推荐使用相对路径,将写入与数据库同级目录,无需映射,例如:tmp/cert.der\n.der和.cer是相同的东西,改个后缀名即可',
2024-09-06 00:13:21 +08:00
component: {
2024-10-29 23:37:18 +08:00
placeholder: 'tmp/cert.der 或 tmp/cert.cer',
2024-09-06 00:13:21 +08:00
},
2024-10-25 22:49:05 +08:00
rules: [{ type: 'filepath' }],
2024-09-06 00:13:21 +08:00
})
derPath!: string;
2024-10-29 23:37:18 +08:00
@TaskInput({
2024-10-30 01:44:02 +08:00
title: 'jks证书保存路径',
helper: '用于java,路径要包含文件名,例如:tmp/cert.jks',
2024-10-29 23:37:18 +08:00
component: {
2024-10-30 01:44:02 +08:00
placeholder: 'tmp/cert.jks',
2024-10-29 23:37:18 +08:00
},
rules: [{ type: 'filepath' }],
})
2024-10-30 01:44:02 +08:00
jksPath!: string;
2024-10-29 23:37:18 +08:00
2024-08-25 11:56:15 +08:00
@TaskInput({
title: '域名证书',
helper: '请选择前置任务输出的域名证书',
component: {
name: 'output-selector',
2024-09-18 14:58:59 +08:00
from: ['CertApply', 'CertApplyLego'],
2024-08-25 11:56:15 +08:00
},
required: true,
})
cert!: CertInfo;
@TaskOutput({
title: '证书保存路径',
2024-09-06 00:13:21 +08:00
type: 'HostCrtPath',
2024-08-25 11:56:15 +08:00
})
hostCrtPath!: string;
@TaskOutput({
title: '私钥保存路径',
2024-09-06 00:13:21 +08:00
type: 'HostKeyPath',
2024-08-25 11:56:15 +08:00
})
hostKeyPath!: string;
2024-09-26 10:24:25 +08:00
@TaskOutput({
title: '中间证书保存路径',
type: 'HostKeyPath',
})
hostIcPath!: string;
2024-09-06 00:13:21 +08:00
@TaskOutput({
title: 'PFX保存路径',
type: 'HostPfxPath',
})
hostPfxPath!: string;
@TaskOutput({
title: 'DER保存路径',
type: 'HostDerPath',
})
hostDerPath!: string;
2024-10-29 23:37:18 +08:00
@TaskOutput({
2024-10-30 01:44:02 +08:00
title: 'jks保存路径',
type: 'HostJksPath',
2024-10-29 23:37:18 +08:00
})
2024-10-30 01:44:02 +08:00
hostJksPath!: string;
2024-10-29 23:37:18 +08:00
2024-08-25 11:56:15 +08:00
async onInstance() {}
copyFile(srcFile: string, destFile: string) {
this.logger.info(`复制文件:${srcFile} => ${destFile}`);
const dir = path.dirname(destFile);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.copyFileSync(srcFile, destFile);
}
async execute(): Promise<void> {
if (!this.isAdmin()) {
throw new Error('只有管理员才能运行此任务');
}
2024-10-30 01:44:02 +08:00
let { crtPath, keyPath, icPath, pfxPath, derPath, jksPath } = this;
2024-08-25 11:56:15 +08:00
const certReader = new CertReader(this.cert);
2024-10-30 01:44:02 +08:00
const handle = async ({ reader, tmpCrtPath, tmpKeyPath, tmpDerPath, tmpPfxPath, tmpIcPath, tmpJksPath }) => {
2024-09-06 00:13:21 +08:00
this.logger.info('复制到目标路径');
if (crtPath) {
crtPath = crtPath.startsWith('/') ? crtPath : path.join(Constants.dataDir, crtPath);
this.copyFile(tmpCrtPath, crtPath);
this.hostCrtPath = crtPath;
}
if (keyPath) {
keyPath = keyPath.startsWith('/') ? keyPath : path.join(Constants.dataDir, keyPath);
this.copyFile(tmpKeyPath, keyPath);
this.hostKeyPath = keyPath;
}
2024-09-26 10:24:25 +08:00
if (icPath) {
icPath = icPath.startsWith('/') ? icPath : path.join(Constants.dataDir, icPath);
2024-09-27 16:25:18 +08:00
this.copyFile(tmpIcPath, icPath);
2024-09-26 10:24:25 +08:00
this.hostIcPath = icPath;
}
2024-09-06 00:13:21 +08:00
if (pfxPath) {
pfxPath = pfxPath.startsWith('/') ? pfxPath : path.join(Constants.dataDir, pfxPath);
this.copyFile(tmpPfxPath, pfxPath);
this.hostPfxPath = pfxPath;
}
if (derPath) {
derPath = derPath.startsWith('/') ? derPath : path.join(Constants.dataDir, derPath);
this.copyFile(tmpDerPath, derPath);
this.hostDerPath = derPath;
}
2024-10-30 01:44:02 +08:00
if (jksPath) {
jksPath = jksPath.startsWith('/') ? jksPath : path.join(Constants.dataDir, jksPath);
this.copyFile(tmpJksPath, jksPath);
this.hostJksPath = jksPath;
2024-10-29 23:37:18 +08:00
}
2024-08-25 11:56:15 +08:00
this.logger.info('请注意,如果使用的是相对路径,那么文件就在你的数据库同级目录下,默认是/data/certd/下面');
2024-09-20 11:11:25 +08:00
this.logger.info(
'请注意,如果使用的是绝对路径,文件在容器内的目录下,你需要给容器做目录映射才能复制到宿主机,需要在docker-compose.yaml中配置主机目录映射: volumes: /你宿主机的路径:/任务配置的证书路径'
);
2024-09-06 00:13:21 +08:00
};
await certReader.readCertFile({ logger: this.logger, handle });
2024-08-25 11:56:15 +08:00
this.logger.info('执行完成');
}
}
new CopyCertToLocalPlugin();