Skip to content

Commit 0eebd4b

Browse files
committed
ShellDriver: add optional arg dest_authorized_keys
Add an addition optional arg "dest_authorized_keys" with the default value of ```~/.ssh/authorized_keys```, An example where this might be used is when an embedded system uses dropbear as it's ssh server daemon. In such a case, the authorized_keys file is ```/etc/dropbear/authorized_keys``` Additionally, the ```put_key_file``` takes an optional ```dest_authorized_keys``` function parameter which defaults to the above arg. Fixes: ##1626 Signed-off-by: Perry Melange <[email protected]>
1 parent 22f4b81 commit 0eebd4b

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

doc/configuration.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,8 @@ Arguments:
17961796
Can be an empty string.
17971797
- keyfile (str): optional, keyfile to upload after login, making the
17981798
`SSHDriver`_ usable
1799+
- dest_authorized_keys (str): optional, default="~/.ssh/authorized_keys",
1800+
filename of the authorized_keys file
17991801
- login_timeout (int, default=60): timeout for login prompt detection in
18001802
seconds
18011803
- await_login_timeout (int, default=2): time in seconds of silence that needs

labgrid/driver/shelldriver.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# pylint: disable=unused-argument
22
"""The ShellDriver provides the CommandProtocol, ConsoleProtocol and
33
InfoProtocol on top of a SerialPort."""
4+
import os
45
import io
56
import re
67
import shlex
@@ -34,6 +35,7 @@ class ShellDriver(CommandMixin, Driver, CommandProtocol, FileTransferProtocol):
3435
username (str): username to login with
3536
password (str): password to login with
3637
keyfile (str): keyfile to bind mount over users authorized keys
38+
dest_authorized_keys (str): optional, default="~/.ssh/authorized_keys", filename of the authorized_keys file
3739
login_timeout (int): optional, timeout for login prompt detection
3840
console_ready (regex): optional, pattern used by the kernel to inform the user that a
3941
console can be activated by pressing enter.
@@ -49,6 +51,7 @@ class ShellDriver(CommandMixin, Driver, CommandProtocol, FileTransferProtocol):
4951
username = attr.ib(validator=attr.validators.instance_of(str))
5052
password = attr.ib(default=None, validator=attr.validators.optional(attr.validators.instance_of(str)))
5153
keyfile = attr.ib(default="", validator=attr.validators.instance_of(str))
54+
dest_authorized_keys = attr.ib(default="~/.ssh/authorized_keys", validator=attr.validators.instance_of(str))
5255
login_timeout = attr.ib(default=60, validator=attr.validators.instance_of(int))
5356
console_ready = attr.ib(default="", validator=attr.validators.instance_of(str))
5457
await_login_timeout = attr.ib(default=2, validator=attr.validators.instance_of(int))
@@ -72,7 +75,7 @@ def on_activate(self):
7275
if self.target.env:
7376
keyfile_path = self.target.env.config.resolve_path(self.keyfile)
7477

75-
self._put_ssh_key(keyfile_path)
78+
self._put_ssh_key(keyfile_path, self.dest_authorized_keys)
7679

7780
def on_deactivate(self):
7881
self._status = 0
@@ -217,8 +220,8 @@ def _write_key(self, keyline, dest):
217220
self._run_check(f'echo -n "{part}" >> {dest}')
218221
self._run_check(f'echo "" >> {dest}')
219222

