Skip to content
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

SSHDriver: implement user switching via su #1220

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Emantor
Copy link
Member

@Emantor Emantor commented Jun 25, 2023

Description

Implement user switching for the SSHDriver via su.

Checklist

  • PR has been tested

Copy link
Member

@Bastian-Krause Bastian-Krause left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message misses su_prompt.

The configuration docs need to be updated and the use case should be described there as well. Maybe something like: Targets might not allow direct SSH access for certain users (such as root), the SSHDriver can log in as another user and use su.

labgrid/driver/sshdriver.py Outdated Show resolved Hide resolved
labgrid/driver/sshdriver.py Outdated Show resolved Hide resolved
labgrid/driver/sshdriver.py Show resolved Hide resolved
labgrid/driver/sshdriver.py Outdated Show resolved Hide resolved
labgrid/driver/sshdriver.py Outdated Show resolved Hide resolved
labgrid/driver/sshdriver.py Outdated Show resolved Hide resolved
labgrid/driver/sshdriver.py Outdated Show resolved Hide resolved
labgrid/driver/sshdriver.py Show resolved Hide resolved
examples/ssh-su-example/conf.yaml Outdated Show resolved Hide resolved
Copy link
Member

@Bastian-Krause Bastian-Krause left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message misses su_prompt.

The configuration docs need to be updated and the use case should be described there as well. Maybe something like: Targets might not allow direct SSH access for certain users (such as root), the SSHDriver can log in as another user and use su.

These points were not addressed, yet.

doc/configuration.rst Outdated Show resolved Hide resolved
doc/configuration.rst Outdated Show resolved Hide resolved
doc/configuration.rst Outdated Show resolved Hide resolved
labgrid/driver/sshdriver.py Outdated Show resolved Hide resolved
labgrid/driver/sshdriver.py Outdated Show resolved Hide resolved
labgrid/driver/sshdriver.py Outdated Show resolved Hide resolved
labgrid/driver/sshdriver.py Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Aug 1, 2023

Codecov Report

Patch coverage: 33.3% and project coverage change: -0.1% ⚠️

Comparison is base (4bfdc59) 62.9% compared to head (6742131) 62.9%.
Report is 9 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master   #1220     +/-   ##
========================================
- Coverage    62.9%   62.9%   -0.1%     
========================================
  Files         161     160      -1     
  Lines       11861   11897     +36     
========================================
+ Hits         7470    7485     +15     
- Misses       4391    4412     +21     
Files Changed Coverage Δ
labgrid/driver/sshdriver.py 52.5% <33.3%> (-3.8%) ⬇️

... and 3 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

doc/configuration.rst Outdated Show resolved Hide resolved
labgrid/driver/sshdriver.py Outdated Show resolved Hide resolved
labgrid/driver/sshdriver.py Outdated Show resolved Hide resolved
Comment on lines +273 to +275
comout, comerr = sub.communicate(timeout=timeout)
stdout += comout
if not self.stderr_merge:
stderr += comerr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this simply..

stdout, stderr = sub.communicate(timeout=timeout)

..as it was before?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in case we are manging the password, we wan't to prepend the additional bytes we might have read after password input.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still having problems understanding this:

Why is the comout variable needed? Couldn't this be stdout directly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use stdout directly, the previous contents will be overwritten if we read additional bytes after password input.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is..

stdout = b""
comout, comerr = sub.communicate(timeout=timeout)
stdout += comout

..any different than..

stdout, comerr = sub.communicate(timeout=timeout)

..? What am I missing here?

Add two new attributes to the driver which will use su to switch to a
user to run a command. The su_password is required for this feature to
be used, su_username only needs to be set if another user than root
should be switched to.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
Co-developed-by: Jan Luebbe <jlu@pengutronix.de>
This avoids potential SSH log messages from ending up in the command output,
especially when using stderr_merge.

Signed-off-by: Jan Luebbe <jlu@pengutronix.de>
Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
Comment on lines +273 to +275
comout, comerr = sub.communicate(timeout=timeout)
stdout += comout
if not self.stderr_merge:
stderr += comerr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still having problems understanding this:

Why is the comout variable needed? Couldn't this be stdout directly?

output = self.handle_password(sub.stdout, sub.stdin, marker)
sub.stdin.close()
self.logger.debug("su leftover output: %s", output)
stderr += output
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this appended to stderr? Why does this have to be error output?

self.logger.debug("Sending command: %s", complete_cmd)
if self.stderr_merge:
stderr_pipe = subprocess.STDOUT
else:
stderr_pipe = subprocess.PIPE
stdin = subprocess.PIPE if self.su_password else None
stdout, stderr = b"", b""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work with..

if stderr is None:

..below.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants