Add support for recursive listing (#885)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-01-25 19:37:40 -05:00
1 parent 1f9fcad1cd
commit 6cd97a4a6e
77 files changed
+2244 -1448

No files matched your search

+7 -6
View File
@@ -267,16 +267,17 @@ func (r *Repository) ValidateToken(ctx context.Context, id, token string, opt ..
return retAT, nil
}
// ListAuthTokens in an org and supports the WithLimit option.
func (r *Repository) ListAuthTokens(ctx context.Context, withOrgId string, opt ...Option) ([]*AuthToken, error) {
if withOrgId == "" {
return nil, fmt.Errorf("list users: missing org id %w", errors.ErrInvalidParameter)
// ListAuthTokens lists auth tokens in the given scopes and supports the
// WithLimit option.
func (r *Repository) ListAuthTokens(ctx context.Context, withScopeIds []string, opt ...Option) ([]*AuthToken, error) {
if len(withScopeIds) == 0 {
return nil, fmt.Errorf("list auth tokens: missing scope id %w", errors.ErrInvalidParameter)
}
opts := getOpts(opt...)
var authTokens []*AuthToken
if err := r.reader.SearchWhere(ctx, &authTokens, "auth_account_id in (select public_id from auth_account where scope_id = ?)", []interface{}{withOrgId}, db.WithLimit(opts.withLimit)); err != nil {
return nil, fmt.Errorf("list users: %w", err)
if err := r.reader.SearchWhere(ctx, &authTokens, "auth_account_id in (select public_id from auth_account where scope_id in (?))", []interface{}{withScopeIds}, db.WithLimit(opts.withLimit)); err != nil {
return nil, fmt.Errorf("list auth tokens: %w", err)
}
for _, at := range authTokens {
at.Token = ""
+29 -5
View File
@@ -626,10 +626,9 @@ func TestRepository_ListAuthTokens(t *testing.T) {
want: []*AuthToken{},
},
{
name: "empty-org-id",
orgId: "",
want: nil,
wantErr: errors.ErrInvalidParameter,
name: "empty-org-id",
orgId: "",
want: []*AuthToken{},
},
}
@@ -641,7 +640,7 @@ func TestRepository_ListAuthTokens(t *testing.T) {
require.NoError(err)
require.NotNil(repo)
got, err := repo.ListAuthTokens(context.Background(), tt.orgId)
got, err := repo.ListAuthTokens(context.Background(), []string{tt.orgId})
if tt.wantErr != nil {
assert.Truef(errors.Is(err, tt.wantErr), "want err: %q got: %q", tt.wantErr, err)
return
@@ -653,3 +652,28 @@ func TestRepository_ListAuthTokens(t *testing.T) {
})
}
}
func TestRepository_ListAuthTokens_Multiple_Scopes(t *testing.T) {
t.Parallel()
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
wrapper := db.TestWrapper(t)
kms := kms.TestKms(t, conn, wrapper)
repo, err := NewRepository(rw, rw, kms)
require.NoError(t, err)
iamRepo := iam.TestRepo(t, conn, wrapper)
org, proj := iam.TestScopes(t, iamRepo)
const numPerScope = 10
var total int
for i := 0; i < numPerScope; i++ {
TestAuthToken(t, conn, kms, "global")
total++
TestAuthToken(t, conn, kms, org.GetPublicId())
total++
}
got, err := repo.ListAuthTokens(context.Background(), []string{"global", org.GetPublicId(), proj.GetPublicId()})
require.NoError(t, err)
assert.Equal(t, total, len(got))
}