Merge pull request #185803 from Ma27/sssd-secrets

nixos/sssd: Add secrets handling (patch originally from @yayayayaka)
This commit is contained in:
Maximilian Bosch 2022-08-18 20:51:41 +02:00 committed by GitHub
commit 6a1263503c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 9 deletions

View file

@ -3,6 +3,10 @@ with lib;
let
cfg = config.services.sssd;
nscd = config.services.nscd;
dataDir = "/var/lib/sssd";
settingsFile = "${dataDir}/sssd.conf";
settingsFileUnsubstituted = pkgs.writeText "${dataDir}/sssd-unsubstituted.conf" cfg.config;
in {
options = {
services.sssd = {
@ -47,6 +51,30 @@ in {
Kerberos will be configured to cache credentials in SSS.
'';
};
environmentFile = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
Environment file as defined in <citerefentry>
<refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum>
</citerefentry>.
Secrets may be passed to the service without adding them to the world-readable
Nix store, by specifying placeholder variables as the option value in Nix and
setting these variables accordingly in the environment file.
<programlisting>
# snippet of sssd-related config
[domain/LDAP]
ldap_default_authtok = $SSSD_LDAP_DEFAULT_AUTHTOK
</programlisting>
<programlisting>
# contents of the environment file
SSSD_LDAP_DEFAULT_AUTHTOK=verysecretpassword
</programlisting>
'';
};
};
};
config = mkMerge [
@ -60,22 +88,29 @@ in {
wants = [ "nss-user-lookup.target" ];
restartTriggers = [
config.environment.etc."nscd.conf".source
config.environment.etc."sssd/sssd.conf".source
settingsFileUnsubstituted
];
script = ''
export LDB_MODULES_PATH+="''${LDB_MODULES_PATH+:}${pkgs.ldb}/modules/ldb:${pkgs.sssd}/modules/ldb"
mkdir -p /var/lib/sss/{pubconf,db,mc,pipes,gpo_cache,secrets} /var/lib/sss/pipes/private /var/lib/sss/pubconf/krb5.include.d
${pkgs.sssd}/bin/sssd -D
${pkgs.sssd}/bin/sssd -D -c ${settingsFile}
'';
serviceConfig = {
Type = "forking";
PIDFile = "/run/sssd.pid";
StateDirectory = baseNameOf dataDir;
# We cannot use LoadCredential here because it's not available in ExecStartPre
EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;
};
};
environment.etc."sssd/sssd.conf" = {
text = cfg.config;
mode = "0400";
preStart = ''
[ -f ${settingsFile} ] && rm -f ${settingsFile}
old_umask=$(umask)
umask 0177
${pkgs.envsubst}/bin/envsubst \
-o ${settingsFile} \
-i ${settingsFileUnsubstituted}
umask $old_umask
'';
};
system.nssModules = [ pkgs.sssd ];

View file

@ -28,7 +28,7 @@ in import ./make-test-python.nix ({pkgs, ...}: {
attrs = {
objectClass = [ "olcDatabaseConfig" "olcMdbConfig" ];
olcDatabase = "{1}mdb";
olcDbDirectory = "/var/db/openldap";
olcDbDirectory = "/var/lib/openldap/db";
olcSuffix = dbSuffix;
olcRootDN = "cn=${ldapRootUser},${dbSuffix}";
olcRootPW = ldapRootPassword;
@ -67,6 +67,8 @@ in import ./make-test-python.nix ({pkgs, ...}: {
services.sssd = {
enable = true;
# just for testing purposes, don't put this into the Nix store in production!
environmentFile = "${pkgs.writeText "ldap-root" "LDAP_BIND_PW=${ldapRootPassword}"}";
config = ''
[sssd]
config_file_version = 2
@ -80,7 +82,7 @@ in import ./make-test-python.nix ({pkgs, ...}: {
ldap_search_base = ${dbSuffix}
ldap_default_bind_dn = cn=${ldapRootUser},${dbSuffix}
ldap_default_authtok_type = password
ldap_default_authtok = ${ldapRootPassword}
ldap_default_authtok = $LDAP_BIND_PW
'';
};
};