cargo-rail 0.22.2

Cargo-native Rust workspace engine for affected CI, Rust source-surface analysis, compiler reuse, dependency coherence, exact-SHA releases, and split/sync
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
//! Machine-owned remote compiler-cache authority.
//!
//! A remote destination is selected by transient machine environment or the
//! private transparent-cache setup receipt. Repository configuration cannot
//! name, enable, or promote remote storage.

mod azure;
mod coordinator;
mod object;
mod s3;
mod url;

use std::fmt;
use std::io::Read;
use std::sync::OnceLock;
use std::sync::atomic::{AtomicBool, Ordering};

use serde::{Deserialize, Serialize};

use crate::compiler::native_cache::RemoteAuthorityId;

const MAX_APPROVED_ENVIRONMENT_NAMES: usize = 512;
const MAX_APPROVED_ENVIRONMENT_NAME_BYTES: usize = 256;
const MAX_APPROVED_ENVIRONMENT_TOTAL_BYTES: usize = 32 * 1024;

/// Machine-owned normalized remote URL.
pub(crate) const REMOTE_URL_ENV: &str = "CARGO_RAIL_CACHE_REMOTE";
/// Machine-owned remote publication mode.
pub(crate) const REMOTE_MODE_ENV: &str = "CARGO_RAIL_CACHE_MODE";
/// Additional reviewed compiler environment names admitted to L2 identity.
pub(crate) const REMOTE_ENVIRONMENT_ENV: &str = "CARGO_RAIL_CACHE_REMOTE_ENVIRONMENT";

const BUILT_IN_ENVIRONMENT_NAMES: &[&str] = &["CARGO_PKG_NAME", "CARGO_PKG_VERSION_PATCH", "OUT_DIR"];

const PRIVATE_REMOTE_ENVIRONMENT: &[&str] = &[
    REMOTE_URL_ENV,
    REMOTE_MODE_ENV,
    REMOTE_ENVIRONMENT_ENV,
    coordinator::MARKER_ENV,
    "AWS_ACCESS_KEY_ID",
    "AWS_ACCOUNT_ID",
    "AWS_SECRET_ACCESS_KEY",
    "AWS_SESSION_TOKEN",
    "AWS_SECURITY_TOKEN",
    "AWS_WEB_IDENTITY_TOKEN_FILE",
    "AWS_ROLE_ARN",
    "AWS_ROLE_SESSION_NAME",
    "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI",
    "AWS_CONTAINER_CREDENTIALS_FULL_URI",
    "AWS_CONTAINER_AUTHORIZATION_TOKEN",
    "AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE",
    "AWS_PROFILE",
    "AWS_DEFAULT_PROFILE",
    "AWS_REGION",
    "AWS_DEFAULT_REGION",
    "AWS_CONFIG_FILE",
    "AWS_SHARED_CREDENTIALS_FILE",
    "AWS_EC2_METADATA_DISABLED",
    "AWS_METADATA_SERVICE_TIMEOUT",
    "AWS_METADATA_SERVICE_NUM_ATTEMPTS",
    "AWS_SDK_LOAD_CONFIG",
    "AWS_LOGIN_CACHE_DIRECTORY",
    "AWS_ENDPOINT_URL",
    "AWS_ENDPOINT_URL_S3",
    "AWS_ENDPOINT_URL_STS",
    "AWS_ENDPOINT_URL_SSO",
    "AWS_ENDPOINT_URL_SSO_OIDC",
    "AWS_IGNORE_CONFIGURED_ENDPOINT_URLS",
    "AZURE_AUTHORITY_HOST",
    "AZURE_CLIENT_ID",
    "AZURE_CONFIG_DIR",
    "AZURE_FEDERATED_TOKEN_FILE",
    "AZURE_SUBSCRIPTION_ID",
    "AZURE_TENANT_ID",
    "AZURE_CLIENT_SECRET",
    "AZURE_CLIENT_CERTIFICATE_PATH",
    "AZURE_CLIENT_CERTIFICATE_PASSWORD",
    "IDENTITY_ENDPOINT",
    "IDENTITY_HEADER",
    "IDENTITY_SERVER_THUMBPRINT",
    "IMDS_ENDPOINT",
    "MSI_ENDPOINT",
    "MSI_SECRET",
];

