Skip to content

Commit

Permalink
DNS: dnscontext doesn't restore resolv.conf (#1342)
Browse files Browse the repository at this point in the history
* new solution for resolv.conf restoring

Signed-off-by: Nikita Skrynnik <[email protected]>

* cleanup

Signed-off-by: Nikita Skrynnik <[email protected]>

* resolve linter issues

Signed-off-by: Nikita Skrynnik <[email protected]>

* fix tests

Signed-off-by: Nikita Skrynnik <[email protected]>

* fix linter issues

Signed-off-by: Nikita Skrynnik <[email protected]>

* use dns library to check that we have commented resolv config

Signed-off-by: Nikita Skrynnik <[email protected]>

* fix Test_DNSContextClient_Usecases test

Signed-off-by: Nikita Skrynnik <[email protected]>

* cleanup

Signed-off-by: Nikita Skrynnik <[email protected]>

* apply review comments

Signed-off-by: Nikita Skrynnik <[email protected]>

* rerun CI

Signed-off-by: Nikita Skrynnik <[email protected]>

Signed-off-by: Nikita Skrynnik <[email protected]>
  • Loading branch information
NikitaSkrynnik authored Sep 23, 2022
1 parent 4056e30 commit 143843a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 31 deletions.
61 changes: 45 additions & 16 deletions pkg/networkservice/connectioncontext/dnscontext/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ package dnscontext
import (
"context"
"os"
"strings"

"github.com/golang/protobuf/ptypes/empty"
"github.com/miekg/dns"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"google.golang.org/grpc"

Expand All @@ -33,12 +35,11 @@ import (
)

type dnsContextClient struct {
chainContext context.Context
resolveConfigPath string
storedResolvConfigPath string
defaultNameServerIP string
resolvconfDNSConfig *networkservice.DNSConfig
dnsConfigsMap *dnsconfig.Map
chainContext context.Context
resolveConfigPath string
defaultNameServerIP string
resolvconfDNSConfig *networkservice.DNSConfig
dnsConfigsMap *dnsconfig.Map
}

// NewClient creates a new DNS client chain component. Setups all DNS traffic to the localhost. Monitors DNS configs from connections.
Expand All @@ -52,7 +53,6 @@ func NewClient(options ...DNSOption) networkservice.NetworkServiceClient {
o.apply(c)
}

c.storedResolvConfigPath = c.resolveConfigPath + ".restore"
c.initialize()

return c
Expand Down Expand Up @@ -96,22 +96,51 @@ func (c *dnsContextClient) Close(ctx context.Context, conn *networkservice.Conne
}

func (c *dnsContextClient) restoreResolvConf() {
originalResolvConf, err := os.ReadFile(c.storedResolvConfigPath)
if err != nil || len(originalResolvConf) == 0 {
bytes, err := os.ReadFile(c.resolveConfigPath)
if err != nil {
return
}
_ = os.WriteFile(c.resolveConfigPath, originalResolvConf, os.ModePerm)

commentLines := make([]string, 0)
for _, line := range strings.Split(string(bytes), "\n") {
if strings.HasPrefix(line, "#") {
commentLines = append(commentLines, line[1:])
}
}

commentedPart := strings.Join(commentLines, "\n")
parsedConfig, _ := dns.ClientConfigFromReader(strings.NewReader(commentedPart))

if len(parsedConfig.Servers) == 0 {
return
}

_ = os.WriteFile(c.resolveConfigPath, []byte(commentedPart), os.ModePerm)
}

func (c *dnsContextClient) storeOriginalResolvConf() {
if _, err := os.Stat(c.storedResolvConfigPath); err == nil {
bytes, err := os.ReadFile(c.resolveConfigPath)
if err != nil {
return
}
originalResolvConf, err := os.ReadFile(c.resolveConfigPath)
originalResolvConfig := string(bytes)

lines := strings.Split(originalResolvConfig, "\n")
for i := range lines {
lines[i] = "#" + lines[i]
}

_ = os.WriteFile(c.resolveConfigPath, []byte(strings.Join(lines, "\n")+"\n\n"), os.ModePerm)
}

func (c *dnsContextClient) appendResolvConf(resolvConf string) error {
bytes, err := os.ReadFile(c.resolveConfigPath)
if err != nil {
return
return err
}
_ = os.WriteFile(c.storedResolvConfigPath, originalResolvConf, os.ModePerm)
originalResolvConfig := string(bytes)

return os.WriteFile(c.resolveConfigPath, []byte(originalResolvConfig+resolvConf), os.ModePerm)
}

func (c *dnsContextClient) initialize() {
Expand All @@ -136,8 +165,8 @@ func (c *dnsContextClient) initialize() {
r.SetValue(nameserverProperty, c.defaultNameServerIP)
r.SetValue(searchProperty, []string{}...)

if err = r.Save(); err != nil {
log.FromContext(c.chainContext).Errorf("An error during save resolve config: %v", err.Error())
if err = c.appendResolvConf(r.String()); err != nil {
log.FromContext(c.chainContext).Errorf("An error during appending resolve config: %v", err.Error())
return
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ func Test_DNSContextClient_Usecases(t *testing.T) {
),
)

const expectedResolvconfFile = `nameserver 127.0.0.1`
const expectedResolvconfFile = `#nameserver 8.8.4.4
#search example.com
#
nameserver 127.0.0.1`

requireFileChanged(ctx, t, resolveConfigPath, expectedResolvconfFile)

request := &networkservice.NetworkServiceRequest{
Expand Down
18 changes: 10 additions & 8 deletions pkg/networkservice/connectioncontext/dnscontext/resolvconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package dnscontext

import (
"os"
"io/ioutil"
"strings"
)

Expand All @@ -42,14 +42,16 @@ func openResolveConfig(p string) (*resolveConfig, error) {
}

func (r *resolveConfig) readProperties() error {
b, err := os.ReadFile(r.path)
b, err := ioutil.ReadFile(r.path)
if err != nil {
return err
}
for _, l := range strings.Split(string(b), "\n") {
words := strings.Split(l, " ")
if len(words) > 1 {
r.properties[words[0]] = words[1:]
if !strings.HasPrefix(l, "#") {
words := strings.Split(l, " ")
if len(words) > 1 {
r.properties[words[0]] = words[1:]
}
}
}
return nil
Expand All @@ -69,8 +71,8 @@ func (r *resolveConfig) SetValue(k string, values ...string) {
}
}

// Save saves resolve config file
func (r *resolveConfig) Save() error {
// String serializes resolve config
func (r *resolveConfig) String() string {
var sb strings.Builder
var index int
for k, v := range r.properties {
Expand All @@ -80,7 +82,7 @@ func (r *resolveConfig) Save() error {
_, _ = sb.WriteRune('\n')
}
}
return os.WriteFile(r.path, []byte(sb.String()), os.ModePerm)
return sb.String()
}

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package dnscontext
import (
"os"
"path"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -71,11 +70,7 @@ options ndots:5`
config.SetValue(searchProperty, "default.svc.cluster.local", "svc.cluster.local", "cluster.local")
config.SetValue(optionsProperty, "ndots:5")
config.SetValue("my_property")
err = config.Save()
require.Nil(t, err)
bytes, err := os.ReadFile(filepath.Clean(p))
require.Nil(t, err)
actual := string(bytes)
actual := config.String()
require.Len(t, actual, len(sample))
for _, l := range strings.Split(actual, "\n") {
require.Contains(t, sample, l)
Expand Down

0 comments on commit 143843a

Please sign in to comment.