Skip to content

Commit

Permalink
[supervisor] add sudo permission to custom images
Browse files Browse the repository at this point in the history
  • Loading branch information
mustard-mh authored and roboquat committed Mar 23, 2022
1 parent 3c82c4b commit 464df6a
Showing 1 changed file with 37 additions and 0 deletions.
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

0 comments on commit 464df6a

Please sign in to comment.