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
//! GH Auditor: Audit and enforce access permission policy for an organisation.
#![warn(missing_docs)]

mod builder;
mod config;
mod error;

pub use builder::AuditorBuilder;

use std::borrow::Cow;

use hyperx::header::{RelationType, TypedHeaders};
use snafu::{OptionExt, ResultExt};

/// Alias `Result` for convenience.
pub type Result<T> = std::result::Result<T, error::Error>;

/// The auditor of a GitHub organisation.
#[derive(Debug)]
pub struct Auditor<'a> {
    /// The authentication token for GitHub.
    auth_key: String,
    /// The HTTP client.
    client: Cow<'a, reqwest::Client>,
    /// The current configuration.
    config: config::Config,
    /// The GitHub organisation in JSON.
    organisation: serde_json::Value,
    /// Whether the auditor ran any audits in the last run.
    has_run_audit: bool,
}

impl<'a> Auditor<'a> {
    /// Perform the audit.
    /// # Errors
    /// If one of the audits has failed.
    pub fn audit(&mut self) -> std::result::Result<(), Vec<error::Error>> {
        self.has_run_audit = false;
        let mut errors = Vec::new();

        macro_rules! try_and_collect_errors {
            ($ex: expr) => {
                if let Err(error) = $ex {
                    log::error!("{}", error);
                    errors.push(error);
                }
            };
        }

        try_and_collect_errors!(self.audit_2fa());
        try_and_collect_errors!(self.audit_admin_commit_activity());
        try_and_collect_errors!(self.audit_all_master_branches_are_protected());

        if !self.has_run_audit {
            errors.push(error::Error::Audit {
                kind: error::AuditError::NoAuditsRan,
            });
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    /// Audit that 2fa is enforced for the organisation.
    fn audit_2fa(&mut self) -> Result<()> {
        if !self.config.enforces_2fa {
            return Ok(());
        }
        self.mark_audit("2 Factor Authentication");

        let enabled = self
            .organisation
            .get("two_factor_requirement_enabled")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);

        if enabled {
            log::info!("✅ 2 Factor Authentication required for members");
            Ok(())
        } else {
            Err(error::Error::Audit {
                kind: error::AuditError::Disabled2Fa,
            })
        }
    }

    /// Audit that all admin accounts have no push activity.
    fn audit_admin_commit_activity(&mut self) -> Result<()> {
        if !self.config.admins_have_no_commit_activity {
            return Ok(());
        }
        self.mark_audit("Admin Commit Activity");

        let members_url = self
            .organisation
            .get("members_url")
            .and_then(serde_json::Value::as_str)
            .context(error::MissingGitHubData)?
            .replace("{/member}", "?role=admin");

        let members = self.get_all(members_url)?;
        let mut found_members = Vec::new();

        for member in members {
            let events_url = member
                .get("events_url")
                .and_then(serde_json::Value::as_str)
                .context(error::MissingGitHubData)?
                .replace("{/privacy}", "");

            let has_pushed = self.find(events_url, |e| {
                e.get("type")
                    .and_then(|v| v.as_str())
                    .map(|t| t == "PushEvent")
                    .unwrap_or(false)
            })?;

            if has_pushed.is_some() {
                found_members.push(member);
            }
        }

        if found_members.is_empty() {
            log::info!("✅ No recent push activity on admin accounts.");
            Ok(())
        } else {
            Err(error::Error::Audit {
                kind: error::AuditError::AdminsHaveCommits(found_members),
            })
        }
    }

    fn audit_all_master_branches_are_protected(&mut self) -> Result<()> {
        if !self.config.all_repos_master_is_protected {
            return Ok(());
        }

        self.mark_audit("Protected master branches.");
        let mut unprotected_repos = Vec::new();

        let repos_url = self
            .organisation
            .get("repos_url")
            .and_then(serde_json::Value::as_str)
            .context(error::MissingGitHubData)?;

        for repo in self.get_all(repos_url)? {
            let branches_url = repo
                .get("branches_url")
                .and_then(serde_json::Value::as_str)
                .context(error::MissingGitHubData)?
                .replace("{/branch}", "?protected=false");

            let master = self.find(branches_url, |r| {
                r.get("name").map(|n| n == "master").unwrap_or(false)
            })?;

            let is_unprotected = master
                .and_then(|b| b.get("protected").and_then(serde_json::Value::as_bool))
                .map(|b| !b)
                .unwrap_or(true);

            if is_unprotected {
                unprotected_repos.push(repo);
            }
        }

        if unprotected_repos.is_empty() {
            log::info!("✅ All master branches are protected");
            Ok(())
        } else {
            Err(error::Error::Audit {
                kind: error::AuditError::UnProtectedMasterBranches(unprotected_repos),
            })
        }
    }

    /// Whether the `Auditor` has run at least one auditing procedure.
    pub fn has_run(&self) -> bool {
        self.has_run_audit
    }

    /// Find the first entity that matches `pred`, if any match. Goes through
    /// GitHub's pagination so will make potentially make multiple requests.
    fn find(
        &self,
        url: String,
        pred: impl FnMut(&&serde_json::Value) -> bool + Copy,
    ) -> Result<Option<serde_json::Value>> {
        let mut next = Some(url);

        while let Some(url) = next {
            let mut response = self
                .client
                .get(&url)
                .bearer_auth(&self.auth_key)
                .send()
                .context(error::Http)?;

            next = response
                .headers()
                .decode::<hyperx::header::Link>()
                .ok()
                .and_then(|v| {
                    v.values()
                        .iter()
                        .find(|link| {
                            link.rel()
                                .map(|rel| rel.contains(&RelationType::Next))
                                .unwrap_or(false)
                        })
                        .map(|l| l.link())
                        .map(str::to_owned)
                });

            let json = response.json::<serde_json::Value>().context(error::Http)?;

            let item = json.as_array().and_then(|v| v.iter().find(pred));

            if let Some(item) = item {
                return Ok(Some(item.clone()));
            }
        }

        Ok(None)
    }

    /// Gets a all entries across all pages from a resource in GitHub.
    fn get_all<'b, I: Into<Cow<'b, str>>>(&self, url: I) -> Result<Vec<serde_json::Value>> {
        let mut entities = Vec::new();
        let mut next = Some(url.into());

        while let Some(url) = next {
            let mut response = self
                .client
                .get(&*url)
                .bearer_auth(&self.auth_key)
                .send()
                .context(error::Http)?;

            next = response
                .headers()
                .decode::<hyperx::header::Link>()
                .ok()
                .and_then(|v| {
                    v.values()
                        .iter()
                        .find(|link| {
                            link.rel()
                                .map(|rel| rel.contains(&RelationType::Next))
                                .unwrap_or(false)
                        })
                        .map(|l| l.link())
                        .map(str::to_owned)
                        .map(Cow::Owned)
                });

            let json = response.json::<serde_json::Value>().context(error::Http)?;

            entities.extend_from_slice(&json.as_array().context(error::MissingGitHubData)?);
        }

        Ok(entities)
    }

    /// Convenience method to mark that at least one audit was performed on
    /// the repo.
    fn mark_audit(&mut self, msg: &str) {
        log::info!("⏳ Auditing {}", msg);
        self.has_run_audit = true;
    }
}