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

Allow sudofn injection into container #227

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,19 @@
c.OSVersion = &o
}

c.configureSudo()
if c.sudofunc == nil {
c.discoverSudo()
}

return nil
}

// SetSudofn inject a sudofn into the connection before Connect()
// @NOTE this will avoid sudo detection if done before Connect()
func (c *Connection) SetSudofn(f func(string) string) {
c.sudofunc = f
}

func sudoNoop(cmd string) string {
return cmd
}
Expand Down Expand Up @@ -287,21 +295,21 @@
return cmd
}

func (c *Connection) configureSudo() {
func (c *Connection) discoverSudo() {
if !c.IsWindows() {
if c.Exec(`[ "$(id -u)" = 0 ]`) == nil {
// user is already root
c.sudofunc = sudoNoop
c.SetSudofn(sudoNoop)
return
}
if c.Exec(`sudo -n -l`) == nil {
// user has passwordless sudo
c.sudofunc = sudoSudo
c.SetSudofn(sudoSudo)
return
}
if c.Exec(`doas -n -- "${SHELL-sh}" -c true`) == nil {
// user has passwordless doas
c.sudofunc = sudoDoas
c.SetSudofn(sudoDoas)
}
return
}
Expand Down Expand Up @@ -382,8 +390,8 @@

// Upload copies a file from a local path src to the remote host path dst. For
// smaller files you should probably use os.WriteFile
func (c *Connection) Upload(src, dst string, opts ...exec.Option) error {

Check failure on line 393 in connection.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary leading newline (whitespace)

Check failure on line 394 in connection.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gofumpt`-ed (gofumpt)
if err := c.checkConnected(); err != nil {
return err
}
Expand Down
Loading