podup 0.21.1

Translate and run docker-compose files on rootless Podman
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Secret and config materialisation.
//!
//! [`Engine::build_secret_binds`] and [`Engine::build_config_binds`] materialise
//! `file:`, inline `content:` and `environment:` secret/config sources into a
//! restricted temp directory and return their bind strings.
//! [`Engine::build_native_secrets`] maps `external: true` secrets/configs to
//! Podman-native secrets attached to the container create spec.

use crate::compose::types::{
	ComposeFile, ConfigConfig, SecretConfig, Service, ServiceConfigRef, ServiceSecretRef,
};
use crate::error::{ComposeError, Result};
use crate::libpod::types::container::Secret;

use super::{staging, Engine};

impl Engine {
	pub(super) fn build_secret_binds(
		&self,
		service: &Service,
		file: &ComposeFile,
	) -> Result<Vec<String>> {
		let mut binds = Vec::new();
		for secret_ref in &service.secrets {
			let (name, target_override, ref_mode, ref_uid, ref_gid) = match secret_ref {
				ServiceSecretRef::Short(s) => (s.clone(), None, None, None, None),
				ServiceSecretRef::Long {
					source,
					target,
					mode,
					uid,
					gid,
				} => (
					source.clone(),
					target.clone(),
					*mode,
					uid.clone(),
					gid.clone(),
				),
			};
			if let Some(config) = file.secrets.get(&name) {
				let target = target_override.unwrap_or_else(|| format!("/run/secrets/{name}"));
				match config {
					SecretConfig {
						file: Some(host_path),
						..
					} => {
						// Resolve like a bind-mount source: a relative `file:` is
						// anchored to the project dir (not the Podman service's cwd)
						// and `~` is expanded — same handling as `volumes:`, which
						// already mount arbitrary host paths.
						let resolved =
							super::container::resolve_bind_source(host_path, &self.base_dir);
						binds.push(format!("{resolved}:{target}:ro"));
					}
					SecretConfig {
						content: Some(content),
						..
					} => {
						let path = self.materialize_inline_full(
							"secrets",
							&name,
							content.as_bytes(),
							ref_mode,
							ref_uid.as_deref(),
							ref_gid.as_deref(),
						)?;
						binds.push(format!("{}:{target}:ro", path.display()));
					}
					SecretConfig {
						environment: Some(env_var),
						..
					} => {
						let value = std::env::var(env_var).map_err(|_| {
							ComposeError::Unsupported(format!(
								"secret '{name}' references env var '{env_var}' which is not set"
							))
						})?;
						let path = self.materialize_inline_full(
							"secrets",
							&name,
							value.as_bytes(),
							ref_mode,
							ref_uid.as_deref(),
							ref_gid.as_deref(),
						)?;
						binds.push(format!("{}:{target}:ro", path.display()));
					}
					// `external: true` secrets are injected natively — see
					// build_native_secrets — not as bind mounts, so skip them here.
					_ => {}
				}
			}
		}
		Ok(binds)
	}

	pub(super) fn build_config_binds(
		&self,
		service: &Service,
		file: &ComposeFile,
	) -> Result<Vec<String>> {
		let mut binds = Vec::new();
		for config_ref in &service.configs {
			let (name, target_override, ref_mode, ref_uid, ref_gid) = match config_ref {
				ServiceConfigRef::Short(s) => (s.clone(), None, None, None, None),
				ServiceConfigRef::Long {
					source,
					target,
					mode,
					uid,
					gid,
				} => (
					source.clone(),
					target.clone(),
					*mode,
					uid.clone(),
					gid.clone(),
				),
			};
			if let Some(cfg) = file.configs.get(&name) {
				let target = target_override.unwrap_or_else(|| format!("/{name}"));
				match cfg {
					ConfigConfig {
						file: Some(host_path),
						..
					} => {
						// Resolve like a bind-mount source: anchor a relative path to
						// the project dir and expand `~`, matching `volumes:` handling.
						let resolved =
							super::container::resolve_bind_source(host_path, &self.base_dir);
						binds.push(format!("{resolved}:{target}:ro"));
					}
					ConfigConfig {
						content: Some(content),
						..
					} => {
						let path = self.materialize_inline_full(
							"configs",
							&name,
							content.as_bytes(),
							ref_mode,
							ref_uid.as_deref(),
							ref_gid.as_deref(),
						)?;
						binds.push(format!("{}:{target}:ro", path.display()));
					}
					ConfigConfig {
						environment: Some(env_var),
						..
					} => {
						let value = std::env::var(env_var).map_err(|_| {
							ComposeError::Unsupported(format!(
								"config '{name}' references env var '{env_var}' which is not set"
							))
						})?;
						let path = self.materialize_inline_full(
							"configs",
							&name,
							value.as_bytes(),
							ref_mode,
							ref_uid.as_deref(),
							ref_gid.as_deref(),
						)?;
						binds.push(format!("{}:{target}:ro", path.display()));
					}
					// `external: true` configs are injected natively — see
					// build_native_secrets — not as bind mounts, so skip them here.
					_ => {}
				}
			}
		}
		Ok(binds)
	}

