Skip to content

Commit d8f5e99

Browse files
tricktronpraveenkumar
authored andcommitted
Gate daemon host file endpoints
Guard host file endpoints behind the modify-hosts-file setting so the daemon does not touch hosts entries when the feature is disabled. Adds tests to cover the disabled and enabled paths.
1 parent 1b336de commit d8f5e99

File tree

2 files changed

+106
-4
lines changed

2 files changed

+106
-4
lines changed

cmd/crc/cmd/daemon.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func run(configuration *types.Configuration) error {
184184
return err
185185
}
186186
go func() {
187-
mux := gatewayAPIMux()
187+
mux := gatewayAPIMux(config, adminHelperHostsFileEditor{})
188188
s := &http.Server{
189189
Handler: handlers.LoggingHandler(os.Stderr, mux),
190190
ReadTimeout: 10 * time.Second,
@@ -331,18 +331,43 @@ func run(configuration *types.Configuration) error {
331331
}
332332
}
333333

334+
type HostsFileEditor interface {
335+
Add(ip string, hostnames ...string) error
336+
Remove(hostnames ...string) error
337+
}
338+
339+
type adminHelperHostsFileEditor struct{}
340+
341+
func (adminHelperHostsFileEditor) Add(ip string, hostnames ...string) error {
342+
return adminhelper.AddToHostsFile(ip, hostnames...)
343+
}
344+
345+
func (adminHelperHostsFileEditor) Remove(hostnames ...string) error {
346+
return adminhelper.RemoveFromHostsFile(hostnames...)
347+
}
348+
334349
// This API is only exposed in the virtual network (only the VM can reach this).
335350
// Any process inside the VM can reach it by connecting to gateway.crc.testing:80.
336-
func gatewayAPIMux() *http.ServeMux {
351+
func gatewayAPIMux(cfg *crcConfig.Config, hostsEditor HostsFileEditor) *http.ServeMux {
337352
mux := http.NewServeMux()
338353
mux.HandleFunc("/hosts/add", func(w http.ResponseWriter, r *http.Request) {
339354
acceptJSONStringArray(w, r, func(hostnames []string) error {
340-
return adminhelper.AddToHostsFile("127.0.0.1", hostnames...)
355+
if !cfg.Get(crcConfig.ModifyHostsFile).AsBool() {
356+
logging.Infof("Skipping hosts file modification because 'modify-hosts-file' is set to false")
357+
358+
return nil
359+
}
360+
return hostsEditor.Add("127.0.0.1", hostnames...)
341361
})
342362
})
343363
mux.HandleFunc("/hosts/remove", func(w http.ResponseWriter, r *http.Request) {
344364
acceptJSONStringArray(w, r, func(hostnames []string) error {
345-
return adminhelper.RemoveFromHostsFile(hostnames...)
365+
if !cfg.Get(crcConfig.ModifyHostsFile).AsBool() {
366+
logging.Infof("Skipping hosts file modification because 'modify-hosts-file' is set to false")
367+
368+
return nil
369+
}
370+
return hostsEditor.Remove(hostnames...)
346371
})
347372
})
348373
return mux

cmd/crc/cmd/daemon_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"net"
77
"net/http"
8+
"net/http/httptest"
89
"net/url"
910
"os"
1011
"regexp"
@@ -154,3 +155,79 @@ func TestCreateNewVirtualNetworkConfig_WhenHostNetworkConfigSet_ThenSetNAT(t *te
154155
// Then
155156
assert.Equal(t, "127.0.0.1", virtualNetworkConfig.NAT["192.168.127.254"])
156157
}
158+
159+
type fakeHostsFileEditor struct {
160+
addCalled bool
161+
removeCalled bool
162+
addIP string
163+
addHosts []string
164+
removeHosts []string
165+
}
166+
167+
func (fake *fakeHostsFileEditor) Add(ip string, hostnames ...string) error {
168+
fake.addCalled = true
169+
fake.addIP = ip
170+
fake.addHosts = append([]string(nil), hostnames...)
171+
return nil
172+
}
173+
174+
func (fake *fakeHostsFileEditor) Remove(hostnames ...string) error {
175+
fake.removeCalled = true
176+
fake.removeHosts = append([]string(nil), hostnames...)
177+
return nil
178+
}
179+
180+
func TestGatewayAPIMux_HostsEndpointsRespectModifyHostsFile(t *testing.T) {
181+
tests := []struct {
182+
name string
183+
modifyHostsFile bool
184+
path string
185+
expectAddCalled bool
186+
expectRemoveCall bool
187+
}{
188+
{
189+
name: "add-enabled",
190+
modifyHostsFile: true,
191+
path: "/hosts/add",
192+
expectAddCalled: true,
193+
},
194+
{
195+
name: "add-disabled",
196+
modifyHostsFile: false,
197+
path: "/hosts/add",
198+
},
199+
{
200+
name: "remove-enabled",
201+
modifyHostsFile: true,
202+
path: "/hosts/remove",
203+
expectRemoveCall: true,
204+
},
205+
{
206+
name: "remove-disabled",
207+
modifyHostsFile: false,
208+
path: "/hosts/remove",
209+
},
210+
}
211+
212+
for _, test := range tests {
213+
t.Run(test.name, func(t *testing.T) {
214+
// Given
215+
cfg := crcConfig.New(crcConfig.NewEmptyInMemoryStorage(), crcConfig.NewEmptyInMemorySecretStorage())
216+
crcConfig.RegisterSettings(cfg)
217+
_, err := cfg.Set(crcConfig.ModifyHostsFile, test.modifyHostsFile)
218+
assert.NoError(t, err)
219+
hostsEditor := &fakeHostsFileEditor{}
220+
mux := gatewayAPIMux(cfg, hostsEditor)
221+
rec := httptest.NewRecorder()
222+
223+
// When
224+
req := httptest.NewRequest(http.MethodPost, test.path, bytes.NewBufferString(`["api.crc.testing"]`))
225+
mux.ServeHTTP(rec, req)
226+
227+
// Then
228+
assert.Equal(t, http.StatusOK, rec.Code)
229+
assert.Equal(t, test.expectAddCalled, hostsEditor.addCalled)
230+
assert.Equal(t, test.expectRemoveCall, hostsEditor.removeCalled)
231+
})
232+
}
233+
}

0 commit comments

Comments
 (0)