Skip to content

Commit c999a05

Browse files
authored
GODRIVER-2190 Support authorizedCollections option for ListCollections (#832)
1 parent 384560d commit c999a05

File tree

4 files changed

+60
-15
lines changed

4 files changed

+60
-15
lines changed

mongo/database.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ func (db *Database) ListCollections(ctx context.Context, filter interface{}, opt
382382
cursorOpts.BatchSize = *lco.BatchSize
383383
op = op.BatchSize(*lco.BatchSize)
384384
}
385+
if lco.AuthorizedCollections != nil {
386+
op = op.AuthorizedCollections(*lco.AuthorizedCollections)
387+
}
385388

386389
retry := driver.RetryNone
387390
if db.client.retryReads {

mongo/integration/database_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,19 @@ func TestDatabase(t *testing.T) {
248248
_, err = evt.Command.LookupErr("cursor", "batchSize")
249249
assert.Nil(mt, err, "expected command %s to contain key 'batchSize'", evt.Command)
250250
})
251+
mt.RunOpts("authorizedCollections", mtest.NewOptions().MinServerVersion("4.0"), func(mt *mtest.T) {
252+
mt.ClearEvents()
253+
lcOpts := options.ListCollections().SetAuthorizedCollections(true)
254+
_, err := mt.DB.ListCollections(mtest.Background, bson.D{}, lcOpts)
255+
assert.Nil(mt, err, "ListCollections error: %v", err)
256+
257+
evt := mt.GetStartedEvent()
258+
assert.Equal(mt, "listCollections", evt.CommandName,
259+
"expected 'listCollections' command to be sent, got %q", evt.CommandName)
260+
_, err = evt.Command.LookupErr("authorizedCollections")
261+
assert.Nil(mt, err, "expected command to contain key 'authorizedCollections'")
262+
263+
})
251264
mt.Run("getMore commands are monitored", func(mt *mtest.T) {
252265
createCollections(mt, 2)
253266
assertGetMoreCommandsAreMonitored(mt, cmdMonitoringCmdName, func() (*mongo.Cursor, error) {

mongo/options/listcollectionsoptions.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ type ListCollectionsOptions struct {
1313

1414
// The maximum number of documents to be included in each batch returned by the server.
1515
BatchSize *int32
16+
17+
// If true, and NameOnly is true, limits the documents returned to only contain collections the user is authorized to use. The default value
18+
// is false. This option is only valid for MongoDB server versions >= 4.0. Server versions < 4.0 ignore this option.
19+
AuthorizedCollections *bool
1620
}
1721

1822
// ListCollections creates a new ListCollectionsOptions instance.
@@ -32,6 +36,13 @@ func (lc *ListCollectionsOptions) SetBatchSize(size int32) *ListCollectionsOptio
3236
return lc
3337
}
3438

39+
// SetAuthorizedCollections sets the value for the AuthorizedCollections field. This option is only valid for MongoDB server versions >= 4.0. Server
40+
// versions < 4.0 ignore this option.
41+
func (lc *ListCollectionsOptions) SetAuthorizedCollections(b bool) *ListCollectionsOptions {
42+
lc.AuthorizedCollections = &b
43+
return lc
44+
}
45+
3546
// MergeListCollectionsOptions combines the given ListCollectionsOptions instances into a single *ListCollectionsOptions
3647
// in a last-one-wins fashion.
3748
func MergeListCollectionsOptions(opts ...*ListCollectionsOptions) *ListCollectionsOptions {
@@ -46,6 +57,9 @@ func MergeListCollectionsOptions(opts ...*ListCollectionsOptions) *ListCollectio
4657
if opt.BatchSize != nil {
4758
lc.BatchSize = opt.BatchSize
4859
}
60+
if opt.AuthorizedCollections != nil {
61+
lc.AuthorizedCollections = opt.AuthorizedCollections
62+
}
4963
}
5064

5165
return lc

x/mongo/driver/operation/list_collections.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,21 @@ import (
2020

2121
// ListCollections performs a listCollections operation.
2222
type ListCollections struct {
23-
filter bsoncore.Document
24-
nameOnly *bool
25-
session *session.Client
26-
clock *session.ClusterClock
27-
monitor *event.CommandMonitor
28-
crypt driver.Crypt
29-
database string
30-
deployment driver.Deployment
31-
readPreference *readpref.ReadPref
32-
selector description.ServerSelector
33-
retry *driver.RetryMode
34-
result driver.CursorResponse
35-
batchSize *int32
36-
serverAPI *driver.ServerAPIOptions
23+
filter bsoncore.Document
24+
nameOnly *bool
25+
authorizedCollections *bool
26+
session *session.Client
27+
clock *session.ClusterClock
28+
monitor *event.CommandMonitor
29+
crypt driver.Crypt
30+
database string
31+
deployment driver.Deployment
32+
readPreference *readpref.ReadPref
33+
selector description.ServerSelector
34+
retry *driver.RetryMode
35+
result driver.CursorResponse
36+
batchSize *int32
37+
serverAPI *driver.ServerAPIOptions
3738
}
3839

3940
// NewListCollections constructs and returns a new ListCollections.
@@ -89,14 +90,17 @@ func (lc *ListCollections) Execute(ctx context.Context) error {
8990
}
9091

9192
func (lc *ListCollections) command(dst []byte, desc description.SelectedServer) ([]byte, error) {
92-
9393
dst = bsoncore.AppendInt32Element(dst, "listCollections", 1)
9494
if lc.filter != nil {
9595
dst = bsoncore.AppendDocumentElement(dst, "filter", lc.filter)
9696
}
9797
if lc.nameOnly != nil {
9898
dst = bsoncore.AppendBooleanElement(dst, "nameOnly", *lc.nameOnly)
9999
}
100+
if lc.authorizedCollections != nil {
101+
dst = bsoncore.AppendBooleanElement(dst, "authorizedCollections", *lc.authorizedCollections)
102+
}
103+
100104
cursorDoc := bsoncore.NewDocumentBuilder()
101105
if lc.batchSize != nil {
102106
cursorDoc.AppendInt32("batchSize", *lc.batchSize)
@@ -126,6 +130,17 @@ func (lc *ListCollections) NameOnly(nameOnly bool) *ListCollections {
126130
return lc
127131
}
128132

133+
// AuthorizedCollections specifies whether to only return collections the user
134+
// is authorized to use.
135+
func (lc *ListCollections) AuthorizedCollections(authorizedCollections bool) *ListCollections {
136+
if lc == nil {
137+
lc = new(ListCollections)
138+
}
139+
140+
lc.authorizedCollections = &authorizedCollections
141+
return lc
142+
}
143+
129144
// Session sets the session for this operation.
130145
func (lc *ListCollections) Session(session *session.Client) *ListCollections {
131146
if lc == nil {

0 commit comments

Comments
 (0)