Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[workspacekit] Make resolv.conf writeable #6467

Merged
merged 1 commit into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion components/workspacekit/cmd/rings.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ var ring1Cmd = &cobra.Command{
}
}

// We deliberately do not bind mount `/etc/resolv.conf`, but instead place a copy
// so that users in the workspace can modify the file.
err = copyResolvConf(ring2Root)
if err != nil {
log.WithError(err).Error("cannot copy resolv.conf")
return
}

env := make([]string, 0, len(os.Environ()))
for _, e := range os.Environ() {
if strings.HasPrefix(e, "WORKSPACEKIT_") {
Expand Down Expand Up @@ -565,7 +573,9 @@ var (
"/dev",
"/etc/hosts",
"/etc/hostname",
"/etc/resolv.conf",
}
rejectMountPaths = map[string]struct{}{
"/etc/resolv.conf": {},
}
)

Expand Down Expand Up @@ -613,6 +623,11 @@ func findBindMountCandidates(procMounts io.Reader, readlink func(path string) (d
continue
}

// reject known paths
if _, ok := rejectMountPaths[path]; ok {
continue
}

// test remaining candidates if they're a Kubernetes configMap or secret
ln, err := readlink(filepath.Join(path, "..data"))
if err != nil {
Expand All @@ -627,6 +642,34 @@ func findBindMountCandidates(procMounts io.Reader, readlink func(path string) (d
return mounts, scanner.Err()
}

// copyResolvConf copies /etc/resolv.conf to <ring2root>/etc/resolv.conf
func copyResolvConf(ring2root string) error {
fn := "/etc/resolv.conf"
stat, err := os.Stat(fn)
if err != nil {
return err
}

org, err := os.Open(fn)
if err != nil {
return err
}
defer org.Close()

dst, err := os.OpenFile(filepath.Join(ring2root, fn), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, stat.Mode())
if err != nil {
return err
}
defer dst.Close()

_, err = io.Copy(dst, org)
if err != nil {
return err
}

return nil
}

func receiveSeccmpFd(conn *net.UnixConn) (libseccomp.ScmpFd, error) {
buf := make([]byte, unix.CmsgSpace(4))

Expand Down
3 changes: 0 additions & 3 deletions components/workspacekit/cmd/rings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func TestFindBindMountCandidates(t *testing.T) {
"/workspace",
"/etc/hosts",
"/etc/hostname",
"/etc/resolv.conf",
},
},
{
Expand All @@ -42,7 +41,6 @@ func TestFindBindMountCandidates(t *testing.T) {
"/sys",
"/etc/hosts",
"/etc/hostname",
"/etc/resolv.conf",
},
},
{
Expand All @@ -60,7 +58,6 @@ func TestFindBindMountCandidates(t *testing.T) {
"/workspace",
"/etc/hosts",
"/etc/hostname",
"/etc/resolv.conf",
"/custom-certs",
},
},
Expand Down