gitbound 1.0.0

A safety-first local GitHub identity manager
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
use crate::{
    config::{Config, Profile},
    error::GitBoundError,
    git::Git,
    github::GitHub,
    process::Runner,
    remote::{RemoteInfo, RemoteProtocol},
    ssh::{self, SshIdentity},
};
use serde::{Deserialize, Serialize};
use std::path::Path;

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum CheckStatus {
    Ok,
    Warning,
    Failure,
    Unverified,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CheckItem {
    pub id: String,
    pub status: CheckStatus,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expected: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub actual: Option<String>,
    pub message: String,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum OverallStatus {
    Ok,
    Warning,
    Failure,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckReport {
    pub repository: String,
    pub profile: Option<String>,
    pub remote: Option<RemoteInfo>,
    pub overall: OverallStatus,
    pub checks: Vec<CheckItem>,
}

impl CheckReport {
    /// The roll-up rule, in one place: any failure fails, anything unresolved
    /// warns, otherwise ok.
    pub fn overall_of(checks: &[CheckItem]) -> OverallStatus {
        if checks
            .iter()
            .any(|check| check.status == CheckStatus::Failure)
        {
            OverallStatus::Failure
        } else if checks
            .iter()
            .any(|check| matches!(check.status, CheckStatus::Warning | CheckStatus::Unverified))
        {
            OverallStatus::Warning
        } else {
            OverallStatus::Ok
        }
    }

    /// Append checks produced outside the core engine — repository policy, for
    /// instance — and re-derive the verdict. Callers must not set `overall`
    /// themselves; the roll-up rule lives here so every producer agrees.
    pub fn extend(&mut self, extra: impl IntoIterator<Item = CheckItem>) {
        self.checks.extend(extra);
        self.overall = Self::overall_of(&self.checks);
    }

    /// Whether this report permits the operation it gates.
    ///
    /// `Unverified` blocks, because a check that could not be completed is not
    /// evidence that anything is correct. The single exception is an
    /// `unverified` **binding**, which only `verify` produces: a binding is a
    /// property of the developer's machine, and a pipeline checkout has none.
    /// Saying so is not the same as failing to perform a check that was needed,
    /// and treating it as fatal made `verify` impossible to pass in the place
    /// it was written for. Every other unverified check stays fatal.
    pub fn enforceable(&self) -> bool {
        !self.checks.iter().any(|check| match check.status {
            CheckStatus::Failure => true,
            CheckStatus::Unverified => check.id != BINDING,
            CheckStatus::Ok | CheckStatus::Warning => false,
        })
    }

    pub fn render_human(&self) -> String {
        let mut output = format!(
            "Repository: {}\nProfile:    {}\n",
            self.repository,
            self.profile.as_deref().unwrap_or("(none)")
        );
        if let Some(remote) = &self.remote {
            output.push_str(&format!(
                "Remote:     {} ({}/{})\n",
                remote.url, remote.owner, remote.repository
            ));
        }
        output.push('\n');
        for check in &self.checks {
            output.push_str(&format!(
                "[{:<10}] {}\n",
                format!("{:?}", check.status).to_uppercase(),
                check.message
            ));
        }
        output.push_str(&format!("\nStatus: {:?}\n", self.overall).to_uppercase());
        output
    }
}

/// The id of the binding check, named because [`CheckReport::enforceable`]
/// singles it out.
const BINDING: &str = "binding";

pub struct CheckOptions<'a> {
    pub remote_name: &'a str,
    pub network: bool,
    /// Whether a repository-local binding is something this caller can expect.
    ///
    /// True on a developer's machine, where an unbound repository is a
    /// misconfiguration. False for `verify`, which runs against a pipeline
    /// checkout that has no user configuration to bind against — there, the
    /// question the run exists to answer is the repository's committed policy,
    /// and an absent binding is reported rather than treated as a fault.
    pub expect_binding: bool,
}

pub fn inspect(
    runner: &dyn Runner,
    config: &Config,
    options: CheckOptions<'_>,
) -> Result<CheckReport, GitBoundError> {
    inspect_with_git(runner, config, Git::new(runner), options)
}

pub fn inspect_at(
    runner: &dyn Runner,
    config: &Config,
    path: &Path,
    options: CheckOptions<'_>,
) -> Result<CheckReport, GitBoundError> {
    inspect_with_git(runner, config, Git::at(runner, path), options)
}

fn inspect_with_git(
    runner: &dyn Runner,
    config: &Config,
    git: Git<'_>,
    options: CheckOptions<'_>,
) -> Result<CheckReport, GitBoundError> {
    let root = git.ensure_repo()?;
    // Read the effective (not just repository-local) profile binding.
    // Directory-scoped includeIf fragments set gitbound.profile, so
    // status/check must honour those to report accurate state. The lookup also
    // falls back to the pre-rename key, so a repository bound by GitPersona
    // reads as bound rather than silently unbound.
    let bound = git.binding_profile(false)?;
    let remote = git.remote(options.remote_name)?;
    let mut checks = Vec::new();

    let Some(profile_name) = bound.clone() else {
        if options.expect_binding {
            checks.push(item(
                BINDING,
                CheckStatus::Failure,
                None,
                None,
                "repository is not bound to a GitBound profile",
            ));
        } else {
            checks.push(item(
                BINDING,
                CheckStatus::Unverified,
                None,
                None,
                "repository is not bound to a GitBound profile; a pipeline checkout has none, so identity is judged against the committed policy alone",
            ));
        }
        // Report what is on disk even with no profile to compare it against.
        // Repository policy is written in absolute terms — an allowed domain, an
        // allowed host — so it can still be applied here, and `policy.evaluate`
        // reads these very items. Returning early instead left it with nothing
        // to read, so `policy_email` reported "no Git author email is
        // configured" for a repository that had one and was violating the rule.
        observe(
            &mut checks,
            &git,
            "git_email",
            "user.email",
            "Git author email",
        )?;
        observe(
            &mut checks,
            &git,
            "git_name",
            "user.name",
            "Git author name",
        )?;
        observe(
            &mut checks,
            &git,
            "signing_format",
            "gpg.format",
            "commit signing format",
        )?;
        observe(
            &mut checks,
            &git,
            "commit_signing",
            "commit.gpgSign",
            "commit signing",
        )?;
        if let Some(remote) = &remote {
            check_transport(&mut checks, remote);
        }
        return Ok(finish(root.display().to_string(), None, remote, checks));
    };
    checks.push(item(
        "binding",
        CheckStatus::Ok,
        Some(profile_name.clone()),
        Some(profile_name.clone()),
        "repository binding is present",
    ));

    let Some(profile) = config.profiles.get(&profile_name) else {
        checks.push(item(
            "profile",
            CheckStatus::Failure,
            Some(profile_name.clone()),
            None,
            "bound profile does not exist in the user configuration",
        ));
        return Ok(finish(
            root.display().to_string(),
            Some(profile_name),
            remote,
            checks,
        ));
    };
    check_equal(
        &mut checks,
        "git_name",
        &profile.git_name,
        git.get("user.name", false)?.as_deref(),
        "Git author name",
    );
    check_equal(
        &mut checks,
        "git_email",
        &profile.git_email,
        git.get("user.email", false)?.as_deref(),
        "Git author email",
    );
    if let Some(signing_key) = &profile.signing_key {
        check_equal(
            &mut checks,
            "signing_key",
            signing_key,
            git.get("user.signingKey", false)?.as_deref(),
            "commit signing key",
        );
        check_equal(
            &mut checks,
            "signing_format",
            profile.signing_format.as_git_value(),
            git.get("gpg.format", false)?.as_deref(),
            "commit signing format",
        );
        if profile.require_signing {
            check_equal(
                &mut checks,
                "commit_signing",
                "true",
                git.get("commit.gpgSign", false)?.as_deref(),
                "required commit signing",
            );
        }
    }

    match &remote {
        None => checks.push(item(
            "remote",
            CheckStatus::Warning,
            Some(options.remote_name.into()),
            None,
            "configured remote is not available",
        )),
        Some(remote) => inspect_remote(runner, &git, profile, remote, options.network, &mut checks),
    }
    Ok(finish(
        root.display().to_string(),
        Some(profile_name),
        remote,
        checks,
    ))
}

fn inspect_remote(
    runner: &dyn Runner,
    git: &Git<'_>,
    profile: &Profile,
    remote: &RemoteInfo,
    network: bool,
    checks: &mut Vec<CheckItem>,
) {
    // An SSH remote is expected to carry the profile's SSH alias, and every
    // other transport the real host. Comparing both against a single field is
    // what made alias users look permanently misconfigured.
    let expected_host = match remote.protocol {
        RemoteProtocol::Ssh => profile.ssh_host(),
        RemoteProtocol::Https | RemoteProtocol::Http => profile.hostname.as_str(),
    };
    check_equal_case_insensitive(
        checks,
        "hostname",
        expected_host,
        Some(&remote.hostname),
        "remote hostname",
    );
    check_transport(checks, remote);
    if profile.allowed_owners.is_empty() {
        checks.push(item(
            "owner",
            CheckStatus::Ok,
            None,
            Some(remote.owner.clone()),
            "remote owner is reported; this profile has no owner restriction",
        ));
    } else if profile
        .allowed_owners
        .iter()
        .any(|owner| owner.eq_ignore_ascii_case(&remote.owner))
    {
        checks.push(item(
            "owner",
            CheckStatus::Ok,
            Some(profile.allowed_owners.join(", ")),
            Some(remote.owner.clone()),
            "remote owner is allowed",
        ));
    } else {
        checks.push(item(
            "owner",
            CheckStatus::Failure,
            Some(profile.allowed_owners.join(", ")),
            Some(remote.owner.clone()),
            "remote owner is not allowed by this profile",
        ));
    }

    if matches!(remote.protocol, RemoteProtocol::Ssh) {
        match Git::expected_ssh_command(profile) {
            Ok(expected) => check_equal(
                checks,
                "ssh_command",
                &expected,
                git.get("core.sshCommand", false).ok().flatten().as_deref(),
                "repository SSH command",
            ),
            Err(error) => checks.push(item(
                "ssh_command",
                CheckStatus::Failure,
                None,
                None,
                error.to_string(),
            )),
        }
    }

    check_credential_helper(checks, git, profile, remote);

    if !network {
        return;
    }
    let github = GitHub::new(runner);
    match github.active_account(&profile.hostname) {
        Ok(Some(active)) if active.eq_ignore_ascii_case(&profile.github_user) => checks.push(item(
            "github_cli",
            CheckStatus::Ok,
            Some(profile.github_user.clone()),
            Some(active),
            "GitHub CLI account matches the profile",
        )),
        Ok(Some(active)) => checks.push(item(
            "github_cli",
            CheckStatus::Failure,
            Some(profile.github_user.clone()),
            Some(active),
            "GitHub CLI account does not match the profile",
        )),
        Ok(None) => checks.push(item(
            "github_cli",
            CheckStatus::Unverified,
            Some(profile.github_user.clone()),
            None,
            "GitHub CLI has no active account for this host",
        )),
        Err(error) => checks.push(item(
            "github_cli",
            CheckStatus::Unverified,
            Some(profile.github_user.clone()),
            None,
            format!("could not verify GitHub CLI identity: {error}"),
        )),
    }

    match remote.protocol {
        RemoteProtocol::Ssh => match ssh::verify(runner, profile) {
            Ok(SshIdentity::Verified(user)) => checks.push(item(
                "ssh_identity",
                CheckStatus::Ok,
                Some(profile.github_user.clone()),
                Some(user),
                "SSH authenticates as the expected GitHub user",
            )),
            Ok(SshIdentity::Rejected(user)) => checks.push(item(
                "ssh_identity",
                CheckStatus::Failure,
                Some(profile.github_user.clone()),
                Some(user),
                "SSH authenticates as a different GitHub user",
            )),
            Ok(SshIdentity::Unavailable(reason)) => checks.push(item(
                "ssh_identity",
                CheckStatus::Unverified,
                Some(profile.github_user.clone()),
                None,
                format!("could not verify SSH identity: {reason}"),
            )),
            Err(error) => checks.push(item(
                "ssh_identity",
                CheckStatus::Unverified,
                Some(profile.github_user.clone()),
                None,
                format!("could not verify SSH identity: {error}"),
            )),
        },
        RemoteProtocol::Https | RemoteProtocol::Http => {}
    }
}

/// Whether pushing over HTTPS would authenticate as the profile.
///
/// This reads `git config`, so unlike the two checks above it costs nothing and
/// works offline — which is why it sits outside the network gate. Leaving it
/// behind that gate meant `--offline` and the pre-commit hook dropped a check
/// they could perfectly well have made.
fn check_credential_helper(
    checks: &mut Vec<CheckItem>,
    git: &Git<'_>,
    profile: &Profile,
    remote: &RemoteInfo,
) {
    let scheme = match remote.protocol {
        RemoteProtocol::Http => "http",
        RemoteProtocol::Https => "https",
        RemoteProtocol::Ssh => return,
    };
    match git.has_compatible_credential_helper(scheme, &profile.hostname) {
        Ok(true) => checks.push(item(
            "credential_helper",
            CheckStatus::Ok,
            Some("gh auth git-credential".into()),
            Some("configured".into()),
            "GitHub CLI credential helper is configured",
        )),
        Ok(false) => checks.push(item(
            "credential_helper",
            CheckStatus::Unverified,
            Some("gh auth git-credential".into()),
            None,
            "HTTPS push identity is unverified; run gh auth setup-git for this host",
        )),
        Err(error) => checks.push(item(
            "credential_helper",
            CheckStatus::Unverified,
            None,
            None,
            format!("could not inspect Git credential helper: {error}"),
        )),
    }
}

/// Cleartext HTTP is wrong regardless of which profile a repository is bound
/// to, or whether it is bound at all, so this is shared by both paths.
fn check_transport(checks: &mut Vec<CheckItem>, remote: &RemoteInfo) {
    if !matches!(remote.protocol, RemoteProtocol::Http) {
        return;
    }
    checks.push(item(
        "transport",
        CheckStatus::Failure,
        Some("https or ssh".into()),
        Some("http".into()),
        format!(
            "remote uses cleartext HTTP; credentials and commits would travel unencrypted. Repoint it at {}",
            remote.as_url(RemoteProtocol::Https, &remote.hostname)
        ),
    ));
}

/// Record a Git setting without judging it.
///
/// Used only where there is no profile to compare against. The status is `ok`
/// because nothing was asserted and nothing failed — the value is evidence for
/// whatever repository policy has to say about it, in the same way an
/// unrestricted `owner` check reports the owner it found. A setting that is
/// absent is omitted entirely rather than reported as an empty string, so
/// policy can tell "no value" from "a value you dislike".
fn observe(
    checks: &mut Vec<CheckItem>,
    git: &Git<'_>,
    id: &str,
    key: &str,
    label: &str,
) -> Result<(), GitBoundError> {
    if let Some(value) = git.get(key, false)? {
        checks.push(item(
            id,
            CheckStatus::Ok,
            None,
            Some(value),
            format!("{label} is reported; no profile is bound to compare it against"),
        ));
    }
    Ok(())
}

fn check_equal(
    checks: &mut Vec<CheckItem>,
    id: &str,
    expected: &str,
    actual: Option<&str>,
    label: &str,
) {
    if actual == Some(expected) {
        checks.push(item(
            id,
            CheckStatus::Ok,
            Some(expected.into()),
            actual.map(str::to_string),
            format!("{label} matches the profile"),
        ));
    } else {
        checks.push(item(
            id,
            CheckStatus::Failure,
            Some(expected.into()),
            actual.map(str::to_string),
            format!("{label} does not match the profile"),
        ));
    }
}

fn check_equal_case_insensitive(
    checks: &mut Vec<CheckItem>,
    id: &str,
    expected: &str,
    actual: Option<&str>,
    label: &str,
) {
    if actual.is_some_and(|value| value.eq_ignore_ascii_case(expected)) {
        checks.push(item(
            id,
            CheckStatus::Ok,
            Some(expected.into()),
            actual.map(str::to_string),
            format!("{label} matches the profile"),
        ));
    } else {
        checks.push(item(
            id,
            CheckStatus::Failure,
            Some(expected.into()),
            actual.map(str::to_string),
            format!("{label} does not match the profile"),
        ));
    }
}

fn item(
    id: &str,
    status: CheckStatus,
    expected: Option<String>,
    actual: Option<String>,
    message: impl Into<String>,
) -> CheckItem {
    CheckItem {
        id: id.into(),
        status,
        expected,
        actual,
        message: message.into(),
    }
}

fn finish(
    repository: String,
    profile: Option<String>,
    remote: Option<RemoteInfo>,
    checks: Vec<CheckItem>,
) -> CheckReport {
    let overall = CheckReport::overall_of(&checks);
    CheckReport {
        repository,
        profile,
        remote,
        overall,
        checks,
    }
}

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

    #[test]
    fn unverified_is_not_enforceable() {
        let report = finish(
            "repo".into(),
            None,
            None,
            vec![item(
                "network",
                CheckStatus::Unverified,
                None,
                None,
                "offline",
            )],
        );
        assert!(!report.enforceable());
        assert_eq!(report.overall, OverallStatus::Warning);
    }
}