-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSshCredentials.cs
120 lines (105 loc) · 4.53 KB
/
SshCredentials.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
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
//-----------------------------------------------------------------------------
// FILE: SshCredentials.cs
// CONTRIBUTOR: Jeff Lill
// COPYRIGHT: Copyright © 2005-2024 by NEONFORGE LLC. All rights reserved.
//
// 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.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Renci.SshNet;
namespace Neon.SSH
{
/// <summary>
/// Provides credentials suitable for connecting to a server machine via SSH.
/// </summary>
public class SshCredentials
{
//---------------------------------------------------------------------
// Static members
/// <summary>
/// Returns credentials based on a user name and password.
/// </summary>
/// <param name="username">The user name.</param>
/// <param name="password">The password.</param>
/// <returns>The <see cref="SshCredentials"/>.</returns>
public static SshCredentials FromUserPassword(string username, string password)
{
Covenant.Requires<ArgumentNullException>(!string.IsNullOrEmpty(username), nameof(username));
Covenant.Requires<ArgumentNullException>(password != null, nameof(password));
return new SshCredentials()
{
Username = username,
AuthenticationMethod = new PasswordAuthenticationMethod(username, password)
};
}
/// <summary>
/// Returns credentials based on a user name and password and an optional passphrase.
/// </summary>
/// <param name="username">The user name.</param>
/// <param name="privateKey">The unencrypted PEM-encoded private key.</param>
/// <param name="passPhrase">Optionally specifies a pass phrase.</param>
/// <returns>The <see cref="SshCredentials"/>.</returns>
public static SshCredentials FromPrivateKey(string username, string privateKey, string passPhrase = null)
{
Covenant.Requires<ArgumentNullException>(!string.IsNullOrEmpty(username), nameof(username));
Covenant.Requires<ArgumentNullException>(privateKey != null, nameof(privateKey));
using (var privateKeyStream = new MemoryStream(Encoding.UTF8.GetBytes(privateKey)))
{
var privateKeyFile = string.IsNullOrWhiteSpace(passPhrase)
? new PrivateKeyFile(privateKeyStream)
: new PrivateKeyFile(privateKeyStream, passPhrase);
return new SshCredentials()
{
Username = username,
AuthenticationMethod = new PrivateKeyAuthenticationMethod(username, privateKeyFile)
};
}
}
/// <summary>
/// Returns an instance indicating that there are no available credentials.
/// </summary>
/// <returns>The credentials.</returns>
public static SshCredentials None
{
get { return new SshCredentials(); }
}
//---------------------------------------------------------------------
// Instance members
private AuthenticationMethod authenticationMethod;
/// <summary>
/// Returns the user name.
/// </summary>
public string Username { get; private set; }
/// <summary>
/// Returns the authentication method to be used to establish a SSH.NET session.
/// </summary>
/// <exception cref="SshProxyException">Thrown if the SSH credentials are not available.</exception>
internal AuthenticationMethod AuthenticationMethod
{
get
{
if (authenticationMethod == null)
{
throw new SshProxyException("cluster SSH credentials are not available.");
}
return authenticationMethod;
}
set { authenticationMethod = value; }
}
}
}