git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #108411 from hercules-ci/vault-multiple-config-files

vault: Support multiple config files (no secrets in store)

commit530df49d7a289bef7135c1d5f222914f19dfe8ec authorRobert Hensing <roberth@users.noreply.github.com> dateSun 24 Jan 2021, 13:49 (6 years ago) parent0a81d0804946f4
4 files changed+116−2
nixos/modules/services/security/vault.nix+44−2 nixos/tests/all-tests.nix+1 nixos/tests/vault-postgresql.nix+70 nixos/tests/vault.nix+1
nixos/modules/services/security/vault.nix+44−2
@@ -27,6 +27,11 @@ let
2727 ''}
2828 ${cfg.extraConfig}
2929 '';
30+
31+ allConfigPaths = [configFile] ++ cfg.extraSettingsPaths;
32+
33+ configOptions = escapeShellArgs (concatMap (p: ["-config" p]) allConfigPaths);
34+
3035in
3136
3237{
@@ -84,7 +89,14 @@ in
8489 storageConfig = mkOption {
8590 type = types.nullOr types.lines;
8691 default = null;
87− description = "Storage configuration";
92+ description = ''
93+ HCL configuration to insert in the storageBackend section.
94+
95+ Confidential values should not be specified here because this option's
96+ value is written to the Nix store, which is publicly readable.
97+ Provide credentials and such in a separate file using
98+ <xref linkend="opt-services.vault.extraSettingsPaths"/>.
99+ '';
88100 };
89101
90102 telemetryConfig = mkOption {
@@ -98,6 +110,36 @@ in
98110 default = "";
99111 description = "Extra text appended to <filename>vault.hcl</filename>.";
100112 };
113+
114+ extraSettingsPaths = mkOption {
115+ type = types.listOf types.path;
116+ default = [];
117+ description = ''
118+ Configuration files to load besides the immutable one defined by the NixOS module.
119+ This can be used to avoid putting credentials in the Nix store, which can be read by any user.
120+
121+ Each path can point to a JSON- or HCL-formatted file, or a directory
122+ to be scanned for files with <literal>.hcl</literal> or
123+ <literal>.json</literal> extensions.
124+
125+ To upload the confidential file with NixOps, use for example:
126+
127+ <programlisting><![CDATA[
128+ # https://releases.nixos.org/nixops/latest/manual/manual.html#opt-deployment.keys
129+ deployment.keys."vault.hcl" = let db = import ./db-credentials.nix; in {
130+ text = ${"''"}
131+ storage "postgresql" {
132+ connection_url = "postgres://''${db.username}:''${db.password}@host.example.com/exampledb?sslmode=verify-ca"
133+ }
134+ ${"''"};
135+ user = "vault";
136+ };
137+ services.vault.extraSettingsPaths = ["/run/keys/vault.hcl"];
138+ services.vault.storageBackend = "postgresql";
139+ users.users.vault.extraGroups = ["keys"];
140+ ]]></programlisting>
141+ '';
142+ };
101143 };
102144 };
103145
@@ -136,7 +178,7 @@ in
136178 serviceConfig = {
137179 User = "vault";
138180 Group = "vault";
139− ExecStart = "${cfg.package}/bin/vault server -config ${configFile}";
181+ ExecStart = "${cfg.package}/bin/vault server ${configOptions}";
140182 ExecReload = "${pkgs.coreutils}/bin/kill -SIGHUP $MAINPID";
141183 PrivateDevices = true;
142184 PrivateTmp = true;
nixos/tests/all-tests.nix+1
@@ -407,6 +407,7 @@ in
407407 uwsgi = handleTest ./uwsgi.nix {};
408408 v2ray = handleTest ./v2ray.nix {};
409409 vault = handleTest ./vault.nix {};
410+ vault-postgresql = handleTest ./vault-postgresql.nix {};
410411 vector = handleTest ./vector.nix {};
411412 victoriametrics = handleTest ./victoriametrics.nix {};
412413 virtualbox = handleTestOn ["x86_64-linux"] ./virtualbox.nix {};
nixos/tests/vault-postgresql.nixnew file+70
@@ -0,0 +1,70 @@
1+/* This test checks that
2+ - multiple config files can be loaded
3+ - the storage backend can be in a file outside the nix store
4+ as is required for security (required because while confidentiality is
5+ always covered, availability isn't)
6+ - the postgres integration works
7+ */
8+import ./make-test-python.nix ({ pkgs, ... }:
9+{
10+ name = "vault-postgresql";
11+ meta = with pkgs.stdenv.lib.maintainers; {
12+ maintainers = [ lnl7 roberth ];
13+ };
14+ machine = { lib, pkgs, ... }: {
15+ virtualisation.memorySize = 512;
16+ environment.systemPackages = [ pkgs.vault ];
17+ environment.variables.VAULT_ADDR = "http://127.0.0.1:8200";
18+ services.vault.enable = true;
19+ services.vault.extraSettingsPaths = [ "/run/vault.hcl" ];
20+
21+ systemd.services.vault = {
22+ after = [
23+ "postgresql.service"
24+ ];
25+ # Try for about 10 minutes rather than the default of 5 attempts.
26+ serviceConfig.RestartSec = 1;
27+ serviceConfig.StartLimitBurst = 600;
28+ };
29+ # systemd.services.vault.unitConfig.RequiresMountsFor = "/run/keys/";
30+
31+ services.postgresql.enable = true;
32+ services.postgresql.initialScript = pkgs.writeText "init.psql" ''
33+ CREATE USER vaultuser WITH ENCRYPTED PASSWORD 'thisisthepass';
34+ GRANT CONNECT ON DATABASE postgres TO vaultuser;
35+
36+ -- https://www.vaultproject.io/docs/configuration/storage/postgresql
37+ CREATE TABLE vault_kv_store (
38+ parent_path TEXT COLLATE "C" NOT NULL,
39+ path TEXT COLLATE "C",
40+ key TEXT COLLATE "C",
41+ value BYTEA,
42+ CONSTRAINT pkey PRIMARY KEY (path, key)
43+ );
44+ CREATE INDEX parent_path_idx ON vault_kv_store (parent_path);
45+
46+ GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO vaultuser;
47+ '';
48+ };
49+
50+ testScript =
51+ ''
52+ secretConfig = """
53+ storage "postgresql" {
54+ connection_url = "postgres://vaultuser:thisisthepass@localhost/postgres?sslmode=disable"
55+ }
56+ """
57+
58+ start_all()
59+
60+ machine.wait_for_unit("multi-user.target")
61+ machine.succeed("cat >/root/vault.hcl <<EOF\n%s\nEOF\n" % secretConfig)
62+ machine.succeed(
63+ "install --owner vault --mode 0400 /root/vault.hcl /run/vault.hcl; rm /root/vault.hcl"
64+ )
65+ machine.wait_for_unit("vault.service")
66+ machine.wait_for_open_port(8200)
67+ machine.succeed("vault operator init")
68+ machine.succeed("vault status | grep Sealed | grep true")
69+ '';
70+})
nixos/tests/vault.nix+1
@@ -8,6 +8,7 @@ import ./make-test-python.nix ({ pkgs, ... }:
88 environment.systemPackages = [ pkgs.vault ];
99 environment.variables.VAULT_ADDR = "http://127.0.0.1:8200";
1010 services.vault.enable = true;
11+ virtualisation.memorySize = 512;
1112 };
1213
1314 testScript =