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

mongodb: Add authentication support #36421

Merged
merged 6 commits into from May 25, 2019
Merged

Conversation

phile314-fh
Copy link
Contributor

@phile314-fh phile314-fh commented Mar 7, 2018

Motivation for this change

Add support for authentication to the MongoDB NixOS module.

MongoDB makes this a bit difficult, because it has no default admin/root user. So if we would just enable authentication this would make MongoDB completely inaccessible. I chose to instead create a default root db user. Users can then change the root password or create additional users using the normal MongoDB functions. There is a minimal security risk, as during initial setup MongoDB is started without authorization on the local loopback interface. Similarly, there will be a time window between the first startup and the time the initial root password can be changed by the user.

This is the best approach I have come up so far. Better ideas are welcome ;-)

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option build-use-sandbox in nix.conf on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Fits CONTRIBUTING.md.

@phile314-fh
Copy link
Contributor Author

ping

1 similar comment
@phile314-fh
Copy link
Contributor Author

ping

@phile314-fh
Copy link
Contributor Author

ping @bluescreen303 @offline @wkennington @cstrahan @rvl
(Not sure who to ping, I took the list from the mongodb package / nixos test)

We have been using this PR for the last 2 months in production without any problem.

@phile314-fh
Copy link
Contributor Author

ping

@Lassulus
Copy link
Member

sorry, this seems to have gotten very little attention. I would test it, if you rebase it on the current master.

@phile314-fh
Copy link
Contributor Author

Rebased on current master.

@Lassulus
Copy link
Member

Lassulus commented May 20, 2019

Wouldn't the option initalPassword would be enough? We could test against it being != null instaed of havin the enableAuth option.

@phile314-fh
Copy link
Contributor Author

I think you are right, initialRootPassword is enough from a technical point of view.

I am wondering a bit about discoverability though. It may not be clear that initialRootPassword also enables authentication. Or is there maybe another way to name/structure it?

@Lassulus
Copy link
Member

Maybe it's enough to mention the authentication in the description of initialRootPassword. I'm not entirely sure, that this is better though.

Co-Authored-By: Lassulus <github@lassul.us>
@phile314-fh
Copy link
Contributor Author

So I prefer the current way with both enableAuth and initialRootPassword, but if you feel strongly about this I can change it to only use initialRootPassword. What do you suggest?

@Lassulus
Copy link
Member

I'm ok with having both.

@Lassulus
Copy link
Member

do you want to squash or should I do it?

@Lassulus Lassulus merged commit 62d4c2b into NixOS:master May 25, 2019
@phile314-fh
Copy link
Contributor Author

Thank you for taking this up and merging it 👍

@aanderse
Copy link
Member

@phile314-fh Doesn't mongodb supports socket authentication? Having a root password option (even if just 'initial') is never desirable, especially if we could work around that problem by using socket authentication.

I know I'm a little bit late to the game here, seeing how this PR was merged 2 months ago, but it seems likely I'll have to start using mongodb in during the 19.09 release of NixOS. After reviewing the differences between the mongodb service in 19.03 and the service in master your PR seemed to make the code a fair bit more complex so I was hoping if it is possible we might simplify before 19.09. Thoughts on switching to socket authentication instead of prompting for passwords to store in a world readable location? Is it possible with mongodb?

@phile314-fh
Copy link
Contributor Author

phile314-fh commented Jul 22, 2019

I do agree that the current state is by far not an optimal solution, just the best I could come up with .....

What exactly do you mean by socket authentication? Do you have a link?

@aanderse
Copy link
Member

@phile314-fh I'm not a mongodb user so it seems some of my assumptions about socket authentication weren't correct. I had assumed unix socket authentication worked similar in mongodb as it does in mysql or postgresql: it does not.

That being said after some research I have discovered there is Localhost Exception which allows you to enable authentication but still have a user connect without a password via localhost only if there are no users in the database yet. The localhost login would only be authorized to create an admin user account and nothing else.

Would this be more or less desirable than what is currently in place? The one advantage I see is that it forces the db admin to securely set a password, whereas with initialRootPassword the db admin could set and leave an insecure password.

I'll be updating this module to remove the PermissionsStartOnly only reference (as this is deprecated) and while I'm in there I could change the module to use the localhost exception if you think that is better than using initialRootPassword. Let me know. Thanks!

@aanderse
Copy link
Member

ping @phile314-fh any opinion on that?

@phile314-fh
Copy link
Contributor Author

I just checked if mongodb would support hashed passwords, but it seems it requires the cleartext password for account creation. So I agree, removing the initialRootPassword is the best way forward. If somebody really wants to create a hard-coded root account, they can abuse the initialScript, but then they are clearly on their own.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants