Skip to content

Commit

Permalink
Check if Linux User Exists Using Lowercase Name (#20)
Browse files Browse the repository at this point in the history
* check linux user exists using lowercase name

* fmt code

* move gid check
  • Loading branch information
osterman authored and goruha committed Mar 28, 2017
1 parent ce8270c commit 141bb37
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
2 changes: 1 addition & 1 deletion api/linux_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ package api
import (
"errors"
"fmt"
"github.com/cloudposse/github-authorized-keys/model/linux"
"github.com/spf13/viper"
"os/exec"
"os/user"
"github.com/cloudposse/github-authorized-keys/model/linux"
)

const (
Expand Down
26 changes: 11 additions & 15 deletions jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/cloudposse/github-authorized-keys/api"
"github.com/cloudposse/github-authorized-keys/config"
model "github.com/cloudposse/github-authorized-keys/model/linux"
"github.com/goruha/permbits"
"github.com/jasonlvhit/gocron"
"github.com/spf13/viper"
"github.com/valyala/fasttemplate"
"strings"
model "github.com/cloudposse/github-authorized-keys/model/linux"
)

const wrapperScriptTpl = `#!/bin/bash
Expand Down Expand Up @@ -53,29 +53,25 @@ func syncUsers(cfg config.Config) {
return
}

// Get all members
// Get all GitHub team members
githubUsers, err := c.GetTeamMembers(team)
if err != nil {
logger.Error(err)
return
}

// Here we will store user name for users that got error during creation
// Track users that were unable to be added to the system
notCreatedUsers := make([]string, 0)

for _, githubUser := range githubUsers {
// Create only non existed users
if !linux.UserExists(*githubUser.Login) {

var gid string = ""
if cfg.UserGID != "" {
gid = cfg.UserGID
}


linuxUser := model.NewUser(*githubUser.Login, gid, cfg.UserGroups, cfg.UserShell)

// Create user and store it's name if there was error during creation
var gid string = ""
if cfg.UserGID != "" {
gid = cfg.UserGID
}
linuxUser := model.NewUser(*githubUser.Login, gid, cfg.UserGroups, cfg.UserShell)
// Only add new users
if !linux.UserExists(linuxUser.Name()) {
// Create user and track if we failed to create their account
if err := linux.UserCreate(linuxUser); err != nil {
logger.Error(err)
notCreatedUsers = append(notCreatedUsers, linuxUser.Name())
Expand Down
2 changes: 1 addition & 1 deletion model/linux/linux_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type User struct {

// NewUser - creates new User
func NewUser(name, gid string, groups []string, shell string) User {
return User{ name: name, gid: gid, groups: groups, shell: shell }
return User{name: name, gid: gid, groups: groups, shell: shell}
}

// Name - return user name
Expand Down

0 comments on commit 141bb37

Please sign in to comment.