github.com/flanksource/konfigadm@v0.12.0/pkg/phases/users.go (about) 1 package phases 2 3 import ( 4 "encoding/base64" 5 "fmt" 6 "strings" 7 8 "github.com/flanksource/konfigadm/pkg/types" 9 ) 10 11 var Users types.Phase = users{} 12 13 type users struct{} 14 15 func (u users) ApplyPhase(sys *types.Config, ctx *types.SystemContext) ([]types.Command, types.Filesystem, error) { 16 files := types.Filesystem{} 17 var commands []types.Command 18 19 for _, user := range sys.Users { 20 21 if user.Sudo != "" { 22 files["/etc/sudoers.d/91-"+user.Name] = types.File{Content: fmt.Sprintf("%s %s", user.Name, user.Sudo)} 23 } 24 cmd := fmt.Sprintf("getent passwd %s || (useradd -m", user.Name) 25 26 if user.Shell != "" { 27 cmd += " -s " + user.Shell 28 } 29 if user.UID != "" { 30 cmd += " -u " + user.UID 31 } 32 33 if user.Gecos != "" { 34 cmd += fmt.Sprintf(" -c \"%s\"", user.Gecos) 35 } 36 cmd += fmt.Sprintf(" %s ) ", user.Name) 37 38 authorizedKeys := base64.StdEncoding.EncodeToString([]byte(strings.Join(user.SSHAuthorizedKeys, "\n"))) 39 40 commands = append(commands, types.Command{Cmd: cmd}) 41 commands = append(commands, types.Command{Cmd: fmt.Sprintf("mkdir -p /home/%s/.ssh/ && ( echo %s | base64 -d > /home/%s/.ssh/authorized_keys ) && chown %s /home/%s/.ssh", user.Name, authorizedKeys, user.Name, user.Name, user.Name)}) 42 43 } 44 return commands, files, nil 45 46 }