2022-10-13 10:03:38 -04:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-10-14 06:17:59 -04:00
|
|
|
"net/netip"
|
2022-10-13 10:03:38 -04:00
|
|
|
"os"
|
|
|
|
"sync"
|
2022-10-18 05:59:28 -04:00
|
|
|
"time"
|
2022-10-13 10:03:38 -04:00
|
|
|
|
|
|
|
"github.com/juanfont/headscale"
|
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
|
|
|
"github.com/juanfont/headscale/integration/dockertestutil"
|
|
|
|
"github.com/juanfont/headscale/integration/hsic"
|
|
|
|
"github.com/juanfont/headscale/integration/tsic"
|
|
|
|
"github.com/ory/dockertest/v3"
|
2022-11-14 08:27:02 -05:00
|
|
|
"github.com/puzpuzpuz/xsync/v2"
|
2022-10-13 10:03:38 -04:00
|
|
|
)
|
|
|
|
|
2022-10-18 06:21:38 -04:00
|
|
|
const (
|
|
|
|
scenarioHashLength = 6
|
|
|
|
maxWait = 60 * time.Second
|
|
|
|
)
|
2022-10-13 10:03:38 -04:00
|
|
|
|
2022-10-18 06:09:10 -04:00
|
|
|
var (
|
|
|
|
errNoHeadscaleAvailable = errors.New("no headscale available")
|
2023-01-17 14:36:46 -05:00
|
|
|
errNoUserAvailable = errors.New("no user available")
|
2022-11-14 12:10:29 -05:00
|
|
|
|
|
|
|
// Tailscale started adding TS2021 support in CapabilityVersion>=28 (v1.24.0), but
|
|
|
|
// proper support in Headscale was only added for CapabilityVersion>=39 clients (v1.30.0).
|
|
|
|
tailscaleVersions2021 = []string{
|
2022-10-18 06:09:10 -04:00
|
|
|
"head",
|
|
|
|
"unstable",
|
2022-12-05 09:06:55 -05:00
|
|
|
"1.34.0",
|
|
|
|
"1.32.3",
|
2022-10-18 06:09:10 -04:00
|
|
|
"1.30.2",
|
2022-11-14 03:45:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
tailscaleVersions2019 = []string{
|
2022-11-14 12:10:29 -05:00
|
|
|
"1.28.0",
|
|
|
|
"1.26.2",
|
2022-10-18 06:09:10 -04:00
|
|
|
"1.24.2",
|
|
|
|
"1.22.2",
|
|
|
|
"1.20.4",
|
|
|
|
}
|
2022-11-14 03:45:01 -05:00
|
|
|
|
|
|
|
// tailscaleVersionsUnavailable = []string{
|
|
|
|
// // These versions seem to fail when fetching from apt.
|
2023-01-02 09:01:29 -05:00
|
|
|
// "1.18.2",
|
2022-12-09 12:27:42 -05:00
|
|
|
// "1.16.2",
|
2022-11-14 03:45:01 -05:00
|
|
|
// "1.14.6",
|
|
|
|
// "1.12.4",
|
|
|
|
// "1.10.2",
|
|
|
|
// "1.8.7",
|
2022-11-14 08:27:29 -05:00
|
|
|
// }.
|
2022-11-14 03:45:01 -05:00
|
|
|
|
|
|
|
TailscaleVersions = append(
|
|
|
|
tailscaleVersions2021,
|
|
|
|
tailscaleVersions2019...,
|
|
|
|
)
|
2022-10-18 06:09:10 -04:00
|
|
|
)
|
2022-10-13 10:03:38 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
type User struct {
|
2022-10-21 07:17:54 -04:00
|
|
|
Clients map[string]TailscaleClient
|
2022-10-13 10:03:38 -04:00
|
|
|
|
|
|
|
createWaitGroup sync.WaitGroup
|
|
|
|
joinWaitGroup sync.WaitGroup
|
2022-10-18 05:59:28 -04:00
|
|
|
syncWaitGroup sync.WaitGroup
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
2022-11-02 05:01:21 -04:00
|
|
|
// TODO(kradalby): make control server configurable, test correctness with Tailscale SaaS.
|
2022-10-13 10:03:38 -04:00
|
|
|
type Scenario struct {
|
|
|
|
// TODO(kradalby): support multiple headcales for later, currently only
|
|
|
|
// use one.
|
2022-11-14 08:27:02 -05:00
|
|
|
controlServers *xsync.MapOf[string, ControlServer]
|
2022-10-13 10:03:38 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
users map[string]*User
|
2022-10-13 10:03:38 -04:00
|
|
|
|
|
|
|
pool *dockertest.Pool
|
|
|
|
network *dockertest.Network
|
2022-11-14 08:27:02 -05:00
|
|
|
|
|
|
|
headscaleLock sync.Mutex
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewScenario() (*Scenario, error) {
|
|
|
|
hash, err := headscale.GenerateRandomStringDNSSafe(scenarioHashLength)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := dockertest.NewPool("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not connect to docker: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-10-18 06:19:43 -04:00
|
|
|
pool.MaxWait = maxWait
|
2022-10-18 05:59:28 -04:00
|
|
|
|
2022-10-13 10:03:38 -04:00
|
|
|
networkName := fmt.Sprintf("hs-%s", hash)
|
|
|
|
if overrideNetworkName := os.Getenv("HEADSCALE_TEST_NETWORK_NAME"); overrideNetworkName != "" {
|
|
|
|
networkName = overrideNetworkName
|
|
|
|
}
|
|
|
|
|
|
|
|
network, err := dockertestutil.GetFirstOrCreateNetwork(pool, networkName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create or get network: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-10-14 04:38:27 -04:00
|
|
|
// We run the test suite in a docker container that calls a couple of endpoints for
|
|
|
|
// readiness checks, this ensures that we can run the tests with individual networks
|
|
|
|
// and have the client reach the different containers
|
|
|
|
err = dockertestutil.AddContainerToNetwork(pool, network, "headscale-test-suite")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to add test suite container to network: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:03:38 -04:00
|
|
|
return &Scenario{
|
2022-11-14 08:27:02 -05:00
|
|
|
controlServers: xsync.NewMapOf[ControlServer](),
|
2023-01-17 14:36:46 -05:00
|
|
|
users: make(map[string]*User),
|
2022-10-13 10:03:38 -04:00
|
|
|
|
|
|
|
pool: pool,
|
|
|
|
network: network,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scenario) Shutdown() error {
|
2022-11-14 08:27:02 -05:00
|
|
|
s.controlServers.Range(func(_ string, control ControlServer) bool {
|
2022-10-13 10:03:38 -04:00
|
|
|
err := control.Shutdown()
|
|
|
|
if err != nil {
|
2022-11-14 08:27:02 -05:00
|
|
|
log.Printf(
|
|
|
|
"Failed to shut down control: %s",
|
|
|
|
fmt.Errorf("failed to tear down control: %w", err),
|
|
|
|
)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
2022-11-14 08:27:02 -05:00
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
2022-10-13 10:03:38 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
for userName, user := range s.users {
|
|
|
|
for _, client := range user.Clients {
|
|
|
|
log.Printf("removing client %s in user %s", client.Hostname(), userName)
|
2022-10-13 10:03:38 -04:00
|
|
|
err := client.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to tear down client: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 04:38:27 -04:00
|
|
|
if err := s.pool.RemoveNetwork(s.network); err != nil {
|
|
|
|
return fmt.Errorf("failed to remove network: %w", err)
|
|
|
|
}
|
2022-10-13 10:03:38 -04:00
|
|
|
|
|
|
|
// TODO(kradalby): This seem redundant to the previous call
|
|
|
|
// if err := s.network.Close(); err != nil {
|
|
|
|
// return fmt.Errorf("failed to tear down network: %w", err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) Users() []string {
|
|
|
|
users := make([]string, 0)
|
|
|
|
for user := range s.users {
|
|
|
|
users = append(users, user)
|
2022-10-23 06:41:35 -04:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return users
|
2022-10-23 06:41:35 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 10:03:38 -04:00
|
|
|
/// Headscale related stuff
|
|
|
|
// Note: These functions assume that there is a _single_ headscale instance for now
|
|
|
|
|
|
|
|
// TODO(kradalby): make port and headscale configurable, multiple instances support?
|
2022-11-14 08:27:02 -05:00
|
|
|
func (s *Scenario) Headscale(opts ...hsic.Option) (ControlServer, error) {
|
|
|
|
s.headscaleLock.Lock()
|
|
|
|
defer s.headscaleLock.Unlock()
|
|
|
|
|
|
|
|
if headscale, ok := s.controlServers.Load("headscale"); ok {
|
|
|
|
return headscale, nil
|
|
|
|
}
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
headscale, err := hsic.New(s.pool, s.network, opts...)
|
2022-10-13 10:03:38 -04:00
|
|
|
if err != nil {
|
2022-11-14 08:27:02 -05:00
|
|
|
return nil, fmt.Errorf("failed to create headscale container: %w", err)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
2022-11-10 11:06:10 -05:00
|
|
|
err = headscale.WaitForReady()
|
|
|
|
if err != nil {
|
2022-11-14 08:27:02 -05:00
|
|
|
return nil, fmt.Errorf("failed reach headscale container: %w", err)
|
2022-11-10 11:06:10 -05:00
|
|
|
}
|
|
|
|
|
2022-11-14 08:27:02 -05:00
|
|
|
s.controlServers.Store("headscale", headscale)
|
2022-10-13 10:03:38 -04:00
|
|
|
|
2022-11-14 08:27:02 -05:00
|
|
|
return headscale, nil
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) CreatePreAuthKey(user string, reusable bool, ephemeral bool) (*v1.PreAuthKey, error) {
|
2022-11-14 08:27:02 -05:00
|
|
|
if headscale, err := s.Headscale(); err == nil {
|
2023-01-17 11:43:44 -05:00
|
|
|
key, err := headscale.CreateAuthKey(user, reusable, ephemeral)
|
2022-10-13 10:03:38 -04:00
|
|
|
if err != nil {
|
2023-01-17 11:43:44 -05:00
|
|
|
return nil, fmt.Errorf("failed to create user: %w", err)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return nil, fmt.Errorf("failed to create user: %w", errNoHeadscaleAvailable)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) CreateUser(user string) error {
|
2022-11-14 08:27:02 -05:00
|
|
|
if headscale, err := s.Headscale(); err == nil {
|
2023-01-17 11:43:44 -05:00
|
|
|
err := headscale.CreateUser(user)
|
2022-10-13 10:03:38 -04:00
|
|
|
if err != nil {
|
2023-01-17 11:43:44 -05:00
|
|
|
return fmt.Errorf("failed to create user: %w", err)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
s.users[user] = &User{
|
2022-10-21 07:17:54 -04:00
|
|
|
Clients: make(map[string]TailscaleClient),
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return fmt.Errorf("failed to create user: %w", errNoHeadscaleAvailable)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Client related stuff
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) CreateTailscaleNodesInUser(
|
|
|
|
userStr string,
|
2022-10-18 05:59:28 -04:00
|
|
|
requestedVersion string,
|
2022-10-13 10:03:38 -04:00
|
|
|
count int,
|
2022-11-08 10:10:03 -05:00
|
|
|
opts ...tsic.Option,
|
2022-10-13 10:03:38 -04:00
|
|
|
) error {
|
2023-01-17 11:43:44 -05:00
|
|
|
if user, ok := s.users[userStr]; ok {
|
2022-10-13 10:03:38 -04:00
|
|
|
for i := 0; i < count; i++ {
|
2022-10-18 05:59:28 -04:00
|
|
|
version := requestedVersion
|
|
|
|
if requestedVersion == "all" {
|
|
|
|
version = TailscaleVersions[i%len(TailscaleVersions)]
|
|
|
|
}
|
|
|
|
|
2022-11-14 08:27:02 -05:00
|
|
|
headscale, err := s.Headscale()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create tailscale node: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cert := headscale.GetCert()
|
|
|
|
hostname := headscale.GetHostname()
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
user.createWaitGroup.Add(1)
|
2022-10-13 10:03:38 -04:00
|
|
|
|
2022-11-08 10:10:03 -05:00
|
|
|
opts = append(opts,
|
|
|
|
tsic.WithHeadscaleTLS(cert),
|
|
|
|
tsic.WithHeadscaleName(hostname),
|
|
|
|
)
|
|
|
|
|
2022-10-13 10:03:38 -04:00
|
|
|
go func() {
|
2023-01-17 11:43:44 -05:00
|
|
|
defer user.createWaitGroup.Done()
|
2022-10-13 10:03:38 -04:00
|
|
|
|
|
|
|
// TODO(kradalby): error handle this
|
2022-11-06 14:22:21 -05:00
|
|
|
tsClient, err := tsic.New(
|
|
|
|
s.pool,
|
|
|
|
version,
|
|
|
|
s.network,
|
2022-11-08 10:10:03 -05:00
|
|
|
opts...,
|
2022-11-06 14:22:21 -05:00
|
|
|
)
|
2022-10-13 10:03:38 -04:00
|
|
|
if err != nil {
|
|
|
|
// return fmt.Errorf("failed to add tailscale node: %w", err)
|
2022-11-06 14:22:21 -05:00
|
|
|
log.Printf("failed to create tailscale node: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tsClient.WaitForReady()
|
|
|
|
if err != nil {
|
|
|
|
// return fmt.Errorf("failed to add tailscale node: %w", err)
|
|
|
|
log.Printf("failed to wait for tailscaled: %s", err)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
user.Clients[tsClient.Hostname()] = tsClient
|
2022-10-13 10:03:38 -04:00
|
|
|
}()
|
|
|
|
}
|
2023-01-17 11:43:44 -05:00
|
|
|
user.createWaitGroup.Wait()
|
2022-10-13 10:03:38 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return fmt.Errorf("failed to add tailscale node: %w", errNoUserAvailable)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scenario) RunTailscaleUp(
|
2023-01-17 11:43:44 -05:00
|
|
|
userStr, loginServer, authKey string,
|
2022-10-13 10:03:38 -04:00
|
|
|
) error {
|
2023-01-17 11:43:44 -05:00
|
|
|
if user, ok := s.users[userStr]; ok {
|
|
|
|
for _, client := range user.Clients {
|
|
|
|
user.joinWaitGroup.Add(1)
|
2022-10-13 10:03:38 -04:00
|
|
|
|
2022-10-21 07:17:54 -04:00
|
|
|
go func(c TailscaleClient) {
|
2023-01-17 11:43:44 -05:00
|
|
|
defer user.joinWaitGroup.Done()
|
2022-10-13 10:03:38 -04:00
|
|
|
|
|
|
|
// TODO(kradalby): error handle this
|
2022-10-18 05:59:28 -04:00
|
|
|
_ = c.Up(loginServer, authKey)
|
|
|
|
}(client)
|
2022-11-13 10:58:20 -05:00
|
|
|
|
|
|
|
err := client.WaitForReady()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error waiting for client %s to be ready: %s", client.Hostname(), err)
|
|
|
|
}
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
2022-11-13 10:58:20 -05:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
user.joinWaitGroup.Wait()
|
2022-10-13 10:03:38 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return fmt.Errorf("failed to up tailscale node: %w", errNoUserAvailable)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
2022-10-14 06:17:59 -04:00
|
|
|
|
2022-10-18 05:59:28 -04:00
|
|
|
func (s *Scenario) CountTailscale() int {
|
|
|
|
count := 0
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
for _, user := range s.users {
|
|
|
|
count += len(user.Clients)
|
2022-10-18 05:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scenario) WaitForTailscaleSync() error {
|
|
|
|
tsCount := s.CountTailscale()
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
for _, user := range s.users {
|
|
|
|
for _, client := range user.Clients {
|
|
|
|
user.syncWaitGroup.Add(1)
|
2022-10-18 05:59:28 -04:00
|
|
|
|
2022-10-21 07:17:54 -04:00
|
|
|
go func(c TailscaleClient) {
|
2023-01-17 11:43:44 -05:00
|
|
|
defer user.syncWaitGroup.Done()
|
2022-10-18 05:59:28 -04:00
|
|
|
|
|
|
|
// TODO(kradalby): error handle this
|
|
|
|
_ = c.WaitForPeers(tsCount)
|
|
|
|
}(client)
|
|
|
|
}
|
2023-01-17 11:43:44 -05:00
|
|
|
user.syncWaitGroup.Wait()
|
2022-10-18 05:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateHeadscaleEnv is a conventient method returning a set up Headcale
|
|
|
|
// test environment with nodes of all versions, joined to the server with X
|
2023-01-17 11:43:44 -05:00
|
|
|
// users.
|
2022-11-08 10:10:03 -05:00
|
|
|
func (s *Scenario) CreateHeadscaleEnv(
|
2023-01-17 11:43:44 -05:00
|
|
|
users map[string]int,
|
2022-11-08 10:10:03 -05:00
|
|
|
tsOpts []tsic.Option,
|
|
|
|
opts ...hsic.Option,
|
|
|
|
) error {
|
2022-11-14 08:27:02 -05:00
|
|
|
headscale, err := s.Headscale(opts...)
|
2022-10-18 05:59:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
for userName, clientCount := range users {
|
|
|
|
err = s.CreateUser(userName)
|
2022-10-18 05:59:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
err = s.CreateTailscaleNodesInUser(userName, "all", clientCount, tsOpts...)
|
2022-10-18 05:59:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
key, err := s.CreatePreAuthKey(userName, true, false)
|
2022-10-18 05:59:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
err = s.RunTailscaleUp(userName, headscale.GetEndpoint(), key.GetKey())
|
2022-10-18 05:59:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) GetIPs(user string) ([]netip.Addr, error) {
|
2022-10-14 06:17:59 -04:00
|
|
|
var ips []netip.Addr
|
2023-01-17 11:43:44 -05:00
|
|
|
if ns, ok := s.users[user]; ok {
|
2022-10-14 06:17:59 -04:00
|
|
|
for _, client := range ns.Clients {
|
|
|
|
clientIps, err := client.IPs()
|
|
|
|
if err != nil {
|
|
|
|
return ips, fmt.Errorf("failed to get ips: %w", err)
|
|
|
|
}
|
|
|
|
ips = append(ips, clientIps...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips, nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return ips, fmt.Errorf("failed to get ips: %w", errNoUserAvailable)
|
2022-10-14 06:17:59 -04:00
|
|
|
}
|
2022-10-18 05:59:28 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) GetClients(user string) ([]TailscaleClient, error) {
|
2022-10-21 07:17:54 -04:00
|
|
|
var clients []TailscaleClient
|
2023-01-17 11:43:44 -05:00
|
|
|
if ns, ok := s.users[user]; ok {
|
2022-10-18 05:59:28 -04:00
|
|
|
for _, client := range ns.Clients {
|
|
|
|
clients = append(clients, client)
|
|
|
|
}
|
|
|
|
|
|
|
|
return clients, nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return clients, fmt.Errorf("failed to get clients: %w", errNoUserAvailable)
|
2022-10-18 05:59:28 -04:00
|
|
|
}
|
2022-10-23 06:41:35 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) ListTailscaleClients(users ...string) ([]TailscaleClient, error) {
|
2022-10-23 06:41:35 -04:00
|
|
|
var allClients []TailscaleClient
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
if len(users) == 0 {
|
|
|
|
users = s.Users()
|
2022-10-23 06:41:35 -04:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
for _, user := range users {
|
|
|
|
clients, err := s.GetClients(user)
|
2022-10-23 06:41:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
allClients = append(allClients, clients...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return allClients, nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) ListTailscaleClientsIPs(users ...string) ([]netip.Addr, error) {
|
2022-10-23 06:41:35 -04:00
|
|
|
var allIps []netip.Addr
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
if len(users) == 0 {
|
|
|
|
users = s.Users()
|
2022-10-23 06:41:35 -04:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
for _, user := range users {
|
|
|
|
ips, err := s.GetIPs(user)
|
2022-10-23 06:41:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
allIps = append(allIps, ips...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return allIps, nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) ListTailscaleClientsFQDNs(users ...string) ([]string, error) {
|
2022-10-23 06:41:35 -04:00
|
|
|
allFQDNs := make([]string, 0)
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
clients, err := s.ListTailscaleClients(users...)
|
2022-10-23 06:41:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, client := range clients {
|
|
|
|
fqdn, err := client.FQDN()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
allFQDNs = append(allFQDNs, fqdn)
|
|
|
|
}
|
|
|
|
|
|
|
|
return allFQDNs, nil
|
|
|
|
}
|
2022-12-22 10:24:37 -05:00
|
|
|
|
|
|
|
func (s *Scenario) WaitForTailscaleLogout() {
|
2023-01-17 11:43:44 -05:00
|
|
|
for _, user := range s.users {
|
|
|
|
for _, client := range user.Clients {
|
|
|
|
user.syncWaitGroup.Add(1)
|
2022-12-22 10:24:37 -05:00
|
|
|
|
|
|
|
go func(c TailscaleClient) {
|
2023-01-17 11:43:44 -05:00
|
|
|
defer user.syncWaitGroup.Done()
|
2022-12-22 10:24:37 -05:00
|
|
|
|
|
|
|
// TODO(kradalby): error handle this
|
|
|
|
_ = c.WaitForLogout()
|
|
|
|
}(client)
|
|
|
|
}
|
2023-01-17 11:43:44 -05:00
|
|
|
user.syncWaitGroup.Wait()
|
2022-12-22 10:24:37 -05:00
|
|
|
}
|
|
|
|
}
|