/// Remove remote selection and credentials from compiler children.
pub(crate) fn scrub_child_environment(command: &mut std::process::Command) {
    for name in PRIVATE_REMOTE_ENVIRONMENT {
        command.env_remove(name);
    }
}

/// One redacted remote-cache failure.
#[derive(Debug, Clone)]
pub(crate) struct RemoteStoreError {
    kind: RemoteStoreErrorKind,
    message: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RemoteStoreErrorKind {
    Integrity,
    Configuration,
    Unavailable,
    Authentication,
}

impl RemoteStoreError {
    pub(super) fn integrity(message: impl Into<String>) -> Self {
        Self::new(RemoteStoreErrorKind::Integrity, message)
    }

    pub(super) fn configuration(message: impl Into<String>) -> Self {
        Self::new(RemoteStoreErrorKind::Configuration, message)
    }

    pub(super) fn unavailable(message: impl Into<String>) -> Self {
        Self::new(RemoteStoreErrorKind::Unavailable, message)
    }

    pub(super) fn authentication(message: impl Into<String>) -> Self {
        Self::new(RemoteStoreErrorKind::Authentication, message)
    }

    fn new(kind: RemoteStoreErrorKind, message: impl Into<String>) -> Self {
        Self {
            kind,
            message: message.into(),
        }
    }

    pub(crate) fn cold_reason(&self) -> &'static str {
        match self.kind {
            RemoteStoreErrorKind::Integrity => "remote_entry_rejected",
            RemoteStoreErrorKind::Configuration
            | RemoteStoreErrorKind::Unavailable
            | RemoteStoreErrorKind::Authentication => "remote_cache_unavailable",
        }
    }
}

impl fmt::Display for RemoteStoreError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.message)
    }
}

impl std::error::Error for RemoteStoreError {}

pub(super) type RemoteStoreResult<T> = Result<T, RemoteStoreError>;

/// Maximum remote authority selected for one process tree.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum RemoteCacheMode {
    Read,
    ReadWrite,
}

/// Canonical non-secret remote policy persisted in private machine setup state.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct InstalledRemoteCache {
    version: u32,
    normalized_url: String,
    mode: RemoteCacheMode,
    additional_environment_names: Vec<String>,
}

impl InstalledRemoteCache {
    const VERSION: u32 = 1;

    pub(crate) fn from_selection(selection: &RemoteCacheSelection) -> Self {
        let additional_environment_names = selection
            .approved_environment_names
            .iter()
            .filter(|name| !BUILT_IN_ENVIRONMENT_NAMES.contains(&name.as_str()))
            .cloned()
            .collect();
        Self {
            version: Self::VERSION,
            normalized_url: selection.normalized_url().to_string(),
            mode: selection.mode,
            additional_environment_names,
        }
    }

    pub(crate) fn selection(&self) -> RemoteStoreResult<RemoteCacheSelection> {
        if self.version != Self::VERSION {
            return Err(RemoteStoreError::configuration(
                "installed remote cache policy has an incompatible version",
            ));
        }
        let selection = RemoteCacheSelection::parse(
            &self.normalized_url,
            Some(self.mode.as_str()),
            &self.additional_environment_names,
        )?;
        if selection.normalized_url() != self.normalized_url {
            return Err(RemoteStoreError::configuration(
                "installed remote cache URL is not canonical",
            ));
        }
        Ok(selection)
    }
}

impl RemoteCacheMode {
    fn parse(value: &str) -> RemoteStoreResult<Self> {
        match value {
            "read" => Ok(Self::Read),
            "read-write" => Ok(Self::ReadWrite),
            _ => Err(RemoteStoreError::configuration(
                "remote cache mode must be 'read' or 'read-write'",
            )),
        }
    }

    pub(crate) const fn as_str(self) -> &'static str {
        match self {
            Self::Read => "read",
            Self::ReadWrite => "read-write",
        }
    }
}

/// Exact normalized remote authority selected by machine state.
#[derive(Clone, PartialEq, Eq)]
pub(crate) struct RemoteCacheSelection {
    authority: url::RemoteCacheAuthority,
    mode: RemoteCacheMode,
    approved_environment_names: Vec<String>,
}

