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
// End-to-end coverage for the CI surface: `verify`, `audit`, the report
// formatters, and the committed repository policy.
//
// These drive the real binary against real Git repositories, like tests/cli.rs,
// because the thing under test is the contract a pipeline depends on — the exit
// code, the file that lands on disk, the annotation on stdout — and none of that
// is observable from a unit test.

use assert_cmd::cargo::cargo_bin_cmd;
use predicates::prelude::*;
use std::{fs, path::Path, process::Command};

fn initialized_repo(parent: &Path) -> std::path::PathBuf {
    let repo = parent.join("repo");
    fs::create_dir(&repo).unwrap();
    assert!(
        Command::new("git")
            .args(["init", "--quiet"])
            .current_dir(&repo)
            .status()
            .unwrap()
            .success()
    );
    repo
}

fn add_profile(repo: &Path, config: &Path) {
    cargo_bin_cmd!()
        .current_dir(repo)
        .env("GITBOUND_CONFIG", config)
        .args([
            "profile",
            "add",
            "work",
            "--github-user",
            "alice-work",
            "--git-name",
            "Alice Work",
            "--git-email",
            "work@example.com",
        ])
        .assert()
        .success();
}

/// Commit with an explicit repository-local identity, so the test never depends
/// on — or disturbs — the developer's global Git configuration.
fn commit_as(repo: &Path, name: &str, email: &str, subject: &str) {
    for (key, value) in [("user.name", name), ("user.email", email)] {
        assert!(
            Command::new("git")
                .args(["config", "--local", key, value])
                .current_dir(repo)
                .status()
                .unwrap()
                .success()
        );
    }
    assert!(
        Command::new("git")
            .args([
                "commit",
                "--allow-empty",
                "--no-gpg-sign",
                "-q",
                "-m",
                subject
            ])
            .current_dir(repo)
            .status()
            .unwrap()
            .success()
    );
}

fn write_policy(repo: &Path, body: &str) {
    fs::write(repo.join(".gitbound.toml"), body).unwrap();
}

const COMPANY_ONLY: &str =
    "schema_version = 1\n[identity]\nallowed_email_domains = [\"company.example\"]\n";

#[test]
fn help_lists_the_ci_commands() {
    cargo_bin_cmd!()
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("verify"))
        .stdout(predicate::str::contains("audit"));
}

#[test]
fn verify_renders_sarif_to_a_file_and_to_stdout() {
    let temp = tempfile::tempdir().unwrap();
    let config = temp.path().join("config.toml");
    let repo = initialized_repo(temp.path());
    add_profile(&repo, &config);
    let sarif = temp.path().join("out.sarif");

    // The repository is unbound, which `verify` reports as `unverified` rather
    // than treating as a fault: a pipeline checkout never has a binding. It is
    // still a SARIF result, so there is something to upload, but on its own it
    // does not fail the run.
    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["verify", "--format", "sarif", "--output"])
        .arg(&sarif)
        .assert()
        .code(0);

    let text = fs::read_to_string(&sarif).unwrap();
    let value: serde_json::Value = serde_json::from_str(&text).expect("SARIF must be valid JSON");
    assert_eq!(value["version"], "2.1.0");
    assert!(
        value["runs"][0]["results"]
            .as_array()
            .expect("results")
            .iter()
            .any(|result| result["ruleId"] == "binding")
    );
}

#[test]
fn verify_emits_workflow_commands_and_a_step_summary_in_a_runner() {
    let temp = tempfile::tempdir().unwrap();
    let config = temp.path().join("config.toml");
    let repo = initialized_repo(temp.path());
    add_profile(&repo, &config);
    let summary = temp.path().join("summary.md");

    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .env("GITHUB_ACTIONS", "true")
        .env("GITHUB_STEP_SUMMARY", &summary)
        .arg("verify")
        .assert()
        .code(0)
        .stdout(predicate::str::contains(
            "::notice title=GitBound%3A binding::",
        ));

    let written = fs::read_to_string(&summary).unwrap();
    assert!(written.contains("| Check | Status |"));
    assert!(written.contains("`binding`"));
}

