monosecret 0.4.4

Declarative secrets, every environment, any provider
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
//! Snapshot tests for the `monosecret` CLI using `insta-cmd`.
//!
//! These tests run the compiled `monosecret` binary and snapshot stdout, stderr,
//! and exit code. Dynamic values (temp directory paths) are redacted with filters
//! so snapshots are stable across machines.
//!
//! To review pending snapshot changes:
//!   devenv shell snapshot:review
//! To accept all pending snapshots without review:
//!   devenv shell snapshot:accept

use std::fs;
use std::process::Command;

use insta_cmd::assert_cmd_snapshot;

fn bin() -> &'static str {
	env!("CARGO_BIN_EXE_monosecret")
}

/// A deterministic per-test HOME so `assert_cmd_snapshot!`'s `env:` section
/// (which insta filters do not touch) is stable across machines and runs.
/// The directory is wiped first so the audit-log first-run note always prints.
fn test_home(name: &str) -> std::path::PathBuf {
	let home = std::path::PathBuf::from(format!("/tmp/monosecret-cli-snapshots-{name}"));
	let _ = fs::remove_dir_all(&home);
	home
}

/// Returns a set of insta filters that redact dynamic temp directory paths so
/// snapshots are portable across machines.
fn snapshot_settings() -> insta::Settings {
	let mut settings = insta::Settings::clone_current();
	// macOS temp dirs: /private/var/folders/xx/xxxx/T/...
	settings.add_filter(r"/private/var/folders/\S+", "[TMPDIR]");
	settings.add_filter(r"/var/folders/\S+", "[TMPDIR]");
	// Linux temp dirs: /tmp/xxx...
	settings.add_filter(r"/tmp/\S+", "[TMPDIR]");
	settings.add_filter(r"/home/runner/work/_temp/\S+", "[TMPDIR]");
	// Windows temp dirs (paths are forward-slash normalised in test configs).
	settings.add_filter(r"[A-Z]:/Users/\S+/AppData/Local/Temp/\S+", "[TMPDIR]");
	settings.add_filter(r"[A-Z]:/a/_temp/\S+", "[TMPDIR]");
	// The audit first-run note is platform-dependent (prints on Unix where
	// HOME is honoured, absent on Windows where etcetera uses USERPROFILE).
	settings.add_filter(r"note: \S+ is now recording \S+ access to \S+[^\n]*\n", "");

	// Strip .exe suffix on Windows so binary names match Unix snapshots.
	settings.add_filter(r"monosecret\.exe", "monosecret");
	// dotenv:// URIs containing redacted temp paths.
	settings.add_filter(r"dotenv://\[TMPDIR\].*", "dotenv://[TMPDIR]...");
	settings
}

/// Convert backslashes to forward slashes so Windows paths interpolated into
/// TOML double-quoted strings are not interpreted as escape sequences.
fn forward_slashes(p: &std::path::Path) -> String {
	p.display().to_string().replace('\\', "/")
}

fn write_config(dir: &std::path::Path, toml: &str) {
	fs::write(dir.join("monosecret.toml"), toml).unwrap();
}

/// Snapshot `monosecret --help` — verifies the top-level help text is stable.
#[test]
fn help_output() {
	let mut cmd = Command::new(bin());
	cmd.arg("--help");
	snapshot_settings().bind(|| {
		assert_cmd_snapshot!(cmd);
	});
}

