Skip to content

Commit

Permalink
feat(cli): install agents via SSH on a custom port (#516)
Browse files Browse the repository at this point in the history
To authenticate to the remote host on a non-standard SSH port
users can now use the `--ssh_port` flag or pass the port directly
via the argument:
```
$ lacework agent install <user@host:port>
```

JIRA: https://lacework.atlassian.net/browse/ALLY-602

Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune authored Aug 13, 2021
1 parent 83f8884 commit 89ff730
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
11 changes: 10 additions & 1 deletion cli/cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
TokenUpdateDesc string
InstallForce bool
InstallSshUser string
InstallSshPort int
InstallAgentToken string
InstallTrustHostKey bool
InstallPassword string
Expand Down Expand Up @@ -136,7 +137,7 @@ To enable a token:
}

agentInstallCmd = &cobra.Command{
Use: "install <[user@]host>",
Use: "install <[user@]host[:port]>",
Short: "install the datacollector agent on a remote host",
Args: cobra.ExactArgs(1),
Long: `For single host installation of the Lacework agent via Secure Shell (SSH).
Expand All @@ -156,6 +157,11 @@ To provide an agent access token of your choice, use the command 'lacework agent
select a token and pass it to the '--token' flag.
$ lacework agent install <user@host> -i /path/to/your/key --token <token>
To authenticate to the remote host on a non-standard SSH port use the '--ssh_port' flag or
pass it directly via the argument.
$ lacework agent install <user@host:port>
`,
RunE: installRemoteAgent,
}
Expand Down Expand Up @@ -202,6 +208,9 @@ func init() {
agentInstallCmd.Flags().StringVar(&agentCmdState.InstallSshUser,
"ssh_username", "", "username to login with",
)
agentInstallCmd.Flags().IntVar(&agentCmdState.InstallSshPort,
"ssh_port", 22, "port to connect to on the remote host",
)
agentInstallCmd.Flags().BoolVar(&agentCmdState.InstallForce,
"force", false, "override any pre-installed agent",
)
Expand Down
21 changes: 20 additions & 1 deletion cli/cmd/agent_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"bytes"
"fmt"
"net"
"strconv"
"strings"

"github.com/AlecAivazis/survey/v2"
Expand All @@ -39,19 +40,37 @@ const agentInstallDownloadURL = "https://packages.lacework.net/install.sh"
func installRemoteAgent(_ *cobra.Command, args []string) error {
var (
user = agentCmdState.InstallSshUser
port = agentCmdState.InstallSshPort
host = args[0]
authSet = false
)
// verify if the user specified the username via user@host

// verify if the user specified the username via "user@host"
if strings.Contains(host, "@") {
userHost := strings.Split(host, "@")
user = userHost[0]
host = userHost[1]
}

// verify if the user specified the port via "host:port"
if strings.Contains(host, ":") {
userHost := strings.Split(host, ":")
host = userHost[0]
p, err := strconv.Atoi(userHost[1])
if err != nil {
return errors.Wrap(err, "invalid port")
}
port = p
}

cli.Log.Debugw("creating runner", "user", user, "host", host)
runner := lwrunner.New(user, host, verifyHostCallback)

if runner.Port != port {
cli.Log.Debugw("ssh settings", "port", port)
runner.Port = port
}

if runner.User == "" {
cli.Log.Debugw("ssh username not set")
user, err := askForUsername()
Expand Down

0 comments on commit 89ff730

Please sign in to comment.