Skip to content

feat: push GetUsers authorization filter to SQL #8497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 17, 2023

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Jul 13, 2023

Prereq to #8447

What this does

This pushes the GetUsers authorization filter into SQL. So all authz checks are done by the SQL WHERE clause.

This is probably not a performance increase, but makes fetching users and the number of users correct if we change permissions.

In the current codebase we allow all users to read all other users. Because of this assumption, GetUsers was lazy in how it handled user counts. If permissions were changed, this value would be inaccurate and leak the total number of users to a caller who does not have permission to know this value.

Removes GetAuthorizedUserCount and GetFilteredUserCount as it is no longer needed.

@Emyrk Emyrk marked this pull request as ready for review July 13, 2023 16:50
Comment on lines -5392 to -5436
users = append(users, user)
}

// Filter out deleted since they should never be returned..
tmp := make([]database.User, 0, len(users))
for _, user := range users {
if !user.Deleted {
tmp = append(tmp, user)
}
}
users = tmp

if params.Search != "" {
tmp := make([]database.User, 0, len(users))
for i, user := range users {
if strings.Contains(strings.ToLower(user.Email), strings.ToLower(params.Search)) {
tmp = append(tmp, users[i])
} else if strings.Contains(strings.ToLower(user.Username), strings.ToLower(params.Search)) {
tmp = append(tmp, users[i])
}
}
users = tmp
}

if len(params.Status) > 0 {
usersFilteredByStatus := make([]database.User, 0, len(users))
for i, user := range users {
if slice.ContainsCompare(params.Status, user.Status, func(a, b database.UserStatus) bool {
return strings.EqualFold(string(a), string(b))
}) {
usersFilteredByStatus = append(usersFilteredByStatus, users[i])
}
}
users = usersFilteredByStatus
}

if len(params.RbacRole) > 0 && !slice.Contains(params.RbacRole, rbac.RoleMember()) {
usersFilteredByRole := make([]database.User, 0, len(users))
for i, user := range users {
if slice.OverlapCompare(params.RbacRole, user.RBACRoles, strings.EqualFold) {
usersFilteredByRole = append(usersFilteredByRole, users[i])
}
}

users = usersFilteredByRole
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this is done by the GetUsers call

@Emyrk Emyrk requested a review from johnstcn July 13, 2023 17:20
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can argue that this is a refactor

@Emyrk Emyrk merged commit 67494a3 into main Jul 17, 2023
@Emyrk Emyrk deleted the stevenmasley/users_sql_filter branch July 17, 2023 13:45
@github-actions github-actions bot locked and limited conversation to collaborators Jul 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants