connections now just reference credential ids

This commit is contained in:
David Sparer
2017-07-27 21:50:50 -05:00
parent e98291498b
commit 7a2ab59346
14 changed files with 107 additions and 63 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Xml.Linq;
using mRemoteNG.Config.Serializers;
using mRemoteNG.Connection;
@@ -78,7 +79,7 @@ namespace mRemoteNGTests.Config.Serializers.ConnectionSerializers
Username = "myuser",
Domain = "superdomain",
Password = "pass",
CredentialRecord = Substitute.For<ICredentialRecord>(),
CredentialRecordId = Guid.Empty,
Hostname = "somehost",
ExtApp = "myextapp",
PreExtApp = "preext1",

View File

@@ -11,18 +11,30 @@ namespace mRemoteNGTests.Config.Serializers.MiscSerializers
{
public class CsvConnectionsSerializerMremotengFormatTests
{
private ICredentialRepositoryList _credentialRepositoryList;
private const string ConnectionName = "myconnection";
private const string Username = "myuser";
private const string Domain = "mydomain";
private const string Password = "mypass123";
[OneTimeSetUp]
public void OneTimeSetup()
{
var credRecord = Substitute.For<ICredentialRecord>();
credRecord.Username.Returns(Username);
credRecord.Domain.Returns(Domain);
credRecord.Password.Returns(Password.ConvertToSecureString());
_credentialRepositoryList = Substitute.For<ICredentialRepositoryList>();
_credentialRepositoryList.GetCredentialRecord(new Guid()).ReturnsForAnyArgs(credRecord);
}
[TestCase(Username)]
[TestCase(Domain)]
[TestCase(Password)]
[TestCase("InheritColors")]
public void CreatesCsv(string valueThatShouldExist)
{
var serializer = new CsvConnectionsSerializerMremotengFormat(new SaveFilter());
var serializer = new CsvConnectionsSerializerMremotengFormat(new SaveFilter(), _credentialRepositoryList);
var connectionInfo = BuildConnectionInfo();
var csv = serializer.Serialize(connectionInfo);
Assert.That(csv, Does.Match(valueThatShouldExist));
@@ -35,7 +47,7 @@ namespace mRemoteNGTests.Config.Serializers.MiscSerializers
public void SerializerRespectsSaveFilterSettings(string valueThatShouldntExist)
{
var saveFilter = new SaveFilter(true);
var serializer = new CsvConnectionsSerializerMremotengFormat(saveFilter);
var serializer = new CsvConnectionsSerializerMremotengFormat(saveFilter, _credentialRepositoryList);
var connectionInfo = BuildConnectionInfo();
var csv = serializer.Serialize(connectionInfo);
Assert.That(csv, Does.Not.Match(valueThatShouldntExist));
@@ -44,7 +56,7 @@ namespace mRemoteNGTests.Config.Serializers.MiscSerializers
[Test]
public void CanSerializeEmptyConnectionInfo()
{
var serializer = new CsvConnectionsSerializerMremotengFormat(new SaveFilter());
var serializer = new CsvConnectionsSerializerMremotengFormat(new SaveFilter(), _credentialRepositoryList);
var connectionInfo = new ConnectionInfo();
var csv = serializer.Serialize(connectionInfo);
Assert.That(csv, Is.Not.Empty);
@@ -53,33 +65,29 @@ namespace mRemoteNGTests.Config.Serializers.MiscSerializers
[Test]
public void CantPassNullToConstructor()
{
Assert.Throws<ArgumentNullException>(() => new CsvConnectionsSerializerMremotengFormat(null));
Assert.Throws<ArgumentNullException>(() => new CsvConnectionsSerializerMremotengFormat(null, _credentialRepositoryList));
}
[Test]
public void CantPassNullToSerializeConnectionInfo()
{
var serializer = new CsvConnectionsSerializerMremotengFormat(new SaveFilter());
var serializer = new CsvConnectionsSerializerMremotengFormat(new SaveFilter(), _credentialRepositoryList);
Assert.Throws<ArgumentNullException>(() => serializer.Serialize((ConnectionInfo)null));
}
[Test]
public void CantPassNullToSerializeConnectionTreeModel()
{
var serializer = new CsvConnectionsSerializerMremotengFormat(new SaveFilter());
var serializer = new CsvConnectionsSerializerMremotengFormat(new SaveFilter(), _credentialRepositoryList);
Assert.Throws<ArgumentNullException>(() => serializer.Serialize((ConnectionTreeModel)null));
}
private ConnectionInfo BuildConnectionInfo()
{
var credRecord = Substitute.For<ICredentialRecord>();
credRecord.Username.Returns(Username);
credRecord.Domain.Returns(Domain);
credRecord.Password.Returns(Password.ConvertToSecureString());
return new ConnectionInfo
{
Name = ConnectionName,
CredentialRecord = credRecord,
CredentialRecordId = Guid.NewGuid(),
Inheritance = {Colors = true}
};
}

View File

@@ -84,7 +84,7 @@ namespace mRemoteNG.App
serializer = new XmlConnectionsSerializer(cryptographyProvider, connectionNodeSerializer);
break;
case ConnectionsSaver.Format.mRCSV:
serializer = new CsvConnectionsSerializerMremotengFormat(saveFilter);
serializer = new CsvConnectionsSerializerMremotengFormat(saveFilter, Runtime.CredentialProviderCatalog);
break;
default:
throw new ArgumentOutOfRangeException(nameof(saveFormat), saveFormat, null);

View File

@@ -37,7 +37,7 @@ namespace mRemoteNG.App.Initialization
{
var defaultCredId = Settings.Default.ConDefaultCredentialRecord;
var matchedCredentials = _credentialsService.GetCredentialRecords().Where(record => record.Id.Equals(defaultCredId)).ToArray();
DefaultConnectionInfo.Instance.CredentialRecord = matchedCredentials.Any() ? matchedCredentials.First() : null;
DefaultConnectionInfo.Instance.CredentialRecordId = matchedCredentials.Any() ? matchedCredentials.First().Id : default(Guid?);
}
}
}

View File

@@ -188,7 +188,7 @@ namespace mRemoteNG.Config.Connections
private void SaveToMremotengFormattedCsv()
{
var csvConnectionsSerializer = new CsvConnectionsSerializerMremotengFormat(SaveFilter);
var csvConnectionsSerializer = new CsvConnectionsSerializerMremotengFormat(SaveFilter, Runtime.CredentialProviderCatalog);
var dataProvider = new FileDataProvider(ConnectionFileName);
var csvContent = csvConnectionsSerializer.Serialize(ConnectionTreeModel);
dataProvider.Save(csvContent);

View File

@@ -42,7 +42,7 @@ namespace mRemoteNG.Config.Serializers
element.Add(new XAttribute("Id", connectionInfo.ConstantID));
element.Add(_saveFilter.SaveCredentialId
? new XAttribute("CredentialId", connectionInfo.CredentialRecord?.Id.ToString() ?? "")
? new XAttribute("CredentialId", connectionInfo.CredentialRecordId?.ToString() ?? "")
: new XAttribute("CredentialId", ""));
element.Add(new XAttribute("Hostname", connectionInfo.Hostname));

View File

@@ -508,16 +508,7 @@ namespace mRemoteNG.Config.Serializers
if (_confVersion >= 2.7)
{
connectionInfo.Inheritance.CredentialRecord = bool.Parse(xmlnode.Attributes["InheritCredentialRecord"]?.Value ?? "False");
var requestedCredentialId = xmlnode.Attributes["CredentialId"]?.Value;
if (!string.IsNullOrEmpty(requestedCredentialId) && _credentialRecords.Any())
{
var matchingCredential = _credentialRecords.Where(record => record.Id.ToString() == requestedCredentialId).ToArray();
if (matchingCredential.Any())
connectionInfo.CredentialRecord = matchingCredential.First();
else
Runtime.MessageCollector?.AddMessage(MessageClass.InformationMsg, string.Format(Language.strFindMatchingCredentialFailed, requestedCredentialId, connectionInfo.Name));
}
connectionInfo.CredentialRecordId = Guid.Parse(xmlnode.Attributes["CredentialId"]?.Value ?? "");
}
}
catch (Exception ex)

View File

@@ -1,7 +1,9 @@
using System;
using System.Linq;
using mRemoteNG.App;
using mRemoteNG.Connection;
using mRemoteNG.Container;
using mRemoteNG.Credential;
using mRemoteNG.Security;
using mRemoteNG.Tree;
using mRemoteNG.Tree.Root;
@@ -13,13 +15,18 @@ namespace mRemoteNG.Config.Serializers
private string _csv = "";
private ConnectionInfo _serializationTarget;
private readonly SaveFilter _saveFilter;
private readonly ICredentialRepositoryList _credentialRepositoryList;
public CsvConnectionsSerializerMremotengFormat(SaveFilter saveFilter)
public CsvConnectionsSerializerMremotengFormat(SaveFilter saveFilter, ICredentialRepositoryList credentialRepositoryList)
{
if (saveFilter == null)
throw new ArgumentNullException(nameof(saveFilter));
if (credentialRepositoryList == null)
throw new ArgumentNullException(nameof(credentialRepositoryList));
_saveFilter = saveFilter;
_credentialRepositoryList = credentialRepositoryList;
}
public string Serialize(ConnectionTreeModel connectionTreeModel)
@@ -83,14 +90,20 @@ namespace mRemoteNG.Config.Serializers
csvLine += con.Name + ";" + GetNodePath(con) + ";" + con.Description + ";" + con.Icon + ";" + con.Panel + ";";
if (_saveFilter.SaveUsername)
csvLine += con.CredentialRecord?.Username + ";";
if (con.CredentialRecordId.HasValue)
{
var credentialRecord =
_credentialRepositoryList.GetCredentialRecord(con.CredentialRecordId.Value);
if (_saveFilter.SavePassword)
csvLine += con.CredentialRecord?.Password.ConvertToUnsecureString() + ";";
if (_saveFilter.SaveUsername)
csvLine += credentialRecord?.Username + ";";
if (_saveFilter.SaveDomain)
csvLine += con.CredentialRecord?.Domain + ";";
if (_saveFilter.SavePassword)
csvLine += credentialRecord?.Password.ConvertToUnsecureString() + ";";
if (_saveFilter.SaveDomain)
csvLine += credentialRecord?.Domain + ";";
}
csvLine += con.Hostname + ";" +
con.Protocol + ";" +

View File

@@ -131,7 +131,7 @@ namespace mRemoteNG.Config.Serializers.Versioning
Guid id;
Guid.TryParse(connectionInfo.ConstantID, out id);
if (map.ContainsKey(id))
connectionInfo.CredentialRecord = map[id];
connectionInfo.CredentialRecordId = map[id].Id;
}
}
}

