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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
//! Convex deployment environment variable provider.
//!
//! Convex stores environment variables per deployment, so a dev deployment
//! and a production deployment of the same project hold independent values.
//! Monosecret maps a declaration key directly to an environment variable
//! name and selects the deployment through the provider URI, so one alias
//! per deployment keeps profiles isolated. Values are read back in plaintext
//! through the deployment's API.
//!
//! # URI format
//!
//! `convex://DEPLOYMENT`
//!
//! `DEPLOYMENT` is a deployment name like `happy-otter-123` or its full host
//! like `happy-otter-123.eu-west-1.convex.cloud` for deployments outside
//! US-East. Authentication comes from a `token` provider credential,
//! `CONVEX_DEPLOY_KEY`, or `CONVEX_DEPLOYMENT_TOKEN`.

use std::borrow::Cow;
use std::collections::BTreeMap;
use std::collections::HashMap;

use reqwest::header::AUTHORIZATION;
use reqwest::header::HeaderMap;
use serde::Deserialize;
use serde::Serialize;

use super::Address;
use super::DiscoveryContext;
use super::Provider;
use super::ProviderCredentials;
use super::ProviderUrl;
use crate::MonosecretError;
use crate::Result;
use crate::Secret;
use crate::SecretBytes;
use crate::config::NativeAddress;

const TOKEN: &str = "token";
const TOKEN_ENVS: &[&str] = &["CONVEX_DEPLOY_KEY", "CONVEX_DEPLOYMENT_TOKEN"];
const DEPLOYMENT_SUFFIX: &str = ".convex.cloud";
/// Convex caps environment variable names at 256 characters.
const MAX_NAME_LEN: usize = 256;

/// Configuration for one Convex deployment's environment variables.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ConvexConfig {
	/// Convex deployment name or full deployment host.
	pub deployment: String,
}

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

	fn try_from(url: &ProviderUrl) -> std::result::Result<Self, Self::Error> {
		if url.scheme() != "convex" {
			return Err(operation_error(format!(
				"invalid scheme '{}' for convex provider; expected 'convex'",
				url.scheme()
			)));
		}

		if !url.username().is_empty() || url.password().is_some() {
			return Err(operation_error(
				"convex:// does not accept credentials in URI userinfo; use the token provider credential",
			));
		}

		let deployment = url
			.host()
			.filter(|value| !value.is_empty())
			.ok_or_else(|| {
				operation_error(
					"convex provider requires a deployment, for example convex://happy-otter-123",
				)
			})?;
		validate_deployment(&deployment)?;

		if !url.path().trim_matches('/').is_empty() {
			return Err(operation_error(
				"convex:// takes no path; put the deployment name or host in the URI authority",
			));
		}

		if let Some((key, _)) = url.query_pairs().next() {
			return Err(operation_error(format!(
				"unknown convex query parameter '{key}'; convex:// takes no query parameters"
			)));
		}

		Ok(Self { deployment })
	}
}

/// Accepts deployment names (`happy-otter-123`) and full deployment hosts
/// (`happy-otter-123.eu-west-1.convex.cloud`); anything else with a dot is
/// rejected so a mistyped host never silently targets another domain.
fn validate_deployment(deployment: &str) -> Result<()> {
	let valid = if deployment.ends_with(DEPLOYMENT_SUFFIX) {
		!deployment.trim_end_matches(DEPLOYMENT_SUFFIX).is_empty()
			&& deployment.len() <= 253
			&& deployment.split('.').all(is_host_label)
	} else {
		!deployment.contains('.')
			&& (3..=63).contains(&deployment.len())
			&& deployment
				.bytes()
				.all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
	};

	if valid {
		Ok(())
	} else {
		Err(operation_error(
			"Convex deployments are names like happy-otter-123 or full hosts like happy-otter-123.eu-west-1.convex.cloud (lowercase letters, digits, and hyphens)",
		))
	}
}

fn is_host_label(label: &str) -> bool {
	!label.is_empty()
		&& label
			.bytes()
			.all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
}

#[derive(Debug, Deserialize)]
struct ListEnvVars {
	#[serde(default, rename = "environmentVariables")]
	environment_variables: BTreeMap<String, Option<String>>,
}

#[derive(Debug, Serialize)]
struct EnvChange<'a> {
	name: &'a str,
	/// `None` deletes the named variable.
	value: Option<&'a str>,
}

