mtls scripts

This commit is contained in:
John Lancaster
2026-03-27 21:15:31 -05:00
parent dd53eb42d4
commit 653e3a5884
+40 -31
View File
@@ -7,6 +7,10 @@ let
description = "The Common Name, DNS Name, or IP address that will be set as the Subject Common Name for the certificate. If no Subject Alternative Names (SANs) are configured (via the --san flag) then the subject will be set as the only SAN."; description = "The Common Name, DNS Name, or IP address that will be set as the Subject Common Name for the certificate. If no Subject Alternative Names (SANs) are configured (via the --san flag) then the subject will be set as the only SAN.";
type = lib.types.str; type = lib.types.str;
}; };
certDir = lib.mkOption {
description = "String path to the directory where the certs will be stored";
type = lib.types.str;
};
keyFilename = lib.mkOption { keyFilename = lib.mkOption {
description = "String filename for the private key"; description = "String filename for the private key";
type = lib.types.str; type = lib.types.str;
@@ -74,7 +78,7 @@ let
}; };
}; };
mtlsGenerate = { mkMtlsGenerateScript = {
pkgs, pkgs,
subject, subject,
provisioner, provisioner,
@@ -88,16 +92,23 @@ let
stepCmd = lib.getExe pkgs.step-cli; stepCmd = lib.getExe pkgs.step-cli;
sanArgs = lib.concatMapStringsSep " " (s: "--san \"${s}\"") san; sanArgs = lib.concatMapStringsSep " " (s: "--san \"${s}\"") san;
in in
(pkgs.writeShellScriptBin "mtls-generate" '' pkgs.writeShellScriptBin "mtls-generate" ''
set -euo pipefail set -euo pipefail
${stepCmd} ca certificate \ ${stepCmd} ca certificate \
${subject} ${tlsCert} ${tlsKey} \ ${subject} ${tlsCert} ${tlsKey} \
--not-before=-5m --not-after=${lifetime} \ --not-before=-5m --not-after=${lifetime} \
--provisioner ${provisioner} \ --provisioner ${provisioner} \
${sanArgs} \ ${sanArgs} \
"$@" "$@"
cat ${tlsCert} ${tlsKey} > ${mtlsBundle} cat ${tlsCert} ${tlsKey} > ${mtlsBundle}
''); '';
mkMtlsCheckScript = mtlsBundle: pkgs.writeShellScriptBin "mtls-check" ''
${lib.getExe pkgs.openssl} x509 \
-noout -subject -issuer \
-ext subjectAltName,extendedKeyUsage \
-enddate -in ${mtlsBundle}
''
mkMtlsRenewScript = { mkMtlsRenewScript = {
pkgs, pkgs,
@@ -238,26 +249,29 @@ in
flake.modules.nixos.mtls = { config, lib, pkgs, ... }: flake.modules.nixos.mtls = { config, lib, pkgs, ... }:
let let
cfg = config.mtls; cfg = config.mtls;
certDir = "/etc/step/certs"; tlsKey = "${cfg.certDir}/${cfg.keyFilename}";
tlsKey = "${certDir}/${cfg.keyFilename}"; tlsCert = "${cfg.certDir}/${cfg.certFilename}";
tlsCert = "${certDir}/${cfg.certFilename}"; mtlsBundle = "${cfg.certDir}/${cfg.bundleFilename}";
mtlsBundle = "${certDir}/${cfg.bundleFilename}";
sanArgs = lib.concatMapStringsSep " " (san: "--san \"${san}\"") cfg.san; sanArgs = lib.concatMapStringsSep " " (san: "--san \"${san}\"") cfg.san;
in in
{ {
options.mtls = opts; options.mtls = opts // {
certDir = lib.mkOption {
description = "String path to where the mtls certs will be stored.";
type = lib.types.str;
default = "/etc/step/certs";
};
};
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
environment.systemPackages = with pkgs; lib.optionals cfg.enable [ environment.systemPackages = with pkgs; lib.optionals cfg.enable [
(mtlsGenerate { step-cli
(mkMtlsGenerateScript {
inherit (cfg) subject provisioner san lifetime; inherit (cfg) subject provisioner san lifetime;
inherit pkgs tlsCert tlsKey mtlsBundle; inherit pkgs tlsCert tlsKey mtlsBundle;
}) })
(writeShellScriptBin "mtls-check" '' (mkMtlsCheckScript { inherit (cfg) mtlsBundle; })
${lib.getExe pkgs.openssl} x509 \ (mkMtlsRenewScript { inherit pkgs tlsCert tlsKey mtlsBundle; })
-noout -subject -issuer \
-ext subjectAltName,extendedKeyUsage \
-enddate -in ${mtlsBundle}
'')
]; ];
systemd.services.mtls-renew = lib.mkIf cfg.renew.enable (mkNixosMtlsRenewService { systemd.services.mtls-renew = lib.mkIf cfg.renew.enable (mkNixosMtlsRenewService {
@@ -284,23 +298,18 @@ in
certDir = lib.mkOption { certDir = lib.mkOption {
description = "String path to where the mtls certs will be stored."; description = "String path to where the mtls certs will be stored.";
type = lib.types.str; type = lib.types.str;
default ="${config.home.homeDirectory}/.step/certs"; default = "${config.home.homeDirectory}/.step/certs";
}; };
}; };
config = { config = {
home.packages = with pkgs; lib.optionals cfg.enable [ home.packages = with pkgs; lib.optionals cfg.enable [
step-cli step-cli
(mtlsGenerate { (mkMtlsGenerateScript {
inherit (cfg) subject provisioner san lifetime; inherit (cfg) subject provisioner san lifetime;
inherit pkgs tlsCert tlsKey mtlsBundle; inherit pkgs tlsCert tlsKey mtlsBundle;
}) })
(writeShellScriptBin "mtls-check" '' (mkMtlsCheckScript { inherit (cfg) mtlsBundle; })
${lib.getExe pkgs.openssl} x509 \
-noout -subject -issuer \
-ext subjectAltName,extendedKeyUsage \
-enddate -in ${mtlsBundle}
'')
(mkMtlsRenewScript { inherit pkgs tlsCert tlsKey mtlsBundle; }) (mkMtlsRenewScript { inherit pkgs tlsCert tlsKey mtlsBundle; })
]; ];