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

[#36] SSHKit.user errors because sudo is missing #51

Merged

Conversation

pmeinhardt
Copy link
Contributor

@pmeinhardt pmeinhardt commented May 9, 2017

Update the functional tests to allow the test/login user to run sudo in order to switch user/group.

TODOs:


[{:ok, output, status}] = SSHKit.run(context, "does-not-exist")
assert status == 127
output = stderr(output)
assert output =~ "'does-not-exist': No such file or directory"
assert stderr(output) =~ "'does-not-exist': No such file or directory"
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@tag boot: 1
test "user", %{hosts: [host]} do
adduser(host, "despicable_me")
add_user_to_group(host, host.user, "danger")
Copy link
Contributor

Choose a reason for hiding this comment

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

why does the user need to be in a group?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The group is set up in the Dockerfile in order to allow passwordless sudoing, required for user switching. Hence also the name "danger".

Maybe you've got a different idea though?

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, nope. maybe renaming the group from danger to something more describing would help?
sudo_enabled or something?

@@ -46,6 +46,7 @@ defmodule SSHKit.FunctionalCase do

def init(host) do
adduser(host, @user)
add_user_to_group(host, @user, "wheel")
Copy link
Contributor

Choose a reason for hiding this comment

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

here again: why can't the user live without a group by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, this can be removed.

pmeinhardt added 3 commits May 9, 2017 13:11
- Install "sudo" package for the Alpine Docker image
- Allow users of system group "wheel" to execute any command
- Add "danger" group whose users can run sudo without password
- Add test user to the "danger" group to allow running sudo
- Use `id -un` over obsoleted `whoami`
- Check exit codes in `SSHKit.umask/2` test file creation
- Use sudo for both switching user and group in a single command
- Quick version of `SSHKit.Utils.shellquote/1`, improved one follows
@pmeinhardt pmeinhardt force-pushed the feature/36-sshkit-user-errors-because-sudo-is-missing branch from 89e4a57 to e11da3f Compare May 9, 2017 11:11
defp sudo(command, nil, nil), do: command
defp sudo(command, username, nil), do: "sudo -n -u #{username} -- sh -c #{shellquote(command)}"
defp sudo(command, nil, groupname), do: "sudo -n -g #{groupname} -- sh -c #{shellquote(command)}"
defp sudo(command, username, groupname), do: "sudo -n -u #{username} -g #{groupname} -- sh -c #{shellquote(command)}"
Copy link
Contributor

Choose a reason for hiding this comment

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

only this match of sudo/3 is ever used, right?

Copy link
Contributor Author

@pmeinhardt pmeinhardt May 11, 2017

Choose a reason for hiding this comment

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

Errr, nope. Unless I missed something, the context may hold any combination of :user and :group being present or not present.

Example:

context =
  hosts
  |> SSHKit.context()
  |> SSHKit.user("tessi")
  |> SSHKit.path("/var/log")

SSHKit.Context.build(context, "ls")

Here, context has a user, but no group set.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, you're right. i just misread the code yesterday evening. let's act as if i never wrote this comment ;)

@@ -3,7 +3,7 @@ defmodule SSHKit.Utils do

def shellescape(value), do: value

def shellquote(value), do: value
def shellquote(value), do: "'#{value}'" # TODO: Proper quoting
Copy link
Contributor

Choose a reason for hiding this comment

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

what could possibly go wrong :trollface:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm glad you included the :trollface:.

That's exactly the reason why I included the "TODO", to make the Credo checks fail until I've added proper quoting on this branch 😁

@@ -9,7 +9,7 @@ defmodule SSHKit.SSHFunctionalTest do
test "opens a connection with username and password", %{hosts: [host]} do
options = [port: host.port, user: host.user, password: host.password]
{:ok, conn} = SSH.connect(host.ip, Keyword.merge(@defaults, options))
{:ok, data, status} = SSH.run(conn, "whoami")
{:ok, data, status} = SSH.run(conn, "id -un")
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@pmeinhardt
Copy link
Contributor Author

Thanks a lot for your feedback @tessi, I've updated the PR description with a checklist for myself to address the remaining issues.

@tessi
Copy link
Contributor

tessi commented May 12, 2017

Thanks! The shellquote might even be done in a separate PR. The only thing left IMO for this PR would be a better name for the sudo-group

@pmeinhardt pmeinhardt merged commit 9705709 into master May 14, 2017
@pmeinhardt pmeinhardt deleted the feature/36-sshkit-user-errors-because-sudo-is-missing branch May 14, 2017 08:51
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.

2 participants