Added method to run tailscale up without authkey

This commit is contained in:
Juan Font Alonso 2022-11-03 16:56:19 +01:00
parent e270cf6d20
commit 181f1eeb4f
2 changed files with 38 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package integration
import ( import (
"net/netip" "net/netip"
"net/url"
"tailscale.com/ipn/ipnstate" "tailscale.com/ipn/ipnstate"
) )
@ -12,6 +13,7 @@ type TailscaleClient interface {
Version() string Version() string
Execute(command []string) (string, error) Execute(command []string) (string, error)
Up(loginServer, authKey string) error Up(loginServer, authKey string) error
UpWithLoginURL(loginServer string) (*url.URL, error)
IPs() ([]netip.Addr, error) IPs() ([]netip.Addr, error)
FQDN() (string, error) FQDN() (string, error)
Status() (*ipnstate.Status, error) Status() (*ipnstate.Status, error)

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"log" "log"
"net/netip" "net/netip"
"net/url"
"strings" "strings"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
@ -22,9 +23,10 @@ const (
) )
var ( var (
errTailscalePingFailed = errors.New("ping failed") errTailscalePingFailed = errors.New("ping failed")
errTailscaleNotLoggedIn = errors.New("tailscale not logged in") errTailscaleNotLoggedIn = errors.New("tailscale not logged in")
errTailscaleWrongPeerCount = errors.New("wrong peer count") errTailscaleWrongPeerCount = errors.New("wrong peer count")
errTailscaleCannotUpWithoutAuthkey = errors.New("cannot up without authkey")
) )
type TailscaleInContainer struct { type TailscaleInContainer struct {
@ -156,6 +158,37 @@ func (t *TailscaleInContainer) Up(
return nil 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) { func (t *TailscaleInContainer) IPs() ([]netip.Addr, error) {
if t.ips != nil && len(t.ips) != 0 { if t.ips != nil && len(t.ips) != 0 {
return t.ips, nil return t.ips, nil