impl fmt::Debug for RemoteCacheSelection {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("RemoteCacheSelection")
            .field("authority", &self.authority.identity().as_str())
            .field("provider", &self.authority.provider_name())
            .field("mode", &self.mode)
            .field("approved_environment_names", &self.approved_environment_names.len())
            .finish()
    }
}

impl RemoteCacheSelection {
    /// Parse one explicit URL and policy without consulting ambient state.
    pub(crate) fn parse(
        remote_url: &str,
        mode: Option<&str>,
        additional_environment: &[String],
    ) -> RemoteStoreResult<Self> {
        let authority = url::RemoteCacheAuthority::parse(remote_url)?;
        let mode = mode
            .map(RemoteCacheMode::parse)
            .transpose()?
            .unwrap_or(RemoteCacheMode::ReadWrite);
        validate_environment_names(additional_environment)?;
        if additional_environment
            .iter()
            .any(|name| environment_name_may_be_secret(name))
        {
            return Err(RemoteStoreError::configuration(
                "remote cache environment policy contains a credential-like name",
            ));
        }
        let mut approved_environment_names = BUILT_IN_ENVIRONMENT_NAMES
            .iter()
            .map(|name| (*name).to_string())
            .chain(additional_environment.iter().cloned())
            .collect::<Vec<_>>();
        approved_environment_names.sort_unstable();
        approved_environment_names.dedup();
        validate_environment_names(&approved_environment_names)?;
        Ok(Self {
            authority,
            mode,
            approved_environment_names,
        })
    }

    /// Load the one optional machine-owned selection.
    pub(crate) fn from_environment() -> RemoteStoreResult<Option<Self>> {
        let remote_url = std::env::var_os(REMOTE_URL_ENV).filter(|value| !value.is_empty());
        let mode = std::env::var_os(REMOTE_MODE_ENV).filter(|value| !value.is_empty());
        let additional = std::env::var_os(REMOTE_ENVIRONMENT_ENV).filter(|value| !value.is_empty());
        let Some(remote_url) = remote_url else {
            if mode.is_some() || additional.is_some() {
                return Err(RemoteStoreError::configuration(
                    "remote cache policy is set without CARGO_RAIL_CACHE_REMOTE",
                ));
            }
            return Ok(None);
        };
        let remote_url = remote_url
            .to_str()
            .ok_or_else(|| RemoteStoreError::configuration("remote cache URL is not valid UTF-8"))?;
        let mode = mode
            .as_deref()
            .map(|value| {
                value
                    .to_str()
                    .ok_or_else(|| RemoteStoreError::configuration("remote cache mode is not valid UTF-8"))
            })
            .transpose()?;
        let additional = additional
            .as_deref()
            .map(parse_environment_policy)
            .transpose()?
            .unwrap_or_default();
        Self::parse(remote_url, mode, &additional).map(Some)
    }

    /// Prefer an explicit transient selection, then one receipt-owned policy.
    pub(crate) fn from_environment_or_installed(
        installed: Option<&InstalledRemoteCache>,
    ) -> RemoteStoreResult<Option<Self>> {
        match Self::from_environment()? {
            Some(selection) => Ok(Some(selection)),
            None => installed.map(InstalledRemoteCache::selection).transpose(),
        }
    }

    pub(crate) fn authority(&self) -> &RemoteAuthorityId {
        self.authority.identity()
    }

    pub(crate) const fn mode(&self) -> RemoteCacheMode {
        self.mode
    }

    pub(crate) fn approved_environment_names(&self) -> &[String] {
        &self.approved_environment_names
    }

    pub(crate) fn normalized_url(&self) -> &str {
        self.authority.normalized_url()
    }

    pub(crate) fn provider_name(&self) -> &'static str {
        self.authority.provider_name()
    }

    pub(crate) fn protocol_name(&self) -> &'static str {
        self.authority.protocol_name()
    }

    pub(crate) fn direct_transport_supported(&self) -> bool {
        self.authority.supports_s3_transport() || self.authority.is_azure_blob()
    }

    pub(crate) fn approves_environment_names(&self, names: &[String]) -> bool {
        names
            .iter()
            .all(|name| self.approved_environment_names.binary_search(name).is_ok())
    }
}

pub(crate) use object::Publication as RemotePublication;

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub(crate) struct RemoteTransferMetrics {
    pub(crate) request_attempts: u64,
    pub(crate) coordinator_requests: u64,
    pub(crate) payload_bytes_read: u64,
    pub(crate) payload_bytes_written: u64,
    pub(crate) service_elapsed_ns: u64,
}

