-
Notifications
You must be signed in to change notification settings - Fork 234
Shelldriver: make destination authorized keys file an arguement #1732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pmelange
wants to merge
1
commit into
labgrid-project:master
Choose a base branch
from
pmelange:25.0.1-shelldriver_authorized_keys_file
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+26
−18
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| # pylint: disable=unused-argument | ||
| """The ShellDriver provides the CommandProtocol, ConsoleProtocol and | ||
| InfoProtocol on top of a SerialPort.""" | ||
| import os | ||
| import io | ||
| import re | ||
| import shlex | ||
|
|
@@ -34,6 +35,7 @@ class ShellDriver(CommandMixin, Driver, CommandProtocol, FileTransferProtocol): | |
| username (str): username to login with | ||
| password (str): password to login with | ||
| keyfile (str): keyfile to bind mount over users authorized keys | ||
| dest_authorized_keys (str): optional, default="~/.ssh/authorized_keys", filename of the authorized_keys file | ||
| login_timeout (int): optional, timeout for login prompt detection | ||
| console_ready (regex): optional, pattern used by the kernel to inform the user that a | ||
| console can be activated by pressing enter. | ||
|
|
@@ -49,6 +51,7 @@ class ShellDriver(CommandMixin, Driver, CommandProtocol, FileTransferProtocol): | |
| username = attr.ib(validator=attr.validators.instance_of(str)) | ||
| password = attr.ib(default=None, validator=attr.validators.optional(attr.validators.instance_of(str))) | ||
| keyfile = attr.ib(default="", validator=attr.validators.instance_of(str)) | ||
| dest_authorized_keys = attr.ib(default="~/.ssh/authorized_keys", validator=attr.validators.instance_of(str)) | ||
| login_timeout = attr.ib(default=60, validator=attr.validators.instance_of(int)) | ||
| console_ready = attr.ib(default="", validator=attr.validators.instance_of(str)) | ||
| await_login_timeout = attr.ib(default=2, validator=attr.validators.instance_of(int)) | ||
|
|
@@ -72,7 +75,7 @@ def on_activate(self): | |
| if self.target.env: | ||
| keyfile_path = self.target.env.config.resolve_path(self.keyfile) | ||
|
|
||
| self._put_ssh_key(keyfile_path) | ||
| self._put_ssh_key(keyfile_path, self.dest_authorized_keys) | ||
|
|
||
| def on_deactivate(self): | ||
| self._status = 0 | ||
|
|
@@ -217,8 +220,8 @@ def _write_key(self, keyline, dest): | |
| self._run_check(f'echo -n "{part}" >> {dest}') | ||
| self._run_check(f'echo "" >> {dest}') | ||
|
|
||
| @step(args=['keyfile_path']) | ||
| def _put_ssh_key(self, keyfile_path): | ||
| @step(args=['keyfile_path', 'dest_authorized_keys']) | ||
| def _put_ssh_key(self, keyfile_path, dest_authorized_keys): | ||
| """Upload an SSH Key to a target""" | ||
| regex = re.compile( | ||
| r"""ssh-(rsa|ed25519) | ||
|
|
@@ -236,7 +239,8 @@ def _put_ssh_key(self, keyfile_path): | |
| f"Could not parse SSH-Key from file: {keyfile}" | ||
| ) | ||
| self.logger.debug("Read Key: %s", new_key) | ||
| auth_keys, _, read_keys = self._run("cat ~/.ssh/authorized_keys") | ||
| dest_authorized_keys_dir = os.path.dirname(dest_authorized_keys) | ||
| auth_keys, _, read_keys = self._run(f"""cat {self.dest_authorized_keys}""") | ||
| self.logger.debug("Exitcode trying to read keys: %s, keys: %s", read_keys, auth_keys) | ||
| result = [] | ||
| _, _, test_write = self._run("touch ~/.test") | ||
|
|
@@ -258,35 +262,37 @@ def _put_ssh_key(self, keyfile_path): | |
|
|
||
| if test_write == 0 and read_keys == 0: | ||
| self.logger.debug("Key not on target and writeable, concatenating...") | ||
| self._write_key(keyline, "~/.ssh/authorized_keys") | ||
| self._write_key(keyline, dest_authorized_keys) | ||
| self._run_check("rm ~/.test") | ||
| return | ||
|
|
||
| if test_write == 0: | ||
| self.logger.debug("Key not on target, testing for .ssh directory") | ||
| _, _, ssh_dir = self._run("[ -d ~/.ssh/ ]") | ||
| self.logger.debug("Key not on target, testing for % directory", dest_authorized_keys_dir) | ||
| _, _, ssh_dir = self._run(f"""[ -d {dest_authorized_keys_dir} ]""") | ||
| if ssh_dir != 0: | ||
| self.logger.debug("~/.ssh did not exist, creating") | ||
| self._run("mkdir ~/.ssh/") | ||
| self._run_check("chmod 700 ~/.ssh/") | ||
| self.logger.debug("Creating ~/.ssh/authorized_keys") | ||
| self._run_check("touch ~/.ssh/authorized_keys") | ||
| self._write_key(keyline, "~/.ssh/authorized_keys") | ||
| self.logger.debug(" % did not exits, creating", dest_authorized_keys_dir) | ||
| self._run(f"""mkdir -p {dest_authorized_keys_dir}""") | ||
| self._run_check(f"""chmod 700 {dest_authorized_keys_dir}""") | ||
| self.logger.debug("Creating %s", dest_authorized_keys) | ||
| self._write_key(keyline, dest_authorized_keys) | ||
| self._run_check("rm ~/.test") | ||
| return | ||
|
|
||
| self.logger.debug("Key not on target and not writeable, using bind mount...") | ||
| self._run_check('mkdir -p -m 700 /tmp/labgrid-ssh/') | ||
| self._run("cp -a ~/.ssh/* /tmp/labgrid-ssh/") | ||
| self._run(f"""cp -a {dest_authorized_keys_dir}/* /tmp/labgrid-ssh/""") | ||
| self._write_key(keyline, "/tmp/labgrid-ssh/authorized_keys") | ||
| self._run_check('chmod 600 /tmp/labgrid-ssh/authorized_keys') | ||
| out, err, exitcode = self._run('mount --bind /tmp/labgrid-ssh/ ~/.ssh/') | ||
| out, err, exitcode = self._run(f"""mount --bind /tmp/labgrid-ssh/ {dest_authorized_keys_dir}""") | ||
| if exitcode != 0: | ||
| self.logger.warning("Could not bind mount ~/.ssh directory: %s %s", out, err) | ||
| self.logger.warning("Could not bind mount %s directory: %s %s", | ||
| dest_authorized_keys_dir, out, err) | ||
|
|
||
| @Driver.check_active | ||
| def put_ssh_key(self, keyfile_path): | ||
| self._put_ssh_key(keyfile_path) | ||
| def put_ssh_key(self, keyfile_path, dest_authorized_keys = None): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While moving the documentation comment, please extend it for all arguments of the function. |
||
| if dest_authorized_keys is None: | ||
| dest_authorized_keys = self.dest_authorized_keys | ||
| self._put_ssh_key(keyfile_path, dest_authorized_keys) | ||
|
|
||
| def _xmodem_getc(self, size, timeout=10): | ||
| """ called by the xmodem.XMODEM instance to read protocol data from the console """ | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This documentation comment should be moved with the function.