Copied etchosts and resolvconf packages to libnetwork.
Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
This commit is contained in:
parent
15f9768ec1
commit
9a5217b514
6 changed files with 650 additions and 3 deletions
|
@ -6,11 +6,11 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/docker/docker/pkg/etchosts"
|
|
||||||
"github.com/docker/docker/pkg/resolvconf"
|
|
||||||
"github.com/docker/libnetwork/driverapi"
|
"github.com/docker/libnetwork/driverapi"
|
||||||
"github.com/docker/libnetwork/netutils"
|
"github.com/docker/libnetwork/netutils"
|
||||||
|
"github.com/docker/libnetwork/pkg/etchosts"
|
||||||
"github.com/docker/libnetwork/pkg/netlabel"
|
"github.com/docker/libnetwork/pkg/netlabel"
|
||||||
|
"github.com/docker/libnetwork/pkg/resolvconf"
|
||||||
"github.com/docker/libnetwork/sandbox"
|
"github.com/docker/libnetwork/sandbox"
|
||||||
"github.com/docker/libnetwork/types"
|
"github.com/docker/libnetwork/types"
|
||||||
)
|
)
|
||||||
|
@ -412,7 +412,7 @@ func (ep *endpoint) setupDNS() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// replace any localhost/127.* but always discard IPv6 entries for now.
|
// replace any localhost/127.* but always discard IPv6 entries for now.
|
||||||
resolvConf, _ = resolvconf.FilterResolvDns(resolvConf, ep.network.enableIPv6)
|
resolvConf, _ = resolvconf.FilterResolvDNS(resolvConf, ep.network.enableIPv6)
|
||||||
return ioutil.WriteFile(ep.container.config.resolvConfPath, resolvConf, 0644)
|
return ioutil.WriteFile(ep.container.config.resolvConfPath, resolvConf, 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
79
libnetwork/pkg/etchosts/etchosts.go
Normal file
79
libnetwork/pkg/etchosts/etchosts.go
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
package etchosts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Record Structure for a single host record
|
||||||
|
type Record struct {
|
||||||
|
Hosts string
|
||||||
|
IP string
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteTo writes record to file and returns bytes written or error
|
||||||
|
func (r Record) WriteTo(w io.Writer) (int64, error) {
|
||||||
|
n, err := fmt.Fprintf(w, "%s\t%s\n", r.IP, r.Hosts)
|
||||||
|
return int64(n), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default hosts config records slice
|
||||||
|
var defaultContent = []Record{
|
||||||
|
{Hosts: "localhost", IP: "127.0.0.1"},
|
||||||
|
{Hosts: "localhost ip6-localhost ip6-loopback", IP: "::1"},
|
||||||
|
{Hosts: "ip6-localnet", IP: "fe00::0"},
|
||||||
|
{Hosts: "ip6-mcastprefix", IP: "ff00::0"},
|
||||||
|
{Hosts: "ip6-allnodes", IP: "ff02::1"},
|
||||||
|
{Hosts: "ip6-allrouters", IP: "ff02::2"},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build function
|
||||||
|
// path is path to host file string required
|
||||||
|
// IP, hostname, and domainname set main record leave empty for no master record
|
||||||
|
// extraContent is an array of extra host records.
|
||||||
|
func Build(path, IP, hostname, domainname string, extraContent []Record) error {
|
||||||
|
content := bytes.NewBuffer(nil)
|
||||||
|
if IP != "" {
|
||||||
|
//set main record
|
||||||
|
var mainRec Record
|
||||||
|
mainRec.IP = IP
|
||||||
|
if domainname != "" {
|
||||||
|
mainRec.Hosts = fmt.Sprintf("%s.%s %s", hostname, domainname, hostname)
|
||||||
|
} else {
|
||||||
|
mainRec.Hosts = hostname
|
||||||
|
}
|
||||||
|
if _, err := mainRec.WriteTo(content); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Write defaultContent slice to buffer
|
||||||
|
for _, r := range defaultContent {
|
||||||
|
if _, err := r.WriteTo(content); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Write extra content from function arguments
|
||||||
|
for _, r := range extraContent {
|
||||||
|
if _, err := r.WriteTo(content); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ioutil.WriteFile(path, content.Bytes(), 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update all IP addresses where hostname matches.
|
||||||
|
// path is path to host file
|
||||||
|
// IP is new IP address
|
||||||
|
// hostname is hostname to search for to replace IP
|
||||||
|
func Update(path, IP, hostname string) error {
|
||||||
|
old, err := ioutil.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var re = regexp.MustCompile(fmt.Sprintf("(\\S*)(\\t%s)", regexp.QuoteMeta(hostname)))
|
||||||
|
return ioutil.WriteFile(path, re.ReplaceAll(old, []byte(IP+"$2")), 0644)
|
||||||
|
}
|
134
libnetwork/pkg/etchosts/etchosts_test.go
Normal file
134
libnetwork/pkg/etchosts/etchosts_test.go
Normal file
|
@ -0,0 +1,134 @@
|
||||||
|
package etchosts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBuildDefault(t *testing.T) {
|
||||||
|
file, err := ioutil.TempFile("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(file.Name())
|
||||||
|
|
||||||
|
// check that /etc/hosts has consistent ordering
|
||||||
|
for i := 0; i <= 5; i++ {
|
||||||
|
err = Build(file.Name(), "", "", "", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
expected := "127.0.0.1\tlocalhost\n::1\tlocalhost ip6-localhost ip6-loopback\nfe00::0\tip6-localnet\nff00::0\tip6-mcastprefix\nff02::1\tip6-allnodes\nff02::2\tip6-allrouters\n"
|
||||||
|
|
||||||
|
if expected != string(content) {
|
||||||
|
t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildHostnameDomainname(t *testing.T) {
|
||||||
|
file, err := ioutil.TempFile("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(file.Name())
|
||||||
|
|
||||||
|
err = Build(file.Name(), "10.11.12.13", "testhostname", "testdomainname", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if expected := "10.11.12.13\ttesthostname.testdomainname testhostname\n"; !bytes.Contains(content, []byte(expected)) {
|
||||||
|
t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildHostname(t *testing.T) {
|
||||||
|
file, err := ioutil.TempFile("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(file.Name())
|
||||||
|
|
||||||
|
err = Build(file.Name(), "10.11.12.13", "testhostname", "", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if expected := "10.11.12.13\ttesthostname\n"; !bytes.Contains(content, []byte(expected)) {
|
||||||
|
t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildNoIP(t *testing.T) {
|
||||||
|
file, err := ioutil.TempFile("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(file.Name())
|
||||||
|
|
||||||
|
err = Build(file.Name(), "", "testhostname", "", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if expected := ""; !bytes.Contains(content, []byte(expected)) {
|
||||||
|
t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpdate(t *testing.T) {
|
||||||
|
file, err := ioutil.TempFile("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(file.Name())
|
||||||
|
|
||||||
|
if err := Build(file.Name(), "10.11.12.13", "testhostname", "testdomainname", nil); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if expected := "10.11.12.13\ttesthostname.testdomainname testhostname\n"; !bytes.Contains(content, []byte(expected)) {
|
||||||
|
t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := Update(file.Name(), "1.1.1.1", "testhostname"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err = ioutil.ReadFile(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if expected := "1.1.1.1\ttesthostname.testdomainname testhostname\n"; !bytes.Contains(content, []byte(expected)) {
|
||||||
|
t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
||||||
|
}
|
||||||
|
}
|
1
libnetwork/pkg/resolvconf/README.md
Normal file
1
libnetwork/pkg/resolvconf/README.md
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Package resolvconf provides utility code to query and update DNS configuration in /etc/resolv.conf
|
195
libnetwork/pkg/resolvconf/resolvconf.go
Normal file
195
libnetwork/pkg/resolvconf/resolvconf.go
Normal file
|
@ -0,0 +1,195 @@
|
||||||
|
// Package resolvconf provides utility code to query and update DNS configuration in /etc/resolv.conf
|
||||||
|
package resolvconf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/Sirupsen/logrus"
|
||||||
|
"github.com/docker/docker/pkg/ioutils"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Note: the default IPv4 & IPv6 resolvers are set to Google's Public DNS
|
||||||
|
defaultIPv4Dns = []string{"nameserver 8.8.8.8", "nameserver 8.8.4.4"}
|
||||||
|
defaultIPv6Dns = []string{"nameserver 2001:4860:4860::8888", "nameserver 2001:4860:4860::8844"}
|
||||||
|
ipv4NumBlock = `(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)`
|
||||||
|
ipv4Address = `(` + ipv4NumBlock + `\.){3}` + ipv4NumBlock
|
||||||
|
// This is not an IPv6 address verifier as it will accept a super-set of IPv6, and also
|
||||||
|
// will *not match* IPv4-Embedded IPv6 Addresses (RFC6052), but that and other variants
|
||||||
|
// -- e.g. other link-local types -- either won't work in containers or are unnecessary.
|
||||||
|
// For readability and sufficiency for Docker purposes this seemed more reasonable than a
|
||||||
|
// 1000+ character regexp with exact and complete IPv6 validation
|
||||||
|
ipv6Address = `([0-9A-Fa-f]{0,4}:){2,7}([0-9A-Fa-f]{0,4})`
|
||||||
|
ipLocalhost = `((127\.([0-9]{1,3}.){2}[0-9]{1,3})|(::1))`
|
||||||
|
|
||||||
|
localhostIPRegexp = regexp.MustCompile(ipLocalhost)
|
||||||
|
localhostNSRegexp = regexp.MustCompile(`(?m)^nameserver\s+` + ipLocalhost + `\s*\n*`)
|
||||||
|
nsIPv6Regexp = regexp.MustCompile(`(?m)^nameserver\s+` + ipv6Address + `\s*\n*`)
|
||||||
|
nsRegexp = regexp.MustCompile(`^\s*nameserver\s*((` + ipv4Address + `)|(` + ipv6Address + `))\s*$`)
|
||||||
|
searchRegexp = regexp.MustCompile(`^\s*search\s*(([^\s]+\s*)*)$`)
|
||||||
|
)
|
||||||
|
|
||||||
|
var lastModified struct {
|
||||||
|
sync.Mutex
|
||||||
|
sha256 string
|
||||||
|
contents []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns the contents of /etc/resolv.conf
|
||||||
|
func Get() ([]byte, error) {
|
||||||
|
resolv, err := ioutil.ReadFile("/etc/resolv.conf")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return resolv, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetIfChanged retrieves the host /etc/resolv.conf file, checks against the last hash
|
||||||
|
// and, if modified since last check, returns the bytes and new hash.
|
||||||
|
// This feature is used by the resolv.conf updater for containers
|
||||||
|
func GetIfChanged() ([]byte, string, error) {
|
||||||
|
lastModified.Lock()
|
||||||
|
defer lastModified.Unlock()
|
||||||
|
|
||||||
|
resolv, err := ioutil.ReadFile("/etc/resolv.conf")
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
newHash, err := ioutils.HashData(bytes.NewReader(resolv))
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
if lastModified.sha256 != newHash {
|
||||||
|
lastModified.sha256 = newHash
|
||||||
|
lastModified.contents = resolv
|
||||||
|
return resolv, newHash, nil
|
||||||
|
}
|
||||||
|
// nothing changed, so return no data
|
||||||
|
return nil, "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLastModified retrieves the last used contents and hash of the host resolv.conf.
|
||||||
|
// Used by containers updating on restart
|
||||||
|
func GetLastModified() ([]byte, string) {
|
||||||
|
lastModified.Lock()
|
||||||
|
defer lastModified.Unlock()
|
||||||
|
|
||||||
|
return lastModified.contents, lastModified.sha256
|
||||||
|
}
|
||||||
|
|
||||||
|
// FilterResolvDNS cleans up the config in resolvConf. It has two main jobs:
|
||||||
|
// 1. It looks for localhost (127.*|::1) entries in the provided
|
||||||
|
// resolv.conf, removing local nameserver entries, and, if the resulting
|
||||||
|
// cleaned config has no defined nameservers left, adds default DNS entries
|
||||||
|
// 2. Given the caller provides the enable/disable state of IPv6, the filter
|
||||||
|
// code will remove all IPv6 nameservers if it is not enabled for containers
|
||||||
|
//
|
||||||
|
// It returns a boolean to notify the caller if changes were made at all
|
||||||
|
func FilterResolvDNS(resolvConf []byte, ipv6Enabled bool) ([]byte, bool) {
|
||||||
|
changed := false
|
||||||
|
cleanedResolvConf := localhostNSRegexp.ReplaceAll(resolvConf, []byte{})
|
||||||
|
// if IPv6 is not enabled, also clean out any IPv6 address nameserver
|
||||||
|
if !ipv6Enabled {
|
||||||
|
cleanedResolvConf = nsIPv6Regexp.ReplaceAll(cleanedResolvConf, []byte{})
|
||||||
|
}
|
||||||
|
// if the resulting resolvConf has no more nameservers defined, add appropriate
|
||||||
|
// default DNS servers for IPv4 and (optionally) IPv6
|
||||||
|
if len(GetNameservers(cleanedResolvConf)) == 0 {
|
||||||
|
logrus.Infof("No non-localhost DNS nameservers are left in resolv.conf. Using default external servers : %v", defaultIPv4Dns)
|
||||||
|
dns := defaultIPv4Dns
|
||||||
|
if ipv6Enabled {
|
||||||
|
logrus.Infof("IPv6 enabled; Adding default IPv6 external servers : %v", defaultIPv6Dns)
|
||||||
|
dns = append(dns, defaultIPv6Dns...)
|
||||||
|
}
|
||||||
|
cleanedResolvConf = append(cleanedResolvConf, []byte("\n"+strings.Join(dns, "\n"))...)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(resolvConf, cleanedResolvConf) {
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
return cleanedResolvConf, changed
|
||||||
|
}
|
||||||
|
|
||||||
|
// getLines parses input into lines and strips away comments.
|
||||||
|
func getLines(input []byte, commentMarker []byte) [][]byte {
|
||||||
|
lines := bytes.Split(input, []byte("\n"))
|
||||||
|
var output [][]byte
|
||||||
|
for _, currentLine := range lines {
|
||||||
|
var commentIndex = bytes.Index(currentLine, commentMarker)
|
||||||
|
if commentIndex == -1 {
|
||||||
|
output = append(output, currentLine)
|
||||||
|
} else {
|
||||||
|
output = append(output, currentLine[:commentIndex])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsLocalhost returns true if ip matches the localhost IP regular expression.
|
||||||
|
// Used for determining if nameserver settings are being passed which are
|
||||||
|
// localhost addresses
|
||||||
|
func IsLocalhost(ip string) bool {
|
||||||
|
return localhostIPRegexp.MatchString(ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetNameservers returns nameservers (if any) listed in /etc/resolv.conf
|
||||||
|
func GetNameservers(resolvConf []byte) []string {
|
||||||
|
nameservers := []string{}
|
||||||
|
for _, line := range getLines(resolvConf, []byte("#")) {
|
||||||
|
var ns = nsRegexp.FindSubmatch(line)
|
||||||
|
if len(ns) > 0 {
|
||||||
|
nameservers = append(nameservers, string(ns[1]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nameservers
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetNameserversAsCIDR returns nameservers (if any) listed in
|
||||||
|
// /etc/resolv.conf as CIDR blocks (e.g., "1.2.3.4/32")
|
||||||
|
// This function's output is intended for net.ParseCIDR
|
||||||
|
func GetNameserversAsCIDR(resolvConf []byte) []string {
|
||||||
|
nameservers := []string{}
|
||||||
|
for _, nameserver := range GetNameservers(resolvConf) {
|
||||||
|
nameservers = append(nameservers, nameserver+"/32")
|
||||||
|
}
|
||||||
|
return nameservers
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSearchDomains returns search domains (if any) listed in /etc/resolv.conf
|
||||||
|
// If more than one search line is encountered, only the contents of the last
|
||||||
|
// one is returned.
|
||||||
|
func GetSearchDomains(resolvConf []byte) []string {
|
||||||
|
domains := []string{}
|
||||||
|
for _, line := range getLines(resolvConf, []byte("#")) {
|
||||||
|
match := searchRegexp.FindSubmatch(line)
|
||||||
|
if match == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
domains = strings.Fields(string(match[1]))
|
||||||
|
}
|
||||||
|
return domains
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build writes a configuration file to path containing a "nameserver" entry
|
||||||
|
// for every element in dns, and a "search" entry for every element in
|
||||||
|
// dnsSearch.
|
||||||
|
func Build(path string, dns, dnsSearch []string) error {
|
||||||
|
content := bytes.NewBuffer(nil)
|
||||||
|
for _, dns := range dns {
|
||||||
|
if _, err := content.WriteString("nameserver " + dns + "\n"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(dnsSearch) > 0 {
|
||||||
|
if searchString := strings.Join(dnsSearch, " "); strings.Trim(searchString, " ") != "." {
|
||||||
|
if _, err := content.WriteString("search " + searchString + "\n"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ioutil.WriteFile(path, content.Bytes(), 0644)
|
||||||
|
}
|
238
libnetwork/pkg/resolvconf/resolvconf_test.go
Normal file
238
libnetwork/pkg/resolvconf/resolvconf_test.go
Normal file
|
@ -0,0 +1,238 @@
|
||||||
|
package resolvconf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGet(t *testing.T) {
|
||||||
|
resolvConfUtils, err := Get()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
resolvConfSystem, err := ioutil.ReadFile("/etc/resolv.conf")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if string(resolvConfUtils) != string(resolvConfSystem) {
|
||||||
|
t.Fatalf("/etc/resolv.conf and GetResolvConf have different content.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetNameservers(t *testing.T) {
|
||||||
|
for resolv, result := range map[string][]string{`
|
||||||
|
nameserver 1.2.3.4
|
||||||
|
nameserver 40.3.200.10
|
||||||
|
search example.com`: {"1.2.3.4", "40.3.200.10"},
|
||||||
|
`search example.com`: {},
|
||||||
|
`nameserver 1.2.3.4
|
||||||
|
search example.com
|
||||||
|
nameserver 4.30.20.100`: {"1.2.3.4", "4.30.20.100"},
|
||||||
|
``: {},
|
||||||
|
` nameserver 1.2.3.4 `: {"1.2.3.4"},
|
||||||
|
`search example.com
|
||||||
|
nameserver 1.2.3.4
|
||||||
|
#nameserver 4.3.2.1`: {"1.2.3.4"},
|
||||||
|
`search example.com
|
||||||
|
nameserver 1.2.3.4 # not 4.3.2.1`: {"1.2.3.4"},
|
||||||
|
} {
|
||||||
|
test := GetNameservers([]byte(resolv))
|
||||||
|
if !strSlicesEqual(test, result) {
|
||||||
|
t.Fatalf("Wrong nameserver string {%s} should be %v. Input: %s", test, result, resolv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetNameserversAsCIDR(t *testing.T) {
|
||||||
|
for resolv, result := range map[string][]string{`
|
||||||
|
nameserver 1.2.3.4
|
||||||
|
nameserver 40.3.200.10
|
||||||
|
search example.com`: {"1.2.3.4/32", "40.3.200.10/32"},
|
||||||
|
`search example.com`: {},
|
||||||
|
`nameserver 1.2.3.4
|
||||||
|
search example.com
|
||||||
|
nameserver 4.30.20.100`: {"1.2.3.4/32", "4.30.20.100/32"},
|
||||||
|
``: {},
|
||||||
|
` nameserver 1.2.3.4 `: {"1.2.3.4/32"},
|
||||||
|
`search example.com
|
||||||
|
nameserver 1.2.3.4
|
||||||
|
#nameserver 4.3.2.1`: {"1.2.3.4/32"},
|
||||||
|
`search example.com
|
||||||
|
nameserver 1.2.3.4 # not 4.3.2.1`: {"1.2.3.4/32"},
|
||||||
|
} {
|
||||||
|
test := GetNameserversAsCIDR([]byte(resolv))
|
||||||
|
if !strSlicesEqual(test, result) {
|
||||||
|
t.Fatalf("Wrong nameserver string {%s} should be %v. Input: %s", test, result, resolv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetSearchDomains(t *testing.T) {
|
||||||
|
for resolv, result := range map[string][]string{
|
||||||
|
`search example.com`: {"example.com"},
|
||||||
|
`search example.com # ignored`: {"example.com"},
|
||||||
|
` search example.com `: {"example.com"},
|
||||||
|
` search example.com # ignored`: {"example.com"},
|
||||||
|
`search foo.example.com example.com`: {"foo.example.com", "example.com"},
|
||||||
|
` search foo.example.com example.com `: {"foo.example.com", "example.com"},
|
||||||
|
` search foo.example.com example.com # ignored`: {"foo.example.com", "example.com"},
|
||||||
|
``: {},
|
||||||
|
`# ignored`: {},
|
||||||
|
`nameserver 1.2.3.4
|
||||||
|
search foo.example.com example.com`: {"foo.example.com", "example.com"},
|
||||||
|
`nameserver 1.2.3.4
|
||||||
|
search dup1.example.com dup2.example.com
|
||||||
|
search foo.example.com example.com`: {"foo.example.com", "example.com"},
|
||||||
|
`nameserver 1.2.3.4
|
||||||
|
search foo.example.com example.com
|
||||||
|
nameserver 4.30.20.100`: {"foo.example.com", "example.com"},
|
||||||
|
} {
|
||||||
|
test := GetSearchDomains([]byte(resolv))
|
||||||
|
if !strSlicesEqual(test, result) {
|
||||||
|
t.Fatalf("Wrong search domain string {%s} should be %v. Input: %s", test, result, resolv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func strSlicesEqual(a, b []string) bool {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, v := range a {
|
||||||
|
if v != b[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuild(t *testing.T) {
|
||||||
|
file, err := ioutil.TempFile("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(file.Name())
|
||||||
|
|
||||||
|
err = Build(file.Name(), []string{"ns1", "ns2", "ns3"}, []string{"search1"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if expected := "nameserver ns1\nnameserver ns2\nnameserver ns3\nsearch search1\n"; !bytes.Contains(content, []byte(expected)) {
|
||||||
|
t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildWithZeroLengthDomainSearch(t *testing.T) {
|
||||||
|
file, err := ioutil.TempFile("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(file.Name())
|
||||||
|
|
||||||
|
err = Build(file.Name(), []string{"ns1", "ns2", "ns3"}, []string{"."})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if expected := "nameserver ns1\nnameserver ns2\nnameserver ns3\n"; !bytes.Contains(content, []byte(expected)) {
|
||||||
|
t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
||||||
|
}
|
||||||
|
if notExpected := "search ."; bytes.Contains(content, []byte(notExpected)) {
|
||||||
|
t.Fatalf("Expected to not find '%s' got '%s'", notExpected, content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilterResolvDns(t *testing.T) {
|
||||||
|
ns0 := "nameserver 10.16.60.14\nnameserver 10.16.60.21\n"
|
||||||
|
|
||||||
|
if result, _ := FilterResolvDNS([]byte(ns0), false); result != nil {
|
||||||
|
if ns0 != string(result) {
|
||||||
|
t.Fatalf("Failed No Localhost: expected \n<%s> got \n<%s>", ns0, string(result))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ns1 := "nameserver 10.16.60.14\nnameserver 10.16.60.21\nnameserver 127.0.0.1\n"
|
||||||
|
if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
|
||||||
|
if ns0 != string(result) {
|
||||||
|
t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ns1 = "nameserver 10.16.60.14\nnameserver 127.0.0.1\nnameserver 10.16.60.21\n"
|
||||||
|
if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
|
||||||
|
if ns0 != string(result) {
|
||||||
|
t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ns1 = "nameserver 127.0.1.1\nnameserver 10.16.60.14\nnameserver 10.16.60.21\n"
|
||||||
|
if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
|
||||||
|
if ns0 != string(result) {
|
||||||
|
t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ns1 = "nameserver ::1\nnameserver 10.16.60.14\nnameserver 127.0.2.1\nnameserver 10.16.60.21\n"
|
||||||
|
if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
|
||||||
|
if ns0 != string(result) {
|
||||||
|
t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ns1 = "nameserver 10.16.60.14\nnameserver ::1\nnameserver 10.16.60.21\nnameserver ::1"
|
||||||
|
if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
|
||||||
|
if ns0 != string(result) {
|
||||||
|
t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// with IPv6 disabled (false param), the IPv6 nameserver should be removed
|
||||||
|
ns1 = "nameserver 10.16.60.14\nnameserver 2002:dead:beef::1\nnameserver 10.16.60.21\nnameserver ::1"
|
||||||
|
if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
|
||||||
|
if ns0 != string(result) {
|
||||||
|
t.Fatalf("Failed Localhost+IPv6 off: expected \n<%s> got \n<%s>", ns0, string(result))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// with IPv6 enabled, the IPv6 nameserver should be preserved
|
||||||
|
ns0 = "nameserver 10.16.60.14\nnameserver 2002:dead:beef::1\nnameserver 10.16.60.21\n"
|
||||||
|
ns1 = "nameserver 10.16.60.14\nnameserver 2002:dead:beef::1\nnameserver 10.16.60.21\nnameserver ::1"
|
||||||
|
if result, _ := FilterResolvDNS([]byte(ns1), true); result != nil {
|
||||||
|
if ns0 != string(result) {
|
||||||
|
t.Fatalf("Failed Localhost+IPv6 on: expected \n<%s> got \n<%s>", ns0, string(result))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// with IPv6 enabled, and no non-localhost servers, Google defaults (both IPv4+IPv6) should be added
|
||||||
|
ns0 = "\nnameserver 8.8.8.8\nnameserver 8.8.4.4\nnameserver 2001:4860:4860::8888\nnameserver 2001:4860:4860::8844"
|
||||||
|
ns1 = "nameserver 127.0.0.1\nnameserver ::1\nnameserver 127.0.2.1"
|
||||||
|
if result, _ := FilterResolvDNS([]byte(ns1), true); result != nil {
|
||||||
|
if ns0 != string(result) {
|
||||||
|
t.Fatalf("Failed no Localhost+IPv6 enabled: expected \n<%s> got \n<%s>", ns0, string(result))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// with IPv6 disabled, and no non-localhost servers, Google defaults (only IPv4) should be added
|
||||||
|
ns0 = "\nnameserver 8.8.8.8\nnameserver 8.8.4.4"
|
||||||
|
ns1 = "nameserver 127.0.0.1\nnameserver ::1\nnameserver 127.0.2.1"
|
||||||
|
if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
|
||||||
|
if ns0 != string(result) {
|
||||||
|
t.Fatalf("Failed no Localhost+IPv6 enabled: expected \n<%s> got \n<%s>", ns0, string(result))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue