feat: 支持中间证书

This commit is contained in:
xiaojunnuo
2024-09-22 23:19:10 +08:00
parent bdc0227c08
commit e86756e4c6
6 changed files with 102 additions and 20 deletions

View File

@@ -9,7 +9,7 @@ import { SshAccess } from '../../access/index.js';
title: '上传证书到主机',
icon: 'line-md:uploading-loop',
group: pluginGroups.host.key,
desc: '支持复制证书到本机',
desc: '支持上传完成后执行脚本命令',
default: {
strategy: {
runStrategy: RunStrategy.SkipWhenSucceed,
@@ -34,6 +34,15 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
})
keyPath!: string;
@TaskInput({
title: '中间证书保存路径',
helper: '需要有写入权限,路径要包含私钥文件名,文件名不能用*?!等特殊符号,例如:/tmp/intermediate.crt',
component: {
placeholder: '/root/deploy/nginx/intermediate.crt',
},
})
icPath!: string;
@TaskInput({
title: 'PFX证书保存路径',
helper: '用于IIS证书部署需要有写入权限路径要包含私钥文件名文件名不能用*?!等特殊符号,例如:/tmp/cert.pfx',
@@ -85,10 +94,24 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
})
mkdirs = true;
@TaskInput({
title: 'shell脚本命令',
component: {
name: 'a-textarea',
vModel: 'value',
rows: 6,
},
helper: '上传后执行脚本命令,不填则不执行\n注意如果目标主机是windows且终端是cmd系统会自动将多行命令通过“&&”连接成一行',
required: false,
})
script!: string;
@TaskInput({
title: '仅复制到当前主机',
helper:
'开启后将直接复制到当前主机某个目录不上传到主机由于是docker启动实际上是复制到docker容器内的“证书保存路径”你需要事先在docker-compose.yaml中配置主机目录映射 volumes: /your_target_path:/your_target_path',
'注意:本配置即将废弃\n' +
'开启后将直接复制到当前主机某个目录不上传到主机由于是docker启动实际上是复制到docker容器内的“证书保存路径”\n' +
'你需要事先在docker-compose.yaml中配置主机目录映射 volumes: /your_target_path:/your_target_path',
value: false,
component: {
name: 'a-switch',
@@ -107,6 +130,10 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
})
hostKeyPath!: string;
@TaskOutput({
title: '中间证书保存路径',
})
hostIcPath!: string;
@TaskOutput({
title: 'PFX保存路径',
})
@@ -129,63 +156,76 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
fs.mkdirSync(dir, { recursive: true });
}
fs.copyFileSync(srcFile, destFile);
this.logger.info(`复制文件:${srcFile} => ${destFile}`);
}
async execute(): Promise<void> {
const { crtPath, keyPath, cert, accessId } = this;
const certReader = new CertReader(cert);
const connectConf: SshAccess = await this.accessService.getById(accessId);
const sshClient = new SshClient(this.logger);
const handle = async (opts: CertReaderHandleContext) => {
const { tmpCrtPath, tmpKeyPath, tmpDerPath, tmpPfxPath } = opts;
const { tmpCrtPath, tmpKeyPath, tmpDerPath, tmpPfxPath, tmpIcPath } = opts;
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.info(`证书复制成功crtPath=${crtPath},keyPath=${keyPath},pfxPath=${this.pfxPath},derPath=${this.derPath}`);
} else {
if (!accessId) {
throw new Error('主机登录授权配置不能为空');
}
this.logger.info('准备上传文件到服务器');
const connectConf: SshAccess = await this.accessService.getById(accessId);
const sshClient = new SshClient(this.logger);
const transports: any = [];
if (crtPath) {
transports.push({
localPath: tmpCrtPath,
remotePath: crtPath,
});
this.logger.info(`上传证书到主机:${crtPath}`);
}
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(`证书上传成功crtPath=${crtPath},keyPath=${keyPath},pfxPath=${this.pfxPath},derPath=${this.derPath}`);
this.logger.info('上传文件到服务器成功');
//输出
this.hostCrtPath = crtPath;
this.hostKeyPath = keyPath;
this.hostIcPath = this.icPath;
this.hostPfxPath = this.pfxPath;
this.hostDerPath = this.derPath;
}
@@ -194,6 +234,15 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
logger: this.logger,
handle,
});
if (this.script.trim()) {
this.logger.info('执行脚本命令');
const scripts = this.script.split('\n');
await sshClient.exec({
connectConf,
script: scripts,
});
}
}
}