View File

@@ -24,7 +24,7 @@ namespace mRemoteNG.Connection
private string _panel = "";
private string _hostname = "";
private ICredentialRecord _credentialRecord;
private Guid? _credentialRecordId;
private ProtocolType _protocol;
private string _extApp = "";
@@ -137,10 +137,10 @@ namespace mRemoteNG.Connection
LocalizedAttributes.LocalizedDescription(nameof(Language.strPropertyDescriptionCredential))]
[Editor(typeof(CredentialRecordListAdaptor), typeof(UITypeEditor))]
[TypeConverter(typeof(ExpandableObjectConverter))]
public virtual ICredentialRecord CredentialRecord
public virtual Guid? CredentialRecordId
{
get { return GetPropertyValue(nameof(CredentialRecord), _credentialRecord); }
set { SetField(ref _credentialRecord, value, nameof(CredentialRecord)); }
get { return GetPropertyValue(nameof(CredentialRecordId), _credentialRecordId); }
set { SetField(ref _credentialRecordId, value, nameof(CredentialRecordId)); }
}
[Obsolete("Use the CredentialRecord property")]

View File

@@ -1,4 +1,5 @@
using System;
using System.Security;
using AxWFICALib;
using System.Windows.Forms;
using mRemoteNG.App;
@@ -117,11 +118,19 @@ namespace mRemoteNG.Connection.Protocol.ICA
{
return;
}
var user = _info.CredentialRecord?.Username ?? "";
var pass = _info.CredentialRecord?.Password ?? "".ConvertToSecureString();
var dom = _info.CredentialRecord?.Domain ?? "";
var user = "";
var pass = new SecureString();
var dom = "";
if (_info.CredentialRecordId.HasValue)
{
var credentialRecord = Runtime.CredentialProviderCatalog.GetCredentialRecord(_info.CredentialRecordId.Value);
user = credentialRecord?.Username ?? "";
pass = credentialRecord?.Password ?? "".ConvertToSecureString();
dom = credentialRecord?.Domain ?? "";
}
if (string.IsNullOrEmpty(user))
{
if (Settings.Default.EmptyCredentials == "windows")

View File

@@ -6,6 +6,7 @@ using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using mRemoteNG.Credential;
using mRemoteNG.Security;
using mRemoteNG.Security.SymmetricEncryption;
using mRemoteNG.Tools.Cmdline;
@@ -72,10 +73,16 @@ namespace mRemoteNG.Connection.Protocol
{
var username = "";
var password = "";
if (!string.IsNullOrEmpty(InterfaceControl.Info.CredentialRecord?.Username))
var credentialRecord = default(ICredentialRecord);
if (InterfaceControl.Info.CredentialRecordId.HasValue)
{
credentialRecord = Runtime.CredentialProviderCatalog.GetCredentialRecord(InterfaceControl.Info.CredentialRecordId.Value);
}
if (!string.IsNullOrEmpty(credentialRecord?.Username))
{
username = InterfaceControl.Info.CredentialRecord?.Username;
username = credentialRecord.Username;
}
else
{
@@ -90,10 +97,10 @@ namespace mRemoteNG.Connection.Protocol
break;
}
}
if (!string.IsNullOrEmpty(InterfaceControl.Info.CredentialRecord?.Password.ConvertToUnsecureString()))
if (!string.IsNullOrEmpty(credentialRecord?.Password.ConvertToUnsecureString()))
{
password = InterfaceControl.Info.CredentialRecord?.Password.ConvertToUnsecureString();
password = credentialRecord.Password.ConvertToUnsecureString();
}
else
{
@@ -103,7 +110,7 @@ namespace mRemoteNG.Connection.Protocol
password = cryptographyProvider.Decrypt(Settings.Default.DefaultPassword, Runtime.EncryptionKey);
}
}
arguments.Add("-" + (int)PuttySSHVersion);
if (((int)Force & (int)ConnectionInfo.Force.NoCredentials) != (int)ConnectionInfo.Force.NoCredentials)

