Skip to content
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ target
# in Rust projects, lock files
Cargo.lock

testing/data/*
testing/data/*

.cargo/*
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ resolver = "2"
anyhow = "1.0.86"
serde = { version = "1.0.214", features = ["derive"] }
serde_json = {version = "1.0.125", features = ["unbounded_depth"]}
rust_am_lib = { git = "https://github.com/ku-sldg/rust-am-lib.git", version = "0.3.0"}
#rust_am_lib = { git = "file://<local_dir>", version = "", branch=""}
rust_am_lib = { git = "https://github.com/ku-sldg/rust-am-lib.git", version = "0.4.0"}
15 changes: 11 additions & 4 deletions executables/goldenevidence_appr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ struct ASP_ARGS_GoldenEvidence_Appr {
env_var_golden: String,
filepath_golden: String,
asp_id_appr: String,
targ_id_appr: String
env_var: String,
filepath: String
//targ_id_appr: String
}

fn deserialize_deep_json(json_data: &str) -> serde_json::Result<Value> {
Expand All @@ -41,13 +43,16 @@ fn body(ev: copland::ASP_RawEv, args: copland::ASP_ARGS) -> Result<Result<()>> {
.context("Could not parse ASP_ARGS for ASP goldenevidence_appr")?;

// Code for specific for this ASP.
let env_var: String = myaspargs.env_var_golden;
let filename: String = myaspargs.filepath_golden;
let env_var: String = myaspargs.env_var_golden.clone();
let filename: String = myaspargs.filepath_golden.clone();

let env_var_string = rust_am_lib::copland::get_env_var_val(env_var)?;

let filename_full = format! {"{env_var_string}{filename}"};

debug_print!{"\n\nReading golden evidence file:\n"};
debug_print!{"{filename_full}"};

let contents = fs::read_to_string(filename_full).expect("Couldn't read (Evidence, GlobalContext) JSON file in goldenevidence_appr");
debug_print!{"\n\nAttempting to decode (Evidence, GlobalContext)...\n\n"};
let my_contents_val = deserialize_deep_json(&contents)?;
Expand All @@ -58,7 +63,9 @@ fn body(ev: copland::ASP_RawEv, args: copland::ASP_ARGS) -> Result<Result<()>> {
let my_evidence: copland::Evidence = my_contents.0;
let my_glob_ctxt: copland::GlobalContext = my_contents.1;

let my_asp_params: copland::ASP_PARAMS = copland::ASP_PARAMS{ ASP_ID: myaspargs.asp_id_appr, ASP_ARGS: serde_json::Value::Null, ASP_PLC: "".to_string(), ASP_TARG_ID: myaspargs.targ_id_appr};
let asp_args_value = serde_json::to_value(&myaspargs)?;

let my_asp_params: copland::ASP_PARAMS = copland::ASP_PARAMS{ ASP_ID: myaspargs.asp_id_appr, ASP_ARGS: asp_args_value.clone() /* serde_json::Value::Null */ /*, ASP_PLC: "".to_string(), ASP_TARG_ID: myaspargs.targ_id_appr */};

let my_et = copland::get_et(my_evidence.clone());
let my_rawev= copland::get_rawev(my_evidence);
Expand Down
13 changes: 13 additions & 0 deletions executables/hamr_readfile_range_many/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "hamr_readfile_range_many"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
rust_am_lib = { workspace = true }
serde_stacker = "0.1"
flate2 = "1.0"
hamrLib = { path = "../../external_deps/hamrLib/hamrLib" }
152 changes: 152 additions & 0 deletions executables/hamr_readfile_range_many/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

// Common Packages
use anyhow::{Context, Result};

use std::fs::File;
use std::io::{self, BufRead, Write};
use std::path::{Path};
use std::collections::HashMap;

use flate2::write::GzEncoder;
use flate2::Compression;

use serde::{Deserialize, Serialize};

use rust_am_lib::{
copland::{self, handle_body},
debug_print,
};

use hamrLib::*;


// This ASP ("hamr_readfile_range_many") is a measurement ASP that parses a HAMR Attestation Report and reads the contents of the specified lines of text from a collection of files.
//
// INPUT:
// The ASP expects a JSON object with an "ASP_ARGS" field containing the following arguments:
// - "attestation_report_filepath": A filepath(String) pointing to a HAMR Attestation Report JSON object

