git.iko.soy/typeable/nixpkgs
on master help sign in
mongodb: Add authentication support

* nixos/mongodb: Add authentication support * nixos/mongodb: Add initial script option * nixos/mongodb: Make initial root password configurable * nixos/mongodb: Start only on loopback interface for setup procedure * nixos/mongodb: Test auth/initial script * nixos/mongodb: Code formatting Co-Authored-By: Lassulus <github@lassul.us>

commit62d4c2b34a10afea0365a70e6901a3fc888d9288 authorphile314-fh <phile314-fh@users.noreply.github.com> dateSat 25 May 2019, 11:09 (7 years ago) parente06dc0b
2 files changed+72−5
nixos/modules/services/databases/mongodb.nix+64−3 nixos/tests/mongodb.nix+8−2
nixos/modules/services/databases/mongodb.nix+64−3
@@ -8,12 +8,13 @@ let
88
99 mongodb = cfg.package;
1010
11− mongoCnf = pkgs.writeText "mongodb.conf"
11+ mongoCnf = cfg: pkgs.writeText "mongodb.conf"
1212 ''
1313 net.bindIp: ${cfg.bind_ip}
1414 ${optionalString cfg.quiet "systemLog.quiet: true"}
1515 systemLog.destination: syslog
1616 storage.dbPath: ${cfg.dbpath}
17+ ${optionalString cfg.enableAuth "security.authorization: enabled"}
1718 ${optionalString (cfg.replSetName != "") "replication.replSetName: ${cfg.replSetName}"}
1819 ${cfg.extraConfig}
1920 '';
@@ -59,6 +60,18 @@ in
5960 description = "quieter output";
6061 };
6162
63+ enableAuth = mkOption {
64+ type = types.bool;
65+ default = false;
66+ description = "Enable client authentication. Creates a default superuser with username root!";
67+ };
68+
69+ initialRootPassword = mkOption {
70+ type = types.nullOr types.string;
71+ default = null;
72+ description = "Password for the root user if auth is enabled.";
73+ };
74+
6275 dbpath = mkOption {
6376 default = "/var/db/mongodb";
6477 description = "Location where MongoDB stores its files";
@@ -84,6 +97,14 @@ in
8497 '';
8598 description = "MongoDB extra configuration in YAML format";
8699 };
100+
101+ initialScript = mkOption {
102+ type = types.nullOr types.path;
103+ default = null;
104+ description = ''
105+ A file containing MongoDB statements to execute on first startup.
106+ '';
107+ };
87108 };
88109
89110 };
@@ -92,6 +113,11 @@ in
92113 ###### implementation
93114
94115 config = mkIf config.services.mongodb.enable {
116+ assertions = [
117+ { assertion = !cfg.enableAuth || cfg.initialRootPassword != null;
118+ message = "`enableAuth` requires `initialRootPassword` to be set.";
119+ }
120+ ];
95121
96122 users.users.mongodb = mkIf (cfg.user == "mongodb")
97123 { name = "mongodb";
@@ -108,7 +134,7 @@ in
108134 after = [ "network.target" ];
109135
110136 serviceConfig = {
111− ExecStart = "${mongodb}/bin/mongod --config ${mongoCnf} --fork --pidfilepath ${cfg.pidFile}";
137+ ExecStart = "${mongodb}/bin/mongod --config ${mongoCnf cfg} --fork --pidfilepath ${cfg.pidFile}";
112138 User = cfg.user;
113139 PIDFile = cfg.pidFile;
114140 Type = "forking";
@@ -116,15 +142,50 @@ in
116142 PermissionsStartOnly = true;
117143 };
118144
119− preStart = ''
145+ preStart = let
146+ cfg_ = cfg // { enableAuth = false; bind_ip = "127.0.0.1"; };
147+ in ''
120148 rm ${cfg.dbpath}/mongod.lock || true
121149 if ! test -e ${cfg.dbpath}; then
122150 install -d -m0700 -o ${cfg.user} ${cfg.dbpath}
151+ # See postStart!
152+ touch ${cfg.dbpath}/.first_startup
123153 fi
124154 if ! test -e ${cfg.pidFile}; then
125155 install -D -o ${cfg.user} /dev/null ${cfg.pidFile}
156+ fi '' + lib.optionalString cfg.enableAuth ''
157+
158+ if ! test -e "${cfg.dbpath}/.auth_setup_complete"; then
159+ systemd-run --unit=mongodb-for-setup --uid=${cfg.user} ${mongodb}/bin/mongod --config ${mongoCnf cfg_}
160+ # wait for mongodb
161+ while ! ${mongodb}/bin/mongo --eval "db.version()" > /dev/null 2>&1; do sleep 0.1; done
162+
163+ ${mongodb}/bin/mongo <<EOF
164+ use admin
165+ db.createUser(
166+ {
167+ user: "root",
168+ pwd: "${cfg.initialRootPassword}",
169+ roles: [
170+ { role: "userAdminAnyDatabase", db: "admin" },
171+ { role: "dbAdminAnyDatabase", db: "admin" },
172+ { role: "readWriteAnyDatabase", db: "admin" }
173+ ]
174+ }
175+ )
176+ EOF
177+ touch "${cfg.dbpath}/.auth_setup_complete"
178+ systemctl stop mongodb-for-setup
126179 fi
127180 '';
181+ postStart = ''
182+ if test -e "${cfg.dbpath}/.first_startup"; then
183+ ${optionalString (cfg.initialScript != null) ''
184+ ${mongodb}/bin/mongo -u root -p ${cfg.initialRootPassword} admin "${cfg.initialScript}"
185+ ''}
186+ rm -f "${cfg.dbpath}/.first_startup"
187+ fi
188+ '';
128189 };
129190
130191 };
nixos/tests/mongodb.nix+8−2
@@ -8,7 +8,7 @@ import ./make-test.nix ({ pkgs, ...} : let
88in {
99 name = "mongodb";
1010 meta = with pkgs.stdenv.lib.maintainers; {
11− maintainers = [ bluescreen303 offline cstrahan rvl ];
11+ maintainers = [ bluescreen303 offline cstrahan rvl phile314 ];
1212 };
1313
1414 nodes = {
@@ -17,6 +17,12 @@ in {
1717 {
1818 services = {
1919 mongodb.enable = true;
20+ mongodb.enableAuth = true;
21+ mongodb.initialRootPassword = "root";
22+ mongodb.initialScript = pkgs.writeText "mongodb_initial.js" ''
23+ db = db.getSiblingDB("nixtest");
24+ db.createUser({user:"nixtest",pwd:"nixtest",roles:[{role:"readWrite",db:"nixtest"}]});
25+ '';
2026 mongodb.extraConfig = ''
2127 # Allow starting engine with only a small virtual disk
2228 storage.journal.enabled: false
@@ -29,6 +35,6 @@ in {
2935 testScript = ''
3036 startAll;
3137 $one->waitForUnit("mongodb.service");
32− $one->succeed("mongo nixtest ${testQuery}") =~ /hello/ or die;
38+ $one->succeed("mongo -u nixtest -p nixtest nixtest ${testQuery}") =~ /hello/ or die;
3339 '';
3440})