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

feat(cli): install agents via SSH on a custom port #516

Merged
merged 1 commit into from
Aug 13, 2021
Merged
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
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
InstallPassword string
InstallIdentityFile string
Expand Down Expand Up @@ -135,7 +136,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 @@ -155,6 +156,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 @@ -201,6 +207,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