From 16b80abd57bb215d0e72f3983f997a007743b8fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Sun, 8 Dec 2019 16:22:56 +0100 Subject: Add openldap replication --- modules/private/databases/openldap/default.nix | 29 +--- .../private/databases/openldap/eldiron_schemas.nix | 21 +++ modules/private/databases/openldap_replication.nix | 164 +++++++++++++++++++++ 3 files changed, 191 insertions(+), 23 deletions(-) create mode 100644 modules/private/databases/openldap/eldiron_schemas.nix create mode 100644 modules/private/databases/openldap_replication.nix (limited to 'modules/private/databases') diff --git a/modules/private/databases/openldap/default.nix b/modules/private/databases/openldap/default.nix index 9f72b29..22f6f7b 100644 --- a/modules/private/databases/openldap/default.nix +++ b/modules/private/databases/openldap/default.nix @@ -2,22 +2,9 @@ let cfg = config.myServices.databases.openldap; ldapConfig = let - kerberosSchema = pkgs.fetchurl { - url = "https://raw.githubusercontent.com/krb5/krb5/master/src/plugins/kdb/ldap/libkdb_ldap/kerberos.schema"; - sha256 = "17fnkkf6s3lznsl7wp6914pqsc78d038rh38l638big8z608ksww"; - }; - puppetSchema = pkgs.fetchurl { - url = "https://raw.githubusercontent.com/puppetlabs/puppet/master/ext/ldap/puppet.schema"; - sha256 = "11bjf5zfvqlim7p9vddcafs0wiq3v8ys77x8h6fbp9c6bdfh0awh"; - }; + eldiron_schemas = pkgs.callPackage ./eldiron_schemas.nix {}; in '' - include ${pkgs.openldap}/etc/schema/core.schema - include ${pkgs.openldap}/etc/schema/cosine.schema - include ${pkgs.openldap}/etc/schema/inetorgperson.schema - include ${pkgs.openldap}/etc/schema/nis.schema - include ${puppetSchema} - include ${kerberosSchema} - include ${./immae.schema} + ${eldiron_schemas} pidfile ${cfg.pids.pid} argsfile ${cfg.pids.args} @@ -33,6 +20,10 @@ let directory ${cfg.dataDir} overlay memberof + moduleload syncprov + overlay syncprov + syncprov-checkpoint 100 10 + TLSCertificateFile ${config.security.acme.directory}/ldap/cert.pem TLSCertificateKeyFile ${config.security.acme.directory}/ldap/key.pem TLSCACertificateFile ${config.security.acme.directory}/ldap/fullchain.pem @@ -126,14 +117,6 @@ in users.users.openldap.extraGroups = [ "keys" ]; networking.firewall.allowedTCPPorts = [ 636 389 ]; - services.cron = { - systemCronJobs = [ - '' - 35 1,13 * * * root ${pkgs.openldap}/bin/slapcat -v -b "dc=immae,dc=eu" -f ${pkgs.writeText "slapd.conf" ldapConfig} -l ${cfg.dataDir}/backup.ldif | ${pkgs.gnugrep}/bin/grep -v "^# id=[0-9a-f]*$" - '' - ]; - }; - security.acme.certs."ldap" = config.myServices.databasesCerts // { user = "openldap"; group = "openldap"; diff --git a/modules/private/databases/openldap/eldiron_schemas.nix b/modules/private/databases/openldap/eldiron_schemas.nix new file mode 100644 index 0000000..7a29988 --- /dev/null +++ b/modules/private/databases/openldap/eldiron_schemas.nix @@ -0,0 +1,21 @@ +{ fetchurl, openldap }: +let + kerberosSchema = fetchurl { + url = "https://raw.githubusercontent.com/krb5/krb5/master/src/plugins/kdb/ldap/libkdb_ldap/kerberos.schema"; + sha256 = "17fnkkf6s3lznsl7wp6914pqsc78d038rh38l638big8z608ksww"; + }; + puppetSchema = fetchurl { + url = "https://raw.githubusercontent.com/puppetlabs/puppet/master/ext/ldap/puppet.schema"; + sha256 = "11bjf5zfvqlim7p9vddcafs0wiq3v8ys77x8h6fbp9c6bdfh0awh"; + }; + schemas = [ + "${openldap}/etc/schema/core.schema" + "${openldap}/etc/schema/cosine.schema" + "${openldap}/etc/schema/inetorgperson.schema" + "${openldap}/etc/schema/nis.schema" + puppetSchema + kerberosSchema + ./immae.schema + ]; +in + builtins.concatStringsSep "\n" (map (v: "include ${v}") schemas) diff --git a/modules/private/databases/openldap_replication.nix b/modules/private/databases/openldap_replication.nix new file mode 100644 index 0000000..c0c16e6 --- /dev/null +++ b/modules/private/databases/openldap_replication.nix @@ -0,0 +1,164 @@ +{ pkgs, config, myconfig, lib, ... }: +let + cfg = config.myServices.databasesReplication.openldap; + eldiron_schemas = pkgs.callPackage ./openldap/eldiron_schemas.nix {}; + ldapConfig = hcfg: name: pkgs.writeText "slapd.conf" '' + ${eldiron_schemas} + pidfile /run/slapd_${name}/slapd.pid + argsfile /run/slapd_${name}/slapd.args + + moduleload back_hdb + backend hdb + database hdb + + suffix "${hcfg.base}" + rootdn "cn=root,${hcfg.base}" + directory ${cfg.base}/${name}/openldap + + index objectClass eq + index uid pres,eq + index entryUUID eq + + include ${config.secrets.location}/openldap_replication/${name}/replication_config + ''; +in +{ + options.myServices.databasesReplication.openldap = { + enable = lib.mkEnableOption "Enable openldap replication"; + base = lib.mkOption { + type = lib.types.path; + description = '' + Base path to put the replications + ''; + }; + hosts = lib.mkOption { + default = {}; + description = '' + Hosts to backup + ''; + type = lib.types.attrsOf (lib.types.submodule { + options = { + package = lib.mkOption { + type = lib.types.package; + default = pkgs.openldap; + description = '' + Openldap package for this host + ''; + }; + url = lib.mkOption { + type = lib.types.str; + description = '' + Host to connect to + ''; + }; + base = lib.mkOption { + type = lib.types.str; + description = '' + Base DN to replicate + ''; + }; + dn = lib.mkOption { + type = lib.types.str; + description = '' + DN to use + ''; + }; + password = lib.mkOption { + type = lib.types.str; + description = '' + Password to use + ''; + }; + }; + }); + }; + }; + + config = lib.mkIf cfg.enable { + users.users.openldap = { + description = "Openldap database user"; + group = "openldap"; + uid = config.ids.uids.openldap; + extraGroups = [ "keys" ]; + }; + users.groups.openldap.gid = config.ids.gids.openldap; + + secrets.keys = lib.flatten (lib.mapAttrsToList (name: hcfg: [ + { + dest = "openldap_replication/${name}/replication_config"; + user = "openldap"; + group = "openldap"; + permissions = "0400"; + text = '' + syncrepl rid=000 + provider=${hcfg.url} + type=refreshAndPersist + searchbase="${hcfg.base}" + retry="5 10 300 +" + attrs="*,+" + schemachecking=off + bindmethod=simple + binddn="${hcfg.dn}" + credentials="${hcfg.password}" + ''; + } + { + dest = "openldap_replication/${name}/replication_password"; + user = "openldap"; + group = "openldap"; + permissions = "0400"; + text = hcfg.password; + } + ]) cfg.hosts); + + services.cron = { + enable = true; + systemCronJobs = lib.flatten (lib.mapAttrsToList (name: hcfg: + let + dataDir = "${cfg.base}/${name}/openldap"; + backupDir = "${cfg.base}/${name}/openldap_backup"; + backup_script = pkgs.writeScript "backup_openldap_${name}" '' + #!${pkgs.stdenv.shell} + + ${hcfg.package}/bin/slapcat -b "${hcfg.base}" -f ${ldapConfig hcfg name} -l ${backupDir}/$(${pkgs.coreutils}/bin/date -Iseconds).ldif + ''; + u = pkgs.callPackage ./utils.nix {}; + cleanup_script = pkgs.writeScript "cleanup_openldap_${name}" (u.exponentialDumps "ldif" backupDir); + in [ + "0 22,4,10,16 * * * root ${backup_script}" + "0 3 * * * root ${cleanup_script}" + ]) cfg.hosts); + }; + + system.activationScripts = lib.attrsets.mapAttrs' (name: hcfg: + lib.attrsets.nameValuePair "openldap_replication_${name}" { + deps = [ "users" "groups" ]; + text = '' + install -m 0700 -o openldap -g openldap -d ${cfg.base}/${name}/openldap + install -m 0700 -o openldap -g openldap -d ${cfg.base}/${name}/openldap_backup + ''; + }) cfg.hosts; + + systemd.services = lib.attrsets.mapAttrs' (name: hcfg: + let + dataDir = "${cfg.base}/${name}/openldap"; + in + lib.attrsets.nameValuePair "openldap_backup_${name}" { + description = "Openldap replication for ${name}"; + wantedBy = [ "multi-user.target" ]; + after = [ "network.target" ]; + unitConfig.RequiresMountsFor = dataDir; + + preStart = '' + mkdir -p /run/slapd_${name} + chown -R "openldap:openldap" /run/slapd_${name} + ''; + + serviceConfig = { + ExecStart = "${hcfg.package}/libexec/slapd -d 0 -u openldap -g openldap -f ${ldapConfig hcfg name}"; + }; + }) cfg.hosts; + }; +} + + -- cgit v1.2.3