nixos/mongodb: replace option initialRootPassword with initialRootPasswordFile

This commit is contained in:
Niklas Korz 2025-01-21 14:47:51 +01:00
parent 3f8a2242b2
commit 2f8af3ea93
2 changed files with 18 additions and 7 deletions

View file

@ -220,6 +220,8 @@
- `racket_7_9` has been removed, as it is insecure. It is recommended to use Racket 8 instead. - `racket_7_9` has been removed, as it is insecure. It is recommended to use Racket 8 instead.
- `services.mongodb.initialRootPassword` has been replaced with the more secure option [`services.mongodb.initialRootPasswordFile`](#opt-services.mongodb.initialRootPasswordFile)
- `rofi` has been updated from 1.7.5 to 1.7.6 which introduces some breaking changes to binary plugins, and also contains a lot of new features and bug fixes. This is highlighted because the patch version bump does not indicate the volume of changes by itself. See the [upstream release notes](https://github.com/davatorium/rofi/releases/tag/1.7.6) for the full list of changes. - `rofi` has been updated from 1.7.5 to 1.7.6 which introduces some breaking changes to binary plugins, and also contains a lot of new features and bug fixes. This is highlighted because the patch version bump does not indicate the volume of changes by itself. See the [upstream release notes](https://github.com/davatorium/rofi/releases/tag/1.7.6) for the full list of changes.
- `ente-auth` now uses the name `enteauth` for its binary. The previous name was `ente_auth`. - `ente-auth` now uses the name `enteauth` for its binary. The previous name was `ente_auth`.

View file

@ -27,6 +27,13 @@ let
in in
{ {
imports = [
(lib.mkRemovedOptionModule [
"services"
"mongodb"
"initialRootPassword"
] "Use services.mongodb.initialRootPasswordFile to securely provide the initial root password.")
];
###### interface ###### interface
@ -64,10 +71,10 @@ in
description = "Enable client authentication. Creates a default superuser with username root!"; description = "Enable client authentication. Creates a default superuser with username root!";
}; };
initialRootPassword = lib.mkOption { initialRootPasswordFile = lib.mkOption {
type = lib.types.nullOr lib.types.str; type = lib.types.nullOr lib.types.path;
default = null; default = null;
description = "Password for the root user if auth is enabled."; description = "Path to the file containing the password for the root user if auth is enabled.";
}; };
dbpath = lib.mkOption { dbpath = lib.mkOption {
@ -116,8 +123,8 @@ in
config = lib.mkIf config.services.mongodb.enable { config = lib.mkIf config.services.mongodb.enable {
assertions = [ assertions = [
{ {
assertion = !cfg.enableAuth || cfg.initialRootPassword != null; assertion = !cfg.enableAuth || cfg.initialRootPasswordFile != null;
message = "`enableAuth` requires `initialRootPassword` to be set."; message = "`enableAuth` requires `initialRootPasswordFile` to be set.";
} }
]; ];
@ -168,12 +175,13 @@ in
# wait for mongodb # wait for mongodb
while ! ${mongoshExe} --eval "db.version()" > /dev/null 2>&1; do sleep 0.1; done while ! ${mongoshExe} --eval "db.version()" > /dev/null 2>&1; do sleep 0.1; done
initialRootPassword=$(<${cfg.initialRootPasswordFile})
${mongoshExe} <<EOF ${mongoshExe} <<EOF
use admin; use admin;
db.createUser( db.createUser(
{ {
user: "root", user: "root",
pwd: "${cfg.initialRootPassword}", pwd: "$initialRootPassword",
roles: [ roles: [
{ role: "userAdminAnyDatabase", db: "admin" }, { role: "userAdminAnyDatabase", db: "admin" },
{ role: "dbAdminAnyDatabase", db: "admin" }, { role: "dbAdminAnyDatabase", db: "admin" },
@ -189,7 +197,8 @@ in
postStart = '' postStart = ''
if test -e "${cfg.dbpath}/.first_startup"; then if test -e "${cfg.dbpath}/.first_startup"; then
${lib.optionalString (cfg.initialScript != null) '' ${lib.optionalString (cfg.initialScript != null) ''
${mongoshExe} ${lib.optionalString (cfg.enableAuth) "-u root -p ${cfg.initialRootPassword}"} admin "${cfg.initialScript}" initialRootPassword=$(<${cfg.initialRootPasswordFile})
${mongoshExe} ${lib.optionalString (cfg.enableAuth) "-u root -p $initialRootPassword"} admin "${cfg.initialScript}"
''} ''}
rm -f "${cfg.dbpath}/.first_startup" rm -f "${cfg.dbpath}/.first_startup"
fi fi