-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
phylum firewall log
subcommand (#1551)
This patch adds the new `phylum firewall log` to allow checking the Aviary log from the CLI. While the UI gives a more user-friendly overview, this should be complementary by giving access to the data in a structure closer to the original layout. The CLI arguments allow applying most filters exposed by the API, but combine the individual package components into a PURL to avoid an excessive number of arguments. Currently neither the `--json` output nor the pretty-printer have a way to paginate through the logs. While this could be done either automatically or by manually specifying the pagination offset, it seems to me like the limit of 10_000 entries per page should be sufficient for all current usecases. Contrary to our existing timestamp columns, the timestamp in the output table uses nanosecond precision. The additional precision seems worthwhile in this case since this automatically groups versions together if they were submitted for analysis as a batch.
- Loading branch information
Showing
15 changed files
with
427 additions
and
15 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,73 @@ | ||
//! Subcommand `phylum firewall`. | ||
use std::borrow::Cow; | ||
use std::str::FromStr; | ||
|
||
use clap::ArgMatches; | ||
use purl::Purl; | ||
|
||
use crate::api::PhylumApi; | ||
use crate::commands::{CommandResult, ExitCode}; | ||
use crate::config::Config; | ||
use crate::format::Format; | ||
use crate::print_user_failure; | ||
use crate::types::FirewallLogFilter; | ||
|
||
/// Handle `phylum firewall` subcommand. | ||
pub async fn handle_firewall( | ||
api: &PhylumApi, | ||
matches: &ArgMatches, | ||
config: Config, | ||
) -> CommandResult { | ||
match matches.subcommand() { | ||
Some(("log", matches)) => handle_log(api, matches, config).await, | ||
_ => unreachable!("invalid clap configuration"), | ||
} | ||
} | ||
|
||
/// Handle `phylum firewall log` subcommand. | ||
pub async fn handle_log(api: &PhylumApi, matches: &ArgMatches, config: Config) -> CommandResult { | ||
let org = config.org(); | ||
let group = matches.get_one::<String>("group").unwrap(); | ||
|
||
// Get log filter args. | ||
let ecosystem = matches.get_one::<String>("ecosystem"); | ||
let purl = matches.get_one::<String>("package"); | ||
let action = matches.get_one::<String>("action"); | ||
let before = matches.get_one::<String>("before"); | ||
let after = matches.get_one::<String>("after"); | ||
let limit = matches.get_one::<i64>("limit").unwrap(); | ||
|
||
// Parse PURL filter. | ||
let parsed_purl = purl.map(|purl| Purl::from_str(purl)); | ||
let (ecosystem, namespace, name, version) = match &parsed_purl { | ||
Some(Ok(purl)) => { | ||
let ecosystem = Cow::Owned(purl.package_type().to_string()); | ||
(Some(ecosystem), purl.namespace(), Some(purl.name()), purl.version()) | ||
}, | ||
Some(Err(err)) => { | ||
print_user_failure!("Could not parse purl {purl:?}: {err}"); | ||
return Ok(ExitCode::Generic); | ||
}, | ||
None => (ecosystem.map(Cow::Borrowed), None, None, None), | ||
}; | ||
|
||
// Construct the filter. | ||
let filter = FirewallLogFilter { | ||
ecosystem: ecosystem.as_ref().map(|e| e.as_str()), | ||
namespace, | ||
name, | ||
version, | ||
action: action.map(String::as_str), | ||
before: before.map(String::as_str), | ||
after: after.map(String::as_str), | ||
limit: Some(*limit as i32), | ||
}; | ||
|
||
let response = api.firewall_log(org, group, filter).await?; | ||
|
||
let pretty = !matches.get_flag("json"); | ||
response.data.write_stdout(pretty); | ||
|
||
Ok(ExitCode::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
Oops, something went wrong.