Skip to content

Commit

Permalink
Merge pull request #1104 from 89luca89/fix/workspace_owners
Browse files Browse the repository at this point in the history
fix(workspace): make /workspace owned by default user
  • Loading branch information
89luca89 authored May 29, 2024
2 parents 1e090de + 83b30b3 commit 6f142a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
3 changes: 2 additions & 1 deletion pkg/copy/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func Chown(path string, userName string) error {
}

uid, _ := strconv.Atoi(userID.Uid)
return os.Lchown(path, uid, -1)
gid, _ := strconv.Atoi(userID.Gid)
return os.Lchown(path, uid, gid)
}

func ChownR(path string, userName string) error {
Expand Down
36 changes: 23 additions & 13 deletions pkg/devcontainer/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,19 @@ const (
ResultLocation = "/var/run/devpod/result.json"
)

func SetupContainer(setupInfo *config.Result, extraWorkspaceEnv []string, chownWorkspace bool, log log.Logger) error {
func SetupContainer(setupInfo *config.Result, extraWorkspaceEnv []string, chownProjects bool, log log.Logger) error {
// write result to ResultLocation
WriteResult(setupInfo, log)

// chown user dir
if chownWorkspace {
err := ChownWorkspace(setupInfo, log)
if err != nil {
return errors.Wrap(err, "chown workspace")
}
err := ChownWorkspace(setupInfo, chownProjects, log)
if err != nil {
return errors.Wrap(err, "chown workspace")
}

// patch remote env
log.Debugf("Patch etc environment & profile...")
err := PatchEtcEnvironment(setupInfo.MergedConfig, log)
err = PatchEtcEnvironment(setupInfo.MergedConfig, log)
if err != nil {
return errors.Wrap(err, "patch etc environment")
}
Expand Down Expand Up @@ -132,7 +130,7 @@ func LinkRootHome(setupInfo *config.Result) error {
return nil
}

func ChownWorkspace(setupInfo *config.Result, log log.Logger) error {
func ChownWorkspace(setupInfo *config.Result, recursive bool, log log.Logger) error {
user := config.GetRemoteUser(setupInfo)
exists, err := markerFileExists("chownWorkspace", "")
if err != nil {
Expand All @@ -141,11 +139,23 @@ func ChownWorkspace(setupInfo *config.Result, log log.Logger) error {
return nil
}

log.Infof("Chown workspace...")
err = copy2.ChownR(setupInfo.SubstitutionContext.ContainerWorkspaceFolder, user)
// do not exit on error, we can have non-fatal errors
if err != nil {
log.Warn(err)
workspaceRoot := filepath.Dir(setupInfo.SubstitutionContext.ContainerWorkspaceFolder)

if workspaceRoot != "/" {
log.Infof("Chown workspace...")
err = copy2.Chown(workspaceRoot, user)
if err != nil {
log.Warn(err)
}
}

if recursive {
log.Infof("Chown projects...")
err = copy2.ChownR(setupInfo.SubstitutionContext.ContainerWorkspaceFolder, user)
// do not exit on error, we can have non-fatal errors
if err != nil {
log.Warn(err)
}
}

return nil
Expand Down

0 comments on commit 6f142a3

Please sign in to comment.