summaryrefslogtreecommitdiff
path: root/src/policies.rs
blob: fb10aed4238e3d957863a3666dcb2748ff94c27d (plain)
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
pub mod policy_result;

use crate::config::VerifyGitCommitsConfig;
use crate::fs::*;
use crate::git::*;
use crate::gpg::*;
use crate::keyring::*;
use crate::reference_update::ReferenceUpdate;

use self::policy_result::PolicyResult;

use git2::Oid;
use rayon::prelude::*;
use std::collections::HashSet;
use std::error::Error;
use std::path::PathBuf;
use std::time::Instant;

use log::*;

pub fn prepend_branch_name<F: Fs, G: Git>(
    git: &G,
    commit_file: PathBuf,
) -> Result<PolicyResult, Box<dyn Error>> {
    info!("Executing policy: prepend_branch_name");

    let branch = git.current_branch()?;
    F::prepend_string_to_file(branch, commit_file)?;
    Ok(PolicyResult::Ok)
}

pub fn verify_git_commits<G: Git, P: Gpg>(
    git: &G,
    gpg: P,
    config: &VerifyGitCommitsConfig,
    ref_update: &ReferenceUpdate,
) -> Result<PolicyResult, Box<dyn Error>> {
    info!("Executing policy: verify_git_commits");
    let start = Instant::now();

    let mut policy_result = PolicyResult::Ok;

    if let ReferenceUpdate::Delete { .. } = ref_update {
        debug!("Delete branch detected, no commits to verify.")
    } else if git.is_tag(ref_update.ref_name())? {
        debug!("Tag detected, no commits to verify.")
    } else {
        let all_commits = commits_to_verify(git, &ref_update, &config.override_tag_pattern)?;

        debug!("Number of commits to verify {} : ", all_commits.len());
        for commit in &all_commits {
            debug!("{:?}", commit);
        }

        let mut keyring =
            Keyring::from_team_fingerprints_file(git.read_file(&config.team_fingerprints_file)?);

        let manually_verified_commmits = find_and_verify_override_tags(
            git,
            &gpg,
            &all_commits,
            config.override_tags_required,
            &mut keyring,
        )?;
        let not_manually_verified_commits = commits_to_verify_excluding_manually_verified(
            git,
            &ref_update,
            manually_verified_commmits,
            &config.override_tag_pattern,
        )?;

        if config.verify_email_addresses {
            policy_result = policy_result.and_then(|| {
                Ok(verify_email_addresses(
                    &config.author_domain,
                    &config.committer_domain,
                    &not_manually_verified_commits,
                ))
            })?;
        }

        if config.verify_commit_signatures {
            policy_result = policy_result.and_then(|| {
                verify_commit_signatures::<G, P>(
                    git,
                    &gpg,
                    &not_manually_verified_commits,
                    &mut keyring,
                )
            })?;
        }

        if config.verify_different_authors {
            policy_result = policy_result
                .and_then(|| verify_different_authors::<G>(&all_commits, git, &ref_update))?;
        }

        if config.verify_rebased {
            policy_result = policy_result.and_then(|| {
                verify_rebased::<G>(&all_commits, git, &ref_update, &config.override_tag_pattern)
            })?;
        }
    }

    info!(
        "Policy verify_git_commits completed in: {} ms",
        start.elapsed().as_millis()
    );

    Ok(policy_result)
}

fn commits_to_verify<G: Git>(
    git: &G,
    ref_update: &ReferenceUpdate,
    override_tag_pattern: &Option<String>,
) -> Result<Vec<Commit>, Box<dyn Error>> {
    let to_exclude = ref_update.old_commit_id().into_iter().collect::<Vec<_>>();
    let to_include = ref_update.new_commit_id().into_iter().collect::<Vec<_>>();
    git.find_new_commits(&to_exclude, &to_include, override_tag_pattern)
}

fn commits_to_verify_excluding_manually_verified<G: Git>(
    git: &G,
    ref_update: &ReferenceUpdate,
    manually_verified: Vec<Oid>,
    override_tag_pattern: &Option<String>,
) -> Result<Vec<Commit>, Box<dyn Error>> {
    let mut to_exclude = manually_verified;
    if let Some(old_commit_id) = ref_update.old_commit_id() {
        to_exclude.push(old_commit_id);
    }
    let to_include = ref_update.new_commit_id().into_iter().collect::<Vec<_>>();
    git.find_new_commits(&to_exclude, &to_include, override_tag_pattern)
}

