2021-12-19 01:06:50 +00:00
|
|
|
{ config, options, pkgs, lib, ... }:
|
2020-11-06 00:33:48 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.keycloak;
|
2021-12-19 01:06:50 +00:00
|
|
|
opt = options.services.keycloak;
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
inherit (lib) types mkOption concatStringsSep mapAttrsToList
|
|
|
|
escapeShellArg recursiveUpdate optionalAttrs boolToString mkOrder
|
|
|
|
sort filterAttrs concatMapStringsSep concatStrings mkIf
|
|
|
|
optionalString optionals mkDefault literalExpression hasSuffix
|
|
|
|
foldl' isAttrs filter attrNames elem literalDocBook
|
|
|
|
maintainers;
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
inherit (builtins) match typeOf;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.keycloak =
|
|
|
|
let
|
|
|
|
inherit (types) bool str nullOr attrsOf path enum anything
|
|
|
|
package port;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
enable = mkOption {
|
|
|
|
type = bool;
|
|
|
|
default = false;
|
|
|
|
example = true;
|
|
|
|
description = ''
|
|
|
|
Whether to enable the Keycloak identity and access management
|
|
|
|
server.
|
|
|
|
'';
|
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
bindAddress = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "\${jboss.bind.address:0.0.0.0}";
|
|
|
|
example = "127.0.0.1";
|
|
|
|
description = ''
|
|
|
|
On which address Keycloak should accept new connections.
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
A special syntax can be used to allow command line Java system
|
|
|
|
properties to override the value: ''${property.name:value}
|
|
|
|
'';
|
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
httpPort = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "\${jboss.http.port:80}";
|
|
|
|
example = "8080";
|
|
|
|
description = ''
|
|
|
|
On which port Keycloak should listen for new HTTP connections.
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
A special syntax can be used to allow command line Java system
|
|
|
|
properties to override the value: ''${property.name:value}
|
|
|
|
'';
|
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
httpsPort = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "\${jboss.https.port:443}";
|
|
|
|
example = "8443";
|
|
|
|
description = ''
|
|
|
|
On which port Keycloak should listen for new HTTPS connections.
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
A special syntax can be used to allow command line Java system
|
|
|
|
properties to override the value: ''${property.name:value}
|
|
|
|
'';
|
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
frontendUrl = mkOption {
|
|
|
|
type = str;
|
|
|
|
apply = x:
|
|
|
|
if x == "" || hasSuffix "/" x then
|
|
|
|
x
|
|
|
|
else
|
|
|
|
x + "/";
|
|
|
|
example = "keycloak.example.com/auth";
|
|
|
|
description = ''
|
|
|
|
The public URL used as base for all frontend requests. Should
|
|
|
|
normally include a trailing <literal>/auth</literal>.
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
See <link xlink:href="https://www.keycloak.org/docs/latest/server_installation/#_hostname">the
|
|
|
|
Hostname section of the Keycloak server installation
|
|
|
|
manual</link> for more information.
|
|
|
|
'';
|
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
forceBackendUrlToFrontendUrl = mkOption {
|
|
|
|
type = bool;
|
|
|
|
default = false;
|
|
|
|
example = true;
|
|
|
|
description = ''
|
|
|
|
Whether Keycloak should force all requests to go through the
|
|
|
|
frontend URL configured in <xref
|
|
|
|
linkend="opt-services.keycloak.frontendUrl" />. By default,
|
|
|
|
Keycloak allows backend requests to instead use its local
|
|
|
|
hostname or IP address and may also advertise it to clients
|
|
|
|
through its OpenID Connect Discovery endpoint.
|
|
|
|
|
|
|
|
See <link
|
|
|
|
xlink:href="https://www.keycloak.org/docs/latest/server_installation/#_hostname">the
|
|
|
|
Hostname section of the Keycloak server installation
|
|
|
|
manual</link> for more information.
|
|
|
|
'';
|
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
sslCertificate = mkOption {
|
|
|
|
type = nullOr path;
|
|
|
|
default = null;
|
|
|
|
example = "/run/keys/ssl_cert";
|
2021-05-28 09:39:13 +00:00
|
|
|
description = ''
|
2022-01-19 23:45:15 +00:00
|
|
|
The path to a PEM formatted certificate to use for TLS/SSL
|
|
|
|
connections.
|
|
|
|
|
|
|
|
This should be a string, not a Nix path, since Nix paths are
|
|
|
|
copied into the world-readable Nix store.
|
2021-05-28 09:39:13 +00:00
|
|
|
'';
|
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
sslCertificateKey = mkOption {
|
|
|
|
type = nullOr path;
|
|
|
|
default = null;
|
|
|
|
example = "/run/keys/ssl_key";
|
2021-05-28 09:39:13 +00:00
|
|
|
description = ''
|
2022-01-19 23:45:15 +00:00
|
|
|
The path to a PEM formatted private key to use for TLS/SSL
|
|
|
|
connections.
|
|
|
|
|
|
|
|
This should be a string, not a Nix path, since Nix paths are
|
|
|
|
copied into the world-readable Nix store.
|
2021-05-28 09:39:13 +00:00
|
|
|
'';
|
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
plugins = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.path;
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
Keycloak plugin jar, ear files or derivations with them
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
database = {
|
|
|
|
type = mkOption {
|
|
|
|
type = enum [ "mysql" "postgresql" ];
|
|
|
|
default = "postgresql";
|
|
|
|
example = "mysql";
|
|
|
|
description = ''
|
|
|
|
The type of database Keycloak should connect to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
host = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "localhost";
|
|
|
|
description = ''
|
|
|
|
Hostname of the database to connect to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
port =
|
|
|
|
let
|
|
|
|
dbPorts = {
|
|
|
|
postgresql = 5432;
|
|
|
|
mysql = 3306;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
mkOption {
|
|
|
|
type = port;
|
2021-05-28 09:39:13 +00:00
|
|
|
default = dbPorts.${cfg.database.type};
|
2022-01-19 23:45:15 +00:00
|
|
|
defaultText = literalDocBook "default port of selected database";
|
2021-05-28 09:39:13 +00:00
|
|
|
description = ''
|
|
|
|
Port of the database to connect to.
|
|
|
|
'';
|
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
useSSL = mkOption {
|
|
|
|
type = bool;
|
|
|
|
default = cfg.database.host != "localhost";
|
|
|
|
defaultText = literalExpression ''config.${opt.database.host} != "localhost"'';
|
|
|
|
description = ''
|
|
|
|
Whether the database connection should be secured by SSL /
|
|
|
|
TLS.
|
|
|
|
'';
|
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
caCert = mkOption {
|
|
|
|
type = nullOr path;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
The SSL / TLS CA certificate that verifies the identity of the
|
|
|
|
database server.
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
Required when PostgreSQL is used and SSL is turned on.
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
For MySQL, if left at <literal>null</literal>, the default
|
|
|
|
Java keystore is used, which should suffice if the server
|
|
|
|
certificate is issued by an official CA.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
createLocally = mkOption {
|
|
|
|
type = bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether a database should be automatically created on the
|
|
|
|
local host. Set this to false if you plan on provisioning a
|
|
|
|
local database yourself. This has no effect if
|
|
|
|
services.keycloak.database.host is customized.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
username = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "keycloak";
|
|
|
|
description = ''
|
|
|
|
Username to use when connecting to an external or manually
|
|
|
|
provisioned database; has no effect when a local database is
|
|
|
|
automatically provisioned.
|
|
|
|
|
|
|
|
To use this with a local database, set <xref
|
|
|
|
linkend="opt-services.keycloak.database.createLocally" /> to
|
|
|
|
<literal>false</literal> and create the database and user
|
|
|
|
manually. The database should be called
|
|
|
|
<literal>keycloak</literal>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = mkOption {
|
|
|
|
type = path;
|
|
|
|
example = "/run/keys/db_password";
|
|
|
|
description = ''
|
|
|
|
File containing the database password.
|
|
|
|
|
|
|
|
This should be a string, not a Nix path, since Nix paths are
|
|
|
|
copied into the world-readable Nix store.
|
|
|
|
'';
|
|
|
|
};
|
2021-05-28 09:39:13 +00:00
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = package;
|
|
|
|
default = pkgs.keycloak;
|
|
|
|
defaultText = literalExpression "pkgs.keycloak";
|
2021-05-28 09:39:13 +00:00
|
|
|
description = ''
|
2022-01-19 23:45:15 +00:00
|
|
|
Keycloak package to use.
|
2021-05-28 09:39:13 +00:00
|
|
|
'';
|
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
initialAdminPassword = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "changeme";
|
2021-05-28 09:39:13 +00:00
|
|
|
description = ''
|
2022-01-19 23:45:15 +00:00
|
|
|
Initial password set for the <literal>admin</literal>
|
|
|
|
user. The password is not stored safely and should be changed
|
|
|
|
immediately in the admin panel.
|
2021-05-28 09:39:13 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
themes = mkOption {
|
|
|
|
type = attrsOf package;
|
|
|
|
default = { };
|
2021-05-28 09:39:13 +00:00
|
|
|
description = ''
|
2022-01-19 23:45:15 +00:00
|
|
|
Additional theme packages for Keycloak. Each theme is linked into
|
|
|
|
subdirectory with a corresponding attribute name.
|
2021-05-28 09:39:13 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
Theme packages consist of several subdirectories which provide
|
|
|
|
different theme types: for example, <literal>account</literal>,
|
|
|
|
<literal>login</literal> etc. After adding a theme to this option you
|
|
|
|
can select it by its name in Keycloak administration console.
|
2021-05-28 09:39:13 +00:00
|
|
|
'';
|
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = attrsOf anything;
|
|
|
|
default = { };
|
|
|
|
example = literalExpression ''
|
|
|
|
{
|
|
|
|
"subsystem=keycloak-server" = {
|
|
|
|
"spi=hostname" = {
|
|
|
|
"provider=default" = null;
|
|
|
|
"provider=fixed" = {
|
|
|
|
enabled = true;
|
|
|
|
properties.hostname = "keycloak.example.com";
|
|
|
|
};
|
|
|
|
default-provider = "fixed";
|
2020-11-06 00:33:48 +00:00
|
|
|
};
|
|
|
|
};
|
2022-01-19 23:45:15 +00:00
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Additional Keycloak configuration options to set in
|
|
|
|
<literal>standalone.xml</literal>.
|
|
|
|
|
|
|
|
Options are expressed as a Nix attribute set which matches the
|
|
|
|
structure of the jboss-cli configuration. The configuration is
|
|
|
|
effectively overlayed on top of the default configuration
|
|
|
|
shipped with Keycloak. To remove existing nodes and undefine
|
|
|
|
attributes from the default configuration, set them to
|
|
|
|
<literal>null</literal>.
|
|
|
|
|
|
|
|
The example configuration does the equivalent of the following
|
|
|
|
script, which removes the hostname provider
|
|
|
|
<literal>default</literal>, adds the deprecated hostname
|
|
|
|
provider <literal>fixed</literal> and defines it the default:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
/subsystem=keycloak-server/spi=hostname/provider=default:remove()
|
|
|
|
/subsystem=keycloak-server/spi=hostname/provider=fixed:add(enabled = true, properties = { hostname = "keycloak.example.com" })
|
|
|
|
/subsystem=keycloak-server/spi=hostname:write-attribute(name=default-provider, value="fixed")
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
You can discover available options by using the <link
|
|
|
|
xlink:href="http://docs.wildfly.org/21/Admin_Guide.html#Command_Line_Interface">jboss-cli.sh</link>
|
|
|
|
program and by referring to the <link
|
|
|
|
xlink:href="https://www.keycloak.org/docs/latest/server_installation/index.html">Keycloak
|
|
|
|
Server Installation and Configuration Guide</link>.
|
|
|
|
'';
|
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
|
|
|
|
config =
|
|
|
|
let
|
|
|
|
# We only want to create a database if we're actually going to connect to it.
|
2021-05-28 09:39:13 +00:00
|
|
|
databaseActuallyCreateLocally = cfg.database.createLocally && cfg.database.host == "localhost";
|
|
|
|
createLocalPostgreSQL = databaseActuallyCreateLocally && cfg.database.type == "postgresql";
|
|
|
|
createLocalMySQL = databaseActuallyCreateLocally && cfg.database.type == "mysql";
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
mySqlCaKeystore = pkgs.runCommand "mysql-ca-keystore" { } ''
|
2021-05-28 09:39:13 +00:00
|
|
|
${pkgs.jre}/bin/keytool -importcert -trustcacerts -alias MySQLCACert -file ${cfg.database.caCert} -keystore $out -storepass notsosecretpassword -noprompt
|
2020-11-06 00:33:48 +00:00
|
|
|
'';
|
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
# Both theme and theme type directories need to be actual directories in one hierarchy to pass Keycloak checks.
|
|
|
|
themesBundle = pkgs.runCommand "keycloak-themes" { } ''
|
|
|
|
linkTheme() {
|
|
|
|
theme="$1"
|
|
|
|
name="$2"
|
|
|
|
|
|
|
|
mkdir "$out/$name"
|
|
|
|
for typeDir in "$theme"/*; do
|
|
|
|
if [ -d "$typeDir" ]; then
|
|
|
|
type="$(basename "$typeDir")"
|
|
|
|
mkdir "$out/$name/$type"
|
|
|
|
for file in "$typeDir"/*; do
|
|
|
|
ln -sn "$file" "$out/$name/$type/$(basename "$file")"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir -p "$out"
|
|
|
|
for theme in ${cfg.package}/themes/*; do
|
|
|
|
if [ -d "$theme" ]; then
|
|
|
|
linkTheme "$theme" "$(basename "$theme")"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
${concatStringsSep "\n" (mapAttrsToList (name: theme: "linkTheme ${theme} ${escapeShellArg name}") cfg.themes)}
|
|
|
|
'';
|
|
|
|
|
|
|
|
keycloakConfig' = foldl' recursiveUpdate
|
|
|
|
{
|
|
|
|
"interface=public".inet-address = cfg.bindAddress;
|
|
|
|
"socket-binding-group=standard-sockets"."socket-binding=http".port = cfg.httpPort;
|
|
|
|
"subsystem=keycloak-server" = {
|
|
|
|
"spi=hostname"."provider=default" = {
|
|
|
|
enabled = true;
|
|
|
|
properties = {
|
|
|
|
inherit (cfg) frontendUrl forceBackendUrlToFrontendUrl;
|
|
|
|
};
|
2020-11-06 00:33:48 +00:00
|
|
|
};
|
2022-01-19 23:45:15 +00:00
|
|
|
"theme=defaults".dir = toString themesBundle;
|
2020-11-06 00:33:48 +00:00
|
|
|
};
|
2022-01-19 23:45:15 +00:00
|
|
|
"subsystem=datasources"."data-source=KeycloakDS" = {
|
|
|
|
max-pool-size = "20";
|
|
|
|
user-name = if databaseActuallyCreateLocally then "keycloak" else cfg.database.username;
|
|
|
|
password = "@db-password@";
|
|
|
|
};
|
|
|
|
} [
|
|
|
|
(optionalAttrs (cfg.database.type == "postgresql") {
|
2020-11-06 00:33:48 +00:00
|
|
|
"subsystem=datasources" = {
|
|
|
|
"jdbc-driver=postgresql" = {
|
|
|
|
driver-module-name = "org.postgresql";
|
|
|
|
driver-name = "postgresql";
|
|
|
|
driver-xa-datasource-class-name = "org.postgresql.xa.PGXADataSource";
|
|
|
|
};
|
|
|
|
"data-source=KeycloakDS" = {
|
2022-01-19 23:45:15 +00:00
|
|
|
connection-url = "jdbc:postgresql://${cfg.database.host}:${toString cfg.database.port}/keycloak";
|
2020-11-06 00:33:48 +00:00
|
|
|
driver-name = "postgresql";
|
2022-01-19 23:45:15 +00:00
|
|
|
"connection-properties=ssl".value = boolToString cfg.database.useSSL;
|
|
|
|
} // (optionalAttrs (cfg.database.caCert != null) {
|
2021-05-28 09:39:13 +00:00
|
|
|
"connection-properties=sslrootcert".value = cfg.database.caCert;
|
2020-11-06 00:33:48 +00:00
|
|
|
"connection-properties=sslmode".value = "verify-ca";
|
|
|
|
});
|
|
|
|
};
|
|
|
|
})
|
2022-01-19 23:45:15 +00:00
|
|
|
(optionalAttrs (cfg.database.type == "mysql") {
|
2020-11-06 00:33:48 +00:00
|
|
|
"subsystem=datasources" = {
|
|
|
|
"jdbc-driver=mysql" = {
|
|
|
|
driver-module-name = "com.mysql";
|
|
|
|
driver-name = "mysql";
|
|
|
|
driver-class-name = "com.mysql.jdbc.Driver";
|
|
|
|
};
|
|
|
|
"data-source=KeycloakDS" = {
|
2022-01-19 23:45:15 +00:00
|
|
|
connection-url = "jdbc:mysql://${cfg.database.host}:${toString cfg.database.port}/keycloak";
|
2020-11-06 00:33:48 +00:00
|
|
|
driver-name = "mysql";
|
2022-01-19 23:45:15 +00:00
|
|
|
"connection-properties=useSSL".value = boolToString cfg.database.useSSL;
|
|
|
|
"connection-properties=requireSSL".value = boolToString cfg.database.useSSL;
|
|
|
|
"connection-properties=verifyServerCertificate".value = boolToString cfg.database.useSSL;
|
2020-11-06 00:33:48 +00:00
|
|
|
"connection-properties=characterEncoding".value = "UTF-8";
|
|
|
|
valid-connection-checker-class-name = "org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker";
|
|
|
|
validate-on-match = true;
|
|
|
|
exception-sorter-class-name = "org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter";
|
2022-01-19 23:45:15 +00:00
|
|
|
} // (optionalAttrs (cfg.database.caCert != null) {
|
2020-11-06 00:33:48 +00:00
|
|
|
"connection-properties=trustCertificateKeyStoreUrl".value = "file:${mySqlCaKeystore}";
|
|
|
|
"connection-properties=trustCertificateKeyStorePassword".value = "notsosecretpassword";
|
|
|
|
});
|
|
|
|
};
|
|
|
|
})
|
2022-01-19 23:45:15 +00:00
|
|
|
(optionalAttrs (cfg.sslCertificate != null && cfg.sslCertificateKey != null) {
|
2020-11-06 00:33:48 +00:00
|
|
|
"socket-binding-group=standard-sockets"."socket-binding=https".port = cfg.httpsPort;
|
2022-01-19 23:45:15 +00:00
|
|
|
"subsystem=elytron" = mkOrder 900 {
|
|
|
|
"key-store=httpsKS" = mkOrder 900 {
|
|
|
|
path = "/run/keycloak/ssl/certificate_private_key_bundle.p12";
|
|
|
|
credential-reference.clear-text = "notsosecretpassword";
|
|
|
|
type = "JKS";
|
|
|
|
};
|
|
|
|
"key-manager=httpsKM" = mkOrder 901 {
|
|
|
|
key-store = "httpsKS";
|
|
|
|
credential-reference.clear-text = "notsosecretpassword";
|
|
|
|
};
|
|
|
|
"server-ssl-context=httpsSSC" = mkOrder 902 {
|
|
|
|
key-manager = "httpsKM";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
"subsystem=undertow" = mkOrder 901 {
|
|
|
|
"server=default-server"."https-listener=https".ssl-context = "httpsSSC";
|
2020-11-06 00:33:48 +00:00
|
|
|
};
|
|
|
|
})
|
|
|
|
cfg.extraConfig
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
/* Produces a JBoss CLI script that creates paths and sets
|
|
|
|
attributes matching those described by `attrs`. When the
|
|
|
|
script is run, the existing settings are effectively overlayed
|
|
|
|
by those from `attrs`. Existing attributes can be unset by
|
|
|
|
defining them `null`.
|
|
|
|
|
|
|
|
JBoss paths and attributes / maps are distinguished by their
|
|
|
|
name, where paths follow a `key=value` scheme.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
mkJbossScript {
|
|
|
|
"subsystem=keycloak-server"."spi=hostname" = {
|
|
|
|
"provider=fixed" = null;
|
|
|
|
"provider=default" = {
|
|
|
|
enabled = true;
|
|
|
|
properties = {
|
|
|
|
inherit frontendUrl;
|
|
|
|
forceBackendUrlToFrontendUrl = false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
=> ''
|
|
|
|
if (outcome != success) of /:read-resource()
|
|
|
|
/:add()
|
|
|
|
end-if
|
|
|
|
if (outcome != success) of /subsystem=keycloak-server:read-resource()
|
|
|
|
/subsystem=keycloak-server:add()
|
|
|
|
end-if
|
|
|
|
if (outcome != success) of /subsystem=keycloak-server/spi=hostname:read-resource()
|
|
|
|
/subsystem=keycloak-server/spi=hostname:add()
|
|
|
|
end-if
|
|
|
|
if (outcome != success) of /subsystem=keycloak-server/spi=hostname/provider=default:read-resource()
|
|
|
|
/subsystem=keycloak-server/spi=hostname/provider=default:add(enabled = true, properties = { forceBackendUrlToFrontendUrl = false, frontendUrl = "https://keycloak.example.com/auth" })
|
|
|
|
end-if
|
|
|
|
if (result != true) of /subsystem=keycloak-server/spi=hostname/provider=default:read-attribute(name="enabled")
|
|
|
|
/subsystem=keycloak-server/spi=hostname/provider=default:write-attribute(name=enabled, value=true)
|
|
|
|
end-if
|
|
|
|
if (result != false) of /subsystem=keycloak-server/spi=hostname/provider=default:read-attribute(name="properties.forceBackendUrlToFrontendUrl")
|
|
|
|
/subsystem=keycloak-server/spi=hostname/provider=default:write-attribute(name=properties.forceBackendUrlToFrontendUrl, value=false)
|
|
|
|
end-if
|
|
|
|
if (result != "https://keycloak.example.com/auth") of /subsystem=keycloak-server/spi=hostname/provider=default:read-attribute(name="properties.frontendUrl")
|
|
|
|
/subsystem=keycloak-server/spi=hostname/provider=default:write-attribute(name=properties.frontendUrl, value="https://keycloak.example.com/auth")
|
|
|
|
end-if
|
|
|
|
if (outcome != success) of /subsystem=keycloak-server/spi=hostname/provider=fixed:read-resource()
|
|
|
|
/subsystem=keycloak-server/spi=hostname/provider=fixed:remove()
|
|
|
|
end-if
|
|
|
|
''
|
|
|
|
*/
|
|
|
|
mkJbossScript = attrs:
|
|
|
|
let
|
|
|
|
/* From a JBoss path and an attrset, produces a JBoss CLI
|
|
|
|
snippet that writes the corresponding attributes starting
|
|
|
|
at `path`. Recurses down into subattrsets as necessary,
|
|
|
|
producing the variable name from its full path in the
|
|
|
|
attrset.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
writeAttributes "/subsystem=keycloak-server/spi=hostname/provider=default" {
|
|
|
|
enabled = true;
|
|
|
|
properties = {
|
|
|
|
forceBackendUrlToFrontendUrl = false;
|
|
|
|
frontendUrl = "https://keycloak.example.com/auth";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
=> ''
|
|
|
|
if (result != true) of /subsystem=keycloak-server/spi=hostname/provider=default:read-attribute(name="enabled")
|
|
|
|
/subsystem=keycloak-server/spi=hostname/provider=default:write-attribute(name=enabled, value=true)
|
|
|
|
end-if
|
|
|
|
if (result != false) of /subsystem=keycloak-server/spi=hostname/provider=default:read-attribute(name="properties.forceBackendUrlToFrontendUrl")
|
|
|
|
/subsystem=keycloak-server/spi=hostname/provider=default:write-attribute(name=properties.forceBackendUrlToFrontendUrl, value=false)
|
|
|
|
end-if
|
|
|
|
if (result != "https://keycloak.example.com/auth") of /subsystem=keycloak-server/spi=hostname/provider=default:read-attribute(name="properties.frontendUrl")
|
|
|
|
/subsystem=keycloak-server/spi=hostname/provider=default:write-attribute(name=properties.frontendUrl, value="https://keycloak.example.com/auth")
|
|
|
|
end-if
|
|
|
|
''
|
|
|
|
*/
|
|
|
|
writeAttributes = path: set:
|
|
|
|
let
|
|
|
|
# JBoss expressions like `${var}` need to be prefixed
|
|
|
|
# with `expression` to evaluate.
|
|
|
|
prefixExpression = string:
|
|
|
|
let
|
2022-01-19 23:45:15 +00:00
|
|
|
matchResult = match ''"\$\{.*}"'' string;
|
2020-11-06 00:33:48 +00:00
|
|
|
in
|
2022-01-19 23:45:15 +00:00
|
|
|
if matchResult != null then
|
|
|
|
"expression " + string
|
|
|
|
else
|
|
|
|
string;
|
2020-11-06 00:33:48 +00:00
|
|
|
|
|
|
|
writeAttribute = attribute: value:
|
|
|
|
let
|
2022-01-19 23:45:15 +00:00
|
|
|
type = typeOf value;
|
2020-11-06 00:33:48 +00:00
|
|
|
in
|
2022-01-19 23:45:15 +00:00
|
|
|
if type == "set" then
|
|
|
|
let
|
|
|
|
names = attrNames value;
|
|
|
|
in
|
|
|
|
foldl' (text: name: text + (writeAttribute "${attribute}.${name}" value.${name})) "" names
|
|
|
|
else if value == null then ''
|
|
|
|
if (outcome == success) of ${path}:read-attribute(name="${attribute}")
|
|
|
|
${path}:undefine-attribute(name="${attribute}")
|
|
|
|
end-if
|
|
|
|
''
|
|
|
|
else if elem type [ "string" "path" "bool" ] then
|
|
|
|
let
|
|
|
|
value' = if type == "bool" then boolToString value else ''"${value}"'';
|
|
|
|
in
|
|
|
|
''
|
|
|
|
if (result != ${prefixExpression value'}) of ${path}:read-attribute(name="${attribute}")
|
|
|
|
${path}:write-attribute(name=${attribute}, value=${value'})
|
2020-11-06 00:33:48 +00:00
|
|
|
end-if
|
|
|
|
''
|
2022-01-19 23:45:15 +00:00
|
|
|
else throw "Unsupported type '${type}' for path '${path}'!";
|
2020-11-06 00:33:48 +00:00
|
|
|
in
|
2022-01-19 23:45:15 +00:00
|
|
|
concatStrings
|
|
|
|
(mapAttrsToList
|
|
|
|
(attribute: value: (writeAttribute attribute value))
|
|
|
|
set);
|
2020-11-06 00:33:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Produces an argument list for the JBoss `add()` function,
|
|
|
|
which adds a JBoss path and takes as its arguments the
|
|
|
|
required subpaths and attributes.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
makeArgList {
|
|
|
|
enabled = true;
|
|
|
|
properties = {
|
|
|
|
forceBackendUrlToFrontendUrl = false;
|
|
|
|
frontendUrl = "https://keycloak.example.com/auth";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
=> ''
|
|
|
|
enabled = true, properties = { forceBackendUrlToFrontendUrl = false, frontendUrl = "https://keycloak.example.com/auth" }
|
|
|
|
''
|
|
|
|
*/
|
|
|
|
makeArgList = set:
|
|
|
|
let
|
|
|
|
makeArg = attribute: value:
|
|
|
|
let
|
2022-01-19 23:45:15 +00:00
|
|
|
type = typeOf value;
|
2020-11-06 00:33:48 +00:00
|
|
|
in
|
2022-01-19 23:45:15 +00:00
|
|
|
if type == "set" then
|
|
|
|
"${attribute} = { " + (makeArgList value) + " }"
|
|
|
|
else if elem type [ "string" "path" "bool" ] then
|
|
|
|
"${attribute} = ${if type == "bool" then boolToString value else ''"${value}"''}"
|
|
|
|
else if value == null then
|
|
|
|
""
|
|
|
|
else
|
|
|
|
throw "Unsupported type '${type}' for attribute '${attribute}'!";
|
|
|
|
|
2020-11-06 00:33:48 +00:00
|
|
|
in
|
2022-01-19 23:45:15 +00:00
|
|
|
concatStringsSep ", " (mapAttrsToList makeArg set);
|
2020-11-06 00:33:48 +00:00
|
|
|
|
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
/* Recurses into the `nodeValue` attrset. Only subattrsets that
|
|
|
|
are JBoss paths, i.e. follows the `key=value` format, are recursed
|
2020-11-06 00:33:48 +00:00
|
|
|
into - the rest are considered JBoss attributes / maps.
|
|
|
|
*/
|
2022-01-19 23:45:15 +00:00
|
|
|
recurse = nodePath: nodeValue:
|
2020-11-06 00:33:48 +00:00
|
|
|
let
|
2022-01-19 23:45:15 +00:00
|
|
|
nodeContent =
|
|
|
|
if isAttrs nodeValue && nodeValue._type or "" == "order" then
|
|
|
|
nodeValue.content
|
|
|
|
else
|
|
|
|
nodeValue;
|
2020-11-06 00:33:48 +00:00
|
|
|
isPath = name:
|
|
|
|
let
|
2022-01-19 23:45:15 +00:00
|
|
|
value = nodeContent.${name};
|
2020-11-06 00:33:48 +00:00
|
|
|
in
|
2022-01-19 23:45:15 +00:00
|
|
|
if (match ".*([=]).*" name) == [ "=" ] then
|
|
|
|
if isAttrs value || value == null then
|
|
|
|
true
|
2020-11-06 00:33:48 +00:00
|
|
|
else
|
2022-01-19 23:45:15 +00:00
|
|
|
throw "Parsing path '${concatStringsSep "." (nodePath ++ [ name ])}' failed: JBoss attributes cannot contain '='!"
|
|
|
|
else
|
|
|
|
false;
|
|
|
|
jbossPath = "/" + concatStringsSep "/" nodePath;
|
|
|
|
children = if !isAttrs nodeContent then { } else nodeContent;
|
|
|
|
subPaths = filter isPath (attrNames children);
|
|
|
|
getPriority = name:
|
|
|
|
let
|
|
|
|
value = children.${name};
|
|
|
|
in
|
|
|
|
if value._type or "" == "order" then value.priority else 1000;
|
|
|
|
orderedSubPaths = sort (a: b: getPriority a < getPriority b) subPaths;
|
|
|
|
jbossAttrs = filterAttrs (name: _: !(isPath name)) children;
|
|
|
|
text =
|
|
|
|
if nodeContent != null then
|
|
|
|
''
|
2020-11-06 00:33:48 +00:00
|
|
|
if (outcome != success) of ${jbossPath}:read-resource()
|
|
|
|
${jbossPath}:add(${makeArgList jbossAttrs})
|
|
|
|
end-if
|
2022-01-19 23:45:15 +00:00
|
|
|
'' + writeAttributes jbossPath jbossAttrs
|
|
|
|
else
|
|
|
|
''
|
2020-11-06 00:33:48 +00:00
|
|
|
if (outcome == success) of ${jbossPath}:read-resource()
|
|
|
|
${jbossPath}:remove()
|
|
|
|
end-if
|
2022-01-19 23:45:15 +00:00
|
|
|
'';
|
|
|
|
in
|
|
|
|
text + concatMapStringsSep "\n" (name: recurse (nodePath ++ [ name ]) children.${name}) orderedSubPaths;
|
2020-11-06 00:33:48 +00:00
|
|
|
in
|
2022-01-19 23:45:15 +00:00
|
|
|
recurse [ ] attrs;
|
2020-11-06 00:33:48 +00:00
|
|
|
|
|
|
|
jbossCliScript = pkgs.writeText "jboss-cli-script" (mkJbossScript keycloakConfig');
|
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
keycloakConfig = pkgs.runCommand "keycloak-config"
|
|
|
|
{
|
|
|
|
nativeBuildInputs = [ cfg.package ];
|
|
|
|
}
|
|
|
|
''
|
|
|
|
export JBOSS_BASE_DIR="$(pwd -P)";
|
|
|
|
export JBOSS_MODULEPATH="${cfg.package}/modules";
|
|
|
|
export JBOSS_LOG_DIR="$JBOSS_BASE_DIR/log";
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
cp -r ${cfg.package}/standalone/configuration .
|
|
|
|
chmod -R u+rwX ./configuration
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
mkdir -p {deployments,ssl}
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
standalone.sh&
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
attempt=1
|
|
|
|
max_attempts=30
|
|
|
|
while ! jboss-cli.sh --connect ':read-attribute(name=server-state)'; do
|
|
|
|
if [[ "$attempt" == "$max_attempts" ]]; then
|
|
|
|
echo "ERROR: Could not connect to Keycloak after $attempt attempts! Failing.." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echo "Keycloak not fully started yet, retrying.. ($attempt/$max_attempts)"
|
|
|
|
sleep 1
|
|
|
|
(( attempt++ ))
|
|
|
|
done
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
jboss-cli.sh --connect --file=${jbossCliScript} --echo-command
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
cp configuration/standalone.xml $out
|
|
|
|
'';
|
2020-11-06 00:33:48 +00:00
|
|
|
in
|
2022-01-19 23:45:15 +00:00
|
|
|
mkIf cfg.enable
|
|
|
|
{
|
2020-11-06 00:33:48 +00:00
|
|
|
assertions = [
|
|
|
|
{
|
2021-05-28 09:39:13 +00:00
|
|
|
assertion = (cfg.database.useSSL && cfg.database.type == "postgresql") -> (cfg.database.caCert != null);
|
|
|
|
message = "A CA certificate must be specified (in 'services.keycloak.database.caCert') when PostgreSQL is used with SSL";
|
2020-11-06 00:33:48 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
environment.systemPackages = [ cfg.package ];
|
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
systemd.services.keycloakPostgreSQLInit = mkIf createLocalPostgreSQL {
|
2020-11-06 00:33:48 +00:00
|
|
|
after = [ "postgresql.service" ];
|
|
|
|
before = [ "keycloak.service" ];
|
|
|
|
bindsTo = [ "postgresql.service" ];
|
2021-05-28 09:39:13 +00:00
|
|
|
path = [ config.services.postgresql.package ];
|
2020-11-06 00:33:48 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
User = "postgres";
|
|
|
|
Group = "postgres";
|
2022-03-10 19:12:11 +00:00
|
|
|
LoadCredential = [ "db_password:${cfg.database.passwordFile}" ];
|
2020-11-06 00:33:48 +00:00
|
|
|
};
|
|
|
|
script = ''
|
2021-05-28 09:39:13 +00:00
|
|
|
set -o errexit -o pipefail -o nounset -o errtrace
|
|
|
|
shopt -s inherit_errexit
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2021-05-28 09:39:13 +00:00
|
|
|
create_role="$(mktemp)"
|
|
|
|
trap 'rm -f "$create_role"' ERR EXIT
|
2020-11-06 00:33:48 +00:00
|
|
|
|
2022-03-10 19:12:11 +00:00
|
|
|
db_password="$(<"$CREDENTIALS_DIRECTORY/db_password")"
|
|
|
|
echo "CREATE ROLE keycloak WITH LOGIN PASSWORD '$db_password' CREATEDB" > "$create_role"
|
2021-05-28 09:39:13 +00:00
|
|
|
psql -tAc "SELECT 1 FROM pg_roles WHERE rolname='keycloak'" | grep -q 1 || psql -tA --file="$create_role"
|
|
|
|
psql -tAc "SELECT 1 FROM pg_database WHERE datname = 'keycloak'" | grep -q 1 || psql -tAc 'CREATE DATABASE "keycloak" OWNER "keycloak"'
|
2020-11-06 00:33:48 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
systemd.services.keycloakMySQLInit = mkIf createLocalMySQL {
|
2020-11-06 00:33:48 +00:00
|
|
|
after = [ "mysql.service" ];
|
|
|
|
before = [ "keycloak.service" ];
|
|
|
|
bindsTo = [ "mysql.service" ];
|
2021-05-28 09:39:13 +00:00
|
|
|
path = [ config.services.mysql.package ];
|
2020-11-06 00:33:48 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
User = config.services.mysql.user;
|
|
|
|
Group = config.services.mysql.group;
|
2022-03-10 19:12:11 +00:00
|
|
|
LoadCredential = [ "db_password:${cfg.database.passwordFile}" ];
|
2020-11-06 00:33:48 +00:00
|
|
|
};
|
|
|
|
script = ''
|
2021-05-28 09:39:13 +00:00
|
|
|
set -o errexit -o pipefail -o nounset -o errtrace
|
|
|
|
shopt -s inherit_errexit
|
2022-03-10 19:12:11 +00:00
|
|
|
db_password="$(<"$CREDENTIALS_DIRECTORY/db_password")"
|
2021-05-04 21:07:42 +00:00
|
|
|
( echo "CREATE USER IF NOT EXISTS 'keycloak'@'localhost' IDENTIFIED BY '$db_password';"
|
2022-03-10 19:12:11 +00:00
|
|
|
echo "CREATE DATABASE IF NOT EXISTS keycloak CHARACTER SET utf8 COLLATE utf8_unicode_ci;"
|
2021-05-04 21:07:42 +00:00
|
|
|
echo "GRANT ALL PRIVILEGES ON keycloak.* TO 'keycloak'@'localhost';"
|
2021-05-28 09:39:13 +00:00
|
|
|
) | mysql -N
|
2020-11-06 00:33:48 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.keycloak =
|
|
|
|
let
|
|
|
|
databaseServices =
|
|
|
|
if createLocalPostgreSQL then [
|
2022-01-19 23:45:15 +00:00
|
|
|
"keycloakPostgreSQLInit.service"
|
|
|
|
"postgresql.service"
|
2020-11-06 00:33:48 +00:00
|
|
|
]
|
|
|
|
else if createLocalMySQL then [
|
2022-01-19 23:45:15 +00:00
|
|
|
"keycloakMySQLInit.service"
|
|
|
|
"mysql.service"
|
2020-11-06 00:33:48 +00:00
|
|
|
]
|
|
|
|
else [ ];
|
2022-01-19 23:45:15 +00:00
|
|
|
in
|
|
|
|
{
|
2020-11-06 00:33:48 +00:00
|
|
|
after = databaseServices;
|
|
|
|
bindsTo = databaseServices;
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2021-05-20 23:08:51 +00:00
|
|
|
path = with pkgs; [
|
2021-05-28 09:39:13 +00:00
|
|
|
cfg.package
|
|
|
|
openssl
|
2021-05-20 23:08:51 +00:00
|
|
|
replace-secret
|
|
|
|
];
|
2020-11-06 00:33:48 +00:00
|
|
|
environment = {
|
|
|
|
JBOSS_LOG_DIR = "/var/log/keycloak";
|
|
|
|
JBOSS_BASE_DIR = "/run/keycloak";
|
|
|
|
JBOSS_MODULEPATH = "${cfg.package}/modules";
|
|
|
|
};
|
|
|
|
serviceConfig = {
|
2022-01-19 23:45:15 +00:00
|
|
|
LoadCredential = [
|
|
|
|
"db_password:${cfg.database.passwordFile}"
|
|
|
|
] ++ optionals (cfg.sslCertificate != null && cfg.sslCertificateKey != null) [
|
|
|
|
"ssl_cert:${cfg.sslCertificate}"
|
|
|
|
"ssl_key:${cfg.sslCertificateKey}"
|
2020-11-06 00:33:48 +00:00
|
|
|
];
|
|
|
|
User = "keycloak";
|
|
|
|
Group = "keycloak";
|
|
|
|
DynamicUser = true;
|
|
|
|
RuntimeDirectory = map (p: "keycloak/" + p) [
|
|
|
|
"configuration"
|
|
|
|
"deployments"
|
|
|
|
"data"
|
|
|
|
"ssl"
|
|
|
|
"log"
|
|
|
|
"tmp"
|
|
|
|
];
|
|
|
|
RuntimeDirectoryMode = 0700;
|
|
|
|
LogsDirectory = "keycloak";
|
|
|
|
AmbientCapabilities = "CAP_NET_BIND_SERVICE";
|
|
|
|
};
|
2022-01-19 23:45:15 +00:00
|
|
|
script = ''
|
|
|
|
set -o errexit -o pipefail -o nounset -o errtrace
|
|
|
|
shopt -s inherit_errexit
|
|
|
|
|
|
|
|
umask u=rwx,g=,o=
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
install_plugin() {
|
|
|
|
if [ -d "$1" ]; then
|
|
|
|
find "$1" -type f \( -iname \*.ear -o -iname \*.jar \) -exec install -m 0500 -o keycloak -g keycloak "{}" "/run/keycloak/deployments/" \;
|
|
|
|
else
|
|
|
|
install -m 0500 -o keycloak -g keycloak "$1" "/run/keycloak/deployments/"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
install -m 0600 ${cfg.package}/standalone/configuration/*.properties /run/keycloak/configuration
|
|
|
|
install -T -m 0600 ${keycloakConfig} /run/keycloak/configuration/standalone.xml
|
|
|
|
|
|
|
|
replace-secret '@db-password@' "$CREDENTIALS_DIRECTORY/db_password" /run/keycloak/configuration/standalone.xml
|
|
|
|
|
|
|
|
export JAVA_OPTS=-Djboss.server.config.user.dir=/run/keycloak/configuration
|
|
|
|
add-user-keycloak.sh -u admin -p '${cfg.initialAdminPassword}'
|
2022-03-30 09:31:56 +00:00
|
|
|
''
|
|
|
|
+ lib.optionalString (cfg.plugins != []) (lib.concatStringsSep "\n" (map (pl: "install_plugin ${lib.escapeShellArg pl}") cfg.plugins)) + "\n"
|
|
|
|
+ optionalString (cfg.sslCertificate != null && cfg.sslCertificateKey != null) ''
|
2022-01-19 23:45:15 +00:00
|
|
|
pushd /run/keycloak/ssl/
|
|
|
|
cat "$CREDENTIALS_DIRECTORY/ssl_cert" <(echo) \
|
|
|
|
"$CREDENTIALS_DIRECTORY/ssl_key" <(echo) \
|
|
|
|
/etc/ssl/certs/ca-certificates.crt \
|
|
|
|
> allcerts.pem
|
|
|
|
openssl pkcs12 -export -in "$CREDENTIALS_DIRECTORY/ssl_cert" -inkey "$CREDENTIALS_DIRECTORY/ssl_key" -chain \
|
|
|
|
-name "${cfg.frontendUrl}" -out certificate_private_key_bundle.p12 \
|
|
|
|
-CAfile allcerts.pem -passout pass:notsosecretpassword
|
|
|
|
popd
|
|
|
|
'' + ''
|
|
|
|
${cfg.package}/bin/standalone.sh
|
|
|
|
'';
|
2020-11-06 00:33:48 +00:00
|
|
|
};
|
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
services.postgresql.enable = mkDefault createLocalPostgreSQL;
|
|
|
|
services.mysql.enable = mkDefault createLocalMySQL;
|
|
|
|
services.mysql.package = mkIf createLocalMySQL pkgs.mariadb;
|
2020-11-06 00:33:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
meta.doc = ./keycloak.xml;
|
2022-01-19 23:45:15 +00:00
|
|
|
meta.maintainers = [ maintainers.talyz ];
|
2020-11-06 00:33:48 +00:00
|
|
|
}
|