Skip to content

Commit

Permalink
cmd/initContainer: Split out the code to configure the user
Browse files Browse the repository at this point in the history
  • Loading branch information
debarshiray committed Aug 30, 2020
1 parent 3b6b9e9 commit 1ac485f
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions src/cmd/initContainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,37 +234,12 @@ func initContainer(cmd *cobra.Command, args []string) error {
}

if _, err := user.Lookup(initContainerFlags.user); err != nil {
if initContainerFlags.homeLink {
if err := redirectPath("/home", "/var/home", true); err != nil {
return err
}
}

sudoGroup, err := utils.GetGroupForSudo()
if err != nil {
return fmt.Errorf("failed to get group for sudo: %w", err)
}

logrus.Debugf("Adding user %s with UID %d:", initContainerFlags.user, initContainerFlags.uid)

useraddArgs := []string{
"--home-dir", initContainerFlags.home,
"--no-create-home",
"--shell", initContainerFlags.shell,
"--uid", fmt.Sprint(initContainerFlags.uid),
"--groups", sudoGroup,
if err := configureUser(initContainerFlags.home,
initContainerFlags.shell,
initContainerFlags.uid,
initContainerFlags.user,
}

logrus.Debug("useradd")
for _, arg := range useraddArgs {
logrus.Debugf("%s", arg)
}

if err := shell.Run("useradd", nil, nil, nil, useraddArgs...); err != nil {
return fmt.Errorf("failed to add user %s with UID %d",
initContainerFlags.user,
initContainerFlags.uid)
initContainerFlags.homeLink); err != nil {
return err
}

logrus.Debugf("Removing password for user %s", initContainerFlags.user)
Expand Down Expand Up @@ -372,6 +347,41 @@ func initContainerHelp(cmd *cobra.Command, args []string) {
}
}

func configureUser(home, shell, uid, user string, homeLink bool) error {
if homeLink {
if err := redirectPath("/home", "/var/home", true); err != nil {
return err
}
}

sudoGroup, err := utils.GetGroupForSudo()
if err != nil {
return fmt.Errorf("failed to get group for sudo: %w", err)
}

logrus.Debugf("Adding user %s with UID %d:", user, uid)

useraddArgs := []string{
"--groups", sudoGroup,
"--home-dir", home,
"--no-create-home",
"--shell", shell,
"--uid", fmt.Sprint(uid),
user,
}

logrus.Debug("useradd")
for _, arg := range useraddArgs {
logrus.Debugf("%s", arg)
}

if err := shell.Run("useradd", nil, nil, nil, useraddArgs...); err != nil {
return fmt.Errorf("failed to add user %s with UID %d", user, uid)
}

return nil
}

func mountBind(containerPath, source, flags string) error {
fi, err := os.Stat(source)
if err != nil {
Expand Down

0 comments on commit 1ac485f

Please sign in to comment.