Skip to content

Commit

Permalink
add restore default resolv.conf file (#1288)
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Tingaikin <denis.tingajkin@xored.com>
  • Loading branch information
denis-tingaikin authored May 13, 2022
1 parent 3f01437 commit b9a2a4e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
42 changes: 36 additions & 6 deletions pkg/networkservice/connectioncontext/dnscontext/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ import (
)

type dnsContextClient struct {
chainContext context.Context
coreFilePath string
resolveConfigPath string
defaultNameServerIP string
dnsConfigManager dnscontext.Manager
updateCorefileQueue serialize.Executor
chainContext context.Context
coreFilePath string
resolveConfigPath string
storedResolvConfigPath string
defaultNameServerIP string
dnsConfigManager dnscontext.Manager
updateCorefileQueue serialize.Executor
}

// NewClient creates a new DNS client chain component. Setups all DNS traffic to the localhost. Monitors DNS configs from connections.
Expand All @@ -56,6 +57,7 @@ func NewClient(options ...DNSOption) networkservice.NetworkServiceClient {
for _, o := range options {
o.apply(c)
}
c.storedResolvConfigPath = c.resolveConfigPath + ".restore"
c.initialize()
return c
}
Expand Down Expand Up @@ -97,13 +99,41 @@ func (c *dnsContextClient) Close(ctx context.Context, conn *networkservice.Conne
return next.Client(ctx).Close(ctx, conn, opts...)
}

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

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

func (c *dnsContextClient) initialize() {
c.restoreResolvConf()

r, err := dnscontext.OpenResolveConfig(c.resolveConfigPath)
if err != nil {
log.FromContext(c.chainContext).Errorf("An error during open resolve config: %v", err.Error())
return
}

c.storeOriginalResolvConf()

go func() {
defer c.restoreResolvConf()
<-c.chainContext.Done()
}()

c.dnsConfigManager.Store("", &networkservice.DNSConfig{
SearchDomains: r.Value(dnscontext.AnyDomain),
DnsServerIps: r.Value(dnscontext.NameserverProperty),
Expand Down
32 changes: 32 additions & 0 deletions pkg/networkservice/connectioncontext/dnscontext/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,37 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
)

func Test_DNSContextClient_Restart(t *testing.T) {
corefilePath := filepath.Join(t.TempDir(), "corefile")
resolveConfigPath := filepath.Join(t.TempDir(), "resolv.conf")
err := ioutil.WriteFile(resolveConfigPath, []byte("nameserver 8.8.4.4\n"), os.ModePerm)
require.NoError(t, err)
const expectedEmptyCorefile = `. {
fanout . 8.8.4.4
log
reload
cache {
denial 0
}
}`
for i := 0; i < 1; i++ {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*200)
defer cancel()
var c = chain.NewNetworkServiceClient(
dnscontext.NewClient(
dnscontext.WithCorefilePath(corefilePath),
dnscontext.WithResolveConfigPath(resolveConfigPath),
dnscontext.WithChainContext(ctx),
),
)
_, _ = c.Request(ctx, &networkservice.NetworkServiceRequest{})

requireFileChanged(ctx, t, corefilePath, expectedEmptyCorefile)

cancel()
}
}

func Test_DNSContextClient_Usecases(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
Expand All @@ -50,6 +81,7 @@ func Test_DNSContextClient_Usecases(t *testing.T) {
dnscontext.NewClient(
dnscontext.WithCorefilePath(corefilePath),
dnscontext.WithResolveConfigPath(resolveConfigPath),
dnscontext.WithChainContext(ctx),
),
)

Expand Down

0 comments on commit b9a2a4e

Please sign in to comment.