@@ -36,7 +36,7 @@ class SSHDriver(CommandMixin, Driver, CommandProtocol, FileTransferProtocol):
3636 explicit_sftp_mode = attr .ib (default = False , validator = attr .validators .instance_of (bool ))
3737 explicit_scp_mode = attr .ib (default = False , validator = attr .validators .instance_of (bool ))
3838 username = attr .ib (default = "" , validator = attr .validators .instance_of (str ))
39- password = attr .ib (default = "" , validator = attr .validators .instance_of (str ))
39+ password = attr .ib (default = None , validator = attr .validators .optional ( attr . validators . instance_of (str ) ))
4040
4141 def __attrs_post_init__ (self ):
4242 super ().__attrs_post_init__ ()
@@ -57,7 +57,9 @@ def _get_username(self):
5757
5858 def _get_password (self ):
5959 """Get the password from this class or from NetworkService"""
60- return self .password or self .networkservice .password
60+ if self .password is not None :
61+ return self .password
62+ return self .networkservice .password
6163
6264 def on_activate (self ):
6365 self .ssh_prefix = ["-o" , "LogLevel=ERROR" ]
@@ -66,7 +68,7 @@ def on_activate(self):
6668 if self .target .env :
6769 keyfile_path = self .target .env .config .resolve_path (self .keyfile )
6870 self .ssh_prefix += ["-i" , keyfile_path ]
69- if not self ._get_password ():
71+ if self ._get_password () is None :
7072 self .ssh_prefix += ["-o" , "PasswordAuthentication=no" ]
7173
7274 self .control = self ._start_own_master ()
@@ -136,14 +138,15 @@ def _start_own_master_once(self, timeout):
136138
137139 env = os .environ .copy ()
138140 pass_file = ''
139- if self ._get_password ():
141+ password = self ._get_password ()
142+ if password is not None :
140143 fd , pass_file = tempfile .mkstemp ()
141144 os .fchmod (fd , stat .S_IRWXU )
142145 #with openssh>=8.4 SSH_ASKPASS_REQUIRE can be used to force SSH_ASK_PASS
143146 #openssh<8.4 requires the DISPLAY var and a detached process with start_new_session=True
144147 env = {'SSH_ASKPASS' : pass_file , 'DISPLAY' :'' , 'SSH_ASKPASS_REQUIRE' :'force' }
145148 with open (fd , 'w' ) as f :
146- f .write ("#!/bin/sh\n echo " + shlex .quote (self . _get_password () ))
149+ f .write ("#!/bin/sh\n echo " + shlex .quote (password ))
147150
148151 self .process = subprocess .Popen (args , env = env ,
149152 stdout = subprocess .PIPE ,
@@ -180,7 +183,7 @@ def _start_own_master_once(self, timeout):
180183 f"Subprocess timed out [{ subprocess_timeout } s] while executing { args } " ,
181184 )
182185 finally :
183- if self ._get_password () and os .path .exists (pass_file ):
186+ if self ._get_password () is not None and os .path .exists (pass_file ):
184187 os .remove (pass_file )
185188
186189 if not os .path .exists (control ):
0 commit comments