2022-11-03 16:40:09 +00:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2022-11-03 23:27:00 +00:00
|
|
|
"context"
|
2022-11-04 10:41:45 +00:00
|
|
|
"errors"
|
2022-11-03 16:40:09 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-11-03 23:23:20 +00:00
|
|
|
"log"
|
2022-11-03 16:40:09 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2022-11-04 10:41:45 +00:00
|
|
|
var errParseAuthPage = errors.New("failed to parse auth page")
|
|
|
|
|
2022-11-03 16:40:09 +00:00
|
|
|
type AuthWebFlowScenario struct {
|
|
|
|
*Scenario
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthWebFlowAuthenticationPingAll(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
|
|
|
|
baseScenario, err := NewScenario()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create scenario: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
scenario := AuthWebFlowScenario{
|
|
|
|
Scenario: baseScenario,
|
|
|
|
}
|
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
"namespace1": len(TailscaleVersions),
|
|
|
|
"namespace2": len(TailscaleVersions),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
allIps, err := scenario.ListTailscaleClientsIPs()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
success := 0
|
|
|
|
|
|
|
|
for _, client := range allClients {
|
|
|
|
for _, ip := range allIps {
|
|
|
|
err := client.Ping(ip.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to ping %s from %s: %s", ip, client.Hostname(), err)
|
|
|
|
} else {
|
|
|
|
success++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps))
|
|
|
|
|
|
|
|
err = scenario.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to tear down scenario: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AuthWebFlowScenario) CreateHeadscaleEnv(namespaces map[string]int) error {
|
|
|
|
err := s.StartHeadscale()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.Headscale().WaitForReady()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for namespaceName, clientCount := range namespaces {
|
2022-11-03 23:10:36 +00:00
|
|
|
log.Printf("creating namespace %s with %d clients", namespaceName, clientCount)
|
2022-11-03 16:40:09 +00:00
|
|
|
err = s.CreateNamespace(namespaceName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.CreateTailscaleNodesInNamespace(namespaceName, "all", clientCount)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.runTailscaleUp(namespaceName, s.Headscale().GetEndpoint())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AuthWebFlowScenario) runTailscaleUp(
|
|
|
|
namespaceStr, loginServer string,
|
|
|
|
) error {
|
2022-11-03 23:10:36 +00:00
|
|
|
log.Printf("running tailscale up's for namespace %s", namespaceStr)
|
2022-11-03 16:40:09 +00:00
|
|
|
if namespace, ok := s.namespaces[namespaceStr]; ok {
|
|
|
|
for _, client := range namespace.Clients {
|
2022-11-03 23:10:36 +00:00
|
|
|
namespace.joinWaitGroup.Add(1)
|
2022-11-03 16:40:09 +00:00
|
|
|
|
2022-11-03 23:10:36 +00:00
|
|
|
go func(c TailscaleClient) {
|
|
|
|
defer namespace.joinWaitGroup.Done()
|
2022-11-03 16:40:09 +00:00
|
|
|
|
2022-11-03 23:10:36 +00:00
|
|
|
// TODO(juanfont): error handle this
|
|
|
|
loginURL, err := c.UpWithLoginURL(loginServer)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to run tailscale up: %s", err)
|
|
|
|
}
|
2022-11-03 16:40:09 +00:00
|
|
|
|
2022-11-03 23:10:36 +00:00
|
|
|
err = s.runHeadscaleRegister(namespaceStr, loginURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to register client: %s", err)
|
|
|
|
}
|
|
|
|
}(client)
|
2022-11-03 16:40:09 +00:00
|
|
|
}
|
2022-11-03 23:10:36 +00:00
|
|
|
namespace.joinWaitGroup.Wait()
|
2022-11-03 16:40:09 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("failed to up tailscale node: %w", errNoNamespaceAvailable)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AuthWebFlowScenario) runHeadscaleRegister(namespaceStr string, loginURL *url.URL) error {
|
2022-11-03 23:10:36 +00:00
|
|
|
log.Printf("loginURL: %s", loginURL)
|
|
|
|
loginURL.Host = fmt.Sprintf("%s:8080", s.Headscale().GetIP())
|
|
|
|
loginURL.Scheme = "http"
|
|
|
|
|
2022-11-03 23:27:00 +00:00
|
|
|
httpClient := &http.Client{}
|
|
|
|
ctx := context.Background()
|
|
|
|
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, loginURL.String(), nil)
|
|
|
|
resp, err := httpClient.Do(req)
|
2022-11-03 16:40:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-03 23:23:20 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2022-11-03 16:40:09 +00:00
|
|
|
// see api.go HTML template
|
2022-11-04 10:41:45 +00:00
|
|
|
codeSep := strings.Split(string(body), "</code>")
|
|
|
|
if len(codeSep) != 2 {
|
|
|
|
return errParseAuthPage
|
|
|
|
}
|
|
|
|
|
|
|
|
keySep := strings.Split(codeSep[0], "key ")
|
|
|
|
if len(keySep) != 2 {
|
|
|
|
return errParseAuthPage
|
|
|
|
}
|
|
|
|
key := keySep[1]
|
|
|
|
log.Printf("registering node %s", key)
|
|
|
|
|
2022-11-03 16:40:09 +00:00
|
|
|
if headscale, ok := s.controlServers["headscale"]; ok {
|
2022-11-03 23:23:20 +00:00
|
|
|
_, err = headscale.Execute([]string{"headscale", "-n", namespaceStr, "nodes", "register", "--key", key})
|
2022-11-03 16:40:09 +00:00
|
|
|
if err != nil {
|
2022-11-03 23:10:36 +00:00
|
|
|
log.Printf("failed to register node: %s", err)
|
2022-11-03 23:23:20 +00:00
|
|
|
|
2022-11-03 16:40:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-11-03 23:10:36 +00:00
|
|
|
|
|
|
|
return nil
|
2022-11-03 16:40:09 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 23:10:36 +00:00
|
|
|
return fmt.Errorf("failed to find headscale: %w", errNoHeadscaleAvailable)
|
2022-11-03 16:40:09 +00:00
|
|
|
}
|