#[macro_use]
extern crate clap;
extern crate tempdir;
extern crate chrono;
extern crate encoding;
use clap::ArgMatches;
use log::{self, info};
use simple_error::SimpleError;
use std::str;
use tempdir::TempDir;
use rusty_hogs::git_scanning::GitScanner;
use rusty_hogs::{SecretScanner, SecretScannerBuilder};
fn main() {
let matches = clap_app!(choctaw_hog =>
(version: "1.0.1")
(author: "Scott Cutler <scutler@newrelic.com>")
(about: "Git secret scanner in Rust")
(@arg REGEX: -r --regex +takes_value "Sets a custom regex JSON file")
(@arg GITPATH: +required "Sets the path (or URL) of the Git repo to scan. SSH links must include username (git@)")
(@arg VERBOSE: -v --verbose ... "Sets the level of debugging information")
(@arg ENTROPY: --entropy ... "Enables entropy scanning")
(@arg CASE: --caseinsensitive "Sets the case insensitive flag for all regexes")
(@arg OUTPUT: -o --outputfile +takes_value "Sets the path to write the scanner results to (stdout by default)")
(@arg PRETTYPRINT: --prettyprint "Outputs the JSON in human readable format")
(@arg SINCECOMMIT: --since_commit +takes_value "Filters commits based on date committed (branch agnostic)")
(@arg UNTILCOMMIT: --until_commit +takes_value "Filters commits based on date committed (branch agnostic)")
(@arg SSHKEYPATH: --sshkeypath +takes_value "Takes a path to a private SSH key for git authentication, defaults to ssh-agent")
(@arg SSHKEYPHRASE: --sshkeyphrase +takes_value "Takes a passphrase to a private SSH key for git authentication, defaults to none")
(@arg HTTPSUSER: --httpsuser +takes_value "Takes a username for HTTPS-based authentication")
(@arg HTTPSPASS: --httpspass +takes_value "Takes a password for HTTPS-based authentication")
)
.get_matches();
match run(&matches) {
Ok(()) => {}
Err(e) => panic!("error: {}", e),
}
}
fn run(arg_matches: &ArgMatches) -> Result<(), SimpleError> {
SecretScanner::set_logging(arg_matches.occurrences_of("VERBOSE"));
let secret_scanner = SecretScannerBuilder::new().conf_argm(arg_matches).build();
let sshkeypath = arg_matches.value_of("SSHKEYPATH");
let sshkeyphrase = arg_matches.value_of("SSHKEYPHRASE");
let httpsuser = arg_matches.value_of("HTTPSUSER");
let httpspass = arg_matches.value_of("HTTPSPASS");
let since_commit = arg_matches.value_of("SINCECOMMIT");
let until_commit = arg_matches.value_of("UNTILCOMMIT");
let scan_entropy = arg_matches.is_present("ENTROPY");
let dest_dir = TempDir::new("rusty_hogs").unwrap();
let dest_dir_path = dest_dir.path();
let source_path: &str = arg_matches.value_of("GITPATH").unwrap();
let git_scanner = GitScanner::new_from_scanner(secret_scanner).init_git_repo(
source_path,
&dest_dir_path,
sshkeypath,
sshkeyphrase,
httpsuser,
httpspass,
);
let findings = git_scanner.perform_scan(None, since_commit, until_commit, scan_entropy);
info!("Found {} secrets", findings.len());
git_scanner.secret_scanner.output_findings(&findings);
Ok(())
}