forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dns_validator.go
50 lines (39 loc) · 1 KB
/
dns_validator.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
package net
import (
gonet "net"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type DNSValidator interface {
Validate([]string) error
}
type dnsValidator struct {
fs boshsys.FileSystem
}
func NewDNSValidator(fs boshsys.FileSystem) DNSValidator {
return &dnsValidator{
fs: fs,
}
}
func (d *dnsValidator) Validate(dnsServers []string) error {
if len(dnsServers) == 0 {
return nil
}
resolvConfContents, err := d.fs.ReadFileString("/etc/resolv.conf")
if err != nil {
return bosherr.WrapError(err, "Reading /etc/resolv.conf")
}
for _, dnsServer := range dnsServers {
if strings.Contains(resolvConfContents, dnsServer) {
return nil
}
canonicalIP := gonet.ParseIP(dnsServer)
if canonicalIP != nil {
if strings.Contains(resolvConfContents, canonicalIP.String()) {
return nil
}
}
}
return bosherr.WrapError(err, "None of the DNS servers that were specified in the manifest were found in /etc/resolv.conf.")
}