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

[supervisor] add sudo permission to custom images #8914

Merged
merged 1 commit into from
Mar 23, 2022
Merged
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
37 changes: 37 additions & 0 deletions components/supervisor/pkg/supervisor/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
package supervisor

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -42,6 +46,9 @@ func AddGitpodUserIfNotExists() error {
return err
}
}
if err := addSudoer(gitpodGroupName); err != nil {
log.WithError(err).Error("add gitpod sudoers")
}

targetUser := &user.User{
Uid: strconv.Itoa(gitpodUID),
Expand Down Expand Up @@ -169,6 +176,36 @@ func addUser(opts *user.User) error {
return nil
}

// addSudoer check and add group to /etc/sudoers
func addSudoer(group string) error {
if group == "" {
return xerrors.Errorf("group name should not be empty")
}
sudoersPath := "/etc/sudoers"
finfo, err := os.Stat(sudoersPath)
if err != nil {
return err
}
b, err := ioutil.ReadFile(sudoersPath)
if err != nil {
return err
}
gitpodSudoer := []byte(fmt.Sprintf("%%%s ALL=NOPASSWD:ALL", group))
// Line starts with "%gitpod ..."
re := regexp.MustCompile(fmt.Sprintf("(?m)^%%%s\\s+.*?$", group))
if len(re.FindStringIndex(string(b))) > 0 {
nb := re.ReplaceAll(b, gitpodSudoer)
return os.WriteFile(sudoersPath, nb, finfo.Mode().Perm())
}
file, err := os.OpenFile(sudoersPath, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(append([]byte("\n"), gitpodSudoer...))
return err
}

func determineCmdFlavour(args []string) bool {
var flags []string
for _, a := range args {
Expand Down