#[derive(Debug, Serialize)]
struct UpdateEnvVars<'a> {
	changes: &'a [EnvChange<'a>],
}

#[derive(Debug, Deserialize)]
struct ApiError {
	#[serde(default)]
	code: Option<String>,
	message: String,
}

/// A Convex deployment environment variable provider.
pub struct ConvexProvider {
	config: ConvexConfig,
	credentials: ProviderCredentials,
	api_base: String,
}

crate::register_provider! {
	struct: ConvexProvider,
	config: ConvexConfig,
	metadata: &super::catalog::CONVEX,
}

impl ConvexProvider {
	pub fn new(config: ConvexConfig) -> Self {
		let host = deployment_host(&config.deployment);
		Self {
			config,
			credentials: ProviderCredentials::new(),
			api_base: format!("https://{host}"),
		}
	}

	fn token(&self) -> Option<SecretBytes> {
		super::credential_or_envs(&self.credentials, TOKEN, TOKEN_ENVS)
	}

	fn auth_headers(&self) -> Result<HeaderMap> {
		let token = self.token().ok_or_else(|| {
			operation_error(format!(
				"Convex auth requires the `{TOKEN}` provider credential, CONVEX_DEPLOY_KEY, or CONVEX_DEPLOYMENT_TOKEN"
			))
		})?;

		let mut headers = HeaderMap::new();
		headers.insert(AUTHORIZATION, convex_header(token.expose_secret())?);
		Ok(headers)
	}

	fn client(&self) -> Result<reqwest::Client> {
		super::http::client_builder()
			.default_headers(self.auth_headers()?)
			// Secret-bearing bodies must remain confined to the deployment's
			// fixed API origin; a redirect response must never choose where a
			// POST body is replayed.
			.redirect(reqwest::redirect::Policy::none())
			.build()
			.map_err(|error| {
				operation_error(format!(
					"failed to build Convex HTTP client: {}",
					crate::error::display_error_chain(&error)
				))
			})
	}

	fn list_path(&self) -> String {
		format!(
			"{}/api/v1/list_environment_variables",
			self.api_base.trim_end_matches('/')
		)
	}

	fn update_path(&self) -> String {
		format!(
			"{}/api/v1/update_environment_variables",
			self.api_base.trim_end_matches('/')
		)
	}

	fn secret_name<'a>(&self, addr: Address<'a>) -> Result<Cow<'a, str>> {
		let name = super::flat_item(self, addr)?;

		if !is_valid_env_key(&name) {
			return Err(operation_error(format!(
				"'{name}' is not a valid Convex environment variable name: names are at most {MAX_NAME_LEN} ASCII letters, digits, or underscores and start with a letter"
			)));
		}

		Ok(name)
	}

	/// Reads the deployment's variables, mapping a `null` value to an absent
	/// entry.
	async fn list_async(&self, client: &reqwest::Client) -> Result<BTreeMap<String, String>> {
		let action = "listing environment variables";
		let response = client
			.get(self.list_path())
			.send()
			.await
			.map_err(|error| reach_error(action, &error))?;

		let status = response.status();
		let body = response
			.bytes()
			.await
			.map_err(|error| reach_error(action, &error))?;
		let page: ListEnvVars = decode_response(status, &body, action)?;

		Ok(page
			.environment_variables
			.into_iter()
			.filter_map(|(name, value)| value.map(|value| (name, value)))
			.collect())
	}

	async fn get_async(&self, client: &reqwest::Client, key: &str) -> Result<Option<SecretBytes>> {
		let variables = self.list_async(client).await?;
		Ok(variables.get(key).map(SecretBytes::from_utf8))
	}

	/// Upserts one variable: the deployment API applies `changes` as a batch,
	/// and a change with a value replaces any existing entry.
	async fn set_async(&self, client: &reqwest::Client, key: &str, value: &str) -> Result<()> {
		let response = client
			.post(self.update_path())
			.json(&UpdateEnvVars {
				changes: &[EnvChange {
					name: key,
					value: Some(value),
				}],
			})
			.send()
			.await
			.map_err(|error| reach_error("updating the environment variable", &error))?;
		check_status(response, "updating the environment variable").await
	}

	/// Removes one variable by sending a `null` value; a name that is already
	/// absent is a successful no-op.
	async fn delete_async(&self, client: &reqwest::Client, key: &str) -> Result<bool> {
		if !self.list_async(client).await?.contains_key(key) {
			return Ok(false);
		}

		let response = client
			.post(self.update_path())
			.json(&UpdateEnvVars {
				changes: &[EnvChange {
					name: key,
					value: None,
				}],
			})
			.send()
			.await
			.map_err(|error| reach_error("deleting the environment variable", &error))?;
		check_status(response, "deleting the environment variable").await?;

		Ok(true)
	}
}

