diff options
| author | tv <tv@krebsco.de> | 2026-12-01 03:50:16 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-12-01 03:50:25 +0000 |
| commit | 75df988e1cb9863ee5d708dbc578f7690bb182de (patch) | |
| tree | 552afe68563d330888a1f06dfac87cc8339608df /pkgs/populate/src/populate.rs | |
| parent | 5df6e41e05b9449a964467bc11a9d7fce2d802ec (diff) | |
populate pipe: directory, a directory the command makes
A pipe source's command printed a file. With directory set, 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, with the modes it has, e.g. secrets with
mode 0600 under 0700, and the fixed time of pipe's output, compared by
checksum. Secrets that should stay in memory can go to a target
directory of their own, e.g. in /run, with a populate run of its own.
The scenario pipe-directory checks the modes, and that a changed
directory gets mirrored, removing what's not in it anymore.
Diffstat (limited to 'pkgs/populate/src/populate.rs')
| -rw-r--r-- | pkgs/populate/src/populate.rs | 41 |
1 files changed, 41 insertions, 0 deletions
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; |
