Skip to content

Commit ec744db

Browse files
csweichelroboquat
authored andcommitted
[workspacekit] Make resolv.conf writeable
1 parent 21fda09 commit ec744db

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

components/workspacekit/cmd/rings.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,14 @@ var ring1Cmd = &cobra.Command{
343343
}
344344
}
345345

346+
// We deliberately do not bind mount `/etc/resolv.conf`, but instead place a copy
347+
// so that users in the workspace can modify the file.
348+
err = copyResolvConf(ring2Root)
349+
if err != nil {
350+
log.WithError(err).Error("cannot copy resolv.conf")
351+
return
352+
}
353+
346354
env := make([]string, 0, len(os.Environ()))
347355
for _, e := range os.Environ() {
348356
if strings.HasPrefix(e, "WORKSPACEKIT_") {
@@ -565,7 +573,9 @@ var (
565573
"/dev",
566574
"/etc/hosts",
567575
"/etc/hostname",
568-
"/etc/resolv.conf",
576+
}
577+
rejectMountPaths = map[string]struct{}{
578+
"/etc/resolv.conf": {},
569579
}
570580
)
571581

@@ -613,6 +623,11 @@ func findBindMountCandidates(procMounts io.Reader, readlink func(path string) (d
613623
continue
614624
}
615625

626+
// reject known paths
627+
if _, ok := rejectMountPaths[path]; ok {
628+
continue
629+
}
630+
616631
// test remaining candidates if they're a Kubernetes configMap or secret
617632
ln, err := readlink(filepath.Join(path, "..data"))
618633
if err != nil {
@@ -627,6 +642,34 @@ func findBindMountCandidates(procMounts io.Reader, readlink func(path string) (d
627642
return mounts, scanner.Err()
628643
}
629644

645+
// copyResolvConf copies /etc/resolv.conf to <ring2root>/etc/resolv.conf
646+
func copyResolvConf(ring2root string) error {
647+
fn := "/etc/resolv.conf"
648+
stat, err := os.Stat(fn)
649+
if err != nil {
650+
return err
651+
}
652+
653+
org, err := os.Open(fn)
654+
if err != nil {
655+
return err
656+
}
657+
defer org.Close()
658+
659+
dst, err := os.OpenFile(filepath.Join(ring2root, fn), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, stat.Mode())
660+
if err != nil {
661+
return err
662+
}
663+
defer dst.Close()
664+
665+
_, err = io.Copy(dst, org)
666+
if err != nil {
667+
return err
668+
}
669+
670+
return nil
671+
}
672+
630673
func receiveSeccmpFd(conn *net.UnixConn) (libseccomp.ScmpFd, error) {
631674
buf := make([]byte, unix.CmsgSpace(4))
632675

components/workspacekit/cmd/rings_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ func TestFindBindMountCandidates(t *testing.T) {
3030
"/workspace",
3131
"/etc/hosts",
3232
"/etc/hostname",
33-
"/etc/resolv.conf",
3433
},
3534
},
3635
{
@@ -42,7 +41,6 @@ func TestFindBindMountCandidates(t *testing.T) {
4241
"/sys",
4342
"/etc/hosts",
4443
"/etc/hostname",
45-
"/etc/resolv.conf",
4644
},
4745
},
4846
{
@@ -60,7 +58,6 @@ func TestFindBindMountCandidates(t *testing.T) {
6058
"/workspace",
6159
"/etc/hosts",
6260
"/etc/hostname",
63-
"/etc/resolv.conf",
6461
"/custom-certs",
6562
},
6663
},

0 commit comments

Comments
 (0)