impl Provider for ConvexProvider {
	/// The deployment supplies isolation; convention writes use the Monosecret
	/// key directly as the variable name.
	fn convention_address(
		&self,
		_project: &str,
		_profile: &str,
		key: &str,
	) -> Result<NativeAddress> {
		Ok(NativeAddress {
			item: key.to_string(),
			..Default::default()
		})
	}

	fn with_credentials(&mut self, credentials: ProviderCredentials) {
		self.credentials = credentials;
	}

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

	fn uri(&self) -> String {
		format!("convex://{}", self.config.deployment)
	}

	fn storage_identity(&self) -> String {
		format!("convex://{}", self.config.deployment)
	}

	fn get(&self, addr: Address<'_>) -> Result<Option<SecretBytes>> {
		let name = self.secret_name(addr)?;
		let client = self.client()?;
		super::block_on(self.get_async(&client, &name))
	}

	fn check_writable(&self, addr: Address<'_>) -> Result<()> {
		self.secret_name(addr).map(|_| ())
	}

	fn set(&self, addr: Address<'_>, value: &SecretBytes) -> Result<()> {
		self.check_writable(addr)?;
		let name = self.secret_name(addr)?;
		let value = super::require_utf8("convex", value)?;
		let client = self.client()?;
		super::block_on(self.set_async(&client, &name, value))
	}

	fn supports_delete(&self) -> bool {
		true
	}

	fn check_deletable(&self, addr: Address<'_>) -> Result<()> {
		self.secret_name(addr).map(|_| ())
	}

	fn delete(&self, addr: Address<'_>) -> Result<bool> {
		self.check_deletable(addr)?;
		let name = self.secret_name(addr)?;
		let client = self.client()?;
		super::block_on(self.delete_async(&client, &name))
	}

	fn describe_write_target(&self, addr: Address<'_>) -> Result<String> {
		let name = self.secret_name(addr)?;
		Ok(format!(
			"Convex deployment '{}' environment variable '{}'",
			self.config.deployment, name
		))
	}

	fn reflect(&self, _context: DiscoveryContext<'_>) -> Result<HashMap<String, Secret>> {
		let client = self.client()?;
		let variables = super::block_on(self.list_async(&client))?;

		Ok(variables
			.into_keys()
			.map(|key| {
				let secret = Secret::required(format!("{key} Convex environment variable"));
				(key, secret)
			})
			.collect())
	}
}

/// Turns a URI deployment into the host serving its deployment API: full
/// hosts are used as written, bare names get the default domain.
fn deployment_host(deployment: &str) -> String {
	if deployment.ends_with(DEPLOYMENT_SUFFIX) {
		deployment.to_string()
	} else {
		format!("{deployment}{DEPLOYMENT_SUFFIX}")
	}
}

/// Builds the deployment API's `Convex` scheme authorization header directly
/// from credential bytes, validating the HTTP header syntax without imposing
/// a UTF-8 requirement.
fn convex_header(value: &[u8]) -> Result<reqwest::header::HeaderValue> {
	let mut header_value = b"Convex ".to_vec();
	header_value.extend_from_slice(value);
	let header_value = SecretBytes::from_vec(header_value);
	let mut header = reqwest::header::HeaderValue::from_bytes(header_value.expose_secret())
		.map_err(|_| {
			operation_error(
				"provider credential cannot be represented in an HTTP Authorization header",
			)
		})?;
	header.set_sensitive(true);
	Ok(header)
}

fn is_valid_env_key(name: &str) -> bool {
	!name.is_empty()
		&& name.len() <= MAX_NAME_LEN
		&& name
			.bytes()
			.next()
			.is_some_and(|byte| byte.is_ascii_alphabetic())
		&& name
			.bytes()
			.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
}

