-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
225 lines (196 loc) · 7.62 KB
/
config.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package config
import (
"reflect"
"strings"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
type Config struct {
Region string `mapstructure:"region"`
NonInteractive bool `mapstructure:"non_interactive"`
DryRun bool `mapstructure:"dry_run"`
Tailscale TailscaleConfig `mapstructure:"tailscale"`
Create CreateConfig `mapstructure:"create"`
Stop StopConfig `mapstructure:"stop"`
}
type CreateConfig struct {
Connect bool `mapstructure:"connect"`
Shutdown string `mapstructure:"shutdown"`
}
type TailscaleConfig struct {
AuthKey string `mapstructure:"auth_key"`
APIKey string `mapstructure:"api_key"`
Tailnet string `mapstructure:"tailnet"`
}
type StopConfig struct {
All bool `mapstructure:"all"`
}
type Policy struct {
ACLs []ACL `json:"acls,omitempty"`
Hosts map[string]string `json:"hosts,omitempty"`
Groups map[string][]string `json:"groups,omitempty"`
Tests []Test `json:"tests,omitempty"`
TagOwners map[string][]string `json:"tagOwners,omitempty"`
AutoApprovers AutoApprovers `json:"autoApprovers,omitempty"`
SSH []SSHConfiguration `json:"ssh,omitempty"`
DerpMap DerpMap `json:"derpMap,omitempty"`
DisableIPv4 bool `json:"disableIPv4,omitempty"`
RandomizeClientPort bool `json:"randomizeClientPort,omitempty"`
}
type ACL struct {
Action string `json:"action,omitempty"`
Src []string `json:"src,omitempty"`
Dst []string `json:"dst,omitempty"`
Proto string `json:"proto,omitempty"`
}
type Test struct {
Src string `json:"src,omitempty"`
Accept []string `json:"accept,omitempty"`
Deny []string `json:"deny,omitempty"`
}
type AutoApprovers struct {
Routes map[string][]string `json:"routes,omitempty"`
ExitNode []string `json:"exitNode,omitempty"`
}
type SSHConfiguration struct {
Action string `json:"action,omitempty"`
Src []string `json:"src,omitempty"`
Dst []string `json:"dst,omitempty"`
Users []string `json:"users,omitempty"`
}
type DerpMap struct {
Regions map[string]DerpRegion `json:"regions,omitempty"`
}
type DerpRegion struct {
RegionID int `json:"regionID,omitempty"`
HostName string `json:"hostName,omitempty"`
}
type Node struct {
Addresses []string `json:"addresses"`
Authorized bool `json:"authorized"`
BlocksIncomingConnections bool `json:"blocksIncomingConnections"`
ClientVersion string `json:"clientVersion"`
Created string `json:"created"`
Expires string `json:"expires"`
Hostname string `json:"hostname"`
ID string `json:"id"`
IsExternal bool `json:"isExternal"`
KeyExpiryDisabled bool `json:"keyExpiryDisabled"`
LastSeen string `json:"lastSeen"`
MachineKey string `json:"NodeKey,omitempty"`
Name string `json:"name,omitempty"`
NodeID string `json:"nodeId"`
NodeKey string `json:"nodeKey"`
OS string `json:"os"`
TailnetLockError string `json:"tailnetLockError,omitempty"`
TailnetLockKey string `json:"tailnetLockKey,omitempty"`
UpdateAvailable bool `json:"updateAvailable"`
User string `json:"user,omitempty"`
Tags []string `json:"tags,omitempty"`
}
type TailscaleStatus struct {
ControlURL string `json:"ControlURL"`
RouteAll bool `json:"RouteAll"`
AllowSingleHosts bool `json:"AllowSingleHosts"`
ExitNodeID string `json:"ExitNodeID"`
ExitNodeIP string `json:"ExitNodeIP"`
ExitNodeAllowLANAccess bool `json:"ExitNodeAllowLANAccess"`
CorpDNS bool `json:"CorpDNS"`
RunSSH bool `json:"RunSSH"`
WantRunning bool `json:"WantRunning"`
LoggedOut bool `json:"LoggedOut"`
ShieldsUp bool `json:"ShieldsUp"`
AdvertiseTags string `json:"AdvertiseTags"`
Hostname string `json:"Hostname"`
NotepadURLs bool `json:"NotepadURLs"`
AdvertiseRoutes string `json:"AdvertiseRoutes"`
NoSNAT bool `json:"NoSNAT"`
NetfilterMode int `json:"NetfilterMode"`
Config struct {
PrivateMachineKey string `json:"PrivateMachineKey"`
PrivateNodeKey string `json:"PrivateNodeKey"`
OldPrivateNodeKey string `json:"OldPrivateNodeKey"`
Provider string `json:"Provider"`
LoginName string `json:"LoginName"`
UserProfile struct {
ID int64 `json:"ID"`
LoginName string `json:"LoginName"`
DisplayName string `json:"DisplayName"`
ProfilePicURL string `json:"ProfilePicURL"`
Roles []string `json:"Roles"`
} `json:"UserProfile"`
NetworkLockKey string `json:"NetworkLockKey"`
NodeID string `json:"NodeID"`
} `json:"Config"`
}
type UserNodes struct {
User string `json:"user"`
Nodes []Node `json:"devices"`
}
func (c *Config) Load(flags *pflag.FlagSet, cmdName string) error {
v := viper.New()
// Tailout looks for configuration files called config.yaml, config.json,
// config.toml, config.hcl, etc.
v.SetConfigName("config")
// Tailout looks for configuration files in the common configuration
// directories.
v.AddConfigPath("/etc/tailout/")
v.AddConfigPath("$HOME/.tailout/")
// Viper logs the configuration file it uses, if any.
err := v.ReadInConfig()
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return err
}
}
// Tailout can be configured with environment variables that start with
// TAILOUT_.
v.SetEnvPrefix("tailout")
v.AutomaticEnv()
// Options with dashes in flag names have underscores when set inside a
// configuration file or with environment variables.
flags.SetNormalizeFunc(func(fs *pflag.FlagSet, name string) pflag.NormalizedName {
name = strings.ReplaceAll(name, "-", "_")
return pflag.NormalizedName(name)
})
// Nested configuration options set with environment variables use an
// underscore as a separator.
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
bindEnvironmentVariables(v, *c)
v.BindPFlags(flags)
// Bind tailscale and command specific nested flags and remove prefix when binding
// FIXME: This is a workaround for a limitation of Viper, found here:
// https://github.com/spf13/viper/issues/1072
flags.Visit(func(f *pflag.Flag) {
flagName := strings.ReplaceAll(f.Name, "-", "_")
v.BindPFlag(cmdName+"."+f.Name, flags.Lookup(f.Name))
if strings.HasPrefix(flagName, "tailscale_") {
v.BindPFlag("tailscale."+strings.TrimPrefix(flagName, "tailscale_"), flags.Lookup(f.Name))
}
})
// Useful for debugging viper fully-merged configuration
// spew.Dump(v.AllSettings())
return v.Unmarshal(c)
}
// bindEnvironmentVariables inspects iface's structure and recursively binds its
// fields to environment variables. This is a workaround to a limitation of
// Viper, found here:
// https://github.com/spf13/viper/issues/188#issuecomment-399884438
func bindEnvironmentVariables(v *viper.Viper, iface interface{}, parts ...string) {
ifv := reflect.ValueOf(iface)
ift := reflect.TypeOf(iface)
for i := 0; i < ift.NumField(); i++ {
val := ifv.Field(i)
typ := ift.Field(i)
tv, ok := typ.Tag.Lookup("mapstructure")
if !ok {
continue
}
switch val.Kind() {
case reflect.Struct:
bindEnvironmentVariables(v, val.Interface(), append(parts, tv)...)
default:
v.BindEnv(strings.Join(append(parts, tv), "."))
}
}
}