Skip to content

feat: add flag to see all tokens if owner #6227

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
Feb 23, 2023
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
Next Next commit
added query for tokens by user id
  • Loading branch information
Kira-Pilot committed Feb 15, 2023
commit 321252bbc3613cb095aa5d4ba038ff3b8dc0481c
4 changes: 4 additions & 0 deletions coderd/database/dbauthz/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func (q *querier) GetAPIKeysByLoginType(ctx context.Context, loginType database.
return fetchWithPostFilter(q.auth, q.db.GetAPIKeysByLoginType)(ctx, loginType)
}

func (q *querier) GetTokensByUserID(ctx context.Context, userID uuid.UUID) ([]database.APIKey, error) {
return fetchWithPostFilter(q.auth, q.db.GetTokensByUserID)(ctx, userID)
}

func (q *querier) GetAPIKeysLastUsedAfter(ctx context.Context, lastUsed time.Time) ([]database.APIKey, error) {
return fetchWithPostFilter(q.auth, q.db.GetAPIKeysLastUsedAfter)(ctx, lastUsed)
}
Expand Down
12 changes: 12 additions & 0 deletions coderd/database/dbauthz/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ func (s *MethodTestSuite) TestAPIKey() {
Asserts(a, rbac.ActionRead, b, rbac.ActionRead).
Returns(slice.New(a, b))
}))
s.Run("GetTokensByUserID", s.Subtest(func(db database.Store, check *expects) {
idA := uuid.New()
idC := uuid.New()

keyA, _ := dbgen.APIKey(s.T(), db, database.APIKey{UserID: idA})
keyB, _ := dbgen.APIKey(s.T(), db, database.APIKey{UserID: idA})
_, _ = dbgen.APIKey(s.T(), db, database.APIKey{UserID: idC})

check.Args(idA).
Asserts(keyA, rbac.ActionRead, keyB, rbac.ActionRead).
Returns(slice.New(keyA, keyB))
}))
s.Run("GetAPIKeysLastUsedAfter", s.Subtest(func(db database.Store, check *expects) {
a, _ := dbgen.APIKey(s.T(), db, database.APIKey{LastUsed: time.Now().Add(time.Hour)})
b, _ := dbgen.APIKey(s.T(), db, database.APIKey{LastUsed: time.Now().Add(time.Hour)})
Expand Down
13 changes: 13 additions & 0 deletions coderd/database/dbfake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,19 @@ func (q *fakeQuerier) GetAPIKeysByLoginType(_ context.Context, t database.LoginT
return apiKeys, nil
}

func (q *fakeQuerier) GetTokensByUserID(_ context.Context, userID uuid.UUID) ([]database.APIKey, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

apiKeys := make([]database.APIKey, 0)
for _, key := range q.apiKeys {
if key.UserID == userID {
apiKeys = append(apiKeys, key)
}
}
return apiKeys, nil
}

func (q *fakeQuerier) DeleteAPIKeyByID(_ context.Context, id string) error {
q.mutex.Lock()
defer q.mutex.Unlock()
Expand Down
1 change: 1 addition & 0 deletions coderd/database/querier.go

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

39 changes: 39 additions & 0 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/apikeys.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ SELECT * FROM api_keys WHERE last_used > $1;
-- name: GetAPIKeysByLoginType :many
SELECT * FROM api_keys WHERE login_type = $1;

-- name: GetTokensByUserID :many
SELECT * FROM api_keys WHERE login_type='token' AND user_id = $1;

-- name: InsertAPIKey :one
INSERT INTO
api_keys (
Expand Down