git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/mysql: fix initialScript option

which was wrongly specified as types.lines Prevent it from getting copied to nix store as people might use it for credentials, and make the tests cover it.

commit14571f5ed02fea504d131b130327f845715a7714 authorFlorian Jacob <fjacob@lavabit.com> dateMon 1 Apr 2019, 21:08 (8 years ago) parent77978c1
2 files changed+13−2
nixos/modules/services/databases/mysql.nix+6−2 nixos/tests/mysql.nix+7
nixos/modules/services/databases/mysql.nix+6−2
@@ -133,7 +133,7 @@ in
133133 };
134134
135135 initialScript = mkOption {
136− type = types.nullOr types.lines;
136+ type = types.nullOr types.path;
137137 default = null;
138138 description = "A file containing SQL statements to be executed on the first startup. Can be used for granting certain permissions on the database";
139139 };
@@ -363,6 +363,8 @@ in
363363 ${optionalString (database.schema != null) ''
364364 echo 'use `${database.name}`;'
365365
366+ # TODO: this silently falls through if database.schema does not exist,
367+ # we should catch this somehow and exit, but can't do it here because we're in a subshell.
366368 if [ -f "${database.schema}" ]
367369 then
368370 cat ${database.schema}
@@ -399,7 +401,9 @@ in
399401 ${optionalString (cfg.initialScript != null)
400402 ''
401403 # Execute initial script
402− cat ${cfg.initialScript} | ${mysql}/bin/mysql -u root -N
404+ # using toString to avoid copying the file to nix store if given as path instead of string,
405+ # as it might contain credentials
406+ cat ${toString cfg.initialScript} | ${mysql}/bin/mysql -u root -N
403407 ''}
404408
405409 ${optionalString (cfg.rootPassword != null)
nixos/tests/mysql.nix+7
@@ -14,6 +14,11 @@ import ./make-test.nix ({ pkgs, ...} : {
1414 { name = "testdb"; schema = ./testdb.sql; }
1515 { name = "empty_testdb"; }
1616 ];
17+ # note that using pkgs.writeText here is generally not a good idea,
18+ # as it will store the password in world-readable /nix/store ;)
19+ services.mysql.initialScript = pkgs.writeText "mysql-init.sql" ''
20+ CREATE USER 'passworduser'@'localhost' IDENTIFIED BY 'password123';
21+ '';
1722 services.mysql.package = pkgs.mysql;
1823 };
1924
@@ -41,6 +46,8 @@ import ./make-test.nix ({ pkgs, ...} : {
4146 $mysql->waitForUnit("mysql");
4247 $mysql->succeed("echo 'use empty_testdb;' | mysql -u root");
4348 $mysql->succeed("echo 'use testdb; select * from tests;' | mysql -u root -N | grep 4");
49+ # ';' acts as no-op, just check whether login succeeds with the user created from the initialScript
50+ $mysql->succeed("echo ';' | mysql -u passworduser --password=password123");
4451
4552 $mariadb->waitForUnit("mysql");
4653 $mariadb->succeed("echo 'use testdb; create table tests (test_id INT, PRIMARY KEY (test_id));' | sudo -u testuser mysql -u testuser");