//
// OUTPUT:
// The ASP returns a raw evidence package (`RawEv`) containing a vector of length 1 with the only member being a byte array (Vec<u8>),
// containing the encoded contents of the Slices_Map structure defined in the hamrLib external dependency library(pub type Slices_Map = HashMap<String, Vec<u8>>;).
// The keys in the HashMap structure are of the form: `<filepath>::<start_index>-<end_index>`, and
// the values are byte arrays (encoded Vec<u8>s) of the file contents at those line ranges. For simplicity, we chose not to preserve line boundaries
// of the contents because that would make the output evidence structure depend on the input file range.

// NOTE: Additionally, we choose to gzip compress the Slices_Map structure to trim down the output evidence size.
// Any dual appraisal ASP will first need to decompress the raw data before decoding to a Slices_Map and proceeding with appraisal.

// ASP Arguments (JSON-decoded)
#[derive(Serialize, Deserialize, Debug, Clone)]
struct ASP_ARGS_HAMR_ReadfileRangeMany {
report_filepath: String
}

fn compress_string(s: &str) -> io::Result<Vec<u8>> {
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
encoder.write_all(s.as_bytes())?;
encoder.finish()
}

fn get_bytevec_fileslice (
s: File_Slice ) -> io::Result<Vec<u8>> {

let lines = read_line_range(s.filepath, s.start_index, s.end_index)?;
let res: Vec<u8> = lines.into_iter()
.flat_map(|s| s.into_bytes())
.collect();
Ok(res)
}

fn read_line_range<P: AsRef<Path>>(
path: P,
start_line: usize,
end_line: usize
) -> io::Result<Vec<String>> {
let file = File::open(path)?;
let reader = io::BufReader::new(file);

let mut lines_in_range = Vec::new();

// Line numbers are typically 1-based, so we adjust for 0-based indexing
let start_index = start_line.saturating_sub(1);
// end_line is inclusive in this implementation

for (index, line_result) in reader.lines().enumerate() {
if index >= start_index && index < end_line {
lines_in_range.push(line_result?);
} else if index >= end_line {
// Stop reading once the end of the range is passed
break;
}
}

Ok(lines_in_range)
}

// function where the work of the ASP is performed.
// May signal an error which will be handled in main.
fn body(_ev: copland::ASP_RawEv, args: copland::ASP_ARGS) -> Result<copland::ASP_RawEv> {
debug_print!("Starting readfile_range_many ASP execution\n");

let myaspargs: ASP_ARGS_HAMR_ReadfileRangeMany =
serde_json::from_value(args).context("Could not decode ASP_ARGS for ASP hamr_readfile_range_many")?;

let report_filepath_string = myaspargs.report_filepath;
let report_filepath = Path::new(&report_filepath_string);

let att_report: HAMR_AttestationReport = get_attestation_report_json(report_filepath)?;

let attestation_report_root = report_filepath.parent().unwrap();

let slices = HAMR_attestation_report_to_File_Slices(att_report, attestation_report_root);

let mut m : Slices_Map = HashMap::new();

for s in slices.into_iter() {

let bline = s.start_index.clone();
let eline = s.end_index.clone();
let uri = s.filepath.clone();

let bline_string= bline.to_string();
let eline_string = eline.to_string();
let uri_slice_string = format!("{uri}::{bline_string}-{eline_string}");

if ! m.contains_key(&uri_slice_string) {
let v = get_bytevec_fileslice(s)?;
m.insert(uri_slice_string, v);
}
};

let res_str = serde_json::to_string(&m)?;

let compressed_str = compress_string(&res_str)?;

let res = compressed_str;

Ok(vec![res])
}

// Main simply invokes the body() function above,
// and checks for Err Result.
// If it detects an Err Result, this ASP will return
// an ASPRunResponse with SUCCESS = false, o/w uses
// ASPRunResponse returned from body()

