listing users (#118)

This commit is contained in:
Jim authored and GitHub committed 2020-06-16 18:59:30 -04:00
1 parent 6ea448a7f3
commit d9073a03d5
10 files changed
+335 -139

No files matched your search

+21 -18
View File
@@ -5,7 +5,7 @@ import (
"github.com/hashicorp/watchtower/internal/oplog"
)
// GetOpts - iterate the inbound Options and return a struct
// GetOpts - iterate the inbound Options and return a struct.
func GetOpts(opt ...Option) Options {
opts := getDefaultOptions()
for _, o := range opt {
@@ -14,18 +14,19 @@ func GetOpts(opt ...Option) Options {
return opts
}
// Option - how Options are passed as arguments
// Option - how Options are passed as arguments.
type Option func(*Options)
// Options = how Options are represented
// Options - how Options are represented.
type Options struct {
withOplog bool
oplogOpts oplogOpts
withDebug bool
withLookup bool
// WithFieldMaskPaths must be accessible from other packages
// WithLimit must be accessible in other packages.
WithLimit int
// WithFieldMaskPaths must be accessible from other packages.
WithFieldMaskPaths []string
// WithNullPaths must be accessible from other packages
// WithNullPaths must be accessible from other packages.
WithNullPaths []string
}
@@ -41,28 +42,21 @@ func getDefaultOptions() Options {
wrapper: nil,
metadata: oplog.Metadata{},
},
withDebug: false,
withLookup: false,
WithFieldMaskPaths: []string{},
WithNullPaths: []string{},
WithLimit: 0,
}
}
// WithLookup enables a lookup
// WithLookup enables a lookup.
func WithLookup(enable bool) Option {
return func(o *Options) {
o.withLookup = enable
}
}
// WithDebug enables debug
func WithDebug(enable bool) Option {
return func(o *Options) {
o.withDebug = enable
}
}
// WithOplog provides an option to write an oplog entry
// WithOplog provides an option to write an oplog entry.
func WithOplog(wrapper wrapping.Wrapper, md oplog.Metadata) Option {
return func(o *Options) {
o.withOplog = true
@@ -73,16 +67,25 @@ func WithOplog(wrapper wrapping.Wrapper, md oplog.Metadata) Option {
}
}
// WithFieldMaskPaths provides an option to provide field mask paths
// WithFieldMaskPaths provides an option to provide field mask paths.
func WithFieldMaskPaths(paths []string) Option {
return func(o *Options) {
o.WithFieldMaskPaths = paths
}
}
// WithNullPaths provides an option to provide null paths
// WithNullPaths provides an option to provide null paths.
func WithNullPaths(paths []string) Option {
return func(o *Options) {
o.WithNullPaths = paths
}
}
// WithLimit provides an option to provide a limit. Intentionally allowing
// negative integers. If WithLimit < 0, then unlimited results are returned.
// If WithLimit == 0, then default limits are used for results.
func WithLimit(limit int) Option {
return func(o *Options) {
o.WithLimit = limit
}
}
+22 -14
View File
@@ -9,9 +9,9 @@ import (
// Test_getOpts provides unit tests for GetOpts and all the options
func Test_getOpts(t *testing.T) {
assert := assert.New(t)
t.Parallel()
t.Run("WithOplog", func(t *testing.T) {
assert := assert.New(t)
// test default of false
opts := GetOpts()
testOpts := getDefaultOptions()
@@ -35,6 +35,7 @@ func Test_getOpts(t *testing.T) {
assert.Equal(opts, testOpts)
})
t.Run("WithLookup", func(t *testing.T) {
assert := assert.New(t)
// test default of true
opts := GetOpts()
testOpts := getDefaultOptions()
@@ -47,20 +48,8 @@ func Test_getOpts(t *testing.T) {
testOpts.withLookup = true
assert.Equal(opts, testOpts)
})
t.Run("WithDebug", func(t *testing.T) {
// test default of false
opts := GetOpts()
testOpts := getDefaultOptions()
testOpts.withDebug = false
assert.Equal(opts, testOpts)
// try setting to true
opts = GetOpts(WithDebug(true))
testOpts = getDefaultOptions()
testOpts.withDebug = true
assert.Equal(opts, testOpts)
})
t.Run("WithFieldMaskPaths", func(t *testing.T) {
assert := assert.New(t)
// test default of []string{}
opts := GetOpts()
testOpts := getDefaultOptions()
@@ -74,6 +63,7 @@ func Test_getOpts(t *testing.T) {
assert.Equal(opts, testOpts)
})
t.Run("WithNullPaths", func(t *testing.T) {
assert := assert.New(t)
// test default of []string{}
opts := GetOpts()
testOpts := getDefaultOptions()
@@ -86,4 +76,22 @@ func Test_getOpts(t *testing.T) {
testOpts.WithNullPaths = testPaths
assert.Equal(opts, testOpts)
})
t.Run("WithLimit", func(t *testing.T) {
assert := assert.New(t)
// test default of 0
opts := GetOpts()
testOpts := getDefaultOptions()
testOpts.WithLimit = 0
assert.Equal(opts, testOpts)
opts = GetOpts(WithLimit(-1))
testOpts = getDefaultOptions()
testOpts.WithLimit = -1
assert.Equal(opts, testOpts)
opts = GetOpts(WithLimit(1))
testOpts = getDefaultOptions()
testOpts.WithLimit = 1
assert.Equal(opts, testOpts)
})
}
+27 -39
View File
@@ -15,7 +15,12 @@ import (
"google.golang.org/protobuf/proto"
)
const NoRowsAffected = 0
const (
NoRowsAffected = 0
// DefaultLimit is the default for results for watchtower
DefaultLimit = 10000
)
// Reader interface defines lookups/searching for resources
type Reader interface {
@@ -28,8 +33,11 @@ type Reader interface {
// LookupWhere will lookup and return the first resource using a where clause with parameters
LookupWhere(ctx context.Context, resource interface{}, where string, args ...interface{}) error
// SearchWhere will search for all the resources it can find using a where clause with parameters
SearchWhere(ctx context.Context, resources interface{}, where string, args ...interface{}) error
// SearchWhere will search for all the resources it can find using a where
// clause with parameters. Supports the WithLimit option. If
// WithLimit < 0, then unlimited results are returned. If WithLimit == 0, then
// default limits are used for results.
SearchWhere(ctx context.Context, resources interface{}, where string, args []interface{}, opt ...Option) error
// ScanRows will scan sql rows into the interface provided
ScanRows(rows *sql.Rows, result interface{}) error
@@ -171,7 +179,6 @@ func (rw *Db) Create(ctx context.Context, i interface{}, opt ...Option) error {
}
opts := GetOpts(opt...)
withOplog := opts.withOplog
withDebug := opts.withDebug
if withOplog {
// let's validate oplog options before we start writing to the database
_, err := validateOplogArgs(i, opts)
@@ -179,10 +186,6 @@ func (rw *Db) Create(ctx context.Context, i interface{}, opt ...Option) error {
return fmt.Errorf("create: oplog validation failed %w", err)
}
}
if withDebug {
rw.underlying.LogMode(true)
defer rw.underlying.LogMode(false)
}
// these fields should be nil, since they are not writeable and we want the
// db to manage them
setFieldsToNil(i, []string{"CreateTime", "UpdateTime"})
@@ -256,7 +259,6 @@ func (rw *Db) Update(ctx context.Context, i interface{}, fieldMaskPaths []string
}
opts := GetOpts(opt...)
withDebug := opts.withDebug
withOplog := opts.withOplog
if withOplog {
// let's validate oplog options before we start writing to the database
@@ -265,10 +267,6 @@ func (rw *Db) Update(ctx context.Context, i interface{}, fieldMaskPaths []string
return NoRowsAffected, fmt.Errorf("update: oplog validation failed %w", err)
}
}
if withDebug {
rw.underlying.LogMode(true)
defer rw.underlying.LogMode(false)
}
if vetter, ok := i.(VetForWriter); ok {
if err := vetter.VetForWrite(ctx, rw, UpdateOp, WithFieldMaskPaths(fieldMaskPaths), WithNullPaths(setToNullPaths)); err != nil {
return NoRowsAffected, fmt.Errorf("update: vet for write failed %w", err)
@@ -293,7 +291,6 @@ func (rw *Db) Update(ctx context.Context, i interface{}, fieldMaskPaths []string
oplogOpts := Options{
oplogOpts: opts.oplogOpts,
withOplog: opts.withOplog,
withDebug: opts.withDebug,
WithFieldMaskPaths: oplogFieldMasks,
WithNullPaths: oplogNullPaths,
}
@@ -327,7 +324,6 @@ func (rw *Db) Delete(ctx context.Context, i interface{}, opt ...Option) (int, er
return NoRowsAffected, fmt.Errorf("delete: primary key is not set")
}
opts := GetOpts(opt...)
withDebug := opts.withDebug
withOplog := opts.withOplog
if withOplog {
_, err := validateOplogArgs(i, opts)
@@ -335,10 +331,6 @@ func (rw *Db) Delete(ctx context.Context, i interface{}, opt ...Option) (int, er
return NoRowsAffected, fmt.Errorf("delete: oplog validation failed %w", err)
}
}
if withDebug {
rw.underlying.LogMode(true)
defer rw.underlying.LogMode(false)
}
underlying := rw.underlying.Delete(i)
if underlying.Error != nil {
return NoRowsAffected, fmt.Errorf("delete: failed %w", underlying.Error)
@@ -374,11 +366,6 @@ func (rw *Db) addOplog(ctx context.Context, opType OpType, opts Options, i inter
return err
}
gdb := rw.underlying
withDebug := opts.withDebug
if withDebug {
gdb.LogMode(true)
defer gdb.LogMode(false)
}
ticketer, err := oplog.NewGormTicketer(gdb, oplog.WithAggregateNames(true))
if err != nil {
return fmt.Errorf("error getting Ticketer %w for WithOplog", err)
@@ -471,12 +458,6 @@ func (rw *Db) LookupByName(ctx context.Context, resource ResourceNamer, opt ...O
if rw.underlying == nil {
return errors.New("error underlying db nil for lookup by name")
}
opts := GetOpts(opt...)
withDebug := opts.withDebug
if withDebug {
rw.underlying.LogMode(true)
defer rw.underlying.LogMode(false)
}
if reflect.ValueOf(resource).Kind() != reflect.Ptr {
return errors.New("error interface parameter must to be a pointer for lookup by name")
}
@@ -497,12 +478,6 @@ func (rw *Db) LookupByPublicId(ctx context.Context, resource ResourcePublicIder,
if rw.underlying == nil {
return errors.New("error underlying db nil for lookup by public id")
}
opts := GetOpts(opt...)
withDebug := opts.withDebug
if withDebug {
rw.underlying.LogMode(true)
defer rw.underlying.LogMode(false)
}
if reflect.ValueOf(resource).Kind() != reflect.Ptr {
return errors.New("error interface parameter must to be a pointer for lookup by public id")
}
@@ -535,15 +510,28 @@ func (rw *Db) LookupWhere(ctx context.Context, resource interface{}, where strin
return nil
}
// SearchWhere will search for all the resources it can find using a where clause with parameters
func (rw *Db) SearchWhere(ctx context.Context, resources interface{}, where string, args ...interface{}) error {
// SearchWhere will search for all the resources it can find using a where
// clause with parameters. Supports the WithLimit option. If
// WithLimit < 0, then unlimited results are returned. If WithLimit == 0, then
// default limits are used for results.
func (rw *Db) SearchWhere(ctx context.Context, resources interface{}, where string, args []interface{}, opt ...Option) error {
opts := GetOpts(opt...)
if rw.underlying == nil {
return errors.New("error underlying db nil for search by")
}
if reflect.ValueOf(resources).Kind() != reflect.Ptr {
return errors.New("error interface parameter must to be a pointer for search by")
}
if err := rw.underlying.Where(where, args...).Find(resources).Error; err != nil {
var err error
switch {
case opts.WithLimit < 0: // any negative number signals unlimited results
err = rw.underlying.Where(where, args...).Find(resources).Error
case opts.WithLimit == 0: // zero signals the default value and default limits
err = rw.underlying.Limit(DefaultLimit).Where(where, args...).Find(resources).Error
default:
err = rw.underlying.Limit(opts.WithLimit).Where(where, args...).Find(resources).Error
}
if err != nil {
// searching with a slice parameter does not return a gorm.ErrRecordNotFound
return err
}
+107 -60
View File
@@ -1,7 +1,6 @@
package db
import (
"bytes"
"context"
"errors"
"fmt"
@@ -10,7 +9,6 @@ import (
"time"
"github.com/golang/protobuf/ptypes"
"github.com/hashicorp/go-hclog"
wrapping "github.com/hashicorp/go-kms-wrapping"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/vault/sdk/helper/base62"
@@ -676,67 +674,116 @@ func TestDb_SearchWhere(t *testing.T) {
t.Parallel()
cleanup, db, _ := TestSetup(t, "postgres")
defer func() {
if err := cleanup(); err != nil {
t.Error(err)
}
err := cleanup()
assert.NoError(t, err)
err = db.Close()
assert.NoError(t, err)
}()
assert := assert.New(t)
defer db.Close()
knownUser := testUser(t, db, "", "", "")
buf := new(bytes.Buffer)
log := hclog.New(&hclog.LoggerOptions{
Output: buf,
Level: hclog.Trace,
})
gorm.LogFormatter = GetGormLogFormatter(log)
db.SetLogger(GetGormLogger(log))
db.LogMode(true)
defer func() {
assert.True(strings.Contains(buf.String(), "syntax error at or near"))
}()
type args struct {
where string
arg []interface{}
opt []Option
}
tests := []struct {
name string
db Db
createCnt int
args args
wantCnt int
wantErr bool
}{
{
name: "no-limit",
db: Db{underlying: db},
createCnt: 10,
args: args{
where: "1=1",
opt: []Option{WithLimit(-1)},
},
wantCnt: 11, // there's an additional knownUser
wantErr: false,
},
{
name: "custom-limit",
db: Db{underlying: db},
createCnt: 10,
args: args{
where: "1=1",
opt: []Option{WithLimit(3)},
},
wantCnt: 3,
wantErr: false,
},
{
name: "simple",
db: Db{underlying: db},
createCnt: 1,
args: args{
where: "public_id = ?",
arg: []interface{}{knownUser.PublicId},
opt: []Option{WithLimit(3)},
},
wantCnt: 1,
wantErr: false,
},
{
name: "not-found",
db: Db{underlying: db},
createCnt: 1,
args: args{
where: "public_id = ?",
arg: []interface{}{"bad-id"},
opt: []Option{WithLimit(3)},
},
wantCnt: 0,
wantErr: false,
},
{
name: "bad-where",
db: Db{underlying: db},
createCnt: 1,
args: args{
where: "bad_column_name = ?",
arg: []interface{}{knownUser.PublicId},
opt: []Option{WithLimit(3)},
},
wantCnt: 0,
wantErr: true,
},
{
name: "nil-underlying",
db: Db{},
createCnt: 1,
args: args{
where: "public_id = ?",
arg: []interface{}{knownUser.PublicId},
opt: []Option{WithLimit(3)},
},
wantCnt: 0,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
testUsers := []*db_test.TestUser{}
for i := 0; i < tt.createCnt; i++ {
testUsers = append(testUsers, testUser(t, db, "", "", ""))
}
assert.Equal(tt.createCnt, len(testUsers))
t.Run("simple", func(t *testing.T) {
w := Db{underlying: db}
id, err := uuid.GenerateUUID()
assert.NoError(err)
user, err := db_test.NewTestUser()
assert.NoError(err)
user.Name = "foo-" + id
err = w.Create(context.Background(), user)
assert.NoError(err)
assert.NotEmpty(user.PublicId)
var foundUsers []db_test.TestUser
err = w.SearchWhere(context.Background(), &foundUsers, "public_id = ?", user.PublicId)
assert.NoError(err)
assert.Equal(foundUsers[0].Id, user.Id)
})
t.Run("tx-nil,", func(t *testing.T) {
w := Db{}
var foundUsers []db_test.TestUser
err := w.SearchWhere(context.Background(), &foundUsers, "public_id = ?", 1)
assert.Error(err)
assert.Equal("error underlying db nil for search by", err.Error())
})
t.Run("not-found", func(t *testing.T) {
w := Db{underlying: db}
id, err := uuid.GenerateUUID()
assert.NoError(err)
var foundUsers []db_test.TestUser
err = w.SearchWhere(context.Background(), &foundUsers, "public_id = ?", id)
assert.NoError(err)
assert.Equal(0, len(foundUsers))
})
t.Run("bad-where", func(t *testing.T) {
w := Db{underlying: db}
id, err := uuid.GenerateUUID()
assert.NoError(err)
var foundUsers []db_test.TestUser
err = w.SearchWhere(context.Background(), &foundUsers, "? = ?", id)
assert.Error(err)
})
var foundUsers []db_test.TestUser
err := tt.db.SearchWhere(context.Background(), &foundUsers, tt.args.where, tt.args.arg, tt.args.opt...)
if tt.wantErr {
require.Error(err)
return
}
require.NoError(err)
assert.Equal(tt.wantCnt, len(foundUsers))
})
}
}
func TestDb_DB(t *testing.T) {
+11
View File
@@ -19,6 +19,7 @@ type options struct {
withScope *Scope
withDescription string
withParentId *string
withLimit int
}
func getDefaultOptions() options {
@@ -28,6 +29,7 @@ func getDefaultOptions() options {
withDescription: "",
withName: "",
withParentId: nil,
withLimit: 0,
}
}
@@ -58,3 +60,12 @@ func WithName(name string) Option {
o.withName = name
}
}
// WithLimit provides an option to provide a limit. Intentionally allowing
// negative integers. If WithLimit < 0, then unlimited results are returned.
// If WithLimit == 0, then default limits are used for results.
func WithLimit(limit int) Option {
return func(o *options) {
o.withLimit = limit
}
}
+18
View File
@@ -34,4 +34,22 @@ func Test_GetOpts(t *testing.T) {
testOpts.withDescription = "test desc"
assert.Equal(opts, testOpts)
})
t.Run("WithLimit", func(t *testing.T) {
assert := assert.New(t)
// test default of 0
opts := getOpts()
testOpts := getDefaultOptions()
testOpts.withLimit = 0
assert.Equal(opts, testOpts)
opts = getOpts(WithLimit(-1))
testOpts = getDefaultOptions()
testOpts.withLimit = -1
assert.Equal(opts, testOpts)
opts = getOpts(WithLimit(1))
testOpts = getDefaultOptions()
testOpts.withLimit = 1
assert.Equal(opts, testOpts)
})
}
+27 -5
View File
@@ -21,10 +21,14 @@ type Repository struct {
reader db.Reader
writer db.Writer
wrapper wrapping.Wrapper
// defaultLimit provides a default for limiting the number of results returned from the repo
defaultLimit int
}
// NewRepository creates a new iam Repository
func NewRepository(r db.Reader, w db.Writer, wrapper wrapping.Wrapper) (*Repository, error) {
// NewRepository creates a new iam Repository. Supports the options: WithLimit
// which sets a default limit on results returned by repo operations.
func NewRepository(r db.Reader, w db.Writer, wrapper wrapping.Wrapper, opt ...Option) (*Repository, error) {
if r == nil {
return nil, errors.New("error creating db repository with nil reader")
}
@@ -34,13 +38,31 @@ func NewRepository(r db.Reader, w db.Writer, wrapper wrapping.Wrapper) (*Reposit
if wrapper == nil {
return nil, errors.New("error creating db repository with nil wrapper")
}
opts := getOpts(opt...)
if opts.withLimit == 0 {
// zero signals the watchtower defaults should be used.
opts.withLimit = db.DefaultLimit
}
return &Repository{
reader: r,
writer: w,
wrapper: wrapper,
reader: r,
writer: w,
wrapper: wrapper,
defaultLimit: opts.withLimit,
}, nil
}
// list will return a listing of resources and honor the WithLimit option or the
// repo defaultLimit
func (r *Repository) list(ctx context.Context, resources interface{}, where string, args []interface{}, opt ...Option) error {
opts := getOpts(opt...)
limit := r.defaultLimit
if opts.withLimit != 0 {
// non-zero signals an override of the default limit for the repo.
limit = opts.withLimit
}
return r.reader.SearchWhere(ctx, resources, where, args, db.WithLimit(limit))
}
// create will create a new iam resource in the db repository with an oplog entry
func (r *Repository) create(ctx context.Context, resource Resource, opt ...Option) (Resource, error) {
if resource == nil {
+4 -3
View File
@@ -49,9 +49,10 @@ func TestNewRepository(t *testing.T) {
wrapper: wrapper,
},
want: &Repository{
reader: rw,
writer: rw,
wrapper: wrapper,
reader: rw,
writer: rw,
wrapper: wrapper,
defaultLimit: db.DefaultLimit,
},
wantErr: false,
},
+13
View File
@@ -105,3 +105,16 @@ func (r *Repository) DeleteUser(ctx context.Context, withPublicId string, opt ..
}
return rowsDeleted, nil
}
// ListUsers in an organization and supports the WithLimit option.
func (r *Repository) ListUsers(ctx context.Context, withOrganizationId string, opt ...Option) ([]*User, error) {
if withOrganizationId == "" {
return nil, fmt.Errorf("list users: missing organization id %w", db.ErrInvalidParameter)
}
var users []*User
err := r.list(ctx, &users, "scope_id = ?", []interface{}{withOrganizationId}, opt...)
if err != nil {
return nil, fmt.Errorf("list users: %w", err)
}
return users, nil
}
+85
View File
@@ -445,3 +445,88 @@ func TestRepository_DeleteUser(t *testing.T) {
})
}
}
func TestRepository_ListUsers(t *testing.T) {
t.Parallel()
cleanup, conn, _ := db.TestSetup(t, "postgres")
defer func() {
err := cleanup()
assert.NoError(t, err)
err = conn.Close()
assert.NoError(t, err)
}()
const testLimit = 10
rw := db.New(conn)
wrapper := db.TestWrapper(t)
repo, err := NewRepository(rw, rw, wrapper, WithLimit(testLimit))
require.NoError(t, err)
org, _ := TestScopes(t, conn)
type args struct {
withOrganizationId string
opt []Option
}
tests := []struct {
name string
createCnt int
args args
wantCnt int
wantErr bool
}{
{
name: "no-limit",
createCnt: repo.defaultLimit + 1,
args: args{
withOrganizationId: org.PublicId,
opt: []Option{WithLimit(-1)},
},
wantCnt: repo.defaultLimit + 1,
wantErr: false,
},
{
name: "default-limit",
createCnt: repo.defaultLimit + 1,
args: args{
withOrganizationId: org.PublicId,
},
wantCnt: repo.defaultLimit,
wantErr: false,
},
{
name: "custom-limit",
createCnt: repo.defaultLimit + 1,
args: args{
withOrganizationId: org.PublicId,
opt: []Option{WithLimit(3)},
},
wantCnt: 3,
wantErr: false,
},
{
name: "bad-org",
createCnt: 1,
args: args{
withOrganizationId: "bad-id",
},
wantCnt: 0,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
testUsers := []*User{}
for i := 0; i < tt.createCnt; i++ {
testUsers = append(testUsers, TestUser(t, conn, org.PublicId))
}
assert.Equal(tt.createCnt, len(testUsers))
got, err := repo.ListUsers(context.Background(), tt.args.withOrganizationId, tt.args.opt...)
if tt.wantErr {
require.Error(err)
return
}
require.NoError(err)
assert.Equal(tt.wantCnt, len(got))
})
}
}