git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/keycloak: keycloak.database* -> keycloak.database.*

Move all database options to their own group / attribute. This makes the configuration clearer and brings it in line with most other modern modules.

commitdbf91bc2f12e80dd1933261e049b801d1a626a4e authortalyz <kim.lindberger@gmail.com> dateFri 14 May 2021, 12:15 (6 years ago) parent83e406e
3 files changed+122−117
nixos/modules/services/web-apps/keycloak.nix+105−103 nixos/modules/services/web-apps/keycloak.xml+11−11 nixos/tests/keycloak.nix+6−3
nixos/modules/services/web-apps/keycloak.nix+105−103
@@ -98,98 +98,100 @@ in
9898 '';
9999 };
100100
101− databaseType = lib.mkOption {
102− type = lib.types.enum [ "mysql" "postgresql" ];
103− default = "postgresql";
104− example = "mysql";
105− description = ''
106− The type of database Keycloak should connect to.
107− '';
108− };
101+ database = {
102+ type = lib.mkOption {
103+ type = lib.types.enum [ "mysql" "postgresql" ];
104+ default = "postgresql";
105+ example = "mysql";
106+ description = ''
107+ The type of database Keycloak should connect to.
108+ '';
109+ };
109110
110− databaseHost = lib.mkOption {
111− type = lib.types.str;
112− default = "localhost";
113− description = ''
114− Hostname of the database to connect to.
115− '';
116− };
111+ host = lib.mkOption {
112+ type = lib.types.str;
113+ default = "localhost";
114+ description = ''
115+ Hostname of the database to connect to.
116+ '';
117+ };
117118
118− databasePort =
119− let
120− dbPorts = {
121− postgresql = 5432;
122− mysql = 3306;
123− };
124− in
125− lib.mkOption {
126− type = lib.types.port;
127− default = dbPorts.${cfg.databaseType};
128− description = ''
129− Port of the database to connect to.
130− '';
131− };
119+ port =
120+ let
121+ dbPorts = {
122+ postgresql = 5432;
123+ mysql = 3306;
124+ };
125+ in
126+ lib.mkOption {
127+ type = lib.types.port;
128+ default = dbPorts.${cfg.database.type};
129+ description = ''
130+ Port of the database to connect to.
131+ '';
132+ };
132133
133− databaseUseSSL = lib.mkOption {
134− type = lib.types.bool;
135− default = cfg.databaseHost != "localhost";
136− description = ''
137− Whether the database connection should be secured by SSL /
138− TLS.
139− '';
140− };
134+ useSSL = lib.mkOption {
135+ type = lib.types.bool;
136+ default = cfg.database.host != "localhost";
137+ description = ''
138+ Whether the database connection should be secured by SSL /
139+ TLS.
140+ '';
141+ };
141142
142− databaseCaCert = lib.mkOption {
143− type = lib.types.nullOr lib.types.path;
144− default = null;
145− description = ''
146− The SSL / TLS CA certificate that verifies the identity of the
147− database server.
143+ caCert = lib.mkOption {
144+ type = lib.types.nullOr lib.types.path;
145+ default = null;
146+ description = ''
147+ The SSL / TLS CA certificate that verifies the identity of the
148+ database server.
148149
149− Required when PostgreSQL is used and SSL is turned on.
150+ Required when PostgreSQL is used and SSL is turned on.
150151
151− For MySQL, if left at <literal>null</literal>, the default
152− Java keystore is used, which should suffice if the server
153− certificate is issued by an official CA.
154− '';
155− };
152+ For MySQL, if left at <literal>null</literal>, the default
153+ Java keystore is used, which should suffice if the server
154+ certificate is issued by an official CA.
155+ '';
156+ };
156157
157− databaseCreateLocally = lib.mkOption {
158− type = lib.types.bool;
159− default = true;
160− description = ''
161− Whether a database should be automatically created on the
162− local host. Set this to false if you plan on provisioning a
163− local database yourself. This has no effect if
164− services.keycloak.databaseHost is customized.
165− '';
166− };
158+ createLocally = lib.mkOption {
159+ type = lib.types.bool;
160+ default = true;
161+ description = ''
162+ Whether a database should be automatically created on the
163+ local host. Set this to false if you plan on provisioning a
164+ local database yourself. This has no effect if
165+ services.keycloak.database.host is customized.
166+ '';
167+ };
167168
168− databaseUsername = lib.mkOption {
169− type = lib.types.str;
170− default = "keycloak";
171− description = ''
172− Username to use when connecting to an external or manually
173− provisioned database; has no effect when a local database is
174− automatically provisioned.
175−
176− To use this with a local database, set <xref
177− linkend="opt-services.keycloak.databaseCreateLocally" /> to
178− <literal>false</literal> and create the database and user
179− manually. The database should be called
180− <literal>keycloak</literal>.
181− '';
182− };
169+ username = lib.mkOption {
170+ type = lib.types.str;
171+ default = "keycloak";
172+ description = ''
173+ Username to use when connecting to an external or manually
174+ provisioned database; has no effect when a local database is
175+ automatically provisioned.
176+
177+ To use this with a local database, set <xref
178+ linkend="opt-services.keycloak.database.createLocally" /> to
179+ <literal>false</literal> and create the database and user
180+ manually. The database should be called
181+ <literal>keycloak</literal>.
182+ '';
183+ };
183184
184− databasePasswordFile = lib.mkOption {
185− type = lib.types.path;
186− example = "/run/keys/db_password";
187− description = ''
188− File containing the database password.
185+ passwordFile = lib.mkOption {
186+ type = lib.types.path;
187+ example = "/run/keys/db_password";
188+ description = ''
189+ File containing the database password.
189190
190− This should be a string, not a Nix path, since Nix paths are
191− copied into the world-readable Nix store.
192− '';
191+ This should be a string, not a Nix path, since Nix paths are
192+ copied into the world-readable Nix store.
193+ '';
194+ };
193195 };
194196
195197 package = lib.mkOption {
@@ -262,12 +264,12 @@ in
262264 config =
263265 let
264266 # We only want to create a database if we're actually going to connect to it.
265− databaseActuallyCreateLocally = cfg.databaseCreateLocally && cfg.databaseHost == "localhost";
266− createLocalPostgreSQL = databaseActuallyCreateLocally && cfg.databaseType == "postgresql";
267− createLocalMySQL = databaseActuallyCreateLocally && cfg.databaseType == "mysql";
267+ databaseActuallyCreateLocally = cfg.database.createLocally && cfg.database.host == "localhost";
268+ createLocalPostgreSQL = databaseActuallyCreateLocally && cfg.database.type == "postgresql";
269+ createLocalMySQL = databaseActuallyCreateLocally && cfg.database.type == "mysql";
268270
269271 mySqlCaKeystore = pkgs.runCommandNoCC "mysql-ca-keystore" {} ''
270− ${pkgs.jre}/bin/keytool -importcert -trustcacerts -alias MySQLCACert -file ${cfg.databaseCaCert} -keystore $out -storepass notsosecretpassword -noprompt
272+ ${pkgs.jre}/bin/keytool -importcert -trustcacerts -alias MySQLCACert -file ${cfg.database.caCert} -keystore $out -storepass notsosecretpassword -noprompt
271273 '';
272274
273275 keycloakConfig' = builtins.foldl' lib.recursiveUpdate {
@@ -283,11 +285,11 @@ in
283285 };
284286 "subsystem=datasources"."data-source=KeycloakDS" = {
285287 max-pool-size = "20";
286− user-name = if databaseActuallyCreateLocally then "keycloak" else cfg.databaseUsername;
288+ user-name = if databaseActuallyCreateLocally then "keycloak" else cfg.database.username;
287289 password = "@db-password@";
288290 };
289291 } [
290− (lib.optionalAttrs (cfg.databaseType == "postgresql") {
292+ (lib.optionalAttrs (cfg.database.type == "postgresql") {
291293 "subsystem=datasources" = {
292294 "jdbc-driver=postgresql" = {
293295 driver-module-name = "org.postgresql";
@@ -295,16 +297,16 @@ in
295297 driver-xa-datasource-class-name = "org.postgresql.xa.PGXADataSource";
296298 };
297299 "data-source=KeycloakDS" = {
298− connection-url = "jdbc:postgresql://${cfg.databaseHost}:${builtins.toString cfg.databasePort}/keycloak";
300+ connection-url = "jdbc:postgresql://${cfg.database.host}:${builtins.toString cfg.database.port}/keycloak";
299301 driver-name = "postgresql";
300− "connection-properties=ssl".value = lib.boolToString cfg.databaseUseSSL;
301− } // (lib.optionalAttrs (cfg.databaseCaCert != null) {
302− "connection-properties=sslrootcert".value = cfg.databaseCaCert;
302+ "connection-properties=ssl".value = lib.boolToString cfg.database.useSSL;
303+ } // (lib.optionalAttrs (cfg.database.caCert != null) {
304+ "connection-properties=sslrootcert".value = cfg.database.caCert;
303305 "connection-properties=sslmode".value = "verify-ca";
304306 });
305307 };
306308 })
307− (lib.optionalAttrs (cfg.databaseType == "mysql") {
309+ (lib.optionalAttrs (cfg.database.type == "mysql") {
308310 "subsystem=datasources" = {
309311 "jdbc-driver=mysql" = {
310312 driver-module-name = "com.mysql";
@@ -312,16 +314,16 @@ in
312314 driver-class-name = "com.mysql.jdbc.Driver";
313315 };
314316 "data-source=KeycloakDS" = {
315− connection-url = "jdbc:mysql://${cfg.databaseHost}:${builtins.toString cfg.databasePort}/keycloak";
317+ connection-url = "jdbc:mysql://${cfg.database.host}:${builtins.toString cfg.database.port}/keycloak";
316318 driver-name = "mysql";
317− "connection-properties=useSSL".value = lib.boolToString cfg.databaseUseSSL;
318− "connection-properties=requireSSL".value = lib.boolToString cfg.databaseUseSSL;
319− "connection-properties=verifyServerCertificate".value = lib.boolToString cfg.databaseUseSSL;
319+ "connection-properties=useSSL".value = lib.boolToString cfg.database.useSSL;
320+ "connection-properties=requireSSL".value = lib.boolToString cfg.database.useSSL;
321+ "connection-properties=verifyServerCertificate".value = lib.boolToString cfg.database.useSSL;
320322 "connection-properties=characterEncoding".value = "UTF-8";
321323 valid-connection-checker-class-name = "org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker";
322324 validate-on-match = true;
323325 exception-sorter-class-name = "org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter";
324− } // (lib.optionalAttrs (cfg.databaseCaCert != null) {
326+ } // (lib.optionalAttrs (cfg.database.caCert != null) {
325327 "connection-properties=trustCertificateKeyStoreUrl".value = "file:${mySqlCaKeystore}";
326328 "connection-properties=trustCertificateKeyStorePassword".value = "notsosecretpassword";
327329 });
@@ -573,8 +575,8 @@ in
573575
574576 assertions = [
575577 {
576− assertion = (cfg.databaseUseSSL && cfg.databaseType == "postgresql") -> (cfg.databaseCaCert != null);
577− message = "A CA certificate must be specified (in 'services.keycloak.databaseCaCert') when PostgreSQL is used with SSL";
578+ assertion = (cfg.database.useSSL && cfg.database.type == "postgresql") -> (cfg.database.caCert != null);
579+ message = "A CA certificate must be specified (in 'services.keycloak.database.caCert') when PostgreSQL is used with SSL";
578580 }
579581 ];
580582
@@ -598,7 +600,7 @@ in
598600 create_role="$(mktemp)"
599601 trap 'rm -f "$create_role"' ERR EXIT
600602
601− echo "CREATE ROLE keycloak WITH LOGIN PASSWORD '$(<'${cfg.databasePasswordFile}')' CREATEDB" > "$create_role"
603+ echo "CREATE ROLE keycloak WITH LOGIN PASSWORD '$(<'${cfg.database.passwordFile}')' CREATEDB" > "$create_role"
602604 psql -tAc "SELECT 1 FROM pg_roles WHERE rolname='keycloak'" | grep -q 1 || psql -tA --file="$create_role"
603605 psql -tAc "SELECT 1 FROM pg_database WHERE datname = 'keycloak'" | grep -q 1 || psql -tAc 'CREATE DATABASE "keycloak" OWNER "keycloak"'
604606 '';
@@ -619,7 +621,7 @@ in
619621 set -o errexit -o pipefail -o nounset -o errtrace
620622 shopt -s inherit_errexit
621623
622− db_password="$(<'${cfg.databasePasswordFile}')"
624+ db_password="$(<'${cfg.database.passwordFile}')"
623625 ( echo "CREATE USER IF NOT EXISTS 'keycloak'@'localhost' IDENTIFIED BY '$db_password';"
624626 echo "CREATE DATABASE keycloak CHARACTER SET utf8 COLLATE utf8_unicode_ci;"
625627 echo "GRANT ALL PRIVILEGES ON keycloak.* TO 'keycloak'@'localhost';"
@@ -659,7 +661,7 @@ in
659661
660662 umask u=rwx,g=,o=
661663
662− install -T -m 0400 -o keycloak -g keycloak '${cfg.databasePasswordFile}' /run/keycloak/secrets/db_password
664+ install -T -m 0400 -o keycloak -g keycloak '${cfg.database.passwordFile}' /run/keycloak/secrets/db_password
663665 '' + lib.optionalString (cfg.certificatePrivateKeyBundle != null) ''
664666 install -T -m 0400 -o keycloak -g keycloak '${cfg.certificatePrivateKeyBundle}' /run/keycloak/secrets/ssl_cert_pk_bundle
665667 '';
nixos/modules/services/web-apps/keycloak.xml+11−11
@@ -41,31 +41,31 @@
4141 <productname>PostgreSQL</productname> or
4242 <productname>MySQL</productname>. Which one is used can be
4343 configured in <xref
44− linkend="opt-services.keycloak.databaseType" />. The selected
44+ linkend="opt-services.keycloak.database.type" />. The selected
4545 database will automatically be enabled and a database and role
4646 created unless <xref
47− linkend="opt-services.keycloak.databaseHost" /> is changed from
47+ linkend="opt-services.keycloak.database.host" /> is changed from
4848 its default of <literal>localhost</literal> or <xref
49− linkend="opt-services.keycloak.databaseCreateLocally" /> is set
49+ linkend="opt-services.keycloak.database.createLocally" /> is set
5050 to <literal>false</literal>.
5151 </para>
5252
5353 <para>
5454 External database access can also be configured by setting
55− <xref linkend="opt-services.keycloak.databaseHost" />, <xref
56− linkend="opt-services.keycloak.databaseUsername" />, <xref
57− linkend="opt-services.keycloak.databaseUseSSL" /> and <xref
58− linkend="opt-services.keycloak.databaseCaCert" /> as
55+ <xref linkend="opt-services.keycloak.database.host" />, <xref
56+ linkend="opt-services.keycloak.database.username" />, <xref
57+ linkend="opt-services.keycloak.database.useSSL" /> and <xref
58+ linkend="opt-services.keycloak.database.caCert" /> as
5959 appropriate. Note that you need to manually create a database
6060 called <literal>keycloak</literal> and allow the configured
6161 database user full access to it.
6262 </para>
6363
6464 <para>
65− <xref linkend="opt-services.keycloak.databasePasswordFile" />
65+ <xref linkend="opt-services.keycloak.database.passwordFile" />
6666 must be set to the path to a file containing the password used
67− to log in to the database. If <xref linkend="opt-services.keycloak.databaseHost" />
68− and <xref linkend="opt-services.keycloak.databaseCreateLocally" />
67+ to log in to the database. If <xref linkend="opt-services.keycloak.database.host" />
68+ and <xref linkend="opt-services.keycloak.database.createLocally" />
6969 are kept at their defaults, the database role
7070 <literal>keycloak</literal> with that password is provisioned
7171 on the local database instance.
@@ -196,7 +196,7 @@ services.keycloak = {
196196 <link linkend="opt-services.keycloak.frontendUrl">frontendUrl</link> = "https://keycloak.example.com/auth";
197197 <link linkend="opt-services.keycloak.forceBackendUrlToFrontendUrl">forceBackendUrlToFrontendUrl</link> = true;
198198 <link linkend="opt-services.keycloak.certificatePrivateKeyBundle">certificatePrivateKeyBundle</link> = "/run/keys/ssl_cert";
199− <link linkend="opt-services.keycloak.databasePasswordFile">databasePasswordFile</link> = "/run/keys/db_password";
199+ <link linkend="opt-services.keycloak.database.passwordFile">database.passwordFile</link> = "/run/keys/db_password";
200200};
201201</programlisting>
202202 </para>
nixos/tests/keycloak.nix+6−3
@@ -19,9 +19,12 @@ let
1919 virtualisation.memorySize = 1024;
2020 services.keycloak = {
2121 enable = true;
22− inherit frontendUrl databaseType initialAdminPassword;
23− databaseUsername = "bogus";
24− databasePasswordFile = pkgs.writeText "dbPassword" "wzf6vOCbPp6cqTH";
22+ inherit frontendUrl initialAdminPassword;
23+ database = {
24+ type = databaseType;
25+ username = "bogus";
26+ passwordFile = pkgs.writeText "dbPassword" "wzf6vOCbPp6cqTH";
27+ };
2528 };
2629 environment.systemPackages = with pkgs; [
2730 xmlstarlet