Skip to content

Support authorizedCollections option for listCollections helpers - #1263

Closed
stIncMale wants to merge 39 commits into
mongodb:5.xfrom
stIncMale:JAVA-4353_5.x
Closed

stIncMale wants to merge 39 commits into
mongodb:5.xfrom
stIncMale:JAVA-4353_5.x

Conversation

@stIncMale

@stIncMale stIncMale commented Nov 20, 2023 •

Copy link
Copy Markdown
Member

This PR is based on the original #855.

API changes

MongoDatabase.listCollectionNames

Changed the return type of a method. The change preserves source-code compatibility (existing source code that uses the changed methods does not have to be changed), breaks binary compatibility (existing source code that uses the changed methods has to be recompiled against the updated driver dependency).

package class/interface method(s) old return type new return type
com.mongodb.client MongoDatabase listCollectionNames MongoIterable ListCollectionsIterable
com.mongodb.reactivestreams.client MongoDatabase listCollectionNames Publisher ListCollectionsPublisher
org.mongodb.scala MongoDatabase listCollectionNames Observable ListCollectionsObservable
com.mongodb.kotlin.client MongoDatabase listCollectionNames MongoIterable ListCollectionsIterable
com.mongodb.kotlin.client.coroutine MongoDatabase listCollectionNames Flow ListCollectionsFlow

authorizedCollections

Added a method to an interface/class. The change preserves source-code and binary compatibility when done to a class, but breaks source-code compatibility for implementors when done to an interface. For the interfaces affected, we disregard such a change as breaking nonetheless.

package class/interface new method
com.mongodb.client ListCollectionsIterable authorizedCollections
com.mongodb.reactivestreams.client ListCollectionsPublisher authorizedCollections
org.mongodb.scala ListCollectionsObservable authorizedCollections
com.mongodb.kotlin.client ListCollectionsIterable authorizedCollections
com.mongodb.kotlin.client.coroutine ListCollectionsFlow authorizedCollections

API design

Note how authorizedCollections is documented to be ignored in some situations. There seem to be two alternatives:

  1. Throw a runtime exception in a situation when we ignore the new option. I didn't do this because the server ignores this situation (the behavior is documented), though it does not make sense to me. I am afraid that someone may end up having a security bug caused by this behavior.
  2. Introduce subtypes for each interface/class where authorizedCollections is added, add this method to the new types, return these types from listCollectionNames. I didn't try this approach in the original PR, maybe because it seemed like more work, or maybe because the approach didn't occur to me. Will see what reviewers think about this.

Other notes

You may see a few methods marked as @VisibleForTesting, which are used reflectively either from Java tests, or from Groovy. The code that effectively required the presence of these methods was already in the tests, I simply made the changes to fit into existing test infrastructure.

JAVA-4353

jyemin added 29 commits October 10, 2023 16:06
  MongoClientSettings#getStreamFactoryFactory()
  MongoClientSettings.Builder#streamFactoryFactory(StreamFactoryFactory)
  AsynchronousSocketChannelStreamFactory
  AsynchronousSocketChannelStreamFactoryFactory
  BufferProvider
  SocketStreamFactory
  Stream
  StreamFactory
  StreamFactoryFactory
  TlsChannelStreamFactoryFactory
  NettyStreamFactory
  NettyStreamFactoryFactory

JAVA-5161
* Remove builders
* Remove now-unused factory configuration
* Simplify usage in MongoClient instantiation

JAVA-5161
* ServerAddress#getSocketAddress
* ServerAddress#getSocketAddresses
* UnixServerAddress#getSocketAddress
* UnixServerAddress#getUnixSocketAddress

JAVA-4937
Since we ended up adding explain support to FindIterable,
no reason to leave it deprecated in the legacy API.

JAVA-5154
The dead code is a remnant of when the driver supported the
full range of OP_REPLY wire protocol message usage.  Since the driver
now only runs against MongoDB releases that support OP_MSG,
OP_REPLY is only used for the response to the `hello` command
in the handshake, and therefore most of the OP_REPLY-handling code is
no longer on any execution paths.

JAVA-5204
Now that Stream is not part of the API, this method
can be removed.  It only existed due to the possibility
that an application creates its own Stream implementation.

JAVA-5180
* getStats
* isCapped

JAVA-5116
from the following internal interfaces:

* ClusterAwareReadWriteBinding
* AsyncClusterAwareReadWriteBinding

The interfaces remain as they have picket up a second method.

JAVA-5217
Since StreamFactoryFactory is now internal, just made that interface
extend AutoCloseable in order to simplify MongoClients code

