Skip to content

feat!: drop reading other 'user' permission #8650

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 16 commits into from
Jul 26, 2023
Merged
Prev Previous commit
Next Next commit
Fix pagination users test
  • Loading branch information
Emyrk committed Jul 25, 2023
commit 72b08624e5f7f7eb77a148a486a8bfceb85e7f9b
4 changes: 2 additions & 2 deletions coderd/users_test.go
Copy link
Member

Choose a reason for hiding this comment

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

We should add unit tests in this file to assert this new behaviour.

Copy link
Member Author

@Emyrk Emyrk Jul 25, 2023

Choose a reason for hiding this comment

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

Which new behavior exactly? The rbac stuff is all tested.

I will add a test for the acl available EDIT: Added for acl available endpoint

Copy link
Member

@johnstcn johnstcn Jul 26, 2023

Choose a reason for hiding this comment

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

While the RBAC stuff may be unit tested, we also need to ensure that the behaviour is as desired at the API endpoint level.

My understanding of this PR is that it removes the ability of org members to read other org members. Is this understanding correct?

If so, I suggest adding test cases to cover the following:

  • As an org member, I can read my own user by ID.
  • As an org member, I can read my own user by name.
  • As an org member, I cannot read another org member by ID.
  • As an org member, I cannot read another org member by name.
  • As an org member, I cannot list all users in the org.
  • As an auditor, I can read another org member by ID.
  • As an auditor, I can read another org member by name.

Copy link
Member Author

Choose a reason for hiding this comment

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

My understanding of this PR is that it removes the ability of org members to read other org members. Is this understanding correct?

Yes. You cannot read other org members or other users. This matrix of who can read who is tedious to test at the api level. So I have matrix tests that answer those questions.

Org Member matrix

{
Name: "ReadOrgMember",
Actions: []rbac.Action{rbac.ActionRead},
Resource: rbac.ResourceOrganizationMember.WithID(currentUser).InOrg(orgID).WithOwner(currentUser.String()),
AuthorizeMap: map[bool][]authSubject{
true: {owner, orgAdmin, userAdmin, orgMemberMe, templateAdmin},
false: {memberMe, otherOrgAdmin, otherOrgMember},
},
},

Read user matrix:

{
Name: "MyUser",
Actions: []rbac.Action{rbac.ActionRead},
Resource: rbac.ResourceUserObject(currentUser),
AuthorizeMap: map[bool][]authSubject{
true: {orgMemberMe, owner, memberMe, templateAdmin, userAdmin},
false: {otherOrgMember, otherOrgAdmin, orgAdmin},
},
},
{
Name: "AUser",
Actions: []rbac.Action{rbac.ActionCreate, rbac.ActionUpdate, rbac.ActionDelete},
Resource: rbac.ResourceUser,
AuthorizeMap: map[bool][]authSubject{
true: {owner, userAdmin},
false: {memberMe, orgMemberMe, orgAdmin, otherOrgMember, otherOrgAdmin, templateAdmin},
},
},

These cover all the cases you just asked.

The OrgMember RBAC object is defined here:

func (m OrganizationMember) RBACObject() rbac.Object {
return rbac.ResourceOrganizationMember.
WithID(m.UserID).
InOrg(m.OrganizationID).
WithOwner(m.UserID.String())
}

And then it is asserted the db makes the right checks in the dbauth_test.go:

s.Run("GetOrganizationMembershipsByUserID", s.Subtest(func(db database.Store, check *expects) {
u := dbgen.User(s.T(), db, database.User{})
a := dbgen.OrganizationMember(s.T(), db, database.OrganizationMember{UserID: u.ID})
b := dbgen.OrganizationMember(s.T(), db, database.OrganizationMember{UserID: u.ID})
check.Args(u.ID).Asserts(a, rbac.ActionRead, b, rbac.ActionRead).Returns(slice.New(a, b))
}))


So these tests cover that any db calls for a given resource as a given actor are correct according to that list you mentioned.

The test I added was the ACL listing available users/groups because it uses dbauth.AsSystemContext to get around the permissions and keep the UX if you are an admin for a given template. I imagine we might need to revisit that in future based on certain feedback, but it was required to keep the UX the same.

Copy link
Member

Choose a reason for hiding this comment

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

I would also really like to see tests at the coderd package level for this change.
That can be done as a follow-up if required, but it would increase confidence and solidify the behaviour.

Copy link
Member Author

Choose a reason for hiding this comment

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

It would increase confidence for sure.

We don't do these style RBAC tests at the coderd package layer atm except in very few cases. Most of our coderd tests run as Owner ☹️.

I'd prefer not to have to write all these, because ideally I wouldn't write new tests, but run the existing tests as various actors and assert various outcomes. But that requires refactoring the tests for fetching users, groups, etc 😢.

Essentially, I think if we are going to test these kinds of things at the coderd level, we should do it in a way that doesn't just add a lot of test code bloat.

Copy link
Member Author

Choose a reason for hiding this comment

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

There might be an issue we should make at some point to audit the rbac assertions in our unit test for correctness.

The current various series of tests are loosely coupled for sure and probably are missing things or have bugs.

Original file line number Diff line number Diff line change
Expand Up @@ -1431,15 +1431,15 @@ func TestGetUsersPagination(t *testing.T) {
require.Equal(t, res.Count, 2)

// if offset is higher than the count postgres returns an empty array
// and not an ErrNoRows error. This also means the count must be 0.
// and not an ErrNoRows error.
res, err = client.Users(ctx, codersdk.UsersRequest{
Pagination: codersdk.Pagination{
Offset: 3,
},
})
require.NoError(t, err)
require.Len(t, res.Users, 0)
require.Equal(t, res.Count, 0)
require.Equal(t, res.Count, -1)
}

func TestPostTokens(t *testing.T) {
Expand Down