diff options
Diffstat (limited to 'pkgs')
| -rw-r--r-- | pkgs/populate/README.md | 13 | ||||
| -rw-r--r-- | pkgs/populate/lib/types/populate.nix | 4 | ||||
| -rw-r--r-- | pkgs/populate/src/check.rs | 2 | ||||
| -rw-r--r-- | pkgs/populate/src/populate.rs | 41 | ||||
| -rwxr-xr-x | pkgs/populate/tests/fixtures.sh | 13 | ||||
| -rw-r--r-- | pkgs/populate/tests/scenarios.nix | 35 |
6 files changed, 106 insertions, 2 deletions
diff --git a/pkgs/populate/README.md b/pkgs/populate/README.md index 0bd1ac1..3a13eea 100644 --- a/pkgs/populate/README.md +++ b/pkgs/populate/README.md @@ -635,6 +635,19 @@ Supported attributes: * `command` - The (shell) command to run. +* `directory` (optional) - + boolean: whether the command makes a directory instead of printing a + file. It gets an empty directory as its first argument, mode 0700 in + populate's temporary directory, which populate removes afterwards, and + fills it, and populate delivers what it made there to the target path, + with the modes it has, e.g. secrets with mode 0600 under 0700. Defaults + to `false`. + +Like the file a command prints, what it makes gets a fixed modification +time, so only checksums tell whether it changed. A directory with +secrets that should only stay in memory can go to a target directory of +its own, e.g. in `/run`, with a populate run of its own. + ### `symlink` Symlink to create at the target, relative to the target directory. diff --git a/pkgs/populate/lib/types/populate.nix b/pkgs/populate/lib/types/populate.nix index 6605f61..2477e44 100644 --- a/pkgs/populate/lib/types/populate.nix +++ b/pkgs/populate/lib/types/populate.nix @@ -338,6 +338,10 @@ command = lib.mkOption { type = lib.types.absolute-pathname; }; + directory = lib.mkOption { + default = false; + type = lib.types.bool; + }; }; }; symlink = lib.types.submodule { diff --git a/pkgs/populate/src/check.rs b/pkgs/populate/src/check.rs index b272446..6c20891 100644 --- a/pkgs/populate/src/check.rs +++ b/pkgs/populate/src/check.rs @@ -109,7 +109,7 @@ fn known_settings(kind: &str) -> &'static [(&'static str, Kind, bool)] { ("dir", String, true), ("identities_file", String, false), ], - "pipe" => &[("command", Absolute, true)], + "pipe" => &[("command", Absolute, true), ("directory", Bool, false)], "symlink" => &[("target", String, true), ("inputs", Inputs, false)], _ => &[], } diff --git a/pkgs/populate/src/populate.rs b/pkgs/populate/src/populate.rs index cab1a91..6c4fd42 100644 --- a/pkgs/populate/src/populate.rs +++ b/pkgs/populate/src/populate.rs @@ -10,6 +10,7 @@ use crate::sh; use crate::spec::{Source, Spec}; use serde_json::{json, Map, Value}; use std::fs; +use std::os::unix::fs::DirBuilderExt; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; @@ -384,6 +385,9 @@ struct Pipe; impl Kind for Pipe { fn deliver(&self, d: &Delivery) -> Result<Outcome> { + if d.source.settings.get("directory").and_then(Value::as_bool) == Some(true) { + return deliver_directory(d); + } let out = d.workdir.join("out"); let file = fs::File::create(&out) .map_err(|e| Failure::Message(format!("error: can't create {}: {e}", out.display())))?; @@ -408,6 +412,43 @@ impl Kind for Pipe { } } +/// A pipe source's directory: the command fills an empty directory, which +/// it gets as $1, mode 0700 in populate's temporary directory, so nothing +/// on the way is anyone else's to read, and what it makes there goes to +/// the target path, with the modes it has, and the fixed time of pipe's +/// output, compared by checksum. +fn deliver_directory(d: &Delivery) -> Result<Outcome> { + let out = d.workdir.join("out"); + fs::DirBuilder::new() + .mode(0o700) + .create(&out) + .map_err(|e| Failure::Message(format!("error: can't create {}: {e}", out.display())))?; + let out = out.to_string_lossy(); + // Through a shell, which runs a command without #! as a script. + run::status(&mut run::local_shell( + "\"$1\" \"$2\"\n", + &[d.string("command")?, &out], + &d.workdir, + ))?; + // The fixed time of pipe's output, so the same contents don't change + // anything at the target path, and only checksums tell them apart. + run::status(Command::new("find").arg(out.as_ref()).args([ + "-exec", + "touch", + "-h", + "-t", + "300001010000", + "{}", + "+", + ]))?; + let config = Config { + use_checksum: true, + ..Config::default() + }; + let changed = rsync::transfer(d.context, &config, &out, d.path)?; + Ok(Outcome::changed(!changed.is_empty())) +} + /// A flake, or an input of one, delivered as a link into the store. struct Flake; diff --git a/pkgs/populate/tests/fixtures.sh b/pkgs/populate/tests/fixtures.sh index 93b302d..375514c 100755 --- a/pkgs/populate/tests/fixtures.sh +++ b/pkgs/populate/tests/fixtures.sh @@ -252,6 +252,19 @@ SH chmod +x /fixtures/pipe2 echo piped > /fixtures/pipe2-data +# pipe-dir: a command for the pipe source type's directory, making a +# directory of secrets in the one it gets, like enstate --pack does, with +# what /fixtures/pipe-dir-data says, which the tests change. +cat > /fixtures/pipe-dir <<'SH' +#! /bin/sh +set -efu +umask 077 +mkdir -p "$1"/sub/deep +cat /fixtures/pipe-dir-data > "$1"/sub/deep/secret +SH +chmod +x /fixtures/pipe-dir +echo one > /fixtures/pipe-dir-data + # alt2: like alt, with other outputs, to change a source to. mkdir /fixtures/alt2 cat > /fixtures/alt2/flake.nix <<'NIX' diff --git a/pkgs/populate/tests/scenarios.nix b/pkgs/populate/tests/scenarios.nix index f7ece9a..dac8e6b 100644 --- a/pkgs/populate/tests/scenarios.nix +++ b/pkgs/populate/tests/scenarios.nix @@ -468,7 +468,40 @@ in expect_eq x "$(cat "$T"/x)" piped ''; }; - }); + }) + # A directory the command makes: delivered with the modes it has, and + # mirrored, when what the command makes changes. + ++ map (targetKind: scenario (let + sources.x.pipe = { command = "/fixtures/pipe-dir"; directory = true; }; + secret = value: '' + expect_eq secret "$(cat "$T"/x/sub/deep/secret)" ${value} + expect_eq modes "$(stat -c %a "$T"/x "$T"/x/sub "$T"/x/sub/deep "$T"/x/sub/deep/secret | paste -s -d ' ' -)" "700 700 700 600" + ''; + in { + name = "pipe-directory-${targetKind}"; + inherit targetKind; + prepareDeployer = '' + echo one > /fixtures/pipe-dir-data + ''; + phases = [ + { + inherit sources; + check = secret "one"; + mutate = '' + echo stray > "$T"/x/stray + ''; + mutateDeployer = '' + echo two > /fixtures/pipe-dir-data + ''; + } + { + inherit sources; + check = secret "two" + '' + test ! -e "$T"/x/stray || fail "stray not removed" + ''; + } + ]; + })) targetKinds; symlink = matrix "symlink" { absolute = [ false true ]; |
