Skip to content

Commit

Permalink
handle --offline, verbosity (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
woodruffw authored Sep 12, 2024
1 parent d8af234 commit a7d71b1
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 25 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ homepage = "https://github.com/woodruffw/zizmor"
[dependencies]
anyhow = "1.0.86"
clap = { version = "4.5.16", features = ["derive", "env"] }
clap-verbosity-flag = "2.2.1"
env_logger = "0.11.5"
github-actions-models = "0.6.0"
itertools = "0.13.0"
Expand Down
2 changes: 1 addition & 1 deletion src/audit/artipacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use itertools::Itertools;
use super::WorkflowAudit;
use crate::{
finding::{Confidence, Finding, Severity},
models::AuditConfig,
AuditConfig,
};
use crate::{models::Workflow, utils::split_patterns};

Expand Down
4 changes: 2 additions & 2 deletions src/audit/excessive_permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use github_actions_models::{
use super::WorkflowAudit;
use crate::{
finding::{Confidence, Severity},
models::AuditConfig,
AuditConfig,
};

// Subjective mapping of permissions to severities, when given `write` access.
Expand Down Expand Up @@ -45,7 +45,7 @@ impl<'a> WorkflowAudit<'a> for ExcessivePermissions<'a> {
"excessive-permissions"
}

fn new(config: crate::models::AuditConfig<'a>) -> anyhow::Result<Self>
fn new(config: AuditConfig<'a>) -> anyhow::Result<Self>
where
Self: Sized,
{
Expand Down
4 changes: 2 additions & 2 deletions src/audit/hardcoded_container_credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use github_actions_models::{
use super::WorkflowAudit;
use crate::{
finding::{Confidence, Severity},
models::AuditConfig,
AuditConfig,
};

pub(crate) struct HardcodedContainerCredentials<'a> {
Expand All @@ -26,7 +26,7 @@ impl<'a> WorkflowAudit<'a> for HardcodedContainerCredentials<'a> {
"hardcoded-container-credentials"
}

fn new(config: crate::models::AuditConfig<'a>) -> anyhow::Result<Self>
fn new(config: AuditConfig<'a>) -> anyhow::Result<Self>
where
Self: Sized,
{
Expand Down
15 changes: 12 additions & 3 deletions src/audit/impostor_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ use std::{
ops::Deref,
};

use anyhow::Result;
use anyhow::{anyhow, Result};
use github_actions_models::workflow::{job::StepBody, Job};

use super::WorkflowAudit;
use crate::{
finding::{Confidence, Finding, Severity},
github_api::{self, Branch, ComparisonStatus, Tag},
models::{AuditConfig, Uses, Workflow},
models::{Uses, Workflow},
AuditConfig,
};

pub const IMPOSTOR_ANNOTATION: &str = "uses a commit that doesn't belong to the specified org/repo";
Expand Down Expand Up @@ -139,7 +140,15 @@ impl<'a> WorkflowAudit<'a> for ImpostorCommit<'a> {
}

fn new(config: AuditConfig<'a>) -> Result<Self> {
let client = github_api::Client::new(config.gh_token);
if config.offline {
return Err(anyhow!("offline audits only requested"));
}

let Some(gh_token) = config.gh_token else {
return Err(anyhow!("can't audit without a GitHub API token"));
};

let client = github_api::Client::new(gh_token);

Ok(ImpostorCommit {
_config: config,
Expand Down
3 changes: 2 additions & 1 deletion src/audit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use anyhow::Result;

use crate::{
finding::{Finding, FindingBuilder},
models::{AuditConfig, Workflow},
models::Workflow,
AuditConfig,
};

pub(crate) mod artipacked;
Expand Down
3 changes: 2 additions & 1 deletion src/audit/pull_request_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use github_actions_models::workflow::Trigger;

use super::WorkflowAudit;
use crate::finding::{Confidence, Finding, Severity};
use crate::models::{AuditConfig, Workflow};
use crate::models::Workflow;
use crate::AuditConfig;

pub(crate) struct PullRequestTarget<'a> {
pub(crate) _config: AuditConfig<'a>,
Expand Down
15 changes: 12 additions & 3 deletions src/audit/ref_confusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
use std::ops::Deref;

use anyhow::Result;
use anyhow::{anyhow, Result};
use github_actions_models::workflow::{job::StepBody, Job};

use super::WorkflowAudit;
use crate::{
finding::{Confidence, Severity},
github_api,
models::{AuditConfig, Uses},
models::Uses,
AuditConfig,
};

const REF_CONFUSION_ANNOTATION: &str =
Expand Down Expand Up @@ -62,9 +63,17 @@ impl<'a> WorkflowAudit<'a> for RefConfusion<'a> {
where
Self: Sized,
{
if config.offline {
return Err(anyhow!("offline audits only requested"));
}

let Some(gh_token) = config.gh_token else {
return Err(anyhow!("can't audit without a GitHub API token"));
};

Ok(Self {
_config: config,
client: github_api::Client::new(config.gh_token),
client: github_api::Client::new(gh_token),
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/audit/template_injection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use github_actions_models::workflow::{job::StepBody, Job};
use super::WorkflowAudit;
use crate::{
finding::{Confidence, Severity},
models::AuditConfig,
utils::iter_expressions,
AuditConfig,
};

pub(crate) struct TemplateInjection<'a> {
Expand Down
2 changes: 1 addition & 1 deletion src/audit/use_trusted_publishing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use github_actions_models::{
use super::WorkflowAudit;
use crate::{
finding::{Confidence, Severity},
models::AuditConfig,
AuditConfig,
};

const USES_MANUAL_CREDENTIAL: &str =
Expand Down
21 changes: 17 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::{io::stdout, path::PathBuf};
use anyhow::{anyhow, Result};
use audit::WorkflowAudit;
use clap::{Parser, ValueEnum};
use models::AuditConfig;
use registry::Registry;

mod audit;
Expand All @@ -25,9 +24,12 @@ struct Args {
#[arg(short, long)]
offline: bool,

#[command(flatten)]
verbose: clap_verbosity_flag::Verbosity,

/// The GitHub API token to use.
#[arg(long, env)]
gh_token: String,
gh_token: Option<String>,

/// The output format to emit. By default, plain text will be emitted
/// on an interactive terminal and JSON otherwise.
Expand All @@ -45,19 +47,30 @@ pub(crate) enum OutputFormat {
Sarif,
}

#[derive(Copy, Clone)]
pub(crate) struct AuditConfig<'a> {
pub(crate) pedantic: bool,
pub(crate) offline: bool,
pub(crate) gh_token: Option<&'a str>,
}

impl<'a> From<&'a Args> for AuditConfig<'a> {
fn from(value: &'a Args) -> Self {
Self {
pedantic: value.pedantic,
gh_token: &value.gh_token,
offline: value.offline,
gh_token: value.gh_token.as_deref(),
}
}
}

fn main() -> Result<()> {
env_logger::init();
let args = Args::parse();

env_logger::Builder::new()
.filter_level(args.verbose.log_level_filter())
.init();

let config = AuditConfig::from(&args);

let mut workflow_paths = vec![];
Expand Down
6 changes: 0 additions & 6 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,6 @@ impl<'w> Iterator for Steps<'w> {
}
}

#[derive(Copy, Clone)]
pub(crate) struct AuditConfig<'a> {
pub(crate) pedantic: bool,
pub(crate) gh_token: &'a str,
}

/// Represents the components of an "action ref", i.e. the value
/// of a `uses:` clause in a normal job step or a reusable workflow job.
/// Does not support `docker://` refs, or "local" (i.e. `./`) refs.
Expand Down

0 comments on commit a7d71b1

Please sign in to comment.