Skip to content

Commit 84cd711

Browse files
flxztEmantor
authored andcommitted
fix: handling of empty password in SSHDriver and NetworkService
Signed-off-by: Felix Zwettler <Felix.Zwettler@duagon.com>
1 parent df51556 commit 84cd711

3 files changed

Lines changed: 11 additions & 8 deletions

File tree

doc/configuration.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ These and the sudo configuration needs to be prepared by the administrator.
683683
Arguments:
684684
- address (str): hostname of the remote system
685685
- username (str): username used by SSH
686-
- password (str, default=""): password used by SSH
686+
- password (str, default=None): optional, password used by SSH
687687
- port (int, default=22): port used by SSH
688688

689689
Used by:

labgrid/driver/sshdriver.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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\necho " + shlex.quote(self._get_password()))
149+
f.write("#!/bin/sh\necho " + 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):

labgrid/resource/networkservice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
class NetworkService(Resource):
1010
address = attr.ib(validator=attr.validators.instance_of(str))
1111
username = attr.ib(validator=attr.validators.instance_of(str))
12-
password = attr.ib(default='', validator=attr.validators.instance_of(str))
12+
password = attr.ib(default=None, validator=attr.validators.optional(attr.validators.instance_of(str)))
1313
port = attr.ib(default=22, validator=attr.validators.instance_of(int))

0 commit comments

Comments
 (0)