Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: VFS permissions handling #312

Merged
merged 19 commits into from
Jul 31, 2024
Merged
1 change: 1 addition & 0 deletions .config/dictionaries/project.dic
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CEST
CHAINCODE
chaincode
chainsync
childs
chrono
ciphertext
codegen
Expand Down
11 changes: 5 additions & 6 deletions hermes/bin/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Hermes app implementation.

use std::collections::HashMap;
use std::{collections::HashMap, sync::Arc};

use crate::{
vfs::Vfs,
Expand Down Expand Up @@ -29,8 +29,7 @@ pub(crate) struct HermesApp {
indexed_modules: HashMap<ModuleId, Module>,

/// App `Vfs` instance
#[allow(dead_code)]
vfs: Vfs,
vfs: Arc<Vfs>,
}

impl HermesApp {
Expand All @@ -43,7 +42,7 @@ impl HermesApp {
Self {
app_name,
indexed_modules,
vfs,
vfs: Arc::new(vfs),
}
}

Expand All @@ -53,8 +52,8 @@ impl HermesApp {
}

/// Get vfs
pub(crate) fn vfs(&self) -> &Vfs {
&self.vfs
pub(crate) fn vfs(&self) -> Arc<Vfs> {
self.vfs.clone()
}

/// Get indexed modules
Expand Down
2 changes: 1 addition & 1 deletion hermes/bin/src/cli/app/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use chrono::Utc;
use clap::Args;
use console::Emoji;

use crate::packaging::app::{manifest::Manifest, ApplicationPackage};
use crate::packaging::app::{ApplicationPackage, Manifest};

/// Hermes application packaging
#[derive(Args)]
Expand Down
54 changes: 5 additions & 49 deletions hermes/bin/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ use console::Emoji;
use crate::{
app::{HermesApp, HermesAppName},
cli::Cli,
hdf5::Path,
packaging::{
app::ApplicationPackage,
sign::certificate::{self, Certificate},
},
reactor::HermesReactor,
vfs::{Vfs, VfsBootstrapper},
vfs::VfsBootstrapper,
};

/// Run cli command
Expand Down Expand Up @@ -46,7 +45,10 @@ impl Run {
let app_name = package.get_metadata()?.get_name()?;

println!("{} Bootstrapping virtual filesystem", Emoji::new("🗄️", ""));
let vfs = bootstrap_vfs(app_name.clone(), &package)?;
let hermes_home_dir = Cli::hermes_home()?;
let mut bootstrapper = VfsBootstrapper::new(hermes_home_dir, app_name.clone());
package.mount_to_vfs(&mut bootstrapper)?;
let vfs = bootstrapper.bootstrap()?;

println!("{} Running application {app_name}\n", Emoji::new("🚀", ""),);
let mut modules = Vec::new();
Expand All @@ -62,49 +64,3 @@ impl Run {
Ok(())
}
}

/// Bootstrap Hermes virtual filesystem
fn bootstrap_vfs(app_name: String, package: &ApplicationPackage) -> anyhow::Result<Vfs> {
let hermes_home_dir = Cli::hermes_home()?;
let mut bootstrapper = VfsBootstrapper::new(hermes_home_dir, app_name);

let root_path = Path::default();
bootstrapper.with_mounted_file(root_path.clone(), package.get_icon_file()?);
bootstrapper.with_mounted_file(root_path.clone(), package.get_metadata_file()?);
if let Some(share_dir) = package.get_share_dir() {
bootstrapper.with_mounted_dir(root_path.clone(), share_dir);
}
if let Some(www_dir) = package.get_www_dir() {
bootstrapper.with_mounted_dir(root_path.clone(), www_dir);
}

for module_info in package.get_modules()? {
let lib_module_dir_path: Path =
format!("{}/{}", Vfs::LIB_DIR, module_info.get_name()).into();
bootstrapper.with_dir_to_create(lib_module_dir_path.clone());

bootstrapper.with_mounted_file(
lib_module_dir_path.clone(),
module_info.get_metadata_file()?,
);
bootstrapper.with_mounted_file(
lib_module_dir_path.clone(),
module_info.get_component_file()?,
);
if let Some(config_schema) = module_info.get_config_schema_file() {
bootstrapper.with_mounted_file(lib_module_dir_path.clone(), config_schema);
}
if let Some(config) = module_info.get_config_file() {
bootstrapper.with_mounted_file(lib_module_dir_path.clone(), config);
}
if let Some(settings_schema) = module_info.get_settings_schema_file() {
bootstrapper.with_mounted_file(lib_module_dir_path.clone(), settings_schema);
}
if let Some(share_dir) = module_info.get_share() {
bootstrapper.with_mounted_dir(lib_module_dir_path.clone(), share_dir);
}
}

let vfs = bootstrapper.bootstrap()?;
Ok(vfs)
}
4 changes: 2 additions & 2 deletions hermes/bin/src/event/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub(crate) struct NotInitializedError;
pub(crate) struct EventLoopPanicsError;

/// Hermes event execution context
type ExecutionContext<'a> = (&'a HermesAppName, &'a ModuleId, &'a Module, &'a Vfs);
type ExecutionContext<'a> = (&'a HermesAppName, &'a ModuleId, &'a Module, Arc<Vfs>);

/// Hermes event queue.
/// It is a singleton struct.
Expand Down Expand Up @@ -158,7 +158,7 @@ pub(crate) fn send(event: HermesEvent) -> anyhow::Result<()> {
/// Execute a hermes event on the provided module and all necessary info.
pub(crate) fn event_dispatch(
app_name: HermesAppName, module_id: ModuleId, module: &Module, event: &dyn HermesEventPayload,
vfs: Vfs,
vfs: Arc<Vfs>,
) {
let runtime_context = HermesRuntimeContext::new(
app_name,
Expand Down
44 changes: 33 additions & 11 deletions hermes/bin/src/hdf5/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@

use std::fmt::Display;

use crate::utils::parse_path;

/// Package path.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Path(Vec<String>);

impl Path {
/// Create new `PackagePath` from path components.
#[allow(dead_code)]
pub(crate) fn new(path_components: Vec<String>) -> Self {
Self(path_components)
pub(crate) fn new(path_elements: Vec<String>) -> Self {
Self(path_elements)
}

/// Create new `PackagePath` from str.
pub(crate) fn from_str(path: &str) -> Self {
let path_components = path
.split('/')
.map(ToString::to_string)
.filter(|s| !s.is_empty())
.collect();
Self(path_components)
Self(parse_path(path))
}

/// Returns an iterator over the path elements.
Expand Down Expand Up @@ -74,6 +70,7 @@ mod tests {

#[test]
fn package_path_test() {
// with '/' delimiter
{
let mut path = Path::from_str("/a/b/c");
assert_eq!(path.pop_elem(), "c".to_string());
Expand All @@ -94,12 +91,37 @@ mod tests {
assert_eq!(path.pop_elem(), String::new());
}
{
let mut path = Path::from_str("a");
let mut path = Path::from_str("/");
assert_eq!(path.pop_elem(), String::new());
}
// with '\' delimiter
{
let mut path = Path::from_str(r"\a\b\c");
assert_eq!(path.pop_elem(), "c".to_string());
assert_eq!(path.pop_elem(), "b".to_string());
assert_eq!(path.pop_elem(), "a".to_string());
assert_eq!(path.pop_elem(), String::new());
}
{
let mut path = Path::from_str("/");
let mut path = Path::from_str(r"a\b\c");
assert_eq!(path.pop_elem(), "c".to_string());
assert_eq!(path.pop_elem(), "b".to_string());
assert_eq!(path.pop_elem(), "a".to_string());
assert_eq!(path.pop_elem(), String::new());
}
{
let mut path = Path::from_str(r"\a");
assert_eq!(path.pop_elem(), "a".to_string());
assert_eq!(path.pop_elem(), String::new());
}
{
let mut path = Path::from_str(r"\");
assert_eq!(path.pop_elem(), String::new());
}

{
let mut path = Path::from_str("a");
assert_eq!(path.pop_elem(), "a".to_string());
assert_eq!(path.pop_elem(), String::new());
}
{
Expand Down
1 change: 1 addition & 0 deletions hermes/bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod packaging;
pub mod reactor;
pub mod runtime_context;
pub mod runtime_extensions;
pub mod utils;
pub mod vfs;
pub mod wasm;

Expand Down
1 change: 1 addition & 0 deletions hermes/bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod packaging;
mod reactor;
mod runtime_context;
mod runtime_extensions;
mod utils;
mod vfs;
mod wasm;

Expand Down
2 changes: 1 addition & 1 deletion hermes/bin/src/packaging/app/author_payload.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A signature payload object for author.cose Hermes application package.
//! Defined at `https://input-output-hk.github.io/hermes/architecture/08_concepts/hermes_packaging_requirements/app_signatures/#wasm-component-module-signatures`.

use crate::packaging::{
use super::super::{
hash::Blake2b256, schema_validation::SchemaValidator, sign::signature::SignaturePayloadEncoding,
};

Expand Down
6 changes: 2 additions & 4 deletions hermes/bin/src/packaging/app/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

use std::path::Path;

use crate::{
hdf5::resources::{FsResource, ResourceBuilder},
packaging::{schema_validation::SchemaValidator, FileError},
};
use super::super::{schema_validation::SchemaValidator, FileError};
use crate::hdf5::resources::{FsResource, ResourceBuilder};

/// Hermes application package manifest.json definition.
#[derive(Debug, PartialEq, Eq)]
Expand Down
Loading
Loading