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

Skip sudo invocation if running user is sudo #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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