headscale/integration/auth_web_flow_test.go

337 lines
7.3 KiB
Go
Raw Normal View History

2022-11-13 22:47:24 +01:00
package integration
import (
"context"
"crypto/tls"
2022-11-13 22:47:24 +01:00
"errors"
"fmt"
"io"
"log"
"net/http"
"net/netip"
2022-11-13 22:47:24 +01:00
"net/url"
"strings"
"testing"
"github.com/juanfont/headscale/integration/hsic"
"github.com/samber/lo"
2022-11-13 22:47:24 +01:00
)
var errParseAuthPage = errors.New("failed to parse auth page")
type AuthWebFlowScenario struct {
*Scenario
}
func TestAuthWebFlowAuthenticationPingAll(t *testing.T) {
IntegrationSkip(t)
t.Parallel()
2022-11-13 22:47:24 +01:00
baseScenario, err := NewScenario(dockertestMaxWait())
2022-11-13 22:47:24 +01:00
if err != nil {
t.Fatalf("failed to create scenario: %s", err)
2022-11-13 22:47:24 +01:00
}
scenario := AuthWebFlowScenario{
Scenario: baseScenario,
}
defer scenario.ShutdownAssertNoPanics(t)
2022-11-13 22:47:24 +01:00
spec := map[string]int{
"user1": len(MustTestVersions),
"user2": len(MustTestVersions),
2022-11-13 22:47:24 +01:00
}
err = scenario.CreateHeadscaleEnv(
spec,
hsic.WithTestName("webauthping"),
hsic.WithEmbeddedDERPServerOnly(),
hsic.WithTLS(),
hsic.WithHostnameAsServerURL(),
)
assertNoErrHeadscaleEnv(t, err)
2022-11-13 22:47:24 +01:00
allClients, err := scenario.ListTailscaleClients()
assertNoErrListClients(t, err)
2022-11-13 22:47:24 +01:00
allIps, err := scenario.ListTailscaleClientsIPs()
assertNoErrListClientIPs(t, err)
2022-11-13 22:47:24 +01:00
err = scenario.WaitForTailscaleSync()
assertNoErrSync(t, err)
2022-11-13 22:47:24 +01:00
// assertClientsState(t, allClients)
allAddrs := lo.Map(allIps, func(x netip.Addr, index int) string {
return x.String()
})
success := pingAllHelper(t, allClients, allAddrs)
t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps))
}
func TestAuthWebFlowLogoutAndRelogin(t *testing.T) {
IntegrationSkip(t)
t.Parallel()
baseScenario, err := NewScenario(dockertestMaxWait())
assertNoErr(t, err)
scenario := AuthWebFlowScenario{
Scenario: baseScenario,
}
defer scenario.ShutdownAssertNoPanics(t)
spec := map[string]int{
"user1": len(MustTestVersions),
"user2": len(MustTestVersions),
}
err = scenario.CreateHeadscaleEnv(spec, hsic.WithTestName("weblogout"))
assertNoErrHeadscaleEnv(t, err)
allClients, err := scenario.ListTailscaleClients()
assertNoErrListClients(t, err)
allIps, err := scenario.ListTailscaleClientsIPs()
assertNoErrListClientIPs(t, err)
err = scenario.WaitForTailscaleSync()
assertNoErrSync(t, err)
// assertClientsState(t, allClients)
allAddrs := lo.Map(allIps, func(x netip.Addr, index int) string {
return x.String()
})
success := pingAllHelper(t, allClients, allAddrs)
t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps))
clientIPs := make(map[TailscaleClient][]netip.Addr)
for _, client := range allClients {
ips, err := client.IPs()
if err != nil {
t.Fatalf("failed to get IPs for client %s: %s", client.Hostname(), err)
}
clientIPs[client] = ips
}
for _, client := range allClients {
2022-12-21 22:29:52 +00:00
err := client.Logout()
if err != nil {
t.Fatalf("failed to logout client %s: %s", client.Hostname(), err)
}
}
err = scenario.WaitForTailscaleLogout()
assertNoErrLogout(t, err)
t.Logf("all clients logged out")
headscale, err := scenario.Headscale()
assertNoErrGetHeadscale(t, err)
for userName := range spec {
err = scenario.runTailscaleUp(userName, headscale.GetEndpoint())
if err != nil {
t.Fatalf("failed to run tailscale up: %s", err)
}
}
2022-11-13 22:47:24 +01:00
t.Logf("all clients logged in again")
allClients, err = scenario.ListTailscaleClients()
assertNoErrListClients(t, err)
allIps, err = scenario.ListTailscaleClientsIPs()
assertNoErrListClientIPs(t, err)
allAddrs = lo.Map(allIps, func(x netip.Addr, index int) string {
return x.String()
})
2022-11-13 22:47:24 +01:00
success = pingAllHelper(t, allClients, allAddrs)
2022-11-13 22:47:24 +01:00
t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps))
for _, client := range allClients {
ips, err := client.IPs()
if err != nil {
t.Fatalf("failed to get IPs for client %s: %s", client.Hostname(), err)
}
// lets check if the IPs are the same
if len(ips) != len(clientIPs[client]) {
t.Fatalf("IPs changed for client %s", client.Hostname())
}
for _, ip := range ips {
found := false
for _, oldIP := range clientIPs[client] {
if ip == oldIP {
found = true
break
}
}
if !found {
t.Fatalf(
"IPs changed for client %s. Used to be %v now %v",
client.Hostname(),
clientIPs[client],
ips,
)
}
}
}
t.Logf("all clients IPs are the same")
2022-11-13 22:47:24 +01:00
}
func (s *AuthWebFlowScenario) CreateHeadscaleEnv(
users map[string]int,
opts ...hsic.Option,
) error {
headscale, err := s.Headscale(opts...)
2022-11-13 22:47:24 +01:00
if err != nil {
return err
}
err = headscale.WaitForRunning()
2022-11-13 22:47:24 +01:00
if err != nil {
return err
}
for userName, clientCount := range users {
log.Printf("creating user %s with %d clients", userName, clientCount)
err = s.CreateUser(userName)
2022-11-13 22:47:24 +01:00
if err != nil {
return err
}
err = s.CreateTailscaleNodesInUser(userName, "all", clientCount)
2022-11-13 22:47:24 +01:00
if err != nil {
return err
}
err = s.runTailscaleUp(userName, headscale.GetEndpoint())
2022-11-13 22:47:24 +01:00
if err != nil {
return err
}
}
return nil
}
func (s *AuthWebFlowScenario) runTailscaleUp(
userStr, loginServer string,
2022-11-13 22:47:24 +01:00
) error {
log.Printf("running tailscale up for user %s", userStr)
if user, ok := s.users[userStr]; ok {
for _, client := range user.Clients {
c := client
user.joinWaitGroup.Go(func() error {
loginURL, err := c.LoginWithURL(loginServer)
2022-11-13 22:47:24 +01:00
if err != nil {
log.Printf("failed to run tailscale up (%s): %s", c.Hostname(), err)
return err
2022-11-13 22:47:24 +01:00
}
err = s.runHeadscaleRegister(userStr, loginURL)
2022-11-13 22:47:24 +01:00
if err != nil {
log.Printf("failed to register client (%s): %s", c.Hostname(), err)
return err
2022-11-13 22:47:24 +01:00
}
return nil
})
err := client.WaitForRunning()
if err != nil {
log.Printf("error waiting for client %s to be ready: %s", client.Hostname(), err)
}
2022-11-13 22:47:24 +01:00
}
if err := user.joinWaitGroup.Wait(); err != nil {
return err
}
2022-11-13 22:47:24 +01:00
for _, client := range user.Clients {
err := client.WaitForRunning()
if err != nil {
return fmt.Errorf("%s failed to up tailscale node: %w", client.Hostname(), err)
}
}
2022-11-13 22:47:24 +01:00
return nil
}
return fmt.Errorf("failed to up tailscale node: %w", errNoUserAvailable)
2022-11-13 22:47:24 +01:00
}
func (s *AuthWebFlowScenario) runHeadscaleRegister(userStr string, loginURL *url.URL) error {
headscale, err := s.Headscale()
if err != nil {
return err
}
2022-11-13 22:47:24 +01:00
log.Printf("loginURL: %s", loginURL)
loginURL.Host = fmt.Sprintf("%s:8080", headscale.GetIP())
2022-11-13 22:47:24 +01:00
loginURL.Scheme = "http"
if len(headscale.GetCert()) > 0 {
loginURL.Scheme = "https"
}
insecureTransport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // nolint
}
httpClient := &http.Client{
Transport: insecureTransport,
}
2022-11-13 22:47:24 +01:00
ctx := context.Background()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, loginURL.String(), nil)
resp, err := httpClient.Do(req)
if err != nil {
return err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
defer resp.Body.Close()
// see api.go HTML template
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)
if headscale, err := s.Headscale(); err == nil {
_, err = headscale.Execute(
[]string{"headscale", "nodes", "register", "--user", userStr, "--key", key},
)
2022-11-13 22:47:24 +01:00
if err != nil {
log.Printf("failed to register node: %s", err)
return err
}
return nil
}
return fmt.Errorf("failed to find headscale: %w", errNoHeadscaleAvailable)
}