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
use std::collections::HashMap;
use std::process::Command;
use std::sync::Mutex;

use serde::Deserialize;
use serde::Serialize;

use crate::MonosecretError;
use crate::Result;
use crate::SecretBytes;
use crate::provider::Address;
use crate::provider::Provider;
use crate::provider::ProviderUrl;
use crate::provider::credential_env_value;
use crate::provider::onepassword::strip_op_session_env;

/// Configuration for the `OnePassword` Environments provider.
///
/// This provider uses `op environment read` to fetch secrets from a
/// [1Password Environment](https://www.1password.dev/environments).
/// Secrets are returned as `KEY=value` lines and parsed directly —
/// no JSON parsing or section/field navigation is needed.
///
/// # URI Schemes
///
/// | Scheme | Auth | Example |
/// |--------|------|---------|
/// | `onepassword+env` | Desktop app | `onepassword+env://blgexucrwfr2dtsxe2q4uu7dp4` |
/// | `onepassword+env` | Desktop app + account | `onepassword+env://work@blgexucrwfr2dtsxe2q4uu7dp4` |
/// | `onepassword+env+token` | Service account token (in URL) | `onepassword+env+token://ops_token@env-id` |
/// | `onepassword+env+token` | Service account token (from env var) | `onepassword+env+token://env-id` |
///
/// # Example
///
/// ```toml
/// [providers]
/// prod-env = "onepassword+env://blgexucrwfr2dtsxe2q4uu7dp4"
/// ci-env   = "onepassword+env+token://ops_abc123@xyz789"
/// ```
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct OnePasswordEnvConfig {
	/// Optional account shorthand (for multiple accounts).
	pub account: Option<String>,
	/// The 1Password Environment ID (UUID).
	pub environment_id: String,
	/// Optional service account token for automated auth.
	#[serde(skip)]
	pub service_account_token: Option<String>,
}

impl TryFrom<&ProviderUrl> for OnePasswordEnvConfig {
	type Error = MonosecretError;

	fn try_from(url: &ProviderUrl) -> std::result::Result<Self, Self::Error> {
		let scheme = url.scheme();

		match scheme {
			"onepassword+env" | "onepassword+env+token" => {}
			_ => {
				return Err(MonosecretError::ProviderOperationFailed(format!(
					"Invalid scheme '{scheme}' for OnePassword Environments provider. \
                     Use 'onepassword+env' or 'onepassword+env+token'"
				)));
			}
		}

		let is_token_scheme = scheme == "onepassword+env+token";
		let mut config = Self::default();

		// The host is the environment ID.
		if let Some(host) = url.host()
			&& host != "localhost"
		{
			config.environment_id.clone_from(&host);
		}

		if config.environment_id.is_empty() {
			// If no host, the path might contain the environment ID.
			let path = url.path();
			let path = path.trim_start_matches('/');

			if !path.is_empty() {
				config.environment_id = path.to_string();
			}
		}

		if config.environment_id.is_empty() {
			return Err(MonosecretError::ProviderOperationFailed(
				"OnePassword Environments provider requires an environment ID in the URI \
                 (e.g. onepassword+env://blgexucrwfr2dtsxe2q4uu7dp4)"
					.into(),
			));
		}

		// Parse username (account or token) and optional password.
		let username = url.username();

		if !username.is_empty() {
			if is_token_scheme {
				if let Some(password) = url.password() {
					config.service_account_token = Some(password);
				} else {
					config.service_account_token = Some(username.clone());
				}
			} else {
				config.account = Some(username.clone());
			}
		}

		Ok(config)
	}
}

crate::register_provider! {
	struct: OnePasswordEnvProvider,
	config: OnePasswordEnvConfig,
	name: "onepassword-env",
	description: "1Password Environments (beta) — key-value secrets via op environment read",
	schemes: ["onepassword+env", "onepassword+env+token"],
	examples: [
		"onepassword+env://blgexucrwfr2dtsxe2q4uu7dp4",
		"onepassword+env://work@blgexucrwfr2dtsxe2q4uu7dp4",
		"onepassword+env+token://ops_abc123@xyz789",
	],
}