fn find_and_verify_override_tags<G: Git, P: Gpg>(
    git: &G,
    gpg: &P,
    commits: &Vec<Commit>,
    required_tags: u8,
    keyring: &mut Keyring,
) -> Result<Vec<Oid>, Box<dyn Error>> {
    let repo_path = git.path();
    gpg.receive_keys(
        keyring,
        &commits
            .iter()
            .filter(|c| c.tags.len() >= required_tags.into())
            .flat_map(|c| c.tags.iter().flat_map(|t| t.tagger_email.as_deref()))
            .collect(),
    )?;

    let tagged_commits = commits
        .iter()
        .filter(|c| c.tags.len() >= required_tags.into())
        .filter_map(|c| {
            let verified_taggers = c
                .tags
                .iter()
                .filter(|t| verify_tag_logging_errors::<G>(&repo_path, t, keyring))
                .filter_map(|t| t.tagger_email.as_ref())
                .collect::<HashSet<_>>();

            if verified_taggers.len() >= required_tags.into() {
                info!("Override tags found for {}. Tags created by {:?}. This commit, and it's ancestors, do not require validation.", c.id, verified_taggers);
                Some(c.id)
            } else {
                None
            }
        })
        .collect();

    Ok(tagged_commits)
}

fn verify_tag_logging_errors<G: Git>(
    repo_path: &std::path::Path,
    tag: &Tag,
    keyring: &Keyring,
) -> bool {
    match G::verify_tag_signature(repo_path, tag, keyring) {
        Ok(result) => result,
        Err(e) => {
            error!(
                "Technical error occurred while trying to validate tag {}. Error: {}",
                tag.name, e
            );
            false
        }
    }
}

fn verify_commit_signatures<G: Git, P: Gpg>(
    git: &G,
    gpg: &P,
    commits: &[Commit],
    keyring: &mut Keyring,
) -> Result<PolicyResult, Box<dyn Error>> {
    gpg.receive_keys(
        keyring,
        &commits
            .iter()
            .filter_map(|c| c.committer_email.as_deref())
            .collect(),
    )?;

    let repo_path = git.path();
    let commits_with_verified_signatures: HashSet<Oid> = commits
        .par_iter()
        .filter(|commit| {
            match G::verify_commit_signature(repo_path, &commit, keyring) {
                Ok(result) => result,
                Err(e) => {
                    error!(
                        "Technical error occurred while trying to validate commit signature {}. Error: {}",
                           commit.id, e
                    );
                    false
                }
            }
        })
        .map(|commit| commit.id)
        .collect();

    commits.iter()
        .map(|commit| {
            if commit.is_identical_tree_to_any_parent {
                info!("Signature verification passed for {}: verified identical to one of its parents, no signature required", commit.id);
                Ok(PolicyResult::Ok)
            } else if commits_with_verified_signatures.contains(&commit.id) {
                info!("Signature verification passed for {}: verified with a valid signature", commit.id);
                Ok(PolicyResult::Ok)
            } else if git.is_trivial_merge_commit(commit)? {
                info!("Signature verification passed for {}: verified to be a trivial merge of its parents, no signature required", commit.id);
                Ok(PolicyResult::Ok)
            }  else {
                error!("Signature verification failed for {}", commit.id);
                if commit.is_merge_commit {
                    Ok(PolicyResult::UnsignedMergeCommit(commit.id))
                } else {
                    Ok(PolicyResult::UnsignedCommit(commit.id))
                }
            }
        })
        .collect()
}

fn verify_different_authors<G: Git>(
    commits: &[Commit],
    git: &G,
    ref_update: &ReferenceUpdate,
) -> Result<PolicyResult, Box<dyn Error>> {
    match ref_update {
        ReferenceUpdate::Delete { .. } => {
            info!("Multiple author verification passed: No checks required for deleting a branch");
            Ok(PolicyResult::Ok)
        }
        ReferenceUpdate::New { new_commit_id, .. } => {
            info!("Multiple author verification passed for {}: New branch does not require multiple authors for a merge commit", new_commit_id);
            Ok(PolicyResult::Ok)
        }
        ReferenceUpdate::Update {
            new_commit_id,
            ref_name,
            ..
        } => {
            let is_merge = git.is_merge_commit(*new_commit_id);
            let is_mainline = git.is_mainline(ref_name)?;

            if !is_mainline {
                info!("Multiple author verification passed for {}: Not updating a mainline branch, does not require multiple authors", new_commit_id);
                Ok(PolicyResult::Ok)
            } else if !is_merge {
                info!("Multiple author verification passed for {}: Not a merge commit, does not require multiple authors", new_commit_id);
                Ok(PolicyResult::Ok)
            } else if commits.len() == 0 {
                info!("Multiple author verification passed for {}: No new commits pushed, does not require multiple authors", new_commit_id);
                Ok(PolicyResult::Ok)
            } else if commits.len() == 1 && commits[0].is_identical_tree_to_any_parent {
                info!("Multiple author verification passed for {}: There is only one commit and it has an identical filetree to one of its parents", new_commit_id);
                Ok(PolicyResult::Ok)
            } else if commits.len() == 1 && git.is_trivial_merge_commit(&commits[0])? {
                info!("Multiple author verification passed for {}: There is only one commit and it is a trivial merge between mainline branches", new_commit_id);
                Ok(PolicyResult::Ok)
            } else {
                let authors: HashSet<_> = commits
                    .iter()
                    .flat_map(|c| {
                        c.tags
                            .iter()
                            .filter_map(|t| t.tagger_email.as_ref())
                            .chain(c.author_email.as_ref())
                    })
                    .collect();
                if authors.len() <= 1 {
                    error!(
                "Multiple author verification failed for {}: requires multiple authors, found {:?}",
                new_commit_id, authors
            );
                    Ok(PolicyResult::NotEnoughAuthors(*new_commit_id))
                } else {
                    info!(
                        "Multiple author verification passed for {}: found multiple authors, {:?}",
                        new_commit_id, authors
                    );
                    Ok(PolicyResult::Ok)
                }
            }
        }
    }
}

