/
SshHelper.cs
57 lines (51 loc) · 1.99 KB
/
SshHelper.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using Kudu.Client.SSHKey;
namespace Kudu.FunctionalTests
{
public static class SshHelper
{
private const string SSHConfigFile = "config";
private const string SSHKeyFile = "id_rsa";
public static bool PrepareSSHEnv(RemoteSSHKeyManager sshManager)
{
string sshKey = ReadManifestFile(SSHKeyFile);
if (!String.IsNullOrEmpty(sshKey))
{
sshManager.SetPrivateKey(sshKey).Wait();
return true;
}
return false;
}
public static IDictionary<string, string> PrepareSSHEnv(out string id_rsa)
{
var sshPath = new DirectoryInfo(Path.Combine(Kudu.TestHarness.PathHelper.TestsRootPath, ".ssh"));
id_rsa = WriteSSHKeys(sshPath);
Dictionary<string, string> environments = new Dictionary<string, string>();
environments["HOME"] = sshPath.Parent.FullName;
environments["HOMEDRIVE"] = sshPath.Root.Name.Trim('\\');
environments["HOMEPATH"] = environments["HOME"].Replace(environments["HOMEDRIVE"], String.Empty);
return environments;
}
private static string WriteSSHKeys(DirectoryInfo sshPath)
{
if (!sshPath.Exists)
{
sshPath.Create();
}
string id_rsa = ReadManifestFile(SSHKeyFile);
File.WriteAllText(Path.Combine(sshPath.FullName, SSHKeyFile), id_rsa);
File.WriteAllText(Path.Combine(sshPath.FullName, SSHConfigFile), ReadManifestFile(SSHConfigFile));
return id_rsa;
}
private static string ReadManifestFile(string fileName)
{
var assembly = typeof(SshHelper).Assembly;
using (var reader = new StreamReader(assembly.GetManifestResourceStream("Kudu.FunctionalTests..ssh." + fileName)))
{
return reader.ReadToEnd();
}
}
}
}