View File

@@ -346,12 +346,12 @@ namespace mRemoteNG.Connection.Protocol.RDP
{
if (_connectionInfo.RDGatewayUseConnectionCredentials == RDGatewayUseConnectionCredentials.Yes)
{
if (_connectionInfo.CredentialRecord != null)
{
_rdpClient.TransportSettings2.GatewayUsername = _connectionInfo.CredentialRecord.Username;
_rdpClient.TransportSettings2.GatewayPassword = _connectionInfo.CredentialRecord.Password.ConvertToUnsecureString();
_rdpClient.TransportSettings2.GatewayDomain = _connectionInfo.CredentialRecord.Domain;
}
if (!_connectionInfo.CredentialRecordId.HasValue) return;
var credentialRecord =
Runtime.CredentialProviderCatalog.GetCredentialRecord(_connectionInfo.CredentialRecordId.Value);
_rdpClient.TransportSettings2.GatewayUsername = credentialRecord?.Username;
_rdpClient.TransportSettings2.GatewayPassword = credentialRecord?.Password.ConvertToUnsecureString();
_rdpClient.TransportSettings2.GatewayDomain = credentialRecord?.Domain;
}
else if (_connectionInfo.RDGatewayUseConnectionCredentials == RDGatewayUseConnectionCredentials.SmartCard)
{
@@ -420,9 +420,17 @@ namespace mRemoteNG.Connection.Protocol.RDP
return;
}
var userName = _connectionInfo.CredentialRecord?.Username ?? "";
var password = _connectionInfo.CredentialRecord?.Password ?? new SecureString();
var domain = _connectionInfo.CredentialRecord?.Domain ?? "";
var userName = "";
var password = new SecureString();
var domain = "";
if (_connectionInfo.CredentialRecordId.HasValue)
{
var credentialRecord = Runtime.CredentialProviderCatalog.GetCredentialRecord(_connectionInfo.CredentialRecordId.Value);
userName = credentialRecord?.Username ?? "";
password = credentialRecord?.Password ?? new SecureString();
domain = credentialRecord?.Domain ?? "";
}
if (string.IsNullOrEmpty(userName))
{

View File

@@ -165,7 +165,11 @@ namespace mRemoteNG.Connection.Protocol.VNC
_VNC.ConnectComplete += VNCEvent_Connected;
_VNC.ConnectionLost += VNCEvent_Disconnected;
FrmMain.ClipboardChanged += VNCEvent_ClipboardChanged;
if (((int)Force & (int)ConnectionInfo.Force.NoCredentials) != (int)ConnectionInfo.Force.NoCredentials && !string.IsNullOrEmpty(Info.CredentialRecord?.Password.ConvertToUnsecureString()))
if (!Info.CredentialRecordId.HasValue)
return;
var credentialRecord = Runtime.CredentialProviderCatalog.GetCredentialRecord(Info.CredentialRecordId.Value);
if (((int)Force & (int)ConnectionInfo.Force.NoCredentials) != (int)ConnectionInfo.Force.NoCredentials
&& credentialRecord?.Password?.Length > 0)
{
_VNC.GetPassword = VNCEvent_Authenticate;
}
@@ -198,9 +202,12 @@ namespace mRemoteNG.Connection.Protocol.VNC
private string VNCEvent_Authenticate()
{
return Info.CredentialRecord?.Password.ConvertToUnsecureString() ?? "";
return Info.CredentialRecordId.HasValue
? Runtime.CredentialProviderCatalog.GetCredentialRecord(Info.CredentialRecordId.Value).Password.ConvertToUnsecureString()
: "";
}
#endregion
#endregion
#region Enums
public enum Defaults