Explorar el Código

Added SshCommand.InputStream to allow writing to stdin of SshCommand (#1293)

* Making all unit tests pass locally.
Excluded MD5 tests on net462 because I get System.InvalidOperationException: 'This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms.'
SshdConfig: do not throw for "Include", just do nothing.
Modified failing dos2unix parameters in Dockerfile.TestServer.
Forceing LF line ending for key files used by integration tests, otherwise using them causes error.
SftpClientTest.Test_Sftp_Multiple_Async_Upload_And_Download_10Files_5MB_Each times out for maxFiles=10, decreasing to 2 to make the test pass.

* Added SshCommand.InputStream.

* Added an integration test for SshCommand.InputStream.

* Reverting changes made to unit tests unrelated to this PR.

* Moved ChannelInputStream's EOF sending from Write to Dispose. Replace SshCommand.InputStream with CreateInputStream to emphasise that a (disposable) resource is created here. EndExecute also closes the _inputStream to make sure that EOF is sent (in case the user forgot to dispose the input stream). Added more unit tests: sending the input one byte at a time, not disposing the input stream, calling CreateInputStream before BeginExecute or AfterEndExecute throws exception.

* Fixing review comments.

* Fix build error after #1286

---------

Co-authored-by: ferenc.vizkeleti <ferenc.vizkeleti@innopay.hu>
Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
Ferenc Vizkeleti hace 1 año
padre
commit
2d0e03b8ce

+ 4 - 1
.gitattributes

@@ -17,4 +17,7 @@
 *.snk binary
 
 # Ensure key files have LF endings for easier usage with ssh-keygen
-test/Data/* eol=lf
+# Also, the dockerfile used for integration tests fails if key files have cr-lf
+test/Data/* eol=lf
+test/Renci.SshNet.IntegrationTests/server/**/* eol=lf
+test/Renci.SshNet.IntegrationTests/user/* eol=lf

+ 217 - 0
src/Renci.SshNet/Common/ChannelInputStream.cs

@@ -0,0 +1,217 @@
+using System;
+using System.IO;
+
+using Renci.SshNet.Channels;
+
+namespace Renci.SshNet.Common
+{
+    /// <summary>
+    /// ChannelInputStream is a one direction stream intended for channel data.
+    /// </summary>
+    internal sealed class ChannelInputStream : Stream
+    {
+        /// <summary>
+        /// Channel to send data to.
+        /// </summary>
+        private readonly IChannelSession _channel;
+
+        /// <summary>
+        /// Total bytes passed through the stream.
+        /// </summary>
+        private long _totalPosition;
+
+        /// <summary>
+        /// Indicates whether the current instance was disposed.
+        /// </summary>
+        private bool _isDisposed;
+
+        internal ChannelInputStream(IChannelSession channel)
+        {
+            _channel = channel;
+        }
+
+        /// <summary>
+        /// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
+        /// </summary>
+        /// <exception cref="IOException">An I/O error occurs.</exception>
+        /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
+        /// <remarks>
+        /// Once flushed, any subsequent read operations no longer block until requested bytes are available. Any write operation reactivates blocking
+        /// reads.
+        /// </remarks>
+        public override void Flush()
+        {
+        }
+
+        /// <summary>
+        /// When overridden in a derived class, sets the position within the current stream.
+        /// </summary>
+        /// <returns>
+        /// The new position within the current stream.
+        /// </returns>
+        /// <param name="offset">A byte offset relative to the origin parameter.</param>
+        /// <param name="origin">A value of type <see cref="SeekOrigin"/> indicating the reference point used to obtain the new position.</param>
+        /// <exception cref="NotSupportedException">The stream does not support seeking, such as if the stream is constructed from a pipe or console output.</exception>
+        public override long Seek(long offset, SeekOrigin origin)
+        {
+            throw new NotSupportedException();
+        }
+
+        /// <summary>
+        /// When overridden in a derived class, sets the length of the current stream.
+        /// </summary>
+        /// <param name="value">The desired length of the current stream in bytes.</param>
+        /// <exception cref="NotSupportedException">The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.</exception>
+        public override void SetLength(long value)
+        {
+            throw new NotSupportedException();
+        }
+
+        /// <summary>
+        /// When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
+        /// </summary>
+        /// <returns>
+        /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero if the stream is closed or end of the stream has been reached.
+        /// </returns>
+        /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
+        /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
+        /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
+        /// <exception cref="ArgumentException">The sum of offset and count is larger than the buffer length.</exception>
+        /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
+        /// <exception cref="NotSupportedException">The stream does not support reading.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <c>null</c>.</exception>
+        /// <exception cref="IOException">An I/O error occurs.</exception>
+        /// <exception cref="ArgumentOutOfRangeException">offset or count is negative.</exception>
+        public override int Read(byte[] buffer, int offset, int count)
+        {
+            throw new NotSupportedException();
+        }
+
+        /// <summary>
+        /// When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
+        /// </summary>
+        /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
+        /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
+        /// <param name="count">The number of bytes to be written to the current stream.</param>
+        /// <exception cref="IOException">An I/O error occurs.</exception>
+        /// <exception cref="NotSupportedException">The stream does not support writing.</exception>
+        /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentException">The sum of offset and count is greater than the buffer length.</exception>
+        /// <exception cref="ArgumentOutOfRangeException">offset or count is negative.</exception>
+        public override void Write(byte[] buffer, int offset, int count)
+        {
+            if (buffer == null)
+            {
+                throw new ArgumentNullException(nameof(buffer));
+            }
+
+            if (offset + count > buffer.Length)
+            {
+                throw new ArgumentException("The sum of offset and count is greater than the buffer length.");
+            }
+
+            if (offset < 0 || count < 0)
+            {
+                throw new ArgumentOutOfRangeException(nameof(offset), "offset or count is negative.");
+            }
+
+            if (_isDisposed)
+            {
+                throw CreateObjectDisposedException();
+            }
+
+            if (count == 0)
+            {
+                return;
+            }
+
+            _channel.SendData(buffer, offset, count);
+            _totalPosition += count;
+        }
+
+        /// <summary>
+        /// Releases the unmanaged resources used by the Stream and optionally releases the managed resources.
+        /// </summary>
+        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
+        protected override void Dispose(bool disposing)
+        {
+            if (!_isDisposed)
+            {
+                _isDisposed = true;
+
+                // Closing the InputStream requires sending EOF.
+                if (disposing && _totalPosition > 0 && _channel?.IsOpen == true)
+                {
+                    _channel.SendEof();
+                }
+            }
+
+            base.Dispose(disposing);
+        }
+
+        /// <summary>
+        /// Gets a value indicating whether the current stream supports reading.
+        /// </summary>
+        /// <returns>
+        /// true if the stream supports reading; otherwise, false.
+        /// </returns>
+        public override bool CanRead
+        {
+            get { return false; }
+        }
+
+        /// <summary>
+        /// Gets a value indicating whether the current stream supports seeking.
+        /// </summary>
+        /// <returns>
+        /// <c>true</c> if the stream supports seeking; otherwise, <c>false</c>.
+        /// </returns>
+        public override bool CanSeek
+        {
+            get { return false; }
+        }
+
+        /// <summary>
+        /// Gets a value indicating whether the current stream supports writing.
+        /// </summary>
+        /// <returns>
+        /// <c>true</c> if the stream supports writing; otherwise, <c>false</c>.
+        /// </returns>
+        public override bool CanWrite
+        {
+            get { return true; }
+        }
+
+        /// <summary>
+        /// Gets the length in bytes of the stream.
+        /// </summary>
+        /// <returns>
+        /// A long value representing the length of the stream in bytes.
+        /// </returns>
+        /// <exception cref="NotSupportedException">A class derived from Stream does not support seeking.</exception>
+        /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
+        public override long Length
+        {
+            get { throw new NotSupportedException(); }
+        }
+
+        /// <summary>
+        /// Gets or sets the position within the current stream.
+        /// </summary>
+        /// <returns>
+        /// The current position within the stream.
+        /// </returns>
+        /// <exception cref="NotSupportedException">The stream does not support seeking.</exception>
+        public override long Position
+        {
+            get { return _totalPosition; }
+            set { throw new NotSupportedException(); }
+        }
+
+        private ObjectDisposedException CreateObjectDisposedException()
+        {
+            return new ObjectDisposedException(GetType().FullName);
+        }
+    }
+}

+ 35 - 1
src/Renci.SshNet/SshCommand.cs

@@ -31,6 +31,7 @@ namespace Renci.SshNet
         private StringBuilder _error;
         private bool _hasError;
         private bool _isDisposed;
+        private ChannelInputStream _inputStream;
 
         /// <summary>
         /// Gets the command text.
@@ -64,6 +65,30 @@ namespace Renci.SshNet
         public Stream ExtendedOutputStream { get; private set; }
 #pragma warning restore CA1859 // Use concrete types when possible for improved performance
 
+        /// <summary>
+        /// Creates and returns the input stream for the command.
+        /// </summary>
+        /// <returns>
+        /// The stream that can be used to transfer data to the command's input stream.
+        /// </returns>
+ #pragma warning disable CA1859 // Use concrete types when possible for improved performance
+        public Stream CreateInputStream()
+#pragma warning restore CA1859 // Use concrete types when possible for improved performance
+        {
+            if (_channel == null)
+            {
+                throw new InvalidOperationException($"The input stream can be used only after calling BeginExecute and before calling EndExecute.");
+            }
+
+            if (_inputStream != null)
+            {
+                throw new InvalidOperationException($"The input stream already exists.");
+            }
+
+            _inputStream = new ChannelInputStream(_channel);
+            return _inputStream;
+        }
+
         /// <summary>
         /// Gets the command execution result.
         /// </summary>
@@ -217,7 +242,6 @@ namespace Renci.SshNet
                     AsyncState = state,
                 };
 
-            // When command re-executed again, create a new channel
             if (_channel is not null)
             {
                 throw new SshException("Invalid operation.");
@@ -252,6 +276,7 @@ namespace Renci.SshNet
 
             _channel = CreateChannel();
             _channel.Open();
+
             _ = _channel.SendExecRequest(CommandText);
 
             return _asyncResult;
@@ -301,6 +326,8 @@ namespace Renci.SshNet
                     throw new ArgumentException("EndExecute can only be called once for each asynchronous operation.");
                 }
 
+                _inputStream?.Close();
+
                 // wait for operation to complete (or time out)
                 WaitOnHandle(_asyncResult.AsyncWaitHandle);
 
@@ -552,6 +579,13 @@ namespace Renci.SshNet
                     _channel = null;
                 }
 
+                var inputStream = _inputStream;
+                if (inputStream != null)
+                {
+                    inputStream.Dispose();
+                    _inputStream = null;
+                }
+
                 var outputStream = OutputStream;
                 if (outputStream != null)
                 {

+ 4 - 2
test/Renci.SshNet.IntegrationTests/Dockerfile.TestServer

@@ -10,7 +10,8 @@ RUN apk update && apk upgrade --no-cache && \
     apk add --no-cache openssh && \
     # install openssh-server-pam to allow for keyboard-interactive authentication
     apk add --no-cache openssh-server-pam && \
-    dos2unix /etc/ssh/* && \
+    # must not use * for dos2unix parameter otherwise it tries to process folders too and fails
+    dos2unix /etc/ssh/ssh*key && \
     chmod 400 /etc/ssh/ssh*key && \
     sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config && \
     sed -i 's/#LogLevel\s*INFO/LogLevel DEBUG3/' /etc/ssh/sshd_config && \
@@ -28,7 +29,8 @@ RUN apk update && apk upgrade --no-cache && \
     adduser -D sshnet && \
     passwd -u sshnet && \
     echo 'sshnet:ssh4ever' | chpasswd && \
-    dos2unix /home/sshnet/.ssh/* && \
+    # must not use * for dos2unix parameter otherwise it tries to process folders too and fails
+    dos2unix /home/sshnet/.ssh/*_key* && \
     chown -R sshnet:sshnet /home/sshnet && \
     chmod -R 700 /home/sshnet/.ssh && \
     chmod -R 644 /home/sshnet/.ssh/authorized_keys && \

+ 89 - 2
test/Renci.SshNet.IntegrationTests/SshClientTests.cs

@@ -17,12 +17,99 @@ namespace Renci.SshNet.IntegrationTests
         [TestMethod]
         public void Echo_Command_with_all_characters()
         {
-            var builder = new StringBuilder();
             var response = _sshClient.RunCommand("echo $'test !@#$%^&*()_+{}:,./<>[];\\|'");
 
             Assert.AreEqual("test !@#$%^&*()_+{}:,./<>[];\\|\n", response.Result);
         }
-    
+
+        [TestMethod]
+        public void Send_InputStream_to_Command()
+        {
+            var inputByteArray = Encoding.UTF8.GetBytes("Hello world!");
+
+            // Make the server echo back the input file with "cat"
+            using (var command = _sshClient.CreateCommand("cat"))
+            {
+                var asyncResult = command.BeginExecute();
+
+                using (var inputStream = command.CreateInputStream())
+                {
+                    inputStream.Write(inputByteArray, 0, inputByteArray.Length);
+                }
+
+                command.EndExecute(asyncResult);
+
+                Assert.AreEqual("Hello world!", command.Result);
+                Assert.AreEqual(string.Empty, command.Error);
+            }
+        }
+
+        [TestMethod]
+        public void Send_InputStream_to_Command_OneByteAtATime()
+        {
+            var inputByteArray = Encoding.UTF8.GetBytes("Hello world!");
+
+            // Make the server echo back the input file with "cat"
+            using (var command = _sshClient.CreateCommand("cat"))
+            {
+                var asyncResult = command.BeginExecute();
+
+                using (var inputStream = command.CreateInputStream())
+                {
+                    for (var i = 0; i < inputByteArray.Length; i++)
+                    {
+                        inputStream.WriteByte(inputByteArray[i]);
+                    }
+                }
+
+                command.EndExecute(asyncResult);
+
+                Assert.AreEqual("Hello world!", command.Result);
+                Assert.AreEqual(string.Empty, command.Error);
+            }
+        }
+
+        [TestMethod]
+        public void Send_InputStream_to_Command_InputStreamNotInUsingBlock_StillWorks()
+        {
+            var inputByteArray = Encoding.UTF8.GetBytes("Hello world!");
+
+            // Make the server echo back the input file with "cat"
+            using (var command = _sshClient.CreateCommand("cat"))
+            {
+                var asyncResult = command.BeginExecute();
+
+                var inputStream = command.CreateInputStream();
+                for (var i = 0; i < inputByteArray.Length; i++)
+                {
+                    inputStream.WriteByte(inputByteArray[i]);
+                }
+
+                command.EndExecute(asyncResult);
+
+                Assert.AreEqual("Hello world!", command.Result);
+                Assert.AreEqual(string.Empty, command.Error);
+            }
+        }
+
+        [TestMethod]
+        public void CreateInputStream_BeforeBeginExecute_ThrowsInvalidOperationException()
+        {
+            var command = _sshClient.CreateCommand("ls");
+
+            Assert.ThrowsException<InvalidOperationException>(command.CreateInputStream);
+        }
+
+        [TestMethod]
+        public void CreateInputStream_AfterEndExecute_ThrowsInvalidOperationException()
+        {
+            var command = _sshClient.CreateCommand("ls");
+            var asyncResult = command.BeginExecute();
+            command.EndExecute(asyncResult);
+
+            Assert.ThrowsException<InvalidOperationException>(command.CreateInputStream);
+        }
+
         public void Dispose()
         {
             _sshClient.Disconnect();

+ 1 - 0
test/Renci.SshNet.TestTools.OpenSSH/SshdConfig.cs

@@ -385,6 +385,7 @@ namespace Renci.SshNet.TestTools.OpenSSH
                 case "AuthorizedKeysFile":
                 case "PasswordAuthentication":
                 case "GatewayPorts":
+                case "Include":
                     break;
                 default:
                     throw new NotSupportedException($"Global option '{name}' is not supported.");