Skip to content

Commit

Permalink
Merge pull request #211 from FrendsPlatform/sshnet-version-other-tasks
Browse files Browse the repository at this point in the history
Other SFTP tasks - Renci.SSHNET library update
  • Loading branch information
jefim authored Aug 28, 2024
2 parents 02adf51 + 7b70403 commit 43d289a
Show file tree
Hide file tree
Showing 26 changed files with 299 additions and 73 deletions.
4 changes: 4 additions & 0 deletions Frends.SFTP.DeleteFiles/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [2.1.0] - 2024-08-19
### Updated
- Updated Renci.SshNet library to version 2024.1.0.

## [2.0.0] - 2024-01-02
### Updated
- [Breaking] Updated dependency SSH.NET to the newest version 2023.0.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ internal static Tuple<string, string, byte[]> GetServerFingerPrintsAndHostKey()
using (var client = new SftpClient(_dockerAddress, 2222, _dockerUsername, _dockerPassword))
{
client.ConnectionInfo.HostKeyAlgorithms.Clear();
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-rsa", (data) => { return new KeyHostAlgorithm("ssh-rsa", new RsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-rsa", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ssh-rsa", new RsaKey(sshKeyData));
});

client.HostKeyReceived += delegate (object sender, HostKeyEventArgs e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,46 @@ internal static void ForceHostKeyAlgorithm(SftpClient client, HostKeyAlgorithms
switch (algorithm)
{
case HostKeyAlgorithms.RSA:
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-rsa", (data) => { return new KeyHostAlgorithm("ssh-rsa", new RsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-rsa", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ssh-rsa", new RsaKey(sshKeyData));
});
break;
case HostKeyAlgorithms.Ed25519:
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-ed25519", (data) => { return new KeyHostAlgorithm("ssh-ed25519", new ED25519Key(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-ed25519", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ssh-ed25519", new ED25519Key(sshKeyData));
});
break;
case HostKeyAlgorithms.DSS:
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-dss", (data) => { return new KeyHostAlgorithm("ssh-dss", new DsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-dss", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ssh-dss", new DsaKey(sshKeyData));
});
break;
case HostKeyAlgorithms.Nistp256:
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp256", (data) => { return new KeyHostAlgorithm("ecdsa-sha2-nistp256", new EcdsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp256", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ecdsa-sha2-nistp256", new EcdsaKey(sshKeyData));
});
break;
case HostKeyAlgorithms.Nistp384:
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp384", (data) => { return new KeyHostAlgorithm("ecdsa-sha2-nistp384", new EcdsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp384", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ecdsa-sha2-nistp384", new EcdsaKey(sshKeyData));
});
break;
case HostKeyAlgorithms.Nistp521:
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp521", (data) => { return new KeyHostAlgorithm("ecdsa-sha2-nistp521", new EcdsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp521", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ecdsa-sha2-nistp521", new EcdsaKey(sshKeyData));
});
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>Latest</LangVersion>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand Down Expand Up @@ -32,7 +32,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="SSH.NET" Version="2023.0.0" />
<PackageReference Include="SSH.NET" Version="2024.1.0" />
<PackageReference Include="SshKeyGenerator" Version="1.1.50" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="7.0.0" />
Expand Down
4 changes: 4 additions & 0 deletions Frends.SFTP.ListFiles/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [2.3.0] - 2024-08-19
### Updated
- Updated Renci.SshNet library to version 2024.1.0.

## [2.2.0] - 2023-12-21
### Updated
- [Breaking] Updated dependency SSH.NET to the newest version 2023.0.0.
Expand Down
38 changes: 21 additions & 17 deletions Frends.SFTP.ListFiles/Frends.SFTP.ListFiles.Tests/Lib/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,27 @@ internal static void GenerateDummyFiles()
}
}

internal static Tuple<string, string, byte[]> GetServerFingerPrintsAndHostKey()
{
Tuple<string, string, byte[]> result = null;
using (var client = new SftpClient(_dockerAddress, 2222, _dockerUsername, _dockerPassword))
{
client.ConnectionInfo.HostKeyAlgorithms.Clear();
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-rsa", (data) => { return new KeyHostAlgorithm("ssh-rsa", new RsaKey(), data); });

client.HostKeyReceived += delegate (object sender, HostKeyEventArgs e)
{
result = new Tuple<string, string, byte[]>(e.FingerPrintMD5, e.FingerPrintSHA256, e.HostKey);
e.CanTrust = true;
};
client.Connect();
client.Disconnect();
}
return result;
internal static Tuple<string, string, byte[]> GetServerFingerPrintsAndHostKey()
{
Tuple<string, string, byte[]> result = null;
using (var client = new SftpClient(_dockerAddress, 2222, _dockerUsername, _dockerPassword))
{
client.ConnectionInfo.HostKeyAlgorithms.Clear();
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-rsa", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ssh-rsa", new RsaKey(sshKeyData));
});

