Skip to content

Commit

Permalink
🐛 set default sudo executable (#2858)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock authored Dec 18, 2023
1 parent a4759d2 commit 3fe9299
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
21 changes: 17 additions & 4 deletions providers/os/connection/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func NewSshConnection(id uint32, conf *inventory.Config, asset *inventory.Asset)
}

if os.Getenv("MONDOO_SSH_SCP") == "on" || conf.Options["ssh_scp"] == "on" {
log.Debug().Msg("use scp file transfer")
res.UseScpFilesystem = true
}

Expand All @@ -100,6 +101,8 @@ func NewSshConnection(id uint32, conf *inventory.Config, asset *inventory.Asset)
// configure sudo
log.Debug().Msg("activated sudo for ssh connection")
res.Sudo = conf.Sudo
} else {
log.Debug().Msg("deactivated sudo for ssh connection since user is root")
}
}

Expand Down Expand Up @@ -281,13 +284,23 @@ func (c *SshConnection) Close() {
}
}

// checks the connection config and set default values if not provided by the user
func (c *SshConnection) setDefaultSettings() {
// we always want to ensure we use the default port if nothing was specified
if c.conf.Port == 0 {
c.conf.Port = 22
}

// we need to check if an executable was provided, otherwise fallback to use sudo
if c.conf.Sudo != nil && c.conf.Sudo.Active && c.conf.Sudo.Executable == "" {
c.conf.Sudo.Executable = "sudo"
}
}

func (c *SshConnection) Connect() error {
cc := c.conf

// we always want to ensure we use the default port if nothing was specified
if cc.Port == 0 {
cc.Port = 22
}
c.setDefaultSettings()

// load known hosts and track the fingerprint of the ssh server for later identification
knownHostsCallback, err := knownHostsCallback()
Expand Down
24 changes: 24 additions & 0 deletions providers/os/connection/ssh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

package connection

import (
"testing"

"github.com/stretchr/testify/assert"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/inventory"
)

func TestSSHDefaultSettings(t *testing.T) {
conn := &SshConnection{
conf: &inventory.Config{
Sudo: &inventory.Sudo{
Active: true,
},
},
}
conn.setDefaultSettings()
assert.Equal(t, int32(22), conn.conf.Port)
assert.Equal(t, "sudo", conn.conf.Sudo.Executable)
}

0 comments on commit 3fe9299

Please sign in to comment.