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
Show file tree
Hide file tree
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 18, 2023
commit ac21f142fe0085cf330867f30bb254d273af1b95
83 changes: 83 additions & 0 deletions integration/users/drop_all_users_from_database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package users

import (
"context"
"fmt"
"testing"

"github.com/FerretDB/FerretDB/integration"
"github.com/FerretDB/FerretDB/integration/setup"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/util/testutil"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

func TestDropAllUsersFromDatabase(t *testing.T) {
t.Parallel()

ctx, collection := setup.Setup(t)
db := collection.Database()
client := collection.Database().Client()
users := client.Database("admin").Collection("system.users")

if setup.IsMongoDB(t) {
assert.NoError(t, collection.Database().RunCommand(ctx, bson.D{
{"dropAllUsersFromDatabase", 1},
}).Err())
} else {
// Erase any previously saved user in the database.
_, err := users.DeleteMany(ctx, bson.D{{"db", db.Name()}})
assert.NoError(t, err)
}

quantity := 5
for i := 1; i <= quantity; i++ {
err := db.RunCommand(ctx, bson.D{
{"createUser", fmt.Sprintf("user_%d", i)},
{"roles", bson.A{}},
{"pwd", "password"},
}).Err()
assert.NoError(t, err)
}

assertDropAllUsersFromDatabase(t, ctx, db, users, quantity)

// FIXME: calling assertDropAllUsersFromDatabase a second time with quantity = 0
// for FerretDB is triggering a "socket was unexpectedly closed: EOF" error for some reason.
// assertDropAllUsersFromDatabase(t, ctx, db, users, 0)
}

func assertDropAllUsersFromDatabase(t *testing.T, ctx context.Context, db *mongo.Database, users *mongo.Collection, quantity int) {
var res bson.D
err := db.RunCommand(ctx, bson.D{
{"dropAllUsersFromDatabase", 1},
}).Decode(&res)

assert.NoError(t, err)

actual := integration.ConvertDocument(t, res)
actual.Remove("$clusterTime")
actual.Remove("operationTime")

expected := must.NotFail(types.NewDocument("n", int32(quantity), "ok", float64(1)))
testutil.AssertEqual(t, expected, actual)

assert.Equal(t, mongo.ErrNoDocuments, users.FindOne(ctx, bson.D{{"db", db.Name()}}).Err())
}
59 changes: 57 additions & 2 deletions internal/handler/msg_dropallusersfromdatabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@

import (
"context"
"errors"

"github.com/FerretDB/FerretDB/internal/backends"
"github.com/FerretDB/FerretDB/internal/handler/common"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/iterator"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/wire"
)

Expand All @@ -29,6 +34,56 @@
return nil, lazyerrors.Error(err)
}

// TODO https://github.com/FerretDB/FerretDB/issues/1492
return nil, common.Unimplemented(document, document.Command())
// NOTE: In MongoDB, the comment field isn't saved in the database, but used for log and profiling.
common.Ignored(document, h.L, "writeConcern", "comment")

dbName, err := common.GetRequiredParam[string](document, "$db")
if err != nil {
return nil, err
}

Check warning on line 43 in internal/handler/msg_dropallusersfromdatabase.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/msg_dropallusersfromdatabase.go#L42-L43

Added lines #L42 - L43 were not covered by tests

// Users are saved in the "admin" database.
adminDB, err := h.b.Database("admin")
if err != nil {
return nil, lazyerrors.Error(err)
}

Check warning on line 49 in internal/handler/msg_dropallusersfromdatabase.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/msg_dropallusersfromdatabase.go#L48-L49

Added lines #L48 - L49 were not covered by tests

users, err := adminDB.Collection("system.users")
if err != nil {
return nil, lazyerrors.Error(err)
}

Check warning on line 54 in internal/handler/msg_dropallusersfromdatabase.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/msg_dropallusersfromdatabase.go#L53-L54

Added lines #L53 - L54 were not covered by tests

qr, err := users.Query(ctx, &backends.QueryParams{
Filter: must.NotFail(types.NewDocument("db", dbName)),
})
if err != nil {
return nil, lazyerrors.Error(err)
}

Check warning on line 61 in internal/handler/msg_dropallusersfromdatabase.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/msg_dropallusersfromdatabase.go#L60-L61

Added lines #L60 - L61 were not covered by tests

var ids []any
defer qr.Iter.Close()
for {
_, v, err := qr.Iter.Next()
if errors.Is(err, iterator.ErrIteratorDone) {
break
}
ids = append(ids, must.NotFail(v.Get("_id")))
}

res, err := users.DeleteAll(ctx, &backends.DeleteAllParams{
IDs: ids,
})
if err != nil {
return nil, lazyerrors.Error(err)
}

Check warning on line 78 in internal/handler/msg_dropallusersfromdatabase.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/msg_dropallusersfromdatabase.go#L77-L78

Added lines #L77 - L78 were not covered by tests

var reply wire.OpMsg
must.NoError(reply.SetSections(wire.OpMsgSection{
Documents: []*types.Document{must.NotFail(types.NewDocument(
"n", res.Deleted,
"ok", float64(1),
))},
}))

return &reply, nil
}
Loading