async fn check_status(response: reqwest::Response, action: &str) -> Result<()> {
	let status = response.status();
	if status.is_success() {
		return Ok(());
	}

	let body = response
		.bytes()
		.await
		.map_err(|error| reach_error(action, &error))?;
	Err(api_error(status, &body, action))
}

fn decode_response<T: serde::de::DeserializeOwned>(
	status: reqwest::StatusCode,
	body: &[u8],
	action: &str,
) -> Result<T> {
	if !status.is_success() {
		return Err(api_error(status, body, action));
	}

	serde_json::from_slice(body).map_err(|error| {
		operation_error(format!(
			"Convex returned invalid JSON while {action} (HTTP {}): {}",
			status.as_u16(),
			crate::error::display_error_chain(&error)
		))
	})
}

fn api_error(status: reqwest::StatusCode, body: &[u8], action: &str) -> MonosecretError {
	let detail = serde_json::from_slice::<ApiError>(body).map_or_else(
		|_| String::from_utf8_lossy(body).trim().to_string(),
		|error| {
			let code = error
				.code
				.map(|code| format!("{code}: "))
				.unwrap_or_default();
			format!("{code}{}", error.message)
		},
	);
	let detail = detail.chars().take(512).collect::<String>();

	if detail.is_empty() {
		operation_error(format!(
			"Convex returned HTTP {} while {action}",
			status.as_u16()
		))
	} else {
		operation_error(format!(
			"Convex returned HTTP {} while {action}: {detail}",
			status.as_u16()
		))
	}
}

fn reach_error(action: &str, error: &reqwest::Error) -> MonosecretError {
	operation_error(format!(
		"failed to reach Convex while {action}: {}",
		crate::error::display_error_chain(error)
	))
}

fn operation_error(message: impl Into<String>) -> MonosecretError {
	MonosecretError::ProviderOperationFailed(message.into())
}

#[cfg(test)]
#[allow(clippy::indexing_slicing)] // test fixtures: indexing is the assertion
mod tests {
	use std::io::BufRead;
	use std::io::BufReader;
	use std::io::Read;
	use std::io::Write;
	use std::net::SocketAddr;
	use std::net::TcpListener;

	use super::*;

	const DEPLOYMENT: &str = "happy-otter-123";
	const REGIONAL: &str = "happy-otter-123.eu-west-1.convex.cloud";

	#[derive(Debug)]
	struct RecordedRequest {
		line: String,
		headers: HashMap<String, String>,
		body: String,
	}

	fn config(spec: &str) -> ConvexConfig {
		ConvexConfig::try_from(&ProviderUrl::new(url::Url::parse(spec).unwrap())).unwrap()
	}

	fn provider_with_token(endpoint: SocketAddr, spec: &str) -> ConvexProvider {
		let mut provider = ConvexProvider::new(config(spec));
		provider.api_base = format!("http://{endpoint}");
		provider.with_credentials(ProviderCredentials::from([(
			TOKEN.to_string(),
			SecretBytes::from_utf8("prod:happy-otter-123|test-key"),
		)]));
		provider
	}

	fn response_server(
		responses: Vec<(&'static str, &'static str)>,
	) -> (SocketAddr, std::thread::JoinHandle<Vec<RecordedRequest>>) {
		let listener = TcpListener::bind("127.0.0.1:0").unwrap();
		let endpoint = listener.local_addr().unwrap();
		let server = std::thread::spawn(move || {
			let mut recorded = Vec::new();

			for (status, body) in responses {
				let (mut stream, _) = listener.accept().unwrap();
				let mut reader = BufReader::new(&mut stream);
				let mut line = String::new();
				reader.read_line(&mut line).unwrap();
				let mut headers = HashMap::new();

				loop {
					let mut header = String::new();
					reader.read_line(&mut header).unwrap();

					if header == "\r\n" || header.is_empty() {
						break;
					}

					if let Some((name, value)) = header.trim_end().split_once(':') {
						headers.insert(name.to_ascii_lowercase(), value.trim().to_string());
					}
				}

				let content_length = headers
					.get("content-length")
					.and_then(|value| value.parse::<usize>().ok())
					.unwrap_or(0);
				let mut request_body = vec![0; content_length];
				reader.read_exact(&mut request_body).unwrap();
				recorded.push(RecordedRequest {
					line: line.trim_end().to_string(),
					headers,
					body: String::from_utf8(request_body).unwrap(),
				});
				write!(
                    stream,
                    "HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
                    body.len()
                )
				.unwrap();
			}

			recorded
		});
		(endpoint, server)
	}

