Skip to main content

gunnar_server/
mirror_fetch.rs

1//! **What `Mirror.Sync` actually does**: one fetch from an upstream, into this
2//! box's own storage engine, and one compare-and-swap that moves the
3//! references.
4//!
5//! # There is one client, and two ways to reach a remote
6//!
7//! [`gunnar_wire::fetch_pack`] is the whole protocol — the advertisement, the
8//! `ls-refs`, the want/have negotiation, the sideband demultiplex, the packfile
9//! streamed into a [`PackIngest`](gunnar_wire::PackIngest). It takes a
10//! `&mut dyn BufRead` and a `&mut dyn Write` and cannot tell what is behind
11//! them, which is the seam this module fills twice and only twice:
12//!
13//! | remote | the pair | who built it |
14//! |---|---|---|
15//! | `ssh://`, `git+ssh://`, `git@host:path` | a russh channel's two halves | `nornir-git`, through `gunnar_client::connect_upload_pack` — the **same dialler** replication pushes over |
16//! | `https://`, `http://` | a buffered request/response adapter | [`crate::mirror_http`] |
17//!
18//! Neither is a second protocol implementation, and that is the point. A bug in
19//! negotiation is one bug, in one file, in `gunnar-wire`.
20//!
21//! # This module writes the references, and `fetch_pack` deliberately does not
22//!
23//! `fetch_pack`'s module doc states the rule: it returns a **plan**
24//! (`FetchReport::updates`, each entry a name, the old value and the new one)
25//! and applies nothing, because applying a whole mirror's plan with no gate, no
26//! atomicity across the batch and no way for an operator to see it first is not
27//! something a protocol crate should be able to do.
28//!
29//! So the apply is here, and it is **one [`GitOps::put_refs_cas`]** with the
30//! plan's own `old` as every expectation. That is not tidiness:
31//!
32//! * A mirror is not the only writer. `git push` reaches the same repository
33//!   over SSH and over smart HTTP, and a fetch that read a tip, spent four
34//!   minutes on the network and then wrote unconditionally would **silently
35//!   destroy** whatever landed in between. The `old` in the plan is read from
36//!   the same `GitOps::refs()` snapshot the `have` list came from, so the CAS
37//!   is checking exactly the value the fetch reasoned about.
38//! * A CAS failure is a **FAILED job that names the reference**, never a skip.
39//!   "Synced" about a repository where one branch quietly did not move is the
40//!   answer that makes a mirror untrustworthy, and it reads identically to a
41//!   mirror that worked.
42//! * Every edit or none. A half-applied plan leaves the repository at a state
43//!   no upstream ever had.
44//!
45//! # …and the batch goes through branch protection first
46//!
47//! A mirror moves references, and until 2026-09-11 it was the only writer on
48//! this box that moved them without asking [`BranchProtectionGate`]. That is
49//! not a smaller version of a push: `Mirror.Sync` needs **Write** and
50//! `Policy.SetProtection` needs **Admin**, so a principal who may not configure
51//! a protection rule could register a mirror against the repository it protects
52//! and have the upstream's tips applied over it — a non-fast-forward onto a ref
53//! whose rule says `allow_force_push: false`, applied by the one path that
54//! never looked. See [`Protection`] for what is decided here and what is
55//! refused as undecidable.
56//!
57//! # The credential is never in this module's output
58//!
59//! The value is fetched from the sealed secret store, held for the length of
60//! one dial, and never formatted. An SSH key is materialised into a
61//! `0600` temporary file because `nornir-git` authenticates from a path and
62//! this tree does not fork a second dialler to change that (one implementation); the file is
63//! unlinked when the fetch returns, on every path including a panic, because
64//! [`tempfile::NamedTempFile`] unlinks on `Drop`. A token becomes an
65//! `Authorization` header value inside [`crate::mirror_http`] and nothing else.
66//! Errors that mention a URL go through [`scrub`] first.
67
68use std::io::{BufRead, Write};
69use std::sync::atomic::{AtomicU64, Ordering};
70use std::sync::Arc;
71
72use gix_hash::ObjectId;
73use gunnar_core::MirrorCredentialKind;
74use gunnar_store::git_storage_trait::{GitOps, RefCas};
75use gunnar_wire::fetch_pack::{self, FetchPack, FetchProgress, FetchReport, LocalRefs};
76
77use crate::job_queue::Reporter;
78use crate::mirror_queue::{Mirror, MirrorOrder};
79use crate::stores::ServerStores;
80
81/// The tracing target a sync's lines carry — the same one every other git
82/// transfer on this box uses, so `RUST_LOG=gunnar::git=info` selects a mirror
83/// as well as a clone.
84const TARGET_GIT: &str = "gunnar::git";
85
86/// **The `known_hosts` a mirror's SSH dial trusts**, under the data root.
87///
88/// Not `~/.ssh/known_hosts`, and this is the same ruling replication's
89/// `REPLICATION_KNOWN_HOSTS` makes for the push direction: a server's identity
90/// is the box, not a home directory. The appliance has no home directory at
91/// all, so the default would be a file that cannot be written and a
92/// trust-on-first-use that silently never remembers — every sync a first
93/// contact, which is the same as no host-key checking with extra steps.
94///
95/// A separate file from replication's, because they are separate rosters: the
96/// standbys this box pushes to and the upstreams it pulls from are different
97/// sets of machines, and a pin acquired for one is not consent for the other.
98pub const MIRROR_KNOWN_HOSTS: &str = "mirror_known_hosts";
99
100/// **The file that makes a mirror's SSH dial refuse an unknown host key**,
101/// under the data root beside [`MIRROR_KNOWN_HOSTS`].
102///
103/// Without it a mirror dials trust-on-first-use: an upstream this box has never
104/// seen is recorded and trusted, and only a *changed* key is refused. That is
105/// the right default for a developer's laptop and a defensible one for a server
106/// being brought up without a provisioning step — it is what replication's own
107/// `strict_host_key` defaults to, and its comment already says that on "is the
108/// right setting for anything that outlives an afternoon". The mirror side had
109/// no way to say on at all: the policy was a literal in [`dial_ssh`].
110///
111/// A file rather than a field on `MirrorRecord`, for the reason
112/// [`MIRROR_KNOWN_HOSTS`] is a file under the data root: the roster of hosts
113/// this box will pull from is the box's, not one repository's, and the pin it
114/// is checked against is already per-box. Create it once, and every mirror on
115/// the box requires a `known_hosts` entry that somebody put there.
116pub const MIRROR_STRICT_HOST_KEY: &str = "mirror_strict_host_key";
117
118/// Does this box refuse an unknown upstream host key? See
119/// [`MIRROR_STRICT_HOST_KEY`].
120///
121/// The presence of the file is read at each dial rather than cached, so an
122/// operator who creates it does not have to restart the server to mean it.
123pub fn strict_host_keys(data_root: &std::path::Path) -> bool {
124    data_root.join(MIRROR_STRICT_HOST_KEY).exists()
125}
126
127/// **The file that lets this box's mirrors dial an address that is not
128/// publicly routable**, under the data root beside [`MIRROR_KNOWN_HOSTS`].
129///
130/// A file and not an environment variable, for the reason `wg.rs` writes out at
131/// length: an env var is process-global, so a rule that can only be exercised
132/// by `set_var` is a rule whose tests flake the moment `cargo test` runs them
133/// on two threads. A file under the data root is per-box, survives a restart,
134/// is visible to an operator looking at the box, and each test gets its own.
135///
136/// It exists because refusing every private destination outright would be a
137/// product regression, not a fix: mirroring from a forge on the operator's own
138/// LAN is an ordinary thing for an appliance to do. What must not be ordinary
139/// is a **low-privileged** caller getting it for free — see
140/// [`check_destination`].
141pub const MIRROR_ALLOW_PRIVATE: &str = "mirror_allow_private_destinations";
142
143/// **The credential a mirror dials with**, in the one shape it exists in
144/// memory.
145///
146/// Deliberately not `Debug`, `Clone` or `Serialize`. The compiler is the only
147/// mechanism that reliably stops a secret being formatted into a log line by
148/// somebody adding a `{:?}` three months from now, so the derive that would
149/// allow it is simply absent.
150pub enum Credential {
151    /// No credential. A public upstream needs none, and a mirror of one works.
152    None,
153    /// An OpenSSH private key, for an `ssh://` upstream.
154    SshKey(Vec<u8>),
155    /// A token, for an `https://` upstream. `username` empty means `Bearer`.
156    Token { username: String, secret: String },
157}
158
159impl Credential {
160    fn kind(&self) -> MirrorCredentialKind {
161        match self {
162            Credential::None => MirrorCredentialKind::None,
163            Credential::SshKey(_) => MirrorCredentialKind::SshKey,
164            Credential::Token { .. } => MirrorCredentialKind::Token,
165        }
166    }
167}
168
169/// **Which transport a remote URL names.**
170///
171/// Decided by the URL and by nothing else — not by which credential happens to
172/// be stored, and not by trying one and falling back. A fallback would turn "I
173/// gave you the wrong kind of key" into "your upstream is down", and those go
174/// to two different people.
175#[derive(Debug, Clone, Copy, PartialEq, Eq)]
176pub enum Wire {
177    /// `ssh://`, `git+ssh://`, or the scp-like `git@host:ns/repo.git`.
178    Ssh,
179    /// `https://` or `http://`.
180    Http,
181}
182
183impl Wire {
184    /// The word an operator sees in a refusal.
185    fn name(self) -> &'static str {
186        match self {
187            Wire::Ssh => "ssh",
188            Wire::Http => "smart http",
189        }
190    }
191}
192
193/// **What transport `url` names, or a refusal that says what gunnar can dial.**
194///
195/// The scp-like form is recognised the way git recognises it: a `:` before any
196/// `/`, with no scheme. A URL that is neither is refused rather than guessed
197/// at — a mirror registered against a path on the local disk would otherwise
198/// look like it was configured and then fail on the first sync with a
199/// connection error.
200pub fn classify(url: &str) -> Result<Wire, String> {
201    let url = url.trim();
202    if url.starts_with("ssh://") || url.starts_with("git+ssh://") {
203        return Ok(Wire::Ssh);
204    }
205    if url.starts_with("https://") || url.starts_with("http://") {
206        return Ok(Wire::Http);
207    }
208    if url.contains("://") {
209        return Err(format!(
210            "{} names a scheme gunnar cannot mirror from. The two transports are `ssh://` \
211             and `https://`",
212            scrub(url)
213        ));
214    }
215    // `git@host:ns/repo.git` — a colon before any slash, and a host in front
216    // of it.
217    match (url.find(':'), url.find('/')) {
218        (Some(colon), slash) if colon > 0 && slash.is_none_or(|slash| colon < slash) => {
219            Ok(Wire::Ssh)
220        }
221        _ => Err(format!(
222            "{url:?} is not a remote gunnar can mirror from. Write it as \
223             `ssh://git@host/ns/repo.git`, `git@host:ns/repo.git` or `https://host/ns/repo.git`"
224        )),
225    }
226}
227
228// ── where a mirror may dial ──────────────────────────────────────────────────
229
230/// **The host and port a mirror URL names**, in the three shapes
231/// [`classify`] admits.
232///
233/// Deliberately not a URL parser: the only question asked of this string is
234/// *which machine would be dialled*, and the answer is the authority. A
235/// userinfo half is dropped — `git@host` names `host` — and an IPv6 literal
236/// keeps its brackets off so it can be parsed as an address.
237pub(crate) fn authority(url: &str) -> Result<(String, u16), String> {
238    let url = url.trim();
239    let (default_port, rest) = if let Some(rest) = url.strip_prefix("ssh://") {
240        (22, rest)
241    } else if let Some(rest) = url.strip_prefix("git+ssh://") {
242        (22, rest)
243    } else if let Some(rest) = url.strip_prefix("https://") {
244        (443, rest)
245    } else if let Some(rest) = url.strip_prefix("http://") {
246        (80, rest)
247    } else {
248        // The scp-like `git@host:ns/repo.git`. `classify` has already decided
249        // this shape is ssh; the colon here separates a PATH, never a port.
250        // The userinfo goes first so a `@` inside it cannot be read as a host,
251        // and a bracketed literal is taken whole — `[::1]:path` is all colons
252        // and splitting on the first one would yield `[`.
253        let rest = url.rsplit('@').next().unwrap_or_default();
254        let host = match rest.strip_prefix('[') {
255            Some(inside) => inside.split(']').next().unwrap_or_default(),
256            None => rest.split(':').next().unwrap_or_default(),
257        };
258        return non_empty(host, url).map(|host| (host, 22));
259    };
260    let authority = rest.split(['/', '?', '#']).next().unwrap_or_default();
261    let authority = authority.rsplit('@').next().unwrap_or_default();
262    // `[::1]:9418` and `[::1]`, before the bare-host split, because an IPv6
263    // literal is all colons.
264    if let Some(rest) = authority.strip_prefix('[') {
265        let Some((host, tail)) = rest.split_once(']') else {
266            return Err(format!("{} names an unterminated IPv6 literal", scrub(url)));
267        };
268        let port = match tail.strip_prefix(':') {
269            None => default_port,
270            Some(digits) => digits
271                .parse()
272                .map_err(|_| format!("{} names a port that is not a number", scrub(url)))?,
273        };
274        return non_empty(host, url).map(|host| (host, port));
275    }
276    match authority.split_once(':') {
277        None => non_empty(authority, url).map(|host| (host, default_port)),
278        Some((host, digits)) => {
279            let port = digits
280                .parse()
281                .map_err(|_| format!("{} names a port that is not a number", scrub(url)))?;
282            non_empty(host, url).map(|host| (host, port))
283        }
284    }
285}
286
287fn non_empty(host: &str, url: &str) -> Result<String, String> {
288    if host.is_empty() {
289        return Err(format!("{} names no host to dial", scrub(url)));
290    }
291    Ok(host.to_owned())
292}
293
294/// **Refuse a destination this server can reach and its caller cannot** — the
295/// pure half, with the addresses handed in.
296///
297/// `Mirror.Add` needs Admin and `Mirror.Sync` needs Write, but *every enrolled
298/// principal is intrinsically Admin in its own namespace*
299/// (`AuthTable::owns_account`), so `Mirror.Add{create_repo}` against
300/// `alice/anything` is available to anybody with a key. Before this check the
301/// only thing standing between that caller and an arbitrary request from the
302/// server's own network position was `remote_url.trim().is_empty()`: the cloud
303/// metadata endpoint at `169.254.169.254`, a control plane on `127.0.0.1`, a
304/// printer on `10.0.0.0/8` — all dialled by the appliance, with the appliance's
305/// routes, and the outcome partly readable back through `Mirror.Status`.
306///
307/// **Every** resolved address has to be routable, not merely the first: a name
308/// that resolves to one public address and one loopback address is the
309/// rebinding shape, and taking the best of the two is how a check like this
310/// gets to be decorative.
311///
312/// The ranges are written out rather than taken from `IpAddr::is_global`, which
313/// is unstable, and each line is the reason it is there.
314pub(crate) fn address_verdict(host: &str, addrs: &[std::net::IpAddr]) -> Result<(), String> {
315    if addrs.is_empty() {
316        return Err(format!("{host} resolves to no address at all"));
317    }
318    for addr in addrs {
319        if let Some(why) = not_routable(*addr) {
320            return Err(format!(
321                "{host} resolves to {addr}, which is {why}. A mirror dials from the server, so a \
322                 destination the caller could not reach on their own is the server's network \
323                 position being borrowed. Create `{MIRROR_ALLOW_PRIVATE}` under this box's data \
324                 root to allow it deliberately."
325            ));
326        }
327    }
328    Ok(())
329}
330
331/// Why `addr` is not somewhere an arbitrary caller should be able to point this
332/// server, or `None` when it is an ordinary public address.
333fn not_routable(addr: std::net::IpAddr) -> Option<&'static str> {
334    use std::net::IpAddr;
335    match addr {
336        IpAddr::V4(v4) => {
337            let [a, b, c, _] = v4.octets();
338            match () {
339                _ if v4.is_unspecified() => Some("the unspecified address"),
340                _ if v4.is_loopback() => Some("loopback — this server itself"),
341                _ if v4.is_private() => Some("a private network (RFC 1918)"),
342                _ if v4.is_link_local() => {
343                    Some("link-local (RFC 3927), the range cloud metadata services answer on")
344                }
345                _ if v4.is_broadcast() => Some("the broadcast address"),
346                _ if v4.is_multicast() => Some("multicast"),
347                _ if v4.is_documentation() => Some("a documentation range (RFC 5737)"),
348                // 100.64.0.0/10, carrier-grade NAT: the provider's own network,
349                // not the caller's.
350                _ if a == 100 && (64..128).contains(&b) => Some("carrier-grade NAT (RFC 6598)"),
351                // 192.0.0.0/24, IETF protocol assignments.
352                _ if a == 192 && b == 0 && c == 0 => Some("an IETF protocol assignment"),
353                // 198.18.0.0/15, benchmarking.
354                _ if a == 198 && (b == 18 || b == 19) => Some("a benchmarking range (RFC 2544)"),
355                // 240.0.0.0/4, reserved.
356                _ if a >= 240 => Some("a reserved range"),
357                _ => None,
358            }
359        }
360        // A v4 address written the long way is the same address. Checked
361        // first, or `::ffff:127.0.0.1` walks straight past every arm below it.
362        IpAddr::V6(v6) if v6.to_ipv4_mapped().is_some() => {
363            not_routable(IpAddr::V4(v6.to_ipv4_mapped().expect("just matched")))
364        }
365        IpAddr::V6(v6) => {
366            let segments = v6.segments();
367            match () {
368                _ if v6.is_unspecified() => Some("the unspecified address"),
369                _ if v6.is_loopback() => Some("loopback — this server itself"),
370                _ if v6.is_multicast() => Some("multicast"),
371                // fc00::/7, unique local.
372                _ if segments[0] & 0xfe00 == 0xfc00 => Some("a unique-local address (RFC 4193)"),
373                // fe80::/10, link-local.
374                _ if segments[0] & 0xffc0 == 0xfe80 => Some("link-local (RFC 4291)"),
375                // 2001:db8::/32, documentation.
376                _ if segments[0] == 0x2001 && segments[1] == 0x0db8 => {
377                    Some("a documentation range (RFC 3849)")
378                }
379                _ => None,
380            }
381        }
382    }
383}
384
385/// [`address_verdict`] with the resolution and the opt-out file read — the
386/// edge, and the only part of this that touches the world.
387///
388/// # The window this does not close, said rather than glossed
389///
390/// The name is resolved here and again by `ureq` or by the ssh dialler a moment
391/// later, so a resolver that answers differently the second time (DNS
392/// rebinding) is not stopped by this. Closing that needs the dial pinned to the
393/// address that was checked, which is a change inside `ureq`'s and
394/// `nornir-git`'s connect paths rather than in this file. What this does close
395/// is the whole of the *static* case — a URL that simply names an internal
396/// address — which is what `Mirror.Add` accepted unchecked.
397pub fn check_destination(url: &str, data_root: &std::path::Path) -> Result<(), String> {
398    use std::net::ToSocketAddrs as _;
399    if data_root.join(MIRROR_ALLOW_PRIVATE).exists() {
400        return Ok(());
401    }
402    let (host, port) = authority(url)?;
403    // A literal needs no resolver, and asking one for it would let a DNS server
404    // answer a question that has no question in it.
405    if let Ok(addr) = host.parse::<std::net::IpAddr>() {
406        return address_verdict(&host, &[addr]);
407    }
408    let addrs: Vec<std::net::IpAddr> = (host.as_str(), port)
409        .to_socket_addrs()
410        .map_err(|err| format!("{host} could not be resolved: {err}"))?
411        .map(|socket| socket.ip())
412        .collect();
413    address_verdict(&host, &addrs)
414}
415
416/// **Refuse a credential that cannot be offered over the transport the URL
417/// names — by name, before anything is dialled.**
418///
419/// The failure this prevents is the quiet one. An SSH key against an `https://`
420/// remote is not "authentication failed": there is nowhere in an HTTP request
421/// to put a private key, so the request goes out anonymous and the upstream
422/// answers 401 — and the operator, who can see that a credential IS stored,
423/// concludes the key is wrong and rotates it. Forever. The same in reverse: a
424/// token has no place in an SSH publickey exchange, so the dial falls through
425/// to whatever else is offerable and fails on authentication with no mention of
426/// the token at all.
427///
428/// A stored credential of no kind is not a mismatch — a public upstream mirrors
429/// fine with nothing, and that path must keep working.
430pub fn check_credential(wire: Wire, credential: &Credential) -> Result<(), String> {
431    match (wire, credential) {
432        (_, Credential::None) => Ok(()),
433        (Wire::Ssh, Credential::SshKey(_)) => Ok(()),
434        (Wire::Http, Credential::Token { .. }) => Ok(()),
435        (wire, held) => Err(format!(
436            "this mirror's remote is dialled over {}, and the credential stored for it is {}. \
437             {} cannot carry it, so the fetch would go out unauthenticated and be refused as \
438             though no credential were set. Store {} instead, with `Mirror.SetCredential`.",
439            wire.name(),
440            held.kind().name(),
441            wire.name(),
442            match wire {
443                Wire::Ssh => "an ssh private key",
444                Wire::Http => "a token",
445            }
446        )),
447    }
448}
449
450// ── the gate ─────────────────────────────────────────────────────────────────
451
452/// **The repository's branch protection, over the batch a mirror is about to
453/// apply.**
454///
455/// One implementation: this is `gunnar_policy::BranchProtectionGate`, the same
456/// object `receive_pack`'s [`crate::transport::PolicyGate`] evaluates, reading
457/// the same document through the same [`crate::transport::load_policy`]. A
458/// second copy of "what does `allow_force_push` mean" is precisely the pair
459/// that drifts, and the three independent ways past branch protection this
460/// repository has had are all the same shape: a second writer that does what
461/// the gated writer does, without the gate.
462///
463/// # What a mirror can decide, and the one thing it cannot
464///
465/// `allow_create`, `allow_delete` and `allow_force_push` are decidable from the
466/// batch and the objects, which are already in the store when [`apply`] runs.
467/// `require_signature` is decidable too — but only against the set of objects
468/// the fetch introduced, so a repository carrying any protection rule makes the
469/// ingest ask for that set ([`crate::storage::ObjectSet::Required`]) instead of
470/// paying nothing for it, exactly as a push does when a gate is live.
471///
472/// `allowed_principals` is the one that cannot be decided here, and it is
473/// refused rather than skipped. The caller's identity does not cross the
474/// control-plane seam on this verb — `mirror_sync(&self, path)` carries a
475/// repository and nothing about who asked — so there is no principal to compare
476/// against the list. "I could not tell" is not "it is fine", so a rule that
477/// names its principals refuses the whole sync by name, and the operator either
478/// drops the rule or stops mirroring onto the refs it covers.
479struct Protection {
480    gate: gunnar_policy::BranchProtectionGate,
481    keys: gunnar_policy::StaticKeyDirectory,
482    repo: gunnar_policy::RepoId,
483}
484
485impl Protection {
486    /// The rules covering `repo`, or `None` when it has none configured.
487    ///
488    /// Read **before** the fetch, because whether any rule exists is what
489    /// decides if the ingest pays for an object-set enumeration — the same
490    /// ordering, for the same reason, as `receive_pack`'s.
491    ///
492    /// `Err` is a policy document that could not be read, and it REFUSES the
493    /// sync — the same fail-closed rule the push path has (GUN-007). A mirror
494    /// that synced with its gates disabled because the policy did not parse
495    /// would be the bypass GUN-032 closed, back through the side door.
496    fn open(
497        stores: &ServerStores,
498        row: &gunnar_core::Repo,
499        git_dir: &std::path::Path,
500    ) -> std::result::Result<Option<Self>, String> {
501        let config = crate::transport::load_policy(
502            stores,
503            row,
504            &git_dir.join(crate::transport::POLICY_FILE),
505        )
506        .map_err(|why| format!("refusing to mirror into {}: {why}", row.key()))?;
507        if config.protection.is_empty() {
508            return Ok(None);
509        }
510        Ok(Some(Protection {
511            gate: gunnar_policy::BranchProtectionGate::new(config.protection.clone()),
512            keys: crate::transport::key_directory(stores),
513            repo: gunnar_policy::RepoId::new(&row.namespace, &row.name),
514        }))
515    }
516
517    /// **Refuse the batch, or let it through.** `Err` is the refusal a FAILED
518    /// job carries, and nothing has been written when it is returned.
519    ///
520    /// `new_objects` is the fetch's own receipt. An ingest that did not
521    /// enumerate hands back the reason rather than an empty set, and an empty
522    /// set here would be a silent always-allow for `require_signature` — so the
523    /// `Err` arm refuses, quoting why.
524    fn check(
525        &self,
526        git: &dyn gunnar_store::git_storage_trait::GitServe,
527        updates: &[gunnar_policy::RefUpdate],
528        new_objects: std::result::Result<&[ObjectId], &str>,
529    ) -> Result<(), String> {
530        use gunnar_policy::Gate as _;
531
532        // The field with no answer on this path, refused first and by name.
533        for update in updates {
534            for rule in self.gate.rules() {
535                if !rule.pattern.matches_ref(&update.name) {
536                    continue;
537                }
538                if rule.allowed_principals.is_some() {
539                    return Err(format!(
540                        "{} is protected by {}, which names the principals that may move it. A \
541                         mirror sync carries no principal — the control-plane verb takes a \
542                         repository and not a caller — so that rule cannot be satisfied and this \
543                         sync is refused rather than applied unchecked. Nothing was moved.",
544                        update.name, rule.pattern
545                    ));
546                }
547                break;
548            }
549        }
550
551        let new_objects = new_objects.map_err(|reason| {
552            format!(
553                "this repository has branch protection and the fetched pack's object set could \
554                 not be enumerated ({reason}), so the gate could not decide. Nothing was moved."
555            )
556        })?;
557        // Never consulted: every rule that could read it has already refused
558        // above. Named rather than left as a plausible-looking value.
559        let principal = gunnar_policy::Principal::machine("mirror");
560        let ctx = gunnar_policy::PushContext::new(
561            &principal,
562            &self.repo,
563            updates,
564            new_objects,
565            git,
566            &self.keys,
567        );
568        match self.gate.check(&ctx) {
569            Ok(gunnar_policy::Verdict::Allow) => Ok(()),
570            Ok(gunnar_policy::Verdict::Reject(rejection)) => Err(format!(
571                "branch protection refused this mirror's reference batch: {}. Nothing was moved.",
572                rejection.reason
573            )),
574            // A gate that could not decide refuses; it does not allow.
575            Err(err) => Err(format!(
576                "branch protection could not decide this mirror's reference batch: {err}. \
577                 Nothing was moved."
578            )),
579        }
580    }
581}
582
583// ── the local half ───────────────────────────────────────────────────────────
584
585/// **What this repository already holds**, as `fetch_pack`'s have-source.
586///
587/// One `GitOps::refs()` call, one snapshot, taken inside `FetchPack::fetch` so
588/// the `have` list and every `old` in the plan come from the same read — a
589/// reference that moves under the fetch cannot make the plan promise an old
590/// value the store never held.
591///
592/// `HEAD` and the pseudo-refs are not rows in `refs()` at all, and a purely
593/// symbolic row under `refs/` carries no oid and is skipped: it is not a thing
594/// to offer as a `have`, and `fetch_pack`'s own doc says an unborn ref should
595/// simply be absent.
596struct StoreRefs<'a> {
597    git: &'a dyn gunnar_store::git_storage_trait::GitServe,
598    /// Objects this store holds that no reference names yet — an interrupted
599    /// import's ([`already_here`]). Offered as `have`s under names the plan
600    /// never matches, so they change what is sent and nothing that is written.
601    extra_haves: Vec<ObjectId>,
602}
603
604impl LocalRefs for StoreRefs<'_> {
605    fn tips(&self) -> gunnar_wire::Result<Vec<(String, ObjectId)>> {
606        let rows = GitOps::refs(self.git).map_err(|err| {
607            gunnar_wire::Error::Store(gunnar_store::Error::backend(format!(
608                "reading this repository's refs to offer them as haves: {err:#}"
609            )))
610        })?;
611        let mut out = Vec::with_capacity(rows.len());
612        for row in rows {
613            if row.name == "HEAD" {
614                continue;
615            }
616            let Some(raw) = row.oid.as_deref() else {
617                continue;
618            };
619            let Ok(oid) = gix_hash::oid::try_from_bytes(raw) else {
620                return Err(gunnar_wire::Error::Store(gunnar_store::Error::backend(
621                    format!("{} holds an oid this store cannot decode", row.name),
622                )));
623            };
624            out.push((row.name, oid.to_owned()));
625        }
626        for (n, oid) in self.extra_haves.iter().enumerate() {
627            out.push((format!("refs/gunnar/resume/{n}"), *oid));
628        }
629        Ok(out)
630    }
631}
632
633/// **The queue's progress field, written from the wire and from the walk.**
634///
635/// Three publishers, one job record, and each of the three is the only place on
636/// the system that knows its own number:
637///
638/// | published by | what | when |
639/// |---|---|---|
640/// | [`FetchProgress::objects_total`] | the denominator | once, the instant the packfile header is read |
641/// | [`FetchProgress::pack_bytes`] | the byte count | as the stream is consumed, while the network is the bottleneck |
642/// | [`PackProgress::indexed`] | the numerator | once per object, as the engine's pack walk splits them |
643///
644/// # Why the numerator arrives from the other side of the seam
645///
646/// `gunnar-wire` reads bytes 8..12 of the header and can publish `(0, N)`
647/// before an object is stored — twelve bytes, and it is the reason the
648/// denominator is live at all. It cannot publish the numerator: counting
649/// objects as they pass means inflating every one of them, which is a second
650/// pack decoder, and gunnar has exactly one (one implementation, and `fetch_pack`'s own
651/// module doc refuses it in those words). The one decoder is the storage
652/// engine's walk, which has to split every entry anyway, so the count is free
653/// exactly there and nowhere else. This type is where the two halves meet.
654///
655/// # `total` is remembered here, and that is not a second counter
656///
657/// [`PackProgress::indexed`] carries its own `total` — the count the engine
658/// read from the same header — and it is *checked* against the one the wire
659/// announced rather than trusted, because two readings of one field that
660/// disagree mean the ingest is being handed a different pack from the one that
661/// was negotiated. `fetch_pack` makes the same comparison at the end, on the
662/// receipt; this is the same fact asserted while there is still a transfer to
663/// abandon.
664struct QueueProgress<'a> {
665    report: &'a Reporter<Mirror>,
666    total: AtomicU64,
667    /// A second sink, for a caller that has to see the *series* rather than the
668    /// job's latest reading. See [`sync_once_watching`].
669    watching: Option<&'a dyn git_storage_trait::PackProgress>,
670}
671
672impl QueueProgress<'_> {
673    /// **The one place a sync's progress pair is written.**
674    ///
675    /// All three publishers go through here — the header's `(0, N)`, the
676    /// engine's per-object readings, and [`finish`]'s `(N, N)` — so the job and
677    /// any watcher see the same values in the same order. Two routes to the
678    /// same field would be two places the series could differ, and the series
679    /// is the whole subject.
680    fn publish(&self, done: u64, total: u64) {
681        self.report.progress(done, total);
682        if let Some(watcher) = self.watching {
683            watcher.indexed(done, total);
684        }
685    }
686}
687
688impl FetchProgress for QueueProgress<'_> {
689    fn objects_total(&self, objects: u32) {
690        self.total.store(u64::from(objects), Ordering::SeqCst);
691        self.publish(0, u64::from(objects));
692    }
693
694    fn pack_bytes(&self, streamed: u64) {
695        self.report.report(|mirror| mirror.bytes = streamed);
696    }
697}
698
699impl git_storage_trait::PackProgress for QueueProgress<'_> {
700    fn indexed(&self, done: u64, total: u64) {
701        // The wire's denominator wins. An engine that read a different total
702        // from the same twelve bytes is a bug worth a log line, not a reason to
703        // renumber a progress bar mid-flight and leave a console watching a
704        // percentage that jumped sideways.
705        let announced = self.total.load(Ordering::SeqCst);
706        let total = if announced == 0 || announced == total {
707            total
708        } else {
709            tracing::warn!(
710                target: TARGET_GIT,
711                announced,
712                indexed_total = total,
713                "the packfile header the wire read and the one the store read declare \
714                 different object counts; the wire's is the one being rendered"
715            );
716            announced
717        };
718        self.publish(done, total);
719    }
720}
721
722// ── the fetch ────────────────────────────────────────────────────────────────
723
724/// **One sync, start to finish.** The [`crate::mirror_queue::MirrorRunner`]
725/// body.
726///
727/// Errors are `String` because they are going onto a job a console will read,
728/// not up a call stack — the same reason `gc_queue`'s runner returns one.
729pub fn sync_once(
730    stores: &Arc<ServerStores>,
731    order: &MirrorOrder,
732    credential: &Credential,
733    report: &Reporter<Mirror>,
734) -> Result<(), String> {
735    sync_once_watching(stores, order, credential, report, None)
736}
737
738/// [`sync_once`], with a **second** place the object count is published to.
739///
740/// # Why this door exists, and why it is not a test hook
741///
742/// [`Reporter::progress`] *overwrites*, by design — its own doc says a progress
743/// that accumulated would be a second counter to keep right — so the job record
744/// holds the latest reading and never the series. That is correct for a console,
745/// which only ever wants the latest reading, and it means the one thing that
746/// cannot be established from the job record afterwards is **whether the count
747/// moved at all**. A publisher that fires `(0, N)` and then `(N, N)` and a
748/// publisher that fires five hundred times in between leave the job in exactly
749/// the same state, so from the outside they are the same publisher.
750///
751/// That is measure-never-assert in its plainest form: a stub is
752/// indistinguishable from an
753/// implementation unless something can see it work — and it is precisely the
754/// defect this seam was opened to close: `(0, N) → (N, N)` was the shape for as
755/// long as no engine published from inside its pack walk. So the series gets a
756/// door, and the acceptance test stands at it. There is one other caller,
757/// [`sync_once`], which passes `None`.
758///
759/// The sink is *additional*: the job is written exactly as before, in the same
760/// order, whether or not anybody is watching. A test that changed what the
761/// production path publishes would be testing itself.
762pub fn sync_once_watching(
763    stores: &Arc<ServerStores>,
764    order: &MirrorOrder,
765    credential: &Credential,
766    report: &Reporter<Mirror>,
767    watching: Option<&dyn git_storage_trait::PackProgress>,
768) -> Result<(), String> {
769    let wire = classify(&order.upstream)?;
770    check_credential(wire, credential)?;
771    // Checked again on the worker, and not only at the door where
772    // `Mirror.Add` checked it: a registration made before this rule existed,
773    // or a name whose A record has moved since, reaches the dial through here
774    // and nowhere else.
775    check_destination(&order.upstream, stores.root())?;
776
777    // Resolved here, on the worker, and not at enqueue time: a repository can
778    // be deleted while its sync waits, and discovering that when the work
779    // starts is a FAILED job saying so, which is the truth.
780    let repo_ref = gunnar_core::resolve_repo_path(&order.path).map_err(|err| {
781        format!(
782            "`{}` is not a valid <namespace>/<name>: {err:#}",
783            order.path
784        )
785    })?;
786    let existing = stores
787        .catalog()
788        .get(&repo_ref)
789        .map_err(|err| format!("reading the catalog row for {}: {err:#}", order.path))?;
790    // **An import's repository is made HERE, by the first sync that reaches
791    // the upstream** (owner order 2026-10-18; `MirrorRecord::creates_repo`).
792    let to_create = existing.is_none()
793        && stores
794            .mirrors()
795            .get(&repo_ref)
796            .map_err(|err| format!("reading the registration for {}: {err:#}", order.path))?
797            .is_some_and(|record| record.creates_repo);
798    if existing.is_none() && !to_create {
799        return Err(format!("{} is no longer a live repository", order.path));
800    }
801    // **An encrypted repository is never fetched into** (wiring tracer B
802    // 2026-10-08): the ingest below runs no encrypted gate (`PushIngest::new`),
803    // so an upstream's plaintext would be stored and framed for the twin.
804    // `Mirror.Add` / `Repos.SetEncrypted` refuse making such a mirror; this is
805    // a registration older than that rule, or a flag set below the door.
806    if existing.as_ref().is_some_and(|row| row.encrypted) {
807        return Err(format!(
808            "{} is encrypted, so it is not fetched into: a mirror copies another server's \
809             plaintext, and an encrypted repository holds only what its members sealed \
810             (remove the mirror)",
811            order.path
812        ));
813    }
814    // **The namespace's room, asked of the same gates a push is held to**
815    // (2026-10-02). A suspended namespace or one already at its cap fetches
816    // nothing — refused here, before the dial, in the gate's own words — and
817    // the fetch is held to the tightest ceiling in the chain as it streams.
818    let ceiling = room_for(&stores.plan_gates(), repo_ref.namespace()).map_err(|(why, line)| {
819        report.report(|r| r.refused = why);
820        line
821    })?;
822
823    let auth = match credential {
824        Credential::Token { username, secret } if username.is_empty() => {
825            Some(format!("Bearer {secret}"))
826        }
827        Credential::Token { username, secret } => {
828            Some(format!("Basic {}", base64_basic(username, secret)))
829        }
830        _ => None,
831    };
832
833    let row = match existing {
834        Some(row) => row,
835        None => create_from_upstream(stores, &repo_ref, order, wire, credential, auth.clone())?,
836    };
837    let fetched = fetch_into(
838        stores, &row, order, wire, credential, auth, ceiling, report, watching,
839    )
840    .and_then(|advertised| landed(stores, &row, order, &advertised).map(|()| advertised));
841    match &fetched {
842        Ok(advertised) => after_landing(stores, &repo_ref, &row, order, advertised),
843        Err(_) if to_create => discard_created(stores, &repo_ref, order),
844        Err(_) => {}
845    }
846    fetched.map(|_| ())
847}
848
849/// **A sync is DONE only when what the upstream advertised is here**
850/// (import-big, 2026-10-26).
851///
852/// Every branch and tag the upstream advertised must be a reference in this
853/// repository when the sync says it is done, and an upstream that advertised
854/// none is not an import that worked: it is an empty import, said as a
855/// failure. Read back from the store, after the apply, rather than inferred
856/// from the plan — the plan is what was meant, this is what landed. Before it,
857/// a fetch whose plan came out empty applied nothing and settled DONE over a
858/// repository with no references at all.
859fn landed(
860    stores: &Arc<ServerStores>,
861    row: &gunnar_core::model::Repo,
862    order: &MirrorOrder,
863    advertised: &[gunnar_wire::upload_pack::AdvertisedRef],
864) -> Result<(), String> {
865    let wanted: Vec<&str> = advertised
866        .iter()
867        .filter(|r| fetch_pack::mirror(&r.name) && r.target.is_some())
868        .map(|r| r.name.as_str())
869        .collect();
870    let upstream = scrub(&order.upstream);
871    if wanted.is_empty() {
872        return Err(format!(
873            "{upstream} has no branches or tags, so there was nothing to import into {}. \
874             Check the address, or push a first commit there and sync again.",
875            order.path
876        ));
877    }
878    let store = stores
879        .repo_store(row)
880        .map_err(|err| format!("opening {} to check what landed: {err:#}", order.path))?;
881    let held = GitOps::refs(store.serve()).map_err(|err| {
882        format!(
883            "reading {}'s references to check what landed: {err:#}",
884            order.path
885        )
886    })?;
887    let missing: Vec<&str> = wanted
888        .iter()
889        .copied()
890        .filter(|name| !held.iter().any(|r| r.name == *name && r.oid.is_some()))
891        .collect();
892    if let Some(first) = missing.first() {
893        return Err(format!(
894            "the fetch from {upstream} ended without its references landing: {} of {} \
895             (first {first}) are not in {}. Nothing is reported as imported that is not \
896             here; sync again.",
897            missing.len(),
898            wanted.len(),
899            order.path
900        ));
901    }
902    Ok(())
903}
904
905/// **Ask the upstream what it is, then make the repository to match.**
906///
907/// One `ls-refs` at the upstream's own object format (`ls_remote_any`): the
908/// format becomes the repository's, and `HEAD`'s `symref-target` its default
909/// branch. Nothing is created when the upstream cannot be reached or refuses
910/// (a private repository without its token), so a failed first import leaves
911/// no repository behind.
912fn create_from_upstream(
913    stores: &Arc<ServerStores>,
914    repo_ref: &gunnar_core::RepoRef,
915    order: &MirrorOrder,
916    wire: Wire,
917    credential: &Credential,
918    auth: Option<String>,
919) -> Result<gunnar_core::model::Repo, String> {
920    let advertisement = ls_remote(stores, order, wire, credential, auth)?;
921    let hash = match advertisement.hash_kind {
922        gix_hash::Kind::Sha1 => gunnar_core::HashKind::Sha1,
923        _ => gunnar_core::HashKind::Sha256,
924    };
925    let mut new = gunnar_core::NewRepo::new(repo_ref.clone())
926        .hash_kind(hash)
927        .visibility(gunnar_core::Visibility::Private);
928    new.description = format!("mirror of {}", scrub(&order.upstream));
929    if let Some(branch) = advertised_head(&advertisement.refs) {
930        new = new.default_branch(branch);
931    }
932    tracing::info!(
933        target: TARGET_GIT,
934        event = "mirror.create_from_upstream",
935        repo = %order.path,
936        hash = ?hash,
937        "an import's repository is made at the upstream's object format and default branch"
938    );
939    let row = stores
940        .create_repo(new, false)
941        .map_err(|err| format!("creating {} for the import: {err:#}", order.path))?;
942    stores
943        .repo_store(&row)
944        .map_err(|err| format!("opening {} to mirror into it: {err:#}", order.path))?;
945    Ok(row)
946}
947
948/// **One `ls-refs` against the upstream**, at its own object format.
949fn ls_remote(
950    stores: &Arc<ServerStores>,
951    order: &MirrorOrder,
952    wire: Wire,
953    credential: &Credential,
954    auth: Option<String>,
955) -> Result<gunnar_wire::fetch_pack::Advertisement, String> {
956    let listed =
957        |err: gunnar_wire::Error| format!("asking {} what it holds: {err}", scrub(&order.upstream));
958    match wire {
959        Wire::Http => {
960            let (mut reader, mut writer) = open_http(&order.upstream, auth)?;
961            gunnar_wire::ls_remote_any(&mut reader, &mut writer).map_err(listed)
962        }
963        Wire::Ssh => dial_ssh(
964            &order.upstream,
965            credential,
966            &stores.root().join(MIRROR_KNOWN_HOSTS),
967            strict_host_keys(stores.root()),
968            |reader, writer| gunnar_wire::ls_remote_any(reader, writer),
969        )?
970        .map_err(listed),
971    }
972}
973
974/// **What an interrupted import already brought in**, offered back to the
975/// upstream as `have`s (import-big, 2026-10-26).
976///
977/// A repository that holds no branch and no tag is either new or an import
978/// that stopped between its pack landing and its references being written —
979/// a restart in the index, the longest step of a big import. The pack's bytes
980/// were durable before that step, and the indexer resumes them on its own after
981/// the restart; what was missing is only the reference write. So the resumed
982/// sync asks the upstream what it advertises, lets the index catch up, and
983/// offers every advertised tip this store already has as a `have`: the
984/// upstream then sends nothing it sent before, and a 6.6 GB kernel pack is not
985/// fetched (or stored) a second time. A new repository has nothing, and pays
986/// one `ls-refs` for finding out.
987#[allow(clippy::too_many_arguments)]
988fn already_here(
989    stores: &Arc<ServerStores>,
990    git: &dyn gunnar_store::git_storage_trait::GitServe,
991    git_dir: &std::path::Path,
992    order: &MirrorOrder,
993    wire: Wire,
994    credential: &Credential,
995    auth: Option<String>,
996    report: &Reporter<Mirror>,
997) -> Result<Vec<ObjectId>, String> {
998    let held =
999        GitOps::refs(git).map_err(|err| format!("reading {}'s references: {err:#}", order.path))?;
1000    if held
1001        .iter()
1002        .any(|r| fetch_pack::mirror(&r.name) && r.oid.is_some())
1003    {
1004        return Ok(Vec::new());
1005    }
1006    let advertisement = ls_remote(stores, order, wire, credential, auth)?;
1007    // An interrupted import's pack is indexed here: held to a cgroup's memory as the
1008    // first try was (IMPORT-2); its object count is not known again, so no refusal.
1009    let held = crate::import_room::MemoryLimit::read()
1010        .and_then(|l| CgroupHold::start(stores, git_dir, &l));
1011    index_landed(git, git_dir, stores.engine(), report, held.as_ref()).map_err(|err| {
1012        format!(
1013            "{} holds objects this server could not index: {err}",
1014            order.path
1015        )
1016    })?;
1017    let mut out = Vec::new();
1018    for advertised in &advertisement.refs {
1019        let Some(target) = advertised.target else {
1020            continue;
1021        };
1022        if !fetch_pack::mirror(&advertised.name) || out.contains(&target) {
1023            continue;
1024        }
1025        if GitOps::has(git, target.as_bytes()).unwrap_or(false) {
1026            out.push(target);
1027        }
1028    }
1029    if !out.is_empty() {
1030        tracing::info!(
1031            target: TARGET_GIT,
1032            event = "mirror.sync.resumed",
1033            repo = %order.path,
1034            haves = out.len(),
1035            "an import that stopped after its pack landed resumes without fetching it again"
1036        );
1037    }
1038    Ok(out)
1039}
1040
1041/// `HEAD`'s branch, as the upstream advertised it, when it is a branch.
1042fn advertised_head(refs: &[gunnar_wire::upload_pack::AdvertisedRef]) -> Option<String> {
1043    refs.iter()
1044        .find(|r| r.name == "HEAD")
1045        .and_then(|r| r.symref_target.clone())
1046        .filter(|target| {
1047            target.starts_with("refs/heads/") && gunnar_store::refname::validate(target).is_ok()
1048        })
1049}
1050
1051/// A first import that failed after its repository was made: the repository
1052/// goes again, so nothing empty is left behind. Best effort — a removal that
1053/// fails is logged, and the registration still says the repository is to come.
1054fn discard_created(
1055    stores: &Arc<ServerStores>,
1056    repo_ref: &gunnar_core::RepoRef,
1057    order: &MirrorOrder,
1058) {
1059    let removed = stores
1060        .catalog()
1061        .delete(repo_ref)
1062        .map_err(|err| format!("{err:#}"))
1063        .and_then(|_| {
1064            stores
1065                .purge_repo(repo_ref)
1066                .map_err(|err| format!("{err:#}"))
1067        });
1068    if let Err(err) = removed {
1069        tracing::warn!(
1070            target: TARGET_GIT,
1071            repo = %order.path,
1072            %err,
1073            "a failed first import could not remove the repository it had made"
1074        );
1075    }
1076}
1077
1078/// **What a landed sync owes the mirror besides its references**: the
1079/// upstream's default branch (when it holds that branch now) and whether the
1080/// upstream uses Git LFS. Neither fails the sync — the references landed.
1081fn after_landing(
1082    stores: &Arc<ServerStores>,
1083    repo_ref: &gunnar_core::RepoRef,
1084    row: &gunnar_core::model::Repo,
1085    order: &MirrorOrder,
1086    advertised: &[gunnar_wire::upload_pack::AdvertisedRef],
1087) {
1088    let mut row = row.clone();
1089    if let Some(branch) = advertised_head(advertised) {
1090        let held = stores
1091            .repo_store(&row)
1092            .ok()
1093            .and_then(|store| GitOps::refs(store.serve()).ok())
1094            .is_some_and(|refs| refs.iter().any(|r| r.name == branch && r.oid.is_some()));
1095        if held && branch != row.default_branch {
1096            match stores.catalog().set_default_branch(repo_ref, &branch) {
1097                Ok(moved) => {
1098                    row = moved;
1099                    if let Err(err) = stores.repoint_head(&row) {
1100                        tracing::warn!(target: TARGET_GIT, repo = %order.path, %err, "pointing HEAD at the upstream's default branch");
1101                    }
1102                }
1103                Err(err) => {
1104                    tracing::warn!(target: TARGET_GIT, repo = %order.path, %err, "taking the upstream's default branch");
1105                }
1106            }
1107        }
1108    }
1109    let lfs = uses_lfs(stores, &row);
1110    if let Err(err) = stores.mirrors().set_lfs_detected(repo_ref, lfs) {
1111        tracing::warn!(target: TARGET_GIT, repo = %order.path, %err, "recording whether the upstream uses LFS");
1112    }
1113}
1114
1115/// **Does the default branch route files through Git LFS?** A `filter=lfs`
1116/// in its root `.gitattributes` is the sign every LFS repository carries; the
1117/// files it names arrive as pointer files, and their content is not mirrored.
1118fn uses_lfs(stores: &Arc<ServerStores>, row: &gunnar_core::model::Repo) -> bool {
1119    use gunnar_store::git_storage_trait::ObjType;
1120    use gunnar_store::object;
1121    let Ok(store) = stores.repo_store(row) else {
1122        return false;
1123    };
1124    let git = store.serve();
1125    let hash = store.hash_kind();
1126    let Some(tip) = GitOps::refs(git)
1127        .ok()
1128        .and_then(|refs| refs.into_iter().find(|r| r.name == row.default_branch))
1129        .and_then(|r| r.oid)
1130        .and_then(|raw| ObjectId::try_from(&raw[..]).ok())
1131    else {
1132        return false;
1133    };
1134    let read = |id: &ObjectId, want: ObjType| match git.read(id.as_bytes()) {
1135        Ok(Some((kind, bytes))) if kind == want => Some(bytes),
1136        _ => None,
1137    };
1138    let Some(commit) =
1139        read(&tip, ObjType::Commit).and_then(|bytes| object::commit(&bytes, hash).ok())
1140    else {
1141        return false;
1142    };
1143    let Some(tree) = read(&commit.tree, ObjType::Tree) else {
1144        return false;
1145    };
1146    let Ok(Some(entry)) = object::tree_entry_named(&tree, hash, b".gitattributes") else {
1147        return false;
1148    };
1149    read(&entry.oid, ObjType::Blob).is_some_and(|attributes| {
1150        String::from_utf8_lossy(&attributes)
1151            .lines()
1152            .any(|line| !line.trim_start().starts_with('#') && line.contains("filter=lfs"))
1153    })
1154}
1155
1156/// **The fetch and the apply**, into a repository that exists. Answers what
1157/// the upstream advertised, for [`after_landing`].
1158#[allow(clippy::too_many_arguments)]
1159fn fetch_into(
1160    stores: &Arc<ServerStores>,
1161    row: &gunnar_core::model::Repo,
1162    order: &MirrorOrder,
1163    wire: Wire,
1164    credential: &Credential,
1165    auth: Option<String>,
1166    ceiling: Option<gunnar_wire::PackCeiling>,
1167    report: &Reporter<Mirror>,
1168    watching: Option<&dyn git_storage_trait::PackProgress>,
1169) -> Result<Vec<gunnar_wire::upload_pack::AdvertisedRef>, String> {
1170    let row = row.clone();
1171    let store = stores
1172        .repo_store(&row)
1173        .map_err(|err| format!("opening {} to mirror into it: {err:#}", order.path))?;
1174    let git = store.serve();
1175    let hash_kind = store.hash_kind();
1176
1177    // Opened BEFORE the ingest, and that ordering is load-bearing for the same
1178    // reason it is on the push path: whether this repository has a protection
1179    // rule at all is what decides if the fetch pays for an object-set
1180    // enumeration.
1181    let protection = Protection::open(stores, &row, store.git_dir())?;
1182
1183    let extra_haves = already_here(
1184        stores,
1185        git,
1186        store.git_dir(),
1187        order,
1188        wire,
1189        credential,
1190        auth.clone(),
1191        report,
1192    )?;
1193    let local = StoreRefs { git, extra_haves };
1194    let progress = QueueProgress {
1195        report,
1196        total: AtomicU64::new(0),
1197        watching,
1198    };
1199    // The ingest a PUSH lands through, unchanged. A fetched pack and a pushed
1200    // pack are the same bytes (`fetch_pack`'s module doc), so this is the same
1201    // storage authority and not a second one.
1202    //
1203    // The object set is asked for exactly when something reads it. A repository
1204    // with a protection rule has a gate that does (`require_signature` walks the
1205    // commits this fetch introduced), so the enumeration is `Required` and an
1206    // engine that cannot produce one must refuse rather than let the gate decide
1207    // on nothing. With no rule configured nobody reads it, and `NotNeeded` keeps
1208    // the fetch from waiting on index work no answer depends on.
1209    //
1210    // The last argument is the numerator's whole route: the ingest hands it to
1211    // the engine, the engine hands it to the pack walk, and the walk publishes
1212    // once per object into `progress` above — which is the same value the wire
1213    // is publishing the denominator and the byte count into. One sink, three
1214    // publishers, no second decoder anywhere on the path.
1215    let ingest = crate::transport::PushIngest::new(
1216        store.git_dir().to_path_buf(),
1217        hash_kind,
1218        stores.engine(),
1219        if protection.is_some() {
1220            crate::storage::ObjectSet::Required
1221        } else {
1222            crate::storage::ObjectSet::NotNeeded
1223        },
1224        Some(&progress),
1225        // A fetched pack is a transaction exactly as a pushed one is: framed before it lands.
1226        Some(crate::transport::PackFrame::of(stores, &row)),
1227    )
1228    // …and held to the data set's room the way a push is (cov-13).
1229    .with_space(stores.space_ledger());
1230
1231    let mut fetch = FetchPack::new(hash_kind, &fetch_pack::mirror, &local).with_progress(&progress);
1232    if let Some(ceiling) = ceiling {
1233        fetch = fetch.with_ceiling(ceiling);
1234    }
1235
1236    let outcome = match wire {
1237        Wire::Http => {
1238            let (mut reader, mut writer) = open_http(&order.upstream, auth)?;
1239            fetch.fetch(&ingest, &mut reader, &mut writer)
1240        }
1241        Wire::Ssh => dial_ssh(
1242            &order.upstream,
1243            credential,
1244            &stores.root().join(MIRROR_KNOWN_HOSTS),
1245            strict_host_keys(stores.root()),
1246            |reader, writer| fetch.fetch(&ingest, reader, writer),
1247        )?,
1248    }
1249    .map_err(|err| {
1250        // Cut at the namespace's ceiling: the plan's refusal, as a field.
1251        if matches!(err, gunnar_wire::Error::OverCeiling(_)) {
1252            report.report(|r| r.refused = crate::mirror_queue::Refusal::PackCap);
1253        }
1254        format!(
1255            "fetching {} from {}: {err}",
1256            order.path,
1257            scrub(&order.upstream)
1258        )
1259    })?;
1260
1261    // **Index the fetched pack ONCE, here, before a single read by oid**
1262    // (import-big, 2026-10-26) — the line `receive_pack::apply` has carried
1263    // since 2026-08-30 and this path never got.
1264    //
1265    // The znippy engine stores a pack's bytes before its oids exist and indexes
1266    // them behind the ack. `apply` reads by oid (the branch-protection gate, and
1267    // `put_refs_cas`, whose dangling-target check asks `has` for every new tip),
1268    // so without this its first read fell into the engine's read-time fallback:
1269    // a whole-pack resolve on this thread beside the indexer resolving the same
1270    // bytes. MEASURED on oden 2026-10-26, torvalds/linux from GitHub (6.60 GB,
1271    // 11 986 735 objects): the import landed only after the two resolvers had
1272    // driven the process to a 19.4 GB peak — past a 16 GB Mini, which is where
1273    // the owner's import of it died with nothing to say so.
1274    //
1275    // A refusal is the backend's own sentence (its memory watchdog gives up by
1276    // name, with what was available and what was needed): the pack is stored
1277    // and could not be indexed on this box, which is "this repository does not
1278    // fit here", never a sync that waits for ever or reads around it.
1279    if outcome.receipt.is_some() {
1280        // **IMPORT-2: the index must fit the memory this server may really use**: the box's
1281        // MemTotal, or the cgroup's memory.max when that is smaller (a container, a
1282        // MemoryMax= unit, where /proc/meminfo is the host's). Refused by name before
1283        // the index when it does not fit with the margin; under a cgroup the store's
1284        // memory guard reads the cgroup's memory, kept fresh while the index runs.
1285        let objects = progress.total.load(Ordering::SeqCst);
1286        let limit = crate::import_room::MemoryLimit::read();
1287        if let Some(limit) = &limit {
1288            crate::import_room::import_room(&order.path, objects, limit)?;
1289        }
1290        let held = limit
1291            .as_ref()
1292            .and_then(|l| CgroupHold::start(stores, store.git_dir(), l));
1293        index_landed(git, store.git_dir(), stores.engine(), report, held.as_ref()).map_err(
1294            |err| {
1295                format!(
1296                "{} was fetched and stored, and this server could not index it: {err}. Nothing \
1297                 was moved. The repository is larger than this server can take in; a server \
1298                 with more memory imports it.",
1299                order.path
1300            )
1301            },
1302        )?;
1303    }
1304    apply(git, order, &outcome, &progress, protection.as_ref())?;
1305    Ok(outcome.advertised)
1306}
1307
1308/// **`GitServe::index_now`, with the index's own progress on the job.**
1309///
1310/// For a kernel-sized pack the index is minutes after the last byte arrived,
1311/// and a job that sat at `N/N` for all of them would read as stalled. So while
1312/// it runs, the engine's index row for this repository (`Engine::index_progress`,
1313/// the same row the dashboard reads) is published as the job's progress: the
1314/// objects indexed of the pack's total. When it returns the pair is the walk's
1315/// again (`finish`).
1316fn index_landed(
1317    git: &dyn gunnar_store::git_storage_trait::GitServe,
1318    git_dir: &std::path::Path,
1319    engine: crate::storage::Engine,
1320    report: &Reporter<Mirror>,
1321    held: Option<&CgroupHold>,
1322) -> Result<usize, String> {
1323    let indexed = indexed_with_progress(
1324        || git.index_now().map_err(|err| format!("{err:#}")),
1325        || {
1326            if let Some(held) = held {
1327                held.refresh();
1328            }
1329            if let Some((_, row)) = engine
1330                .index_progress()
1331                .into_iter()
1332                .find(|(dir, row)| dir == git_dir && row.active && row.objects_total > 0)
1333            {
1334                report.progress(row.objects_done, row.objects_total);
1335            }
1336        },
1337    );
1338    if let Ok(packs) = &indexed {
1339        tracing::info!(
1340            target: TARGET_GIT,
1341            event = "mirror.sync.indexed",
1342            packs,
1343            "the fetched pack was indexed once, before the references were checked"
1344        );
1345    }
1346    indexed
1347}
1348
1349/// **The cgroup's memory, held under one import's index** (IMPORT-2).
1350///
1351/// Only when the limit is a cgroup's: the store's memory guard is pointed at a
1352/// meminfo-shaped file of the cgroup's memory (`import_room::cgroup_meminfo`), refreshed
1353/// every half second by the index's progress poller, with the import's margin as the
1354/// floor. Dropped after the index: the file goes and the store gets the process-wide
1355/// guard back, so a later push into the same repository reads no stale file.
1356struct CgroupHold {
1357    engine: crate::storage::Engine,
1358    git_dir: std::path::PathBuf,
1359    cgroup: std::path::PathBuf,
1360    file: std::path::PathBuf,
1361}
1362
1363impl CgroupHold {
1364    fn start(
1365        stores: &Arc<ServerStores>,
1366        git_dir: &std::path::Path,
1367        limit: &crate::import_room::MemoryLimit,
1368    ) -> Option<CgroupHold> {
1369        let cgroup = limit.cgroup_dir()?.to_path_buf();
1370        let dir = stores.root().join("import-memory");
1371        std::fs::create_dir_all(&dir).ok()?;
1372        let name: String = git_dir
1373            .to_string_lossy()
1374            .chars()
1375            .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
1376            .collect();
1377        let held = CgroupHold {
1378            engine: stores.engine(),
1379            git_dir: git_dir.to_path_buf(),
1380            cgroup,
1381            file: dir.join(format!("{name}.meminfo")),
1382        };
1383        if !held.refresh() {
1384            return None;
1385        }
1386        let floor = limit.bytes / 100 * crate::import_room::MARGIN_PERCENT;
1387        if !held
1388            .engine
1389            .hold_index_memory(&held.git_dir, Some((&held.file, floor)))
1390        {
1391            let _ = std::fs::remove_file(&held.file);
1392            return None;
1393        }
1394        tracing::info!(
1395            target: TARGET_GIT,
1396            event = "mirror.import.memory_held",
1397            repo = %held.git_dir.display(),
1398            limit = limit.bytes,
1399            floor,
1400            "the import's index reads this server's cgroup memory ({}) and pauses under {}",
1401            limit.words(),
1402            crate::budget::human(floor)
1403        );
1404        Some(held)
1405    }
1406
1407    /// Write the cgroup's memory now (a temp file and a rename, so the guard never
1408    /// reads half a file).
1409    fn refresh(&self) -> bool {
1410        let Some(c) = crate::import_room::cgroup_memory_in(&self.cgroup) else {
1411            return false;
1412        };
1413        let tmp = self.file.with_extension("meminfo.tmp");
1414        std::fs::write(&tmp, crate::import_room::cgroup_meminfo(&c)).is_ok()
1415            && std::fs::rename(&tmp, &self.file).is_ok()
1416    }
1417}
1418
1419impl Drop for CgroupHold {
1420    fn drop(&mut self) {
1421        self.engine.hold_index_memory(&self.git_dir, None);
1422        let _ = std::fs::remove_file(&self.file);
1423    }
1424}
1425
1426/// **`index` on this thread while `progress` is asked every half second on a scoped one**
1427/// ([`index_landed`]). The poller stops when the index ends, however it ends: a panic in
1428/// the index is an `Err` naming it (RA4-9).
1429fn indexed_with_progress(
1430    index: impl FnOnce() -> Result<usize, String>,
1431    progress: impl Fn() + Sync,
1432) -> Result<usize, String> {
1433    let done = std::sync::atomic::AtomicBool::new(false);
1434    std::thread::scope(|scope| {
1435        // A32-14, stays a scoped thread: it reads the index row for the length of one
1436        // `index_now` on the mirror job's own lane and is joined before this returns.
1437        scope.spawn(|| {
1438            while !done.load(Ordering::SeqCst) {
1439                progress();
1440                for _ in 0..10 {
1441                    if done.load(Ordering::SeqCst) {
1442                        return;
1443                    }
1444                    std::thread::sleep(std::time::Duration::from_millis(50));
1445                }
1446            }
1447        });
1448        // RA4-9: a panic in the index is caught here, so `done` is set however it ends and the
1449        // scope (which waits on the poller) returns; the mirror job then fails by name.
1450        let indexed = std::panic::catch_unwind(std::panic::AssertUnwindSafe(index));
1451        done.store(true, Ordering::SeqCst);
1452        indexed.unwrap_or_else(|panic| {
1453            let words = panic
1454                .downcast_ref::<&str>()
1455                .map(|s| s.to_string())
1456                .or_else(|| panic.downcast_ref::<String>().cloned())
1457                .unwrap_or_else(|| "a panic with no words".into());
1458            Err(format!("the index panicked: {words}"))
1459        })
1460    })
1461}
1462
1463/// **How much a mirror sync into `namespace` may fetch, decided by the front
1464/// doors' own plan gates** ([`ServerStores::install_plan_gates`]).
1465///
1466/// The push path's questions, in its order (`gunnar_ssh::server`'s
1467/// `ask_plan`): the gate's verdict on a `Write`, then — for a write that grows
1468/// data, which a fetch is — `pack_quota` (is the namespace already at its
1469/// cap?) and the TIGHTEST `pack_ceiling` in the chain. The first `Refuse` is
1470/// the answer, verbatim. The ceiling is bound to the namespace's shared pack
1471/// ledger, marked here before any gate measured, so a mirror fetch and a push
1472/// landing at the same moment share the room instead of each spending it.
1473///
1474/// `Ok(None)` is "nothing caps it": a chain with no gate that answers, which is
1475/// the community appliance's ordinary state.
1476///
1477/// A refusal comes back with WHICH question refused it — the plan's verdict
1478/// on a write ([`crate::mirror_queue::Refusal::Plan`]) or the pack room
1479/// ([`crate::mirror_queue::Refusal::PackCap`]) — so the job can say so as a
1480/// field (`MirrorJob.refused_reason`).
1481pub(crate) fn room_for(
1482    gates: &[Arc<dyn gunnar_ssh::PlanGate>],
1483    namespace: &str,
1484) -> Result<Option<gunnar_wire::PackCeiling>, (crate::mirror_queue::Refusal, String)> {
1485    use gunnar_ssh::{PlanAction, PlanVerdict};
1486    if gates.is_empty() {
1487        return Ok(None);
1488    }
1489    // Taken BEFORE any gate measures, for the reason the push path gives:
1490    // only one of the two roundings is a quota that leaks.
1491    let mark = gunnar_lfs::AllowanceLedger::shared_pack().mark(namespace);
1492    let mut tightest: Option<gunnar_ssh::PackCeiling> = None;
1493    for gate in gates {
1494        if let PlanVerdict::Refuse(line) = gate.verdict(namespace, PlanAction::Write) {
1495            return Err((crate::mirror_queue::Refusal::Plan, line));
1496        }
1497        if let PlanVerdict::Refuse(line) = gate.pack_quota(namespace) {
1498            return Err((crate::mirror_queue::Refusal::PackCap, line));
1499        }
1500        if let Some(asked) = gate.pack_ceiling(namespace) {
1501            if tightest
1502                .as_ref()
1503                .is_none_or(|held| asked.bytes < held.bytes)
1504            {
1505                tightest = Some(asked);
1506            }
1507        }
1508    }
1509    Ok(tightest.map(|ceiling| gunnar_wire::PackCeiling {
1510        bytes: ceiling.bytes,
1511        refusal: ceiling.refusal,
1512        ledger: Some(
1513            Arc::new(crate::transport::PackMark { mark }) as Arc<dyn gunnar_wire::PackLedger>
1514        ),
1515    }))
1516}
1517
1518/// **Apply the plan, in one transaction, against the values the fetch
1519/// reasoned about.**
1520///
1521/// See the module doc for why this is `put_refs_cas` and why a rejection is a
1522/// failure rather than a skip.
1523fn apply(
1524    git: &dyn gunnar_store::git_storage_trait::GitServe,
1525    order: &MirrorOrder,
1526    outcome: &FetchReport,
1527    progress: &QueueProgress<'_>,
1528    protection: Option<&Protection>,
1529) -> Result<(), String> {
1530    let zero = "0".repeat(outcome.hash_kind.len_in_hex());
1531
1532    // Owned first, because `RefCas` borrows its oids and the batch has to
1533    // outlive every one of them.
1534    // (name, old, new, peeled)
1535    type OwnedUpdate = (String, Option<Vec<u8>>, Option<Vec<u8>>, Option<Vec<u8>>);
1536    let mut owned: Vec<OwnedUpdate> = Vec::new();
1537    let mut updates: Vec<(String, String, String)> = Vec::new();
1538
1539    for plan in &outcome.updates {
1540        // **The name came off somebody else's wire, so it goes through the ref
1541        // name rules before it is written.**
1542        //
1543        // `plan.name` is the string an upstream's `ls-refs` answered with,
1544        // carried untouched from `fetch_pack`, and the only filter it passed on
1545        // the way is `fetch_pack::mirror`'s prefix test — which admits
1546        // `refs/heads/../../etc/passwd` exactly as readily as `refs/heads/main`.
1547        // Receive-pack puts every client-supplied name through
1548        // `gunnar_store::refname::validate` at the same point in its own path
1549        // (`receive_pack.rs`'s `funny refname`), and a fetch is the same
1550        // untrusted input arriving through a different door.
1551        //
1552        // It is checked HERE rather than relied on from the storage engine. The
1553        // memory backend does validate in `put_refs_cas`, so a name like this
1554        // would be refused there today — but that is a property of one engine,
1555        // and "a rule one backend enforces by accident is not a rule" is the
1556        // sentence `refname` itself is written under. The engines are swappable
1557        // (`GUNNAR_ENGINE`), and this is the one caller that can guarantee the
1558        // check ran.
1559        //
1560        // Only the names that came from the upstream. The prune half below
1561        // reads its names out of this repository's OWN refs, and a bad name
1562        // already in the store is one a prune should be able to delete rather
1563        // than one it must refuse to touch.
1564        gunnar_store::refname::validate(plan.name.as_bytes()).map_err(|err| {
1565            format!(
1566                "the upstream advertised a reference name gunnar will not write: {err}. Nothing \
1567                 was moved. A name is not a path, and this one would not have been a name."
1568            )
1569        })?;
1570        // The peel the remote already sent, rather than a peel computed here.
1571        // `ls-refs` was asked with `peel`, so an annotated tag arrived with the
1572        // commit it resolves to; recomputing it would be an object read per tag
1573        // for an answer already in hand (the tag re-peel, from the other side).
1574        let peeled = outcome
1575            .advertised
1576            .iter()
1577            .find(|advertised| advertised.name == plan.name)
1578            .and_then(|advertised| advertised.peeled)
1579            .map(|oid| oid.as_bytes().to_vec());
1580        updates.push((
1581            plan.name.clone(),
1582            plan.old
1583                .map(|old| old.to_string())
1584                .unwrap_or_else(|| zero.clone()),
1585            plan.new.to_string(),
1586        ));
1587        owned.push((
1588            plan.name.clone(),
1589            plan.old.map(|old| old.as_bytes().to_vec()),
1590            Some(plan.new.as_bytes().to_vec()),
1591            peeled,
1592        ));
1593    }
1594
1595    if order.prune {
1596        // **What the upstream no longer has.** Only over the same namespace the
1597        // ref plan covers (`refs/heads/*`, `refs/tags/*`): a prune that reached
1598        // wider would delete this box's own `refs/gunnar/*` bookkeeping on the
1599        // grounds that an upstream never advertised it.
1600        let held = GitOps::refs(git)
1601            .map_err(|err| format!("reading {}'s refs to prune them: {err:#}", order.path))?;
1602        for row in held {
1603            if !fetch_pack::mirror(&row.name) {
1604                continue;
1605            }
1606            if outcome
1607                .advertised
1608                .iter()
1609                .any(|advertised| advertised.name == row.name)
1610            {
1611                continue;
1612            }
1613            let Some(old) = row.oid else { continue };
1614            updates.push((row.name.clone(), hex(&old), zero.clone()));
1615            owned.push((row.name, Some(old), None, None));
1616        }
1617    }
1618
1619    if owned.is_empty() {
1620        finish(outcome, progress, Vec::new());
1621        return Ok(());
1622    }
1623
1624    // **The gate, between the plan and the write.** Before this line the batch
1625    // below was the one reference write on the box that no gate had seen; a
1626    // repository with no protection rule configured has no `Protection` at all,
1627    // so a mirror of an unprotected repository pays nothing for this.
1628    if let Some(protection) = protection {
1629        let gated: Vec<gunnar_policy::RefUpdate> = owned
1630            .iter()
1631            .map(|(name, old, new, _)| {
1632                // The null-oid spelling never appears here: `owned` already
1633                // carries absence as `None`, which is what `RefUpdate`'s three
1634                // constructors take. `try_from_bytes` cannot fail on a value the
1635                // store or the wire produced, and an oid this store cannot
1636                // decode is the one `StoreRefs::tips` already refuses by name.
1637                let decode = |raw: &Vec<u8>| {
1638                    gix_hash::oid::try_from_bytes(raw)
1639                        .map(gix_hash::oid::to_owned)
1640                        .map_err(|err| {
1641                            format!("{name} holds an oid this server cannot decode: {err}")
1642                        })
1643                };
1644                Ok(gunnar_policy::RefUpdate {
1645                    name: name.clone(),
1646                    old: old.as_ref().map(&decode).transpose()?,
1647                    new: new.as_ref().map(&decode).transpose()?,
1648                })
1649            })
1650            .collect::<Result<_, String>>()?;
1651        let new_objects = match outcome.receipt.as_ref() {
1652            // Nothing was fetched, so nothing was introduced. That is a measured
1653            // empty set and not an unasked question — see `FetchReport::receipt`.
1654            None => Ok(Vec::new()),
1655            Some(receipt) => receipt.new_objects.clone(),
1656        };
1657        protection.check(git, &gated, new_objects.as_deref().map_err(String::as_str))?;
1658    }
1659
1660    let batch: Vec<RefCas<'_>> = owned
1661        .iter()
1662        .map(|(name, old, new, peeled)| RefCas {
1663            name: name.clone(),
1664            old: old.as_deref(),
1665            new: new.as_deref(),
1666            peeled: peeled.clone(),
1667        })
1668        .collect();
1669
1670    // ONE call. Every edit or none — a loop with a break leaves what it already
1671    // applied applied, and a half-mirrored repository is a state no upstream
1672    // ever had.
1673    GitOps::put_refs_cas(git, &batch).map_err(|err| {
1674        format!(
1675            "the fetch succeeded and the reference update did not: {err:#}. Nothing was moved. \
1676             The usual cause is a push that landed on {} while the fetch was running; the \
1677             expected values came from the snapshot the fetch negotiated against, and \
1678             overwriting a concurrent push is the one thing a mirror must never do. Sync again.",
1679            order.path
1680        )
1681    })?;
1682
1683    tracing::info!(
1684        target: TARGET_GIT,
1685        event = "mirror.sync.applied",
1686        repo = %order.path,
1687        refs = updates.len(),
1688        objects = outcome.objects_done,
1689        bytes = outcome.pack_bytes,
1690        "a mirror sync moved this repository's references"
1691    );
1692
1693    finish(outcome, progress, updates);
1694    Ok(())
1695}
1696
1697/// **Close the job's numbers out**, on both paths out of [`apply`].
1698///
1699/// One function and not two copies, because the pair below is the whole of what
1700/// a console reads when a sync ends and the two paths differ only in whether
1701/// any reference moved.
1702///
1703/// The `progress` call is the second half of the pair `FetchProgress` opened.
1704/// `objects_total` published `(0, N)` the instant the packfile header arrived;
1705/// **this publishes `(N, N)`**, and without it a finished job sits for ever at
1706/// nought per cent of a total it knows — which is worse than no progress bar,
1707/// because it reads as a transfer that stalled. `fetch_pack` has already
1708/// refused the transfer if the declared count and the indexed count disagree,
1709/// so `objects_done` is the same `N` and this cannot report a completion the
1710/// store did not make.
1711///
1712/// A fetch that wanted nothing — the ordinary answer for a poll of an idle
1713/// upstream — has no header, no `N`, and therefore **no progress at all**: the
1714/// pair stays `None`, and a caller reads `has_progress = false` rather than a
1715/// fabricated `0/0`.
1716fn finish(
1717    outcome: &FetchReport,
1718    progress: &QueueProgress<'_>,
1719    updates: Vec<(String, String, String)>,
1720) {
1721    if outcome.receipt.is_some() {
1722        let total = u64::from(outcome.objects_total);
1723        progress.publish(u64::from(outcome.objects_done), total);
1724    }
1725    progress.report.report(|mirror| {
1726        mirror.objects = u64::from(outcome.objects_done);
1727        mirror.bytes = outcome.pack_bytes;
1728        mirror.updates = updates;
1729    });
1730}
1731
1732/// Lower-case hex, the spelling `gunnar.v1.RefUpdate` uses.
1733fn hex(raw: &[u8]) -> String {
1734    let mut out = String::with_capacity(raw.len() * 2);
1735    for byte in raw {
1736        use std::fmt::Write as _;
1737        let _ = write!(out, "{byte:02x}");
1738    }
1739    out
1740}
1741
1742/// `base64(user:secret)`, the Basic-auth pair.
1743///
1744/// Written out rather than pulled in: the whole alphabet is 64 characters and
1745/// the encoder is nine lines, against a dependency that would be in the
1746/// appliance's graph for this one call. It is not a general base64 — it does
1747/// exactly the one thing, and `mirror_http`'s tests assert on the bytes it
1748/// produces.
1749fn base64_basic(user: &str, secret: &str) -> String {
1750    const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1751    let raw = format!("{user}:{secret}");
1752    let raw = raw.as_bytes();
1753    let mut out = String::with_capacity(raw.len().div_ceil(3) * 4);
1754    for chunk in raw.chunks(3) {
1755        let b = [
1756            chunk[0],
1757            chunk.get(1).copied().unwrap_or(0),
1758            chunk.get(2).copied().unwrap_or(0),
1759        ];
1760        let packed = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]);
1761        out.push(ALPHABET[(packed >> 18) as usize & 63] as char);
1762        out.push(ALPHABET[(packed >> 12) as usize & 63] as char);
1763        out.push(if chunk.len() > 1 {
1764            ALPHABET[(packed >> 6) as usize & 63] as char
1765        } else {
1766            '='
1767        });
1768        out.push(if chunk.len() > 2 {
1769            ALPHABET[packed as usize & 63] as char
1770        } else {
1771            '='
1772        });
1773    }
1774    out
1775}
1776
1777// ── the ssh half ─────────────────────────────────────────────────────────────
1778
1779/// **Dial `git-upload-pack` over SSH and hand the channel's two halves to
1780/// `run`.**
1781///
1782/// `gunnar_client::connect_upload_pack` is the fetch-side twin of the
1783/// `connect_receive_pack` replication already pushes with, over
1784/// `nornir-git`'s russh transport. There is no second dialler here and there
1785/// must not be: that transport owns the connect, the publickey exchange, the
1786/// `known_hosts` policy, the blocking bridge and — load-bearing — the `Drop`
1787/// that tears the channel down *inside* the driving runtime.
1788///
1789/// # The key is a file for the length of one dial, and then it is not
1790///
1791/// `nornir_git::ssh::SshAuth` authenticates from a **path**. gunnar's mirror
1792/// credential is sealed bytes in the control database, because a private key
1793/// sitting in the clear on `/data` is the thing that store exists to prevent.
1794/// The two are bridged by a [`tempfile::NamedTempFile`] created `0600` and
1795/// dropped — and therefore unlinked — before this function returns, on every
1796/// path out of it including a panic.
1797///
1798/// The alternative was an in-memory key API on `nornir-git`, which is the right
1799/// long-term shape, and it is a change to a crate outside this repository. It
1800/// is named here rather than left as a silent compromise.
1801#[cfg(feature = "replication")]
1802fn dial_ssh<T>(
1803    url: &str,
1804    credential: &Credential,
1805    known_hosts: &std::path::Path,
1806    strict: bool,
1807    run: impl FnOnce(&mut dyn BufRead, &mut dyn Write) -> T,
1808) -> Result<T, String> {
1809    use gunnar_client::ssh::{HostKeyPolicy, SshOptions};
1810    use std::io::BufReader;
1811
1812    let mut options = SshOptions::new();
1813    // A server has no ssh-agent and no interactive user. Offering the agent
1814    // would be offering identities nobody configured for this mirror — the
1815    // silent substitution `gunnar_client::resolve_identity` exists to forbid.
1816    options.use_agent = false;
1817
1818    // Held for the whole of `run`: dropping it unlinks the file, and the dial
1819    // reads it during `connect_upload_pack`.
1820    let key_file = match credential {
1821        Credential::SshKey(bytes) => {
1822            let mut file = tempfile::Builder::new()
1823                .prefix("gunnar-mirror-key-")
1824                .tempfile()
1825                .map_err(|err| format!("no room to materialise the mirror's ssh key: {err}"))?;
1826            #[cfg(unix)]
1827            {
1828                use std::os::unix::fs::PermissionsExt as _;
1829                std::fs::set_permissions(file.path(), std::fs::Permissions::from_mode(0o600))
1830                    .map_err(|err| {
1831                        format!("the mirror's ssh key could not be made private: {err}")
1832                    })?;
1833            }
1834            file.write_all(bytes)
1835                .and_then(|()| file.flush())
1836                .map_err(|err| format!("writing the mirror's ssh key: {err}"))?;
1837            options.key_path = Some(file.path().to_path_buf());
1838            Some(file)
1839        }
1840        // No key named means the ladder, which on a server finds nothing; that
1841        // is the honest behaviour for a public upstream over ssh and it is what
1842        // `gunnar-client` warns about at start-up.
1843        _ => None,
1844    };
1845    // Trust-on-first-use against a `known_hosts` under the data root is what
1846    // replication does when an operator has not said otherwise, and a
1847    // mirror is dialling the same kind of box. See [`MIRROR_KNOWN_HOSTS`] for
1848    // why the file is NOT `~/.ssh/known_hosts`.
1849    //
1850    // **Unless the box says otherwise**, which it could not before: the policy
1851    // was this literal and nothing else, so a first sync trusted whatever
1852    // answered on the upstream's address, on a path that is about to offer it a
1853    // private key. See [`MIRROR_STRICT_HOST_KEY`].
1854    options.host_key = if strict {
1855        HostKeyPolicy::StrictKnownHosts
1856    } else {
1857        HostKeyPolicy::TofuKnownHosts
1858    };
1859    options.known_hosts = Some(known_hosts.to_path_buf());
1860
1861    let location = nornir_git::ssh::parse_ssh_url(url)
1862        .map_err(|err| format!("{} is not an ssh url: {err}", scrub(url)))?;
1863    let mut channel = gunnar_client::connect_upload_pack(&location, &options).map_err(|err| {
1864        // `{:#}` over an `anyhow::Error`, and not `{err}`: `gunnar_client::Error`
1865        // is a thiserror enum whose `Ssh` variant Displays as the bare word
1866        // "ssh" and carries the real sentence — which host key, which policy —
1867        // as its `source`. A FAILED job reading `opening git-upload-pack on …:
1868        // ssh` tells an operator nothing they can act on, and the reason was
1869        // one formatting flag away the whole time.
1870        format!(
1871            "opening git-upload-pack on {}: {:#}",
1872            scrub(url),
1873            anyhow::Error::new(err)
1874        )
1875    })?;
1876    let out = {
1877        let (reader, writer) = channel.io();
1878        let mut reader = BufReader::new(reader);
1879        run(&mut reader, writer)
1880    };
1881    drop(key_file);
1882    Ok(out)
1883}
1884
1885/// **What a build with no ssh dialler says**, rather than what it does.
1886///
1887/// `replication` is the feature that carries `gunnar-client`'s wire half and
1888/// `nornir-git`'s russh transport; without it there is no dialler in the
1889/// binary. A mirror registered against an `ssh://` remote on such a build is a
1890/// registration that can never run, and it says so at the one place it can be
1891/// acted on — a FAILED job naming the flag — rather than failing as a
1892/// connection error that sends an operator to look at the network.
1893///
1894/// The smart-HTTP half is behind its own feature, `mirror-http` (A32-18), which every
1895/// appliance row names: [`NO_MIRROR_HTTP`] is its refusal.
1896#[cfg(not(feature = "replication"))]
1897fn dial_ssh<T>(
1898    url: &str,
1899    _credential: &Credential,
1900    _known_hosts: &std::path::Path,
1901    _strict: bool,
1902    _run: impl FnOnce(&mut dyn BufRead, &mut dyn Write) -> T,
1903) -> Result<T, String> {
1904    Err(format!(
1905        "{} is an ssh remote and this build carries no ssh dialler: it was compiled without \
1906         `--features replication`, which is what links `gunnar-client`'s russh transport. \
1907         Mirror from the upstream's `https://` URL, or use a build that has the feature.",
1908        scrub(url)
1909    ))
1910}
1911
1912/// **Take any userinfo out of a URL before it is shown to anybody.**
1913///
1914/// `https://user:token@host/path` is a legal way to write a remote and an
1915/// operator may well have written one. Everything between `//` and the last
1916/// `@` of the authority is replaced, so a token that arrived in the URL cannot
1917/// leave through an error string.
1918///
1919/// Not a parser: this runs in an error path, and a URL this cannot make sense
1920/// of is returned with the whole authority elided rather than passed through.
1921pub(crate) fn scrub(url: &str) -> String {
1922    let Some((scheme, rest)) = url.split_once("://") else {
1923        return url.to_owned();
1924    };
1925    let (authority, path) = match rest.find('/') {
1926        Some(cut) => (&rest[..cut], &rest[cut..]),
1927        None => (rest, ""),
1928    };
1929    match authority.rfind('@') {
1930        Some(cut) => format!("{scheme}://<redacted>@{}{path}", &authority[cut + 1..]),
1931        None => url.to_owned(),
1932    }
1933}
1934
1935/// **An `https://` remote's two halves** ([`crate::mirror_http`], feature `mirror-http`).
1936#[cfg(feature = "mirror-http")]
1937fn open_http(
1938    url: &str,
1939    auth: Option<String>,
1940) -> Result<(crate::mirror_http::HttpRead, crate::mirror_http::HttpWrite), String> {
1941    crate::mirror_http::open(url, auth)
1942}
1943
1944/// The refusal an `https://` remote gets in a build without `mirror-http` (A32-18): the
1945/// airgap default has no HTTP client, and says so where it can be acted on.
1946pub const NO_MIRROR_HTTP: &str = "this build carries no HTTP client: it was compiled without gunnar-server's `mirror-http` feature (the appliance rows name it)";
1947
1948#[cfg(not(feature = "mirror-http"))]
1949fn open_http(url: &str, _auth: Option<String>) -> Result<(std::io::Empty, std::io::Sink), String> {
1950    Err(format!("{} is an https remote and {NO_MIRROR_HTTP}. Mirror from the upstream's ssh:// URL, or use a build that has the feature.", scrub(url)))
1951}
1952
1953#[cfg(test)]
1954mod index_panic_tests {
1955    use super::*;
1956
1957    /// ★ RA4-9 RED: **an index that panics ends the index step with a named refusal**, at
1958    /// once — the progress poller is stopped however the index ends, so the mirror job
1959    /// reaches FAILED instead of hanging for good.
1960    #[test]
1961    fn an_index_that_panics_is_a_named_failure_not_a_hung_job() {
1962        let (tx, rx) = std::sync::mpsc::channel();
1963        std::thread::spawn(move || {
1964            let r = std::panic::catch_unwind(|| {
1965                indexed_with_progress(|| panic!("the index broke"), || {})
1966            });
1967            let _ = tx.send(r);
1968        });
1969        let r = rx
1970            .recv_timeout(std::time::Duration::from_secs(5))
1971            .expect("the index step ended within 5 s: no hang");
1972        let said = r.expect("a panic in the index is an answer, not a panic up the mirror job");
1973        let why = said.expect_err("a refusal");
1974        assert!(
1975            why.contains("the index panicked") && why.contains("the index broke"),
1976            "{why}"
1977        );
1978    }
1979}
1980
1981#[cfg(test)]
1982mod tests {
1983    use super::*;
1984
1985    /// **The userinfo scrub is the last thing between a token in a URL and the
1986    /// log.** Asserted on the bytes, because "it does not appear" is the whole
1987    /// property.
1988    /// ★ A32-18: the airgap default carries no HTTP client, so an `https://` remote is refused
1989    /// by name (and its token never shown), never dialled.
1990    #[cfg(not(feature = "mirror-http"))]
1991    #[test]
1992    fn an_https_remote_is_refused_by_name_in_a_build_without_mirror_http() {
1993        let e = open_http(
1994            "https://user:tok3n@forge.mock.invalid/ns/repo.git",
1995            Some("Bearer tok3n".into()),
1996        )
1997        .expect_err("refused");
1998        assert!(
1999            e.contains(NO_MIRROR_HTTP) && e.contains("mirror-http"),
2000            "{e}"
2001        );
2002        assert!(!e.contains("tok3n"), "{e}");
2003    }
2004
2005    #[test]
2006    fn a_url_with_userinfo_never_shows_it() {
2007        let scrubbed = scrub("https://alice:ghp_SUPERSECRET@example.test/ns/repo.git");
2008        assert!(
2009            !scrubbed.contains("ghp_SUPERSECRET"),
2010            "the token survived the scrub: {scrubbed}"
2011        );
2012        assert!(
2013            !scrubbed.contains("alice"),
2014            "the whole userinfo goes, not just the password half: {scrubbed}"
2015        );
2016        assert_eq!(scrubbed, "https://<redacted>@example.test/ns/repo.git");
2017    }
2018
2019    /// A URL with no userinfo is left exactly as the operator wrote it — an
2020    /// error that mangled the ordinary case would be worse than no scrub.
2021    #[test]
2022    fn a_plain_url_is_untouched() {
2023        let url = "https://example.test/ns/repo.git";
2024        assert_eq!(scrub(url), url);
2025        assert_eq!(
2026            scrub("ssh://git@example.test/ns/repo.git"),
2027            "ssh://<redacted>@example.test/ns/repo.git"
2028        );
2029    }
2030
2031    /// **Both transports are recognised from the URL alone**, and the scp-like
2032    /// form is ssh — the shape every forge prints on its clone button.
2033    #[test]
2034    fn a_remote_url_names_its_transport() {
2035        assert_eq!(classify("ssh://git@host/ns/repo.git"), Ok(Wire::Ssh));
2036        assert_eq!(classify("git+ssh://git@host/ns/repo.git"), Ok(Wire::Ssh));
2037        assert_eq!(classify("git@host:ns/repo.git"), Ok(Wire::Ssh));
2038        assert_eq!(classify("https://host/ns/repo.git"), Ok(Wire::Http));
2039        assert_eq!(
2040            classify("http://127.0.0.1:9418/ns/repo.git"),
2041            Ok(Wire::Http)
2042        );
2043    }
2044
2045    /// A remote gunnar cannot dial is refused when it is REGISTERED, not on the
2046    /// first sync — and the refusal names the two forms that work.
2047    #[test]
2048    fn a_remote_that_is_neither_is_refused_by_name() {
2049        let err = classify("/srv/git/ns/repo.git").expect_err("a local path is not a remote");
2050        assert!(err.contains("ssh://") && err.contains("https://"), "{err}");
2051        let err = classify("git://host/ns/repo.git").expect_err("the dumb git protocol");
2052        assert!(err.contains("scheme gunnar cannot mirror from"), "{err}");
2053    }
2054
2055    /// **The mismatch is refused by name, in both directions.**
2056    ///
2057    /// The failure being prevented is not an error — it is a 401 (or an SSH
2058    /// authentication failure) against an operator who can see that a
2059    /// credential IS stored, and who will therefore rotate a key that was never
2060    /// the problem.
2061    #[test]
2062    fn a_credential_of_the_wrong_kind_is_refused_by_name() {
2063        let key = Credential::SshKey(b"-----BEGIN OPENSSH PRIVATE KEY-----".to_vec());
2064        let token = Credential::Token {
2065            username: String::new(),
2066            secret: "ghp_SECRET".to_owned(),
2067        };
2068
2069        let err = check_credential(Wire::Http, &key).expect_err("a key cannot ride on http");
2070        assert!(
2071            err.contains("smart http") && err.contains("ssh private key"),
2072            "{err}"
2073        );
2074        assert!(
2075            !err.contains("BEGIN OPENSSH"),
2076            "the refusal quoted the key: {err}"
2077        );
2078
2079        let err = check_credential(Wire::Ssh, &token).expect_err("a token cannot ride on ssh");
2080        assert!(err.contains("ssh") && err.contains("a token"), "{err}");
2081        assert!(
2082            !err.contains("ghp_SECRET"),
2083            "the refusal quoted the token: {err}"
2084        );
2085
2086        // …and the matching pairs, plus the public case, are all fine.
2087        check_credential(Wire::Ssh, &key).expect("a key over ssh");
2088        check_credential(Wire::Http, &token).expect("a token over http");
2089        check_credential(Wire::Ssh, &Credential::None).expect("a public ssh upstream");
2090        check_credential(Wire::Http, &Credential::None).expect("a public http upstream");
2091    }
2092
2093    /// **The authority, out of each of the three shapes** — and the default
2094    /// port with it, because the port is half of what gets dialled.
2095    #[test]
2096    fn a_remote_url_names_the_machine_it_would_dial() {
2097        for (url, want) in [
2098            ("https://host/ns/repo.git", ("host".to_owned(), 443)),
2099            ("http://host/ns/repo.git", ("host".to_owned(), 80)),
2100            ("http://host:9418/ns/repo.git", ("host".to_owned(), 9418)),
2101            ("ssh://git@host/ns/repo.git", ("host".to_owned(), 22)),
2102            ("ssh://git@host:2222/ns/repo.git", ("host".to_owned(), 2222)),
2103            ("git@host:ns/repo.git", ("host".to_owned(), 22)),
2104            // The userinfo is not the host, and a `@` inside it must not become
2105            // one either.
2106            (
2107                "https://user:pass@host/ns/repo.git",
2108                ("host".to_owned(), 443),
2109            ),
2110            // An IPv6 literal is all colons; the brackets are what separates the
2111            // address from the port, and splitting on the first colon would
2112            // yield `[`.
2113            ("ssh://git@[::1]:2222/ns/repo.git", ("::1".to_owned(), 2222)),
2114            (
2115                "https://[2606:4700::1111]/ns/repo.git",
2116                ("2606:4700::1111".to_owned(), 443),
2117            ),
2118            ("git@[::1]:ns/repo.git", ("::1".to_owned(), 22)),
2119        ] {
2120            assert_eq!(authority(url), Ok(want.clone()), "{url}");
2121        }
2122        assert!(authority("https:///ns/repo.git").is_err(), "no host at all");
2123    }
2124
2125    /// **Every range a caller must not be able to point this server at**, and
2126    /// the two that must still work.
2127    ///
2128    /// Asserted on [`address_verdict`] rather than on [`check_destination`]
2129    /// because the addresses are handed in: no resolver runs, so this is the
2130    /// same answer on a laptop, on a build box with a corporate DNS and in a
2131    /// container with none.
2132    #[test]
2133    fn a_mirror_refuses_every_address_only_the_server_can_reach() {
2134        for addr in [
2135            "127.0.0.1",
2136            "0.0.0.0",
2137            "10.0.0.5",
2138            "172.16.0.1",
2139            "192.168.1.1",
2140            // The one the finding names: cloud instance metadata.
2141            "169.254.169.254",
2142            "100.64.0.1",
2143            "192.0.0.1",
2144            "198.18.0.1",
2145            "240.0.0.1",
2146            "255.255.255.255",
2147            "224.0.0.1",
2148            "::1",
2149            "::",
2150            "fc00::1",
2151            "fe80::1",
2152            "ff02::1",
2153            // …written the long way. Without the mapped-address arm this one
2154            // walks past every v6 test and is allowed.
2155            "::ffff:127.0.0.1",
2156            "::ffff:169.254.169.254",
2157        ] {
2158            let ip: std::net::IpAddr = addr.parse().expect("a literal");
2159            let err = address_verdict("upstream.invalid", &[ip])
2160                .expect_err("{addr} must not be dialled on a caller's say-so");
2161            assert!(
2162                err.contains(addr) || err.contains(&ip.to_string()),
2163                "{addr}: {err}"
2164            );
2165            assert!(err.contains(MIRROR_ALLOW_PRIVATE), "{addr}: {err}");
2166        }
2167
2168        for addr in ["93.184.216.34", "8.8.8.8", "2606:4700:4700::1111"] {
2169            let ip: std::net::IpAddr = addr.parse().expect("a literal");
2170            address_verdict("upstream.invalid", &[ip])
2171                .unwrap_or_else(|err| panic!("{addr} is an ordinary public address: {err}"));
2172        }
2173    }
2174
2175    /// **One bad answer out of several is a refusal.** A name that resolves to a
2176    /// routable address and a loopback address is the rebinding shape, and a
2177    /// check that took the first answer, or the best one, would pass it.
2178    #[test]
2179    fn one_private_answer_among_several_refuses_the_whole_name() {
2180        let addrs: Vec<std::net::IpAddr> = ["93.184.216.34", "127.0.0.1"]
2181            .iter()
2182            .map(|a| a.parse().expect("a literal"))
2183            .collect();
2184        let err = address_verdict("both.invalid", &addrs).expect_err("one of them is loopback");
2185        assert!(err.contains("127.0.0.1"), "{err}");
2186
2187        // And nothing at all is not "fine": a name with no address is refused
2188        // rather than allowed through to a dial that would fail anyway.
2189        assert!(address_verdict("nowhere.invalid", &[]).is_err());
2190    }
2191
2192    /// The Basic pair is encoded the way every HTTP client encodes it. Checked
2193    /// against a value anybody can verify by hand rather than against this
2194    /// function's own output.
2195    #[test]
2196    fn basic_auth_is_ordinary_base64() {
2197        assert_eq!(
2198            base64_basic("Aladdin", "open sesame"),
2199            "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
2200        );
2201        assert_eq!(base64_basic("a", "b"), "YTpi");
2202        assert_eq!(base64_basic("ab", "b"), "YWI6Yg==");
2203    }
2204}