2021-12-05 21:45:35 +01:00
|
|
|
{ config, lib, options, pkgs, ... }:
|
2017-06-07 22:31:40 +02:00
|
|
|
|
|
|
|
with lib;
|
2018-08-09 23:22:53 +02:00
|
|
|
|
2017-06-07 22:31:40 +02:00
|
|
|
let
|
|
|
|
cfg = config.services.vault;
|
2021-12-05 21:45:35 +01:00
|
|
|
opt = options.services.vault;
|
2017-06-27 16:54:25 +02:00
|
|
|
|
2017-06-07 22:31:40 +02:00
|
|
|
configFile = pkgs.writeText "vault.hcl" ''
|
|
|
|
listener "tcp" {
|
2017-06-27 16:54:25 +02:00
|
|
|
address = "${cfg.address}"
|
2017-06-29 00:08:36 +02:00
|
|
|
${if (cfg.tlsCertFile == null || cfg.tlsKeyFile == null) then ''
|
|
|
|
tls_disable = "true"
|
|
|
|
'' else ''
|
|
|
|
tls_cert_file = "${cfg.tlsCertFile}"
|
|
|
|
tls_key_file = "${cfg.tlsKeyFile}"
|
|
|
|
''}
|
2017-06-27 16:54:25 +02:00
|
|
|
${cfg.listenerExtraConfig}
|
2017-06-07 22:31:40 +02:00
|
|
|
}
|
2017-06-27 16:54:25 +02:00
|
|
|
storage "${cfg.storageBackend}" {
|
2017-06-29 23:10:56 +02:00
|
|
|
${optionalString (cfg.storagePath != null) ''path = "${cfg.storagePath}"''}
|
|
|
|
${optionalString (cfg.storageConfig != null) cfg.storageConfig}
|
2017-06-07 22:31:40 +02:00
|
|
|
}
|
2017-06-27 16:54:25 +02:00
|
|
|
${optionalString (cfg.telemetryConfig != "") ''
|
|
|
|
telemetry {
|
|
|
|
${cfg.telemetryConfig}
|
|
|
|
}
|
|
|
|
''}
|
2018-08-09 23:22:53 +02:00
|
|
|
${cfg.extraConfig}
|
2017-06-07 22:31:40 +02:00
|
|
|
'';
|
2021-01-04 16:28:16 +01:00
|
|
|
|
2021-01-19 18:14:29 +01:00
|
|
|
allConfigPaths = [configFile] ++ cfg.extraSettingsPaths;
|
2021-01-04 16:28:16 +01:00
|
|
|
|
|
|
|
configOptions = escapeShellArgs (concatMap (p: ["-config" p]) allConfigPaths);
|
|
|
|
|
2017-06-07 22:31:40 +02:00
|
|
|
in
|
2018-08-09 23:22:53 +02:00
|
|
|
|
2017-06-07 22:31:40 +02:00
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.vault = {
|
2017-06-27 16:54:25 +02:00
|
|
|
enable = mkEnableOption "Vault daemon";
|
2017-06-07 22:31:40 +02:00
|
|
|
|
2018-08-09 23:22:53 +02:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.vault;
|
2021-10-03 18:06:03 +02:00
|
|
|
defaultText = literalExpression "pkgs.vault";
|
2018-08-09 23:22:53 +02:00
|
|
|
description = "This option specifies the vault package to use.";
|
|
|
|
};
|
|
|
|
|
2017-06-27 16:54:25 +02:00
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1:8200";
|
|
|
|
description = "The name of the ip interface to listen to";
|
2017-06-07 22:31:40 +02:00
|
|
|
};
|
|
|
|
|
2017-06-27 16:54:25 +02:00
|
|
|
tlsCertFile = mkOption {
|
2017-06-29 00:08:36 +02:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2017-06-27 16:54:25 +02:00
|
|
|
example = "/path/to/your/cert.pem";
|
2017-06-29 00:08:36 +02:00
|
|
|
description = "TLS certificate file. TLS will be disabled unless this option is set";
|
2017-06-07 22:31:40 +02:00
|
|
|
};
|
|
|
|
|
2017-06-27 16:54:25 +02:00
|
|
|
tlsKeyFile = mkOption {
|
2017-06-29 00:08:36 +02:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2017-06-27 16:54:25 +02:00
|
|
|
example = "/path/to/your/key.pem";
|
2017-06-29 00:08:36 +02:00
|
|
|
description = "TLS private key file. TLS will be disabled unless this option is set";
|
2017-06-27 16:54:25 +02:00
|
|
|
};
|
2017-06-07 22:31:40 +02:00
|
|
|
|
2017-06-27 16:54:25 +02:00
|
|
|
listenerExtraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = ''
|
|
|
|
tls_min_version = "tls12"
|
|
|
|
'';
|
2018-08-09 23:22:53 +02:00
|
|
|
description = "Extra text appended to the listener section.";
|
2017-06-27 16:54:25 +02:00
|
|
|
};
|
2017-06-07 22:31:40 +02:00
|
|
|
|
2017-06-27 16:54:25 +02:00
|
|
|
storageBackend = mkOption {
|
2019-08-28 18:42:18 +02:00
|
|
|
type = types.enum [ "inmem" "file" "consul" "zookeeper" "s3" "azure" "dynamodb" "etcd" "mssql" "mysql" "postgresql" "swift" "gcs" "raft" ];
|
2017-06-27 16:54:25 +02:00
|
|
|
default = "inmem";
|
|
|
|
description = "The name of the type of storage backend";
|
|
|
|
};
|
2017-06-07 22:31:40 +02:00
|
|
|
|
2017-06-29 23:10:56 +02:00
|
|
|
storagePath = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = if cfg.storageBackend == "file" then "/var/lib/vault" else null;
|
2021-12-05 21:45:35 +01:00
|
|
|
defaultText = literalExpression ''
|
|
|
|
if config.${opt.storageBackend} == "file"
|
|
|
|
then "/var/lib/vault"
|
|
|
|
else null
|
|
|
|
'';
|
2017-06-29 23:10:56 +02:00
|
|
|
description = "Data directory for file backend";
|
|
|
|
};
|
|
|
|
|
2017-06-27 16:54:25 +02:00
|
|
|
storageConfig = mkOption {
|
2017-06-29 23:10:56 +02:00
|
|
|
type = types.nullOr types.lines;
|
|
|
|
default = null;
|
2021-01-04 16:28:16 +01:00
|
|
|
description = ''
|
|
|
|
HCL configuration to insert in the storageBackend section.
|
|
|
|
|
|
|
|
Confidential values should not be specified here because this option's
|
|
|
|
value is written to the Nix store, which is publicly readable.
|
|
|
|
Provide credentials and such in a separate file using
|
2021-01-19 18:14:29 +01:00
|
|
|
<xref linkend="opt-services.vault.extraSettingsPaths"/>.
|
2021-01-04 16:28:16 +01:00
|
|
|
'';
|
2017-06-07 22:31:40 +02:00
|
|
|
};
|
|
|
|
|
2017-06-27 16:54:25 +02:00
|
|
|
telemetryConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = "Telemetry configuration";
|
|
|
|
};
|
2018-08-09 23:22:53 +02:00
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = "Extra text appended to <filename>vault.hcl</filename>.";
|
|
|
|
};
|
2021-01-04 16:28:16 +01:00
|
|
|
|
2021-01-19 18:14:29 +01:00
|
|
|
extraSettingsPaths = mkOption {
|
2021-01-04 16:28:16 +01:00
|
|
|
type = types.listOf types.path;
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
Configuration files to load besides the immutable one defined by the NixOS module.
|
|
|
|
This can be used to avoid putting credentials in the Nix store, which can be read by any user.
|
|
|
|
|
|
|
|
Each path can point to a JSON- or HCL-formatted file, or a directory
|
|
|
|
to be scanned for files with <literal>.hcl</literal> or
|
|
|
|
<literal>.json</literal> extensions.
|
|
|
|
|
|
|
|
To upload the confidential file with NixOps, use for example:
|
|
|
|
|
|
|
|
<programlisting><![CDATA[
|
|
|
|
# https://releases.nixos.org/nixops/latest/manual/manual.html#opt-deployment.keys
|
|
|
|
deployment.keys."vault.hcl" = let db = import ./db-credentials.nix; in {
|
|
|
|
text = ${"''"}
|
|
|
|
storage "postgresql" {
|
|
|
|
connection_url = "postgres://''${db.username}:''${db.password}@host.example.com/exampledb?sslmode=verify-ca"
|
|
|
|
}
|
|
|
|
${"''"};
|
|
|
|
user = "vault";
|
|
|
|
};
|
2021-01-19 18:14:29 +01:00
|
|
|
services.vault.extraSettingsPaths = ["/run/keys/vault.hcl"];
|
2021-01-04 16:28:16 +01:00
|
|
|
services.vault.storageBackend = "postgresql";
|
|
|
|
users.users.vault.extraGroups = ["keys"];
|
|
|
|
]]></programlisting>
|
|
|
|
'';
|
|
|
|
};
|
2017-06-07 22:31:40 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-06-29 23:10:56 +02:00
|
|
|
config = mkIf cfg.enable {
|
|
|
|
assertions = [
|
|
|
|
{ assertion = cfg.storageBackend == "inmem" -> (cfg.storagePath == null && cfg.storageConfig == null);
|
|
|
|
message = ''The "inmem" storage expects no services.vault.storagePath nor services.vault.storageConfig'';
|
|
|
|
}
|
|
|
|
{ assertion = (cfg.storageBackend == "file" -> (cfg.storagePath != null && cfg.storageConfig == null)) && (cfg.storagePath != null -> cfg.storageBackend == "file");
|
|
|
|
message = ''You must set services.vault.storagePath only when using the "file" backend'';
|
|
|
|
}
|
|
|
|
];
|
2017-06-07 22:31:40 +02:00
|
|
|
|
2018-06-30 01:58:35 +02:00
|
|
|
users.users.vault = {
|
2017-06-27 16:54:25 +02:00
|
|
|
name = "vault";
|
|
|
|
group = "vault";
|
|
|
|
uid = config.ids.uids.vault;
|
|
|
|
description = "Vault daemon user";
|
|
|
|
};
|
2018-06-30 01:58:35 +02:00
|
|
|
users.groups.vault.gid = config.ids.gids.vault;
|
2017-06-07 22:31:40 +02:00
|
|
|
|
2019-11-04 18:08:57 +01:00
|
|
|
systemd.tmpfiles.rules = optional (cfg.storagePath != null)
|
|
|
|
"d '${cfg.storagePath}' 0700 vault vault - -";
|
2019-02-24 13:53:36 +01:00
|
|
|
|
2017-06-27 16:54:25 +02:00
|
|
|
systemd.services.vault = {
|
|
|
|
description = "Vault server daemon";
|
2017-06-07 22:31:40 +02:00
|
|
|
|
2017-06-27 16:54:25 +02:00
|
|
|
wantedBy = ["multi-user.target"];
|
2017-06-28 02:58:19 +02:00
|
|
|
after = [ "network.target" ]
|
|
|
|
++ optional (config.services.consul.enable && cfg.storageBackend == "consul") "consul.service";
|
2017-06-27 16:54:25 +02:00
|
|
|
|
2017-07-03 21:46:02 +02:00
|
|
|
restartIfChanged = false; # do not restart on "nixos-rebuild switch". It would seal the storage and disrupt the clients.
|
|
|
|
|
2020-09-09 09:31:27 +02:00
|
|
|
startLimitIntervalSec = 60;
|
|
|
|
startLimitBurst = 3;
|
2017-06-27 16:54:25 +02:00
|
|
|
serviceConfig = {
|
|
|
|
User = "vault";
|
|
|
|
Group = "vault";
|
2021-01-04 16:28:16 +01:00
|
|
|
ExecStart = "${cfg.package}/bin/vault server ${configOptions}";
|
2020-01-24 18:59:13 +01:00
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -SIGHUP $MAINPID";
|
2017-06-27 16:54:25 +02:00
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
ProtectSystem = "full";
|
|
|
|
ProtectHome = "read-only";
|
|
|
|
AmbientCapabilities = "cap_ipc_lock";
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
KillSignal = "SIGINT";
|
|
|
|
TimeoutStopSec = "30s";
|
|
|
|
Restart = "on-failure";
|
|
|
|
};
|
2017-06-28 03:15:20 +02:00
|
|
|
|
2017-06-29 23:10:56 +02:00
|
|
|
unitConfig.RequiresMountsFor = optional (cfg.storagePath != null) cfg.storagePath;
|
2017-06-27 16:54:25 +02:00
|
|
|
};
|
2017-06-07 22:31:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|