	#[test]
	fn parses_and_round_trips_configuration() {
		let provider = ConvexProvider::new(config(&format!("convex://{DEPLOYMENT}")));
		assert_eq!(provider.uri(), format!("convex://{DEPLOYMENT}"));
		assert_eq!(config(&provider.uri()), provider.config);
		assert_eq!(
			provider.api_base,
			format!("https://{DEPLOYMENT}.convex.cloud")
		);

		let regional = ConvexProvider::new(config(&format!("convex://{REGIONAL}")));
		assert_eq!(regional.uri(), format!("convex://{REGIONAL}"));
		assert_eq!(regional.api_base, format!("https://{REGIONAL}"));
	}

	#[test]
	fn rejects_invalid_configuration() {
		for spec in [
			"convex://",
			"convex:///path",
			"convex://an",
			"convex://has%20space",
			"convex://UPPER",
			"convex://happy-otter-123/path",
			"convex://happy-otter-123?team=1",
			"convex://happy-otter-123.example.com",
		] {
			assert!(
				ConvexConfig::try_from(&ProviderUrl::new(url::Url::parse(spec).unwrap())).is_err(),
				"{spec}"
			);
		}
	}

	#[test]
	fn registration_declares_read_delete_and_credentials() {
		let registration = crate::provider::PROVIDER_REGISTRY
			.iter()
			.find(|registration| registration.metadata.info.name == "convex")
			.unwrap();
		assert_eq!(registration.metadata.credential_names, &[TOKEN]);
		assert!(registration.metadata.reads);
		assert!(registration.metadata.deletes);
	}

	#[test]
	fn missing_token_names_the_credential_and_environment_variables() {
		let _lock = crate::tests::scrub_resolution_env();
		let provider = ConvexProvider::new(config(&format!("convex://{DEPLOYMENT}")));
		let error = provider
			.get(Address::convention("p", "production", "KEY"))
			.unwrap_err();
		assert!(
			error.to_string().contains(
				"the `token` provider credential, CONVEX_DEPLOY_KEY, or CONVEX_DEPLOYMENT_TOKEN"
			),
			"{error}"
		);
	}