#[test]
fn schema_and_check_json_are_value_free() {
	let dir = tempfile::tempdir().unwrap();
	write_config(
		dir.path(),
		r#"
[project]
name = "demo"
revision = "1.0"

[profiles.default]
TOKEN = { description = "token", required = true }
OPTIONAL = { description = "optional", required = false }
"#,
	);
	let env_path = dir.path().join("values.env");
	fs::write(&env_path, "TOKEN=super-secret-value\n").unwrap();

	let schema = Command::new(bin())
		.args([
			"--file",
			dir.path().join("monosecret.toml").to_str().unwrap(),
			"schema",
		])
		.output()
		.unwrap();
	assert!(schema.status.success());
	let schema: serde_json::Value = serde_json::from_slice(&schema.stdout).unwrap();
	assert_eq!(
		schema.get("title").and_then(serde_json::Value::as_str),
		Some("Monosecret")
	);

	let report = Command::new(bin())
		.args([
			"--file",
			dir.path().join("monosecret.toml").to_str().unwrap(),
			"check",
			"--json",
			"--provider",
			&format!("dotenv:{}", env_path.display()),
		])
		.env("HOME", dir.path())
		.output()
		.unwrap();
	assert!(
		report.status.success(),
		"{}",
		String::from_utf8_lossy(&report.stderr)
	);
	let report_text = String::from_utf8(report.stdout).unwrap();
	assert!(!report_text.contains("super-secret-value"));
	let report: serde_json::Value = serde_json::from_str(&report_text).unwrap();
	let entries = report
		.get("secrets")
		.and_then(serde_json::Value::as_array)
		.expect("report secrets array");
	assert_eq!(
		entries
			.first()
			.and_then(|entry| entry.get("name"))
			.and_then(serde_json::Value::as_str),
		Some("OPTIONAL")
	);
	assert_eq!(
		entries
			.get(1)
			.and_then(|entry| entry.get("name"))
			.and_then(serde_json::Value::as_str),
		Some("TOKEN")
	);
}

/// Snapshot `monosecret manifest --format json` — verifies the secret-value-free
/// manifest output is stable.
#[test]
fn manifest_json() {
	let dir = tempfile::tempdir().unwrap();
	write_config(
		dir.path(),
		r#"
[project]
name = "demo"
revision = "1.0"

[providers]
local = "dotenv://.env"

[profiles.default]
DATABASE_URL = { description = "PostgreSQL connection string", required = true }
API_TOKEN = { description = "API authentication token", required = true, generate = true, type = "password" }
OPTIONAL = { description = "Optional config", required = false, default = "not-a-secret" }
"#,
	);

	let mut cmd = Command::new(bin());
	cmd.env("HOME", test_home("manifest-json"))
		.current_dir(dir.path())
		.args(["manifest", "--format", "json"]);
	snapshot_settings().bind(|| {
		assert_cmd_snapshot!(cmd);
	});
}

/// Snapshot `monosecret env --shell dotenv` — verifies dotenv output format.
#[test]
fn env_dotenv() {
	let dir = tempfile::tempdir().unwrap();
	let dotenv = dir.path().join(".env");
	fs::write(
		&dotenv,
		"DATABASE_URL=postgres://localhost\nAPI_TOKEN=secret\n",
	)
	.unwrap();

	write_config(
		dir.path(),
		&format!(
			r#"
[project]
name = "demo"
revision = "1.0"

[providers]
local = "dotenv://{}"

[profiles.default]
DATABASE_URL = {{ description = "Database URL", required = true, providers = ["local"] }}
API_TOKEN = {{ description = "API token", required = true, providers = ["local"] }}
"#,
			forward_slashes(&dotenv)
		),
	);

	let mut cmd = Command::new(bin());
	cmd.env("HOME", test_home("env-dotenv"));
	cmd.current_dir(dir.path()).args([
		"--reason",
		"test",
		"env",
		"--shell",
		"dotenv",
		"--provider",
		"local",
	]);
	snapshot_settings().bind(|| {
		assert_cmd_snapshot!(cmd);
	});
}

/// Snapshot `monosecret env --shell bash` — verifies bash export format.
#[test]
fn env_bash() {
	let dir = tempfile::tempdir().unwrap();
	let dotenv = dir.path().join(".env");
	fs::write(
		&dotenv,
		"DATABASE_URL=postgres://localhost\nAPI_TOKEN=secret\n",
	)
	.unwrap();

	write_config(
		dir.path(),
		&format!(
			r#"
[project]
name = "demo"
revision = "1.0"

[providers]
local = "dotenv://{}"

[profiles.default]
DATABASE_URL = {{ description = "Database URL", required = true, providers = ["local"] }}
API_TOKEN = {{ description = "API token", required = true, providers = ["local"] }}
"#,
			forward_slashes(&dotenv)
		),
	);

	let mut cmd = Command::new(bin());
	cmd.env("HOME", test_home("env-bash"));
	cmd.current_dir(dir.path()).args([
		"--reason",
		"test",
		"env",
		"--shell",
		"bash",
		"--provider",
		"local",
	]);
	snapshot_settings().bind(|| {
		assert_cmd_snapshot!(cmd);
	});
}

