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

Implement usersInfo command #3897

Merged
merged 57 commits into from
Jan 4, 2024
Merged
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
5d2c18e
wip
AlekSi Dec 12, 2023
25afee7
wip
henvic Dec 12, 2023
0b7235e
wip
henvic Dec 14, 2023
575bd2b
wip
henvic Dec 15, 2023
f5e1d08
Merge branch 'main' into feat-create-user
henvic Dec 15, 2023
5fcc3b0
wip
henvic Dec 16, 2023
c66ae27
wip
henvic Dec 18, 2023
910bf7f
wip
henvic Dec 18, 2023
42cabfc
wip
henvic Dec 18, 2023
8c43dd6
Merge branch 'main' into feat-create-user
henvic Dec 18, 2023
6624561
wip
henvic Dec 18, 2023
ac21f14
wip
henvic Dec 18, 2023
3b876d2
wip
henvic Dec 18, 2023
1f83cf4
wip
rumyantseva Dec 18, 2023
13c9018
wip
henvic Dec 19, 2023
a89061f
Merge branch 'main' into feat-drop-user
henvic Dec 19, 2023
2791849
Merge branch 'main' into feat-drop-user
henvic Dec 19, 2023
bdb41e1
Merge branch 'main' into feat-drop-user
henvic Dec 19, 2023
c162803
wip
henvic Dec 19, 2023
3cdd260
Merge branch 'main' into feat-drop-user
henvic Dec 19, 2023
f75e28d
Merge branch 'main' into feat-drop-user
AlekSi Dec 19, 2023
813b2f4
wip
henvic Dec 19, 2023
e12c48f
Merge branch 'feat-drop-user' into feat-drop-all-users-from-db
henvic Dec 19, 2023
e27b488
update comments
henvic Dec 19, 2023
d4ec94e
satisfying linter
henvic Dec 19, 2023
57188be
wip
henvic Dec 21, 2023
c542e46
Merge branch 'main' into feat-usersInfo
henvic Dec 21, 2023
0cd62b3
wip
henvic Dec 21, 2023
8710ff1
wip
henvic Dec 21, 2023
c3a4c75
Merge branch 'main' into feat-usersInfo
henvic Dec 22, 2023
03fed34
wip
henvic Dec 22, 2023
0986d44
wip
henvic Dec 22, 2023
ca1d188
happy linter
henvic Dec 22, 2023
45b211f
wip
henvic Dec 22, 2023
4a89e49
maybe revert this commit
henvic Dec 22, 2023
6800d57
Merge branch 'main' into feat-usersInfo
henvic Dec 26, 2023
443c2a5
wip
henvic Dec 26, 2023
de9c14e
wip
henvic Dec 27, 2023
723dfb7
wip
henvic Dec 27, 2023
1d42afd
Merge branch 'main' into feat-usersInfo
henvic Dec 27, 2023
8d8e080
Merge branch 'main' into feat-usersInfo
henvic Dec 27, 2023
97b3097
wip
henvic Dec 27, 2023
eea7ec3
wip
henvic Dec 28, 2023
10f6f28
test
henvic Dec 28, 2023
a6726a5
Merge branch 'main' into feat-usersInfo
henvic Dec 28, 2023
78442d1
Merge branch 'main' into feat-usersInfo
henvic Dec 28, 2023
46f322d
wip
henvic Dec 29, 2023
1ed4778
Merge branch 'main' into feat-usersInfo
henvic Dec 29, 2023
04a5e0e
wip
henvic Jan 2, 2024
69e3a36
Merge branch 'main' into feat-usersInfo
henvic Jan 2, 2024
a9839a0
fix doc
henvic Jan 2, 2024
906ba81
wip
henvic Jan 2, 2024
039f3cb
Merge branch 'main' into feat-usersInfo
henvic Jan 4, 2024
1213b0e
wip
henvic Jan 4, 2024
f2e7a23
Merge branch 'main' into feat-usersInfo
henvic Jan 4, 2024
1bfcef6
Merge branch 'main' into feat-usersInfo
AlekSi Jan 4, 2024
3b98b30
Merge branch 'main' into feat-usersInfo
mergify[bot] Jan 4, 2024
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
wip
  • Loading branch information
henvic committed Dec 22, 2023
commit 8710ff15146d3efd428095b0073b306f90ad076e
99 changes: 84 additions & 15 deletions internal/handler/msg_usersinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,34 @@ func (h *Handler) MsgUsersInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs
"showAuthenticationRestrictions", "comment", "filter",
)

