-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dummy sha256 plugin using hipcheck-sdk
- Loading branch information
1 parent
97727ba
commit a3f9994
Showing
6 changed files
with
125 additions
and
8 deletions.
There are no files selected for viewing
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,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"] } |
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,3 @@ | ||
{ | ||
"type": "integer" | ||
} |
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,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 | ||
} |