2022-10-26 07:31:30 -04:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-21 07:56:18 -05:00
|
|
|
"strings"
|
2022-10-26 07:31:30 -04:00
|
|
|
"testing"
|
2022-11-08 10:10:03 -05:00
|
|
|
"time"
|
2022-10-26 07:31:30 -04:00
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
"github.com/juanfont/headscale/hscontrol/policy"
|
2022-11-11 07:20:12 -05:00
|
|
|
"github.com/juanfont/headscale/integration/hsic"
|
|
|
|
"github.com/juanfont/headscale/integration/tsic"
|
2022-11-19 06:41:12 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-10-26 07:31:30 -04:00
|
|
|
)
|
|
|
|
|
2022-11-19 06:41:12 -05:00
|
|
|
var retry = func(times int, sleepInterval time.Duration,
|
|
|
|
doWork func() (string, string, error),
|
|
|
|
) (string, string, error) {
|
|
|
|
var result string
|
|
|
|
var stderr string
|
2022-11-11 07:20:12 -05:00
|
|
|
var err error
|
2022-11-19 06:41:12 -05:00
|
|
|
|
2022-11-11 07:20:12 -05:00
|
|
|
for attempts := 0; attempts < times; attempts++ {
|
2022-11-19 06:41:12 -05:00
|
|
|
tempResult, tempStderr, err := doWork()
|
|
|
|
|
|
|
|
result += tempResult
|
|
|
|
stderr += tempStderr
|
|
|
|
|
2022-11-11 07:20:12 -05:00
|
|
|
if err == nil {
|
2022-11-19 06:41:12 -05:00
|
|
|
return result, stderr, nil
|
2022-11-08 10:10:03 -05:00
|
|
|
}
|
2022-11-21 08:08:24 -05:00
|
|
|
|
|
|
|
// If we get a permission denied error, we can fail immediately
|
|
|
|
// since that is something we wont recover from by retrying.
|
|
|
|
if err != nil && strings.Contains(stderr, "Permission denied (tailscale)") {
|
|
|
|
return result, stderr, err
|
|
|
|
}
|
|
|
|
|
2022-11-11 07:20:12 -05:00
|
|
|
time.Sleep(sleepInterval)
|
2022-11-08 10:10:03 -05:00
|
|
|
}
|
|
|
|
|
2022-11-19 06:41:12 -05:00
|
|
|
return result, stderr, err
|
2022-11-11 07:20:12 -05:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
func TestSSHOneUserAllToAll(t *testing.T) {
|
2022-11-11 07:20:12 -05:00
|
|
|
IntegrationSkip(t)
|
2022-11-24 10:17:24 -05:00
|
|
|
t.Parallel()
|
2022-11-11 07:20:12 -05:00
|
|
|
|
2022-10-26 07:31:30 -04:00
|
|
|
scenario, err := NewScenario()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create scenario: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-11-11 07:20:12 -05:00
|
|
|
spec := map[string]int{
|
2023-01-17 11:43:44 -05:00
|
|
|
"user1": len(TailscaleVersions) - 5,
|
2022-11-11 07:20:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec,
|
|
|
|
[]tsic.Option{tsic.WithSSH()},
|
|
|
|
hsic.WithACLPolicy(
|
2023-05-21 12:37:59 -04:00
|
|
|
&policy.ACLPolicy{
|
2022-11-11 07:20:12 -05:00
|
|
|
Groups: map[string][]string{
|
2023-01-17 11:43:44 -05:00
|
|
|
"group:integration-test": {"user1"},
|
2022-10-26 07:31:30 -04:00
|
|
|
},
|
2023-05-21 12:37:59 -04:00
|
|
|
ACLs: []policy.ACL{
|
2022-11-11 07:20:12 -05:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
|
|
|
},
|
2023-05-21 12:37:59 -04:00
|
|
|
SSHs: []policy.SSH{
|
2022-11-11 07:20:12 -05:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"group:integration-test"},
|
|
|
|
Destinations: []string{"group:integration-test"},
|
|
|
|
Users: []string{"ssh-it-user"},
|
|
|
|
},
|
2022-10-26 07:31:30 -04:00
|
|
|
},
|
|
|
|
},
|
2022-11-11 07:20:12 -05:00
|
|
|
),
|
2022-11-24 10:41:33 -05:00
|
|
|
hsic.WithConfigEnv(map[string]string{
|
2022-11-24 11:26:52 -05:00
|
|
|
"HEADSCALE_EXPERIMENTAL_FEATURE_SSH": "1",
|
2022-11-24 10:41:33 -05:00
|
|
|
}),
|
2022-11-11 07:20:12 -05:00
|
|
|
)
|
2022-10-26 07:31:30 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create headscale environment: %s", err)
|
|
|
|
}
|
2022-11-08 10:10:03 -05:00
|
|
|
|
|
|
|
allClients, err := scenario.ListTailscaleClients()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get clients: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-10-26 07:31:30 -04:00
|
|
|
err = scenario.WaitForTailscaleSync()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed wait for tailscale clients to be in sync: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-11-08 10:10:03 -05:00
|
|
|
_, err = scenario.ListTailscaleClientsFQDNs()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get FQDNs: %s", err)
|
|
|
|
}
|
2022-10-26 07:31:30 -04:00
|
|
|
|
2022-11-08 10:10:03 -05:00
|
|
|
for _, client := range allClients {
|
|
|
|
for _, peer := range allClients {
|
|
|
|
if client.Hostname() == peer.Hostname() {
|
|
|
|
continue
|
2022-10-26 07:31:30 -04:00
|
|
|
}
|
|
|
|
|
2022-11-19 06:41:12 -05:00
|
|
|
assertSSHHostname(t, client, peer)
|
2022-10-26 07:31:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to tear down scenario: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2022-11-11 07:20:12 -05:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
func TestSSHMultipleUsersAllToAll(t *testing.T) {
|
2022-11-11 07:20:12 -05:00
|
|
|
IntegrationSkip(t)
|
2022-11-24 10:17:24 -05:00
|
|
|
t.Parallel()
|
2022-11-11 07:20:12 -05:00
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create scenario: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-01-17 11:43:44 -05:00
|
|
|
"user1": len(TailscaleVersions) - 5,
|
|
|
|
"user2": len(TailscaleVersions) - 5,
|
2022-11-11 07:20:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec,
|
|
|
|
[]tsic.Option{tsic.WithSSH()},
|
|
|
|
hsic.WithACLPolicy(
|
2023-05-21 12:37:59 -04:00
|
|
|
&policy.ACLPolicy{
|
2022-11-11 07:20:12 -05:00
|
|
|
Groups: map[string][]string{
|
2023-01-17 11:43:44 -05:00
|
|
|
"group:integration-test": {"user1", "user2"},
|
2022-11-11 07:20:12 -05:00
|
|
|
},
|
2023-05-21 12:37:59 -04:00
|
|
|
ACLs: []policy.ACL{
|
2022-11-11 07:20:12 -05:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
|
|
|
},
|
2023-05-21 12:37:59 -04:00
|
|
|
SSHs: []policy.SSH{
|
2022-11-11 07:20:12 -05:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"group:integration-test"},
|
|
|
|
Destinations: []string{"group:integration-test"},
|
|
|
|
Users: []string{"ssh-it-user"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
2022-11-24 10:41:33 -05:00
|
|
|
hsic.WithConfigEnv(map[string]string{
|
2022-11-24 11:26:52 -05:00
|
|
|
"HEADSCALE_EXPERIMENTAL_FEATURE_SSH": "1",
|
2022-11-24 10:41:33 -05:00
|
|
|
}),
|
2022-11-11 07:20:12 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create headscale environment: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
nsOneClients, err := scenario.ListTailscaleClients("user1")
|
2022-11-11 07:20:12 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get clients: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
nsTwoClients, err := scenario.ListTailscaleClients("user2")
|
2022-11-11 07:20:12 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get clients: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.WaitForTailscaleSync()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed wait for tailscale clients to be in sync: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = scenario.ListTailscaleClientsFQDNs()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get FQDNs: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
testInterUserSSH := func(sourceClients []TailscaleClient, targetClients []TailscaleClient) {
|
2022-11-11 07:20:12 -05:00
|
|
|
for _, client := range sourceClients {
|
|
|
|
for _, peer := range targetClients {
|
2022-11-19 06:41:12 -05:00
|
|
|
assertSSHHostname(t, client, peer)
|
2022-11-11 07:20:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
testInterUserSSH(nsOneClients, nsTwoClients)
|
|
|
|
testInterUserSSH(nsTwoClients, nsOneClients)
|
2022-11-11 07:20:12 -05:00
|
|
|
|
2022-11-19 06:41:12 -05:00
|
|
|
err = scenario.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to tear down scenario: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSSHNoSSHConfigured(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
2022-11-24 10:17:24 -05:00
|
|
|
t.Parallel()
|
2022-11-19 06:41:12 -05:00
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create scenario: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-01-17 11:43:44 -05:00
|
|
|
"user1": len(TailscaleVersions) - 5,
|
2022-11-19 06:41:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec,
|
|
|
|
[]tsic.Option{tsic.WithSSH()},
|
|
|
|
hsic.WithACLPolicy(
|
2023-05-21 12:37:59 -04:00
|
|
|
&policy.ACLPolicy{
|
2022-11-19 06:41:12 -05:00
|
|
|
Groups: map[string][]string{
|
2023-01-17 11:43:44 -05:00
|
|
|
"group:integration-test": {"user1"},
|
2022-11-19 06:41:12 -05:00
|
|
|
},
|
2023-05-21 12:37:59 -04:00
|
|
|
ACLs: []policy.ACL{
|
2022-11-19 06:41:12 -05:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
|
|
|
},
|
2023-05-21 12:37:59 -04:00
|
|
|
SSHs: []policy.SSH{},
|
2022-11-19 06:41:12 -05:00
|
|
|
},
|
|
|
|
),
|
|
|
|
hsic.WithTestName("sshnoneconfigured"),
|
2022-11-24 10:41:33 -05:00
|
|
|
hsic.WithConfigEnv(map[string]string{
|
2022-11-24 11:26:52 -05:00
|
|
|
"HEADSCALE_EXPERIMENTAL_FEATURE_SSH": "1",
|
2022-11-24 10:41:33 -05:00
|
|
|
}),
|
2022-11-11 07:20:12 -05:00
|
|
|
)
|
2022-11-19 06:41:12 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create headscale environment: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
allClients, err := scenario.ListTailscaleClients()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get clients: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.WaitForTailscaleSync()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed wait for tailscale clients to be in sync: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = scenario.ListTailscaleClientsFQDNs()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get FQDNs: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, client := range allClients {
|
|
|
|
for _, peer := range allClients {
|
|
|
|
if client.Hostname() == peer.Hostname() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
assertSSHPermissionDenied(t, client, peer)
|
|
|
|
}
|
|
|
|
}
|
2022-11-11 07:20:12 -05:00
|
|
|
|
|
|
|
err = scenario.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to tear down scenario: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 06:41:12 -05:00
|
|
|
func TestSSHIsBlockedInACL(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
2022-11-24 10:17:24 -05:00
|
|
|
t.Parallel()
|
2022-11-11 07:20:12 -05:00
|
|
|
|
2022-11-19 06:41:12 -05:00
|
|
|
scenario, err := NewScenario()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create scenario: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-01-17 11:43:44 -05:00
|
|
|
"user1": len(TailscaleVersions) - 5,
|
2022-11-19 06:41:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec,
|
|
|
|
[]tsic.Option{tsic.WithSSH()},
|
|
|
|
hsic.WithACLPolicy(
|
2023-05-21 12:37:59 -04:00
|
|
|
&policy.ACLPolicy{
|
2022-11-19 06:41:12 -05:00
|
|
|
Groups: map[string][]string{
|
2023-01-17 11:43:44 -05:00
|
|
|
"group:integration-test": {"user1"},
|
2022-11-19 06:41:12 -05:00
|
|
|
},
|
2023-05-21 12:37:59 -04:00
|
|
|
ACLs: []policy.ACL{
|
2022-11-19 06:41:12 -05:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"*:80"},
|
|
|
|
},
|
|
|
|
},
|
2023-05-21 12:37:59 -04:00
|
|
|
SSHs: []policy.SSH{
|
2022-11-19 06:41:12 -05:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"group:integration-test"},
|
|
|
|
Destinations: []string{"group:integration-test"},
|
|
|
|
Users: []string{"ssh-it-user"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
hsic.WithTestName("sshisblockedinacl"),
|
2022-11-24 10:41:33 -05:00
|
|
|
hsic.WithConfigEnv(map[string]string{
|
2022-11-24 11:26:52 -05:00
|
|
|
"HEADSCALE_EXPERIMENTAL_FEATURE_SSH": "1",
|
2022-11-24 10:41:33 -05:00
|
|
|
}),
|
2022-11-19 06:41:12 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create headscale environment: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
allClients, err := scenario.ListTailscaleClients()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get clients: %s", err)
|
|
|
|
}
|
2022-11-11 07:20:12 -05:00
|
|
|
|
2022-11-19 06:41:12 -05:00
|
|
|
err = scenario.WaitForTailscaleSync()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed wait for tailscale clients to be in sync: %s", err)
|
|
|
|
}
|
2022-11-11 07:20:12 -05:00
|
|
|
|
2022-11-19 06:41:12 -05:00
|
|
|
_, err = scenario.ListTailscaleClientsFQDNs()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get FQDNs: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, client := range allClients {
|
|
|
|
for _, peer := range allClients {
|
|
|
|
if client.Hostname() == peer.Hostname() {
|
|
|
|
continue
|
2022-11-11 07:20:12 -05:00
|
|
|
}
|
|
|
|
|
2022-11-19 06:41:12 -05:00
|
|
|
assertSSHTimeout(t, client, peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to tear down scenario: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
func TestSSUserOnlyIsolation(t *testing.T) {
|
2022-11-19 06:41:12 -05:00
|
|
|
IntegrationSkip(t)
|
2022-11-24 10:17:24 -05:00
|
|
|
t.Parallel()
|
2022-11-19 06:41:12 -05:00
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create scenario: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-01-17 11:43:44 -05:00
|
|
|
"useracl1": len(TailscaleVersions) - 5,
|
|
|
|
"useracl2": len(TailscaleVersions) - 5,
|
2022-11-19 06:41:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec,
|
|
|
|
[]tsic.Option{tsic.WithSSH()},
|
|
|
|
hsic.WithACLPolicy(
|
2023-05-21 12:37:59 -04:00
|
|
|
&policy.ACLPolicy{
|
2022-11-19 06:41:12 -05:00
|
|
|
Groups: map[string][]string{
|
2023-01-17 11:43:44 -05:00
|
|
|
"group:ssh1": {"useracl1"},
|
|
|
|
"group:ssh2": {"useracl2"},
|
2022-11-19 06:41:12 -05:00
|
|
|
},
|
2023-05-21 12:37:59 -04:00
|
|
|
ACLs: []policy.ACL{
|
2022-11-19 06:41:12 -05:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
|
|
|
},
|
2023-05-21 12:37:59 -04:00
|
|
|
SSHs: []policy.SSH{
|
2022-11-19 06:41:12 -05:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"group:ssh1"},
|
|
|
|
Destinations: []string{"group:ssh1"},
|
|
|
|
Users: []string{"ssh-it-user"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"group:ssh2"},
|
|
|
|
Destinations: []string{"group:ssh2"},
|
|
|
|
Users: []string{"ssh-it-user"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
2023-01-17 11:43:44 -05:00
|
|
|
hsic.WithTestName("sshtwouseraclblock"),
|
2022-11-24 10:41:33 -05:00
|
|
|
hsic.WithConfigEnv(map[string]string{
|
2022-11-24 11:26:52 -05:00
|
|
|
"HEADSCALE_EXPERIMENTAL_FEATURE_SSH": "1",
|
2022-11-24 10:41:33 -05:00
|
|
|
}),
|
2022-11-19 06:41:12 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create headscale environment: %s", err)
|
|
|
|
}
|
2022-11-11 07:20:12 -05:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
ssh1Clients, err := scenario.ListTailscaleClients("useracl1")
|
2022-11-19 06:41:12 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get clients: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
ssh2Clients, err := scenario.ListTailscaleClients("useracl2")
|
2022-11-19 06:41:12 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get clients: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.WaitForTailscaleSync()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed wait for tailscale clients to be in sync: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = scenario.ListTailscaleClientsFQDNs()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get FQDNs: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-06-08 13:50:59 -04:00
|
|
|
for _, client := range ssh1Clients {
|
|
|
|
for _, peer := range ssh2Clients {
|
|
|
|
if client.Hostname() == peer.Hostname() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
assertSSHPermissionDenied(t, client, peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, client := range ssh2Clients {
|
|
|
|
for _, peer := range ssh1Clients {
|
|
|
|
if client.Hostname() == peer.Hostname() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
assertSSHPermissionDenied(t, client, peer)
|
|
|
|
}
|
|
|
|
}
|
2022-11-19 06:41:12 -05:00
|
|
|
|
|
|
|
for _, client := range ssh1Clients {
|
|
|
|
for _, peer := range ssh1Clients {
|
|
|
|
if client.Hostname() == peer.Hostname() {
|
|
|
|
continue
|
2022-11-11 07:20:12 -05:00
|
|
|
}
|
|
|
|
|
2022-11-19 06:41:12 -05:00
|
|
|
assertSSHHostname(t, client, peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, client := range ssh2Clients {
|
|
|
|
for _, peer := range ssh2Clients {
|
|
|
|
if client.Hostname() == peer.Hostname() {
|
|
|
|
continue
|
2022-11-11 07:20:12 -05:00
|
|
|
}
|
|
|
|
|
2022-11-19 06:41:12 -05:00
|
|
|
assertSSHHostname(t, client, peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to tear down scenario: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doSSH(t *testing.T, client TailscaleClient, peer TailscaleClient) (string, string, error) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
peerFQDN, _ := peer.FQDN()
|
|
|
|
|
|
|
|
command := []string{
|
|
|
|
"ssh", "-o StrictHostKeyChecking=no", "-o ConnectTimeout=1",
|
|
|
|
fmt.Sprintf("%s@%s", "ssh-it-user", peerFQDN),
|
|
|
|
"'hostname'",
|
|
|
|
}
|
|
|
|
|
|
|
|
return retry(10, 1*time.Second, func() (string, string, error) {
|
|
|
|
return client.Execute(command)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertSSHHostname(t *testing.T, client TailscaleClient, peer TailscaleClient) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
result, _, err := doSSH(t, client, peer)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-11-21 07:56:18 -05:00
|
|
|
assert.Contains(t, peer.ID(), strings.ReplaceAll(result, "\n", ""))
|
2022-11-19 06:41:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func assertSSHPermissionDenied(t *testing.T, client TailscaleClient, peer TailscaleClient) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
result, stderr, err := doSSH(t, client, peer)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
assert.Empty(t, result)
|
|
|
|
|
|
|
|
assert.Contains(t, stderr, "Permission denied (tailscale)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertSSHTimeout(t *testing.T, client TailscaleClient, peer TailscaleClient) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
result, stderr, err := doSSH(t, client, peer)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Empty(t, result)
|
|
|
|
|
|
|
|
assert.Contains(t, stderr, "Connection timed out")
|
2022-11-11 07:20:12 -05:00
|
|
|
}
|