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';
|
2024-05-27 18:38:41 +08:00
|
|
|
|
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({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
name: 'uploadCertToHost',
|
|
|
|
|
|
title: '上传证书到主机',
|
2024-09-20 11:11:25 +08:00
|
|
|
|
icon: 'line-md:uploading-loop',
|
2024-07-21 02:26:03 +08:00
|
|
|
|
group: pluginGroups.host.key,
|
2024-09-22 23:19:10 +08:00
|
|
|
|
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',
|
2024-05-27 18:38:41 +08:00
|
|
|
|
component: {
|
2024-07-03 23:39:12 +08:00
|
|
|
|
placeholder: '/root/deploy/nginx/cert.pem',
|
2024-05-27 18:38:41 +08:00
|
|
|
|
},
|
2023-01-11 20:39:48 +08:00
|
|
|
|
})
|
|
|
|
|
|
crtPath!: string;
|
|
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '私钥保存路径',
|
2024-09-06 00:13:21 +08:00
|
|
|
|
helper: '需要有写入权限,路径要包含私钥文件名,文件名不能用*?!等特殊符号,例如:/tmp/cert.key',
|
2024-05-27 18:38:41 +08:00
|
|
|
|
component: {
|
2024-06-19 00:21:13 +08:00
|
|
|
|
placeholder: '/root/deploy/nginx/cert.key',
|
2024-05-27 18:38:41 +08:00
|
|
|
|
},
|
2023-01-11 20:39:48 +08:00
|
|
|
|
})
|
|
|
|
|
|
keyPath!: string;
|
2024-09-06 00:13:21 +08:00
|
|
|
|
|
2024-09-22 23:19:10 +08:00
|
|
|
|
@TaskInput({
|
|
|
|
|
|
title: '中间证书保存路径',
|
|
|
|
|
|
helper: '需要有写入权限,路径要包含私钥文件名,文件名不能用*?!等特殊符号,例如:/tmp/intermediate.crt',
|
|
|
|
|
|
component: {
|
|
|
|
|
|
placeholder: '/root/deploy/nginx/intermediate.crt',
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
icPath!: string;
|
|
|
|
|
|
|
2024-09-06 00:13:21 +08:00
|
|
|
|
@TaskInput({
|
|
|
|
|
|
title: 'PFX证书保存路径',
|
2024-09-20 11:11:25 +08:00
|
|
|
|
helper: '用于IIS证书部署,需要有写入权限,路径要包含私钥文件名,文件名不能用*?!等特殊符号,例如:/tmp/cert.pfx',
|
2024-09-06 00:13:21 +08:00
|
|
|
|
component: {
|
|
|
|
|
|
placeholder: '/root/deploy/nginx/cert.pfx',
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
pfxPath!: string;
|
|
|
|
|
|
|
|
|
|
|
|
@TaskInput({
|
|
|
|
|
|
title: 'DER证书保存路径',
|
2024-09-20 11:11:25 +08:00
|
|
|
|
helper: '用于Apache证书部署,需要有写入权限,路径要包含私钥文件名,文件名不能用*?!等特殊符号,例如:/tmp/cert.der',
|
2024-09-06 00:13:21 +08:00
|
|
|
|
component: {
|
|
|
|
|
|
placeholder: '/root/deploy/nginx/cert.der',
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
derPath!: string;
|
|
|
|
|
|
|
2023-01-11 20:39:48 +08:00
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '域名证书',
|
|
|
|
|
|
helper: '请选择前置任务输出的域名证书',
|
2023-01-11 20:39:48 +08:00
|
|
|
|
component: {
|
2024-05-27 18:38:41 +08:00
|
|
|
|
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;
|
2024-07-08 11:19:02 +08:00
|
|
|
|
|
2023-01-11 20:39:48 +08:00
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '主机登录配置',
|
|
|
|
|
|
helper: 'access授权',
|
2023-01-11 20:39:48 +08:00
|
|
|
|
component: {
|
2024-05-27 18:38:41 +08:00
|
|
|
|
name: 'pi-access-selector',
|
|
|
|
|
|
type: 'ssh',
|
2022-11-07 23:31:20 +08:00
|
|
|
|
},
|
2024-09-26 10:24:25 +08:00
|
|
|
|
required: true,
|
2023-01-11 20:39:48 +08:00
|
|
|
|
})
|
|
|
|
|
|
accessId!: string;
|
|
|
|
|
|
|
2024-07-08 11:19:02 +08:00
|
|
|
|
@TaskInput({
|
|
|
|
|
|
title: '自动创建远程目录',
|
|
|
|
|
|
helper: '是否自动创建远程目录,如果关闭则你需要自己确保远程目录存在',
|
2024-08-06 11:23:23 +08:00
|
|
|
|
value: true,
|
2024-07-08 11:19:02 +08:00
|
|
|
|
component: {
|
|
|
|
|
|
name: 'a-switch',
|
|
|
|
|
|
vModel: 'checked',
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
mkdirs = true;
|
|
|
|
|
|
|
2024-09-22 23:19:10 +08:00
|
|
|
|
@TaskInput({
|
|
|
|
|
|
title: 'shell脚本命令',
|
|
|
|
|
|
component: {
|
|
|
|
|
|
name: 'a-textarea',
|
|
|
|
|
|
vModel: 'value',
|
|
|
|
|
|
rows: 6,
|
|
|
|
|
|
},
|
|
|
|
|
|
helper: '上传后执行脚本命令,不填则不执行\n注意:如果目标主机是windows,且终端是cmd,系统会自动将多行命令通过“&&”连接成一行',
|
|
|
|
|
|
required: false,
|
|
|
|
|
|
})
|
|
|
|
|
|
script!: string;
|
|
|
|
|
|
|
2023-01-11 20:39:48 +08:00
|
|
|
|
@TaskOutput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '证书保存路径',
|
2023-01-11 20:39:48 +08:00
|
|
|
|
})
|
|
|
|
|
|
hostCrtPath!: string;
|
|
|
|
|
|
|
|
|
|
|
|
@TaskOutput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '私钥保存路径',
|
2023-01-11 20:39:48 +08:00
|
|
|
|
})
|
|
|
|
|
|
hostKeyPath!: string;
|
|
|
|
|
|
|
2024-09-22 23:19:10 +08:00
|
|
|
|
@TaskOutput({
|
|
|
|
|
|
title: '中间证书保存路径',
|
|
|
|
|
|
})
|
|
|
|
|
|
hostIcPath!: 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() {}
|
2024-05-30 10:54:18 +08:00
|
|
|
|
|
|
|
|
|
|
copyFile(srcFile: string, destFile: string) {
|
2024-09-06 00:13:21 +08:00
|
|
|
|
if (!srcFile || !destFile) {
|
|
|
|
|
|
this.logger.warn(`srcFile:${srcFile} 或 destFile:${destFile} 为空,不复制`);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-05-30 10:54:18 +08:00
|
|
|
|
const dir = destFile.substring(0, destFile.lastIndexOf('/'));
|
|
|
|
|
|
if (!fs.existsSync(dir)) {
|
|
|
|
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
|
|
|
|
}
|
|
|
|
|
|
fs.copyFileSync(srcFile, destFile);
|
2024-09-22 23:19:10 +08:00
|
|
|
|
this.logger.info(`复制文件:${srcFile} => ${destFile}`);
|
2024-05-30 10:54:18 +08:00
|
|
|
|
}
|
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;
|
2023-05-23 18:01:20 +08:00
|
|
|
|
const certReader = new CertReader(cert);
|
2024-09-05 15:36:35 +08:00
|
|
|
|
|
|
|
|
|
|
const handle = async (opts: CertReaderHandleContext) => {
|
2024-09-22 23:19:10 +08:00
|
|
|
|
const { tmpCrtPath, tmpKeyPath, tmpDerPath, tmpPfxPath, tmpIcPath } = opts;
|
2024-09-26 15:15:17 +08:00
|
|
|
|
// if (this.copyToThisHost) {
|
|
|
|
|
|
// this.logger.info('复制到目标路径');
|
|
|
|
|
|
// this.copyFile(tmpCrtPath, crtPath);
|
|
|
|
|
|
// this.copyFile(tmpKeyPath, keyPath);
|
|
|
|
|
|
// this.copyFile(tmpIcPath, this.icPath);
|
|
|
|
|
|
// this.copyFile(tmpPfxPath, this.pfxPath);
|
|
|
|
|
|
// this.copyFile(tmpDerPath, this.derPath);
|
|
|
|
|
|
// this.logger.warn('复制到当前主机功能已迁移到 “复制到本机”插件,请尽快换成复制到本机插件');
|
|
|
|
|
|
// return;
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
if (accessId == null) {
|
|
|
|
|
|
this.logger.error('复制到当前主机功能已迁移到 “复制到本机”插件,请换成复制到本机插件');
|
2024-09-26 13:20:10 +08:00
|
|
|
|
return;
|
2024-09-26 10:24:25 +08:00
|
|
|
|
}
|
2024-09-26 13:20:10 +08:00
|
|
|
|
const connectConf: SshAccess = await this.accessService.getById(accessId);
|
|
|
|
|
|
const sshClient = new SshClient(this.logger);
|
|
|
|
|
|
|
2024-09-26 10:24:25 +08:00
|
|
|
|
if (!accessId) {
|
|
|
|
|
|
throw new Error('主机登录授权配置不能为空');
|
|
|
|
|
|
}
|
|
|
|
|
|
this.logger.info('准备上传文件到服务器');
|
|
|
|
|
|
|
|
|
|
|
|
const transports: any = [];
|
|
|
|
|
|
if (crtPath) {
|
|
|
|
|
|
transports.push({
|
|
|
|
|
|
localPath: tmpCrtPath,
|
|
|
|
|
|
remotePath: crtPath,
|
2024-07-08 11:10:08 +08:00
|
|
|
|
});
|
2024-09-26 10:24:25 +08:00
|
|
|
|
this.logger.info(`上传证书到主机:${crtPath}`);
|
2024-05-30 10:54:18 +08:00
|
|
|
|
}
|
2024-09-26 10:24:25 +08:00
|
|
|
|
if (keyPath) {
|
|
|
|
|
|
transports.push({
|
|
|
|
|
|
localPath: tmpKeyPath,
|
|
|
|
|
|
remotePath: keyPath,
|
|
|
|
|
|
});
|
|
|
|
|
|
this.logger.info(`上传私钥到主机:${keyPath}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (this.icPath) {
|
|
|
|
|
|
transports.push({
|
|
|
|
|
|
localPath: tmpIcPath,
|
|
|
|
|
|
remotePath: this.icPath,
|
|
|
|
|
|
});
|
|
|
|
|
|
this.logger.info(`上传中间证书到主机:${this.icPath}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (this.pfxPath) {
|
|
|
|
|
|
transports.push({
|
|
|
|
|
|
localPath: tmpPfxPath,
|
|
|
|
|
|
remotePath: this.pfxPath,
|
|
|
|
|
|
});
|
|
|
|
|
|
this.logger.info(`上传PFX证书到主机:${this.pfxPath}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (this.derPath) {
|
|
|
|
|
|
transports.push({
|
|
|
|
|
|
localPath: tmpDerPath,
|
|
|
|
|
|
remotePath: this.derPath,
|
|
|
|
|
|
});
|
|
|
|
|
|
this.logger.info(`上传DER证书到主机:${this.derPath}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
this.logger.info('开始上传文件到服务器');
|
|
|
|
|
|
await sshClient.uploadFiles({
|
|
|
|
|
|
connectConf,
|
|
|
|
|
|
transports,
|
|
|
|
|
|
mkdirs: this.mkdirs,
|
|
|
|
|
|
});
|
|
|
|
|
|
this.logger.info('上传文件到服务器成功');
|
|
|
|
|
|
//输出
|
|
|
|
|
|
this.hostCrtPath = crtPath;
|
|
|
|
|
|
this.hostKeyPath = keyPath;
|
|
|
|
|
|
this.hostIcPath = this.icPath;
|
|
|
|
|
|
this.hostPfxPath = this.pfxPath;
|
|
|
|
|
|
this.hostDerPath = this.derPath;
|
2024-09-05 15:36:35 +08:00
|
|
|
|
};
|
2024-09-26 10:24:25 +08:00
|
|
|
|
|
2024-09-05 15:36:35 +08:00
|
|
|
|
await certReader.readCertFile({
|
|
|
|
|
|
logger: this.logger,
|
|
|
|
|
|
handle,
|
|
|
|
|
|
});
|
2024-09-22 23:19:10 +08:00
|
|
|
|
|
2024-09-25 03:22:14 +08:00
|
|
|
|
if (this.script && this.script?.trim()) {
|
2024-09-26 13:20:10 +08:00
|
|
|
|
const connectConf: SshAccess = await this.accessService.getById(accessId);
|
|
|
|
|
|
const sshClient = new SshClient(this.logger);
|
2024-09-22 23:19:10 +08:00
|
|
|
|
this.logger.info('执行脚本命令');
|
|
|
|
|
|
const scripts = this.script.split('\n');
|
|
|
|
|
|
await sshClient.exec({
|
|
|
|
|
|
connectConf,
|
|
|
|
|
|
script: scripts,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-11-07 23:31:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-05-09 09:56:31 +08:00
|
|
|
|
|
|
|
|
|
|
new UploadCertToHostPlugin();
|