Skip to content

Commit bc8722d

Browse files
authored
fix(hostsfile): keep dns modifications made during runtime (#57)
1 parent d6500cf commit bc8722d

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

pkg/hostsfile/parser.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,21 @@ func (f *File) Save(ctx context.Context) error {
289289
return fmt.Errorf("can't write, was not loaded from a file")
290290
}
291291

292-
b, err := f.Marshal(ctx)
292+
var b []byte
293+
var err error
294+
if f.fileLocation != "" {
295+
f.lock.Lock()
296+
// re-read the hosts file to get potential
297+
// changes outside of our block
298+
b, err := ioutil.ReadFile(f.fileLocation)
299+
if err != nil {
300+
return err
301+
}
302+
f.contents = b
303+
f.lock.Unlock()
304+
}
305+
306+
b, err = f.Marshal(ctx)
293307
if err != nil {
294308
return errors.Wrap(err, "failed to marshal hostsfile")
295309
}

pkg/hostsfile/parser_test.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ func TestFile_Marshal(t *testing.T) {
8989
if err != nil {
9090
t.Error(err)
9191
}
92+
f.clock = clock.NewMock()
9293

9394
err = f.Load(context.Background())
9495
if err != nil {
9596
t.Error(errors.Wrap(err, "failed to load valid hosts file"))
9697
}
9798

98-
f.clock = clock.NewMock()
9999
b, err := f.Marshal(context.Background())
100100
if err != nil {
101101
t.Error(errors.Wrap(err, "failed to marshal hosts file"))
@@ -111,13 +111,13 @@ func TestFile_Marshal(t *testing.T) {
111111
if err != nil {
112112
t.Error(err)
113113
}
114+
f.clock = clock.NewMock()
114115

115116
err = f.Load(context.Background())
116117
if err != nil {
117118
t.Error(errors.Wrap(err, "failed to load valid hosts file"))
118119
}
119120

120-
f.clock = clock.NewMock()
121121
b, err = f.Marshal(context.Background())
122122
if err != nil {
123123
t.Error(errors.Wrap(err, "failed to marshal hosts file"))
@@ -131,6 +131,34 @@ func TestFile_Marshal(t *testing.T) {
131131
if !reflect.DeepEqual(expected, b) {
132132
t.Error("expected: ", cmp.Diff(expected, b))
133133
}
134+
135+
// modify a hosts file outside of our library, ensure the changes
136+
// are kept when Marshal'd
137+
filePath = "./testdata/load/hosts-with-block.hosts"
138+
f, err = New(filePath, "")
139+
if err != nil {
140+
t.Error(err)
141+
}
142+
f.clock = clock.NewMock()
143+
144+
err = f.Load(context.Background())
145+
if err != nil {
146+
t.Error(errors.Wrap(err, "failed to load valid hosts file"))
147+
}
148+
149+
// append a new entry
150+
f.contents = append([]byte(
151+
"127.0.0.1 helloworld.name.io\n",
152+
), f.contents...)
153+
154+
b, err = f.Marshal(context.Background())
155+
if err != nil {
156+
t.Error(errors.Wrap(err, "failed to marshal hosts file"))
157+
}
158+
159+
if !reflect.DeepEqual(b, f.contents) {
160+
t.Error("expected: ", cmp.Diff(b, f.contents))
161+
}
134162
}
135163

136164
// Ensure that we don't corrupt hosts files that have data inside of

0 commit comments

Comments
 (0)