Skip to content

Commit

Permalink
perf: ssh登录支持openssh格式私钥、支持私钥密码
Browse files Browse the repository at this point in the history
  • Loading branch information
greper committed Jun 18, 2024
1 parent fd54c2f commit 5c2c508
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { CertInfo, CertReader } from '@certd/plugin-cert';
@IsTaskPlugin({
name: 'CloudflareDeployToCDN',
title: '部署证书到CF CDN',
desc: '暂未实现,不可用',
default: {
strategy: {
runStrategy: RunStrategy.SkipWhenSucceed,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { AccessInput, IAccess, IsAccess } from '@certd/pipeline';
import { ConnectConfig } from 'ssh2';

@IsAccess({
name: 'ssh',
title: '主机登录授权',
desc: '',
input: {},
})
export class SshAccess implements IAccess {
export class SshAccess implements IAccess, ConnectConfig {
@AccessInput({
title: '主机地址',
component: {
Expand All @@ -19,11 +20,12 @@ export class SshAccess implements IAccess {
title: '端口',
value: '22',
component: {
name: 'a-input-number',
placeholder: '22',
},
rules: [{ required: true, message: '此项必填' }],
})
port!: string;
port!: number;
@AccessInput({
title: '用户名',
value: 'root',
Expand All @@ -40,14 +42,24 @@ export class SshAccess implements IAccess {
})
password!: string;
@AccessInput({
title: '密钥',
helper: '密钥或密码必填一项',
title: '私钥登录',
helper: '私钥或密码必填一项',
component: {
name: 'a-textarea',
vModel: 'value',
},
})
privateKey!: string;

@AccessInput({
title: '私钥密码',
helper: '如果你的私钥有密码的话',
component: {
name: 'a-input-password',
vModel: 'value',
},
})
passphrase!: string;
}

new SshAccess();
13 changes: 8 additions & 5 deletions packages/ui/certd-server/src/plugins/plugin-host/lib/ssh.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-ignore
import ssh2 from 'ssh2';
import ssh2, { ConnectConfig } from 'ssh2';
import path from 'path';
import _ from 'lodash';
import { ILogger } from '@certd/pipeline';
Expand All @@ -19,7 +19,7 @@ export class SshClient {
}
* @param options
*/
uploadFiles(options: { connectConf: any; transports: any }) {
uploadFiles(options: { connectConf: ConnectConfig; transports: any }) {
const { connectConf, transports } = options;
const conn = new ssh2.Client();

Expand Down Expand Up @@ -53,7 +53,10 @@ export class SshClient {
});
}

exec(options: { connectConf: any; script: string | Array<string> }) {
exec(options: {
connectConf: ConnectConfig;
script: string | Array<string>;
}) {
let { script } = options;
const { connectConf } = options;
if (_.isArray(script)) {
Expand Down Expand Up @@ -99,7 +102,7 @@ export class SshClient {
});
}

shell(options: { connectConf: any; script: string }) {
shell(options: { connectConf: ConnectConfig; script: string }) {
const { connectConf, script } = options;
return new Promise((resolve, reject) => {
this.connect({
Expand Down Expand Up @@ -132,7 +135,7 @@ export class SshClient {
});
}

connect(options: { connectConf: any; onReady: any; onError: any }) {
connect(options: { connectConf: ConnectConfig; onReady: any; onError: any }) {
const { connectConf, onReady, onError } = options;
const conn = new ssh2.Client();
conn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
crtPath!: string;
@TaskInput({
title: '私钥保存路径',
helper: '需要有写入权限,路径要包含证书文件名',
helper: '需要有写入权限,路径要包含私钥文件名',
component: {
placeholder: '/root/deploy/nginx/cert.crt',
placeholder: '/root/deploy/nginx/cert.key',
},
})
keyPath!: string;
Expand Down

0 comments on commit 5c2c508

Please sign in to comment.