-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support docker_image * test: add docker_image test case * test: fix test failed when git default branch set `main` * Minor tweaks --------- Co-authored-by: j178 <[email protected]>
- Loading branch information
Showing
8 changed files
with
171 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
use crate::hook::Hook; | ||
use crate::languages::docker::Docker; | ||
use crate::languages::{LanguageImpl, DEFAULT_VERSION}; | ||
use crate::run::run_by_batch; | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
pub struct DockerImage; | ||
|
||
impl LanguageImpl for DockerImage { | ||
fn default_version(&self) -> &str { | ||
DEFAULT_VERSION | ||
} | ||
|
||
fn environment_dir(&self) -> Option<&str> { | ||
None | ||
} | ||
|
||
async fn install(&self, _: &Hook) -> anyhow::Result<()> { | ||
Ok(()) | ||
} | ||
|
||
async fn check_health(&self) -> anyhow::Result<()> { | ||
todo!() | ||
} | ||
|
||
async fn run( | ||
&self, | ||
hook: &Hook, | ||
filenames: &[&String], | ||
env_vars: Arc<HashMap<&'static str, String>>, | ||
) -> anyhow::Result<(i32, Vec<u8>)> { | ||
let cmds = shlex::split(&hook.entry).ok_or(anyhow::anyhow!("Failed to parse entry"))?; | ||
|
||
let cmds = Arc::new(cmds); | ||
let hook_args = Arc::new(hook.args.clone()); | ||
|
||
let run = move |batch: Vec<String>| { | ||
let cmds = cmds.clone(); | ||
let hook_args = hook_args.clone(); | ||
let env_vars = env_vars.clone(); | ||
|
||
async move { | ||
let mut cmd = Docker::docker_cmd().await?; | ||
let cmd = cmd | ||
.args(&cmds[..]) | ||
.args(hook_args.as_ref()) | ||
.args(batch) | ||
.check(false) | ||
.envs(env_vars.as_ref()); | ||
|
||
let mut output = cmd.output().await?; | ||
output.stdout.extend(output.stderr); | ||
let code = output.status.code().unwrap_or(1); | ||
anyhow::Ok((code, output.stdout)) | ||
} | ||
}; | ||
|
||
let results = run_by_batch(hook, filenames, run).await?; | ||
|
||
// Collect results | ||
let mut combined_status = 0; | ||
let mut combined_output = Vec::new(); | ||
|
||
for (code, output) in results { | ||
combined_status |= code; | ||
combined_output.extend(output); | ||
} | ||
|
||
Ok((combined_status, combined_output)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use anyhow::Result; | ||
use assert_cmd::Command; | ||
use assert_fs::fixture::{FileWriteStr, PathChild}; | ||
|
||
use crate::common::{cmd_snapshot, TestContext}; | ||
|
||
#[test] | ||
fn docker_image() -> Result<()> { | ||
let context = TestContext::new(); | ||
context.init_project(); | ||
|
||
let cwd = context.workdir(); | ||
// Test suit from https://github.com/super-linter/super-linter/tree/main/test/linters/gitleaks/bad | ||
cwd.child("gitleaks_bad_01.txt") | ||
.write_str(indoc::indoc! {r" | ||
aws_access_key_id = AROA47DSWDEZA3RQASWB | ||
aws_secret_access_key = wQwdsZDiWg4UA5ngO0OSI2TkM4kkYxF6d2S1aYWM | ||
"})?; | ||
|
||
Command::new("docker") | ||
.args(["pull", "zricethezav/gitleaks:v8.21.2"]) | ||
.assert() | ||
.success(); | ||
|
||
context.write_pre_commit_config(indoc::indoc! {r" | ||
repos: | ||
- repo: local | ||
hooks: | ||
- id: gitleaks-docker | ||
name: Detect hardcoded secrets | ||
language: docker_image | ||
entry: zricethezav/gitleaks:v8.21.2 git --pre-commit --redact --staged --verbose | ||
pass_filenames: false | ||
"}); | ||
context.git_add("."); | ||
|
||
let filters = context | ||
.filters() | ||
.into_iter() | ||
.chain([(r"\d\d?:\d\d(AM|PM)", "[TIME]")]) | ||
.collect::<Vec<_>>(); | ||
|
||
cmd_snapshot!(filters, context.run(), @r#" | ||
success: false | ||
exit_code: 1 | ||
----- stdout ----- | ||
Detect hardcoded secrets.................................................Failed | ||
- hook id: gitleaks-docker | ||
- exit code: 1 | ||
Finding: aws_access_key_id = REDACTED | ||
Secret: REDACTED | ||
RuleID: generic-api-key | ||
Entropy: 3.521928 | ||
File: gitleaks_bad_01.txt | ||
Line: 1 | ||
Fingerprint: gitleaks_bad_01.txt:generic-api-key:1 | ||
Finding: aws_secret_access_key = REDACTED | ||
Secret: REDACTED | ||
RuleID: generic-api-key | ||
Entropy: 4.703056 | ||
File: gitleaks_bad_01.txt | ||
Line: 2 | ||
Fingerprint: gitleaks_bad_01.txt:generic-api-key:2 | ||
○ | ||
│╲ | ||
│ ○ | ||
○ ░ | ||
░ gitleaks | ||
[TIME] INF 1 commits scanned. | ||
[TIME] INF scan completed in [TIME] | ||
[TIME] WRN leaks found: 2 | ||
----- stderr ----- | ||
"#); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters