2022-11-13 21:47:24 +00:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2022-12-19 18:15:31 +00:00
|
|
|
"net/netip"
|
2022-11-13 21:47:24 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2022-11-14 14:01:31 +00:00
|
|
|
|
|
|
|
"github.com/juanfont/headscale/integration/hsic"
|
2023-02-02 09:14:33 +00:00
|
|
|
"github.com/samber/lo"
|
2022-11-13 21:47:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var errParseAuthPage = errors.New("failed to parse auth page")
|
|
|
|
|
|
|
|
type AuthWebFlowScenario struct {
|
|
|
|
*Scenario
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthWebFlowAuthenticationPingAll(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
2022-11-22 11:05:58 +00:00
|
|
|
t.Parallel()
|
2022-11-13 21:47:24 +00:00
|
|
|
|
2024-04-21 16:28:17 +00:00
|
|
|
baseScenario, err := NewScenario(dockertestMaxWait())
|
2022-11-13 21:47:24 +00:00
|
|
|
if err != nil {
|
2023-08-29 06:33:33 +00:00
|
|
|
t.Fatalf("failed to create scenario: %s", err)
|
2022-11-13 21:47:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scenario := AuthWebFlowScenario{
|
|
|
|
Scenario: baseScenario,
|
|
|
|
}
|
2024-09-17 09:44:55 +00:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2022-11-13 21:47:24 +00:00
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-08-31 12:57:43 +00:00
|
|
|
"user1": len(MustTestVersions),
|
|
|
|
"user2": len(MustTestVersions),
|
2022-11-13 21:47:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 14:01:31 +00:00
|
|
|
err = scenario.CreateHeadscaleEnv(spec, hsic.WithTestName("webauthping"))
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrHeadscaleEnv(t, err)
|
2022-11-13 21:47:24 +00:00
|
|
|
|
|
|
|
allClients, err := scenario.ListTailscaleClients()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrListClients(t, err)
|
2022-11-13 21:47:24 +00:00
|
|
|
|
|
|
|
allIps, err := scenario.ListTailscaleClientsIPs()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrListClientIPs(t, err)
|
2022-11-13 21:47:24 +00:00
|
|
|
|
|
|
|
err = scenario.WaitForTailscaleSync()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrSync(t, err)
|
2022-11-13 21:47:24 +00:00
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
// assertClientsState(t, allClients)
|
2024-02-09 06:26:41 +00:00
|
|
|
|
2023-02-02 09:14:33 +00:00
|
|
|
allAddrs := lo.Map(allIps, func(x netip.Addr, index int) string {
|
|
|
|
return x.String()
|
|
|
|
})
|
2022-12-19 18:15:31 +00:00
|
|
|
|
2023-02-02 09:14:33 +00:00
|
|
|
success := pingAllHelper(t, allClients, allAddrs)
|
2022-12-19 18:15:31 +00:00
|
|
|
t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthWebFlowLogoutAndRelogin(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-04-21 16:28:17 +00:00
|
|
|
baseScenario, err := NewScenario(dockertestMaxWait())
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-12-19 18:15:31 +00:00
|
|
|
|
|
|
|
scenario := AuthWebFlowScenario{
|
|
|
|
Scenario: baseScenario,
|
|
|
|
}
|
2024-09-17 09:44:55 +00:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2022-12-19 18:15:31 +00:00
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-08-31 12:57:43 +00:00
|
|
|
"user1": len(MustTestVersions),
|
|
|
|
"user2": len(MustTestVersions),
|
2022-12-19 18:15:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec, hsic.WithTestName("weblogout"))
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrHeadscaleEnv(t, err)
|
2022-12-19 18:15:31 +00:00
|
|
|
|
|
|
|
allClients, err := scenario.ListTailscaleClients()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrListClients(t, err)
|
2022-12-19 18:15:31 +00:00
|
|
|
|
|
|
|
allIps, err := scenario.ListTailscaleClientsIPs()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrListClientIPs(t, err)
|
2022-12-19 18:15:31 +00:00
|
|
|
|
|
|
|
err = scenario.WaitForTailscaleSync()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrSync(t, err)
|
2022-12-19 18:15:31 +00:00
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
// assertClientsState(t, allClients)
|
2024-02-09 06:26:41 +00:00
|
|
|
|
2023-02-02 09:14:33 +00:00
|
|
|
allAddrs := lo.Map(allIps, func(x netip.Addr, index int) string {
|
|
|
|
return x.String()
|
|
|
|
})
|
2022-12-19 18:15:31 +00:00
|
|
|
|
2023-02-02 09:14:33 +00:00
|
|
|
success := pingAllHelper(t, allClients, allAddrs)
|
2022-12-19 18:15:31 +00:00
|
|
|
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 {
|
2023-08-29 06:33:33 +00:00
|
|
|
t.Fatalf("failed to get IPs for client %s: %s", client.Hostname(), err)
|
2022-12-19 18:15:31 +00:00
|
|
|
}
|
|
|
|
clientIPs[client] = ips
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, client := range allClients {
|
2022-12-21 22:29:52 +00:00
|
|
|
err := client.Logout()
|
2022-12-19 18:15:31 +00:00
|
|
|
if err != nil {
|
2023-08-29 06:33:33 +00:00
|
|
|
t.Fatalf("failed to logout client %s: %s", client.Hostname(), err)
|
2022-12-19 18:15:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 06:33:33 +00:00
|
|
|
err = scenario.WaitForTailscaleLogout()
|
|
|
|
assertNoErrLogout(t, err)
|
2022-12-19 18:15:31 +00:00
|
|
|
|
|
|
|
t.Logf("all clients logged out")
|
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrGetHeadscale(t, err)
|
2022-12-19 18:15:31 +00:00
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
for userName := range spec {
|
|
|
|
err = scenario.runTailscaleUp(userName, headscale.GetEndpoint())
|
2022-12-19 18:15:31 +00:00
|
|
|
if err != nil {
|
2023-08-29 06:33:33 +00:00
|
|
|
t.Fatalf("failed to run tailscale up: %s", err)
|
2022-12-19 18:15:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-13 21:47:24 +00:00
|
|
|
|
2022-12-19 18:15:31 +00:00
|
|
|
t.Logf("all clients logged in again")
|
|
|
|
|
|
|
|
allClients, err = scenario.ListTailscaleClients()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrListClients(t, err)
|
2022-12-19 18:15:31 +00:00
|
|
|
|
|
|
|
allIps, err = scenario.ListTailscaleClientsIPs()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrListClientIPs(t, err)
|
2022-12-19 18:15:31 +00:00
|
|
|
|
2023-02-02 09:14:33 +00:00
|
|
|
allAddrs = lo.Map(allIps, func(x netip.Addr, index int) string {
|
|
|
|
return x.String()
|
|
|
|
})
|
2022-11-13 21:47:24 +00:00
|
|
|
|
2023-02-02 09:14:33 +00:00
|
|
|
success = pingAllHelper(t, allClients, allAddrs)
|
2022-11-13 21:47:24 +00:00
|
|
|
t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps))
|
|
|
|
|
2022-12-19 18:15:31 +00:00
|
|
|
for _, client := range allClients {
|
|
|
|
ips, err := client.IPs()
|
|
|
|
if err != nil {
|
2023-08-29 06:33:33 +00:00
|
|
|
t.Fatalf("failed to get IPs for client %s: %s", client.Hostname(), err)
|
2022-12-19 18:15:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// lets check if the IPs are the same
|
|
|
|
if len(ips) != len(clientIPs[client]) {
|
2023-08-29 06:33:33 +00:00
|
|
|
t.Fatalf("IPs changed for client %s", client.Hostname())
|
2022-12-19 18:15:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ip := range ips {
|
|
|
|
found := false
|
|
|
|
for _, oldIP := range clientIPs[client] {
|
|
|
|
if ip == oldIP {
|
|
|
|
found = true
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2023-08-29 06:33:33 +00:00
|
|
|
t.Fatalf(
|
2023-02-02 09:14:33 +00:00
|
|
|
"IPs changed for client %s. Used to be %v now %v",
|
|
|
|
client.Hostname(),
|
|
|
|
clientIPs[client],
|
|
|
|
ips,
|
|
|
|
)
|
2022-12-19 18:15:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("all clients IPs are the same")
|
2022-11-13 21:47:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 14:01:31 +00:00
|
|
|
func (s *AuthWebFlowScenario) CreateHeadscaleEnv(
|
2023-01-17 16:43:44 +00:00
|
|
|
users map[string]int,
|
2022-11-14 14:01:31 +00:00
|
|
|
opts ...hsic.Option,
|
|
|
|
) error {
|
|
|
|
headscale, err := s.Headscale(opts...)
|
2022-11-13 21:47:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-29 06:33:33 +00:00
|
|
|
err = headscale.WaitForRunning()
|
2022-11-13 21:47:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
for userName, clientCount := range users {
|
|
|
|
log.Printf("creating user %s with %d clients", userName, clientCount)
|
|
|
|
err = s.CreateUser(userName)
|
2022-11-13 21:47:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
err = s.CreateTailscaleNodesInUser(userName, "all", clientCount)
|
2022-11-13 21:47:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
err = s.runTailscaleUp(userName, headscale.GetEndpoint())
|
2022-11-13 21:47:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AuthWebFlowScenario) runTailscaleUp(
|
2023-01-17 16:43:44 +00:00
|
|
|
userStr, loginServer string,
|
2022-11-13 21:47:24 +00:00
|
|
|
) error {
|
2023-01-17 16:43:44 +00:00
|
|
|
log.Printf("running tailscale up for user %s", userStr)
|
|
|
|
if user, ok := s.users[userStr]; ok {
|
|
|
|
for _, client := range user.Clients {
|
2023-08-29 06:33:33 +00:00
|
|
|
c := client
|
|
|
|
user.joinWaitGroup.Go(func() error {
|
|
|
|
loginURL, err := c.LoginWithURL(loginServer)
|
2022-11-13 21:47:24 +00:00
|
|
|
if err != nil {
|
2023-08-29 06:33:33 +00:00
|
|
|
log.Printf("failed to run tailscale up (%s): %s", c.Hostname(), err)
|
|
|
|
|
|
|
|
return err
|
2022-11-13 21:47:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
err = s.runHeadscaleRegister(userStr, loginURL)
|
2022-11-13 21:47:24 +00:00
|
|
|
if err != nil {
|
2023-08-29 06:33:33 +00:00
|
|
|
log.Printf("failed to register client (%s): %s", c.Hostname(), err)
|
|
|
|
|
|
|
|
return err
|
2022-11-13 21:47:24 +00:00
|
|
|
}
|
2022-11-19 13:35:34 +00:00
|
|
|
|
2023-08-29 06:33:33 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
err := client.WaitForRunning()
|
2022-11-19 13:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error waiting for client %s to be ready: %s", client.Hostname(), err)
|
|
|
|
}
|
2022-11-13 21:47:24 +00:00
|
|
|
}
|
2023-08-29 06:33:33 +00:00
|
|
|
|
|
|
|
if err := user.joinWaitGroup.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-13 21:47:24 +00:00
|
|
|
|
2023-04-12 07:25:51 +00:00
|
|
|
for _, client := range user.Clients {
|
2023-08-29 06:33:33 +00:00
|
|
|
err := client.WaitForRunning()
|
2023-04-12 07:25:51 +00:00
|
|
|
if err != nil {
|
2023-08-29 06:33:33 +00:00
|
|
|
return fmt.Errorf("%s failed to up tailscale node: %w", client.Hostname(), err)
|
2023-04-12 07:25:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 21:47:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
return fmt.Errorf("failed to up tailscale node: %w", errNoUserAvailable)
|
2022-11-13 21:47:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
func (s *AuthWebFlowScenario) runHeadscaleRegister(userStr string, loginURL *url.URL) error {
|
2022-11-14 13:27:02 +00:00
|
|
|
headscale, err := s.Headscale()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-13 21:47:24 +00:00
|
|
|
log.Printf("loginURL: %s", loginURL)
|
2022-11-14 13:27:02 +00:00
|
|
|
loginURL.Host = fmt.Sprintf("%s:8080", headscale.GetIP())
|
2022-11-13 21:47:24 +00:00
|
|
|
loginURL.Scheme = "http"
|
|
|
|
|
|
|
|
httpClient := &http.Client{}
|
|
|
|
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)
|
|
|
|
|
2022-11-14 13:27:02 +00:00
|
|
|
if headscale, err := s.Headscale(); err == nil {
|
2022-11-14 11:07:02 +00:00
|
|
|
_, err = headscale.Execute(
|
2023-01-23 12:55:30 +00:00
|
|
|
[]string{"headscale", "nodes", "register", "--user", userStr, "--key", key},
|
2022-11-14 11:07:02 +00:00
|
|
|
)
|
2022-11-13 21:47:24 +00: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)
|
|
|
|
}
|