Skip to content

Commit

Permalink
gitdb: convert usernames to lowercase.
Browse files Browse the repository at this point in the history
  • Loading branch information
rgooch committed Feb 10, 2021
1 parent db9ad00 commit bdccb94
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 13 deletions.
18 changes: 18 additions & 0 deletions pkg/auth/userinfo/gitdb/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"regexp"
"sort"
"strings"

"github.com/Cloud-Foundations/Dominator/lib/decoders"
"github.com/Cloud-Foundations/Dominator/lib/repowatch"
Expand Down Expand Up @@ -96,6 +97,21 @@ func (ls *loadStateType) loadDirectory(dirname string) error {
ls.logger.Printf("%s/%s: GroupMembers are not sorted\n",
dirname, group.Name)
}
userMap := make(map[string]struct{}, len(group.UserMembers))
for index, rawUser := range group.UserMembers {
user := strings.ToLower(rawUser)
if user != rawUser {
ls.logger.Printf("%s/%s: mixed case user: %s\n",
dirname, group.Name, rawUser)
}
group.UserMembers[index] = user
if _, ok := userMap[user]; ok {
ls.logger.Printf("%s/%s: duplicate entry for user: %s\n",
dirname, group.Name, user)
} else {
userMap[user] = struct{}{}
}
}
if !sort.StringsAreSorted(group.UserMembers) {
ls.logger.Printf("%s/%s: UserMembers are not sorted\n",
dirname, group.Name)
Expand Down Expand Up @@ -202,6 +218,7 @@ func (uinfo *UserInfo) getGroups() ([]string, error) {
}

func (uinfo *UserInfo) getUserGroups(username string) ([]string, error) {
username = strings.ToLower(username)
uinfo.rwMutex.RLock()
groupsMap := uinfo.groupsPerUser[username]
uinfo.rwMutex.RUnlock()
Expand Down Expand Up @@ -266,6 +283,7 @@ func (uinfo *UserInfo) loadDatabase(dirname string) error {
}

func (uinfo *UserInfo) testUserInGroup(username, groupname string) bool {
username = strings.ToLower(username)
uinfo.rwMutex.RLock()
defer uinfo.rwMutex.RUnlock()
if groups, ok := uinfo.groupsPerUser[username]; !ok {
Expand Down
61 changes: 50 additions & 11 deletions pkg/auth/userinfo/gitdb/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,72 @@ func TestTree(t *testing.T) {
}

func (uinfo *UserInfo) testDB(t *testing.T) {
if groups, ok := uinfo.groupsPerUser["userA"]; !ok {
t.Fatal("no groups for userA")
uinfo.testUserA(t)
uinfo.testUserB(t)
uinfo.testUserC(t)
}

func (uinfo *UserInfo) testUserA(t *testing.T) {
if _, ok := uinfo.groupsPerUser["userA"]; ok {
t.Fatal("userA has groups: DB not case-folded")
} else if groups, ok := uinfo.groupsPerUser["usera"]; !ok {
t.Fatal("no groups for usera")
} else if _, ok := groups["project0"]; !ok {
t.Fatal("userA not found in project0")
t.Fatal("usera not found in project0")
} else if !uinfo.TestUserInGroup("userA", "project0") {
t.Fatal("userA not found in project0 using TestUserInGroup")
} else if !uinfo.TestUserInGroup("usera", "project0") {
t.Fatal("usera not found in project0 using TestUserInGroup")
} else if _, ok := groups["team0"]; !ok {
t.Fatal("userA not found in team0")
t.Fatal("usera not found in team0")
} else if _, ok := groups["unpermitted0"]; ok {
t.Fatal("userA found in unpermitted team")
t.Fatal("usera found in unpermitted team")
} else if uinfo.TestUserInGroup("userA", "unpermitted0") {
t.Fatal("userA found in unpermitted0 using TestUserInGroup")
} else if uinfo.TestUserInGroup("usera", "unpermitted0") {
t.Fatal("usera found in unpermitted0 using TestUserInGroup")
} else if len(groups) != 2 {
t.Fatalf("userA in %d groups, expected 2", len(groups))
t.Fatalf("usera in %d groups, expected 2", len(groups))
} else if g, err := uinfo.GetUserGroups("userA"); err != nil {
t.Fatal(err)
} else if len(g) != 2 {
t.Fatalf("userA in %d groups using GetUserGroups, expected 2",
len(groups))
} else if g, err := uinfo.GetUserGroups("usera"); err != nil {
t.Fatal(err)
} else if len(g) != 2 {
t.Fatalf("usera in %d groups using GetUserGroups, expected 2",
len(groups))
}
if groups, ok := uinfo.groupsPerUser["userB"]; !ok {
t.Fatal("no groups for userB")
}

func (uinfo *UserInfo) testUserB(t *testing.T) {
if groups, ok := uinfo.groupsPerUser["userb"]; !ok {
t.Fatal("no groups for userb")
} else if _, ok := groups["project1"]; !ok {
t.Fatal("userA not found in project1")
t.Fatal("userb not found in project1")
} else if _, ok := groups["team1"]; !ok {
t.Fatal("userA not found in team1")
t.Fatal("userb not found in team1")
} else if len(groups) != 2 {
t.Fatalf("userb in %d groups, expected 2", len(groups))
}
}

func (uinfo *UserInfo) testUserC(t *testing.T) {
if groups, ok := uinfo.groupsPerUser["userc"]; !ok {
t.Fatal("no groups for userc")
} else if _, ok := groups["project0"]; !ok {
t.Fatal("userc not found in project0")
} else if !uinfo.TestUserInGroup("userc", "project0") {
t.Fatal("userc not found in project0 using TestUserInGroup")
} else if _, ok := groups["team0"]; !ok {
t.Fatal("userc not found in team0")
} else if len(groups) != 2 {
t.Fatalf("userB in %d groups, expected 2", len(groups))
t.Fatalf("userc in %d groups, expected 2", len(groups))
} else if g, err := uinfo.GetUserGroups("userc"); err != nil {
t.Fatal(err)
} else if len(g) != 2 {
t.Fatalf("userc in %d groups using GetUserGroups, expected 2",
len(groups))
}
}
3 changes: 2 additions & 1 deletion pkg/auth/userinfo/gitdb/testdata/flat/groups.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
{
"Name": "team0",
"UserMembers": [
"userA"
"userA",
"userc"
]
},
{
Expand Down
3 changes: 2 additions & 1 deletion pkg/auth/userinfo/gitdb/testdata/tree/teams/groups.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
{
"Name": "team0",
"UserMembers": [
"userA"
"userA",
"userc"
]
},
{
Expand Down

0 comments on commit bdccb94

Please sign in to comment.