Skip to content

Commit

Permalink
Skip sudo invocation if running user is sudo
Browse files Browse the repository at this point in the history
  • Loading branch information
terrorbyte committed Aug 23, 2024
1 parent e9bde3a commit 0cd4ea4
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (
"context"
"errors"
"fmt"
"github.com/DBCDK/morph/utils"
"golang.org/x/crypto/ssh/terminal"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"

"github.com/DBCDK/morph/utils"
"golang.org/x/crypto/ssh/terminal"
)

type Context interface {
Expand Down Expand Up @@ -156,20 +157,22 @@ func (sshCtx *SSHContext) SudoCmdContext(ctx context.Context, host Host, parts .

cmd, cmdArgs := sshCtx.sshArgs(host, nil)

// normalize sudo
if parts[0] == "sudo" {
parts = parts[1:]
}
cmdArgs = append(cmdArgs, "sudo")
if host.GetTargetUser() != "root" {
// normalize sudo
if parts[0] == "sudo" {
parts = parts[1:]
}
cmdArgs = append(cmdArgs, "sudo")

if sshCtx.sudoPassword != "" {
cmdArgs = append(cmdArgs, "-S")
} else {
// no password supplied; request non-interactive sudo, which will fail with an error if a password was required
cmdArgs = append(cmdArgs, "-n")
}
if sshCtx.sudoPassword != "" {
cmdArgs = append(cmdArgs, "-S")
} else {
// no password supplied; request non-interactive sudo, which will fail with an error if a password was required
cmdArgs = append(cmdArgs, "-n")
}

cmdArgs = append(cmdArgs, "-p", "''", "-k", "--")
cmdArgs = append(cmdArgs, "-p", "''", "-k", "--")
}
cmdArgs = append(cmdArgs, parts...)

command := exec.CommandContext(ctx, cmd, cmdArgs...)
Expand Down

0 comments on commit 0cd4ea4

Please sign in to comment.