2022-02-12 23:49:27 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2021-02-06 20:04:21 +00:00
|
|
|
let
|
|
|
|
cfg = config.services.step-ca;
|
|
|
|
settingsFormat = (pkgs.formats.json { });
|
|
|
|
in
|
|
|
|
{
|
2021-03-20 22:14:26 +00:00
|
|
|
meta.maintainers = with lib.maintainers; [ mohe2015 ];
|
|
|
|
|
2021-02-06 20:04:21 +00:00
|
|
|
options = {
|
|
|
|
services.step-ca = {
|
2022-08-28 20:18:44 +01:00
|
|
|
enable = lib.mkEnableOption (lib.mdDoc "the smallstep certificate authority server");
|
|
|
|
openFirewall = lib.mkEnableOption (lib.mdDoc "opening the certificate authority server port");
|
2021-02-06 20:04:21 +00:00
|
|
|
package = lib.mkOption {
|
|
|
|
type = lib.types.package;
|
|
|
|
default = pkgs.step-ca;
|
2021-10-03 17:06:03 +01:00
|
|
|
defaultText = lib.literalExpression "pkgs.step-ca";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Which step-ca package to use.";
|
2021-02-06 20:04:21 +00:00
|
|
|
};
|
|
|
|
address = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
example = "127.0.0.1";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-02-06 20:04:21 +00:00
|
|
|
The address (without port) the certificate authority should listen at.
|
2022-07-28 22:19:15 +01:00
|
|
|
This combined with {option}`services.step-ca.port` overrides {option}`services.step-ca.settings.address`.
|
2021-02-06 20:04:21 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
port = lib.mkOption {
|
|
|
|
type = lib.types.port;
|
|
|
|
example = 8443;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-02-06 20:04:21 +00:00
|
|
|
The port the certificate authority should listen on.
|
2022-07-28 22:19:15 +01:00
|
|
|
This combined with {option}`services.step-ca.address` overrides {option}`services.step-ca.settings.address`.
|
2021-02-06 20:04:21 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
settings = lib.mkOption {
|
|
|
|
type = with lib.types; attrsOf anything;
|
2022-08-30 01:30:04 +01:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Settings that go into {file}`ca.json`. See
|
|
|
|
[the step-ca manual](https://smallstep.com/docs/step-ca/configuration)
|
2022-08-03 02:05:47 +01:00
|
|
|
for more information. The easiest way to
|
2022-08-30 01:30:04 +01:00
|
|
|
configure this module would be to run `step ca init`
|
|
|
|
to generate {file}`ca.json` and then import it using
|
|
|
|
`builtins.fromJSON`.
|
|
|
|
[This article](https://smallstep.com/docs/step-cli/basic-crypto-operations#run-an-offline-x509-certificate-authority)
|
2021-02-06 20:04:21 +00:00
|
|
|
may also be useful if you want to customize certain aspects of
|
|
|
|
certificate generation for your CA.
|
2022-08-30 01:30:04 +01:00
|
|
|
You need to change the database storage path to {file}`/var/lib/step-ca/db`.
|
2021-02-06 20:04:21 +00:00
|
|
|
|
2022-08-30 01:30:04 +01:00
|
|
|
::: {.warning}
|
|
|
|
The {option}`services.step-ca.settings.address` option
|
|
|
|
will be ignored and overwritten by
|
|
|
|
{option}`services.step-ca.address` and
|
|
|
|
{option}`services.step-ca.port`.
|
|
|
|
:::
|
2021-02-06 20:04:21 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
intermediatePasswordFile = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
|
|
|
example = "/run/keys/smallstep-password";
|
2022-08-30 01:30:04 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-02-06 20:04:21 +00:00
|
|
|
Path to the file containing the password for the intermediate
|
|
|
|
certificate private key.
|
|
|
|
|
2022-08-30 01:30:04 +01:00
|
|
|
::: {.warning}
|
|
|
|
Make sure to use a quoted absolute path instead of a path literal
|
|
|
|
to prevent it from being copied to the globally readable Nix
|
|
|
|
store.
|
|
|
|
:::
|
2021-02-06 20:04:21 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf config.services.step-ca.enable (
|
|
|
|
let
|
|
|
|
configFile = settingsFormat.generate "ca.json" (cfg.settings // {
|
|
|
|
address = cfg.address + ":" + toString cfg.port;
|
|
|
|
});
|
|
|
|
in
|
|
|
|
{
|
|
|
|
assertions =
|
|
|
|
[
|
|
|
|
{
|
|
|
|
assertion = !lib.isStorePath cfg.intermediatePasswordFile;
|
|
|
|
message = ''
|
|
|
|
<option>services.step-ca.intermediatePasswordFile</option> points to
|
|
|
|
a file in the Nix store. You should use a quoted absolute path to
|
|
|
|
prevent this.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
systemd.packages = [ cfg.package ];
|
|
|
|
|
|
|
|
# configuration file indirection is needed to support reloading
|
|
|
|
environment.etc."smallstep/ca.json".source = configFile;
|
|
|
|
|
|
|
|
systemd.services."step-ca" = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
restartTriggers = [ configFile ];
|
|
|
|
unitConfig = {
|
|
|
|
ConditionFileNotEmpty = ""; # override upstream
|
|
|
|
};
|
|
|
|
serviceConfig = {
|
2021-12-15 19:27:18 +00:00
|
|
|
User = "step-ca";
|
|
|
|
Group = "step-ca";
|
|
|
|
UMask = "0077";
|
2021-02-06 20:04:21 +00:00
|
|
|
Environment = "HOME=%S/step-ca";
|
|
|
|
WorkingDirectory = ""; # override upstream
|
|
|
|
ReadWriteDirectories = ""; # override upstream
|
|
|
|
|
|
|
|
# LocalCredential handles file permission problems arising from the use of DynamicUser.
|
|
|
|
LoadCredential = "intermediate_password:${cfg.intermediatePasswordFile}";
|
|
|
|
|
|
|
|
ExecStart = [
|
|
|
|
"" # override upstream
|
|
|
|
"${cfg.package}/bin/step-ca /etc/smallstep/ca.json --password-file \${CREDENTIALS_DIRECTORY}/intermediate_password"
|
|
|
|
];
|
|
|
|
|
|
|
|
# ProtectProc = "invisible"; # not supported by upstream yet
|
2021-10-26 19:47:30 +01:00
|
|
|
# ProcSubset = "pid"; # not supported by upstream yet
|
2021-02-06 20:04:21 +00:00
|
|
|
# PrivateUsers = true; # doesn't work with privileged ports therefore not supported by upstream
|
|
|
|
|
|
|
|
DynamicUser = true;
|
|
|
|
StateDirectory = "step-ca";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-12-15 19:27:18 +00:00
|
|
|
users.users.step-ca = {
|
|
|
|
home = "/var/lib/step-ca";
|
|
|
|
group = "step-ca";
|
|
|
|
isSystemUser = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
users.groups.step-ca = {};
|
|
|
|
|
2021-02-06 20:04:21 +00:00
|
|
|
networking.firewall = lib.mkIf cfg.openFirewall {
|
|
|
|
allowedTCPPorts = [ cfg.port ];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|