-
Notifications
You must be signed in to change notification settings - Fork 0
/
tailnet.go
81 lines (69 loc) · 1.97 KB
/
tailnet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package config
import (
"errors"
"fmt"
"os"
"strings"
)
type TailnetType string // tailscale or headscale
const (
TailnetTypeTailscale TailnetType = "tailscale"
TailnetTypeHeadscale TailnetType = "headscale"
)
func (t TailnetType) String() string {
return string(t)
}
const (
TailnetApiKeySuffix = "TAILNET_API_KEY"
)
var (
ErrMissingTailnetType = errors.New("missing tailnet type")
ErrMissingTailnet = errors.New("missing tailnet")
ErrMissingTailnetApiKey = errors.New("missing tailnet api key")
ErrMissingUser = errors.New("missing headscale user")
ErrMissingHeadscaleControlServer = errors.New("missing headscale control server")
)
// Configuration for the tailnet exit nodes will join
type TailnetConfig struct {
// The type of tailnet, tailscale or headscale
Type TailnetType `yaml:"type"`
// The network of the tailnet. On tailscale this is the network id
Tailnet string `yaml:"tailnet"`
// The api token to communicate with the tailnet
ApiKey string `yaml:"apiKey"`
// The user to register exist nodes for.
User string `yaml:"user"`
// the control server to use. This is require for headscale
ControlServer string `yaml:"controlServer"`
}
func tailnetSecretEnv(name, suffix string) string {
if strings.Contains(name, ".") {
name = strings.ReplaceAll(name, ".", "_")
}
return fmt.Sprintf("%s_%s", name, suffix)
}
func (c *TailnetConfig) Validate() error {
if c.Type == "" {
return ErrMissingTailnetType
}
if c.Tailnet == "" {
return ErrMissingTailnet
}
if c.ApiKey == "" {
key := os.Getenv(tailnetSecretEnv(c.Tailnet, TailnetApiKeySuffix))
if key == "" {
return ErrMissingTailnetApiKey
}
c.ApiKey = key
}
if c.User == "" && c.Type == TailnetTypeHeadscale {
return ErrMissingUser
}
if c.Type == TailnetTypeHeadscale && c.ControlServer == "" {
return errors.New("missing control server")
}
if c.ControlServer == "" {
c.ControlServer = "https://controlplane.tailscale.com"
}
return nil
}