Compare commits
5 Commits
863ff2d04d
...
fac9c7f5ce
| Author | SHA1 | Date | |
|---|---|---|---|
| fac9c7f5ce | |||
| a7b65e4eee | |||
| 1fe4d59ce6 | |||
| 86bb800886 | |||
| c4cf7c8096 |
+54
-51
@@ -1,7 +1,7 @@
|
||||
{ self, inputs, lib, ... }:
|
||||
let
|
||||
# Options that will be in common between
|
||||
opts = {
|
||||
# Options that will be in common between the nixos module and the home-manager module.
|
||||
mkOpts = config: let cfg = config.mtls; in {
|
||||
enable = lib.mkEnableOption "Enable mTLS";
|
||||
subject = lib.mkOption {
|
||||
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.";
|
||||
@@ -11,20 +11,25 @@ let
|
||||
description = "String path to the directory where the certs will be stored";
|
||||
type = lib.types.str;
|
||||
};
|
||||
keyFilename = lib.mkOption {
|
||||
description = "String filename for the private key";
|
||||
caFile = lib.mkOption {
|
||||
description = "String path for the root CA file";
|
||||
type = lib.types.str;
|
||||
default = "key.pem";
|
||||
default = "${cfg.certDir}/root_ca.crt";
|
||||
};
|
||||
certFilename = lib.mkOption {
|
||||
description = "String filename for the public certificate";
|
||||
keyFile = lib.mkOption {
|
||||
description = "String path for the private key";
|
||||
type = lib.types.str;
|
||||
default = "cert.pem";
|
||||
default = "${cfg.certDir}/key.pem";
|
||||
};
|
||||
bundleFilename = lib.mkOption {
|
||||
description = "String filename for the mTLS key bundle";
|
||||
certFile = lib.mkOption {
|
||||
description = "String path for the public cert";
|
||||
type = lib.types.str;
|
||||
default = "mtls.pem";
|
||||
default = "${cfg.certDir}/cert.pem";
|
||||
};
|
||||
bundleFile = lib.mkOption {
|
||||
description = "String path for the mTLS key bundle";
|
||||
type = lib.types.str;
|
||||
default = "${cfg.certDir}/mtls.pem";
|
||||
};
|
||||
san = lib.mkOption {
|
||||
description = "List of SAN to give the mTLS cert";
|
||||
@@ -37,7 +42,7 @@ let
|
||||
};
|
||||
lifetime = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "6h";
|
||||
default = "24h";
|
||||
};
|
||||
renew = {
|
||||
enable = lib.mkOption {
|
||||
@@ -83,9 +88,9 @@ let
|
||||
subject,
|
||||
provisioner,
|
||||
san,
|
||||
tlsCert,
|
||||
tlsKey,
|
||||
mtlsBundle,
|
||||
certFile,
|
||||
keyFile,
|
||||
bundleFile,
|
||||
lifetime,
|
||||
}:
|
||||
let
|
||||
@@ -95,26 +100,26 @@ let
|
||||
pkgs.writeShellScriptBin "mtls-generate" ''
|
||||
set -euo pipefail
|
||||
${stepCmd} ca certificate \
|
||||
${subject} ${tlsCert} ${tlsKey} \
|
||||
${subject} ${certFile} ${keyFile} \
|
||||
--not-before=-5m --not-after=${lifetime} \
|
||||
--provisioner ${provisioner} \
|
||||
${sanArgs} \
|
||||
"$@"
|
||||
cat ${tlsCert} ${tlsKey} > ${mtlsBundle}
|
||||
cat ${certFile} ${keyFile} > ${bundleFile}
|
||||
'';
|
||||
|
||||
mkMtlsCheckScript = { pkgs, mtlsBundle }: pkgs.writeShellScriptBin "mtls-check" ''
|
||||
mkMtlsCheckScript = { pkgs, bundleFile }: pkgs.writeShellScriptBin "mtls-check" ''
|
||||
${lib.getExe pkgs.openssl} x509 \
|
||||
-noout -subject -issuer \
|
||||
-ext subjectAltName,extendedKeyUsage \
|
||||
-enddate -in ${mtlsBundle}
|
||||
-enddate -in ${bundleFile}
|
||||
'';
|
||||
|
||||
mkMtlsRenewScript = {
|
||||
pkgs,
|
||||
tlsCert,
|
||||
tlsKey,
|
||||
mtlsBundle,
|
||||
certFile,
|
||||
keyFile,
|
||||
bundleFile,
|
||||
reloadUnits ? [ ],
|
||||
postCommands ? [ ],
|
||||
systemctlArgs ? [ ],
|
||||
@@ -134,17 +139,17 @@ let
|
||||
pkgs.writeShellScriptBin "mtls-renew" ''
|
||||
set -euo pipefail
|
||||
|
||||
if ${lib.getExe pkgs.step-cli} certificate needs-renewal "${tlsCert}"; then
|
||||
if ${lib.getExe pkgs.step-cli} certificate needs-renewal "${certFile}"; then
|
||||
${echoCmd} "Renewing mTLS certificate"
|
||||
else
|
||||
${echoCmd} "Skipping renew"
|
||||
exit "$?"
|
||||
fi
|
||||
|
||||
${lib.getExe pkgs.step-cli} ca renew --force "${tlsCert}" "${tlsKey}"
|
||||
${lib.getExe pkgs.step-cli} ca renew --force "${certFile}" "${keyFile}"
|
||||
|
||||
umask 077
|
||||
${lib.getExe' pkgs.coreutils "cat"} "${tlsCert}" "${tlsKey}" > "${mtlsBundle}"
|
||||
${lib.getExe' pkgs.coreutils "cat"} "${certFile}" "${keyFile}" > "${bundleFile}"
|
||||
|
||||
${echoCmd} "Reloading units:"
|
||||
${renewReloadScript}
|
||||
@@ -155,9 +160,9 @@ let
|
||||
|
||||
mkNixosMtlsRenewService = {
|
||||
pkgs,
|
||||
tlsCert,
|
||||
tlsKey,
|
||||
mtlsBundle,
|
||||
certFile,
|
||||
keyFile,
|
||||
bundleFile,
|
||||
reloadUnits ? [ ],
|
||||
postCommands ? [ ],
|
||||
user ? "root",
|
||||
@@ -166,7 +171,7 @@ let
|
||||
let
|
||||
serviceGroup = if group == null then user else group;
|
||||
renewScript = mkMtlsRenewScript {
|
||||
inherit pkgs tlsCert tlsKey mtlsBundle reloadUnits postCommands;
|
||||
inherit pkgs certFile keyFile bundleFile reloadUnits postCommands;
|
||||
};
|
||||
in
|
||||
{
|
||||
@@ -200,15 +205,15 @@ let
|
||||
|
||||
mkHomeManagerMtlsRenewService = {
|
||||
pkgs,
|
||||
tlsCert,
|
||||
tlsKey,
|
||||
mtlsBundle,
|
||||
certFile,
|
||||
keyFile,
|
||||
bundleFile,
|
||||
reloadUnits ? [ ],
|
||||
postCommands ? [ ],
|
||||
}:
|
||||
let
|
||||
renewScript = mkMtlsRenewScript {
|
||||
inherit pkgs tlsCert tlsKey mtlsBundle reloadUnits postCommands;
|
||||
inherit pkgs certFile keyFile bundleFile reloadUnits postCommands;
|
||||
systemctlArgs = [ "--user" ];
|
||||
};
|
||||
in
|
||||
@@ -249,17 +254,14 @@ in
|
||||
flake.modules.nixos.mtls = { config, lib, pkgs, ... }:
|
||||
let
|
||||
cfg = config.mtls;
|
||||
tlsKey = "${cfg.certDir}/${cfg.keyFilename}";
|
||||
tlsCert = "${cfg.certDir}/${cfg.certFilename}";
|
||||
mtlsBundle = "${cfg.certDir}/${cfg.bundleFilename}";
|
||||
sanArgs = lib.concatMapStringsSep " " (san: "--san \"${san}\"") cfg.san;
|
||||
in
|
||||
{
|
||||
options.mtls = opts // {
|
||||
options.mtls = (mkOpts config) // {
|
||||
certDir = lib.mkOption {
|
||||
description = "String path to where the mtls certs will be stored.";
|
||||
type = lib.types.str;
|
||||
default = "/etc/step/certs";
|
||||
default = "/etc/step-ca/certs";
|
||||
};
|
||||
};
|
||||
|
||||
@@ -267,11 +269,11 @@ in
|
||||
environment.systemPackages = with pkgs; lib.optionals cfg.enable [
|
||||
# step-cli
|
||||
(mkMtlsGenerateScript {
|
||||
inherit (cfg) subject provisioner san lifetime;
|
||||
inherit pkgs tlsCert tlsKey mtlsBundle;
|
||||
inherit pkgs;
|
||||
inherit (cfg) subject provisioner san certFile keyFile bundleFile lifetime;
|
||||
})
|
||||
(mkMtlsCheckScript { inherit pkgs mtlsBundle; })
|
||||
(mkMtlsRenewScript { inherit pkgs tlsCert tlsKey mtlsBundle; })
|
||||
(mkMtlsCheckScript { inherit pkgs; inherit (cfg) bundleFile; })
|
||||
(mkMtlsRenewScript { inherit pkgs; inherit (cfg) certFile keyFile bundleFile; })
|
||||
];
|
||||
|
||||
systemd.tmpfiles.rules = [
|
||||
@@ -279,7 +281,8 @@ in
|
||||
];
|
||||
|
||||
systemd.services.mtls-renew = lib.mkIf cfg.renew.enable (mkNixosMtlsRenewService {
|
||||
inherit pkgs tlsCert tlsKey mtlsBundle;
|
||||
inherit pkgs;
|
||||
inherit (cfg) certFile keyFile bundleFile;
|
||||
inherit (cfg.renew) reloadUnits postCommands user group;
|
||||
});
|
||||
|
||||
@@ -292,13 +295,13 @@ in
|
||||
flake.modules.homeManager.mtls = { config, lib, pkgs, ... }:
|
||||
let
|
||||
cfg = config.mtls;
|
||||
tlsKey = "${cfg.certDir}/${cfg.keyFilename}";
|
||||
tlsCert = "${cfg.certDir}/${cfg.certFilename}";
|
||||
mtlsBundle = "${cfg.certDir}/${cfg.bundleFilename}";
|
||||
keyFile = cfg.keyFile;
|
||||
certFile = cfg.certFile;
|
||||
bundleFile = cfg.bundleFile;
|
||||
sanArgs = lib.concatMapStringsSep " " (san: "--san \"${san}\"") cfg.san;
|
||||
in
|
||||
{
|
||||
options.mtls = opts // {
|
||||
options.mtls = (mkOpts config) // {
|
||||
certDir = lib.mkOption {
|
||||
description = "String path to where the mtls certs will be stored.";
|
||||
type = lib.types.str;
|
||||
@@ -311,10 +314,10 @@ in
|
||||
# step-cli
|
||||
(mkMtlsGenerateScript {
|
||||
inherit (cfg) subject provisioner san lifetime;
|
||||
inherit pkgs tlsCert tlsKey mtlsBundle;
|
||||
inherit pkgs certFile keyFile bundleFile;
|
||||
})
|
||||
(mkMtlsCheckScript { inherit pkgs mtlsBundle; })
|
||||
(mkMtlsRenewScript { inherit pkgs tlsCert tlsKey mtlsBundle; })
|
||||
(mkMtlsCheckScript { inherit pkgs bundleFile; })
|
||||
(mkMtlsRenewScript { inherit pkgs certFile keyFile bundleFile; })
|
||||
];
|
||||
|
||||
systemd.user.tmpfiles.rules = lib.mkIf cfg.enable [
|
||||
@@ -322,7 +325,7 @@ in
|
||||
];
|
||||
|
||||
systemd.user.services.mtls-renew = lib.mkIf cfg.renew.enable (mkHomeManagerMtlsRenewService {
|
||||
inherit pkgs tlsCert tlsKey mtlsBundle;
|
||||
inherit pkgs certFile keyFile bundleFile;
|
||||
inherit (cfg.renew) reloadUnits postCommands;
|
||||
});
|
||||
|
||||
|
||||
+12
-12
@@ -19,6 +19,11 @@
|
||||
type = lib.types.str;
|
||||
default = "john-ubuntu";
|
||||
};
|
||||
repoUrl = lib.mkOption {
|
||||
description = "URL to the REST endpoint";
|
||||
type = lib.types.str;
|
||||
default = "rest:https://soteria.john-stream.com/${cfg.repoName}";
|
||||
};
|
||||
passwordFile = lib.mkOption {
|
||||
description = "String path to the restic password file";
|
||||
type = lib.types.str;
|
||||
@@ -44,29 +49,24 @@
|
||||
};
|
||||
};
|
||||
|
||||
config = let
|
||||
resticRepository = "rest:https://soteria.john-stream.com/${cfg.repoName}";
|
||||
caCert = "${config.mtls.certDir}/root_ca.crt";
|
||||
mtlsBundle = "${config.mtls.certDir}/${config.mtls.bundleFilename}";
|
||||
in
|
||||
{
|
||||
config = {
|
||||
home.sessionVariables = {
|
||||
RESTIC_REPOSITORY = resticRepository;
|
||||
RESTIC_REPOSITORY = cfg.repoUrl;
|
||||
RESTIC_PASSWORD_FILE = cfg.passwordFile;
|
||||
RESTIC_CACERT = caCert;
|
||||
RESTIC_TLS_CLIENT_CERT = mtlsBundle;
|
||||
RESTIC_CACERT = config.mtls.caFile;
|
||||
RESTIC_TLS_CLIENT_CERT = config.mtls.bundleFile;
|
||||
};
|
||||
|
||||
# This is necessary because the restic service in home manager doesn't otherwise expose these options.
|
||||
systemd.user.services."restic-backups-${cfg.repoName}".Service.Environment = [
|
||||
"RESTIC_CACERT=${caCert}"
|
||||
"RESTIC_TLS_CLIENT_CERT=${mtlsBundle}"
|
||||
"RESTIC_CACERT=${config.mtls.caFile}"
|
||||
"RESTIC_TLS_CLIENT_CERT=${config.mtls.bundleFile}"
|
||||
];
|
||||
|
||||
services.restic = {
|
||||
enable = true;
|
||||
backups.${cfg.repoName} = {
|
||||
repository = resticRepository;
|
||||
repository = cfg.repoUrl;
|
||||
passwordFile = cfg.passwordFile;
|
||||
paths = cfg.paths;
|
||||
timerConfig = {
|
||||
|
||||
@@ -6,7 +6,8 @@ let
|
||||
fingerprint = "2036c44f7b5901566ff7611ea6c927291ecc6d2dd00779c0eead70ec77fa10d6";
|
||||
in
|
||||
{
|
||||
flake.modules.nixos.janus-ca = { config, lib, ... }:
|
||||
flake.modules.nixos.janus-ca =
|
||||
{ config, lib, ... }:
|
||||
let
|
||||
johnHome = lib.attrByPath [ "users" "users" username "home" ] "/home/${username}" config;
|
||||
johnGroup = lib.attrByPath [ "users" "users" username "group" ] username config;
|
||||
@@ -17,12 +18,13 @@ in
|
||||
"L+ ${home}/.step/config/defaults.json - - - - /etc/step/config/defaults.json"
|
||||
"L+ ${home}/.step/certs/root_ca.crt - - - - /etc/step/certs/root_ca.crt"
|
||||
];
|
||||
in {
|
||||
in
|
||||
{
|
||||
environment.etc."step/config/defaults.json".text = builtins.toJSON {
|
||||
inherit ca-url fingerprint;
|
||||
root = "/etc/step/certs/root_ca.crt";
|
||||
root = "/etc/step-ca/certs/root_ca.crt";
|
||||
};
|
||||
environment.etc."step/certs/root_ca.crt".source = ./root_ca.crt;
|
||||
environment.etc."step-ca/certs/root_ca.crt".source = ./root_ca.crt;
|
||||
systemd.tmpfiles.rules =
|
||||
mkStepRules johnHome username johnGroup
|
||||
++ mkStepRules "/root" "root" "root";
|
||||
@@ -73,7 +75,6 @@ in
|
||||
];
|
||||
};
|
||||
|
||||
|
||||
flake-file.inputs = {
|
||||
wrappers = {
|
||||
url = "github:lassulus/wrappers";
|
||||
|
||||
@@ -12,8 +12,6 @@ in
|
||||
flake.modules.homeManager."${hostname}" = { config, pkgs, lib, ... }:
|
||||
let
|
||||
flakeDir = "${config.xdg.configHome}/home-manager/jsl-dendritic";
|
||||
certDir = "${config.mtls.certDir}";
|
||||
mtlsBundle = "${certDir}/${config.mtls.bundleFilename}";
|
||||
resticPasswordFile = "${config.xdg.configHome}/restic/password.txt";
|
||||
|
||||
testPushCmd = (pkgs.writeShellScriptBin "test-push" ''
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
++ lib.optional isAdmin "wheel"
|
||||
++ lib.optional config.virtualisation.docker.enable "docker"
|
||||
++ lib.optional (isAdmin && config.services.forgejo.enable) config.services.forgejo.group
|
||||
++ lib.optional (isAdmin && config.services.postgresql.enable) config.services.postgresql.group;
|
||||
++ lib.optional (isAdmin && config.services.postgresql.enable) "postgres";
|
||||
};
|
||||
|
||||
security.sudo-rs.enable = lib.mkIf isAdmin true;
|
||||
|
||||
Reference in New Issue
Block a user