client.HostKeyReceived += delegate (object sender, HostKeyEventArgs e)
{
result = new Tuple<string, string, byte[]>(e.FingerPrintMD5, e.FingerPrintSHA256, e.HostKey);
e.CanTrust = true;
};
client.Connect();
client.Disconnect();
}
return result;
}

internal static string ConvertToSHA256Hex(byte[] hostKey)
Expand Down
36 changes: 30 additions & 6 deletions Frends.SFTP.ListFiles/Frends.SFTP.ListFiles/Definitions/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,46 @@ internal static void ForceHostKeyAlgorithm(SftpClient client, HostKeyAlgorithms
switch (algorithm)
{
case HostKeyAlgorithms.RSA:
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-rsa", (data) => { return new KeyHostAlgorithm("ssh-rsa", new RsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-rsa", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ssh-rsa", new RsaKey(sshKeyData));
});
break;
case HostKeyAlgorithms.Ed25519:
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-ed25519", (data) => { return new KeyHostAlgorithm("ssh-ed25519", new ED25519Key(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-ed25519", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ssh-ed25519", new ED25519Key(sshKeyData));
});
break;
case HostKeyAlgorithms.DSS:
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-dss", (data) => { return new KeyHostAlgorithm("ssh-dss", new DsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-dss", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ssh-dss", new DsaKey(sshKeyData));
});
break;
case HostKeyAlgorithms.nistp256:
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp256", (data) => { return new KeyHostAlgorithm("ecdsa-sha2-nistp256", new EcdsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp256", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ecdsa-sha2-nistp256", new EcdsaKey(sshKeyData));
});
break;
case HostKeyAlgorithms.nistp384:
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp384", (data) => { return new KeyHostAlgorithm("ecdsa-sha2-nistp384", new EcdsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp384", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ecdsa-sha2-nistp384", new EcdsaKey(sshKeyData));
});
break;
case HostKeyAlgorithms.nistp521:
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp521", (data) => { return new KeyHostAlgorithm("ecdsa-sha2-nistp521", new EcdsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp521", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ecdsa-sha2-nistp521", new EcdsaKey(sshKeyData));
});
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<AssemblyName>Frends.SFTP.ListFiles</AssemblyName>
<RootNamespace>Frends.SFTP.ListFiles</RootNamespace>

<Version>2.2.0</Version>
<Version>2.3.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand All @@ -29,7 +29,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="SSH.NET" Version="2023.0.0" />
<PackageReference Include="SSH.NET" Version="2024.1.0" />
<PackageReference Include="SshKeyGenerator" Version="1.1.50" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="7.0.0" />
Expand Down
6 changes: 6 additions & 0 deletions Frends.SFTP.MoveFile/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [1.2.0] - 2024-08-19
### Updated
- Updated Renci.SshNet library to version 2024.1.0.
### Added
- Added more tests.

## [1.1.0] - 2023-12-22
### Updated
- [Breaking] Updated dependency SSH.NET to the newest version 2023.0.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,15 @@ public async Task MoveFile_TestWithPrivateKeyFileRsaFromString()
var result = await SFTP.MoveFile(_input, _connection, default);
Assert.IsNotNull(result.Files);
}

[Test]
public async Task MoveFile_TestWithInteractiveKeyboardAuthentication()
{
var connection = Helpers.GetSftpConnection();
connection.UseKeyboardInteractiveAuthentication = true;

var result = await SFTP.MoveFile(_input, connection, default);
Assert.IsNotNull(result.Files);
}
}

22 changes: 22 additions & 0 deletions Frends.SFTP.MoveFile/Frends.SFTP.MoveFile.Tests/ErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;
using Frends.SFTP.MoveFile.Definitions;
using Frends.SFTP.MoveFile.Enums;

