From ca2d3f7063dce713f6743ace9a4a07d5cc6c2119 Mon Sep 17 00:00:00 2001 From: Salim Afiune Date: Mon, 5 Apr 2021 14:15:33 -0500 Subject: [PATCH] refactor(cli): use official url for agent installs (#369) Signed-off-by: Salim Afiune Maya --- cli/cmd/agent_install.go | 47 +++-------------------------------- cli/cmd/agent_install_test.go | 19 -------------- 2 files changed, 4 insertions(+), 62 deletions(-) diff --git a/cli/cmd/agent_install.go b/cli/cmd/agent_install.go index c2c35164b..1e32157ce 100644 --- a/cli/cmd/agent_install.go +++ b/cli/cmd/agent_install.go @@ -21,9 +21,7 @@ package cmd import ( "bytes" "fmt" - "io/ioutil" "net" - "net/http" "strings" "github.com/AlecAivazis/survey/v2" @@ -35,6 +33,9 @@ import ( "github.com/lacework/go-sdk/lwrunner" ) +// Official download url for installing Lacework agents +const agentInstallDownloadURL = "https://packages.lacework.net/install.sh" + func installRemoteAgent(_ *cobra.Command, args []string) error { var ( user = agentCmdState.InstallSshUser @@ -115,13 +116,7 @@ func installRemoteAgent(_ *cobra.Command, args []string) error { return err } } - - downloadUrl, err := latestAgentInstallDownloadUrl() - if err != nil { - return err - } - - cmd := fmt.Sprintf("sudo sh -c \"curl -sSL %s | sh -s -- %s\"", downloadUrl, token) + cmd := fmt.Sprintf("sudo sh -c \"curl -sSL %s | sh -s -- %s\"", agentInstallDownloadURL, token) return runInstallCommandOnRemoteHost(runner, cmd) } @@ -150,40 +145,6 @@ func runInstallCommandOnRemoteHost(runner *lwrunner.Runner, cmd string) error { return nil } -func latestAgentInstallDownloadUrl() (string, error) { - sha, err := latestAgentVersionSHA() - if err != nil { - return "", err - } - - url := fmt.Sprintf("https://s3-us-west-2.amazonaws.com/www.lacework.net/download/%s/install.sh", sha) - cli.Log.Debugw("latest agent install.sh", "url", url) - return url, nil -} - -func latestAgentVersionSHA() (string, error) { - url := "https://techally-artifacts.s3-us-west-2.amazonaws.com/lacework-cli-prod/agent-install/latest" - cli.Log.Debugw("fetching latest agent version SHA", "url", url) - resp, err := http.Get(url) - if err != nil { - return "", errors.Wrap(err, "unable to fetch latest agent version") - } - defer resp.Body.Close() - - if resp.StatusCode == 404 { - return "", errors.New("agent version artifact not found. Report this to support@lacework.net") - } - - shaBytes, err := ioutil.ReadAll(resp.Body) - if err != nil { - return "", errors.Wrap(err, "unable to read response from latest agent version") - } - - sha := strings.TrimSpace(string(shaBytes)) - cli.Log.Debugw("latest agent version SHA", "sha", sha) - return sha, nil -} - func isAgentInstalledOnRemoteHost(runner *lwrunner.Runner) error { agentVersionCmd := "sudo sh -c \"/var/lib/lacework/datacollector -v\"" diff --git a/cli/cmd/agent_install_test.go b/cli/cmd/agent_install_test.go index 4147292b4..7f91ac60a 100644 --- a/cli/cmd/agent_install_test.go +++ b/cli/cmd/agent_install_test.go @@ -21,31 +21,12 @@ package cmd import ( "bytes" "fmt" - "regexp" "testing" "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) -func TestLatestAgentInstallDownloadUrl(t *testing.T) { - downloadUrl, err := latestAgentInstallDownloadUrl() - if assert.Nil(t, err) { - assert.Contains(t, downloadUrl, "https://s3-us-west-2.amazonaws.com/www.lacework.net/download") - assert.Contains(t, downloadUrl, "install.sh") - } -} - -func TestLatestAgentVersionSHA(t *testing.T) { - sha, err := latestAgentVersionSHA() - if assert.Nil(t, err) { - // Example: 3.3.5_2020-11-16_master_ac0e65055f11f4f59bab6ea4dfa61dcafaa9a3f1 - assert.Regexpf(t, - regexp.MustCompile("[0-9]*\\.[0-9]*\\.[0-9]*_[0-9]*-[0-9]*-[0-9]*_\\w*_*"), sha, - "agent version SHA doesn't match regex AGENT_VERSION_YYYY-MM-DD_BRANCHNAME_GITHASH") - } -} - func TestFormatRunnerError(t *testing.T) { cases := []struct { expected error