From 4d553af1c391d5c1da90dc24c41dcd1b254a7394 Mon Sep 17 00:00:00 2001 From: "ARTECH\\sgrampone" Date: Mon, 22 Aug 2022 15:48:26 -0300 Subject: [PATCH 1/2] Avoid validation of options.password when using private key file. --- .../GeneXusSftp/Sftp/SftpClient.cs | 37 +++++++------------ .../SecurityAPITestNetCore.csproj | 1 + 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/dotnet/src/extensions/SecurityAPI/dotnet/dotnetframework/GeneXusSftp/Sftp/SftpClient.cs b/dotnet/src/extensions/SecurityAPI/dotnet/dotnetframework/GeneXusSftp/Sftp/SftpClient.cs index bb51d04c4..aa7a53a92 100644 --- a/dotnet/src/extensions/SecurityAPI/dotnet/dotnetframework/GeneXusSftp/Sftp/SftpClient.cs +++ b/dotnet/src/extensions/SecurityAPI/dotnet/dotnetframework/GeneXusSftp/Sftp/SftpClient.cs @@ -51,27 +51,19 @@ public override bool Connect(SftpOptions options) return false; } bool useKey = false; - if (SecurityUtils.compareStrings("", options.KeyPath) || SecurityUtils.compareStrings("", options.User) || SecurityUtils.compareStrings("", options.KeyPassword)) - { - useKey = false; - if (SecurityUtils.compareStrings("", options.User) - || SecurityUtils.compareStrings("", options.Password)) - { - - this.error.setError("SF001", "Authentication misconfiguration"); - return false; - } - else - { - useKey = false; - } - } - else - { - useKey = true; - } - - + if (!SecurityUtils.compareStrings("", options.KeyPath) ) + { + useKey = true; + }else + { + if (SecurityUtils.compareStrings("", options.User) + || SecurityUtils.compareStrings("", options.Password)) + { + + this.error.setError("SF001", "Authentication misconfiguration. Missing user or password"); + return false; + } + } if (SecurityUtils.compareStrings("", options.Host)) { @@ -318,9 +310,6 @@ private void SetupChannelSftp(SftpOptions options, bool useKey) PrivateKeyFile keyFile = new PrivateKeyFile(options.KeyPath, options.KeyPassword); method.Add(new PrivateKeyAuthenticationMethod(options.User, keyFile)); - - - } else { diff --git a/dotnet/src/extensions/SecurityAPI/test/dotnetcore/SecurityAPITestNetCore/SecurityAPITestNetCore.csproj b/dotnet/src/extensions/SecurityAPI/test/dotnetcore/SecurityAPITestNetCore/SecurityAPITestNetCore.csproj index 12937f909..bfb50beb9 100644 --- a/dotnet/src/extensions/SecurityAPI/test/dotnetcore/SecurityAPITestNetCore/SecurityAPITestNetCore.csproj +++ b/dotnet/src/extensions/SecurityAPI/test/dotnetcore/SecurityAPITestNetCore/SecurityAPITestNetCore.csproj @@ -49,6 +49,7 @@ + From 738456b0085d633d65691e7181ee0ba4f7d6113f Mon Sep 17 00:00:00 2001 From: "ARTECH\\sgrampone" Date: Mon, 22 Aug 2022 16:06:26 -0300 Subject: [PATCH 2/2] Add test --- .../Sftp/TestLoginWithKeyWithoutPassword.cs | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 dotnet/src/extensions/SecurityAPI/test/dotnetframework/SecurityAPITest/Sftp/TestLoginWithKeyWithoutPassword.cs diff --git a/dotnet/src/extensions/SecurityAPI/test/dotnetframework/SecurityAPITest/Sftp/TestLoginWithKeyWithoutPassword.cs b/dotnet/src/extensions/SecurityAPI/test/dotnetframework/SecurityAPITest/Sftp/TestLoginWithKeyWithoutPassword.cs new file mode 100644 index 000000000..5d46335c3 --- /dev/null +++ b/dotnet/src/extensions/SecurityAPI/test/dotnetframework/SecurityAPITest/Sftp/TestLoginWithKeyWithoutPassword.cs @@ -0,0 +1,74 @@ +using SecurityAPITest.SecurityAPICommons.commons; +using NUnit.Framework; +using Sftp.GeneXusSftp; +using System.IO; +using System; + +namespace SecurityAPITest.Sftp +{ + [TestFixture] + [RunIfRunSettingsConfigured] + public class TestLoginWithKeyWithoutPassword : SecurityAPITestObject + { + protected static string host; + protected static string user; + protected static string keyPath; + protected static string keyPassword; + protected static string localPath; + protected static string remoteDir; + protected static string remoteFilePath; + protected static string localDir; + + + [SetUp] + public virtual void SetUp() + { + + host = TestContextParameter("gx_ftp_host"); + user = TestContextParameter("gx_sftp_user"); + string known_hosts_content_base64 = TestContextParameter("gx_ftp_known_hosts_content_base64"); + keyPath = Path.Combine(BASE_PATH, "Temp", "sftptest", "key", "id_rsa"); + string id_rsaConentBase64 = TestContextParameter("gx_ftp_id_rsa_content_base64"); + File.WriteAllBytes(keyPath, Convert.FromBase64String(id_rsaConentBase64)); + keyPassword = TestContextParameter("gx_sftp_key_password"); + localPath = Path.Combine(BASE_PATH, "Temp", "sftptest", "sftptest1.txt"); + remoteDir = "sftp"; + remoteFilePath = "sftp/sftptest1.txt"; + localDir = Path.Combine(BASE_PATH, "Temp", "sftptest", "back"); + } + + private SftpClient TestConnection(SftpOptions options) + { + SftpClient client = new SftpClient(); + bool connected = client.Connect(options); + True(connected, client); + return client; + } + + private void TestPut(SftpClient client) + { + bool put = client.Put(localPath, remoteDir); + True(put, client); + } + + private void TestGet(SftpClient client) + { + bool get = client.Get(remoteFilePath, localDir); + True(get, client); + } + + [Test] + public void TestWithKey() + { + SftpOptions options = new SftpOptions(); + options.Host = host; + options.User = user; + options.AllowHostKeyChecking = false; + options.KeyPassword = keyPassword; + SftpClient client = TestConnection(options); + TestPut(client); + TestGet(client); + client.Disconnect(); + } + } +}