2022-08-31 12:08:46 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2022-10-25 15:28:39 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2023-06-15 11:51:14 -04:00
|
|
|
package base_test
|
2022-08-31 12:08:46 -04:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2023-05-08 15:04:26 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2023-04-21 06:50:53 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/target"
|
2022-08-31 12:08:46 -04:00
|
|
|
"github.com/hashicorp/boundary/testing/internal/e2e"
|
|
|
|
|
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
|
|
|
|
|
)
|
|
|
|
|
|
2023-06-27 14:03:48 -04:00
|
|
|
// TestCliTcpTargetConnectTargetBasic uses the boundary cli to create a number of
|
2023-04-21 06:50:53 -04:00
|
|
|
// supporting objects to connect to a target. It then attempts to connect to
|
|
|
|
|
// that target and verifies that the connection was successful.
|
2023-06-26 13:21:45 -04:00
|
|
|
func TestCliTcpTargetConnectTargetBasic(t *testing.T) {
|
2022-08-31 12:08:46 -04:00
|
|
|
e2e.MaybeSkipTest(t)
|
2023-05-16 13:06:11 -04:00
|
|
|
c, err := loadTestConfig()
|
2022-08-31 12:08:46 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2022-10-25 15:46:10 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
boundary.AuthenticateAdminCli(t, ctx)
|
2024-04-03 09:44:44 -04:00
|
|
|
orgId, err := boundary.CreateOrgCli(t, ctx)
|
|
|
|
|
require.NoError(t, err)
|
2022-12-06 15:59:17 -05:00
|
|
|
t.Cleanup(func() {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
boundary.AuthenticateAdminCli(t, ctx)
|
2024-04-03 09:44:44 -04:00
|
|
|
output := e2e.RunCommand(ctx, "boundary", e2e.WithArgs("scopes", "delete", "-id", orgId))
|
2022-12-06 15:59:17 -05:00
|
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
|
|
|
})
|
2024-04-03 09:44:44 -04:00
|
|
|
projectId, err := boundary.CreateProjectCli(t, ctx, orgId)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
hostCatalogId, err := boundary.CreateHostCatalogCli(t, ctx, projectId)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
hostSetId, err := boundary.CreateHostSetCli(t, ctx, hostCatalogId)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
hostId, err := boundary.CreateHostCli(t, ctx, hostCatalogId, c.TargetAddress)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = boundary.AddHostToHostSetCli(t, ctx, hostSetId, hostId)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
targetId, err := boundary.CreateTargetCli(t, ctx, projectId, c.TargetPort)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = boundary.AddHostSourceToTargetCli(t, ctx, targetId, hostSetId)
|
|
|
|
|
require.NoError(t, err)
|
2022-08-31 12:08:46 -04:00
|
|
|
|
|
|
|
|
// Connect to target and print host's IP address
|
2022-10-25 15:28:39 -04:00
|
|
|
output := e2e.RunCommand(ctx, "boundary",
|
|
|
|
|
e2e.WithArgs(
|
|
|
|
|
"connect",
|
2024-04-03 09:44:44 -04:00
|
|
|
"-target-id", targetId,
|
2022-10-25 15:28:39 -04:00
|
|
|
"-exec", "/usr/bin/ssh", "--",
|
|
|
|
|
"-l", c.TargetSshUser,
|
|
|
|
|
"-i", c.TargetSshKeyPath,
|
|
|
|
|
"-o", "UserKnownHostsFile=/dev/null",
|
|
|
|
|
"-o", "StrictHostKeyChecking=no",
|
|
|
|
|
"-o", "IdentitiesOnly=yes", // forces the use of the provided key
|
|
|
|
|
"-p", "{{boundary.port}}", // this is provided by boundary
|
|
|
|
|
"{{boundary.ip}}",
|
|
|
|
|
"hostname", "-i",
|
|
|
|
|
),
|
2022-09-23 14:21:00 -04:00
|
|
|
)
|
2022-08-31 12:08:46 -04:00
|
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
|
|
|
|
|
|
|
|
parts := strings.Fields(string(output.Stdout))
|
|
|
|
|
hostIp := parts[len(parts)-1]
|
2023-09-06 17:29:35 -04:00
|
|
|
require.Equal(t, c.TargetAddress, hostIp, "SSH session did not return expected output")
|
2022-08-31 12:08:46 -04:00
|
|
|
t.Log("Successfully connected to target")
|
|
|
|
|
}
|
2023-04-21 06:50:53 -04:00
|
|
|
|
2023-07-06 16:41:39 -04:00
|
|
|
// TestCliTcpTargetConnectTargetBasic uses the boundary cli to create a number of
|
|
|
|
|
// supporting objects to connect to a target. It then attempts to connect to
|
|
|
|
|
// that target via the combination of target name and target scope name,
|
|
|
|
|
// and verifies that the connection was successful.
|
|
|
|
|
func TestCliTcpTargetConnectTargetViaTargetAndScopeNames(t *testing.T) {
|
|
|
|
|
e2e.MaybeSkipTest(t)
|
|
|
|
|
c, err := loadTestConfig()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
boundary.AuthenticateAdminCli(t, ctx)
|
2024-04-03 09:44:44 -04:00
|
|
|
orgId, err := boundary.CreateOrgCli(t, ctx)
|
|
|
|
|
require.NoError(t, err)
|
2023-07-06 16:41:39 -04:00
|
|
|
t.Cleanup(func() {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
boundary.AuthenticateAdminCli(t, ctx)
|
2024-04-03 09:44:44 -04:00
|
|
|
output := e2e.RunCommand(ctx, "boundary", e2e.WithArgs("scopes", "delete", "-id", orgId))
|
2023-07-06 16:41:39 -04:00
|
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
|
|
|
})
|
|
|
|
|
testProjectName := `E2E/Project-With\Name`
|
|
|
|
|
testTargetName := `E2E/Test-Target-With\Name`
|
2024-04-03 09:44:44 -04:00
|
|
|
projectId, err := boundary.CreateProjectCli(t, ctx, orgId, boundary.WithName(testProjectName))
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
hostCatalogId, err := boundary.CreateHostCatalogCli(t, ctx, projectId)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
hostSetId, err := boundary.CreateHostSetCli(t, ctx, hostCatalogId)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
hostId, err := boundary.CreateHostCli(t, ctx, hostCatalogId, c.TargetAddress)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = boundary.AddHostToHostSetCli(t, ctx, hostSetId, hostId)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
targetId, err := boundary.CreateTargetCli(t, ctx, projectId, c.TargetPort, target.WithName(testTargetName))
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = boundary.AddHostSourceToTargetCli(t, ctx, targetId, hostSetId)
|
|
|
|
|
require.NoError(t, err)
|
2023-07-06 16:41:39 -04:00
|
|
|
|
|
|
|
|
// Connect to target via target and scope names, and print host's IP address
|
|
|
|
|
output := e2e.RunCommand(ctx, "boundary",
|
|
|
|
|
e2e.WithArgs(
|
|
|
|
|
"connect",
|
|
|
|
|
"-target-name", testTargetName,
|
|
|
|
|
"-target-scope-name", testProjectName,
|
|
|
|
|
"-exec", "/usr/bin/ssh", "--",
|
|
|
|
|
"-l", c.TargetSshUser,
|
|
|
|
|
"-i", c.TargetSshKeyPath,
|
|
|
|
|
"-o", "UserKnownHostsFile=/dev/null",
|
|
|
|
|
"-o", "StrictHostKeyChecking=no",
|
|
|
|
|
"-o", "IdentitiesOnly=yes", // forces the use of the provided key
|
|
|
|
|
"-p", "{{boundary.port}}", // this is provided by boundary
|
|
|
|
|
"{{boundary.ip}}",
|
|
|
|
|
"hostname", "-i",
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
|
|
|
|
|
|
|
|
parts := strings.Fields(string(output.Stdout))
|
|
|
|
|
hostIp := parts[len(parts)-1]
|
2023-09-06 17:29:35 -04:00
|
|
|
require.Equal(t, c.TargetAddress, hostIp, "SSH session did not return expected output")
|
2023-07-06 16:41:39 -04:00
|
|
|
t.Log("Successfully connected to target by its name and scope name")
|
|
|
|
|
|
|
|
|
|
// Connect to target via target name and scope ID, and print host's IP address
|
|
|
|
|
output = e2e.RunCommand(ctx, "boundary",
|
|
|
|
|
e2e.WithArgs(
|
|
|
|
|
"connect",
|
|
|
|
|
"-target-name", testTargetName,
|
2024-04-03 09:44:44 -04:00
|
|
|
"-target-scope-id", projectId,
|
2023-07-06 16:41:39 -04:00
|
|
|
"-exec", "/usr/bin/ssh", "--",
|
|
|
|
|
"-l", c.TargetSshUser,
|
|
|
|
|
"-i", c.TargetSshKeyPath,
|
|
|
|
|
"-o", "UserKnownHostsFile=/dev/null",
|
|
|
|
|
"-o", "StrictHostKeyChecking=no",
|
|
|
|
|
"-o", "IdentitiesOnly=yes", // forces the use of the provided key
|
|
|
|
|
"-p", "{{boundary.port}}", // this is provided by boundary
|
|
|
|
|
"{{boundary.ip}}",
|
|
|
|
|
"hostname", "-i",
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
|
|
|
|
|
|
|
|
parts = strings.Fields(string(output.Stdout))
|
|
|
|
|
hostIp = parts[len(parts)-1]
|
2023-09-06 17:29:35 -04:00
|
|
|
require.Equal(t, c.TargetAddress, hostIp, "SSH session did not return expected output")
|
2023-07-06 16:41:39 -04:00
|
|
|
t.Log("Successfully connected to target by its name and scope ID")
|
|
|
|
|
}
|