pub(crate) enum RemoteBody {
    Direct(object::EntryBody),
    Coordinated(coordinator::PackReader),
}

impl Read for RemoteBody {
    fn read(&mut self, output: &mut [u8]) -> std::io::Result<usize> {
        match self {
            Self::Direct(body) => body.read(output),
            Self::Coordinated(body) => body.read(output),
        }
    }
}

impl RemoteBody {
    pub(crate) fn copy_compressed_to<W: std::io::Write>(&mut self, writer: &mut W) -> std::io::Result<u64> {
        match self {
            Self::Direct(body) => body.copy_compressed_to(writer),
            Self::Coordinated(body) => {
                let copied = std::io::copy(&mut *body, writer)?;
                body.finish()?;
                Ok(copied)
            }
        }
    }
}

pub(crate) enum RemoteLookup {
    Miss,
    Conflict,
    Unique {
        environment_names: Vec<String>,
        action_key: String,
        result_key: String,
        body: RemoteBody,
        bytes: u64,
        compressed_bytes: u64,
    },
}

/// One coordinator-preferred store with a lazy direct fallback.
pub(crate) struct RemoteStore {
    selection: RemoteCacheSelection,
    coordinator: Option<coordinator::Client>,
    coordinator_connect_error: Option<RemoteStoreError>,
    coordinator_failed: AtomicBool,
    direct: OnceLock<RemoteStoreResult<object::ObjectStore>>,
}

impl RemoteStore {
    pub(crate) fn connect(
        selection: &RemoteCacheSelection,
        installation: Option<&crate::cache::installation::InstallationReceipt>,
    ) -> RemoteStoreResult<Self> {
        let (coordinator, coordinator_connect_error) =
            match installation.map(|receipt| coordinator::connect(selection, receipt)) {
                Some(Ok(coordinator)) => (coordinator, None),
                Some(Err(error)) => (None, Some(error)),
                None => (None, None),
            };
        Ok(Self {
            selection: selection.clone(),
            coordinator,
            coordinator_connect_error,
            coordinator_failed: AtomicBool::new(false),
            direct: OnceLock::new(),
        })
    }

    fn direct(&self) -> RemoteStoreResult<&object::ObjectStore> {
        match self.direct.get_or_init(|| object::connect(&self.selection)) {
            Ok(store) => Ok(store),
            Err(error) => Err(error.clone()),
        }
    }

    pub(crate) fn metrics(&self) -> RemoteTransferMetrics {
        let direct = self
            .direct
            .get()
            .and_then(|store| store.as_ref().ok())
            .map_or_else(object::TransferMetrics::default, object::ObjectStore::metrics);
        let coordinated = self
            .coordinator
            .as_ref()
            .map_or_else(coordinator::Metrics::default, coordinator::Client::metrics);
        RemoteTransferMetrics {
            request_attempts: direct.request_attempts.saturating_add(coordinated.request_attempts),
            coordinator_requests: coordinated.requests,
            payload_bytes_read: direct.payload_bytes_read.saturating_add(coordinated.payload_bytes_read),
            payload_bytes_written: direct
                .payload_bytes_written
                .saturating_add(coordinated.payload_bytes_written),
            service_elapsed_ns: direct.service_elapsed_ns.saturating_add(coordinated.service_elapsed_ns),
        }
    }

    pub(crate) fn coordinator_connect_error(&self) -> Option<&RemoteStoreError> {
        self.coordinator_connect_error.as_ref()
    }

