Skip to content

Commit

Permalink
refactor(cli): use official url for agent installs (#369)
Browse files Browse the repository at this point in the history
Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune authored Apr 5, 2021
1 parent 57b33e3 commit ca2d3f7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 62 deletions.
47 changes: 4 additions & 43 deletions cli/cmd/agent_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ package cmd
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
"strings"

"github.com/AlecAivazis/survey/v2"
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 [email protected]")
}

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\""

Expand Down
19 changes: 0 additions & 19 deletions cli/cmd/agent_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ca2d3f7

Please sign in to comment.