-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8c100e
commit 76a2388
Showing
28 changed files
with
634 additions
and
713 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
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 --" |
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
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); | ||
} | ||
} |
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 |
---|---|---|
@@ -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?) | ||
} | ||
} |
Oops, something went wrong.