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

Use the mongosh command instead of the old mongo command #703

Merged
merged 10 commits into from
Mar 29, 2024

Conversation

stevenpost
Copy link
Contributor

This PR builds on the work @witjoh in #677, the focus here is on getting the minimal set of needed changes and cleaning up his branch. I'll try to move any real cleanup code and other features he was working on into separate PRs for an easy review.

Fixes #648

@stevenpost
Copy link
Contributor Author

Good news, everyone! I'm down to a single failing acceptance test. Once I get that one figured out, I just need to clean up my branch to get it ready for review and properly describe my changes (into the commit message). And do some additional testing to make sure things behave as expected.

The failing unit tests are because of additional debug info, which is going to be removed during branch cleanup.

@h-haaks
Copy link
Contributor

h-haaks commented Mar 21, 2024

Nice work @stevenpost
Keep going until we have this all green, and then we could see if there is even more we could rip out into separte PRs.
Remember PR titles end up in the changelog :)

}
# the new mongosh package is the same for all distros.
# and it follows its own versioning
$package_name = 'mongodb-mongosh'
Copy link
Contributor

Choose a reason for hiding this comment

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

If users have local scripts or puppet code using shell this change will break that...
I think we should make the client class install both both in the beginning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tend to agree for versions < 6.0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I discussed this with @witjoh (he's currently sitting right across me) and he has a point. Let's not bother with this, we already indicate we have breaking changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

I honestly don't understand why you want to push such a break.
The mongo command was deprecated in 5.0 and is a valid command for another 7 months.
The only thing we have to do is add 10 lines of code to install the package.
We don't have to support the command in providers as that is internal module code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Users can still add a package resource themselves. If we wouldn't have any other breaking changes, I'd agree, but since we already do, what's the point of adding deprecated commands to new installations?

Copy link
Contributor

Choose a reason for hiding this comment

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

See your point. I'm not going to hold back on approval because of this even if I don't agree.

Choose a reason for hiding this comment

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

FYI for anyone who does need to support both mongo < 4.4 and > 5.0 ( sadly that's me ) then you can always add a simple mongo version check to the respective mongodb.rb files and have an IF ELSE statement with both the old code and the new :-) ( yes....it's not ideal....but hey it works )

@h-haaks
Copy link
Contributor

h-haaks commented Mar 22, 2024

Could we have a little talk about c16c54f ?
Is this the same issue as #587 (comment)?

I wonder if this could be caused by config being read in a class method and not an instance method or something like that.
I haven't looked deeply into the provider code yet, but my guess is something like this

Provider reads config in prefetch
Config is changed and server restartet.
Provider uses outdated config when trying to update resource

@stevenpost
Copy link
Contributor Author

Could we have a little talk about c16c54f ? Is this the same issue as #587 (comment)?

I wonder if this could be caused by config being read in a class method and not an instance method or something like that. I haven't looked deeply into the provider code yet, but my guess is something like this

Provider reads config in prefetch Config is changed and server restartet. Provider uses outdated config when trying to update resource

Change in the commit: no, it really needs to be silently ignored and has nothing to do with the provider itself. I'll expand on this in the proper commit.

The issue you referenced: at first glance it is because the provider doesn't properly handle reading the yaml file. Although the config is valid yaml, the provider expects this rather weird structure with the dots, at least from a first glance. I'll see if I can tackle this in a follow-up PR.

@stevenpost stevenpost force-pushed the mongosh branch 4 times, most recently from 2843038 to e230c29 Compare March 22, 2024 13:05
The provider previously used the external IP to connect instead of the
loopback.  When using the loopback, the option
`enableLocalhostAuthBypass` (true by default) allows to bypass
authentication when a user doesn't yet exists.  Which is exactly what
would prevent us from executing administrative tasks (like setting up
the replicaset) when auth is enabled, but not yet set up.

We can only connect to localhost when MongoDB is listening on either the
'bind all' address (0.0.0.0 or ::0) or the local loopback address
(127.0.0.1 or ::1).
The `db.serverCmdLineOpts()` method always requires authentication when
auth is enabled. It doesn't take the `enableLocalAuthBypass` parameter
into account, the subsequent authentication attempt always fails if a
user doesn't exists. This commit switches to the `rs.status()` method,
as that method does take the `enableLocalAuthBypass` parameter into
account and allows us to obtain a shell before with authentication
enabled, but before the user exists.
> Deprecated mongosh Method
> This method is deprecated in mongosh. For alternative methods, see
> Compatibility Changes with Legacy mongo Shell.
> https://www.mongodb.com/docs/mongodb-shell/reference/compatibility/#std-label-compatibility

See https://www.mongodb.com/docs/manual/reference/method/db.collection.insert/
Databases in MongoDB are created lazy, meaning there is no `create`
command. Thus if a record is inserted the DB is created.  When auth is
enabled, any attempt to insert data is denied because of missing auth.
However the `db.createUser()` function works because of the config
option `enableBypassLocalAuth`. For an overview see
https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set-with-keyfile-access-control/
@stevenpost stevenpost marked this pull request as ready for review March 27, 2024 10:11
@stevenpost
Copy link
Contributor Author

Hi all,

A summary of what's in this PR, most in the provider, one in the associated type:

  • Don't attempt a login for commands known to work without
  • Replace the use of the mongo command with mongosh
  • Fix replicaset with auth, this is spread over 3 commits, see the commit message for details.

@stevenpost stevenpost requested a review from h-haaks March 27, 2024 10:17
@stevenpost
Copy link
Contributor Author

stevenpost commented Mar 27, 2024

A note about backwards-compatibility.

Existing client code may need a few changes:

  • Since the versioning of the new package is different, users who had this pinned on '4.4.x' using class { 'mongodb::client': version => '<version>' } should change the version to either 'present' or an existing version of the mongodb-shell package. Pinning the version can also be done using the client_version parameter on the mongodb::globals class.
  • Any code that specified the user requiring the DB should break that connection. This isn't required anymore, and will cause a dependency cycle. (The user actually needs to be created first, details in commit 18ed042)
  • The file /root/.mongorc.js has been replaced with '/root/.mongoshrc.js, the old file isn't cleaned up as some users may depend on it for old scripts after migration. This triggers a restart of the server when first applied.

Apart from the tests already in this module, this code was tested in 2 scenario's:

  • Migrating an existing cluster with auth enabled to this new code
  • Setting up a new cluster with auth enabled

The nodes used for these tests were running RHEL 8 and I tested against MongoDB 4.4.27.

Please give it a whirl :)

@stevenpost
Copy link
Contributor Author

stevenpost commented Mar 27, 2024

Testing update: seems to work fine using RHEL 8 and MongoDB 6.0.12 as well.

A curly brace was not placed properly.
@stevenpost
Copy link
Contributor Author

Update: I found an issue with the authentication script, but fixing that is causing failures again during roll-out. Investigating.

All errors not related to authentication mean that auth is not required.
@stevenpost
Copy link
Contributor Author

Update: I found an issue with the authentication script, but fixing that is causing failures again during roll-out. Investigating.

Fixed, it was another exception thrown by the rs.status() command when testing for authentication.

@witjoh witjoh merged commit b9491d2 into voxpupuli:master Mar 29, 2024
24 checks passed
@stevenpost stevenpost deleted the mongosh branch March 29, 2024 09:00
@whiphubley
Copy link

@stevenpost as an end user I can confirm the module can now create the initial admin user and db when auth is enabled which ( for us ) was the major issue with this module...so thanks very much for your work on this.

@h-haaks h-haaks added the enhancement New feature or request label Apr 28, 2024
@h-haaks h-haaks mentioned this pull request Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

removed mongo cli in 6.0 is not compatible with outputs from new mongosh
4 participants