forked from mesosphere/mesos-dns
/
config.go
257 lines (230 loc) · 7.75 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package records
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"strings"
"time"
"github.com/mesosphere/mesos-dns/logging"
"github.com/miekg/dns"
)
// Config holds mesos dns configuration
type Config struct {
// Refresh frequency: the frequency in seconds of regenerating records (default 60)
RefreshSeconds int
// Resolver port: port used to listen for slave requests (default 53)
Port int
// Timeout is the default connect/read/write timeout for outbound
// queries
Timeout int
// Timeout in seconds waiting for the master to return data from StateJson
StateTimeoutSeconds int
// Zookeeper Detection Timeout: how long in seconds to wait for Zookeeper to
// be initially responsive. Default is 30 and 0 means no timeout.
ZkDetectionTimeout int
// NOTE(tsenart): HTTPPort, DNSOn and HTTPOn have defined JSON keys for
// backwards compatibility with external API clients.
HTTPPort int `json:"HttpPort"`
// TTL: the TTL value used for SRV and A records (default 60)
TTL int32
// SOA record fields (see http://tools.ietf.org/html/rfc1035#page-18)
SOASerial uint32 // initial version number (incremented on refresh)
SOARefresh uint32 // refresh interval
SOARetry uint32 // retry interval
SOAExpire uint32 // expiration time
SOAMinttl uint32 // minimum TTL
SOAMname string // primary name server
SOARname string // email of admin esponsible
// Mesos master(s): a list of IP:port pairs for one or more Mesos masters
Masters []string
// DNS server: IP address of the DNS server for forwarded accesses
Resolvers []string
// IPSources is the prioritized list of task IP sources
IPSources []string // e.g. ["host", "docker", "mesos", "rkt"]
// Zookeeper: a single Zk url
Zk string
// Domain: name of the domain used (default "mesos", ie .mesos domain)
Domain string
// File is the location of the config.json file
File string
// ListenAddr is the server listener address
Listener string
// Value of RecursionAvailable for responses in Mesos domain
RecurseOn bool
// Enable serving DSN and HTTP requests
DNSOn bool `json:"DnsOn"`
HTTPOn bool `json:"HttpOn"`
// Enable replies for external requests
ExternalOn bool
// EnforceRFC952 will enforce an older, more strict set of rules for DNS labels
EnforceRFC952 bool
// Enumeration enabled via the API enumeration endpoint
EnumerationOn bool
}
// NewConfig return the default config of the resolver
func NewConfig() Config {
return Config{
ZkDetectionTimeout: 30,
RefreshSeconds: 60,
TTL: 60,
Domain: "mesos",
Port: 53,
Timeout: 5,
StateTimeoutSeconds: 300,
SOARname: "root.ns1.mesos",
SOAMname: "ns1.mesos",
SOARefresh: 60,
SOARetry: 600,
SOAExpire: 86400,
SOAMinttl: 60,
Resolvers: []string{"8.8.8.8"},
Listener: "0.0.0.0",
HTTPPort: 8123,
DNSOn: true,
HTTPOn: true,
ExternalOn: true,
RecurseOn: true,
IPSources: []string{"netinfo", "mesos", "host"},
EnumerationOn: true,
}
}
// SetConfig instantiates a Config struct read in from config.json
func SetConfig(cjson string) Config {
c, err := readConfig(cjson)
if err != nil {
logging.Error.Fatal(err)
}
logging.Verbose.Printf("config loaded from %q", c.File)
// validate and complete configuration file
err = validateEnabledServices(c)
if err != nil {
logging.Error.Fatalf("service validation failed: %v", err)
}
if err = validateMasters(c.Masters); err != nil {
logging.Error.Fatalf("Masters validation failed: %v", err)
}
if c.ExternalOn {
if len(c.Resolvers) == 0 {
c.Resolvers = GetLocalDNS()
}
if err = validateResolvers(c.Resolvers); err != nil {
logging.Error.Fatalf("Resolvers validation failed: %v", err)
}
}
if err = validateIPSources(c.IPSources); err != nil {
logging.Error.Fatalf("IPSources validation failed: %v", err)
}
c.Domain = strings.ToLower(c.Domain)
// SOA record fields
c.SOARname = strings.TrimRight(strings.Replace(c.SOARname, "@", ".", -1), ".") + "."
c.SOAMname = strings.TrimRight(c.SOAMname, ".") + "."
c.SOASerial = uint32(time.Now().Unix())
// print configuration file
logging.Verbose.Println("Mesos-DNS configuration:")
logging.Verbose.Println(" - Masters: " + strings.Join(c.Masters, ", "))
logging.Verbose.Println(" - Zookeeper: ", c.Zk)
logging.Verbose.Println(" - ZookeeperDetectionTimeout: ", c.ZkDetectionTimeout)
logging.Verbose.Println(" - RefreshSeconds: ", c.RefreshSeconds)
logging.Verbose.Println(" - Domain: " + c.Domain)
logging.Verbose.Println(" - Listener: " + c.Listener)
logging.Verbose.Println(" - Port: ", c.Port)
logging.Verbose.Println(" - DnsOn: ", c.DNSOn)
logging.Verbose.Println(" - TTL: ", c.TTL)
logging.Verbose.Println(" - Timeout: ", c.Timeout)
logging.Verbose.Println(" - StateTimeoutSeconds: ", c.StateTimeoutSeconds)
logging.Verbose.Println(" - Resolvers: " + strings.Join(c.Resolvers, ", "))
logging.Verbose.Println(" - ExternalOn: ", c.ExternalOn)
logging.Verbose.Println(" - SOAMname: " + c.SOAMname)
logging.Verbose.Println(" - SOARname: " + c.SOARname)
logging.Verbose.Println(" - SOASerial: ", c.SOASerial)
logging.Verbose.Println(" - SOARefresh: ", c.SOARefresh)
logging.Verbose.Println(" - SOARetry: ", c.SOARetry)
logging.Verbose.Println(" - SOAExpire: ", c.SOAExpire)
logging.Verbose.Println(" - SOAExpire: ", c.SOAMinttl)
logging.Verbose.Println(" - RecurseOn: ", c.RecurseOn)
logging.Verbose.Println(" - HttpPort: ", c.HTTPPort)
logging.Verbose.Println(" - HttpOn: ", c.HTTPOn)
logging.Verbose.Println(" - ConfigFile: ", c.File)
logging.Verbose.Println(" - EnforceRFC952: ", c.EnforceRFC952)
logging.Verbose.Println(" - IPSources: ", c.IPSources)
logging.Verbose.Println(" - EnumerationOn", c.EnumerationOn)
return *c
}
func readConfig(file string) (*Config, error) {
c := NewConfig()
workingDir := "."
for _, name := range []string{"HOME", "USERPROFILE"} { // *nix, windows
if dir := os.Getenv(name); dir != "" {
workingDir = dir
}
}
var err error
c.File, err = filepath.Abs(strings.Replace(file, "~/", workingDir+"/", 1))
if err != nil {
return nil, fmt.Errorf("cannot find configuration file")
} else if bs, err := ioutil.ReadFile(c.File); err != nil {
return nil, fmt.Errorf("missing configuration file: %q", c.File)
} else if err = json.Unmarshal(bs, &c); err != nil {
return nil, fmt.Errorf("failed to unmarshal config file %q: %v", c.File, err)
}
return &c, nil
}
func unique(ss []string) []string {
set := make(map[string]struct{}, len(ss))
out := make([]string, 0, len(ss))
for _, s := range ss {
if _, ok := set[s]; !ok {
set[s] = struct{}{}
out = append(out, s)
}
}
return out
}
// GetLocalDNS returns the first nameserver in /etc/resolv.conf
// Used for non-Mesos queries.
func GetLocalDNS() []string {
conf, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
logging.Error.Fatalf("%v", err)
}
return nonLocalAddies(conf.Servers)
}
// Returns non-local nameserver entries
func nonLocalAddies(cservers []string) []string {
bad := localAddies()
good := []string{}
for i := 0; i < len(cservers); i++ {
local := false
for x := 0; x < len(bad); x++ {
if cservers[i] == bad[x] {
local = true
}
}
if !local {
good = append(good, cservers[i])
}
}
return good
}
// Returns an array of local ipv4 addresses
func localAddies() []string {
addies, err := net.InterfaceAddrs()
if err != nil {
logging.Error.Println(err)
}
bad := []string{}
for i := 0; i < len(addies); i++ {
ip, _, err := net.ParseCIDR(addies[i].String())
if err != nil {
logging.Error.Println(err)
}
t4 := ip.To4()
if t4 != nil {
bad = append(bad, t4.String())
}
}
return bad
}