From 8ecf08c2e09727912e81cae44b1085592c1fca9b Mon Sep 17 00:00:00 2001 From: Stan Ryzhov <60649800+stasryzhov@users.noreply.github.com> Date: Fri, 16 Feb 2024 19:25:54 +0000 Subject: [PATCH] backport of commit 89cc01f8344a743881078cfd9985487005ffd8e9 --- ...p_connect_exec_long_lasting_script_test.go | 119 ++++++++++++++++++ .../base/testdata/long_lasting_test_script.sh | 10 ++ 2 files changed, 129 insertions(+) create mode 100644 testing/internal/e2e/tests/base/target_tcp_connect_exec_long_lasting_script_test.go create mode 100755 testing/internal/e2e/tests/base/testdata/long_lasting_test_script.sh diff --git a/testing/internal/e2e/tests/base/target_tcp_connect_exec_long_lasting_script_test.go b/testing/internal/e2e/tests/base/target_tcp_connect_exec_long_lasting_script_test.go new file mode 100644 index 0000000000..d7e967341d --- /dev/null +++ b/testing/internal/e2e/tests/base/target_tcp_connect_exec_long_lasting_script_test.go @@ -0,0 +1,119 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package base_test + +import ( + "context" + "fmt" + "os/exec" + "testing" + + "github.com/hashicorp/boundary/internal/target" + "github.com/hashicorp/boundary/testing/internal/e2e" + "github.com/hashicorp/boundary/testing/internal/e2e/boundary" + "github.com/stretchr/testify/require" + "golang.org/x/sync/errgroup" +) + +// TestCliTcpTargetConnectExecLongLastingScript verifies that SSH requests sent to target +// can execute long-lasting scripts successfully. +// It sends two SSH requests: +// - to execute the script saved on the target +// - to execute the script sent with the SSH request +func TestCliTcpTargetConnectExecLongLastingScript(t *testing.T) { + e2e.MaybeSkipTest(t) + c, err := loadTestConfig() + require.NoError(t, err) + + ctx := context.Background() + boundary.AuthenticateAdminCli(t, ctx) + + // Create test organization + newOrgId := boundary.CreateNewOrgCli(t, ctx) + + // Delete organization after the test is completed + t.Cleanup(func() { + ctx := context.Background() + boundary.AuthenticateAdminCli(t, ctx) + output := e2e.RunCommand(ctx, "boundary", e2e.WithArgs("scopes", "delete", "-id", newOrgId)) + require.NoError(t, output.Err, string(output.Stderr)) + }) + // Create test project + newProjectId := boundary.CreateNewProjectCli(t, ctx, newOrgId) + + // Create static credentials + newCredentialStoreId := boundary.CreateNewCredentialStoreStaticCli(t, ctx, newProjectId) + newCredentialsId := boundary.CreateNewStaticCredentialPrivateKeyCli(t, ctx, newCredentialStoreId, c.TargetSshUser, c.TargetSshKeyPath) + + // Create TCP target + newTargetId := boundary.CreateNewTargetCli(t, ctx, newProjectId, c.TargetPort, + target.WithType("tcp"), + target.WithAddress(c.TargetAddress), + ) + boundary.AddBrokeredCredentialSourceToTargetCli(t, ctx, newTargetId, newCredentialsId) + + // Start a session + ctxCancel, cancel := context.WithCancel(context.Background()) + proxyPort := "12345" + cmdChan := make(chan *e2e.CommandResult) + go func() { + t.Log("Starting session...") + cmdChan <- e2e.RunCommand(ctxCancel, "boundary", + e2e.WithArgs( + "connect", + "-target-id", newTargetId, + "-listen-port", proxyPort, + "-format", "json", + ), + ) + }() + t.Cleanup(cancel) + boundary.WaitForSessionCli(t, ctx, newProjectId) + + t.Log("Copying script to host...") + output := e2e.RunCommand(ctx, "scp", + e2e.WithArgs( + "-i", c.TargetSshKeyPath, + "-P", proxyPort, + "-o", "UserKnownHostsFile=/dev/null", + "-o", "StrictHostKeyChecking=no", + "-o", "IdentitiesOnly=yes", // forces the use of the provided key + "testdata/long_lasting_test_script.sh", + fmt.Sprintf("%s@localhost:%s", c.TargetSshUser, "long_lasting_test_script.sh"), + ), + ) + require.NoError(t, output.Err, string(output.Stderr)) + + // Send SSH requests to the target to execute long-lasting scripts + var eg errgroup.Group + eg.Go(func() error { + t.Log("Executing the long-lasting script saved on the target...") + cmd := exec.CommandContext(ctx, "/usr/bin/ssh", + "-v", + "-l", c.TargetSshUser, + "-i", c.TargetSshKeyPath, + "-o", "UserKnownHostsFile=/dev/null", + "-o", "StrictHostKeyChecking=no", + "-p", proxyPort, + "localhost", + "./long_lasting_test_script.sh") + return cmd.Run() + }) + + eg.Go(func() error { + t.Log("Executing a long-lasting script sent with the ssh request...") + cmd := exec.CommandContext(ctx, "/usr/bin/ssh", + "-v", + "-l", c.TargetSshUser, + "-i", c.TargetSshKeyPath, + "-o", "UserKnownHostsFile=/dev/null", + "-o", "StrictHostKeyChecking=no", + "-p", proxyPort, + "localhost", + "sleep 10") + return cmd.Run() + }) + + require.NoError(t, eg.Wait()) +} diff --git a/testing/internal/e2e/tests/base/testdata/long_lasting_test_script.sh b/testing/internal/e2e/tests/base/testdata/long_lasting_test_script.sh new file mode 100755 index 0000000000..30096edb6e --- /dev/null +++ b/testing/internal/e2e/tests/base/testdata/long_lasting_test_script.sh @@ -0,0 +1,10 @@ +#!/bin/bash +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: BUSL-1.1 + +for i in {1..10} +do + echo iteration $i + sleep 1; +done +exit 0; \ No newline at end of file