var users []usersInfoPair
var (
users []usersInfoPair
allDBs bool // allDBs set to true means we want users from all databases
singleDB bool // singleDB set to true means we want users from a single database (when usersInfo: 1)
)

switch user := usersInfo.(type) {
case *types.Document, string:
case *types.Document:
if user.Has("forAllDBs") {
allDBs = true
break
}

var u usersInfoPair
if err = u.extract(user, dbName); err != nil {
return nil, lazyerrors.Error(err)
}

users = append(users, u)
case string:
var u usersInfoPair
if err = u.extract(user, dbName); err != nil {
return nil, lazyerrors.Error(err)
}

users = append(users, u)
case int: // {usersInfo: 1 }
break
case int, int32: // {usersInfo: 1 } // int for MongoDB and int32 for FerretDB
henvic marked this conversation as resolved.
Show resolved Hide resolved
singleDB = true
case *types.Array:
for i := 0; i < user.Len(); i++ {
var ui any
Expand All @@ -103,8 +119,11 @@ func (h *Handler) MsgUsersInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs
}
}
default:
msg := fmt.Sprintf("required parameter %q has unexpected type %T", document.Command(), usersInfo)
return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrBadValue, msg, document.Command())
return nil, handlererrors.NewCommandErrorMsgWithArgument(
handlererrors.ErrBadValue,
"UserName must be either a string or an object",
document.Command(),
)
}

// Users are saved in the "admin" database.
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -118,8 +137,15 @@ func (h *Handler) MsgUsersInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs
return nil, lazyerrors.Error(err)
}

var filter *types.Document
filter, err = usersInfoQueryFilter(allDBs, singleDB, dbName, users)

if err != nil {
return nil, lazyerrors.Error(err)
}

qr, err := usersCol.Query(ctx, &backends.QueryParams{
Filter: usersInfoQueryFilter(users),
Filter: filter,
})

if err != nil {
Expand All @@ -128,16 +154,25 @@ func (h *Handler) MsgUsersInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs

defer qr.Iter.Close()

var res []*types.Document
var res *types.Array
res, err = types.NewArray()

if err != nil {
return nil, lazyerrors.Error(err)
}

for {
_, v, err := qr.Iter.Next()

if errors.Is(err, iterator.ErrIteratorDone) {
break
} else if err != nil {
}

if err != nil {
return nil, lazyerrors.Error(err)
}
res = append(res, v)

res.Append(v)
}

var reply wire.OpMsg
Expand Down Expand Up @@ -170,7 +205,17 @@ func (p *usersInfoPair) extract(v any, dbName string) error {
p.username, ok = ui.(string)

if !ok {
return lazyerrors.Errorf("unexpected type %T for username", ui)
return handlererrors.NewCommandErrorMsg(
handlererrors.ErrBadValue,
fmt.Sprintf("UserName must contain a string field named: user. But, has type %T", ui),
)
}

if !vt.Has("db") {
return handlererrors.NewCommandErrorMsg(
handlererrors.ErrBadValue,
"UserName must contain a string field named: db.",
)
}

db, err := vt.Get("db")
Expand All @@ -182,7 +227,10 @@ func (p *usersInfoPair) extract(v any, dbName string) error {
if db != nil {
p.db, ok = db.(string)
if !ok {
return lazyerrors.Errorf("unexpected type %T for db", db)
return handlererrors.NewCommandErrorMsg(
handlererrors.ErrBadValue,
fmt.Sprintf("UserName must contain a string field named: db. But, has type %T", db),
)
}
}

Expand All @@ -197,7 +245,28 @@ func (p *usersInfoPair) extract(v any, dbName string) error {
}
}

func usersInfoQueryFilter(u []usersInfoPair) *types.Document {
doc := must.NotFail(types.NewDocument())
return doc
// usersInfoQueryFilter returns a filter for usersInfo command.
//
// When allDBs is true, it returns a filter for all databases.
// When singleDB is true, it returns a filter for a single database (case when usersInfo: 1 is invoked).
// Otherwise, it filters by any pair of user and database.
func usersInfoQueryFilter(allDBs, singleDB bool, dbName string, pairs []usersInfoPair) (*types.Document, error) {
filter := must.NotFail(types.NewDocument())

if allDBs {
return filter, nil
}

if singleDB {
rumyantseva marked this conversation as resolved.
Show resolved Hide resolved
filter.Set("db", dbName)
return filter, nil
}

ids := types.MakeArray(len(pairs))
for i, p := range pairs {
ids.Set(i, p.db+"."+p.username)
}

filter.Set("_id", ids)
return filter, nil
}