    pub(crate) fn lookup(&self, base_action_key: &str) -> RemoteStoreResult<RemoteLookup> {
        if !self.coordinator_failed.load(Ordering::Acquire)
            && let Some(coordinator) = &self.coordinator
        {
            match coordinator.lookup(base_action_key) {
                Ok(coordinator::Lookup::Miss) => return Ok(RemoteLookup::Miss),
                Ok(coordinator::Lookup::Conflict) => return Ok(RemoteLookup::Conflict),
                Ok(coordinator::Lookup::Unique {
                    environment_names,
                    action_key,
                    result_key,
                    body,
                    bytes,
                    compressed_bytes,
                }) => {
                    return Ok(RemoteLookup::Unique {
                        environment_names,
                        action_key,
                        result_key,
                        body: RemoteBody::Coordinated(body),
                        bytes,
                        compressed_bytes,
                    });
                }
                Err(_) => self.coordinator_failed.store(true, Ordering::Release),
            }
        }
        match self.direct()?.lookup(base_action_key)? {
            object::Lookup::Miss => Ok(RemoteLookup::Miss),
            object::Lookup::Conflict => Ok(RemoteLookup::Conflict),
            object::Lookup::Unique {
                environment_names,
                action_key,
                result_key,
                body,
                bytes,
                compressed_bytes,
            } => Ok(RemoteLookup::Unique {
                environment_names,
                action_key,
                result_key,
                body: RemoteBody::Direct(body),
                bytes,
                compressed_bytes,
            }),
        }
    }

    pub(crate) fn publish(
        &self,
        association: &crate::compiler::native_cache::pack::NativeAssociation,
        base_action_key: &str,
        environment_names: &[String],
        pack: std::fs::File,
    ) -> RemoteStoreResult<RemotePublication> {
        if !self.coordinator_failed.load(Ordering::Acquire)
            && let Some(coordinator) = &self.coordinator
        {
            match coordinator.publish(
                association,
                base_action_key,
                environment_names,
                pack.try_clone()
                    .map_err(|error| RemoteStoreError::unavailable(error.to_string()))?,
            ) {
                Ok(publication) => return Ok(publication),
                Err(_) => self.coordinator_failed.store(true, Ordering::Release),
            }
        }
        self.direct()?
            .publish(association, base_action_key, environment_names, pack)
    }
}

pub(crate) fn run_coordinator_if_requested() -> Option<i32> {
    coordinator::run_if_requested()
}

pub(crate) fn stop_installed_coordinators(receipt: &crate::cache::installation::InstallationReceipt) {
    coordinator::stop_all(receipt);
}

/// Redacted projection of one selected machine-owned target.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub(crate) struct RemoteCacheConfigurationStatus {
    pub(crate) provider: &'static str,
    pub(crate) protocol: &'static str,
    pub(crate) authority: String,
    pub(crate) mode: &'static str,
    pub(crate) shared_environment_names: usize,
    pub(crate) activation: &'static str,
}

impl RemoteCacheConfigurationStatus {
    pub(crate) fn from_selection(selection: &RemoteCacheSelection) -> Self {
        Self {
            provider: selection.provider_name(),
            protocol: selection.protocol_name(),
            authority: selection.authority().as_str().to_string(),
            mode: selection.mode().as_str(),
            shared_environment_names: selection.approved_environment_names().len(),
            activation: if selection.direct_transport_supported() {
                "direct_transport_selected"
            } else {
                "authority_selected_transport_inactive"
            },
        }
    }
}

pub(crate) fn configuration_status(
    current_dir: &std::path::Path,
) -> RemoteStoreResult<Option<RemoteCacheConfigurationStatus>> {
    if let Some(selection) = RemoteCacheSelection::from_environment()? {
        return Ok(Some(RemoteCacheConfigurationStatus::from_selection(&selection)));
    }
    let installed = crate::cache::installation::installed_remote(current_dir)
        .map_err(|_| RemoteStoreError::configuration("installed remote cache policy is unavailable"))?;
    installed
        .as_ref()
        .map(InstalledRemoteCache::selection)
        .transpose()
        .map(|selection| selection.as_ref().map(RemoteCacheConfigurationStatus::from_selection))
}

fn parse_environment_policy(value: &std::ffi::OsStr) -> RemoteStoreResult<Vec<String>> {
    let value = value
        .to_str()
        .ok_or_else(|| RemoteStoreError::configuration("remote cache environment policy is not valid UTF-8"))?;
    if value.is_empty() {
        return Ok(Vec::new());
    }
    let names = value.split(',').map(str::to_string).collect::<Vec<_>>();
    validate_environment_names(&names)?;
    Ok(names)
}

fn strictly_sorted_unique(values: &[String]) -> bool {
    values.windows(2).all(|pair| pair[0] < pair[1])
}

fn valid_environment_name(name: &str) -> bool {
    !name.is_empty()
        && name.len() <= MAX_APPROVED_ENVIRONMENT_NAME_BYTES
        && !name.as_bytes().contains(&0)
        && !name.contains('=')
        && !name.chars().any(char::is_control)
}

