1- import pysftp
1+ import io
22
3+ import paramiko
34from ted_sws import config
45from ted_sws .notice_publisher .adapters .sftp_publisher_abc import SFTPPublisherABC
56
@@ -9,36 +10,43 @@ class SFTPPublisher(SFTPPublisherABC):
910
1011 """
1112
12- def __init__ (self , hostname : str = None , username : str = None , password : str = None , port : int = None ):
13+ def __init__ (self , hostname : str = None , username : str = None , password : str = None , port : int = None ,
14+ private_key : str = None , private_key_passphrase : str = None ):
1315 """Constructor Method"""
1416 self .hostname = hostname if hostname else config .SFTP_PUBLISH_HOST
1517 self .username = username if username else config .SFTP_PUBLISH_USER
1618 self .password = password if password else config .SFTP_PUBLISH_PASSWORD
1719 self .port = port if port else config .SFTP_PUBLISH_PORT
1820 self .connection = None
1921 self .is_connected = False
22+ self .private_key = None
23+ self .private_key_passphrase = private_key_passphrase if private_key_passphrase else config .SFTP_PRIVATE_KEY_PASSPHRASE
24+ private_key = private_key if private_key else config .SFTP_PRIVATE_KEY
25+ if private_key :
26+ self .private_key = paramiko .RSAKey .from_private_key (io .StringIO (private_key ),
27+ password = self .private_key_passphrase )
28+
29+ self ._sftp = None
30+ self ._ssh = None
2031
2132 def connect (self ):
2233 """Connects to the sftp server and returns the sftp connection object"""
2334
24- cnopts = pysftp .CnOpts ()
25- # TODO: to be checked/removed when SSL will be setup
26- cnopts .hostkeys = None
27- # Get the sftp connection object
28- self .connection = pysftp .Connection (
29- host = self .hostname ,
30- username = self .username ,
31- password = self .password ,
32- port = self .port ,
33- cnopts = cnopts
34- )
35-
35+ ssh_client = paramiko .SSHClient ()
36+ ssh_client .set_missing_host_key_policy (paramiko .AutoAddPolicy ())
37+ ssh_client .connect (self .hostname , username = self .username ,
38+ pkey = self .private_key , port = self .port )
39+ self ._ssh = ssh_client
40+ self ._sftp = ssh_client .open_sftp ()
3641 self .is_connected = True
3742
3843 def disconnect (self ):
3944 """Closes the sftp connection"""
4045 if self .is_connected :
41- self .connection .close ()
46+ self ._sftp .close ()
47+ self ._ssh .close ()
48+ self ._sftp = None
49+ self ._ssh = None
4250 self .is_connected = False
4351
4452 def __del__ (self ):
@@ -48,12 +56,25 @@ def publish(self, source_path: str, remote_path: str) -> bool:
4856 """
4957 Publish file_content to the sftp server remote path.
5058 """
51- self .connection .put (source_path , remote_path )
59+ self ._sftp .put (source_path , remote_path )
5260 return True
5361
5462 def remove (self , remote_path : str ) -> bool :
5563 """
5664
5765 """
58- self .connection . unlink (remote_path )
66+ self ._sftp . remove (remote_path )
5967 return True
68+
69+ def exists (self , remote_path : str ) -> bool :
70+ """
71+ Check if remote_path exists.
72+ :param remote_path:
73+ :return:
74+ """
75+ if self .is_connected :
76+ try :
77+ self ._sftp .stat (remote_path )
78+ except IOError :
79+ return False
80+ return True
0 commit comments