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

PoC: sync: add sudo support #305

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions cmd/mutagen/sync/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,9 @@ var createConfiguration struct {
// permission propagation mode, taking priority over defaultGroup on beta if
// specified.
defaultGroupBeta string
// sudo specifies if the agent should be started with 'sudo'. This can
// be useful when root SSH is disabled.
sudo bool
}

func init() {
Expand Down
13 changes: 8 additions & 5 deletions pkg/agent/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
// remote environment is cmd.exe-based and returns hints as to whether or not
// installation should be attempted and whether or not the remote environment is
// cmd.exe-based.
func connect(logger *logging.Logger, transport Transport, mode, prompter string, cmdExe bool) (io.ReadWriteCloser, bool, bool, error) {
func connect(logger *logging.Logger, transport Transport, mode, prompter string, cmdExe bool, sudo bool) (io.ReadWriteCloser, bool, bool, error) {
// Compute the agent invocation command, relative to the user's home
// directory on the remote. Unless we have reason to assume that this is a
// cmd.exe environment, we construct a path using forward slashes. This will
Expand Down Expand Up @@ -66,6 +66,9 @@ func connect(logger *logging.Logger, transport Transport, mode, prompter string,

// Compute the command to invoke.
command := fmt.Sprintf("%s %s", agentInvocationPath, mode)
if sudo && !cmdExe {
command = "sudo " + command
}

// Create an agent process.
message := "Connecting to agent (POSIX)..."
Expand Down Expand Up @@ -168,7 +171,7 @@ func connect(logger *logging.Logger, transport Transport, mode, prompter string,

// Dial connects to an agent-based endpoint using the specified transport,
// connection mode, and prompter.
func Dial(logger *logging.Logger, transport Transport, mode, prompter string) (io.ReadWriteCloser, error) {
func Dial(logger *logging.Logger, transport Transport, mode string, sudo bool, prompter string) (io.ReadWriteCloser, error) {
// Validate that the mode is sane.
if !(mode == ModeSynchronizer || mode == ModeForwarder) {
panic("invalid agent dial mode")
Expand All @@ -177,11 +180,11 @@ func Dial(logger *logging.Logger, transport Transport, mode, prompter string) (i
// Attempt a connection. If this fails but we detect a Windows cmd.exe
// environment in the process, then re-attempt a connection under the
// cmd.exe assumption.
stream, tryInstall, cmdExe, err := connect(logger, transport, mode, prompter, false)
stream, tryInstall, cmdExe, err := connect(logger, transport, mode, prompter, false, sudo)
if err == nil {
return stream, nil
} else if cmdExe {
stream, tryInstall, cmdExe, err = connect(logger, transport, mode, prompter, true)
stream, tryInstall, cmdExe, err = connect(logger, transport, mode, prompter, true, sudo)
if err == nil {
return stream, nil
}
Expand All @@ -199,7 +202,7 @@ func Dial(logger *logging.Logger, transport Transport, mode, prompter string) (i
}

// Re-attempt connectivity.
stream, _, _, err = connect(logger, transport, mode, prompter, cmdExe)
stream, _, _, err = connect(logger, transport, mode, prompter, cmdExe, sudo)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/transport/ssh/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (t *sshTransport) ClassifyError(processState *os.ProcessState, errorOutput
// hypothesis (instead of the cmd.exe hypothesis).
if process.IsPOSIXShellInvalidCommand(processState) {
return true, false, nil
} else if process.IsPOSIXShellCommandNotFound(processState) {
} else if process.IsPOSIXShellCommandNotFound(processState, errorOutput) {
return true, false, nil
} else if process.OutputIsWindowsInvalidCommand(errorOutput) {
// A Windows invalid command error doesn't necessarily indicate that
Expand Down
4 changes: 4 additions & 0 deletions pkg/configuration/synchronization/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type Configuration struct {
// setting ownership of new files and directories in "portable"
// permission propagation mode.
DefaultGroup string `yaml:"defaultGroup"`
// Sudo specifies if the agent should be started with 'sudo'. This can
// be useful when root SSH is disabled.
Sudo bool `yaml:"sudo"`
} `yaml:"permissions"`
}

Expand All @@ -86,5 +89,6 @@ func (c *Configuration) Configuration() *synchronization.Configuration {
DefaultDirectoryMode: uint32(c.Permissions.DefaultDirectoryMode),
DefaultOwner: c.Permissions.DefaultOwner,
DefaultGroup: c.Permissions.DefaultGroup,
Sudo: c.Permissions.Sudo,
}
}
2 changes: 1 addition & 1 deletion pkg/forwarding/protocols/docker/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (p *protocolHandler) Connect(
// cancellation.
go func() {
// Perform the dialing operation.
stream, err := agent.Dial(logger, transport, agent.ModeForwarder, prompter)
stream, err := agent.Dial(logger, transport, agent.ModeForwarder, false, prompter)

// Transmit the result or, if cancelled, close the stream.
select {
Expand Down
2 changes: 1 addition & 1 deletion pkg/forwarding/protocols/ssh/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (p *protocolHandler) Connect(
// cancellation.
go func() {
// Perform the dialing operation.
stream, err := agent.Dial(logger, transport, agent.ModeForwarder, prompter)
stream, err := agent.Dial(logger, transport, agent.ModeForwarder, false, prompter)

// Transmit the result or, if cancelled, close the stream.
select {
Expand Down
12 changes: 10 additions & 2 deletions pkg/process/exit_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package process

import (
"os"
"strings"
)

const (
Expand Down Expand Up @@ -35,6 +36,13 @@ func IsPOSIXShellInvalidCommand(state *os.ProcessState) bool {

// IsPOSIXShellCommandNotFound returns whether or not a process state represents
// a "command not found" error from a POSIX shell.
func IsPOSIXShellCommandNotFound(state *os.ProcessState) bool {
return state.ExitCode() == posixShellCommandNotFoundExitCode
func IsPOSIXShellCommandNotFound(state *os.ProcessState, errorOutput string) bool {
if state.ExitCode() == posixShellCommandNotFoundExitCode {
return true
}
if state.ExitCode() == 1 &&
strings.HasSuffix(strings.TrimSpace(errorOutput), "command not found") {
return true
}
return false
}
8 changes: 7 additions & 1 deletion pkg/synchronization/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ func (c *Configuration) Equal(other *Configuration) bool {
c.DefaultFileMode == other.DefaultFileMode &&
c.DefaultDirectoryMode == other.DefaultDirectoryMode &&
c.DefaultOwner == other.DefaultOwner &&
c.DefaultGroup == other.DefaultGroup
c.DefaultGroup == other.DefaultGroup &&
c.Sudo == other.Sudo
}

// MergeConfigurations merges two configurations of differing priorities. Both
Expand Down Expand Up @@ -278,6 +279,11 @@ func MergeConfigurations(lower, higher *Configuration) *Configuration {
result.DefaultGroup = lower.DefaultGroup
}

// Merge sudo.
if higher.Sudo || lower.Sudo {
result.Sudo = true
}

// Done.
return result
}
23 changes: 17 additions & 6 deletions pkg/synchronization/configuration.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion pkg/synchronization/configuration.proto
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,9 @@ message Configuration {
// propagation mode.
string defaultGroup = 66;

// Fields 67-80 are reserved for future permission configuration parameters.
// Sudo specifies if the agent should be started with 'sudo'. This can
// be useful when root SSH is disabled.
bool sudo = 67;

// Fields 68-80 are reserved for future permission configuration parameters.
}
2 changes: 1 addition & 1 deletion pkg/synchronization/protocols/docker/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (h *protocolHandler) Connect(
// cancellation.
go func() {
// Perform the dialing operation.
stream, err := agent.Dial(logger, transport, agent.ModeSynchronizer, prompter)
stream, err := agent.Dial(logger, transport, agent.ModeSynchronizer, false, prompter)

// Transmit the result or, if cancelled, close the stream.
select {
Expand Down
4 changes: 3 additions & 1 deletion pkg/synchronization/protocols/ssh/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ func (h *protocolHandler) Connect(
// Create a channel to deliver the dialing result.
results := make(chan dialResult)

logger.Debug("configuration.Sudo:", configuration.Sudo)

// Perform dialing in a background Goroutine so that we can monitor for
// cancellation.
go func() {
// Perform the dialing operation.
stream, err := agent.Dial(logger, transport, agent.ModeSynchronizer, prompter)
stream, err := agent.Dial(logger, transport, agent.ModeSynchronizer, configuration.Sudo, prompter)

// Transmit the result or, if cancelled, close the stream.
select {
Expand Down