/// Provider for 1Password Environments.
///
/// Calls `op environment read <id>` and parses the `KEY=value` output.
///
/// **Caching:** 1Password Environments always returns all variables at once
/// (there is no per-key CLI command). To avoid calling `op` for every
/// `get()` / `get_batch()`, the provider caches the full environment on
/// first access and reuses it for the lifetime of the provider instance.
/// Supports both desktop app auth and service account tokens.
pub struct OnePasswordEnvProvider {
	config: OnePasswordEnvConfig,
	op_command: String,
	/// Provider-local dependency secrets that are passed to the `op` child process.
	/// Interior mutability because delivery is post-construction and the
	/// factory shares the provider as an `Arc` when it registers an auth
	/// preflight (a `&mut self` hook cannot be forwarded through the blanket
	/// `impl Provider for Arc<T>`).
	dependency_env: Mutex<HashMap<String, SecretBytes>>,
	/// Lazy cache of all environment variables, populated on first access.
	cache: Mutex<Option<HashMap<String, SecretBytes>>>,
}

impl OnePasswordEnvProvider {
	pub fn new(config: OnePasswordEnvConfig) -> Self {
		let op_command = std::env::var("MONOSECRET_OPCLI_PATH")
			.or_else(|_| std::env::var("SECRETSPEC_OPCLI_PATH"))
			.unwrap_or_else(|_| "op".to_string());
		Self {
			config,
			op_command,
			dependency_env: Mutex::new(HashMap::new()),
			cache: Mutex::new(None),
		}
	}

	/// Returns the cached variables, populating the cache on first call.
	fn cached_variables(
		&self,
	) -> Result<std::sync::MutexGuard<'_, Option<HashMap<String, SecretBytes>>>> {
		let mut guard = self
			.cache
			.lock()
			.map_err(|e| MonosecretError::ProviderOperationFailed(e.to_string()))?;

		if guard.is_none() {
			let output = self.fetch_environment()?;
			let mut vars = HashMap::new();

			for line in output.lines() {
				if let Some((k, v)) = line.split_once('=') {
					vars.insert(k.to_string(), SecretBytes::from_utf8(v.to_string()));
				}
			}
			*guard = Some(vars);
		}

		Ok(guard)
	}

	/// Runs `op environment read <id>` and returns the raw stdout.
	///
	/// Called once by [`cached_variables`]; subsequent lookups use the cache.
	fn fetch_environment(&self) -> Result<String> {
		let mut cmd = Command::new(&self.op_command);
		strip_op_session_env(&mut cmd);

		if let Some(ref token) = self.config.service_account_token {
			cmd.env("OP_SERVICE_ACCOUNT_TOKEN", token);
		} else if let Some(token) = self
			.dependency_env
			.lock()
			.map_err(|error| {
				MonosecretError::ProviderOperationFailed(format!(
					"provider dependency delivery failed: {error}"
				))
			})?
			.get("OP_SERVICE_ACCOUNT_TOKEN")
			.cloned()
		{
			cmd.env("OP_SERVICE_ACCOUNT_TOKEN", credential_env_value(&token)?);
		}

		if let Some(ref account) = self.config.account {
			cmd.arg("--account").arg(account);
		}

		cmd.arg("environment")
			.arg("read")
			.arg(&self.config.environment_id);

		let output = cmd.output().map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                MonosecretError::ProviderOperationFailed(
                    "The 'op' CLI is not installed. Install it from https://1password.com/downloads/command-line".into(),
                )
            } else {
                MonosecretError::Io(e)
            }
        })?;

		if !output.status.success() {
			let stderr = String::from_utf8_lossy(&output.stderr);
			let msg = stderr.trim().to_string();

			if msg.contains("isn't an environment")
				|| msg.contains("not found")
				|| msg.contains("doesn't exist")
			{
				return Err(MonosecretError::ProviderOperationFailed(format!(
					"1Password environment '{}' not found",
					self.config.environment_id
				)));
			}

			if msg.contains("not currently signed in") || msg.contains("no active session") {
				return Err(MonosecretError::ProviderOperationFailed(
                    "Not signed in to 1Password. Sign in via the desktop app or set a service account token.".into(),
                ));
			}

			return Err(MonosecretError::ProviderOperationFailed(msg));
		}

		String::from_utf8(output.stdout)
			.map_err(|e| MonosecretError::ProviderOperationFailed(e.to_string()))
	}
}

impl Provider for OnePasswordEnvProvider {
	fn configure_dependency_secrets(&self, dependencies: &[(String, SecretBytes)]) -> Result<()> {
		let mut env = self.dependency_env.lock().map_err(|error| {
			MonosecretError::ProviderOperationFailed(format!(
				"provider dependency delivery failed: {error}"
			))
		})?;

		for (name, value) in dependencies {
			if name == "OP_SERVICE_ACCOUNT_TOKEN" {
				env.insert(name.clone(), value.clone());
			}
		}

		Ok(())
	}

