Skip to content

Commit

Permalink
Allow sudofn injection into container
Browse files Browse the repository at this point in the history
- allow connection objects to have a sudofn injected
- if sudofn is set, then Connect won't try to detect it

Signed-off-by: James Nesbitt <[email protected]>
  • Loading branch information
james-nesbitt committed Nov 5, 2024
1 parent dc14b8a commit e2eb795
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,19 @@ func (c *Connection) Connect() error {
c.OSVersion = &o
}

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

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 @@ -291,17 +299,17 @@ func (c *Connection) configureSudo() {
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

0 comments on commit e2eb795

Please sign in to comment.