depot/ops/nixos/lib/vault-agent.nix

103 lines
3.2 KiB
Nix
Raw Normal View History

2022-01-23 23:38:40 +00:00
# SPDX-FileCopyrightText: 2020 Luke Granger-Brown <depot@lukegb.com>
#
# SPDX-License-Identifier: Apache-2.0
{ pkgs, config, depot, lib, ... }:
let
2022-03-06 22:26:49 +00:00
inherit (lib) mkEnableOption mkOption types mkBefore;
2022-01-23 23:38:40 +00:00
mkDefault = lib.mkOverride 900;
format = pkgs.formats.json {};
2022-03-06 22:26:49 +00:00
templatePathDirectories = lib.unique (map (t: dirOf t.destination) config.my.vault.settings.template);
2022-03-11 03:27:58 +00:00
# Remove empty lists at the top level because they make Vault implode.
cleanedSettings = lib.filterAttrs (n: v: !((builtins.typeOf v) == "list" && (builtins.length v) == 0)) config.my.vault.settings;
2022-01-23 23:38:40 +00:00
in
{
options.my.vault = {
enable = mkEnableOption "vault agent";
roleID = mkOption {
type = types.str;
default = config.networking.hostName;
};
secretIDPath = mkOption {
type = types.str;
default = "/var/lib/vault-agent/secret-id";
};
settings = mkOption {
type = format.type;
default = {};
};
bindMountStateTo = mkOption {
type = types.nullOr types.str;
default = null;
};
};
config = {
my.vault.enable = mkDefault true;
2022-03-06 22:26:49 +00:00
my.vault.settings = {
pid_file = mkDefault "/run/vault-agent/pid";
2022-03-11 21:48:06 +00:00
vault = {
address = mkDefault "https://vault.int.lukegb.com";
retry.num_retries = mkDefault 1;
};
2022-03-06 22:26:49 +00:00
auto_auth.method = mkDefault [{
2022-01-23 23:38:40 +00:00
type = "approle";
config = {
role_id_file_path = pkgs.writeText "${config.my.vault.roleID}-role-id" config.my.vault.roleID;
secret_id_file_path = config.my.vault.secretIDPath;
remove_secret_id_file_after_reading = false;
};
}];
2022-03-06 22:26:49 +00:00
cache.use_auto_auth_token = mkDefault true;
2022-01-23 23:38:40 +00:00
listener.unix = {
address = mkDefault "/run/vault-agent/sock";
2022-03-06 22:26:49 +00:00
tls_disable = mkDefault true;
2022-01-23 23:38:40 +00:00
};
};
2022-03-06 22:26:49 +00:00
users.groups.vault-agent = {};
users.users.vault-agent = { isSystemUser = true; group = "vault-agent"; };
2022-01-23 23:38:40 +00:00
systemd = lib.optionalAttrs config.my.vault.enable {
services.vault-agent = {
description = "Hashicorp Vault Agent";
wants = [ "network.target" ];
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
path = with pkgs; [ glibc.bin ];
environment.VAULT_CLIENT_TIMEOUT = "15m";
2022-01-23 23:38:40 +00:00
serviceConfig = {
RuntimeDirectory = "vault-agent";
RuntimeDirectoryMode = "0750";
2022-01-23 23:38:40 +00:00
StateDirectory = "vault-agent";
StateDirectoryMode = "0700";
User = "vault-agent";
2022-03-06 22:26:49 +00:00
NoNewPrivileges = true;
2022-01-23 23:38:40 +00:00
ProtectSystem = "strict";
ProtectHome = "yes";
2022-03-06 22:26:49 +00:00
ReadWritePaths = templatePathDirectories;
2022-03-11 03:27:58 +00:00
ExecStart = "${pkgs.vault}/bin/vault agent -config=${format.generate "vault-agent.json" cleanedSettings}";
2022-01-23 23:38:40 +00:00
};
};
mounts = lib.optional (config.my.vault.bindMountStateTo != null) {
unitConfig.RequiresMountsFor = "${config.my.vault.bindMountStateTo} /var/lib";
2022-01-23 23:38:40 +00:00
options = "bind";
what = config.my.vault.bindMountStateTo;
where = "/var/lib/vault-agent";
2022-01-23 23:38:40 +00:00
requiredBy = [ "vault-agent.service" ];
before = [ "vault-agent.service" ];
wantedBy = [ "vault-agent.service" ];
};
};
};
}