fn verify_rebased<G: Git>(
    commits: &[Commit],
    git: &G,
    ref_update: &ReferenceUpdate,
    override_tag_pattern: &Option<String>,
) -> Result<PolicyResult, Box<dyn Error>> {
    match ref_update {
        ReferenceUpdate::Delete { .. } => {
            info!("Rebase verification passed: No checks required for deleting a branch");
            Ok(PolicyResult::Ok)
        }
        ReferenceUpdate::New { new_commit_id, .. } => {
            info!("Rebase verification passed for {}: New branch does not require being rebased for a merge commit", new_commit_id);
            Ok(PolicyResult::Ok)
        }
        ReferenceUpdate::Update {
            old_commit_id,
            new_commit_id,
            ref_name,
        } => {
            let is_merge = git.is_merge_commit(*new_commit_id);
            let is_mainline = git.is_mainline(ref_name)?;
            let new_commit = git.find_commit(*new_commit_id, override_tag_pattern)?;

            if !is_mainline {
                info!(
                    "Rebase verification passed for {}: Not updating a mainline branch",
                    new_commit_id
                );
                Ok(PolicyResult::Ok)
            } else if !is_merge {
                info!(
                    "Rebase verification passed for {}: Not a merge commit",
                    new_commit_id
                );
                Ok(PolicyResult::Ok)
            } else if commits.len() == 0 {
                info!(
                    "Rebase verification passed for {}: No new commits pushed",
                    new_commit_id
                );
                Ok(PolicyResult::Ok)
            } else if !git.is_descendent_of(*new_commit_id, *old_commit_id)? {
                info!(
                    "Rebase verification passed for {0}: Commit Id {0} is not a descendent of Commit Id {1}, it is most likely that a force-push has occurred",
                    new_commit_id,
                    old_commit_id
                );
                Ok(PolicyResult::Ok)
            } else {
                let mut new_commit_is_rebased = true;
                for parent_id in &new_commit.parents {
                    let parent_is_descendent_of_old_id = parent_id == old_commit_id
                        || git.is_descendent_of(*parent_id, *old_commit_id)?;
                    new_commit_is_rebased = new_commit_is_rebased && parent_is_descendent_of_old_id;
                }

                if new_commit_is_rebased {
                    info!(
                        "Rebase verification passed for {}: Branch is up to date with the mainline it's being merged into",
                        new_commit_id
                    );
                    Ok(PolicyResult::Ok)
                } else {
                    error!("Rebase verification failed for {}: branch must be rebased before it can be merged into the mainline", new_commit_id);
                    Ok(PolicyResult::NotRebased(*new_commit_id))
                }
            }
        }
    }
}

fn verify_email_addresses(
    author_domain: &str,
    committer_domain: &str,
    commits: &[Commit],
) -> PolicyResult {
    commits
        .iter()
        .map(
            |commit| match (&commit.author_email, &commit.committer_email) {
                (None, _) => {
                    error!(
                        "Email address verification failed for {}: missing author email",
                        commit.id
                    );
                    PolicyResult::MissingAuthorEmail(commit.id)
                }
                (_, None) => {
                    error!(
                        "Email address verification failed for {}: missing committer email",
                        commit.id
                    );
                    PolicyResult::MissingCommitterEmail(commit.id)
                }
                (Some(s), _) if !s.ends_with(&format!("@{}", author_domain)) => {
                    error!(
                        "Email address verification failed for {}: invalid author email {}",
                        commit.id,
                        s.to_string()
                    );
                    PolicyResult::InvalidAuthorEmail(commit.id, s.to_string())
                }
                (_, Some(s)) if !s.ends_with(&format!("@{}", committer_domain)) => {
                    error!(
                        "Email address verification failed for {}: invalid committer email {}",
                        commit.id,
                        s.to_string()
                    );
                    PolicyResult::InvalidCommitterEmail(commit.id, s.to_string())
                }
                _ => {
                    info!("Email address verification passed for {}", commit.id);
                    PolicyResult::Ok
                }
            },
        )
        .collect()
}