	fn convention_address(
		&self,
		_project: &str,
		_profile: &str,
		key: &str,
	) -> Result<crate::config::NativeAddress> {
		Ok(crate::config::NativeAddress {
			item: key.to_string(),
			field: None,
			vault: None,
			section: None,
			version: None,
		})
	}

	fn get(&self, addr: Address<'_>) -> Result<Option<SecretBytes>> {
		let key = match addr {
			Address::Convention { key, .. } => key,
			Address::Native(reference) => reference.field.as_deref().unwrap_or(&reference.item),
		};

		let guard = self.cached_variables()?;
		let vars = guard.as_ref().expect("cache was just populated");
		Ok(vars.get(key).cloned())
	}

	fn set(&self, addr: Address<'_>, _value: &SecretBytes) -> Result<()> {
		self.check_writable(addr)
	}

	fn check_writable(&self, _addr: Address<'_>) -> Result<()> {
		Err(MonosecretError::ProviderOperationFailed(
			"1Password Environments provider is read-only. Manage environment variables in the 1Password desktop app (Developer > View Environments).".into(),
		))
	}

	fn name(&self) -> &str {
		Self::PROVIDER_NAME
	}

	fn uri(&self) -> String {
		let mut uri = if self.config.service_account_token.is_some() {
			"onepassword+env+token://".to_string()
		} else {
			"onepassword+env://".to_string()
		};

		if let Some(ref account) = self.config.account {
			uri.push_str(&ProviderUrl::encode(account));
			uri.push('@');
		}

		uri.push_str(&self.config.environment_id);
		uri
	}
}

#[cfg(all(test, unix))]
mod dependency_env_tests {
	use std::fs;
	use std::os::unix::fs::PermissionsExt;

	use super::*;

	fn write_op_env_stub(script: &std::path::Path, log: &std::path::Path) {
		let script_body = format!(
			r#"#!/bin/sh
printf '%s\n' "$OP_SERVICE_ACCOUNT_TOKEN" >> '{}'
printf 'API_KEY=%s\nOTHER=value\n' "$OP_SERVICE_ACCOUNT_TOKEN"
"#,
			log.display()
		);
		fs::write(script, script_body).unwrap();
		let mut permissions = fs::metadata(script).unwrap().permissions();
		permissions.set_mode(0o755);
		fs::set_permissions(script, permissions).unwrap();
	}

	#[test]
	fn dependency_token_is_command_scoped_when_fetching_environment() {
		let temp = tempfile::TempDir::new().unwrap();
		let script = temp.path().join("op-env-stub");
		let log = temp.path().join("calls.log");
		write_op_env_stub(&script, &log);

		let mut provider = OnePasswordEnvProvider::new(OnePasswordEnvConfig {
			environment_id: "env-id".into(),
			service_account_token: None,
			..OnePasswordEnvConfig::default()
		});
		provider.op_command = script.display().to_string();
		provider
			.configure_dependency_secrets(&[
				("IGNORED".into(), SecretBytes::from("ignored")),
				(
					"OP_SERVICE_ACCOUNT_TOKEN".into(),
					SecretBytes::from("dependency-token"),
				),
			])
			.unwrap();

		let first = provider
			.get(Address::convention("project", "default", "API_KEY"))
			.unwrap()
			.unwrap();
		let second = provider
			.get(Address::convention("project", "default", "API_KEY"))
			.unwrap()
			.unwrap();

		assert_eq!(first.expose_secret(), b"dependency-token");
		assert_eq!(second.expose_secret(), b"dependency-token");
		assert_eq!(fs::read_to_string(log).unwrap(), "dependency-token\n");
	}

	#[test]
	fn explicit_env_uri_token_takes_precedence_over_dependency_token() {
		let temp = tempfile::TempDir::new().unwrap();
		let script = temp.path().join("op-env-stub");
		let log = temp.path().join("calls.log");
		write_op_env_stub(&script, &log);

		let mut provider = OnePasswordEnvProvider::new(OnePasswordEnvConfig {
			environment_id: "env-id".into(),
			service_account_token: Some("uri-token".into()),
			..OnePasswordEnvConfig::default()
		});
		provider.op_command = script.display().to_string();
		provider
			.configure_dependency_secrets(&[(
				"OP_SERVICE_ACCOUNT_TOKEN".into(),
				SecretBytes::from("dependency-token"),
			)])
			.unwrap();

		let value = provider
			.get(Address::convention("project", "default", "API_KEY"))
			.unwrap()
			.unwrap();

		assert_eq!(value.expose_secret(), b"uri-token");
		assert_eq!(fs::read_to_string(log).unwrap(), "uri-token\n");
	}
}