Skip to content

feat: add groups and group members to telemetry snapshot #13655

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 6 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: removed dead code
  • Loading branch information
austinrhode committed Jun 24, 2024
commit c865d65ea2a04418a8a650dda797c570575c5627
18 changes: 16 additions & 2 deletions coderd/database/dbauthz/dbauthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -1305,11 +1305,25 @@ func (q *querier) GetGroupByOrgAndName(ctx context.Context, arg database.GetGrou
return fetch(q.log, q.auth, q.db.GetGroupByOrgAndName)(ctx, arg)
}

func (q *querier) GetGroupMembers(ctx context.Context, id uuid.UUID) ([]database.User, error) {
func (q *querier) GetGroupMembers(ctx context.Context) ([]database.GroupMember, error) {
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceSystem); err != nil {
return nil, err
}
return q.db.GetGroupMembers(ctx)
}

func (q *querier) GetGroupMembersByGroupID(ctx context.Context, id uuid.UUID) ([]database.User, error) {
if _, err := q.GetGroupByID(ctx, id); err != nil { // AuthZ check
return nil, err
}
return q.db.GetGroupMembers(ctx, id)
return q.db.GetGroupMembersByGroupID(ctx, id)
}

func (q *querier) GetGroups(ctx context.Context) ([]database.Group, error) {
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceSystem); err != nil {
return nil, err
}
return q.db.GetGroups(ctx)
}

func (q *querier) GetGroupsByOrganizationAndUserID(ctx context.Context, arg database.GetGroupsByOrganizationAndUserIDParams) ([]database.Group, error) {
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/dbauthz/dbauthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (s *MethodTestSuite) TestGroup() {
Name: g.Name,
}).Asserts(g, policy.ActionRead).Returns(g)
}))
s.Run("GetGroupMembers", s.Subtest(func(db database.Store, check *expects) {
s.Run("GetGroupMembersByGroupID", s.Subtest(func(db database.Store, check *expects) {
g := dbgen.Group(s.T(), db, database.Group{})
_ = dbgen.GroupMember(s.T(), db, database.GroupMember{})
check.Args(g.ID).Asserts(g, policy.ActionRead)
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/dbgen/dbgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestGenerator(t *testing.T) {
exp := []database.User{u}
dbgen.GroupMember(t, db, database.GroupMember{GroupID: g.ID, UserID: u.ID})

require.Equal(t, exp, must(db.GetGroupMembers(context.Background(), g.ID)))
require.Equal(t, exp, must(db.GetGroupMembersByGroupID(context.Background(), g.ID)))
})

t.Run("Organization", func(t *testing.T) {
Expand Down
20 changes: 19 additions & 1 deletion coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2344,7 +2344,16 @@ func (q *FakeQuerier) GetGroupByOrgAndName(_ context.Context, arg database.GetGr
return database.Group{}, sql.ErrNoRows
}

func (q *FakeQuerier) GetGroupMembers(_ context.Context, id uuid.UUID) ([]database.User, error) {
func (q *FakeQuerier) GetGroupMembers(_ context.Context) ([]database.GroupMember, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

out := make([]database.GroupMember, len(q.groupMembers))
copy(out, q.groupMembers)
return out, nil
}

func (q *FakeQuerier) GetGroupMembersByGroupID(_ context.Context, id uuid.UUID) ([]database.User, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

Expand Down Expand Up @@ -2373,6 +2382,15 @@ func (q *FakeQuerier) GetGroupMembers(_ context.Context, id uuid.UUID) ([]databa
return users, nil
}

func (q *FakeQuerier) GetGroups(_ context.Context) ([]database.Group, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

out := make([]database.Group, len(q.groups))
copy(out, q.groups)
return out, nil
}

func (q *FakeQuerier) GetGroupsByOrganizationAndUserID(_ context.Context, arg database.GetGroupsByOrganizationAndUserIDParams) ([]database.Group, error) {
err := validateDatabaseType(arg)
if err != nil {
Expand Down
18 changes: 16 additions & 2 deletions coderd/database/dbmetrics/dbmetrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 35 additions & 5 deletions coderd/database/dbmock/dbmock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 64 additions & 2 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions coderd/database/queries/groupmembers.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
-- name: GetGroupMembers :many
SELECT * FROM group_members;

-- name: GetGroupMembersByGroupID :many
SELECT
users.*
FROM
Expand Down
3 changes: 3 additions & 0 deletions coderd/database/queries/groups.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
-- name: GetGroups :many
SELECT * FROM groups;

-- name: GetGroupByID :one
SELECT
*
Expand Down
10 changes: 5 additions & 5 deletions enterprise/coderd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
return
}

currentMembers, err := api.Database.GetGroupMembers(ctx, group.ID)
currentMembers, err := api.Database.GetGroupMembersByGroupID(ctx, group.ID)
if err != nil {
httpapi.InternalServerError(rw, err)
return
Expand Down Expand Up @@ -276,7 +276,7 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
return
}

patchedMembers, err := api.Database.GetGroupMembers(ctx, group.ID)
patchedMembers, err := api.Database.GetGroupMembersByGroupID(ctx, group.ID)
if err != nil {
httpapi.InternalServerError(rw, err)
return
Expand Down Expand Up @@ -317,7 +317,7 @@ func (api *API) deleteGroup(rw http.ResponseWriter, r *http.Request) {
return
}

groupMembers, getMembersErr := api.Database.GetGroupMembers(ctx, group.ID)
groupMembers, getMembersErr := api.Database.GetGroupMembersByGroupID(ctx, group.ID)
if getMembersErr != nil {
httpapi.InternalServerError(rw, getMembersErr)
return
Expand Down Expand Up @@ -363,7 +363,7 @@ func (api *API) group(rw http.ResponseWriter, r *http.Request) {
group = httpmw.GroupParam(r)
)

users, err := api.Database.GetGroupMembers(ctx, group.ID)
users, err := api.Database.GetGroupMembersByGroupID(ctx, group.ID)
if err != nil && !xerrors.Is(err, sql.ErrNoRows) {
httpapi.InternalServerError(rw, err)
return
Expand Down Expand Up @@ -408,7 +408,7 @@ func (api *API) groups(rw http.ResponseWriter, r *http.Request) {

resp := make([]codersdk.Group, 0, len(groups))
for _, group := range groups {
members, err := api.Database.GetGroupMembers(ctx, group.ID)
members, err := api.Database.GetGroupMembersByGroupID(ctx, group.ID)
if err != nil {
httpapi.InternalServerError(rw, err)
return
Expand Down
4 changes: 2 additions & 2 deletions enterprise/coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (api *API) templateAvailablePermissions(rw http.ResponseWriter, r *http.Req
sdkGroups := make([]codersdk.Group, 0, len(groups))
for _, group := range groups {
// nolint:gocritic
members, err := api.Database.GetGroupMembers(dbauthz.AsSystemRestricted(ctx), group.ID)
members, err := api.Database.GetGroupMembersByGroupID(dbauthz.AsSystemRestricted(ctx), group.ID)
if err != nil {
httpapi.InternalServerError(rw, err)
return
Expand Down Expand Up @@ -128,7 +128,7 @@ func (api *API) templateACL(rw http.ResponseWriter, r *http.Request) {
// them read the group members.
// We should probably at least return more truncated user data here.
// nolint:gocritic
members, err = api.Database.GetGroupMembers(dbauthz.AsSystemRestricted(ctx), group.ID)
members, err = api.Database.GetGroupMembersByGroupID(dbauthz.AsSystemRestricted(ctx), group.ID)
if err != nil {
httpapi.InternalServerError(rw, err)
return
Expand Down
Loading