From bbf443e2538c84ae75b36ed529e85d8024c5a583 Mon Sep 17 00:00:00 2001 From: Giles Hutton Date: Mon, 12 Aug 2024 13:17:00 +0100 Subject: [PATCH 1/3] Removes SSH and GCP command builders --- .../pkg/executor/executor_docker.go | 98 ++----------------- 1 file changed, 6 insertions(+), 92 deletions(-) diff --git a/integration-tests/pkg/executor/executor_docker.go b/integration-tests/pkg/executor/executor_docker.go index e9537169b3..445378ba01 100644 --- a/integration-tests/pkg/executor/executor_docker.go +++ b/integration-tests/pkg/executor/executor_docker.go @@ -24,60 +24,20 @@ type dockerExecutor struct { builder CommandBuilder } -type sshCommandBuilder struct { - user string - address string - keyPath string -} - -type gcloudCommandBuilder struct { - user string - instance string - options string - vmType string -} - type localCommandBuilder struct { } -func newSSHCommandBuilder() CommandBuilder { - host_info := config.HostInfo() - return &sshCommandBuilder{ - user: host_info.User, - address: host_info.Address, - keyPath: host_info.Options, - } -} - -func newGcloudCommandBuilder() CommandBuilder { - host_info := config.HostInfo() - gcb := &gcloudCommandBuilder{ - user: host_info.User, - instance: host_info.Address, - options: host_info.Options, - vmType: config.VMInfo().Config, - } - if gcb.user == "" && (strings.Contains(gcb.vmType, "coreos") || strings.Contains(gcb.vmType, "flatcar")) { - gcb.user = "core" - } - return gcb -} - func newLocalCommandBuilder() CommandBuilder { return &localCommandBuilder{} } func newDockerExecutor() (*dockerExecutor, error) { - e := dockerExecutor{} - switch config.HostInfo().Kind { - case "ssh": - e.builder = newSSHCommandBuilder() - case "gcloud": - e.builder = newGcloudCommandBuilder() - case "local": - e.builder = newLocalCommandBuilder() - } - return &e, nil + // While this function can't fail, to conform to the + // same construction API as other executors, we keep the + // error return value. + return &dockerExecutor{ + builder: newLocalCommandBuilder(), + }, nil } // Exec executes the provided command with retries on non-zero error from the command. @@ -254,49 +214,3 @@ func (e *localCommandBuilder) RemoteCopyCommand(remoteSrc string, localDst strin } return nil } - -func (e *gcloudCommandBuilder) ExecCommand(args ...string) *exec.Cmd { - cmdArgs := []string{"compute", "ssh"} - if len(e.options) > 0 { - opts := strings.Split(e.options, " ") - cmdArgs = append(cmdArgs, opts...) - } - userInstance := e.instance - if e.user != "" { - userInstance = e.user + "@" + e.instance - } - - cmdArgs = append(cmdArgs, userInstance, "--", "-T") - cmdArgs = append(cmdArgs, common.QuoteArgs(args)...) - return exec.Command("gcloud", cmdArgs...) -} - -func (e *gcloudCommandBuilder) RemoteCopyCommand(remoteSrc string, localDst string) *exec.Cmd { - cmdArgs := []string{"compute", "scp"} - if len(e.options) > 0 { - opts := strings.Split(e.options, " ") - cmdArgs = append(cmdArgs, opts...) - } - userInstance := e.instance - if e.user != "" { - userInstance = e.user + "@" + e.instance - } - cmdArgs = append(cmdArgs, userInstance+":"+remoteSrc, localDst) - return exec.Command("gcloud", cmdArgs...) -} - -func (e *sshCommandBuilder) ExecCommand(args ...string) *exec.Cmd { - cmdArgs := []string{ - "-o", "StrictHostKeyChecking=no", "-i", e.keyPath, - e.user + "@" + e.address} - - cmdArgs = append(cmdArgs, common.QuoteArgs(args)...) - return exec.Command("ssh", cmdArgs...) -} - -func (e *sshCommandBuilder) RemoteCopyCommand(remoteSrc string, localDst string) *exec.Cmd { - args := []string{ - "-o", "StrictHostKeyChecking=no", "-i", e.keyPath, - e.user + "@" + e.address + ":" + remoteSrc, localDst} - return exec.Command("scp", args...) -} From e5dcde246945f3efbfcf7d5f9fd7379e8d1f1933 Mon Sep 17 00:00:00 2001 From: Giles Hutton Date: Mon, 12 Aug 2024 13:23:53 +0100 Subject: [PATCH 2/3] Remove unused import --- integration-tests/pkg/executor/executor_docker.go | 1 - 1 file changed, 1 deletion(-) diff --git a/integration-tests/pkg/executor/executor_docker.go b/integration-tests/pkg/executor/executor_docker.go index 445378ba01..971765b08c 100644 --- a/integration-tests/pkg/executor/executor_docker.go +++ b/integration-tests/pkg/executor/executor_docker.go @@ -8,7 +8,6 @@ import ( "strings" "github.com/pkg/errors" - "github.com/stackrox/collector/integration-tests/pkg/common" "github.com/stackrox/collector/integration-tests/pkg/config" ) From 53242de7c9e1ef4ed9875597a2ac53e4bae5073d Mon Sep 17 00:00:00 2001 From: Giles Hutton Date: Mon, 12 Aug 2024 15:02:05 +0100 Subject: [PATCH 3/3] Remove extra host config values, and simplify kind checks --- integration-tests/integration_test.go | 2 +- integration-tests/pkg/config/config.go | 14 ++++++-------- integration-tests/pkg/config/env.go | 5 +---- integration-tests/pkg/executor/executor.go | 2 +- integration-tests/suites/base.go | 2 +- 5 files changed, 10 insertions(+), 15 deletions(-) diff --git a/integration-tests/integration_test.go b/integration-tests/integration_test.go index 2ba06a8016..c73f7b388e 100644 --- a/integration-tests/integration_test.go +++ b/integration-tests/integration_test.go @@ -21,7 +21,7 @@ func TestImageLabelJSON(t *testing.T) { // TestMissingProcScrape only works with local fake proc directory func TestMissingProcScrape(t *testing.T) { - if config.HostInfo().IsLocal() { + if !config.HostInfo().IsK8s() { suite.Run(t, new(suites.MissingProcScrapeTestSuite)) } } diff --git a/integration-tests/pkg/config/config.go b/integration-tests/pkg/config/config.go index c935f3a561..ed581f1f6b 100644 --- a/integration-tests/pkg/config/config.go +++ b/integration-tests/pkg/config/config.go @@ -48,16 +48,17 @@ func init() { // Host contains information about how to connect to the host upon // which the tests are running type Host struct { - Kind string - User string - Address string - Options string + Kind string } func (h *Host) IsLocal() bool { return h.Kind == "local" } +func (h *Host) IsK8s() bool { + return h.Kind == "k8s" +} + // VM contains metadata about the machine upon which the tests are // running. type VM struct { @@ -116,10 +117,7 @@ func StopTimeout() string { func HostInfo() *Host { if host_options == nil { host_options = &Host{ - Kind: ReadEnvVarWithDefault(envHostType, "local"), - User: ReadEnvVar(envHostUser), - Address: ReadEnvVar(envHostAddress), - Options: ReadEnvVar(envHostOptions), + Kind: ReadEnvVarWithDefault(envHostType, "local"), } } diff --git a/integration-tests/pkg/config/env.go b/integration-tests/pkg/config/env.go index ec886e8a74..e3bf8f0ab8 100644 --- a/integration-tests/pkg/config/env.go +++ b/integration-tests/pkg/config/env.go @@ -12,10 +12,7 @@ const ( envCollectorLogLevel = "COLLECTOR_LOG_LEVEL" envCollectorPreArguments = "COLLECTOR_PRE_ARGUMENTS" - envHostType = "REMOTE_HOST_TYPE" - envHostUser = "REMOTE_HOST_USER" - envHostAddress = "REMOTE_HOST_ADDRESS" - envHostOptions = "REMOTE_HOST_OPTIONS" + envHostType = "REMOTE_HOST_TYPE" envVMInstanceType = "VM_INSTANCE_TYPE" envVMConfig = "VM_CONFIG" diff --git a/integration-tests/pkg/executor/executor.go b/integration-tests/pkg/executor/executor.go index a14478a0b3..7c78050db0 100644 --- a/integration-tests/pkg/executor/executor.go +++ b/integration-tests/pkg/executor/executor.go @@ -33,7 +33,7 @@ type CommandBuilder interface { } func New() (Executor, error) { - if config.HostInfo().Kind == "k8s" { + if config.HostInfo().IsK8s() { return newK8sExecutor() } return newDockerExecutor() diff --git a/integration-tests/suites/base.go b/integration-tests/suites/base.go index e02d62311f..e1be8306ef 100644 --- a/integration-tests/suites/base.go +++ b/integration-tests/suites/base.go @@ -521,7 +521,7 @@ func (s *IntegrationTestSuiteBase) waitForFileToBeDeleted(file string) error { case <-timer: return fmt.Errorf("Timed out waiting for %s to be deleted", file) case <-ticker.C: - if config.HostInfo().Kind == "local" { + if config.HostInfo().IsLocal() { if _, err := os.Stat(file); os.IsNotExist(err) { return nil }