-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSSHHandler.cs
More file actions
526 lines (440 loc) · 21.3 KB
/
Copy pathSSHHandler.cs
File metadata and controls
526 lines (440 loc) · 21.3 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Copyright 2021 Keyfactor
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Renci.SshNet;
using Microsoft.Extensions.Logging;
using Keyfactor.Logging;
using Keyfactor.PKI.PrivateKeys;
using Keyfactor.PKI.PEM;
using Renci.SshNet.Common;
namespace Keyfactor.Extensions.Orchestrator.RemoteFile.RemoteHandlers
{
class SSHHandler : BaseRemoteHandler
{
private readonly string[] IgnoreErrors = { "Could not chdir to home directory" };
private ConnectionInfo Connection { get; set; }
private string SudoImpersonatedUser { get; set; }
private bool IsStoreServerLinux { get; set; }
private bool UseShellCommands { get; set; }
private string UserId { get; set; }
private string Password { get; set; }
private SshClient sshClient;
internal SSHHandler(string server, string serverLogin, string serverPassword, bool isStoreServerLinux, int sshPort, string sudoImpersonatedUser, bool useShellCommands)
{
_logger.MethodEntry(LogLevel.Debug);
Server = server;
SudoImpersonatedUser = sudoImpersonatedUser;
IsStoreServerLinux = isStoreServerLinux;
UseShellCommands = useShellCommands;
UserId = serverLogin;
Password = serverPassword;
if (serverPassword.Length < PASSWORD_LENGTH_MAX)
{
KeyboardInteractiveAuthenticationMethod keyboardAuthentication = new KeyboardInteractiveAuthenticationMethod(UserId);
keyboardAuthentication.AuthenticationPrompt += KeyboardAuthentication_AuthenticationPrompt;
Connection = new ConnectionInfo(server, sshPort, serverLogin, new PasswordAuthenticationMethod(serverLogin, serverPassword), keyboardAuthentication);
}
else
{
PrivateKeyFile privateKeyFile;
string privateKey = string.Empty;
string[] sshSecret = serverPassword.Split(new[] { "|||" }, 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
try
{
privateKey = FormatPrivateKey(sshSecret[0]);
}
catch (Exception)
{
privateKey = ConvertToPKCS1(sshSecret[0]);
}
using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(privateKey)))
{
using (MemoryStream ms2 = new MemoryStream(Encoding.ASCII.GetBytes(sshSecret.Length == 1 ? string.Empty : sshSecret[1])))
{
privateKeyFile = new PrivateKeyFile(ms, null, ms2.Length == 0 ? null : ms2);
}
}
Connection = new ConnectionInfo(server, sshPort, serverLogin, new PrivateKeyAuthenticationMethod(serverLogin, privateKeyFile));
}
try
{
sshClient = new SshClient(Connection);
sshClient.Connect();
//method call below necessary to check edge condition where password for user id has expired. SCP (and possibly SFTP) download hangs in that scenario
if (useShellCommands)
CheckConnection();
}
catch (Exception ex)
{
throw new RemoteFileException($"Error making a SSH connection to remote server {Connection.Host}, for user {Connection.Username}. Please contact your company's system administrator to verify connection and permission settings.", ex);
}
_logger.MethodExit(LogLevel.Debug);
}
public override void Terminate()
{
_logger.MethodEntry(LogLevel.Debug);
sshClient.Disconnect();
sshClient.Dispose();
_logger.MethodExit(LogLevel.Debug);
}
public override string RunCommand(string commandText, object[] arguments, bool withSudo, string[] passwordsToMaskInLog)
{
_logger.MethodEntry(LogLevel.Debug);
string sudo = $"sudo -S ";
string echo = $"echo -e '\n' | ";
try
{
if (withSudo && IsStoreServerLinux)
{
if (string.IsNullOrEmpty(SudoImpersonatedUser))
commandText = sudo + commandText;
else
commandText = sudo + $"-u {SudoImpersonatedUser}" + " " + commandText;
}
if (IsStoreServerLinux)
{
commandText = echo + commandText;
}
else
{
commandText = "powershell -Command \"" + commandText + "\"";
commandText = commandText.Replace(@"\", @"\\");
}
string displayCommand = commandText;
if (passwordsToMaskInLog != null)
{
foreach (string password in passwordsToMaskInLog)
displayCommand = displayCommand.Replace(password, PASSWORD_MASK_VALUE);
}
using (SshCommand command = sshClient.CreateCommand($"{commandText}"))
{
_logger.LogDebug($"RunCommand: {displayCommand}");
command.Execute();
_logger.LogDebug($"SSH Results: {displayCommand}::: {command.Result}::: {command.Error}");
if (!String.IsNullOrEmpty(command.Error)/* && !IgnoreError(command.Error)*/)
throw new ApplicationException(command.Error);
_logger.MethodExit(LogLevel.Debug);
return command.Result;
}
}
catch (Exception ex)
{
_logger.LogError($"Exception during RunCommand...{RemoteFileException.FlattenExceptionMessages(ex, ex.Message)}");
throw;
}
}
public override void UploadCertificateFile(string path, string fileName, byte[] certBytes)
{
_logger.MethodEntry(LogLevel.Debug);
_logger.LogDebug($"UploadCertificateFile: {path}{fileName}");
string uploadPath = path+fileName;
if (!string.IsNullOrEmpty(ApplicationSettings.SeparateUploadFilePath) && IsStoreServerLinux)
{
uploadPath = ApplicationSettings.SeparateUploadFilePath + fileName;
_logger.LogDebug($"uploadPath: {uploadPath}");
}
bool scpError = false;
using (ScpClient client = new ScpClient(Connection))
{
try
{
_logger.LogDebug($"SCP connection attempt to {Connection.Host} using login {Connection.Username} and connection method {Connection.AuthenticationMethods[0].Name}");
client.OperationTimeout = System.TimeSpan.FromSeconds(60);
client.Connect();
using (MemoryStream stream = new MemoryStream(certBytes))
{
client.Upload(stream, FormatFTPPath(uploadPath, false));
}
}
catch (Exception ex)
{
scpError = true;
_logger.LogDebug($"SCP upload failed. Attempting with SFTP protocol...");
}
finally
{
client.Disconnect();
}
}
if (scpError)
{
using (SftpClient client = new SftpClient(Connection))
{
try
{
_logger.LogDebug($"SFTP connection attempt to {Connection.Host} using login {Connection.Username} and connection method {Connection.AuthenticationMethods[0].Name}");
client.OperationTimeout = System.TimeSpan.FromSeconds(60);
client.Connect();
using (MemoryStream stream = new MemoryStream(certBytes))
{
client.UploadFile(stream, FormatFTPPath(uploadPath, !IsStoreServerLinux));
}
}
catch (Exception ex)
{
_logger.LogError($"Upload Exception: {RemoteFileException.FlattenExceptionMessages(ex, "Exception during SFTP download...")}");
throw new RemoteFileException($"Error attempting SFTP file transfer to {Connection.Host} using login {Connection.Username} and connection method {Connection.AuthenticationMethods[0].Name}. Please contact your company's system administrator to verify connection and permission settings.", ex);
}
finally
{
client.Disconnect();
}
}
}
if (!string.IsNullOrEmpty(ApplicationSettings.SeparateUploadFilePath) && IsStoreServerLinux)
{
RunCommand($"tee {path}/{fileName} < {uploadPath} > /dev/null", null, ApplicationSettings.UseSudo, null);
RunCommand($"rm {uploadPath}", null, ApplicationSettings.UseSudo, null);
}
_logger.MethodExit(LogLevel.Debug);
}
public override byte[] DownloadCertificateFile(string path)
{
_logger.MethodEntry(LogLevel.Debug);
_logger.LogDebug($"DownloadCertificateFile: {path}");
byte[] rtnStore = new byte[] { };
string downloadPath = path;
string altPathOnly = string.Empty;
string altFileNameOnly = string.Empty;
if (!string.IsNullOrEmpty(ApplicationSettings.SeparateUploadFilePath) && IsStoreServerLinux)
{
_logger.LogDebug("Splitting store path");
SplitStorePathFile(path, out altPathOnly, out altFileNameOnly);
downloadPath = ApplicationSettings.SeparateUploadFilePath + altFileNameOnly;
RunCommand($"cp {path} {downloadPath}", null, ApplicationSettings.UseSudo, null);
if (string.IsNullOrEmpty(SudoImpersonatedUser))
RunCommand($"chown {Connection.Username} {downloadPath}", null, ApplicationSettings.UseSudo, null);
}
bool scpError = false;
_logger.LogDebug($"Download path: {downloadPath}");
_logger.LogDebug($"IsStoreServerLinux: {IsStoreServerLinux}");
_logger.LogDebug($"Attempting SCP download...");
using (ScpClient client = new ScpClient(Connection))
{
try
{
_logger.LogDebug($"SCP connection attempt from {Connection.Host} using login {Connection.Username} and connection method {Connection.AuthenticationMethods[0].Name}");
client.OperationTimeout = System.TimeSpan.FromSeconds(60);
client.Connect();
using (MemoryStream stream = new MemoryStream())
{
client.Download(FormatFTPPath(downloadPath, false), stream);
rtnStore = stream.ToArray();
}
}
catch (Exception ex)
{
scpError = true;
_logger.LogError($"Download Exception: {RemoteFileException.FlattenExceptionMessages(ex, "Exception during SCP download...")}");
_logger.LogDebug($"SCP download failed. Attempting with SFTP protocol...");
}
finally
{
client.Disconnect();
}
}
if (scpError)
{
_logger.LogDebug($"Attempting SFTP download...");
using (SftpClient client = new SftpClient(Connection))
{
try
{
_logger.LogDebug($"SFTP connection attempt from {Connection.Host} using login {Connection.Username} and connection method {Connection.AuthenticationMethods[0].Name}");
client.OperationTimeout = System.TimeSpan.FromSeconds(60);
client.Connect();
using (MemoryStream stream = new MemoryStream())
{
client.DownloadFile(FormatFTPPath(downloadPath, !IsStoreServerLinux), stream);
rtnStore = stream.ToArray();
}
}
catch (Exception ex)
{
_logger.LogError($"Download Exception: {RemoteFileException.FlattenExceptionMessages(ex, "Exception during SFTP download...")}");
throw new RemoteFileException($"Error attempting SFTP file transfer from {Connection.Host} using login {Connection.Username} and connection method {Connection.AuthenticationMethods[0].Name}. Please contact your company's system administrator to verify connection and permission settings.", ex);
}
finally
{
client.Disconnect();
}
}
}
if (!string.IsNullOrEmpty(ApplicationSettings.SeparateUploadFilePath) && IsStoreServerLinux)
{
RunCommand($"rm {downloadPath}", null, ApplicationSettings.UseSudo, null);
}
_logger.MethodExit(LogLevel.Debug);
return rtnStore;
}
public override void CreateEmptyStoreFile(string path, string linuxFilePermissions, string linuxFileOwner)
{
_logger.MethodEntry(LogLevel.Debug);
string[] linuxGroupOwner = linuxFileOwner.Split(":");
string linuxFileGroup = String.Empty;
if (linuxGroupOwner.Length == 2)
{
linuxFileOwner = linuxGroupOwner[0];
linuxFileGroup = $"-g {linuxGroupOwner[1]}";
}
if (IsStoreServerLinux)
{
string pathOnly = string.Empty;
string fileName = string.Empty;
SplitStorePathFile(path, out pathOnly, out fileName);
if (UseShellCommands)
{
linuxFilePermissions = string.IsNullOrEmpty(linuxFilePermissions) ? GetFolderPermissions(pathOnly) : linuxFilePermissions;
linuxFileOwner = string.IsNullOrEmpty(linuxFileOwner) ? GetFolderOwner(pathOnly) : linuxFileOwner;
AreLinuxPermissionsValid(linuxFilePermissions);
RunCommand($"install -m {linuxFilePermissions} -o {linuxFileOwner} {linuxFileGroup} /dev/null {path}", null, ApplicationSettings.UseSudo, null);
}
else
UploadCertificateFile(pathOnly, fileName, Array.Empty<byte>());
}
else
RunCommand($@"Out-File -FilePath ""{path}""", null, false, null);
_logger.MethodExit(LogLevel.Debug);
}
public override bool DoesFileExist(string path)
{
_logger.MethodEntry(LogLevel.Debug);
_logger.LogDebug($"DoesFileExist: {path}");
bool exists = false;
if (UseShellCommands)
{
exists = Convert.ToBoolean(RunCommand($"ls {path} >> /dev/null 2>&1 && echo True || echo False", null, ApplicationSettings.UseSudo, null));
}
else
{
using (SftpClient client = new SftpClient(Connection))
{
try
{
client.Connect();
string existsPath = FormatFTPPath(path, !IsStoreServerLinux);
exists = client.Exists(existsPath);
_logger.LogDebug(existsPath);
}
catch (Exception ex)
{
_logger.LogError(RemoteFileException.FlattenExceptionMessages(ex, "Error checking existence of file {path} using SFTP"));
throw;
}
finally
{
_logger.MethodExit(LogLevel.Debug);
client.Disconnect();
}
}
}
return exists;
}
public override void RemoveCertificateFile(string path, string fileName)
{
_logger.MethodEntry(LogLevel.Debug);
_logger.LogDebug($"RemoveCertificateFile: {path} {fileName}");
RunCommand($"rm {path}{fileName}", null, ApplicationSettings.UseSudo, null);
_logger.MethodExit(LogLevel.Debug);
}
private string GetFolderPermissions(string path)
{
_logger.MethodEntry(LogLevel.Debug);
try
{
return RunCommand($"stat -c '%a' {path}", null, ApplicationSettings.UseSudo, null).Replace($"\n",string.Empty);
}
finally
{
_logger.MethodExit(LogLevel.Debug);
}
}
private string GetFolderOwner(string path)
{
_logger.MethodEntry(LogLevel.Debug);
try
{
return RunCommand($"stat -c '%U' {path}", null, ApplicationSettings.UseSudo, null).Replace($"\n", string.Empty);
}
finally
{
_logger.MethodExit(LogLevel.Debug);
}
}
private void KeyboardAuthentication_AuthenticationPrompt(object sender, AuthenticationPromptEventArgs e)
{
_logger.MethodEntry(LogLevel.Debug);
foreach (AuthenticationPrompt prompt in e.Prompts)
{
if (prompt.Request.StartsWith("Password"))
prompt.Response = Password;
}
_logger.MethodExit(LogLevel.Debug);
}
private void SplitStorePathFile(string pathFileName, out string path, out string fileName)
{
_logger.MethodEntry(LogLevel.Debug);
try
{
int separatorIndex = pathFileName.LastIndexOf(pathFileName.Substring(0, 1) == "/" ? @"/" : @"\");
fileName = pathFileName.Substring(separatorIndex + 1);
path = pathFileName.Substring(0, separatorIndex + 1);
}
catch (Exception ex)
{
throw new RemoteFileException($"Error attempting to parse certficate store/key path={pathFileName}.", ex);
}
_logger.MethodEntry(LogLevel.Debug);
}
private string FormatPrivateKey(string privateKey)
{
_logger.MethodEntry(LogLevel.Debug);
_logger.MethodExit(LogLevel.Debug);
String keyType = privateKey.Contains("OPENSSH PRIVATE KEY") ? "OPENSSH" : "RSA";
return privateKey.Replace($" {keyType} PRIVATE ", "^^^").Replace(" ", System.Environment.NewLine).Replace("^^^", $" {keyType} PRIVATE ") + System.Environment.NewLine;
}
private string ConvertToPKCS1(string privateKey)
{
_logger.MethodEntry(LogLevel.Debug);
privateKey = privateKey.Replace(System.Environment.NewLine, string.Empty).Replace("-----BEGIN PRIVATE KEY-----", string.Empty).Replace("-----END PRIVATE KEY-----", string.Empty);
PrivateKeyConverter conv = PrivateKeyConverterFactory.FromPkcs8Blob(Convert.FromBase64String(privateKey), string.Empty);
RSA alg = (RSA)conv.ToNetPrivateKey();
string pemString = PemUtilities.DERToPEM(alg.ExportRSAPrivateKey(), PemUtilities.PemObjectType.PrivateKey);
_logger.MethodExit(LogLevel.Debug);
return pemString.Replace("PRIVATE", "RSA PRIVATE");
}
private string FormatFTPPath(string path, bool addLeadingSlashForWindows)
{
_logger.MethodEntry(LogLevel.Debug);
string rtnPath = IsStoreServerLinux ? path : path.Replace("\\", "/");
rtnPath = addLeadingSlashForWindows ? "/" + rtnPath : rtnPath;
_logger.LogTrace($"Formatted path: {rtnPath}");
_logger.MethodExit(LogLevel.Debug);
return rtnPath;
}
private void CheckConnection()
{
try
{
RunCommand("echo", null, ApplicationSettings.UseSudo, null);
}
catch (Exception ex)
{
_logger.LogError(RemoteFileException.FlattenExceptionMessages(ex, "Error validating server connection."));
throw;
}
}
private bool IgnoreError(string err)
{
return IgnoreErrors.Any(p => err.Contains(p, StringComparison.OrdinalIgnoreCase));
}
}
}