fn environment_name_may_be_secret(name: &str) -> bool {
    let name = name.to_ascii_uppercase();
    matches!(
        name.as_str(),
        "SSH_AUTH_SOCK"
            | "GPG_AGENT_INFO"
            | "DOCKER_AUTH_CONFIG"
            | "GOOGLE_APPLICATION_CREDENTIALS"
            | "AWS_ACCESS_KEY_ID"
            | "AWS_SECRET_ACCESS_KEY"
            | "AWS_SESSION_TOKEN"
    ) || [
        "_TOKEN",
        "_SECRET",
        "_PASSWORD",
        "_PASSWD",
        "_PRIVATE_KEY",
        "_ACCESS_KEY",
        "_CREDENTIAL",
        "_CREDENTIALS",
        "_AUTHORIZATION",
    ]
    .iter()
    .any(|suffix| name.ends_with(suffix))
}

fn validate_environment_names(names: &[String]) -> RemoteStoreResult<()> {
    if names.len() > MAX_APPROVED_ENVIRONMENT_NAMES
        || !strictly_sorted_unique(names)
        || names.iter().any(|name| !valid_environment_name(name))
        || names
            .iter()
            .try_fold(0_usize, |total, name| total.checked_add(name.len()))
            .is_none_or(|bytes| bytes > MAX_APPROVED_ENVIRONMENT_TOTAL_BYTES)
    {
        return Err(RemoteStoreError::configuration(
            "remote cache environment policy is invalid",
        ));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn selection_defaults_to_write_and_keeps_identity_independent_of_mode() {
        let url = "s3://rail-cache/team?owner=123456789012&region=us-east-1";
        let read = RemoteCacheSelection::parse(url, Some("read"), &[]).expect("read selection");
        let write = RemoteCacheSelection::parse(url, None, &[]).expect("write selection");
        assert_eq!(read.authority(), write.authority());
        assert_eq!(read.mode(), RemoteCacheMode::Read);
        assert_eq!(write.mode(), RemoteCacheMode::ReadWrite);
    }

    #[test]
    fn every_accepted_provider_selects_a_bounded_direct_transport() {
        for url in [
            "s3://rail-cache/team?owner=123456789012&region=us-east-1",
            "azure://railcache/cargo-rail/team",
            "r2://0123456789abcdef0123456789abcdef/rail-cache/team",
            "s3+http://127.0.0.1:9000/rail-cache/team?region=test-1",
        ] {
            let selection = RemoteCacheSelection::parse(url, Some("read"), &[]).expect("provider selection");
            assert!(selection.direct_transport_supported(), "inactive provider: {url}");
        }
    }

    #[test]
    fn selection_rejects_secret_like_or_noncanonical_environment_policy() {
        let url = "s3://rail-cache/team?owner=123456789012&region=us-east-1";
        RemoteCacheSelection::parse(url, None, &["CI_TOKEN".to_string()]).unwrap_err();
        RemoteCacheSelection::parse(url, None, &["Z_FLAG".to_string(), "A_FLAG".to_string()]).unwrap_err();
    }

    #[test]
    fn selection_approves_observed_portable_cargo_package_metadata() {
        let selection =
            RemoteCacheSelection::parse("s3://rail-cache/team?owner=123456789012&region=us-east-1", None, &[])
                .expect("selection");
        assert!(
            selection
                .approves_environment_names(&["CARGO_PKG_NAME".to_string(), "CARGO_PKG_VERSION_PATCH".to_string(),])
        );
    }

    #[test]
    fn every_remote_authority_variable_is_explicitly_removed_from_compiler_children() {
        let mut command = std::process::Command::new("rustc");
        for name in PRIVATE_REMOTE_ENVIRONMENT {
            command.env(name, "private");
        }
        scrub_child_environment(&mut command);

        let overrides = command
            .get_envs()
            .map(|(name, value)| (name.to_string_lossy().into_owned(), value.is_none()))
            .collect::<std::collections::BTreeMap<_, _>>();
        for name in PRIVATE_REMOTE_ENVIRONMENT {
            assert_eq!(overrides.get(*name), Some(&true), "{name} was not removed");
        }
    }
}