220-
@step(args=['keyfile_path'])
221-
def _put_ssh_key(self, keyfile_path):
223+
@step(args=['keyfile_path', 'dest_authorized_keys'])
224+
def _put_ssh_key(self, keyfile_path, dest_authorized_keys):
222225
"""Upload an SSH Key to a target"""
223226
regex = re.compile(
224227
r"""ssh-(rsa|ed25519)
@@ -236,7 +239,8 @@ def _put_ssh_key(self, keyfile_path):
236239
f"Could not parse SSH-Key from file: {keyfile}"
237240
)
238241
self.logger.debug("Read Key: %s", new_key)
239-
auth_keys, _, read_keys = self._run("cat ~/.ssh/authorized_keys")
242+
dest_authorized_keys_dir = os.path.dirname(dest_authorized_keys)
243+
auth_keys, _, read_keys = self._run(f"""cat {self.dest_authorized_keys}""")
240244
self.logger.debug("Exitcode trying to read keys: %s, keys: %s", read_keys, auth_keys)
241245
result = []
242246
_, _, test_write = self._run("touch ~/.test")
@@ -258,35 +262,37 @@ def _put_ssh_key(self, keyfile_path):
258262

259263
if test_write == 0 and read_keys == 0:
260264
self.logger.debug("Key not on target and writeable, concatenating...")
261-
self._write_key(keyline, "~/.ssh/authorized_keys")
265+
self._write_key(keyline, dest_authorized_keys)
262266
self._run_check("rm ~/.test")
263267
return
264268

265269
if test_write == 0:
266-
self.logger.debug("Key not on target, testing for .ssh directory")
267-
_, _, ssh_dir = self._run("[ -d ~/.ssh/ ]")
270+
self.logger.debug("Key not on target, testing for % directory", dest_authorized_keys_dir)
271+
_, _, ssh_dir = self._run(f"""[ -d {dest_authorized_keys_dir} ]""")
268272
if ssh_dir != 0:
269-
self.logger.debug("~/.ssh did not exist, creating")
270-
self._run("mkdir ~/.ssh/")
271-
self._run_check("chmod 700 ~/.ssh/")
272-
self.logger.debug("Creating ~/.ssh/authorized_keys")
273-
self._run_check("touch ~/.ssh/authorized_keys")
274-
self._write_key(keyline, "~/.ssh/authorized_keys")
273+
self.logger.debug(" % did not exits, creating", dest_authorized_keys_dir)
274+
self._run(f"""mkdir -p {dest_authorized_keys_dir}""")
275+
self._run_check(f"""chmod 700 {dest_authorized_keys_dir}""")
276+
self.logger.debug("Creating %", dest_authorized_keys)
277+
self._write_key(keyline, dest_authorized_keys)
275278
self._run_check("rm ~/.test")
276279
return
277280

278281
self.logger.debug("Key not on target and not writeable, using bind mount...")
279282
self._run_check('mkdir -p -m 700 /tmp/labgrid-ssh/')
280-
self._run("cp -a ~/.ssh/* /tmp/labgrid-ssh/")
283+
self._run(f"""cp -a {dest_authorized_keys_dir}/* /tmp/labgrid-ssh/""")
281284
self._write_key(keyline, "/tmp/labgrid-ssh/authorized_keys")
282285
self._run_check('chmod 600 /tmp/labgrid-ssh/authorized_keys')
283-
out, err, exitcode = self._run('mount --bind /tmp/labgrid-ssh/ ~/.ssh/')
286+
out, err, exitcode = self._run(f"""mount --bind /tmp/labgrid-ssh/ {dest_authorized_keys_dir}""")
284287
if exitcode != 0:
285-
self.logger.warning("Could not bind mount ~/.ssh directory: %s %s", out, err)
288+
self.logger.warning("Could not bind mount % directory: % %",
289+
dest_authorized_keys_dir, out, err)
286290

287291
@Driver.check_active
288-
def put_ssh_key(self, keyfile_path):
289-
self._put_ssh_key(keyfile_path)
292+
def put_ssh_key(self, keyfile_path, dest_authorized_keys = None):
293+
if dest_authorized_keys is None:
294+
dest_authorized_keys = self.dest_authorized_keys
295+
self._put_ssh_key(keyfile_path, dest_authorized_keys)
290296

291297
def _xmodem_getc(self, size, timeout=10):
292298
""" called by the xmodem.XMODEM instance to read protocol data from the console """

0 commit comments

Comments
 (0)