From 181f1eeb4fd968f45c8ba11bbd3752eacd3e0df3 Mon Sep 17 00:00:00 2001 From: Juan Font Alonso Date: Thu, 3 Nov 2022 16:56:19 +0100 Subject: [PATCH] Added method to run tailscale up without authkey --- integration/tailscale.go | 2 ++ integration/tsic/tsic.go | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/integration/tailscale.go b/integration/tailscale.go index 298817e3..c7242ada 100644 --- a/integration/tailscale.go +++ b/integration/tailscale.go @@ -2,6 +2,7 @@ package integration import ( "net/netip" + "net/url" "tailscale.com/ipn/ipnstate" ) @@ -12,6 +13,7 @@ type TailscaleClient interface { Version() string Execute(command []string) (string, error) Up(loginServer, authKey string) error + UpWithLoginURL(loginServer string) (*url.URL, error) IPs() ([]netip.Addr, error) FQDN() (string, error) Status() (*ipnstate.Status, error) diff --git a/integration/tsic/tsic.go b/integration/tsic/tsic.go index da3eac6a..68bc6827 100644 --- a/integration/tsic/tsic.go +++ b/integration/tsic/tsic.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "net/netip" + "net/url" "strings" "github.com/cenkalti/backoff/v4" @@ -22,9 +23,10 @@ const ( ) var ( - errTailscalePingFailed = errors.New("ping failed") - errTailscaleNotLoggedIn = errors.New("tailscale not logged in") - errTailscaleWrongPeerCount = errors.New("wrong peer count") + errTailscalePingFailed = errors.New("ping failed") + errTailscaleNotLoggedIn = errors.New("tailscale not logged in") + errTailscaleWrongPeerCount = errors.New("wrong peer count") + errTailscaleCannotUpWithoutAuthkey = errors.New("cannot up without authkey") ) type TailscaleInContainer struct { @@ -156,6 +158,37 @@ func (t *TailscaleInContainer) Up( return nil } +func (t *TailscaleInContainer) UpWithLoginURL( + loginServer string, +) (*url.URL, error) { + command := []string{ + "tailscale", + "up", + "-login-server", + loginServer, + "--hostname", + t.hostname, + } + + _, stderr, err := t.Execute(command) + if err != errTailscaleNotLoggedIn { + return nil, errTailscaleCannotUpWithoutAuthkey + } + + urlStr := strings.ReplaceAll(stderr, "\nTo authenticate, visit:\n\n\t", "") + urlStr = strings.TrimSpace(urlStr) + + // parse URL + loginUrl, err := url.Parse(urlStr) + if err != nil { + log.Printf("Could not parse login URL: %s", err) + log.Printf("Original join command result: %s", stderr) + return nil, err + } + + return loginUrl, nil +} + func (t *TailscaleInContainer) IPs() ([]netip.Addr, error) { if t.ips != nil && len(t.ips) != 0 { return t.ips, nil