/// A pipeline checkout, with a remote and a committed policy but no binding.
fn pipeline_checkout(temp: &Path, email: &str) -> std::path::PathBuf {
    let repo = initialized_repo(temp);
    assert!(
        Command::new("git")
            .args([
                "remote",
                "add",
                "origin",
                "https://github.com/company-name/project.git",
            ])
            .current_dir(&repo)
            .status()
            .unwrap()
            .success()
    );
    for (key, value) in [("user.name", "Alice"), ("user.email", email)] {
        assert!(
            Command::new("git")
                .args(["config", "--local", key, value])
                .current_dir(&repo)
                .status()
                .unwrap()
                .success()
        );
    }
    repo
}

#[test]
fn verify_applies_repository_policy_to_an_unbound_checkout() {
    // The whole point of CI mode. `inspect` used to return as soon as it found
    // no binding, so no `git_email` item ever reached `Policy::evaluate`, which
    // then reported "no Git author email is configured" for a repository that
    // had one and was breaking the rule. The verdict was still `failure` — but
    // only because the missing binding failed it, so the policy was decorative.
    let temp = tempfile::tempdir().unwrap();
    let config = temp.path().join("config.toml");
    let repo = pipeline_checkout(temp.path(), "alice@personal.example");
    write_policy(&repo, COMPANY_ONLY);

    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["verify", "--format", "json"])
        .assert()
        .code(1)
        .stdout(predicate::str::contains(
            "alice@personal.example is not permitted by .gitbound.toml",
        ))
        .stdout(predicate::str::contains("no Git author email is configured").not());
}

#[test]
fn verify_passes_a_compliant_pipeline_checkout() {
    // And the other direction, which is what makes the gate usable: a checkout
    // that satisfies the committed policy exits 0 even though it has no
    // binding, because a pipeline never has one.
    let temp = tempfile::tempdir().unwrap();
    let config = temp.path().join("config.toml");
    let repo = pipeline_checkout(temp.path(), "alice@company.example");
    write_policy(&repo, COMPANY_ONLY);

    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["verify", "--format", "human"])
        .assert()
        .code(0)
        .stdout(predicate::str::contains(
            "author email is permitted by repository policy",
        ));
}

#[test]
fn check_still_fails_an_unbound_repository() {
    // `verify` excusing an absent binding must not have relaxed the gate that
    // runs on the machine which owns the binding.
    let temp = tempfile::tempdir().unwrap();
    let config = temp.path().join("config.toml");
    let repo = pipeline_checkout(temp.path(), "alice@company.example");
    write_policy(&repo, COMPANY_ONLY);

    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["check", "--format", "human"])
        .assert()
        .code(1)
        .stdout(predicate::str::contains(
            "repository is not bound to a GitBound profile",
        ));
}

#[test]
fn require_policy_refuses_a_repository_that_declares_none() {
    // Without a policy there is no rule to break, so `verify` would otherwise
    // pass having checked nothing — and deleting the file would be enough to
    // switch the gate off.
    let temp = tempfile::tempdir().unwrap();
    let config = temp.path().join("config.toml");
    let repo = pipeline_checkout(temp.path(), "alice@company.example");

    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["verify"])
        .assert()
        .code(0);

    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["verify", "--require-policy"])
        .assert()
        .code(1)
        .stdout(predicate::str::contains("commits no .gitbound.toml"));

    // A file that declares no rules constrains nothing, so it counts as absent.
    write_policy(
        &repo,
        "schema_version = 1
",
    );
    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["verify", "--require-policy"])
        .assert()
        .code(1)
        .stdout(predicate::str::contains("declares no rules"));

    write_policy(&repo, COMPANY_ONLY);
    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["verify", "--require-policy"])
        .assert()
        .code(0);
}

#[test]
fn an_explicit_format_wins_over_the_runner_environment() {
    let temp = tempfile::tempdir().unwrap();
    let config = temp.path().join("config.toml");
    let repo = initialized_repo(temp.path());
    add_profile(&repo, &config);

    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .env("GITHUB_ACTIONS", "true")
        .args(["status", "--json"])
        .assert()
        .success()
        .stdout(predicate::str::contains("\"overall\""))
        .stdout(predicate::str::contains("::error").not());
}

