Skip to content

Commit

Permalink
feat: dummy sha256 plugin using hipcheck-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickjcasey authored and j-lanson committed Sep 24, 2024
1 parent 97727ba commit a3f9994
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 8 deletions.
10 changes: 10 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 @@ -18,6 +18,7 @@ members = [
"plugins/dummy_rand_data_sdk",
"plugins/dummy_rand_data",
"plugins/dummy_sha256",
"plugins/dummy_sha256_sdk",
"sdk/rust",
]

Expand Down
18 changes: 10 additions & 8 deletions hipcheck/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,15 +548,17 @@ fn cmd_plugin(args: PluginArgs) {

let tgt_dir = "./target/debug";

let entrypoint1 = match args.sdk {
true => {
pathbuf![tgt_dir, "dummy_rand_data_sdk"]
}
false => {
pathbuf![tgt_dir, "dummy_rand_data"]
}
let (entrypoint1, entrypoint2) = match args.sdk {
true => (
pathbuf![tgt_dir, "dummy_rand_data_sdk"],
pathbuf![tgt_dir, "dummy_sha256_sdk"],
),
false => (
pathbuf![tgt_dir, "dummy_rand_data"],
pathbuf![tgt_dir, "dummy_sha256"],
),
};
let entrypoint2 = pathbuf![tgt_dir, "dummy_sha256"];

let plugin1 = Plugin {
name: "dummy/rand_data".to_owned(),
entrypoint: entrypoint1.display().to_string(),
Expand Down
12 changes: 12 additions & 0 deletions plugins/dummy_sha256_sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "dummy_sha256_sdk"
version = "0.1.0"
license = "Apache-2.0"
edition = "2021"
publish = false

[dependencies]
clap = { version = "4.5.18", features = ["derive"] }
hipcheck-sdk = { path = "../../sdk/rust" }
sha2 = "0.10.8"
tokio = { version = "1.40.0", features = ["rt"] }
3 changes: 3 additions & 0 deletions plugins/dummy_sha256_sdk/schema/query_schema_sha256.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "integer"
}
89 changes: 89 additions & 0 deletions plugins/dummy_sha256_sdk/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// SPDX-License-Identifier: Apache-2.0

use clap::Parser;
use hipcheck_sdk::prelude::*;
use sha2::{Digest, Sha256};

static SHA256_KEY_SCHEMA: &str = include_str!("../schema/query_schema_sha256.json");
static SHA256_OUTPUT_SCHEMA: &str = include_str!("../schema/query_schema_sha256.json");

/// calculate sha256 of provided content
fn sha256(content: &[u8]) -> Vec<u8> {
let mut hasher = Sha256::new();
hasher.update(content);
hasher.finalize().to_vec()
}

/// This plugin takes in a Value::Array(Vec<Value::Number>) and calculates its sha256
#[derive(Clone, Debug)]
struct Sha256Plugin;

#[async_trait]
impl Query for Sha256Plugin {
fn input_schema(&self) -> JsonSchema {
from_str(SHA256_KEY_SCHEMA).unwrap()
}

fn output_schema(&self) -> JsonSchema {
from_str(SHA256_OUTPUT_SCHEMA).unwrap()
}

async fn run(
&self,
_engine: &mut PluginEngine,
input: Value,
) -> hipcheck_sdk::error::Result<Value> {
let Value::Array(data) = &input else {
return Err(Error::UnexpectedPluginQueryInputFormat);
};

let data = data
.iter()
.map(|elem| elem.as_u64().map(|num| num as u8))
.collect::<Option<Vec<_>>>()
.ok_or_else(|| Error::UnexpectedPluginQueryInputFormat)?;

let hash = sha256(&data);
// convert to Value
let hash = hash.iter().map(|x| Value::Number((*x).into())).collect();
Ok(hash)
}
}

impl Plugin for Sha256Plugin {
const PUBLISHER: &'static str = "dummy";

const NAME: &'static str = "sha256";

fn set_config(&self, _config: Value) -> std::result::Result<(), ConfigError> {
Ok(())
}

fn default_policy_expr(&self) -> hipcheck_sdk::prelude::Result<String> {
Ok("".to_owned())
}

fn explain_default_query(&self) -> hipcheck_sdk::prelude::Result<Option<String>> {
Ok(Some("calculate sha256 of provided array".to_owned()))
}

fn queries(&self) -> impl Iterator<Item = NamedQuery> {
vec![NamedQuery {
name: "sha256",
inner: Box::new(Sha256Plugin),
}]
.into_iter()
}
}

#[derive(Parser, Debug)]
struct Args {
#[arg(long)]
port: u16,
}

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
let args = Args::try_parse().unwrap();
PluginServer::register(Sha256Plugin).listen(args.port).await
}

0 comments on commit a3f9994

Please sign in to comment.