From 692489cc50c8025ede1646627a7a583a4feb3798 Mon Sep 17 00:00:00 2001
From: yayogev <yaronyogev@gmail.com>
Date: Thu, 31 Aug 2017 16:45:23 +0300
Subject: US2876 handle SSH errors

ido not stop, but report as 'completed with errors'
if there were any errors in SSH calls

Change-Id: Ice7e6c4324686adc2d9eec27a9b6187f0fe6808f
Signed-off-by: yayogev <yaronyogev@gmail.com>
---
 app/utils/constants.py      |  1 +
 app/utils/ssh_connection.py | 32 ++++++++++++++++++--------------
 2 files changed, 19 insertions(+), 14 deletions(-)

(limited to 'app/utils')

diff --git a/app/utils/constants.py b/app/utils/constants.py
index 7aa0343..44850b3 100644
--- a/app/utils/constants.py
+++ b/app/utils/constants.py
@@ -22,6 +22,7 @@ class ScanStatus(StringEnum):
     PENDING = "pending"
     RUNNING = "running"
     COMPLETED = "completed"
+    COMPLETED_WITH_ERRORS = "completed_with_errors"
     FAILED = "failed"
 
 
diff --git a/app/utils/ssh_connection.py b/app/utils/ssh_connection.py
index b0f202a..e9dd39a 100644
--- a/app/utils/ssh_connection.py
+++ b/app/utils/ssh_connection.py
@@ -12,7 +12,10 @@ import os
 import paramiko
 
 from utils.binary_converter import BinaryConverter
+from discover.scan_error import ScanError
 
+class SshError(Exception):
+    pass
 
 class SshConnection(BinaryConverter):
     connections = {}
@@ -117,6 +120,7 @@ class SshConnection(BinaryConverter):
                                     else self.DEFAULT_PORT,
                                     password=self.pwd, timeout=30)
         else:
+            port = None
             try:
                 port = self.port if self.port is not None else self.DEFAULT_PORT
                 self.ssh_client.connect(self.host,
@@ -146,12 +150,12 @@ class SshConnection(BinaryConverter):
             err_lines = [l for l in err.splitlines()
                          if 'Loaded plugin: ' not in l]
             if err_lines:
-                self.log.error("CLI access: \n" +
-                               "Host: {}\nCommand: {}\nError: {}\n".
-                               format(self.host, cmd, err))
+                msg = "CLI access: \nHost: {}\nCommand: {}\nError: {}\n"
+                msg = msg.format(self.host, cmd, err)
+                self.log.error(msg)
                 stderr.close()
                 stdout.close()
-                return ""
+                raise SshError(msg)
         ret = self.binary2str(stdout.read())
         stderr.close()
         stdout.close()
@@ -165,11 +169,11 @@ class SshConnection(BinaryConverter):
         try:
             self.ftp.put(local_path, remote_path)
         except IOError as e:
-            self.log.error('SFTP copy_file failed to copy file: ' +
-                           'local: ' + local_path +
-                           ', remote host: ' + self.host +
-                           ', error: ' + str(e))
-            return str(e)
+            msg = 'SFTP copy_file failed to copy file: ' \
+                  'local: {}, remote host: {}, error: {}' \
+                .format(local_path, self.host, str(e))
+            self.log.error(msg)
+            raise SshError(msg)
         try:
             remote_file = self.ftp.file(remote_path, 'a+')
         except IOError as e:
@@ -201,11 +205,11 @@ class SshConnection(BinaryConverter):
         try:
             self.ftp.get(remote_path, local_path)
         except IOError as e:
-            self.log.error('SFTP copy_file_from_remote failed to copy file: '
-                           'remote host: {}, '
-                           'remote_path: {}, local: {}, error: {}'
-                           .format(self.host, remote_path, local_path, str(e)))
-            return str(e)
+            msg = 'SFTP copy_file_from_remote failed to copy file: ' \
+                  'remote host: {}, remote_path: {}, local: {}, error: {}'
+            msg = msg.format(self.host, remote_path, local_path, str(e))
+            self.log.error(msg)
+            raise SshError(msg)
         self.log.info('SFTP copy_file_from_remote success: host={},{} -> {}'.
                       format(self.host, remote_path, local_path))
         return ''
-- 
cgit 1.2.3-korg