	#[test]
	fn environment_token_is_used_when_no_credential_is_configured() {
		let _lock = crate::tests::scrub_resolution_env();
		let _fallback = crate::tests::EnvVarGuard::remove("CONVEX_DEPLOYMENT_TOKEN");
		let _token = crate::tests::EnvVarGuard::set("CONVEX_DEPLOY_KEY", "env-key");
		let empty = Box::leak(r#"{"environmentVariables":{}}"#.to_string().into_boxed_str());
		let (endpoint, server) = response_server(vec![("200 OK", empty)]);
		let mut provider = ConvexProvider::new(config(&format!("convex://{DEPLOYMENT}")));
		provider.api_base = format!("http://{endpoint}");

		assert_eq!(
			provider
				.get(Address::convention("p", "production", "KEY"))
				.unwrap(),
			None
		);
		let requests = server.join().unwrap();
		assert_eq!(
			requests[0].headers.get("authorization").map(String::as_str),
			Some("Convex env-key")
		);
	}

	#[test]
	fn second_environment_variable_is_the_fallback() {
		let _lock = crate::tests::scrub_resolution_env();
		let _first = crate::tests::EnvVarGuard::remove("CONVEX_DEPLOY_KEY");
		let _second = crate::tests::EnvVarGuard::set("CONVEX_DEPLOYMENT_TOKEN", "deployment-token");
		let empty = Box::leak(r#"{"environmentVariables":{}}"#.to_string().into_boxed_str());
		let (endpoint, server) = response_server(vec![("200 OK", empty)]);
		let mut provider = ConvexProvider::new(config(&format!("convex://{DEPLOYMENT}")));
		provider.api_base = format!("http://{endpoint}");

		assert_eq!(
			provider
				.get(Address::convention("p", "production", "KEY"))
				.unwrap(),
			None
		);
		let requests = server.join().unwrap();
		assert_eq!(
			requests[0].headers.get("authorization").map(String::as_str),
			Some("Convex deployment-token")
		);
	}

	#[test]
	fn invalid_credential_bytes_are_rejected_without_leaking() {
		let _lock = crate::tests::scrub_resolution_env();
		let mut provider = ConvexProvider::new(config(&format!("convex://{DEPLOYMENT}")));
		provider.with_credentials(ProviderCredentials::from([(
			TOKEN.into(),
			SecretBytes::from_slice(b"key\r\n"),
		)]));
		let error = provider
			.get(Address::convention("p", "production", "KEY"))
			.unwrap_err();
		assert!(
			error.to_string().contains("HTTP Authorization header"),
			"{error}"
		);
		assert!(!format!("{error:?}: {error}").contains("key\r\n"));
	}

	#[test]
	fn convention_is_flat_and_names_are_validated() {
		let provider = ConvexProvider::new(config(&format!("convex://{DEPLOYMENT}")));
		let address = provider
			.convention_address("project", "production", "DATABASE_URL")
			.unwrap();
		assert_eq!(address.item, "DATABASE_URL");

		// Convex names must start with a letter, unlike Netlify or Vercel.
		for key in [
			"",
			"1STARTS_WITH_DIGIT",
			"_LEADING",
			"HAS SPACE",
			"HAS-DASH",
		] {
			assert!(
				provider
					.check_writable(Address::convention("project", "production", key))
					.is_err(),
				"{key}"
			);
		}
	}

	#[test]
	fn reads_the_deployment_value() {
		let body = Box::leak(
			r#"{"environmentVariables":{"API_KEY":"prod-value","OTHER":"untouched"}}"#
				.to_string()
				.into_boxed_str(),
		);
		let (endpoint, server) = response_server(vec![("200 OK", body)]);
		let provider = provider_with_token(endpoint, &format!("convex://{DEPLOYMENT}"));

		let value = provider
			.get(Address::convention("p", "production", "API_KEY"))
			.unwrap()
			.unwrap();
		assert_eq!(value.expose_secret(), b"prod-value");

		let requests = server.join().unwrap();
		assert!(
			requests[0]
				.line
				.starts_with("GET /api/v1/list_environment_variables"),
			"{}",
			requests[0].line
		);
	}

	#[test]
	fn missing_and_null_values_resolve_to_none() {
		for body in [
			r#"{"environmentVariables":{"OTHER":"value"}}"#,
			r#"{"environmentVariables":{"API_KEY":null}}"#,
		] {
			let body = Box::leak(body.to_string().into_boxed_str());
			let (endpoint, server) = response_server(vec![("200 OK", body)]);
			let provider = provider_with_token(endpoint, &format!("convex://{DEPLOYMENT}"));
			assert_eq!(
				provider
					.get(Address::convention("p", "production", "API_KEY"))
					.unwrap(),
				None,
				"{body}"
			);
			server.join().unwrap();
		}
	}

	#[test]
	fn writes_a_change_with_a_value() {
		let (endpoint, server) = response_server(vec![("200 OK", "")]);
		let provider = provider_with_token(endpoint, &format!("convex://{DEPLOYMENT}"));

		provider
			.set(
				Address::convention("p", "production", "API_KEY"),
				&SecretBytes::from_utf8("new-value"),
			)
			.unwrap();

		let requests = server.join().unwrap();
		assert!(
			requests[0]
				.line
				.starts_with("POST /api/v1/update_environment_variables"),
			"{}",
			requests[0].line
		);
		let body: serde_json::Value = serde_json::from_str(&requests[0].body).unwrap();
		assert_eq!(
			body,
			serde_json::json!({"changes": [{"name": "API_KEY", "value": "new-value"}]})
		);
	}

	#[test]
	fn deletes_a_variable_with_a_null_value() {
		let listed = Box::leak(
			r#"{"environmentVariables":{"API_KEY":"value","OTHER":"kept"}}"#
				.to_string()
				.into_boxed_str(),
		);
		let (endpoint, server) = response_server(vec![("200 OK", listed), ("200 OK", "")]);
		let provider = provider_with_token(endpoint, &format!("convex://{DEPLOYMENT}"));

		assert!(
			provider
				.delete(Address::convention("p", "production", "API_KEY"))
				.unwrap()
		);

		let requests = server.join().unwrap();
		assert!(
			requests[1]
				.line
				.starts_with("POST /api/v1/update_environment_variables"),
			"{}",
			requests[1].line
		);
		let body: serde_json::Value = serde_json::from_str(&requests[1].body).unwrap();
		assert_eq!(
			body,
			serde_json::json!({"changes": [{"name": "API_KEY", "value": null}]})
		);
	}

	#[test]
	fn deleting_a_missing_variable_is_idempotent() {
		let listed = Box::leak(r#"{"environmentVariables":{}}"#.to_string().into_boxed_str());
		let (endpoint, server) = response_server(vec![("200 OK", listed)]);
		let provider = provider_with_token(endpoint, &format!("convex://{DEPLOYMENT}"));

		assert!(
			!provider
				.delete(Address::convention("p", "production", "MISSING"))
				.unwrap()
		);
		assert_eq!(server.join().unwrap().len(), 1);
	}

	#[test]
	fn non_utf8_values_are_rejected_before_any_request() {
		let provider = ConvexProvider::new(config(&format!("convex://{DEPLOYMENT}")));
		let error = provider
			.set(
				Address::convention("p", "production", "API_KEY"),
				&SecretBytes::from_slice(b"\xff\xfe"),
			)
			.unwrap_err();
		assert!(
			error.to_string().contains("requires UTF-8 secret values"),
			"{error}"
		);
	}

	#[test]
	fn api_errors_surface_the_code_and_message() {
		let unauthorized =
			r#"{"code":"AccessTokenInvalid","message":"Access Token could not be decoded"}"#;
		let (endpoint, server) = response_server(vec![("401 Unauthorized", unauthorized)]);
		let provider = provider_with_token(endpoint, &format!("convex://{DEPLOYMENT}"));

		let error = provider
			.get(Address::convention("p", "production", "API_KEY"))
			.unwrap_err();
		assert!(
			error.to_string().contains("HTTP 401 while listing"),
			"{error}"
		);
		assert!(
			error
				.to_string()
				.contains("AccessTokenInvalid: Access Token could not be decoded"),
			"{error}"
		);
		server.join().unwrap();
	}

	#[test]
	fn invalid_json_and_bare_error_bodies_are_reported() {
		let (endpoint, server) = response_server(vec![("200 OK", "not-json")]);
		let provider = provider_with_token(endpoint, &format!("convex://{DEPLOYMENT}"));
		let error = provider
			.get(Address::convention("p", "production", "API_KEY"))
			.unwrap_err();
		assert!(
			error.to_string().contains("invalid JSON while listing"),
			"{error}"
		);
		server.join().unwrap();

		let (endpoint, server) = response_server(vec![("500 Internal Server Error", "boom")]);
		let provider = provider_with_token(endpoint, &format!("convex://{DEPLOYMENT}"));
		let error = provider
			.get(Address::convention("p", "production", "API_KEY"))
			.unwrap_err();
		assert!(error.to_string().contains("HTTP 500"), "{error}");
		assert!(error.to_string().contains("boom"), "{error}");
		server.join().unwrap();
	}

	#[test]
	fn reflection_lists_the_deployment_variables() {
		let body = Box::leak(
			r#"{"environmentVariables":{"API_KEY":"a","DATABASE_URL":"b","REMOVED":null}}"#
				.to_string()
				.into_boxed_str(),
		);
		let (endpoint, server) = response_server(vec![("200 OK", body)]);
		let provider = provider_with_token(endpoint, &format!("convex://{DEPLOYMENT}"));

		let reflected = provider
			.reflect(DiscoveryContext::new("project", "production"))
			.unwrap();

		assert_eq!(reflected.len(), 2);
		assert!(reflected.contains_key("API_KEY"));
		assert!(reflected.contains_key("DATABASE_URL"));
		assert!(!reflected.contains_key("REMOVED"));
		server.join().unwrap();
	}

	#[test]
	fn storage_identity_and_write_target_name_the_deployment() {
		let provider = ConvexProvider::new(config(&format!("convex://{DEPLOYMENT}")));
		assert_eq!(
			provider.storage_identity(),
			format!("convex://{DEPLOYMENT}")
		);
		assert_eq!(
			provider
				.describe_write_target(Address::convention("p", "production", "KEY"))
				.unwrap(),
			format!("Convex deployment '{DEPLOYMENT}' environment variable 'KEY'")
		);
	}
}