Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added lockdown configuration, wick audit, and wick run --dryrun #411

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ rustflags = ["-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"]
linker = "aarch64-linux-gnu-gcc"

[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"
linker = "x86_64-w64-mingw32-gcc"

[registries.crates-io]
protocol = "sparse"
42 changes: 26 additions & 16 deletions Cargo.lock

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

17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ wasmtime = { version = "11.0", default-features = false, features = [
wasmtime-wasi = { version = "11.0", features = ["sync"] }
wasi-common = { version = "11.0" }
# wasmrs
wasmrs = { version = "0.14.0" }
wasmrs-codec = { version = "0.14.0" }
wasmrs-frames = { version = "0.14.0" }
wasmrs-guest = { version = "0.14.0" }
wasmrs-host = { version = "0.14.0" }
wasmrs-runtime = { version = "0.14.0" }
wasmrs-rx = { version = "0.14.0" }
wasmrs-wasmtime = { version = "0.14.0" }
wasmrs = { version = "0.15.0" }
wasmrs-codec = { version = "0.15.0" }
wasmrs-frames = { version = "0.15.0" }
wasmrs-guest = { version = "0.15.0" }
wasmrs-host = { version = "0.15.0" }
wasmrs-runtime = { version = "0.15.0" }
wasmrs-rx = { version = "0.15.0" }
wasmrs-wasmtime = { version = "0.15.0" }
#
# Other
#
Expand Down Expand Up @@ -209,3 +209,4 @@ walkdir = { version = "2.3", default-features = false }
xdg = { version = "2.4", default-features = false }
byteorder = { version = "1.4", default-features = false }
rstest = { version = "0.18", default-features = false }
wildmatch = { version = "2.1.1", default-features = false }
15 changes: 9 additions & 6 deletions crates/bins/wick/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub(crate) mod wasm;

use clap::{Parser, Subcommand};

use crate::options::logging::LoggingOptions;
use crate::options::GlobalOptions;
use crate::LoggingOptions;

#[derive(Parser, Debug, Clone)]
#[clap(
Expand All @@ -35,20 +35,23 @@ pub(crate) struct Cli {

#[derive(Debug, Clone, Subcommand)]
pub(crate) enum CliCommand {
// Core commands
/// Start a persistent host from a manifest.
#[clap(name = "serve")]
Serve(serve::Options),
/// Load a manifest and execute an entrypoint component (temporarily disabled).

/// Run a wick application.
#[clap(name = "run")]
Run(run::Options),
/// Invoke a component from a manifest or wasm module.

/// Invoke an operation.
#[clap(name = "invoke")]
Invoke(invoke::Options),
/// Print the components in a manifest or wasm module.

/// Print the signature of a component.
#[clap(name = "list")]
List(list::Options),
/// Execute a component with test data and assert its output.

/// Run test cases against a component.
#[clap(name = "test")]
Test(test::Options),

Expand Down
4 changes: 4 additions & 0 deletions crates/bins/wick/src/commands/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::Subcommand;

pub(crate) mod audit;
pub(crate) mod dot;
pub(crate) mod expand;

Expand All @@ -8,6 +9,9 @@ pub(crate) enum SubCommands {
/// Generate a dot-syntax graph of a composite component.
#[clap(name = "dot")]
Dot(dot::Options),
/// Generate an audit report for a component or application.
#[clap(name = "audit")]
Audit(audit::Options),
/// Validate and output an expanded configuration.
#[clap(name = "expand")]
Expand(expand::Options),
Expand Down
73 changes: 73 additions & 0 deletions crates/bins/wick/src/commands/config/audit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use anyhow::Result;
use clap::Args;
use serde_json::json;
use structured_output::StructuredOutput;
use wick_config::audit::Audit;
use wick_config::WickConfiguration;

use crate::utils::{fetch_wick_tree, parse_config_string, reconcile_fetch_options};

#[derive(Debug, Clone, Args)]
#[clap(rename_all = "kebab-case")]
#[group(skip)]
pub(crate) struct Options {
#[clap(flatten)]
pub(crate) oci: crate::options::oci::OciOptions,

#[clap(flatten)]
pub(crate) component: crate::options::component::ComponentOptions,

/// Turn the audit report into a lockdown configuration.
#[clap(long = "lockdown", action)]
lockdown: bool,
}

#[allow(clippy::unused_async)]
pub(crate) async fn handle(
opts: Options,
settings: wick_settings::Settings,
span: tracing::Span,
) -> Result<StructuredOutput> {
let runtime_config = parse_config_string(opts.component.with.as_deref())?;
let options = reconcile_fetch_options(&opts.component.path, &settings, opts.oci, None);
let config = fetch_wick_tree(&opts.component.path, options.clone(), runtime_config, span.clone()).await?;
let flattened = config.flatten();
let report = Audit::new_flattened(&flattened);

if opts.lockdown {
let config = WickConfiguration::Lockdown(report.into());
let config_json = serde_json::to_value(&config)?;
let config_yaml = config.into_v1_yaml()?;

Ok(StructuredOutput::new(config_yaml, config_json))
} else {
let mut buffer = String::new();
gen_report(&report, &mut buffer);
let filtered = report.iter().filter(|a| !a.resources.is_empty()).collect::<Vec<_>>();
let json = json!({"audit":filtered});

Ok(StructuredOutput::new(buffer, json))
}
}

fn gen_report(audit: &[Audit], buffer: &mut String) {
for audit in audit {
if audit.resources.is_empty() {
continue;
}
let resource_lines = audit
.resources
.iter()
.map(|r| format!(" {}", r))
.collect::<Vec<_>>()
.join("\n");
let message = format!(
"
name: {}
resources:
{}",
audit.name, resource_lines
);
buffer.push_str(&message);
}
}
28 changes: 12 additions & 16 deletions crates/bins/wick/src/commands/config/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,21 @@ use structured_output::StructuredOutput;
use wick_config::WickConfiguration;
use wick_host::ComponentHostBuilder;

use crate::options::get_auth_for_scope;
use crate::utils::{merge_config, parse_config_string};
use crate::utils::{get_auth_for_scope, merge_config, parse_config_string};

#[derive(Debug, Clone, Args)]
#[clap(rename_all = "kebab-case")]
#[group(skip)]
pub(crate) struct Options {
/// Path to composite component to load.
#[clap(action)]
pub(crate) path: String,
#[clap(flatten)]
pub(crate) oci: crate::options::oci::OciOptions,

#[clap(flatten)]
pub(crate) component: crate::options::component::ComponentOptions,
/// Operation to render.
#[clap(action)]
pub(crate) operation: String,

#[clap(flatten)]
pub(crate) oci: crate::oci::Options,

/// Pass configuration necessary to instantiate the component (JSON).
#[clap(long = "with", short = 'w', action)]
with: Option<String>,

/// Pass configuration necessary to invoke the operation (JSON).
#[clap(long = "op-with", action)]
op_with: Option<String>,
Expand All @@ -41,7 +34,10 @@ pub(crate) async fn handle(
span: tracing::Span,
) -> Result<StructuredOutput> {
span.in_scope(|| debug!("Generate dotviz graph"));
let configured_creds = settings.credentials.iter().find(|c| opts.path.starts_with(&c.scope));
let configured_creds = settings
.credentials
.iter()
.find(|c| opts.component.path.starts_with(&c.scope));

let (username, password) = get_auth_for_scope(
configured_creds,
Expand All @@ -53,7 +49,7 @@ pub(crate) async fn handle(
let mut fetch_opts: wick_oci_utils::OciOptions = opts.oci.clone().into();
fetch_opts.set_username(username).set_password(password);

let path = PathBuf::from(&opts.path);
let path = PathBuf::from(&opts.component.path);

if !path.exists() {
fetch_opts.set_cache_dir(env.global().cache().clone());
Expand All @@ -63,9 +59,9 @@ pub(crate) async fn handle(
fetch_opts.set_cache_dir(path_dir.join(env.local().cache()));
};

let root_config = parse_config_string(opts.with.as_deref())?;
let root_config = parse_config_string(opts.component.with.as_deref())?;

let mut config = WickConfiguration::fetch(&opts.path, fetch_opts).await?;
let mut config = WickConfiguration::fetch(&opts.component.path, fetch_opts).await?;
config.set_root_config(root_config);
let manifest = config.finish()?.try_component_config()?;

Expand Down
Loading
Loading