#[test]
fn a_repository_policy_can_reject_an_otherwise_valid_identity() {
    let temp = tempfile::tempdir().unwrap();
    let config = temp.path().join("config.toml");
    let repo = initialized_repo(temp.path());
    add_profile(&repo, &config);
    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["bind", "work"])
        .assert()
        .success();

    // The profile's own address is work@example.com, which this policy forbids.
    write_policy(&repo, COMPANY_ONLY);

    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["verify", "--json"])
        .assert()
        .code(1)
        .stdout(predicate::str::contains("policy_email"));

    // ...and --no-policy must put the behaviour back exactly as it was.
    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["verify", "--json", "--no-policy"])
        .assert()
        .stdout(predicate::str::contains("policy_email").not());
}

#[test]
fn an_absent_policy_changes_nothing() {
    let temp = tempfile::tempdir().unwrap();
    let config = temp.path().join("config.toml");
    let repo = initialized_repo(temp.path());
    add_profile(&repo, &config);

    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["status", "--json"])
        .assert()
        .success()
        .stdout(predicate::str::contains("policy_").not());
}

#[test]
fn an_unreadable_policy_schema_is_a_usage_error() {
    let temp = tempfile::tempdir().unwrap();
    let config = temp.path().join("config.toml");
    let repo = initialized_repo(temp.path());
    add_profile(&repo, &config);
    write_policy(&repo, "schema_version = 99\n");

    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .arg("verify")
        .assert()
        .code(2)
        .stderr(predicate::str::contains("schema_version 99"));
}

#[test]
fn audit_names_the_commit_that_breaks_policy() {
    let temp = tempfile::tempdir().unwrap();
    let config = temp.path().join("config.toml");
    let repo = initialized_repo(temp.path());
    add_profile(&repo, &config);

    commit_as(&repo, "Alice", "alice@company.example", "first");
    commit_as(&repo, "Bob", "bob@personal.example", "second");
    commit_as(&repo, "Alice", "alice@company.example", "third");
    write_policy(&repo, COMPANY_ONLY);

    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["audit", "--range", "HEAD", "--json"])
        .assert()
        .code(1)
        .stdout(predicate::str::contains("commit_author"))
        .stdout(predicate::str::contains("bob@personal.example"));
}

#[test]
fn audit_passes_a_clean_range() {
    let temp = tempfile::tempdir().unwrap();
    let config = temp.path().join("config.toml");
    let repo = initialized_repo(temp.path());
    add_profile(&repo, &config);
    commit_as(&repo, "Alice", "alice@company.example", "first");
    write_policy(&repo, COMPANY_ONLY);

    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["audit", "--range", "HEAD", "--json"])
        .assert()
        .success()
        .stdout(predicate::str::contains("commit_range"));
}

#[test]
fn audit_enforce_signing_fails_unsigned_commits() {
    let temp = tempfile::tempdir().unwrap();
    let config = temp.path().join("config.toml");
    let repo = initialized_repo(temp.path());
    add_profile(&repo, &config);
    commit_as(&repo, "Alice", "alice@company.example", "unsigned");

    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["audit", "--range", "HEAD", "--enforce-signing", "--json"])
        .assert()
        .code(1)
        .stdout(predicate::str::contains("commit_signature"));
}

#[test]
fn audit_refuses_an_option_shaped_range() {
    let temp = tempfile::tempdir().unwrap();
    let config = temp.path().join("config.toml");
    let repo = initialized_repo(temp.path());
    add_profile(&repo, &config);
    commit_as(&repo, "Alice", "alice@company.example", "first");

    cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["audit", "--range", "--output=/tmp/pwned"])
        .assert()
        .code(2);
}

#[test]
fn verify_junit_is_well_formed_xml() {
    let temp = tempfile::tempdir().unwrap();
    let config = temp.path().join("config.toml");
    let repo = initialized_repo(temp.path());
    add_profile(&repo, &config);

    let output = cargo_bin_cmd!()
        .current_dir(&repo)
        .env("GITBOUND_CONFIG", &config)
        .args(["verify", "--format", "junit"])
        .output()
        .unwrap();
    let xml = String::from_utf8(output.stdout).unwrap();
    assert!(xml.starts_with("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    assert_eq!(
        xml.matches("<testcase").count(),
        xml.matches("</testcase>").count(),
        "every testcase must be closed"
    );
    assert!(xml.trim_end().ends_with("</testsuites>"));
}