	/// Build the Podman-native secret references for a service: every `external:
	/// true` secret and config it references, mapped to an existing `podman
	/// secret` and mounted into the container. Each source is preflighted with
	/// [`Engine::ensure_external_exists`] so a missing secret fails closed with a
	/// clear message instead of starting a container that lacks it.
	pub(super) async fn build_native_secrets(
		&self,
		service: &Service,
		file: &ComposeFile,
	) -> Result<Vec<Secret>> {
		let secrets = collect_native_secrets(service, file)?;
		for secret in &secrets {
			self.ensure_external_exists("secret", "secrets", &secret.source)
				.await?;
		}
		Ok(secrets)
	}

	fn materialize_inline_full(
		&self,
		kind: &str,
		name: &str,
		content: &[u8],
		mode: Option<u32>,
		uid: Option<&str>,
		gid: Option<&str>,
	) -> Result<std::path::PathBuf> {
		if std::path::Path::new(name)
			.components()
			.any(|c| !matches!(c, std::path::Component::Normal(_)))
		{
			return Err(ComposeError::Unsupported(format!(
				"{kind} name must not contain path separators or '..': {name}"
			)));
		}

		let dir = self.staging_dir()?.join(kind);
		staging::create_private_subdir(&dir)?;

		let path = dir.join(name);
		staging::write_private_file(&path, content)?;

		if let Some(m) = mode {
			staging::apply_mode(&path, m)?;
		}
		staging::apply_owner(&path, uid, gid);

		Ok(path)
	}
}

/// Collect the Podman-native secret specs for a service without touching the
/// daemon: every `external: true` secret and config it references becomes a
/// [`Secret`] pointing at an existing `podman secret`. Pure, so the mapping is
/// unit-testable; [`Engine::build_native_secrets`] adds the existence preflight.
///
/// A dangerous `mode:` (execute, setuid, setgid or sticky bits) on any
/// reference is rejected here — the same class of check `apply_mode` enforces
/// for the bind-mount path — so a hostile mode never reaches the daemon.
fn collect_native_secrets(service: &Service, file: &ComposeFile) -> Result<Vec<Secret>> {
	let mut secrets = Vec::new();

	for secret_ref in &service.secrets {
		let (name, target_override, mode, uid, gid) = match secret_ref {
			ServiceSecretRef::Short(s) => (s.clone(), None, None, None, None),
			ServiceSecretRef::Long {
				source,
				target,
				mode,
				uid,
				gid,
			} => (
				source.clone(),
				target.clone(),
				*mode,
				uid.clone(),
				gid.clone(),
			),
		};
		if let Some(def) = file.secrets.get(&name) {
			if def.external == Some(true) {
				let source = def.name.clone().unwrap_or_else(|| name.clone());
				// Default the mount filename to the compose name (so apps still
				// find /run/secrets/<name> even when the Podman secret is named
				// differently); a long-form target overrides it.
				let target = target_override.unwrap_or(name);
				secrets.push(native_secret(source, target, mode, uid, gid)?);
			}
		}
	}

	for config_ref in &service.configs {
		let (name, target_override, mode, uid, gid) = match config_ref {
			ServiceConfigRef::Short(s) => (s.clone(), None, None, None, None),
			ServiceConfigRef::Long {
				source,
				target,
				mode,
				uid,
				gid,
			} => (
				source.clone(),
				target.clone(),
				*mode,
				uid.clone(),
				gid.clone(),
			),
		};
		if let Some(def) = file.configs.get(&name) {
			if def.external == Some(true) {
				let source = def.name.clone().unwrap_or_else(|| name.clone());
				// Configs default to an absolute container-root path, matching the
				// bind-mount config behaviour; an absolute target is used as-is.
				let target = target_override.unwrap_or_else(|| format!("/{name}"));
				secrets.push(native_secret(source, target, mode, uid, gid)?);
			}
		}
	}

	Ok(secrets)
}

