2022-10-26 07:31:30 -04:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-08 10:10:03 -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
|
|
|
|
|
|
|
"github.com/juanfont/headscale"
|
|
|
|
)
|
|
|
|
|
2022-11-08 10:10:03 -05:00
|
|
|
func TestSSHOneNamespaceAllToAll(t *testing.T) {
|
2022-10-26 07:31:30 -04:00
|
|
|
IntegrationSkip(t)
|
|
|
|
|
2022-11-08 10:10:03 -05:00
|
|
|
retry := func(times int, sleepInverval time.Duration, doWork func() (string, error)) (string, error) {
|
|
|
|
var err error
|
|
|
|
for attempts := 0; attempts < times; attempts++ {
|
|
|
|
result, err := doWork()
|
|
|
|
if err == nil {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
time.Sleep(sleepInverval)
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-10-26 07:31:30 -04:00
|
|
|
scenario, err := NewScenario()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create scenario: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
spec := &HeadscaleSpec{
|
|
|
|
namespaces: map[string]int{
|
2022-11-08 10:10:03 -05:00
|
|
|
"namespace1": len(TailscaleVersions) - 5,
|
2022-10-26 07:31:30 -04:00
|
|
|
},
|
|
|
|
enableSSH: true,
|
|
|
|
acl: &headscale.ACLPolicy{
|
|
|
|
Groups: map[string][]string{
|
2022-11-08 10:10:03 -05:00
|
|
|
"group:integration-test": {"namespace1"},
|
2022-10-26 07:31:30 -04:00
|
|
|
},
|
|
|
|
ACLs: []headscale.ACL{
|
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SSHs: []headscale.SSH{
|
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"group:integration-test"},
|
|
|
|
Destinations: []string{"group:integration-test"},
|
|
|
|
Users: []string{"ssh-it-user"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec)
|
|
|
|
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
|
|
|
success := 0
|
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-08 10:10:03 -05:00
|
|
|
clientFQDN, _ := client.FQDN()
|
|
|
|
peerFQDN, _ := peer.FQDN()
|
|
|
|
|
|
|
|
t.Run(
|
|
|
|
fmt.Sprintf("%s-%s", clientFQDN, peerFQDN),
|
|
|
|
func(t *testing.T) {
|
|
|
|
command := []string{
|
|
|
|
"ssh", "-o StrictHostKeyChecking=no", "-o ConnectTimeout=1",
|
|
|
|
fmt.Sprintf("%s@%s", "ssh-it-user", peer.Hostname()),
|
|
|
|
"'hostname'",
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := retry(10, 1*time.Second, func() (string, error) {
|
|
|
|
return client.Execute(command)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to execute command over SSH: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(peer.ID(), result) {
|
|
|
|
t.Logf(
|
|
|
|
"failed to get correct container ID from %s, expected: %s, got: %s",
|
|
|
|
peer.Hostname(),
|
|
|
|
peer.ID(),
|
|
|
|
result,
|
|
|
|
)
|
|
|
|
t.Fail()
|
|
|
|
} else {
|
|
|
|
success++
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2022-10-26 07:31:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 10:10:03 -05:00
|
|
|
t.Logf(
|
|
|
|
"%d successful pings out of %d",
|
|
|
|
success,
|
|
|
|
(len(allClients)*len(allClients))-len(allClients),
|
|
|
|
)
|
|
|
|
|
2022-10-26 07:31:30 -04:00
|
|
|
err = scenario.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to tear down scenario: %s", err)
|
|
|
|
}
|
|
|
|
}
|