fn main() {
// debug print the current working directory
if let Ok(_cwd) = std::env::current_dir() {
debug_print!("Current working directory: {}\n", _cwd.display());
} else {
debug_print!("Could not get current working directory\n");
}
// debug print the program arguments on newlines
for _arg in std::env::args() {
debug_print!("arg: {}\n", _arg);
}
handle_body(body);
}
13 changes: 13 additions & 0 deletions executables/hashfile/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,25 @@ use rust_am_lib::{
// e.g.
use sha2::{Digest, Sha256};

/*
// ASP Arguments (JSON-decoded)
#[derive(Serialize, Deserialize, Debug, Clone)]
struct ASP_ARGS_Hashfile {
env_var: String,
filepath: String
}
*/
// ASP Arguments (JSON-decoded)
#[derive(Serialize, Deserialize, Debug, Clone)]
struct ASP_ARGS_Hashfile
{
env_var_golden: String,
filepath_golden: String,
asp_id_appr: String,
env_var: String,
filepath: String
//targ_id_appr: String
}

// function where the work of the ASP is performed.
// May signal an error which will be handled in main.
Expand Down
38 changes: 6 additions & 32 deletions executables/provision_goldenevidence/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,19 @@
#![allow(non_snake_case)]

// Common Packages
use std::fs;
use anyhow::{Context, Result};
use rust_am_lib::{
copland::{self, handle_body, vec_to_rawev},
copland::{self, GlobalContext, EvidenceT, handle_body, vec_to_rawev},
debug_print,
};
use serde::{Deserialize, Serialize};
use serde_json::{Value, from_value};
use serde_stacker::Deserializer;

#[derive(Serialize, Deserialize, Debug, Clone)]
struct ASP_ARGS_Provision_GoldenEvidence {
env_var_golden: String,
filepath_golden: String,
et_context: String,
et_golden: String
}

fn deserialize_deep_json(json_data: &str) -> serde_json::Result<Value> {
let mut de = serde_json::de::Deserializer::from_str(json_data);
de.disable_recursion_limit(); // This method is only available with the feature

// Wrap with serde_stacker's Deserializer to use a dynamically growing stack
let stacker_de = Deserializer::new(&mut de);

// Deserialize the data
let value = Value::deserialize(stacker_de)?;

Ok(value)
et_context: GlobalContext,
et_golden: EvidenceT
}

// function where the work of the ASP is performed.
Expand All @@ -51,18 +35,8 @@ fn body(ev: copland::ASP_RawEv, args: copland::ASP_ARGS) -> Result<copland::ASP_

let my_rawev = vec_to_rawev(vecvec);

let fp = myaspargs.et_golden;
debug_print!{"\n\nAttempting to read (EvidenceT, GlobalContext) JSON structure from file: {}\n\n", fp};

let contents = fs::read_to_string(fp).expect("Couldn't read (EvidenceT, GlobalContext) JSON file in provision_goldenevidence ASP at fp: {}");
debug_print!{"\n\nAttempting to decode (EvidenceT, GlobalContext)...\n\n"};
let my_contents_val = deserialize_deep_json(&contents)?;
let my_contents: (copland::EvidenceT, copland::GlobalContext) = from_value(my_contents_val)?;
debug_print!("\nDecoded (EvidenceT, GlobalContext) as:");
debug_print!("{:?}", my_contents);

let my_evidenceT: copland::EvidenceT = my_contents.0;
let my_glob_ctxt: copland::GlobalContext = my_contents.1;
let my_evidenceT: copland::EvidenceT = myaspargs.et_golden;
let my_glob_ctxt: copland::GlobalContext = myaspargs.et_context;


let my_evidence: copland::Evidence = (my_rawev,my_evidenceT);
Expand All @@ -73,7 +47,7 @@ fn body(ev: copland::ASP_RawEv, args: copland::ASP_ARGS) -> Result<copland::ASP_

debug_print!("Attempting to write golden evidence to filename: {filename_full}");
std::fs::write(filename_full, my_json_string)?;
Ok(vec![])
Ok(vec![Vec::new()])
}

fn main() {
Expand Down
1 change: 1 addition & 0 deletions executables/readfile_range_many_appr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ serde = {workspace = true}
serde_json = {workspace = true}
serde_stacker = "0.1"
flate2 = "1.0"
hamrLib = { path = "../../external_deps/hamrLib/hamrLib" }
Loading