Skip to content
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

fix: list access keys query #3151

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions internal/server/data/access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func ListAccessKeys(tx ReadTxn, opts ListAccessKeyOptions) ([]models.AccessKey,
query.B("FROM access_keys AS k INNER JOIN identities AS u")
query.B("ON k.issued_for = u.id")
query.B("WHERE k.deleted_at is null AND u.deleted_at is null")
query.B("AND k.organization_id = ?", tx.OrganizationID())

if !opts.IncludeExpired {
// TODO: can we remove the need to check for both the zero value and nil?
Expand All @@ -136,6 +137,7 @@ func ListAccessKeys(tx ReadTxn, opts ListAccessKeyOptions) ([]models.AccessKey,
if opts.ByName != "" {
query.B("AND k.name = ?", opts.ByName)
}
query.B("ORDER BY k.name ASC")
if opts.Pagination != nil {
opts.Pagination.PaginateQuery(query)
}
Expand Down
53 changes: 45 additions & 8 deletions internal/server/data/access_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,23 +291,23 @@ func TestListAccessKeys(t *testing.T) {
createIdentities(t, db, user, otherUser)

first := &models.AccessKey{
Name: "first",
Name: "alpha",
Copy link
Contributor Author

@dnephin dnephin Sep 7, 2022

Choose a reason for hiding this comment

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

I changed these names so that the expected values in the existing tests sort correctly. I changed the insertion order below so that the tests would fail without this change.

Model: models.Model{ID: 5},
IssuedFor: user.ID,
ProviderID: InfraProvider(db).ID,
ExpiresAt: time.Now().Add(time.Hour).UTC(),
KeyID: "1234567890",
}
second := &models.AccessKey{
Name: "second",
Name: "beta",
Model: models.Model{ID: 6},
IssuedFor: user.ID,
ProviderID: InfraProvider(db).ID,
ExpiresAt: time.Now().Add(-time.Hour).UTC(),
KeyID: "1234567891",
}
third := &models.AccessKey{
Name: "third",
Name: "charlie",
Model: models.Model{ID: 7},
IssuedFor: user.ID,
ProviderID: InfraProvider(db).ID,
Expand All @@ -316,7 +316,7 @@ func TestListAccessKeys(t *testing.T) {
KeyID: "1234567892",
}
deleted := &models.AccessKey{
Name: "forth",
Name: "delta",
Model: models.Model{ID: 8},
IssuedFor: user.ID,
ProviderID: InfraProvider(db).ID,
Expand All @@ -328,20 +328,57 @@ func TestListAccessKeys(t *testing.T) {
deleted.DeletedAt.Valid = true

forth := &models.AccessKey{
Name: "forth",
Name: "delta",
Model: models.Model{ID: 9},
IssuedFor: otherUser.ID,
ProviderID: InfraProvider(db).ID,
ExpiresAt: time.Now().Add(time.Hour).UTC(),
KeyID: "1234567894",
}

createAccessKeys(t, db, first, second, third, forth, deleted)
createAccessKeys(t, db, forth, third, second, first, deleted)

otherOrg := &models.Organization{Name: "other", Domain: "ok.example.com"}
assert.NilError(t, CreateOrganization(db, otherOrg))

t.Run("setup other org", func(t *testing.T) {
tx := txnForTestCase(t, db, otherOrg.ID)

otherOrgUser := &models.Identity{
Name: "gamma@other.org",
OrganizationMember: models.OrganizationMember{OrganizationID: otherOrg.ID},
}
assert.NilError(t, CreateIdentity(tx, otherOrgUser))

otherOrgKey := &models.AccessKey{
Model: models.Model{ID: 17},
OrganizationMember: models.OrganizationMember{OrganizationID: otherOrg.ID},
Name: "epsilon",
IssuedFor: otherOrgUser.ID,
ProviderID: InfraProvider(tx).ID,
ExpiresAt: time.Now().Add(time.Hour).UTC(),
KeyID: "2234567800",
}
createAccessKeys(t, tx, otherOrgKey)
assert.NilError(t, tx.Commit())
})

cmpAccessKeyShallow := cmp.Comparer(func(x, y models.AccessKey) bool {
return x.ID == y.ID && x.IssuedForName == y.IssuedForName
})

t.Run("default other org ID", func(t *testing.T) {
tx := txnForTestCase(t, db, otherOrg.ID)

actual, err := ListAccessKeys(tx, ListAccessKeyOptions{})
assert.NilError(t, err)

expected := []models.AccessKey{
{Model: models.Model{ID: 17}, IssuedForName: "gamma@other.org"},
}
assert.DeepEqual(t, actual, expected, cmpAccessKeyShallow)
})

t.Run("default", func(t *testing.T) {
actual, err := ListAccessKeys(db, ListAccessKeyOptions{})
assert.NilError(t, err)
Expand All @@ -367,7 +404,7 @@ func TestListAccessKeys(t *testing.T) {
})

t.Run("by name", func(t *testing.T) {
actual, err := ListAccessKeys(db, ListAccessKeyOptions{ByName: "first"})
actual, err := ListAccessKeys(db, ListAccessKeyOptions{ByName: "alpha"})
assert.NilError(t, err)

expected := []models.AccessKey{
Expand All @@ -388,7 +425,7 @@ func TestListAccessKeys(t *testing.T) {

t.Run("by name and expired", func(t *testing.T) {
actual, err := ListAccessKeys(db, ListAccessKeyOptions{
ByName: "second",
ByName: "beta",
IncludeExpired: true,
})
assert.NilError(t, err)
Expand Down