/// Assemble one [`Secret`] from a compose reference. A dangerous `mode:`
/// (execute/setuid/setgid/sticky) is rejected before the spec is built so it
/// never reaches Podman. `uid`/`gid` are numeric in libpod, so non-numeric
/// values (a user/group name) are dropped to the default.
fn native_secret(
	source: String,
	target: String,
	mode: Option<u32>,
	uid: Option<String>,
	gid: Option<String>,
) -> Result<Secret> {
	if let Some(m) = mode {
		staging::reject_dangerous_secret_mode(m, &source)?;
	}
	Ok(Secret {
		source,
		target: Some(target),
		uid: uid.and_then(|s| s.parse().ok()),
		gid: gid.and_then(|s| s.parse().ok()),
		mode,
	})
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::libpod::Client;
	use std::path::PathBuf;

	fn engine_with_base(base: &str) -> Engine {
		Engine::with_base_dir(
			Client::new("unused"),
			"proj".to_string(),
			PathBuf::from(base),
		)
	}

	#[test]
	fn secret_file_relative_path_is_anchored_to_base_dir() {
		// A relative `file:` must resolve against the project dir, not the
		// Podman service's cwd — same as a bind-mount source. Build the expected
		// path via `Path::join` so the separator is correct on every platform.
		let base = PathBuf::from("/srv/project");
		let yaml = "services:\n  web:\n    image: nginx\n    secrets: [tok]\nsecrets:\n  tok:\n    file: secret.txt\n";
		let file = crate::compose::parse_str_raw(yaml).unwrap();
		let engine = engine_with_base(&base.to_string_lossy());
		let binds = engine
			.build_secret_binds(&file.services["web"], &file)
			.unwrap();
		let expected = format!("{}:/run/secrets/tok:ro", base.join("secret.txt").display());
		assert_eq!(binds, vec![expected]);
	}

	#[cfg(unix)]
	#[test]
	fn config_file_absolute_path_is_passed_through() {
		// Absolute paths are honored unchanged (compose allows mounting any host
		// file, exactly as `volumes:` does). Uses a Unix-absolute path, so the
		// assertion is gated to Unix.
		let yaml = "services:\n  web:\n    image: nginx\n    configs: [cfg]\nconfigs:\n  cfg:\n    file: /etc/app/cfg.yaml\n";
		let file = crate::compose::parse_str_raw(yaml).unwrap();
		let engine = engine_with_base("/srv/project");
		let binds = engine
			.build_config_binds(&file.services["web"], &file)
			.unwrap();
		assert_eq!(binds, vec!["/etc/app/cfg.yaml:/cfg:ro"]);
	}

	#[test]
	fn external_secret_becomes_native_targeting_compose_name() {
		// An `external: true` secret maps to a Podman-native secret. With no
		// custom name the source equals the compose name, and the mount filename
		// defaults to that name so apps still read /run/secrets/<name>.
		let yaml = "services:\n  web:\n    image: nginx\n    secrets: [tok]\nsecrets:\n  tok:\n    external: true\n";
		let file = crate::compose::parse_str_raw(yaml).unwrap();
		let secrets = collect_native_secrets(&file.services["web"], &file).unwrap();
		assert_eq!(secrets.len(), 1);
		assert_eq!(secrets[0].source, "tok");
		assert_eq!(secrets[0].target.as_deref(), Some("tok"));
		assert!(secrets[0].uid.is_none() && secrets[0].gid.is_none() && secrets[0].mode.is_none());
	}

	#[test]
	fn external_secret_long_form_maps_source_target_and_perms() {
		// A long-form ref overrides the mount name, and a custom top-level `name:`
		// is the actual Podman secret to look up. Numeric uid/gid/mode pass through.
		let yaml = "services:\n  web:\n    image: nginx\n    secrets:\n      - source: tok\n        target: app_tok\n        uid: \"100\"\n        gid: \"101\"\n        mode: 256\nsecrets:\n  tok:\n    external: true\n    name: real_tok\n";
		let file = crate::compose::parse_str_raw(yaml).unwrap();
		let secrets = collect_native_secrets(&file.services["web"], &file).unwrap();
		assert_eq!(secrets.len(), 1);
		let s = &secrets[0];
		assert_eq!(s.source, "real_tok");
		assert_eq!(s.target.as_deref(), Some("app_tok"));
		assert_eq!(s.uid, Some(100));
		assert_eq!(s.gid, Some(101));
		assert_eq!(s.mode, Some(256));
	}

	#[test]
	fn external_config_becomes_native_with_absolute_default_target() {
		// Configs default to an absolute container-root path, matching the
		// bind-mount config behaviour; an absolute target is mounted as-is.
		let yaml = "services:\n  web:\n    image: nginx\n    configs: [cfg]\nconfigs:\n  cfg:\n    external: true\n";
		let file = crate::compose::parse_str_raw(yaml).unwrap();
		let secrets = collect_native_secrets(&file.services["web"], &file).unwrap();
		assert_eq!(secrets.len(), 1);
		assert_eq!(secrets[0].source, "cfg");
		assert_eq!(secrets[0].target.as_deref(), Some("/cfg"));
	}

	#[test]
	fn non_external_secret_is_not_a_native_secret() {
		// A `file:` secret is materialised as a bind mount, not a native secret.
		let yaml = "services:\n  web:\n    image: nginx\n    secrets: [tok]\nsecrets:\n  tok:\n    file: ./tok.txt\n";
		let file = crate::compose::parse_str_raw(yaml).unwrap();
		let secrets = collect_native_secrets(&file.services["web"], &file).unwrap();
		assert!(secrets.is_empty());
	}

	#[test]
	fn non_numeric_uid_drops_to_default() {
		// libpod secret uid/gid are numeric; a user/group name has no native
		// equivalent here and falls back to the default rather than erroring.
		let yaml = "services:\n  web:\n    image: nginx\n    secrets:\n      - source: tok\n        uid: appuser\nsecrets:\n  tok:\n    external: true\n";
		let file = crate::compose::parse_str_raw(yaml).unwrap();
		let secrets = collect_native_secrets(&file.services["web"], &file).unwrap();
		assert_eq!(secrets.len(), 1);
		assert!(secrets[0].uid.is_none());
	}

	#[test]
	fn native_secret_rejects_setuid_mode() {
		// A hostile compose file requesting setuid on an external secret must be
		// refused before the spec reaches Podman — the same guard the bind-mount
		// path enforces. 0o4000 is setuid.
		let yaml = "services:\n  web:\n    image: nginx\n    secrets:\n      - source: tok\n        mode: 2048\nsecrets:\n  tok:\n    external: true\n";
		let file = crate::compose::parse_str_raw(yaml).unwrap();
		assert!(collect_native_secrets(&file.services["web"], &file).is_err());
	}

	#[test]
	fn native_secret_rejects_execute_mode() {
		// 0o777 (= 511) sets execute bits; a secret holds data, never code.
		let yaml = "services:\n  web:\n    image: nginx\n    secrets:\n      - source: tok\n        mode: 511\nsecrets:\n  tok:\n    external: true\n";
		let file = crate::compose::parse_str_raw(yaml).unwrap();
		assert!(collect_native_secrets(&file.services["web"], &file).is_err());
	}

	#[test]
	fn native_config_rejects_setgid_mode() {
		// External configs share the same mode guard. 0o2000 (= 1024) is setgid.
		let yaml = "services:\n  web:\n    image: nginx\n    configs:\n      - source: cfg\n        mode: 1024\nconfigs:\n  cfg:\n    external: true\n";
		let file = crate::compose::parse_str_raw(yaml).unwrap();
		assert!(collect_native_secrets(&file.services["web"], &file).is_err());
	}

	#[test]
	fn native_secret_allows_world_readable_mode() {
		// 0o444 (= 292, world-readable) is the Podman/compose default for a
		// native secret materialised inside the container — it must be allowed,
		// unlike the shared-host bind-mount path which rejects o+r.
		let yaml = "services:\n  web:\n    image: nginx\n    secrets:\n      - source: tok\n        mode: 292\nsecrets:\n  tok:\n    external: true\n";
		let file = crate::compose::parse_str_raw(yaml).unwrap();
		let secrets = collect_native_secrets(&file.services["web"], &file).unwrap();
		assert_eq!(secrets[0].mode, Some(0o444));
	}
}