Expand All @@ -11,6 +12,27 @@ namespace Frends.SFTP.MoveFile.Tests;
[TestFixture]
public class ErrorTests
{
[Test]
public async Task MoveFile_TestNoSourceFilesFound()
{
var connection = Helpers.GetSftpConnection();

var input = new Input
{
Directory = "/upload",
Pattern = "filenotexisting.txt",
TargetDirectory = "/upload/moved",
CreateTargetDirectories = true,
IfTargetFileExists = FileExistsOperation.Throw
};

var result = await SFTP.MoveFile(input, connection, default);

Assert.IsNotNull(result);
Assert.AreEqual("No files were found matching the given pattern.", result.Message);
Assert.AreEqual(0, result.Files.Count);
}

[Test]
public void MoveFile_TestThrowsWithWrongPort()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ internal static Tuple<string, string, byte[]> GetServerFingerPrintsAndHostKey()
using (var client = new SftpClient(_dockerAddress, 2222, _dockerUsername, _dockerPassword))
{
client.ConnectionInfo.HostKeyAlgorithms.Clear();
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-rsa", (data) => { return new KeyHostAlgorithm("ssh-rsa", new RsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-rsa", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ssh-rsa", new RsaKey(sshKeyData));
});

client.HostKeyReceived += delegate (object sender, HostKeyEventArgs e)
{
Expand Down
42 changes: 33 additions & 9 deletions Frends.SFTP.MoveFile/Frends.SFTP.MoveFile/Definitions/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,46 @@ internal static void ForceHostKeyAlgorithm(SftpClient client, HostKeyAlgorithms
switch (algorithm)
{
case HostKeyAlgorithms.RSA:
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-rsa", (data) => { return new KeyHostAlgorithm("ssh-rsa", new RsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-rsa", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ssh-rsa", new RsaKey(sshKeyData));
});
break;
case HostKeyAlgorithms.Ed25519:
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-ed25519", (data) => { return new KeyHostAlgorithm("ssh-ed25519", new ED25519Key(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-ed25519", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ssh-ed25519", new ED25519Key(sshKeyData));
});
break;
case HostKeyAlgorithms.DSS:
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-dss", (data) => { return new KeyHostAlgorithm("ssh-dss", new DsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ssh-dss", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ssh-dss", new DsaKey(sshKeyData));
});
break;
case HostKeyAlgorithms.nistp256:
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp256", (data) => { return new KeyHostAlgorithm("ecdsa-sha2-nistp256", new EcdsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp256", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ecdsa-sha2-nistp256", new EcdsaKey(sshKeyData));
});
break;
case HostKeyAlgorithms.nistp384:
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp384", (data) => { return new KeyHostAlgorithm("ecdsa-sha2-nistp384", new EcdsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp384", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ecdsa-sha2-nistp384", new EcdsaKey(sshKeyData));
});
break;
case HostKeyAlgorithms.nistp521:
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp521", (data) => { return new KeyHostAlgorithm("ecdsa-sha2-nistp521", new EcdsaKey(), data); });
client.ConnectionInfo.HostKeyAlgorithms.Add("ecdsa-sha2-nistp521", (data) =>
{
var sshKeyData = new SshKeyData(data);
return new KeyHostAlgorithm("ecdsa-sha2-nistp521", new EcdsaKey(sshKeyData));
});
break;
}

Expand Down Expand Up @@ -178,7 +202,7 @@ internal static bool FileMatchesMask(string filename, string mask)
}

return Regex.IsMatch(filename, pattern, RegexOptions.IgnoreCase);
}
}

internal static Encoding GetEncoding(FileEncoding encoding, string encodingString, bool enableBom)
{
Expand All @@ -200,6 +224,6 @@ internal static Encoding GetEncoding(FileEncoding encoding, string encodingStrin
return e;
default:
throw new ArgumentOutOfRangeException($"Unknown Encoding type: '{encoding}'.");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<AssemblyName>Frends.SFTP.MoveFile</AssemblyName>
<RootNamespace>Frends.SFTP.MoveFile</RootNamespace>

<Version>1.1.0</Version>
<Version>1.2.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand All @@ -29,7 +29,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="SSH.NET" Version="2023.0.0" />
<PackageReference Include="SSH.NET" Version="2024.1.0" />
<PackageReference Include="SshKeyGenerator" Version="1.1.50" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
</ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions Frends.SFTP.ReadFile/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [2.2.0] - 2024-08-19
### Updated
- Updated Renci.SshNet library to version 2024.1.0.

## [2.1.0] - 2023-12-22
### Updated
- [Breaking] Updated dependency SSH.NET to the newest version 2023.0.0.
Expand Down
Loading

0 comments on commit 43d289a

Please sign in to comment.