-
Notifications
You must be signed in to change notification settings - Fork 85
/
SshSession.ts
228 lines (207 loc) · 7.36 KB
/
SshSession.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import {
ICommandArguments,
ICommandOptionDefinition,
IProfile,
Logger
} from "@zowe/imperative";
import { ISshSession } from "./doc/ISshSession";
/**
* Utility Methods for Brightside
* @export
*/
export class SshSession {
/**
* Default ssh port 22
* @static
* @memberof AbstractSession
*/
public static readonly DEFAULT_SSH_PORT = 22;
/**
* Obtain session info and defaults
* @readonly
* @type {ISession}
* @memberof AbstractSession
*/
get ISshSession(): ISshSession {
return this.mISshSession;
}
public static SSH_CONNECTION_OPTION_GROUP = "z/OS Ssh Connection Options";
/**
* Option used in profile creation and commands for hostname for z/OS SSH
*/
public static SSH_OPTION_HOST: ICommandOptionDefinition = {
name: "host",
aliases: ["H"],
description: "The z/OS SSH server host name.",
type: "string",
required: false,
group: SshSession.SSH_CONNECTION_OPTION_GROUP
};
public static SSH_OPTION_HOST_PROFILE: ICommandOptionDefinition = {
...SshSession.SSH_OPTION_HOST,
required: false
};
/**
* Option used in profile creation and commands for port for z/OS SSH
*/
public static SSH_OPTION_PORT: ICommandOptionDefinition = {
name: "port",
aliases: ["P"],
description: "The z/OS SSH server port.",
type: "number",
defaultValue: 22,
group: SshSession.SSH_CONNECTION_OPTION_GROUP
};
/**
* Option used in profile creation and commands for username / ID for z/OS SSH
*/
public static SSH_OPTION_USER: ICommandOptionDefinition = {
name: "user",
aliases: ["u"],
description: "Mainframe user name, which can be the same as your TSO login.",
type: "string",
required: false,
group: SshSession.SSH_CONNECTION_OPTION_GROUP
};
public static SSH_OPTION_USER_PROFILE: ICommandOptionDefinition = {
...SshSession.SSH_OPTION_USER,
required: false
};
/**
* Option used in profile creation and commands for password/passphrase for z/OS SSH
*/
public static SSH_OPTION_PASSWORD: ICommandOptionDefinition = {
name: "password",
aliases: ["pass", "pw"],
description: "Mainframe password, which can be the same as your TSO password.",
type: "string",
group: SshSession.SSH_CONNECTION_OPTION_GROUP
};
/**
* Option used in profile creation and commands for private key path
*/
public static SSH_OPTION_PRIVATEKEY: ICommandOptionDefinition = {
name: "privateKey",
aliases: ["key", "pk"],
description: "Path to a file containing your private key, that must match a public key stored in the server for authentication",
type: "string",
group: SshSession.SSH_CONNECTION_OPTION_GROUP
};
/**
* Option used in profile creation and commands for passphrase for private key
*/
public static SSH_OPTION_KEYPASSPHRASE: ICommandOptionDefinition = {
name: "keyPassphrase",
aliases: ["passphrase", "kp"],
description: "Private key passphrase, which unlocks the private key.",
type: "string",
group: SshSession.SSH_CONNECTION_OPTION_GROUP
};
/**
* Option used in profile creation and commands for passphrase for private key
*/
public static SSH_OPTION_HANDSHAKETIMEOUT: ICommandOptionDefinition = {
name: "handshakeTimeout",
aliases: ["timeout", "to"],
description: "How long in milliseconds to wait for the SSH handshake to complete.",
type: "number",
group: SshSession.SSH_CONNECTION_OPTION_GROUP
};
/**
* Options related to connecting to z/OS SSH
* These options can be filled in if the user creates a profile
*/
public static SSH_CONNECTION_OPTIONS: ICommandOptionDefinition[] = [
SshSession.SSH_OPTION_HOST,
SshSession.SSH_OPTION_PORT,
SshSession.SSH_OPTION_USER,
SshSession.SSH_OPTION_PASSWORD,
SshSession.SSH_OPTION_PRIVATEKEY,
SshSession.SSH_OPTION_KEYPASSPHRASE,
SshSession.SSH_OPTION_HANDSHAKETIMEOUT
];
/**
* Given command line arguments, create an SSH session configuration object.
* @param {IProfile} args - The arguments specified by the user
* @returns {ISshSession} - A session configuration to be used for an SSH session.
*/
public static createSshSessCfgFromArgs(args: ICommandArguments): ISshSession {
return {
privateKey: args.privateKey,
keyPassphrase: args.keyPassphrase,
handshakeTimeout: args.handshakeTimeout
};
}
/**
* Given a z/OS SSH profile, create a SSH Client Session.
* @static
* @deprecated Use SshSession.createSshSessCfgFromArgs & others
* @param {IProfile} profile - The SSH profile contents
* @returns {Session} - A session for usage in the SSH Client
*/
public static createBasicSshSession(profile: IProfile): SshSession {
this.log.debug("Creating a z/OS SSH session from the profile named %s", profile.name);
return new SshSession({
hostname: profile.host,
port: profile.port,
user: profile.user,
password: profile.password,
privateKey: profile.privateKey,
keyPassphrase: profile.keyPassphrase,
handshakeTimeout: profile.handshakeTimeout
});
}
/**
* Given command line arguments, create a SSH Client Session.
* @static
* @deprecated Use SshSession.createSshSessCfgFromArgs & others
* @param {IProfile} args - The arguments specified by the user
* @returns {SshSession} - A session for usage in the SSH Client
*/
public static createBasicSshSessionFromArguments(args: ICommandArguments): SshSession {
this.log.debug("Creating a z/OS SSH session from arguments");
return new SshSession({
hostname: args.host,
port: args.port,
user: args.user,
password: args.password,
privateKey: args.privateKey,
keyPassphrase: args.keyPassphrase,
handshakeTimeout: args.handshakeTimeout
});
}
/**
* Creates an instance of AbstractSession.
* @param {ISshSession} session: SshSession parameter object
*/
constructor(private mISshSession: ISshSession) {
mISshSession = this.buildSession(mISshSession);
}
private static get log(): Logger {
return Logger.getAppLogger();
}
/**
* Builds an ISshSession so all required pieces are filled in
* @private
* @param {ISession} session - the fully populated session
* @memberof AbstractSession
*/
private buildSession(session: ISshSession): ISshSession {
const populatedSession = session;
// set port if not set
if (populatedSession.port === undefined || populatedSession.port === null) {
populatedSession.port = SshSession.DEFAULT_SSH_PORT;
}
return populatedSession;
}
}