Skip to content

Commit

Permalink
mttx pull
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Mar 21, 2024
1 parent f8c100e commit 76a2388
Show file tree
Hide file tree
Showing 28 changed files with 634 additions and 713 deletions.
4 changes: 4 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[alias]
mattrax = "run -p mattrax --"
mttx = "run -p mttx -- --server http://localhost:3000 "
mattraxd = "run -p mattraxd --"
98 changes: 92 additions & 6 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions apps/mttx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ publish = false

[dependencies]
mattrax-utils = { path = "../../crates/utils" }
mattrax-policy = { path = "../../crates/policy" }

clap = { version = "4.5.0", features = ["derive"] }
tracing-subscriber = "0.3.18"
tracing = "0.1.40"

graph-rs-sdk = "2.0.0-beta.0"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
serde_yaml = "0.9.33"
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] }
graph-rs-sdk = "2.0.0-beta.0"
reqwest = { version = "0.12.0", features = ["json"] }
urlencoding = "2.1.3"
12 changes: 10 additions & 2 deletions apps/mttx/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::PathBuf;

use clap::{Parser, Subcommand};

mod pull;
mod push;
mod validate;

Expand All @@ -16,8 +17,14 @@ pub struct Cli {
#[arg(short = 'v', short_alias = 'V', long, action = clap::builder::ArgAction::Version)]
version: (),

#[arg(short, long)]
pub config: PathBuf,
// The backend API server
#[arg(
short,
long,
help = "The Mattrax API server to connect to",
default_value = "https://cloud.mattrax.app"
)]
pub server: String,

#[command(subcommand)]
pub command: Commands,
Expand All @@ -27,4 +34,5 @@ pub struct Cli {
pub enum Commands {
Validate(validate::Command),
Push(push::Command),
Pull(pull::Command),
}
85 changes: 85 additions & 0 deletions apps/mttx/src/cli/pull.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::{fs, path::PathBuf};

use mattrax_policy::Policy;
use reqwest::{Client, Url};
use tracing::{error, info};

#[derive(clap::Args)]
#[command(about = "Pull a policy from Mattrax to a local file")]
pub struct Command {
#[arg(help = "The ID of the policy to pull")]
policy_id: String,

#[arg(help = "The file to write the policy to")]
path: PathBuf,

#[arg(long, short, action, help = "Overwrite the file if it already exists")]
force: bool,
}

impl Command {
pub async fn run(&self, base_url: Url, client: Client) {
if !self.force && self.path.exists() {
error!("File already exists at {:?}", self.path);
return;
}

let Ok(url) = base_url
.join(&format!(
"/api/cli/policy/{}",
urlencoding::encode(&self.policy_id)
))
.map_err(|err| error!("Error constructing url to Mattrax API: {err}"))
else {
return;
};

let Ok(response) = client
.get(url)
.send()
.await
.map_err(|err| error!("Error doing HTTP request to Mattrax API: {err}"))
else {
return;
};
if !response.status().is_success() {
error!(
"Error fetching policy from Mattrax: {:?}",
response.status()
);
return;
}

// TODO: use a proper struct for the return type
let Ok(body) = response
.json::<serde_json::Value>()
.await
.map_err(|err| error!("Error decoding response from Mattrax API: {err}"))
else {
return;
};

let policy = Policy {
name: body
.as_object()
.unwrap()
.get("name")
.unwrap()
.as_str()
.unwrap()
.to_string(),
};

let Ok(yaml) = serde_yaml::to_string(&policy)
.map_err(|err| error!("Error serializing policy to YAML: {err}"))
else {
return;
};

fs::write(&self.path, yaml)
.map_err(|err| error!("Error writing policy to file: {err}"))
.ok();

info!("Successfully pulled '{}' from Mattrax!", policy.name);
}
}
3 changes: 2 additions & 1 deletion apps/mttx/src/cli/push.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use reqwest::{Client, Url};
use tracing::info;

#[derive(clap::Args)]
#[command(about = "Push a policy file to Mattrax")]
pub struct Command {}

impl Command {
pub fn run(&self) {
pub async fn run(&self, base_url: Url, client: Client) {
info!("Hello World");

// TODO: Push the policy up into Mattrax.
Expand Down
27 changes: 23 additions & 4 deletions apps/mttx/src/cli/validate.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
use tracing::info;
use std::path::PathBuf;

use tracing::{error, info};

#[derive(clap::Args)]
#[command(about = "Validate a policy file is valid offline")]
pub struct Command {}

impl Command {
pub fn run(&self) {
info!("Hello World");
pub fn run(&self, config_path: PathBuf) {
if !config_path.exists() {
error!("Config file was not found at {config_path:?}");
return;
}

let Ok(config_raw) = std::fs::read_to_string(&config_path)
.map_err(|err| error!("Failed to read config file: {err}"))
else {
return;
};

let Ok(file) = serde_yaml::from_str::<serde_yaml::Value>(&config_raw)
.map_err(|err| error!("Failed to parse config file: {err}"))
else {
return;
};

info!("{file:#?}");

// TODO: Validate the policy file against the schema.
// TODO: Validate the policy file against the schema (can Serde do this for us?)
}
}
Loading

0 comments on commit 76a2388

Please sign in to comment.