/// Snapshot `monosecret check --no-prompt` with all required secrets present —
/// verifies the check summary output.
#[test]
fn check_all_present() {
	let dir = tempfile::tempdir().unwrap();
	let dotenv = dir.path().join(".env");
	fs::write(
		&dotenv,
		"DATABASE_URL=postgres://localhost\nAPI_TOKEN=secret\n",
	)
	.unwrap();

	write_config(
		dir.path(),
		&format!(
			r#"
[project]
name = "demo"
revision = "1.0"

[providers]
local = "dotenv://{}"

[profiles.default]
DATABASE_URL = {{ description = "Database URL", required = true, providers = ["local"] }}
API_TOKEN = {{ description = "API token", required = true, providers = ["local"] }}
"#,
			forward_slashes(&dotenv)
		),
	);

	let mut cmd = Command::new(bin());
	cmd.env("HOME", test_home("check-all-present"));
	cmd.current_dir(dir.path()).args([
		"--reason",
		"test",
		"check",
		"--no-prompt",
		"--provider",
		"local",
	]);
	snapshot_settings().bind(|| {
		assert_cmd_snapshot!(cmd);
	});
}

/// Snapshot `monosecret check --no-prompt` with a missing required secret —
/// verifies the error output and non-zero exit code.
#[test]
fn check_missing_required() {
	let dir = tempfile::tempdir().unwrap();
	let dotenv = dir.path().join(".env");
	fs::write(&dotenv, "").unwrap();

	write_config(
		dir.path(),
		&format!(
			r#"
[project]
name = "demo"
revision = "1.0"

[providers]
local = "dotenv://{}"

[profiles.default]
API_TOKEN = {{ description = "API token", required = true, providers = ["local"] }}
"#,
			forward_slashes(&dotenv)
		),
	);

	let mut cmd = Command::new(bin());
	cmd.env("HOME", test_home("check-missing-required"));
	cmd.current_dir(dir.path()).args([
		"--reason",
		"test",
		"check",
		"--no-prompt",
		"--provider",
		"local",
	]);
	snapshot_settings().bind(|| {
		assert_cmd_snapshot!(cmd);
	});
}

/// Snapshot `monosecret get` for a secret with a value in the provider.
#[test]
fn get_secret_value() {
	let dir = tempfile::tempdir().unwrap();
	let dotenv = dir.path().join(".env");
	fs::write(&dotenv, "API_TOKEN=my-secret-token\n").unwrap();

	write_config(
		dir.path(),
		&format!(
			r#"
[project]
name = "demo"
revision = "1.0"

[providers]
local = "dotenv://{}"

[profiles.default]
API_TOKEN = {{ description = "API token", required = true, providers = ["local"] }}
"#,
			forward_slashes(&dotenv)
		),
	);

	let mut cmd = Command::new(bin());
	cmd.env("HOME", test_home("get-secret-value"));
	cmd.current_dir(dir.path()).args([
		"--reason",
		"test",
		"get",
		"API_TOKEN",
		"--provider",
		"local",
	]);
	snapshot_settings().bind(|| {
		assert_cmd_snapshot!(cmd);
	});
}

/// Snapshot `monosecret get` for a missing secret that has a default value.
#[test]
fn get_secret_with_default() {
	let dir = tempfile::tempdir().unwrap();
	let dotenv = dir.path().join(".env");
	fs::write(&dotenv, "").unwrap();

	write_config(
		dir.path(),
		&format!(
			r#"
[project]
name = "demo"
revision = "1.0"

[providers]
local = "dotenv://{}"

[profiles.default]
DATABASE_URL = {{ description = "Database URL", required = false, default = "postgres://localhost/dev", providers = ["local"] }}
"#,
			forward_slashes(&dotenv)
		),
	);

	let mut cmd = Command::new(bin());
	cmd.env("HOME", test_home("get-secret-with-default"));
	cmd.current_dir(dir.path()).args([
		"--reason",
		"test",
		"get",
		"DATABASE_URL",
		"--provider",
		"local",
	]);
	snapshot_settings().bind(|| {
		assert_cmd_snapshot!(cmd);
	});
}