JAVA-5158
* ConnectionPoolOpenedEvent
* ConnectionAddedEvent
* ConnectionRemovedEvent
* ConnectionPoolListener#connectionPoolOpened
* ConnectionPoolListener#connectionAdded
* ConnectionPoolListener#connectionRemoved

JAVA-5151
* ClusterListenerAdapter
* ConnectionPoolListenerAdapter
* ServerListenerAdapter
* ServerMonitorListenerAdapter

JAVA-5151
It's still used by IterableCodecProvider, so it can't be removed entirely.
(Though IterableCodecProvider is almost entirely supplanted by CollectionCodecProvider,
it was not itself deprecated because it provides a codec for any Iterable, while
CollectionCodecProvider only provides one for any Collection, which is not quite the
same thing).

JAVA-5142
@stIncMale stIncMale self-assigned this Nov 20, 2023
Comment on lines 44 to 46

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are alternatives to ignoring the option, as described in the "API design" section of the PR description.

@stIncMale
stIncMale force-pushed the JAVA-4353_5.x branch 2 times, most recently from 495d60e to 18dbb33 Compare November 20, 2023 23:52
@stIncMale
stIncMale requested review from jyemin and rozza November 21, 2023 00:03

@rozza rozza left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure its worth adding a ListCollection Mapper type to wrap the Observable / Iterable. As listCollectionNames is a helper built on top of listCollections it be easier if it automatically sets authorizedCollections=true.

What do you think?

The listCollections and listDatabases API should be inline - so either way this may require another ticket to bring the listDatabase method inline.

@rozza rozza Nov 21, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is annoying that its needed.

@stIncMale stIncMale Nov 21, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1) I'm not 100% sure its worth adding a ListCollection Mapper type to wrap the Observable / Iterable.

If we don't have

  • ListCollectionsIterableImpl.map returning ListCollectionsIterable
  • ListCollectionsPublisherImpl.map returning ListCollectionsPublisher

how will

  • com.mongodb.client.internal.MongoDatabaseImpl.listCollectionNames
  • com.mongodb.reactivestreams.client.internal.MongoDatabaseImpl.listCollectionNames

be implemented?

2) As listCollectionNames is a helper built on top of listCollections it be easier if it automatically sets authorizedCollections=true.

listCollectionNames can set authorizedCollections=true automatically only if it know when a user wants authorizedCollections=true, and when a user doesn't want it. But how will listCollectionNames know that information without a user explicitly providing it?

3) The listCollections and listDatabases API should be inline - so either way this may require another ticket to bring the listDatabase method inline.

  1. They have already been different: there is ListDatabasesIterable.nameOnly, while the same option for ListCollectionsIterable can be set only internally by the driver when listCollectionNames is called. If we expose ListDatabasesIterable from listDatabaseNames, then a user will be able to call ListDatabasesIterable.nameOnly(false), which does not make sense. However, if we have ListDatabaseNamesIterable (similarly to ListCollectionNamesIterable, which is the solution Jeff prefers, then we won't have to expose nameOnly when listDatabaseNames is called. Note that even if we have a ticket to deal with this (I'll create it), it can only be done in a major release, and I am not sure we want to do it in the upcoming major release.
  2. The ListCollectionsPublisherImpl.map method added in the PR is not part of API, it's an internal method. While a similar method could be added to ListDatabasesPublisherImpl now, it will be unused. I don't think we should add unused internal method to ListDatabasesPublisherImpl only because a similar method is present on ListCollectionsPublisherImpl.

@jyemin jyemin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the API design, I am inclined to go with option 2 and introduce new ListCollectionNamesIterable and related interfaces. It is more work to maintain but will protect against the existing weirdness of having an option that is ignored for listCollections and the possibility of more options like this being added in the future.

Somehow we ended up with three nearly identical classes, all
of which were unnecessarily complicated.

Remove two of them and simplify the remaining one to use
CompletableFuture.
@stIncMale

stIncMale commented Nov 22, 2023 •

Copy link
Copy Markdown
Member Author

Regarding the API design, I am inclined to go with option 2 and introduce new ListCollectionNamesIterable and related interfaces.

This turned out surprisingly non-trivial (I am talking only about synchronous Java API, haven't touched anything else). The approach I tried yesterday was a dead end. I have another one in mind.

@stIncMale
stIncMale marked this pull request as draft November 24, 2023 18:08
@stIncMale
stIncMale force-pushed the JAVA-4353_5.x branch 2 times, most recently from ce47fa2 to bcc7ac1 Compare November 27, 2023 05:28
@stIncMale

Copy link
Copy Markdown
Member Author

Regarding the API design, I am inclined to go with option 2 and introduce new ListCollectionNamesIterable and related interfaces.

Done in #1270.

@stIncMale
stIncMale deleted the JAVA-4353_5.x branch December 8, 2023 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants